Debian Codegraph @

doc/guide/src/AdmitsEquality.adoc

raw
1 = AdmitsEquality
2
3 A <<TypeConstructor#>> admits equality if whenever it is applied to
4 equality types, the result is an <<EqualityType#>>. This notion enables
5 one to determine whether a type constructor application yields an
6 equality type solely from the application, without looking at the
7 definition of the type constructor. It helps to ensure that
8 <<PolymorphicEquality#>> is only applied to sensible values.
9
10 The definition of admits equality depends on whether the type
11 constructor was declared by a `type` definition or a
12 `datatype` declaration.
13
14
15 == Type definitions
16
17 For type definition
18
19 [source,sml]
20 ----
21 type ('a1, ..., 'an) t = ...
22 ----
23
24 type constructor `t` admits equality if the right-hand side of the
25 definition is an equality type after replacing `'a1`, ...,
26 `'an` by equality types (it doesn't matter which equality types
27 are chosen).
28
29 For a nullary type definition, this amounts to the right-hand side
30 being an equality type. For example, after the definition
31
32 [source,sml]
33 ----
34 type t = bool * int
35 ----
36
37 type constructor `t` admits equality because `bool * int` is
38 an equality type. On the other hand, after the definition
39
40 [source,sml]
41 ----
42 type t = bool * int * real
43 ----
44
45 type constructor `t` does not admit equality, because `real`
46 is not an equality type.
47
48 For another example, after the definition
49
50 [source,sml]
51 ----
52 type 'a t = bool * 'a
53 ----
54
55 type constructor `t` admits equality because `bool * int`
56 is an equality type (we could have chosen any equality type other than
57 `int`).
58
59 On the other hand, after the definition
60
61 [source,sml]
62 ----
63 type 'a t = real * 'a
64 ----
65
66 type constructor `t` does not admit equality because
67 `real * int` is not equality type.
68
69 We can check that a type constructor admits equality using an
70 `eqtype` specification.
71
72 [source,sml]
73 ----
74 structure Ok: sig eqtype 'a t end =
75 struct
76 type 'a t = bool * 'a
77 end
78 ----
79
80 [source,sml]
81 ----
82 structure Bad: sig eqtype 'a t end =
83 struct
84 type 'a t = real * int * 'a
85 end
86 ----
87
88 On `structure Bad`, MLton reports the following error.
89 ----
90 Error: z.sml 1.16-1.34.
91 Type in structure disagrees with signature (admits equality): t.
92 structure: type 'a t = [real] * _ * _
93 defn at: z.sml 3.15-3.15
94 signature: [eqtype] 'a t
95 spec at: z.sml 1.30-1.30
96 ----
97
98 The `structure:` section provides an explanation of why the type
99 did not admit equality, highlighting the problematic component
100 (`real`).
101
102
103 == Datatype declarations
104
105 For a type constructor declared by a datatype declaration to admit
106 equality, every <<Variant#,variant>> of the datatype must admit equality. For
107 example, the following datatype admits equality because `bool` and
108 `char * int` are equality types.
109
110 [source,sml]
111 ----
112 datatype t = A of bool | B of char * int
113 ----
114
115 Nullary constructors trivially admit equality, so that the following
116 datatype admits equality.
117
118 [source,sml]
119 ----
120 datatype t = A | B | C
121 ----
122
123 For a parameterized datatype constructor to admit equality, we
124 consider each <<Variant#,variant>> as a type definition, and require that the
125 definition admit equality. For example, for the datatype
126
127 [source,sml]
128 ----
129 datatype 'a t = A of bool * 'a | B of 'a
130 ----
131
132 the type definitions
133
134 [source,sml]
135 ----
136 type 'a tA = bool * 'a
137 type 'a tB = 'a
138 ----
139
140 both admit equality. Thus, type constructor `t` admits equality.
141
142 On the other hand, the following datatype does not admit equality.
143
144 [source,sml]
145 ----
146 datatype 'a t = A of bool * 'a | B of real * 'a
147 ----
148
149 As with type definitions, we can check using an `eqtype`
150 specification.
151
152 [source,sml]
153 ----
154 structure Bad: sig eqtype 'a t end =
155 struct
156 datatype 'a t = A of bool * 'a | B of real * 'a
157 end
158 ----
159
160 MLton reports the following error.
161
162 ----
163 Error: z.sml 1.16-1.34.
164 Type in structure disagrees with signature (admits equality): t.
165 structure: datatype 'a t = B of [real] * _ | ...
166 defn at: z.sml 3.19-3.19
167 signature: [eqtype] 'a t
168 spec at: z.sml 1.30-1.30
169 ----
170
171 MLton indicates the problematic constructor (`B`), as well as
172 the problematic component of the constructor's argument.
173
174
175 === Recursive datatypes
176
177 A recursive datatype like
178
179 [source,sml]
180 ----
181 datatype t = A | B of int * t
182 ----
183
184 introduces a new problem, since in order to decide whether `t`
185 admits equality, we need to know for the `B` <<Variant#,variant>> whether
186 `t` admits equality. The <<DefinitionOfStandardML#,Definition>>
187 answers this question by requiring a type constructor to admit
188 equality if it is consistent to do so. So, in our above example, if
189 we assume that `t` admits equality, then the <<Variant#,variant>>
190 `B of int * t` admits equality. Then, since the `A` <<Variant#,variant>>
191 trivially admits equality, so does the type constructor `t`.
192 Thus, it was consistent to assume that `t` admits equality, and
193 so, `t` does admit equality.
194
195 On the other hand, in the following declaration
196
197 [source,sml]
198 ----
199 datatype t = A | B of real * t
200 ----
201
202 if we assume that `t` admits equality, then the `B` <<Variant#,variant>>
203 does not admit equality. Hence, the type constructor `t` does not
204 admit equality, and our assumption was inconsistent. Hence, `t`
205 does not admit equality.
206
207 The same kind of reasoning applies to mutually recursive datatypes as
208 well. For example, the following defines both `t` and `u` to
209 admit equality.
210
211 [source,sml]
212 ----
213 datatype t = A | B of u
214 and u = C | D of t
215 ----
216
217 But the following defines neither `t` nor `u` to admit
218 equality.
219
220 [source,sml]
221 ----
222 datatype t = A | B of u * real
223 and u = C | D of t
224 ----
225
226 As always, we can check whether a type admits equality using an
227 `eqtype` specification.
228
229 [source,sml]
230 ----
231 structure Bad: sig eqtype t eqtype u end =
232 struct
233 datatype t = A | B of u * real
234 and u = C | D of t
235 end
236 ----
237
238 MLton reports the following error.
239
240 ----
241 Error: z.sml 1.16-1.40.
242 Type in structure disagrees with signature (admits equality): t.
243 structure: datatype t = B of [_str.u] * [real] | ...
244 defn at: z.sml 3.16-3.16
245 signature: [eqtype] t
246 spec at: z.sml 1.27-1.27
247 Error: z.sml 1.16-1.40.
248 Type in structure disagrees with signature (admits equality): u.
249 structure: datatype u = D of [_str.t] | ...
250 defn at: z.sml 4.11-4.11
251 signature: [eqtype] u
252 spec at: z.sml 1.36-1.36
253 ----