Changes between Version 33 and Version 34 of Records/OverloadedRecordFields/Plan


Ignore:
Timestamp:
Aug 29, 2013 2:50:18 PM (21 months ago)
Author:
adamgundry
Comment:

revert misplaced optimism

Legend:

Unmodified
Added
Removed
Modified
  • Records/OverloadedRecordFields/Plan

    v33 v34  
    6363type instance GetResult (T a) "x" = [a] 
    6464 
    65 instance Has (T a) "x" [a] where 
     65instance (b ~ [a]) => Has (T a) "x" b where  
    6666  getField _ (MkT x) = x 
    6767}}} 
    6868 
    69 The equality superclass on the `Has` class is important for type inference. For example, if the constraint `Has (T c) "x" d` is encountered, the instance will not match initially (because `d` is not of the form `[c]`), but a derived equality constraint `d ~ GetResult (T c) "x"` will be generated. This computes to `d ~ [c]`, so the original constraint becomes `Has (T c) "x" [c]`, which means the instance will match. Moreover, the `GetResult` type family ensures that the third parameter is functionally dependent on the first two, which is needed to [#Troubleinparadise avoid ambiguity errors when composing overloaded fields]. 
     69The bare type variable `b` in the instance head is important, so that we get an instance match from the first two parameters only, then the equality constraint `(b ~ [a])` improves `b`. For example, if the constraint `Has (T c) "x" d` is encountered during type inference, the instance will match and generate the constraints `(a ~ c, b ~ d, b ~ [a])`. Moreover, the `GetResult` type family ensures that the third parameter is functionally dependent on the first two, which is needed to [#Troubleinparadise avoid ambiguity errors when composing overloaded fields]. 
    7070 
    7171The reason for using a three-parameter class, rather than just two parameters and a type family, is to support the syntactic sugar. With a two-parameter class we could easily end up inferring types like the following, and it would be hard to reapply the sugar: