Abstracting type-classes

There are several ways in which type-classes can be generalized further. In this section, we will focus on extending the number of type parameters from one to many. The extension to multiparameter type-classes demands that we specify relations between type parameters by way of functional dependencies.

Multiparameter type-classes

We can view regular type-classes (for example Ord a, Monad a, and so on.) as a way to specify a set of types. Multiparameter classes, on the other hand, specify a set of type relations. For example, the Coerce type-class specifies a relation between two type parameters:

class Coerce a b where
  coerce :: a -> b

instance Coerce Int String where
  coerce = show

instance Coerce Int [Int] where coerce x = ...

Get Haskell Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.