Commit b0626b63 by Simon Peyton Jones

### Document promotion of existential data types

`Thanks to Richard Eisenberg for writing this.`
parent 57d67983
 ... @@ -5530,8 +5530,9 @@ The following restrictions apply to promotion: ... @@ -5530,8 +5530,9 @@ The following restrictions apply to promotion: higher-kinded datatypes such as data Fix f = In (f (Fix f)), higher-kinded datatypes such as data Fix f = In (f (Fix f)), or datatypes whose kinds involve promoted types such as or datatypes whose kinds involve promoted types such as Vec :: * -> Nat -> *. Vec :: * -> Nat -> *. We do not promote datatypes whose constructors are kind We do not promote data constructors that are kind polymorphic, involve constraints, or use existential quantification. polymorphic, involve constraints, mention type or data families, or involve types that are not promotable. We do not promote data family instances (). We do not promote data family instances (). ... @@ -5586,7 +5587,7 @@ Note that this requires . ... @@ -5586,7 +5587,7 @@ Note that this requires . Promoting existential data constructors Note that we do promote existential data constructors that are otherwise suitable. For example, consider the following: data Ex :: * where MkEx :: forall a. a -> Ex Both the type Ex and the data constructor MkEx get promoted, with the polymorphic kind 'MkEx :: forall k. k -> Ex. Somewhat surprisingly, you can write a type family to extract the member of a type-level existential: type family UnEx (ex :: Ex) :: k type instance UnEx (MkEx x) = x At first blush, UnEx seems poorly-kinded. The return kind k is not mentioned in the arguments, and thus it would seem that an instance would have to return a member of k for any k. However, this is not the case. The type family UnEx is a kind-indexed type family. The return kind k is an implicit parameter to UnEx. The elaborated definitions are as follows: type family UnEx (k :: BOX) (ex :: Ex) :: k type instance UnEx k (MkEx k x) = x Thus, the instance triggers only when the implicit parameter to UnEx matches the implicit parameter to MkEx. Because k is actually a parameter to UnEx, the kind is not escaping the existential, and the above code is valid. See also Trac #7347. ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!