小编kin*_*ndl的帖子

使用存在量化来键入解析器的签名

在开始时,我有一个解析器的简单类型:

data Parser a = Parser ([Token] -> Either String (a, [Token]))
Run Code Online (Sandbox Code Playgroud)


我在左侧使用Either作为错误消息,在右侧使用其余标记的解析表达式.

此函数"解包"解析器函数.

parse :: Parser a -> [Token] -> Either String (a, [Token])
parse (Parser p) = p
Run Code Online (Sandbox Code Playgroud)

我的目标是使Parser更加通用,它不仅将令牌作为输入.所以我使用了ExistentialQuantification编译指示并将其更改为:

data Parser a = forall b. ([b] -> Either String (a, [b]))
Run Code Online (Sandbox Code Playgroud)


我想知道的是:函数"解析"现在有哪些类型?


我无法弄明白,也无法推断出来.GHCi给出了这个错误:

Couldn't match type `t' with `[b] -> Either String (t1, [b])'
  `t' is a rigid type variable bound by
    the inferred type of parse :: Parser t1 -> t
    at ParserCombinator.hs:9:1
In the expression: …
Run Code Online (Sandbox Code Playgroud)

parsing haskell types existential-type

5
推荐指数
1
解决办法
135
查看次数

标签 统计

existential-type ×1

haskell ×1

parsing ×1

types ×1