| 1 |
= EqualityType |
| 2 |
|
| 3 |
An equality type is a type to which <<PolymorphicEquality#>> can be |
| 4 |
applied. The <<DefinitionOfStandardML#,Definition>> and the |
| 5 |
<<BasisLibrary#,Basis Library>> precisely spell out which types are |
| 6 |
equality types. |
| 7 |
|
| 8 |
* `bool`, `char`, `IntInf.int`, ``Int__<N>__.int``, `string`, and ``Word__<N>__.word`` are equality types. |
| 9 |
|
| 10 |
* for any `t`, both `t array` and `t ref` are equality types. |
| 11 |
|
| 12 |
* if `t` is an equality type, then `t list`, and `t vector` are equality types. |
| 13 |
|
| 14 |
* if `t1`, ..., `tn` are equality types, then `t1 * ... * tn` and `{l1: t1, ..., ln: tn}` are equality types. |
| 15 |
|
| 16 |
* if `t1`, ..., `tn` are equality types and `t` <<AdmitsEquality#>>, then `(t1, ..., tn) t` is an equality type. |
| 17 |
|
| 18 |
To check that a type t is an equality type, use the following idiom. |
| 19 |
[source,sml] |
| 20 |
---- |
| 21 |
structure S: sig eqtype t end = |
| 22 |
struct |
| 23 |
type t = ... |
| 24 |
end |
| 25 |
---- |
| 26 |
|
| 27 |
Notably, `exn` and `real` are not equality types. Neither is `t1 \-> t2`, for any `t1` and `t2`. |
| 28 |
|
| 29 |
Equality on arrays and ref cells is by identity, not structure. |
| 30 |
For example, `ref 13 = ref 13` is `false`. |
| 31 |
On the other hand, equality for lists, strings, and vectors is by |
| 32 |
structure, not identity. For example, the following equalities hold. |
| 33 |
|
| 34 |
[source,sml] |
| 35 |
---- |
| 36 |
val _ = [1, 2, 3] = 1 :: [2, 3] |
| 37 |
val _ = "foo" = concat ["f", "o", "o"] |
| 38 |
val _ = Vector.fromList [1, 2, 3] = Vector.tabulate (3, fn i => i + 1) |
| 39 |
---- |