On 21/07/2011, at 9:08 AM, Paul Johnson wrote:
> I would have thought that the compiler, as a matter of optimisation, could
> insert a check to see if (==) is comparing an object with itself. The only
> way I can see this breaking is with perverse instances of Eq that would
> return False for "f == f".
== is a function with user-defined instances.
It would be, to put it minimally, bad manners,
but there's nothing to actually *stop* a programmer
writing
data Boojum = Plant | Snark
instance Eq Boojum where
Plant == Plant = True
_ == _ = False
f x = x == x
main = print $ f Snark
Presumably inside the body of f, x and x would be
identical pointers, but the only right answer is False,
not True.
If you think this is a bit far fetched,
consider the IEEE definition of equality for
floating-point numbers:
let x = 0.0/0.0 in x == x
The answer is False, so the optimisation breaks down even
with a system-defined type.
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe