Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / EqualityType.adoc
CommitLineData
7f918cf1
CE
1EqualityType
2============
3
4An equality type is a type to which <:PolymorphicEquality:> can be
5applied. The <:DefinitionOfStandardML:Definition> and the
6<:BasisLibrary:Basis Library> precisely spell out which types are
7equality 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
19To check that a type t is an equality type, use the following idiom.
20[source,sml]
21----
22structure S: sig eqtype t end =
23 struct
24 type t = ...
25 end
26----
27
28Notably, `exn` and `real` are not equality types. Neither is `t1 -> t2`, for any `t1` and `t2`.
29
30Equality on arrays and ref cells is by identity, not structure.
31For example, `ref 13 = ref 13` is `false`.
32On the other hand, equality for lists, strings, and vectors is by
33structure, not identity. For example, the following equalities hold.
34
35[source,sml]
36----
37val _ = [1, 2, 3] = 1 :: [2, 3]
38val _ = "foo" = concat ["f", "o", "o"]
39val _ = Vector.fromList [1, 2, 3] = Vector.tabulate (3, fn i => i + 1)
40----