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