Type class instances
The set of types that instantiates the Eq
type class is not fixed. As programmers, we can extend it with additional types.
Type class instantiation
As we saw in the What is ad hoc polymorphism? section, the Suit
data type does not instantiate the Eq
type class and therefore does not support (==)
and (/=)
. We can remedy that by writing our own instance for it:
instance Eq Suit where Hearts == Hearts = True Diamonds == Diamonds = True Spades == Spades = True Clubs == Clubs = True _ == _ = False Hearts /= Hearts = False Diamonds /= Diamonds = False Spades /= Spades = False Clubs...