[Haskell-cafe] Does GHC compare pointers when eval'ing (==)

Richard A. O'Keefe ok at cs.otago.ac.nz
Thu Aug 21 00:55:10 UTC 2014


On 21/08/2014, at 3:48 AM, Edward Kmett wrote:

> I wanted this too when I first got into Haskell, but ultimately it comes down to not being semantically correct here.
> 
> NaN /= NaN, so referential equality of anything that might ever compare inside two NaN's doesn't imply value equality. 
> 
> e.g. Anything with a polymorphic field can't use this.
> 
> You can argue that that was a bad call, but it was the expected call under IEEE semantics.

If I recall correctly, Prolog handles this by saying that
there is 'unification' and 'arithmetic comparison'
and NaN = NaN is true but NaN =:= NaN is false.

One way to handle this in a revised prelude would be
to have
   class MaybeEq a
     where equalIfPossible :: a -> a -> Maybe Bool
   class (MaybeEq a) => MaybeOrd a
     where compareIfPossible :: a -> a -> Maybe Order
     and then a bunch of IEEE-like operations, e.g.,
      x =? y if x = y or x,y are not comparable
      x =! y if x = y

   instance Eq t => MaybeEq t
     where equalIfPossible x y = Just (x == y)
   instance Ord t => MaybeOrd t
     where compareIfPossible x y = Just (compare x y)

and to move Double and Float to MaybeEq and MaybeOrd instead
of Eq and Ord, and support deriving (MaybeEq,MaybeOrd) in the
obvious way.

Then there could be a debate about whether to allow
instance Eq Double
  where x == y = if isNaN x then isNaN y else
                 case equalIfPossible x y of
                   Just True -> True
                   _         -> False
or not.




More information about the Haskell-Cafe mailing list