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