ADTs in Haskell can automatically become instance of some typeclasses (like Show
, Eq
) by deriving from them.
data Maybe a = Nothing | Just a
deriving (Eq, Ord)
My question is, how does this deriving work, i.e. how does Haskell know how to implement the functions of the derived typeclass for the deriving ADT?
Also, why is deriving restricted to certain typeclasses only? Why can't I write my own typeclass which can be derived?