gpt4 book ai didi

haskell - Parsec:在 2014 年获取 buildExpressionParser 示例进行类型检查

转载 作者:行者123 更新时间:2023-12-02 15:31:24 24 4
gpt4 key购买 nike

这是来自 http://hackage.haskell.org/package/parsec-3.1.7/docs/Text-Parsec-Expr.html 的示例:

expr    = buildExpressionParser table term
<?> "expression"

term = parens expr
<|> natural
<?> "simple expression"

table = [ [prefix "-" negate, prefix "+" id ]
, [postfix "++" (+1)]
, [binary "*" (*) AssocLeft, binary "/" (div) AssocLeft ]
, [binary "+" (+) AssocLeft, binary "-" (-) AssocLeft ]
]

binary name fun assoc = Infix (do{ reservedOp name; return fun }) assoc
prefix name fun = Prefix (do{ reservedOp name; return fun })
postfix name fun = Postfix (do{ reservedOp name; return fun })

似乎您需要一些导入才能开始:

import Text.Parsec
import Text.Parsec.Expr
import Text.Parsec.Token

现在它几乎无法对每一行进行类型检查。

有谁知道怎么解决吗?

更新

错误看起来像这样:

Couldn't match expected type ‘ParsecT s u m a0’
with actual type ‘String -> ParsecT s9 u9 m9 ()’
Relevant bindings include
name :: GenTokenParser s9 u9 m9 (bound at Eval2.hs:28:9)
postfix :: GenTokenParser s9 u9 m9 -> (a -> a) -> Operator s u m a
(bound at Eval2.hs:28:1)
Probable cause: ‘reservedOp’ is applied to too few arguments
In a stmt of a 'do' block: reservedOp name
In the first argument of ‘Postfix’, namely
‘(do { reservedOp name;
return fun })’

让我对 reservedOp 的类型感到好奇

λ :t reservedOp
reservedOp
:: GenTokenParser s u m
-> String -> Text.Parsec.Prim.ParsecT s u m ()

所以查看这里的文档 http://hackage.haskell.org/package/parsec-3.1.7/docs/Text-Parsec-Token.html :

reservedOp :: String -> ParsecT s u m ()

奥利?那么什么是 GenTokenParser s u m 以及如何获得它?

只是为了好玩

看看这个类型。这可能意味着什么?

λ :t TokenParser
TokenParser
:: Text.Parsec.Prim.ParsecT s u m String
-> (String -> Text.Parsec.Prim.ParsecT s u m ())
-> Text.Parsec.Prim.ParsecT s u m String
-> (String -> Text.Parsec.Prim.ParsecT s u m ())
-> Text.Parsec.Prim.ParsecT s u m Char
-> Text.Parsec.Prim.ParsecT s u m String
-> Text.Parsec.Prim.ParsecT s u m Integer
-> Text.Parsec.Prim.ParsecT s u m Integer
-> Text.Parsec.Prim.ParsecT s u m Double
-> Text.Parsec.Prim.ParsecT s u m (Either Integer Double)
-> Text.Parsec.Prim.ParsecT s u m Integer
-> Text.Parsec.Prim.ParsecT s u m Integer
-> Text.Parsec.Prim.ParsecT s u m Integer
-> (String -> Text.Parsec.Prim.ParsecT s u m String)
-> (forall a.
Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m a)
-> Text.Parsec.Prim.ParsecT s u m ()
-> (forall a.
Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m a)
-> (forall a.
Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m a)
-> (forall a.
Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m a)
-> (forall a.
Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m a)
-> (forall a.
Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m a)
-> Text.Parsec.Prim.ParsecT s u m String
-> Text.Parsec.Prim.ParsecT s u m String
-> Text.Parsec.Prim.ParsecT s u m String
-> Text.Parsec.Prim.ParsecT s u m String
-> (forall a.
Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m [a])
-> (forall a.
Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m [a])
-> (forall a.
Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m [a])
-> (forall a.
Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m [a])
-> GenTokenParser s u m

最佳答案

感谢@Christian Conkle 和@bheklilr 以及这里的http://hackage.haskell.org/package/parsec-3.1.7/docs/Text-Parsec-Token.html#v:makeTokenParser

这是 2014 版本的 buildExpressionParser 示例,它应该位于 Parsec 文档中,而不是现有的内容中。

import Text.Parsec
import Text.Parsec.Expr
import Text.Parsec.Token
import Text.Parsec.Language (javaStyle)

lexer = makeTokenParser javaStyle

expr = buildExpressionParser table term
<?> "expression"

term = parens lexer expr
<|> natural lexer
<?> "simple expression"

table = [ [prefix "-" negate, prefix "+" id ]
, [postfix "++" (+1)]
, [binary "*" (*) AssocLeft, binary "/" (div) AssocLeft ]
, [binary "+" (+) AssocLeft, binary "-" (-) AssocLeft ]
]

binary name fun assoc = Infix (do{ reservedOp lexer name; return fun }) assoc
prefix name fun = Prefix (do{ reservedOp lexer name; return fun })
postfix name fun = Postfix (do{ reservedOp lexer name; return fun })

关于haskell - Parsec:在 2014 年获取 buildExpressionParser 示例进行类型检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26537189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com