[Haskell-cafe] Injective type classes?

Antonio Regidor Garcia chikitosan at gmail.com
Tue Mar 30 07:58:28 UTC 2021


El Tue, Mar 30, 2021 at 07:27:11AM +0000, Tom Smeding escribió:
> Hi Cafe,
> 
> With this class definition with this instance:
> 
> class C a where
> instance C b => C (a, b)
> 
> GHC can of course infer, given 'C b', that 'C (a, b)'. This is nothing more
> than the meaning of the instance declaration above.
> 
> However, GHC cannot infer the inverse:

Because the inverse doesn't need to hold. A priori, nothing prevents the existence of (a, b) instances where b isn't an instance.

For example, if Ord a and Ord b hold, then Ord (a, b) holds, but not the opposite. If, for example, you have Ord a but not Ord b, you can make (a, b) an instance of Ord by simply defining (a1, b1) `compare` (a2, b2) = a1 `compare` a2.

Best,

Antonio


More information about the Haskell-Cafe mailing list