[Haskell-cafe] Arithmetic expressions with GADTs: parsing

Sean Leather leather at cs.uu.nl
Wed May 2 15:30:50 CEST 2012

Hi Romildo,

On Wed, May 2, 2012 at 3:08 PM, j.romildo wrote:

> You are right in the sense that I cannot mix Expr Bool and Expr Double
> in a (O op l r) expression.
> But the parser should be able to parse any form of expressions. So I
> rewrite my program to take this into account.
> The new versions still does not compile:
> Expr.hs:27:23:
>     Couldn't match expected type `Double' with actual type `Bool'
>     Expected type: ParsecT
>                     String () Data.Functor.Identity.Identity (Expr Double)
>      Actual type: ParsecT
>                     String () Data.Functor.Identity.Identity (Expr Bool)
>    In the first argument of `(<|>)', namely `pBool'
>    In the second argument of `(<|>)', namely `pBool <|> pEqual'

You appear to still be having the same problem. Perhaps this because you
don't quite understand the type error? Let me help you dissect it. Since
this is a type error, I'm not going to assume anything about what should or
should not work. I'll only look at the types.

First, let's comment out the offending line, so that your file type-checks:

Line 27: -- pExpr     = pArit <|> pBool <|> pEqual

Next, let's look at each of the components relevant to the type error. The
first mentioned is (<|>). In GHCi, we can find out more information about

*Expr> :i (<|>)
(<|>) :: ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
  -- Defined in Text.Parsec.Prim
infixr 1 <|>

More information about the Haskell-Cafe mailing list