Changes between Version 3 and Version 4 of Records/OverloadedRecordFields/Implementation


Ignore:
Timestamp:
Jul 16, 2013 11:37:03 AM (2 years ago)
Author:
adamgundry
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Records/OverloadedRecordFields/Implementation

    v3 v4  
    33Here be dragons. This page describes implementation details and progress on the implementation of [wiki:Records/OverloadedRecordFields/Plan the overloaded record fields plan]. Development of the extension is taking place on forks of the [https://github.com/adamgundry/ghc ghc] and [https://github.com/adamgundry/packages-base packages-base] repositories (on branch 'overloaded-record-fields').
    44
    5 == Upcoming changes ==
     5== The basic idea ==
    66
    7 Typechecking a record datatype will still generates record selectors, but their names contain the name of their type. Additionally, a dictionary function (for the `Has` instance) is generated. Thus
     7Typechecking a record datatype still generates record selectors, but their names have a `$sel` prefix and end with the name of their type. Additionally, a dictionary function (for the `Has` instance) is generated. Thus
    88
    99{{{
     
    1414
    1515{{{
    16 $sel_x_T :: T -> Int      -- record selector (used to be called `x`)
     16$sel_x_T :: T -> Int       -- record selector (used to be called `x`)
    1717$df_x_T  :: Has T "x" Int  -- dictionary function (coerced selector)
    1818}}}
    1919
    20 The `dcFields` field of `DataCon.DataCon` (constructor `MkData`) stores a list of `FastString` field names, rather than `Name`s.
     20== The naming of cats ==
    2121
    22 The `AvailTC` constructor of `Avail.AvailInfo` represents a type and its pieces that are in scope. Record fields are stored in a separate list (rather than bundled in with data constructors and class methods), which contains the `FastString` field name, and perhaps the names of the record selector and dictionary function.
     22The `AvailTC Name [Name] [OccName]` constructor of `AvailInfo` represents a type and its pieces that are in scope. Record fields are now stored in a separate list (the third argument). Every field should have a corresponding selector (in the second argument). Since we have the name of the type (the first argument), we can find the selector corresponding to a field. Similar changes are required to the `IEThingWith` constructor of `IE`, which represents a thing that can be imported or exported.
    2323
    24 Similarly, the `RdrName.Parent` type has an extra constructor `FldParent` which stores the field name (and possibly the dfunid). Thus it is easy to tell whether a `RdrName.GlobalRdrElt` (`GRE`) is a field. In this case, the name of the `GRE` is the selector function.
     24The `Parent` type has an extra constructor `FldParent Name OccName` that stores the parent `Name` and the field `OccName`. The `GlobalRdrElt` (`GRE`) for a field stores the selector name directly, and uses the `FldParent` constructor to store the field. Thus a field `foo` of type `T` gives rise to two entries in the `GlobalRdrEnv`:
    2525
    26 The `HsExpr.HsExpr` type has an extra constructor `HsOverloadedRecFld FastString`. When `-XOverloadedRecordFields` is enabled, and the renamer encounters `HsVar "x"` where `x` refers to multiple `GRE`s that are all record fields, it replaces it with `HsOverloadedRecFld "x"`. There needs to be a similar constructor for non-overloaded projections, so that we can pretty-print the field name but store the selector name.
     26{{{
     27$sel_foo_T  |->  GRE $sel_foo_T (ParentIs T) LocalDef
     28foo         |->  GRE $sel_foo_T (FldParent T foo) LocalDef
     29}}}
     30
     31Note that the `OccName` used when adding a GRE to the environment now depends on the parent field: for `FldParent` it is the field rather than the selector name.
     32
     33The `dcFields` field of `DataCon` stores a list of
     34
     35{{{
     36type FieldLabel = (OccName, Name)
     37}}}
     38
     39where the first component is the field and the second is the selector function.
     40
     41== Next steps ==
     42
     43The `HsExpr.HsExpr` type has an extra constructor `HsOverloadedRecFld OccName`. When `-XOverloadedRecordFields` is enabled, and the renamer encounters `HsVar "x"` where `x` refers to multiple `GRE`s that are all record fields, it replaces it with `HsOverloadedRecFld "x"`. There needs to be a similar constructor for non-overloaded projections, so that we can pretty-print the field name but store the selector name.
    2744
    2845When the typechecker sees `HsOverloadedRecFld x` it emits a wanted constraint `Has alpha x beta` and returns type `alpha -> beta` where `alpha` and `beta` are fresh unification variables.