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