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