| 1 |
= EqualityTypeVariable |
| 2 |
|
| 3 |
An equality type variable is a type variable that starts with two or |
| 4 |
more primes, as in `''a` or `''b`. The canonical use of equality type |
| 5 |
variables is in specifying the type of the <<PolymorphicEquality#>> |
| 6 |
function, which is `''a * ''a \-> bool`. Equality type variables |
| 7 |
ensure that polymorphic equality is only used on |
| 8 |
<<EqualityType#,equality types>>, by requiring that at every use of a |
| 9 |
polymorphic value, equality type variables are instantiated by |
| 10 |
equality types. |
| 11 |
|
| 12 |
For example, the following program is type correct because polymorphic |
| 13 |
equality is applied to variables of type `''a`. |
| 14 |
|
| 15 |
[source,sml] |
| 16 |
---- |
| 17 |
fun f (x: ''a, y: ''a): bool = x = y |
| 18 |
---- |
| 19 |
|
| 20 |
On the other hand, the following program is not type correct, because |
| 21 |
polymorphic equality is applied to variables of type `'a`, which is |
| 22 |
not an equality type. |
| 23 |
|
| 24 |
[source,sml] |
| 25 |
---- |
| 26 |
fun f (x: 'a, y: 'a): bool = x = y |
| 27 |
---- |
| 28 |
|
| 29 |
MLton reports the following error, indicating that polymorphic |
| 30 |
equality expects equality types, but didn't get them. |
| 31 |
|
| 32 |
---- |
| 33 |
Error: z.sml 1.30-1.34. |
| 34 |
Function applied to incorrect argument. |
| 35 |
expects: [<equality>] * [<equality>] |
| 36 |
but got: ['a] * ['a] |
| 37 |
in: = (x, y) |
| 38 |
---- |
| 39 |
|
| 40 |
As an example of using such a function that requires equality types, |
| 41 |
suppose that `f` has polymorphic type `''a \-> unit`. Then, `f 13` is |
| 42 |
type correct because `int` is an equality type. On the other hand, |
| 43 |
`f 13.0` and `f (fn x \=> x)` are not type correct, because `real` and |
| 44 |
arrow types are not equality types. We can test these facts with the |
| 45 |
following short programs. First, we verify that such an `f` can be |
| 46 |
applied to integers. |
| 47 |
|
| 48 |
[source,sml] |
| 49 |
---- |
| 50 |
functor Ok (val f: ''a -> unit): sig end = |
| 51 |
struct |
| 52 |
val () = f 13 |
| 53 |
val () = f 14 |
| 54 |
end |
| 55 |
---- |
| 56 |
|
| 57 |
We can do better, and verify that such an `f` can be applied to |
| 58 |
any integer. |
| 59 |
|
| 60 |
[source,sml] |
| 61 |
---- |
| 62 |
functor Ok (val f: ''a -> unit): sig end = |
| 63 |
struct |
| 64 |
fun g (x: int) = f x |
| 65 |
end |
| 66 |
---- |
| 67 |
|
| 68 |
Even better, we don't need to introduce a dummy function name; we can |
| 69 |
use a type constraint. |
| 70 |
|
| 71 |
[source,sml] |
| 72 |
---- |
| 73 |
functor Ok (val f: ''a -> unit): sig end = |
| 74 |
struct |
| 75 |
val _ = f: int -> unit |
| 76 |
end |
| 77 |
---- |
| 78 |
|
| 79 |
Even better, we can use a signature constraint. |
| 80 |
|
| 81 |
[source,sml] |
| 82 |
---- |
| 83 |
functor Ok (S: sig val f: ''a -> unit end): |
| 84 |
sig val f: int -> unit end = S |
| 85 |
---- |
| 86 |
|
| 87 |
This functor concisely verifies that a function of polymorphic type |
| 88 |
`''a \-> unit` can be safely used as a function of type `int \-> unit`. |
| 89 |
|
| 90 |
As above, we can verify that such an `f` can not be used at |
| 91 |
non-equality types. |
| 92 |
|
| 93 |
[source,sml] |
| 94 |
---- |
| 95 |
functor Bad (S: sig val f: ''a -> unit end): |
| 96 |
sig val f: real -> unit end = S |
| 97 |
|
| 98 |
functor Bad (S: sig val f: ''a -> unit end): |
| 99 |
sig val f: ('a -> 'a) -> unit end = S |
| 100 |
---- |
| 101 |
|
| 102 |
MLton reports the following errors. |
| 103 |
|
| 104 |
---- |
| 105 |
Error: z.sml 2.4-2.30. |
| 106 |
Variable in structure disagrees with signature (type): f. |
| 107 |
structure: val f: [<equality>] -> _ |
| 108 |
defn at: z.sml 1.25-1.25 |
| 109 |
signature: val f: [real] -> _ |
| 110 |
spec at: z.sml 2.12-2.12 |
| 111 |
Error: z.sml 5.4-5.36. |
| 112 |
Variable in structure disagrees with signature (type): f. |
| 113 |
structure: val f: [<equality>] -> _ |
| 114 |
defn at: z.sml 4.25-4.25 |
| 115 |
signature: val f: [_ -> _] -> _ |
| 116 |
spec at: z.sml 5.12-5.12 |
| 117 |
---- |
| 118 |
|
| 119 |
|
| 120 |
== Equality type variables in type and datatype declarations |
| 121 |
|
| 122 |
Equality type variables can be used in type and datatype declarations; |
| 123 |
however they play no special role. For example, |
| 124 |
|
| 125 |
[source,sml] |
| 126 |
---- |
| 127 |
type 'a t = 'a * int |
| 128 |
---- |
| 129 |
|
| 130 |
is completely identical to |
| 131 |
|
| 132 |
[source,sml] |
| 133 |
---- |
| 134 |
type ''a t = ''a * int |
| 135 |
---- |
| 136 |
|
| 137 |
In particular, such a definition does _not_ require that `t` only be |
| 138 |
applied to equality types. |
| 139 |
|
| 140 |
Similarly, |
| 141 |
|
| 142 |
[source,sml] |
| 143 |
---- |
| 144 |
datatype 'a t = A | B of 'a |
| 145 |
---- |
| 146 |
|
| 147 |
is completely identical to |
| 148 |
|
| 149 |
[source,sml] |
| 150 |
---- |
| 151 |
datatype ''a t = A | B of ''a |
| 152 |
---- |