adamc@2
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@2
|
2 * All rights reserved.
|
adamc@2
|
3 *
|
adamc@2
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@2
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@2
|
6 *
|
adamc@2
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@2
|
8 * this list of conditions and the following disclaimer.
|
adamc@2
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@2
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@2
|
11 * and/or other materials provided with the distribution.
|
adamc@2
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@2
|
13 * derived from this software without specific prior written permission.
|
adamc@2
|
14 *
|
adamc@2
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@2
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@2
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@2
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@2
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@2
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@2
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@2
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@2
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@2
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@2
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@2
|
26 *)
|
adamc@2
|
27
|
adamc@2
|
28 structure Elaborate :> ELABORATE = struct
|
adamc@2
|
29
|
adamc@14
|
30 structure P = Prim
|
adamc@4
|
31 structure L = Source
|
adamc@2
|
32 structure L' = Elab
|
adamc@2
|
33 structure E = ElabEnv
|
adamc@2
|
34 structure U = ElabUtil
|
adamc@82
|
35 structure D = Disjoint
|
adamc@2
|
36
|
adamc@3
|
37 open Print
|
adamc@3
|
38 open ElabPrint
|
adamc@3
|
39
|
adamc@172
|
40 structure IM = IntBinaryMap
|
adamc@62
|
41 structure SS = BinarySetFn(struct
|
adamc@62
|
42 type ord_key = string
|
adamc@62
|
43 val compare = String.compare
|
adamc@62
|
44 end)
|
adamc@62
|
45
|
adamc@2
|
46 fun elabExplicitness e =
|
adamc@2
|
47 case e of
|
adamc@2
|
48 L.Explicit => L'.Explicit
|
adamc@2
|
49 | L.Implicit => L'.Implicit
|
adamc@2
|
50
|
adamc@2
|
51 fun occursKind r =
|
adamc@76
|
52 U.Kind.exists (fn L'.KUnif (_, _, r') => r = r'
|
adamc@2
|
53 | _ => false)
|
adamc@2
|
54
|
adamc@3
|
55 datatype kunify_error =
|
adamc@2
|
56 KOccursCheckFailed of L'.kind * L'.kind
|
adamc@2
|
57 | KIncompatible of L'.kind * L'.kind
|
adamc@2
|
58
|
adamc@3
|
59 exception KUnify' of kunify_error
|
adamc@3
|
60
|
adamc@3
|
61 fun kunifyError err =
|
adamc@2
|
62 case err of
|
adamc@2
|
63 KOccursCheckFailed (k1, k2) =>
|
adamc@3
|
64 eprefaces "Kind occurs check failed"
|
adamc@3
|
65 [("Kind 1", p_kind k1),
|
adamc@3
|
66 ("Kind 2", p_kind k2)]
|
adamc@2
|
67 | KIncompatible (k1, k2) =>
|
adamc@3
|
68 eprefaces "Incompatible kinds"
|
adamc@3
|
69 [("Kind 1", p_kind k1),
|
adamc@3
|
70 ("Kind 2", p_kind k2)]
|
adamc@2
|
71
|
adamc@3
|
72 fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) =
|
adamc@2
|
73 let
|
adamc@3
|
74 fun err f = raise KUnify' (f (k1All, k2All))
|
adamc@2
|
75 in
|
adamc@2
|
76 case (k1, k2) of
|
adamc@2
|
77 (L'.KType, L'.KType) => ()
|
adamc@82
|
78 | (L'.KUnit, L'.KUnit) => ()
|
adamc@82
|
79
|
adamc@2
|
80 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
|
adamc@3
|
81 (unifyKinds' d1 d2;
|
adamc@3
|
82 unifyKinds' r1 r2)
|
adamc@2
|
83 | (L'.KName, L'.KName) => ()
|
adamc@3
|
84 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
|
adamc@2
|
85
|
adamc@2
|
86 | (L'.KError, _) => ()
|
adamc@2
|
87 | (_, L'.KError) => ()
|
adamc@2
|
88
|
adamc@76
|
89 | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' k1All k2All
|
adamc@76
|
90 | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' k1All k2All
|
adamc@2
|
91
|
adamc@76
|
92 | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) =>
|
adamc@2
|
93 if r1 = r2 then
|
adamc@2
|
94 ()
|
adamc@2
|
95 else
|
adamc@2
|
96 r1 := SOME k2All
|
adamc@2
|
97
|
adamc@76
|
98 | (L'.KUnif (_, _, r), _) =>
|
adamc@2
|
99 if occursKind r k2All then
|
adamc@2
|
100 err KOccursCheckFailed
|
adamc@2
|
101 else
|
adamc@2
|
102 r := SOME k2All
|
adamc@76
|
103 | (_, L'.KUnif (_, _, r)) =>
|
adamc@2
|
104 if occursKind r k1All then
|
adamc@2
|
105 err KOccursCheckFailed
|
adamc@2
|
106 else
|
adamc@2
|
107 r := SOME k1All
|
adamc@2
|
108
|
adamc@2
|
109 | _ => err KIncompatible
|
adamc@2
|
110 end
|
adamc@2
|
111
|
adamc@3
|
112 exception KUnify of L'.kind * L'.kind * kunify_error
|
adamc@3
|
113
|
adamc@3
|
114 fun unifyKinds k1 k2 =
|
adamc@3
|
115 unifyKinds' k1 k2
|
adamc@3
|
116 handle KUnify' err => raise KUnify (k1, k2, err)
|
adamc@3
|
117
|
adamc@3
|
118 datatype con_error =
|
adamc@3
|
119 UnboundCon of ErrorMsg.span * string
|
adamc@157
|
120 | UnboundDatatype of ErrorMsg.span * string
|
adamc@34
|
121 | UnboundStrInCon of ErrorMsg.span * string
|
adamc@3
|
122 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
|
adamc@67
|
123 | DuplicateField of ErrorMsg.span * string
|
adamc@3
|
124
|
adamc@5
|
125 fun conError env err =
|
adamc@3
|
126 case err of
|
adamc@3
|
127 UnboundCon (loc, s) =>
|
adamc@3
|
128 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
|
adamc@157
|
129 | UnboundDatatype (loc, s) =>
|
adamc@157
|
130 ErrorMsg.errorAt loc ("Unbound datatype " ^ s)
|
adamc@34
|
131 | UnboundStrInCon (loc, s) =>
|
adamc@34
|
132 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
|
adamc@3
|
133 | WrongKind (c, k1, k2, kerr) =>
|
adamc@3
|
134 (ErrorMsg.errorAt (#2 c) "Wrong kind";
|
adamc@5
|
135 eprefaces' [("Constructor", p_con env c),
|
adamc@5
|
136 ("Have kind", p_kind k1),
|
adamc@5
|
137 ("Need kind", p_kind k2)];
|
adamc@3
|
138 kunifyError kerr)
|
adamc@67
|
139 | DuplicateField (loc, s) =>
|
adamc@67
|
140 ErrorMsg.errorAt loc ("Duplicate record field " ^ s)
|
adamc@3
|
141
|
adamc@5
|
142 fun checkKind env c k1 k2 =
|
adamc@3
|
143 unifyKinds k1 k2
|
adamc@3
|
144 handle KUnify (k1, k2, err) =>
|
adamc@5
|
145 conError env (WrongKind (c, k1, k2, err))
|
adamc@3
|
146
|
adamc@3
|
147 val dummy = ErrorMsg.dummySpan
|
adamc@3
|
148
|
adamc@3
|
149 val ktype = (L'.KType, dummy)
|
adamc@3
|
150 val kname = (L'.KName, dummy)
|
adamc@12
|
151 val ktype_record = (L'.KRecord ktype, dummy)
|
adamc@3
|
152
|
adamc@3
|
153 val cerror = (L'.CError, dummy)
|
adamc@3
|
154 val kerror = (L'.KError, dummy)
|
adamc@10
|
155 val eerror = (L'.EError, dummy)
|
adamc@31
|
156 val sgnerror = (L'.SgnError, dummy)
|
adamc@31
|
157 val strerror = (L'.StrError, dummy)
|
adamc@3
|
158
|
adamc@56
|
159 val int = ref cerror
|
adamc@56
|
160 val float = ref cerror
|
adamc@56
|
161 val string = ref cerror
|
adamc@56
|
162
|
adamc@3
|
163 local
|
adamc@3
|
164 val count = ref 0
|
adamc@3
|
165 in
|
adamc@3
|
166
|
adamc@3
|
167 fun resetKunif () = count := 0
|
adamc@3
|
168
|
adamc@76
|
169 fun kunif loc =
|
adamc@3
|
170 let
|
adamc@3
|
171 val n = !count
|
adamc@3
|
172 val s = if n <= 26 then
|
adamc@3
|
173 str (chr (ord #"A" + n))
|
adamc@3
|
174 else
|
adamc@3
|
175 "U" ^ Int.toString (n - 26)
|
adamc@3
|
176 in
|
adamc@3
|
177 count := n + 1;
|
adamc@76
|
178 (L'.KUnif (loc, s, ref NONE), dummy)
|
adamc@3
|
179 end
|
adamc@3
|
180
|
adamc@3
|
181 end
|
adamc@3
|
182
|
adamc@10
|
183 local
|
adamc@10
|
184 val count = ref 0
|
adamc@10
|
185 in
|
adamc@10
|
186
|
adamc@10
|
187 fun resetCunif () = count := 0
|
adamc@10
|
188
|
adamc@76
|
189 fun cunif (loc, k) =
|
adamc@10
|
190 let
|
adamc@10
|
191 val n = !count
|
adamc@10
|
192 val s = if n <= 26 then
|
adamc@10
|
193 str (chr (ord #"A" + n))
|
adamc@10
|
194 else
|
adamc@10
|
195 "U" ^ Int.toString (n - 26)
|
adamc@10
|
196 in
|
adamc@10
|
197 count := n + 1;
|
adamc@76
|
198 (L'.CUnif (loc, k, s, ref NONE), dummy)
|
adamc@10
|
199 end
|
adamc@10
|
200
|
adamc@10
|
201 end
|
adamc@10
|
202
|
adamc@18
|
203 fun elabKind (k, loc) =
|
adamc@18
|
204 case k of
|
adamc@18
|
205 L.KType => (L'.KType, loc)
|
adamc@18
|
206 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
|
adamc@18
|
207 | L.KName => (L'.KName, loc)
|
adamc@18
|
208 | L.KRecord k => (L'.KRecord (elabKind k), loc)
|
adamc@82
|
209 | L.KUnit => (L'.KUnit, loc)
|
adamc@76
|
210 | L.KWild => kunif loc
|
adamc@18
|
211
|
adamc@67
|
212 fun foldKind (dom, ran, loc)=
|
adamc@67
|
213 (L'.KArrow ((L'.KArrow ((L'.KName, loc),
|
adamc@67
|
214 (L'.KArrow (dom,
|
adamc@67
|
215 (L'.KArrow (ran, ran), loc)), loc)), loc),
|
adamc@67
|
216 (L'.KArrow (ran,
|
adamc@67
|
217 (L'.KArrow ((L'.KRecord dom, loc),
|
adamc@67
|
218 ran), loc)), loc)), loc)
|
adamc@67
|
219
|
adamc@83
|
220 fun elabCon (env, denv) (c, loc) =
|
adamc@3
|
221 case c of
|
adamc@3
|
222 L.CAnnot (c, k) =>
|
adamc@3
|
223 let
|
adamc@3
|
224 val k' = elabKind k
|
adamc@83
|
225 val (c', ck, gs) = elabCon (env, denv) c
|
adamc@3
|
226 in
|
adamc@5
|
227 checkKind env c' ck k';
|
adamc@83
|
228 (c', k', gs)
|
adamc@3
|
229 end
|
adamc@3
|
230
|
adamc@3
|
231 | L.TFun (t1, t2) =>
|
adamc@3
|
232 let
|
adamc@83
|
233 val (t1', k1, gs1) = elabCon (env, denv) t1
|
adamc@83
|
234 val (t2', k2, gs2) = elabCon (env, denv) t2
|
adamc@3
|
235 in
|
adamc@5
|
236 checkKind env t1' k1 ktype;
|
adamc@5
|
237 checkKind env t2' k2 ktype;
|
adamc@83
|
238 ((L'.TFun (t1', t2'), loc), ktype, gs1 @ gs2)
|
adamc@3
|
239 end
|
adamc@3
|
240 | L.TCFun (e, x, k, t) =>
|
adamc@3
|
241 let
|
adamc@3
|
242 val e' = elabExplicitness e
|
adamc@3
|
243 val k' = elabKind k
|
adamc@3
|
244 val env' = E.pushCRel env x k'
|
adamc@83
|
245 val (t', tk, gs) = elabCon (env', D.enter denv) t
|
adamc@3
|
246 in
|
adamc@5
|
247 checkKind env t' tk ktype;
|
adamc@83
|
248 ((L'.TCFun (e', x, k', t'), loc), ktype, gs)
|
adamc@3
|
249 end
|
adamc@85
|
250 | L.TDisjoint (c1, c2, c) =>
|
adamc@85
|
251 let
|
adamc@85
|
252 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@85
|
253 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@85
|
254
|
adamc@85
|
255 val ku1 = kunif loc
|
adamc@85
|
256 val ku2 = kunif loc
|
adamc@85
|
257
|
adamc@90
|
258 val (denv', gs3) = D.assert env denv (c1', c2')
|
adamc@90
|
259 val (c', k, gs4) = elabCon (env, denv') c
|
adamc@85
|
260 in
|
adamc@85
|
261 checkKind env c1' k1 (L'.KRecord ku1, loc);
|
adamc@85
|
262 checkKind env c2' k2 (L'.KRecord ku2, loc);
|
adamc@85
|
263
|
adamc@90
|
264 ((L'.TDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3 @ gs4)
|
adamc@85
|
265 end
|
adamc@3
|
266 | L.TRecord c =>
|
adamc@3
|
267 let
|
adamc@83
|
268 val (c', ck, gs) = elabCon (env, denv) c
|
adamc@3
|
269 val k = (L'.KRecord ktype, loc)
|
adamc@3
|
270 in
|
adamc@5
|
271 checkKind env c' ck k;
|
adamc@83
|
272 ((L'.TRecord c', loc), ktype, gs)
|
adamc@3
|
273 end
|
adamc@3
|
274
|
adamc@34
|
275 | L.CVar ([], s) =>
|
adamc@3
|
276 (case E.lookupC env s of
|
adamc@9
|
277 E.NotBound =>
|
adamc@5
|
278 (conError env (UnboundCon (loc, s));
|
adamc@83
|
279 (cerror, kerror, []))
|
adamc@9
|
280 | E.Rel (n, k) =>
|
adamc@83
|
281 ((L'.CRel n, loc), k, [])
|
adamc@9
|
282 | E.Named (n, k) =>
|
adamc@83
|
283 ((L'.CNamed n, loc), k, []))
|
adamc@34
|
284 | L.CVar (m1 :: ms, s) =>
|
adamc@34
|
285 (case E.lookupStr env m1 of
|
adamc@42
|
286 NONE => (conError env (UnboundStrInCon (loc, m1));
|
adamc@83
|
287 (cerror, kerror, []))
|
adamc@34
|
288 | SOME (n, sgn) =>
|
adamc@34
|
289 let
|
adamc@34
|
290 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
291 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@34
|
292 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@34
|
293 (strerror, sgnerror))
|
adamc@34
|
294 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
295 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
296
|
adamc@34
|
297 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
|
adamc@34
|
298 NONE => (conError env (UnboundCon (loc, s));
|
adamc@34
|
299 kerror)
|
adamc@34
|
300 | SOME (k, _) => k
|
adamc@34
|
301 in
|
adamc@83
|
302 ((L'.CModProj (n, ms, s), loc), k, [])
|
adamc@34
|
303 end)
|
adamc@34
|
304
|
adamc@3
|
305 | L.CApp (c1, c2) =>
|
adamc@3
|
306 let
|
adamc@83
|
307 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@83
|
308 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@76
|
309 val dom = kunif loc
|
adamc@76
|
310 val ran = kunif loc
|
adamc@3
|
311 in
|
adamc@5
|
312 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
|
adamc@5
|
313 checkKind env c2' k2 dom;
|
adamc@83
|
314 ((L'.CApp (c1', c2'), loc), ran, gs1 @ gs2)
|
adamc@3
|
315 end
|
adamc@67
|
316 | L.CAbs (x, ko, t) =>
|
adamc@3
|
317 let
|
adamc@67
|
318 val k' = case ko of
|
adamc@76
|
319 NONE => kunif loc
|
adamc@67
|
320 | SOME k => elabKind k
|
adamc@3
|
321 val env' = E.pushCRel env x k'
|
adamc@83
|
322 val (t', tk, gs) = elabCon (env', D.enter denv) t
|
adamc@3
|
323 in
|
adamc@8
|
324 ((L'.CAbs (x, k', t'), loc),
|
adamc@83
|
325 (L'.KArrow (k', tk), loc),
|
adamc@83
|
326 gs)
|
adamc@3
|
327 end
|
adamc@3
|
328
|
adamc@84
|
329 | L.CDisjoint (c1, c2, c) =>
|
adamc@84
|
330 let
|
adamc@84
|
331 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@84
|
332 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@84
|
333
|
adamc@84
|
334 val ku1 = kunif loc
|
adamc@84
|
335 val ku2 = kunif loc
|
adamc@84
|
336
|
adamc@90
|
337 val (denv', gs3) = D.assert env denv (c1', c2')
|
adamc@90
|
338 val (c', k, gs4) = elabCon (env, denv') c
|
adamc@84
|
339 in
|
adamc@84
|
340 checkKind env c1' k1 (L'.KRecord ku1, loc);
|
adamc@84
|
341 checkKind env c2' k2 (L'.KRecord ku2, loc);
|
adamc@84
|
342
|
adamc@90
|
343 ((L'.CDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3 @ gs4)
|
adamc@84
|
344 end
|
adamc@84
|
345
|
adamc@3
|
346 | L.CName s =>
|
adamc@83
|
347 ((L'.CName s, loc), kname, [])
|
adamc@3
|
348
|
adamc@3
|
349 | L.CRecord xcs =>
|
adamc@3
|
350 let
|
adamc@76
|
351 val k = kunif loc
|
adamc@3
|
352
|
adamc@83
|
353 val (xcs', gs) = ListUtil.foldlMap (fn ((x, c), gs) =>
|
adamc@83
|
354 let
|
adamc@83
|
355 val (x', xk, gs1) = elabCon (env, denv) x
|
adamc@83
|
356 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@83
|
357 in
|
adamc@83
|
358 checkKind env x' xk kname;
|
adamc@83
|
359 checkKind env c' ck k;
|
adamc@83
|
360 ((x', c'), gs1 @ gs2 @ gs)
|
adamc@83
|
361 end) [] xcs
|
adamc@67
|
362
|
adamc@67
|
363 val rc = (L'.CRecord (k, xcs'), loc)
|
adamc@67
|
364 (* Add duplicate field checking later. *)
|
adamc@83
|
365
|
adamc@83
|
366 fun prove (xcs, ds) =
|
adamc@83
|
367 case xcs of
|
adamc@83
|
368 [] => ds
|
adamc@83
|
369 | xc :: rest =>
|
adamc@83
|
370 let
|
adamc@83
|
371 val r1 = (L'.CRecord (k, [xc]), loc)
|
adamc@83
|
372 val ds = foldl (fn (xc', ds) =>
|
adamc@83
|
373 let
|
adamc@83
|
374 val r2 = (L'.CRecord (k, [xc']), loc)
|
adamc@83
|
375 in
|
adamc@90
|
376 D.prove env denv (r1, r2, loc) @ ds
|
adamc@83
|
377 end)
|
adamc@83
|
378 ds rest
|
adamc@83
|
379 in
|
adamc@83
|
380 prove (rest, ds)
|
adamc@83
|
381 end
|
adamc@3
|
382 in
|
adamc@83
|
383 (rc, (L'.KRecord k, loc), prove (xcs', gs))
|
adamc@3
|
384 end
|
adamc@3
|
385 | L.CConcat (c1, c2) =>
|
adamc@3
|
386 let
|
adamc@83
|
387 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@83
|
388 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@76
|
389 val ku = kunif loc
|
adamc@3
|
390 val k = (L'.KRecord ku, loc)
|
adamc@3
|
391 in
|
adamc@5
|
392 checkKind env c1' k1 k;
|
adamc@5
|
393 checkKind env c2' k2 k;
|
adamc@83
|
394 ((L'.CConcat (c1', c2'), loc), k,
|
adamc@90
|
395 D.prove env denv (c1', c2', loc) @ gs1 @ gs2)
|
adamc@3
|
396 end
|
adamc@67
|
397 | L.CFold =>
|
adamc@67
|
398 let
|
adamc@76
|
399 val dom = kunif loc
|
adamc@76
|
400 val ran = kunif loc
|
adamc@67
|
401 in
|
adamc@67
|
402 ((L'.CFold (dom, ran), loc),
|
adamc@83
|
403 foldKind (dom, ran, loc),
|
adamc@83
|
404 [])
|
adamc@67
|
405 end
|
adamc@3
|
406
|
adamc@83
|
407 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), [])
|
adamc@82
|
408
|
adamc@18
|
409 | L.CWild k =>
|
adamc@18
|
410 let
|
adamc@18
|
411 val k' = elabKind k
|
adamc@18
|
412 in
|
adamc@83
|
413 (cunif (loc, k'), k', [])
|
adamc@18
|
414 end
|
adamc@18
|
415
|
adamc@6
|
416 fun kunifsRemain k =
|
adamc@6
|
417 case k of
|
adamc@76
|
418 L'.KUnif (_, _, ref NONE) => true
|
adamc@6
|
419 | _ => false
|
adamc@10
|
420 fun cunifsRemain c =
|
adamc@10
|
421 case c of
|
adamc@76
|
422 L'.CUnif (loc, _, _, ref NONE) => SOME loc
|
adamc@76
|
423 | _ => NONE
|
adamc@6
|
424
|
adamc@76
|
425 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
|
adamc@76
|
426 con = fn _ => false,
|
adamc@76
|
427 exp = fn _ => false,
|
adamc@76
|
428 sgn_item = fn _ => false,
|
adamc@76
|
429 sgn = fn _ => false,
|
adamc@76
|
430 str = fn _ => false,
|
adamc@76
|
431 decl = fn _ => false}
|
adamc@10
|
432
|
adamc@76
|
433 val cunifsInDecl = U.Decl.search {kind = fn _ => NONE,
|
adamc@76
|
434 con = cunifsRemain,
|
adamc@76
|
435 exp = fn _ => NONE,
|
adamc@76
|
436 sgn_item = fn _ => NONE,
|
adamc@76
|
437 sgn = fn _ => NONE,
|
adamc@76
|
438 str = fn _ => NONE,
|
adamc@76
|
439 decl = fn _ => NONE}
|
adamc@10
|
440
|
adamc@10
|
441 fun occursCon r =
|
adamc@10
|
442 U.Con.exists {kind = fn _ => false,
|
adamc@76
|
443 con = fn L'.CUnif (_, _, _, r') => r = r'
|
adamc@10
|
444 | _ => false}
|
adamc@10
|
445
|
adamc@10
|
446 datatype cunify_error =
|
adamc@10
|
447 CKind of L'.kind * L'.kind * kunify_error
|
adamc@10
|
448 | COccursCheckFailed of L'.con * L'.con
|
adamc@10
|
449 | CIncompatible of L'.con * L'.con
|
adamc@10
|
450 | CExplicitness of L'.con * L'.con
|
adamc@141
|
451 | CKindof of L'.kind * L'.con
|
adamc@153
|
452 | CRecordFailure of PD.pp_desc * PD.pp_desc
|
adamc@10
|
453
|
adamc@10
|
454 exception CUnify' of cunify_error
|
adamc@10
|
455
|
adamc@10
|
456 fun cunifyError env err =
|
adamc@10
|
457 case err of
|
adamc@10
|
458 CKind (k1, k2, kerr) =>
|
adamc@10
|
459 (eprefaces "Kind unification failure"
|
adamc@10
|
460 [("Kind 1", p_kind k1),
|
adamc@10
|
461 ("Kind 2", p_kind k2)];
|
adamc@10
|
462 kunifyError kerr)
|
adamc@10
|
463 | COccursCheckFailed (c1, c2) =>
|
adamc@10
|
464 eprefaces "Constructor occurs check failed"
|
adamc@10
|
465 [("Con 1", p_con env c1),
|
adamc@10
|
466 ("Con 2", p_con env c2)]
|
adamc@10
|
467 | CIncompatible (c1, c2) =>
|
adamc@10
|
468 eprefaces "Incompatible constructors"
|
adamc@10
|
469 [("Con 1", p_con env c1),
|
adamc@10
|
470 ("Con 2", p_con env c2)]
|
adamc@10
|
471 | CExplicitness (c1, c2) =>
|
adamc@10
|
472 eprefaces "Differing constructor function explicitness"
|
adamc@10
|
473 [("Con 1", p_con env c1),
|
adamc@10
|
474 ("Con 2", p_con env c2)]
|
adamc@141
|
475 | CKindof (k, c) =>
|
adamc@147
|
476 eprefaces "Unexpected kind for kindof calculation"
|
adamc@141
|
477 [("Kind", p_kind k),
|
adamc@141
|
478 ("Con", p_con env c)]
|
adamc@153
|
479 | CRecordFailure (s1, s2) =>
|
adamc@153
|
480 eprefaces "Can't unify record constructors"
|
adamc@153
|
481 [("Summary 1", s1),
|
adamc@153
|
482 ("Summary 2", s2)]
|
adamc@10
|
483
|
adamc@13
|
484 exception SynUnif = E.SynUnif
|
adamc@12
|
485
|
adamc@81
|
486 open ElabOps
|
adamc@43
|
487
|
adamc@12
|
488 type record_summary = {
|
adamc@12
|
489 fields : (L'.con * L'.con) list,
|
adamc@12
|
490 unifs : (L'.con * L'.con option ref) list,
|
adamc@12
|
491 others : L'.con list
|
adamc@12
|
492 }
|
adamc@12
|
493
|
adamc@12
|
494 fun summaryToCon {fields, unifs, others} =
|
adamc@12
|
495 let
|
adamc@12
|
496 val c = (L'.CRecord (ktype, []), dummy)
|
adamc@12
|
497 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
|
adamc@12
|
498 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
|
adamc@12
|
499 in
|
adamc@12
|
500 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
|
adamc@12
|
501 end
|
adamc@12
|
502
|
adamc@12
|
503 fun p_summary env s = p_con env (summaryToCon s)
|
adamc@12
|
504
|
adamc@12
|
505 exception CUnify of L'.con * L'.con * cunify_error
|
adamc@12
|
506
|
adamc@12
|
507 fun hnormKind (kAll as (k, _)) =
|
adamc@12
|
508 case k of
|
adamc@76
|
509 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
|
adamc@12
|
510 | _ => kAll
|
adamc@12
|
511
|
adamc@12
|
512 fun kindof env (c, loc) =
|
adamc@12
|
513 case c of
|
adamc@12
|
514 L'.TFun _ => ktype
|
adamc@12
|
515 | L'.TCFun _ => ktype
|
adamc@85
|
516 | L'.TDisjoint _ => ktype
|
adamc@12
|
517 | L'.TRecord _ => ktype
|
adamc@12
|
518
|
adamc@12
|
519 | L'.CRel xn => #2 (E.lookupCRel env xn)
|
adamc@12
|
520 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
|
adamc@34
|
521 | L'.CModProj (n, ms, x) =>
|
adamc@34
|
522 let
|
adamc@34
|
523 val (_, sgn) = E.lookupStrNamed env n
|
adamc@34
|
524 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
525 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@34
|
526 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@34
|
527 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
528 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
529 in
|
adamc@34
|
530 case E.projectCon env {sgn = sgn, str = str, field = x} of
|
adamc@34
|
531 NONE => raise Fail "kindof: Unknown con in structure"
|
adamc@34
|
532 | SOME (k, _) => k
|
adamc@34
|
533 end
|
adamc@34
|
534
|
adamc@12
|
535 | L'.CApp (c, _) =>
|
adamc@141
|
536 (case hnormKind (kindof env c) of
|
adamc@141
|
537 (L'.KArrow (_, k), _) => k
|
adamc@141
|
538 | (L'.KError, _) => kerror
|
adamc@141
|
539 | k => raise CUnify' (CKindof (k, c)))
|
adamc@12
|
540 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
|
adamc@84
|
541 | L'.CDisjoint (_, _, c) => kindof env c
|
adamc@12
|
542
|
adamc@12
|
543 | L'.CName _ => kname
|
adamc@12
|
544
|
adamc@12
|
545 | L'.CRecord (k, _) => (L'.KRecord k, loc)
|
adamc@12
|
546 | L'.CConcat (c, _) => kindof env c
|
adamc@67
|
547 | L'.CFold (dom, ran) => foldKind (dom, ran, loc)
|
adamc@12
|
548
|
adamc@82
|
549 | L'.CUnit => (L'.KUnit, loc)
|
adamc@82
|
550
|
adamc@12
|
551 | L'.CError => kerror
|
adamc@76
|
552 | L'.CUnif (_, k, _, _) => k
|
adamc@12
|
553
|
adamc@90
|
554 val hnormCon = D.hnormCon
|
adamc@86
|
555
|
adamc@86
|
556 fun unifyRecordCons (env, denv) (c1, c2) =
|
adamc@12
|
557 let
|
adamc@92
|
558 fun rkindof c =
|
adamc@147
|
559 case hnormKind (kindof env c) of
|
adamc@92
|
560 (L'.KRecord k, _) => k
|
adamc@141
|
561 | (L'.KError, _) => kerror
|
adamc@141
|
562 | k => raise CUnify' (CKindof (k, c))
|
adamc@92
|
563
|
adamc@92
|
564 val k1 = rkindof c1
|
adamc@92
|
565 val k2 = rkindof c2
|
adamc@86
|
566
|
adamc@86
|
567 val (r1, gs1) = recordSummary (env, denv) c1
|
adamc@86
|
568 val (r2, gs2) = recordSummary (env, denv) c2
|
adamc@12
|
569 in
|
adamc@12
|
570 unifyKinds k1 k2;
|
adamc@86
|
571 unifySummaries (env, denv) (k1, r1, r2);
|
adamc@86
|
572 gs1 @ gs2
|
adamc@12
|
573 end
|
adamc@12
|
574
|
adamc@86
|
575 and recordSummary (env, denv) c =
|
adamc@86
|
576 let
|
adamc@86
|
577 val (c, gs) = hnormCon (env, denv) c
|
adamc@12
|
578
|
adamc@86
|
579 val (sum, gs') =
|
adamc@86
|
580 case c of
|
adamc@86
|
581 (L'.CRecord (_, xcs), _) => ({fields = xcs, unifs = [], others = []}, [])
|
adamc@86
|
582 | (L'.CConcat (c1, c2), _) =>
|
adamc@86
|
583 let
|
adamc@86
|
584 val (s1, gs1) = recordSummary (env, denv) c1
|
adamc@86
|
585 val (s2, gs2) = recordSummary (env, denv) c2
|
adamc@86
|
586 in
|
adamc@86
|
587 ({fields = #fields s1 @ #fields s2,
|
adamc@86
|
588 unifs = #unifs s1 @ #unifs s2,
|
adamc@86
|
589 others = #others s1 @ #others s2},
|
adamc@86
|
590 gs1 @ gs2)
|
adamc@86
|
591 end
|
adamc@86
|
592 | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary (env, denv) c
|
adamc@86
|
593 | c' as (L'.CUnif (_, _, _, r), _) => ({fields = [], unifs = [(c', r)], others = []}, [])
|
adamc@86
|
594 | c' => ({fields = [], unifs = [], others = [c']}, [])
|
adamc@86
|
595 in
|
adamc@86
|
596 (sum, gs @ gs')
|
adamc@86
|
597 end
|
adamc@86
|
598
|
adamc@86
|
599 and consEq (env, denv) (c1, c2) =
|
adamc@86
|
600 (case unifyCons (env, denv) c1 c2 of
|
adamc@86
|
601 [] => true
|
adamc@86
|
602 | _ => false)
|
adamc@12
|
603 handle CUnify _ => false
|
adamc@12
|
604
|
adamc@80
|
605 and consNeq env (c1, c2) =
|
adamc@86
|
606 case (#1 (ElabOps.hnormCon env c1), #1 (ElabOps.hnormCon env c2)) of
|
adamc@80
|
607 (L'.CName x1, L'.CName x2) => x1 <> x2
|
adamc@80
|
608 | _ => false
|
adamc@80
|
609
|
adamc@86
|
610 and unifySummaries (env, denv) (k, s1 : record_summary, s2 : record_summary) =
|
adamc@12
|
611 let
|
adamc@14
|
612 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
|
adamc@14
|
613 ("#2", p_summary env s2)]*)
|
adamc@12
|
614
|
adamc@12
|
615 fun eatMatching p (ls1, ls2) =
|
adamc@12
|
616 let
|
adamc@12
|
617 fun em (ls1, ls2, passed1) =
|
adamc@12
|
618 case ls1 of
|
adamc@12
|
619 [] => (rev passed1, ls2)
|
adamc@12
|
620 | h1 :: t1 =>
|
adamc@12
|
621 let
|
adamc@12
|
622 fun search (ls2', passed2) =
|
adamc@12
|
623 case ls2' of
|
adamc@12
|
624 [] => em (t1, ls2, h1 :: passed1)
|
adamc@12
|
625 | h2 :: t2 =>
|
adamc@12
|
626 if p (h1, h2) then
|
adamc@12
|
627 em (t1, List.revAppend (passed2, t2), passed1)
|
adamc@12
|
628 else
|
adamc@12
|
629 search (t2, h2 :: passed2)
|
adamc@12
|
630 in
|
adamc@12
|
631 search (ls2, [])
|
adamc@12
|
632 end
|
adamc@12
|
633 in
|
adamc@12
|
634 em (ls1, ls2, [])
|
adamc@12
|
635 end
|
adamc@12
|
636
|
adamc@12
|
637 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
|
adamc@80
|
638 not (consNeq env (x1, x2))
|
adamc@86
|
639 andalso consEq (env, denv) (c1, c2)
|
adamc@86
|
640 andalso consEq (env, denv) (x1, x2))
|
adamc@79
|
641 (#fields s1, #fields s2)
|
adamc@14
|
642 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
|
adamc@14
|
643 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
|
adamc@12
|
644 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
|
adamc@86
|
645 val (others1, others2) = eatMatching (consEq (env, denv)) (#others s1, #others s2)
|
adamc@12
|
646
|
adamc@12
|
647 fun unifFields (fs, others, unifs) =
|
adamc@12
|
648 case (fs, others, unifs) of
|
adamc@12
|
649 ([], [], _) => ([], [], unifs)
|
adamc@12
|
650 | (_, _, []) => (fs, others, [])
|
adamc@12
|
651 | (_, _, (_, r) :: rest) =>
|
adamc@12
|
652 let
|
adamc@12
|
653 val r' = ref NONE
|
adamc@141
|
654 val kr = (L'.KRecord k, dummy)
|
adamc@141
|
655 val cr' = (L'.CUnif (dummy, kr, "recd", r'), dummy)
|
adamc@12
|
656
|
adamc@12
|
657 val prefix = case (fs, others) of
|
adamc@12
|
658 ([], other :: others) =>
|
adamc@12
|
659 List.foldl (fn (other, c) =>
|
adamc@12
|
660 (L'.CConcat (c, other), dummy))
|
adamc@12
|
661 other others
|
adamc@12
|
662 | (fs, []) =>
|
adamc@12
|
663 (L'.CRecord (k, fs), dummy)
|
adamc@12
|
664 | (fs, others) =>
|
adamc@12
|
665 List.foldl (fn (other, c) =>
|
adamc@12
|
666 (L'.CConcat (c, other), dummy))
|
adamc@12
|
667 (L'.CRecord (k, fs), dummy) others
|
adamc@12
|
668 in
|
adamc@12
|
669 r := SOME (L'.CConcat (prefix, cr'), dummy);
|
adamc@12
|
670 ([], [], (cr', r') :: rest)
|
adamc@12
|
671 end
|
adamc@12
|
672
|
adamc@12
|
673 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
|
adamc@12
|
674 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
|
adamc@12
|
675
|
adamc@86
|
676 val clear = case (fs1, others1, fs2, others2) of
|
adamc@86
|
677 ([], [], [], []) => true
|
adamc@12
|
678 | _ => false
|
adamc@12
|
679 val empty = (L'.CRecord (k, []), dummy)
|
adamc@12
|
680 fun pairOffUnifs (unifs1, unifs2) =
|
adamc@12
|
681 case (unifs1, unifs2) of
|
adamc@12
|
682 ([], _) =>
|
adamc@86
|
683 if clear then
|
adamc@12
|
684 List.app (fn (_, r) => r := SOME empty) unifs2
|
adamc@12
|
685 else
|
adamc@153
|
686 raise CUnify' (CRecordFailure (p_summary env s1, p_summary env s2))
|
adamc@12
|
687 | (_, []) =>
|
adamc@86
|
688 if clear then
|
adamc@12
|
689 List.app (fn (_, r) => r := SOME empty) unifs1
|
adamc@12
|
690 else
|
adamc@153
|
691 raise CUnify' (CRecordFailure (p_summary env s1, p_summary env s2))
|
adamc@12
|
692 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
|
adamc@12
|
693 (r2 := SOME c1;
|
adamc@12
|
694 pairOffUnifs (rest1, rest2))
|
adamc@12
|
695 in
|
adamc@12
|
696 pairOffUnifs (unifs1, unifs2)
|
adamc@12
|
697 end
|
adamc@12
|
698
|
adamc@86
|
699 and unifyCons' (env, denv) c1 c2 =
|
adamc@86
|
700 let
|
adamc@86
|
701 val (c1, gs1) = hnormCon (env, denv) c1
|
adamc@86
|
702 val (c2, gs2) = hnormCon (env, denv) c2
|
adamc@90
|
703 val gs3 = unifyCons'' (env, denv) c1 c2
|
adamc@86
|
704 in
|
adamc@90
|
705 gs1 @ gs2 @ gs3
|
adamc@86
|
706 end
|
adamc@11
|
707
|
adamc@86
|
708 and unifyCons'' (env, denv) (c1All as (c1, _)) (c2All as (c2, _)) =
|
adamc@10
|
709 let
|
adamc@10
|
710 fun err f = raise CUnify' (f (c1All, c2All))
|
adamc@12
|
711
|
adamc@86
|
712 fun isRecord () = unifyRecordCons (env, denv) (c1All, c2All)
|
adamc@10
|
713 in
|
adamc@92
|
714 (*eprefaces "unifyCons''" [("c1All", p_con env c1All),
|
adamc@92
|
715 ("c2All", p_con env c2All)];*)
|
adamc@92
|
716
|
adamc@10
|
717 case (c1, c2) of
|
adamc@88
|
718 (L'.CUnit, L'.CUnit) => []
|
adamc@88
|
719
|
adamc@88
|
720 | (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
|
adamc@86
|
721 unifyCons' (env, denv) d1 d2
|
adamc@86
|
722 @ unifyCons' (env, denv) r1 r2
|
adamc@10
|
723 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
|
adamc@10
|
724 if expl1 <> expl2 then
|
adamc@10
|
725 err CExplicitness
|
adamc@10
|
726 else
|
adamc@10
|
727 (unifyKinds d1 d2;
|
adamc@86
|
728 unifyCons' (E.pushCRel env x1 d1, D.enter denv) r1 r2)
|
adamc@86
|
729 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' (env, denv) r1 r2
|
adamc@10
|
730
|
adamc@10
|
731 | (L'.CRel n1, L'.CRel n2) =>
|
adamc@10
|
732 if n1 = n2 then
|
adamc@86
|
733 []
|
adamc@10
|
734 else
|
adamc@10
|
735 err CIncompatible
|
adamc@10
|
736 | (L'.CNamed n1, L'.CNamed n2) =>
|
adamc@10
|
737 if n1 = n2 then
|
adamc@86
|
738 []
|
adamc@10
|
739 else
|
adamc@10
|
740 err CIncompatible
|
adamc@10
|
741
|
adamc@10
|
742 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
|
adamc@86
|
743 (unifyCons' (env, denv) d1 d2;
|
adamc@86
|
744 unifyCons' (env, denv) r1 r2)
|
adamc@10
|
745 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
|
adamc@10
|
746 (unifyKinds k1 k2;
|
adamc@86
|
747 unifyCons' (E.pushCRel env x1 k1, D.enter denv) c1 c2)
|
adamc@10
|
748
|
adamc@10
|
749 | (L'.CName n1, L'.CName n2) =>
|
adamc@10
|
750 if n1 = n2 then
|
adamc@86
|
751 []
|
adamc@10
|
752 else
|
adamc@10
|
753 err CIncompatible
|
adamc@10
|
754
|
adamc@34
|
755 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
|
adamc@34
|
756 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
|
adamc@86
|
757 []
|
adamc@34
|
758 else
|
adamc@34
|
759 err CIncompatible
|
adamc@34
|
760
|
adamc@86
|
761 | (L'.CError, _) => []
|
adamc@86
|
762 | (_, L'.CError) => []
|
adamc@10
|
763
|
adamc@92
|
764 | (L'.CRecord _, _) => isRecord ()
|
adamc@92
|
765 | (_, L'.CRecord _) => isRecord ()
|
adamc@92
|
766 | (L'.CConcat _, _) => isRecord ()
|
adamc@92
|
767 | (_, L'.CConcat _) => isRecord ()
|
adamc@92
|
768 (*| (L'.CUnif (_, (L'.KRecord _, _), _, _), _) => isRecord ()
|
adamc@92
|
769 | (_, L'.CUnif (_, (L'.KRecord _, _), _, _)) => isRecord ()*)
|
adamc@10
|
770
|
adamc@76
|
771 | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) =>
|
adamc@10
|
772 if r1 = r2 then
|
adamc@86
|
773 []
|
adamc@10
|
774 else
|
adamc@10
|
775 (unifyKinds k1 k2;
|
adamc@86
|
776 r1 := SOME c2All;
|
adamc@86
|
777 [])
|
adamc@10
|
778
|
adamc@76
|
779 | (L'.CUnif (_, _, _, r), _) =>
|
adamc@10
|
780 if occursCon r c2All then
|
adamc@10
|
781 err COccursCheckFailed
|
adamc@10
|
782 else
|
adamc@86
|
783 (r := SOME c2All;
|
adamc@86
|
784 [])
|
adamc@76
|
785 | (_, L'.CUnif (_, _, _, r)) =>
|
adamc@10
|
786 if occursCon r c1All then
|
adamc@10
|
787 err COccursCheckFailed
|
adamc@10
|
788 else
|
adamc@86
|
789 (r := SOME c1All;
|
adamc@86
|
790 [])
|
adamc@10
|
791
|
adamc@12
|
792
|
adamc@71
|
793 | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) =>
|
adamc@71
|
794 (unifyKinds dom1 dom2;
|
adamc@86
|
795 unifyKinds ran1 ran2;
|
adamc@86
|
796 [])
|
adamc@71
|
797
|
adamc@10
|
798 | _ => err CIncompatible
|
adamc@10
|
799 end
|
adamc@10
|
800
|
adamc@86
|
801 and unifyCons (env, denv) c1 c2 =
|
adamc@86
|
802 unifyCons' (env, denv) c1 c2
|
adamc@10
|
803 handle CUnify' err => raise CUnify (c1, c2, err)
|
adamc@10
|
804 | KUnify args => raise CUnify (c1, c2, CKind args)
|
adamc@10
|
805
|
adamc@10
|
806 datatype exp_error =
|
adamc@10
|
807 UnboundExp of ErrorMsg.span * string
|
adamc@34
|
808 | UnboundStrInExp of ErrorMsg.span * string
|
adamc@10
|
809 | Unify of L'.exp * L'.con * L'.con * cunify_error
|
adamc@11
|
810 | Unif of string * L'.con
|
adamc@11
|
811 | WrongForm of string * L'.exp * L'.con
|
adamc@92
|
812 | IncompatibleCons of L'.con * L'.con
|
adamc@171
|
813 | DuplicatePatternVariable of ErrorMsg.span * string
|
adamc@171
|
814 | PatUnify of L'.pat * L'.con * L'.con * cunify_error
|
adamc@174
|
815 | UnboundConstructor of ErrorMsg.span * string list * string
|
adamc@171
|
816 | PatHasArg of ErrorMsg.span
|
adamc@171
|
817 | PatHasNoArg of ErrorMsg.span
|
adamc@172
|
818 | Inexhaustive of ErrorMsg.span
|
adamc@10
|
819
|
adamc@10
|
820 fun expError env err =
|
adamc@10
|
821 case err of
|
adamc@10
|
822 UnboundExp (loc, s) =>
|
adamc@10
|
823 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
|
adamc@34
|
824 | UnboundStrInExp (loc, s) =>
|
adamc@34
|
825 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
|
adamc@10
|
826 | Unify (e, c1, c2, uerr) =>
|
adamc@10
|
827 (ErrorMsg.errorAt (#2 e) "Unification failure";
|
adamc@10
|
828 eprefaces' [("Expression", p_exp env e),
|
adamc@10
|
829 ("Have con", p_con env c1),
|
adamc@10
|
830 ("Need con", p_con env c2)];
|
adamc@10
|
831 cunifyError env uerr)
|
adamc@11
|
832 | Unif (action, c) =>
|
adamc@11
|
833 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
|
adamc@11
|
834 eprefaces' [("Con", p_con env c)])
|
adamc@11
|
835 | WrongForm (variety, e, t) =>
|
adamc@11
|
836 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
|
adamc@11
|
837 eprefaces' [("Expression", p_exp env e),
|
adamc@11
|
838 ("Type", p_con env t)])
|
adamc@92
|
839 | IncompatibleCons (c1, c2) =>
|
adamc@92
|
840 (ErrorMsg.errorAt (#2 c1) "Incompatible constructors";
|
adamc@92
|
841 eprefaces' [("Con 1", p_con env c1),
|
adamc@92
|
842 ("Con 2", p_con env c2)])
|
adamc@171
|
843 | DuplicatePatternVariable (loc, s) =>
|
adamc@171
|
844 ErrorMsg.errorAt loc ("Duplicate pattern variable " ^ s)
|
adamc@171
|
845 | PatUnify (p, c1, c2, uerr) =>
|
adamc@171
|
846 (ErrorMsg.errorAt (#2 p) "Unification failure for pattern";
|
adamc@171
|
847 eprefaces' [("Pattern", p_pat env p),
|
adamc@171
|
848 ("Have con", p_con env c1),
|
adamc@171
|
849 ("Need con", p_con env c2)];
|
adamc@171
|
850 cunifyError env uerr)
|
adamc@174
|
851 | UnboundConstructor (loc, ms, s) =>
|
adamc@174
|
852 ErrorMsg.errorAt loc ("Unbound constructor " ^ String.concatWith "." (ms @ [s]) ^ " in pattern")
|
adamc@171
|
853 | PatHasArg loc =>
|
adamc@171
|
854 ErrorMsg.errorAt loc "Constructor expects no argument but is used with argument"
|
adamc@171
|
855 | PatHasNoArg loc =>
|
adamc@171
|
856 ErrorMsg.errorAt loc "Constructor expects argument but is used with no argument"
|
adamc@172
|
857 | Inexhaustive loc =>
|
adamc@172
|
858 ErrorMsg.errorAt loc "Inexhaustive 'case'"
|
adamc@10
|
859
|
adamc@86
|
860 fun checkCon (env, denv) e c1 c2 =
|
adamc@86
|
861 unifyCons (env, denv) c1 c2
|
adamc@10
|
862 handle CUnify (c1, c2, err) =>
|
adamc@86
|
863 (expError env (Unify (e, c1, c2, err));
|
adamc@86
|
864 [])
|
adamc@10
|
865
|
adamc@171
|
866 fun checkPatCon (env, denv) p c1 c2 =
|
adamc@171
|
867 unifyCons (env, denv) c1 c2
|
adamc@171
|
868 handle CUnify (c1, c2, err) =>
|
adamc@171
|
869 (expError env (PatUnify (p, c1, c2, err));
|
adamc@171
|
870 [])
|
adamc@171
|
871
|
adamc@14
|
872 fun primType env p =
|
adamc@56
|
873 case p of
|
adamc@56
|
874 P.Int _ => !int
|
adamc@56
|
875 | P.Float _ => !float
|
adamc@56
|
876 | P.String _ => !string
|
adamc@14
|
877
|
adamc@71
|
878 fun recCons (k, nm, v, rest, loc) =
|
adamc@71
|
879 (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc),
|
adamc@71
|
880 rest), loc)
|
adamc@71
|
881
|
adamc@71
|
882 fun foldType (dom, loc) =
|
adamc@71
|
883 (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc),
|
adamc@71
|
884 (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc),
|
adamc@71
|
885 (L'.TCFun (L'.Explicit, "v", dom,
|
adamc@71
|
886 (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc),
|
adamc@71
|
887 (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc),
|
adamc@71
|
888 (L'.CApp ((L'.CRel 3, loc),
|
adamc@71
|
889 recCons (dom,
|
adamc@71
|
890 (L'.CRel 2, loc),
|
adamc@71
|
891 (L'.CRel 1, loc),
|
adamc@71
|
892 (L'.CRel 0, loc),
|
adamc@71
|
893 loc)), loc)), loc)),
|
adamc@71
|
894 loc)), loc)), loc),
|
adamc@71
|
895 (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc),
|
adamc@71
|
896 (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc),
|
adamc@71
|
897 (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)),
|
adamc@71
|
898 loc)), loc)), loc)
|
adamc@71
|
899
|
adamc@15
|
900 fun typeof env (e, loc) =
|
adamc@15
|
901 case e of
|
adamc@56
|
902 L'.EPrim p => primType env p
|
adamc@15
|
903 | L'.ERel n => #2 (E.lookupERel env n)
|
adamc@15
|
904 | L'.ENamed n => #2 (E.lookupENamed env n)
|
adamc@34
|
905 | L'.EModProj (n, ms, x) =>
|
adamc@34
|
906 let
|
adamc@34
|
907 val (_, sgn) = E.lookupStrNamed env n
|
adamc@34
|
908 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
909 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@147
|
910 NONE => raise Fail "typeof: Unknown substructure"
|
adamc@34
|
911 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
912 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
913 in
|
adamc@34
|
914 case E.projectVal env {sgn = sgn, str = str, field = x} of
|
adamc@34
|
915 NONE => raise Fail "typeof: Unknown val in structure"
|
adamc@34
|
916 | SOME t => t
|
adamc@34
|
917 end
|
adamc@34
|
918
|
adamc@15
|
919 | L'.EApp (e1, _) =>
|
adamc@15
|
920 (case #1 (typeof env e1) of
|
adamc@15
|
921 L'.TFun (_, c) => c
|
adamc@15
|
922 | _ => raise Fail "typeof: Bad EApp")
|
adamc@26
|
923 | L'.EAbs (_, _, ran, _) => ran
|
adamc@15
|
924 | L'.ECApp (e1, c) =>
|
adamc@15
|
925 (case #1 (typeof env e1) of
|
adamc@15
|
926 L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1
|
adamc@15
|
927 | _ => raise Fail "typeof: Bad ECApp")
|
adamc@15
|
928 | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc)
|
adamc@15
|
929
|
adamc@29
|
930 | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, _, t) => (x, t)) xes), loc), loc)
|
adamc@15
|
931 | L'.EField (_, _, {field, ...}) => field
|
adamc@149
|
932 | L'.ECut (_, _, {rest, ...}) => (L'.TRecord rest, loc)
|
adamc@71
|
933 | L'.EFold dom => foldType (dom, loc)
|
adamc@15
|
934
|
adamc@171
|
935 | L'.ECase (_, _, t) => t
|
adamc@171
|
936
|
adamc@15
|
937 | L'.EError => cerror
|
adamc@15
|
938
|
adamc@86
|
939 fun elabHead (env, denv) (e as (_, loc)) t =
|
adamc@15
|
940 let
|
adamc@15
|
941 fun unravel (t, e) =
|
adamc@86
|
942 let
|
adamc@86
|
943 val (t, gs) = hnormCon (env, denv) t
|
adamc@86
|
944 in
|
adamc@86
|
945 case t of
|
adamc@86
|
946 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
|
adamc@86
|
947 let
|
adamc@86
|
948 val u = cunif (loc, k)
|
adamc@86
|
949
|
adamc@86
|
950 val (e, t, gs') = unravel (subConInCon (0, u) t',
|
adamc@86
|
951 (L'.ECApp (e, u), loc))
|
adamc@86
|
952 in
|
adamc@86
|
953 (e, t, gs @ gs')
|
adamc@86
|
954 end
|
adamc@86
|
955 | _ => (e, t, gs)
|
adamc@86
|
956 end
|
adamc@15
|
957 in
|
adamc@15
|
958 unravel (t, e)
|
adamc@15
|
959 end
|
adamc@15
|
960
|
adamc@174
|
961 fun elabPat (pAll as (p, loc), (env, denv, bound)) =
|
adamc@171
|
962 let
|
adamc@171
|
963 val perror = (L'.PWild, loc)
|
adamc@171
|
964 val terror = (L'.CError, loc)
|
adamc@171
|
965 val pterror = (perror, terror)
|
adamc@171
|
966 val rerror = (pterror, (env, bound))
|
adamc@171
|
967
|
adamc@171
|
968 fun pcon (pc, po, to, dn) =
|
adamc@171
|
969
|
adamc@171
|
970 case (po, to) of
|
adamc@171
|
971 (NONE, SOME _) => (expError env (PatHasNoArg loc);
|
adamc@171
|
972 rerror)
|
adamc@171
|
973 | (SOME _, NONE) => (expError env (PatHasArg loc);
|
adamc@171
|
974 rerror)
|
adamc@174
|
975 | (NONE, NONE) => (((L'.PCon (pc, NONE), loc), dn),
|
adamc@171
|
976 (env, bound))
|
adamc@171
|
977 | (SOME p, SOME t) =>
|
adamc@171
|
978 let
|
adamc@174
|
979 val ((p', pt), (env, bound)) = elabPat (p, (env, denv, bound))
|
adamc@171
|
980 in
|
adamc@174
|
981 (((L'.PCon (pc, SOME p'), loc), dn),
|
adamc@171
|
982 (env, bound))
|
adamc@171
|
983 end
|
adamc@171
|
984 in
|
adamc@171
|
985 case p of
|
adamc@171
|
986 L.PWild => (((L'.PWild, loc), cunif (loc, (L'.KType, loc))),
|
adamc@171
|
987 (env, bound))
|
adamc@171
|
988 | L.PVar x =>
|
adamc@171
|
989 let
|
adamc@171
|
990 val t = if SS.member (bound, x) then
|
adamc@171
|
991 (expError env (DuplicatePatternVariable (loc, x));
|
adamc@171
|
992 terror)
|
adamc@171
|
993 else
|
adamc@171
|
994 cunif (loc, (L'.KType, loc))
|
adamc@171
|
995 in
|
adamc@171
|
996 (((L'.PVar x, loc), t),
|
adamc@171
|
997 (E.pushERel env x t, SS.add (bound, x)))
|
adamc@171
|
998 end
|
adamc@173
|
999 | L.PPrim p => (((L'.PPrim p, loc), primType env p),
|
adamc@173
|
1000 (env, bound))
|
adamc@171
|
1001 | L.PCon ([], x, po) =>
|
adamc@171
|
1002 (case E.lookupConstructor env x of
|
adamc@174
|
1003 NONE => (expError env (UnboundConstructor (loc, [], x));
|
adamc@171
|
1004 rerror)
|
adamc@174
|
1005 | SOME (n, to, dn) => pcon (L'.PConVar n, po, to, (L'.CNamed dn, loc)))
|
adamc@174
|
1006 | L.PCon (m1 :: ms, x, po) =>
|
adamc@174
|
1007 (case E.lookupStr env m1 of
|
adamc@174
|
1008 NONE => (expError env (UnboundStrInExp (loc, m1));
|
adamc@174
|
1009 rerror)
|
adamc@174
|
1010 | SOME (n, sgn) =>
|
adamc@174
|
1011 let
|
adamc@174
|
1012 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@174
|
1013 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@174
|
1014 NONE => raise Fail "typeof: Unknown substructure"
|
adamc@174
|
1015 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@174
|
1016 ((L'.StrVar n, loc), sgn) ms
|
adamc@174
|
1017 in
|
adamc@174
|
1018 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
|
adamc@174
|
1019 NONE => (expError env (UnboundConstructor (loc, m1 :: ms, x));
|
adamc@174
|
1020 rerror)
|
adamc@174
|
1021 | SOME (_, to, dn) => pcon (L'.PConProj (n, ms, x), po, to, dn)
|
adamc@174
|
1022 end)
|
adamc@174
|
1023
|
adamc@174
|
1024 | L.PRecord _ => raise Fail "Elaborate PRecord"
|
adamc@171
|
1025 end
|
adamc@171
|
1026
|
adamc@172
|
1027 datatype coverage =
|
adamc@172
|
1028 Wild
|
adamc@173
|
1029 | None
|
adamc@172
|
1030 | Datatype of coverage IM.map
|
adamc@172
|
1031
|
adamc@172
|
1032 fun exhaustive (env, denv, t, ps) =
|
adamc@172
|
1033 let
|
adamc@172
|
1034 fun pcCoverage pc =
|
adamc@172
|
1035 case pc of
|
adamc@172
|
1036 L'.PConVar n => n
|
adamc@174
|
1037 | L'.PConProj (m1, ms, x) =>
|
adamc@174
|
1038 let
|
adamc@174
|
1039 val (str, sgn) = E.chaseMpath env (m1, ms)
|
adamc@174
|
1040 in
|
adamc@174
|
1041 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
|
adamc@174
|
1042 NONE => raise Fail "exhaustive: Can't project constructor"
|
adamc@174
|
1043 | SOME (n, _, _) => n
|
adamc@174
|
1044 end
|
adamc@172
|
1045
|
adamc@172
|
1046 fun coverage (p, _) =
|
adamc@172
|
1047 case p of
|
adamc@172
|
1048 L'.PWild => Wild
|
adamc@172
|
1049 | L'.PVar _ => Wild
|
adamc@173
|
1050 | L'.PPrim _ => None
|
adamc@172
|
1051 | L'.PCon (pc, NONE) => Datatype (IM.insert (IM.empty, pcCoverage pc, Wild))
|
adamc@172
|
1052 | L'.PCon (pc, SOME p) => Datatype (IM.insert (IM.empty, pcCoverage pc, coverage p))
|
adamc@172
|
1053
|
adamc@172
|
1054 fun merge (c1, c2) =
|
adamc@172
|
1055 case (c1, c2) of
|
adamc@173
|
1056 (None, _) => c2
|
adamc@173
|
1057 | (_, None) => c1
|
adamc@173
|
1058
|
adamc@173
|
1059 | (Wild, _) => Wild
|
adamc@172
|
1060 | (_, Wild) => Wild
|
adamc@172
|
1061
|
adamc@172
|
1062 | (Datatype cm1, Datatype cm2) => Datatype (IM.unionWith merge (cm1, cm2))
|
adamc@172
|
1063
|
adamc@172
|
1064 fun combinedCoverage ps =
|
adamc@172
|
1065 case ps of
|
adamc@172
|
1066 [] => raise Fail "Empty pattern list for coverage checking"
|
adamc@172
|
1067 | [p] => coverage p
|
adamc@172
|
1068 | p :: ps => merge (coverage p, combinedCoverage ps)
|
adamc@172
|
1069
|
adamc@172
|
1070 fun isTotal (c, t) =
|
adamc@172
|
1071 case c of
|
adamc@173
|
1072 None => (false, [])
|
adamc@173
|
1073 | Wild => (true, [])
|
adamc@172
|
1074 | Datatype cm =>
|
adamc@172
|
1075 let
|
adamc@172
|
1076 val ((t, _), gs) = hnormCon (env, denv) t
|
adamc@174
|
1077
|
adamc@174
|
1078 fun dtype cons =
|
adamc@174
|
1079 foldl (fn ((_, n, to), (total, gs)) =>
|
adamc@174
|
1080 case IM.find (cm, n) of
|
adamc@174
|
1081 NONE => (false, gs)
|
adamc@174
|
1082 | SOME c' =>
|
adamc@174
|
1083 case to of
|
adamc@174
|
1084 NONE => (total, gs)
|
adamc@174
|
1085 | SOME t' =>
|
adamc@174
|
1086 let
|
adamc@174
|
1087 val (total, gs') = isTotal (c', t')
|
adamc@174
|
1088 in
|
adamc@174
|
1089 (total, gs' @ gs)
|
adamc@174
|
1090 end)
|
adamc@174
|
1091 (true, gs) cons
|
adamc@172
|
1092 in
|
adamc@172
|
1093 case t of
|
adamc@172
|
1094 L'.CNamed n =>
|
adamc@172
|
1095 let
|
adamc@172
|
1096 val dt = E.lookupDatatype env n
|
adamc@172
|
1097 val cons = E.constructors dt
|
adamc@172
|
1098 in
|
adamc@174
|
1099 dtype cons
|
adamc@174
|
1100 end
|
adamc@174
|
1101 | L'.CModProj (m1, ms, x) =>
|
adamc@174
|
1102 let
|
adamc@174
|
1103 val (str, sgn) = E.chaseMpath env (m1, ms)
|
adamc@174
|
1104 in
|
adamc@174
|
1105 case E.projectDatatype env {str = str, sgn = sgn, field = x} of
|
adamc@174
|
1106 NONE => raise Fail "isTotal: Can't project datatype"
|
adamc@174
|
1107 | SOME cons => dtype cons
|
adamc@172
|
1108 end
|
adamc@172
|
1109 | L'.CError => (true, gs)
|
adamc@172
|
1110 | _ => raise Fail "isTotal: Not a datatype"
|
adamc@172
|
1111 end
|
adamc@172
|
1112 in
|
adamc@172
|
1113 isTotal (combinedCoverage ps, t)
|
adamc@172
|
1114 end
|
adamc@172
|
1115
|
adamc@92
|
1116 fun elabExp (env, denv) (eAll as (e, loc)) =
|
adamc@91
|
1117 let
|
adamc@92
|
1118
|
adamc@92
|
1119 in
|
adamc@92
|
1120 (*eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)];*)
|
adamc@10
|
1121
|
adamc@91
|
1122 case e of
|
adamc@91
|
1123 L.EAnnot (e, t) =>
|
adamc@91
|
1124 let
|
adamc@91
|
1125 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@91
|
1126 val (t', _, gs2) = elabCon (env, denv) t
|
adamc@91
|
1127 val gs3 = checkCon (env, denv) e' et t'
|
adamc@91
|
1128 in
|
adamc@91
|
1129 (e', t', gs1 @ gs2 @ gs3)
|
adamc@91
|
1130 end
|
adamc@34
|
1131
|
adamc@91
|
1132 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
|
adamc@91
|
1133 | L.EVar ([], s) =>
|
adamc@91
|
1134 (case E.lookupE env s of
|
adamc@91
|
1135 E.NotBound =>
|
adamc@91
|
1136 (expError env (UnboundExp (loc, s));
|
adamc@91
|
1137 (eerror, cerror, []))
|
adamc@91
|
1138 | E.Rel (n, t) => ((L'.ERel n, loc), t, [])
|
adamc@91
|
1139 | E.Named (n, t) => ((L'.ENamed n, loc), t, []))
|
adamc@91
|
1140 | L.EVar (m1 :: ms, s) =>
|
adamc@91
|
1141 (case E.lookupStr env m1 of
|
adamc@91
|
1142 NONE => (expError env (UnboundStrInExp (loc, m1));
|
adamc@91
|
1143 (eerror, cerror, []))
|
adamc@91
|
1144 | SOME (n, sgn) =>
|
adamc@91
|
1145 let
|
adamc@91
|
1146 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@91
|
1147 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@91
|
1148 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@91
|
1149 (strerror, sgnerror))
|
adamc@91
|
1150 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@91
|
1151 ((L'.StrVar n, loc), sgn) ms
|
adamc@10
|
1152
|
adamc@91
|
1153 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
|
adamc@91
|
1154 NONE => (expError env (UnboundExp (loc, s));
|
adamc@91
|
1155 cerror)
|
adamc@91
|
1156 | SOME t => t
|
adamc@91
|
1157 in
|
adamc@91
|
1158 ((L'.EModProj (n, ms, s), loc), t, [])
|
adamc@91
|
1159 end)
|
adamc@86
|
1160
|
adamc@91
|
1161 | L.EApp (e1, e2) =>
|
adamc@91
|
1162 let
|
adamc@91
|
1163 val (e1', t1, gs1) = elabExp (env, denv) e1
|
adamc@91
|
1164 val (e1', t1, gs2) = elabHead (env, denv) e1' t1
|
adamc@91
|
1165 val (e2', t2, gs3) = elabExp (env, denv) e2
|
adamc@71
|
1166
|
adamc@91
|
1167 val dom = cunif (loc, ktype)
|
adamc@91
|
1168 val ran = cunif (loc, ktype)
|
adamc@91
|
1169 val t = (L'.TFun (dom, ran), dummy)
|
adamc@91
|
1170
|
adamc@91
|
1171 val gs4 = checkCon (env, denv) e1' t1 t
|
adamc@91
|
1172 val gs5 = checkCon (env, denv) e2' t2 dom
|
adamc@91
|
1173 in
|
adamc@91
|
1174 ((L'.EApp (e1', e2'), loc), ran, gs1 @ gs2 @ gs3 @ gs4 @ gs5)
|
adamc@91
|
1175 end
|
adamc@91
|
1176 | L.EAbs (x, to, e) =>
|
adamc@91
|
1177 let
|
adamc@91
|
1178 val (t', gs1) = case to of
|
adamc@91
|
1179 NONE => (cunif (loc, ktype), [])
|
adamc@91
|
1180 | SOME t =>
|
adamc@91
|
1181 let
|
adamc@91
|
1182 val (t', tk, gs) = elabCon (env, denv) t
|
adamc@91
|
1183 in
|
adamc@91
|
1184 checkKind env t' tk ktype;
|
adamc@91
|
1185 (t', gs)
|
adamc@91
|
1186 end
|
adamc@91
|
1187 val (e', et, gs2) = elabExp (E.pushERel env x t', denv) e
|
adamc@91
|
1188 in
|
adamc@91
|
1189 ((L'.EAbs (x, t', et, e'), loc),
|
adamc@91
|
1190 (L'.TFun (t', et), loc),
|
adamc@91
|
1191 gs1 @ gs2)
|
adamc@91
|
1192 end
|
adamc@91
|
1193 | L.ECApp (e, c) =>
|
adamc@91
|
1194 let
|
adamc@91
|
1195 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@91
|
1196 val (e', et, gs2) = elabHead (env, denv) e' et
|
adamc@91
|
1197 val (c', ck, gs3) = elabCon (env, denv) c
|
adamc@91
|
1198 val ((et', _), gs4) = hnormCon (env, denv) et
|
adamc@91
|
1199 in
|
adamc@91
|
1200 case et' of
|
adamc@91
|
1201 L'.CError => (eerror, cerror, [])
|
adamc@91
|
1202 | L'.TCFun (_, _, k, eb) =>
|
adamc@91
|
1203 let
|
adamc@91
|
1204 val () = checkKind env c' ck k
|
adamc@91
|
1205 val eb' = subConInCon (0, c') eb
|
adamc@91
|
1206 handle SynUnif => (expError env (Unif ("substitution", eb));
|
adamc@91
|
1207 cerror)
|
adamc@91
|
1208 in
|
adamc@91
|
1209 ((L'.ECApp (e', c'), loc), eb', gs1 @ gs2 @ gs3 @ gs4)
|
adamc@91
|
1210 end
|
adamc@91
|
1211
|
adamc@91
|
1212 | L'.CUnif _ =>
|
adamc@91
|
1213 (expError env (Unif ("application", et));
|
adamc@91
|
1214 (eerror, cerror, []))
|
adamc@91
|
1215
|
adamc@91
|
1216 | _ =>
|
adamc@91
|
1217 (expError env (WrongForm ("constructor function", e', et));
|
adamc@91
|
1218 (eerror, cerror, []))
|
adamc@91
|
1219 end
|
adamc@91
|
1220 | L.ECAbs (expl, x, k, e) =>
|
adamc@91
|
1221 let
|
adamc@91
|
1222 val expl' = elabExplicitness expl
|
adamc@91
|
1223 val k' = elabKind k
|
adamc@91
|
1224 val (e', et, gs) = elabExp (E.pushCRel env x k', D.enter denv) e
|
adamc@91
|
1225 in
|
adamc@91
|
1226 ((L'.ECAbs (expl', x, k', e'), loc),
|
adamc@91
|
1227 (L'.TCFun (expl', x, k', et), loc),
|
adamc@91
|
1228 gs)
|
adamc@91
|
1229 end
|
adamc@91
|
1230
|
adamc@91
|
1231 | L.EDisjoint (c1, c2, e) =>
|
adamc@91
|
1232 let
|
adamc@91
|
1233 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@91
|
1234 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@91
|
1235
|
adamc@91
|
1236 val ku1 = kunif loc
|
adamc@91
|
1237 val ku2 = kunif loc
|
adamc@91
|
1238
|
adamc@91
|
1239 val (denv', gs3) = D.assert env denv (c1', c2')
|
adamc@91
|
1240 val (e', t, gs4) = elabExp (env, denv') e
|
adamc@91
|
1241 in
|
adamc@91
|
1242 checkKind env c1' k1 (L'.KRecord ku1, loc);
|
adamc@91
|
1243 checkKind env c2' k2 (L'.KRecord ku2, loc);
|
adamc@91
|
1244
|
adamc@91
|
1245 (e', (L'.TDisjoint (c1', c2', t), loc), gs1 @ gs2 @ gs3 @ gs4)
|
adamc@91
|
1246 end
|
adamc@91
|
1247
|
adamc@91
|
1248 | L.ERecord xes =>
|
adamc@91
|
1249 let
|
adamc@91
|
1250 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
|
adamc@91
|
1251 let
|
adamc@91
|
1252 val (x', xk, gs1) = elabCon (env, denv) x
|
adamc@91
|
1253 val (e', et, gs2) = elabExp (env, denv) e
|
adamc@91
|
1254 in
|
adamc@91
|
1255 checkKind env x' xk kname;
|
adamc@91
|
1256 ((x', e', et), gs1 @ gs2 @ gs)
|
adamc@91
|
1257 end)
|
adamc@91
|
1258 [] xes
|
adamc@91
|
1259
|
adamc@91
|
1260 val k = (L'.KType, loc)
|
adamc@91
|
1261
|
adamc@91
|
1262 fun prove (xets, gs) =
|
adamc@91
|
1263 case xets of
|
adamc@91
|
1264 [] => gs
|
adamc@91
|
1265 | (x, _, t) :: rest =>
|
adamc@91
|
1266 let
|
adamc@91
|
1267 val xc = (x, t)
|
adamc@91
|
1268 val r1 = (L'.CRecord (k, [xc]), loc)
|
adamc@91
|
1269 val gs = foldl (fn ((x', _, t'), gs) =>
|
adamc@91
|
1270 let
|
adamc@91
|
1271 val xc' = (x', t')
|
adamc@91
|
1272 val r2 = (L'.CRecord (k, [xc']), loc)
|
adamc@91
|
1273 in
|
adamc@91
|
1274 D.prove env denv (r1, r2, loc) @ gs
|
adamc@91
|
1275 end)
|
adamc@91
|
1276 gs rest
|
adamc@91
|
1277 in
|
adamc@91
|
1278 prove (rest, gs)
|
adamc@91
|
1279 end
|
adamc@91
|
1280 in
|
adamc@91
|
1281 ((L'.ERecord xes', loc),
|
adamc@91
|
1282 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
|
adamc@91
|
1283 prove (xes', gs))
|
adamc@91
|
1284 end
|
adamc@91
|
1285
|
adamc@91
|
1286 | L.EField (e, c) =>
|
adamc@91
|
1287 let
|
adamc@91
|
1288 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@91
|
1289 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@91
|
1290
|
adamc@91
|
1291 val ft = cunif (loc, ktype)
|
adamc@91
|
1292 val rest = cunif (loc, ktype_record)
|
adamc@91
|
1293 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
|
adamc@91
|
1294
|
adamc@91
|
1295 val gs3 =
|
adamc@91
|
1296 checkCon (env, denv) e' et
|
adamc@91
|
1297 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
|
adamc@91
|
1298 val gs4 = D.prove env denv (first, rest, loc)
|
adamc@91
|
1299 in
|
adamc@91
|
1300 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ gs2 @ gs3 @ gs4)
|
adamc@91
|
1301 end
|
adamc@91
|
1302
|
adamc@149
|
1303 | L.ECut (e, c) =>
|
adamc@149
|
1304 let
|
adamc@149
|
1305 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@149
|
1306 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@149
|
1307
|
adamc@149
|
1308 val ft = cunif (loc, ktype)
|
adamc@149
|
1309 val rest = cunif (loc, ktype_record)
|
adamc@149
|
1310 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
|
adamc@149
|
1311
|
adamc@149
|
1312 val gs3 =
|
adamc@149
|
1313 checkCon (env, denv) e' et
|
adamc@149
|
1314 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
|
adamc@149
|
1315 val gs4 = D.prove env denv (first, rest, loc)
|
adamc@149
|
1316 in
|
adamc@149
|
1317 ((L'.ECut (e', c', {field = ft, rest = rest}), loc), (L'.TRecord rest, loc), gs1 @ gs2 @ gs3 @ gs4)
|
adamc@149
|
1318 end
|
adamc@149
|
1319
|
adamc@91
|
1320 | L.EFold =>
|
adamc@91
|
1321 let
|
adamc@91
|
1322 val dom = kunif loc
|
adamc@91
|
1323 in
|
adamc@91
|
1324 ((L'.EFold dom, loc), foldType (dom, loc), [])
|
adamc@91
|
1325 end
|
adamc@170
|
1326
|
adamc@171
|
1327 | L.ECase (e, pes) =>
|
adamc@171
|
1328 let
|
adamc@171
|
1329 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@171
|
1330 val result = cunif (loc, (L'.KType, loc))
|
adamc@171
|
1331 val (pes', gs) = ListUtil.foldlMap
|
adamc@171
|
1332 (fn ((p, e), gs) =>
|
adamc@171
|
1333 let
|
adamc@174
|
1334 val ((p', pt), (env, _)) = elabPat (p, (env, denv, SS.empty))
|
adamc@171
|
1335
|
adamc@171
|
1336 val gs1 = checkPatCon (env, denv) p' pt et
|
adamc@171
|
1337 val (e', et, gs2) = elabExp (env, denv) e
|
adamc@171
|
1338 val gs3 = checkCon (env, denv) e' et result
|
adamc@171
|
1339 in
|
adamc@171
|
1340 ((p', e'), gs1 @ gs2 @ gs3 @ gs)
|
adamc@171
|
1341 end)
|
adamc@171
|
1342 gs1 pes
|
adamc@172
|
1343
|
adamc@172
|
1344 val (total, gs') = exhaustive (env, denv, et, map #1 pes')
|
adamc@171
|
1345 in
|
adamc@172
|
1346 if total then
|
adamc@172
|
1347 ()
|
adamc@172
|
1348 else
|
adamc@172
|
1349 expError env (Inexhaustive loc);
|
adamc@172
|
1350
|
adamc@172
|
1351 ((L'.ECase (e', pes', result), loc), result, gs' @ gs)
|
adamc@171
|
1352 end
|
adamc@91
|
1353 end
|
adamc@12
|
1354
|
adamc@12
|
1355
|
adamc@6
|
1356 datatype decl_error =
|
adamc@91
|
1357 KunifsRemain of L'.decl list
|
adamc@91
|
1358 | CunifsRemain of L'.decl list
|
adamc@91
|
1359
|
adamc@91
|
1360 fun lspan [] = ErrorMsg.dummySpan
|
adamc@91
|
1361 | lspan ((_, loc) :: _) = loc
|
adamc@6
|
1362
|
adamc@6
|
1363 fun declError env err =
|
adamc@6
|
1364 case err of
|
adamc@91
|
1365 KunifsRemain ds =>
|
adamc@91
|
1366 (ErrorMsg.errorAt (lspan ds) "Some kind unification variables are undetermined in declaration";
|
adamc@91
|
1367 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
|
adamc@91
|
1368 | CunifsRemain ds =>
|
adamc@91
|
1369 (ErrorMsg.errorAt (lspan ds) "Some constructor unification variables are undetermined in declaration";
|
adamc@91
|
1370 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
|
adamc@6
|
1371
|
adamc@31
|
1372 datatype sgn_error =
|
adamc@31
|
1373 UnboundSgn of ErrorMsg.span * string
|
adamc@31
|
1374 | UnmatchedSgi of L'.sgn_item
|
adamc@31
|
1375 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
|
adamc@31
|
1376 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
|
adamc@156
|
1377 | SgiMismatchedDatatypes of L'.sgn_item * L'.sgn_item * (L'.con * L'.con * cunify_error) option
|
adamc@41
|
1378 | SgnWrongForm of L'.sgn * L'.sgn
|
adamc@42
|
1379 | UnWhereable of L'.sgn * string
|
adamc@75
|
1380 | WhereWrongKind of L'.kind * L'.kind * kunify_error
|
adamc@58
|
1381 | NotIncludable of L'.sgn
|
adamc@62
|
1382 | DuplicateCon of ErrorMsg.span * string
|
adamc@62
|
1383 | DuplicateVal of ErrorMsg.span * string
|
adamc@62
|
1384 | DuplicateSgn of ErrorMsg.span * string
|
adamc@62
|
1385 | DuplicateStr of ErrorMsg.span * string
|
adamc@88
|
1386 | NotConstraintsable of L'.sgn
|
adamc@31
|
1387
|
adamc@31
|
1388 fun sgnError env err =
|
adamc@31
|
1389 case err of
|
adamc@31
|
1390 UnboundSgn (loc, s) =>
|
adamc@31
|
1391 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
|
adamc@31
|
1392 | UnmatchedSgi (sgi as (_, loc)) =>
|
adamc@31
|
1393 (ErrorMsg.errorAt loc "Unmatched signature item";
|
adamc@31
|
1394 eprefaces' [("Item", p_sgn_item env sgi)])
|
adamc@31
|
1395 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
|
adamc@31
|
1396 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
|
adamc@59
|
1397 eprefaces' [("Have", p_sgn_item env sgi1),
|
adamc@59
|
1398 ("Need", p_sgn_item env sgi2),
|
adamc@31
|
1399 ("Kind 1", p_kind k1),
|
adamc@31
|
1400 ("Kind 2", p_kind k2)];
|
adamc@31
|
1401 kunifyError kerr)
|
adamc@31
|
1402 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
|
adamc@31
|
1403 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
|
adamc@59
|
1404 eprefaces' [("Have", p_sgn_item env sgi1),
|
adamc@59
|
1405 ("Need", p_sgn_item env sgi2),
|
adamc@31
|
1406 ("Con 1", p_con env c1),
|
adamc@31
|
1407 ("Con 2", p_con env c2)];
|
adamc@31
|
1408 cunifyError env cerr)
|
adamc@156
|
1409 | SgiMismatchedDatatypes (sgi1, sgi2, cerro) =>
|
adamc@156
|
1410 (ErrorMsg.errorAt (#2 sgi1) "Mismatched 'datatype' specifications:";
|
adamc@156
|
1411 eprefaces' [("Have", p_sgn_item env sgi1),
|
adamc@156
|
1412 ("Need", p_sgn_item env sgi2)];
|
adamc@156
|
1413 Option.app (fn (c1, c2, ue) =>
|
adamc@156
|
1414 (eprefaces "Unification error"
|
adamc@156
|
1415 [("Con 1", p_con env c1),
|
adamc@156
|
1416 ("Con 2", p_con env c2)];
|
adamc@156
|
1417 cunifyError env ue)) cerro)
|
adamc@41
|
1418 | SgnWrongForm (sgn1, sgn2) =>
|
adamc@41
|
1419 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
|
adamc@41
|
1420 eprefaces' [("Sig 1", p_sgn env sgn1),
|
adamc@41
|
1421 ("Sig 2", p_sgn env sgn2)])
|
adamc@42
|
1422 | UnWhereable (sgn, x) =>
|
adamc@42
|
1423 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
|
adamc@42
|
1424 eprefaces' [("Signature", p_sgn env sgn),
|
adamc@42
|
1425 ("Field", PD.string x)])
|
adamc@75
|
1426 | WhereWrongKind (k1, k2, kerr) =>
|
adamc@75
|
1427 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
|
adamc@75
|
1428 eprefaces' [("Have", p_kind k1),
|
adamc@75
|
1429 ("Need", p_kind k2)];
|
adamc@75
|
1430 kunifyError kerr)
|
adamc@58
|
1431 | NotIncludable sgn =>
|
adamc@58
|
1432 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
|
adamc@58
|
1433 eprefaces' [("Signature", p_sgn env sgn)])
|
adamc@62
|
1434 | DuplicateCon (loc, s) =>
|
adamc@62
|
1435 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
|
adamc@62
|
1436 | DuplicateVal (loc, s) =>
|
adamc@62
|
1437 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
|
adamc@62
|
1438 | DuplicateSgn (loc, s) =>
|
adamc@62
|
1439 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
|
adamc@62
|
1440 | DuplicateStr (loc, s) =>
|
adamc@62
|
1441 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
|
adamc@88
|
1442 | NotConstraintsable sgn =>
|
adamc@88
|
1443 (ErrorMsg.errorAt (#2 sgn) "Invalid signature for 'open constraints'";
|
adamc@88
|
1444 eprefaces' [("Signature", p_sgn env sgn)])
|
adamc@31
|
1445
|
adamc@31
|
1446 datatype str_error =
|
adamc@31
|
1447 UnboundStr of ErrorMsg.span * string
|
adamc@44
|
1448 | NotFunctor of L'.sgn
|
adamc@46
|
1449 | FunctorRebind of ErrorMsg.span
|
adamc@61
|
1450 | UnOpenable of L'.sgn
|
adamc@75
|
1451 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
|
adamc@156
|
1452 | DuplicateConstructor of string * ErrorMsg.span
|
adamc@158
|
1453 | NotDatatype of ErrorMsg.span
|
adamc@31
|
1454
|
adamc@31
|
1455 fun strError env err =
|
adamc@31
|
1456 case err of
|
adamc@31
|
1457 UnboundStr (loc, s) =>
|
adamc@31
|
1458 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
|
adamc@44
|
1459 | NotFunctor sgn =>
|
adamc@44
|
1460 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
|
adamc@44
|
1461 eprefaces' [("Signature", p_sgn env sgn)])
|
adamc@46
|
1462 | FunctorRebind loc =>
|
adamc@46
|
1463 ErrorMsg.errorAt loc "Attempt to rebind functor"
|
adamc@61
|
1464 | UnOpenable sgn =>
|
adamc@61
|
1465 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
|
adamc@61
|
1466 eprefaces' [("Signature", p_sgn env sgn)])
|
adamc@75
|
1467 | NotType (k, (k1, k2, ue)) =>
|
adamc@75
|
1468 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
|
adamc@75
|
1469 eprefaces' [("Kind", p_kind k),
|
adamc@75
|
1470 ("Subkind 1", p_kind k1),
|
adamc@75
|
1471 ("Subkind 2", p_kind k2)];
|
adamc@75
|
1472 kunifyError ue)
|
adamc@156
|
1473 | DuplicateConstructor (x, loc) =>
|
adamc@156
|
1474 ErrorMsg.errorAt loc ("Duplicate datatype constructor " ^ x)
|
adamc@158
|
1475 | NotDatatype loc =>
|
adamc@158
|
1476 ErrorMsg.errorAt loc "Trying to import non-datatype as a datatype"
|
adamc@31
|
1477
|
adamc@42
|
1478 val hnormSgn = E.hnormSgn
|
adamc@31
|
1479
|
adamc@88
|
1480 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
|
adamc@76
|
1481 case sgi of
|
adamc@76
|
1482 L.SgiConAbs (x, k) =>
|
adamc@76
|
1483 let
|
adamc@76
|
1484 val k' = elabKind k
|
adamc@31
|
1485
|
adamc@76
|
1486 val (env', n) = E.pushCNamed env x k' NONE
|
adamc@76
|
1487 in
|
adamc@88
|
1488 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
|
adamc@76
|
1489 end
|
adamc@31
|
1490
|
adamc@76
|
1491 | L.SgiCon (x, ko, c) =>
|
adamc@76
|
1492 let
|
adamc@76
|
1493 val k' = case ko of
|
adamc@76
|
1494 NONE => kunif loc
|
adamc@76
|
1495 | SOME k => elabKind k
|
adamc@31
|
1496
|
adamc@83
|
1497 val (c', ck, gs') = elabCon (env, denv) c
|
adamc@76
|
1498 val (env', n) = E.pushCNamed env x k' (SOME c')
|
adamc@76
|
1499 in
|
adamc@76
|
1500 checkKind env c' ck k';
|
adamc@31
|
1501
|
adamc@88
|
1502 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
1503 end
|
adamc@31
|
1504
|
adamc@157
|
1505 | L.SgiDatatype (x, xcs) =>
|
adamc@157
|
1506 let
|
adamc@157
|
1507 val k = (L'.KType, loc)
|
adamc@157
|
1508 val (env, n) = E.pushCNamed env x k NONE
|
adamc@157
|
1509 val t = (L'.CNamed n, loc)
|
adamc@157
|
1510
|
adamc@157
|
1511 val (xcs, (used, env, gs)) =
|
adamc@157
|
1512 ListUtil.foldlMap
|
adamc@157
|
1513 (fn ((x, to), (used, env, gs)) =>
|
adamc@157
|
1514 let
|
adamc@157
|
1515 val (to, t, gs') = case to of
|
adamc@157
|
1516 NONE => (NONE, t, gs)
|
adamc@157
|
1517 | SOME t' =>
|
adamc@157
|
1518 let
|
adamc@157
|
1519 val (t', tk, gs') = elabCon (env, denv) t'
|
adamc@157
|
1520 in
|
adamc@157
|
1521 checkKind env t' tk k;
|
adamc@157
|
1522 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
|
adamc@157
|
1523 end
|
adamc@157
|
1524
|
adamc@157
|
1525 val (env, n') = E.pushENamed env x t
|
adamc@157
|
1526 in
|
adamc@157
|
1527 if SS.member (used, x) then
|
adamc@157
|
1528 strError env (DuplicateConstructor (x, loc))
|
adamc@157
|
1529 else
|
adamc@157
|
1530 ();
|
adamc@157
|
1531 ((x, n', to), (SS.add (used, x), env, gs'))
|
adamc@157
|
1532 end)
|
adamc@157
|
1533 (SS.empty, env, []) xcs
|
adamc@157
|
1534 in
|
adamc@157
|
1535 ([(L'.SgiDatatype (x, n, xcs), loc)], (env, denv, gs))
|
adamc@157
|
1536 end
|
adamc@156
|
1537
|
adamc@158
|
1538 | L.SgiDatatypeImp (_, [], _) => raise Fail "Empty SgiDatatypeImp"
|
adamc@158
|
1539
|
adamc@158
|
1540 | L.SgiDatatypeImp (x, m1 :: ms, s) =>
|
adamc@158
|
1541 (case E.lookupStr env m1 of
|
adamc@158
|
1542 NONE => (strError env (UnboundStr (loc, m1));
|
adamc@158
|
1543 ([], (env, denv, gs)))
|
adamc@158
|
1544 | SOME (n, sgn) =>
|
adamc@158
|
1545 let
|
adamc@158
|
1546 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@158
|
1547 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@158
|
1548 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@158
|
1549 (strerror, sgnerror))
|
adamc@158
|
1550 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@158
|
1551 ((L'.StrVar n, loc), sgn) ms
|
adamc@158
|
1552 in
|
adamc@161
|
1553 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
|
adamc@161
|
1554 ((L'.CModProj (n, ms, s), _), gs) =>
|
adamc@161
|
1555 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
|
adamc@161
|
1556 NONE => (conError env (UnboundDatatype (loc, s));
|
adamc@161
|
1557 ([], (env, denv, gs)))
|
adamc@161
|
1558 | SOME xncs =>
|
adamc@161
|
1559 let
|
adamc@161
|
1560 val k = (L'.KType, loc)
|
adamc@161
|
1561 val t = (L'.CModProj (n, ms, s), loc)
|
adamc@161
|
1562 val (env, n') = E.pushCNamed env x k (SOME t)
|
adamc@161
|
1563 val env = E.pushDatatype env n' xncs
|
adamc@158
|
1564
|
adamc@161
|
1565 val t = (L'.CNamed n', loc)
|
adamc@161
|
1566 val env = foldl (fn ((x, n, to), env) =>
|
adamc@161
|
1567 let
|
adamc@161
|
1568 val t = case to of
|
adamc@161
|
1569 NONE => t
|
adamc@161
|
1570 | SOME t' => (L'.TFun (t', t), loc)
|
adamc@161
|
1571 in
|
adamc@161
|
1572 E.pushENamedAs env x n t
|
adamc@161
|
1573 end) env xncs
|
adamc@161
|
1574 in
|
adamc@162
|
1575 ([(L'.SgiDatatypeImp (x, n', n, ms, s, xncs), loc)], (env, denv, gs))
|
adamc@161
|
1576 end)
|
adamc@161
|
1577 | _ => (strError env (NotDatatype loc);
|
adamc@161
|
1578 ([], (env, denv, [])))
|
adamc@158
|
1579 end)
|
adamc@156
|
1580
|
adamc@76
|
1581 | L.SgiVal (x, c) =>
|
adamc@76
|
1582 let
|
adamc@83
|
1583 val (c', ck, gs') = elabCon (env, denv) c
|
adamc@31
|
1584
|
adamc@76
|
1585 val (env', n) = E.pushENamed env x c'
|
adamc@76
|
1586 in
|
adamc@76
|
1587 (unifyKinds ck ktype
|
adamc@76
|
1588 handle KUnify ue => strError env (NotType (ck, ue)));
|
adamc@31
|
1589
|
adamc@88
|
1590 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
1591 end
|
adamc@31
|
1592
|
adamc@76
|
1593 | L.SgiStr (x, sgn) =>
|
adamc@76
|
1594 let
|
adamc@83
|
1595 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
1596 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@76
|
1597 in
|
adamc@88
|
1598 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
1599 end
|
adamc@31
|
1600
|
adamc@76
|
1601 | L.SgiSgn (x, sgn) =>
|
adamc@76
|
1602 let
|
adamc@83
|
1603 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
1604 val (env', n) = E.pushSgnNamed env x sgn'
|
adamc@76
|
1605 in
|
adamc@88
|
1606 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
1607 end
|
adamc@31
|
1608
|
adamc@76
|
1609 | L.SgiInclude sgn =>
|
adamc@76
|
1610 let
|
adamc@83
|
1611 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
1612 in
|
adamc@76
|
1613 case #1 (hnormSgn env sgn') of
|
adamc@76
|
1614 L'.SgnConst sgis =>
|
adamc@88
|
1615 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
|
adamc@76
|
1616 | _ => (sgnError env (NotIncludable sgn');
|
adamc@88
|
1617 ([], (env, denv, [])))
|
adamc@88
|
1618 end
|
adamc@88
|
1619
|
adamc@88
|
1620 | L.SgiConstraint (c1, c2) =>
|
adamc@88
|
1621 let
|
adamc@88
|
1622 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@88
|
1623 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@88
|
1624
|
adamc@90
|
1625 val (denv, gs3) = D.assert env denv (c1', c2')
|
adamc@88
|
1626 in
|
adamc@88
|
1627 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
|
adamc@88
|
1628 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
|
adamc@88
|
1629
|
adamc@90
|
1630 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2 @ gs3))
|
adamc@76
|
1631 end
|
adamc@31
|
1632
|
adamc@83
|
1633 and elabSgn (env, denv) (sgn, loc) =
|
adamc@31
|
1634 case sgn of
|
adamc@31
|
1635 L.SgnConst sgis =>
|
adamc@31
|
1636 let
|
adamc@88
|
1637 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
|
adamc@62
|
1638
|
adamc@62
|
1639 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
|
adamc@62
|
1640 case sgi of
|
adamc@62
|
1641 L'.SgiConAbs (x, _, _) =>
|
adamc@62
|
1642 (if SS.member (cons, x) then
|
adamc@62
|
1643 sgnError env (DuplicateCon (loc, x))
|
adamc@62
|
1644 else
|
adamc@62
|
1645 ();
|
adamc@62
|
1646 (SS.add (cons, x), vals, sgns, strs))
|
adamc@62
|
1647 | L'.SgiCon (x, _, _, _) =>
|
adamc@62
|
1648 (if SS.member (cons, x) then
|
adamc@62
|
1649 sgnError env (DuplicateCon (loc, x))
|
adamc@62
|
1650 else
|
adamc@62
|
1651 ();
|
adamc@62
|
1652 (SS.add (cons, x), vals, sgns, strs))
|
adamc@156
|
1653 | L'.SgiDatatype (x, _, xncs) =>
|
adamc@156
|
1654 let
|
adamc@156
|
1655 val vals = foldl (fn ((x, _, _), vals) =>
|
adamc@156
|
1656 (if SS.member (vals, x) then
|
adamc@156
|
1657 sgnError env (DuplicateVal (loc, x))
|
adamc@156
|
1658 else
|
adamc@156
|
1659 ();
|
adamc@156
|
1660 SS.add (vals, x)))
|
adamc@156
|
1661 vals xncs
|
adamc@156
|
1662 in
|
adamc@156
|
1663 if SS.member (cons, x) then
|
adamc@156
|
1664 sgnError env (DuplicateCon (loc, x))
|
adamc@156
|
1665 else
|
adamc@156
|
1666 ();
|
adamc@156
|
1667 (SS.add (cons, x), vals, sgns, strs)
|
adamc@156
|
1668 end
|
adamc@162
|
1669 | L'.SgiDatatypeImp (x, _, _, _, _, _) =>
|
adamc@156
|
1670 (if SS.member (cons, x) then
|
adamc@156
|
1671 sgnError env (DuplicateCon (loc, x))
|
adamc@156
|
1672 else
|
adamc@156
|
1673 ();
|
adamc@156
|
1674 (SS.add (cons, x), vals, sgns, strs))
|
adamc@62
|
1675 | L'.SgiVal (x, _, _) =>
|
adamc@62
|
1676 (if SS.member (vals, x) then
|
adamc@62
|
1677 sgnError env (DuplicateVal (loc, x))
|
adamc@62
|
1678 else
|
adamc@62
|
1679 ();
|
adamc@62
|
1680 (cons, SS.add (vals, x), sgns, strs))
|
adamc@62
|
1681 | L'.SgiSgn (x, _, _) =>
|
adamc@62
|
1682 (if SS.member (sgns, x) then
|
adamc@62
|
1683 sgnError env (DuplicateSgn (loc, x))
|
adamc@62
|
1684 else
|
adamc@62
|
1685 ();
|
adamc@62
|
1686 (cons, vals, SS.add (sgns, x), strs))
|
adamc@62
|
1687 | L'.SgiStr (x, _, _) =>
|
adamc@62
|
1688 (if SS.member (strs, x) then
|
adamc@62
|
1689 sgnError env (DuplicateStr (loc, x))
|
adamc@62
|
1690 else
|
adamc@62
|
1691 ();
|
adamc@88
|
1692 (cons, vals, sgns, SS.add (strs, x)))
|
adamc@88
|
1693 | L'.SgiConstraint _ => (cons, vals, sgns, strs))
|
adamc@62
|
1694 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
|
adamc@31
|
1695 in
|
adamc@83
|
1696 ((L'.SgnConst sgis', loc), gs)
|
adamc@31
|
1697 end
|
adamc@31
|
1698 | L.SgnVar x =>
|
adamc@31
|
1699 (case E.lookupSgn env x of
|
adamc@31
|
1700 NONE =>
|
adamc@31
|
1701 (sgnError env (UnboundSgn (loc, x));
|
adamc@83
|
1702 ((L'.SgnError, loc), []))
|
adamc@83
|
1703 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
|
adamc@41
|
1704 | L.SgnFun (m, dom, ran) =>
|
adamc@41
|
1705 let
|
adamc@83
|
1706 val (dom', gs1) = elabSgn (env, denv) dom
|
adamc@41
|
1707 val (env', n) = E.pushStrNamed env m dom'
|
adamc@83
|
1708 val (ran', gs2) = elabSgn (env', denv) ran
|
adamc@41
|
1709 in
|
adamc@83
|
1710 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
|
adamc@41
|
1711 end
|
adamc@42
|
1712 | L.SgnWhere (sgn, x, c) =>
|
adamc@42
|
1713 let
|
adamc@83
|
1714 val (sgn', ds1) = elabSgn (env, denv) sgn
|
adamc@83
|
1715 val (c', ck, ds2) = elabCon (env, denv) c
|
adamc@42
|
1716 in
|
adamc@42
|
1717 case #1 (hnormSgn env sgn') of
|
adamc@83
|
1718 L'.SgnError => (sgnerror, [])
|
adamc@42
|
1719 | L'.SgnConst sgis =>
|
adamc@75
|
1720 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
|
adamc@75
|
1721 x' = x andalso
|
adamc@75
|
1722 (unifyKinds k ck
|
adamc@75
|
1723 handle KUnify x => sgnError env (WhereWrongKind x);
|
adamc@42
|
1724 true)
|
adamc@42
|
1725 | _ => false) sgis then
|
adamc@83
|
1726 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
|
adamc@42
|
1727 else
|
adamc@42
|
1728 (sgnError env (UnWhereable (sgn', x));
|
adamc@83
|
1729 (sgnerror, []))
|
adamc@42
|
1730 | _ => (sgnError env (UnWhereable (sgn', x));
|
adamc@83
|
1731 (sgnerror, []))
|
adamc@42
|
1732 end
|
adamc@59
|
1733 | L.SgnProj (m, ms, x) =>
|
adamc@59
|
1734 (case E.lookupStr env m of
|
adamc@59
|
1735 NONE => (strError env (UnboundStr (loc, m));
|
adamc@83
|
1736 (sgnerror, []))
|
adamc@59
|
1737 | SOME (n, sgn) =>
|
adamc@59
|
1738 let
|
adamc@59
|
1739 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@59
|
1740 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@59
|
1741 NONE => (strError env (UnboundStr (loc, m));
|
adamc@59
|
1742 (strerror, sgnerror))
|
adamc@59
|
1743 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@59
|
1744 ((L'.StrVar n, loc), sgn) ms
|
adamc@59
|
1745 in
|
adamc@59
|
1746 case E.projectSgn env {sgn = sgn, str = str, field = x} of
|
adamc@59
|
1747 NONE => (sgnError env (UnboundSgn (loc, x));
|
adamc@83
|
1748 (sgnerror, []))
|
adamc@83
|
1749 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
|
adamc@59
|
1750 end)
|
adamc@59
|
1751
|
adamc@31
|
1752
|
adamc@66
|
1753 fun selfify env {str, strs, sgn} =
|
adamc@66
|
1754 case #1 (hnormSgn env sgn) of
|
adamc@66
|
1755 L'.SgnError => sgn
|
adamc@66
|
1756 | L'.SgnVar _ => sgn
|
adamc@66
|
1757
|
adamc@66
|
1758 | L'.SgnConst sgis =>
|
adamc@66
|
1759 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
|
adamc@66
|
1760 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
|
adamc@158
|
1761 | (L'.SgiDatatype (x, n, xncs), loc) =>
|
adamc@162
|
1762 (L'.SgiDatatypeImp (x, n, str, strs, x, xncs), loc)
|
adamc@66
|
1763 | (L'.SgiStr (x, n, sgn), loc) =>
|
adamc@66
|
1764 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
|
adamc@66
|
1765 | x => x) sgis), #2 sgn)
|
adamc@66
|
1766 | L'.SgnFun _ => sgn
|
adamc@66
|
1767 | L'.SgnWhere _ => sgn
|
adamc@66
|
1768 | L'.SgnProj (m, ms, x) =>
|
adamc@66
|
1769 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
|
adamc@66
|
1770 (L'.StrVar m, #2 sgn) ms,
|
adamc@66
|
1771 sgn = #2 (E.lookupStrNamed env m),
|
adamc@66
|
1772 field = x} of
|
adamc@66
|
1773 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
|
adamc@66
|
1774 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
|
adamc@66
|
1775
|
adamc@66
|
1776 fun selfifyAt env {str, sgn} =
|
adamc@66
|
1777 let
|
adamc@66
|
1778 fun self (str, _) =
|
adamc@66
|
1779 case str of
|
adamc@66
|
1780 L'.StrVar x => SOME (x, [])
|
adamc@66
|
1781 | L'.StrProj (str, x) =>
|
adamc@66
|
1782 (case self str of
|
adamc@66
|
1783 NONE => NONE
|
adamc@66
|
1784 | SOME (m, ms) => SOME (m, ms @ [x]))
|
adamc@66
|
1785 | _ => NONE
|
adamc@66
|
1786 in
|
adamc@66
|
1787 case self str of
|
adamc@66
|
1788 NONE => sgn
|
adamc@66
|
1789 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
|
adamc@66
|
1790 end
|
adamc@66
|
1791
|
adamc@88
|
1792 fun dopen (env, denv) {str, strs, sgn} =
|
adamc@66
|
1793 let
|
adamc@66
|
1794 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
|
adamc@66
|
1795 (L'.StrVar str, #2 sgn) strs
|
adamc@66
|
1796 in
|
adamc@66
|
1797 case #1 (hnormSgn env sgn) of
|
adamc@66
|
1798 L'.SgnConst sgis =>
|
adamc@88
|
1799 ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) =>
|
adamc@162
|
1800 let
|
adamc@162
|
1801 val d =
|
adamc@162
|
1802 case sgi of
|
adamc@162
|
1803 L'.SgiConAbs (x, n, k) =>
|
adamc@162
|
1804 let
|
adamc@162
|
1805 val c = (L'.CModProj (str, strs, x), loc)
|
adamc@162
|
1806 in
|
adamc@162
|
1807 (L'.DCon (x, n, k, c), loc)
|
adamc@162
|
1808 end
|
adamc@162
|
1809 | L'.SgiCon (x, n, k, c) =>
|
adamc@162
|
1810 (L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
|
adamc@162
|
1811 | L'.SgiDatatype (x, n, xncs) =>
|
adamc@162
|
1812 (L'.DDatatypeImp (x, n, str, strs, x, xncs), loc)
|
adamc@162
|
1813 | L'.SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
|
adamc@162
|
1814 (L'.DDatatypeImp (x, n, m1, ms, x', xncs), loc)
|
adamc@162
|
1815 | L'.SgiVal (x, n, t) =>
|
adamc@162
|
1816 (L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc)
|
adamc@162
|
1817 | L'.SgiStr (x, n, sgn) =>
|
adamc@162
|
1818 (L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc)
|
adamc@162
|
1819 | L'.SgiSgn (x, n, sgn) =>
|
adamc@162
|
1820 (L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc)
|
adamc@162
|
1821 | L'.SgiConstraint (c1, c2) =>
|
adamc@162
|
1822 (L'.DConstraint (c1, c2), loc)
|
adamc@162
|
1823 in
|
adamc@162
|
1824 (d, (E.declBinds env' d, denv'))
|
adamc@162
|
1825 end)
|
adamc@88
|
1826 (env, denv) sgis
|
adamc@66
|
1827 | _ => (strError env (UnOpenable sgn);
|
adamc@88
|
1828 ([], (env, denv)))
|
adamc@66
|
1829 end
|
adamc@66
|
1830
|
adamc@88
|
1831 fun dopenConstraints (loc, env, denv) {str, strs} =
|
adamc@88
|
1832 case E.lookupStr env str of
|
adamc@88
|
1833 NONE => (strError env (UnboundStr (loc, str));
|
adamc@88
|
1834 denv)
|
adamc@88
|
1835 | SOME (n, sgn) =>
|
adamc@88
|
1836 let
|
adamc@88
|
1837 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@88
|
1838 case E.projectStr env {str = str, sgn = sgn, field = m} of
|
adamc@88
|
1839 NONE => (strError env (UnboundStr (loc, m));
|
adamc@88
|
1840 (strerror, sgnerror))
|
adamc@88
|
1841 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@88
|
1842 ((L'.StrVar n, loc), sgn) strs
|
adamc@88
|
1843
|
adamc@88
|
1844 val cso = E.projectConstraints env {sgn = sgn, str = st}
|
adamc@88
|
1845
|
adamc@88
|
1846 val denv = case cso of
|
adamc@88
|
1847 NONE => (strError env (UnboundStr (loc, str));
|
adamc@88
|
1848 denv)
|
adamc@90
|
1849 | SOME cs => foldl (fn ((c1, c2), denv) =>
|
adamc@90
|
1850 let
|
adamc@90
|
1851 val (denv, gs) = D.assert env denv (c1, c2)
|
adamc@90
|
1852 in
|
adamc@90
|
1853 case gs of
|
adamc@90
|
1854 [] => ()
|
adamc@90
|
1855 | _ => raise Fail "dopenConstraints: Sub-constraints remain";
|
adamc@90
|
1856
|
adamc@90
|
1857 denv
|
adamc@90
|
1858 end) denv cs
|
adamc@88
|
1859 in
|
adamc@88
|
1860 denv
|
adamc@88
|
1861 end
|
adamc@88
|
1862
|
adamc@31
|
1863 fun sgiOfDecl (d, loc) =
|
adamc@31
|
1864 case d of
|
adamc@123
|
1865 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
|
adamc@156
|
1866 | L'.DDatatype x => [(L'.SgiDatatype x, loc)]
|
adamc@156
|
1867 | L'.DDatatypeImp x => [(L'.SgiDatatypeImp x, loc)]
|
adamc@123
|
1868 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
|
adamc@123
|
1869 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
|
adamc@123
|
1870 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
|
adamc@123
|
1871 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
|
adamc@123
|
1872 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
|
adamc@123
|
1873 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
|
adamc@123
|
1874 | L'.DExport _ => []
|
adamc@88
|
1875
|
adamc@88
|
1876 fun sgiBindsD (env, denv) (sgi, _) =
|
adamc@88
|
1877 case sgi of
|
adamc@90
|
1878 L'.SgiConstraint (c1, c2) =>
|
adamc@90
|
1879 (case D.assert env denv (c1, c2) of
|
adamc@90
|
1880 (denv, []) => denv
|
adamc@90
|
1881 | _ => raise Fail "sgiBindsD: Sub-constraints remain")
|
adamc@88
|
1882 | _ => denv
|
adamc@31
|
1883
|
adamc@86
|
1884 fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) =
|
adamc@35
|
1885 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
|
adamc@31
|
1886 (L'.SgnError, _) => ()
|
adamc@31
|
1887 | (_, L'.SgnError) => ()
|
adamc@31
|
1888
|
adamc@31
|
1889 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
|
adamc@31
|
1890 let
|
adamc@156
|
1891 fun folder (sgi2All as (sgi, loc), (env, denv)) =
|
adamc@31
|
1892 let
|
adamc@31
|
1893 fun seek p =
|
adamc@31
|
1894 let
|
adamc@88
|
1895 fun seek (env, denv) ls =
|
adamc@31
|
1896 case ls of
|
adamc@31
|
1897 [] => (sgnError env (UnmatchedSgi sgi2All);
|
adamc@88
|
1898 (env, denv))
|
adamc@31
|
1899 | h :: t =>
|
adamc@31
|
1900 case p h of
|
adamc@88
|
1901 NONE => seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t
|
adamc@88
|
1902 | SOME envs => envs
|
adamc@31
|
1903 in
|
adamc@88
|
1904 seek (env, denv) sgis1
|
adamc@31
|
1905 end
|
adamc@31
|
1906 in
|
adamc@31
|
1907 case sgi of
|
adamc@31
|
1908 L'.SgiConAbs (x, n2, k2) =>
|
adamc@31
|
1909 seek (fn sgi1All as (sgi1, _) =>
|
adamc@31
|
1910 let
|
adamc@41
|
1911 fun found (x', n1, k1, co1) =
|
adamc@41
|
1912 if x = x' then
|
adamc@41
|
1913 let
|
adamc@41
|
1914 val () = unifyKinds k1 k2
|
adamc@41
|
1915 handle KUnify (k1, k2, err) =>
|
adamc@41
|
1916 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
|
adamc@41
|
1917 val env = E.pushCNamedAs env x n1 k1 co1
|
adamc@41
|
1918 in
|
adamc@41
|
1919 SOME (if n1 = n2 then
|
adamc@41
|
1920 env
|
adamc@41
|
1921 else
|
adamc@88
|
1922 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)),
|
adamc@88
|
1923 denv)
|
adamc@41
|
1924 end
|
adamc@41
|
1925 else
|
adamc@41
|
1926 NONE
|
adamc@31
|
1927 in
|
adamc@31
|
1928 case sgi1 of
|
adamc@41
|
1929 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
|
adamc@41
|
1930 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
|
adamc@159
|
1931 | L'.SgiDatatype (x', n1, _) => found (x', n1, (L'.KType, loc), NONE)
|
adamc@162
|
1932 | L'.SgiDatatypeImp (x', n1, m1, ms, s, _) =>
|
adamc@159
|
1933 found (x', n1, (L'.KType, loc), SOME (L'.CModProj (m1, ms, s), loc))
|
adamc@31
|
1934 | _ => NONE
|
adamc@31
|
1935 end)
|
adamc@31
|
1936
|
adamc@31
|
1937 | L'.SgiCon (x, n2, k2, c2) =>
|
adamc@31
|
1938 seek (fn sgi1All as (sgi1, _) =>
|
adamc@31
|
1939 case sgi1 of
|
adamc@41
|
1940 L'.SgiCon (x', n1, k1, c1) =>
|
adamc@41
|
1941 if x = x' then
|
adamc@41
|
1942 let
|
adamc@88
|
1943 fun good () = SOME (E.pushCNamedAs env x n2 k2 (SOME c2), denv)
|
adamc@41
|
1944 in
|
adamc@86
|
1945 (case unifyCons (env, denv) c1 c2 of
|
adamc@86
|
1946 [] => good ()
|
adamc@86
|
1947 | _ => NONE)
|
adamc@86
|
1948 handle CUnify (c1, c2, err) =>
|
adamc@86
|
1949 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
|
adamc@86
|
1950 good ())
|
adamc@41
|
1951 end
|
adamc@41
|
1952 else
|
adamc@41
|
1953 NONE
|
adamc@31
|
1954 | _ => NONE)
|
adamc@31
|
1955
|
adamc@156
|
1956 | L'.SgiDatatype (x, n2, xncs2) =>
|
adamc@156
|
1957 seek (fn sgi1All as (sgi1, _) =>
|
adamc@160
|
1958 let
|
adamc@160
|
1959 fun found (n1, xncs1) =
|
adamc@160
|
1960 let
|
adamc@160
|
1961 fun mismatched ue =
|
adamc@160
|
1962 (sgnError env (SgiMismatchedDatatypes (sgi1All, sgi2All, ue));
|
adamc@160
|
1963 SOME (env, denv))
|
adamc@156
|
1964
|
adamc@160
|
1965 fun good () =
|
adamc@160
|
1966 let
|
adamc@160
|
1967 val env = E.sgiBinds env sgi2All
|
adamc@160
|
1968 val env = if n1 = n2 then
|
adamc@160
|
1969 env
|
adamc@160
|
1970 else
|
adamc@160
|
1971 E.pushCNamedAs env x n1 (L'.KType, loc)
|
adamc@160
|
1972 (SOME (L'.CNamed n1, loc))
|
adamc@160
|
1973 in
|
adamc@160
|
1974 SOME (env, denv)
|
adamc@160
|
1975 end
|
adamc@156
|
1976
|
adamc@160
|
1977 fun xncBad ((x1, _, t1), (x2, _, t2)) =
|
adamc@160
|
1978 String.compare (x1, x2) <> EQUAL
|
adamc@160
|
1979 orelse case (t1, t2) of
|
adamc@160
|
1980 (NONE, NONE) => false
|
adamc@160
|
1981 | (SOME t1, SOME t2) =>
|
adamc@160
|
1982 not (List.null (unifyCons (env, denv) t1 t2))
|
adamc@160
|
1983 | _ => true
|
adamc@160
|
1984 in
|
adamc@160
|
1985 (if length xncs1 <> length xncs2
|
adamc@160
|
1986 orelse ListPair.exists xncBad (xncs1, xncs2) then
|
adamc@160
|
1987 mismatched NONE
|
adamc@160
|
1988 else
|
adamc@160
|
1989 good ())
|
adamc@160
|
1990 handle CUnify ue => mismatched (SOME ue)
|
adamc@160
|
1991 end
|
adamc@160
|
1992 in
|
adamc@160
|
1993 case sgi1 of
|
adamc@160
|
1994 L'.SgiDatatype (x', n1, xncs1) =>
|
adamc@160
|
1995 if x' = x then
|
adamc@160
|
1996 found (n1, xncs1)
|
adamc@160
|
1997 else
|
adamc@160
|
1998 NONE
|
adamc@162
|
1999 | L'.SgiDatatypeImp (x', n1, _, _, _, xncs1) =>
|
adamc@162
|
2000 if x' = x then
|
adamc@162
|
2001 found (n1, xncs1)
|
adamc@162
|
2002 else
|
adamc@162
|
2003 NONE
|
adamc@160
|
2004 | _ => NONE
|
adamc@160
|
2005 end)
|
adamc@156
|
2006
|
adamc@162
|
2007 | L'.SgiDatatypeImp (x, n2, m12, ms2, s2, _) =>
|
adamc@158
|
2008 seek (fn sgi1All as (sgi1, _) =>
|
adamc@158
|
2009 case sgi1 of
|
adamc@162
|
2010 L'.SgiDatatypeImp (x', n1, m11, ms1, s1, _) =>
|
adamc@158
|
2011 if x = x' then
|
adamc@158
|
2012 let
|
adamc@158
|
2013 val k = (L'.KType, loc)
|
adamc@158
|
2014 val t1 = (L'.CModProj (m11, ms1, s1), loc)
|
adamc@158
|
2015 val t2 = (L'.CModProj (m12, ms2, s2), loc)
|
adamc@158
|
2016
|
adamc@158
|
2017 fun good () =
|
adamc@158
|
2018 let
|
adamc@158
|
2019 val env = E.pushCNamedAs env x n1 k (SOME t1)
|
adamc@158
|
2020 val env = E.pushCNamedAs env x n2 k (SOME t2)
|
adamc@158
|
2021 in
|
adamc@158
|
2022 SOME (env, denv)
|
adamc@158
|
2023 end
|
adamc@158
|
2024 in
|
adamc@158
|
2025 (case unifyCons (env, denv) t1 t2 of
|
adamc@158
|
2026 [] => good ()
|
adamc@158
|
2027 | _ => NONE)
|
adamc@158
|
2028 handle CUnify (c1, c2, err) =>
|
adamc@158
|
2029 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
|
adamc@158
|
2030 good ())
|
adamc@158
|
2031 end
|
adamc@158
|
2032 else
|
adamc@158
|
2033 NONE
|
adamc@158
|
2034
|
adamc@158
|
2035 | _ => NONE)
|
adamc@156
|
2036
|
adamc@32
|
2037 | L'.SgiVal (x, n2, c2) =>
|
adamc@32
|
2038 seek (fn sgi1All as (sgi1, _) =>
|
adamc@32
|
2039 case sgi1 of
|
adamc@41
|
2040 L'.SgiVal (x', n1, c1) =>
|
adamc@41
|
2041 if x = x' then
|
adamc@86
|
2042 (case unifyCons (env, denv) c1 c2 of
|
adamc@88
|
2043 [] => SOME (env, denv)
|
adamc@86
|
2044 | _ => NONE)
|
adamc@86
|
2045 handle CUnify (c1, c2, err) =>
|
adamc@86
|
2046 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
|
adamc@88
|
2047 SOME (env, denv))
|
adamc@41
|
2048 else
|
adamc@41
|
2049 NONE
|
adamc@32
|
2050 | _ => NONE)
|
adamc@32
|
2051
|
adamc@33
|
2052 | L'.SgiStr (x, n2, sgn2) =>
|
adamc@33
|
2053 seek (fn sgi1All as (sgi1, _) =>
|
adamc@33
|
2054 case sgi1 of
|
adamc@41
|
2055 L'.SgiStr (x', n1, sgn1) =>
|
adamc@41
|
2056 if x = x' then
|
adamc@66
|
2057 let
|
adamc@86
|
2058 val () = subSgn (env, denv) sgn1 sgn2
|
adamc@66
|
2059 val env = E.pushStrNamedAs env x n1 sgn1
|
adamc@66
|
2060 val env = if n1 = n2 then
|
adamc@66
|
2061 env
|
adamc@66
|
2062 else
|
adamc@66
|
2063 E.pushStrNamedAs env x n2
|
adamc@66
|
2064 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
|
adamc@66
|
2065 sgn = sgn2})
|
adamc@66
|
2066 in
|
adamc@88
|
2067 SOME (env, denv)
|
adamc@66
|
2068 end
|
adamc@41
|
2069 else
|
adamc@41
|
2070 NONE
|
adamc@33
|
2071 | _ => NONE)
|
adamc@59
|
2072
|
adamc@59
|
2073 | L'.SgiSgn (x, n2, sgn2) =>
|
adamc@59
|
2074 seek (fn sgi1All as (sgi1, _) =>
|
adamc@59
|
2075 case sgi1 of
|
adamc@59
|
2076 L'.SgiSgn (x', n1, sgn1) =>
|
adamc@59
|
2077 if x = x' then
|
adamc@65
|
2078 let
|
adamc@86
|
2079 val () = subSgn (env, denv) sgn1 sgn2
|
adamc@86
|
2080 val () = subSgn (env, denv) sgn2 sgn1
|
adamc@65
|
2081
|
adamc@65
|
2082 val env = E.pushSgnNamedAs env x n2 sgn2
|
adamc@65
|
2083 val env = if n1 = n2 then
|
adamc@65
|
2084 env
|
adamc@65
|
2085 else
|
adamc@65
|
2086 E.pushSgnNamedAs env x n1 sgn2
|
adamc@65
|
2087 in
|
adamc@88
|
2088 SOME (env, denv)
|
adamc@65
|
2089 end
|
adamc@59
|
2090 else
|
adamc@59
|
2091 NONE
|
adamc@59
|
2092 | _ => NONE)
|
adamc@88
|
2093
|
adamc@88
|
2094 | L'.SgiConstraint (c2, d2) =>
|
adamc@88
|
2095 seek (fn sgi1All as (sgi1, _) =>
|
adamc@88
|
2096 case sgi1 of
|
adamc@88
|
2097 L'.SgiConstraint (c1, d1) =>
|
adamc@88
|
2098 if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then
|
adamc@90
|
2099 let
|
adamc@90
|
2100 val (denv, gs) = D.assert env denv (c2, d2)
|
adamc@90
|
2101 in
|
adamc@90
|
2102 case gs of
|
adamc@90
|
2103 [] => ()
|
adamc@90
|
2104 | _ => raise Fail "subSgn: Sub-constraints remain";
|
adamc@90
|
2105
|
adamc@90
|
2106 SOME (env, denv)
|
adamc@90
|
2107 end
|
adamc@88
|
2108 else
|
adamc@88
|
2109 NONE
|
adamc@88
|
2110 | _ => NONE)
|
adamc@31
|
2111 end
|
adamc@31
|
2112 in
|
adamc@88
|
2113 ignore (foldl folder (env, denv) sgis2)
|
adamc@31
|
2114 end
|
adamc@31
|
2115
|
adamc@41
|
2116 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
|
adamc@43
|
2117 let
|
adamc@43
|
2118 val ran1 =
|
adamc@43
|
2119 if n1 = n2 then
|
adamc@43
|
2120 ran1
|
adamc@43
|
2121 else
|
adamc@43
|
2122 subStrInSgn (n1, n2) ran1
|
adamc@43
|
2123 in
|
adamc@86
|
2124 subSgn (env, denv) dom2 dom1;
|
adamc@86
|
2125 subSgn (E.pushStrNamedAs env m2 n2 dom2, denv) ran1 ran2
|
adamc@43
|
2126 end
|
adamc@41
|
2127
|
adamc@41
|
2128 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
|
adamc@41
|
2129
|
adamc@61
|
2130
|
adamc@88
|
2131 fun elabDecl ((d, loc), (env, denv, gs)) =
|
adamc@76
|
2132 case d of
|
adamc@76
|
2133 L.DCon (x, ko, c) =>
|
adamc@76
|
2134 let
|
adamc@76
|
2135 val k' = case ko of
|
adamc@76
|
2136 NONE => kunif loc
|
adamc@76
|
2137 | SOME k => elabKind k
|
adamc@76
|
2138
|
adamc@83
|
2139 val (c', ck, gs') = elabCon (env, denv) c
|
adamc@76
|
2140 val (env', n) = E.pushCNamed env x k' (SOME c')
|
adamc@76
|
2141 in
|
adamc@76
|
2142 checkKind env c' ck k';
|
adamc@76
|
2143
|
adamc@88
|
2144 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
2145 end
|
adamc@156
|
2146 | L.DDatatype (x, xcs) =>
|
adamc@156
|
2147 let
|
adamc@156
|
2148 val k = (L'.KType, loc)
|
adamc@156
|
2149 val (env, n) = E.pushCNamed env x k NONE
|
adamc@156
|
2150 val t = (L'.CNamed n, loc)
|
adamc@156
|
2151
|
adamc@156
|
2152 val (xcs, (used, env, gs)) =
|
adamc@156
|
2153 ListUtil.foldlMap
|
adamc@156
|
2154 (fn ((x, to), (used, env, gs)) =>
|
adamc@156
|
2155 let
|
adamc@156
|
2156 val (to, t, gs') = case to of
|
adamc@156
|
2157 NONE => (NONE, t, gs)
|
adamc@156
|
2158 | SOME t' =>
|
adamc@156
|
2159 let
|
adamc@156
|
2160 val (t', tk, gs') = elabCon (env, denv) t'
|
adamc@156
|
2161 in
|
adamc@156
|
2162 checkKind env t' tk k;
|
adamc@156
|
2163 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
|
adamc@156
|
2164 end
|
adamc@156
|
2165
|
adamc@156
|
2166 val (env, n') = E.pushENamed env x t
|
adamc@156
|
2167 in
|
adamc@156
|
2168 if SS.member (used, x) then
|
adamc@156
|
2169 strError env (DuplicateConstructor (x, loc))
|
adamc@156
|
2170 else
|
adamc@156
|
2171 ();
|
adamc@156
|
2172 ((x, n', to), (SS.add (used, x), env, gs'))
|
adamc@156
|
2173 end)
|
adamc@156
|
2174 (SS.empty, env, []) xcs
|
adamc@171
|
2175
|
adamc@171
|
2176 val env = E.pushDatatype env n xcs
|
adamc@156
|
2177 in
|
adamc@156
|
2178 ([(L'.DDatatype (x, n, xcs), loc)], (env, denv, gs))
|
adamc@156
|
2179 end
|
adamc@156
|
2180
|
adamc@157
|
2181 | L.DDatatypeImp (_, [], _) => raise Fail "Empty DDatatypeImp"
|
adamc@157
|
2182
|
adamc@157
|
2183 | L.DDatatypeImp (x, m1 :: ms, s) =>
|
adamc@157
|
2184 (case E.lookupStr env m1 of
|
adamc@157
|
2185 NONE => (expError env (UnboundStrInExp (loc, m1));
|
adamc@157
|
2186 ([], (env, denv, gs)))
|
adamc@157
|
2187 | SOME (n, sgn) =>
|
adamc@157
|
2188 let
|
adamc@157
|
2189 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@157
|
2190 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@157
|
2191 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@157
|
2192 (strerror, sgnerror))
|
adamc@157
|
2193 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@157
|
2194 ((L'.StrVar n, loc), sgn) ms
|
adamc@157
|
2195 in
|
adamc@158
|
2196 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
|
adamc@158
|
2197 ((L'.CModProj (n, ms, s), _), gs) =>
|
adamc@158
|
2198 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
|
adamc@158
|
2199 NONE => (conError env (UnboundDatatype (loc, s));
|
adamc@158
|
2200 ([], (env, denv, gs)))
|
adamc@158
|
2201 | SOME xncs =>
|
adamc@158
|
2202 let
|
adamc@158
|
2203 val k = (L'.KType, loc)
|
adamc@158
|
2204 val t = (L'.CModProj (n, ms, s), loc)
|
adamc@158
|
2205 val (env, n') = E.pushCNamed env x k (SOME t)
|
adamc@158
|
2206 val env = E.pushDatatype env n' xncs
|
adamc@157
|
2207
|
adamc@158
|
2208 val t = (L'.CNamed n', loc)
|
adamc@158
|
2209 val env = foldl (fn ((x, n, to), env) =>
|
adamc@158
|
2210 let
|
adamc@158
|
2211 val t = case to of
|
adamc@158
|
2212 NONE => t
|
adamc@158
|
2213 | SOME t' => (L'.TFun (t', t), loc)
|
adamc@158
|
2214 in
|
adamc@158
|
2215 E.pushENamedAs env x n t
|
adamc@158
|
2216 end) env xncs
|
adamc@158
|
2217 in
|
adamc@162
|
2218 ([(L'.DDatatypeImp (x, n', n, ms, s, xncs), loc)], (env, denv, gs))
|
adamc@158
|
2219 end)
|
adamc@158
|
2220 | _ => (strError env (NotDatatype loc);
|
adamc@158
|
2221 ([], (env, denv, [])))
|
adamc@157
|
2222 end)
|
adamc@157
|
2223
|
adamc@76
|
2224 | L.DVal (x, co, e) =>
|
adamc@76
|
2225 let
|
adamc@123
|
2226 val (c', _, gs1) = case co of
|
adamc@83
|
2227 NONE => (cunif (loc, ktype), ktype, [])
|
adamc@83
|
2228 | SOME c => elabCon (env, denv) c
|
adamc@76
|
2229
|
adamc@83
|
2230 val (e', et, gs2) = elabExp (env, denv) e
|
adamc@76
|
2231 val (env', n) = E.pushENamed env x c'
|
adamc@86
|
2232
|
adamc@86
|
2233 val gs3 = checkCon (env, denv) e' et c'
|
adamc@76
|
2234 in
|
adamc@88
|
2235 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, gs1 @ gs2 @ gs3 @ gs))
|
adamc@76
|
2236 end
|
adamc@123
|
2237 | L.DValRec vis =>
|
adamc@123
|
2238 let
|
adamc@123
|
2239 val (vis, gs) = ListUtil.foldlMap
|
adamc@123
|
2240 (fn ((x, co, e), gs) =>
|
adamc@123
|
2241 let
|
adamc@123
|
2242 val (c', _, gs1) = case co of
|
adamc@123
|
2243 NONE => (cunif (loc, ktype), ktype, [])
|
adamc@123
|
2244 | SOME c => elabCon (env, denv) c
|
adamc@123
|
2245 in
|
adamc@123
|
2246 ((x, c', e), gs1 @ gs)
|
adamc@123
|
2247 end) [] vis
|
adamc@123
|
2248
|
adamc@123
|
2249 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
|
adamc@123
|
2250 let
|
adamc@123
|
2251 val (env, n) = E.pushENamed env x c'
|
adamc@123
|
2252 in
|
adamc@123
|
2253 ((x, n, c', e), env)
|
adamc@123
|
2254 end) env vis
|
adamc@123
|
2255
|
adamc@123
|
2256 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
|
adamc@123
|
2257 let
|
adamc@123
|
2258 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@123
|
2259
|
adamc@123
|
2260 val gs2 = checkCon (env, denv) e' et c'
|
adamc@123
|
2261 in
|
adamc@123
|
2262 ((x, n, c', e'), gs1 @ gs2 @ gs)
|
adamc@123
|
2263 end) gs vis
|
adamc@123
|
2264 in
|
adamc@123
|
2265 ([(L'.DValRec vis, loc)], (env, denv, gs))
|
adamc@123
|
2266 end
|
adamc@76
|
2267
|
adamc@76
|
2268 | L.DSgn (x, sgn) =>
|
adamc@76
|
2269 let
|
adamc@83
|
2270 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
2271 val (env', n) = E.pushSgnNamed env x sgn'
|
adamc@76
|
2272 in
|
adamc@88
|
2273 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
2274 end
|
adamc@76
|
2275
|
adamc@76
|
2276 | L.DStr (x, sgno, str) =>
|
adamc@76
|
2277 let
|
adamc@93
|
2278 val () = if x = "Basis" then
|
adamc@93
|
2279 raise Fail "Not allowed to redefine structure 'Basis'"
|
adamc@93
|
2280 else
|
adamc@93
|
2281 ()
|
adamc@93
|
2282
|
adamc@83
|
2283 val formal = Option.map (elabSgn (env, denv)) sgno
|
adamc@76
|
2284
|
adamc@83
|
2285 val (str', sgn', gs') =
|
adamc@78
|
2286 case formal of
|
adamc@78
|
2287 NONE =>
|
adamc@78
|
2288 let
|
adamc@83
|
2289 val (str', actual, ds) = elabStr (env, denv) str
|
adamc@78
|
2290 in
|
adamc@83
|
2291 (str', selfifyAt env {str = str', sgn = actual}, ds)
|
adamc@78
|
2292 end
|
adamc@83
|
2293 | SOME (formal, gs1) =>
|
adamc@78
|
2294 let
|
adamc@78
|
2295 val str =
|
adamc@78
|
2296 case #1 (hnormSgn env formal) of
|
adamc@78
|
2297 L'.SgnConst sgis =>
|
adamc@78
|
2298 (case #1 str of
|
adamc@78
|
2299 L.StrConst ds =>
|
adamc@78
|
2300 let
|
adamc@78
|
2301 val needed = foldl (fn ((sgi, _), needed) =>
|
adamc@78
|
2302 case sgi of
|
adamc@78
|
2303 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
|
adamc@78
|
2304 | _ => needed)
|
adamc@78
|
2305 SS.empty sgis
|
adamc@78
|
2306
|
adamc@78
|
2307 val needed = foldl (fn ((d, _), needed) =>
|
adamc@78
|
2308 case d of
|
adamc@78
|
2309 L.DCon (x, _, _) => (SS.delete (needed, x)
|
adamc@78
|
2310 handle NotFound => needed)
|
adamc@78
|
2311 | L.DOpen _ => SS.empty
|
adamc@78
|
2312 | _ => needed)
|
adamc@78
|
2313 needed ds
|
adamc@78
|
2314 in
|
adamc@78
|
2315 case SS.listItems needed of
|
adamc@78
|
2316 [] => str
|
adamc@78
|
2317 | xs =>
|
adamc@78
|
2318 let
|
adamc@78
|
2319 val kwild = (L.KWild, #2 str)
|
adamc@78
|
2320 val cwild = (L.CWild kwild, #2 str)
|
adamc@78
|
2321 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
|
adamc@78
|
2322 in
|
adamc@78
|
2323 (L.StrConst (ds @ ds'), #2 str)
|
adamc@78
|
2324 end
|
adamc@78
|
2325 end
|
adamc@78
|
2326 | _ => str)
|
adamc@78
|
2327 | _ => str
|
adamc@78
|
2328
|
adamc@83
|
2329 val (str', actual, gs2) = elabStr (env, denv) str
|
adamc@78
|
2330 in
|
adamc@158
|
2331 subSgn (env, denv) (selfifyAt env {str = str', sgn = actual}) formal;
|
adamc@83
|
2332 (str', formal, gs1 @ gs2)
|
adamc@78
|
2333 end
|
adamc@76
|
2334
|
adamc@76
|
2335 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@76
|
2336 in
|
adamc@76
|
2337 case #1 (hnormSgn env sgn') of
|
adamc@76
|
2338 L'.SgnFun _ =>
|
adamc@76
|
2339 (case #1 str' of
|
adamc@76
|
2340 L'.StrFun _ => ()
|
adamc@76
|
2341 | _ => strError env (FunctorRebind loc))
|
adamc@76
|
2342 | _ => ();
|
adamc@76
|
2343
|
adamc@88
|
2344 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
2345 end
|
adamc@76
|
2346
|
adamc@76
|
2347 | L.DFfiStr (x, sgn) =>
|
adamc@76
|
2348 let
|
adamc@83
|
2349 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
2350
|
adamc@76
|
2351 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@76
|
2352 in
|
adamc@88
|
2353 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
2354 end
|
adamc@76
|
2355
|
adamc@76
|
2356 | L.DOpen (m, ms) =>
|
adamc@88
|
2357 (case E.lookupStr env m of
|
adamc@88
|
2358 NONE => (strError env (UnboundStr (loc, m));
|
adamc@88
|
2359 ([], (env, denv, [])))
|
adamc@88
|
2360 | SOME (n, sgn) =>
|
adamc@88
|
2361 let
|
adamc@88
|
2362 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@88
|
2363 case E.projectStr env {str = str, sgn = sgn, field = m} of
|
adamc@88
|
2364 NONE => (strError env (UnboundStr (loc, m));
|
adamc@88
|
2365 (strerror, sgnerror))
|
adamc@88
|
2366 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@88
|
2367 ((L'.StrVar n, loc), sgn) ms
|
adamc@83
|
2368
|
adamc@88
|
2369 val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn}
|
adamc@88
|
2370 val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms}
|
adamc@88
|
2371 in
|
adamc@88
|
2372 (ds, (env', denv', []))
|
adamc@88
|
2373 end)
|
adamc@88
|
2374
|
adamc@88
|
2375 | L.DConstraint (c1, c2) =>
|
adamc@88
|
2376 let
|
adamc@88
|
2377 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@88
|
2378 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@90
|
2379 val gs3 = D.prove env denv (c1', c2', loc)
|
adamc@88
|
2380
|
adamc@90
|
2381 val (denv', gs4) = D.assert env denv (c1', c2')
|
adamc@88
|
2382 in
|
adamc@88
|
2383 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
|
adamc@88
|
2384 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
|
adamc@88
|
2385
|
adamc@90
|
2386 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', gs1 @ gs2 @ gs3 @ gs4))
|
adamc@88
|
2387 end
|
adamc@88
|
2388
|
adamc@88
|
2389 | L.DOpenConstraints (m, ms) =>
|
adamc@88
|
2390 let
|
adamc@88
|
2391 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
|
adamc@88
|
2392 in
|
adamc@88
|
2393 ([], (env, denv, []))
|
adamc@88
|
2394 end
|
adamc@3
|
2395
|
adamc@109
|
2396 | L.DExport str =>
|
adamc@100
|
2397 let
|
adamc@109
|
2398 val (str', sgn, gs) = elabStr (env, denv) str
|
adamc@100
|
2399
|
adamc@109
|
2400 val sgn =
|
adamc@109
|
2401 case #1 (hnormSgn env sgn) of
|
adamc@109
|
2402 L'.SgnConst sgis =>
|
adamc@109
|
2403 let
|
adamc@146
|
2404 fun doOne (all as (sgi, _), env) =
|
adamc@146
|
2405 (case sgi of
|
adamc@146
|
2406 L'.SgiVal (x, n, t) =>
|
adamc@146
|
2407 (case hnormCon (env, denv) t of
|
adamc@146
|
2408 ((L'.TFun (dom, ran), _), []) =>
|
adamc@146
|
2409 (case (hnormCon (env, denv) dom, hnormCon (env, denv) ran) of
|
adamc@146
|
2410 (((L'.TRecord domR, _), []),
|
adamc@146
|
2411 ((L'.CApp (tf, arg3), _), [])) =>
|
adamc@146
|
2412 (case (hnormCon (env, denv) tf, hnormCon (env, denv) arg3) of
|
adamc@146
|
2413 (((L'.CApp (tf, arg2), _), []),
|
adamc@146
|
2414 (((L'.CRecord (_, []), _), []))) =>
|
adamc@146
|
2415 (case (hnormCon (env, denv) tf) of
|
adamc@146
|
2416 ((L'.CApp (tf, arg1), _), []) =>
|
adamc@146
|
2417 (case (hnormCon (env, denv) tf,
|
adamc@146
|
2418 hnormCon (env, denv) domR,
|
adamc@152
|
2419 hnormCon (env, denv) arg1,
|
adamc@146
|
2420 hnormCon (env, denv) arg2) of
|
adamc@152
|
2421 ((tf, []), (domR, []), (arg1, []),
|
adamc@146
|
2422 ((L'.CRecord (_, []), _), [])) =>
|
adamc@146
|
2423 let
|
adamc@146
|
2424 val t = (L'.CApp (tf, arg1), loc)
|
adamc@146
|
2425 val t = (L'.CApp (t, arg2), loc)
|
adamc@146
|
2426 val t = (L'.CApp (t, arg3), loc)
|
adamc@146
|
2427 in
|
adamc@146
|
2428 (L'.SgiVal (x, n, (L'.TFun ((L'.TRecord domR, loc),
|
adamc@146
|
2429 t),
|
adamc@146
|
2430 loc)), loc)
|
adamc@146
|
2431 end
|
adamc@146
|
2432 | _ => all)
|
adamc@146
|
2433 | _ => all)
|
adamc@146
|
2434 | _ => all)
|
adamc@146
|
2435 | _ => all)
|
adamc@146
|
2436 | _ => all)
|
adamc@146
|
2437 | _ => all,
|
adamc@146
|
2438 E.sgiBinds env all)
|
adamc@109
|
2439 in
|
adamc@146
|
2440 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
|
adamc@109
|
2441 end
|
adamc@109
|
2442 | _ => sgn
|
adamc@100
|
2443 in
|
adamc@109
|
2444 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs))
|
adamc@100
|
2445 end
|
adamc@100
|
2446
|
adamc@83
|
2447 and elabStr (env, denv) (str, loc) =
|
adamc@31
|
2448 case str of
|
adamc@31
|
2449 L.StrConst ds =>
|
adamc@31
|
2450 let
|
adamc@88
|
2451 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
|
adamc@123
|
2452 val sgis = ListUtil.mapConcat sgiOfDecl ds'
|
adamc@63
|
2453
|
adamc@63
|
2454 val (sgis, _, _, _, _) =
|
adamc@64
|
2455 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
|
adamc@63
|
2456 case sgi of
|
adamc@64
|
2457 L'.SgiConAbs (x, n, k) =>
|
adamc@64
|
2458 let
|
adamc@64
|
2459 val (cons, x) =
|
adamc@64
|
2460 if SS.member (cons, x) then
|
adamc@64
|
2461 (cons, "?" ^ x)
|
adamc@64
|
2462 else
|
adamc@64
|
2463 (SS.add (cons, x), x)
|
adamc@64
|
2464 in
|
adamc@64
|
2465 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
2466 end
|
adamc@64
|
2467 | L'.SgiCon (x, n, k, c) =>
|
adamc@64
|
2468 let
|
adamc@64
|
2469 val (cons, x) =
|
adamc@64
|
2470 if SS.member (cons, x) then
|
adamc@64
|
2471 (cons, "?" ^ x)
|
adamc@64
|
2472 else
|
adamc@64
|
2473 (SS.add (cons, x), x)
|
adamc@64
|
2474 in
|
adamc@64
|
2475 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
2476 end
|
adamc@156
|
2477 | L'.SgiDatatype (x, n, xncs) =>
|
adamc@156
|
2478 let
|
adamc@156
|
2479 val (cons, x) =
|
adamc@156
|
2480 if SS.member (cons, x) then
|
adamc@156
|
2481 (cons, "?" ^ x)
|
adamc@156
|
2482 else
|
adamc@156
|
2483 (SS.add (cons, x), x)
|
adamc@156
|
2484
|
adamc@156
|
2485 val (xncs, vals) =
|
adamc@156
|
2486 ListUtil.foldlMap
|
adamc@156
|
2487 (fn ((x, n, t), vals) =>
|
adamc@156
|
2488 if SS.member (vals, x) then
|
adamc@156
|
2489 (("?" ^ x, n, t), vals)
|
adamc@156
|
2490 else
|
adamc@156
|
2491 ((x, n, t), SS.add (vals, x)))
|
adamc@156
|
2492 vals xncs
|
adamc@156
|
2493 in
|
adamc@156
|
2494 ((L'.SgiDatatype (x, n, xncs), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@156
|
2495 end
|
adamc@162
|
2496 | L'.SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
|
adamc@156
|
2497 let
|
adamc@156
|
2498 val (cons, x) =
|
adamc@156
|
2499 if SS.member (cons, x) then
|
adamc@156
|
2500 (cons, "?" ^ x)
|
adamc@156
|
2501 else
|
adamc@156
|
2502 (SS.add (cons, x), x)
|
adamc@156
|
2503 in
|
adamc@162
|
2504 ((L'.SgiDatatypeImp (x, n, m1, ms, x', xncs), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@156
|
2505 end
|
adamc@64
|
2506 | L'.SgiVal (x, n, c) =>
|
adamc@64
|
2507 let
|
adamc@64
|
2508 val (vals, x) =
|
adamc@64
|
2509 if SS.member (vals, x) then
|
adamc@64
|
2510 (vals, "?" ^ x)
|
adamc@64
|
2511 else
|
adamc@64
|
2512 (SS.add (vals, x), x)
|
adamc@64
|
2513 in
|
adamc@64
|
2514 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
2515 end
|
adamc@64
|
2516 | L'.SgiSgn (x, n, sgn) =>
|
adamc@64
|
2517 let
|
adamc@64
|
2518 val (sgns, x) =
|
adamc@64
|
2519 if SS.member (sgns, x) then
|
adamc@64
|
2520 (sgns, "?" ^ x)
|
adamc@64
|
2521 else
|
adamc@64
|
2522 (SS.add (sgns, x), x)
|
adamc@64
|
2523 in
|
adamc@64
|
2524 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
2525 end
|
adamc@64
|
2526
|
adamc@64
|
2527 | L'.SgiStr (x, n, sgn) =>
|
adamc@64
|
2528 let
|
adamc@64
|
2529 val (strs, x) =
|
adamc@64
|
2530 if SS.member (strs, x) then
|
adamc@64
|
2531 (strs, "?" ^ x)
|
adamc@64
|
2532 else
|
adamc@64
|
2533 (SS.add (strs, x), x)
|
adamc@64
|
2534 in
|
adamc@64
|
2535 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@88
|
2536 end
|
adamc@88
|
2537 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs))
|
adamc@64
|
2538
|
adamc@63
|
2539 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
|
adamc@31
|
2540 in
|
adamc@83
|
2541 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
|
adamc@31
|
2542 end
|
adamc@31
|
2543 | L.StrVar x =>
|
adamc@31
|
2544 (case E.lookupStr env x of
|
adamc@31
|
2545 NONE =>
|
adamc@31
|
2546 (strError env (UnboundStr (loc, x));
|
adamc@83
|
2547 (strerror, sgnerror, []))
|
adamc@83
|
2548 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
|
adamc@34
|
2549 | L.StrProj (str, x) =>
|
adamc@34
|
2550 let
|
adamc@83
|
2551 val (str', sgn, gs) = elabStr (env, denv) str
|
adamc@34
|
2552 in
|
adamc@34
|
2553 case E.projectStr env {str = str', sgn = sgn, field = x} of
|
adamc@34
|
2554 NONE => (strError env (UnboundStr (loc, x));
|
adamc@83
|
2555 (strerror, sgnerror, []))
|
adamc@83
|
2556 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
|
adamc@34
|
2557 end
|
adamc@41
|
2558 | L.StrFun (m, dom, ranO, str) =>
|
adamc@41
|
2559 let
|
adamc@83
|
2560 val (dom', gs1) = elabSgn (env, denv) dom
|
adamc@41
|
2561 val (env', n) = E.pushStrNamed env m dom'
|
adamc@83
|
2562 val (str', actual, gs2) = elabStr (env', denv) str
|
adamc@41
|
2563
|
adamc@83
|
2564 val (formal, gs3) =
|
adamc@41
|
2565 case ranO of
|
adamc@83
|
2566 NONE => (actual, [])
|
adamc@41
|
2567 | SOME ran =>
|
adamc@41
|
2568 let
|
adamc@83
|
2569 val (ran', gs) = elabSgn (env', denv) ran
|
adamc@41
|
2570 in
|
adamc@86
|
2571 subSgn (env', denv) actual ran';
|
adamc@83
|
2572 (ran', gs)
|
adamc@41
|
2573 end
|
adamc@41
|
2574 in
|
adamc@41
|
2575 ((L'.StrFun (m, n, dom', formal, str'), loc),
|
adamc@83
|
2576 (L'.SgnFun (m, n, dom', formal), loc),
|
adamc@83
|
2577 gs1 @ gs2 @ gs3)
|
adamc@41
|
2578 end
|
adamc@44
|
2579 | L.StrApp (str1, str2) =>
|
adamc@44
|
2580 let
|
adamc@83
|
2581 val (str1', sgn1, gs1) = elabStr (env, denv) str1
|
adamc@83
|
2582 val (str2', sgn2, gs2) = elabStr (env, denv) str2
|
adamc@44
|
2583 in
|
adamc@44
|
2584 case #1 (hnormSgn env sgn1) of
|
adamc@83
|
2585 L'.SgnError => (strerror, sgnerror, [])
|
adamc@44
|
2586 | L'.SgnFun (m, n, dom, ran) =>
|
adamc@86
|
2587 (subSgn (env, denv) sgn2 dom;
|
adamc@44
|
2588 case #1 (hnormSgn env ran) of
|
adamc@83
|
2589 L'.SgnError => (strerror, sgnerror, [])
|
adamc@44
|
2590 | L'.SgnConst sgis =>
|
adamc@44
|
2591 ((L'.StrApp (str1', str2'), loc),
|
adamc@83
|
2592 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
|
adamc@83
|
2593 gs1 @ gs2)
|
adamc@44
|
2594 | _ => raise Fail "Unable to hnormSgn in functor application")
|
adamc@44
|
2595 | _ => (strError env (NotFunctor sgn1);
|
adamc@83
|
2596 (strerror, sgnerror, []))
|
adamc@44
|
2597 end
|
adamc@31
|
2598
|
adamc@56
|
2599 fun elabFile basis env file =
|
adamc@56
|
2600 let
|
adamc@83
|
2601 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
|
adamc@83
|
2602 val () = case gs of
|
adamc@83
|
2603 [] => ()
|
adamc@83
|
2604 | _ => raise Fail "Unresolved disjointness constraints in Basis"
|
adamc@83
|
2605
|
adamc@56
|
2606 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
|
adamc@56
|
2607
|
adamc@88
|
2608 val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn}
|
adamc@56
|
2609
|
adamc@56
|
2610 fun discoverC r x =
|
adamc@56
|
2611 case E.lookupC env' x of
|
adamc@56
|
2612 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
|
adamc@56
|
2613 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
|
adamc@56
|
2614 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
|
adamc@56
|
2615
|
adamc@56
|
2616 val () = discoverC int "int"
|
adamc@56
|
2617 val () = discoverC float "float"
|
adamc@56
|
2618 val () = discoverC string "string"
|
adamc@56
|
2619
|
adamc@83
|
2620 fun elabDecl' (d, (env, gs)) =
|
adamc@76
|
2621 let
|
adamc@76
|
2622 val () = resetKunif ()
|
adamc@76
|
2623 val () = resetCunif ()
|
adamc@88
|
2624 val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs))
|
adamc@76
|
2625 in
|
adamc@76
|
2626 if ErrorMsg.anyErrors () then
|
adamc@76
|
2627 ()
|
adamc@76
|
2628 else (
|
adamc@76
|
2629 if List.exists kunifsInDecl ds then
|
adamc@91
|
2630 declError env (KunifsRemain ds)
|
adamc@76
|
2631 else
|
adamc@76
|
2632 ();
|
adamc@76
|
2633
|
adamc@76
|
2634 case ListUtil.search cunifsInDecl ds of
|
adamc@76
|
2635 NONE => ()
|
adamc@76
|
2636 | SOME loc =>
|
adamc@91
|
2637 declError env (CunifsRemain ds)
|
adamc@76
|
2638 );
|
adamc@76
|
2639
|
adamc@83
|
2640 (ds, (env, gs))
|
adamc@76
|
2641 end
|
adamc@76
|
2642
|
adamc@83
|
2643 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
|
adamc@56
|
2644 in
|
adamc@84
|
2645 if ErrorMsg.anyErrors () then
|
adamc@84
|
2646 ()
|
adamc@84
|
2647 else
|
adamc@90
|
2648 app (fn (loc, env, denv, c1, c2) =>
|
adamc@84
|
2649 case D.prove env denv (c1, c2, loc) of
|
adamc@84
|
2650 [] => ()
|
adamc@84
|
2651 | _ =>
|
adamc@86
|
2652 (ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
|
adamc@84
|
2653 eprefaces' [("Con 1", p_con env c1),
|
adamc@90
|
2654 ("Con 2", p_con env c2),
|
adamc@90
|
2655 ("Hnormed 1", p_con env (ElabOps.hnormCon env c1)),
|
adamc@90
|
2656 ("Hnormed 2", p_con env (ElabOps.hnormCon env c2))])) gs;
|
adamc@83
|
2657
|
adamc@56
|
2658 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
|
adamc@56
|
2659 end
|
adamc@2
|
2660
|
adamc@2
|
2661 end
|