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@4
|
30 structure L = Source
|
adamc@2
|
31 structure L' = Elab
|
adamc@2
|
32 structure E = ElabEnv
|
adamc@2
|
33 structure U = ElabUtil
|
adamc@2
|
34
|
adamc@3
|
35 open Print
|
adamc@3
|
36 open ElabPrint
|
adamc@3
|
37
|
adamc@2
|
38 fun elabKind (k, loc) =
|
adamc@2
|
39 case k of
|
adamc@2
|
40 L.KType => (L'.KType, loc)
|
adamc@2
|
41 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
|
adamc@2
|
42 | L.KName => (L'.KName, loc)
|
adamc@2
|
43 | L.KRecord k => (L'.KRecord (elabKind k), loc)
|
adamc@2
|
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@2
|
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@2
|
77 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
|
adamc@3
|
78 (unifyKinds' d1 d2;
|
adamc@3
|
79 unifyKinds' r1 r2)
|
adamc@2
|
80 | (L'.KName, L'.KName) => ()
|
adamc@3
|
81 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
|
adamc@2
|
82
|
adamc@2
|
83 | (L'.KError, _) => ()
|
adamc@2
|
84 | (_, L'.KError) => ()
|
adamc@2
|
85
|
adamc@3
|
86 | (L'.KUnif (_, ref (SOME k1All)), _) => unifyKinds' k1All k2All
|
adamc@3
|
87 | (_, L'.KUnif (_, ref (SOME k2All))) => unifyKinds' k1All k2All
|
adamc@2
|
88
|
adamc@2
|
89 | (L'.KUnif (_, r1), L'.KUnif (_, r2)) =>
|
adamc@2
|
90 if r1 = r2 then
|
adamc@2
|
91 ()
|
adamc@2
|
92 else
|
adamc@2
|
93 r1 := SOME k2All
|
adamc@2
|
94
|
adamc@2
|
95 | (L'.KUnif (_, r), _) =>
|
adamc@2
|
96 if occursKind r k2All then
|
adamc@2
|
97 err KOccursCheckFailed
|
adamc@2
|
98 else
|
adamc@2
|
99 r := SOME k2All
|
adamc@2
|
100 | (_, L'.KUnif (_, r)) =>
|
adamc@2
|
101 if occursKind r k1All then
|
adamc@2
|
102 err KOccursCheckFailed
|
adamc@2
|
103 else
|
adamc@2
|
104 r := SOME k1All
|
adamc@2
|
105
|
adamc@2
|
106 | _ => err KIncompatible
|
adamc@2
|
107 end
|
adamc@2
|
108
|
adamc@3
|
109 exception KUnify of L'.kind * L'.kind * kunify_error
|
adamc@3
|
110
|
adamc@3
|
111 fun unifyKinds k1 k2 =
|
adamc@3
|
112 unifyKinds' k1 k2
|
adamc@3
|
113 handle KUnify' err => raise KUnify (k1, k2, err)
|
adamc@3
|
114
|
adamc@3
|
115 datatype con_error =
|
adamc@3
|
116 UnboundCon of ErrorMsg.span * string
|
adamc@3
|
117 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
|
adamc@3
|
118
|
adamc@5
|
119 fun conError env err =
|
adamc@3
|
120 case err of
|
adamc@3
|
121 UnboundCon (loc, s) =>
|
adamc@3
|
122 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
|
adamc@3
|
123 | WrongKind (c, k1, k2, kerr) =>
|
adamc@3
|
124 (ErrorMsg.errorAt (#2 c) "Wrong kind";
|
adamc@5
|
125 eprefaces' [("Constructor", p_con env c),
|
adamc@5
|
126 ("Have kind", p_kind k1),
|
adamc@5
|
127 ("Need kind", p_kind k2)];
|
adamc@3
|
128 kunifyError kerr)
|
adamc@3
|
129
|
adamc@5
|
130 fun checkKind env c k1 k2 =
|
adamc@3
|
131 unifyKinds k1 k2
|
adamc@3
|
132 handle KUnify (k1, k2, err) =>
|
adamc@5
|
133 conError env (WrongKind (c, k1, k2, err))
|
adamc@3
|
134
|
adamc@3
|
135 val dummy = ErrorMsg.dummySpan
|
adamc@3
|
136
|
adamc@3
|
137 val ktype = (L'.KType, dummy)
|
adamc@3
|
138 val kname = (L'.KName, dummy)
|
adamc@12
|
139 val ktype_record = (L'.KRecord ktype, dummy)
|
adamc@3
|
140
|
adamc@3
|
141 val cerror = (L'.CError, dummy)
|
adamc@3
|
142 val kerror = (L'.KError, dummy)
|
adamc@10
|
143 val eerror = (L'.EError, dummy)
|
adamc@3
|
144
|
adamc@3
|
145 local
|
adamc@3
|
146 val count = ref 0
|
adamc@3
|
147 in
|
adamc@3
|
148
|
adamc@3
|
149 fun resetKunif () = count := 0
|
adamc@3
|
150
|
adamc@3
|
151 fun kunif () =
|
adamc@3
|
152 let
|
adamc@3
|
153 val n = !count
|
adamc@3
|
154 val s = if n <= 26 then
|
adamc@3
|
155 str (chr (ord #"A" + n))
|
adamc@3
|
156 else
|
adamc@3
|
157 "U" ^ Int.toString (n - 26)
|
adamc@3
|
158 in
|
adamc@3
|
159 count := n + 1;
|
adamc@3
|
160 (L'.KUnif (s, ref NONE), dummy)
|
adamc@3
|
161 end
|
adamc@3
|
162
|
adamc@3
|
163 end
|
adamc@3
|
164
|
adamc@10
|
165 local
|
adamc@10
|
166 val count = ref 0
|
adamc@10
|
167 in
|
adamc@10
|
168
|
adamc@10
|
169 fun resetCunif () = count := 0
|
adamc@10
|
170
|
adamc@10
|
171 fun cunif k =
|
adamc@10
|
172 let
|
adamc@10
|
173 val n = !count
|
adamc@10
|
174 val s = if n <= 26 then
|
adamc@10
|
175 str (chr (ord #"A" + n))
|
adamc@10
|
176 else
|
adamc@10
|
177 "U" ^ Int.toString (n - 26)
|
adamc@10
|
178 in
|
adamc@10
|
179 count := n + 1;
|
adamc@10
|
180 (L'.CUnif (k, s, ref NONE), dummy)
|
adamc@10
|
181 end
|
adamc@10
|
182
|
adamc@10
|
183 end
|
adamc@10
|
184
|
adamc@3
|
185 fun elabCon env (c, loc) =
|
adamc@3
|
186 case c of
|
adamc@3
|
187 L.CAnnot (c, k) =>
|
adamc@3
|
188 let
|
adamc@3
|
189 val k' = elabKind k
|
adamc@3
|
190 val (c', ck) = elabCon env c
|
adamc@3
|
191 in
|
adamc@5
|
192 checkKind env c' ck k';
|
adamc@3
|
193 (c', k')
|
adamc@3
|
194 end
|
adamc@3
|
195
|
adamc@3
|
196 | L.TFun (t1, t2) =>
|
adamc@3
|
197 let
|
adamc@3
|
198 val (t1', k1) = elabCon env t1
|
adamc@3
|
199 val (t2', k2) = elabCon env t2
|
adamc@3
|
200 in
|
adamc@5
|
201 checkKind env t1' k1 ktype;
|
adamc@5
|
202 checkKind env t2' k2 ktype;
|
adamc@3
|
203 ((L'.TFun (t1', t2'), loc), ktype)
|
adamc@3
|
204 end
|
adamc@3
|
205 | L.TCFun (e, x, k, t) =>
|
adamc@3
|
206 let
|
adamc@3
|
207 val e' = elabExplicitness e
|
adamc@3
|
208 val k' = elabKind k
|
adamc@3
|
209 val env' = E.pushCRel env x k'
|
adamc@3
|
210 val (t', tk) = elabCon env' t
|
adamc@3
|
211 in
|
adamc@5
|
212 checkKind env t' tk ktype;
|
adamc@3
|
213 ((L'.TCFun (e', x, k', t'), loc), ktype)
|
adamc@3
|
214 end
|
adamc@3
|
215 | L.TRecord c =>
|
adamc@3
|
216 let
|
adamc@3
|
217 val (c', ck) = elabCon env c
|
adamc@3
|
218 val k = (L'.KRecord ktype, loc)
|
adamc@3
|
219 in
|
adamc@5
|
220 checkKind env c' ck k;
|
adamc@3
|
221 ((L'.TRecord c', loc), ktype)
|
adamc@3
|
222 end
|
adamc@3
|
223
|
adamc@3
|
224 | L.CVar s =>
|
adamc@3
|
225 (case E.lookupC env s of
|
adamc@9
|
226 E.NotBound =>
|
adamc@5
|
227 (conError env (UnboundCon (loc, s));
|
adamc@3
|
228 (cerror, kerror))
|
adamc@9
|
229 | E.Rel (n, k) =>
|
adamc@3
|
230 ((L'.CRel n, loc), k)
|
adamc@9
|
231 | E.Named (n, k) =>
|
adamc@3
|
232 ((L'.CNamed n, loc), k))
|
adamc@3
|
233 | L.CApp (c1, c2) =>
|
adamc@3
|
234 let
|
adamc@3
|
235 val (c1', k1) = elabCon env c1
|
adamc@3
|
236 val (c2', k2) = elabCon env c2
|
adamc@3
|
237 val dom = kunif ()
|
adamc@3
|
238 val ran = kunif ()
|
adamc@3
|
239 in
|
adamc@5
|
240 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
|
adamc@5
|
241 checkKind env c2' k2 dom;
|
adamc@3
|
242 ((L'.CApp (c1', c2'), loc), ran)
|
adamc@3
|
243 end
|
adamc@8
|
244 | L.CAbs (x, k, t) =>
|
adamc@3
|
245 let
|
adamc@3
|
246 val k' = elabKind k
|
adamc@3
|
247 val env' = E.pushCRel env x k'
|
adamc@3
|
248 val (t', tk) = elabCon env' t
|
adamc@3
|
249 in
|
adamc@8
|
250 ((L'.CAbs (x, k', t'), loc),
|
adamc@3
|
251 (L'.KArrow (k', tk), loc))
|
adamc@3
|
252 end
|
adamc@3
|
253
|
adamc@3
|
254 | L.CName s =>
|
adamc@3
|
255 ((L'.CName s, loc), kname)
|
adamc@3
|
256
|
adamc@3
|
257 | L.CRecord xcs =>
|
adamc@3
|
258 let
|
adamc@3
|
259 val k = kunif ()
|
adamc@3
|
260
|
adamc@3
|
261 val xcs' = map (fn (x, c) =>
|
adamc@3
|
262 let
|
adamc@3
|
263 val (x', xk) = elabCon env x
|
adamc@3
|
264 val (c', ck) = elabCon env c
|
adamc@3
|
265 in
|
adamc@5
|
266 checkKind env x' xk kname;
|
adamc@5
|
267 checkKind env c' ck k;
|
adamc@3
|
268 (x', c')
|
adamc@3
|
269 end) xcs
|
adamc@3
|
270 in
|
adamc@5
|
271 ((L'.CRecord (k, xcs'), loc), (L'.KRecord k, loc))
|
adamc@3
|
272 end
|
adamc@3
|
273 | L.CConcat (c1, c2) =>
|
adamc@3
|
274 let
|
adamc@3
|
275 val (c1', k1) = elabCon env c1
|
adamc@3
|
276 val (c2', k2) = elabCon env c2
|
adamc@3
|
277 val ku = kunif ()
|
adamc@3
|
278 val k = (L'.KRecord ku, loc)
|
adamc@3
|
279 in
|
adamc@5
|
280 checkKind env c1' k1 k;
|
adamc@5
|
281 checkKind env c2' k2 k;
|
adamc@3
|
282 ((L'.CConcat (c1', c2'), loc), k)
|
adamc@3
|
283 end
|
adamc@3
|
284
|
adamc@6
|
285 fun kunifsRemain k =
|
adamc@6
|
286 case k of
|
adamc@6
|
287 L'.KUnif (_, ref NONE) => true
|
adamc@6
|
288 | _ => false
|
adamc@10
|
289 fun cunifsRemain c =
|
adamc@10
|
290 case c of
|
adamc@10
|
291 L'.CUnif (_, _, ref NONE) => true
|
adamc@10
|
292 | _ => false
|
adamc@6
|
293
|
adamc@6
|
294 val kunifsInKind = U.Kind.exists kunifsRemain
|
adamc@6
|
295 val kunifsInCon = U.Con.exists {kind = kunifsRemain,
|
adamc@6
|
296 con = fn _ => false}
|
adamc@10
|
297 val kunifsInExp = U.Exp.exists {kind = kunifsRemain,
|
adamc@10
|
298 con = fn _ => false,
|
adamc@10
|
299 exp = fn _ => false}
|
adamc@10
|
300
|
adamc@10
|
301 val cunifsInCon = U.Con.exists {kind = fn _ => false,
|
adamc@10
|
302 con = cunifsRemain}
|
adamc@10
|
303 val cunifsInExp = U.Exp.exists {kind = fn _ => false,
|
adamc@10
|
304 con = cunifsRemain,
|
adamc@10
|
305 exp = fn _ => false}
|
adamc@10
|
306
|
adamc@10
|
307 fun occursCon r =
|
adamc@10
|
308 U.Con.exists {kind = fn _ => false,
|
adamc@10
|
309 con = fn L'.CUnif (_, _, r') => r = r'
|
adamc@10
|
310 | _ => false}
|
adamc@10
|
311
|
adamc@10
|
312 datatype cunify_error =
|
adamc@10
|
313 CKind of L'.kind * L'.kind * kunify_error
|
adamc@10
|
314 | COccursCheckFailed of L'.con * L'.con
|
adamc@10
|
315 | CIncompatible of L'.con * L'.con
|
adamc@10
|
316 | CExplicitness of L'.con * L'.con
|
adamc@12
|
317 | CKindof of L'.con
|
adamc@12
|
318 | CRecordFailure
|
adamc@10
|
319
|
adamc@10
|
320 exception CUnify' of cunify_error
|
adamc@10
|
321
|
adamc@10
|
322 fun cunifyError env err =
|
adamc@10
|
323 case err of
|
adamc@10
|
324 CKind (k1, k2, kerr) =>
|
adamc@10
|
325 (eprefaces "Kind unification failure"
|
adamc@10
|
326 [("Kind 1", p_kind k1),
|
adamc@10
|
327 ("Kind 2", p_kind k2)];
|
adamc@10
|
328 kunifyError kerr)
|
adamc@10
|
329 | COccursCheckFailed (c1, c2) =>
|
adamc@10
|
330 eprefaces "Constructor occurs check failed"
|
adamc@10
|
331 [("Con 1", p_con env c1),
|
adamc@10
|
332 ("Con 2", p_con env c2)]
|
adamc@10
|
333 | CIncompatible (c1, c2) =>
|
adamc@10
|
334 eprefaces "Incompatible constructors"
|
adamc@10
|
335 [("Con 1", p_con env c1),
|
adamc@10
|
336 ("Con 2", p_con env c2)]
|
adamc@10
|
337 | CExplicitness (c1, c2) =>
|
adamc@10
|
338 eprefaces "Differing constructor function explicitness"
|
adamc@10
|
339 [("Con 1", p_con env c1),
|
adamc@10
|
340 ("Con 2", p_con env c2)]
|
adamc@12
|
341 | CKindof c =>
|
adamc@12
|
342 eprefaces "Kind unification variable blocks kindof calculation"
|
adamc@12
|
343 [("Con", p_con env c)]
|
adamc@12
|
344 | CRecordFailure =>
|
adamc@12
|
345 eprefaces "Can't unify record constructors" []
|
adamc@10
|
346
|
adamc@12
|
347 exception SynUnif
|
adamc@12
|
348
|
adamc@12
|
349 val liftConInCon =
|
adamc@12
|
350 U.Con.mapB {kind = fn k => k,
|
adamc@12
|
351 con = fn bound => fn c =>
|
adamc@12
|
352 case c of
|
adamc@12
|
353 L'.CRel xn =>
|
adamc@12
|
354 if xn < bound then
|
adamc@12
|
355 c
|
adamc@12
|
356 else
|
adamc@12
|
357 L'.CRel (xn + 1)
|
adamc@12
|
358 | L'.CUnif _ => raise SynUnif
|
adamc@12
|
359 | _ => c,
|
adamc@12
|
360 bind = fn (bound, U.Con.Rel _) => bound + 1
|
adamc@12
|
361 | (bound, _) => bound}
|
adamc@12
|
362
|
adamc@12
|
363 val subConInCon =
|
adamc@12
|
364 U.Con.mapB {kind = fn k => k,
|
adamc@12
|
365 con = fn (xn, rep) => fn c =>
|
adamc@12
|
366 case c of
|
adamc@12
|
367 L'.CRel xn' =>
|
adamc@12
|
368 if xn = xn' then
|
adamc@12
|
369 #1 rep
|
adamc@12
|
370 else
|
adamc@12
|
371 c
|
adamc@12
|
372 | L'.CUnif _ => raise SynUnif
|
adamc@12
|
373 | _ => c,
|
adamc@12
|
374 bind = fn ((xn, rep), U.Con.Rel _) => (xn+1, liftConInCon 0 rep)
|
adamc@12
|
375 | (ctx, _) => ctx}
|
adamc@12
|
376
|
adamc@12
|
377 type record_summary = {
|
adamc@12
|
378 fields : (L'.con * L'.con) list,
|
adamc@12
|
379 unifs : (L'.con * L'.con option ref) list,
|
adamc@12
|
380 others : L'.con list
|
adamc@12
|
381 }
|
adamc@12
|
382
|
adamc@12
|
383 fun summaryToCon {fields, unifs, others} =
|
adamc@12
|
384 let
|
adamc@12
|
385 val c = (L'.CRecord (ktype, []), dummy)
|
adamc@12
|
386 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
|
adamc@12
|
387 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
|
adamc@12
|
388 in
|
adamc@12
|
389 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
|
adamc@12
|
390 end
|
adamc@12
|
391
|
adamc@12
|
392 fun p_summary env s = p_con env (summaryToCon s)
|
adamc@12
|
393
|
adamc@12
|
394 exception CUnify of L'.con * L'.con * cunify_error
|
adamc@12
|
395
|
adamc@12
|
396 fun hnormKind (kAll as (k, _)) =
|
adamc@12
|
397 case k of
|
adamc@12
|
398 L'.KUnif (_, ref (SOME k)) => hnormKind k
|
adamc@12
|
399 | _ => kAll
|
adamc@12
|
400
|
adamc@12
|
401 fun kindof env (c, loc) =
|
adamc@12
|
402 case c of
|
adamc@12
|
403 L'.TFun _ => ktype
|
adamc@12
|
404 | L'.TCFun _ => ktype
|
adamc@12
|
405 | L'.TRecord _ => ktype
|
adamc@12
|
406
|
adamc@12
|
407 | L'.CRel xn => #2 (E.lookupCRel env xn)
|
adamc@12
|
408 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
|
adamc@12
|
409 | L'.CApp (c, _) =>
|
adamc@12
|
410 (case #1 (hnormKind (kindof env c)) of
|
adamc@12
|
411 L'.KArrow (_, k) => k
|
adamc@12
|
412 | L'.KError => kerror
|
adamc@12
|
413 | _ => raise CUnify' (CKindof c))
|
adamc@12
|
414 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
|
adamc@12
|
415
|
adamc@12
|
416 | L'.CName _ => kname
|
adamc@12
|
417
|
adamc@12
|
418 | L'.CRecord (k, _) => (L'.KRecord k, loc)
|
adamc@12
|
419 | L'.CConcat (c, _) => kindof env c
|
adamc@12
|
420
|
adamc@12
|
421 | L'.CError => kerror
|
adamc@12
|
422 | L'.CUnif (k, _, _) => k
|
adamc@12
|
423
|
adamc@12
|
424 fun unifyRecordCons env (c1, c2) =
|
adamc@12
|
425 let
|
adamc@12
|
426 val k1 = kindof env c1
|
adamc@12
|
427 val k2 = kindof env c2
|
adamc@12
|
428 in
|
adamc@12
|
429 unifyKinds k1 k2;
|
adamc@12
|
430 unifySummaries env (k1, recordSummary env c1, recordSummary env c2)
|
adamc@12
|
431 end
|
adamc@12
|
432
|
adamc@12
|
433 and recordSummary env c : record_summary =
|
adamc@12
|
434 case hnormCon env c of
|
adamc@12
|
435 (L'.CRecord (_, xcs), _) => {fields = xcs, unifs = [], others = []}
|
adamc@12
|
436 | (L'.CConcat (c1, c2), _) =>
|
adamc@12
|
437 let
|
adamc@12
|
438 val s1 = recordSummary env c1
|
adamc@12
|
439 val s2 = recordSummary env c2
|
adamc@12
|
440 in
|
adamc@12
|
441 {fields = #fields s1 @ #fields s2,
|
adamc@12
|
442 unifs = #unifs s1 @ #unifs s2,
|
adamc@12
|
443 others = #others s1 @ #others s2}
|
adamc@12
|
444 end
|
adamc@12
|
445 | (L'.CUnif (_, _, ref (SOME c)), _) => recordSummary env c
|
adamc@12
|
446 | c' as (L'.CUnif (_, _, r), _) => {fields = [], unifs = [(c', r)], others = []}
|
adamc@12
|
447 | c' => {fields = [], unifs = [], others = [c']}
|
adamc@12
|
448
|
adamc@12
|
449 and consEq env (c1, c2) =
|
adamc@12
|
450 (unifyCons env c1 c2;
|
adamc@12
|
451 true)
|
adamc@12
|
452 handle CUnify _ => false
|
adamc@12
|
453
|
adamc@12
|
454 and unifySummaries env (k, s1 : record_summary, s2 : record_summary) =
|
adamc@12
|
455 let
|
adamc@12
|
456 val () = eprefaces "Summaries" [("#1", p_summary env s1),
|
adamc@12
|
457 ("#2", p_summary env s2)]
|
adamc@12
|
458
|
adamc@12
|
459 fun eatMatching p (ls1, ls2) =
|
adamc@12
|
460 let
|
adamc@12
|
461 fun em (ls1, ls2, passed1) =
|
adamc@12
|
462 case ls1 of
|
adamc@12
|
463 [] => (rev passed1, ls2)
|
adamc@12
|
464 | h1 :: t1 =>
|
adamc@12
|
465 let
|
adamc@12
|
466 fun search (ls2', passed2) =
|
adamc@12
|
467 case ls2' of
|
adamc@12
|
468 [] => em (t1, ls2, h1 :: passed1)
|
adamc@12
|
469 | h2 :: t2 =>
|
adamc@12
|
470 if p (h1, h2) then
|
adamc@12
|
471 em (t1, List.revAppend (passed2, t2), passed1)
|
adamc@12
|
472 else
|
adamc@12
|
473 search (t2, h2 :: passed2)
|
adamc@12
|
474 in
|
adamc@12
|
475 search (ls2, [])
|
adamc@12
|
476 end
|
adamc@12
|
477 in
|
adamc@12
|
478 em (ls1, ls2, [])
|
adamc@12
|
479 end
|
adamc@12
|
480
|
adamc@12
|
481 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
|
adamc@12
|
482 if consEq env (x1, x2) then
|
adamc@12
|
483 (unifyCons env c1 c2;
|
adamc@12
|
484 true)
|
adamc@12
|
485 else
|
adamc@12
|
486 false) (#fields s1, #fields s2)
|
adamc@12
|
487 val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
|
adamc@12
|
488 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]
|
adamc@12
|
489 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
|
adamc@12
|
490 val (others1, others2) = eatMatching (consEq env) (#others s1, #others s2)
|
adamc@12
|
491
|
adamc@12
|
492 fun unifFields (fs, others, unifs) =
|
adamc@12
|
493 case (fs, others, unifs) of
|
adamc@12
|
494 ([], [], _) => ([], [], unifs)
|
adamc@12
|
495 | (_, _, []) => (fs, others, [])
|
adamc@12
|
496 | (_, _, (_, r) :: rest) =>
|
adamc@12
|
497 let
|
adamc@12
|
498 val r' = ref NONE
|
adamc@12
|
499 val cr' = (L'.CUnif (k, "recd", r'), dummy)
|
adamc@12
|
500
|
adamc@12
|
501 val prefix = case (fs, others) of
|
adamc@12
|
502 ([], other :: others) =>
|
adamc@12
|
503 List.foldl (fn (other, c) =>
|
adamc@12
|
504 (L'.CConcat (c, other), dummy))
|
adamc@12
|
505 other others
|
adamc@12
|
506 | (fs, []) =>
|
adamc@12
|
507 (L'.CRecord (k, fs), dummy)
|
adamc@12
|
508 | (fs, others) =>
|
adamc@12
|
509 List.foldl (fn (other, c) =>
|
adamc@12
|
510 (L'.CConcat (c, other), dummy))
|
adamc@12
|
511 (L'.CRecord (k, fs), dummy) others
|
adamc@12
|
512 in
|
adamc@12
|
513 r := SOME (L'.CConcat (prefix, cr'), dummy);
|
adamc@12
|
514 ([], [], (cr', r') :: rest)
|
adamc@12
|
515 end
|
adamc@12
|
516
|
adamc@12
|
517 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
|
adamc@12
|
518 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
|
adamc@12
|
519
|
adamc@12
|
520 val clear1 = case (fs1, others1) of
|
adamc@12
|
521 ([], []) => true
|
adamc@12
|
522 | _ => false
|
adamc@12
|
523 val clear2 = case (fs2, others2) of
|
adamc@12
|
524 ([], []) => true
|
adamc@12
|
525 | _ => false
|
adamc@12
|
526 val empty = (L'.CRecord (k, []), dummy)
|
adamc@12
|
527 fun pairOffUnifs (unifs1, unifs2) =
|
adamc@12
|
528 case (unifs1, unifs2) of
|
adamc@12
|
529 ([], _) =>
|
adamc@12
|
530 if clear1 then
|
adamc@12
|
531 List.app (fn (_, r) => r := SOME empty) unifs2
|
adamc@12
|
532 else
|
adamc@12
|
533 raise CUnify' CRecordFailure
|
adamc@12
|
534 | (_, []) =>
|
adamc@12
|
535 if clear2 then
|
adamc@12
|
536 List.app (fn (_, r) => r := SOME empty) unifs1
|
adamc@12
|
537 else
|
adamc@12
|
538 raise CUnify' CRecordFailure
|
adamc@12
|
539 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
|
adamc@12
|
540 (r2 := SOME c1;
|
adamc@12
|
541 pairOffUnifs (rest1, rest2))
|
adamc@12
|
542 in
|
adamc@12
|
543 pairOffUnifs (unifs1, unifs2)
|
adamc@12
|
544 end
|
adamc@12
|
545
|
adamc@12
|
546 and hnormCon env (cAll as (c, _)) =
|
adamc@11
|
547 case c of
|
adamc@11
|
548 L'.CUnif (_, _, ref (SOME c)) => hnormCon env c
|
adamc@11
|
549
|
adamc@11
|
550 | L'.CNamed xn =>
|
adamc@11
|
551 (case E.lookupCNamed env xn of
|
adamc@11
|
552 (_, _, SOME c') => hnormCon env c'
|
adamc@11
|
553 | _ => cAll)
|
adamc@11
|
554
|
adamc@12
|
555 | L'.CApp (c1, c2) =>
|
adamc@12
|
556 (case hnormCon env c1 of
|
adamc@12
|
557 (L'.CAbs (_, _, cb), _) =>
|
adamc@12
|
558 ((hnormCon env (subConInCon (0, c2) cb))
|
adamc@12
|
559 handle SynUnif => cAll)
|
adamc@12
|
560 | _ => cAll)
|
adamc@12
|
561
|
adamc@12
|
562 | L'.CConcat (c1, c2) =>
|
adamc@12
|
563 (case (hnormCon env c1, hnormCon env c2) of
|
adamc@12
|
564 ((L'.CRecord (k, xcs1), loc), (L'.CRecord (_, xcs2), _)) =>
|
adamc@12
|
565 (L'.CRecord (k, xcs1 @ xcs2), loc)
|
adamc@12
|
566 | _ => cAll)
|
adamc@12
|
567
|
adamc@11
|
568 | _ => cAll
|
adamc@11
|
569
|
adamc@12
|
570 and unifyCons' env c1 c2 =
|
adamc@11
|
571 unifyCons'' env (hnormCon env c1) (hnormCon env c2)
|
adamc@11
|
572
|
adamc@11
|
573 and unifyCons'' env (c1All as (c1, _)) (c2All as (c2, _)) =
|
adamc@10
|
574 let
|
adamc@10
|
575 fun err f = raise CUnify' (f (c1All, c2All))
|
adamc@12
|
576
|
adamc@12
|
577 fun isRecord () = unifyRecordCons env (c1All, c2All)
|
adamc@10
|
578 in
|
adamc@10
|
579 case (c1, c2) of
|
adamc@10
|
580 (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
|
adamc@10
|
581 (unifyCons' env d1 d2;
|
adamc@10
|
582 unifyCons' env r1 r2)
|
adamc@10
|
583 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
|
adamc@10
|
584 if expl1 <> expl2 then
|
adamc@10
|
585 err CExplicitness
|
adamc@10
|
586 else
|
adamc@10
|
587 (unifyKinds d1 d2;
|
adamc@10
|
588 unifyCons' (E.pushCRel env x1 d1) r1 r2)
|
adamc@10
|
589 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env r1 r2
|
adamc@10
|
590
|
adamc@10
|
591 | (L'.CRel n1, L'.CRel n2) =>
|
adamc@10
|
592 if n1 = n2 then
|
adamc@10
|
593 ()
|
adamc@10
|
594 else
|
adamc@10
|
595 err CIncompatible
|
adamc@10
|
596 | (L'.CNamed n1, L'.CNamed n2) =>
|
adamc@10
|
597 if n1 = n2 then
|
adamc@10
|
598 ()
|
adamc@10
|
599 else
|
adamc@10
|
600 err CIncompatible
|
adamc@10
|
601
|
adamc@10
|
602 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
|
adamc@10
|
603 (unifyCons' env d1 d2;
|
adamc@10
|
604 unifyCons' env r1 r2)
|
adamc@10
|
605 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
|
adamc@10
|
606 (unifyKinds k1 k2;
|
adamc@10
|
607 unifyCons' (E.pushCRel env x1 k1) c1 c2)
|
adamc@10
|
608
|
adamc@10
|
609 | (L'.CName n1, L'.CName n2) =>
|
adamc@10
|
610 if n1 = n2 then
|
adamc@10
|
611 ()
|
adamc@10
|
612 else
|
adamc@10
|
613 err CIncompatible
|
adamc@10
|
614
|
adamc@10
|
615 | (L'.CError, _) => ()
|
adamc@10
|
616 | (_, L'.CError) => ()
|
adamc@10
|
617
|
adamc@10
|
618 | (L'.CUnif (_, _, ref (SOME c1All)), _) => unifyCons' env c1All c2All
|
adamc@10
|
619 | (_, L'.CUnif (_, _, ref (SOME c2All))) => unifyCons' env c1All c2All
|
adamc@10
|
620
|
adamc@10
|
621 | (L'.CUnif (k1, _, r1), L'.CUnif (k2, _, r2)) =>
|
adamc@10
|
622 if r1 = r2 then
|
adamc@10
|
623 ()
|
adamc@10
|
624 else
|
adamc@10
|
625 (unifyKinds k1 k2;
|
adamc@10
|
626 r1 := SOME c2All)
|
adamc@10
|
627
|
adamc@10
|
628 | (L'.CUnif (_, _, r), _) =>
|
adamc@10
|
629 if occursCon r c2All then
|
adamc@10
|
630 err COccursCheckFailed
|
adamc@10
|
631 else
|
adamc@10
|
632 r := SOME c2All
|
adamc@10
|
633 | (_, L'.CUnif (_, _, r)) =>
|
adamc@10
|
634 if occursCon r c1All then
|
adamc@10
|
635 err COccursCheckFailed
|
adamc@10
|
636 else
|
adamc@10
|
637 r := SOME c1All
|
adamc@10
|
638
|
adamc@12
|
639 | (L'.CRecord _, _) => isRecord ()
|
adamc@12
|
640 | (_, L'.CRecord _) => isRecord ()
|
adamc@12
|
641 | (L'.CConcat _, _) => isRecord ()
|
adamc@12
|
642 | (_, L'.CConcat _) => isRecord ()
|
adamc@12
|
643
|
adamc@10
|
644 | _ => err CIncompatible
|
adamc@10
|
645 end
|
adamc@10
|
646
|
adamc@12
|
647 and unifyCons env c1 c2 =
|
adamc@10
|
648 unifyCons' env c1 c2
|
adamc@10
|
649 handle CUnify' err => raise CUnify (c1, c2, err)
|
adamc@10
|
650 | KUnify args => raise CUnify (c1, c2, CKind args)
|
adamc@10
|
651
|
adamc@10
|
652 datatype exp_error =
|
adamc@10
|
653 UnboundExp of ErrorMsg.span * string
|
adamc@10
|
654 | Unify of L'.exp * L'.con * L'.con * cunify_error
|
adamc@11
|
655 | Unif of string * L'.con
|
adamc@11
|
656 | WrongForm of string * L'.exp * L'.con
|
adamc@10
|
657
|
adamc@10
|
658 fun expError env err =
|
adamc@10
|
659 case err of
|
adamc@10
|
660 UnboundExp (loc, s) =>
|
adamc@10
|
661 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
|
adamc@10
|
662 | Unify (e, c1, c2, uerr) =>
|
adamc@10
|
663 (ErrorMsg.errorAt (#2 e) "Unification failure";
|
adamc@10
|
664 eprefaces' [("Expression", p_exp env e),
|
adamc@10
|
665 ("Have con", p_con env c1),
|
adamc@10
|
666 ("Need con", p_con env c2)];
|
adamc@10
|
667 cunifyError env uerr)
|
adamc@11
|
668 | Unif (action, c) =>
|
adamc@11
|
669 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
|
adamc@11
|
670 eprefaces' [("Con", p_con env c)])
|
adamc@11
|
671 | WrongForm (variety, e, t) =>
|
adamc@11
|
672 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
|
adamc@11
|
673 eprefaces' [("Expression", p_exp env e),
|
adamc@11
|
674 ("Type", p_con env t)])
|
adamc@10
|
675
|
adamc@10
|
676 fun checkCon env e c1 c2 =
|
adamc@10
|
677 unifyCons env c1 c2
|
adamc@10
|
678 handle CUnify (c1, c2, err) =>
|
adamc@10
|
679 expError env (Unify (e, c1, c2, err))
|
adamc@10
|
680
|
adamc@10
|
681 fun elabExp env (e, loc) =
|
adamc@10
|
682 case e of
|
adamc@10
|
683 L.EAnnot (e, t) =>
|
adamc@10
|
684 let
|
adamc@10
|
685 val (e', et) = elabExp env e
|
adamc@10
|
686 val (t', _) = elabCon env t
|
adamc@10
|
687 in
|
adamc@10
|
688 checkCon env e' et t';
|
adamc@10
|
689 (e', t')
|
adamc@10
|
690 end
|
adamc@10
|
691
|
adamc@10
|
692 | L.EVar s =>
|
adamc@10
|
693 (case E.lookupE env s of
|
adamc@10
|
694 E.NotBound =>
|
adamc@10
|
695 (expError env (UnboundExp (loc, s));
|
adamc@10
|
696 (eerror, cerror))
|
adamc@10
|
697 | E.Rel (n, t) => ((L'.ERel n, loc), t)
|
adamc@10
|
698 | E.Named (n, t) => ((L'.ENamed n, loc), t))
|
adamc@10
|
699 | L.EApp (e1, e2) =>
|
adamc@10
|
700 let
|
adamc@10
|
701 val (e1', t1) = elabExp env e1
|
adamc@10
|
702 val (e2', t2) = elabExp env e2
|
adamc@10
|
703
|
adamc@10
|
704 val dom = cunif ktype
|
adamc@10
|
705 val ran = cunif ktype
|
adamc@10
|
706 val t = (L'.TFun (dom, ran), dummy)
|
adamc@10
|
707 in
|
adamc@10
|
708 checkCon env e1' t1 t;
|
adamc@10
|
709 checkCon env e2' t2 dom;
|
adamc@10
|
710 ((L'.EApp (e1', e2'), loc), ran)
|
adamc@10
|
711 end
|
adamc@10
|
712 | L.EAbs (x, to, e) =>
|
adamc@10
|
713 let
|
adamc@10
|
714 val t' = case to of
|
adamc@10
|
715 NONE => cunif ktype
|
adamc@10
|
716 | SOME t =>
|
adamc@10
|
717 let
|
adamc@10
|
718 val (t', tk) = elabCon env t
|
adamc@10
|
719 in
|
adamc@10
|
720 checkKind env t' tk ktype;
|
adamc@10
|
721 t'
|
adamc@10
|
722 end
|
adamc@10
|
723 val (e', et) = elabExp (E.pushERel env x t') e
|
adamc@10
|
724 in
|
adamc@10
|
725 ((L'.EAbs (x, t', e'), loc),
|
adamc@10
|
726 (L'.TFun (t', et), loc))
|
adamc@10
|
727 end
|
adamc@10
|
728 | L.ECApp (e, c) =>
|
adamc@10
|
729 let
|
adamc@10
|
730 val (e', et) = elabExp env e
|
adamc@10
|
731 val (c', ck) = elabCon env c
|
adamc@10
|
732 in
|
adamc@11
|
733 case #1 (hnormCon env et) of
|
adamc@11
|
734 L'.CError => (eerror, cerror)
|
adamc@11
|
735 | L'.TCFun (_, _, k, eb) =>
|
adamc@11
|
736 let
|
adamc@11
|
737 val () = checkKind env c' ck k
|
adamc@11
|
738 val eb' = subConInCon (0, c') eb
|
adamc@11
|
739 handle SynUnif => (expError env (Unif ("substitution", eb));
|
adamc@11
|
740 cerror)
|
adamc@11
|
741 in
|
adamc@11
|
742 ((L'.ECApp (e', c'), loc), eb')
|
adamc@11
|
743 end
|
adamc@11
|
744
|
adamc@11
|
745 | L'.CUnif _ =>
|
adamc@11
|
746 (expError env (Unif ("application", et));
|
adamc@11
|
747 (eerror, cerror))
|
adamc@11
|
748
|
adamc@11
|
749 | _ =>
|
adamc@11
|
750 (expError env (WrongForm ("constructor function", e', et));
|
adamc@11
|
751 (eerror, cerror))
|
adamc@10
|
752 end
|
adamc@11
|
753 | L.ECAbs (expl, x, k, e) =>
|
adamc@11
|
754 let
|
adamc@11
|
755 val expl' = elabExplicitness expl
|
adamc@11
|
756 val k' = elabKind k
|
adamc@11
|
757 val (e', et) = elabExp (E.pushCRel env x k') e
|
adamc@11
|
758 in
|
adamc@11
|
759 ((L'.ECAbs (expl', x, k', e'), loc),
|
adamc@11
|
760 (L'.TCFun (expl', x, k', et), loc))
|
adamc@11
|
761 end
|
adamc@6
|
762
|
adamc@12
|
763 | L.ERecord xes =>
|
adamc@12
|
764 let
|
adamc@12
|
765 val xes' = map (fn (x, e) =>
|
adamc@12
|
766 let
|
adamc@12
|
767 val (x', xk) = elabCon env x
|
adamc@12
|
768 val (e', et) = elabExp env e
|
adamc@12
|
769 in
|
adamc@12
|
770 checkKind env x' xk kname;
|
adamc@12
|
771 (x', e', et)
|
adamc@12
|
772 end) xes
|
adamc@12
|
773 in
|
adamc@12
|
774 ((L'.ERecord (map (fn (x', e', _) => (x', e')) xes'), loc),
|
adamc@12
|
775 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc))
|
adamc@12
|
776 end
|
adamc@12
|
777
|
adamc@12
|
778 | L.EField (e, c) =>
|
adamc@12
|
779 let
|
adamc@12
|
780 val (e', et) = elabExp env e
|
adamc@12
|
781 val (c', ck) = elabCon env c
|
adamc@12
|
782
|
adamc@12
|
783 val ft = cunif ktype
|
adamc@12
|
784 val rest = cunif ktype_record
|
adamc@12
|
785 in
|
adamc@12
|
786 checkKind env c' ck kname;
|
adamc@12
|
787 checkCon env e' et (L'.TRecord (L'.CConcat ((L'.CRecord (ktype, [(c', ft)]), loc), rest), loc), loc);
|
adamc@12
|
788 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft)
|
adamc@12
|
789 end
|
adamc@12
|
790
|
adamc@12
|
791
|
adamc@6
|
792 datatype decl_error =
|
adamc@6
|
793 KunifsRemainKind of ErrorMsg.span * L'.kind
|
adamc@6
|
794 | KunifsRemainCon of ErrorMsg.span * L'.con
|
adamc@10
|
795 | KunifsRemainExp of ErrorMsg.span * L'.exp
|
adamc@10
|
796 | CunifsRemainCon of ErrorMsg.span * L'.con
|
adamc@10
|
797 | CunifsRemainExp of ErrorMsg.span * L'.exp
|
adamc@6
|
798
|
adamc@6
|
799 fun declError env err =
|
adamc@6
|
800 case err of
|
adamc@6
|
801 KunifsRemainKind (loc, k) =>
|
adamc@6
|
802 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in kind";
|
adamc@6
|
803 eprefaces' [("Kind", p_kind k)])
|
adamc@6
|
804 | KunifsRemainCon (loc, c) =>
|
adamc@6
|
805 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in constructor";
|
adamc@6
|
806 eprefaces' [("Constructor", p_con env c)])
|
adamc@10
|
807 | KunifsRemainExp (loc, e) =>
|
adamc@10
|
808 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in expression";
|
adamc@10
|
809 eprefaces' [("Expression", p_exp env e)])
|
adamc@10
|
810 | CunifsRemainCon (loc, c) =>
|
adamc@10
|
811 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in constructor";
|
adamc@10
|
812 eprefaces' [("Constructor", p_con env c)])
|
adamc@10
|
813 | CunifsRemainExp (loc, e) =>
|
adamc@10
|
814 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in expression";
|
adamc@10
|
815 eprefaces' [("Expression", p_exp env e)])
|
adamc@6
|
816
|
adamc@3
|
817 fun elabDecl env (d, loc) =
|
adamc@5
|
818 (resetKunif ();
|
adamc@12
|
819 resetCunif ();
|
adamc@5
|
820 case d of
|
adamc@5
|
821 L.DCon (x, ko, c) =>
|
adamc@5
|
822 let
|
adamc@5
|
823 val k' = case ko of
|
adamc@5
|
824 NONE => kunif ()
|
adamc@5
|
825 | SOME k => elabKind k
|
adamc@3
|
826
|
adamc@5
|
827 val (c', ck) = elabCon env c
|
adamc@11
|
828 val (env', n) = E.pushCNamed env x k' (SOME c')
|
adamc@5
|
829 in
|
adamc@5
|
830 checkKind env c' ck k';
|
adamc@6
|
831
|
adamc@6
|
832 if kunifsInKind k' then
|
adamc@6
|
833 declError env (KunifsRemainKind (loc, k'))
|
adamc@6
|
834 else
|
adamc@6
|
835 ();
|
adamc@6
|
836
|
adamc@6
|
837 if kunifsInCon c' then
|
adamc@6
|
838 declError env (KunifsRemainCon (loc, c'))
|
adamc@6
|
839 else
|
adamc@6
|
840 ();
|
adamc@6
|
841
|
adamc@5
|
842 (env',
|
adamc@5
|
843 (L'.DCon (x, n, k', c'), loc))
|
adamc@10
|
844 end
|
adamc@10
|
845 | L.DVal (x, co, e) =>
|
adamc@10
|
846 let
|
adamc@10
|
847 val (c', ck) = case co of
|
adamc@10
|
848 NONE => (cunif ktype, ktype)
|
adamc@10
|
849 | SOME c => elabCon env c
|
adamc@10
|
850
|
adamc@10
|
851 val (e', et) = elabExp env e
|
adamc@10
|
852 val (env', n) = E.pushENamed env x c'
|
adamc@10
|
853 in
|
adamc@10
|
854 checkCon env e' et c';
|
adamc@10
|
855
|
adamc@10
|
856 if kunifsInCon c' then
|
adamc@10
|
857 declError env (KunifsRemainCon (loc, c'))
|
adamc@10
|
858 else
|
adamc@10
|
859 ();
|
adamc@10
|
860
|
adamc@10
|
861 if cunifsInCon c' then
|
adamc@10
|
862 declError env (CunifsRemainCon (loc, c'))
|
adamc@10
|
863 else
|
adamc@10
|
864 ();
|
adamc@10
|
865
|
adamc@10
|
866 if kunifsInExp e' then
|
adamc@10
|
867 declError env (KunifsRemainExp (loc, e'))
|
adamc@10
|
868 else
|
adamc@10
|
869 ();
|
adamc@10
|
870
|
adamc@10
|
871 if cunifsInExp e' then
|
adamc@10
|
872 declError env (CunifsRemainExp (loc, e'))
|
adamc@10
|
873 else
|
adamc@10
|
874 ();
|
adamc@10
|
875
|
adamc@10
|
876 (env',
|
adamc@10
|
877 (L'.DVal (x, n, c', e'), loc))
|
adamc@5
|
878 end)
|
adamc@3
|
879
|
adamc@5
|
880 fun elabFile env ds =
|
adamc@5
|
881 ListUtil.mapfoldl (fn (d, env) => elabDecl env d) env ds
|
adamc@2
|
882
|
adamc@2
|
883 end
|