An equality type is a type to which PolymorphicEquality can be applied. The Definition and the Basis Library precisely spell out which types are equality types.
-
bool,char,IntInf.int,Int<N>.int,string, andWord<N>.wordare equality types. -
for any
t, botht arrayandt refare equality types. -
if
tis an equality type, thent list, andt vectorare equality types. -
if
t1, …,tnare equality types, thent1 * … * tnand{l1: t1, …, ln: tn}are equality types. -
if
t1, …,tnare equality types andtAdmitsEquality, then(t1, …, tn) tis an equality type.
To check that a type t is an equality type, use the following idiom.
structure S: sig eqtype t end =
struct
type t = ...
end
Notably, exn and real are not equality types. Neither is t1 -> t2, for any t1 and t2.
Equality on arrays and ref cells is by identity, not structure.
For example, ref 13 = ref 13 is false.
On the other hand, equality for lists, strings, and vectors is by
structure, not identity. For example, the following equalities hold.
val _ = [1, 2, 3] = 1 :: [2, 3]
val _ = "foo" = concat ["f", "o", "o"]
val _ = Vector.fromList [1, 2, 3] = Vector.tabulate (3, fn i => i + 1)