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