[Haskell-cafe] Does GHC compare pointers when eval'ing (==)
John Meacham
john at repetae.net
Thu Aug 21 01:19:26 UTC 2014
What you are discussing is "observable sharing". It breaks things
quite dramatically. Among other things, rather important optimizations
such as inlining are no longer valid if you allow it and reasoning
about efficiency goes right out the window.
It is possible to do safely without breaking things when done in the IO Monad,
import System.Mem.StableName
sameThing :: a -> a -> IO a
sameThing x y = liftM2 (==) (makeStableName x) (makeStableName y)
But whether it does what you expect after optimizations is
questionable and would likely behave differently under different
compilers.
John
On Wed, Aug 20, 2014 at 5:55 PM, Richard A. O'Keefe <ok at cs.otago.ac.nz> wrote:
>
> 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.
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
--
John Meacham - http://notanumber.net/
More information about the Haskell-Cafe
mailing list