Changes between Version 81 and Version 82 of TypeFunctions


Ignore:
Timestamp:
Apr 6, 2008 8:37:02 AM (7 years ago)
Author:
chak
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TypeFunctions

    v81 v82  
    1 = Type Functions and Associated Types in GHC - The Master Plan =
     1= Type Functions, Type Families, and Associated Types in GHC - The Master Plan =
    22
    3 This page serves as a collection of notes concerning the implementation of type functions and associated types, especially about the implications for type checking, interface files, and F,,C,, intermediate code generation.
     3This page serves as a collection of notes concerning the implementation of type families (aka type functions) and associated types, especially about the implications for type checking, interface files, and F,,C,, intermediate code generation.
    44
    5 == Aims ==
     5== Status ==
    66
    7 New features:
    8  * Open type-indexed data types and type functions
    9  * Associated data types and type synonyms, which are type-indexed data types and type functions associated with a class - i.e., associated types are syntactic sugar for type-indexed types and type functions.
     7Detailed information about implemented and unimplemented features as well as bugs and plans for further improvements is at [wiki:TypeFunctionsStatus implementation status].  The following provides a summary:
    108
    11 Revised features
    12  * We may want to re-implement functional dependencies using associated type synonyms.
     9Implemented features:
     10 * All basic functionality of open data type families, open type synonym families, and equality constraints has been implemented.
     11 * Type checking is fully integrated with GADTs.
    1312
    14 We keep track of the current [wiki:TypeFunctionsStatus implementation status].
     13Missing features:
     14 * Superclass equalities.
     15 * Data family instances in GADT form.
     16 * Closed or exhaustive synonym families.
     17 * Re-implement functional dependencies using type families.
    1518
    16 We also have notes on [wiki:TypeFunctionsSynTC type checking with indexed synonyms.]
     19Speculative ideas:
     20 * [wiki:TypeFunctions/ClassFamilies Class families.]
     21 * Our type-indexed data types are open.  However, we currently don't allow case expressions mixing constructors from different indexes.  We could do that if we had a story for open function definitions outside of classes.  Class instances of entire data families (including `deriving` clauses at family declarations to derive for all instances) requires the same sort of capabilities as case expressions mixing data constructors from different indexes.  This is, as they require to build a dictionary that applies to all family instances (as opposed to a distinct dictionary per instance, which is what we have now).
    1722
    18 A related extension are [wiki:TypeFunctions/ClassFamilies class families.]
    1923
    2024== Terminology ==
     
    4145Note: we previously used the term "indexed type", but have now switched to using "type family".  Please change any  uses of the former into the latter as you come across them.
    4246
    43 == Specification and Restrictions ==
    44 
    45 The user-level definition of the type-family extensions is given here [http://haskell.org/haskellwiki/GHC/Indexed_types]. Section 4, "Definition of the type system extension" constitues the specification.
    46 
    47 Refinement of the specification in the ''Beyond Associated Types'' paper.  (I'll actually link this paper here once it is a bit more coherent.)  Some [wiki:TypeFunctionsExamples examples are on an extra page].
    48  * Kind signatures of indexed data type families have the form
    49  {{{
    50 data family T a1 .. an [:: <kind>]
    51 }}}
    52  and introduce a type family whose kind is determined by the kinds of the `ai` (which can have kind annotations) and the optional signature `<kind>` (which defaulys to `*`).
    53  * Kind signatures of type function have the form
    54  {{{
    55 type family T a1 .. an [:: <kind>]
    56 }}}
    57  and introduce `n`-ary type functions (with `n` >= 1), which may be of higher-kind.  Again, the type variables can have kind signatures and the result kind signature is optional, with `*` being the default.  Type instances for an `n`-ary type function must specify exactly `n` arguments, which serve as indexes.
    58  * Applications of type synonym families need to supply all indexes after unfolding of all ordinary type synonyms.  (This is the same saturation requirement that we already have on ordinary type synonyms.)
    59  * Instances of data and type families have the keyword `instance` after the first keyword.  They otherwise have the same form as ordinary data type and type synonym declarations, respectively, but can have non-variable type indexes as arguments.  Type indexes can include applications of data families, but no type synonym families.
    60  * Instances of type families are only valid if a kind signature for the type constructor is in scope.  The kind of an indexed type is solely determined from the kind signature.  Instances must conform to this kind.  In particular, the argument count of data and newtype instances must match the arity indicated by the kind.  The number of arguments of a type synonym instance must be equal to the number of type indexes (i.e., type variables in the head) of the family declaration.  Instances of data families can be both data types or newtypes (or a mix of the two).
    61  * Data family instances can have deriving clauses as usual (but they do not support the non-standard deriving of `Typeable`).
    62  * Associated types are type families declared as part of a type class.  The syntax of family declarations in class declarations and of type instance declarations in instance declarations is as for toplevel declarations, but without the `family` and `instance` keywords.
    63  * Instances of an associated type can only be defined in instances of its class.  However, it is admissible to omit the type definition in instances of the class (similar to how methods may be omitted).  Then, the only inhabitant of the corresponding type is `undefined`.
    64  * All argument variables of an associated type family declaration need to be class parameters.  There may not be any repetitions, but the order of the variables can differ from that in the class head and the type family can be defined over a subset of the class parameters.
    65  * In instances, the type indexes of a type declaration must be identical to the corresponding class parameters (i.e., those that share the same variable name in the class declaration).  And all arguments that where not connected to a class parameter in the family declaration must be variables; i.e., cannot be used as type indexes.
    66  * Type contexts (including super class and instance contexts) can have equational constraints of the form `t1 ~ t2`, where the two types `t1` and `t2` need to be rank 0 types. 
    67  * In an export and import list, associated types are treated as subcomponents of their type class, just like the class methods.  In particular, `C(..)` denotes class `C` with all its methods and all its associated types.  If the associated types of a class are explicitly listed in the parenthesis, each type name needs to be prefixed with the keyword `type`; i.e., to denote class `C` with associated type `T` and method `foo`, we write `C(type T, foo)`.
    68  * In export and import lists, all data constructors of newtype and data families defined in any newtype or data instance is regarded to be a subcomponent of the family type constructor, and hence specified by `F(..)` if `F` is the family type constructor.  Instead of specifying them all with "`..`", they can also be explicitly listed, just as with vanilla data types.
    69  * Instances data families may not overlap (as such instances correspond to indeterminate type functions).  Type synonym instances may overlap if the right-hand sides are syntactically equal under the overlap substitution.  (Rational: We cannot be more lazy about checking overlap, as we otherwise cannot guarantee that we generate an F,,C,, program that fulfils the formal consistency criterion.)
    70  * FFI signatures do not look through indexed newtypes nor through indexed synonyms.  (The main reason for not looking through indexed synonyms is as they may occur in the rhs of a vanilla newtype.)
    71  * To enable indexed type families, the switch `-ftype-families` needs to be used (which is implied by `-fglasgow-exts`).
    72 
    73 Restrictions:
    74  * We currently don't allow indexed GADTs. I cannot see any fundamental problem in supporting them, but I want to keep it simple for the moment. (When allowing this, a constructor signature in an associated GADT can of course only refine the instantiation of the type arguments specific to the instance in which the constructor is defined.)
    75 
    76 
    7747
    7848== How It Works ==
     
    8656
    8757
    88 == Possible Extensions ==
     58== References ==
    8959
    90  * Our type-indexed data types are open.  However, we currently don't allow case expressions mixing constructors from different indexes.  We could do that if we had a story for open function definitions outside of classes.
    91  * Class instances of entire data families (including `deriving` clauses at family declarations to derive for all instances) requires the same sort of capabilities as case expressions mixing data constructors from different indexes.  This is, as they require to build a dictionary that applies to all family instances (as opposed to a distinct dictionary per instance, which is what we have now).
     60 * [http://www.cse.unsw.edu.au/~chak/papers/CKPM05.html Associated Types with Class.] Manuel M. T. Chakravarty, Gabriele Keller, Simon Peyton Jones, and Simon Marlow. In Proceedings of The 32nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL'05), ACM Press, pages 1-13, 2005.
     61 * [http://www.cse.unsw.edu.au/~chak/papers/CKP05.html Associated Type Synonyms.] Manuel M. T. Chakravarty, Gabriele Keller, and Simon Peyton Jones. In Proceedings of The Tenth ACM SIGPLAN International Conference on Functional Programming, ACM Press, pages 241-253, 2005.
     62 * [http://www.cse.unsw.edu.au/~chak/papers/SSPC07.html Towards Open Type Functions for Haskell.] Tom Schrijvers, Martin Sulzmann, Simon Peyton-Jones, and Manuel M. T. Chakravarty. Presented at IFL 2007.
     63 * [http://www.cse.unsw.edu.au/~chak/papers/SPCS08.html Type Checking with Open Type Functions.] Tom Schrijvers, Simon Peyton-Jones, Manuel M. T. Chakravarty, and Martin Sulzmann. Unpublished draft.
     64 * Old and outdated wiki material on [wiki:TypeFunctionsSynTC type checking with indexed synonyms.]
     65