Changes between Version 6 and Version 7 of Defaulting


Ignore:
Timestamp:
Nov 21, 2006 11:25:28 AM (7 years ago)
Author:
malcolm.wallace@…
Comment:

add comments about MPTC, cancelling defaults, etc

Legend:

Unmodified
Added
Removed
Modified
  • Defaulting

    v6 v7  
    88 * Defaults are limited to Prelude numeric classes.  A tool like Hat, which transforms Haskell source, cannot transform the defaults, because there is no way make defaults apply to the transformed classes rather than the original ones. 
    99 
    10  * Defaults cannot be applied to user-defined classes.  It would be nice for some applications to be able to allow a default clause to name the class being defaulted over, as well as the type to choose.  Examples include QuickCheck, where you might wish to default the Arbitrary class to something small (e.g. Bool) when a value is otherwise unconstrained. 
     10 * Defaults cannot be applied to user-defined classes.  It would be nice for some applications to be able to allow a default clause to name the class being defaulted over, as well as the type to choose.  Examples include !QuickCheck, where you might wish to default the {{{Arbitrary}}} class to something small (e.g. {{{Bool}}}) when a value is otherwise unconstrained. 
    1111 
    1212 * Report specification of defaulting is impossible to implement in the presence of recursive modules.  (Should it be specified that a group of mutually recursive modules must have exactly the same defaulting?) 
     
    1818   * A compromise might be to allow defaults which will be inherited to be specified only in the module that defines a class, but groups of mutually recursive modules may override defaulting locally. this will avoid the import changing behavior problem and allow some sort of inheritence of defaults. 
    1919 
    20 == Proposal 1 == 
     20== Proposal 1 - name the class == 
    2121 
    2222Allow defaulting clauses of the following form 
     
    6161 
    6262 
    63 == Proposal 2 == 
     63== Proposal 2 - name the class + no lists == 
    6464 
    6565Change default decls to (a) name the class being defaulted, and (b) permit only one default type per class, rather than a list. 
    6666Possible syntax, by analogy with instance decls: 
    6767{{{ 
    68 topdecl  ->  default  tycls type 
     68topdecl  ->  default tycls type 
    6969}}} 
    7070 
     
    7575}}} 
    7676the type 'a', constrained by class 'C' but otherwise unresolved, is instantiated to the sole type 'T'. 
    77 If there is no unique choice, for instance because more than one class-with-a-default is mentioned in the context (and their default types are different), then it is a static error.  Note that it is OK to have several classes in the context of the defaultable value, provided only one of them is declared to yield a default type, or if more  
    78 than one yields a type, those types are the same.  Choosing a default after context simplification means that no conflicts between super- and sub-classes can arise. 
    7977 
    80 Some examples: 
     78 * If there is no unique choice, for instance because more than one class-with-a-default is mentioned in the context (and their default types are different), then it is a static error. 
     79 * Note that it is OK to have several classes in the context of the defaultable value, 
     80   * provided only one of them is declared to yield a default type, 
     81   * or if more than one yields a type, those types are the same. 
     82 * Choosing a default after context simplification means that no conflicts between super- and sub-classes can arise. 
     83 
     84=== Examples === 
     85Here are some examples, to clarify the idea: 
    8186{{{ 
    8287default Eq Integer 
     
    108113A static error, because there are no defaults declared for any of the classes involved. 
    109114 
     115=== Backwards compatibility === 
    110116The current Haskell'98 default-default behaviour can be specified as: 
    111117{{{ 
    112 default ( Num => Integer 
    113         , Real => Integer 
    114         , Enum => Integer 
    115         , Integral => Integer 
    116         , Fractional => Double 
    117         , RealFrac => Double 
    118         , Floating => Double 
    119         , RealFloat => Double 
    120         ) 
     118default Num Integer 
     119default Real Integer 
     120default Enum Integer 
     121default Integral Integer 
     122default Fractional Double 
     123default RealFrac Double 
     124default Floating Double 
     125default RealFloat Double 
    121126}}} 
    122127 
     128=== Turning off defaults === 
     129In Haskell'98, the default decl not only declares a new default type for some classes, but can also implicitly remove defaults for other classes (at the same time!). 
     130Whilst {{{default ()}}} switches off all defaulting,  {{{default (Int)}}} changes the default type for the Num, Real, Enum, Integral classes, and also declares there is no 
     131default for the Fractional, !RealFrac, Floating, and !RealFloat classes (by virtue of Int's failure to be a instance of those). 
     132 
     133Under the current proposal, there is no explicit means to turn off defaults.  A minor modification rectifies this shortcoming - simply permit the omission of the ''type'' part of the default decl.  An omitted type indicates there is no default type for this class. 
     134 
     135=== Multi-parameter type classes === 
     136Defaulting for MPTC follows the scheme for single-parameter classes. 
     137{{{ 
     138topdecl  ->  default tycls type_1 type_2 ... type_n 
     139}}} 
     140That is, there is a single unique relation between types that can be chosen if ''all'' of its variables are otherwise unconstrained. 
     141 
     142Open questions: if some of the variables are already resolved to actual types, is it OK to default the remaining variables?  Assuming the resolved types are in the default relation anyway?  What if the already-resolved types are not in the default relation?  Would anyone ever want to use defaulting to choose different types depending on what other types have already beeen resolved? 
     143 
    123144=== Pros === 
    124  * less ad hoc than current method 
     145 * simpler, more general, and less ad hoc than current method 
    125146 * overcomes the Hat transformation problem 
    126147 * does not rely on textual ordering of decl 
     
    130151 * not sure if this exactly captures the existing Haskell'98 module (but because defaults are currently limited to Prelude classes, it probably does). 
    131152 
    132 == Proposal 3 == 
     153== Proposal 3 - global scope == 
     154 
    133155Orthogonal to the issue of how to specify defaults, is the question of whether they should be module-local, or if instead we should treat them like instance declarations - global in scope.  One can also imagine permitting explicit lexical scoping, i.e. a local override of the defaults, purely within a single function definition. 
    134156