Changes between Version 27 and Version 28 of Records/DeclaredOverloadedRecordFields


Ignore:
Timestamp:
Mar 8, 2012 9:30:34 AM (3 years ago)
Author:
AntC
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Records/DeclaredOverloadedRecordFields

    v27 v28  
    33== Thumbnail Sketch ==
    44
    5 This proposal is addressing the narrow issue of '''namespacing for record field names''' by allowing more than one record in the same module to share a field name. Specifically the record field name is overloaded so that:
     5This proposal is addressing the narrow issue of '''namespacing for record field names''' by allowing more than one record in the same module to share a field name. Specifically each field name is overloaded so that:
    66 * __Within the same module__, many record types can be declared to share the field name.
    77 * The field name can be exported so that records __in other modules__ can share it.
     
    99
    1010The export/import is under usual H98 namespace and module/qualification control, so that for the record type in an importing module:
    11  * Some fields are both `get`able and `set`able;
     11 * Some fields are both readable and updatable;
    1212 * Some are read-only;
    1313 * Some are completely hidden.
     
    1919 * Field selectors are ordinary functions named for the field (but overloaded rather than H98's monomorphic), so field selection is regular function application. (There is no need for syntactically-based disambiguation at point of use.)
    2020
    21 === Implementation: the `Has` class, and methods `get` and `set` ===
    22 
    23 Record declarations generate a `Has` instance for each record type/field combination. As well as type arguments for the record and field, there is a third argument for the field's type, which is set at the instance level using equality constraints in a functional-dependencies style. Here is the `Has` class (`r` is the record, `fld` is the proxy type for the field, `t` is the fields type), an example declaration, its `Has` instance, and examples of use:
     21=== Implementation: the `Has` class, with methods `get` and `set` ===
     22
     23Record declarations generate a `Has` instance for each record type/field combination. As well as type arguments for the record and field, there is a third argument for the field's resulting type. This is set at the instance level using equality constraints in a functional-dependencies style. Here is the `Has` class (`r` is the record, `fld` is the proxy type for the field, `t` is the fields type), with an example record declaration, its `Has` instance, and examples of use:
    2424{{{
    2525    class Has r fld t                                             where
     
    2828
    2929    data Customer = Cust{ customer_id :: Int, ... }                     -- declaration syntax same as H98
     30
    3031    instance (t ~ Int) => Has Customer Proxy_customer_id t        where -- Has instance generated, with ~ constraint
    3132        get Cust{ customer_id } _ = customer_id                         -- DisambiguateRecordFields pattern
     
    3435    myCust :: Customer                                                  -- usual record decl
    3536    ... myCust{ customer_id = 27 }                                      -- polymorphic record update
    36     ... (customer_id myCust) ...                                        -- field selection is func apply
     37    ... (customer_id myCust) ...                                        -- field selection is func apply, or:
    3738    ... myCust.customer_id ...                                          -- dot notation is sugar for reverse func apply
    3839}}}
     
    4041Note that the''' `Has` mechanism''' uses a Proxy as the type 'peg' for a field (this is the wildcard argument to `get` and `set`):
    4142 * The Proxy must be declared once, and is then under regular name control.
    42  * The field selector function also must be declared once, using the Proxy.
    43 
    44 {{{
    45     -- fieldLabel customer_id :: r -> Int                               -- new declaration, desugars to Proxy and func
     43 * The field selector function also must be declared once, defined using the Proxy.
     44
     45{{{
     46    fieldLabel customer_id :: r -> Int                                  -- new declaration, desugars to Proxy and func:
    4647    data Proxy_customer_id                                              -- phantom
    4748    customer_id :: r{ customer_id :: Int } => r -> Int                  -- r{ ... } is sugar for Has constraint
     
    6667 * `Has` uses type family functions to manage type-changing update, which adds complexity -- see Implementer's view.
    6768 * Multiple fields can be updated in a single expression (using familiar H98 syntax), but this desugars to nested updates, which is inefficient.
    68  * Pattern matching and record creation using data constructor prefix to { ... } work as per H98 (using DisambiguateRecordFields and friends).
     69 * Pattern matching and record creation using the data constructor prefixed to { ... } work as per H98 (using DisambiguateRecordFields and friends).
    6970
    7071.