| 1 |
= PolymorphicEquality |
| 2 |
:toc: |
| 3 |
|
| 4 |
Polymorphic equality is a built-in function in |
| 5 |
<<StandardML#,Standard ML>> that compares two values of the same type |
| 6 |
for equality. It is specified as |
| 7 |
|
| 8 |
[source,sml] |
| 9 |
---- |
| 10 |
val = : ''a * ''a -> bool |
| 11 |
---- |
| 12 |
|
| 13 |
The `''a` in the specification are |
| 14 |
<<EqualityTypeVariable#,equality type variables>>, and indicate that |
| 15 |
polymorphic equality can only be applied to values of an |
| 16 |
<<EqualityType#,equality type>>. It is not allowed in SML to rebind |
| 17 |
`=`, so a programmer is guaranteed that `=` always denotes polymorphic |
| 18 |
equality. |
| 19 |
|
| 20 |
|
| 21 |
== Equality of ground types |
| 22 |
|
| 23 |
Ground types like `char`, `int`, and `word` may be compared (to values |
| 24 |
of the same type). For example, `13 = 14` is type correct and yields |
| 25 |
`false`. |
| 26 |
|
| 27 |
|
| 28 |
== Equality of reals |
| 29 |
|
| 30 |
The one ground type that can not be compared is `real`. So, |
| 31 |
`13.0 = 14.0` is not type correct. One can use `Real.==` to compare |
| 32 |
reals for equality, but beware that this has different algebraic |
| 33 |
properties than polymorphic equality. |
| 34 |
|
| 35 |
See http://standardml.org/Basis/real.html for a discussion of why |
| 36 |
`real` is not an equality type. |
| 37 |
|
| 38 |
|
| 39 |
== Equality of functions |
| 40 |
|
| 41 |
Comparison of functions is not allowed. |
| 42 |
|
| 43 |
|
| 44 |
== Equality of immutable types |
| 45 |
|
| 46 |
Polymorphic equality can be used on <<Immutable#,immutable>> values like |
| 47 |
tuples, records, lists, and vectors. For example, |
| 48 |
|
| 49 |
---- |
| 50 |
(1, 2, 3) = (4, 5, 6) |
| 51 |
---- |
| 52 |
|
| 53 |
is a type-correct expression yielding `false`, while |
| 54 |
|
| 55 |
---- |
| 56 |
[1, 2, 3] = [1, 2, 3] |
| 57 |
---- |
| 58 |
|
| 59 |
is type correct and yields `true`. |
| 60 |
|
| 61 |
Equality on immutable values is computed by structure, which means |
| 62 |
that values are compared by recursively descending the data structure |
| 63 |
until ground types are reached, at which point the ground types are |
| 64 |
compared with primitive equality tests (like comparison of |
| 65 |
characters). So, the expression |
| 66 |
|
| 67 |
---- |
| 68 |
[1, 2, 3] = [1, 1 + 1, 1 + 1 + 1] |
| 69 |
---- |
| 70 |
|
| 71 |
is guaranteed to yield `true`, even though the lists may occupy |
| 72 |
different locations in memory. |
| 73 |
|
| 74 |
Because of structural equality, immutable values can only be compared |
| 75 |
if their components can be compared. For example, `[1, 2, 3]` can be |
| 76 |
compared, but `[1.0, 2.0, 3.0]` can not. The SML type system uses |
| 77 |
<<EqualityType#,equality types>> to ensure that structural equality is |
| 78 |
only applied to valid values. |
| 79 |
|
| 80 |
|
| 81 |
== Equality of mutable values |
| 82 |
|
| 83 |
In contrast to immutable values, polymorphic equality of |
| 84 |
<<Mutable#,mutable>> values (like ref cells and arrays) is performed by |
| 85 |
pointer comparison, not by structure. So, the expression |
| 86 |
|
| 87 |
---- |
| 88 |
ref 13 = ref 13 |
| 89 |
---- |
| 90 |
|
| 91 |
is guaranteed to yield `false`, even though the ref cells hold the |
| 92 |
same contents. |
| 93 |
|
| 94 |
Because equality of mutable values is not structural, arrays and refs |
| 95 |
can be compared _even if their components are not equality types_. |
| 96 |
Hence, the following expression is type correct (and yields true). |
| 97 |
|
| 98 |
[source,sml] |
| 99 |
---- |
| 100 |
let |
| 101 |
val r = ref 13.0 |
| 102 |
in |
| 103 |
r = r |
| 104 |
end |
| 105 |
---- |
| 106 |
|
| 107 |
|
| 108 |
== Equality of datatypes |
| 109 |
|
| 110 |
Polymorphic equality of datatypes is structural. Two values of the |
| 111 |
same datatype are equal if they are of the same <<Variant#,variant>> and |
| 112 |
if the <<Variant#,variant>>’s arguments are equal (recursively). So, |
| 113 |
with the datatype |
| 114 |
|
| 115 |
[source,sml] |
| 116 |
---- |
| 117 |
datatype t = A | B of t |
| 118 |
---- |
| 119 |
|
| 120 |
then `B (B A) = B A` is type correct and yields `false`, while `A = A` |
| 121 |
and `B A = B A` yield `true`. |
| 122 |
|
| 123 |
As polymorphic equality descends two values to compare them, it uses |
| 124 |
pointer equality whenever it reaches a mutable value. So, with the |
| 125 |
datatype |
| 126 |
|
| 127 |
[source,sml] |
| 128 |
---- |
| 129 |
datatype t = A of int ref | ... |
| 130 |
---- |
| 131 |
|
| 132 |
then `A (ref 13) = A (ref 13)` is type correct and yields `false`, |
| 133 |
because the pointer equality on the two ref cells yields `false`. |
| 134 |
|
| 135 |
One weakness of the SML type system is that datatypes do not inherit |
| 136 |
the special property of the `ref` and `array` type constructors that |
| 137 |
allows them to be compared regardless of their component type. For |
| 138 |
example, after declaring |
| 139 |
|
| 140 |
[source,sml] |
| 141 |
---- |
| 142 |
datatype 'a t = A of 'a ref |
| 143 |
---- |
| 144 |
|
| 145 |
one might expect to be able to compare two values of type `real t`, |
| 146 |
because pointer comparison on a ref cell would suffice. |
| 147 |
Unfortunately, the type system can only express that a user-defined |
| 148 |
datatype <<AdmitsEquality#,admits equality>> or not. In this case, `t` |
| 149 |
admits equality, which means that `int t` can be compared but that |
| 150 |
`real t` can not. We can confirm this with the program |
| 151 |
|
| 152 |
[source,sml] |
| 153 |
---- |
| 154 |
datatype 'a t = A of 'a ref |
| 155 |
fun f (x: real t, y: real t) = x = y |
| 156 |
---- |
| 157 |
|
| 158 |
on which MLton reports the following error. |
| 159 |
|
| 160 |
---- |
| 161 |
Error: z.sml 2.32-2.36. |
| 162 |
Function applied to incorrect argument. |
| 163 |
expects: [<equality>] t * [<equality>] t |
| 164 |
but got: [real] t * [real] t |
| 165 |
in: = (x, y) |
| 166 |
---- |
| 167 |
|
| 168 |
|
| 169 |
== Implementation |
| 170 |
|
| 171 |
Polymorphic equality is implemented by recursively descending the two |
| 172 |
values being compared, stopping as soon as they are determined to be |
| 173 |
unequal, or exploring the entire values to determine that they are |
| 174 |
equal. Hence, polymorphic equality can take time proportional to the |
| 175 |
size of the smaller value. |
| 176 |
|
| 177 |
MLton uses some optimizations to improve performance. |
| 178 |
|
| 179 |
* When computing structural equality, first do a pointer comparison. |
| 180 |
If the comparison yields `true`, then stop and return `true`, since |
| 181 |
the structural comparison is guaranteed to do so. If the pointer |
| 182 |
comparison fails, then recursively descend the values. |
| 183 |
|
| 184 |
* If a datatype is an enum (e.g. `datatype t = A | B | C`), then a |
| 185 |
single comparison suffices to compare values of the datatype. No case |
| 186 |
dispatch is required to determine whether the two values are of the |
| 187 |
same <<Variant#,variant>>. |
| 188 |
|
| 189 |
* When comparing a known constant non-value-carrying |
| 190 |
<<Variant#,variant>>, use a single comparison. For example, the |
| 191 |
following code will compile into a single comparison for `A = x`. |
| 192 |
+ |
| 193 |
[source,sml] |
| 194 |
---- |
| 195 |
datatype t = A | B | C of ... |
| 196 |
fun f x = ... if A = x then ... |
| 197 |
---- |
| 198 |
|
| 199 |
* When comparing a small constant `IntInf.int` to another |
| 200 |
`IntInf.int`, use a single comparison against the constant. No case |
| 201 |
dispatch is required. |
| 202 |
|
| 203 |
|
| 204 |
== Also see |
| 205 |
|
| 206 |
* <<AdmitsEquality#>> |
| 207 |
* <<EqualityType#>> |
| 208 |
* <<EqualityTypeVariable#>> |