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