Source code for standard instances of typeclass instances

I was interested to know about declaring an Ord instance for (a,b) , and I would like to quickly look through the hackers to confirm my intuition that the comparison is first on a , and then, in case of equality, on b . In particular, I went here . Since hackers have links to the source code for declarations and data functions, I assumed that there will also be source code for instance declarations, but I can not find them. Is there a reason why they are not there, or I just did not look complicated enough? type Answer = Either Explanation Directions :)

+6
source share
3 answers

I went to the foreplay , clicked the source link for the Ord typeclass , it scrolls a bit, and found that it is defined as

 deriving instance (Ord a, Ord b) => Ord (a, b) 

It uses the extension StandaloneDeriving . It basically generates the same code as the type

 data (a, b) = (a, b) deriving Ord 
+4
source

The Ord instance for tuples is deduced according to the rules from the language specification that are returned with respect to Gofer .

 instance (Eq a, Eq b) => Eq (a,b) where (x,y) == (u,v) = x==u && y==v instance (Ord a, Ord b) => Ord (a,b) where (x,y) <= (u,v) = x<u || (x==u && y<=v) 
+4
source

The Haskell 98 report states in section 10.1 :

Class methods automatically introduced by derived instances of Eq and Ord - (==), (/ =), cf. (<), (<=), (>), (> =), max and min. The last seven operators are defined so as to compare their arguments lexicographically with respect to a given set of constructors, with earlier constructors in the data type declaration, counting less than later ones.

Derived comparisons always cross constructors from left to right.

...

All derived operations of class Eq and Ord are strict in both arguments.

+4
source

Source: https://habr.com/ru/post/914719/


All Articles