You write
pSign :: Num a => MyParser (a -> a)
which means that pSign
is a polymorphic value that, for any given instantiation of a
, produces a parser containing a monomorphic function. Instead, you want a monomorphic parser containing a polymorphic function, so:
pSign :: MyParser (forall a. Num a => a -> a)
You will need to make a few other changes as well to get GHC to understand how to keep things polymorphic until the last second. Here is a complete, compilable example.
{-# LANGUAGE ImpredicativeTypes, LiberalTypeSynonyms, RankNTypes, ScopedTypeVariables #-}
import Text.ParserCombinators.Parsec
type MyParser = Parser
data Expr = IntExpr Integer | DoubleExpr Double
pSign :: MyParser (forall a. Num a => a -> a) -- returns id for + or negate for -
pReal :: (forall a. Num a => a -> a) -> Either Integer Double -> MyParser Expr
foo :: MyParser (Either Integer Double)
pSign = undefined
pReal = undefined
foo = undefined
pNum :: MyParser Expr
pNum =
pSign >>= \(sign :: forall a. Num a => a -> a) ->
foo >>= \numVal ->
pReal sign numVal