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@62
|
40 structure SS = BinarySetFn(struct
|
adamc@62
|
41 type ord_key = string
|
adamc@62
|
42 val compare = String.compare
|
adamc@62
|
43 end)
|
adamc@62
|
44
|
adamc@2
|
45 fun elabExplicitness e =
|
adamc@2
|
46 case e of
|
adamc@2
|
47 L.Explicit => L'.Explicit
|
adamc@2
|
48 | L.Implicit => L'.Implicit
|
adamc@2
|
49
|
adamc@2
|
50 fun occursKind r =
|
adamc@76
|
51 U.Kind.exists (fn L'.KUnif (_, _, r') => r = r'
|
adamc@2
|
52 | _ => false)
|
adamc@2
|
53
|
adamc@3
|
54 datatype kunify_error =
|
adamc@2
|
55 KOccursCheckFailed of L'.kind * L'.kind
|
adamc@2
|
56 | KIncompatible of L'.kind * L'.kind
|
adamc@2
|
57
|
adamc@3
|
58 exception KUnify' of kunify_error
|
adamc@3
|
59
|
adamc@3
|
60 fun kunifyError err =
|
adamc@2
|
61 case err of
|
adamc@2
|
62 KOccursCheckFailed (k1, k2) =>
|
adamc@3
|
63 eprefaces "Kind occurs check failed"
|
adamc@3
|
64 [("Kind 1", p_kind k1),
|
adamc@3
|
65 ("Kind 2", p_kind k2)]
|
adamc@2
|
66 | KIncompatible (k1, k2) =>
|
adamc@3
|
67 eprefaces "Incompatible kinds"
|
adamc@3
|
68 [("Kind 1", p_kind k1),
|
adamc@3
|
69 ("Kind 2", p_kind k2)]
|
adamc@2
|
70
|
adamc@3
|
71 fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) =
|
adamc@2
|
72 let
|
adamc@3
|
73 fun err f = raise KUnify' (f (k1All, k2All))
|
adamc@2
|
74 in
|
adamc@2
|
75 case (k1, k2) of
|
adamc@2
|
76 (L'.KType, L'.KType) => ()
|
adamc@82
|
77 | (L'.KUnit, L'.KUnit) => ()
|
adamc@82
|
78
|
adamc@2
|
79 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
|
adamc@3
|
80 (unifyKinds' d1 d2;
|
adamc@3
|
81 unifyKinds' r1 r2)
|
adamc@2
|
82 | (L'.KName, L'.KName) => ()
|
adamc@3
|
83 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
|
adamc@2
|
84
|
adamc@2
|
85 | (L'.KError, _) => ()
|
adamc@2
|
86 | (_, L'.KError) => ()
|
adamc@2
|
87
|
adamc@76
|
88 | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' k1All k2All
|
adamc@76
|
89 | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' k1All k2All
|
adamc@2
|
90
|
adamc@76
|
91 | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) =>
|
adamc@2
|
92 if r1 = r2 then
|
adamc@2
|
93 ()
|
adamc@2
|
94 else
|
adamc@2
|
95 r1 := SOME k2All
|
adamc@2
|
96
|
adamc@76
|
97 | (L'.KUnif (_, _, r), _) =>
|
adamc@2
|
98 if occursKind r k2All then
|
adamc@2
|
99 err KOccursCheckFailed
|
adamc@2
|
100 else
|
adamc@2
|
101 r := SOME k2All
|
adamc@76
|
102 | (_, L'.KUnif (_, _, r)) =>
|
adamc@2
|
103 if occursKind r k1All then
|
adamc@2
|
104 err KOccursCheckFailed
|
adamc@2
|
105 else
|
adamc@2
|
106 r := SOME k1All
|
adamc@2
|
107
|
adamc@2
|
108 | _ => err KIncompatible
|
adamc@2
|
109 end
|
adamc@2
|
110
|
adamc@3
|
111 exception KUnify of L'.kind * L'.kind * kunify_error
|
adamc@3
|
112
|
adamc@3
|
113 fun unifyKinds k1 k2 =
|
adamc@3
|
114 unifyKinds' k1 k2
|
adamc@3
|
115 handle KUnify' err => raise KUnify (k1, k2, err)
|
adamc@3
|
116
|
adamc@3
|
117 datatype con_error =
|
adamc@3
|
118 UnboundCon of ErrorMsg.span * string
|
adamc@34
|
119 | UnboundStrInCon of ErrorMsg.span * string
|
adamc@3
|
120 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
|
adamc@67
|
121 | DuplicateField of ErrorMsg.span * string
|
adamc@3
|
122
|
adamc@5
|
123 fun conError env err =
|
adamc@3
|
124 case err of
|
adamc@3
|
125 UnboundCon (loc, s) =>
|
adamc@3
|
126 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
|
adamc@34
|
127 | UnboundStrInCon (loc, s) =>
|
adamc@34
|
128 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
|
adamc@3
|
129 | WrongKind (c, k1, k2, kerr) =>
|
adamc@3
|
130 (ErrorMsg.errorAt (#2 c) "Wrong kind";
|
adamc@5
|
131 eprefaces' [("Constructor", p_con env c),
|
adamc@5
|
132 ("Have kind", p_kind k1),
|
adamc@5
|
133 ("Need kind", p_kind k2)];
|
adamc@3
|
134 kunifyError kerr)
|
adamc@67
|
135 | DuplicateField (loc, s) =>
|
adamc@67
|
136 ErrorMsg.errorAt loc ("Duplicate record field " ^ s)
|
adamc@3
|
137
|
adamc@5
|
138 fun checkKind env c k1 k2 =
|
adamc@3
|
139 unifyKinds k1 k2
|
adamc@3
|
140 handle KUnify (k1, k2, err) =>
|
adamc@5
|
141 conError env (WrongKind (c, k1, k2, err))
|
adamc@3
|
142
|
adamc@3
|
143 val dummy = ErrorMsg.dummySpan
|
adamc@3
|
144
|
adamc@3
|
145 val ktype = (L'.KType, dummy)
|
adamc@3
|
146 val kname = (L'.KName, dummy)
|
adamc@12
|
147 val ktype_record = (L'.KRecord ktype, dummy)
|
adamc@3
|
148
|
adamc@3
|
149 val cerror = (L'.CError, dummy)
|
adamc@3
|
150 val kerror = (L'.KError, dummy)
|
adamc@10
|
151 val eerror = (L'.EError, dummy)
|
adamc@31
|
152 val sgnerror = (L'.SgnError, dummy)
|
adamc@31
|
153 val strerror = (L'.StrError, dummy)
|
adamc@3
|
154
|
adamc@56
|
155 val int = ref cerror
|
adamc@56
|
156 val float = ref cerror
|
adamc@56
|
157 val string = ref cerror
|
adamc@56
|
158
|
adamc@3
|
159 local
|
adamc@3
|
160 val count = ref 0
|
adamc@3
|
161 in
|
adamc@3
|
162
|
adamc@3
|
163 fun resetKunif () = count := 0
|
adamc@3
|
164
|
adamc@76
|
165 fun kunif loc =
|
adamc@3
|
166 let
|
adamc@3
|
167 val n = !count
|
adamc@3
|
168 val s = if n <= 26 then
|
adamc@3
|
169 str (chr (ord #"A" + n))
|
adamc@3
|
170 else
|
adamc@3
|
171 "U" ^ Int.toString (n - 26)
|
adamc@3
|
172 in
|
adamc@3
|
173 count := n + 1;
|
adamc@76
|
174 (L'.KUnif (loc, s, ref NONE), dummy)
|
adamc@3
|
175 end
|
adamc@3
|
176
|
adamc@3
|
177 end
|
adamc@3
|
178
|
adamc@10
|
179 local
|
adamc@10
|
180 val count = ref 0
|
adamc@10
|
181 in
|
adamc@10
|
182
|
adamc@10
|
183 fun resetCunif () = count := 0
|
adamc@10
|
184
|
adamc@76
|
185 fun cunif (loc, k) =
|
adamc@10
|
186 let
|
adamc@10
|
187 val n = !count
|
adamc@10
|
188 val s = if n <= 26 then
|
adamc@10
|
189 str (chr (ord #"A" + n))
|
adamc@10
|
190 else
|
adamc@10
|
191 "U" ^ Int.toString (n - 26)
|
adamc@10
|
192 in
|
adamc@10
|
193 count := n + 1;
|
adamc@76
|
194 (L'.CUnif (loc, k, s, ref NONE), dummy)
|
adamc@10
|
195 end
|
adamc@10
|
196
|
adamc@10
|
197 end
|
adamc@10
|
198
|
adamc@18
|
199 fun elabKind (k, loc) =
|
adamc@18
|
200 case k of
|
adamc@18
|
201 L.KType => (L'.KType, loc)
|
adamc@18
|
202 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
|
adamc@18
|
203 | L.KName => (L'.KName, loc)
|
adamc@18
|
204 | L.KRecord k => (L'.KRecord (elabKind k), loc)
|
adamc@82
|
205 | L.KUnit => (L'.KUnit, loc)
|
adamc@76
|
206 | L.KWild => kunif loc
|
adamc@18
|
207
|
adamc@67
|
208 fun foldKind (dom, ran, loc)=
|
adamc@67
|
209 (L'.KArrow ((L'.KArrow ((L'.KName, loc),
|
adamc@67
|
210 (L'.KArrow (dom,
|
adamc@67
|
211 (L'.KArrow (ran, ran), loc)), loc)), loc),
|
adamc@67
|
212 (L'.KArrow (ran,
|
adamc@67
|
213 (L'.KArrow ((L'.KRecord dom, loc),
|
adamc@67
|
214 ran), loc)), loc)), loc)
|
adamc@67
|
215
|
adamc@3
|
216 fun elabCon env (c, loc) =
|
adamc@3
|
217 case c of
|
adamc@3
|
218 L.CAnnot (c, k) =>
|
adamc@3
|
219 let
|
adamc@3
|
220 val k' = elabKind k
|
adamc@3
|
221 val (c', ck) = elabCon env c
|
adamc@3
|
222 in
|
adamc@5
|
223 checkKind env c' ck k';
|
adamc@3
|
224 (c', k')
|
adamc@3
|
225 end
|
adamc@3
|
226
|
adamc@3
|
227 | L.TFun (t1, t2) =>
|
adamc@3
|
228 let
|
adamc@3
|
229 val (t1', k1) = elabCon env t1
|
adamc@3
|
230 val (t2', k2) = elabCon env t2
|
adamc@3
|
231 in
|
adamc@5
|
232 checkKind env t1' k1 ktype;
|
adamc@5
|
233 checkKind env t2' k2 ktype;
|
adamc@3
|
234 ((L'.TFun (t1', t2'), loc), ktype)
|
adamc@3
|
235 end
|
adamc@3
|
236 | L.TCFun (e, x, k, t) =>
|
adamc@3
|
237 let
|
adamc@3
|
238 val e' = elabExplicitness e
|
adamc@3
|
239 val k' = elabKind k
|
adamc@3
|
240 val env' = E.pushCRel env x k'
|
adamc@3
|
241 val (t', tk) = elabCon env' t
|
adamc@3
|
242 in
|
adamc@5
|
243 checkKind env t' tk ktype;
|
adamc@3
|
244 ((L'.TCFun (e', x, k', t'), loc), ktype)
|
adamc@3
|
245 end
|
adamc@3
|
246 | L.TRecord c =>
|
adamc@3
|
247 let
|
adamc@3
|
248 val (c', ck) = elabCon env c
|
adamc@3
|
249 val k = (L'.KRecord ktype, loc)
|
adamc@3
|
250 in
|
adamc@5
|
251 checkKind env c' ck k;
|
adamc@3
|
252 ((L'.TRecord c', loc), ktype)
|
adamc@3
|
253 end
|
adamc@3
|
254
|
adamc@34
|
255 | L.CVar ([], s) =>
|
adamc@3
|
256 (case E.lookupC env s of
|
adamc@9
|
257 E.NotBound =>
|
adamc@5
|
258 (conError env (UnboundCon (loc, s));
|
adamc@3
|
259 (cerror, kerror))
|
adamc@9
|
260 | E.Rel (n, k) =>
|
adamc@3
|
261 ((L'.CRel n, loc), k)
|
adamc@9
|
262 | E.Named (n, k) =>
|
adamc@3
|
263 ((L'.CNamed n, loc), k))
|
adamc@34
|
264 | L.CVar (m1 :: ms, s) =>
|
adamc@34
|
265 (case E.lookupStr env m1 of
|
adamc@42
|
266 NONE => (conError env (UnboundStrInCon (loc, m1));
|
adamc@34
|
267 (cerror, kerror))
|
adamc@34
|
268 | SOME (n, sgn) =>
|
adamc@34
|
269 let
|
adamc@34
|
270 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
271 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@34
|
272 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@34
|
273 (strerror, sgnerror))
|
adamc@34
|
274 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
275 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
276
|
adamc@34
|
277 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
|
adamc@34
|
278 NONE => (conError env (UnboundCon (loc, s));
|
adamc@34
|
279 kerror)
|
adamc@34
|
280 | SOME (k, _) => k
|
adamc@34
|
281 in
|
adamc@34
|
282 ((L'.CModProj (n, ms, s), loc), k)
|
adamc@34
|
283 end)
|
adamc@34
|
284
|
adamc@3
|
285 | L.CApp (c1, c2) =>
|
adamc@3
|
286 let
|
adamc@3
|
287 val (c1', k1) = elabCon env c1
|
adamc@3
|
288 val (c2', k2) = elabCon env c2
|
adamc@76
|
289 val dom = kunif loc
|
adamc@76
|
290 val ran = kunif loc
|
adamc@3
|
291 in
|
adamc@5
|
292 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
|
adamc@5
|
293 checkKind env c2' k2 dom;
|
adamc@3
|
294 ((L'.CApp (c1', c2'), loc), ran)
|
adamc@3
|
295 end
|
adamc@67
|
296 | L.CAbs (x, ko, t) =>
|
adamc@3
|
297 let
|
adamc@67
|
298 val k' = case ko of
|
adamc@76
|
299 NONE => kunif loc
|
adamc@67
|
300 | SOME k => elabKind k
|
adamc@3
|
301 val env' = E.pushCRel env x k'
|
adamc@3
|
302 val (t', tk) = elabCon env' t
|
adamc@3
|
303 in
|
adamc@8
|
304 ((L'.CAbs (x, k', t'), loc),
|
adamc@3
|
305 (L'.KArrow (k', tk), loc))
|
adamc@3
|
306 end
|
adamc@3
|
307
|
adamc@3
|
308 | L.CName s =>
|
adamc@3
|
309 ((L'.CName s, loc), kname)
|
adamc@3
|
310
|
adamc@3
|
311 | L.CRecord xcs =>
|
adamc@3
|
312 let
|
adamc@76
|
313 val k = kunif loc
|
adamc@3
|
314
|
adamc@3
|
315 val xcs' = map (fn (x, c) =>
|
adamc@3
|
316 let
|
adamc@3
|
317 val (x', xk) = elabCon env x
|
adamc@3
|
318 val (c', ck) = elabCon env c
|
adamc@3
|
319 in
|
adamc@5
|
320 checkKind env x' xk kname;
|
adamc@5
|
321 checkKind env c' ck k;
|
adamc@3
|
322 (x', c')
|
adamc@3
|
323 end) xcs
|
adamc@67
|
324
|
adamc@67
|
325 val rc = (L'.CRecord (k, xcs'), loc)
|
adamc@67
|
326 (* Add duplicate field checking later. *)
|
adamc@3
|
327 in
|
adamc@67
|
328 (rc, (L'.KRecord k, loc))
|
adamc@3
|
329 end
|
adamc@3
|
330 | L.CConcat (c1, c2) =>
|
adamc@3
|
331 let
|
adamc@3
|
332 val (c1', k1) = elabCon env c1
|
adamc@3
|
333 val (c2', k2) = elabCon env c2
|
adamc@76
|
334 val ku = kunif loc
|
adamc@3
|
335 val k = (L'.KRecord ku, loc)
|
adamc@3
|
336 in
|
adamc@82
|
337 case D.prove env D.empty (c1', c2', loc) of
|
adamc@82
|
338 [] => ()
|
adamc@82
|
339 | _ => raise Fail "Can't prove disjointness";
|
adamc@82
|
340
|
adamc@5
|
341 checkKind env c1' k1 k;
|
adamc@5
|
342 checkKind env c2' k2 k;
|
adamc@3
|
343 ((L'.CConcat (c1', c2'), loc), k)
|
adamc@3
|
344 end
|
adamc@67
|
345 | L.CFold =>
|
adamc@67
|
346 let
|
adamc@76
|
347 val dom = kunif loc
|
adamc@76
|
348 val ran = kunif loc
|
adamc@67
|
349 in
|
adamc@67
|
350 ((L'.CFold (dom, ran), loc),
|
adamc@67
|
351 foldKind (dom, ran, loc))
|
adamc@67
|
352 end
|
adamc@3
|
353
|
adamc@82
|
354 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc))
|
adamc@82
|
355
|
adamc@18
|
356 | L.CWild k =>
|
adamc@18
|
357 let
|
adamc@18
|
358 val k' = elabKind k
|
adamc@18
|
359 in
|
adamc@76
|
360 (cunif (loc, k'), k')
|
adamc@18
|
361 end
|
adamc@18
|
362
|
adamc@6
|
363 fun kunifsRemain k =
|
adamc@6
|
364 case k of
|
adamc@76
|
365 L'.KUnif (_, _, ref NONE) => true
|
adamc@6
|
366 | _ => false
|
adamc@10
|
367 fun cunifsRemain c =
|
adamc@10
|
368 case c of
|
adamc@76
|
369 L'.CUnif (loc, _, _, ref NONE) => SOME loc
|
adamc@76
|
370 | _ => NONE
|
adamc@6
|
371
|
adamc@76
|
372 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
|
adamc@76
|
373 con = fn _ => false,
|
adamc@76
|
374 exp = fn _ => false,
|
adamc@76
|
375 sgn_item = fn _ => false,
|
adamc@76
|
376 sgn = fn _ => false,
|
adamc@76
|
377 str = fn _ => false,
|
adamc@76
|
378 decl = fn _ => false}
|
adamc@10
|
379
|
adamc@76
|
380 val cunifsInDecl = U.Decl.search {kind = fn _ => NONE,
|
adamc@76
|
381 con = cunifsRemain,
|
adamc@76
|
382 exp = fn _ => NONE,
|
adamc@76
|
383 sgn_item = fn _ => NONE,
|
adamc@76
|
384 sgn = fn _ => NONE,
|
adamc@76
|
385 str = fn _ => NONE,
|
adamc@76
|
386 decl = fn _ => NONE}
|
adamc@10
|
387
|
adamc@10
|
388 fun occursCon r =
|
adamc@10
|
389 U.Con.exists {kind = fn _ => false,
|
adamc@76
|
390 con = fn L'.CUnif (_, _, _, r') => r = r'
|
adamc@10
|
391 | _ => false}
|
adamc@10
|
392
|
adamc@10
|
393 datatype cunify_error =
|
adamc@10
|
394 CKind of L'.kind * L'.kind * kunify_error
|
adamc@10
|
395 | COccursCheckFailed of L'.con * L'.con
|
adamc@10
|
396 | CIncompatible of L'.con * L'.con
|
adamc@10
|
397 | CExplicitness of L'.con * L'.con
|
adamc@12
|
398 | CKindof of L'.con
|
adamc@12
|
399 | CRecordFailure
|
adamc@10
|
400
|
adamc@10
|
401 exception CUnify' of cunify_error
|
adamc@10
|
402
|
adamc@10
|
403 fun cunifyError env err =
|
adamc@10
|
404 case err of
|
adamc@10
|
405 CKind (k1, k2, kerr) =>
|
adamc@10
|
406 (eprefaces "Kind unification failure"
|
adamc@10
|
407 [("Kind 1", p_kind k1),
|
adamc@10
|
408 ("Kind 2", p_kind k2)];
|
adamc@10
|
409 kunifyError kerr)
|
adamc@10
|
410 | COccursCheckFailed (c1, c2) =>
|
adamc@10
|
411 eprefaces "Constructor occurs check failed"
|
adamc@10
|
412 [("Con 1", p_con env c1),
|
adamc@10
|
413 ("Con 2", p_con env c2)]
|
adamc@10
|
414 | CIncompatible (c1, c2) =>
|
adamc@10
|
415 eprefaces "Incompatible constructors"
|
adamc@10
|
416 [("Con 1", p_con env c1),
|
adamc@10
|
417 ("Con 2", p_con env c2)]
|
adamc@10
|
418 | CExplicitness (c1, c2) =>
|
adamc@10
|
419 eprefaces "Differing constructor function explicitness"
|
adamc@10
|
420 [("Con 1", p_con env c1),
|
adamc@10
|
421 ("Con 2", p_con env c2)]
|
adamc@12
|
422 | CKindof c =>
|
adamc@12
|
423 eprefaces "Kind unification variable blocks kindof calculation"
|
adamc@12
|
424 [("Con", p_con env c)]
|
adamc@12
|
425 | CRecordFailure =>
|
adamc@12
|
426 eprefaces "Can't unify record constructors" []
|
adamc@10
|
427
|
adamc@13
|
428 exception SynUnif = E.SynUnif
|
adamc@12
|
429
|
adamc@81
|
430 open ElabOps
|
adamc@43
|
431
|
adamc@12
|
432 type record_summary = {
|
adamc@12
|
433 fields : (L'.con * L'.con) list,
|
adamc@12
|
434 unifs : (L'.con * L'.con option ref) list,
|
adamc@12
|
435 others : L'.con list
|
adamc@12
|
436 }
|
adamc@12
|
437
|
adamc@12
|
438 fun summaryToCon {fields, unifs, others} =
|
adamc@12
|
439 let
|
adamc@12
|
440 val c = (L'.CRecord (ktype, []), dummy)
|
adamc@12
|
441 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
|
adamc@12
|
442 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
|
adamc@12
|
443 in
|
adamc@12
|
444 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
|
adamc@12
|
445 end
|
adamc@12
|
446
|
adamc@12
|
447 fun p_summary env s = p_con env (summaryToCon s)
|
adamc@12
|
448
|
adamc@12
|
449 exception CUnify of L'.con * L'.con * cunify_error
|
adamc@12
|
450
|
adamc@12
|
451 fun hnormKind (kAll as (k, _)) =
|
adamc@12
|
452 case k of
|
adamc@76
|
453 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
|
adamc@12
|
454 | _ => kAll
|
adamc@12
|
455
|
adamc@12
|
456 fun kindof env (c, loc) =
|
adamc@12
|
457 case c of
|
adamc@12
|
458 L'.TFun _ => ktype
|
adamc@12
|
459 | L'.TCFun _ => ktype
|
adamc@12
|
460 | L'.TRecord _ => ktype
|
adamc@12
|
461
|
adamc@12
|
462 | L'.CRel xn => #2 (E.lookupCRel env xn)
|
adamc@12
|
463 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
|
adamc@34
|
464 | L'.CModProj (n, ms, x) =>
|
adamc@34
|
465 let
|
adamc@34
|
466 val (_, sgn) = E.lookupStrNamed env n
|
adamc@34
|
467 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
468 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@34
|
469 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@34
|
470 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
471 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
472 in
|
adamc@34
|
473 case E.projectCon env {sgn = sgn, str = str, field = x} of
|
adamc@34
|
474 NONE => raise Fail "kindof: Unknown con in structure"
|
adamc@34
|
475 | SOME (k, _) => k
|
adamc@34
|
476 end
|
adamc@34
|
477
|
adamc@12
|
478 | L'.CApp (c, _) =>
|
adamc@12
|
479 (case #1 (hnormKind (kindof env c)) of
|
adamc@12
|
480 L'.KArrow (_, k) => k
|
adamc@12
|
481 | L'.KError => kerror
|
adamc@12
|
482 | _ => raise CUnify' (CKindof c))
|
adamc@12
|
483 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
|
adamc@12
|
484
|
adamc@12
|
485 | L'.CName _ => kname
|
adamc@12
|
486
|
adamc@12
|
487 | L'.CRecord (k, _) => (L'.KRecord k, loc)
|
adamc@12
|
488 | L'.CConcat (c, _) => kindof env c
|
adamc@67
|
489 | L'.CFold (dom, ran) => foldKind (dom, ran, loc)
|
adamc@12
|
490
|
adamc@82
|
491 | L'.CUnit => (L'.KUnit, loc)
|
adamc@82
|
492
|
adamc@12
|
493 | L'.CError => kerror
|
adamc@76
|
494 | L'.CUnif (_, k, _, _) => k
|
adamc@12
|
495
|
adamc@12
|
496 fun unifyRecordCons env (c1, c2) =
|
adamc@12
|
497 let
|
adamc@12
|
498 val k1 = kindof env c1
|
adamc@12
|
499 val k2 = kindof env c2
|
adamc@12
|
500 in
|
adamc@12
|
501 unifyKinds k1 k2;
|
adamc@12
|
502 unifySummaries env (k1, recordSummary env c1, recordSummary env c2)
|
adamc@12
|
503 end
|
adamc@12
|
504
|
adamc@12
|
505 and recordSummary env c : record_summary =
|
adamc@12
|
506 case hnormCon env c of
|
adamc@12
|
507 (L'.CRecord (_, xcs), _) => {fields = xcs, unifs = [], others = []}
|
adamc@12
|
508 | (L'.CConcat (c1, c2), _) =>
|
adamc@12
|
509 let
|
adamc@12
|
510 val s1 = recordSummary env c1
|
adamc@12
|
511 val s2 = recordSummary env c2
|
adamc@12
|
512 in
|
adamc@12
|
513 {fields = #fields s1 @ #fields s2,
|
adamc@12
|
514 unifs = #unifs s1 @ #unifs s2,
|
adamc@12
|
515 others = #others s1 @ #others s2}
|
adamc@12
|
516 end
|
adamc@76
|
517 | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary env c
|
adamc@76
|
518 | c' as (L'.CUnif (_, _, _, r), _) => {fields = [], unifs = [(c', r)], others = []}
|
adamc@12
|
519 | c' => {fields = [], unifs = [], others = [c']}
|
adamc@12
|
520
|
adamc@12
|
521 and consEq env (c1, c2) =
|
adamc@12
|
522 (unifyCons env c1 c2;
|
adamc@12
|
523 true)
|
adamc@12
|
524 handle CUnify _ => false
|
adamc@12
|
525
|
adamc@80
|
526 and consNeq env (c1, c2) =
|
adamc@80
|
527 case (#1 (hnormCon env c1), #1 (hnormCon env c2)) of
|
adamc@80
|
528 (L'.CName x1, L'.CName x2) => x1 <> x2
|
adamc@80
|
529 | _ => false
|
adamc@80
|
530
|
adamc@12
|
531 and unifySummaries env (k, s1 : record_summary, s2 : record_summary) =
|
adamc@12
|
532 let
|
adamc@14
|
533 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
|
adamc@14
|
534 ("#2", p_summary env s2)]*)
|
adamc@12
|
535
|
adamc@12
|
536 fun eatMatching p (ls1, ls2) =
|
adamc@12
|
537 let
|
adamc@12
|
538 fun em (ls1, ls2, passed1) =
|
adamc@12
|
539 case ls1 of
|
adamc@12
|
540 [] => (rev passed1, ls2)
|
adamc@12
|
541 | h1 :: t1 =>
|
adamc@12
|
542 let
|
adamc@12
|
543 fun search (ls2', passed2) =
|
adamc@12
|
544 case ls2' of
|
adamc@12
|
545 [] => em (t1, ls2, h1 :: passed1)
|
adamc@12
|
546 | h2 :: t2 =>
|
adamc@12
|
547 if p (h1, h2) then
|
adamc@12
|
548 em (t1, List.revAppend (passed2, t2), passed1)
|
adamc@12
|
549 else
|
adamc@12
|
550 search (t2, h2 :: passed2)
|
adamc@12
|
551 in
|
adamc@12
|
552 search (ls2, [])
|
adamc@12
|
553 end
|
adamc@12
|
554 in
|
adamc@12
|
555 em (ls1, ls2, [])
|
adamc@12
|
556 end
|
adamc@12
|
557
|
adamc@12
|
558 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
|
adamc@80
|
559 not (consNeq env (x1, x2))
|
adamc@80
|
560 andalso consEq env (c1, c2)
|
adamc@80
|
561 andalso consEq env (x1, x2))
|
adamc@79
|
562 (#fields s1, #fields s2)
|
adamc@14
|
563 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
|
adamc@14
|
564 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
|
adamc@12
|
565 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
|
adamc@12
|
566 val (others1, others2) = eatMatching (consEq env) (#others s1, #others s2)
|
adamc@12
|
567
|
adamc@12
|
568 fun unifFields (fs, others, unifs) =
|
adamc@12
|
569 case (fs, others, unifs) of
|
adamc@12
|
570 ([], [], _) => ([], [], unifs)
|
adamc@12
|
571 | (_, _, []) => (fs, others, [])
|
adamc@12
|
572 | (_, _, (_, r) :: rest) =>
|
adamc@12
|
573 let
|
adamc@12
|
574 val r' = ref NONE
|
adamc@76
|
575 val cr' = (L'.CUnif (dummy, k, "recd", r'), dummy)
|
adamc@12
|
576
|
adamc@12
|
577 val prefix = case (fs, others) of
|
adamc@12
|
578 ([], other :: others) =>
|
adamc@12
|
579 List.foldl (fn (other, c) =>
|
adamc@12
|
580 (L'.CConcat (c, other), dummy))
|
adamc@12
|
581 other others
|
adamc@12
|
582 | (fs, []) =>
|
adamc@12
|
583 (L'.CRecord (k, fs), dummy)
|
adamc@12
|
584 | (fs, others) =>
|
adamc@12
|
585 List.foldl (fn (other, c) =>
|
adamc@12
|
586 (L'.CConcat (c, other), dummy))
|
adamc@12
|
587 (L'.CRecord (k, fs), dummy) others
|
adamc@12
|
588 in
|
adamc@12
|
589 r := SOME (L'.CConcat (prefix, cr'), dummy);
|
adamc@12
|
590 ([], [], (cr', r') :: rest)
|
adamc@12
|
591 end
|
adamc@12
|
592
|
adamc@12
|
593 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
|
adamc@12
|
594 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
|
adamc@12
|
595
|
adamc@12
|
596 val clear1 = case (fs1, others1) of
|
adamc@12
|
597 ([], []) => true
|
adamc@12
|
598 | _ => false
|
adamc@12
|
599 val clear2 = case (fs2, others2) of
|
adamc@12
|
600 ([], []) => true
|
adamc@12
|
601 | _ => false
|
adamc@12
|
602 val empty = (L'.CRecord (k, []), dummy)
|
adamc@12
|
603 fun pairOffUnifs (unifs1, unifs2) =
|
adamc@12
|
604 case (unifs1, unifs2) of
|
adamc@12
|
605 ([], _) =>
|
adamc@12
|
606 if clear1 then
|
adamc@12
|
607 List.app (fn (_, r) => r := SOME empty) unifs2
|
adamc@12
|
608 else
|
adamc@12
|
609 raise CUnify' CRecordFailure
|
adamc@12
|
610 | (_, []) =>
|
adamc@12
|
611 if clear2 then
|
adamc@12
|
612 List.app (fn (_, r) => r := SOME empty) unifs1
|
adamc@12
|
613 else
|
adamc@12
|
614 raise CUnify' CRecordFailure
|
adamc@12
|
615 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
|
adamc@12
|
616 (r2 := SOME c1;
|
adamc@12
|
617 pairOffUnifs (rest1, rest2))
|
adamc@12
|
618 in
|
adamc@12
|
619 pairOffUnifs (unifs1, unifs2)
|
adamc@12
|
620 end
|
adamc@12
|
621
|
adamc@11
|
622
|
adamc@12
|
623 and unifyCons' env c1 c2 =
|
adamc@11
|
624 unifyCons'' env (hnormCon env c1) (hnormCon env c2)
|
adamc@11
|
625
|
adamc@11
|
626 and unifyCons'' env (c1All as (c1, _)) (c2All as (c2, _)) =
|
adamc@10
|
627 let
|
adamc@10
|
628 fun err f = raise CUnify' (f (c1All, c2All))
|
adamc@12
|
629
|
adamc@12
|
630 fun isRecord () = unifyRecordCons env (c1All, c2All)
|
adamc@10
|
631 in
|
adamc@10
|
632 case (c1, c2) of
|
adamc@10
|
633 (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
|
adamc@10
|
634 (unifyCons' env d1 d2;
|
adamc@10
|
635 unifyCons' env r1 r2)
|
adamc@10
|
636 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
|
adamc@10
|
637 if expl1 <> expl2 then
|
adamc@10
|
638 err CExplicitness
|
adamc@10
|
639 else
|
adamc@10
|
640 (unifyKinds d1 d2;
|
adamc@10
|
641 unifyCons' (E.pushCRel env x1 d1) r1 r2)
|
adamc@10
|
642 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env r1 r2
|
adamc@10
|
643
|
adamc@10
|
644 | (L'.CRel n1, L'.CRel n2) =>
|
adamc@10
|
645 if n1 = n2 then
|
adamc@10
|
646 ()
|
adamc@10
|
647 else
|
adamc@10
|
648 err CIncompatible
|
adamc@10
|
649 | (L'.CNamed n1, L'.CNamed n2) =>
|
adamc@10
|
650 if n1 = n2 then
|
adamc@10
|
651 ()
|
adamc@10
|
652 else
|
adamc@10
|
653 err CIncompatible
|
adamc@10
|
654
|
adamc@10
|
655 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
|
adamc@10
|
656 (unifyCons' env d1 d2;
|
adamc@10
|
657 unifyCons' env r1 r2)
|
adamc@10
|
658 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
|
adamc@10
|
659 (unifyKinds k1 k2;
|
adamc@10
|
660 unifyCons' (E.pushCRel env x1 k1) c1 c2)
|
adamc@10
|
661
|
adamc@10
|
662 | (L'.CName n1, L'.CName n2) =>
|
adamc@10
|
663 if n1 = n2 then
|
adamc@10
|
664 ()
|
adamc@10
|
665 else
|
adamc@10
|
666 err CIncompatible
|
adamc@10
|
667
|
adamc@34
|
668 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
|
adamc@34
|
669 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
|
adamc@34
|
670 ()
|
adamc@34
|
671 else
|
adamc@34
|
672 err CIncompatible
|
adamc@34
|
673
|
adamc@10
|
674 | (L'.CError, _) => ()
|
adamc@10
|
675 | (_, L'.CError) => ()
|
adamc@10
|
676
|
adamc@76
|
677 | (L'.CUnif (_, _, _, ref (SOME c1All)), _) => unifyCons' env c1All c2All
|
adamc@76
|
678 | (_, L'.CUnif (_, _, _, ref (SOME c2All))) => unifyCons' env c1All c2All
|
adamc@10
|
679
|
adamc@76
|
680 | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) =>
|
adamc@10
|
681 if r1 = r2 then
|
adamc@10
|
682 ()
|
adamc@10
|
683 else
|
adamc@10
|
684 (unifyKinds k1 k2;
|
adamc@10
|
685 r1 := SOME c2All)
|
adamc@10
|
686
|
adamc@76
|
687 | (L'.CUnif (_, _, _, r), _) =>
|
adamc@10
|
688 if occursCon r c2All then
|
adamc@10
|
689 err COccursCheckFailed
|
adamc@10
|
690 else
|
adamc@10
|
691 r := SOME c2All
|
adamc@76
|
692 | (_, L'.CUnif (_, _, _, r)) =>
|
adamc@10
|
693 if occursCon r c1All then
|
adamc@10
|
694 err COccursCheckFailed
|
adamc@10
|
695 else
|
adamc@10
|
696 r := SOME c1All
|
adamc@10
|
697
|
adamc@12
|
698 | (L'.CRecord _, _) => isRecord ()
|
adamc@12
|
699 | (_, L'.CRecord _) => isRecord ()
|
adamc@12
|
700 | (L'.CConcat _, _) => isRecord ()
|
adamc@12
|
701 | (_, L'.CConcat _) => isRecord ()
|
adamc@12
|
702
|
adamc@71
|
703 | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) =>
|
adamc@71
|
704 (unifyKinds dom1 dom2;
|
adamc@71
|
705 unifyKinds ran1 ran2)
|
adamc@71
|
706
|
adamc@10
|
707 | _ => err CIncompatible
|
adamc@10
|
708 end
|
adamc@10
|
709
|
adamc@12
|
710 and unifyCons env c1 c2 =
|
adamc@10
|
711 unifyCons' env c1 c2
|
adamc@10
|
712 handle CUnify' err => raise CUnify (c1, c2, err)
|
adamc@10
|
713 | KUnify args => raise CUnify (c1, c2, CKind args)
|
adamc@10
|
714
|
adamc@10
|
715 datatype exp_error =
|
adamc@10
|
716 UnboundExp of ErrorMsg.span * string
|
adamc@34
|
717 | UnboundStrInExp of ErrorMsg.span * string
|
adamc@10
|
718 | Unify of L'.exp * L'.con * L'.con * cunify_error
|
adamc@11
|
719 | Unif of string * L'.con
|
adamc@11
|
720 | WrongForm of string * L'.exp * L'.con
|
adamc@10
|
721
|
adamc@10
|
722 fun expError env err =
|
adamc@10
|
723 case err of
|
adamc@10
|
724 UnboundExp (loc, s) =>
|
adamc@10
|
725 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
|
adamc@34
|
726 | UnboundStrInExp (loc, s) =>
|
adamc@34
|
727 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
|
adamc@10
|
728 | Unify (e, c1, c2, uerr) =>
|
adamc@10
|
729 (ErrorMsg.errorAt (#2 e) "Unification failure";
|
adamc@10
|
730 eprefaces' [("Expression", p_exp env e),
|
adamc@10
|
731 ("Have con", p_con env c1),
|
adamc@10
|
732 ("Need con", p_con env c2)];
|
adamc@10
|
733 cunifyError env uerr)
|
adamc@11
|
734 | Unif (action, c) =>
|
adamc@11
|
735 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
|
adamc@11
|
736 eprefaces' [("Con", p_con env c)])
|
adamc@11
|
737 | WrongForm (variety, e, t) =>
|
adamc@11
|
738 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
|
adamc@11
|
739 eprefaces' [("Expression", p_exp env e),
|
adamc@11
|
740 ("Type", p_con env t)])
|
adamc@10
|
741
|
adamc@10
|
742 fun checkCon env e c1 c2 =
|
adamc@10
|
743 unifyCons env c1 c2
|
adamc@10
|
744 handle CUnify (c1, c2, err) =>
|
adamc@10
|
745 expError env (Unify (e, c1, c2, err))
|
adamc@10
|
746
|
adamc@14
|
747 fun primType env p =
|
adamc@56
|
748 case p of
|
adamc@56
|
749 P.Int _ => !int
|
adamc@56
|
750 | P.Float _ => !float
|
adamc@56
|
751 | P.String _ => !string
|
adamc@14
|
752
|
adamc@71
|
753 fun recCons (k, nm, v, rest, loc) =
|
adamc@71
|
754 (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc),
|
adamc@71
|
755 rest), loc)
|
adamc@71
|
756
|
adamc@71
|
757 fun foldType (dom, loc) =
|
adamc@71
|
758 (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc),
|
adamc@71
|
759 (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc),
|
adamc@71
|
760 (L'.TCFun (L'.Explicit, "v", dom,
|
adamc@71
|
761 (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc),
|
adamc@71
|
762 (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc),
|
adamc@71
|
763 (L'.CApp ((L'.CRel 3, loc),
|
adamc@71
|
764 recCons (dom,
|
adamc@71
|
765 (L'.CRel 2, loc),
|
adamc@71
|
766 (L'.CRel 1, loc),
|
adamc@71
|
767 (L'.CRel 0, loc),
|
adamc@71
|
768 loc)), loc)), loc)),
|
adamc@71
|
769 loc)), loc)), loc),
|
adamc@71
|
770 (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc),
|
adamc@71
|
771 (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc),
|
adamc@71
|
772 (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)),
|
adamc@71
|
773 loc)), loc)), loc)
|
adamc@71
|
774
|
adamc@15
|
775 fun typeof env (e, loc) =
|
adamc@15
|
776 case e of
|
adamc@56
|
777 L'.EPrim p => primType env p
|
adamc@15
|
778 | L'.ERel n => #2 (E.lookupERel env n)
|
adamc@15
|
779 | L'.ENamed n => #2 (E.lookupENamed env n)
|
adamc@34
|
780 | L'.EModProj (n, ms, x) =>
|
adamc@34
|
781 let
|
adamc@34
|
782 val (_, sgn) = E.lookupStrNamed env n
|
adamc@34
|
783 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
784 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@34
|
785 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@34
|
786 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
787 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
788 in
|
adamc@34
|
789 case E.projectVal env {sgn = sgn, str = str, field = x} of
|
adamc@34
|
790 NONE => raise Fail "typeof: Unknown val in structure"
|
adamc@34
|
791 | SOME t => t
|
adamc@34
|
792 end
|
adamc@34
|
793
|
adamc@15
|
794 | L'.EApp (e1, _) =>
|
adamc@15
|
795 (case #1 (typeof env e1) of
|
adamc@15
|
796 L'.TFun (_, c) => c
|
adamc@15
|
797 | _ => raise Fail "typeof: Bad EApp")
|
adamc@26
|
798 | L'.EAbs (_, _, ran, _) => ran
|
adamc@15
|
799 | L'.ECApp (e1, c) =>
|
adamc@15
|
800 (case #1 (typeof env e1) of
|
adamc@15
|
801 L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1
|
adamc@15
|
802 | _ => raise Fail "typeof: Bad ECApp")
|
adamc@15
|
803 | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc)
|
adamc@15
|
804
|
adamc@29
|
805 | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, _, t) => (x, t)) xes), loc), loc)
|
adamc@15
|
806 | L'.EField (_, _, {field, ...}) => field
|
adamc@71
|
807 | L'.EFold dom => foldType (dom, loc)
|
adamc@15
|
808
|
adamc@15
|
809 | L'.EError => cerror
|
adamc@15
|
810
|
adamc@15
|
811 fun elabHead env (e as (_, loc)) t =
|
adamc@15
|
812 let
|
adamc@15
|
813 fun unravel (t, e) =
|
adamc@15
|
814 case hnormCon env t of
|
adamc@15
|
815 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
|
adamc@15
|
816 let
|
adamc@76
|
817 val u = cunif (loc, k)
|
adamc@15
|
818 in
|
adamc@15
|
819 unravel (subConInCon (0, u) t',
|
adamc@15
|
820 (L'.ECApp (e, u), loc))
|
adamc@15
|
821 end
|
adamc@15
|
822 | _ => (e, t)
|
adamc@15
|
823 in
|
adamc@15
|
824 unravel (t, e)
|
adamc@15
|
825 end
|
adamc@15
|
826
|
adamc@10
|
827 fun elabExp env (e, loc) =
|
adamc@10
|
828 case e of
|
adamc@10
|
829 L.EAnnot (e, t) =>
|
adamc@10
|
830 let
|
adamc@10
|
831 val (e', et) = elabExp env e
|
adamc@10
|
832 val (t', _) = elabCon env t
|
adamc@10
|
833 in
|
adamc@10
|
834 checkCon env e' et t';
|
adamc@10
|
835 (e', t')
|
adamc@10
|
836 end
|
adamc@10
|
837
|
adamc@56
|
838 | L.EPrim p => ((L'.EPrim p, loc), primType env p)
|
adamc@34
|
839 | L.EVar ([], s) =>
|
adamc@10
|
840 (case E.lookupE env s of
|
adamc@10
|
841 E.NotBound =>
|
adamc@10
|
842 (expError env (UnboundExp (loc, s));
|
adamc@10
|
843 (eerror, cerror))
|
adamc@10
|
844 | E.Rel (n, t) => ((L'.ERel n, loc), t)
|
adamc@10
|
845 | E.Named (n, t) => ((L'.ENamed n, loc), t))
|
adamc@34
|
846 | L.EVar (m1 :: ms, s) =>
|
adamc@34
|
847 (case E.lookupStr env m1 of
|
adamc@42
|
848 NONE => (expError env (UnboundStrInExp (loc, m1));
|
adamc@34
|
849 (eerror, cerror))
|
adamc@34
|
850 | SOME (n, sgn) =>
|
adamc@34
|
851 let
|
adamc@34
|
852 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@34
|
853 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@34
|
854 NONE => (conError env (UnboundStrInCon (loc, m));
|
adamc@34
|
855 (strerror, sgnerror))
|
adamc@34
|
856 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@34
|
857 ((L'.StrVar n, loc), sgn) ms
|
adamc@34
|
858
|
adamc@34
|
859 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
|
adamc@34
|
860 NONE => (expError env (UnboundExp (loc, s));
|
adamc@34
|
861 cerror)
|
adamc@34
|
862 | SOME t => t
|
adamc@34
|
863 in
|
adamc@34
|
864 ((L'.EModProj (n, ms, s), loc), t)
|
adamc@34
|
865 end)
|
adamc@34
|
866
|
adamc@10
|
867 | L.EApp (e1, e2) =>
|
adamc@10
|
868 let
|
adamc@10
|
869 val (e1', t1) = elabExp env e1
|
adamc@15
|
870 val (e1', t1) = elabHead env e1' t1
|
adamc@10
|
871 val (e2', t2) = elabExp env e2
|
adamc@10
|
872
|
adamc@76
|
873 val dom = cunif (loc, ktype)
|
adamc@76
|
874 val ran = cunif (loc, ktype)
|
adamc@10
|
875 val t = (L'.TFun (dom, ran), dummy)
|
adamc@10
|
876 in
|
adamc@10
|
877 checkCon env e1' t1 t;
|
adamc@10
|
878 checkCon env e2' t2 dom;
|
adamc@10
|
879 ((L'.EApp (e1', e2'), loc), ran)
|
adamc@10
|
880 end
|
adamc@10
|
881 | L.EAbs (x, to, e) =>
|
adamc@10
|
882 let
|
adamc@10
|
883 val t' = case to of
|
adamc@76
|
884 NONE => cunif (loc, ktype)
|
adamc@10
|
885 | SOME t =>
|
adamc@10
|
886 let
|
adamc@10
|
887 val (t', tk) = elabCon env t
|
adamc@10
|
888 in
|
adamc@10
|
889 checkKind env t' tk ktype;
|
adamc@10
|
890 t'
|
adamc@10
|
891 end
|
adamc@10
|
892 val (e', et) = elabExp (E.pushERel env x t') e
|
adamc@10
|
893 in
|
adamc@26
|
894 ((L'.EAbs (x, t', et, e'), loc),
|
adamc@10
|
895 (L'.TFun (t', et), loc))
|
adamc@10
|
896 end
|
adamc@10
|
897 | L.ECApp (e, c) =>
|
adamc@10
|
898 let
|
adamc@10
|
899 val (e', et) = elabExp env e
|
adamc@15
|
900 val (e', et) = elabHead env e' et
|
adamc@10
|
901 val (c', ck) = elabCon env c
|
adamc@10
|
902 in
|
adamc@11
|
903 case #1 (hnormCon env et) of
|
adamc@11
|
904 L'.CError => (eerror, cerror)
|
adamc@11
|
905 | L'.TCFun (_, _, k, eb) =>
|
adamc@11
|
906 let
|
adamc@11
|
907 val () = checkKind env c' ck k
|
adamc@11
|
908 val eb' = subConInCon (0, c') eb
|
adamc@11
|
909 handle SynUnif => (expError env (Unif ("substitution", eb));
|
adamc@11
|
910 cerror)
|
adamc@11
|
911 in
|
adamc@11
|
912 ((L'.ECApp (e', c'), loc), eb')
|
adamc@11
|
913 end
|
adamc@11
|
914
|
adamc@11
|
915 | L'.CUnif _ =>
|
adamc@11
|
916 (expError env (Unif ("application", et));
|
adamc@11
|
917 (eerror, cerror))
|
adamc@11
|
918
|
adamc@11
|
919 | _ =>
|
adamc@11
|
920 (expError env (WrongForm ("constructor function", e', et));
|
adamc@11
|
921 (eerror, cerror))
|
adamc@10
|
922 end
|
adamc@11
|
923 | L.ECAbs (expl, x, k, e) =>
|
adamc@11
|
924 let
|
adamc@11
|
925 val expl' = elabExplicitness expl
|
adamc@11
|
926 val k' = elabKind k
|
adamc@11
|
927 val (e', et) = elabExp (E.pushCRel env x k') e
|
adamc@11
|
928 in
|
adamc@11
|
929 ((L'.ECAbs (expl', x, k', e'), loc),
|
adamc@11
|
930 (L'.TCFun (expl', x, k', et), loc))
|
adamc@11
|
931 end
|
adamc@6
|
932
|
adamc@12
|
933 | L.ERecord xes =>
|
adamc@12
|
934 let
|
adamc@12
|
935 val xes' = map (fn (x, e) =>
|
adamc@12
|
936 let
|
adamc@12
|
937 val (x', xk) = elabCon env x
|
adamc@12
|
938 val (e', et) = elabExp env e
|
adamc@12
|
939 in
|
adamc@12
|
940 checkKind env x' xk kname;
|
adamc@12
|
941 (x', e', et)
|
adamc@12
|
942 end) xes
|
adamc@12
|
943 in
|
adamc@29
|
944 ((L'.ERecord xes', loc),
|
adamc@12
|
945 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc))
|
adamc@12
|
946 end
|
adamc@12
|
947
|
adamc@12
|
948 | L.EField (e, c) =>
|
adamc@12
|
949 let
|
adamc@12
|
950 val (e', et) = elabExp env e
|
adamc@12
|
951 val (c', ck) = elabCon env c
|
adamc@12
|
952
|
adamc@76
|
953 val ft = cunif (loc, ktype)
|
adamc@76
|
954 val rest = cunif (loc, ktype_record)
|
adamc@12
|
955 in
|
adamc@12
|
956 checkKind env c' ck kname;
|
adamc@12
|
957 checkCon env e' et (L'.TRecord (L'.CConcat ((L'.CRecord (ktype, [(c', ft)]), loc), rest), loc), loc);
|
adamc@12
|
958 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft)
|
adamc@12
|
959 end
|
adamc@71
|
960
|
adamc@71
|
961 | L.EFold =>
|
adamc@71
|
962 let
|
adamc@76
|
963 val dom = kunif loc
|
adamc@71
|
964 in
|
adamc@71
|
965 ((L'.EFold dom, loc), foldType (dom, loc))
|
adamc@71
|
966 end
|
adamc@12
|
967
|
adamc@12
|
968
|
adamc@6
|
969 datatype decl_error =
|
adamc@76
|
970 KunifsRemain of ErrorMsg.span
|
adamc@76
|
971 | CunifsRemain of ErrorMsg.span
|
adamc@6
|
972
|
adamc@6
|
973 fun declError env err =
|
adamc@6
|
974 case err of
|
adamc@76
|
975 KunifsRemain loc =>
|
adamc@76
|
976 ErrorMsg.errorAt loc "Some kind unification variables are undetermined in declaration"
|
adamc@76
|
977 | CunifsRemain loc =>
|
adamc@76
|
978 ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in declaration"
|
adamc@6
|
979
|
adamc@31
|
980 datatype sgn_error =
|
adamc@31
|
981 UnboundSgn of ErrorMsg.span * string
|
adamc@31
|
982 | UnmatchedSgi of L'.sgn_item
|
adamc@31
|
983 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
|
adamc@31
|
984 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
|
adamc@41
|
985 | SgnWrongForm of L'.sgn * L'.sgn
|
adamc@42
|
986 | UnWhereable of L'.sgn * string
|
adamc@75
|
987 | WhereWrongKind of L'.kind * L'.kind * kunify_error
|
adamc@58
|
988 | NotIncludable of L'.sgn
|
adamc@62
|
989 | DuplicateCon of ErrorMsg.span * string
|
adamc@62
|
990 | DuplicateVal of ErrorMsg.span * string
|
adamc@62
|
991 | DuplicateSgn of ErrorMsg.span * string
|
adamc@62
|
992 | DuplicateStr of ErrorMsg.span * string
|
adamc@31
|
993
|
adamc@31
|
994 fun sgnError env err =
|
adamc@31
|
995 case err of
|
adamc@31
|
996 UnboundSgn (loc, s) =>
|
adamc@31
|
997 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
|
adamc@31
|
998 | UnmatchedSgi (sgi as (_, loc)) =>
|
adamc@31
|
999 (ErrorMsg.errorAt loc "Unmatched signature item";
|
adamc@31
|
1000 eprefaces' [("Item", p_sgn_item env sgi)])
|
adamc@31
|
1001 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
|
adamc@31
|
1002 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
|
adamc@59
|
1003 eprefaces' [("Have", p_sgn_item env sgi1),
|
adamc@59
|
1004 ("Need", p_sgn_item env sgi2),
|
adamc@31
|
1005 ("Kind 1", p_kind k1),
|
adamc@31
|
1006 ("Kind 2", p_kind k2)];
|
adamc@31
|
1007 kunifyError kerr)
|
adamc@31
|
1008 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
|
adamc@31
|
1009 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
|
adamc@59
|
1010 eprefaces' [("Have", p_sgn_item env sgi1),
|
adamc@59
|
1011 ("Need", p_sgn_item env sgi2),
|
adamc@31
|
1012 ("Con 1", p_con env c1),
|
adamc@31
|
1013 ("Con 2", p_con env c2)];
|
adamc@31
|
1014 cunifyError env cerr)
|
adamc@41
|
1015 | SgnWrongForm (sgn1, sgn2) =>
|
adamc@41
|
1016 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
|
adamc@41
|
1017 eprefaces' [("Sig 1", p_sgn env sgn1),
|
adamc@41
|
1018 ("Sig 2", p_sgn env sgn2)])
|
adamc@42
|
1019 | UnWhereable (sgn, x) =>
|
adamc@42
|
1020 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
|
adamc@42
|
1021 eprefaces' [("Signature", p_sgn env sgn),
|
adamc@42
|
1022 ("Field", PD.string x)])
|
adamc@75
|
1023 | WhereWrongKind (k1, k2, kerr) =>
|
adamc@75
|
1024 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
|
adamc@75
|
1025 eprefaces' [("Have", p_kind k1),
|
adamc@75
|
1026 ("Need", p_kind k2)];
|
adamc@75
|
1027 kunifyError kerr)
|
adamc@58
|
1028 | NotIncludable sgn =>
|
adamc@58
|
1029 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
|
adamc@58
|
1030 eprefaces' [("Signature", p_sgn env sgn)])
|
adamc@62
|
1031 | DuplicateCon (loc, s) =>
|
adamc@62
|
1032 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
|
adamc@62
|
1033 | DuplicateVal (loc, s) =>
|
adamc@62
|
1034 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
|
adamc@62
|
1035 | DuplicateSgn (loc, s) =>
|
adamc@62
|
1036 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
|
adamc@62
|
1037 | DuplicateStr (loc, s) =>
|
adamc@62
|
1038 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
|
adamc@31
|
1039
|
adamc@31
|
1040 datatype str_error =
|
adamc@31
|
1041 UnboundStr of ErrorMsg.span * string
|
adamc@44
|
1042 | NotFunctor of L'.sgn
|
adamc@46
|
1043 | FunctorRebind of ErrorMsg.span
|
adamc@61
|
1044 | UnOpenable of L'.sgn
|
adamc@75
|
1045 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
|
adamc@31
|
1046
|
adamc@31
|
1047 fun strError env err =
|
adamc@31
|
1048 case err of
|
adamc@31
|
1049 UnboundStr (loc, s) =>
|
adamc@31
|
1050 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
|
adamc@44
|
1051 | NotFunctor sgn =>
|
adamc@44
|
1052 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
|
adamc@44
|
1053 eprefaces' [("Signature", p_sgn env sgn)])
|
adamc@46
|
1054 | FunctorRebind loc =>
|
adamc@46
|
1055 ErrorMsg.errorAt loc "Attempt to rebind functor"
|
adamc@61
|
1056 | UnOpenable sgn =>
|
adamc@61
|
1057 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
|
adamc@61
|
1058 eprefaces' [("Signature", p_sgn env sgn)])
|
adamc@75
|
1059 | NotType (k, (k1, k2, ue)) =>
|
adamc@75
|
1060 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
|
adamc@75
|
1061 eprefaces' [("Kind", p_kind k),
|
adamc@75
|
1062 ("Subkind 1", p_kind k1),
|
adamc@75
|
1063 ("Subkind 2", p_kind k2)];
|
adamc@75
|
1064 kunifyError ue)
|
adamc@31
|
1065
|
adamc@42
|
1066 val hnormSgn = E.hnormSgn
|
adamc@31
|
1067
|
adamc@31
|
1068 fun elabSgn_item ((sgi, loc), env) =
|
adamc@76
|
1069 case sgi of
|
adamc@76
|
1070 L.SgiConAbs (x, k) =>
|
adamc@76
|
1071 let
|
adamc@76
|
1072 val k' = elabKind k
|
adamc@31
|
1073
|
adamc@76
|
1074 val (env', n) = E.pushCNamed env x k' NONE
|
adamc@76
|
1075 in
|
adamc@76
|
1076 ([(L'.SgiConAbs (x, n, k'), loc)], env')
|
adamc@76
|
1077 end
|
adamc@31
|
1078
|
adamc@76
|
1079 | L.SgiCon (x, ko, c) =>
|
adamc@76
|
1080 let
|
adamc@76
|
1081 val k' = case ko of
|
adamc@76
|
1082 NONE => kunif loc
|
adamc@76
|
1083 | SOME k => elabKind k
|
adamc@31
|
1084
|
adamc@76
|
1085 val (c', ck) = elabCon env c
|
adamc@76
|
1086 val (env', n) = E.pushCNamed env x k' (SOME c')
|
adamc@76
|
1087 in
|
adamc@76
|
1088 checkKind env c' ck k';
|
adamc@31
|
1089
|
adamc@76
|
1090 ([(L'.SgiCon (x, n, k', c'), loc)], env')
|
adamc@76
|
1091 end
|
adamc@31
|
1092
|
adamc@76
|
1093 | L.SgiVal (x, c) =>
|
adamc@76
|
1094 let
|
adamc@76
|
1095 val (c', ck) = elabCon env c
|
adamc@31
|
1096
|
adamc@76
|
1097 val (env', n) = E.pushENamed env x c'
|
adamc@76
|
1098 in
|
adamc@76
|
1099 (unifyKinds ck ktype
|
adamc@76
|
1100 handle KUnify ue => strError env (NotType (ck, ue)));
|
adamc@31
|
1101
|
adamc@76
|
1102 ([(L'.SgiVal (x, n, c'), loc)], env')
|
adamc@76
|
1103 end
|
adamc@31
|
1104
|
adamc@76
|
1105 | L.SgiStr (x, sgn) =>
|
adamc@76
|
1106 let
|
adamc@76
|
1107 val sgn' = elabSgn env sgn
|
adamc@76
|
1108 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@76
|
1109 in
|
adamc@76
|
1110 ([(L'.SgiStr (x, n, sgn'), loc)], env')
|
adamc@76
|
1111 end
|
adamc@31
|
1112
|
adamc@76
|
1113 | L.SgiSgn (x, sgn) =>
|
adamc@76
|
1114 let
|
adamc@76
|
1115 val sgn' = elabSgn env sgn
|
adamc@76
|
1116 val (env', n) = E.pushSgnNamed env x sgn'
|
adamc@76
|
1117 in
|
adamc@76
|
1118 ([(L'.SgiSgn (x, n, sgn'), loc)], env')
|
adamc@76
|
1119 end
|
adamc@31
|
1120
|
adamc@76
|
1121 | L.SgiInclude sgn =>
|
adamc@76
|
1122 let
|
adamc@76
|
1123 val sgn' = elabSgn env sgn
|
adamc@76
|
1124 in
|
adamc@76
|
1125 case #1 (hnormSgn env sgn') of
|
adamc@76
|
1126 L'.SgnConst sgis =>
|
adamc@76
|
1127 (sgis, foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis)
|
adamc@76
|
1128 | _ => (sgnError env (NotIncludable sgn');
|
adamc@76
|
1129 ([], env))
|
adamc@76
|
1130 end
|
adamc@31
|
1131
|
adamc@31
|
1132 and elabSgn env (sgn, loc) =
|
adamc@31
|
1133 case sgn of
|
adamc@31
|
1134 L.SgnConst sgis =>
|
adamc@31
|
1135 let
|
adamc@58
|
1136 val (sgis', _) = ListUtil.foldlMapConcat elabSgn_item env sgis
|
adamc@62
|
1137
|
adamc@62
|
1138 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
|
adamc@62
|
1139 case sgi of
|
adamc@62
|
1140 L'.SgiConAbs (x, _, _) =>
|
adamc@62
|
1141 (if SS.member (cons, x) then
|
adamc@62
|
1142 sgnError env (DuplicateCon (loc, x))
|
adamc@62
|
1143 else
|
adamc@62
|
1144 ();
|
adamc@62
|
1145 (SS.add (cons, x), vals, sgns, strs))
|
adamc@62
|
1146 | L'.SgiCon (x, _, _, _) =>
|
adamc@62
|
1147 (if SS.member (cons, x) then
|
adamc@62
|
1148 sgnError env (DuplicateCon (loc, x))
|
adamc@62
|
1149 else
|
adamc@62
|
1150 ();
|
adamc@62
|
1151 (SS.add (cons, x), vals, sgns, strs))
|
adamc@62
|
1152 | L'.SgiVal (x, _, _) =>
|
adamc@62
|
1153 (if SS.member (vals, x) then
|
adamc@62
|
1154 sgnError env (DuplicateVal (loc, x))
|
adamc@62
|
1155 else
|
adamc@62
|
1156 ();
|
adamc@62
|
1157 (cons, SS.add (vals, x), sgns, strs))
|
adamc@62
|
1158 | L'.SgiSgn (x, _, _) =>
|
adamc@62
|
1159 (if SS.member (sgns, x) then
|
adamc@62
|
1160 sgnError env (DuplicateSgn (loc, x))
|
adamc@62
|
1161 else
|
adamc@62
|
1162 ();
|
adamc@62
|
1163 (cons, vals, SS.add (sgns, x), strs))
|
adamc@62
|
1164 | L'.SgiStr (x, _, _) =>
|
adamc@62
|
1165 (if SS.member (strs, x) then
|
adamc@62
|
1166 sgnError env (DuplicateStr (loc, x))
|
adamc@62
|
1167 else
|
adamc@62
|
1168 ();
|
adamc@62
|
1169 (cons, vals, sgns, SS.add (strs, x))))
|
adamc@62
|
1170 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
|
adamc@31
|
1171 in
|
adamc@31
|
1172 (L'.SgnConst sgis', loc)
|
adamc@31
|
1173 end
|
adamc@31
|
1174 | L.SgnVar x =>
|
adamc@31
|
1175 (case E.lookupSgn env x of
|
adamc@31
|
1176 NONE =>
|
adamc@31
|
1177 (sgnError env (UnboundSgn (loc, x));
|
adamc@31
|
1178 (L'.SgnError, loc))
|
adamc@31
|
1179 | SOME (n, sgis) => (L'.SgnVar n, loc))
|
adamc@41
|
1180 | L.SgnFun (m, dom, ran) =>
|
adamc@41
|
1181 let
|
adamc@41
|
1182 val dom' = elabSgn env dom
|
adamc@41
|
1183 val (env', n) = E.pushStrNamed env m dom'
|
adamc@41
|
1184 val ran' = elabSgn env' ran
|
adamc@41
|
1185 in
|
adamc@41
|
1186 (L'.SgnFun (m, n, dom', ran'), loc)
|
adamc@41
|
1187 end
|
adamc@42
|
1188 | L.SgnWhere (sgn, x, c) =>
|
adamc@42
|
1189 let
|
adamc@42
|
1190 val sgn' = elabSgn env sgn
|
adamc@42
|
1191 val (c', ck) = elabCon env c
|
adamc@42
|
1192 in
|
adamc@42
|
1193 case #1 (hnormSgn env sgn') of
|
adamc@42
|
1194 L'.SgnError => sgnerror
|
adamc@42
|
1195 | L'.SgnConst sgis =>
|
adamc@75
|
1196 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
|
adamc@75
|
1197 x' = x andalso
|
adamc@75
|
1198 (unifyKinds k ck
|
adamc@75
|
1199 handle KUnify x => sgnError env (WhereWrongKind x);
|
adamc@42
|
1200 true)
|
adamc@42
|
1201 | _ => false) sgis then
|
adamc@42
|
1202 (L'.SgnWhere (sgn', x, c'), loc)
|
adamc@42
|
1203 else
|
adamc@42
|
1204 (sgnError env (UnWhereable (sgn', x));
|
adamc@42
|
1205 sgnerror)
|
adamc@42
|
1206 | _ => (sgnError env (UnWhereable (sgn', x));
|
adamc@42
|
1207 sgnerror)
|
adamc@42
|
1208 end
|
adamc@59
|
1209 | L.SgnProj (m, ms, x) =>
|
adamc@59
|
1210 (case E.lookupStr env m of
|
adamc@59
|
1211 NONE => (strError env (UnboundStr (loc, m));
|
adamc@59
|
1212 sgnerror)
|
adamc@59
|
1213 | SOME (n, sgn) =>
|
adamc@59
|
1214 let
|
adamc@59
|
1215 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@59
|
1216 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@59
|
1217 NONE => (strError env (UnboundStr (loc, m));
|
adamc@59
|
1218 (strerror, sgnerror))
|
adamc@59
|
1219 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@59
|
1220 ((L'.StrVar n, loc), sgn) ms
|
adamc@59
|
1221 in
|
adamc@59
|
1222 case E.projectSgn env {sgn = sgn, str = str, field = x} of
|
adamc@59
|
1223 NONE => (sgnError env (UnboundSgn (loc, x));
|
adamc@59
|
1224 sgnerror)
|
adamc@59
|
1225 | SOME _ => (L'.SgnProj (n, ms, x), loc)
|
adamc@59
|
1226 end)
|
adamc@59
|
1227
|
adamc@31
|
1228
|
adamc@66
|
1229 fun selfify env {str, strs, sgn} =
|
adamc@66
|
1230 case #1 (hnormSgn env sgn) of
|
adamc@66
|
1231 L'.SgnError => sgn
|
adamc@66
|
1232 | L'.SgnVar _ => sgn
|
adamc@66
|
1233
|
adamc@66
|
1234 | L'.SgnConst sgis =>
|
adamc@66
|
1235 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
|
adamc@66
|
1236 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
|
adamc@66
|
1237 | (L'.SgiStr (x, n, sgn), loc) =>
|
adamc@66
|
1238 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
|
adamc@66
|
1239 | x => x) sgis), #2 sgn)
|
adamc@66
|
1240 | L'.SgnFun _ => sgn
|
adamc@66
|
1241 | L'.SgnWhere _ => sgn
|
adamc@66
|
1242 | L'.SgnProj (m, ms, x) =>
|
adamc@66
|
1243 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
|
adamc@66
|
1244 (L'.StrVar m, #2 sgn) ms,
|
adamc@66
|
1245 sgn = #2 (E.lookupStrNamed env m),
|
adamc@66
|
1246 field = x} of
|
adamc@66
|
1247 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
|
adamc@66
|
1248 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
|
adamc@66
|
1249
|
adamc@66
|
1250 fun selfifyAt env {str, sgn} =
|
adamc@66
|
1251 let
|
adamc@66
|
1252 fun self (str, _) =
|
adamc@66
|
1253 case str of
|
adamc@66
|
1254 L'.StrVar x => SOME (x, [])
|
adamc@66
|
1255 | L'.StrProj (str, x) =>
|
adamc@66
|
1256 (case self str of
|
adamc@66
|
1257 NONE => NONE
|
adamc@66
|
1258 | SOME (m, ms) => SOME (m, ms @ [x]))
|
adamc@66
|
1259 | _ => NONE
|
adamc@66
|
1260 in
|
adamc@66
|
1261 case self str of
|
adamc@66
|
1262 NONE => sgn
|
adamc@66
|
1263 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
|
adamc@66
|
1264 end
|
adamc@66
|
1265
|
adamc@66
|
1266 fun dopen env {str, strs, sgn} =
|
adamc@66
|
1267 let
|
adamc@66
|
1268 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
|
adamc@66
|
1269 (L'.StrVar str, #2 sgn) strs
|
adamc@66
|
1270 in
|
adamc@66
|
1271 case #1 (hnormSgn env sgn) of
|
adamc@66
|
1272 L'.SgnConst sgis =>
|
adamc@66
|
1273 ListUtil.foldlMap (fn ((sgi, loc), env') =>
|
adamc@66
|
1274 case sgi of
|
adamc@66
|
1275 L'.SgiConAbs (x, n, k) =>
|
adamc@66
|
1276 ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc),
|
adamc@66
|
1277 E.pushCNamedAs env' x n k NONE)
|
adamc@66
|
1278 | L'.SgiCon (x, n, k, c) =>
|
adamc@66
|
1279 ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc),
|
adamc@66
|
1280 E.pushCNamedAs env' x n k (SOME c))
|
adamc@66
|
1281 | L'.SgiVal (x, n, t) =>
|
adamc@66
|
1282 ((L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc),
|
adamc@66
|
1283 E.pushENamedAs env' x n t)
|
adamc@66
|
1284 | L'.SgiStr (x, n, sgn) =>
|
adamc@66
|
1285 ((L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc),
|
adamc@66
|
1286 E.pushStrNamedAs env' x n sgn)
|
adamc@66
|
1287 | L'.SgiSgn (x, n, sgn) =>
|
adamc@66
|
1288 ((L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc),
|
adamc@66
|
1289 E.pushSgnNamedAs env' x n sgn))
|
adamc@66
|
1290 env sgis
|
adamc@66
|
1291 | _ => (strError env (UnOpenable sgn);
|
adamc@66
|
1292 ([], env))
|
adamc@66
|
1293 end
|
adamc@66
|
1294
|
adamc@31
|
1295 fun sgiOfDecl (d, loc) =
|
adamc@31
|
1296 case d of
|
adamc@59
|
1297 L'.DCon (x, n, k, c) => (L'.SgiCon (x, n, k, c), loc)
|
adamc@59
|
1298 | L'.DVal (x, n, t, _) => (L'.SgiVal (x, n, t), loc)
|
adamc@59
|
1299 | L'.DSgn (x, n, sgn) => (L'.SgiSgn (x, n, sgn), loc)
|
adamc@59
|
1300 | L'.DStr (x, n, sgn, _) => (L'.SgiStr (x, n, sgn), loc)
|
adamc@59
|
1301 | L'.DFfiStr (x, n, sgn) => (L'.SgiStr (x, n, sgn), loc)
|
adamc@31
|
1302
|
adamc@35
|
1303 fun subSgn env sgn1 (sgn2 as (_, loc2)) =
|
adamc@35
|
1304 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
|
adamc@31
|
1305 (L'.SgnError, _) => ()
|
adamc@31
|
1306 | (_, L'.SgnError) => ()
|
adamc@31
|
1307
|
adamc@31
|
1308 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
|
adamc@31
|
1309 let
|
adamc@31
|
1310 fun folder (sgi2All as (sgi, _), env) =
|
adamc@31
|
1311 let
|
adamc@31
|
1312 fun seek p =
|
adamc@31
|
1313 let
|
adamc@31
|
1314 fun seek env ls =
|
adamc@31
|
1315 case ls of
|
adamc@31
|
1316 [] => (sgnError env (UnmatchedSgi sgi2All);
|
adamc@31
|
1317 env)
|
adamc@31
|
1318 | h :: t =>
|
adamc@31
|
1319 case p h of
|
adamc@31
|
1320 NONE => seek (E.sgiBinds env h) t
|
adamc@31
|
1321 | SOME env => env
|
adamc@31
|
1322 in
|
adamc@31
|
1323 seek env sgis1
|
adamc@31
|
1324 end
|
adamc@31
|
1325 in
|
adamc@31
|
1326 case sgi of
|
adamc@31
|
1327 L'.SgiConAbs (x, n2, k2) =>
|
adamc@31
|
1328 seek (fn sgi1All as (sgi1, _) =>
|
adamc@31
|
1329 let
|
adamc@41
|
1330 fun found (x', n1, k1, co1) =
|
adamc@41
|
1331 if x = x' then
|
adamc@41
|
1332 let
|
adamc@41
|
1333 val () = unifyKinds k1 k2
|
adamc@41
|
1334 handle KUnify (k1, k2, err) =>
|
adamc@41
|
1335 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
|
adamc@41
|
1336 val env = E.pushCNamedAs env x n1 k1 co1
|
adamc@41
|
1337 in
|
adamc@41
|
1338 SOME (if n1 = n2 then
|
adamc@41
|
1339 env
|
adamc@41
|
1340 else
|
adamc@41
|
1341 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)))
|
adamc@41
|
1342 end
|
adamc@41
|
1343 else
|
adamc@41
|
1344 NONE
|
adamc@31
|
1345 in
|
adamc@31
|
1346 case sgi1 of
|
adamc@41
|
1347 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
|
adamc@41
|
1348 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
|
adamc@31
|
1349 | _ => NONE
|
adamc@31
|
1350 end)
|
adamc@31
|
1351
|
adamc@31
|
1352 | L'.SgiCon (x, n2, k2, c2) =>
|
adamc@31
|
1353 seek (fn sgi1All as (sgi1, _) =>
|
adamc@31
|
1354 case sgi1 of
|
adamc@41
|
1355 L'.SgiCon (x', n1, k1, c1) =>
|
adamc@41
|
1356 if x = x' then
|
adamc@41
|
1357 let
|
adamc@41
|
1358 val () = unifyCons env c1 c2
|
adamc@41
|
1359 handle CUnify (c1, c2, err) =>
|
adamc@41
|
1360 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
|
adamc@41
|
1361 in
|
adamc@41
|
1362 SOME (E.pushCNamedAs env x n2 k2 (SOME c2))
|
adamc@41
|
1363 end
|
adamc@41
|
1364 else
|
adamc@41
|
1365 NONE
|
adamc@31
|
1366 | _ => NONE)
|
adamc@31
|
1367
|
adamc@32
|
1368 | L'.SgiVal (x, n2, c2) =>
|
adamc@32
|
1369 seek (fn sgi1All as (sgi1, _) =>
|
adamc@32
|
1370 case sgi1 of
|
adamc@41
|
1371 L'.SgiVal (x', n1, c1) =>
|
adamc@41
|
1372 if x = x' then
|
adamc@41
|
1373 let
|
adamc@41
|
1374 val () = unifyCons env c1 c2
|
adamc@41
|
1375 handle CUnify (c1, c2, err) =>
|
adamc@41
|
1376 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
|
adamc@41
|
1377 in
|
adamc@41
|
1378 SOME env
|
adamc@41
|
1379 end
|
adamc@41
|
1380 else
|
adamc@41
|
1381 NONE
|
adamc@32
|
1382 | _ => NONE)
|
adamc@32
|
1383
|
adamc@33
|
1384 | L'.SgiStr (x, n2, sgn2) =>
|
adamc@33
|
1385 seek (fn sgi1All as (sgi1, _) =>
|
adamc@33
|
1386 case sgi1 of
|
adamc@41
|
1387 L'.SgiStr (x', n1, sgn1) =>
|
adamc@41
|
1388 if x = x' then
|
adamc@66
|
1389 let
|
adamc@66
|
1390 val () = subSgn env sgn1 sgn2
|
adamc@66
|
1391 val env = E.pushStrNamedAs env x n1 sgn1
|
adamc@66
|
1392 val env = if n1 = n2 then
|
adamc@66
|
1393 env
|
adamc@66
|
1394 else
|
adamc@66
|
1395 E.pushStrNamedAs env x n2
|
adamc@66
|
1396 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
|
adamc@66
|
1397 sgn = sgn2})
|
adamc@66
|
1398 in
|
adamc@66
|
1399 SOME env
|
adamc@66
|
1400 end
|
adamc@41
|
1401 else
|
adamc@41
|
1402 NONE
|
adamc@33
|
1403 | _ => NONE)
|
adamc@59
|
1404
|
adamc@59
|
1405 | L'.SgiSgn (x, n2, sgn2) =>
|
adamc@59
|
1406 seek (fn sgi1All as (sgi1, _) =>
|
adamc@59
|
1407 case sgi1 of
|
adamc@59
|
1408 L'.SgiSgn (x', n1, sgn1) =>
|
adamc@59
|
1409 if x = x' then
|
adamc@65
|
1410 let
|
adamc@65
|
1411 val () = subSgn env sgn1 sgn2
|
adamc@65
|
1412 val () = subSgn env sgn2 sgn1
|
adamc@65
|
1413
|
adamc@65
|
1414 val env = E.pushSgnNamedAs env x n2 sgn2
|
adamc@65
|
1415 val env = if n1 = n2 then
|
adamc@65
|
1416 env
|
adamc@65
|
1417 else
|
adamc@65
|
1418 E.pushSgnNamedAs env x n1 sgn2
|
adamc@65
|
1419 in
|
adamc@65
|
1420 SOME env
|
adamc@65
|
1421 end
|
adamc@59
|
1422 else
|
adamc@59
|
1423 NONE
|
adamc@59
|
1424 | _ => NONE)
|
adamc@31
|
1425 end
|
adamc@31
|
1426 in
|
adamc@31
|
1427 ignore (foldl folder env sgis2)
|
adamc@31
|
1428 end
|
adamc@31
|
1429
|
adamc@41
|
1430 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
|
adamc@43
|
1431 let
|
adamc@43
|
1432 val ran1 =
|
adamc@43
|
1433 if n1 = n2 then
|
adamc@43
|
1434 ran1
|
adamc@43
|
1435 else
|
adamc@43
|
1436 subStrInSgn (n1, n2) ran1
|
adamc@43
|
1437 in
|
adamc@43
|
1438 subSgn env dom2 dom1;
|
adamc@43
|
1439 subSgn (E.pushStrNamedAs env m2 n2 dom2) ran1 ran2
|
adamc@43
|
1440 end
|
adamc@41
|
1441
|
adamc@41
|
1442 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
|
adamc@41
|
1443
|
adamc@61
|
1444
|
adamc@31
|
1445 fun elabDecl ((d, loc), env) =
|
adamc@76
|
1446 case d of
|
adamc@76
|
1447 L.DCon (x, ko, c) =>
|
adamc@76
|
1448 let
|
adamc@76
|
1449 val k' = case ko of
|
adamc@76
|
1450 NONE => kunif loc
|
adamc@76
|
1451 | SOME k => elabKind k
|
adamc@76
|
1452
|
adamc@76
|
1453 val (c', ck) = elabCon env c
|
adamc@76
|
1454 val (env', n) = E.pushCNamed env x k' (SOME c')
|
adamc@76
|
1455 in
|
adamc@76
|
1456 checkKind env c' ck k';
|
adamc@76
|
1457
|
adamc@76
|
1458 ([(L'.DCon (x, n, k', c'), loc)], env')
|
adamc@76
|
1459 end
|
adamc@76
|
1460 | L.DVal (x, co, e) =>
|
adamc@76
|
1461 let
|
adamc@76
|
1462 val (c', ck) = case co of
|
adamc@76
|
1463 NONE => (cunif (loc, ktype), ktype)
|
adamc@76
|
1464 | SOME c => elabCon env c
|
adamc@76
|
1465
|
adamc@76
|
1466 val (e', et) = elabExp env e
|
adamc@76
|
1467 val (env', n) = E.pushENamed env x c'
|
adamc@76
|
1468 in
|
adamc@76
|
1469 checkCon env e' et c';
|
adamc@76
|
1470
|
adamc@76
|
1471 ([(L'.DVal (x, n, c', e'), loc)], env')
|
adamc@76
|
1472 end
|
adamc@76
|
1473
|
adamc@76
|
1474 | L.DSgn (x, sgn) =>
|
adamc@76
|
1475 let
|
adamc@76
|
1476 val sgn' = elabSgn env sgn
|
adamc@76
|
1477 val (env', n) = E.pushSgnNamed env x sgn'
|
adamc@76
|
1478 in
|
adamc@76
|
1479 ([(L'.DSgn (x, n, sgn'), loc)], env')
|
adamc@76
|
1480 end
|
adamc@76
|
1481
|
adamc@76
|
1482 | L.DStr (x, sgno, str) =>
|
adamc@76
|
1483 let
|
adamc@76
|
1484 val formal = Option.map (elabSgn env) sgno
|
adamc@76
|
1485
|
adamc@78
|
1486 val (str', sgn') =
|
adamc@78
|
1487 case formal of
|
adamc@78
|
1488 NONE =>
|
adamc@78
|
1489 let
|
adamc@78
|
1490 val (str', actual) = elabStr env str
|
adamc@78
|
1491 in
|
adamc@78
|
1492 (str', selfifyAt env {str = str', sgn = actual})
|
adamc@78
|
1493 end
|
adamc@78
|
1494 | SOME formal =>
|
adamc@78
|
1495 let
|
adamc@78
|
1496 val str =
|
adamc@78
|
1497 case #1 (hnormSgn env formal) of
|
adamc@78
|
1498 L'.SgnConst sgis =>
|
adamc@78
|
1499 (case #1 str of
|
adamc@78
|
1500 L.StrConst ds =>
|
adamc@78
|
1501 let
|
adamc@78
|
1502 val needed = foldl (fn ((sgi, _), needed) =>
|
adamc@78
|
1503 case sgi of
|
adamc@78
|
1504 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
|
adamc@78
|
1505 | _ => needed)
|
adamc@78
|
1506 SS.empty sgis
|
adamc@78
|
1507
|
adamc@78
|
1508 val needed = foldl (fn ((d, _), needed) =>
|
adamc@78
|
1509 case d of
|
adamc@78
|
1510 L.DCon (x, _, _) => (SS.delete (needed, x)
|
adamc@78
|
1511 handle NotFound => needed)
|
adamc@78
|
1512 | L.DOpen _ => SS.empty
|
adamc@78
|
1513 | _ => needed)
|
adamc@78
|
1514 needed ds
|
adamc@78
|
1515 in
|
adamc@78
|
1516 case SS.listItems needed of
|
adamc@78
|
1517 [] => str
|
adamc@78
|
1518 | xs =>
|
adamc@78
|
1519 let
|
adamc@78
|
1520 val kwild = (L.KWild, #2 str)
|
adamc@78
|
1521 val cwild = (L.CWild kwild, #2 str)
|
adamc@78
|
1522 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
|
adamc@78
|
1523 in
|
adamc@78
|
1524 (L.StrConst (ds @ ds'), #2 str)
|
adamc@78
|
1525 end
|
adamc@78
|
1526 end
|
adamc@78
|
1527 | _ => str)
|
adamc@78
|
1528 | _ => str
|
adamc@78
|
1529
|
adamc@78
|
1530 val (str', actual) = elabStr env str
|
adamc@78
|
1531 in
|
adamc@78
|
1532 subSgn env actual formal;
|
adamc@78
|
1533 (str', formal)
|
adamc@78
|
1534 end
|
adamc@76
|
1535
|
adamc@76
|
1536 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@76
|
1537 in
|
adamc@76
|
1538 case #1 (hnormSgn env sgn') of
|
adamc@76
|
1539 L'.SgnFun _ =>
|
adamc@76
|
1540 (case #1 str' of
|
adamc@76
|
1541 L'.StrFun _ => ()
|
adamc@76
|
1542 | _ => strError env (FunctorRebind loc))
|
adamc@76
|
1543 | _ => ();
|
adamc@76
|
1544
|
adamc@76
|
1545 ([(L'.DStr (x, n, sgn', str'), loc)], env')
|
adamc@76
|
1546 end
|
adamc@76
|
1547
|
adamc@76
|
1548 | L.DFfiStr (x, sgn) =>
|
adamc@76
|
1549 let
|
adamc@76
|
1550 val sgn' = elabSgn env sgn
|
adamc@76
|
1551
|
adamc@76
|
1552 val (env', n) = E.pushStrNamed env x sgn'
|
adamc@76
|
1553 in
|
adamc@76
|
1554 ([(L'.DFfiStr (x, n, sgn'), loc)], env')
|
adamc@76
|
1555 end
|
adamc@76
|
1556
|
adamc@76
|
1557 | L.DOpen (m, ms) =>
|
adamc@76
|
1558 case E.lookupStr env m of
|
adamc@76
|
1559 NONE => (strError env (UnboundStr (loc, m));
|
adamc@76
|
1560 ([], env))
|
adamc@76
|
1561 | SOME (n, sgn) =>
|
adamc@30
|
1562 let
|
adamc@76
|
1563 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@76
|
1564 case E.projectStr env {str = str, sgn = sgn, field = m} of
|
adamc@76
|
1565 NONE => (strError env (UnboundStr (loc, m));
|
adamc@76
|
1566 (strerror, sgnerror))
|
adamc@76
|
1567 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
|
adamc@76
|
1568 ((L'.StrVar n, loc), sgn) ms
|
adamc@30
|
1569 in
|
adamc@76
|
1570 dopen env {str = n, strs = ms, sgn = sgn}
|
adamc@30
|
1571 end
|
adamc@3
|
1572
|
adamc@31
|
1573 and elabStr env (str, loc) =
|
adamc@31
|
1574 case str of
|
adamc@31
|
1575 L.StrConst ds =>
|
adamc@31
|
1576 let
|
adamc@61
|
1577 val (ds', env') = ListUtil.foldlMapConcat elabDecl env ds
|
adamc@59
|
1578 val sgis = map sgiOfDecl ds'
|
adamc@63
|
1579
|
adamc@63
|
1580 val (sgis, _, _, _, _) =
|
adamc@64
|
1581 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
|
adamc@63
|
1582 case sgi of
|
adamc@64
|
1583 L'.SgiConAbs (x, n, k) =>
|
adamc@64
|
1584 let
|
adamc@64
|
1585 val (cons, x) =
|
adamc@64
|
1586 if SS.member (cons, x) then
|
adamc@64
|
1587 (cons, "?" ^ x)
|
adamc@64
|
1588 else
|
adamc@64
|
1589 (SS.add (cons, x), x)
|
adamc@64
|
1590 in
|
adamc@64
|
1591 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
1592 end
|
adamc@64
|
1593 | L'.SgiCon (x, n, k, c) =>
|
adamc@64
|
1594 let
|
adamc@64
|
1595 val (cons, x) =
|
adamc@64
|
1596 if SS.member (cons, x) then
|
adamc@64
|
1597 (cons, "?" ^ x)
|
adamc@64
|
1598 else
|
adamc@64
|
1599 (SS.add (cons, x), x)
|
adamc@64
|
1600 in
|
adamc@64
|
1601 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
1602 end
|
adamc@64
|
1603 | L'.SgiVal (x, n, c) =>
|
adamc@64
|
1604 let
|
adamc@64
|
1605 val (vals, x) =
|
adamc@64
|
1606 if SS.member (vals, x) then
|
adamc@64
|
1607 (vals, "?" ^ x)
|
adamc@64
|
1608 else
|
adamc@64
|
1609 (SS.add (vals, x), x)
|
adamc@64
|
1610 in
|
adamc@64
|
1611 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
1612 end
|
adamc@64
|
1613 | L'.SgiSgn (x, n, sgn) =>
|
adamc@64
|
1614 let
|
adamc@64
|
1615 val (sgns, x) =
|
adamc@64
|
1616 if SS.member (sgns, x) then
|
adamc@64
|
1617 (sgns, "?" ^ x)
|
adamc@64
|
1618 else
|
adamc@64
|
1619 (SS.add (sgns, x), x)
|
adamc@64
|
1620 in
|
adamc@64
|
1621 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
1622 end
|
adamc@64
|
1623
|
adamc@64
|
1624 | L'.SgiStr (x, n, sgn) =>
|
adamc@64
|
1625 let
|
adamc@64
|
1626 val (strs, x) =
|
adamc@64
|
1627 if SS.member (strs, x) then
|
adamc@64
|
1628 (strs, "?" ^ x)
|
adamc@64
|
1629 else
|
adamc@64
|
1630 (SS.add (strs, x), x)
|
adamc@64
|
1631 in
|
adamc@64
|
1632 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
|
adamc@64
|
1633 end)
|
adamc@64
|
1634
|
adamc@63
|
1635 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
|
adamc@31
|
1636 in
|
adamc@31
|
1637 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc))
|
adamc@31
|
1638 end
|
adamc@31
|
1639 | L.StrVar x =>
|
adamc@31
|
1640 (case E.lookupStr env x of
|
adamc@31
|
1641 NONE =>
|
adamc@31
|
1642 (strError env (UnboundStr (loc, x));
|
adamc@31
|
1643 (strerror, sgnerror))
|
adamc@31
|
1644 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn))
|
adamc@34
|
1645 | L.StrProj (str, x) =>
|
adamc@34
|
1646 let
|
adamc@34
|
1647 val (str', sgn) = elabStr env str
|
adamc@34
|
1648 in
|
adamc@34
|
1649 case E.projectStr env {str = str', sgn = sgn, field = x} of
|
adamc@34
|
1650 NONE => (strError env (UnboundStr (loc, x));
|
adamc@34
|
1651 (strerror, sgnerror))
|
adamc@34
|
1652 | SOME sgn => ((L'.StrProj (str', x), loc), sgn)
|
adamc@34
|
1653 end
|
adamc@41
|
1654 | L.StrFun (m, dom, ranO, str) =>
|
adamc@41
|
1655 let
|
adamc@41
|
1656 val dom' = elabSgn env dom
|
adamc@41
|
1657 val (env', n) = E.pushStrNamed env m dom'
|
adamc@41
|
1658 val (str', actual) = elabStr env' str
|
adamc@41
|
1659
|
adamc@41
|
1660 val formal =
|
adamc@41
|
1661 case ranO of
|
adamc@41
|
1662 NONE => actual
|
adamc@41
|
1663 | SOME ran =>
|
adamc@41
|
1664 let
|
adamc@42
|
1665 val ran' = elabSgn env' ran
|
adamc@41
|
1666 in
|
adamc@41
|
1667 subSgn env' actual ran';
|
adamc@41
|
1668 ran'
|
adamc@41
|
1669 end
|
adamc@41
|
1670 in
|
adamc@41
|
1671 ((L'.StrFun (m, n, dom', formal, str'), loc),
|
adamc@41
|
1672 (L'.SgnFun (m, n, dom', formal), loc))
|
adamc@41
|
1673 end
|
adamc@44
|
1674 | L.StrApp (str1, str2) =>
|
adamc@44
|
1675 let
|
adamc@44
|
1676 val (str1', sgn1) = elabStr env str1
|
adamc@44
|
1677 val (str2', sgn2) = elabStr env str2
|
adamc@44
|
1678 in
|
adamc@44
|
1679 case #1 (hnormSgn env sgn1) of
|
adamc@44
|
1680 L'.SgnError => (strerror, sgnerror)
|
adamc@44
|
1681 | L'.SgnFun (m, n, dom, ran) =>
|
adamc@44
|
1682 (subSgn env sgn2 dom;
|
adamc@44
|
1683 case #1 (hnormSgn env ran) of
|
adamc@44
|
1684 L'.SgnError => (strerror, sgnerror)
|
adamc@44
|
1685 | L'.SgnConst sgis =>
|
adamc@44
|
1686 ((L'.StrApp (str1', str2'), loc),
|
adamc@44
|
1687 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc))
|
adamc@44
|
1688 | _ => raise Fail "Unable to hnormSgn in functor application")
|
adamc@44
|
1689 | _ => (strError env (NotFunctor sgn1);
|
adamc@44
|
1690 (strerror, sgnerror))
|
adamc@44
|
1691 end
|
adamc@31
|
1692
|
adamc@56
|
1693 fun elabFile basis env file =
|
adamc@56
|
1694 let
|
adamc@56
|
1695 val sgn = elabSgn env (L.SgnConst basis, ErrorMsg.dummySpan)
|
adamc@56
|
1696 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
|
adamc@56
|
1697
|
adamc@61
|
1698 val (ds, env') = dopen env' {str = basis_n, strs = [], sgn = sgn}
|
adamc@56
|
1699
|
adamc@56
|
1700 fun discoverC r x =
|
adamc@56
|
1701 case E.lookupC env' x of
|
adamc@56
|
1702 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
|
adamc@56
|
1703 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
|
adamc@56
|
1704 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
|
adamc@56
|
1705
|
adamc@56
|
1706 val () = discoverC int "int"
|
adamc@56
|
1707 val () = discoverC float "float"
|
adamc@56
|
1708 val () = discoverC string "string"
|
adamc@56
|
1709
|
adamc@76
|
1710 fun elabDecl' (d, env) =
|
adamc@76
|
1711 let
|
adamc@76
|
1712 val () = resetKunif ()
|
adamc@76
|
1713 val () = resetCunif ()
|
adamc@76
|
1714 val (ds, env) = elabDecl (d, env)
|
adamc@76
|
1715 in
|
adamc@76
|
1716 if ErrorMsg.anyErrors () then
|
adamc@76
|
1717 ()
|
adamc@76
|
1718 else (
|
adamc@76
|
1719 if List.exists kunifsInDecl ds then
|
adamc@76
|
1720 declError env (KunifsRemain (#2 d))
|
adamc@76
|
1721 else
|
adamc@76
|
1722 ();
|
adamc@76
|
1723
|
adamc@76
|
1724 case ListUtil.search cunifsInDecl ds of
|
adamc@76
|
1725 NONE => ()
|
adamc@76
|
1726 | SOME loc =>
|
adamc@76
|
1727 declError env (CunifsRemain loc)
|
adamc@76
|
1728 );
|
adamc@76
|
1729
|
adamc@76
|
1730 (ds, env)
|
adamc@76
|
1731 end
|
adamc@76
|
1732
|
adamc@76
|
1733 val (file, _) = ListUtil.foldlMapConcat elabDecl' env' file
|
adamc@56
|
1734 in
|
adamc@56
|
1735 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
|
adamc@56
|
1736 end
|
adamc@2
|
1737
|
adamc@2
|
1738 end
|