0

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.

Shane
  • 2,315
  • 3
  • 21
  • 33
  • 1
    Depending on your use case, it might be necessary to check whether the parsed `Integer` is in the `Int` range, and if not let the parse fail. – Daniel Fischer Nov 25 '12 at 19:03

1 Answers1

3

This should work.

aTerm =  parens aExpression
     <|> liftM GetV identifier
     <|> liftM (N . fromInteger) integer
dave4420
  • 46,404
  • 6
  • 118
  • 152