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