I am exploring the concept of method-less typeclasses that would inherit from a superclass but signal more constraint on their subject. Rust does this™:
For example,
-- | Partial Equivalence Relation.
-- The laws are Symmetry and Transitivity.
class PartialEq a where
(==), (/=) :: a -> a -> Bool
x /= y = not (x == y)
x == y = not (x /= y)
-- | Total Equivalence Relation.
-- Two more laws are brought in:
-- * Reflexivity
-- * (==) and (/=) are strict inverses
class PartialEq a => Eq a
-- Tumbleweeds…
And similarly, PartialOrd
would take from PartialEq
, and Ord
would take from Eq
and PartialOrd
.
And we’d be able to say goodbye to those unlawful instances:
I’m not advocating (right now) for changing the definitions of what we have in ghc-prim to match these, but if I were, I’d bring tooling on the table that could be easily used by everyday haskellers to automate the verification of those laws for their own instances.