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@62
|
40 structure SS = BinarySetFn(struct
|
adamc@62
|
41 type ord_key = string
|
adamc@62
|
42 val compare = String.compare
|
adamc@62
|
43 end)
|
adamc@62
|
44
|
adamc@2
|
45 fun elabExplicitness e =
|
adamc@2
|
46 case e of
|
adamc@2
|
47 L.Explicit => L'.Explicit
|
adamc@2
|
48 | L.Implicit => L'.Implicit
|
adamc@2
|
49
|
adamc@2
|
50 fun occursKind r =
|
adamc@76
|
51 U.Kind.exists (fn L'.KUnif (_, _, r') => r = r'
|
adamc@2
|
52 | _ => false)
|
adamc@2
|
53
|
adamc@3
|
54 datatype kunify_error =
|
adamc@2
|
55 KOccursCheckFailed of L'.kind * L'.kind
|
adamc@2
|
56 | KIncompatible of L'.kind * L'.kind
|
adamc@2
|
57
|
adamc@3
|
58 exception KUnify' of kunify_error
|
adamc@3
|
59
|
adamc@3
|
60 fun kunifyError err =
|
adamc@2
|
61 case err of
|
adamc@2
|
62 KOccursCheckFailed (k1, k2) =>
|
adamc@3
|
63 eprefaces "Kind occurs check failed"
|
adamc@3
|
64 [("Kind 1", p_kind k1),
|
adamc@3
|
65 ("Kind 2", p_kind k2)]
|
adamc@2
|
66 | KIncompatible (k1, k2) =>
|
adamc@3
|
67 eprefaces "Incompatible kinds"
|
adamc@3
|
68 [("Kind 1", p_kind k1),
|
adamc@3
|
69 ("Kind 2", p_kind k2)]
|
adamc@2
|
70
|
adamc@3
|
71 fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) =
|
adamc@2
|
72 let
|
adamc@3
|
73 fun err f = raise KUnify' (f (k1All, k2All))
|
adamc@2
|
74 in
|
adamc@2
|
75 case (k1, k2) of
|
adamc@2
|
76 (L'.KType, L'.KType) => ()
|
adamc@82
|
77 | (L'.KUnit, L'.KUnit) => ()
|
adamc@82
|
78
|
adamc@2
|
79 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
|
adamc@3
|
80 (unifyKinds' d1 d2;
|
adamc@3
|
81 unifyKinds' r1 r2)
|
adamc@2
|
82 | (L'.KName, L'.KName) => ()
|
adamc@3
|
83 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
|
adamc@2
|
84
|
adamc@2
|
85 | (L'.KError, _) => ()
|
adamc@2
|
86 | (_, L'.KError) => ()
|
adamc@2
|
87
|
adamc@76
|
88 | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' k1All k2All
|
adamc@76
|
89 | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' k1All k2All
|
adamc@2
|
90
|
adamc@76
|
91 | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) =>
|
adamc@2
|
92 if r1 = r2 then
|
adamc@2
|
93 ()
|
adamc@2
|
94 else
|
adamc@2
|
95 r1 := SOME k2All
|
adamc@2
|
96
|
adamc@76
|
97 | (L'.KUnif (_, _, r), _) =>
|
adamc@2
|
98 if occursKind r k2All then
|
adamc@2
|
99 err KOccursCheckFailed
|
adamc@2
|
100 else
|
adamc@2
|
101 r := SOME k2All
|
adamc@76
|
102 | (_, L'.KUnif (_, _, r)) =>
|
adamc@2
|
103 if occursKind r k1All then
|
adamc@2
|
104 err KOccursCheckFailed
|
adamc@2
|
105 else
|
adamc@2
|
106 r := SOME k1All
|
adamc@2
|
107
|
adamc@2
|
108 | _ => err KIncompatible
|
adamc@2
|
109 end
|
adamc@2
|
110
|
adamc@3
|
111 exception KUnify of L'.kind * L'.kind * kunify_error
|
adamc@3
|
112
|
adamc@3
|
113 fun unifyKinds k1 k2 =
|
adamc@3
|
114 unifyKinds' k1 k2
|
adamc@3
|
115 handle KUnify' err => raise KUnify (k1, k2, err)
|
adamc@3
|
116
|
adamc@3
|
117 datatype con_error =
|
adamc@3
|
118 UnboundCon of ErrorMsg.span * string
|
adamc@34
|
119 | UnboundStrInCon of ErrorMsg.span * string
|
adamc@3
|
120 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
|
adamc@67
|
121 | DuplicateField of ErrorMsg.span * string
|
adamc@3
|
122
|
adamc@5
|
123 fun conError env err =
|
adamc@3
|
124 case err of
|
adamc@3
|
125 UnboundCon (loc, s) =>
|
adamc@3
|
126 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
|
adamc@34
|
127 | UnboundStrInCon (loc, s) =>
|
adamc@34
|
128 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
|
adamc@3
|
129 | WrongKind (c, k1, k2, kerr) =>
|
adamc@3
|
130 (ErrorMsg.errorAt (#2 c) "Wrong kind";
|
adamc@5
|
131 eprefaces' [("Constructor", p_con env c),
|
adamc@5
|
132 ("Have kind", p_kind k1),
|
adamc@5
|
133 ("Need kind", p_kind k2)];
|
adamc@3
|
134 kunifyError kerr)
|
adamc@67
|
135 | DuplicateField (loc, s) =>
|
adamc@67
|
136 ErrorMsg.errorAt loc ("Duplicate record field " ^ s)
|
adamc@3
|
137
|
adamc@5
|
138 fun checkKind env c k1 k2 =
|
adamc@3
|
139 unifyKinds k1 k2
|
adamc@3
|
140 handle KUnify (k1, k2, err) =>
|
adamc@5
|
141 conError env (WrongKind (c, k1, k2, err))
|
adamc@3
|
142
|
adamc@3
|
143 val dummy = ErrorMsg.dummySpan
|
adamc@3
|
144
|
adamc@3
|
145 val ktype = (L'.KType, dummy)
|
adamc@3
|
146 val kname = (L'.KName, dummy)
|
adamc@12
|
147 val ktype_record = (L'.KRecord ktype, dummy)
|
adamc@3
|
148
|
adamc@3
|
149 val cerror = (L'.CError, dummy)
|
adamc@3
|
150 val kerror = (L'.KError, dummy)
|
adamc@10
|
151 val eerror = (L'.EError, dummy)
|
adamc@31
|
152 val sgnerror = (L'.SgnError, dummy)
|
adamc@31
|
153 val strerror = (L'.StrError, dummy)
|
adamc@3
|
154
|
adamc@56
|
155 val int = ref cerror
|
adamc@56
|
156 val float = ref cerror
|
adamc@56
|
157 val string = ref cerror
|
adamc@56
|
158
|
adamc@3
|
159 local
|
adamc@3
|
160 val count = ref 0
|
adamc@3
|
161 in
|
adamc@3
|
162
|
adamc@3
|
163 fun resetKunif () = count := 0
|
adamc@3
|
164
|
adamc@76
|
165 fun kunif loc =
|
adamc@3
|
166 let
|
adamc@3
|
167 val n = !count
|
adamc@3
|
168 val s = if n <= 26 then
|
adamc@3
|
169 str (chr (ord #"A" + n))
|
adamc@3
|
170 else
|
adamc@3
|
171 "U" ^ Int.toString (n - 26)
|
adamc@3
|
172 in
|
adamc@3
|
173 count := n + 1;
|
adamc@76
|
174 (L'.KUnif (loc, s, ref NONE), dummy)
|
adamc@3
|
175 end
|
adamc@3
|
176
|
adamc@3
|
177 end
|
adamc@3
|
178
|
adamc@10
|
179 local
|
adamc@10
|
180 val count = ref 0
|
adamc@10
|
181 in
|
adamc@10
|
182
|
adamc@10
|
183 fun resetCunif () = count := 0
|
adamc@10
|
184
|
adamc@76
|
185 fun cunif (loc, k) =
|
adamc@10
|
186 let
|
adamc@10
|
187 val n = !count
|
adamc@10
|
188 val s = if n <= 26 then
|
adamc@10
|
189 str (chr (ord #"A" + n))
|
adamc@10
|
190 else
|
adamc@10
|
191 "U" ^ Int.toString (n - 26)
|
adamc@10
|
192 in
|
adamc@10
|
193 count := n + 1;
|
adamc@76
|
194 (L'.CUnif (loc, k, s, ref NONE), dummy)
|
adamc@10
|
195 end
|
adamc@10
|
196
|
adamc@10
|
197 end
|
adamc@10
|
198
|
adamc@18
|
199 fun elabKind (k, loc) =
|
adamc@18
|
200 case k of
|
adamc@18
|
201 L.KType => (L'.KType, loc)
|
adamc@18
|
202 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
|
adamc@18
|
203 | L.KName => (L'.KName, loc)
|
adamc@18
|
204 | L.KRecord k => (L'.KRecord (elabKind k), loc)
|
adamc@82
|
205 | L.KUnit => (L'.KUnit, loc)
|
adamc@76
|
206 | L.KWild => kunif loc
|
adamc@18
|
207
|
adamc@67
|
208 fun foldKind (dom, ran, loc)=
|
adamc@67
|
209 (L'.KArrow ((L'.KArrow ((L'.KName, loc),
|
adamc@67
|
210 (L'.KArrow (dom,
|
adamc@67
|
211 (L'.KArrow (ran, ran), loc)), loc)), loc),
|
adamc@67
|
212 (L'.KArrow (ran,
|
adamc@67
|
213 (L'.KArrow ((L'.KRecord dom, loc),
|
adamc@67
|
214 ran), loc)), loc)), loc)
|
adamc@67
|
215
|
adamc@83
|
216 fun elabCon (env, denv) (c, loc) =
|
adamc@3
|
217 case c of
|
adamc@3
|
218 L.CAnnot (c, k) =>
|
adamc@3
|
219 let
|
adamc@3
|
220 val k' = elabKind k
|
adamc@83
|
221 val (c', ck, gs) = elabCon (env, denv) c
|
adamc@3
|
222 in
|
adamc@5
|
223 checkKind env c' ck k';
|
adamc@83
|
224 (c', k', gs)
|
adamc@3
|
225 end
|
adamc@3
|
226
|
adamc@3
|
227 | L.TFun (t1, t2) =>
|
adamc@3
|
228 let
|
adamc@83
|
229 val (t1', k1, gs1) = elabCon (env, denv) t1
|
adamc@83
|
230 val (t2', k2, gs2) = elabCon (env, denv) t2
|
adamc@3
|
231 in
|
adamc@5
|
232 checkKind env t1' k1 ktype;
|
adamc@5
|
233 checkKind env t2' k2 ktype;
|
adamc@83
|
234 ((L'.TFun (t1', t2'), loc), ktype, gs1 @ gs2)
|
adamc@3
|
235 end
|
adamc@3
|
236 | L.TCFun (e, x, k, t) =>
|
adamc@3
|
237 let
|
adamc@3
|
238 val e' = elabExplicitness e
|
adamc@3
|
239 val k' = elabKind k
|
adamc@3
|
240 val env' = E.pushCRel env x k'
|
adamc@83
|
241 val (t', tk, gs) = elabCon (env', D.enter denv) t
|
adamc@3
|
242 in
|
adamc@5
|
243 checkKind env t' tk ktype;
|
adamc@83
|
244 ((L'.TCFun (e', x, k', t'), loc), ktype, gs)
|
adamc@3
|
245 end
|
adamc@3
|
246 | L.TRecord c =>
|
adamc@3
|
247 let
|
adamc@83
|
248 val (c', ck, gs) = elabCon (env, denv) c
|
adamc@3
|
249 val k = (L'.KRecord ktype, loc)
|
adamc@3
|
250 in
|
adamc@5
|
251 checkKind env c' ck k;
|
adamc@83
|
252 ((L'.TRecord c', loc), ktype, gs)
|
adamc@3
|
253 end
|
adamc@3
|
254
|
adamc@34
|
255 | L.CVar ([], s) =>
|
adamc@3
|
256 (case E.lookupC env s of
|
adamc@9
|
257 E.NotBound =>
|
adamc@5
|
258 (conError env (UnboundCon (loc, s));
|
adamc@83
|
259 (cerror, kerror, []))
|
adamc@9
|
260 | E.Rel (n, k) =>
|
adamc@83
|
261 ((L'.CRel n, loc), k, [])
|
adamc@9
|
262 | E.Named (n, k) =>
|
adamc@83
|
263 ((L'.CNamed n, loc), k, []))
|
adamc@34
|
264 | L.CVar (m1 :: ms, s) =>
|
adamc@34
|
265 (case E.lookupStr env m1 of
|
adamc@42
|
266 NONE => (conError env (UnboundStrInCon (loc, m1));
|
adamc@83
|
267 (cerror, kerror, []))
|
adamc@34
|
268 | SOME (n, sgn) =>
|
adamc@34
|
269 let
|
adamc@34
|
270 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
271 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@34
|
272 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@34
|
273 (strerror, sgnerror))
|
adamc@34
|
274 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
275 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
276
|
adamc@34
|
277 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
|
adamc@34
|
278 NONE => (conError env (UnboundCon (loc, s));
|
adamc@34
|
279 kerror)
|
adamc@34
|
280 | SOME (k, _) => k
|
adamc@34
|
281 in
|
adamc@83
|
282 ((L'.CModProj (n, ms, s), loc), k, [])
|
adamc@34
|
283 end)
|
adamc@34
|
284
|
adamc@3
|
285 | L.CApp (c1, c2) =>
|
adamc@3
|
286 let
|
adamc@83
|
287 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@83
|
288 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@76
|
289 val dom = kunif loc
|
adamc@76
|
290 val ran = kunif loc
|
adamc@3
|
291 in
|
adamc@5
|
292 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
|
adamc@5
|
293 checkKind env c2' k2 dom;
|
adamc@83
|
294 ((L'.CApp (c1', c2'), loc), ran, gs1 @ gs2)
|
adamc@3
|
295 end
|
adamc@67
|
296 | L.CAbs (x, ko, t) =>
|
adamc@3
|
297 let
|
adamc@67
|
298 val k' = case ko of
|
adamc@76
|
299 NONE => kunif loc
|
adamc@67
|
300 | SOME k => elabKind k
|
adamc@3
|
301 val env' = E.pushCRel env x k'
|
adamc@83
|
302 val (t', tk, gs) = elabCon (env', D.enter denv) t
|
adamc@3
|
303 in
|
adamc@8
|
304 ((L'.CAbs (x, k', t'), loc),
|
adamc@83
|
305 (L'.KArrow (k', tk), loc),
|
adamc@83
|
306 gs)
|
adamc@3
|
307 end
|
adamc@3
|
308
|
adamc@84
|
309 | L.CDisjoint (c1, c2, c) =>
|
adamc@84
|
310 let
|
adamc@84
|
311 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@84
|
312 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@84
|
313
|
adamc@84
|
314 val ku1 = kunif loc
|
adamc@84
|
315 val ku2 = kunif loc
|
adamc@84
|
316
|
adamc@84
|
317 val denv' = D.assert env denv (c1', c2')
|
adamc@84
|
318 val (c', k, gs3) = elabCon (env, denv') c
|
adamc@84
|
319 in
|
adamc@84
|
320 checkKind env c1' k1 (L'.KRecord ku1, loc);
|
adamc@84
|
321 checkKind env c2' k2 (L'.KRecord ku2, loc);
|
adamc@84
|
322
|
adamc@84
|
323 ((L'.CDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3)
|
adamc@84
|
324 end
|
adamc@84
|
325
|
adamc@3
|
326 | L.CName s =>
|
adamc@83
|
327 ((L'.CName s, loc), kname, [])
|
adamc@3
|
328
|
adamc@3
|
329 | L.CRecord xcs =>
|
adamc@3
|
330 let
|
adamc@76
|
331 val k = kunif loc
|
adamc@3
|
332
|
adamc@83
|
333 val (xcs', gs) = ListUtil.foldlMap (fn ((x, c), gs) =>
|
adamc@83
|
334 let
|
adamc@83
|
335 val (x', xk, gs1) = elabCon (env, denv) x
|
adamc@83
|
336 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@83
|
337 in
|
adamc@83
|
338 checkKind env x' xk kname;
|
adamc@83
|
339 checkKind env c' ck k;
|
adamc@83
|
340 ((x', c'), gs1 @ gs2 @ gs)
|
adamc@83
|
341 end) [] xcs
|
adamc@67
|
342
|
adamc@67
|
343 val rc = (L'.CRecord (k, xcs'), loc)
|
adamc@67
|
344 (* Add duplicate field checking later. *)
|
adamc@83
|
345
|
adamc@83
|
346 fun prove (xcs, ds) =
|
adamc@83
|
347 case xcs of
|
adamc@83
|
348 [] => ds
|
adamc@83
|
349 | xc :: rest =>
|
adamc@83
|
350 let
|
adamc@83
|
351 val r1 = (L'.CRecord (k, [xc]), loc)
|
adamc@83
|
352 val ds = foldl (fn (xc', ds) =>
|
adamc@83
|
353 let
|
adamc@83
|
354 val r2 = (L'.CRecord (k, [xc']), loc)
|
adamc@83
|
355 in
|
adamc@83
|
356 map (fn cs => (loc, env, denv, cs)) (D.prove env denv (r1, r2, loc))
|
adamc@83
|
357 @ ds
|
adamc@83
|
358 end)
|
adamc@83
|
359 ds rest
|
adamc@83
|
360 in
|
adamc@83
|
361 prove (rest, ds)
|
adamc@83
|
362 end
|
adamc@3
|
363 in
|
adamc@83
|
364 (rc, (L'.KRecord k, loc), prove (xcs', gs))
|
adamc@3
|
365 end
|
adamc@3
|
366 | L.CConcat (c1, c2) =>
|
adamc@3
|
367 let
|
adamc@83
|
368 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@83
|
369 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@76
|
370 val ku = kunif loc
|
adamc@3
|
371 val k = (L'.KRecord ku, loc)
|
adamc@3
|
372 in
|
adamc@5
|
373 checkKind env c1' k1 k;
|
adamc@5
|
374 checkKind env c2' k2 k;
|
adamc@83
|
375 ((L'.CConcat (c1', c2'), loc), k,
|
adamc@83
|
376 map (fn cs => (loc, env, denv, cs)) (D.prove env denv (c1', c2', loc)) @ gs1 @ gs2)
|
adamc@3
|
377 end
|
adamc@67
|
378 | L.CFold =>
|
adamc@67
|
379 let
|
adamc@76
|
380 val dom = kunif loc
|
adamc@76
|
381 val ran = kunif loc
|
adamc@67
|
382 in
|
adamc@67
|
383 ((L'.CFold (dom, ran), loc),
|
adamc@83
|
384 foldKind (dom, ran, loc),
|
adamc@83
|
385 [])
|
adamc@67
|
386 end
|
adamc@3
|
387
|
adamc@83
|
388 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), [])
|
adamc@82
|
389
|
adamc@18
|
390 | L.CWild k =>
|
adamc@18
|
391 let
|
adamc@18
|
392 val k' = elabKind k
|
adamc@18
|
393 in
|
adamc@83
|
394 (cunif (loc, k'), k', [])
|
adamc@18
|
395 end
|
adamc@18
|
396
|
adamc@6
|
397 fun kunifsRemain k =
|
adamc@6
|
398 case k of
|
adamc@76
|
399 L'.KUnif (_, _, ref NONE) => true
|
adamc@6
|
400 | _ => false
|
adamc@10
|
401 fun cunifsRemain c =
|
adamc@10
|
402 case c of
|
adamc@76
|
403 L'.CUnif (loc, _, _, ref NONE) => SOME loc
|
adamc@76
|
404 | _ => NONE
|
adamc@6
|
405
|
adamc@76
|
406 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
|
adamc@76
|
407 con = fn _ => false,
|
adamc@76
|
408 exp = fn _ => false,
|
adamc@76
|
409 sgn_item = fn _ => false,
|
adamc@76
|
410 sgn = fn _ => false,
|
adamc@76
|
411 str = fn _ => false,
|
adamc@76
|
412 decl = fn _ => false}
|
adamc@10
|
413
|
adamc@76
|
414 val cunifsInDecl = U.Decl.search {kind = fn _ => NONE,
|
adamc@76
|
415 con = cunifsRemain,
|
adamc@76
|
416 exp = fn _ => NONE,
|
adamc@76
|
417 sgn_item = fn _ => NONE,
|
adamc@76
|
418 sgn = fn _ => NONE,
|
adamc@76
|
419 str = fn _ => NONE,
|
adamc@76
|
420 decl = fn _ => NONE}
|
adamc@10
|
421
|
adamc@10
|
422 fun occursCon r =
|
adamc@10
|
423 U.Con.exists {kind = fn _ => false,
|
adamc@76
|
424 con = fn L'.CUnif (_, _, _, r') => r = r'
|
adamc@10
|
425 | _ => false}
|
adamc@10
|
426
|
adamc@10
|
427 datatype cunify_error =
|
adamc@10
|
428 CKind of L'.kind * L'.kind * kunify_error
|
adamc@10
|
429 | COccursCheckFailed of L'.con * L'.con
|
adamc@10
|
430 | CIncompatible of L'.con * L'.con
|
adamc@10
|
431 | CExplicitness of L'.con * L'.con
|
adamc@12
|
432 | CKindof of L'.con
|
adamc@12
|
433 | CRecordFailure
|
adamc@10
|
434
|
adamc@10
|
435 exception CUnify' of cunify_error
|
adamc@10
|
436
|
adamc@10
|
437 fun cunifyError env err =
|
adamc@10
|
438 case err of
|
adamc@10
|
439 CKind (k1, k2, kerr) =>
|
adamc@10
|
440 (eprefaces "Kind unification failure"
|
adamc@10
|
441 [("Kind 1", p_kind k1),
|
adamc@10
|
442 ("Kind 2", p_kind k2)];
|
adamc@10
|
443 kunifyError kerr)
|
adamc@10
|
444 | COccursCheckFailed (c1, c2) =>
|
adamc@10
|
445 eprefaces "Constructor occurs check failed"
|
adamc@10
|
446 [("Con 1", p_con env c1),
|
adamc@10
|
447 ("Con 2", p_con env c2)]
|
adamc@10
|
448 | CIncompatible (c1, c2) =>
|
adamc@10
|
449 eprefaces "Incompatible constructors"
|
adamc@10
|
450 [("Con 1", p_con env c1),
|
adamc@10
|
451 ("Con 2", p_con env c2)]
|
adamc@10
|
452 | CExplicitness (c1, c2) =>
|
adamc@10
|
453 eprefaces "Differing constructor function explicitness"
|
adamc@10
|
454 [("Con 1", p_con env c1),
|
adamc@10
|
455 ("Con 2", p_con env c2)]
|
adamc@12
|
456 | CKindof c =>
|
adamc@12
|
457 eprefaces "Kind unification variable blocks kindof calculation"
|
adamc@12
|
458 [("Con", p_con env c)]
|
adamc@12
|
459 | CRecordFailure =>
|
adamc@12
|
460 eprefaces "Can't unify record constructors" []
|
adamc@10
|
461
|
adamc@13
|
462 exception SynUnif = E.SynUnif
|
adamc@12
|
463
|
adamc@81
|
464 open ElabOps
|
adamc@43
|
465
|
adamc@12
|
466 type record_summary = {
|
adamc@12
|
467 fields : (L'.con * L'.con) list,
|
adamc@12
|
468 unifs : (L'.con * L'.con option ref) list,
|
adamc@12
|
469 others : L'.con list
|
adamc@12
|
470 }
|
adamc@12
|
471
|
adamc@12
|
472 fun summaryToCon {fields, unifs, others} =
|
adamc@12
|
473 let
|
adamc@12
|
474 val c = (L'.CRecord (ktype, []), dummy)
|
adamc@12
|
475 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
|
adamc@12
|
476 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
|
adamc@12
|
477 in
|
adamc@12
|
478 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
|
adamc@12
|
479 end
|
adamc@12
|
480
|
adamc@12
|
481 fun p_summary env s = p_con env (summaryToCon s)
|
adamc@12
|
482
|
adamc@12
|
483 exception CUnify of L'.con * L'.con * cunify_error
|
adamc@12
|
484
|
adamc@12
|
485 fun hnormKind (kAll as (k, _)) =
|
adamc@12
|
486 case k of
|
adamc@76
|
487 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
|
adamc@12
|
488 | _ => kAll
|
adamc@12
|
489
|
adamc@12
|
490 fun kindof env (c, loc) =
|
adamc@12
|
491 case c of
|
adamc@12
|
492 L'.TFun _ => ktype
|
adamc@12
|
493 | L'.TCFun _ => ktype
|
adamc@12
|
494 | L'.TRecord _ => ktype
|
adamc@12
|
495
|
adamc@12
|
496 | L'.CRel xn => #2 (E.lookupCRel env xn)
|
adamc@12
|
497 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
|
adamc@34
|
498 | L'.CModProj (n, ms, x) =>
|
adamc@34
|
499 let
|
adamc@34
|
500 val (_, sgn) = E.lookupStrNamed env n
|
adamc@34
|
501 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
502 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@34
|
503 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@34
|
504 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
505 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
506 in
|
adamc@34
|
507 case E.projectCon env {sgn = sgn, str = str, field = x} of
|
adamc@34
|
508 NONE => raise Fail "kindof: Unknown con in structure"
|
adamc@34
|
509 | SOME (k, _) => k
|
adamc@34
|
510 end
|
adamc@34
|
511
|
adamc@12
|
512 | L'.CApp (c, _) =>
|
adamc@12
|
513 (case #1 (hnormKind (kindof env c)) of
|
adamc@12
|
514 L'.KArrow (_, k) => k
|
adamc@12
|
515 | L'.KError => kerror
|
adamc@12
|
516 | _ => raise CUnify' (CKindof c))
|
adamc@12
|
517 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
|
adamc@84
|
518 | L'.CDisjoint (_, _, c) => kindof env c
|
adamc@12
|
519
|
adamc@12
|
520 | L'.CName _ => kname
|
adamc@12
|
521
|
adamc@12
|
522 | L'.CRecord (k, _) => (L'.KRecord k, loc)
|
adamc@12
|
523 | L'.CConcat (c, _) => kindof env c
|
adamc@67
|
524 | L'.CFold (dom, ran) => foldKind (dom, ran, loc)
|
adamc@12
|
525
|
adamc@82
|
526 | L'.CUnit => (L'.KUnit, loc)
|
adamc@82
|
527
|
adamc@12
|
528 | L'.CError => kerror
|
adamc@76
|
529 | L'.CUnif (_, k, _, _) => k
|
adamc@12
|
530
|
adamc@12
|
531 fun unifyRecordCons env (c1, c2) =
|
adamc@12
|
532 let
|
adamc@12
|
533 val k1 = kindof env c1
|
adamc@12
|
534 val k2 = kindof env c2
|
adamc@12
|
535 in
|
adamc@12
|
536 unifyKinds k1 k2;
|
adamc@12
|
537 unifySummaries env (k1, recordSummary env c1, recordSummary env c2)
|
adamc@12
|
538 end
|
adamc@12
|
539
|
adamc@12
|
540 and recordSummary env c : record_summary =
|
adamc@12
|
541 case hnormCon env c of
|
adamc@12
|
542 (L'.CRecord (_, xcs), _) => {fields = xcs, unifs = [], others = []}
|
adamc@12
|
543 | (L'.CConcat (c1, c2), _) =>
|
adamc@12
|
544 let
|
adamc@12
|
545 val s1 = recordSummary env c1
|
adamc@12
|
546 val s2 = recordSummary env c2
|
adamc@12
|
547 in
|
adamc@12
|
548 {fields = #fields s1 @ #fields s2,
|
adamc@12
|
549 unifs = #unifs s1 @ #unifs s2,
|
adamc@12
|
550 others = #others s1 @ #others s2}
|
adamc@12
|
551 end
|
adamc@76
|
552 | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary env c
|
adamc@76
|
553 | c' as (L'.CUnif (_, _, _, r), _) => {fields = [], unifs = [(c', r)], others = []}
|
adamc@12
|
554 | c' => {fields = [], unifs = [], others = [c']}
|
adamc@12
|
555
|
adamc@12
|
556 and consEq env (c1, c2) =
|
adamc@12
|
557 (unifyCons env c1 c2;
|
adamc@12
|
558 true)
|
adamc@12
|
559 handle CUnify _ => false
|
adamc@12
|
560
|
adamc@80
|
561 and consNeq env (c1, c2) =
|
adamc@80
|
562 case (#1 (hnormCon env c1), #1 (hnormCon env c2)) of
|
adamc@80
|
563 (L'.CName x1, L'.CName x2) => x1 <> x2
|
adamc@80
|
564 | _ => false
|
adamc@80
|
565
|
adamc@12
|
566 and unifySummaries env (k, s1 : record_summary, s2 : record_summary) =
|
adamc@12
|
567 let
|
adamc@14
|
568 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
|
adamc@14
|
569 ("#2", p_summary env s2)]*)
|
adamc@12
|
570
|
adamc@12
|
571 fun eatMatching p (ls1, ls2) =
|
adamc@12
|
572 let
|
adamc@12
|
573 fun em (ls1, ls2, passed1) =
|
adamc@12
|
574 case ls1 of
|
adamc@12
|
575 [] => (rev passed1, ls2)
|
adamc@12
|
576 | h1 :: t1 =>
|
adamc@12
|
577 let
|
adamc@12
|
578 fun search (ls2', passed2) =
|
adamc@12
|
579 case ls2' of
|
adamc@12
|
580 [] => em (t1, ls2, h1 :: passed1)
|
adamc@12
|
581 | h2 :: t2 =>
|
adamc@12
|
582 if p (h1, h2) then
|
adamc@12
|
583 em (t1, List.revAppend (passed2, t2), passed1)
|
adamc@12
|
584 else
|
adamc@12
|
585 search (t2, h2 :: passed2)
|
adamc@12
|
586 in
|
adamc@12
|
587 search (ls2, [])
|
adamc@12
|
588 end
|
adamc@12
|
589 in
|
adamc@12
|
590 em (ls1, ls2, [])
|
adamc@12
|
591 end
|
adamc@12
|
592
|
adamc@12
|
593 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
|
adamc@80
|
594 not (consNeq env (x1, x2))
|
adamc@80
|
595 andalso consEq env (c1, c2)
|
adamc@80
|
596 andalso consEq env (x1, x2))
|
adamc@79
|
597 (#fields s1, #fields s2)
|
adamc@14
|
598 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
|
adamc@14
|
599 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
|
adamc@12
|
600 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
|
adamc@12
|
601 val (others1, others2) = eatMatching (consEq env) (#others s1, #others s2)
|
adamc@12
|
602
|
adamc@12
|
603 fun unifFields (fs, others, unifs) =
|
adamc@12
|
604 case (fs, others, unifs) of
|
adamc@12
|
605 ([], [], _) => ([], [], unifs)
|
adamc@12
|
606 | (_, _, []) => (fs, others, [])
|
adamc@12
|
607 | (_, _, (_, r) :: rest) =>
|
adamc@12
|
608 let
|
adamc@12
|
609 val r' = ref NONE
|
adamc@76
|
610 val cr' = (L'.CUnif (dummy, k, "recd", r'), dummy)
|
adamc@12
|
611
|
adamc@12
|
612 val prefix = case (fs, others) of
|
adamc@12
|
613 ([], other :: others) =>
|
adamc@12
|
614 List.foldl (fn (other, c) =>
|
adamc@12
|
615 (L'.CConcat (c, other), dummy))
|
adamc@12
|
616 other others
|
adamc@12
|
617 | (fs, []) =>
|
adamc@12
|
618 (L'.CRecord (k, fs), dummy)
|
adamc@12
|
619 | (fs, others) =>
|
adamc@12
|
620 List.foldl (fn (other, c) =>
|
adamc@12
|
621 (L'.CConcat (c, other), dummy))
|
adamc@12
|
622 (L'.CRecord (k, fs), dummy) others
|
adamc@12
|
623 in
|
adamc@12
|
624 r := SOME (L'.CConcat (prefix, cr'), dummy);
|
adamc@12
|
625 ([], [], (cr', r') :: rest)
|
adamc@12
|
626 end
|
adamc@12
|
627
|
adamc@12
|
628 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
|
adamc@12
|
629 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
|
adamc@12
|
630
|
adamc@12
|
631 val clear1 = case (fs1, others1) of
|
adamc@12
|
632 ([], []) => true
|
adamc@12
|
633 | _ => false
|
adamc@12
|
634 val clear2 = case (fs2, others2) of
|
adamc@12
|
635 ([], []) => true
|
adamc@12
|
636 | _ => false
|
adamc@12
|
637 val empty = (L'.CRecord (k, []), dummy)
|
adamc@12
|
638 fun pairOffUnifs (unifs1, unifs2) =
|
adamc@12
|
639 case (unifs1, unifs2) of
|
adamc@12
|
640 ([], _) =>
|
adamc@12
|
641 if clear1 then
|
adamc@12
|
642 List.app (fn (_, r) => r := SOME empty) unifs2
|
adamc@12
|
643 else
|
adamc@12
|
644 raise CUnify' CRecordFailure
|
adamc@12
|
645 | (_, []) =>
|
adamc@12
|
646 if clear2 then
|
adamc@12
|
647 List.app (fn (_, r) => r := SOME empty) unifs1
|
adamc@12
|
648 else
|
adamc@12
|
649 raise CUnify' CRecordFailure
|
adamc@12
|
650 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
|
adamc@12
|
651 (r2 := SOME c1;
|
adamc@12
|
652 pairOffUnifs (rest1, rest2))
|
adamc@12
|
653 in
|
adamc@12
|
654 pairOffUnifs (unifs1, unifs2)
|
adamc@12
|
655 end
|
adamc@12
|
656
|
adamc@11
|
657
|
adamc@12
|
658 and unifyCons' env c1 c2 =
|
adamc@11
|
659 unifyCons'' env (hnormCon env c1) (hnormCon env c2)
|
adamc@11
|
660
|
adamc@11
|
661 and unifyCons'' env (c1All as (c1, _)) (c2All as (c2, _)) =
|
adamc@10
|
662 let
|
adamc@10
|
663 fun err f = raise CUnify' (f (c1All, c2All))
|
adamc@12
|
664
|
adamc@12
|
665 fun isRecord () = unifyRecordCons env (c1All, c2All)
|
adamc@10
|
666 in
|
adamc@10
|
667 case (c1, c2) of
|
adamc@10
|
668 (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
|
adamc@10
|
669 (unifyCons' env d1 d2;
|
adamc@10
|
670 unifyCons' env r1 r2)
|
adamc@10
|
671 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
|
adamc@10
|
672 if expl1 <> expl2 then
|
adamc@10
|
673 err CExplicitness
|
adamc@10
|
674 else
|
adamc@10
|
675 (unifyKinds d1 d2;
|
adamc@10
|
676 unifyCons' (E.pushCRel env x1 d1) r1 r2)
|
adamc@10
|
677 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env r1 r2
|
adamc@10
|
678
|
adamc@10
|
679 | (L'.CRel n1, L'.CRel n2) =>
|
adamc@10
|
680 if n1 = n2 then
|
adamc@10
|
681 ()
|
adamc@10
|
682 else
|
adamc@10
|
683 err CIncompatible
|
adamc@10
|
684 | (L'.CNamed n1, L'.CNamed n2) =>
|
adamc@10
|
685 if n1 = n2 then
|
adamc@10
|
686 ()
|
adamc@10
|
687 else
|
adamc@10
|
688 err CIncompatible
|
adamc@10
|
689
|
adamc@10
|
690 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
|
adamc@10
|
691 (unifyCons' env d1 d2;
|
adamc@10
|
692 unifyCons' env r1 r2)
|
adamc@10
|
693 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
|
adamc@10
|
694 (unifyKinds k1 k2;
|
adamc@10
|
695 unifyCons' (E.pushCRel env x1 k1) c1 c2)
|
adamc@10
|
696
|
adamc@10
|
697 | (L'.CName n1, L'.CName n2) =>
|
adamc@10
|
698 if n1 = n2 then
|
adamc@10
|
699 ()
|
adamc@10
|
700 else
|
adamc@10
|
701 err CIncompatible
|
adamc@10
|
702
|
adamc@34
|
703 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
|
adamc@34
|
704 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
|
adamc@34
|
705 ()
|
adamc@34
|
706 else
|
adamc@34
|
707 err CIncompatible
|
adamc@34
|
708
|
adamc@10
|
709 | (L'.CError, _) => ()
|
adamc@10
|
710 | (_, L'.CError) => ()
|
adamc@10
|
711
|
adamc@76
|
712 | (L'.CUnif (_, _, _, ref (SOME c1All)), _) => unifyCons' env c1All c2All
|
adamc@76
|
713 | (_, L'.CUnif (_, _, _, ref (SOME c2All))) => unifyCons' env c1All c2All
|
adamc@10
|
714
|
adamc@76
|
715 | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) =>
|
adamc@10
|
716 if r1 = r2 then
|
adamc@10
|
717 ()
|
adamc@10
|
718 else
|
adamc@10
|
719 (unifyKinds k1 k2;
|
adamc@10
|
720 r1 := SOME c2All)
|
adamc@10
|
721
|
adamc@76
|
722 | (L'.CUnif (_, _, _, r), _) =>
|
adamc@10
|
723 if occursCon r c2All then
|
adamc@10
|
724 err COccursCheckFailed
|
adamc@10
|
725 else
|
adamc@10
|
726 r := SOME c2All
|
adamc@76
|
727 | (_, L'.CUnif (_, _, _, r)) =>
|
adamc@10
|
728 if occursCon r c1All then
|
adamc@10
|
729 err COccursCheckFailed
|
adamc@10
|
730 else
|
adamc@10
|
731 r := SOME c1All
|
adamc@10
|
732
|
adamc@12
|
733 | (L'.CRecord _, _) => isRecord ()
|
adamc@12
|
734 | (_, L'.CRecord _) => isRecord ()
|
adamc@12
|
735 | (L'.CConcat _, _) => isRecord ()
|
adamc@12
|
736 | (_, L'.CConcat _) => isRecord ()
|
adamc@12
|
737
|
adamc@71
|
738 | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) =>
|
adamc@71
|
739 (unifyKinds dom1 dom2;
|
adamc@71
|
740 unifyKinds ran1 ran2)
|
adamc@71
|
741
|
adamc@10
|
742 | _ => err CIncompatible
|
adamc@10
|
743 end
|
adamc@10
|
744
|
adamc@12
|
745 and unifyCons env c1 c2 =
|
adamc@10
|
746 unifyCons' env c1 c2
|
adamc@10
|
747 handle CUnify' err => raise CUnify (c1, c2, err)
|
adamc@10
|
748 | KUnify args => raise CUnify (c1, c2, CKind args)
|
adamc@10
|
749
|
adamc@10
|
750 datatype exp_error =
|
adamc@10
|
751 UnboundExp of ErrorMsg.span * string
|
adamc@34
|
752 | UnboundStrInExp of ErrorMsg.span * string
|
adamc@10
|
753 | Unify of L'.exp * L'.con * L'.con * cunify_error
|
adamc@11
|
754 | Unif of string * L'.con
|
adamc@11
|
755 | WrongForm of string * L'.exp * L'.con
|
adamc@10
|
756
|
adamc@10
|
757 fun expError env err =
|
adamc@10
|
758 case err of
|
adamc@10
|
759 UnboundExp (loc, s) =>
|
adamc@10
|
760 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
|
adamc@34
|
761 | UnboundStrInExp (loc, s) =>
|
adamc@34
|
762 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
|
adamc@10
|
763 | Unify (e, c1, c2, uerr) =>
|
adamc@10
|
764 (ErrorMsg.errorAt (#2 e) "Unification failure";
|
adamc@10
|
765 eprefaces' [("Expression", p_exp env e),
|
adamc@10
|
766 ("Have con", p_con env c1),
|
adamc@10
|
767 ("Need con", p_con env c2)];
|
adamc@10
|
768 cunifyError env uerr)
|
adamc@11
|
769 | Unif (action, c) =>
|
adamc@11
|
770 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
|
adamc@11
|
771 eprefaces' [("Con", p_con env c)])
|
adamc@11
|
772 | WrongForm (variety, e, t) =>
|
adamc@11
|
773 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
|
adamc@11
|
774 eprefaces' [("Expression", p_exp env e),
|
adamc@11
|
775 ("Type", p_con env t)])
|
adamc@10
|
776
|
adamc@10
|
777 fun checkCon env e c1 c2 =
|
adamc@10
|
778 unifyCons env c1 c2
|
adamc@10
|
779 handle CUnify (c1, c2, err) =>
|
adamc@10
|
780 expError env (Unify (e, c1, c2, err))
|
adamc@10
|
781
|
adamc@14
|
782 fun primType env p =
|
adamc@56
|
783 case p of
|
adamc@56
|
784 P.Int _ => !int
|
adamc@56
|
785 | P.Float _ => !float
|
adamc@56
|
786 | P.String _ => !string
|
adamc@14
|
787
|
adamc@71
|
788 fun recCons (k, nm, v, rest, loc) =
|
adamc@71
|
789 (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc),
|
adamc@71
|
790 rest), loc)
|
adamc@71
|
791
|
adamc@71
|
792 fun foldType (dom, loc) =
|
adamc@71
|
793 (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc),
|
adamc@71
|
794 (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc),
|
adamc@71
|
795 (L'.TCFun (L'.Explicit, "v", dom,
|
adamc@71
|
796 (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc),
|
adamc@71
|
797 (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc),
|
adamc@71
|
798 (L'.CApp ((L'.CRel 3, loc),
|
adamc@71
|
799 recCons (dom,
|
adamc@71
|
800 (L'.CRel 2, loc),
|
adamc@71
|
801 (L'.CRel 1, loc),
|
adamc@71
|
802 (L'.CRel 0, loc),
|
adamc@71
|
803 loc)), loc)), loc)),
|
adamc@71
|
804 loc)), loc)), loc),
|
adamc@71
|
805 (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc),
|
adamc@71
|
806 (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc),
|
adamc@71
|
807 (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)),
|
adamc@71
|
808 loc)), loc)), loc)
|
adamc@71
|
809
|
adamc@15
|
810 fun typeof env (e, loc) =
|
adamc@15
|
811 case e of
|
adamc@56
|
812 L'.EPrim p => primType env p
|
adamc@15
|
813 | L'.ERel n => #2 (E.lookupERel env n)
|
adamc@15
|
814 | L'.ENamed n => #2 (E.lookupENamed env n)
|
adamc@34
|
815 | L'.EModProj (n, ms, x) =>
|
adamc@34
|
816 let
|
adamc@34
|
817 val (_, sgn) = E.lookupStrNamed env n
|
adamc@34
|
818 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
819 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@34
|
820 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@34
|
821 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
822 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
823 in
|
adamc@34
|
824 case E.projectVal env {sgn = sgn, str = str, field = x} of
|
adamc@34
|
825 NONE => raise Fail "typeof: Unknown val in structure"
|
adamc@34
|
826 | SOME t => t
|
adamc@34
|
827 end
|
adamc@34
|
828
|
adamc@15
|
829 | L'.EApp (e1, _) =>
|
adamc@15
|
830 (case #1 (typeof env e1) of
|
adamc@15
|
831 L'.TFun (_, c) => c
|
adamc@15
|
832 | _ => raise Fail "typeof: Bad EApp")
|
adamc@26
|
833 | L'.EAbs (_, _, ran, _) => ran
|
adamc@15
|
834 | L'.ECApp (e1, c) =>
|
adamc@15
|
835 (case #1 (typeof env e1) of
|
adamc@15
|
836 L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1
|
adamc@15
|
837 | _ => raise Fail "typeof: Bad ECApp")
|
adamc@15
|
838 | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc)
|
adamc@15
|
839
|
adamc@29
|
840 | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, _, t) => (x, t)) xes), loc), loc)
|
adamc@15
|
841 | L'.EField (_, _, {field, ...}) => field
|
adamc@71
|
842 | L'.EFold dom => foldType (dom, loc)
|
adamc@15
|
843
|
adamc@15
|
844 | L'.EError => cerror
|
adamc@15
|
845
|
adamc@15
|
846 fun elabHead env (e as (_, loc)) t =
|
adamc@15
|
847 let
|
adamc@15
|
848 fun unravel (t, e) =
|
adamc@15
|
849 case hnormCon env t of
|
adamc@15
|
850 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
|
adamc@15
|
851 let
|
adamc@76
|
852 val u = cunif (loc, k)
|
adamc@15
|
853 in
|
adamc@15
|
854 unravel (subConInCon (0, u) t',
|
adamc@15
|
855 (L'.ECApp (e, u), loc))
|
adamc@15
|
856 end
|
adamc@15
|
857 | _ => (e, t)
|
adamc@15
|
858 in
|
adamc@15
|
859 unravel (t, e)
|
adamc@15
|
860 end
|
adamc@15
|
861
|
adamc@83
|
862 fun elabExp (env, denv) (e, loc) =
|
adamc@10
|
863 case e of
|
adamc@10
|
864 L.EAnnot (e, t) =>
|
adamc@10
|
865 let
|
adamc@83
|
866 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@83
|
867 val (t', _, gs2) = elabCon (env, denv) t
|
adamc@10
|
868 in
|
adamc@10
|
869 checkCon env e' et t';
|
adamc@83
|
870 (e', t', gs1 @ gs2)
|
adamc@10
|
871 end
|
adamc@10
|
872
|
adamc@83
|
873 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
|
adamc@34
|
874 | L.EVar ([], s) =>
|
adamc@10
|
875 (case E.lookupE env s of
|
adamc@10
|
876 E.NotBound =>
|
adamc@10
|
877 (expError env (UnboundExp (loc, s));
|
adamc@83
|
878 (eerror, cerror, []))
|
adamc@83
|
879 | E.Rel (n, t) => ((L'.ERel n, loc), t, [])
|
adamc@83
|
880 | E.Named (n, t) => ((L'.ENamed n, loc), t, []))
|
adamc@34
|
881 | L.EVar (m1 :: ms, s) =>
|
adamc@34
|
882 (case E.lookupStr env m1 of
|
adamc@42
|
883 NONE => (expError env (UnboundStrInExp (loc, m1));
|
adamc@83
|
884 (eerror, cerror, []))
|
adamc@34
|
885 | SOME (n, sgn) =>
|
adamc@34
|
886 let
|
adamc@34
|
887 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
888 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@34
|
889 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@34
|
890 (strerror, sgnerror))
|
adamc@34
|
891 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
892 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
893
|
adamc@34
|
894 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
|
adamc@34
|
895 NONE => (expError env (UnboundExp (loc, s));
|
adamc@34
|
896 cerror)
|
adamc@34
|
897 | SOME t => t
|
adamc@34
|
898 in
|
adamc@83
|
899 ((L'.EModProj (n, ms, s), loc), t, [])
|
adamc@34
|
900 end)
|
adamc@34
|
901
|
adamc@10
|
902 | L.EApp (e1, e2) =>
|
adamc@10
|
903 let
|
adamc@83
|
904 val (e1', t1, gs1) = elabExp (env, denv) e1
|
adamc@15
|
905 val (e1', t1) = elabHead env e1' t1
|
adamc@83
|
906 val (e2', t2, gs2) = elabExp (env, denv) e2
|
adamc@10
|
907
|
adamc@76
|
908 val dom = cunif (loc, ktype)
|
adamc@76
|
909 val ran = cunif (loc, ktype)
|
adamc@10
|
910 val t = (L'.TFun (dom, ran), dummy)
|
adamc@10
|
911 in
|
adamc@10
|
912 checkCon env e1' t1 t;
|
adamc@10
|
913 checkCon env e2' t2 dom;
|
adamc@83
|
914 ((L'.EApp (e1', e2'), loc), ran, gs1 @ gs2)
|
adamc@10
|
915 end
|
adamc@10
|
916 | L.EAbs (x, to, e) =>
|
adamc@10
|
917 let
|
adamc@83
|
918 val (t', gs1) = case to of
|
adamc@83
|
919 NONE => (cunif (loc, ktype), [])
|
adamc@83
|
920 | SOME t =>
|
adamc@83
|
921 let
|
adamc@83
|
922 val (t', tk, gs) = elabCon (env, denv) t
|
adamc@83
|
923 in
|
adamc@83
|
924 checkKind env t' tk ktype;
|
adamc@83
|
925 (t', gs)
|
adamc@83
|
926 end
|
adamc@83
|
927 val (e', et, gs2) = elabExp (E.pushERel env x t', denv) e
|
adamc@10
|
928 in
|
adamc@26
|
929 ((L'.EAbs (x, t', et, e'), loc),
|
adamc@83
|
930 (L'.TFun (t', et), loc),
|
adamc@83
|
931 gs1 @ gs2)
|
adamc@10
|
932 end
|
adamc@10
|
933 | L.ECApp (e, c) =>
|
adamc@10
|
934 let
|
adamc@83
|
935 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@15
|
936 val (e', et) = elabHead env e' et
|
adamc@83
|
937 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@10
|
938 in
|
adamc@11
|
939 case #1 (hnormCon env et) of
|
adamc@83
|
940 L'.CError => (eerror, cerror, [])
|
adamc@11
|
941 | L'.TCFun (_, _, k, eb) =>
|
adamc@11
|
942 let
|
adamc@11
|
943 val () = checkKind env c' ck k
|
adamc@11
|
944 val eb' = subConInCon (0, c') eb
|
adamc@11
|
945 handle SynUnif => (expError env (Unif ("substitution", eb));
|
adamc@11
|
946 cerror)
|
adamc@11
|
947 in
|
adamc@83
|
948 ((L'.ECApp (e', c'), loc), eb', gs1 @ gs2)
|
adamc@11
|
949 end
|
adamc@11
|
950
|
adamc@11
|
951 | L'.CUnif _ =>
|
adamc@11
|
952 (expError env (Unif ("application", et));
|
adamc@83
|
953 (eerror, cerror, []))
|
adamc@11
|
954
|
adamc@11
|
955 | _ =>
|
adamc@11
|
956 (expError env (WrongForm ("constructor function", e', et));
|
adamc@83
|
957 (eerror, cerror, []))
|
adamc@10
|
958 end
|
adamc@11
|
959 | L.ECAbs (expl, x, k, e) =>
|
adamc@11
|
960 let
|
adamc@11
|
961 val expl' = elabExplicitness expl
|
adamc@11
|
962 val k' = elabKind k
|
adamc@83
|
963 val (e', et, gs) = elabExp (E.pushCRel env x k', D.enter denv) e
|
adamc@11
|
964 in
|
adamc@11
|
965 ((L'.ECAbs (expl', x, k', e'), loc),
|
adamc@83
|
966 (L'.TCFun (expl', x, k', et), loc),
|
adamc@83
|
967 gs)
|
adamc@11
|
968 end
|
adamc@6
|
969
|
adamc@12
|
970 | L.ERecord xes =>
|
adamc@12
|
971 let
|
adamc@83
|
972 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
|
adamc@83
|
973 let
|
adamc@83
|
974 val (x', xk, gs1) = elabCon (env, denv) x
|
adamc@83
|
975 val (e', et, gs2) = elabExp (env, denv) e
|
adamc@83
|
976 in
|
adamc@83
|
977 checkKind env x' xk kname;
|
adamc@83
|
978 ((x', e', et), gs1 @ gs2 @ gs)
|
adamc@83
|
979 end)
|
adamc@83
|
980 [] xes
|
adamc@12
|
981 in
|
adamc@29
|
982 ((L'.ERecord xes', loc),
|
adamc@83
|
983 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
|
adamc@83
|
984 gs)
|
adamc@12
|
985 end
|
adamc@12
|
986
|
adamc@12
|
987 | L.EField (e, c) =>
|
adamc@12
|
988 let
|
adamc@83
|
989 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@83
|
990 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@12
|
991
|
adamc@76
|
992 val ft = cunif (loc, ktype)
|
adamc@76
|
993 val rest = cunif (loc, ktype_record)
|
adamc@12
|
994 in
|
adamc@12
|
995 checkKind env c' ck kname;
|
adamc@12
|
996 checkCon env e' et (L'.TRecord (L'.CConcat ((L'.CRecord (ktype, [(c', ft)]), loc), rest), loc), loc);
|
adamc@83
|
997 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ gs2)
|
adamc@12
|
998 end
|
adamc@71
|
999
|
adamc@71
|
1000 | L.EFold =>
|
adamc@71
|
1001 let
|
adamc@76
|
1002 val dom = kunif loc
|
adamc@71
|
1003 in
|
adamc@83
|
1004 ((L'.EFold dom, loc), foldType (dom, loc), [])
|
adamc@71
|
1005 end
|
adamc@12
|
1006
|
adamc@12
|
1007
|
adamc@6
|
1008 datatype decl_error =
|
adamc@76
|
1009 KunifsRemain of ErrorMsg.span
|
adamc@76
|
1010 | CunifsRemain of ErrorMsg.span
|
adamc@6
|
1011
|
adamc@6
|
1012 fun declError env err =
|
adamc@6
|
1013 case err of
|
adamc@76
|
1014 KunifsRemain loc =>
|
adamc@76
|
1015 ErrorMsg.errorAt loc "Some kind unification variables are undetermined in declaration"
|
adamc@76
|
1016 | CunifsRemain loc =>
|
adamc@76
|
1017 ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in declaration"
|
adamc@6
|
1018
|
adamc@31
|
1019 datatype sgn_error =
|
adamc@31
|
1020 UnboundSgn of ErrorMsg.span * string
|
adamc@31
|
1021 | UnmatchedSgi of L'.sgn_item
|
adamc@31
|
1022 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
|
adamc@31
|
1023 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
|
adamc@41
|
1024 | SgnWrongForm of L'.sgn * L'.sgn
|
adamc@42
|
1025 | UnWhereable of L'.sgn * string
|
adamc@75
|
1026 | WhereWrongKind of L'.kind * L'.kind * kunify_error
|
adamc@58
|
1027 | NotIncludable of L'.sgn
|
adamc@62
|
1028 | DuplicateCon of ErrorMsg.span * string
|
adamc@62
|
1029 | DuplicateVal of ErrorMsg.span * string
|
adamc@62
|
1030 | DuplicateSgn of ErrorMsg.span * string
|
adamc@62
|
1031 | DuplicateStr of ErrorMsg.span * string
|
adamc@31
|
1032
|
adamc@31
|
1033 fun sgnError env err =
|
adamc@31
|
1034 case err of
|
adamc@31
|
1035 UnboundSgn (loc, s) =>
|
adamc@31
|
1036 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
|
adamc@31
|
1037 | UnmatchedSgi (sgi as (_, loc)) =>
|
adamc@31
|
1038 (ErrorMsg.errorAt loc "Unmatched signature item";
|
adamc@31
|
1039 eprefaces' [("Item", p_sgn_item env sgi)])
|
adamc@31
|
1040 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
|
adamc@31
|
1041 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
|
adamc@59
|
1042 eprefaces' [("Have", p_sgn_item env sgi1),
|
adamc@59
|
1043 ("Need", p_sgn_item env sgi2),
|
adamc@31
|
1044 ("Kind 1", p_kind k1),
|
adamc@31
|
1045 ("Kind 2", p_kind k2)];
|
adamc@31
|
1046 kunifyError kerr)
|
adamc@31
|
1047 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
|
adamc@31
|
1048 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
|
adamc@59
|
1049 eprefaces' [("Have", p_sgn_item env sgi1),
|
adamc@59
|
1050 ("Need", p_sgn_item env sgi2),
|
adamc@31
|
1051 ("Con 1", p_con env c1),
|
adamc@31
|
1052 ("Con 2", p_con env c2)];
|
adamc@31
|
1053 cunifyError env cerr)
|
adamc@41
|
1054 | SgnWrongForm (sgn1, sgn2) =>
|
adamc@41
|
1055 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
|
adamc@41
|
1056 eprefaces' [("Sig 1", p_sgn env sgn1),
|
adamc@41
|
1057 ("Sig 2", p_sgn env sgn2)])
|
adamc@42
|
1058 | UnWhereable (sgn, x) =>
|
adamc@42
|
1059 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
|
adamc@42
|
1060 eprefaces' [("Signature", p_sgn env sgn),
|
adamc@42
|
1061 ("Field", PD.string x)])
|
adamc@75
|
1062 | WhereWrongKind (k1, k2, kerr) =>
|
adamc@75
|
1063 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
|
adamc@75
|
1064 eprefaces' [("Have", p_kind k1),
|
adamc@75
|
1065 ("Need", p_kind k2)];
|
adamc@75
|
1066 kunifyError kerr)
|
adamc@58
|
1067 | NotIncludable sgn =>
|
adamc@58
|
1068 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
|
adamc@58
|
1069 eprefaces' [("Signature", p_sgn env sgn)])
|
adamc@62
|
1070 | DuplicateCon (loc, s) =>
|
adamc@62
|
1071 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
|
adamc@62
|
1072 | DuplicateVal (loc, s) =>
|
adamc@62
|
1073 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
|
adamc@62
|
1074 | DuplicateSgn (loc, s) =>
|
adamc@62
|
1075 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
|
adamc@62
|
1076 | DuplicateStr (loc, s) =>
|
adamc@62
|
1077 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
|
adamc@31
|
1078
|
adamc@31
|
1079 datatype str_error =
|
adamc@31
|
1080 UnboundStr of ErrorMsg.span * string
|
adamc@44
|
1081 | NotFunctor of L'.sgn
|
adamc@46
|
1082 | FunctorRebind of ErrorMsg.span
|
adamc@61
|
1083 | UnOpenable of L'.sgn
|
adamc@75
|
1084 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
|
adamc@31
|
1085
|
adamc@31
|
1086 fun strError env err =
|
adamc@31
|
1087 case err of
|
adamc@31
|
1088 UnboundStr (loc, s) =>
|
adamc@31
|
1089 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
|
adamc@44
|
1090 | NotFunctor sgn =>
|
adamc@44
|
1091 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
|
adamc@44
|
1092 eprefaces' [("Signature", p_sgn env sgn)])
|
adamc@46
|
1093 | FunctorRebind loc =>
|
adamc@46
|
1094 ErrorMsg.errorAt loc "Attempt to rebind functor"
|
adamc@61
|
1095 | UnOpenable sgn =>
|
adamc@61
|
1096 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
|
adamc@61
|
1097 eprefaces' [("Signature", p_sgn env sgn)])
|
adamc@75
|
1098 | NotType (k, (k1, k2, ue)) =>
|
adamc@75
|
1099 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
|
adamc@75
|
1100 eprefaces' [("Kind", p_kind k),
|
adamc@75
|
1101 ("Subkind 1", p_kind k1),
|
adamc@75
|
1102 ("Subkind 2", p_kind k2)];
|
adamc@75
|
1103 kunifyError ue)
|
adamc@31
|
1104
|
adamc@42
|
1105 val hnormSgn = E.hnormSgn
|
adamc@31
|
1106
|
adamc@83
|
1107 fun elabSgn_item denv ((sgi, loc), (env, gs)) =
|
adamc@76
|
1108 case sgi of
|
adamc@76
|
1109 L.SgiConAbs (x, k) =>
|
adamc@76
|
1110 let
|
adamc@76
|
1111 val k' = elabKind k
|
adamc@31
|
1112
|
adamc@76
|
1113 val (env', n) = E.pushCNamed env x k' NONE
|
adamc@76
|
1114 in
|
adamc@83
|
1115 ([(L'.SgiConAbs (x, n, k'), loc)], (env', gs))
|
adamc@76
|
1116 end
|
adamc@31
|
1117
|
adamc@76
|
1118 | L.SgiCon (x, ko, c) =>
|
adamc@76
|
1119 let
|
adamc@76
|
1120 val k' = case ko of
|
adamc@76
|
1121 NONE => kunif loc
|
adamc@76
|
1122 | SOME k => elabKind k
|
adamc@31
|
1123
|
adamc@83
|
1124 val (c', ck, gs') = elabCon (env, denv) c
|
adamc@76
|
1125 val (env', n) = E.pushCNamed env x k' (SOME c')
|
adamc@76
|
1126 in
|
adamc@76
|
1127 checkKind env c' ck k';
|
adamc@31
|
1128
|
adamc@83
|
1129 ([(L'.SgiCon (x, n, k', c'), loc)], (env', gs' @ gs))
|
adamc@76
|
1130 end
|
adamc@31
|
1131
|
adamc@76
|
1132 | L.SgiVal (x, c) =>
|
adamc@76
|
1133 let
|
adamc@83
|
1134 val (c', ck, gs') = elabCon (env, denv) c
|
adamc@31
|
1135
|
adamc@76
|
1136 val (env', n) = E.pushENamed env x c'
|
adamc@76
|
1137 in
|
adamc@76
|
1138 (unifyKinds ck ktype
|
adamc@76
|
1139 handle KUnify ue => strError env (NotType (ck, ue)));
|
adamc@31
|
1140
|
adamc@83
|
1141 ([(L'.SgiVal (x, n, c'), loc)], (env', gs' @ gs))
|
adamc@76
|
1142 end
|
adamc@31
|
1143
|
adamc@76
|
1144 | L.SgiStr (x, sgn) =>
|
adamc@76
|
1145 let
|
adamc@83
|
1146 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
1147 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@76
|
1148 in
|
adamc@83
|
1149 ([(L'.SgiStr (x, n, sgn'), loc)], (env', gs' @ gs))
|
adamc@76
|
1150 end
|
adamc@31
|
1151
|
adamc@76
|
1152 | L.SgiSgn (x, sgn) =>
|
adamc@76
|
1153 let
|
adamc@83
|
1154 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
1155 val (env', n) = E.pushSgnNamed env x sgn'
|
adamc@76
|
1156 in
|
adamc@83
|
1157 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', gs' @ gs))
|
adamc@76
|
1158 end
|
adamc@31
|
1159
|
adamc@76
|
1160 | L.SgiInclude sgn =>
|
adamc@76
|
1161 let
|
adamc@83
|
1162 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
1163 in
|
adamc@76
|
1164 case #1 (hnormSgn env sgn') of
|
adamc@76
|
1165 L'.SgnConst sgis =>
|
adamc@83
|
1166 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, gs' @ gs))
|
adamc@76
|
1167 | _ => (sgnError env (NotIncludable sgn');
|
adamc@83
|
1168 ([], (env, [])))
|
adamc@76
|
1169 end
|
adamc@31
|
1170
|
adamc@83
|
1171 and elabSgn (env, denv) (sgn, loc) =
|
adamc@31
|
1172 case sgn of
|
adamc@31
|
1173 L.SgnConst sgis =>
|
adamc@31
|
1174 let
|
adamc@83
|
1175 val (sgis', (_, gs)) = ListUtil.foldlMapConcat (elabSgn_item denv) (env, []) sgis
|
adamc@62
|
1176
|
adamc@62
|
1177 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
|
adamc@62
|
1178 case sgi of
|
adamc@62
|
1179 L'.SgiConAbs (x, _, _) =>
|
adamc@62
|
1180 (if SS.member (cons, x) then
|
adamc@62
|
1181 sgnError env (DuplicateCon (loc, x))
|
adamc@62
|
1182 else
|
adamc@62
|
1183 ();
|
adamc@62
|
1184 (SS.add (cons, x), vals, sgns, strs))
|
adamc@62
|
1185 | L'.SgiCon (x, _, _, _) =>
|
adamc@62
|
1186 (if SS.member (cons, x) then
|
adamc@62
|
1187 sgnError env (DuplicateCon (loc, x))
|
adamc@62
|
1188 else
|
adamc@62
|
1189 ();
|
adamc@62
|
1190 (SS.add (cons, x), vals, sgns, strs))
|
adamc@62
|
1191 | L'.SgiVal (x, _, _) =>
|
adamc@62
|
1192 (if SS.member (vals, x) then
|
adamc@62
|
1193 sgnError env (DuplicateVal (loc, x))
|
adamc@62
|
1194 else
|
adamc@62
|
1195 ();
|
adamc@62
|
1196 (cons, SS.add (vals, x), sgns, strs))
|
adamc@62
|
1197 | L'.SgiSgn (x, _, _) =>
|
adamc@62
|
1198 (if SS.member (sgns, x) then
|
adamc@62
|
1199 sgnError env (DuplicateSgn (loc, x))
|
adamc@62
|
1200 else
|
adamc@62
|
1201 ();
|
adamc@62
|
1202 (cons, vals, SS.add (sgns, x), strs))
|
adamc@62
|
1203 | L'.SgiStr (x, _, _) =>
|
adamc@62
|
1204 (if SS.member (strs, x) then
|
adamc@62
|
1205 sgnError env (DuplicateStr (loc, x))
|
adamc@62
|
1206 else
|
adamc@62
|
1207 ();
|
adamc@62
|
1208 (cons, vals, sgns, SS.add (strs, x))))
|
adamc@62
|
1209 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
|
adamc@31
|
1210 in
|
adamc@83
|
1211 ((L'.SgnConst sgis', loc), gs)
|
adamc@31
|
1212 end
|
adamc@31
|
1213 | L.SgnVar x =>
|
adamc@31
|
1214 (case E.lookupSgn env x of
|
adamc@31
|
1215 NONE =>
|
adamc@31
|
1216 (sgnError env (UnboundSgn (loc, x));
|
adamc@83
|
1217 ((L'.SgnError, loc), []))
|
adamc@83
|
1218 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
|
adamc@41
|
1219 | L.SgnFun (m, dom, ran) =>
|
adamc@41
|
1220 let
|
adamc@83
|
1221 val (dom', gs1) = elabSgn (env, denv) dom
|
adamc@41
|
1222 val (env', n) = E.pushStrNamed env m dom'
|
adamc@83
|
1223 val (ran', gs2) = elabSgn (env', denv) ran
|
adamc@41
|
1224 in
|
adamc@83
|
1225 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
|
adamc@41
|
1226 end
|
adamc@42
|
1227 | L.SgnWhere (sgn, x, c) =>
|
adamc@42
|
1228 let
|
adamc@83
|
1229 val (sgn', ds1) = elabSgn (env, denv) sgn
|
adamc@83
|
1230 val (c', ck, ds2) = elabCon (env, denv) c
|
adamc@42
|
1231 in
|
adamc@42
|
1232 case #1 (hnormSgn env sgn') of
|
adamc@83
|
1233 L'.SgnError => (sgnerror, [])
|
adamc@42
|
1234 | L'.SgnConst sgis =>
|
adamc@75
|
1235 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
|
adamc@75
|
1236 x' = x andalso
|
adamc@75
|
1237 (unifyKinds k ck
|
adamc@75
|
1238 handle KUnify x => sgnError env (WhereWrongKind x);
|
adamc@42
|
1239 true)
|
adamc@42
|
1240 | _ => false) sgis then
|
adamc@83
|
1241 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
|
adamc@42
|
1242 else
|
adamc@42
|
1243 (sgnError env (UnWhereable (sgn', x));
|
adamc@83
|
1244 (sgnerror, []))
|
adamc@42
|
1245 | _ => (sgnError env (UnWhereable (sgn', x));
|
adamc@83
|
1246 (sgnerror, []))
|
adamc@42
|
1247 end
|
adamc@59
|
1248 | L.SgnProj (m, ms, x) =>
|
adamc@59
|
1249 (case E.lookupStr env m of
|
adamc@59
|
1250 NONE => (strError env (UnboundStr (loc, m));
|
adamc@83
|
1251 (sgnerror, []))
|
adamc@59
|
1252 | SOME (n, sgn) =>
|
adamc@59
|
1253 let
|
adamc@59
|
1254 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@59
|
1255 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@59
|
1256 NONE => (strError env (UnboundStr (loc, m));
|
adamc@59
|
1257 (strerror, sgnerror))
|
adamc@59
|
1258 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@59
|
1259 ((L'.StrVar n, loc), sgn) ms
|
adamc@59
|
1260 in
|
adamc@59
|
1261 case E.projectSgn env {sgn = sgn, str = str, field = x} of
|
adamc@59
|
1262 NONE => (sgnError env (UnboundSgn (loc, x));
|
adamc@83
|
1263 (sgnerror, []))
|
adamc@83
|
1264 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
|
adamc@59
|
1265 end)
|
adamc@59
|
1266
|
adamc@31
|
1267
|
adamc@66
|
1268 fun selfify env {str, strs, sgn} =
|
adamc@66
|
1269 case #1 (hnormSgn env sgn) of
|
adamc@66
|
1270 L'.SgnError => sgn
|
adamc@66
|
1271 | L'.SgnVar _ => sgn
|
adamc@66
|
1272
|
adamc@66
|
1273 | L'.SgnConst sgis =>
|
adamc@66
|
1274 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
|
adamc@66
|
1275 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
|
adamc@66
|
1276 | (L'.SgiStr (x, n, sgn), loc) =>
|
adamc@66
|
1277 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
|
adamc@66
|
1278 | x => x) sgis), #2 sgn)
|
adamc@66
|
1279 | L'.SgnFun _ => sgn
|
adamc@66
|
1280 | L'.SgnWhere _ => sgn
|
adamc@66
|
1281 | L'.SgnProj (m, ms, x) =>
|
adamc@66
|
1282 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
|
adamc@66
|
1283 (L'.StrVar m, #2 sgn) ms,
|
adamc@66
|
1284 sgn = #2 (E.lookupStrNamed env m),
|
adamc@66
|
1285 field = x} of
|
adamc@66
|
1286 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
|
adamc@66
|
1287 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
|
adamc@66
|
1288
|
adamc@66
|
1289 fun selfifyAt env {str, sgn} =
|
adamc@66
|
1290 let
|
adamc@66
|
1291 fun self (str, _) =
|
adamc@66
|
1292 case str of
|
adamc@66
|
1293 L'.StrVar x => SOME (x, [])
|
adamc@66
|
1294 | L'.StrProj (str, x) =>
|
adamc@66
|
1295 (case self str of
|
adamc@66
|
1296 NONE => NONE
|
adamc@66
|
1297 | SOME (m, ms) => SOME (m, ms @ [x]))
|
adamc@66
|
1298 | _ => NONE
|
adamc@66
|
1299 in
|
adamc@66
|
1300 case self str of
|
adamc@66
|
1301 NONE => sgn
|
adamc@66
|
1302 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
|
adamc@66
|
1303 end
|
adamc@66
|
1304
|
adamc@66
|
1305 fun dopen env {str, strs, sgn} =
|
adamc@66
|
1306 let
|
adamc@66
|
1307 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
|
adamc@66
|
1308 (L'.StrVar str, #2 sgn) strs
|
adamc@66
|
1309 in
|
adamc@66
|
1310 case #1 (hnormSgn env sgn) of
|
adamc@66
|
1311 L'.SgnConst sgis =>
|
adamc@66
|
1312 ListUtil.foldlMap (fn ((sgi, loc), env') =>
|
adamc@66
|
1313 case sgi of
|
adamc@66
|
1314 L'.SgiConAbs (x, n, k) =>
|
adamc@66
|
1315 ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc),
|
adamc@66
|
1316 E.pushCNamedAs env' x n k NONE)
|
adamc@66
|
1317 | L'.SgiCon (x, n, k, c) =>
|
adamc@66
|
1318 ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc),
|
adamc@66
|
1319 E.pushCNamedAs env' x n k (SOME c))
|
adamc@66
|
1320 | L'.SgiVal (x, n, t) =>
|
adamc@66
|
1321 ((L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc),
|
adamc@66
|
1322 E.pushENamedAs env' x n t)
|
adamc@66
|
1323 | L'.SgiStr (x, n, sgn) =>
|
adamc@66
|
1324 ((L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc),
|
adamc@66
|
1325 E.pushStrNamedAs env' x n sgn)
|
adamc@66
|
1326 | L'.SgiSgn (x, n, sgn) =>
|
adamc@66
|
1327 ((L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc),
|
adamc@66
|
1328 E.pushSgnNamedAs env' x n sgn))
|
adamc@66
|
1329 env sgis
|
adamc@66
|
1330 | _ => (strError env (UnOpenable sgn);
|
adamc@66
|
1331 ([], env))
|
adamc@66
|
1332 end
|
adamc@66
|
1333
|
adamc@31
|
1334 fun sgiOfDecl (d, loc) =
|
adamc@31
|
1335 case d of
|
adamc@59
|
1336 L'.DCon (x, n, k, c) => (L'.SgiCon (x, n, k, c), loc)
|
adamc@59
|
1337 | L'.DVal (x, n, t, _) => (L'.SgiVal (x, n, t), loc)
|
adamc@59
|
1338 | L'.DSgn (x, n, sgn) => (L'.SgiSgn (x, n, sgn), loc)
|
adamc@59
|
1339 | L'.DStr (x, n, sgn, _) => (L'.SgiStr (x, n, sgn), loc)
|
adamc@59
|
1340 | L'.DFfiStr (x, n, sgn) => (L'.SgiStr (x, n, sgn), loc)
|
adamc@31
|
1341
|
adamc@35
|
1342 fun subSgn env sgn1 (sgn2 as (_, loc2)) =
|
adamc@35
|
1343 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
|
adamc@31
|
1344 (L'.SgnError, _) => ()
|
adamc@31
|
1345 | (_, L'.SgnError) => ()
|
adamc@31
|
1346
|
adamc@31
|
1347 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
|
adamc@31
|
1348 let
|
adamc@31
|
1349 fun folder (sgi2All as (sgi, _), env) =
|
adamc@31
|
1350 let
|
adamc@31
|
1351 fun seek p =
|
adamc@31
|
1352 let
|
adamc@31
|
1353 fun seek env ls =
|
adamc@31
|
1354 case ls of
|
adamc@31
|
1355 [] => (sgnError env (UnmatchedSgi sgi2All);
|
adamc@31
|
1356 env)
|
adamc@31
|
1357 | h :: t =>
|
adamc@31
|
1358 case p h of
|
adamc@31
|
1359 NONE => seek (E.sgiBinds env h) t
|
adamc@31
|
1360 | SOME env => env
|
adamc@31
|
1361 in
|
adamc@31
|
1362 seek env sgis1
|
adamc@31
|
1363 end
|
adamc@31
|
1364 in
|
adamc@31
|
1365 case sgi of
|
adamc@31
|
1366 L'.SgiConAbs (x, n2, k2) =>
|
adamc@31
|
1367 seek (fn sgi1All as (sgi1, _) =>
|
adamc@31
|
1368 let
|
adamc@41
|
1369 fun found (x', n1, k1, co1) =
|
adamc@41
|
1370 if x = x' then
|
adamc@41
|
1371 let
|
adamc@41
|
1372 val () = unifyKinds k1 k2
|
adamc@41
|
1373 handle KUnify (k1, k2, err) =>
|
adamc@41
|
1374 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
|
adamc@41
|
1375 val env = E.pushCNamedAs env x n1 k1 co1
|
adamc@41
|
1376 in
|
adamc@41
|
1377 SOME (if n1 = n2 then
|
adamc@41
|
1378 env
|
adamc@41
|
1379 else
|
adamc@41
|
1380 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)))
|
adamc@41
|
1381 end
|
adamc@41
|
1382 else
|
adamc@41
|
1383 NONE
|
adamc@31
|
1384 in
|
adamc@31
|
1385 case sgi1 of
|
adamc@41
|
1386 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
|
adamc@41
|
1387 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
|
adamc@31
|
1388 | _ => NONE
|
adamc@31
|
1389 end)
|
adamc@31
|
1390
|
adamc@31
|
1391 | L'.SgiCon (x, n2, k2, c2) =>
|
adamc@31
|
1392 seek (fn sgi1All as (sgi1, _) =>
|
adamc@31
|
1393 case sgi1 of
|
adamc@41
|
1394 L'.SgiCon (x', n1, k1, c1) =>
|
adamc@41
|
1395 if x = x' then
|
adamc@41
|
1396 let
|
adamc@41
|
1397 val () = unifyCons env c1 c2
|
adamc@41
|
1398 handle CUnify (c1, c2, err) =>
|
adamc@41
|
1399 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
|
adamc@41
|
1400 in
|
adamc@41
|
1401 SOME (E.pushCNamedAs env x n2 k2 (SOME c2))
|
adamc@41
|
1402 end
|
adamc@41
|
1403 else
|
adamc@41
|
1404 NONE
|
adamc@31
|
1405 | _ => NONE)
|
adamc@31
|
1406
|
adamc@32
|
1407 | L'.SgiVal (x, n2, c2) =>
|
adamc@32
|
1408 seek (fn sgi1All as (sgi1, _) =>
|
adamc@32
|
1409 case sgi1 of
|
adamc@41
|
1410 L'.SgiVal (x', n1, c1) =>
|
adamc@41
|
1411 if x = x' then
|
adamc@41
|
1412 let
|
adamc@41
|
1413 val () = unifyCons env c1 c2
|
adamc@41
|
1414 handle CUnify (c1, c2, err) =>
|
adamc@41
|
1415 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
|
adamc@41
|
1416 in
|
adamc@41
|
1417 SOME env
|
adamc@41
|
1418 end
|
adamc@41
|
1419 else
|
adamc@41
|
1420 NONE
|
adamc@32
|
1421 | _ => NONE)
|
adamc@32
|
1422
|
adamc@33
|
1423 | L'.SgiStr (x, n2, sgn2) =>
|
adamc@33
|
1424 seek (fn sgi1All as (sgi1, _) =>
|
adamc@33
|
1425 case sgi1 of
|
adamc@41
|
1426 L'.SgiStr (x', n1, sgn1) =>
|
adamc@41
|
1427 if x = x' then
|
adamc@66
|
1428 let
|
adamc@66
|
1429 val () = subSgn env sgn1 sgn2
|
adamc@66
|
1430 val env = E.pushStrNamedAs env x n1 sgn1
|
adamc@66
|
1431 val env = if n1 = n2 then
|
adamc@66
|
1432 env
|
adamc@66
|
1433 else
|
adamc@66
|
1434 E.pushStrNamedAs env x n2
|
adamc@66
|
1435 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
|
adamc@66
|
1436 sgn = sgn2})
|
adamc@66
|
1437 in
|
adamc@66
|
1438 SOME env
|
adamc@66
|
1439 end
|
adamc@41
|
1440 else
|
adamc@41
|
1441 NONE
|
adamc@33
|
1442 | _ => NONE)
|
adamc@59
|
1443
|
adamc@59
|
1444 | L'.SgiSgn (x, n2, sgn2) =>
|
adamc@59
|
1445 seek (fn sgi1All as (sgi1, _) =>
|
adamc@59
|
1446 case sgi1 of
|
adamc@59
|
1447 L'.SgiSgn (x', n1, sgn1) =>
|
adamc@59
|
1448 if x = x' then
|
adamc@65
|
1449 let
|
adamc@65
|
1450 val () = subSgn env sgn1 sgn2
|
adamc@65
|
1451 val () = subSgn env sgn2 sgn1
|
adamc@65
|
1452
|
adamc@65
|
1453 val env = E.pushSgnNamedAs env x n2 sgn2
|
adamc@65
|
1454 val env = if n1 = n2 then
|
adamc@65
|
1455 env
|
adamc@65
|
1456 else
|
adamc@65
|
1457 E.pushSgnNamedAs env x n1 sgn2
|
adamc@65
|
1458 in
|
adamc@65
|
1459 SOME env
|
adamc@65
|
1460 end
|
adamc@59
|
1461 else
|
adamc@59
|
1462 NONE
|
adamc@59
|
1463 | _ => NONE)
|
adamc@31
|
1464 end
|
adamc@31
|
1465 in
|
adamc@31
|
1466 ignore (foldl folder env sgis2)
|
adamc@31
|
1467 end
|
adamc@31
|
1468
|
adamc@41
|
1469 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
|
adamc@43
|
1470 let
|
adamc@43
|
1471 val ran1 =
|
adamc@43
|
1472 if n1 = n2 then
|
adamc@43
|
1473 ran1
|
adamc@43
|
1474 else
|
adamc@43
|
1475 subStrInSgn (n1, n2) ran1
|
adamc@43
|
1476 in
|
adamc@43
|
1477 subSgn env dom2 dom1;
|
adamc@43
|
1478 subSgn (E.pushStrNamedAs env m2 n2 dom2) ran1 ran2
|
adamc@43
|
1479 end
|
adamc@41
|
1480
|
adamc@41
|
1481 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
|
adamc@41
|
1482
|
adamc@61
|
1483
|
adamc@83
|
1484 fun elabDecl denv ((d, loc), (env, gs)) =
|
adamc@76
|
1485 case d of
|
adamc@76
|
1486 L.DCon (x, ko, c) =>
|
adamc@76
|
1487 let
|
adamc@76
|
1488 val k' = case ko of
|
adamc@76
|
1489 NONE => kunif loc
|
adamc@76
|
1490 | SOME k => elabKind k
|
adamc@76
|
1491
|
adamc@83
|
1492 val (c', ck, gs') = elabCon (env, denv) c
|
adamc@76
|
1493 val (env', n) = E.pushCNamed env x k' (SOME c')
|
adamc@76
|
1494 in
|
adamc@76
|
1495 checkKind env c' ck k';
|
adamc@76
|
1496
|
adamc@83
|
1497 ([(L'.DCon (x, n, k', c'), loc)], (env', gs' @ gs))
|
adamc@76
|
1498 end
|
adamc@76
|
1499 | L.DVal (x, co, e) =>
|
adamc@76
|
1500 let
|
adamc@83
|
1501 val (c', ck, gs1) = case co of
|
adamc@83
|
1502 NONE => (cunif (loc, ktype), ktype, [])
|
adamc@83
|
1503 | SOME c => elabCon (env, denv) c
|
adamc@76
|
1504
|
adamc@83
|
1505 val (e', et, gs2) = elabExp (env, denv) e
|
adamc@76
|
1506 val (env', n) = E.pushENamed env x c'
|
adamc@76
|
1507 in
|
adamc@76
|
1508 checkCon env e' et c';
|
adamc@76
|
1509
|
adamc@83
|
1510 ([(L'.DVal (x, n, c', e'), loc)], (env', gs1 @ gs2 @ gs))
|
adamc@76
|
1511 end
|
adamc@76
|
1512
|
adamc@76
|
1513 | L.DSgn (x, sgn) =>
|
adamc@76
|
1514 let
|
adamc@83
|
1515 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
1516 val (env', n) = E.pushSgnNamed env x sgn'
|
adamc@76
|
1517 in
|
adamc@83
|
1518 ([(L'.DSgn (x, n, sgn'), loc)], (env', gs' @ gs))
|
adamc@76
|
1519 end
|
adamc@76
|
1520
|
adamc@76
|
1521 | L.DStr (x, sgno, str) =>
|
adamc@76
|
1522 let
|
adamc@83
|
1523 val formal = Option.map (elabSgn (env, denv)) sgno
|
adamc@76
|
1524
|
adamc@83
|
1525 val (str', sgn', gs') =
|
adamc@78
|
1526 case formal of
|
adamc@78
|
1527 NONE =>
|
adamc@78
|
1528 let
|
adamc@83
|
1529 val (str', actual, ds) = elabStr (env, denv) str
|
adamc@78
|
1530 in
|
adamc@83
|
1531 (str', selfifyAt env {str = str', sgn = actual}, ds)
|
adamc@78
|
1532 end
|
adamc@83
|
1533 | SOME (formal, gs1) =>
|
adamc@78
|
1534 let
|
adamc@78
|
1535 val str =
|
adamc@78
|
1536 case #1 (hnormSgn env formal) of
|
adamc@78
|
1537 L'.SgnConst sgis =>
|
adamc@78
|
1538 (case #1 str of
|
adamc@78
|
1539 L.StrConst ds =>
|
adamc@78
|
1540 let
|
adamc@78
|
1541 val needed = foldl (fn ((sgi, _), needed) =>
|
adamc@78
|
1542 case sgi of
|
adamc@78
|
1543 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
|
adamc@78
|
1544 | _ => needed)
|
adamc@78
|
1545 SS.empty sgis
|
adamc@78
|
1546
|
adamc@78
|
1547 val needed = foldl (fn ((d, _), needed) =>
|
adamc@78
|
1548 case d of
|
adamc@78
|
1549 L.DCon (x, _, _) => (SS.delete (needed, x)
|
adamc@78
|
1550 handle NotFound => needed)
|
adamc@78
|
1551 | L.DOpen _ => SS.empty
|
adamc@78
|
1552 | _ => needed)
|
adamc@78
|
1553 needed ds
|
adamc@78
|
1554 in
|
adamc@78
|
1555 case SS.listItems needed of
|
adamc@78
|
1556 [] => str
|
adamc@78
|
1557 | xs =>
|
adamc@78
|
1558 let
|
adamc@78
|
1559 val kwild = (L.KWild, #2 str)
|
adamc@78
|
1560 val cwild = (L.CWild kwild, #2 str)
|
adamc@78
|
1561 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
|
adamc@78
|
1562 in
|
adamc@78
|
1563 (L.StrConst (ds @ ds'), #2 str)
|
adamc@78
|
1564 end
|
adamc@78
|
1565 end
|
adamc@78
|
1566 | _ => str)
|
adamc@78
|
1567 | _ => str
|
adamc@78
|
1568
|
adamc@83
|
1569 val (str', actual, gs2) = elabStr (env, denv) str
|
adamc@78
|
1570 in
|
adamc@78
|
1571 subSgn env actual formal;
|
adamc@83
|
1572 (str', formal, gs1 @ gs2)
|
adamc@78
|
1573 end
|
adamc@76
|
1574
|
adamc@76
|
1575 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@76
|
1576 in
|
adamc@76
|
1577 case #1 (hnormSgn env sgn') of
|
adamc@76
|
1578 L'.SgnFun _ =>
|
adamc@76
|
1579 (case #1 str' of
|
adamc@76
|
1580 L'.StrFun _ => ()
|
adamc@76
|
1581 | _ => strError env (FunctorRebind loc))
|
adamc@76
|
1582 | _ => ();
|
adamc@76
|
1583
|
adamc@83
|
1584 ([(L'.DStr (x, n, sgn', str'), loc)], (env', gs' @ gs))
|
adamc@76
|
1585 end
|
adamc@76
|
1586
|
adamc@76
|
1587 | L.DFfiStr (x, sgn) =>
|
adamc@76
|
1588 let
|
adamc@83
|
1589 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
1590
|
adamc@76
|
1591 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@76
|
1592 in
|
adamc@83
|
1593 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', gs' @ gs))
|
adamc@76
|
1594 end
|
adamc@76
|
1595
|
adamc@76
|
1596 | L.DOpen (m, ms) =>
|
adamc@76
|
1597 case E.lookupStr env m of
|
adamc@76
|
1598 NONE => (strError env (UnboundStr (loc, m));
|
adamc@83
|
1599 ([], (env, [])))
|
adamc@76
|
1600 | SOME (n, sgn) =>
|
adamc@30
|
1601 let
|
adamc@76
|
1602 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@76
|
1603 case E.projectStr env {str = str, sgn = sgn, field = m} of
|
adamc@76
|
1604 NONE => (strError env (UnboundStr (loc, m));
|
adamc@76
|
1605 (strerror, sgnerror))
|
adamc@76
|
1606 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@76
|
1607 ((L'.StrVar n, loc), sgn) ms
|
adamc@83
|
1608
|
adamc@83
|
1609 val (ds, env') = dopen env {str = n, strs = ms, sgn = sgn}
|
adamc@30
|
1610 in
|
adamc@83
|
1611 (ds, (env', []))
|
adamc@30
|
1612 end
|
adamc@3
|
1613
|
adamc@83
|
1614 and elabStr (env, denv) (str, loc) =
|
adamc@31
|
1615 case str of
|
adamc@31
|
1616 L.StrConst ds =>
|
adamc@31
|
1617 let
|
adamc@83
|
1618 val (ds', (env', gs)) = ListUtil.foldlMapConcat (elabDecl denv) (env, []) ds
|
adamc@59
|
1619 val sgis = map sgiOfDecl ds'
|
adamc@63
|
1620
|
adamc@63
|
1621 val (sgis, _, _, _, _) =
|
adamc@64
|
1622 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
|
adamc@63
|
1623 case sgi of
|
adamc@64
|
1624 L'.SgiConAbs (x, n, k) =>
|
adamc@64
|
1625 let
|
adamc@64
|
1626 val (cons, x) =
|
adamc@64
|
1627 if SS.member (cons, x) then
|
adamc@64
|
1628 (cons, "?" ^ x)
|
adamc@64
|
1629 else
|
adamc@64
|
1630 (SS.add (cons, x), x)
|
adamc@64
|
1631 in
|
adamc@64
|
1632 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
1633 end
|
adamc@64
|
1634 | L'.SgiCon (x, n, k, c) =>
|
adamc@64
|
1635 let
|
adamc@64
|
1636 val (cons, x) =
|
adamc@64
|
1637 if SS.member (cons, x) then
|
adamc@64
|
1638 (cons, "?" ^ x)
|
adamc@64
|
1639 else
|
adamc@64
|
1640 (SS.add (cons, x), x)
|
adamc@64
|
1641 in
|
adamc@64
|
1642 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
1643 end
|
adamc@64
|
1644 | L'.SgiVal (x, n, c) =>
|
adamc@64
|
1645 let
|
adamc@64
|
1646 val (vals, x) =
|
adamc@64
|
1647 if SS.member (vals, x) then
|
adamc@64
|
1648 (vals, "?" ^ x)
|
adamc@64
|
1649 else
|
adamc@64
|
1650 (SS.add (vals, x), x)
|
adamc@64
|
1651 in
|
adamc@64
|
1652 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
1653 end
|
adamc@64
|
1654 | L'.SgiSgn (x, n, sgn) =>
|
adamc@64
|
1655 let
|
adamc@64
|
1656 val (sgns, x) =
|
adamc@64
|
1657 if SS.member (sgns, x) then
|
adamc@64
|
1658 (sgns, "?" ^ x)
|
adamc@64
|
1659 else
|
adamc@64
|
1660 (SS.add (sgns, x), x)
|
adamc@64
|
1661 in
|
adamc@64
|
1662 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
1663 end
|
adamc@64
|
1664
|
adamc@64
|
1665 | L'.SgiStr (x, n, sgn) =>
|
adamc@64
|
1666 let
|
adamc@64
|
1667 val (strs, x) =
|
adamc@64
|
1668 if SS.member (strs, x) then
|
adamc@64
|
1669 (strs, "?" ^ x)
|
adamc@64
|
1670 else
|
adamc@64
|
1671 (SS.add (strs, x), x)
|
adamc@64
|
1672 in
|
adamc@64
|
1673 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
1674 end)
|
adamc@64
|
1675
|
adamc@63
|
1676 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
|
adamc@31
|
1677 in
|
adamc@83
|
1678 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
|
adamc@31
|
1679 end
|
adamc@31
|
1680 | L.StrVar x =>
|
adamc@31
|
1681 (case E.lookupStr env x of
|
adamc@31
|
1682 NONE =>
|
adamc@31
|
1683 (strError env (UnboundStr (loc, x));
|
adamc@83
|
1684 (strerror, sgnerror, []))
|
adamc@83
|
1685 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
|
adamc@34
|
1686 | L.StrProj (str, x) =>
|
adamc@34
|
1687 let
|
adamc@83
|
1688 val (str', sgn, gs) = elabStr (env, denv) str
|
adamc@34
|
1689 in
|
adamc@34
|
1690 case E.projectStr env {str = str', sgn = sgn, field = x} of
|
adamc@34
|
1691 NONE => (strError env (UnboundStr (loc, x));
|
adamc@83
|
1692 (strerror, sgnerror, []))
|
adamc@83
|
1693 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
|
adamc@34
|
1694 end
|
adamc@41
|
1695 | L.StrFun (m, dom, ranO, str) =>
|
adamc@41
|
1696 let
|
adamc@83
|
1697 val (dom', gs1) = elabSgn (env, denv) dom
|
adamc@41
|
1698 val (env', n) = E.pushStrNamed env m dom'
|
adamc@83
|
1699 val (str', actual, gs2) = elabStr (env', denv) str
|
adamc@41
|
1700
|
adamc@83
|
1701 val (formal, gs3) =
|
adamc@41
|
1702 case ranO of
|
adamc@83
|
1703 NONE => (actual, [])
|
adamc@41
|
1704 | SOME ran =>
|
adamc@41
|
1705 let
|
adamc@83
|
1706 val (ran', gs) = elabSgn (env', denv) ran
|
adamc@41
|
1707 in
|
adamc@41
|
1708 subSgn env' actual ran';
|
adamc@83
|
1709 (ran', gs)
|
adamc@41
|
1710 end
|
adamc@41
|
1711 in
|
adamc@41
|
1712 ((L'.StrFun (m, n, dom', formal, str'), loc),
|
adamc@83
|
1713 (L'.SgnFun (m, n, dom', formal), loc),
|
adamc@83
|
1714 gs1 @ gs2 @ gs3)
|
adamc@41
|
1715 end
|
adamc@44
|
1716 | L.StrApp (str1, str2) =>
|
adamc@44
|
1717 let
|
adamc@83
|
1718 val (str1', sgn1, gs1) = elabStr (env, denv) str1
|
adamc@83
|
1719 val (str2', sgn2, gs2) = elabStr (env, denv) str2
|
adamc@44
|
1720 in
|
adamc@44
|
1721 case #1 (hnormSgn env sgn1) of
|
adamc@83
|
1722 L'.SgnError => (strerror, sgnerror, [])
|
adamc@44
|
1723 | L'.SgnFun (m, n, dom, ran) =>
|
adamc@44
|
1724 (subSgn env sgn2 dom;
|
adamc@44
|
1725 case #1 (hnormSgn env ran) of
|
adamc@83
|
1726 L'.SgnError => (strerror, sgnerror, [])
|
adamc@44
|
1727 | L'.SgnConst sgis =>
|
adamc@44
|
1728 ((L'.StrApp (str1', str2'), loc),
|
adamc@83
|
1729 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
|
adamc@83
|
1730 gs1 @ gs2)
|
adamc@44
|
1731 | _ => raise Fail "Unable to hnormSgn in functor application")
|
adamc@44
|
1732 | _ => (strError env (NotFunctor sgn1);
|
adamc@83
|
1733 (strerror, sgnerror, []))
|
adamc@44
|
1734 end
|
adamc@31
|
1735
|
adamc@56
|
1736 fun elabFile basis env file =
|
adamc@56
|
1737 let
|
adamc@83
|
1738 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
|
adamc@83
|
1739 val () = case gs of
|
adamc@83
|
1740 [] => ()
|
adamc@83
|
1741 | _ => raise Fail "Unresolved disjointness constraints in Basis"
|
adamc@83
|
1742
|
adamc@56
|
1743 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
|
adamc@56
|
1744
|
adamc@61
|
1745 val (ds, env') = dopen env' {str = basis_n, strs = [], sgn = sgn}
|
adamc@56
|
1746
|
adamc@56
|
1747 fun discoverC r x =
|
adamc@56
|
1748 case E.lookupC env' x of
|
adamc@56
|
1749 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
|
adamc@56
|
1750 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
|
adamc@56
|
1751 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
|
adamc@56
|
1752
|
adamc@56
|
1753 val () = discoverC int "int"
|
adamc@56
|
1754 val () = discoverC float "float"
|
adamc@56
|
1755 val () = discoverC string "string"
|
adamc@56
|
1756
|
adamc@83
|
1757 fun elabDecl' (d, (env, gs)) =
|
adamc@76
|
1758 let
|
adamc@76
|
1759 val () = resetKunif ()
|
adamc@76
|
1760 val () = resetCunif ()
|
adamc@83
|
1761 val (ds, (env, gs)) = elabDecl D.empty (d, (env, gs))
|
adamc@76
|
1762 in
|
adamc@76
|
1763 if ErrorMsg.anyErrors () then
|
adamc@76
|
1764 ()
|
adamc@76
|
1765 else (
|
adamc@76
|
1766 if List.exists kunifsInDecl ds then
|
adamc@76
|
1767 declError env (KunifsRemain (#2 d))
|
adamc@76
|
1768 else
|
adamc@76
|
1769 ();
|
adamc@76
|
1770
|
adamc@76
|
1771 case ListUtil.search cunifsInDecl ds of
|
adamc@76
|
1772 NONE => ()
|
adamc@76
|
1773 | SOME loc =>
|
adamc@76
|
1774 declError env (CunifsRemain loc)
|
adamc@76
|
1775 );
|
adamc@76
|
1776
|
adamc@83
|
1777 (ds, (env, gs))
|
adamc@76
|
1778 end
|
adamc@76
|
1779
|
adamc@83
|
1780 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
|
adamc@56
|
1781 in
|
adamc@84
|
1782 if ErrorMsg.anyErrors () then
|
adamc@84
|
1783 ()
|
adamc@84
|
1784 else
|
adamc@84
|
1785 app (fn (loc, env, denv, (c1, c2)) =>
|
adamc@84
|
1786 case D.prove env denv (c1, c2, loc) of
|
adamc@84
|
1787 [] => ()
|
adamc@84
|
1788 | _ =>
|
adamc@84
|
1789 (ErrorMsg.errorAt loc "Remaining constraint";
|
adamc@84
|
1790 eprefaces' [("Con 1", p_con env c1),
|
adamc@84
|
1791 ("Con 2", p_con env c2)])) gs;
|
adamc@83
|
1792
|
adamc@56
|
1793 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
|
adamc@56
|
1794 end
|
adamc@2
|
1795
|
adamc@2
|
1796 end
|