Changes between Version 11 and Version 12 of Records/TypePunningDeclaredOverloadedRecordFields


Ignore:
Timestamp:
Mar 25, 2012 10:22:00 PM (3 years ago)
Author:
AntC
Comment:

clarify approach for HR fields

Legend:

Unmodified
Added
Removed
Modified
  • Records/TypePunningDeclaredOverloadedRecordFields

    v11 v12  
    8686 * The Type must be declared once (within the scope), and is then under regular name control.[[BR]](Probably you're doing this already for critical fields to share.) 
    8787 * The type functions are not associated types, because: 
    88     * `GetResult` for shared fields depends only on the Field's type (per Customer_id above); 
     88    * `GetResult` for shared fields depends only on the Field's type (per `Customer_id` above); 
    8989    * `SetResult` for non-parametric record types continues the same record type. 
    9090 * The field selector function also must be declared once, defined punning on the field's type.[[BR]](See below for syntactic sugar to declare these.) 
     
    123123'''Technical capabilities''' and limitations for the `Has` class: 
    124124 * Monomorphic fields can be `get` and `set`. 
    125  * Parametric polymorphic fields can be applied in polymorphic contexts, and can be `set` including changing the type of the record.[[BR]](This uses the SetResult type function.)[[BR]]To do: provide example with desugarring. 
     125 * Parametric polymorphic fields can be applied in polymorphic contexts, and can be `set` including changing the type of the record.[[BR]](This uses the `SetResult` type function.)[[BR]]To do: provide example with desugarring. 
    126126 * Multiple fields can be updated in a single expression (using familiar H98 syntax), but this desugars to nested updates, which is inefficient. 
    127127 * Pattern matching and record creation using the data constructor prefixed to { ... } work as per H98 (using `DisambiguateRecordFields` and friends). 
     
    135135 
    136136    newtype Rev = Rev (forall a. [a] -> [a]) 
    137     rev :: HR -> Rev -> (forall a. [a] -> [a])                          -- generated selector is monomorphic 
    138     rev r = get r (undefined :: Rev) 
     137    rev :: HR -> Rev -> (forall a. [a] -> [a])                          -- generated selector is polymorphic 
     138    rev r = let (Rev fn) = get r (undefined :: Rev) in fn               -- unwrap from the newtype 
    139139 
    140140    instance Has HR Rev                         where                   -- Has instance generated 
    141         get HR{ rev = (Rev x) } = x                                     -- looks OK, but see GetResult 
    142         set x HR{ .. }    = HR{ rev = x, .. }                           -- note x is already wrapped 
     141        get HR{ rev = (Rev x) } = Rev x                                 -- can't unwrap here, 'cos can't spec Poly 
     142        set (Rev x) HR{ .. }    = HR{ rev = Rev x, .. }                 -- note x is already wrapped 
    143143 
    144     type instance GetResult HR Rev     = (forall a. [a] -> [a])         -- no can do! not allowed forall's on RHS 
    145                                                                         -- (and can't do equality constraints on type instances) 
    146     {- ??can we do better -- perhaps an eq constraint on the Has instance: 
    147                  (GetResult HR Rev ~ ([a] -> [a])) => Has ... 
    148        plus a non-commital result for getResult 
    149     -} 
     144    type instance GetResult r Rev     = Rev 
    150145 
    151 {- instead, do explicit newtype wrapping for higher-rank types:   -} 
    152146 
    153     newtype Rev = Rev (forall a. [a] -> [a])        deriving (Has) 
    154     newtype OrdRev = OrdRev (Ord a => [a] -> [a])   deriving (Has)      -- class constrained 
     147}}} 
    155148 
    156     data HRW = HRW{ rev :: Rev, ordRev :: OrdRev } sharing (Rev, OrdRev) 
    157 }}} 
    158  * Now we can now apply the wrapped function polymorphically (after unwrapping within the user code. 
     149 * Now we can now apply the wrapped function polymorphically (after unwrapping within the user code). 
    159150 
    160151