adamc@412
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@445
|
2 * All rights reserved.
|
adamc@445
|
3 *
|
adamc@445
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@445
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@445
|
6 *
|
adamc@445
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@445
|
8 * this list of conditions and the following disclaimer.
|
adamc@445
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@445
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@445
|
11 * and/or other materials provided with the distribution.
|
adamc@445
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@445
|
13 * derived from this software without specific prior written permission.
|
adamc@445
|
14 *
|
adamc@445
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@445
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@445
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@445
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@445
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@445
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@445
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@445
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@445
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@445
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@445
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@445
|
26 *)
|
adamc@403
|
27
|
adamc@403
|
28 structure Elaborate :> ELABORATE = struct
|
adamc@403
|
29
|
adamc@403
|
30 structure P = Prim
|
adamc@403
|
31 structure L = Source
|
adamc@403
|
32 structure L' = Elab
|
adamc@403
|
33 structure E = ElabEnv
|
adamc@403
|
34 structure U = ElabUtil
|
adamc@403
|
35 structure D = Disjoint
|
adamc@403
|
36
|
adamc@403
|
37 open Print
|
adamc@403
|
38 open ElabPrint
|
adamc@403
|
39 open ElabErr
|
adamc@403
|
40
|
adamc@403
|
41 structure IM = IntBinaryMap
|
adamc@403
|
42
|
adamc@403
|
43 structure SK = struct
|
adamc@403
|
44 type ord_key = string
|
adamc@403
|
45 val compare = String.compare
|
adamc@403
|
46 end
|
adamc@403
|
47
|
adamc@403
|
48 structure SS = BinarySetFn(SK)
|
adamc@403
|
49 structure SM = BinaryMapFn(SK)
|
adamc@403
|
50
|
adamc@403
|
51 val basis_r = ref 0
|
adamc@403
|
52
|
adamc@403
|
53 fun elabExplicitness e =
|
adamc@403
|
54 case e of
|
adamc@403
|
55 L.Explicit => L'.Explicit
|
adamc@403
|
56 | L.Implicit => L'.Implicit
|
adamc@403
|
57
|
adamc@403
|
58 fun occursKind r =
|
adamc@403
|
59 U.Kind.exists (fn L'.KUnif (_, _, r') => r = r'
|
adamc@403
|
60 | _ => false)
|
adamc@403
|
61
|
adamc@403
|
62 exception KUnify' of kunify_error
|
adamc@403
|
63
|
adamc@403
|
64 fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) =
|
adamc@403
|
65 let
|
adamc@403
|
66 fun err f = raise KUnify' (f (k1All, k2All))
|
adamc@403
|
67 in
|
adamc@403
|
68 case (k1, k2) of
|
adamc@403
|
69 (L'.KType, L'.KType) => ()
|
adamc@403
|
70 | (L'.KUnit, L'.KUnit) => ()
|
adamc@403
|
71
|
adamc@403
|
72 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
|
adamc@403
|
73 (unifyKinds' d1 d2;
|
adamc@403
|
74 unifyKinds' r1 r2)
|
adamc@403
|
75 | (L'.KName, L'.KName) => ()
|
adamc@403
|
76 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
|
adamc@403
|
77 | (L'.KTuple ks1, L'.KTuple ks2) =>
|
adamc@403
|
78 ((ListPair.appEq (fn (k1, k2) => unifyKinds' k1 k2) (ks1, ks2))
|
adamc@403
|
79 handle ListPair.UnequalLengths => err KIncompatible)
|
adamc@403
|
80
|
adamc@403
|
81 | (L'.KError, _) => ()
|
adamc@403
|
82 | (_, L'.KError) => ()
|
adamc@403
|
83
|
adamc@403
|
84 | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' k1All k2All
|
adamc@403
|
85 | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' k1All k2All
|
adamc@403
|
86
|
adamc@403
|
87 | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) =>
|
adamc@403
|
88 if r1 = r2 then
|
adamc@403
|
89 ()
|
adamc@403
|
90 else
|
adamc@403
|
91 r1 := SOME k2All
|
adamc@403
|
92
|
adamc@403
|
93 | (L'.KUnif (_, _, r), _) =>
|
adamc@403
|
94 if occursKind r k2All then
|
adamc@403
|
95 err KOccursCheckFailed
|
adamc@403
|
96 else
|
adamc@403
|
97 r := SOME k2All
|
adamc@403
|
98 | (_, L'.KUnif (_, _, r)) =>
|
adamc@403
|
99 if occursKind r k1All then
|
adamc@403
|
100 err KOccursCheckFailed
|
adamc@403
|
101 else
|
adamc@403
|
102 r := SOME k1All
|
adamc@403
|
103
|
adamc@403
|
104 | _ => err KIncompatible
|
adamc@403
|
105 end
|
adamc@403
|
106
|
adamc@403
|
107 exception KUnify of L'.kind * L'.kind * kunify_error
|
adamc@403
|
108
|
adamc@403
|
109 fun unifyKinds k1 k2 =
|
adamc@403
|
110 unifyKinds' k1 k2
|
adamc@403
|
111 handle KUnify' err => raise KUnify (k1, k2, err)
|
adamc@403
|
112
|
adamc@403
|
113 fun checkKind env c k1 k2 =
|
adamc@403
|
114 unifyKinds k1 k2
|
adamc@403
|
115 handle KUnify (k1, k2, err) =>
|
adamc@403
|
116 conError env (WrongKind (c, k1, k2, err))
|
adamc@403
|
117
|
adamc@403
|
118 val dummy = ErrorMsg.dummySpan
|
adamc@403
|
119
|
adamc@403
|
120 val ktype = (L'.KType, dummy)
|
adamc@403
|
121 val kname = (L'.KName, dummy)
|
adamc@403
|
122 val ktype_record = (L'.KRecord ktype, dummy)
|
adamc@403
|
123
|
adamc@403
|
124 val cerror = (L'.CError, dummy)
|
adamc@403
|
125 val kerror = (L'.KError, dummy)
|
adamc@403
|
126 val eerror = (L'.EError, dummy)
|
adamc@403
|
127 val sgnerror = (L'.SgnError, dummy)
|
adamc@403
|
128 val strerror = (L'.StrError, dummy)
|
adamc@403
|
129
|
adamc@403
|
130 val int = ref cerror
|
adamc@403
|
131 val float = ref cerror
|
adamc@403
|
132 val string = ref cerror
|
adamc@403
|
133 val table = ref cerror
|
adamc@403
|
134
|
adamc@403
|
135 local
|
adamc@403
|
136 val count = ref 0
|
adamc@403
|
137 in
|
adamc@403
|
138
|
adamc@403
|
139 fun resetKunif () = count := 0
|
adamc@403
|
140
|
adamc@403
|
141 fun kunif loc =
|
adamc@403
|
142 let
|
adamc@403
|
143 val n = !count
|
adamc@403
|
144 val s = if n <= 26 then
|
adamc@403
|
145 str (chr (ord #"A" + n))
|
adamc@403
|
146 else
|
adamc@403
|
147 "U" ^ Int.toString (n - 26)
|
adamc@403
|
148 in
|
adamc@403
|
149 count := n + 1;
|
adamc@403
|
150 (L'.KUnif (loc, s, ref NONE), dummy)
|
adamc@403
|
151 end
|
adamc@403
|
152
|
adamc@403
|
153 end
|
adamc@403
|
154
|
adamc@403
|
155 local
|
adamc@403
|
156 val count = ref 0
|
adamc@403
|
157 in
|
adamc@403
|
158
|
adamc@403
|
159 fun resetCunif () = count := 0
|
adamc@403
|
160
|
adamc@403
|
161 fun cunif (loc, k) =
|
adamc@403
|
162 let
|
adamc@403
|
163 val n = !count
|
adamc@403
|
164 val s = if n <= 26 then
|
adamc@403
|
165 str (chr (ord #"A" + n))
|
adamc@403
|
166 else
|
adamc@403
|
167 "U" ^ Int.toString (n - 26)
|
adamc@403
|
168 in
|
adamc@403
|
169 count := n + 1;
|
adamc@403
|
170 (L'.CUnif (loc, k, s, ref NONE), dummy)
|
adamc@403
|
171 end
|
adamc@403
|
172
|
adamc@403
|
173 end
|
adamc@403
|
174
|
adamc@403
|
175 fun elabKind (k, loc) =
|
adamc@403
|
176 case k of
|
adamc@403
|
177 L.KType => (L'.KType, loc)
|
adamc@403
|
178 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
|
adamc@403
|
179 | L.KName => (L'.KName, loc)
|
adamc@403
|
180 | L.KRecord k => (L'.KRecord (elabKind k), loc)
|
adamc@403
|
181 | L.KUnit => (L'.KUnit, loc)
|
adamc@403
|
182 | L.KTuple ks => (L'.KTuple (map elabKind ks), loc)
|
adamc@403
|
183 | L.KWild => kunif loc
|
adamc@403
|
184
|
adamc@403
|
185 fun foldKind (dom, ran, loc)=
|
adamc@403
|
186 (L'.KArrow ((L'.KArrow ((L'.KName, loc),
|
adamc@403
|
187 (L'.KArrow (dom,
|
adamc@403
|
188 (L'.KArrow (ran, ran), loc)), loc)), loc),
|
adamc@403
|
189 (L'.KArrow (ran,
|
adamc@403
|
190 (L'.KArrow ((L'.KRecord dom, loc),
|
adamc@403
|
191 ran), loc)), loc)), loc)
|
adamc@403
|
192
|
adamc@403
|
193 fun hnormKind (kAll as (k, _)) =
|
adamc@403
|
194 case k of
|
adamc@403
|
195 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
|
adamc@403
|
196 | _ => kAll
|
adamc@403
|
197
|
adamc@403
|
198 fun elabCon (env, denv) (c, loc) =
|
adamc@403
|
199 case c of
|
adamc@403
|
200 L.CAnnot (c, k) =>
|
adamc@403
|
201 let
|
adamc@403
|
202 val k' = elabKind k
|
adamc@403
|
203 val (c', ck, gs) = elabCon (env, denv) c
|
adamc@403
|
204 in
|
adamc@403
|
205 checkKind env c' ck k';
|
adamc@403
|
206 (c', k', gs)
|
adamc@403
|
207 end
|
adamc@403
|
208
|
adamc@403
|
209 | L.TFun (t1, t2) =>
|
adamc@403
|
210 let
|
adamc@403
|
211 val (t1', k1, gs1) = elabCon (env, denv) t1
|
adamc@403
|
212 val (t2', k2, gs2) = elabCon (env, denv) t2
|
adamc@403
|
213 in
|
adamc@403
|
214 checkKind env t1' k1 ktype;
|
adamc@403
|
215 checkKind env t2' k2 ktype;
|
adamc@403
|
216 ((L'.TFun (t1', t2'), loc), ktype, gs1 @ gs2)
|
adamc@403
|
217 end
|
adamc@403
|
218 | L.TCFun (e, x, k, t) =>
|
adamc@403
|
219 let
|
adamc@403
|
220 val e' = elabExplicitness e
|
adamc@403
|
221 val k' = elabKind k
|
adamc@403
|
222 val env' = E.pushCRel env x k'
|
adamc@403
|
223 val (t', tk, gs) = elabCon (env', D.enter denv) t
|
adamc@403
|
224 in
|
adamc@403
|
225 checkKind env t' tk ktype;
|
adamc@403
|
226 ((L'.TCFun (e', x, k', t'), loc), ktype, gs)
|
adamc@403
|
227 end
|
adamc@403
|
228 | L.CDisjoint (c1, c2, c) =>
|
adamc@403
|
229 let
|
adamc@403
|
230 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@403
|
231 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@403
|
232
|
adamc@403
|
233 val ku1 = kunif loc
|
adamc@403
|
234 val ku2 = kunif loc
|
adamc@403
|
235
|
adamc@403
|
236 val (denv', gs3) = D.assert env denv (c1', c2')
|
adamc@403
|
237 val (c', k, gs4) = elabCon (env, denv') c
|
adamc@403
|
238 in
|
adamc@403
|
239 checkKind env c1' k1 (L'.KRecord ku1, loc);
|
adamc@403
|
240 checkKind env c2' k2 (L'.KRecord ku2, loc);
|
adamc@403
|
241
|
adamc@403
|
242 ((L'.CDisjoint (L'.Instantiate, c1', c2', c'), loc), k, gs1 @ gs2 @ gs3 @ gs4)
|
adamc@403
|
243 end
|
adamc@403
|
244 | L.TRecord c =>
|
adamc@403
|
245 let
|
adamc@403
|
246 val (c', ck, gs) = elabCon (env, denv) c
|
adamc@403
|
247 val k = (L'.KRecord ktype, loc)
|
adamc@403
|
248 in
|
adamc@403
|
249 checkKind env c' ck k;
|
adamc@403
|
250 ((L'.TRecord c', loc), ktype, gs)
|
adamc@403
|
251 end
|
adamc@403
|
252
|
adamc@403
|
253 | L.CVar ([], s) =>
|
adamc@403
|
254 (case E.lookupC env s of
|
adamc@403
|
255 E.NotBound =>
|
adamc@403
|
256 (conError env (UnboundCon (loc, s));
|
adamc@403
|
257 (cerror, kerror, []))
|
adamc@403
|
258 | E.Rel (n, k) =>
|
adamc@403
|
259 ((L'.CRel n, loc), k, [])
|
adamc@403
|
260 | E.Named (n, k) =>
|
adamc@403
|
261 ((L'.CNamed n, loc), k, []))
|
adamc@403
|
262 | L.CVar (m1 :: ms, s) =>
|
adamc@403
|
263 (case E.lookupStr env m1 of
|
adamc@403
|
264 NONE => (conError env (UnboundStrInCon (loc, m1));
|
adamc@403
|
265 (cerror, kerror, []))
|
adamc@403
|
266 | SOME (n, sgn) =>
|
adamc@403
|
267 let
|
adamc@403
|
268 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@403
|
269 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@403
|
270 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@403
|
271 (strerror, sgnerror))
|
adamc@403
|
272 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@403
|
273 ((L'.StrVar n, loc), sgn) ms
|
adamc@403
|
274
|
adamc@403
|
275 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
|
adamc@403
|
276 NONE => (conError env (UnboundCon (loc, s));
|
adamc@403
|
277 kerror)
|
adamc@403
|
278 | SOME (k, _) => k
|
adamc@403
|
279 in
|
adamc@403
|
280 ((L'.CModProj (n, ms, s), loc), k, [])
|
adamc@403
|
281 end)
|
adamc@403
|
282
|
adamc@403
|
283 | L.CApp (c1, c2) =>
|
adamc@403
|
284 let
|
adamc@403
|
285 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@403
|
286 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@403
|
287 val dom = kunif loc
|
adamc@403
|
288 val ran = kunif loc
|
adamc@403
|
289 in
|
adamc@403
|
290 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
|
adamc@403
|
291 checkKind env c2' k2 dom;
|
adamc@403
|
292 ((L'.CApp (c1', c2'), loc), ran, gs1 @ gs2)
|
adamc@403
|
293 end
|
adamc@403
|
294 | L.CAbs (x, ko, t) =>
|
adamc@403
|
295 let
|
adamc@403
|
296 val k' = case ko of
|
adamc@403
|
297 NONE => kunif loc
|
adamc@403
|
298 | SOME k => elabKind k
|
adamc@403
|
299 val env' = E.pushCRel env x k'
|
adamc@403
|
300 val (t', tk, gs) = elabCon (env', D.enter denv) t
|
adamc@403
|
301 in
|
adamc@403
|
302 ((L'.CAbs (x, k', t'), loc),
|
adamc@403
|
303 (L'.KArrow (k', tk), loc),
|
adamc@403
|
304 gs)
|
adamc@403
|
305 end
|
adamc@403
|
306
|
adamc@403
|
307 | L.CName s =>
|
adamc@403
|
308 ((L'.CName s, loc), kname, [])
|
adamc@403
|
309
|
adamc@403
|
310 | L.CRecord xcs =>
|
adamc@403
|
311 let
|
adamc@403
|
312 val k = kunif loc
|
adamc@403
|
313
|
adamc@403
|
314 val (xcs', gs) = ListUtil.foldlMap (fn ((x, c), gs) =>
|
adamc@403
|
315 let
|
adamc@403
|
316 val (x', xk, gs1) = elabCon (env, denv) x
|
adamc@403
|
317 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@403
|
318 in
|
adamc@403
|
319 checkKind env x' xk kname;
|
adamc@403
|
320 checkKind env c' ck k;
|
adamc@403
|
321 ((x', c'), gs1 @ gs2 @ gs)
|
adamc@403
|
322 end) [] xcs
|
adamc@403
|
323
|
adamc@403
|
324 val rc = (L'.CRecord (k, xcs'), loc)
|
adamc@403
|
325 (* Add duplicate field checking later. *)
|
adamc@403
|
326
|
adamc@403
|
327 fun prove (xcs, ds) =
|
adamc@403
|
328 case xcs of
|
adamc@403
|
329 [] => ds
|
adamc@403
|
330 | xc :: rest =>
|
adamc@403
|
331 let
|
adamc@403
|
332 val r1 = (L'.CRecord (k, [xc]), loc)
|
adamc@403
|
333 val ds = foldl (fn (xc', ds) =>
|
adamc@403
|
334 let
|
adamc@403
|
335 val r2 = (L'.CRecord (k, [xc']), loc)
|
adamc@403
|
336 in
|
adamc@403
|
337 D.prove env denv (r1, r2, loc) @ ds
|
adamc@403
|
338 end)
|
adamc@403
|
339 ds rest
|
adamc@403
|
340 in
|
adamc@403
|
341 prove (rest, ds)
|
adamc@403
|
342 end
|
adamc@403
|
343 in
|
adamc@403
|
344 (rc, (L'.KRecord k, loc), prove (xcs', gs))
|
adamc@403
|
345 end
|
adamc@403
|
346 | L.CConcat (c1, c2) =>
|
adamc@403
|
347 let
|
adamc@403
|
348 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@403
|
349 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@403
|
350 val ku = kunif loc
|
adamc@403
|
351 val k = (L'.KRecord ku, loc)
|
adamc@403
|
352 in
|
adamc@403
|
353 checkKind env c1' k1 k;
|
adamc@403
|
354 checkKind env c2' k2 k;
|
adamc@403
|
355 ((L'.CConcat (c1', c2'), loc), k,
|
adamc@403
|
356 D.prove env denv (c1', c2', loc) @ gs1 @ gs2)
|
adamc@403
|
357 end
|
adamc@403
|
358 | L.CFold =>
|
adamc@403
|
359 let
|
adamc@403
|
360 val dom = kunif loc
|
adamc@403
|
361 val ran = kunif loc
|
adamc@403
|
362 in
|
adamc@403
|
363 ((L'.CFold (dom, ran), loc),
|
adamc@403
|
364 foldKind (dom, ran, loc),
|
adamc@403
|
365 [])
|
adamc@403
|
366 end
|
adamc@403
|
367
|
adamc@403
|
368 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), [])
|
adamc@403
|
369
|
adamc@403
|
370 | L.CTuple cs =>
|
adamc@403
|
371 let
|
adamc@403
|
372 val (cs', ks, gs) = foldl (fn (c, (cs', ks, gs)) =>
|
adamc@403
|
373 let
|
adamc@403
|
374 val (c', k, gs') = elabCon (env, denv) c
|
adamc@403
|
375 in
|
adamc@403
|
376 (c' :: cs', k :: ks, gs' @ gs)
|
adamc@403
|
377 end) ([], [], []) cs
|
adamc@403
|
378 in
|
adamc@403
|
379 ((L'.CTuple (rev cs'), loc), (L'.KTuple (rev ks), loc), gs)
|
adamc@403
|
380 end
|
adamc@403
|
381 | L.CProj (c, n) =>
|
adamc@403
|
382 let
|
adamc@403
|
383 val (c', k, gs) = elabCon (env, denv) c
|
adamc@403
|
384 in
|
adamc@403
|
385 case hnormKind k of
|
adamc@403
|
386 (L'.KTuple ks, _) =>
|
adamc@403
|
387 if n <= 0 orelse n > length ks then
|
adamc@403
|
388 (conError env (ProjBounds (c', n));
|
adamc@83
|
389 (cerror, kerror, []))
|
adamc@403
|
390 else
|
adamc@403
|
391 ((L'.CProj (c', n), loc), List.nth (ks, n - 1), gs)
|
adamc@403
|
392 | k => (conError env (ProjMismatch (c', k));
|
adamc@403
|
393 (cerror, kerror, []))
|
adamc@403
|
394 end
|
adamc@403
|
395
|
adamc@403
|
396 | L.CWild k =>
|
adamc@403
|
397 let
|
adamc@403
|
398 val k' = elabKind k
|
adamc@403
|
399 in
|
adamc@403
|
400 (cunif (loc, k'), k', [])
|
adamc@403
|
401 end
|
adamc@403
|
402
|
adamc@403
|
403 fun kunifsRemain k =
|
adamc@403
|
404 case k of
|
adamc@403
|
405 L'.KUnif (_, _, ref NONE) => true
|
adamc@403
|
406 | _ => false
|
adamc@403
|
407 fun cunifsRemain c =
|
adamc@403
|
408 case c of
|
adamc@403
|
409 L'.CUnif (loc, _, _, ref NONE) => SOME loc
|
adamc@403
|
410 | _ => NONE
|
adamc@403
|
411
|
adamc@403
|
412 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
|
adamc@403
|
413 con = fn _ => false,
|
adamc@403
|
414 exp = fn _ => false,
|
adamc@403
|
415 sgn_item = fn _ => false,
|
adamc@403
|
416 sgn = fn _ => false,
|
adamc@403
|
417 str = fn _ => false,
|
adamc@403
|
418 decl = fn _ => false}
|
adamc@403
|
419
|
adamc@403
|
420 val cunifsInDecl = U.Decl.search {kind = fn _ => NONE,
|
adamc@403
|
421 con = cunifsRemain,
|
adamc@403
|
422 exp = fn _ => NONE,
|
adamc@403
|
423 sgn_item = fn _ => NONE,
|
adamc@403
|
424 sgn = fn _ => NONE,
|
adamc@403
|
425 str = fn _ => NONE,
|
adamc@403
|
426 decl = fn _ => NONE}
|
adamc@403
|
427
|
adamc@403
|
428 fun occursCon r =
|
adamc@403
|
429 U.Con.exists {kind = fn _ => false,
|
adamc@403
|
430 con = fn L'.CUnif (_, _, _, r') => r = r'
|
adamc@403
|
431 | _ => false}
|
adamc@403
|
432
|
adamc@403
|
433 exception CUnify' of cunify_error
|
adamc@403
|
434
|
adamc@403
|
435 exception SynUnif = E.SynUnif
|
adamc@403
|
436
|
adamc@403
|
437 open ElabOps
|
adamc@403
|
438
|
adamc@403
|
439 type record_summary = {
|
adamc@403
|
440 fields : (L'.con * L'.con) list,
|
adamc@403
|
441 unifs : (L'.con * L'.con option ref) list,
|
adamc@403
|
442 others : L'.con list
|
adamc@403
|
443 }
|
adamc@403
|
444
|
adamc@403
|
445 fun summaryToCon {fields, unifs, others} =
|
adamc@403
|
446 let
|
adamc@403
|
447 val c = (L'.CRecord (ktype, []), dummy)
|
adamc@403
|
448 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
|
adamc@403
|
449 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
|
adamc@403
|
450 in
|
adamc@403
|
451 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
|
adamc@403
|
452 end
|
adamc@403
|
453
|
adamc@403
|
454 fun p_summary env s = p_con env (summaryToCon s)
|
adamc@403
|
455
|
adamc@403
|
456 exception CUnify of L'.con * L'.con * cunify_error
|
adamc@403
|
457
|
adamc@403
|
458 fun kindof env (c, loc) =
|
adamc@403
|
459 case c of
|
adamc@403
|
460 L'.TFun _ => ktype
|
adamc@403
|
461 | L'.TCFun _ => ktype
|
adamc@403
|
462 | L'.TRecord _ => ktype
|
adamc@403
|
463
|
adamc@403
|
464 | L'.CRel xn => #2 (E.lookupCRel env xn)
|
adamc@403
|
465 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
|
adamc@403
|
466 | L'.CModProj (n, ms, x) =>
|
adamc@403
|
467 let
|
adamc@403
|
468 val (_, sgn) = E.lookupStrNamed env n
|
adamc@403
|
469 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@403
|
470 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@403
|
471 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@403
|
472 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@403
|
473 ((L'.StrVar n, loc), sgn) ms
|
adamc@403
|
474 in
|
adamc@403
|
475 case E.projectCon env {sgn = sgn, str = str, field = x} of
|
adamc@403
|
476 NONE => raise Fail "kindof: Unknown con in structure"
|
adamc@403
|
477 | SOME (k, _) => k
|
adamc@403
|
478 end
|
adamc@403
|
479
|
adamc@403
|
480 | L'.CApp (c, _) =>
|
adamc@403
|
481 (case hnormKind (kindof env c) of
|
adamc@403
|
482 (L'.KArrow (_, k), _) => k
|
adamc@403
|
483 | (L'.KError, _) => kerror
|
adamc@413
|
484 | k => raise CUnify' (CKindof (k, c, "arrow")))
|
adamc@403
|
485 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
|
adamc@403
|
486 | L'.CDisjoint (_, _, _, c) => kindof env c
|
adamc@403
|
487
|
adamc@403
|
488 | L'.CName _ => kname
|
adamc@403
|
489
|
adamc@403
|
490 | L'.CRecord (k, _) => (L'.KRecord k, loc)
|
adamc@403
|
491 | L'.CConcat (c, _) => kindof env c
|
adamc@403
|
492 | L'.CFold (dom, ran) => foldKind (dom, ran, loc)
|
adamc@403
|
493
|
adamc@403
|
494 | L'.CUnit => (L'.KUnit, loc)
|
adamc@403
|
495
|
adamc@403
|
496 | L'.CTuple cs => (L'.KTuple (map (kindof env) cs), loc)
|
adamc@403
|
497 | L'.CProj (c, n) =>
|
adamc@403
|
498 (case hnormKind (kindof env c) of
|
adamc@403
|
499 (L'.KTuple ks, _) => List.nth (ks, n - 1)
|
adamc@413
|
500 | k => raise CUnify' (CKindof (k, c, "tuple")))
|
adamc@403
|
501
|
adamc@403
|
502 | L'.CError => kerror
|
adamc@403
|
503 | L'.CUnif (_, k, _, _) => k
|
adamc@403
|
504
|
adamc@403
|
505 val hnormCon = D.hnormCon
|
adamc@403
|
506
|
adamc@403
|
507 datatype con_summary =
|
adamc@403
|
508 Nil
|
adamc@403
|
509 | Cons
|
adamc@403
|
510 | Unknown
|
adamc@403
|
511
|
adamc@403
|
512 fun compatible cs =
|
adamc@403
|
513 case cs of
|
adamc@403
|
514 (Unknown, _) => false
|
adamc@403
|
515 | (_, Unknown) => false
|
adamc@403
|
516 | (s1, s2) => s1 = s2
|
adamc@403
|
517
|
adamc@403
|
518 fun summarizeCon (env, denv) c =
|
adamc@403
|
519 let
|
adamc@403
|
520 val (c, gs) = hnormCon (env, denv) c
|
adamc@403
|
521 in
|
adamc@403
|
522 case #1 c of
|
adamc@403
|
523 L'.CRecord (_, []) => (Nil, gs)
|
adamc@403
|
524 | L'.CRecord (_, _ :: _) => (Cons, gs)
|
adamc@403
|
525 | L'.CConcat ((L'.CRecord (_, _ :: _), _), _) => (Cons, gs)
|
adamc@403
|
526 | L'.CDisjoint (_, _, _, c) =>
|
adamc@34
|
527 let
|
adamc@403
|
528 val (s, gs') = summarizeCon (env, denv) c
|
adamc@34
|
529 in
|
adamc@403
|
530 (s, gs @ gs')
|
adamc@403
|
531 end
|
adamc@403
|
532 | _ => (Unknown, gs)
|
adamc@403
|
533 end
|
adamc@403
|
534
|
adamc@403
|
535 fun p_con_summary s =
|
adamc@403
|
536 Print.PD.string (case s of
|
adamc@403
|
537 Nil => "Nil"
|
adamc@403
|
538 | Cons => "Cons"
|
adamc@403
|
539 | Unknown => "Unknown")
|
adamc@403
|
540
|
adamc@403
|
541 exception SummaryFailure
|
adamc@403
|
542
|
adamc@513
|
543 fun isUnitCon env (c, loc) =
|
adamc@513
|
544 case c of
|
adamc@513
|
545 L'.TFun _ => false
|
adamc@513
|
546 | L'.TCFun _ => false
|
adamc@513
|
547 | L'.TRecord _ => false
|
adamc@513
|
548
|
adamc@513
|
549 | L'.CRel xn => #1 (#2 (E.lookupCRel env xn)) = L'.KUnit
|
adamc@513
|
550 | L'.CNamed xn => #1 (#2 (E.lookupCNamed env xn)) = L'.KUnit
|
adamc@513
|
551 | L'.CModProj (n, ms, x) =>
|
adamc@513
|
552 let
|
adamc@513
|
553 val (_, sgn) = E.lookupStrNamed env n
|
adamc@513
|
554 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@513
|
555 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@513
|
556 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@513
|
557 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@513
|
558 ((L'.StrVar n, loc), sgn) ms
|
adamc@513
|
559 in
|
adamc@513
|
560 case E.projectCon env {sgn = sgn, str = str, field = x} of
|
adamc@513
|
561 NONE => raise Fail "kindof: Unknown con in structure"
|
adamc@513
|
562 | SOME ((k, _), _) => k = L'.KUnit
|
adamc@513
|
563 end
|
adamc@513
|
564
|
adamc@513
|
565 | L'.CApp (c, _) =>
|
adamc@513
|
566 (case hnormKind (kindof env c) of
|
adamc@513
|
567 (L'.KArrow (_, k), _) => #1 k = L'.KUnit
|
adamc@513
|
568 | (L'.KError, _) => false
|
adamc@513
|
569 | k => raise CUnify' (CKindof (k, c, "arrow")))
|
adamc@513
|
570 | L'.CAbs _ => false
|
adamc@513
|
571 | L'.CDisjoint (_, _, _, c) => isUnitCon env c
|
adamc@513
|
572
|
adamc@513
|
573 | L'.CName _ => false
|
adamc@513
|
574
|
adamc@513
|
575 | L'.CRecord _ => false
|
adamc@513
|
576 | L'.CConcat _ => false
|
adamc@513
|
577 | L'.CFold _ => false
|
adamc@513
|
578
|
adamc@513
|
579 | L'.CUnit => true
|
adamc@513
|
580
|
adamc@513
|
581 | L'.CTuple _ => false
|
adamc@513
|
582 | L'.CProj (c, n) =>
|
adamc@513
|
583 (case hnormKind (kindof env c) of
|
adamc@513
|
584 (L'.KTuple ks, _) => #1 (List.nth (ks, n - 1)) = L'.KUnit
|
adamc@513
|
585 | k => raise CUnify' (CKindof (k, c, "tuple")))
|
adamc@513
|
586
|
adamc@513
|
587 | L'.CError => false
|
adamc@513
|
588 | L'.CUnif (_, k, _, _) => #1 k = L'.KUnit
|
adamc@513
|
589
|
adamc@403
|
590 fun unifyRecordCons (env, denv) (c1, c2) =
|
adamc@403
|
591 let
|
adamc@403
|
592 fun rkindof c =
|
adamc@403
|
593 case hnormKind (kindof env c) of
|
adamc@403
|
594 (L'.KRecord k, _) => k
|
adamc@403
|
595 | (L'.KError, _) => kerror
|
adamc@413
|
596 | k => raise CUnify' (CKindof (k, c, "record"))
|
adamc@403
|
597
|
adamc@403
|
598 val k1 = rkindof c1
|
adamc@403
|
599 val k2 = rkindof c2
|
adamc@403
|
600
|
adamc@403
|
601 val (r1, gs1) = recordSummary (env, denv) c1
|
adamc@403
|
602 val (r2, gs2) = recordSummary (env, denv) c2
|
adamc@403
|
603 in
|
adamc@403
|
604 unifyKinds k1 k2;
|
adamc@403
|
605 unifySummaries (env, denv) (k1, r1, r2);
|
adamc@403
|
606 gs1 @ gs2
|
adamc@403
|
607 end
|
adamc@403
|
608
|
adamc@403
|
609 and recordSummary (env, denv) c =
|
adamc@403
|
610 let
|
adamc@403
|
611 val (c, gs) = hnormCon (env, denv) c
|
adamc@403
|
612
|
adamc@403
|
613 val (sum, gs') =
|
adamc@403
|
614 case c of
|
adamc@403
|
615 (L'.CRecord (_, xcs), _) => ({fields = xcs, unifs = [], others = []}, [])
|
adamc@403
|
616 | (L'.CConcat (c1, c2), _) =>
|
adamc@403
|
617 let
|
adamc@403
|
618 val (s1, gs1) = recordSummary (env, denv) c1
|
adamc@403
|
619 val (s2, gs2) = recordSummary (env, denv) c2
|
adamc@403
|
620 in
|
adamc@403
|
621 ({fields = #fields s1 @ #fields s2,
|
adamc@403
|
622 unifs = #unifs s1 @ #unifs s2,
|
adamc@403
|
623 others = #others s1 @ #others s2},
|
adamc@403
|
624 gs1 @ gs2)
|
adamc@403
|
625 end
|
adamc@403
|
626 | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary (env, denv) c
|
adamc@403
|
627 | c' as (L'.CUnif (_, _, _, r), _) => ({fields = [], unifs = [(c', r)], others = []}, [])
|
adamc@403
|
628 | c' => ({fields = [], unifs = [], others = [c']}, [])
|
adamc@403
|
629 in
|
adamc@403
|
630 (sum, gs @ gs')
|
adamc@403
|
631 end
|
adamc@403
|
632
|
adamc@403
|
633 and consEq (env, denv) (c1, c2) =
|
adamc@403
|
634 let
|
adamc@403
|
635 val gs = unifyCons (env, denv) c1 c2
|
adamc@403
|
636 in
|
adamc@403
|
637 List.all (fn (loc, env, denv, c1, c2) =>
|
adamc@403
|
638 case D.prove env denv (c1, c2, loc) of
|
adamc@403
|
639 [] => true
|
adamc@403
|
640 | _ => false) gs
|
adamc@403
|
641 end
|
adamc@403
|
642 handle CUnify _ => false
|
adamc@403
|
643
|
adamc@403
|
644 and consNeq env (c1, c2) =
|
adamc@403
|
645 case (#1 (ElabOps.hnormCon env c1), #1 (ElabOps.hnormCon env c2)) of
|
adamc@403
|
646 (L'.CName x1, L'.CName x2) => x1 <> x2
|
adamc@403
|
647 | _ => false
|
adamc@403
|
648
|
adamc@403
|
649 and unifySummaries (env, denv) (k, s1 : record_summary, s2 : record_summary) =
|
adamc@403
|
650 let
|
adamc@403
|
651 val loc = #2 k
|
adamc@403
|
652 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
|
adamc@403
|
653 ("#2", p_summary env s2)]*)
|
adamc@403
|
654
|
adamc@403
|
655 fun eatMatching p (ls1, ls2) =
|
adamc@403
|
656 let
|
adamc@403
|
657 fun em (ls1, ls2, passed1) =
|
adamc@403
|
658 case ls1 of
|
adamc@403
|
659 [] => (rev passed1, ls2)
|
adamc@403
|
660 | h1 :: t1 =>
|
adamc@403
|
661 let
|
adamc@403
|
662 fun search (ls2', passed2) =
|
adamc@403
|
663 case ls2' of
|
adamc@403
|
664 [] => em (t1, ls2, h1 :: passed1)
|
adamc@403
|
665 | h2 :: t2 =>
|
adamc@403
|
666 if p (h1, h2) then
|
adamc@403
|
667 em (t1, List.revAppend (passed2, t2), passed1)
|
adamc@403
|
668 else
|
adamc@403
|
669 search (t2, h2 :: passed2)
|
adamc@403
|
670 in
|
adamc@403
|
671 search (ls2, [])
|
adamc@403
|
672 end
|
adamc@403
|
673 in
|
adamc@403
|
674 em (ls1, ls2, [])
|
adamc@403
|
675 end
|
adamc@403
|
676
|
adamc@403
|
677 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
|
adamc@403
|
678 not (consNeq env (x1, x2))
|
adamc@403
|
679 andalso consEq (env, denv) (c1, c2)
|
adamc@403
|
680 andalso consEq (env, denv) (x1, x2))
|
adamc@403
|
681 (#fields s1, #fields s2)
|
adamc@403
|
682 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
|
adamc@403
|
683 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
|
adamc@403
|
684 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
|
adamc@403
|
685 val (others1, others2) = eatMatching (consEq (env, denv)) (#others s1, #others s2)
|
adamc@403
|
686 (*val () = eprefaces "Summaries3" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
|
adamc@403
|
687 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
|
adamc@403
|
688
|
adamc@403
|
689 fun unifFields (fs, others, unifs) =
|
adamc@403
|
690 case (fs, others, unifs) of
|
adamc@403
|
691 ([], [], _) => ([], [], unifs)
|
adamc@403
|
692 | (_, _, []) => (fs, others, [])
|
adamc@403
|
693 | (_, _, (_, r) :: rest) =>
|
adamc@403
|
694 let
|
adamc@403
|
695 val r' = ref NONE
|
adamc@403
|
696 val kr = (L'.KRecord k, dummy)
|
adamc@403
|
697 val cr' = (L'.CUnif (dummy, kr, "recd", r'), dummy)
|
adamc@403
|
698
|
adamc@403
|
699 val prefix = case (fs, others) of
|
adamc@403
|
700 ([], other :: others) =>
|
adamc@403
|
701 List.foldl (fn (other, c) =>
|
adamc@403
|
702 (L'.CConcat (c, other), dummy))
|
adamc@403
|
703 other others
|
adamc@403
|
704 | (fs, []) =>
|
adamc@403
|
705 (L'.CRecord (k, fs), dummy)
|
adamc@403
|
706 | (fs, others) =>
|
adamc@403
|
707 List.foldl (fn (other, c) =>
|
adamc@403
|
708 (L'.CConcat (c, other), dummy))
|
adamc@403
|
709 (L'.CRecord (k, fs), dummy) others
|
adamc@403
|
710 in
|
adamc@403
|
711 r := SOME (L'.CConcat (prefix, cr'), dummy);
|
adamc@403
|
712 ([], [], (cr', r') :: rest)
|
adamc@403
|
713 end
|
adamc@403
|
714
|
adamc@403
|
715 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
|
adamc@403
|
716 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
|
adamc@403
|
717
|
adamc@403
|
718 (*val () = eprefaces "Summaries4" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
|
adamc@403
|
719 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
|
adamc@403
|
720
|
adamc@403
|
721 fun isGuessable (other, fs) =
|
adamc@403
|
722 let
|
adamc@403
|
723 val gs = guessFold (env, denv) (other, (L'.CRecord (k, fs), loc), [], SummaryFailure)
|
adamc@403
|
724 in
|
adamc@403
|
725 List.all (fn (loc, env, denv, c1, c2) =>
|
adamc@403
|
726 case D.prove env denv (c1, c2, loc) of
|
adamc@403
|
727 [] => true
|
adamc@403
|
728 | _ => false) gs
|
adamc@403
|
729 end
|
adamc@403
|
730 handle SummaryFailure => false
|
adamc@403
|
731
|
adamc@403
|
732 val (fs1, fs2, others1, others2) =
|
adamc@403
|
733 case (fs1, fs2, others1, others2) of
|
adamc@403
|
734 ([], _, [other1], []) =>
|
adamc@403
|
735 if isGuessable (other1, fs2) then
|
adamc@403
|
736 ([], [], [], [])
|
adamc@403
|
737 else
|
adamc@403
|
738 (fs1, fs2, others1, others2)
|
adamc@403
|
739 | (_, [], [], [other2]) =>
|
adamc@403
|
740 if isGuessable (other2, fs1) then
|
adamc@403
|
741 ([], [], [], [])
|
adamc@403
|
742 else
|
adamc@403
|
743 (fs1, fs2, others1, others2)
|
adamc@403
|
744 | _ => (fs1, fs2, others1, others2)
|
adamc@403
|
745
|
adamc@403
|
746 (*val () = eprefaces "Summaries5" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
|
adamc@403
|
747 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
|
adamc@403
|
748
|
adamc@403
|
749 val clear = case (fs1, others1, fs2, others2) of
|
adamc@403
|
750 ([], [], [], []) => true
|
adamc@403
|
751 | _ => false
|
adamc@403
|
752 val empty = (L'.CRecord (k, []), dummy)
|
adamc@403
|
753
|
adamc@403
|
754 fun unsummarize {fields, unifs, others} =
|
adamc@403
|
755 let
|
adamc@403
|
756 val c = (L'.CRecord (k, fields), loc)
|
adamc@403
|
757
|
adamc@403
|
758 val c = foldl (fn ((c1, _), c2) => (L'.CConcat (c1, c2), loc))
|
adamc@403
|
759 c unifs
|
adamc@403
|
760 in
|
adamc@403
|
761 foldl (fn (c1, c2) => (L'.CConcat (c1, c2), loc))
|
adamc@403
|
762 c others
|
adamc@403
|
763 end
|
adamc@403
|
764
|
adamc@403
|
765 fun pairOffUnifs (unifs1, unifs2) =
|
adamc@403
|
766 case (unifs1, unifs2) of
|
adamc@403
|
767 ([], _) =>
|
adamc@403
|
768 if clear then
|
adamc@403
|
769 List.app (fn (_, r) => r := SOME empty) unifs2
|
adamc@403
|
770 else
|
adamc@403
|
771 raise CUnify' (CRecordFailure (unsummarize s1, unsummarize s2))
|
adamc@403
|
772 | (_, []) =>
|
adamc@403
|
773 if clear then
|
adamc@403
|
774 List.app (fn (_, r) => r := SOME empty) unifs1
|
adamc@403
|
775 else
|
adamc@403
|
776 raise CUnify' (CRecordFailure (unsummarize s1, unsummarize s2))
|
adamc@403
|
777 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
|
adamc@403
|
778 (r2 := SOME c1;
|
adamc@403
|
779 pairOffUnifs (rest1, rest2))
|
adamc@403
|
780 in
|
adamc@403
|
781 pairOffUnifs (unifs1, unifs2)
|
adamc@403
|
782 (*before eprefaces "Summaries'" [("#1", p_summary env s1),
|
adamc@403
|
783 ("#2", p_summary env s2)]*)
|
adamc@403
|
784 end
|
adamc@403
|
785
|
adamc@403
|
786 and guessFold (env, denv) (c1, c2, gs, ex) =
|
adamc@403
|
787 let
|
adamc@403
|
788 val loc = #2 c1
|
adamc@403
|
789
|
adamc@403
|
790 fun unfold (dom, ran, f, i, r, c) =
|
adamc@403
|
791 let
|
adamc@403
|
792 val nm = cunif (loc, (L'.KName, loc))
|
adamc@412
|
793 val v =
|
adamc@412
|
794 case dom of
|
adamc@412
|
795 (L'.KUnit, _) => (L'.CUnit, loc)
|
adamc@412
|
796 | _ => cunif (loc, dom)
|
adamc@403
|
797 val rest = cunif (loc, (L'.KRecord dom, loc))
|
adamc@403
|
798 val acc = (L'.CFold (dom, ran), loc)
|
adamc@403
|
799 val acc = (L'.CApp (acc, f), loc)
|
adamc@403
|
800 val acc = (L'.CApp (acc, i), loc)
|
adamc@403
|
801 val acc = (L'.CApp (acc, rest), loc)
|
adamc@403
|
802
|
adamc@403
|
803 val (iS, gs3) = summarizeCon (env, denv) i
|
adamc@403
|
804
|
adamc@403
|
805 val app = (L'.CApp (f, nm), loc)
|
adamc@403
|
806 val app = (L'.CApp (app, v), loc)
|
adamc@403
|
807 val app = (L'.CApp (app, acc), loc)
|
adamc@403
|
808 val (appS, gs4) = summarizeCon (env, denv) app
|
adamc@403
|
809
|
adamc@403
|
810 val (cS, gs5) = summarizeCon (env, denv) c
|
adamc@403
|
811 in
|
adamc@403
|
812 (*prefaces "Summaries" [("iS", p_con_summary iS),
|
adamc@403
|
813 ("appS", p_con_summary appS),
|
adamc@403
|
814 ("cS", p_con_summary cS)];*)
|
adamc@403
|
815
|
adamc@403
|
816 if compatible (iS, appS) then
|
adamc@403
|
817 raise ex
|
adamc@403
|
818 else if compatible (cS, iS) then
|
adamc@403
|
819 let
|
adamc@403
|
820 (*val () = prefaces "Same?" [("i", p_con env i),
|
adamc@403
|
821 ("c", p_con env c)]*)
|
adamc@403
|
822 val gs6 = unifyCons (env, denv) i c
|
adamc@403
|
823 (*val () = TextIO.print "Yes!\n"*)
|
adamc@403
|
824
|
adamc@403
|
825 val gs7 = unifyCons (env, denv) r (L'.CRecord (dom, []), loc)
|
adamc@403
|
826 in
|
adamc@403
|
827 gs @ gs3 @ gs5 @ gs6 @ gs7
|
adamc@403
|
828 end
|
adamc@403
|
829 else if compatible (cS, appS) then
|
adamc@403
|
830 let
|
adamc@403
|
831 (*val () = prefaces "Same?" [("app", p_con env app),
|
adamc@403
|
832 ("c", p_con env c),
|
adamc@403
|
833 ("app'", p_con env (#1 (hnormCon (env, denv) app)))]*)
|
adamc@403
|
834 val gs6 = unifyCons (env, denv) app c
|
adamc@403
|
835 (*val () = TextIO.print "Yes!\n"*)
|
adamc@403
|
836
|
adamc@403
|
837 val singleton = (L'.CRecord (dom, [(nm, v)]), loc)
|
adamc@403
|
838 val concat = (L'.CConcat (singleton, rest), loc)
|
adamc@403
|
839 (*val () = prefaces "Pre-crew" [("r", p_con env r),
|
adamc@403
|
840 ("concat", p_con env concat)]*)
|
adamc@403
|
841 val gs7 = unifyCons (env, denv) r concat
|
adamc@403
|
842 in
|
adamc@403
|
843 (*prefaces "The crew" [("nm", p_con env nm),
|
adamc@403
|
844 ("v", p_con env v),
|
adamc@403
|
845 ("rest", p_con env rest)];*)
|
adamc@403
|
846
|
adamc@403
|
847 gs @ gs3 @ gs4 @ gs5 @ gs6 @ gs7
|
adamc@403
|
848 end
|
adamc@403
|
849 else
|
adamc@403
|
850 raise ex
|
adamc@403
|
851 end
|
adamc@403
|
852 handle _ => raise ex
|
adamc@403
|
853 in
|
adamc@403
|
854 case (#1 c1, #1 c2) of
|
adamc@403
|
855 (L'.CApp ((L'.CApp ((L'.CApp ((L'.CFold (dom, ran), _), f), _), i), _), r), _) =>
|
adamc@403
|
856 unfold (dom, ran, f, i, r, c2)
|
adamc@403
|
857 | (_, L'.CApp ((L'.CApp ((L'.CApp ((L'.CFold (dom, ran), _), f), _), i), _), r)) =>
|
adamc@403
|
858 unfold (dom, ran, f, i, r, c1)
|
adamc@403
|
859 | _ => raise ex
|
adamc@403
|
860 end
|
adamc@403
|
861
|
adamc@403
|
862 and unifyCons' (env, denv) c1 c2 =
|
adamc@403
|
863 case (#1 c1, #1 c2) of
|
adamc@403
|
864 (L'.CDisjoint (_, x1, y1, t1), L'.CDisjoint (_, x2, y2, t2)) =>
|
adamc@403
|
865 let
|
adamc@403
|
866 val gs1 = unifyCons' (env, denv) x1 x2
|
adamc@403
|
867 val gs2 = unifyCons' (env, denv) y1 y2
|
adamc@403
|
868 val (denv', gs3) = D.assert env denv (x1, y1)
|
adamc@403
|
869 val gs4 = unifyCons' (env, denv') t1 t2
|
adamc@403
|
870 in
|
adamc@403
|
871 gs1 @ gs2 @ gs3 @ gs4
|
adamc@403
|
872 end
|
adamc@403
|
873 | _ =>
|
adamc@513
|
874 if isUnitCon env c1 andalso isUnitCon env c2 then
|
adamc@513
|
875 []
|
adamc@513
|
876 else
|
adamc@403
|
877 let
|
adamc@411
|
878 val (c1, gs1) = hnormCon (env, denv) c1
|
adamc@411
|
879 val (c2, gs2) = hnormCon (env, denv) c2
|
adamc@403
|
880 in
|
adamc@411
|
881 let
|
adamc@411
|
882 val gs3 = unifyCons'' (env, denv) c1 c2
|
adamc@411
|
883 in
|
adamc@411
|
884 gs1 @ gs2 @ gs3
|
adamc@411
|
885 end
|
adamc@411
|
886 handle ex => guessFold (env, denv) (c1, c2, gs1 @ gs2, ex)
|
adamc@403
|
887 end
|
adamc@403
|
888
|
adamc@403
|
889 and unifyCons'' (env, denv) (c1All as (c1, loc)) (c2All as (c2, _)) =
|
adamc@403
|
890 let
|
adamc@403
|
891 fun err f = raise CUnify' (f (c1All, c2All))
|
adamc@403
|
892
|
adamc@403
|
893 fun isRecord () = unifyRecordCons (env, denv) (c1All, c2All)
|
adamc@403
|
894 in
|
adamc@403
|
895 (*eprefaces "unifyCons''" [("c1All", p_con env c1All),
|
adamc@403
|
896 ("c2All", p_con env c2All)];*)
|
adamc@403
|
897
|
adamc@403
|
898 case (c1, c2) of
|
adamc@403
|
899 (L'.CUnit, L'.CUnit) => []
|
adamc@403
|
900
|
adamc@403
|
901 | (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
|
adamc@403
|
902 unifyCons' (env, denv) d1 d2
|
adamc@403
|
903 @ unifyCons' (env, denv) r1 r2
|
adamc@403
|
904 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
|
adamc@403
|
905 if expl1 <> expl2 then
|
adamc@403
|
906 err CExplicitness
|
adamc@403
|
907 else
|
adamc@403
|
908 (unifyKinds d1 d2;
|
adamc@403
|
909 unifyCons' (E.pushCRel env x1 d1, D.enter denv) r1 r2)
|
adamc@403
|
910 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' (env, denv) r1 r2
|
adamc@403
|
911
|
adamc@403
|
912 | (L'.CRel n1, L'.CRel n2) =>
|
adamc@403
|
913 if n1 = n2 then
|
adamc@403
|
914 []
|
adamc@403
|
915 else
|
adamc@403
|
916 err CIncompatible
|
adamc@403
|
917 | (L'.CNamed n1, L'.CNamed n2) =>
|
adamc@403
|
918 if n1 = n2 then
|
adamc@403
|
919 []
|
adamc@403
|
920 else
|
adamc@403
|
921 err CIncompatible
|
adamc@403
|
922
|
adamc@403
|
923 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
|
adamc@403
|
924 (unifyCons' (env, denv) d1 d2;
|
adamc@403
|
925 unifyCons' (env, denv) r1 r2)
|
adamc@403
|
926 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
|
adamc@403
|
927 (unifyKinds k1 k2;
|
adamc@403
|
928 unifyCons' (E.pushCRel env x1 k1, D.enter denv) c1 c2)
|
adamc@403
|
929
|
adamc@403
|
930 | (L'.CName n1, L'.CName n2) =>
|
adamc@403
|
931 if n1 = n2 then
|
adamc@403
|
932 []
|
adamc@403
|
933 else
|
adamc@403
|
934 err CIncompatible
|
adamc@403
|
935
|
adamc@403
|
936 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
|
adamc@403
|
937 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
|
adamc@403
|
938 []
|
adamc@403
|
939 else
|
adamc@403
|
940 err CIncompatible
|
adamc@403
|
941
|
adamc@403
|
942 | (L'.CTuple cs1, L'.CTuple cs2) =>
|
adamc@403
|
943 ((ListPair.foldlEq (fn (c1, c2, gs) =>
|
adamc@403
|
944 let
|
adamc@403
|
945 val gs' = unifyCons' (env, denv) c1 c2
|
adamc@403
|
946 in
|
adamc@403
|
947 gs' @ gs
|
adamc@403
|
948 end) [] (cs1, cs2))
|
adamc@403
|
949 handle ListPair.UnequalLengths => err CIncompatible)
|
adamc@403
|
950
|
adamc@403
|
951 | (L'.CProj ((L'.CUnif (_, _, _, ref (SOME c1)), loc), n1), _) =>
|
adamc@403
|
952 unifyCons' (env, denv) (L'.CProj (c1, n1), loc) c2All
|
adamc@403
|
953 | (_, L'.CProj ((L'.CUnif (_, _, _, ref (SOME c2)), loc), n2)) =>
|
adamc@403
|
954 unifyCons' (env, denv) c1All (L'.CProj (c2, n2), loc)
|
adamc@403
|
955 | (L'.CProj ((L'.CUnif (_, (L'.KTuple ks, _), _, r), loc), n), _) =>
|
adamc@403
|
956 let
|
adamc@403
|
957 val us = map (fn k => cunif (loc, k)) ks
|
adamc@403
|
958 in
|
adamc@403
|
959 r := SOME (L'.CTuple us, loc);
|
adamc@403
|
960 unifyCons' (env, denv) (List.nth (us, n - 1)) c2All
|
adamc@403
|
961 end
|
adamc@403
|
962 | (_, L'.CProj ((L'.CUnif (_, (L'.KTuple ks, _), _, r), loc), n)) =>
|
adamc@403
|
963 let
|
adamc@403
|
964 val us = map (fn k => cunif (loc, k)) ks
|
adamc@403
|
965 in
|
adamc@403
|
966 r := SOME (L'.CTuple us, loc);
|
adamc@403
|
967 unifyCons' (env, denv) c1All (List.nth (us, n - 1))
|
adamc@403
|
968 end
|
adamc@403
|
969 | (L'.CProj (c1, n1), L'.CProj (c2, n2)) =>
|
adamc@403
|
970 if n1 = n2 then
|
adamc@403
|
971 unifyCons' (env, denv) c1 c2
|
adamc@403
|
972 else
|
adamc@403
|
973 err CIncompatible
|
adamc@403
|
974
|
adamc@403
|
975 | (L'.CError, _) => []
|
adamc@403
|
976 | (_, L'.CError) => []
|
adamc@403
|
977
|
adamc@403
|
978 | (L'.CRecord _, _) => isRecord ()
|
adamc@403
|
979 | (_, L'.CRecord _) => isRecord ()
|
adamc@403
|
980 | (L'.CConcat _, _) => isRecord ()
|
adamc@403
|
981 | (_, L'.CConcat _) => isRecord ()
|
adamc@403
|
982
|
adamc@403
|
983 | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) =>
|
adamc@403
|
984 if r1 = r2 then
|
adamc@403
|
985 []
|
adamc@403
|
986 else
|
adamc@403
|
987 (unifyKinds k1 k2;
|
adamc@403
|
988 r1 := SOME c2All;
|
adamc@403
|
989 [])
|
adamc@403
|
990
|
adamc@403
|
991 | (L'.CUnif (_, _, _, r), _) =>
|
adamc@403
|
992 if occursCon r c2All then
|
adamc@403
|
993 err COccursCheckFailed
|
adamc@403
|
994 else
|
adamc@403
|
995 (r := SOME c2All;
|
adamc@403
|
996 [])
|
adamc@403
|
997 | (_, L'.CUnif (_, _, _, r)) =>
|
adamc@403
|
998 if occursCon r c1All then
|
adamc@403
|
999 err COccursCheckFailed
|
adamc@403
|
1000 else
|
adamc@403
|
1001 (r := SOME c1All;
|
adamc@403
|
1002 [])
|
adamc@403
|
1003
|
adamc@403
|
1004 | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) =>
|
adamc@403
|
1005 (unifyKinds dom1 dom2;
|
adamc@403
|
1006 unifyKinds ran1 ran2;
|
adamc@403
|
1007 [])
|
adamc@403
|
1008
|
adamc@403
|
1009 | _ => err CIncompatible
|
adamc@403
|
1010 end
|
adamc@403
|
1011
|
adamc@403
|
1012 and unifyCons (env, denv) c1 c2 =
|
adamc@403
|
1013 unifyCons' (env, denv) c1 c2
|
adamc@403
|
1014 handle CUnify' err => raise CUnify (c1, c2, err)
|
adamc@403
|
1015 | KUnify args => raise CUnify (c1, c2, CKind args)
|
adamc@403
|
1016
|
adamc@403
|
1017 fun checkCon (env, denv) e c1 c2 =
|
adamc@403
|
1018 unifyCons (env, denv) c1 c2
|
adamc@403
|
1019 handle CUnify (c1, c2, err) =>
|
adamc@403
|
1020 (expError env (Unify (e, c1, c2, err));
|
adamc@83
|
1021 [])
|
adamc@403
|
1022
|
adamc@403
|
1023 fun checkPatCon (env, denv) p c1 c2 =
|
adamc@403
|
1024 unifyCons (env, denv) c1 c2
|
adamc@403
|
1025 handle CUnify (c1, c2, err) =>
|
adamc@403
|
1026 (expError env (PatUnify (p, c1, c2, err));
|
adamc@86
|
1027 [])
|
adamc@71
|
1028
|
adamc@403
|
1029 fun primType env p =
|
adamc@403
|
1030 case p of
|
adamc@403
|
1031 P.Int _ => !int
|
adamc@403
|
1032 | P.Float _ => !float
|
adamc@403
|
1033 | P.String _ => !string
|
adamc@403
|
1034
|
adamc@403
|
1035 fun recCons (k, nm, v, rest, loc) =
|
adamc@403
|
1036 (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc),
|
adamc@403
|
1037 rest), loc)
|
adamc@403
|
1038
|
adamc@403
|
1039 fun foldType (dom, loc) =
|
adamc@403
|
1040 (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc),
|
adamc@403
|
1041 (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc),
|
adamc@403
|
1042 (L'.TCFun (L'.Explicit, "v", dom,
|
adamc@403
|
1043 (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc),
|
adamc@403
|
1044 (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc),
|
adamc@403
|
1045 (L'.CDisjoint (L'.Instantiate,
|
adamc@403
|
1046 (L'.CRecord
|
adamc@403
|
1047 ((L'.KUnit, loc),
|
adamc@403
|
1048 [((L'.CRel 2, loc),
|
adamc@403
|
1049 (L'.CUnit, loc))]), loc),
|
adamc@403
|
1050 (L'.CRel 0, loc),
|
adamc@403
|
1051 (L'.CApp ((L'.CRel 3, loc),
|
adamc@403
|
1052 recCons (dom,
|
adamc@403
|
1053 (L'.CRel 2, loc),
|
adamc@403
|
1054 (L'.CRel 1, loc),
|
adamc@403
|
1055 (L'.CRel 0, loc),
|
adamc@403
|
1056 loc)), loc)),
|
adamc@403
|
1057 loc)), loc)),
|
adamc@403
|
1058 loc)), loc)), loc),
|
adamc@403
|
1059 (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc),
|
adamc@403
|
1060 (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc),
|
adamc@403
|
1061 (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)),
|
adamc@403
|
1062 loc)), loc)), loc)
|
adamc@403
|
1063
|
adamc@403
|
1064 datatype constraint =
|
adamc@403
|
1065 Disjoint of D.goal
|
adamc@403
|
1066 | TypeClass of E.env * L'.con * L'.exp option ref * ErrorMsg.span
|
adamc@403
|
1067
|
adamc@403
|
1068 val enD = map Disjoint
|
adamc@403
|
1069
|
adamc@403
|
1070 fun elabHead (env, denv) infer (e as (_, loc)) t =
|
adamc@403
|
1071 let
|
adamc@403
|
1072 fun unravel (t, e) =
|
adamc@403
|
1073 let
|
adamc@403
|
1074 val (t, gs) = hnormCon (env, denv) t
|
adamc@403
|
1075 in
|
adamc@403
|
1076 case t of
|
adamc@403
|
1077 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
|
adamc@403
|
1078 let
|
adamc@403
|
1079 val u = cunif (loc, k)
|
adamc@403
|
1080
|
adamc@403
|
1081 val t'' = subConInCon (0, u) t'
|
adamc@403
|
1082 val (e, t, gs') = unravel (t'', (L'.ECApp (e, u), loc))
|
adamc@403
|
1083 in
|
adamc@403
|
1084 (*prefaces "Unravel" [("t'", p_con env t'),
|
adamc@403
|
1085 ("t''", p_con env t'')];*)
|
adamc@403
|
1086 (e, t, enD gs @ gs')
|
adamc@403
|
1087 end
|
adamc@403
|
1088 | (L'.TFun (dom as (L'.CApp (cl, _), _), ran), _) =>
|
adamc@403
|
1089 let
|
adamc@403
|
1090 val cl = #1 (hnormCon (env, denv) cl)
|
adamc@403
|
1091 in
|
adamc@403
|
1092 if infer <> L.TypesOnly andalso E.isClass env cl then
|
adamc@403
|
1093 let
|
adamc@403
|
1094 val r = ref NONE
|
adamc@403
|
1095 val (e, t, gs') = unravel (ran, (L'.EApp (e, (L'.EUnif r, loc)), loc))
|
adamc@403
|
1096 in
|
adamc@403
|
1097 (e, t, TypeClass (env, dom, r, loc) :: enD gs @ gs')
|
adamc@403
|
1098 end
|
adamc@403
|
1099 else
|
adamc@403
|
1100 (e, t, enD gs)
|
adamc@403
|
1101 end
|
adamc@228
|
1102 | _ => (e, t, enD gs)
|
adamc@86
|
1103 end
|
adamc@15
|
1104 in
|
adamc@403
|
1105 case infer of
|
adamc@403
|
1106 L.DontInfer => (e, t, [])
|
adamc@403
|
1107 | _ => unravel (t, e)
|
adamc@15
|
1108 end
|
adamc@15
|
1109
|
adamc@174
|
1110 fun elabPat (pAll as (p, loc), (env, denv, bound)) =
|
adamc@171
|
1111 let
|
adamc@171
|
1112 val perror = (L'.PWild, loc)
|
adamc@171
|
1113 val terror = (L'.CError, loc)
|
adamc@171
|
1114 val pterror = (perror, terror)
|
adamc@171
|
1115 val rerror = (pterror, (env, bound))
|
adamc@171
|
1116
|
adamc@191
|
1117 fun pcon (pc, po, xs, to, dn, dk) =
|
adamc@188
|
1118 case (po, to) of
|
adamc@188
|
1119 (NONE, SOME _) => (expError env (PatHasNoArg loc);
|
adamc@188
|
1120 rerror)
|
adamc@188
|
1121 | (SOME _, NONE) => (expError env (PatHasArg loc);
|
adamc@188
|
1122 rerror)
|
adamc@191
|
1123 | (NONE, NONE) =>
|
adamc@191
|
1124 let
|
adamc@191
|
1125 val k = (L'.KType, loc)
|
adamc@191
|
1126 val unifs = map (fn _ => cunif (loc, k)) xs
|
adamc@191
|
1127 val dn = foldl (fn (u, dn) => (L'.CApp (dn, u), loc)) dn unifs
|
adamc@191
|
1128 in
|
adamc@191
|
1129 (((L'.PCon (dk, pc, unifs, NONE), loc), dn),
|
adamc@191
|
1130 (env, bound))
|
adamc@191
|
1131 end
|
adamc@188
|
1132 | (SOME p, SOME t) =>
|
adamc@188
|
1133 let
|
adamc@188
|
1134 val ((p', pt), (env, bound)) = elabPat (p, (env, denv, bound))
|
adamc@191
|
1135
|
adamc@191
|
1136 val k = (L'.KType, loc)
|
adamc@191
|
1137 val unifs = map (fn _ => cunif (loc, k)) xs
|
adamc@194
|
1138 val nxs = length unifs - 1
|
adamc@194
|
1139 val t = ListUtil.foldli (fn (i, u, t) => subConInCon (nxs - i, u) t) t unifs
|
adamc@191
|
1140 val dn = foldl (fn (u, dn) => (L'.CApp (dn, u), loc)) dn unifs
|
adamc@188
|
1141 in
|
adamc@191
|
1142 ignore (checkPatCon (env, denv) p' pt t);
|
adamc@191
|
1143 (((L'.PCon (dk, pc, unifs, SOME p'), loc), dn),
|
adamc@188
|
1144 (env, bound))
|
adamc@188
|
1145 end
|
adamc@171
|
1146 in
|
adamc@171
|
1147 case p of
|
adamc@171
|
1148 L.PWild => (((L'.PWild, loc), cunif (loc, (L'.KType, loc))),
|
adamc@171
|
1149 (env, bound))
|
adamc@171
|
1150 | L.PVar x =>
|
adamc@171
|
1151 let
|
adamc@171
|
1152 val t = if SS.member (bound, x) then
|
adamc@171
|
1153 (expError env (DuplicatePatternVariable (loc, x));
|
adamc@171
|
1154 terror)
|
adamc@171
|
1155 else
|
adamc@171
|
1156 cunif (loc, (L'.KType, loc))
|
adamc@171
|
1157 in
|
adamc@182
|
1158 (((L'.PVar (x, t), loc), t),
|
adamc@171
|
1159 (E.pushERel env x t, SS.add (bound, x)))
|
adamc@171
|
1160 end
|
adamc@173
|
1161 | L.PPrim p => (((L'.PPrim p, loc), primType env p),
|
adamc@173
|
1162 (env, bound))
|
adamc@171
|
1163 | L.PCon ([], x, po) =>
|
adamc@171
|
1164 (case E.lookupConstructor env x of
|
adamc@174
|
1165 NONE => (expError env (UnboundConstructor (loc, [], x));
|
adamc@171
|
1166 rerror)
|
adamc@191
|
1167 | SOME (dk, n, xs, to, dn) => pcon (L'.PConVar n, po, xs, to, (L'.CNamed dn, loc), dk))
|
adamc@174
|
1168 | L.PCon (m1 :: ms, x, po) =>
|
adamc@174
|
1169 (case E.lookupStr env m1 of
|
adamc@174
|
1170 NONE => (expError env (UnboundStrInExp (loc, m1));
|
adamc@174
|
1171 rerror)
|
adamc@174
|
1172 | SOME (n, sgn) =>
|
adamc@174
|
1173 let
|
adamc@174
|
1174 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@174
|
1175 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@182
|
1176 NONE => raise Fail "elabPat: Unknown substructure"
|
adamc@174
|
1177 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@174
|
1178 ((L'.StrVar n, loc), sgn) ms
|
adamc@174
|
1179 in
|
adamc@174
|
1180 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
|
adamc@174
|
1181 NONE => (expError env (UnboundConstructor (loc, m1 :: ms, x));
|
adamc@174
|
1182 rerror)
|
adamc@191
|
1183 | SOME (dk, _, xs, to, dn) => pcon (L'.PConProj (n, ms, x), po, xs, to, dn, dk)
|
adamc@174
|
1184 end)
|
adamc@174
|
1185
|
adamc@175
|
1186 | L.PRecord (xps, flex) =>
|
adamc@175
|
1187 let
|
adamc@175
|
1188 val (xpts, (env, bound, _)) =
|
adamc@175
|
1189 ListUtil.foldlMap (fn ((x, p), (env, bound, fbound)) =>
|
adamc@175
|
1190 let
|
adamc@175
|
1191 val ((p', t), (env, bound)) = elabPat (p, (env, denv, bound))
|
adamc@175
|
1192 in
|
adamc@175
|
1193 if SS.member (fbound, x) then
|
adamc@175
|
1194 expError env (DuplicatePatField (loc, x))
|
adamc@175
|
1195 else
|
adamc@175
|
1196 ();
|
adamc@175
|
1197 ((x, p', t), (env, bound, SS.add (fbound, x)))
|
adamc@175
|
1198 end)
|
adamc@175
|
1199 (env, bound, SS.empty) xps
|
adamc@175
|
1200
|
adamc@175
|
1201 val k = (L'.KType, loc)
|
adamc@175
|
1202 val c = (L'.CRecord (k, map (fn (x, _, t) => ((L'.CName x, loc), t)) xpts), loc)
|
adamc@176
|
1203 val c =
|
adamc@175
|
1204 if flex then
|
adamc@176
|
1205 (L'.CConcat (c, cunif (loc, (L'.KRecord k, loc))), loc)
|
adamc@175
|
1206 else
|
adamc@176
|
1207 c
|
adamc@175
|
1208 in
|
adamc@182
|
1209 (((L'.PRecord xpts, loc),
|
adamc@175
|
1210 (L'.TRecord c, loc)),
|
adamc@175
|
1211 (env, bound))
|
adamc@175
|
1212 end
|
adamc@175
|
1213
|
adamc@171
|
1214 end
|
adamc@171
|
1215
|
adamc@172
|
1216 datatype coverage =
|
adamc@172
|
1217 Wild
|
adamc@173
|
1218 | None
|
adamc@172
|
1219 | Datatype of coverage IM.map
|
adamc@175
|
1220 | Record of coverage SM.map list
|
adamc@172
|
1221
|
adamc@243
|
1222 fun c2s c =
|
adamc@243
|
1223 case c of
|
adamc@243
|
1224 Wild => "Wild"
|
adamc@243
|
1225 | None => "None"
|
adamc@243
|
1226 | Datatype _ => "Datatype"
|
adamc@243
|
1227 | Record _ => "Record"
|
adamc@243
|
1228
|
adamc@172
|
1229 fun exhaustive (env, denv, t, ps) =
|
adamc@172
|
1230 let
|
adamc@402
|
1231 fun depth (p, _) =
|
adamc@402
|
1232 case p of
|
adamc@402
|
1233 L'.PWild => 0
|
adamc@402
|
1234 | L'.PVar _ => 0
|
adamc@402
|
1235 | L'.PPrim _ => 0
|
adamc@402
|
1236 | L'.PCon (_, _, _, NONE) => 1
|
adamc@402
|
1237 | L'.PCon (_, _, _, SOME p) => 1 + depth p
|
adamc@402
|
1238 | L'.PRecord xps => foldl (fn ((_, p, _), n) => Int.max (depth p, n)) 0 xps
|
adamc@402
|
1239
|
adamc@402
|
1240 val depth = 1 + foldl (fn (p, n) => Int.max (depth p, n)) 0 ps
|
adamc@402
|
1241
|
adamc@172
|
1242 fun pcCoverage pc =
|
adamc@172
|
1243 case pc of
|
adamc@172
|
1244 L'.PConVar n => n
|
adamc@174
|
1245 | L'.PConProj (m1, ms, x) =>
|
adamc@174
|
1246 let
|
adamc@174
|
1247 val (str, sgn) = E.chaseMpath env (m1, ms)
|
adamc@174
|
1248 in
|
adamc@174
|
1249 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
|
adamc@174
|
1250 NONE => raise Fail "exhaustive: Can't project constructor"
|
adamc@191
|
1251 | SOME (_, n, _, _, _) => n
|
adamc@174
|
1252 end
|
adamc@172
|
1253
|
adamc@172
|
1254 fun coverage (p, _) =
|
adamc@172
|
1255 case p of
|
adamc@172
|
1256 L'.PWild => Wild
|
adamc@172
|
1257 | L'.PVar _ => Wild
|
adamc@173
|
1258 | L'.PPrim _ => None
|
adamc@191
|
1259 | L'.PCon (_, pc, _, NONE) => Datatype (IM.insert (IM.empty, pcCoverage pc, Wild))
|
adamc@191
|
1260 | L'.PCon (_, pc, _, SOME p) => Datatype (IM.insert (IM.empty, pcCoverage pc, coverage p))
|
adamc@182
|
1261 | L'.PRecord xps => Record [foldl (fn ((x, p, _), fmap) =>
|
adamc@182
|
1262 SM.insert (fmap, x, coverage p)) SM.empty xps]
|
adamc@176
|
1263
|
adamc@172
|
1264 fun merge (c1, c2) =
|
adamc@172
|
1265 case (c1, c2) of
|
adamc@173
|
1266 (None, _) => c2
|
adamc@173
|
1267 | (_, None) => c1
|
adamc@173
|
1268
|
adamc@173
|
1269 | (Wild, _) => Wild
|
adamc@172
|
1270 | (_, Wild) => Wild
|
adamc@172
|
1271
|
adamc@172
|
1272 | (Datatype cm1, Datatype cm2) => Datatype (IM.unionWith merge (cm1, cm2))
|
adamc@172
|
1273
|
adamc@175
|
1274 | (Record fm1, Record fm2) => Record (fm1 @ fm2)
|
adamc@175
|
1275
|
adamc@175
|
1276 | _ => None
|
adamc@175
|
1277
|
adamc@172
|
1278 fun combinedCoverage ps =
|
adamc@172
|
1279 case ps of
|
adamc@172
|
1280 [] => raise Fail "Empty pattern list for coverage checking"
|
adamc@172
|
1281 | [p] => coverage p
|
adamc@172
|
1282 | p :: ps => merge (coverage p, combinedCoverage ps)
|
adamc@172
|
1283
|
adamc@402
|
1284 fun enumerateCases depth t =
|
adamc@402
|
1285 if depth = 0 then
|
adamc@402
|
1286 [Wild]
|
adamc@402
|
1287 else
|
adamc@402
|
1288 let
|
adamc@402
|
1289 fun dtype cons =
|
adamc@402
|
1290 ListUtil.mapConcat (fn (_, n, to) =>
|
adamc@402
|
1291 case to of
|
adamc@402
|
1292 NONE => [Datatype (IM.insert (IM.empty, n, Wild))]
|
adamc@402
|
1293 | SOME t => map (fn c => Datatype (IM.insert (IM.empty, n, c)))
|
adamc@402
|
1294 (enumerateCases (depth-1) t)) cons
|
adamc@402
|
1295 in
|
adamc@402
|
1296 case #1 (#1 (hnormCon (env, denv) t)) of
|
adamc@402
|
1297 L'.CNamed n =>
|
adamc@402
|
1298 (let
|
adamc@402
|
1299 val dt = E.lookupDatatype env n
|
adamc@402
|
1300 val cons = E.constructors dt
|
adamc@175
|
1301 in
|
adamc@402
|
1302 dtype cons
|
adamc@402
|
1303 end handle E.UnboundNamed _ => [Wild])
|
adamc@402
|
1304 | L'.TRecord c =>
|
adamc@402
|
1305 (case #1 (#1 (hnormCon (env, denv) c)) of
|
adamc@402
|
1306 L'.CRecord (_, xts) =>
|
adamc@402
|
1307 let
|
adamc@402
|
1308 val xts = map (fn (x, t) => (#1 (hnormCon (env, denv) x), t)) xts
|
adamc@402
|
1309
|
adamc@402
|
1310 fun exponentiate fs =
|
adamc@402
|
1311 case fs of
|
adamc@402
|
1312 [] => [SM.empty]
|
adamc@402
|
1313 | ((L'.CName x, _), t) :: rest =>
|
adamc@402
|
1314 let
|
adamc@402
|
1315 val this = enumerateCases depth t
|
adamc@402
|
1316 val rest = exponentiate rest
|
adamc@402
|
1317 in
|
adamc@402
|
1318 ListUtil.mapConcat (fn fmap =>
|
adamc@402
|
1319 map (fn c => SM.insert (fmap, x, c)) this) rest
|
adamc@402
|
1320 end
|
adamc@402
|
1321 | _ => raise Fail "exponentiate: Not CName"
|
adamc@402
|
1322 in
|
adamc@402
|
1323 if List.exists (fn ((L'.CName _, _), _) => false
|
adamc@402
|
1324 | (c, _) => true) xts then
|
adamc@402
|
1325 [Wild]
|
adamc@402
|
1326 else
|
adamc@402
|
1327 map (fn ls => Record [ls]) (exponentiate xts)
|
adamc@402
|
1328 end
|
adamc@402
|
1329 | _ => [Wild])
|
adamc@402
|
1330 | _ => [Wild]
|
adamc@402
|
1331 end
|
adamc@175
|
1332
|
adamc@175
|
1333 fun coverageImp (c1, c2) =
|
adamc@243
|
1334 let
|
adamc@243
|
1335 val r =
|
adamc@243
|
1336 case (c1, c2) of
|
adamc@243
|
1337 (Wild, _) => true
|
adamc@243
|
1338
|
adamc@243
|
1339 | (Datatype cmap1, Datatype cmap2) =>
|
adamc@243
|
1340 List.all (fn (n, c2) =>
|
adamc@243
|
1341 case IM.find (cmap1, n) of
|
adamc@243
|
1342 NONE => false
|
adamc@243
|
1343 | SOME c1 => coverageImp (c1, c2)) (IM.listItemsi cmap2)
|
adamc@243
|
1344 | (Datatype cmap1, Wild) =>
|
adamc@243
|
1345 List.all (fn (n, c1) => coverageImp (c1, Wild)) (IM.listItemsi cmap1)
|
adamc@243
|
1346
|
adamc@243
|
1347 | (Record fmaps1, Record fmaps2) =>
|
adamc@243
|
1348 List.all (fn fmap2 =>
|
adamc@243
|
1349 List.exists (fn fmap1 =>
|
adamc@243
|
1350 List.all (fn (x, c2) =>
|
adamc@243
|
1351 case SM.find (fmap1, x) of
|
adamc@243
|
1352 NONE => true
|
adamc@243
|
1353 | SOME c1 => coverageImp (c1, c2))
|
adamc@243
|
1354 (SM.listItemsi fmap2))
|
adamc@243
|
1355 fmaps1) fmaps2
|
adamc@243
|
1356
|
adamc@243
|
1357 | (Record fmaps1, Wild) =>
|
adamc@243
|
1358 List.exists (fn fmap1 =>
|
adamc@243
|
1359 List.all (fn (x, c1) => coverageImp (c1, Wild))
|
adamc@243
|
1360 (SM.listItemsi fmap1)) fmaps1
|
adamc@243
|
1361
|
adamc@243
|
1362 | _ => false
|
adamc@243
|
1363 in
|
adamc@243
|
1364 (*TextIO.print ("coverageImp(" ^ c2s c1 ^ ", " ^ c2s c2 ^ ") = " ^ Bool.toString r ^ "\n");*)
|
adamc@243
|
1365 r
|
adamc@243
|
1366 end
|
adamc@175
|
1367
|
adamc@172
|
1368 fun isTotal (c, t) =
|
adamc@172
|
1369 case c of
|
adamc@173
|
1370 None => (false, [])
|
adamc@173
|
1371 | Wild => (true, [])
|
adamc@172
|
1372 | Datatype cm =>
|
adamc@172
|
1373 let
|
adamc@172
|
1374 val ((t, _), gs) = hnormCon (env, denv) t
|
adamc@174
|
1375
|
adamc@174
|
1376 fun dtype cons =
|
adamc@174
|
1377 foldl (fn ((_, n, to), (total, gs)) =>
|
adamc@174
|
1378 case IM.find (cm, n) of
|
adamc@174
|
1379 NONE => (false, gs)
|
adamc@174
|
1380 | SOME c' =>
|
adamc@174
|
1381 case to of
|
adamc@174
|
1382 NONE => (total, gs)
|
adamc@174
|
1383 | SOME t' =>
|
adamc@174
|
1384 let
|
adamc@174
|
1385 val (total, gs') = isTotal (c', t')
|
adamc@174
|
1386 in
|
adamc@174
|
1387 (total, gs' @ gs)
|
adamc@174
|
1388 end)
|
adamc@174
|
1389 (true, gs) cons
|
adamc@191
|
1390
|
adamc@191
|
1391 fun unapp t =
|
adamc@191
|
1392 case t of
|
adamc@191
|
1393 L'.CApp ((t, _), _) => unapp t
|
adamc@191
|
1394 | _ => t
|
adamc@172
|
1395 in
|
adamc@191
|
1396 case unapp t of
|
adamc@172
|
1397 L'.CNamed n =>
|
adamc@172
|
1398 let
|
adamc@172
|
1399 val dt = E.lookupDatatype env n
|
adamc@172
|
1400 val cons = E.constructors dt
|
adamc@172
|
1401 in
|
adamc@174
|
1402 dtype cons
|
adamc@174
|
1403 end
|
adamc@174
|
1404 | L'.CModProj (m1, ms, x) =>
|
adamc@174
|
1405 let
|
adamc@174
|
1406 val (str, sgn) = E.chaseMpath env (m1, ms)
|
adamc@174
|
1407 in
|
adamc@174
|
1408 case E.projectDatatype env {str = str, sgn = sgn, field = x} of
|
adamc@174
|
1409 NONE => raise Fail "isTotal: Can't project datatype"
|
adamc@191
|
1410 | SOME (_, cons) => dtype cons
|
adamc@172
|
1411 end
|
adamc@172
|
1412 | L'.CError => (true, gs)
|
adamc@341
|
1413 | c =>
|
adamc@341
|
1414 (prefaces "Not a datatype" [("c", p_con env (c, ErrorMsg.dummySpan))];
|
adamc@341
|
1415 raise Fail "isTotal: Not a datatype")
|
adamc@172
|
1416 end
|
adamc@402
|
1417 | Record _ => (List.all (fn c2 => coverageImp (c, c2)) (enumerateCases depth t), [])
|
adamc@172
|
1418 in
|
adamc@172
|
1419 isTotal (combinedCoverage ps, t)
|
adamc@172
|
1420 end
|
adamc@172
|
1421
|
adamc@217
|
1422 fun unmodCon env (c, loc) =
|
adamc@217
|
1423 case c of
|
adamc@217
|
1424 L'.CNamed n =>
|
adamc@217
|
1425 (case E.lookupCNamed env n of
|
adamc@217
|
1426 (_, _, SOME (c as (L'.CModProj _, _))) => unmodCon env c
|
adamc@217
|
1427 | _ => (c, loc))
|
adamc@217
|
1428 | L'.CModProj (m1, ms, x) =>
|
adamc@217
|
1429 let
|
adamc@217
|
1430 val (str, sgn) = E.chaseMpath env (m1, ms)
|
adamc@217
|
1431 in
|
adamc@217
|
1432 case E.projectCon env {str = str, sgn = sgn, field = x} of
|
adamc@217
|
1433 NONE => raise Fail "unmodCon: Can't projectCon"
|
adamc@217
|
1434 | SOME (_, SOME (c as (L'.CModProj _, _))) => unmodCon env c
|
adamc@217
|
1435 | _ => (c, loc)
|
adamc@217
|
1436 end
|
adamc@217
|
1437 | _ => (c, loc)
|
adamc@217
|
1438
|
adamc@467
|
1439 fun normClassKey envs c =
|
adamc@467
|
1440 let
|
adamc@467
|
1441 val c = ElabOps.hnormCon envs c
|
adamc@467
|
1442 in
|
adamc@467
|
1443 case #1 c of
|
adamc@467
|
1444 L'.CApp (c1, c2) =>
|
adamc@467
|
1445 let
|
adamc@467
|
1446 val c1 = normClassKey envs c1
|
adamc@467
|
1447 val c2 = normClassKey envs c2
|
adamc@467
|
1448 in
|
adamc@467
|
1449 (L'.CApp (c1, c2), #2 c)
|
adamc@467
|
1450 end
|
adamc@467
|
1451 | _ => c
|
adamc@467
|
1452 end
|
adamc@467
|
1453
|
adamc@467
|
1454 fun normClassConstraint env (c, loc) =
|
adamc@217
|
1455 case c of
|
adamc@217
|
1456 L'.CApp (f, x) =>
|
adamc@217
|
1457 let
|
adamc@467
|
1458 val f = unmodCon env f
|
adamc@467
|
1459 val x = normClassKey env x
|
adamc@217
|
1460 in
|
adamc@467
|
1461 (L'.CApp (f, x), loc)
|
adamc@217
|
1462 end
|
adamc@467
|
1463 | L'.CUnif (_, _, _, ref (SOME c)) => normClassConstraint env c
|
adamc@467
|
1464 | _ => (c, loc)
|
adamc@216
|
1465
|
adamc@447
|
1466
|
adamc@447
|
1467 val makeInstantiable =
|
adamc@447
|
1468 let
|
adamc@447
|
1469 fun kind k = k
|
adamc@447
|
1470 fun con c =
|
adamc@447
|
1471 case c of
|
adamc@447
|
1472 L'.CDisjoint (L'.LeaveAlone, c1, c2, c) => L'.CDisjoint (L'.Instantiate, c1, c2, c)
|
adamc@447
|
1473 | _ => c
|
adamc@447
|
1474 in
|
adamc@447
|
1475 U.Con.map {kind = kind, con = con}
|
adamc@447
|
1476 end
|
adamc@447
|
1477
|
adamc@92
|
1478 fun elabExp (env, denv) (eAll as (e, loc)) =
|
adamc@91
|
1479 let
|
adamc@280
|
1480 (*val () = eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)];*)
|
adamc@280
|
1481
|
adamc@280
|
1482 val r = case e of
|
adamc@91
|
1483 L.EAnnot (e, t) =>
|
adamc@91
|
1484 let
|
adamc@91
|
1485 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@91
|
1486 val (t', _, gs2) = elabCon (env, denv) t
|
adamc@91
|
1487 val gs3 = checkCon (env, denv) e' et t'
|
adamc@91
|
1488 in
|
adamc@228
|
1489 (e', t', gs1 @ enD gs2 @ enD gs3)
|
adamc@91
|
1490 end
|
adamc@34
|
1491
|
adamc@91
|
1492 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
|
adamc@403
|
1493 | L.EVar ([], s, infer) =>
|
adamc@91
|
1494 (case E.lookupE env s of
|
adamc@91
|
1495 E.NotBound =>
|
adamc@91
|
1496 (expError env (UnboundExp (loc, s));
|
adamc@91
|
1497 (eerror, cerror, []))
|
adamc@403
|
1498 | E.Rel (n, t) => elabHead (env, denv) infer (L'.ERel n, loc) t
|
adamc@403
|
1499 | E.Named (n, t) => elabHead (env, denv) infer (L'.ENamed n, loc) t)
|
adamc@403
|
1500 | L.EVar (m1 :: ms, s, infer) =>
|
adamc@91
|
1501 (case E.lookupStr env m1 of
|
adamc@91
|
1502 NONE => (expError env (UnboundStrInExp (loc, m1));
|
adamc@91
|
1503 (eerror, cerror, []))
|
adamc@91
|
1504 | SOME (n, sgn) =>
|
adamc@91
|
1505 let
|
adamc@91
|
1506 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@91
|
1507 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@91
|
1508 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@91
|
1509 (strerror, sgnerror))
|
adamc@91
|
1510 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@91
|
1511 ((L'.StrVar n, loc), sgn) ms
|
adamc@10
|
1512
|
adamc@91
|
1513 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
|
adamc@91
|
1514 NONE => (expError env (UnboundExp (loc, s));
|
adamc@91
|
1515 cerror)
|
adamc@91
|
1516 | SOME t => t
|
adamc@91
|
1517 in
|
adamc@403
|
1518 elabHead (env, denv) infer (L'.EModProj (n, ms, s), loc) t
|
adamc@91
|
1519 end)
|
adamc@86
|
1520
|
adamc@349
|
1521 | L.EWild =>
|
adamc@210
|
1522 let
|
adamc@349
|
1523 val r = ref NONE
|
adamc@349
|
1524 val c = cunif (loc, (L'.KType, loc))
|
adamc@210
|
1525 in
|
adamc@349
|
1526 ((L'.EUnif r, loc), c, [TypeClass (env, c, r, loc)])
|
adamc@210
|
1527 end
|
adamc@210
|
1528
|
adamc@91
|
1529 | L.EApp (e1, e2) =>
|
adamc@91
|
1530 let
|
adamc@91
|
1531 val (e1', t1, gs1) = elabExp (env, denv) e1
|
adamc@403
|
1532 val (e2', t2, gs2) = elabExp (env, denv) e2
|
adamc@71
|
1533
|
adamc@91
|
1534 val dom = cunif (loc, ktype)
|
adamc@91
|
1535 val ran = cunif (loc, ktype)
|
adamc@91
|
1536 val t = (L'.TFun (dom, ran), dummy)
|
adamc@91
|
1537
|
adamc@403
|
1538 val gs3 = checkCon (env, denv) e1' t1 t
|
adamc@403
|
1539 val gs4 = checkCon (env, denv) e2' t2 dom
|
adamc@403
|
1540
|
adamc@403
|
1541 val gs = gs1 @ gs2 @ enD gs3 @ enD gs4
|
adamc@91
|
1542 in
|
adamc@205
|
1543 ((L'.EApp (e1', e2'), loc), ran, gs)
|
adamc@91
|
1544 end
|
adamc@91
|
1545 | L.EAbs (x, to, e) =>
|
adamc@91
|
1546 let
|
adamc@91
|
1547 val (t', gs1) = case to of
|
adamc@91
|
1548 NONE => (cunif (loc, ktype), [])
|
adamc@91
|
1549 | SOME t =>
|
adamc@91
|
1550 let
|
adamc@91
|
1551 val (t', tk, gs) = elabCon (env, denv) t
|
adamc@91
|
1552 in
|
adamc@91
|
1553 checkKind env t' tk ktype;
|
adamc@91
|
1554 (t', gs)
|
adamc@91
|
1555 end
|
adamc@467
|
1556 val dom = normClassConstraint env t'
|
adamc@467
|
1557 val (e', et, gs2) = elabExp (E.pushERel env x dom, denv) e
|
adamc@91
|
1558 in
|
adamc@91
|
1559 ((L'.EAbs (x, t', et, e'), loc),
|
adamc@91
|
1560 (L'.TFun (t', et), loc),
|
adamc@467
|
1561 enD gs1 @ gs2)
|
adamc@91
|
1562 end
|
adamc@91
|
1563 | L.ECApp (e, c) =>
|
adamc@91
|
1564 let
|
adamc@91
|
1565 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@328
|
1566 val oldEt = et
|
adamc@403
|
1567 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@403
|
1568 val ((et', _), gs3) = hnormCon (env, denv) et
|
adamc@91
|
1569 in
|
adamc@91
|
1570 case et' of
|
adamc@91
|
1571 L'.CError => (eerror, cerror, [])
|
adamc@328
|
1572 | L'.TCFun (_, x, k, eb) =>
|
adamc@91
|
1573 let
|
adamc@91
|
1574 val () = checkKind env c' ck k
|
adamc@91
|
1575 val eb' = subConInCon (0, c') eb
|
adamc@339
|
1576 handle SynUnif => (expError env (Unif ("substitution", loc, eb));
|
adamc@91
|
1577 cerror)
|
adamc@91
|
1578 in
|
adamc@328
|
1579 (*prefaces "Elab ECApp" [("e", SourcePrint.p_exp eAll),
|
adamc@328
|
1580 ("et", p_con env oldEt),
|
adamc@328
|
1581 ("x", PD.string x),
|
adamc@328
|
1582 ("eb", p_con (E.pushCRel env x k) eb),
|
adamc@328
|
1583 ("c", p_con env c'),
|
adamc@328
|
1584 ("eb'", p_con env eb')];*)
|
adamc@403
|
1585 ((L'.ECApp (e', c'), loc), eb', gs1 @ enD gs2 @ enD gs3)
|
adamc@91
|
1586 end
|
adamc@91
|
1587
|
adamc@91
|
1588 | _ =>
|
adamc@91
|
1589 (expError env (WrongForm ("constructor function", e', et));
|
adamc@91
|
1590 (eerror, cerror, []))
|
adamc@91
|
1591 end
|
adamc@91
|
1592 | L.ECAbs (expl, x, k, e) =>
|
adamc@91
|
1593 let
|
adamc@91
|
1594 val expl' = elabExplicitness expl
|
adamc@91
|
1595 val k' = elabKind k
|
adamc@328
|
1596
|
adamc@328
|
1597 val env' = E.pushCRel env x k'
|
adamc@328
|
1598 val (e', et, gs) = elabExp (env', D.enter denv) e
|
adamc@91
|
1599 in
|
adamc@91
|
1600 ((L'.ECAbs (expl', x, k', e'), loc),
|
adamc@91
|
1601 (L'.TCFun (expl', x, k', et), loc),
|
adamc@91
|
1602 gs)
|
adamc@91
|
1603 end
|
adamc@91
|
1604
|
adamc@91
|
1605 | L.EDisjoint (c1, c2, e) =>
|
adamc@91
|
1606 let
|
adamc@91
|
1607 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@91
|
1608 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@91
|
1609
|
adamc@91
|
1610 val ku1 = kunif loc
|
adamc@91
|
1611 val ku2 = kunif loc
|
adamc@91
|
1612
|
adamc@91
|
1613 val (denv', gs3) = D.assert env denv (c1', c2')
|
adamc@91
|
1614 val (e', t, gs4) = elabExp (env, denv') e
|
adamc@91
|
1615 in
|
adamc@91
|
1616 checkKind env c1' k1 (L'.KRecord ku1, loc);
|
adamc@91
|
1617 checkKind env c2' k2 (L'.KRecord ku2, loc);
|
adamc@91
|
1618
|
adamc@345
|
1619 (e', (L'.CDisjoint (L'.LeaveAlone, c1', c2', t), loc), enD gs1 @ enD gs2 @ enD gs3 @ gs4)
|
adamc@91
|
1620 end
|
adamc@91
|
1621
|
adamc@91
|
1622 | L.ERecord xes =>
|
adamc@91
|
1623 let
|
adamc@91
|
1624 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
|
adamc@91
|
1625 let
|
adamc@91
|
1626 val (x', xk, gs1) = elabCon (env, denv) x
|
adamc@91
|
1627 val (e', et, gs2) = elabExp (env, denv) e
|
adamc@91
|
1628 in
|
adamc@91
|
1629 checkKind env x' xk kname;
|
adamc@228
|
1630 ((x', e', et), enD gs1 @ gs2 @ gs)
|
adamc@91
|
1631 end)
|
adamc@91
|
1632 [] xes
|
adamc@91
|
1633
|
adamc@91
|
1634 val k = (L'.KType, loc)
|
adamc@91
|
1635
|
adamc@91
|
1636 fun prove (xets, gs) =
|
adamc@91
|
1637 case xets of
|
adamc@91
|
1638 [] => gs
|
adamc@91
|
1639 | (x, _, t) :: rest =>
|
adamc@91
|
1640 let
|
adamc@91
|
1641 val xc = (x, t)
|
adamc@91
|
1642 val r1 = (L'.CRecord (k, [xc]), loc)
|
adamc@91
|
1643 val gs = foldl (fn ((x', _, t'), gs) =>
|
adamc@91
|
1644 let
|
adamc@91
|
1645 val xc' = (x', t')
|
adamc@91
|
1646 val r2 = (L'.CRecord (k, [xc']), loc)
|
adamc@91
|
1647 in
|
adamc@91
|
1648 D.prove env denv (r1, r2, loc) @ gs
|
adamc@91
|
1649 end)
|
adamc@91
|
1650 gs rest
|
adamc@91
|
1651 in
|
adamc@91
|
1652 prove (rest, gs)
|
adamc@91
|
1653 end
|
adamc@228
|
1654
|
adamc@228
|
1655 val gsD = List.mapPartial (fn Disjoint d => SOME d | _ => NONE) gs
|
adamc@228
|
1656 val gsO = List.filter (fn Disjoint _ => false | _ => true) gs
|
adamc@91
|
1657 in
|
adamc@255
|
1658 (*TextIO.print ("|gsO| = " ^ Int.toString (length gsO) ^ "\n");*)
|
adamc@91
|
1659 ((L'.ERecord xes', loc),
|
adamc@91
|
1660 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
|
adamc@228
|
1661 enD (prove (xes', gsD)) @ gsO)
|
adamc@91
|
1662 end
|
adamc@91
|
1663
|
adamc@91
|
1664 | L.EField (e, c) =>
|
adamc@91
|
1665 let
|
adamc@91
|
1666 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@91
|
1667 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@91
|
1668
|
adamc@91
|
1669 val ft = cunif (loc, ktype)
|
adamc@91
|
1670 val rest = cunif (loc, ktype_record)
|
adamc@91
|
1671 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
|
adamc@91
|
1672
|
adamc@91
|
1673 val gs3 =
|
adamc@91
|
1674 checkCon (env, denv) e' et
|
adamc@91
|
1675 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
|
adamc@91
|
1676 val gs4 = D.prove env denv (first, rest, loc)
|
adamc@91
|
1677 in
|
adamc@228
|
1678 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ enD gs2 @ enD gs3 @ enD gs4)
|
adamc@91
|
1679 end
|
adamc@91
|
1680
|
adamc@445
|
1681 | L.EConcat (e1, e2) =>
|
adamc@339
|
1682 let
|
adamc@339
|
1683 val (e1', e1t, gs1) = elabExp (env, denv) e1
|
adamc@445
|
1684 val (e2', e2t, gs2) = elabExp (env, denv) e2
|
adamc@445
|
1685
|
adamc@445
|
1686 val r1 = cunif (loc, ktype_record)
|
adamc@445
|
1687 val r2 = cunif (loc, ktype_record)
|
adamc@445
|
1688
|
adamc@445
|
1689 val gs3 = checkCon (env, denv) e1' e1t (L'.TRecord r1, loc)
|
adamc@445
|
1690 val gs4 = checkCon (env, denv) e2' e2t (L'.TRecord r2, loc)
|
adamc@445
|
1691 val gs5 = D.prove env denv (r1, r2, loc)
|
adamc@339
|
1692 in
|
adamc@445
|
1693 ((L'.EConcat (e1', r1, e2', r2), loc),
|
adamc@445
|
1694 (L'.TRecord ((L'.CConcat (r1, r2), loc)), loc),
|
adamc@445
|
1695 gs1 @ gs2 @ enD gs3 @ enD gs4 @ enD gs5)
|
adamc@339
|
1696 end
|
adamc@149
|
1697 | L.ECut (e, c) =>
|
adamc@149
|
1698 let
|
adamc@149
|
1699 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@149
|
1700 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@149
|
1701
|
adamc@149
|
1702 val ft = cunif (loc, ktype)
|
adamc@149
|
1703 val rest = cunif (loc, ktype_record)
|
adamc@149
|
1704 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
|
adamc@149
|
1705
|
adamc@149
|
1706 val gs3 =
|
adamc@149
|
1707 checkCon (env, denv) e' et
|
adamc@149
|
1708 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
|
adamc@149
|
1709 val gs4 = D.prove env denv (first, rest, loc)
|
adamc@149
|
1710 in
|
adamc@228
|
1711 ((L'.ECut (e', c', {field = ft, rest = rest}), loc), (L'.TRecord rest, loc),
|
adamc@228
|
1712 gs1 @ enD gs2 @ enD gs3 @ enD gs4)
|
adamc@149
|
1713 end
|
adamc@493
|
1714 | L.ECutMulti (e, c) =>
|
adamc@493
|
1715 let
|
adamc@493
|
1716 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@493
|
1717 val (c', ck, gs2) = elabCon (env, denv) c
|
adamc@493
|
1718
|
adamc@493
|
1719 val rest = cunif (loc, ktype_record)
|
adamc@493
|
1720
|
adamc@493
|
1721 val gs3 =
|
adamc@493
|
1722 checkCon (env, denv) e' et
|
adamc@493
|
1723 (L'.TRecord (L'.CConcat (c', rest), loc), loc)
|
adamc@493
|
1724 val gs4 = D.prove env denv (c', rest, loc)
|
adamc@493
|
1725 in
|
adamc@493
|
1726 ((L'.ECutMulti (e', c', {rest = rest}), loc), (L'.TRecord rest, loc),
|
adamc@493
|
1727 gs1 @ enD gs2 @ enD gs3 @ enD gs4)
|
adamc@493
|
1728 end
|
adamc@149
|
1729
|
adamc@91
|
1730 | L.EFold =>
|
adamc@91
|
1731 let
|
adamc@91
|
1732 val dom = kunif loc
|
adamc@91
|
1733 in
|
adamc@91
|
1734 ((L'.EFold dom, loc), foldType (dom, loc), [])
|
adamc@91
|
1735 end
|
adamc@170
|
1736
|
adamc@171
|
1737 | L.ECase (e, pes) =>
|
adamc@171
|
1738 let
|
adamc@171
|
1739 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@171
|
1740 val result = cunif (loc, (L'.KType, loc))
|
adamc@171
|
1741 val (pes', gs) = ListUtil.foldlMap
|
adamc@171
|
1742 (fn ((p, e), gs) =>
|
adamc@171
|
1743 let
|
adamc@174
|
1744 val ((p', pt), (env, _)) = elabPat (p, (env, denv, SS.empty))
|
adamc@171
|
1745
|
adamc@171
|
1746 val gs1 = checkPatCon (env, denv) p' pt et
|
adamc@171
|
1747 val (e', et, gs2) = elabExp (env, denv) e
|
adamc@171
|
1748 val gs3 = checkCon (env, denv) e' et result
|
adamc@171
|
1749 in
|
adamc@228
|
1750 ((p', e'), enD gs1 @ gs2 @ enD gs3 @ gs)
|
adamc@171
|
1751 end)
|
adamc@171
|
1752 gs1 pes
|
adamc@172
|
1753
|
adamc@172
|
1754 val (total, gs') = exhaustive (env, denv, et, map #1 pes')
|
adamc@171
|
1755 in
|
adamc@172
|
1756 if total then
|
adamc@172
|
1757 ()
|
adamc@172
|
1758 else
|
adamc@172
|
1759 expError env (Inexhaustive loc);
|
adamc@172
|
1760
|
adamc@228
|
1761 ((L'.ECase (e', pes', {disc = et, result = result}), loc), result, enD gs' @ gs)
|
adamc@171
|
1762 end
|
adamc@447
|
1763
|
adamc@447
|
1764 | L.ELet (eds, e) =>
|
adamc@447
|
1765 let
|
adamc@447
|
1766 val (eds, (env, gs1)) = ListUtil.foldlMap (elabEdecl denv) (env, []) eds
|
adamc@447
|
1767 val (e, t, gs2) = elabExp (env, denv) e
|
adamc@447
|
1768 in
|
adamc@447
|
1769 ((L'.ELet (eds, e), loc), t, gs1 @ gs2)
|
adamc@447
|
1770 end
|
adamc@280
|
1771 in
|
adamc@513
|
1772 (*prefaces "elabExp" [("e", SourcePrint.p_exp eAll),
|
adamc@513
|
1773 ("t", PD.string (LargeInt.toString (Time.toMilliseconds (Time.- (Time.now (), befor)))))];*)
|
adamc@280
|
1774 r
|
adamc@91
|
1775 end
|
adamc@31
|
1776
|
adamc@447
|
1777 and elabEdecl denv (dAll as (d, loc), (env, gs : constraint list)) =
|
adamc@447
|
1778 let
|
adamc@447
|
1779 val r =
|
adamc@447
|
1780 case d of
|
adamc@447
|
1781 L.EDVal (x, co, e) =>
|
adamc@447
|
1782 let
|
adamc@447
|
1783 val (c', _, gs1) = case co of
|
adamc@447
|
1784 NONE => (cunif (loc, ktype), ktype, [])
|
adamc@447
|
1785 | SOME c => elabCon (env, denv) c
|
adamc@447
|
1786
|
adamc@447
|
1787 val (e', et, gs2) = elabExp (env, denv) e
|
adamc@447
|
1788 val gs3 = checkCon (env, denv) e' et c'
|
adamc@467
|
1789 val c' = normClassConstraint env c'
|
adamc@447
|
1790 val env' = E.pushERel env x c'
|
adamc@447
|
1791 val c' = makeInstantiable c'
|
adamc@447
|
1792 in
|
adamc@467
|
1793 ((L'.EDVal (x, c', e'), loc), (env', enD gs1 @ gs2 @ enD gs3 @ gs))
|
adamc@447
|
1794 end
|
adamc@447
|
1795 | L.EDValRec vis =>
|
adamc@447
|
1796 let
|
adamc@447
|
1797 fun allowable (e, _) =
|
adamc@447
|
1798 case e of
|
adamc@447
|
1799 L.EAbs _ => true
|
adamc@447
|
1800 | L.ECAbs (_, _, _, e) => allowable e
|
adamc@447
|
1801 | L.EDisjoint (_, _, e) => allowable e
|
adamc@447
|
1802 | _ => false
|
adamc@447
|
1803
|
adamc@447
|
1804 val (vis, gs) = ListUtil.foldlMap
|
adamc@447
|
1805 (fn ((x, co, e), gs) =>
|
adamc@447
|
1806 let
|
adamc@447
|
1807 val (c', _, gs1) = case co of
|
adamc@447
|
1808 NONE => (cunif (loc, ktype), ktype, [])
|
adamc@447
|
1809 | SOME c => elabCon (env, denv) c
|
adamc@447
|
1810 in
|
adamc@447
|
1811 ((x, c', e), enD gs1 @ gs)
|
adamc@447
|
1812 end) gs vis
|
adamc@447
|
1813
|
adamc@447
|
1814 val env = foldl (fn ((x, c', _), env) => E.pushERel env x c') env vis
|
adamc@447
|
1815
|
adamc@447
|
1816 val (vis, gs) = ListUtil.foldlMap (fn ((x, c', e), gs) =>
|
adamc@447
|
1817 let
|
adamc@447
|
1818 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@447
|
1819
|
adamc@447
|
1820 val gs2 = checkCon (env, denv) e' et c'
|
adamc@447
|
1821
|
adamc@447
|
1822 val c' = makeInstantiable c'
|
adamc@447
|
1823 in
|
adamc@447
|
1824 if allowable e then
|
adamc@447
|
1825 ()
|
adamc@447
|
1826 else
|
adamc@447
|
1827 expError env (IllegalRec (x, e'));
|
adamc@447
|
1828 ((x, c', e'), gs1 @ enD gs2 @ gs)
|
adamc@447
|
1829 end) gs vis
|
adamc@447
|
1830 in
|
adamc@447
|
1831 ((L'.EDValRec vis, loc), (env, gs))
|
adamc@447
|
1832 end
|
adamc@447
|
1833 in
|
adamc@447
|
1834 r
|
adamc@447
|
1835 end
|
adamc@447
|
1836
|
adamc@42
|
1837 val hnormSgn = E.hnormSgn
|
adamc@31
|
1838
|
adamc@210
|
1839 fun tableOf () = (L'.CModProj (!basis_r, [], "sql_table"), ErrorMsg.dummySpan)
|
adamc@338
|
1840 fun sequenceOf () = (L'.CModProj (!basis_r, [], "sql_sequence"), ErrorMsg.dummySpan)
|
adamc@459
|
1841 fun cookieOf () = (L'.CModProj (!basis_r, [], "http_cookie"), ErrorMsg.dummySpan)
|
adamc@205
|
1842
|
adamc@469
|
1843 fun dopenConstraints (loc, env, denv) {str, strs} =
|
adamc@469
|
1844 case E.lookupStr env str of
|
adamc@469
|
1845 NONE => (strError env (UnboundStr (loc, str));
|
adamc@469
|
1846 denv)
|
adamc@469
|
1847 | SOME (n, sgn) =>
|
adamc@469
|
1848 let
|
adamc@469
|
1849 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@469
|
1850 case E.projectStr env {str = str, sgn = sgn, field = m} of
|
adamc@469
|
1851 NONE => (strError env (UnboundStr (loc, m));
|
adamc@469
|
1852 (strerror, sgnerror))
|
adamc@469
|
1853 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@469
|
1854 ((L'.StrVar n, loc), sgn) strs
|
adamc@469
|
1855
|
adamc@469
|
1856 val cso = E.projectConstraints env {sgn = sgn, str = st}
|
adamc@469
|
1857
|
adamc@469
|
1858 val denv = case cso of
|
adamc@469
|
1859 NONE => (strError env (UnboundStr (loc, str));
|
adamc@469
|
1860 denv)
|
adamc@469
|
1861 | SOME cs => foldl (fn ((c1, c2), denv) =>
|
adamc@469
|
1862 let
|
adamc@469
|
1863 val (denv, gs) = D.assert env denv (c1, c2)
|
adamc@469
|
1864 in
|
adamc@469
|
1865 case gs of
|
adamc@469
|
1866 [] => ()
|
adamc@469
|
1867 | _ => raise Fail "dopenConstraints: Sub-constraints remain";
|
adamc@469
|
1868
|
adamc@469
|
1869 denv
|
adamc@469
|
1870 end) denv cs
|
adamc@469
|
1871 in
|
adamc@469
|
1872 denv
|
adamc@469
|
1873 end
|
adamc@469
|
1874
|
adamc@88
|
1875 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
|
adamc@76
|
1876 case sgi of
|
adamc@76
|
1877 L.SgiConAbs (x, k) =>
|
adamc@76
|
1878 let
|
adamc@76
|
1879 val k' = elabKind k
|
adamc@31
|
1880
|
adamc@76
|
1881 val (env', n) = E.pushCNamed env x k' NONE
|
adamc@76
|
1882 in
|
adamc@88
|
1883 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
|
adamc@76
|
1884 end
|
adamc@31
|
1885
|
adamc@76
|
1886 | L.SgiCon (x, ko, c) =>
|
adamc@76
|
1887 let
|
adamc@76
|
1888 val k' = case ko of
|
adamc@76
|
1889 NONE => kunif loc
|
adamc@76
|
1890 | SOME k => elabKind k
|
adamc@31
|
1891
|
adamc@83
|
1892 val (c', ck, gs') = elabCon (env, denv) c
|
adamc@76
|
1893 val (env', n) = E.pushCNamed env x k' (SOME c')
|
adamc@76
|
1894 in
|
adamc@76
|
1895 checkKind env c' ck k';
|
adamc@31
|
1896
|
adamc@88
|
1897 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
1898 end
|
adamc@31
|
1899
|
adamc@191
|
1900 | L.SgiDatatype (x, xs, xcs) =>
|
adamc@157
|
1901 let
|
adamc@157
|
1902 val k = (L'.KType, loc)
|
adamc@191
|
1903 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
|
adamc@191
|
1904 val (env, n) = E.pushCNamed env x k' NONE
|
adamc@157
|
1905 val t = (L'.CNamed n, loc)
|
adamc@194
|
1906 val nxs = length xs - 1
|
adamc@194
|
1907 val t = ListUtil.foldli (fn (i, _, t) => (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
|
adamc@157
|
1908
|
adamc@284
|
1909 val (env', denv') = foldl (fn (x, (env', denv')) =>
|
adamc@284
|
1910 (E.pushCRel env' x k,
|
adamc@284
|
1911 D.enter denv')) (env, denv) xs
|
adamc@284
|
1912
|
adamc@157
|
1913 val (xcs, (used, env, gs)) =
|
adamc@157
|
1914 ListUtil.foldlMap
|
adamc@157
|
1915 (fn ((x, to), (used, env, gs)) =>
|
adamc@157
|
1916 let
|
adamc@157
|
1917 val (to, t, gs') = case to of
|
adamc@157
|
1918 NONE => (NONE, t, gs)
|
adamc@157
|
1919 | SOME t' =>
|
adamc@157
|
1920 let
|
adamc@284
|
1921 val (t', tk, gs') = elabCon (env', denv') t'
|
adamc@157
|
1922 in
|
adamc@284
|
1923 checkKind env' t' tk k;
|
adamc@157
|
1924 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
|
adamc@157
|
1925 end
|
adamc@191
|
1926 val t = foldl (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc)) t xs
|
adamc@157
|
1927
|
adamc@157
|
1928 val (env, n') = E.pushENamed env x t
|
adamc@157
|
1929 in
|
adamc@157
|
1930 if SS.member (used, x) then
|
adamc@157
|
1931 strError env (DuplicateConstructor (x, loc))
|
adamc@157
|
1932 else
|
adamc@157
|
1933 ();
|
adamc@157
|
1934 ((x, n', to), (SS.add (used, x), env, gs'))
|
adamc@157
|
1935 end)
|
adamc@157
|
1936 (SS.empty, env, []) xcs
|
adamc@191
|
1937
|
adamc@191
|
1938 val env = E.pushDatatype env n xs xcs
|
adamc@157
|
1939 in
|
adamc@191
|
1940 ([(L'.SgiDatatype (x, n, xs, xcs), loc)], (env, denv, gs))
|
adamc@157
|
1941 end
|
adamc@156
|
1942
|
adamc@158
|
1943 | L.SgiDatatypeImp (_, [], _) => raise Fail "Empty SgiDatatypeImp"
|
adamc@158
|
1944
|
adamc@158
|
1945 | L.SgiDatatypeImp (x, m1 :: ms, s) =>
|
adamc@158
|
1946 (case E.lookupStr env m1 of
|
adamc@158
|
1947 NONE => (strError env (UnboundStr (loc, m1));
|
adamc@158
|
1948 ([], (env, denv, gs)))
|
adamc@158
|
1949 | SOME (n, sgn) =>
|
adamc@158
|
1950 let
|
adamc@158
|
1951 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@158
|
1952 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@158
|
1953 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@158
|
1954 (strerror, sgnerror))
|
adamc@158
|
1955 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@158
|
1956 ((L'.StrVar n, loc), sgn) ms
|
adamc@158
|
1957 in
|
adamc@161
|
1958 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
|
adamc@161
|
1959 ((L'.CModProj (n, ms, s), _), gs) =>
|
adamc@161
|
1960 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
|
adamc@161
|
1961 NONE => (conError env (UnboundDatatype (loc, s));
|
adamc@161
|
1962 ([], (env, denv, gs)))
|
adamc@191
|
1963 | SOME (xs, xncs) =>
|
adamc@161
|
1964 let
|
adamc@161
|
1965 val k = (L'.KType, loc)
|
adamc@191
|
1966 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
|
adamc@191
|
1967
|
adamc@161
|
1968 val t = (L'.CModProj (n, ms, s), loc)
|
adamc@191
|
1969 val (env, n') = E.pushCNamed env x k' (SOME t)
|
adamc@191
|
1970 val env = E.pushDatatype env n' xs xncs
|
adamc@158
|
1971
|
adamc@161
|
1972 val t = (L'.CNamed n', loc)
|
adamc@161
|
1973 val env = foldl (fn ((x, n, to), env) =>
|
adamc@161
|
1974 let
|
adamc@161
|
1975 val t = case to of
|
adamc@161
|
1976 NONE => t
|
adamc@161
|
1977 | SOME t' => (L'.TFun (t', t), loc)
|
adamc@191
|
1978
|
adamc@191
|
1979 val t = foldr (fn (x, t) =>
|
adamc@191
|
1980 (L'.TCFun (L'.Implicit, x, k, t), loc))
|
adamc@191
|
1981 t xs
|
adamc@161
|
1982 in
|
adamc@161
|
1983 E.pushENamedAs env x n t
|
adamc@161
|
1984 end) env xncs
|
adamc@161
|
1985 in
|
adamc@191
|
1986 ([(L'.SgiDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, gs))
|
adamc@161
|
1987 end)
|
adamc@161
|
1988 | _ => (strError env (NotDatatype loc);
|
adamc@161
|
1989 ([], (env, denv, [])))
|
adamc@158
|
1990 end)
|
adamc@156
|
1991
|
adamc@76
|
1992 | L.SgiVal (x, c) =>
|
adamc@76
|
1993 let
|
adamc@83
|
1994 val (c', ck, gs') = elabCon (env, denv) c
|
adamc@31
|
1995
|
adamc@76
|
1996 val (env', n) = E.pushENamed env x c'
|
adamc@467
|
1997 val c' = normClassConstraint env c'
|
adamc@76
|
1998 in
|
adamc@76
|
1999 (unifyKinds ck ktype
|
adamc@76
|
2000 handle KUnify ue => strError env (NotType (ck, ue)));
|
adamc@31
|
2001
|
adamc@467
|
2002 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
2003 end
|
adamc@31
|
2004
|
adamc@76
|
2005 | L.SgiStr (x, sgn) =>
|
adamc@76
|
2006 let
|
adamc@83
|
2007 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
2008 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@76
|
2009 in
|
adamc@88
|
2010 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
2011 end
|
adamc@31
|
2012
|
adamc@76
|
2013 | L.SgiSgn (x, sgn) =>
|
adamc@76
|
2014 let
|
adamc@83
|
2015 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
2016 val (env', n) = E.pushSgnNamed env x sgn'
|
adamc@76
|
2017 in
|
adamc@88
|
2018 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
|
adamc@76
|
2019 end
|
adamc@31
|
2020
|
adamc@76
|
2021 | L.SgiInclude sgn =>
|
adamc@76
|
2022 let
|
adamc@83
|
2023 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@76
|
2024 in
|
adamc@76
|
2025 case #1 (hnormSgn env sgn') of
|
adamc@76
|
2026 L'.SgnConst sgis =>
|
adamc@88
|
2027 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
|
adamc@76
|
2028 | _ => (sgnError env (NotIncludable sgn');
|
adamc@88
|
2029 ([], (env, denv, [])))
|
adamc@88
|
2030 end
|
adamc@88
|
2031
|
adamc@88
|
2032 | L.SgiConstraint (c1, c2) =>
|
adamc@88
|
2033 let
|
adamc@88
|
2034 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@88
|
2035 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@88
|
2036
|
adamc@90
|
2037 val (denv, gs3) = D.assert env denv (c1', c2')
|
adamc@88
|
2038 in
|
adamc@88
|
2039 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
|
adamc@88
|
2040 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
|
adamc@88
|
2041
|
adamc@90
|
2042 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2 @ gs3))
|
adamc@76
|
2043 end
|
adamc@31
|
2044
|
adamc@211
|
2045 | L.SgiClassAbs x =>
|
adamc@211
|
2046 let
|
adamc@211
|
2047 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
|
adamc@211
|
2048 val (env, n) = E.pushCNamed env x k NONE
|
adamc@211
|
2049 val env = E.pushClass env n
|
adamc@211
|
2050 in
|
adamc@211
|
2051 ([(L'.SgiClassAbs (x, n), loc)], (env, denv, []))
|
adamc@211
|
2052 end
|
adamc@211
|
2053
|
adamc@211
|
2054 | L.SgiClass (x, c) =>
|
adamc@211
|
2055 let
|
adamc@211
|
2056 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
|
adamc@211
|
2057 val (c', ck, gs) = elabCon (env, denv) c
|
adamc@211
|
2058 val (env, n) = E.pushCNamed env x k (SOME c')
|
adamc@211
|
2059 val env = E.pushClass env n
|
adamc@211
|
2060 in
|
adamc@211
|
2061 checkKind env c' ck k;
|
adamc@211
|
2062 ([(L'.SgiClass (x, n, c'), loc)], (env, denv, []))
|
adamc@211
|
2063 end
|
adamc@211
|
2064
|
adamc@83
|
2065 and elabSgn (env, denv) (sgn, loc) =
|
adamc@31
|
2066 case sgn of
|
adamc@31
|
2067 L.SgnConst sgis =>
|
adamc@31
|
2068 let
|
adamc@88
|
2069 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
|
adamc@62
|
2070
|
adamc@62
|
2071 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
|
adamc@62
|
2072 case sgi of
|
adamc@62
|
2073 L'.SgiConAbs (x, _, _) =>
|
adamc@62
|
2074 (if SS.member (cons, x) then
|
adamc@62
|
2075 sgnError env (DuplicateCon (loc, x))
|
adamc@62
|
2076 else
|
adamc@62
|
2077 ();
|
adamc@62
|
2078 (SS.add (cons, x), vals, sgns, strs))
|
adamc@62
|
2079 | L'.SgiCon (x, _, _, _) =>
|
adamc@62
|
2080 (if SS.member (cons, x) then
|
adamc@62
|
2081 sgnError env (DuplicateCon (loc, x))
|
adamc@62
|
2082 else
|
adamc@62
|
2083 ();
|
adamc@62
|
2084 (SS.add (cons, x), vals, sgns, strs))
|
adamc@191
|
2085 | L'.SgiDatatype (x, _, _, xncs) =>
|
adamc@156
|
2086 let
|
adamc@156
|
2087 val vals = foldl (fn ((x, _, _), vals) =>
|
adamc@156
|
2088 (if SS.member (vals, x) then
|
adamc@156
|
2089 sgnError env (DuplicateVal (loc, x))
|
adamc@156
|
2090 else
|
adamc@156
|
2091 ();
|
adamc@156
|
2092 SS.add (vals, x)))
|
adamc@156
|
2093 vals xncs
|
adamc@156
|
2094 in
|
adamc@156
|
2095 if SS.member (cons, x) then
|
adamc@156
|
2096 sgnError env (DuplicateCon (loc, x))
|
adamc@156
|
2097 else
|
adamc@156
|
2098 ();
|
adamc@156
|
2099 (SS.add (cons, x), vals, sgns, strs)
|
adamc@156
|
2100 end
|
adamc@191
|
2101 | L'.SgiDatatypeImp (x, _, _, _, _, _, _) =>
|
adamc@156
|
2102 (if SS.member (cons, x) then
|
adamc@156
|
2103 sgnError env (DuplicateCon (loc, x))
|
adamc@156
|
2104 else
|
adamc@156
|
2105 ();
|
adamc@156
|
2106 (SS.add (cons, x), vals, sgns, strs))
|
adamc@62
|
2107 | L'.SgiVal (x, _, _) =>
|
adamc@62
|
2108 (if SS.member (vals, x) then
|
adamc@62
|
2109 sgnError env (DuplicateVal (loc, x))
|
adamc@62
|
2110 else
|
adamc@62
|
2111 ();
|
adamc@62
|
2112 (cons, SS.add (vals, x), sgns, strs))
|
adamc@62
|
2113 | L'.SgiSgn (x, _, _) =>
|
adamc@62
|
2114 (if SS.member (sgns, x) then
|
adamc@62
|
2115 sgnError env (DuplicateSgn (loc, x))
|
adamc@62
|
2116 else
|
adamc@62
|
2117 ();
|
adamc@62
|
2118 (cons, vals, SS.add (sgns, x), strs))
|
adamc@62
|
2119 | L'.SgiStr (x, _, _) =>
|
adamc@62
|
2120 (if SS.member (strs, x) then
|
adamc@62
|
2121 sgnError env (DuplicateStr (loc, x))
|
adamc@62
|
2122 else
|
adamc@62
|
2123 ();
|
adamc@88
|
2124 (cons, vals, sgns, SS.add (strs, x)))
|
adamc@203
|
2125 | L'.SgiConstraint _ => (cons, vals, sgns, strs)
|
adamc@211
|
2126 | L'.SgiClassAbs (x, _) =>
|
adamc@211
|
2127 (if SS.member (cons, x) then
|
adamc@211
|
2128 sgnError env (DuplicateCon (loc, x))
|
adamc@211
|
2129 else
|
adamc@211
|
2130 ();
|
adamc@211
|
2131 (SS.add (cons, x), vals, sgns, strs))
|
adamc@211
|
2132 | L'.SgiClass (x, _, _) =>
|
adamc@211
|
2133 (if SS.member (cons, x) then
|
adamc@211
|
2134 sgnError env (DuplicateCon (loc, x))
|
adamc@211
|
2135 else
|
adamc@211
|
2136 ();
|
adamc@460
|
2137 (SS.add (cons, x), vals, sgns, strs)))
|
adamc@62
|
2138 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
|
adamc@31
|
2139 in
|
adamc@83
|
2140 ((L'.SgnConst sgis', loc), gs)
|
adamc@31
|
2141 end
|
adamc@31
|
2142 | L.SgnVar x =>
|
adamc@31
|
2143 (case E.lookupSgn env x of
|
adamc@31
|
2144 NONE =>
|
adamc@31
|
2145 (sgnError env (UnboundSgn (loc, x));
|
adamc@83
|
2146 ((L'.SgnError, loc), []))
|
adamc@83
|
2147 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
|
adamc@41
|
2148 | L.SgnFun (m, dom, ran) =>
|
adamc@41
|
2149 let
|
adamc@83
|
2150 val (dom', gs1) = elabSgn (env, denv) dom
|
adamc@41
|
2151 val (env', n) = E.pushStrNamed env m dom'
|
adamc@469
|
2152 val denv' = dopenConstraints (loc, env', denv) {str = m, strs = []}
|
adamc@469
|
2153 val (ran', gs2) = elabSgn (env', denv') ran
|
adamc@41
|
2154 in
|
adamc@83
|
2155 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
|
adamc@41
|
2156 end
|
adamc@42
|
2157 | L.SgnWhere (sgn, x, c) =>
|
adamc@42
|
2158 let
|
adamc@83
|
2159 val (sgn', ds1) = elabSgn (env, denv) sgn
|
adamc@83
|
2160 val (c', ck, ds2) = elabCon (env, denv) c
|
adamc@42
|
2161 in
|
adamc@42
|
2162 case #1 (hnormSgn env sgn') of
|
adamc@83
|
2163 L'.SgnError => (sgnerror, [])
|
adamc@42
|
2164 | L'.SgnConst sgis =>
|
adamc@75
|
2165 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
|
adamc@75
|
2166 x' = x andalso
|
adamc@75
|
2167 (unifyKinds k ck
|
adamc@75
|
2168 handle KUnify x => sgnError env (WhereWrongKind x);
|
adamc@42
|
2169 true)
|
adamc@42
|
2170 | _ => false) sgis then
|
adamc@83
|
2171 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
|
adamc@42
|
2172 else
|
adamc@42
|
2173 (sgnError env (UnWhereable (sgn', x));
|
adamc@83
|
2174 (sgnerror, []))
|
adamc@42
|
2175 | _ => (sgnError env (UnWhereable (sgn', x));
|
adamc@83
|
2176 (sgnerror, []))
|
adamc@42
|
2177 end
|
adamc@59
|
2178 | L.SgnProj (m, ms, x) =>
|
adamc@59
|
2179 (case E.lookupStr env m of
|
adamc@59
|
2180 NONE => (strError env (UnboundStr (loc, m));
|
adamc@83
|
2181 (sgnerror, []))
|
adamc@59
|
2182 | SOME (n, sgn) =>
|
adamc@59
|
2183 let
|
adamc@59
|
2184 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@59
|
2185 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@59
|
2186 NONE => (strError env (UnboundStr (loc, m));
|
adamc@59
|
2187 (strerror, sgnerror))
|
adamc@59
|
2188 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@59
|
2189 ((L'.StrVar n, loc), sgn) ms
|
adamc@59
|
2190 in
|
adamc@59
|
2191 case E.projectSgn env {sgn = sgn, str = str, field = x} of
|
adamc@59
|
2192 NONE => (sgnError env (UnboundSgn (loc, x));
|
adamc@83
|
2193 (sgnerror, []))
|
adamc@83
|
2194 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
|
adamc@59
|
2195 end)
|
adamc@59
|
2196
|
adamc@31
|
2197
|
adamc@66
|
2198 fun selfify env {str, strs, sgn} =
|
adamc@66
|
2199 case #1 (hnormSgn env sgn) of
|
adamc@66
|
2200 L'.SgnError => sgn
|
adamc@66
|
2201 | L'.SgnVar _ => sgn
|
adamc@66
|
2202
|
adamc@66
|
2203 | L'.SgnConst sgis =>
|
adamc@66
|
2204 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
|
adamc@66
|
2205 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
|
adamc@191
|
2206 | (L'.SgiDatatype (x, n, xs, xncs), loc) =>
|
adamc@191
|
2207 (L'.SgiDatatypeImp (x, n, str, strs, x, xs, xncs), loc)
|
adamc@218
|
2208 | (L'.SgiClassAbs (x, n), loc) =>
|
adamc@218
|
2209 (L'.SgiClass (x, n, (L'.CModProj (str, strs, x), loc)), loc)
|
adamc@66
|
2210 | (L'.SgiStr (x, n, sgn), loc) =>
|
adamc@66
|
2211 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
|
adamc@66
|
2212 | x => x) sgis), #2 sgn)
|
adamc@66
|
2213 | L'.SgnFun _ => sgn
|
adamc@66
|
2214 | L'.SgnWhere _ => sgn
|
adamc@66
|
2215 | L'.SgnProj (m, ms, x) =>
|
adamc@66
|
2216 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
|
adamc@66
|
2217 (L'.StrVar m, #2 sgn) ms,
|
adamc@66
|
2218 sgn = #2 (E.lookupStrNamed env m),
|
adamc@66
|
2219 field = x} of
|
adamc@66
|
2220 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
|
adamc@66
|
2221 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
|
adamc@66
|
2222
|
adamc@66
|
2223 fun selfifyAt env {str, sgn} =
|
adamc@66
|
2224 let
|
adamc@66
|
2225 fun self (str, _) =
|
adamc@66
|
2226 case str of
|
adamc@66
|
2227 L'.StrVar x => SOME (x, [])
|
adamc@66
|
2228 | L'.StrProj (str, x) =>
|
adamc@66
|
2229 (case self str of
|
adamc@66
|
2230 NONE => NONE
|
adamc@66
|
2231 | SOME (m, ms) => SOME (m, ms @ [x]))
|
adamc@66
|
2232 | _ => NONE
|
adamc@66
|
2233 in
|
adamc@66
|
2234 case self str of
|
adamc@66
|
2235 NONE => sgn
|
adamc@66
|
2236 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
|
adamc@66
|
2237 end
|
adamc@66
|
2238
|
adamc@88
|
2239 fun dopen (env, denv) {str, strs, sgn} =
|
adamc@66
|
2240 let
|
adamc@66
|
2241 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
|
adamc@66
|
2242 (L'.StrVar str, #2 sgn) strs
|
adamc@66
|
2243 in
|
adamc@66
|
2244 case #1 (hnormSgn env sgn) of
|
adamc@66
|
2245 L'.SgnConst sgis =>
|
adamc@88
|
2246 ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) =>
|
adamc@162
|
2247 let
|
adamc@162
|
2248 val d =
|
adamc@162
|
2249 case sgi of
|
adamc@162
|
2250 L'.SgiConAbs (x, n, k) =>
|
adamc@162
|
2251 let
|
adamc@162
|
2252 val c = (L'.CModProj (str, strs, x), loc)
|
adamc@162
|
2253 in
|
adamc@162
|
2254 (L'.DCon (x, n, k, c), loc)
|
adamc@162
|
2255 end
|
adamc@162
|
2256 | L'.SgiCon (x, n, k, c) =>
|
adamc@162
|
2257 (L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
|
adamc@191
|
2258 | L'.SgiDatatype (x, n, xs, xncs) =>
|
adamc@191
|
2259 (L'.DDatatypeImp (x, n, str, strs, x, xs, xncs), loc)
|
adamc@191
|
2260 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
|
adamc@191
|
2261 (L'.DDatatypeImp (x, n, m1, ms, x', xs, xncs), loc)
|
adamc@162
|
2262 | L'.SgiVal (x, n, t) =>
|
adamc@162
|
2263 (L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc)
|
adamc@162
|
2264 | L'.SgiStr (x, n, sgn) =>
|
adamc@162
|
2265 (L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc)
|
adamc@162
|
2266 | L'.SgiSgn (x, n, sgn) =>
|
adamc@162
|
2267 (L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc)
|
adamc@162
|
2268 | L'.SgiConstraint (c1, c2) =>
|
adamc@162
|
2269 (L'.DConstraint (c1, c2), loc)
|
adamc@211
|
2270 | L'.SgiClassAbs (x, n) =>
|
adamc@211
|
2271 let
|
adamc@211
|
2272 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
|
adamc@211
|
2273 val c = (L'.CModProj (str, strs, x), loc)
|
adamc@211
|
2274 in
|
adamc@211
|
2275 (L'.DCon (x, n, k, c), loc)
|
adamc@211
|
2276 end
|
adamc@211
|
2277 | L'.SgiClass (x, n, _) =>
|
adamc@211
|
2278 let
|
adamc@211
|
2279 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
|
adamc@211
|
2280 val c = (L'.CModProj (str, strs, x), loc)
|
adamc@211
|
2281 in
|
adamc@211
|
2282 (L'.DCon (x, n, k, c), loc)
|
adamc@211
|
2283 end
|
adamc@162
|
2284 in
|
adamc@162
|
2285 (d, (E.declBinds env' d, denv'))
|
adamc@162
|
2286 end)
|
adamc@88
|
2287 (env, denv) sgis
|
adamc@66
|
2288 | _ => (strError env (UnOpenable sgn);
|
adamc@88
|
2289 ([], (env, denv)))
|
adamc@66
|
2290 end
|
adamc@66
|
2291
|
adamc@31
|
2292 fun sgiOfDecl (d, loc) =
|
adamc@31
|
2293 case d of
|
adamc@123
|
2294 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
|
adamc@156
|
2295 | L'.DDatatype x => [(L'.SgiDatatype x, loc)]
|
adamc@156
|
2296 | L'.DDatatypeImp x => [(L'.SgiDatatypeImp x, loc)]
|
adamc@123
|
2297 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
|
adamc@123
|
2298 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
|
adamc@123
|
2299 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
|
adamc@123
|
2300 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
|
adamc@123
|
2301 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
|
adamc@123
|
2302 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
|
adamc@123
|
2303 | L'.DExport _ => []
|
adamc@460
|
2304 | L'.DTable (tn, x, n, c) => [(L'.SgiVal (x, n, (L'.CApp (tableOf (), c), loc)), loc)]
|
adamc@460
|
2305 | L'.DSequence (tn, x, n) => [(L'.SgiVal (x, n, sequenceOf ()), loc)]
|
adamc@213
|
2306 | L'.DClass (x, n, c) => [(L'.SgiClass (x, n, c), loc)]
|
adamc@271
|
2307 | L'.DDatabase _ => []
|
adamc@460
|
2308 | L'.DCookie (tn, x, n, c) => [(L'.SgiVal (x, n, (L'.CApp (cookieOf (), c), loc)), loc)]
|
adamc@88
|
2309
|
adamc@88
|
2310 fun sgiBindsD (env, denv) (sgi, _) =
|
adamc@88
|
2311 case sgi of
|
adamc@90
|
2312 L'.SgiConstraint (c1, c2) =>
|
adamc@90
|
2313 (case D.assert env denv (c1, c2) of
|
adamc@90
|
2314 (denv, []) => denv
|
adamc@90
|
2315 | _ => raise Fail "sgiBindsD: Sub-constraints remain")
|
adamc@88
|
2316 | _ => denv
|
adamc@31
|
2317
|
adamc@86
|
2318 fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) =
|
adamc@469
|
2319 ((*prefaces "subSgn" [("sgn1", p_sgn env sgn1),
|
adamc@469
|
2320 ("sgn2", p_sgn env sgn2)];*)
|
adamc@35
|
2321 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
|
adamc@31
|
2322 (L'.SgnError, _) => ()
|
adamc@31
|
2323 | (_, L'.SgnError) => ()
|
adamc@31
|
2324
|
adamc@31
|
2325 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
|
adamc@31
|
2326 let
|
adamc@362
|
2327 (*val () = prefaces "subSgn" [("sgn1", p_sgn env sgn1),
|
adamc@362
|
2328 ("sgn2", p_sgn env sgn2),
|
adamc@362
|
2329 ("sgis1", p_sgn env (L'.SgnConst sgis1, loc2)),
|
adamc@362
|
2330 ("sgis2", p_sgn env (L'.SgnConst sgis2, loc2))]*)
|
adamc@362
|
2331
|
adamc@156
|
2332 fun folder (sgi2All as (sgi, loc), (env, denv)) =
|
adamc@31
|
2333 let
|
adamc@362
|
2334 (*val () = prefaces "folder" [("sgis1", p_sgn env (L'.SgnConst sgis1, loc2))]*)
|
adamc@362
|
2335
|
adamc@31
|
2336 fun seek p =
|
adamc@31
|
2337 let
|
adamc@88
|
2338 fun seek (env, denv) ls =
|
adamc@31
|
2339 case ls of
|
adamc@31
|
2340 [] => (sgnError env (UnmatchedSgi sgi2All);
|
adamc@88
|
2341 (env, denv))
|
adamc@31
|
2342 | h :: t =>
|
adamc@469
|
2343 case p (env, h) of
|
adamc@469
|
2344 NONE =>
|
adamc@469
|
2345 let
|
adamc@469
|
2346 val env = case #1 h of
|
adamc@469
|
2347 L'.SgiCon (x, n, k, c) =>
|
adamc@471
|
2348 if E.checkENamed env n then
|
adamc@471
|
2349 env
|
adamc@471
|
2350 else
|
adamc@471
|
2351 E.pushCNamedAs env x n k (SOME c)
|
adamc@469
|
2352 | L'.SgiConAbs (x, n, k) =>
|
adamc@471
|
2353 if E.checkENamed env n then
|
adamc@471
|
2354 env
|
adamc@471
|
2355 else
|
adamc@471
|
2356 E.pushCNamedAs env x n k NONE
|
adamc@469
|
2357 | _ => env
|
adamc@469
|
2358 in
|
adamc@469
|
2359 seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t
|
adamc@469
|
2360 end
|
adamc@88
|
2361 | SOME envs => envs
|
adamc@31
|
2362 in
|
adamc@88
|
2363 seek (env, denv) sgis1
|
adamc@31
|
2364 end
|
adamc@31
|
2365 in
|
adamc@31
|
2366 case sgi of
|
adamc@31
|
2367 L'.SgiConAbs (x, n2, k2) =>
|
adamc@469
|
2368 seek (fn (env, sgi1All as (sgi1, _)) =>
|
adamc@31
|
2369 let
|
adamc@41
|
2370 fun found (x', n1, k1, co1) =
|
adamc@41
|
2371 if x = x' then
|
adamc@41
|
2372 let
|
adamc@41
|
2373 val () = unifyKinds k1 k2
|
adamc@41
|
2374 handle KUnify (k1, k2, err) =>
|
adamc@41
|
2375 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
|
adamc@41
|
2376 val env = E.pushCNamedAs env x n1 k1 co1
|
adamc@41
|
2377 in
|
adamc@41
|
2378 SOME (if n1 = n2 then
|
adamc@41
|
2379 env
|
adamc@41
|
2380 else
|
adamc@88
|
2381 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)),
|
adamc@88
|
2382 denv)
|
adamc@41
|
2383 end
|
adamc@41
|
2384 else
|
adamc@41
|
2385 NONE
|
adamc@31
|
2386 in
|
adamc@31
|
2387 case sgi1 of
|
adamc@41
|
2388 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
|
adamc@41
|
2389 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
|
adamc@191
|
2390 | L'.SgiDatatype (x', n1, xs, _) =>
|
adamc@191
|
2391 let
|
adamc@191
|
2392 val k = (L'.KType, loc)
|
adamc@191
|
2393 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
|
adamc@191
|
2394 in
|
adamc@191
|
2395 found (x', n1, k', NONE)
|
adamc@191
|
2396 end
|
adamc@191
|
2397 | L'.SgiDatatypeImp (x', n1, m1, ms, s, xs, _) =>
|
adamc@191
|
2398 let
|
adamc@191
|
2399 val k = (L'.KType, loc)
|
adamc@191
|
2400 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
|
adamc@191
|
2401 in
|
adamc@191
|
2402 found (x', n1, k', SOME (L'.CModProj (m1, ms, s), loc))
|
adamc@191
|
2403 end
|
adamc@211
|
2404 | L'.SgiClassAbs (x', n1) => found (x', n1,
|
adamc@218
|
2405 (L'.KArrow ((L'.KType, loc),
|
adamc@218
|
2406 (L'.KType, loc)), loc),
|
adamc@218
|
2407 NONE)
|
adamc@211
|
2408 | L'.SgiClass (x', n1, c) => found (x', n1,
|
adamc@211
|
2409 (L'.KArrow ((L'.KType, loc),
|
adamc@211
|
2410 (L'.KType, loc)), loc),
|
adamc@211
|
2411 SOME c)
|
adamc@31
|
2412 | _ => NONE
|
adamc@31
|
2413 end)
|
adamc@31
|
2414
|
adamc@31
|
2415 | L'.SgiCon (x, n2, k2, c2) =>
|
adamc@469
|
2416 seek (fn (env, sgi1All as (sgi1, _)) =>
|
adamc@211
|
2417 let
|
adamc@211
|
2418 fun found (x', n1, k1, c1) =
|
adamc@211
|
2419 if x = x' then
|
adamc@211
|
2420 let
|
adamc@327
|
2421 fun good () =
|
adamc@327
|
2422 let
|
adamc@327
|
2423 val env = E.pushCNamedAs env x n2 k2 (SOME c2)
|
adamc@327
|
2424 val env = if n1 = n2 then
|
adamc@327
|
2425 env
|
adamc@327
|
2426 else
|
adamc@327
|
2427 E.pushCNamedAs env x n1 k1 (SOME c1)
|
adamc@327
|
2428 in
|
adamc@327
|
2429 SOME (env, denv)
|
adamc@327
|
2430 end
|
adamc@211
|
2431 in
|
adamc@211
|
2432 (case unifyCons (env, denv) c1 c2 of
|
adamc@211
|
2433 [] => good ()
|
adamc@211
|
2434 | _ => NONE)
|
adamc@211
|
2435 handle CUnify (c1, c2, err) =>
|
adamc@211
|
2436 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
|
adamc@211
|
2437 good ())
|
adamc@211
|
2438 end
|
adamc@211
|
2439 else
|
adamc@211
|
2440 NONE
|
adamc@211
|
2441 in
|
adamc@211
|
2442 case sgi1 of
|
adamc@211
|
2443 L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, c1)
|
adamc@211
|
2444 | L'.SgiClass (x', n1, c1) =>
|
adamc@211
|
2445 found (x', n1, (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc), c1)
|
adamc@211
|
2446 | _ => NONE
|
adamc@211
|
2447 end)
|
adamc@31
|
2448
|
adamc@191
|
2449 | L'.SgiDatatype (x, n2, xs2, xncs2) =>
|
adamc@469
|
2450 seek (fn (env, sgi1All as (sgi1, _)) =>
|
adamc@160
|
2451 let
|
adamc@191
|
2452 fun found (n1, xs1, xncs1) =
|
adamc@160
|
2453 let
|
adamc@160
|
2454 fun mismatched ue =
|
adamc@160
|
2455 (sgnError env (SgiMismatchedDatatypes (sgi1All, sgi2All, ue));
|
adamc@160
|
2456 SOME (env, denv))
|
adamc@156
|
2457
|
adamc@191
|
2458 val k = (L'.KType, loc)
|
adamc@191
|
2459 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs1
|
adamc@191
|
2460
|
adamc@160
|
2461 fun good () =
|
adamc@160
|
2462 let
|
adamc@471
|
2463 val env = E.sgiBinds env sgi1All
|
adamc@160
|
2464 val env = if n1 = n2 then
|
adamc@160
|
2465 env
|
adamc@160
|
2466 else
|
adamc@471
|
2467 E.pushCNamedAs env x n2 k'
|
adamc@471
|
2468 (SOME (L'.CNamed n1, loc))
|
adamc@160
|
2469 in
|
adamc@160
|
2470 SOME (env, denv)
|
adamc@160
|
2471 end
|
adamc@156
|
2472
|
adamc@397
|
2473 val env = E.pushCNamedAs env x n1 k' NONE
|
adamc@397
|
2474 val env = if n1 = n2 then
|
adamc@397
|
2475 env
|
adamc@397
|
2476 else
|
adamc@397
|
2477 E.pushCNamedAs env x n2 k' (SOME (L'.CNamed n1, loc))
|
adamc@191
|
2478 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs1
|
adamc@160
|
2479 fun xncBad ((x1, _, t1), (x2, _, t2)) =
|
adamc@160
|
2480 String.compare (x1, x2) <> EQUAL
|
adamc@160
|
2481 orelse case (t1, t2) of
|
adamc@160
|
2482 (NONE, NONE) => false
|
adamc@160
|
2483 | (SOME t1, SOME t2) =>
|
adamc@160
|
2484 not (List.null (unifyCons (env, denv) t1 t2))
|
adamc@160
|
2485 | _ => true
|
adamc@160
|
2486 in
|
adamc@191
|
2487 (if xs1 <> xs2
|
adamc@191
|
2488 orelse length xncs1 <> length xncs2
|
adamc@160
|
2489 orelse ListPair.exists xncBad (xncs1, xncs2) then
|
adamc@160
|
2490 mismatched NONE
|
adamc@160
|
2491 else
|
adamc@160
|
2492 good ())
|
adamc@160
|
2493 handle CUnify ue => mismatched (SOME ue)
|
adamc@160
|
2494 end
|
adamc@160
|
2495 in
|
adamc@160
|
2496 case sgi1 of
|
adamc@191
|
2497 L'.SgiDatatype (x', n1, xs, xncs1) =>
|
adamc@160
|
2498 if x' = x then
|
adamc@191
|
2499 found (n1, xs, xncs1)
|
adamc@160
|
2500 else
|
adamc@160
|
2501 NONE
|
adamc@191
|
2502 | L'.SgiDatatypeImp (x', n1, _, _, _, xs, xncs1) =>
|
adamc@162
|
2503 if x' = x then
|
adamc@191
|
2504 found (n1, xs, xncs1)
|
adamc@162
|
2505 else
|
adamc@162
|
2506 NONE
|
adamc@160
|
2507 | _ => NONE
|
adamc@160
|
2508 end)
|
adamc@156
|
2509
|
adamc@191
|
2510 | L'.SgiDatatypeImp (x, n2, m12, ms2, s2, xs, _) =>
|
adamc@469
|
2511 seek (fn (env, sgi1All as (sgi1, _)) =>
|
adamc@158
|
2512 case sgi1 of
|
adamc@191
|
2513 L'.SgiDatatypeImp (x', n1, m11, ms1, s1, _, _) =>
|
adamc@158
|
2514 if x = x' then
|
adamc@158
|
2515 let
|
adamc@158
|
2516 val k = (L'.KType, loc)
|
adamc@191
|
2517 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
|
adamc@158
|
2518 val t1 = (L'.CModProj (m11, ms1, s1), loc)
|
adamc@158
|
2519 val t2 = (L'.CModProj (m12, ms2, s2), loc)
|
adamc@158
|
2520
|
adamc@158
|
2521 fun good () =
|
adamc@158
|
2522 let
|
adamc@191
|
2523 val env = E.pushCNamedAs env x n1 k' (SOME t1)
|
adamc@191
|
2524 val env = E.pushCNamedAs env x n2 k' (SOME t2)
|
adamc@158
|
2525 in
|
adamc@158
|
2526 SOME (env, denv)
|
adamc@158
|
2527 end
|
adamc@158
|
2528 in
|
adamc@158
|
2529 (case unifyCons (env, denv) t1 t2 of
|
adamc@158
|
2530 [] => good ()
|
adamc@158
|
2531 | _ => NONE)
|
adamc@158
|
2532 handle CUnify (c1, c2, err) =>
|
adamc@158
|
2533 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
|
adamc@158
|
2534 good ())
|
adamc@158
|
2535 end
|
adamc@158
|
2536 else
|
adamc@158
|
2537 NONE
|
adamc@158
|
2538
|
adamc@158
|
2539 | _ => NONE)
|
adamc@156
|
2540
|
adamc@32
|
2541 | L'.SgiVal (x, n2, c2) =>
|
adamc@469
|
2542 seek (fn (env, sgi1All as (sgi1, _)) =>
|
adamc@32
|
2543 case sgi1 of
|
adamc@41
|
2544 L'.SgiVal (x', n1, c1) =>
|
adamc@41
|
2545 if x = x' then
|
adamc@362
|
2546 ((*prefaces "Pre" [("c1", p_con env c1),
|
adamc@362
|
2547 ("c2", p_con env c2)];*)
|
adamc@362
|
2548 case unifyCons (env, denv) c1 c2 of
|
adamc@88
|
2549 [] => SOME (env, denv)
|
adamc@86
|
2550 | _ => NONE)
|
adamc@86
|
2551 handle CUnify (c1, c2, err) =>
|
adamc@86
|
2552 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
|
adamc@88
|
2553 SOME (env, denv))
|
adamc@41
|
2554 else
|
adamc@41
|
2555 NONE
|
adamc@32
|
2556 | _ => NONE)
|
adamc@32
|
2557
|
adamc@33
|
2558 | L'.SgiStr (x, n2, sgn2) =>
|
adamc@469
|
2559 seek (fn (env, sgi1All as (sgi1, _)) =>
|
adamc@33
|
2560 case sgi1 of
|
adamc@41
|
2561 L'.SgiStr (x', n1, sgn1) =>
|
adamc@41
|
2562 if x = x' then
|
adamc@66
|
2563 let
|
adamc@86
|
2564 val () = subSgn (env, denv) sgn1 sgn2
|
adamc@66
|
2565 val env = E.pushStrNamedAs env x n1 sgn1
|
adamc@66
|
2566 val env = if n1 = n2 then
|
adamc@66
|
2567 env
|
adamc@66
|
2568 else
|
adamc@66
|
2569 E.pushStrNamedAs env x n2
|
adamc@66
|
2570 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
|
adamc@66
|
2571 sgn = sgn2})
|
adamc@66
|
2572 in
|
adamc@88
|
2573 SOME (env, denv)
|
adamc@66
|
2574 end
|
adamc@41
|
2575 else
|
adamc@41
|
2576 NONE
|
adamc@33
|
2577 | _ => NONE)
|
adamc@59
|
2578
|
adamc@59
|
2579 | L'.SgiSgn (x, n2, sgn2) =>
|
adamc@469
|
2580 seek (fn (env, sgi1All as (sgi1, _)) =>
|
adamc@59
|
2581 case sgi1 of
|
adamc@59
|
2582 L'.SgiSgn (x', n1, sgn1) =>
|
adamc@59
|
2583 if x = x' then
|
adamc@65
|
2584 let
|
adamc@86
|
2585 val () = subSgn (env, denv) sgn1 sgn2
|
adamc@86
|
2586 val () = subSgn (env, denv) sgn2 sgn1
|
adamc@65
|
2587
|
adamc@65
|
2588 val env = E.pushSgnNamedAs env x n2 sgn2
|
adamc@65
|
2589 val env = if n1 = n2 then
|
adamc@65
|
2590 env
|
adamc@65
|
2591 else
|
adamc@65
|
2592 E.pushSgnNamedAs env x n1 sgn2
|
adamc@65
|
2593 in
|
adamc@88
|
2594 SOME (env, denv)
|
adamc@65
|
2595 end
|
adamc@59
|
2596 else
|
adamc@59
|
2597 NONE
|
adamc@59
|
2598 | _ => NONE)
|
adamc@88
|
2599
|
adamc@88
|
2600 | L'.SgiConstraint (c2, d2) =>
|
adamc@469
|
2601 seek (fn (env, sgi1All as (sgi1, _)) =>
|
adamc@88
|
2602 case sgi1 of
|
adamc@88
|
2603 L'.SgiConstraint (c1, d1) =>
|
adamc@88
|
2604 if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then
|
adamc@90
|
2605 let
|
adamc@90
|
2606 val (denv, gs) = D.assert env denv (c2, d2)
|
adamc@90
|
2607 in
|
adamc@90
|
2608 case gs of
|
adamc@90
|
2609 [] => ()
|
adamc@90
|
2610 | _ => raise Fail "subSgn: Sub-constraints remain";
|
adamc@90
|
2611
|
adamc@90
|
2612 SOME (env, denv)
|
adamc@90
|
2613 end
|
adamc@88
|
2614 else
|
adamc@88
|
2615 NONE
|
adamc@88
|
2616 | _ => NONE)
|
adamc@203
|
2617
|
adamc@211
|
2618 | L'.SgiClassAbs (x, n2) =>
|
adamc@469
|
2619 seek (fn (env, sgi1All as (sgi1, _)) =>
|
adamc@211
|
2620 let
|
adamc@211
|
2621 fun found (x', n1, co) =
|
adamc@211
|
2622 if x = x' then
|
adamc@211
|
2623 let
|
adamc@211
|
2624 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
|
adamc@211
|
2625 val env = E.pushCNamedAs env x n1 k co
|
adamc@211
|
2626 in
|
adamc@211
|
2627 SOME (if n1 = n2 then
|
adamc@211
|
2628 env
|
adamc@211
|
2629 else
|
adamc@211
|
2630 E.pushCNamedAs env x n2 k (SOME (L'.CNamed n1, loc2)),
|
adamc@211
|
2631 denv)
|
adamc@211
|
2632 end
|
adamc@211
|
2633 else
|
adamc@211
|
2634 NONE
|
adamc@211
|
2635 in
|
adamc@211
|
2636 case sgi1 of
|
adamc@211
|
2637 L'.SgiClassAbs (x', n1) => found (x', n1, NONE)
|
adamc@211
|
2638 | L'.SgiClass (x', n1, c) => found (x', n1, SOME c)
|
adamc@211
|
2639 | _ => NONE
|
adamc@211
|
2640 end)
|
adamc@211
|
2641 | L'.SgiClass (x, n2, c2) =>
|
adamc@469
|
2642 seek (fn (env, sgi1All as (sgi1, _)) =>
|
adamc@211
|
2643 let
|
adamc@211
|
2644 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
|
adamc@211
|
2645
|
adamc@211
|
2646 fun found (x', n1, c1) =
|
adamc@211
|
2647 if x = x' then
|
adamc@211
|
2648 let
|
adamc@218
|
2649 fun good () =
|
adamc@218
|
2650 let
|
adamc@218
|
2651 val env = E.pushCNamedAs env x n2 k (SOME c2)
|
adamc@218
|
2652 val env = if n1 = n2 then
|
adamc@218
|
2653 env
|
adamc@218
|
2654 else
|
adamc@218
|
2655 E.pushCNamedAs env x n1 k (SOME c1)
|
adamc@218
|
2656 in
|
adamc@218
|
2657 SOME (env, denv)
|
adamc@218
|
2658 end
|
adamc@211
|
2659 in
|
adamc@211
|
2660 (case unifyCons (env, denv) c1 c2 of
|
adamc@211
|
2661 [] => good ()
|
adamc@211
|
2662 | _ => NONE)
|
adamc@211
|
2663 handle CUnify (c1, c2, err) =>
|
adamc@211
|
2664 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
|
adamc@211
|
2665 good ())
|
adamc@211
|
2666 end
|
adamc@211
|
2667 else
|
adamc@211
|
2668 NONE
|
adamc@211
|
2669 in
|
adamc@211
|
2670 case sgi1 of
|
adamc@211
|
2671 L'.SgiClass (x', n1, c1) => found (x', n1, c1)
|
adamc@211
|
2672 | _ => NONE
|
adamc@211
|
2673 end)
|
adamc@31
|
2674 end
|
adamc@31
|
2675 in
|
adamc@88
|
2676 ignore (foldl folder (env, denv) sgis2)
|
adamc@31
|
2677 end
|
adamc@31
|
2678
|
adamc@41
|
2679 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
|
adamc@43
|
2680 let
|
adamc@480
|
2681 val ran2 =
|
adamc@43
|
2682 if n1 = n2 then
|
adamc@480
|
2683 ran2
|
adamc@43
|
2684 else
|
adamc@480
|
2685 subStrInSgn (n2, n1) ran2
|
adamc@43
|
2686 in
|
adamc@86
|
2687 subSgn (env, denv) dom2 dom1;
|
adamc@480
|
2688 subSgn (E.pushStrNamedAs env m1 n1 dom2, denv) ran1 ran2
|
adamc@43
|
2689 end
|
adamc@41
|
2690
|
adamc@469
|
2691 | _ => sgnError env (SgnWrongForm (sgn1, sgn2)))
|
adamc@41
|
2692
|
adamc@61
|
2693
|
adamc@312
|
2694 fun positive self =
|
adamc@312
|
2695 let
|
adamc@312
|
2696 open L
|
adamc@312
|
2697
|
adamc@312
|
2698 fun none (c, _) =
|
adamc@312
|
2699 case c of
|
adamc@312
|
2700 CAnnot (c, _) => none c
|
adamc@312
|
2701
|
adamc@312
|
2702 | TFun (c1, c2) => none c1 andalso none c2
|
adamc@312
|
2703 | TCFun (_, _, _, c) => none c
|
adamc@312
|
2704 | TRecord c => none c
|
adamc@312
|
2705
|
adamc@312
|
2706 | CVar ([], x) => x <> self
|
adamc@312
|
2707 | CVar _ => true
|
adamc@312
|
2708 | CApp (c1, c2) => none c1 andalso none c2
|
adamc@312
|
2709 | CAbs _ => false
|
adamc@312
|
2710 | CDisjoint (c1, c2, c3) => none c1 andalso none c2 andalso none c3
|
adamc@312
|
2711
|
adamc@312
|
2712 | CName _ => true
|
adamc@312
|
2713
|
adamc@312
|
2714 | CRecord xcs => List.all (fn (c1, c2) => none c1 andalso none c2) xcs
|
adamc@312
|
2715 | CConcat (c1, c2) => none c1 andalso none c2
|
adamc@312
|
2716 | CFold => true
|
adamc@312
|
2717
|
adamc@312
|
2718 | CUnit => true
|
adamc@312
|
2719
|
adamc@312
|
2720 | CTuple cs => List.all none cs
|
adamc@312
|
2721 | CProj (c, _) => none c
|
adamc@312
|
2722
|
adamc@312
|
2723 | CWild _ => false
|
adamc@312
|
2724
|
adamc@312
|
2725 fun pos (c, _) =
|
adamc@312
|
2726 case c of
|
adamc@312
|
2727 CAnnot (c, _) => pos c
|
adamc@312
|
2728
|
adamc@312
|
2729 | TFun (c1, c2) => none c1 andalso pos c2
|
adamc@312
|
2730 | TCFun (_, _, _, c) => pos c
|
adamc@312
|
2731 | TRecord c => pos c
|
adamc@312
|
2732
|
adamc@312
|
2733 | CVar _ => true
|
adamc@312
|
2734 | CApp (c1, c2) => pos c1 andalso none c2
|
adamc@312
|
2735 | CAbs _ => false
|
adamc@312
|
2736 | CDisjoint (c1, c2, c3) => none c1 andalso none c2 andalso none c3
|
adamc@312
|
2737
|
adamc@312
|
2738 | CName _ => true
|
adamc@312
|
2739
|
adamc@312
|
2740 | CRecord xcs => List.all (fn (c1, c2) => none c1 andalso pos c2) xcs
|
adamc@312
|
2741 | CConcat (c1, c2) => pos c1 andalso pos c2
|
adamc@312
|
2742 | CFold => true
|
adamc@312
|
2743
|
adamc@312
|
2744 | CUnit => true
|
adamc@312
|
2745
|
adamc@312
|
2746 | CTuple cs => List.all pos cs
|
adamc@312
|
2747 | CProj (c, _) => pos c
|
adamc@312
|
2748
|
adamc@312
|
2749 | CWild _ => false
|
adamc@312
|
2750 in
|
adamc@312
|
2751 pos
|
adamc@312
|
2752 end
|
adamc@312
|
2753
|
adamc@325
|
2754 fun wildifyStr env (str, sgn) =
|
adamc@325
|
2755 case #1 (hnormSgn env sgn) of
|
adamc@325
|
2756 L'.SgnConst sgis =>
|
adamc@325
|
2757 (case #1 str of
|
adamc@325
|
2758 L.StrConst ds =>
|
adamc@325
|
2759 let
|
adamc@493
|
2760 fun decompileKind (k, loc) =
|
adamc@493
|
2761 case k of
|
adamc@493
|
2762 L'.KType => SOME (L.KType, loc)
|
adamc@493
|
2763 | L'.KArrow (k1, k2) =>
|
adamc@493
|
2764 (case (decompileKind k1, decompileKind k2) of
|
adamc@493
|
2765 (SOME k1, SOME k2) => SOME (L.KArrow (k1, k2), loc)
|
adamc@493
|
2766 | _ => NONE)
|
adamc@493
|
2767 | L'.KName => SOME (L.KName, loc)
|
adamc@493
|
2768 | L'.KRecord k =>
|
adamc@493
|
2769 (case decompileKind k of
|
adamc@493
|
2770 SOME k => SOME (L.KRecord k, loc)
|
adamc@493
|
2771 | _ => NONE)
|
adamc@493
|
2772 | L'.KUnit => SOME (L.KUnit, loc)
|
adamc@493
|
2773 | L'.KTuple ks =>
|
adamc@493
|
2774 let
|
adamc@493
|
2775 val ks' = List.mapPartial decompileKind ks
|
adamc@493
|
2776 in
|
adamc@493
|
2777 if length ks' = length ks then
|
adamc@493
|
2778 SOME (L.KTuple ks', loc)
|
adamc@493
|
2779 else
|
adamc@493
|
2780 NONE
|
adamc@493
|
2781 end
|
adamc@493
|
2782
|
adamc@493
|
2783 | L'.KError => NONE
|
adamc@493
|
2784 | L'.KUnif (_, _, ref (SOME k)) => decompileKind k
|
adamc@493
|
2785 | L'.KUnif _ => NONE
|
adamc@493
|
2786
|
adamc@325
|
2787 fun decompileCon env (c, loc) =
|
adamc@325
|
2788 case c of
|
adamc@325
|
2789 L'.CRel i =>
|
adamc@325
|
2790 let
|
adamc@325
|
2791 val (s, _) = E.lookupCRel env i
|
adamc@325
|
2792 in
|
adamc@325
|
2793 SOME (L.CVar ([], s), loc)
|
adamc@325
|
2794 end
|
adamc@325
|
2795 | L'.CNamed i =>
|
adamc@325
|
2796 let
|
adamc@325
|
2797 val (s, _, _) = E.lookupCNamed env i
|
adamc@325
|
2798 in
|
adamc@325
|
2799 SOME (L.CVar ([], s), loc)
|
adamc@325
|
2800 end
|
adamc@325
|
2801 | L'.CModProj (m1, ms, x) =>
|
adamc@325
|
2802 let
|
adamc@325
|
2803 val (s, _) = E.lookupStrNamed env m1
|
adamc@325
|
2804 in
|
adamc@325
|
2805 SOME (L.CVar (s :: ms, x), loc)
|
adamc@325
|
2806 end
|
adamc@325
|
2807 | L'.CName s => SOME (L.CName s, loc)
|
adamc@325
|
2808 | L'.CRecord (_, xcs) =>
|
adamc@325
|
2809 let
|
adamc@325
|
2810 fun fields xcs =
|
adamc@325
|
2811 case xcs of
|
adamc@325
|
2812 [] => SOME []
|
adamc@325
|
2813 | (x, t) :: xcs =>
|
adamc@325
|
2814 case (decompileCon env x, decompileCon env t, fields xcs) of
|
adamc@325
|
2815 (SOME x, SOME t, SOME xcs) => SOME ((x, t) :: xcs)
|
adamc@325
|
2816 | _ => NONE
|
adamc@325
|
2817 in
|
adamc@325
|
2818 Option.map (fn xcs => (L.CRecord xcs, loc))
|
adamc@325
|
2819 (fields xcs)
|
adamc@325
|
2820 end
|
adamc@325
|
2821 | L'.CConcat (c1, c2) =>
|
adamc@325
|
2822 (case (decompileCon env c1, decompileCon env c2) of
|
adamc@325
|
2823 (SOME c1, SOME c2) => SOME (L.CConcat (c1, c2), loc)
|
adamc@325
|
2824 | _ => NONE)
|
adamc@325
|
2825 | L'.CUnit => SOME (L.CUnit, loc)
|
adamc@325
|
2826
|
adamc@325
|
2827 | _ => NONE
|
adamc@325
|
2828
|
adamc@469
|
2829 val (neededC, constraints, neededV, _) =
|
adamc@469
|
2830 foldl (fn ((sgi, loc), (neededC, constraints, neededV, env')) =>
|
adamc@325
|
2831 let
|
adamc@469
|
2832 val (needed, constraints, neededV) =
|
adamc@325
|
2833 case sgi of
|
adamc@493
|
2834 L'.SgiConAbs (x, _, k) => (SM.insert (neededC, x, k), constraints, neededV)
|
adamc@469
|
2835 | L'.SgiConstraint cs => (neededC, (env', cs, loc) :: constraints, neededV)
|
adamc@469
|
2836
|
adamc@469
|
2837 | L'.SgiVal (x, _, t) =>
|
adamc@469
|
2838 let
|
adamc@469
|
2839 fun default () = (neededC, constraints, neededV)
|
adamc@469
|
2840
|
adamc@469
|
2841 val t = normClassConstraint env' t
|
adamc@469
|
2842 in
|
adamc@469
|
2843 case #1 t of
|
adamc@469
|
2844 L'.CApp (f, _) =>
|
adamc@469
|
2845 if E.isClass env' f then
|
adamc@469
|
2846 (neededC, constraints, SS.add (neededV, x))
|
adamc@469
|
2847 else
|
adamc@469
|
2848 default ()
|
adamc@469
|
2849
|
adamc@469
|
2850 | _ => default ()
|
adamc@469
|
2851 end
|
adamc@469
|
2852
|
adamc@469
|
2853 | _ => (neededC, constraints, neededV)
|
adamc@325
|
2854 in
|
adamc@469
|
2855 (needed, constraints, neededV, E.sgiBinds env' (sgi, loc))
|
adamc@325
|
2856 end)
|
adamc@493
|
2857 (SM.empty, [], SS.empty, env) sgis
|
adamc@325
|
2858
|
adamc@469
|
2859 val (neededC, neededV) = foldl (fn ((d, _), needed as (neededC, neededV)) =>
|
adamc@469
|
2860 case d of
|
adamc@493
|
2861 L.DCon (x, _, _) => ((#1 (SM.remove (neededC, x)), neededV)
|
adamc@469
|
2862 handle NotFound =>
|
adamc@469
|
2863 needed)
|
adamc@493
|
2864 | L.DClass (x, _) => ((#1 (SM.remove (neededC, x)), neededV)
|
adamc@469
|
2865 handle NotFound => needed)
|
adamc@469
|
2866 | L.DVal (x, _, _) => ((neededC, SS.delete (neededV, x))
|
adamc@469
|
2867 handle NotFound => needed)
|
adamc@493
|
2868 | L.DOpen _ => (SM.empty, SS.empty)
|
adamc@469
|
2869 | _ => needed)
|
adamc@469
|
2870 (neededC, neededV) ds
|
adamc@469
|
2871
|
adamc@469
|
2872 val ds' = List.mapPartial (fn (env', (c1, c2), loc) =>
|
adamc@325
|
2873 case (decompileCon env' c1, decompileCon env' c2) of
|
adamc@325
|
2874 (SOME c1, SOME c2) =>
|
adamc@325
|
2875 SOME (L.DConstraint (c1, c2), loc)
|
adamc@325
|
2876 | _ => NONE) constraints
|
adamc@469
|
2877
|
adamc@469
|
2878 val ds' =
|
adamc@469
|
2879 case SS.listItems neededV of
|
adamc@469
|
2880 [] => ds'
|
adamc@469
|
2881 | xs =>
|
adamc@469
|
2882 let
|
adamc@469
|
2883 val ewild = (L.EWild, #2 str)
|
adamc@469
|
2884 val ds'' = map (fn x => (L.DVal (x, NONE, ewild), #2 str)) xs
|
adamc@469
|
2885 in
|
adamc@469
|
2886 ds'' @ ds'
|
adamc@469
|
2887 end
|
adamc@469
|
2888
|
adamc@469
|
2889 val ds' =
|
adamc@493
|
2890 case SM.listItemsi neededC of
|
adamc@469
|
2891 [] => ds'
|
adamc@469
|
2892 | xs =>
|
adamc@469
|
2893 let
|
adamc@493
|
2894 val ds'' = map (fn (x, k) =>
|
adamc@493
|
2895 let
|
adamc@493
|
2896 val k =
|
adamc@493
|
2897 case decompileKind k of
|
adamc@493
|
2898 NONE => (L.KWild, #2 str)
|
adamc@493
|
2899 | SOME k => k
|
adamc@493
|
2900 val cwild = (L.CWild k, #2 str)
|
adamc@493
|
2901 in
|
adamc@493
|
2902 (L.DCon (x, NONE, cwild), #2 str)
|
adamc@493
|
2903 end) xs
|
adamc@469
|
2904 in
|
adamc@469
|
2905 ds'' @ ds'
|
adamc@469
|
2906 end
|
adamc@325
|
2907 in
|
adamc@469
|
2908 (L.StrConst (ds @ ds'), #2 str)
|
adamc@325
|
2909 end
|
adamc@325
|
2910 | _ => str)
|
adamc@325
|
2911 | _ => str
|
adamc@325
|
2912
|
adamc@280
|
2913 fun elabDecl (dAll as (d, loc), (env, denv, gs : constraint list)) =
|
adamc@255
|
2914 let
|
adamc@255
|
2915 (*val () = preface ("elabDecl", SourcePrint.p_decl (d, loc))*)
|
adamc@255
|
2916
|
adamc@255
|
2917 val r =
|
adamc@255
|
2918 case d of
|
adamc@255
|
2919 L.DCon (x, ko, c) =>
|
adamc@255
|
2920 let
|
adamc@255
|
2921 val k' = case ko of
|
adamc@255
|
2922 NONE => kunif loc
|
adamc@255
|
2923 | SOME k => elabKind k
|
adamc@255
|
2924
|
adamc@255
|
2925 val (c', ck, gs') = elabCon (env, denv) c
|
adamc@255
|
2926 val (env', n) = E.pushCNamed env x k' (SOME c')
|
adamc@255
|
2927 in
|
adamc@255
|
2928 checkKind env c' ck k';
|
adamc@255
|
2929
|
adamc@255
|
2930 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, enD gs' @ gs))
|
adamc@255
|
2931 end
|
adamc@255
|
2932 | L.DDatatype (x, xs, xcs) =>
|
adamc@255
|
2933 let
|
adamc@312
|
2934 val positive = List.all (fn (_, to) =>
|
adamc@312
|
2935 case to of
|
adamc@312
|
2936 NONE => true
|
adamc@312
|
2937 | SOME t => positive x t) xcs
|
adamc@312
|
2938
|
adamc@255
|
2939 val k = (L'.KType, loc)
|
adamc@255
|
2940 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
|
adamc@255
|
2941 val (env, n) = E.pushCNamed env x k' NONE
|
adamc@255
|
2942 val t = (L'.CNamed n, loc)
|
adamc@255
|
2943 val nxs = length xs - 1
|
adamc@255
|
2944 val t = ListUtil.foldli (fn (i, _, t) => (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
|
adamc@255
|
2945
|
adamc@255
|
2946 val (env', denv') = foldl (fn (x, (env', denv')) =>
|
adamc@255
|
2947 (E.pushCRel env' x k,
|
adamc@255
|
2948 D.enter denv')) (env, denv) xs
|
adamc@255
|
2949
|
adamc@255
|
2950 val (xcs, (used, env, gs')) =
|
adamc@255
|
2951 ListUtil.foldlMap
|
adamc@255
|
2952 (fn ((x, to), (used, env, gs)) =>
|
adamc@255
|
2953 let
|
adamc@255
|
2954 val (to, t, gs') = case to of
|
adamc@255
|
2955 NONE => (NONE, t, gs)
|
adamc@255
|
2956 | SOME t' =>
|
adamc@255
|
2957 let
|
adamc@255
|
2958 val (t', tk, gs') = elabCon (env', denv') t'
|
adamc@255
|
2959 in
|
adamc@255
|
2960 checkKind env' t' tk k;
|
adamc@255
|
2961 (SOME t', (L'.TFun (t', t), loc), enD gs' @ gs)
|
adamc@255
|
2962 end
|
adamc@255
|
2963 val t = foldr (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc)) t xs
|
adamc@255
|
2964
|
adamc@255
|
2965 val (env, n') = E.pushENamed env x t
|
adamc@255
|
2966 in
|
adamc@255
|
2967 if SS.member (used, x) then
|
adamc@255
|
2968 strError env (DuplicateConstructor (x, loc))
|
adamc@255
|
2969 else
|
adamc@255
|
2970 ();
|
adamc@255
|
2971 ((x, n', to), (SS.add (used, x), env, gs'))
|
adamc@255
|
2972 end)
|
adamc@255
|
2973 (SS.empty, env, []) xcs
|
adamc@255
|
2974
|
adamc@255
|
2975 val env = E.pushDatatype env n xs xcs
|
adamc@312
|
2976 val d' = (L'.DDatatype (x, n, xs, xcs), loc)
|
adamc@255
|
2977 in
|
adamc@312
|
2978 if positive then
|
adamc@312
|
2979 ()
|
adamc@312
|
2980 else
|
adamc@312
|
2981 declError env (Nonpositive d');
|
adamc@312
|
2982
|
adamc@312
|
2983 ([d'], (env, denv, gs' @ gs))
|
adamc@255
|
2984 end
|
adamc@255
|
2985
|
adamc@255
|
2986 | L.DDatatypeImp (_, [], _) => raise Fail "Empty DDatatypeImp"
|
adamc@255
|
2987
|
adamc@255
|
2988 | L.DDatatypeImp (x, m1 :: ms, s) =>
|
adamc@255
|
2989 (case E.lookupStr env m1 of
|
adamc@255
|
2990 NONE => (expError env (UnboundStrInExp (loc, m1));
|
adamc@255
|
2991 ([], (env, denv, gs)))
|
adamc@255
|
2992 | SOME (n, sgn) =>
|
adamc@255
|
2993 let
|
adamc@255
|
2994 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@255
|
2995 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@255
|
2996 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@255
|
2997 (strerror, sgnerror))
|
adamc@255
|
2998 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@255
|
2999 ((L'.StrVar n, loc), sgn) ms
|
adamc@255
|
3000 in
|
adamc@255
|
3001 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
|
adamc@255
|
3002 ((L'.CModProj (n, ms, s), _), gs') =>
|
adamc@255
|
3003 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
|
adamc@255
|
3004 NONE => (conError env (UnboundDatatype (loc, s));
|
adamc@255
|
3005 ([], (env, denv, gs)))
|
adamc@255
|
3006 | SOME (xs, xncs) =>
|
adamc@255
|
3007 let
|
adamc@255
|
3008 val k = (L'.KType, loc)
|
adamc@255
|
3009 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
|
adamc@255
|
3010 val t = (L'.CModProj (n, ms, s), loc)
|
adamc@255
|
3011 val (env, n') = E.pushCNamed env x k' (SOME t)
|
adamc@255
|
3012 val env = E.pushDatatype env n' xs xncs
|
adamc@255
|
3013
|
adamc@255
|
3014 val t = (L'.CNamed n', loc)
|
adamc@255
|
3015 val env = foldl (fn ((x, n, to), env) =>
|
adamc@255
|
3016 let
|
adamc@255
|
3017 val t = case to of
|
adamc@255
|
3018 NONE => t
|
adamc@255
|
3019 | SOME t' => (L'.TFun (t', t), loc)
|
adamc@255
|
3020
|
adamc@255
|
3021 val t = foldr (fn (x, t) =>
|
adamc@255
|
3022 (L'.TCFun (L'.Implicit, x, k, t), loc))
|
adamc@255
|
3023 t xs
|
adamc@255
|
3024 in
|
adamc@255
|
3025 E.pushENamedAs env x n t
|
adamc@255
|
3026 end) env xncs
|
adamc@255
|
3027 in
|
adamc@255
|
3028 ([(L'.DDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, enD gs' @ gs))
|
adamc@255
|
3029 end)
|
adamc@255
|
3030 | _ => (strError env (NotDatatype loc);
|
adamc@255
|
3031 ([], (env, denv, [])))
|
adamc@255
|
3032 end)
|
adamc@255
|
3033
|
adamc@255
|
3034 | L.DVal (x, co, e) =>
|
adamc@255
|
3035 let
|
adamc@255
|
3036 val (c', _, gs1) = case co of
|
adamc@255
|
3037 NONE => (cunif (loc, ktype), ktype, [])
|
adamc@255
|
3038 | SOME c => elabCon (env, denv) c
|
adamc@255
|
3039
|
adamc@255
|
3040 val (e', et, gs2) = elabExp (env, denv) e
|
adamc@255
|
3041 val gs3 = checkCon (env, denv) e' et c'
|
adamc@467
|
3042 val c = normClassConstraint env c'
|
adamc@255
|
3043 val (env', n) = E.pushENamed env x c'
|
adamc@334
|
3044 val c' = makeInstantiable c'
|
adamc@255
|
3045 in
|
adamc@255
|
3046 (*prefaces "DVal" [("x", Print.PD.string x),
|
adamc@362
|
3047 ("c'", p_con env c')];*)
|
adamc@467
|
3048 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, enD gs1 @ gs2 @ enD gs3 @ gs))
|
adamc@255
|
3049 end
|
adamc@255
|
3050 | L.DValRec vis =>
|
adamc@255
|
3051 let
|
adamc@255
|
3052 fun allowable (e, _) =
|
adamc@255
|
3053 case e of
|
adamc@255
|
3054 L.EAbs _ => true
|
adamc@255
|
3055 | L.ECAbs (_, _, _, e) => allowable e
|
adamc@255
|
3056 | L.EDisjoint (_, _, e) => allowable e
|
adamc@255
|
3057 | _ => false
|
adamc@255
|
3058
|
adamc@255
|
3059 val (vis, gs) = ListUtil.foldlMap
|
adamc@255
|
3060 (fn ((x, co, e), gs) =>
|
adamc@255
|
3061 let
|
adamc@255
|
3062 val (c', _, gs1) = case co of
|
adamc@255
|
3063 NONE => (cunif (loc, ktype), ktype, [])
|
adamc@255
|
3064 | SOME c => elabCon (env, denv) c
|
adamc@255
|
3065 in
|
adamc@255
|
3066 ((x, c', e), enD gs1 @ gs)
|
adamc@280
|
3067 end) gs vis
|
adamc@255
|
3068
|
adamc@255
|
3069 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
|
adamc@255
|
3070 let
|
adamc@255
|
3071 val (env, n) = E.pushENamed env x c'
|
adamc@255
|
3072 in
|
adamc@255
|
3073 ((x, n, c', e), env)
|
adamc@255
|
3074 end) env vis
|
adamc@255
|
3075
|
adamc@255
|
3076 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
|
adamc@255
|
3077 let
|
adamc@255
|
3078 val (e', et, gs1) = elabExp (env, denv) e
|
adamc@255
|
3079
|
adamc@255
|
3080 val gs2 = checkCon (env, denv) e' et c'
|
adamc@334
|
3081
|
adamc@334
|
3082 val c' = makeInstantiable c'
|
adamc@255
|
3083 in
|
adamc@255
|
3084 if allowable e then
|
adamc@255
|
3085 ()
|
adamc@255
|
3086 else
|
adamc@255
|
3087 expError env (IllegalRec (x, e'));
|
adamc@255
|
3088 ((x, n, c', e'), gs1 @ enD gs2 @ gs)
|
adamc@255
|
3089 end) gs vis
|
adamc@255
|
3090 in
|
adamc@255
|
3091 ([(L'.DValRec vis, loc)], (env, denv, gs))
|
adamc@255
|
3092 end
|
adamc@255
|
3093
|
adamc@255
|
3094 | L.DSgn (x, sgn) =>
|
adamc@255
|
3095 let
|
adamc@255
|
3096 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@255
|
3097 val (env', n) = E.pushSgnNamed env x sgn'
|
adamc@255
|
3098 in
|
adamc@255
|
3099 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, enD gs' @ gs))
|
adamc@255
|
3100 end
|
adamc@255
|
3101
|
adamc@255
|
3102 | L.DStr (x, sgno, str) =>
|
adamc@255
|
3103 let
|
adamc@255
|
3104 val () = if x = "Basis" then
|
adamc@255
|
3105 raise Fail "Not allowed to redefine structure 'Basis'"
|
adamc@255
|
3106 else
|
adamc@255
|
3107 ()
|
adamc@255
|
3108
|
adamc@255
|
3109 val formal = Option.map (elabSgn (env, denv)) sgno
|
adamc@255
|
3110
|
adamc@255
|
3111 val (str', sgn', gs') =
|
adamc@255
|
3112 case formal of
|
adamc@255
|
3113 NONE =>
|
adamc@255
|
3114 let
|
adamc@255
|
3115 val (str', actual, gs') = elabStr (env, denv) str
|
adamc@255
|
3116 in
|
adamc@255
|
3117 (str', selfifyAt env {str = str', sgn = actual}, gs')
|
adamc@255
|
3118 end
|
adamc@255
|
3119 | SOME (formal, gs1) =>
|
adamc@255
|
3120 let
|
adamc@325
|
3121 val str = wildifyStr env (str, formal)
|
adamc@255
|
3122 val (str', actual, gs2) = elabStr (env, denv) str
|
adamc@255
|
3123 in
|
adamc@255
|
3124 subSgn (env, denv) (selfifyAt env {str = str', sgn = actual}) formal;
|
adamc@255
|
3125 (str', formal, enD gs1 @ gs2)
|
adamc@255
|
3126 end
|
adamc@255
|
3127
|
adamc@255
|
3128 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@255
|
3129 in
|
adamc@255
|
3130 case #1 (hnormSgn env sgn') of
|
adamc@255
|
3131 L'.SgnFun _ =>
|
adamc@255
|
3132 (case #1 str' of
|
adamc@255
|
3133 L'.StrFun _ => ()
|
adamc@255
|
3134 | _ => strError env (FunctorRebind loc))
|
adamc@255
|
3135 | _ => ();
|
adamc@255
|
3136
|
adamc@255
|
3137 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs))
|
adamc@255
|
3138 end
|
adamc@255
|
3139
|
adamc@255
|
3140 | L.DFfiStr (x, sgn) =>
|
adamc@255
|
3141 let
|
adamc@255
|
3142 val (sgn', gs') = elabSgn (env, denv) sgn
|
adamc@255
|
3143
|
adamc@255
|
3144 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@255
|
3145 in
|
adamc@255
|
3146 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, enD gs' @ gs))
|
adamc@255
|
3147 end
|
adamc@255
|
3148
|
adamc@255
|
3149 | L.DOpen (m, ms) =>
|
adamc@255
|
3150 (case E.lookupStr env m of
|
adamc@255
|
3151 NONE => (strError env (UnboundStr (loc, m));
|
adamc@255
|
3152 ([], (env, denv, gs)))
|
adamc@255
|
3153 | SOME (n, sgn) =>
|
adamc@255
|
3154 let
|
adamc@255
|
3155 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@255
|
3156 case E.projectStr env {str = str, sgn = sgn, field = m} of
|
adamc@255
|
3157 NONE => (strError env (UnboundStr (loc, m));
|
adamc@255
|
3158 (strerror, sgnerror))
|
adamc@255
|
3159 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@255
|
3160 ((L'.StrVar n, loc), sgn) ms
|
adamc@255
|
3161
|
adamc@255
|
3162 val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn}
|
adamc@255
|
3163 val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms}
|
adamc@255
|
3164 in
|
adamc@255
|
3165 (ds, (env', denv', gs))
|
adamc@255
|
3166 end)
|
adamc@255
|
3167
|
adamc@255
|
3168 | L.DConstraint (c1, c2) =>
|
adamc@255
|
3169 let
|
adamc@255
|
3170 val (c1', k1, gs1) = elabCon (env, denv) c1
|
adamc@255
|
3171 val (c2', k2, gs2) = elabCon (env, denv) c2
|
adamc@255
|
3172 val gs3 = D.prove env denv (c1', c2', loc)
|
adamc@255
|
3173
|
adamc@255
|
3174 val (denv', gs4) = D.assert env denv (c1', c2')
|
adamc@255
|
3175 in
|
adamc@255
|
3176 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
|
adamc@255
|
3177 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
|
adamc@255
|
3178
|
adamc@255
|
3179 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', enD gs1 @ enD gs2 @ enD gs3 @ enD gs4 @ gs))
|
adamc@255
|
3180 end
|
adamc@255
|
3181
|
adamc@255
|
3182 | L.DOpenConstraints (m, ms) =>
|
adamc@255
|
3183 let
|
adamc@255
|
3184 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
|
adamc@255
|
3185 in
|
adamc@255
|
3186 ([], (env, denv, gs))
|
adamc@255
|
3187 end
|
adamc@255
|
3188
|
adamc@255
|
3189 | L.DExport str =>
|
adamc@255
|
3190 let
|
adamc@255
|
3191 val (str', sgn, gs') = elabStr (env, denv) str
|
adamc@255
|
3192
|
adamc@255
|
3193 val sgn =
|
adamc@255
|
3194 case #1 (hnormSgn env sgn) of
|
adamc@255
|
3195 L'.SgnConst sgis =>
|
adamc@255
|
3196 let
|
adamc@255
|
3197 fun doOne (all as (sgi, _), env) =
|
adamc@255
|
3198 (case sgi of
|
adamc@255
|
3199 L'.SgiVal (x, n, t) =>
|
adamc@325
|
3200 let
|
adamc@325
|
3201 fun doPage (makeRes, ran) =
|
adamc@325
|
3202 case hnormCon (env, denv) ran of
|
adamc@325
|
3203 ((L'.CApp (tf, arg), _), []) =>
|
adamc@325
|
3204 (case (hnormCon (env, denv) tf, hnormCon (env, denv) arg) of
|
adamc@325
|
3205 (((L'.CModProj (basis, [], "transaction"), _), []),
|
adamc@325
|
3206 ((L'.CApp (tf, arg3), _), [])) =>
|
adamc@325
|
3207 (case (basis = !basis_r,
|
adamc@325
|
3208 hnormCon (env, denv) tf, hnormCon (env, denv) arg3) of
|
adamc@325
|
3209 (true,
|
adamc@325
|
3210 ((L'.CApp (tf, arg2), _), []),
|
adamc@325
|
3211 (((L'.CRecord (_, []), _), []))) =>
|
adamc@325
|
3212 (case (hnormCon (env, denv) tf) of
|
adamc@325
|
3213 ((L'.CApp (tf, arg1), _), []) =>
|
adamc@325
|
3214 (case (hnormCon (env, denv) tf,
|
adamc@325
|
3215 hnormCon (env, denv) arg1,
|
adamc@325
|
3216 hnormCon (env, denv) arg2) of
|
adamc@325
|
3217 ((tf, []), (arg1, []),
|
adamc@325
|
3218 ((L'.CRecord (_, []), _), [])) =>
|
adamc@325
|
3219 let
|
adamc@325
|
3220 val t = (L'.CApp (tf, arg1), loc)
|
adamc@325
|
3221 val t = (L'.CApp (t, arg2), loc)
|
adamc@325
|
3222 val t = (L'.CApp (t, arg3), loc)
|
adamc@325
|
3223 val t = (L'.CApp (
|
adamc@325
|
3224 (L'.CModProj
|
adamc@325
|
3225 (basis, [], "transaction"), loc),
|
adamc@325
|
3226 t), loc)
|
adamc@325
|
3227 in
|
adamc@325
|
3228 (L'.SgiVal (x, n, makeRes t), loc)
|
adamc@325
|
3229 end
|
adamc@325
|
3230 | _ => all)
|
adamc@325
|
3231 | _ => all)
|
adamc@325
|
3232 | _ => all)
|
adamc@325
|
3233 | _ => all)
|
adamc@325
|
3234 | _ => all
|
adamc@325
|
3235 in
|
adamc@325
|
3236 case hnormCon (env, denv) t of
|
adamc@325
|
3237 ((L'.TFun (dom, ran), _), []) =>
|
adamc@325
|
3238 (case hnormCon (env, denv) dom of
|
adamc@325
|
3239 ((L'.TRecord domR, _), []) =>
|
adamc@325
|
3240 doPage (fn t => (L'.TFun ((L'.TRecord domR,
|
adamc@325
|
3241 loc),
|
adamc@325
|
3242 t), loc), ran)
|
adamc@325
|
3243 | _ => all)
|
adamc@325
|
3244 | _ => doPage (fn t => t, t)
|
adamc@325
|
3245 end
|
adamc@255
|
3246 | _ => all,
|
adamc@255
|
3247 E.sgiBinds env all)
|
adamc@255
|
3248 in
|
adamc@255
|
3249 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
|
adamc@255
|
3250 end
|
adamc@255
|
3251 | _ => sgn
|
adamc@255
|
3252 in
|
adamc@255
|
3253 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs' @ gs))
|
adamc@255
|
3254 end
|
adamc@255
|
3255
|
adamc@255
|
3256 | L.DTable (x, c) =>
|
adamc@255
|
3257 let
|
adamc@255
|
3258 val (c', k, gs') = elabCon (env, denv) c
|
adamc@255
|
3259 val (env, n) = E.pushENamed env x (L'.CApp (tableOf (), c'), loc)
|
adamc@255
|
3260 in
|
adamc@255
|
3261 checkKind env c' k (L'.KRecord (L'.KType, loc), loc);
|
adamc@255
|
3262 ([(L'.DTable (!basis_r, x, n, c'), loc)], (env, denv, enD gs' @ gs))
|
adamc@255
|
3263 end
|
adamc@338
|
3264 | L.DSequence x =>
|
adamc@338
|
3265 let
|
adamc@338
|
3266 val (env, n) = E.pushENamed env x (sequenceOf ())
|
adamc@338
|
3267 in
|
adamc@338
|
3268 ([(L'.DSequence (!basis_r, x, n), loc)], (env, denv, gs))
|
adamc@338
|
3269 end
|
adamc@255
|
3270
|
adamc@255
|
3271 | L.DClass (x, c) =>
|
adamc@255
|
3272 let
|
adamc@255
|
3273 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
|
adamc@280
|
3274 val (c', ck, gs') = elabCon (env, denv) c
|
adamc@255
|
3275 val (env, n) = E.pushCNamed env x k (SOME c')
|
adamc@255
|
3276 val env = E.pushClass env n
|
adamc@255
|
3277 in
|
adamc@255
|
3278 checkKind env c' ck k;
|
adamc@280
|
3279 ([(L'.DClass (x, n, c'), loc)], (env, denv, enD gs' @ gs))
|
adamc@255
|
3280 end
|
adamc@271
|
3281
|
adamc@280
|
3282 | L.DDatabase s => ([(L'.DDatabase s, loc)], (env, denv, gs))
|
adamc@280
|
3283
|
adamc@459
|
3284 | L.DCookie (x, c) =>
|
adamc@459
|
3285 let
|
adamc@459
|
3286 val (c', k, gs') = elabCon (env, denv) c
|
adamc@459
|
3287 val (env, n) = E.pushENamed env x (L'.CApp (cookieOf (), c'), loc)
|
adamc@459
|
3288 in
|
adamc@459
|
3289 checkKind env c' k (L'.KType, loc);
|
adamc@459
|
3290 ([(L'.DCookie (!basis_r, x, n, c'), loc)], (env, denv, enD gs' @ gs))
|
adamc@459
|
3291 end
|
adamc@459
|
3292
|
adamc@280
|
3293 (*val tcs = List.filter (fn TypeClass _ => true | _ => false) (#3 (#2 r))*)
|
adamc@255
|
3294 in
|
adamc@280
|
3295 (*prefaces "elabDecl" [("e", SourcePrint.p_decl dAll),
|
adamc@513
|
3296 ("t", PD.string (LargeInt.toString (Time.toMilliseconds
|
adamc@513
|
3297 (Time.- (Time.now (), befor)))))];*)
|
adamc@280
|
3298
|
adamc@255
|
3299 r
|
adamc@255
|
3300 end
|
adamc@211
|
3301
|
adamc@83
|
3302 and elabStr (env, denv) (str, loc) =
|
adamc@31
|
3303 case str of
|
adamc@31
|
3304 L.StrConst ds =>
|
adamc@31
|
3305 let
|
adamc@88
|
3306 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
|
adamc@123
|
3307 val sgis = ListUtil.mapConcat sgiOfDecl ds'
|
adamc@63
|
3308
|
adamc@63
|
3309 val (sgis, _, _, _, _) =
|
adamc@64
|
3310 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
|
adamc@63
|
3311 case sgi of
|
adamc@64
|
3312 L'.SgiConAbs (x, n, k) =>
|
adamc@64
|
3313 let
|
adamc@64
|
3314 val (cons, x) =
|
adamc@64
|
3315 if SS.member (cons, x) then
|
adamc@64
|
3316 (cons, "?" ^ x)
|
adamc@64
|
3317 else
|
adamc@64
|
3318 (SS.add (cons, x), x)
|
adamc@64
|
3319 in
|
adamc@64
|
3320 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
3321 end
|
adamc@64
|
3322 | L'.SgiCon (x, n, k, c) =>
|
adamc@64
|
3323 let
|
adamc@64
|
3324 val (cons, x) =
|
adamc@64
|
3325 if SS.member (cons, x) then
|
adamc@64
|
3326 (cons, "?" ^ x)
|
adamc@64
|
3327 else
|
adamc@64
|
3328 (SS.add (cons, x), x)
|
adamc@64
|
3329 in
|
adamc@64
|
3330 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
3331 end
|
adamc@191
|
3332 | L'.SgiDatatype (x, n, xs, xncs) =>
|
adamc@156
|
3333 let
|
adamc@156
|
3334 val (cons, x) =
|
adamc@156
|
3335 if SS.member (cons, x) then
|
adamc@156
|
3336 (cons, "?" ^ x)
|
adamc@156
|
3337 else
|
adamc@156
|
3338 (SS.add (cons, x), x)
|
adamc@156
|
3339
|
adamc@156
|
3340 val (xncs, vals) =
|
adamc@156
|
3341 ListUtil.foldlMap
|
adamc@156
|
3342 (fn ((x, n, t), vals) =>
|
adamc@156
|
3343 if SS.member (vals, x) then
|
adamc@156
|
3344 (("?" ^ x, n, t), vals)
|
adamc@156
|
3345 else
|
adamc@156
|
3346 ((x, n, t), SS.add (vals, x)))
|
adamc@156
|
3347 vals xncs
|
adamc@156
|
3348 in
|
adamc@191
|
3349 ((L'.SgiDatatype (x, n, xs, xncs), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@156
|
3350 end
|
adamc@191
|
3351 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
|
adamc@156
|
3352 let
|
adamc@156
|
3353 val (cons, x) =
|
adamc@156
|
3354 if SS.member (cons, x) then
|
adamc@156
|
3355 (cons, "?" ^ x)
|
adamc@156
|
3356 else
|
adamc@156
|
3357 (SS.add (cons, x), x)
|
adamc@156
|
3358 in
|
adamc@191
|
3359 ((L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@156
|
3360 end
|
adamc@64
|
3361 | L'.SgiVal (x, n, c) =>
|
adamc@64
|
3362 let
|
adamc@64
|
3363 val (vals, x) =
|
adamc@64
|
3364 if SS.member (vals, x) then
|
adamc@64
|
3365 (vals, "?" ^ x)
|
adamc@64
|
3366 else
|
adamc@64
|
3367 (SS.add (vals, x), x)
|
adamc@64
|
3368 in
|
adamc@64
|
3369 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
3370 end
|
adamc@64
|
3371 | L'.SgiSgn (x, n, sgn) =>
|
adamc@64
|
3372 let
|
adamc@64
|
3373 val (sgns, x) =
|
adamc@64
|
3374 if SS.member (sgns, x) then
|
adamc@64
|
3375 (sgns, "?" ^ x)
|
adamc@64
|
3376 else
|
adamc@64
|
3377 (SS.add (sgns, x), x)
|
adamc@64
|
3378 in
|
adamc@64
|
3379 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
3380 end
|
adamc@64
|
3381
|
adamc@64
|
3382 | L'.SgiStr (x, n, sgn) =>
|
adamc@64
|
3383 let
|
adamc@64
|
3384 val (strs, x) =
|
adamc@64
|
3385 if SS.member (strs, x) then
|
adamc@64
|
3386 (strs, "?" ^ x)
|
adamc@64
|
3387 else
|
adamc@64
|
3388 (SS.add (strs, x), x)
|
adamc@64
|
3389 in
|
adamc@64
|
3390 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@88
|
3391 end
|
adamc@203
|
3392 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs)
|
adamc@211
|
3393 | L'.SgiClassAbs (x, n) =>
|
adamc@211
|
3394 let
|
adamc@211
|
3395 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
|
adamc@211
|
3396
|
adamc@211
|
3397 val (cons, x) =
|
adamc@211
|
3398 if SS.member (cons, x) then
|
adamc@211
|
3399 (cons, "?" ^ x)
|
adamc@211
|
3400 else
|
adamc@211
|
3401 (SS.add (cons, x), x)
|
adamc@211
|
3402 in
|
adamc@211
|
3403 ((L'.SgiClassAbs (x, n), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@211
|
3404 end
|
adamc@211
|
3405 | L'.SgiClass (x, n, c) =>
|
adamc@211
|
3406 let
|
adamc@211
|
3407 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
|
adamc@211
|
3408
|
adamc@211
|
3409 val (cons, x) =
|
adamc@211
|
3410 if SS.member (cons, x) then
|
adamc@211
|
3411 (cons, "?" ^ x)
|
adamc@211
|
3412 else
|
adamc@211
|
3413 (SS.add (cons, x), x)
|
adamc@211
|
3414 in
|
adamc@211
|
3415 ((L'.SgiClass (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@203
|
3416 end)
|
adamc@64
|
3417
|
adamc@63
|
3418 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
|
adamc@31
|
3419 in
|
adamc@83
|
3420 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
|
adamc@31
|
3421 end
|
adamc@31
|
3422 | L.StrVar x =>
|
adamc@31
|
3423 (case E.lookupStr env x of
|
adamc@31
|
3424 NONE =>
|
adamc@31
|
3425 (strError env (UnboundStr (loc, x));
|
adamc@83
|
3426 (strerror, sgnerror, []))
|
adamc@83
|
3427 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
|
adamc@34
|
3428 | L.StrProj (str, x) =>
|
adamc@34
|
3429 let
|
adamc@83
|
3430 val (str', sgn, gs) = elabStr (env, denv) str
|
adamc@34
|
3431 in
|
adamc@34
|
3432 case E.projectStr env {str = str', sgn = sgn, field = x} of
|
adamc@34
|
3433 NONE => (strError env (UnboundStr (loc, x));
|
adamc@83
|
3434 (strerror, sgnerror, []))
|
adamc@83
|
3435 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
|
adamc@34
|
3436 end
|
adamc@41
|
3437 | L.StrFun (m, dom, ranO, str) =>
|
adamc@41
|
3438 let
|
adamc@83
|
3439 val (dom', gs1) = elabSgn (env, denv) dom
|
adamc@41
|
3440 val (env', n) = E.pushStrNamed env m dom'
|
adamc@83
|
3441 val (str', actual, gs2) = elabStr (env', denv) str
|
adamc@41
|
3442
|
adamc@83
|
3443 val (formal, gs3) =
|
adamc@41
|
3444 case ranO of
|
adamc@83
|
3445 NONE => (actual, [])
|
adamc@41
|
3446 | SOME ran =>
|
adamc@41
|
3447 let
|
adamc@83
|
3448 val (ran', gs) = elabSgn (env', denv) ran
|
adamc@41
|
3449 in
|
adamc@86
|
3450 subSgn (env', denv) actual ran';
|
adamc@83
|
3451 (ran', gs)
|
adamc@41
|
3452 end
|
adamc@41
|
3453 in
|
adamc@41
|
3454 ((L'.StrFun (m, n, dom', formal, str'), loc),
|
adamc@83
|
3455 (L'.SgnFun (m, n, dom', formal), loc),
|
adamc@228
|
3456 enD gs1 @ gs2 @ enD gs3)
|
adamc@41
|
3457 end
|
adamc@44
|
3458 | L.StrApp (str1, str2) =>
|
adamc@44
|
3459 let
|
adamc@83
|
3460 val (str1', sgn1, gs1) = elabStr (env, denv) str1
|
adamc@325
|
3461 val str2 =
|
adamc@325
|
3462 case sgn1 of
|
adamc@325
|
3463 (L'.SgnFun (_, _, dom, _), _) =>
|
adamc@325
|
3464 wildifyStr env (str2, dom)
|
adamc@325
|
3465 | _ => str2
|
adamc@83
|
3466 val (str2', sgn2, gs2) = elabStr (env, denv) str2
|
adamc@44
|
3467 in
|
adamc@44
|
3468 case #1 (hnormSgn env sgn1) of
|
adamc@83
|
3469 L'.SgnError => (strerror, sgnerror, [])
|
adamc@44
|
3470 | L'.SgnFun (m, n, dom, ran) =>
|
adamc@86
|
3471 (subSgn (env, denv) sgn2 dom;
|
adamc@44
|
3472 case #1 (hnormSgn env ran) of
|
adamc@83
|
3473 L'.SgnError => (strerror, sgnerror, [])
|
adamc@44
|
3474 | L'.SgnConst sgis =>
|
adamc@44
|
3475 ((L'.StrApp (str1', str2'), loc),
|
adamc@83
|
3476 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
|
adamc@83
|
3477 gs1 @ gs2)
|
adamc@44
|
3478 | _ => raise Fail "Unable to hnormSgn in functor application")
|
adamc@44
|
3479 | _ => (strError env (NotFunctor sgn1);
|
adamc@83
|
3480 (strerror, sgnerror, []))
|
adamc@44
|
3481 end
|
adamc@31
|
3482
|
adamc@325
|
3483 fun elabFile basis topStr topSgn env file =
|
adamc@56
|
3484 let
|
adamc@83
|
3485 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
|
adamc@83
|
3486 val () = case gs of
|
adamc@83
|
3487 [] => ()
|
adamc@207
|
3488 | _ => (app (fn (_, env, _, c1, c2) =>
|
adamc@207
|
3489 prefaces "Unresolved"
|
adamc@207
|
3490 [("c1", p_con env c1),
|
adamc@207
|
3491 ("c2", p_con env c2)]) gs;
|
adamc@207
|
3492 raise Fail "Unresolved disjointness constraints in Basis")
|
adamc@83
|
3493
|
adamc@56
|
3494 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
|
adamc@210
|
3495 val () = basis_r := basis_n
|
adamc@56
|
3496
|
adamc@88
|
3497 val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn}
|
adamc@56
|
3498
|
adamc@56
|
3499 fun discoverC r x =
|
adamc@56
|
3500 case E.lookupC env' x of
|
adamc@56
|
3501 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
|
adamc@56
|
3502 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
|
adamc@56
|
3503 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
|
adamc@56
|
3504
|
adamc@56
|
3505 val () = discoverC int "int"
|
adamc@56
|
3506 val () = discoverC float "float"
|
adamc@56
|
3507 val () = discoverC string "string"
|
adamc@203
|
3508 val () = discoverC table "sql_table"
|
adamc@56
|
3509
|
adamc@325
|
3510 val (topSgn, gs) = elabSgn (env', D.empty) (L.SgnConst topSgn, ErrorMsg.dummySpan)
|
adamc@325
|
3511 val () = case gs of
|
adamc@325
|
3512 [] => ()
|
adamc@325
|
3513 | _ => raise Fail "Unresolved disjointness constraints in top.urs"
|
adamc@325
|
3514 val (topStr, topSgn', gs) = elabStr (env', D.empty) (L.StrConst topStr, ErrorMsg.dummySpan)
|
adamc@325
|
3515 val () = case gs of
|
adamc@325
|
3516 [] => ()
|
adamc@329
|
3517 | _ => app (fn Disjoint (loc, env, denv, c1, c2) =>
|
adamc@329
|
3518 (case D.prove env denv (c1, c2, loc) of
|
adamc@329
|
3519 [] => ()
|
adamc@329
|
3520 | _ =>
|
adamc@329
|
3521 (prefaces "Unresolved constraint in top.ur"
|
adamc@329
|
3522 [("loc", PD.string (ErrorMsg.spanToString loc)),
|
adamc@329
|
3523 ("c1", p_con env c1),
|
adamc@329
|
3524 ("c2", p_con env c2)];
|
adamc@478
|
3525 raise Fail "Unresolved constraint in top.ur"))
|
adamc@329
|
3526 | TypeClass _ => raise Fail "Unresolved type class constraint in top.ur") gs
|
adamc@325
|
3527 val () = subSgn (env', D.empty) topSgn' topSgn
|
adamc@325
|
3528
|
adamc@325
|
3529 val (env', top_n) = E.pushStrNamed env' "Top" topSgn
|
adamc@325
|
3530
|
adamc@325
|
3531 val (ds', (env', _)) = dopen (env', D.empty) {str = top_n, strs = [], sgn = topSgn}
|
adamc@325
|
3532
|
adamc@83
|
3533 fun elabDecl' (d, (env, gs)) =
|
adamc@76
|
3534 let
|
adamc@76
|
3535 val () = resetKunif ()
|
adamc@76
|
3536 val () = resetCunif ()
|
adamc@88
|
3537 val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs))
|
adamc@76
|
3538 in
|
adamc@76
|
3539 if ErrorMsg.anyErrors () then
|
adamc@76
|
3540 ()
|
adamc@76
|
3541 else (
|
adamc@76
|
3542 if List.exists kunifsInDecl ds then
|
adamc@91
|
3543 declError env (KunifsRemain ds)
|
adamc@76
|
3544 else
|
adamc@76
|
3545 ();
|
adamc@76
|
3546
|
adamc@76
|
3547 case ListUtil.search cunifsInDecl ds of
|
adamc@76
|
3548 NONE => ()
|
adamc@76
|
3549 | SOME loc =>
|
adamc@91
|
3550 declError env (CunifsRemain ds)
|
adamc@76
|
3551 );
|
adamc@76
|
3552
|
adamc@83
|
3553 (ds, (env, gs))
|
adamc@76
|
3554 end
|
adamc@76
|
3555
|
adamc@83
|
3556 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
|
adamc@56
|
3557 in
|
adamc@84
|
3558 if ErrorMsg.anyErrors () then
|
adamc@84
|
3559 ()
|
adamc@84
|
3560 else
|
adamc@228
|
3561 app (fn Disjoint (loc, env, denv, c1, c2) =>
|
adamc@228
|
3562 (case D.prove env denv (c1, c2, loc) of
|
adamc@228
|
3563 [] => ()
|
adamc@228
|
3564 | _ =>
|
adamc@228
|
3565 (ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
|
adamc@228
|
3566 eprefaces' [("Con 1", p_con env c1),
|
adamc@228
|
3567 ("Con 2", p_con env c2),
|
adamc@228
|
3568 ("Hnormed 1", p_con env (ElabOps.hnormCon env c1)),
|
adamc@228
|
3569 ("Hnormed 2", p_con env (ElabOps.hnormCon env c2))]))
|
adamc@228
|
3570 | TypeClass (env, c, r, loc) =>
|
adamc@349
|
3571 let
|
adamc@467
|
3572 val c = normClassKey env c
|
adamc@349
|
3573 in
|
adamc@349
|
3574 case E.resolveClass env c of
|
adamc@349
|
3575 SOME e => r := SOME e
|
adamc@349
|
3576 | NONE => expError env (Unresolvable (loc, c))
|
adamc@349
|
3577 end) gs;
|
adamc@83
|
3578
|
adamc@325
|
3579 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan)
|
adamc@325
|
3580 :: ds
|
adamc@325
|
3581 @ (L'.DStr ("Top", top_n, topSgn, topStr), ErrorMsg.dummySpan)
|
adamc@325
|
3582 :: ds' @ file
|
adamc@56
|
3583 end
|
adamc@2
|
3584
|
adamc@2
|
3585 end
|