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