[Haskell-cafe] pointer equality

Richard O'Keefe ok at cs.otago.ac.nz
Thu Jul 21 05:53:28 CEST 2011


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.






More information about the Haskell-Cafe mailing list