Changes between Version 2 and Version 3 of TypeFunctions/ClassFamilies


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

--

Legend:

Unmodified
Added
Removed
Modified
  • TypeFunctions/ClassFamilies

    v2 v3  
    4040instance 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 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`. 
     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`. 
     43 
     44=== The language extension === 
     45 
     46We define class families as 
     47{{{ 
     48class family C a1 .. an 
     49}}} 
     50and class-family instances as 
     51{{{ 
     52class instance ctxt => C t1 .. tn where { sigs } 
     53}}} 
     54Class instances of class-family instances take the normal form.  The only additional constraint is that the class parameters are type instances of the class-family instance types.  That is, if we have 
     55{{{ 
     56instance ctxt' => C s1 .. sn where { .. } 
     57}}} 
     58then we need to have that each `si` is a type instance of `ti` for this to be a class instance of the class-family instance `C t1 .. tn`. 
     59 
     60As with data families, the class families can be associated with a class by declaring them in the class.  In this case, we omit the keywords `family` and `instance` in the family and instance declarations, respectively.  Moreover, all type indexes of an associated class need to be class parameters of the parent class. 
     61 
     62'''OPEN QUESTION:''' Do we allow associated types and classes(?!?) in class-family instances? 
     63 
     64=== Type checking === 
     65 
     66Like with data families, there is little impact on type checking.  Methods of class-family instances have signatures whole class constraints are not just variables.  For example, 
     67{{{ 
     68class instance C Int a where  
     69  foo :: a -> a 
     70}}} 
     71gives us 
     72{{{ 
     73foo :: C Int a => a -> a 
     74}}} 
     75Otherwise, superclasses and class instance introduce the usual given constraints. 
     76 
     77=== Desugaring === 
     78 
     79A class family declaration corresponds to a data family: 
     80{{{ 
     81class family C a1 .. an 
     82    || 
     83    vv 
     84data family C a1 .. an 
     85}}} 
     86 
     87A class-family instance corresponds to a data-family instance, which is the classes dictionary type. 
     88{{{ 
     89class instance forall b1..bn. ctxt => C t1 .. tn where { sigs } 
     90    || 
     91    vv 
     92data :R42C b1 .. bn = R42C { super :: ctxt, sigs } 
     93coe $Co:R42C b1 .. bn :: C t1 .. tn ~ :R42C b1 .. bn 
     94$WR42C :: ctxt -> sigs -> C t1 .. tn 
     95  -- the datacon wrapper and the field selectors 
     96  -- use the coercion $Co:R42C to move between the 
     97  -- indexed dictionary type and the representation 
     98  -- dictionary type (the current code in MkId for 
     99  -- data families should do all this already) 
     100}}} 
     101Moreover, the class-family instance will have a representation as a `Class.Class` in GHC, where the `classTyCon` is `:R42C` (i.e., the instance tycon of the dictionary).  We might also want `Class.Class` to have a `classParent` field as we have this at them moment for instance `TyCon`s. 
     102 
     103Finally, a class instance of a class-family instance is translated as usual: 
     104{{{ 
     105instance forall c1..cm. ctxt' => C s1 .. sn where { methods } 
     106    || 
     107    vv 
     108$dCs1sm :: ctxt' -> C s1 .. sn 
     109$dCs1sm dicts = $WR42C <superdict> methods 
     110}}} 
     111Moreover, we will have a `InstEnv.Instance` representation of the instance where `is_class` is the name of the class family and `is_tys` is `s1` to `sn`.  This is as constraint simplification does not know anything about the class-family instances. 
     112 
     113=== Related work === 
     114 
     115Compare to '''composite class signatures''' and '''submodules''' of the ''Modular Type Classes'' paper.