Search code examples
haskellparsec

Type errors with Parsec


I'm using Parsec 3.1.2 with GHC 7.4.1 to try to write a parser for a somewhat hairy data file format. I've what I'd think is a pretty trivial case, but I'm getting a type error. I'm trying to follow the applicative functor examples from Real World Haskell.

import Text.ParserCombinators.Parsec hiding (many, optional, (<|>))
import Text.ParserCombinators.Parsec.Char
import Text.Parsec.String
import Control.Applicative
p_int = many char ' ' *> many1 digit <* many char ' '

Now, originally I got the following type error:

Couldn't match expected type `[Char]'
            with actual type `Text.Parsec.Prim.ParsecT s0 u0 m0 [a0]'
In the return type of a call of `many1'
In the second argument of `(*>)', namely `many1 digit'
In the first argument of `(<*)', namely
  `many char ' ' *> many1 digit'

Based on Trivial parsec example produces a type error I tried adding the NoMonomorphismRestriction language pragma, but this hasn't helped.

I confess, I've found the learning curve to Parsec quite steep, even though I've got a bit of Haskell experience. It doesn't help that the Real World Haskell book's examples are based on Parsec 2.


Solution

  • You are writing this code:

    many char ' '
    

    This will pass 2 arguments to the many function: char and ' '. What you want to do is to pass the result of char ' ' to the many function, which is done like this:

    many (char ' ')