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

Chris Wong lambda.fairy at gmail.com
Wed Aug 20 09:59:42 UTC 2014


On Wed, Aug 20, 2014 at 9:20 PM, Michael Snoyman <michael at snoyman.com> wrote:
>
>
>
> On Wed, Aug 20, 2014 at 12:15 PM, Erik Hesselink <hesselink at gmail.com>
> wrote:
>>
>> On Wed, Aug 20, 2014 at 10:35 AM, Michael Snoyman <michael at snoyman.com>
>> wrote:
>> >
>> > On Wed, Aug 20, 2014 at 11:28 AM, Johan Tibell <johan.tibell at gmail.com>
>> > wrote:
>> >>
>> >> On Wed, Aug 20, 2014 at 10:23 AM, Erik Hesselink <hesselink at gmail.com>
>> >> wrote:
>> >>>
>> >>> As I understood, the question was if GHC would first compare pointers,
>> >>> and only call the Eq instance if the pointers are not equal. I guess
>> >>> this would be safe, but I don't think GHC does such a thing.
>> >>
>> >> I think the reason it isn't done is that it's not always an
>> >> optimization.
>> >> We do it manually in e.g. bytestring.
>> >
>> > There are two cases I can think of where it would also change the
>> > semantics
>> > of the code:
>> >
>> > 1. An Eq instance that doesn't obey the reflective property (not
>> > recommended):
>> >
>> > data BadEq = BadEq
>> > instance Eq BadEq where
>> >     BadEq == BadEq = False
>>
>> I think this is just a buggy instance, and if you do this, nothing is
>> guaranteed. Many functions with an Eq constraint will also not work.
>> Interestingly, reflexivity is not a law listed in the haddock
>> documentation.
>>
>> Erik
>
>
> I agree that this is terrible practice, and I hope no one is actually doing
> this. It's unfortunate that reflexivity isn't listed in the Haddocks.
> Perhaps we should start a separate proposal for making reflexivity an
> officially required law of the Eq class.
>
> Michael

The problem with requiring reflexivity (or *any* law related to
Eq/Ord, for that matter) is that Double and Float break it.

Observe:

    ghci> let nan = 0.0 / 0.0 in nan == nan
    False

Rust[1] side-steps this issue by having separate Eq (lawful) and
PartialEq (unlawful) classes, but it's a pretty intrusive change.

Chris

[1] http://doc.rust-lang.org/core/cmp/trait.PartialEq.html


More information about the Haskell-Cafe mailing list