Changes between Version 1 and Version 2 of TypeFunctions/ClassFamilies


Ignore:
Timestamp:
May 16, 2007 1:06:46 AM (7 years ago)
Author:
chak
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TypeFunctions/ClassFamilies

    v1 v2  
    33== Class Families == 
    44 
    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 quite useful for generalising class APIs for commonly used data structures. 
     5Our 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. 
    66 
    77=== An example === 
     
    1111insert :: Set s => a -> s a -> s a 
    1212}}} 
    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`. 
     13is 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`. 
    1414 
    1515With indexed class families, we can define a set class as follows: 
     
    2929  insert x s | x `elem` s = s 
    3030             | otherwise  = x:s 
    31 instance C [] a 
     31instance Eq a => C [] a 
    3232}}} 
    3333and sets as finite maps 
     
    3838  empty = Data.Map.empty 
    3939  insert x s = Data.Map.insert x () s 
    40 instance C MapSet a 
     40instance Ord a => C MapSet a 
    4141}}} 
    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. 
     42we 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 catch-all instance  for each class instance.  That is somewhat ugly especially, as it requires the use of `-fallow-undecidable-instances`.