write-you-a-haskell icon indicating copy to clipboard operation
write-you-a-haskell copied to clipboard

Parser/Type error for + and -

Open jonmountjoy opened this issue 8 years ago • 2 comments

I think https://github.com/sdiehl/write-you-a-haskell/pull/90 from @jeffreyrosenbluth introduced a bug. At least, I think it was that commit.

Prior to this commit (ie. using #1e609fb), you can do something like the following:

Poly> 2 + 3
5 : Int

Now, I get the following:

Poly> 2 + 3
Cannot unify types:
	Int
with
	Int -> a

Weirdly, it works for the * operator, but fails only for + and -:

I suspect there's something weird going on in the parser because:

Poly> 2 + x
"<stdin>" (line 1, column 5):
unexpected "x"
expecting digit
Poly> 2 * x
4 : Int

jonmountjoy avatar Jun 18 '17 17:06 jonmountjoy

In fact, the error is on this line. If you revert the way terms are parsed, all will be good. ie. Ex.buildExpressionParser table aexp. I don't quite understand the new code, and why it sort of works for some operators...

jonmountjoy avatar Jun 18 '17 18:06 jonmountjoy

I think i found the problem: The "+" character is parsed as a positive sign inside Tok.integer lexer. I fixed the problem this way:

Parser.hs, line 20

integer :: Parser Integer
integer = Tok.natural lexer

Parser.hs line, 82

aexp :: Parser Expr
aexp = do
  r <- many1 $ choice [parens expr,bool,number,ifthen,fix,try letrecin, letin, lambda, variable]
  return $ foldr1 App r

expr :: Parser Expr
expr = Ex.buildExpressionParser table aexp

tly000 avatar Aug 27 '17 15:08 tly000