[Haskell-beginners] Problem with GADTs and Parsec

Lorenzo Tabacchini lortabac at gmx.com
Sat Feb 8 19:10:18 UTC 2014


Hello,
I am developing a very simple programming language for didactic purposes.
Expressions can be composed of different types, like Int, Bool, String etc...
The AST is compiled to JavaScript and the result is shown in the browser.
The code is more or less like this:

{-# LANGUAGE GADTs #-}
data Expr a where
 Int :: Int -> Expr Int
 Bool :: Bool -> Expr Bool
 Text :: String -> Expr String
 -- Int operations
 Add :: Expr Int -> Expr Int -> Expr Int
 -- Bool operations
 And :: Expr Bool -> Expr Bool -> Expr Bool
 -- etc. etc.

-- converts to JavaScript
compile :: Expr a -> String
compile (Int n) = show n
compile (Text t) = show t
compile (Add x y) = compile x ++ "+" ++ compile y
-- etc. etc.

The compilation works perfectly, but I am stuck with the parser.
Since I don't know the types in advance, I would like to parse the whole expression at once.
If I do:

exprParser :: Parsec String u (Expr a)
exprParser = parens exprParser
 <|> (reserved "true" >> return (Bool True))
 <|> (reserved "false" >> return (Bool False))
 <|> (stringLiteral >>= return . Text)

The compiler says: "Couldn't match type `Bool' with `[Char]'"

Where am I wrong?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140208/d8d7de97/attachment.html>


More information about the Beginners mailing list