I'm trying to write a parser using the parsers
package using do
syntax. Here is an example:
{-# 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
The error GHC gives me is Could not deduce (Monad p) arising from a do statement from the context: TokenParsing p
. This error message is correct that TokenParsing
does not provide Monad
as a super class, however it does provide Applicative
which means because I have this language extension turned on, I should be able to use the do
syntax with just Applicative
. What am I doing wrong/missing here?
Figured it out. To get this example to work on ghc 8.0.2 you would need to add underscore generators, like this:
{-# 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
There is already a ghc bug to address this here: https://ghc.haskell.org/trac/ghc/ticket/12666
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With