41 | | * For `x3`, we'd have to map over `T`, thus `mapT MkAge x3`. But what if `mapT` didn't exist? We'd have to make it. And not all data types have maps. `S` is a harder one: you could only map over S-values if `m` was a functor. There's a lot of discussion abou this on #2110. |
| 41 | * For `x3`, we'd have to map over `T`, thus `mapT MkAge x3`. But what if `mapT` didn't exist? We'd have to make it. And not all data types have maps. `S` is a harder one: you could only map over S-values if `m` was a functor. There's a lot of discussion about this on #2110. |
| 42 | |
| 43 | == Goal == |
| 44 | |
| 45 | In summary: The programmer expects zero-cost conversions between a newtypes ''N'' and the type ''T'' it is based on. We want to allow the programmer to have zero-cost conversions between ''C N'' and ''C T''. Requirements: |
| 46 | * It should be sound, i.e. have an implementation in Core with existing coercions, without new coercions or `unsafeCoerce`. |
| 47 | * It should respect module boundaries, i.e. not allow the user to create a function which he could not write using plain Haskell (and non-zero cost). |
| 48 | * (desirable:) It should be exportable and composable, i.e. the module of ''N'' should be able to specify that a conversion between ''N'' and ''T'' is allowed (or not), and the module of ''C'' should be able to specify if a coercion can be lifted (or not), and if both allow it, the user should be able to combine that even if the modules of ''N'' and ''T'' are independent. |
| 49 | * (desirable:) It should be possible to use such a coercion in one module in internal code, but not export it. |
| 50 | * (desirable:) It should be possible to add REWRITE rules that turn, for example `map N` into the zero-cost conversion from ''C T'' to ''C N''. |
| 51 | |
| 52 | == Use cases == |
| 53 | |
| 54 | To clarify these requirements, here some benchmarks; feel free to expand if you have further needs. |
| 55 | |
| 56 | * Allow the user to define a function of type `C N -> C T` with a zero-cost model. |
| 57 | * Allow the author of `Data.Set` prevent a coercion `Set N -> Set T` (as it would break internal invariants). |
| 58 | * Allow the author of a escaping-ensuring `newtype HTML = HTML String` to use the coercion `[String] -> [HTML]` internally, but prevent the user of the library from doing that. |
| 59 | |
| 191 | == Proposal 3 == |
| 192 | |
| 193 | This is a write-down of a discussion between me (nomeata) and SPJ at TLCA 2013, but my memory might have tricked me and I might have filled in some bits myself, so beware. |
| 194 | |
| 195 | Assume there is a module provided by GHC with these definitions |
| 196 | {{{ |
| 197 | data NT a b -- abstract |
| 198 | coerce :: NT a b -> a -> b |
| 199 | refl :: NT a a |
| 200 | sym :: NT a b -> NT b a |
| 201 | trans :: NT a b -> NT b c -> NT a c |
| 202 | }}} |
| 203 | and the intention that `NT a b` is a witness that `a` and `b` have the same representation and that `coerce n` has zero runtime cost. |
| 204 | |
| 205 | Besides the trivial `refl` values, values of type `NT` cannot be created by the user, but only by a special `deriving` statement giving the type of a function that creates `NT` values, but without a definition, e.g. |
| 206 | {{{ |
| 207 | deriving nt :: NT N T |
| 208 | deriving list :: NT a b -> NT [a] [b] |
| 209 | }}} |
| 210 | |
| 211 | The compiler either rejects the declaration or creates an implementation automatically. It should only create an implementation if a non-zero-cost-implementation could be written by the user instead. (I expect that this means that the constructors of the return type are in scope.) |
| 212 | |
| 213 | This solves the abstraction problem for `Data.Map`: The library author only exports `NT a b -> NT (Map k a) (Map k b)`, but not NT a b -> NT (Map a v) (Map b v)`. |
| 214 | |
| 215 | Of course with an `NT` data type, it is possible to define this type class, e.g. |
| 216 | {{{ |
| 217 | class NT' a b where |
| 218 | theNT :: NT a b |
| 219 | coerce' :: NT' a b => a -> b |
| 220 | coerce' = coerce theNT |
| 221 | }}} |
| 222 | and use it where more convenient than the function-based variant. |
| 223 | |
| 224 | As to the implementation, I quite naively expect that the definition of `NT` and related function in terms of Core to be straight-forward (`newtype NT a b = a ~_R b`). The code that does the `deriving` is maybe non-trivial, as it has to build the term from available newtype axioms (where the constructor is in scope), coercions given as parameters and the application of type constructors to coercions (again, where the data constructors are in scope). |
| 225 | |
| 226 | |