使用`Parsing` applicative的ApplicativeDo语言扩展仍然在寻找Monad实例

时间:2017-02-16 21:35:44

标签: haskell ghc applicative

我正在尝试使用parsers package使用do语法编写解析器。这是一个例子:

{-# LANGUAGE ApplicativeDo #-}
import Text.Parser.Char (string, spaces)
import Text.Parser.Token (TokenParsing, natural)

issueParser :: TokenParsing p => p Integer
issueParser = do
  spaces
  string "**Issue:**"
  spaces
  string "https://github.com" <|> string "github.com"
  string "/commercialhaskell/stack/issues/"
  natural

GHC给出的错误是Could not deduce (Monad p) arising from a do statement from the context: TokenParsing p。此错误消息是正确的TokenParsing未提供Monad作为超类,但它确实提供Applicative,这意味着因为我已启用此语言扩展,我应该可以使用只有do的{​​{1}}语法。我在做错了什么/在这里失踪了?

1 个答案:

答案 0 :(得分:14)

想出来。要使此示例在ghc 8.0.2上运行,您需要添加下划线生成器,如下所示:

{-# LANGUAGE ApplicativeDo #-}
import Text.Parser.Char (string, spaces)
import Text.Parser.Token (TokenParsing, natural)

issueParser :: TokenParsing p => p Integer
issueParser = do
  _ <- spaces
  _ <- string "**Issue:**"
  _ <- spaces
  _ <- string "https://github.com" <|> string "github.com"
  _ <- string "/commercialhaskell/stack/issues/"
  n <- natural
  pure n

这里已经存在一个ghc错误:https://ghc.haskell.org/trac/ghc/ticket/12666