Negative literals and the meaning of case -2 of -2 -> True

Simon Marlow simonmar@microsoft.com
Fri, 17 May 2002 10:34:18 +0100


> To find out how Haskell implementations treat negated=20
> literals, I tested=20
> the following program:
>=20
> ------------------------------------------------
> main =3D print (minusTwo,trueOrFalse)
>=20
> minusTwo =3D -2::N
>=20
> trueOrFalse =3D
>     case minusTwo of
>       -2 -> True
>       _ -> False
>=20
> data N =3D Negate N | FromInteger Integer deriving (Eq,Show)
>=20
> instance Num N where
>   negate =3D Negate
>   fromInteger =3D FromInteger
> -------------------------------------------------
>=20
> The result is:
>=20
>     * ghc 5.02.2: main outputs: (FromInteger (-2),True)

GHC has two bugs in this area, one of which has been fixed recently.
The current output is (Negate (FromInteger 2),False) (i.e. the same as
hbc).  We were being a little too eager to replace 'negate (fromInteger
N)' by 'fromInteger (-N)'.  There is also a bug in the pattern handling,
however.

Thanks for a nice test case...

Cheers,
	Simon