Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ApplicativeDo language extension with `Parsing` applicative still looking for Monad instance

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?

like image 915
Asa Avatar asked Feb 16 '17 21:02

Asa


1 Answers

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

like image 51
Asa Avatar answered Oct 01 '22 01:10

Asa