Negation
Ross Paterson
ross at soi.city.ac.uk
Mon Feb 8 11:59:59 EST 2010
On Mon, Feb 08, 2010 at 04:18:07PM +0000, Simon Peyton-Jones wrote:
> Which of these definitions are correct Haskell?
>
> x1 = 4 + -5
> x2 = -4 + 5
> x3 = 4 - -5
> x4 = -4 - 5
> x5 = 4 * -5
> x6 = -4 * 5
>
> Ghc accepts x2, x4, x6 and rejects the others with a message like
> Foo.hs:4:7:
> Precedence parsing error
> cannot mix `+' [infixl 6] and prefix `-' [infixl 6] in the same infix expression
>
> Hugs accepts them all.
>
> I believe that the language specifies that all should be rejected. http://haskell.org/onlinereport/syntax-iso.html
I think GHC conforms to the Report; here is the relevant part of the grammar:
exp6 -> exp7
| lexp6
lexp6 -> (lexp6 | exp7) + exp7
| (lexp6 | exp7) - exp7
| - exp7
exp7 -> exp8
| lexp7
lexp7 -> (lexp7 | exp8) * exp8
But I agree they should all be legal, i.e. that unary minus should bind
more tightly than any infix operator (as in C). Note that Hugs does
not do that:
Hugs> -5 `mod` 2
-1
Hugs> (-5) `mod` 2
1
Hugs> -(5 `mod` 2)
-1
More information about the Haskell-prime
mailing list