Changes between Version 13 and Version 14 of Records/OverloadedRecordFields/Plan


Ignore:
Timestamp:
Jul 1, 2013 8:35:59 AM (2 years ago)
Author:
adamgundry
Comment:

tell the latest story

Legend:

Unmodified
Added
Removed
Modified
  • Records/OverloadedRecordFields/Plan

    v13 v14  
    3535
    3636
     37=== Projections ===
     38
     39Record field constraints are introduced by projections. If there are two or more fields `x` in scope, then an occurrence of `x` has type `a { x :: b } => a -> b` instead of generating an ambiguity error. If there is a single field `x` in scope, then it refers to the usual monomorphic record selector (ensuring backwards compatibility). If there are any normal identifiers `x` in scope (as well as fields) then a use of `x` leads to an ambiguity error.
     40
     41
    3742=== Record field constraints ===
    3843
     
    4651Recall that `Symbol` is the kind of type-level strings. The notation extends to conjunctions:  `r {x :: tx, y :: ty}` means `(Has r "x" tx, Has r "y" ty)`. Note also that `r` and `t` might be arbitrary types, not just type variables or type constructors.  For example, `T (Maybe v) { x :: [Maybe v] }` means `(Has (T (Maybe b)) "x" [Maybe v])`.
    4752
    48 Instances for the `Has` typeclass are implicitly generated,  corresponding to fields in datatype definitions. For example, the data type
     53Instances for the `Has` typeclass are automatically generated (for modules with `-XOverloadedRecordFields` enabled) using the record fields that are in scope. For example, the data type
    4954
    5055{{{
     
    6267
    6368
    64 === Projections: the dot operator ===
    65 
    66 Record field constraints are introduced by projections. If there are two or more fields `x` in scope, then an occurrence of `x` has type `a { x :: b } => a -> b` instead of generating an ambiguity error. If there is a single field `x` in scope, then it refers to the usual monomorphic record selector (ensuring backwards compatibility). If there are any normal identifiers `x` in scope (as well as fields) then a use of `x` leads to an ambiguity error.
    67 
    68 
    6969=== Representation hiding ===
    7070
    71 At present, a datatype in one module can declare a field, but if the selector function is not exported, then the field is hidden from clients of the module. It is important to support this. Typeclasses in general have no controls over their scope, but for implicitly generated `Has` instances, the instance is in scope iff the record field selector function is.
    72 
    73 This enables representation hiding: exporting the field selector is a proxy for permitting access to the field. For example, consider the following module:
     71At present, a datatype in one module can declare a field, but if the selector function is not exported, then the field is hidden from clients of the module. It is important to support this. Typeclasses in general have no controls over their scope, but for implicitly generated `Has` instances, the instance is available for a module if `-XOverloadedRecordFields` is enabled for that module and the record field selector function is in scope.
     72
     73This enables representation hiding: just like at present, exporting the field selector permits access to the field. For example, consider the following module:
    7474
    7575{{{
     
    8080}}}
    8181
    82 Any module that imports `M` will have access to the `x` field from `R` but not from `S`, because the instance `Has R "x" Int` will be in scope but the instance `Has S "x" Bool` will not be. Thus `R { x :: Int }` will be solved but `S { x :: Bool }` will not.
     82Any module that imports `M` will have access to the `x` field from `R` but not from `S`, because the instance `Has R "x" Int` will be available but the instance `Has S "x" Bool` will not be. Thus `R { x :: Int }` will be solved but `S { x :: Bool }` will not.
     83
     84
     85=== Multiple modules and automatic instance generation ===
     86
     87Note that `Has` instances are generated on a per-module basis, using the fields that are in scope for that module, and automatically generated instances are never exported. Thus it doesn't matter whether `-XOverloadedRecordFields` was on in the module that defined the datatype. The availability of the instances in a particular module depends only on whether the flag is enabled for that module.
     88
     89Suppose module `M` imports module `N`, `N` imports module `O`, and only `N` has the extension enabled. Now `N` can project any field in scope (including those defined in `O`), but `M` cannot access any `Has` instances.
     90
     91This means that
     92 * the extension is required whenever a `Has` constraint must be solved;
     93 * no new mechanism for hiding instances is required; and
     94 * records defined in existing modules (or other packages) without the extension can still be overloaded.
     95
     96
     97=== Higher-rank fields ===
     98
     99If a field has a rank-1 type, the `Has` encoding works fine: for example,
     100
     101{{{
     102data T = MkT { x :: forall a . a -> a }
     103}}}
     104
     105gives rise to the instance
     106
     107{{{
     108instance (b ~ a -> a) => Has T "x" b
     109}}}
     110
     111However, if a field has a rank-2 type or higher (so the selector function has rank at least 3), things are looking dangerously impredicative:
     112
     113{{{
     114data T b = MkT { x :: (forall a . a -> a) -> b }
     115}}}
     116
     117would give
     118
     119{{{
     120instance (c ~ ((forall a . a -> a) -> b)) => Has (T b) "x" c
     121}}}
     122
     123but this is currently forbidden by GHC, even with `-XImpredicativeTypes` enabled. Indeed, it would not be much use if it were possible, because bidirectional type inference relies on being able to immediately infer the type of neutral terms like `x e`, but overloaded record fields prevent this. Non-overloaded field names are likely to be needed in this case.
    83124
    84125
     
    128169
    129170
    130 === Virtual record fields ===
    131 
    132 It is easy to support virtual record fields, by permitting explicit `Has` instances:
     171=== Polymorphic record update for lenses ===
     172
     173As noted above, supporting a polymorphic version of the existing record update syntax (in its full generality) is difficult. However, even if the existing record update syntax remains monomorphic, an additional motivation for polymorphic update comes from [http://hackage.haskell.org/package/lens lens]. If we automatically generate instances of an extra class like
     174
     175{{{
     176class Set (r :: *) (x :: Symbol) (t :: *) where
     177  setFld :: r -> t -> r
     178}}}
     179
     180and supply the instance (where `&x` is used for explicit type application of the `x` argument)
     181
     182{{{
     183instance (Functor f, Has s x a, Set s x a, fs ~ f s, fa ~ f a) => Has (a -> fa) x (s -> fs) where
     184  getFld f r = setFld &s &x &a r <$> f (getFld &s &x &a r)
     185}}}
     186
     187then every record field (for which a `Set` instance can be generated) is automagically a lens. This reduces the need for the current name-mangling Template Haskell implemented in the lens library. (Note that this instance requires explicit type application, or a proxy-based workaround, in order to supply the `x` argument.)
     188
     189More work is needed to identify the right way to formulate the `Set` class: type-changing update requires a slightly more general version, and there is a story for [https://github.com/ekmett/lens/issues/197 multiple update]. Higher-rank fields remain problematic.
     190
     191
     192=== User-defined `Has` instances ===
     193
     194Should the user be allowed to write explicit `Has` instances? For example:
    133195
    134196{{{
     
    137199}}}
    138200
    139 Note that if `r` is a datatype with a field `x`, the virtual field will overlap, and the usual rules about overlap checking apply. Explicit instances follow the usual instance scope rules, so a virtual record field instance is always exported and imported.
    140 
    141 `Has` constraints are slightly more general than the syntactic sugar suggests: one could imagine building constraints of the form `Has r l t` where `l` is non-canonical, for example a variable or type family. It's hard to imagine uses for such constraints, though. One idea is giving virtual fields of all possible names to a type:
    142 
    143 {{{
    144 instance Has T l () where
    145   getFld _ = ()
    146 }}}
    147 
    148 
    149 === Record selectors ===
    150 
    151 Even with `-XOverloadedRecordFields` enabled, monomorphic record selector functions will be generated by default for backwards compatibility reasons, and for use when there is no ambiguity. They will not be usable if multiple selectors with the same name are in scope.
    152 
    153 Optionally, we could [wiki:Records/DeclaredOverloadedRecordFields/NoMonoRecordFields add a flag `-XNoMonoRecordFields`] to disable the generation of the usual monomorphic record field selector functions.  This is not essential, but would free up the namespace for other record systems (e.g. '''lens'''). Even if the selector functions are suppressed, we still need to be able to mention the fields in import and export lists, to control access to them (as discussed in the [wiki:Records/OverloadedRecordFields/Plan#Representationhiding representation hiding] section).
    154 
    155 We could also add a flag `-XPolyRecordFields` to generate polymorphic selector functions. This implies `-XNoMonoRecordFields`. For example, if a record with field `x` is declared then the function
    156 
    157 {{{
    158 x :: Has r "x" t => r -> t
    159 x = getFld
    160 }}}
    161 
    162 would be generated. However, these have slightly odd behaviour: if two independent imported modules declare fields with the same label, they will both generate identical polymorphic selectors, so only one of them should be brought into scope.
    163 
    164 
    165 === Monomorphism restriction and defaulting ===
    166 
    167 The monomorphism restriction may cause annoyance, since
    168 
    169 {{{
    170 foo = \ e -> x e
    171 }}}
    172 
    173 would naturally be assigned a polymorphic type. If there is only one `x` in scope, perhaps the constraint solver should pick that one (analogously to the other defaulting rules). However, this would mean that bringing a new `x` into scope (e.g. adding an import) could break code. Of course, it is already the case that bringing new identifiers into scope can create ambiguity!
    174 
    175 For example, suppose the following definitions are in scope:
    176 {{{
    177 data T = MkT { x :: Int, y :: Int }
    178 data S = MkS { y :: Bool }
    179 }}}
    180 
    181 Inferring the type of `foo = \ e -> x e` results in `alpha -> beta` subject to the constraint `alpha { x :: beta }`. However, the monomorphism restriction prevents this constraint from being generalised. There is only one `x` field in scope, so defaulting specialises the type to `T -> Int`. If the `y` field was used, it would instead give rise to an ambiguity error.
    182 
    183 
    184 === Higher-rank fields ===
    185 
    186 If a field has a rank-1 type, the `Has` encoding works fine: for example,
    187 
    188 {{{
    189 data T = MkT { x :: forall a . a -> a }
    190 }}}
    191 
    192 gives rise to the instance
    193 
    194 {{{
    195 instance (b ~ a -> a) => Has T "x" b
    196 }}}
    197 
    198 However, if a field has a rank-2 type or higher (so the selector function has rank at least 3), things are looking dangerously impredicative:
    199 
    200 {{{
    201 data T b = MkT { x :: (forall a . a -> a) -> b }
    202 }}}
    203 
    204 would give
    205 
    206 {{{
    207 instance (c ~ ((forall a . a -> a) -> b)) => Has (T b) "x" c
    208 }}}
    209 
    210 but this is currently forbidden by GHC, even with `-XImpredicativeTypes` enabled. Indeed, it would not be much use if it were possible, because bidirectional type inference relies on being able to immediately infer the type of neutral terms like `x e`, but overloaded record fields prevent this. Traditional monomorphic selector functions are likely to be needed in this case.
    211 
    212 
    213 === Multiple modules and implicit instance generation ===
    214 
    215 When should `Has` instances be implicitly generated? I can think of three options:
    216  1. If the extension is on for a module, generate instances for all datatypes in that module when checking their declarations. This means that record projections are not available to code that imports a datatype definition from a module without the flag. Some mechanism will need to restrict the scope of instances based on import/export of selectors.
    217  2. If the extension is on for a module, generate instances for all record selectors that are in scope, but do not export them. Thus it doesn't matter whether the flag was on in the module that defined the datatype, and the availability of the instances in a particular module depends only on whether the flag is enabled.
    218  3. If the extension is on for a module, generate and export instances for all record selectors that are in scope and do not already have instances. This is a hybrid of (1) and (2), and also requires a mechanism to restrict instance scope based on import/export of selectors.
    219 
    220 Note that (2) can be equivalently implemented (as far as the user is concerned) by not really generating instances at all, but solving `Has` constraints directly based on the selectors in scope, much as `SingI` constraints are solved on-the-fly.
    221 
    222 Suppose module `M` imports module `N`, `N` imports module `O`, and only `N` has the extension enabled. Under (1), `N` can project fields it defines (but not those defined in `O`), and `M` also has access to the `Has` instances for `N` (but not the dot syntax). Under (2), `N` can project any field in scope (including those defined in `O`), but `M` cannot access any `Has` instances. Under (3), `N` can project any field in scope, and `M` has access to the `Has` instances for `N` and `O` (but not fields defined in `M`).
    223 
    224 I think (2) is probably the right choice here, because
    225  * the extension is required whenever dot notation is used or a `Has` constraint must be solved;
    226  * no new mechanism for hiding instances is required; and
    227  * records defined in existing modules (or other packages) without the extension can still be used with dot notation.
     201Even with an explicit `Has` instance as above, the name `x` will not be in scope unless a datatype has a field with name `x`. Thus it is not really useful. The previous proposal, where `(.x)` always meant "project out the `x` field", used explicit `Has` instances for virtual fields.
     202
     203
     204=== Hiding record selectors ===
     205
     206Optionally, we could [wiki:Records/DeclaredOverloadedRecordFields/NoMonoRecordFields add a flag `-XNoMonoRecordFields`] to suppress the record selectors. Just as `-XOverloadedRecordFields` applies to a client module, and generates `Has` instances for that module, so `-XNoMonoRecordFields` in a client module would hide all the record selectors that should otherwise be in scope. The idea is that another record system could use Template Haskell to generate functions in place of selectors, and these would not clash.
     207
     208Since the selectors are hidden by clients (on import) rather than on export, fields can still be mentioned in import and export lists, to control access to them (as discussed in the [wiki:Records/OverloadedRecordFields/Plan#Representationhiding representation hiding] section), and used for record update.
    228209
    229210
     
    248229
    249230  baz = foo S
     231
     232  quux = y
    250233}}}
    251234
     
    255238
    256239When checking `baz`, the constraint `S { x :: gamma }` is generated and rejected, since the `x` from `S` is not in scope.
     240
     241When checking `quux`, the only `y` field in scope is of type `S -> Bool` so that is its type.