Changes between Version 1 and Version 2 of TypeFunctions/ClassFamilies
 Timestamp:
 May 16, 2007 1:06:46 AM (7 years ago)
Legend:
 Unmodified
 Added
 Removed
 Modified

TypeFunctions/ClassFamilies
v1 v2 3 3 == Class Families == 4 4 5 Our translation of data families in combination with the desugaring of classes into data types motivates the concept of '''indexed class families''', which turns out to be quiteuseful for generalising class APIs for commonly used data structures.5 Our translation of data families in combination with the desugaring of classes into data types suggest the idea of '''indexed class families''', which turns out to be rather useful for generalising class APIs for commonly used data structures. 6 6 7 7 === An example === … … 11 11 insert :: Set s => a > s a > s a 12 12 }}} 13 is too general. We need additional type constraints whose exact form ''depends on the type constructor'' we use to construct the sets; i.e., it varies on an instance by instance basis. For list , we just need `Eq`, but for sets as finite maps, we need `Ord`.13 is too general. We need additional type constraints whose exact form ''depends on the type constructor'' we use to construct the sets; i.e., it varies on an instance by instance basis. For lists, we just need `Eq`, but for sets as finite maps, we need `Ord`. 14 14 15 15 With indexed class families, we can define a set class as follows: … … 29 29 insert x s  x `elem` s = s 30 30  otherwise = x:s 31 instance C [] a31 instance Eq a => C [] a 32 32 }}} 33 33 and sets as finite maps … … 38 38 empty = Data.Map.empty 39 39 insert x s = Data.Map.insert x () s 40 instance C MapSet a40 instance Ord a => C MapSet a 41 41 }}} 42 we instantiate `C` differently for different type indexes. The class family instances have no members in this case, but use existing classes as superclass to supply `insert` with the equality and ordering methods, respectively.42 we instantiate `C` differently for different type indexes. The class family instances have no members in this case, but use existing classes as a superclass to supply `insert` with the equality and ordering methods, respectively. As we want to use these superclasses for sets of any element type of which we have an instance of the superclasses, we need a catchall instance for each class instance. That is somewhat ugly especially, as it requires the use of `fallowundecidableinstances`.