Make Eq type class single method
Joachim Breitner
mail at joachim-breitner.de
Wed Oct 20 15:25:22 UTC 2021
Hi,
Am Mittwoch, dem 20.10.2021 um 16:08 +0100 schrieb Tom Ellis:
> Since (/=) is an Eq class method it can be implemented on (Int, Int)
> as
>
> (x1, x2) /= (y1, y2) = (x1 /= y1) || (x2 /= y2)
>
> If it were not a class method it would have to be implemented as,
> after inlining
>
> (x1, x2) /= (y1, y2) = not ((x1 == y1) && (x2 == y2))
>
> Might not the latter be less efficient, because of the extra `not`?
maybe. I think I’ll simply try it out, and see if for example GHC
itself speeds up if (/=) is not a class method.
Note that if the inner Eq is using the default implementation, then
there will be _two_ not there.
I’d expect that not will quickly inline, and the cost of an inlined not
might be small compared to the cost of projecting (/=) out of the class
dictionary (in polymorphic code). And in other cases, the (/=) might
inline and the not might be fused with the surrounding code via case-
of-case.
Cheers,
Joachim
--
Joachim Breitner
mail at joachim-breitner.de
http://www.joachim-breitner.de/
More information about the Libraries
mailing list