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