Search code examples
haskellparsec

Using ParSec integer function to give a Int?


I need to get an Int type from the integer function with Parsec. My code at the moment is

aTerm =  parens aExpression
     <|> liftM GetV identifier
     <|> liftM N integer

Where the type of N is

N    :: Num a => a -> Expr a

The error I am getting is

Shane.hs:227:18:
    Couldn't match expected type `Int' with actual type `Integer'
    Expected type: Text.Parsec.Prim.ParsecT String u0 Identity Int
      Actual type: Text.Parsec.Prim.ParsecT String u0 Identity Integer
    In the second argument of `liftM', namely `integer'
    In the second argument of `(<|>)', namely `liftM N integer'
Failed, modules loaded: none.

Is it possible to extract an Int type here? Using fromInteger some way? Changing from Int to Integer isn't an option.


Solution

  • This should work.

    aTerm =  parens aExpression
         <|> liftM GetV identifier
         <|> liftM (N . fromInteger) integer