-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Haskell Design Patterns
By :
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.
We can view regular type-classes (for example a, 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 = [x]
The type signature of coerce is as follows:
coerce :: Coerce a b => a -> b
This states that coerce is the function a -> b if a is coerce-able to b, that is, if the relation (Coerce a b) exists. In our case, coerce will work...
Change the font size
Change margin width
Change background colour