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


Ignore:
Timestamp:
Jul 1, 2013 7:39:43 AM (20 months ago)
Author:
adamgundry
Comment:

leave dot alone for now

Legend:

Unmodified
Added
Removed
Modified
  • Records/OverloadedRecordFields/Plan

    v12 v13  
    1616are declared in the same module, there is no way to determine which type an occurrence of the `personId` record selector refers to. A common workaround is to use a unique prefix for each record type, but this leads to less clear code and obfuscates relationships between fields of different records. Qualified names can be used to distinguish record selectors from different modules, but using one module per record is often impractical. 
    1717 
    18 Instead, we want to be able to write postfix polymorphic record projections, so that `e.personId` resolves the ambiguity using the type of `e`. In general, this requires a new form of constraint `r { x :: t }` stating that type `r` has a field `x` of type `t`. For example, the following declaration should be accepted: 
     18Instead, we want to be able to write polymorphic record projections, so that the ambiguous identifier `personId` is resolved using the type of `e`. In general, this requires a new form of constraint `r { x :: t }` stating that type `r` has a field `x` of type `t`. For example, the following declaration should be accepted: 
    1919 
    2020{{{ 
    2121getPersonId :: r { personId :: Int } => r -> Int 
    22 getPersonId e = e.personId 
     22getPersonId e = personId e 
    2323}}} 
    2424 
     
    2828== Design == 
    2929 
    30 In the sequel, we will describe the `-XOverloadedRecordFields` extension, which permits multiple field declarations with the same label, introduces new record field constraints and a new syntax for record projection. 
     30In the sequel, we will describe the `-XOverloadedRecordFields` extension, which permits multiple field declarations with the same label and introduces new record field constraints. 
     31 
     32Previous versions of this proposal suggested changing the lexical syntax so that record projections could be written postfix, immediately following a dot. For example, `e.personId` would be roughly equivalent to `personId e`. This would be a breaking change (when the extension was enabled) as composition would need spaces around the dot operator. However, it would mean that the field name would not have to be in scope, allowing better library separation. For example, `e.personId` would be valid even if no `personId` fields were in scope. 
     33 
     34In the light of feedback, we propose '''no changes to dot syntax''' for the time being. In the future, we could add a separate extension to treat [wiki:Records/DeclaredOverloadedRecordFields/DotPostfix dot as postfix function application]. Note that the [http://hackage.haskell.org/package/lens lens] library encourages the use of dot with no spaces, as composition is used to chain lenses. 
    3135 
    3236 
     
    6064=== Projections: the dot operator ===  
    6165 
    62 Record field constraints are introduced by projections, which are written using the dot operator with no space following it.  That is, if `e :: r` then `e.x :: r { x :: t } => t`.  The right section `(.x) :: a { x :: b } => a -> b` is available but the left section `(e.)` is not (what would its type be?). 
    63  
    64 The composition operator must be written with spaces on both sides, for consistency. This will break old code, but only when the `-XOverloadedRecordFields` extension is enabled. There is no ambiguity, and dot notation is already space-aware: `M.x` is a qualified name whereas `M . x` is the composition of a data constructor `M` with a function `x`.  Similarly `e.x` can mean record projection, distinct from `e . x`. Note that dot (for qualified names or record projection) binds more tightly than function application, so `f e.x` means the same as `f (e.x)`. Parentheses can be used to write `(f e).x`. 
     66Record 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. 
    6567 
    6668 
     
    119121because the type of `v` is only determined later, by constraint solving. 
    120122 
    121 Annoyingly, nested updates will require some annotations. In the following example, the outer update need not be annotated (since `v` is a variable that is explicitly given a type by the context) but the inner update must be (since `v.x` is not a variable): 
     123Annoyingly, nested updates will require some annotations. In the following example, the outer update need not be annotated (since `v` is a variable that is explicitly given a type by the context) but the inner update must be (since `x v` is not a variable): 
    122124{{{ 
    123125  f :: T Int -> T Int 
    124   f v = v { x = v.x { y = 6 } } 
     126  f v = v { x = (x v){ y = 6 } } 
    125127}}} 
    126128 
     
    155157{{{ 
    156158x :: Has r "x" t => r -> t 
    157 x e = e.x 
     159x = getFld 
    158160}}} 
    159161 
     
    166168 
    167169{{{ 
    168 foo = \ e -> e.x 
     170foo = \ e -> x e 
    169171}}} 
    170172 
     
    177179}}} 
    178180 
    179 Inferring the type of `foo = \ e -> e.x` 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. 
     181Inferring 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. 
    180182 
    181183 
     
    206208}}} 
    207209 
    208 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 `e.x`, but overloaded record fields prevent this. Traditional monomorphic selector functions are likely to be needed in this case. 
     210but 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. 
    209211 
    210212 
     
    240242  import M 
    241243 
    242   foo e = e.x 
     244  foo e = x e 
    243245  
    244246  bar :: Bool