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@3
|
139
|
adamc@3
|
140 val cerror = (L'.CError, dummy)
|
adamc@3
|
141 val kerror = (L'.KError, dummy)
|
adamc@10
|
142 val eerror = (L'.EError, dummy)
|
adamc@3
|
143
|
adamc@3
|
144 local
|
adamc@3
|
145 val count = ref 0
|
adamc@3
|
146 in
|
adamc@3
|
147
|
adamc@3
|
148 fun resetKunif () = count := 0
|
adamc@3
|
149
|
adamc@3
|
150 fun kunif () =
|
adamc@3
|
151 let
|
adamc@3
|
152 val n = !count
|
adamc@3
|
153 val s = if n <= 26 then
|
adamc@3
|
154 str (chr (ord #"A" + n))
|
adamc@3
|
155 else
|
adamc@3
|
156 "U" ^ Int.toString (n - 26)
|
adamc@3
|
157 in
|
adamc@3
|
158 count := n + 1;
|
adamc@3
|
159 (L'.KUnif (s, ref NONE), dummy)
|
adamc@3
|
160 end
|
adamc@3
|
161
|
adamc@3
|
162 end
|
adamc@3
|
163
|
adamc@10
|
164 local
|
adamc@10
|
165 val count = ref 0
|
adamc@10
|
166 in
|
adamc@10
|
167
|
adamc@10
|
168 fun resetCunif () = count := 0
|
adamc@10
|
169
|
adamc@10
|
170 fun cunif k =
|
adamc@10
|
171 let
|
adamc@10
|
172 val n = !count
|
adamc@10
|
173 val s = if n <= 26 then
|
adamc@10
|
174 str (chr (ord #"A" + n))
|
adamc@10
|
175 else
|
adamc@10
|
176 "U" ^ Int.toString (n - 26)
|
adamc@10
|
177 in
|
adamc@10
|
178 count := n + 1;
|
adamc@10
|
179 (L'.CUnif (k, s, ref NONE), dummy)
|
adamc@10
|
180 end
|
adamc@10
|
181
|
adamc@10
|
182 end
|
adamc@10
|
183
|
adamc@3
|
184 fun elabCon env (c, loc) =
|
adamc@3
|
185 case c of
|
adamc@3
|
186 L.CAnnot (c, k) =>
|
adamc@3
|
187 let
|
adamc@3
|
188 val k' = elabKind k
|
adamc@3
|
189 val (c', ck) = elabCon env c
|
adamc@3
|
190 in
|
adamc@5
|
191 checkKind env c' ck k';
|
adamc@3
|
192 (c', k')
|
adamc@3
|
193 end
|
adamc@3
|
194
|
adamc@3
|
195 | L.TFun (t1, t2) =>
|
adamc@3
|
196 let
|
adamc@3
|
197 val (t1', k1) = elabCon env t1
|
adamc@3
|
198 val (t2', k2) = elabCon env t2
|
adamc@3
|
199 in
|
adamc@5
|
200 checkKind env t1' k1 ktype;
|
adamc@5
|
201 checkKind env t2' k2 ktype;
|
adamc@3
|
202 ((L'.TFun (t1', t2'), loc), ktype)
|
adamc@3
|
203 end
|
adamc@3
|
204 | L.TCFun (e, x, k, t) =>
|
adamc@3
|
205 let
|
adamc@3
|
206 val e' = elabExplicitness e
|
adamc@3
|
207 val k' = elabKind k
|
adamc@3
|
208 val env' = E.pushCRel env x k'
|
adamc@3
|
209 val (t', tk) = elabCon env' t
|
adamc@3
|
210 in
|
adamc@5
|
211 checkKind env t' tk ktype;
|
adamc@3
|
212 ((L'.TCFun (e', x, k', t'), loc), ktype)
|
adamc@3
|
213 end
|
adamc@3
|
214 | L.TRecord c =>
|
adamc@3
|
215 let
|
adamc@3
|
216 val (c', ck) = elabCon env c
|
adamc@3
|
217 val k = (L'.KRecord ktype, loc)
|
adamc@3
|
218 in
|
adamc@5
|
219 checkKind env c' ck k;
|
adamc@3
|
220 ((L'.TRecord c', loc), ktype)
|
adamc@3
|
221 end
|
adamc@3
|
222
|
adamc@3
|
223 | L.CVar s =>
|
adamc@3
|
224 (case E.lookupC env s of
|
adamc@9
|
225 E.NotBound =>
|
adamc@5
|
226 (conError env (UnboundCon (loc, s));
|
adamc@3
|
227 (cerror, kerror))
|
adamc@9
|
228 | E.Rel (n, k) =>
|
adamc@3
|
229 ((L'.CRel n, loc), k)
|
adamc@9
|
230 | E.Named (n, k) =>
|
adamc@3
|
231 ((L'.CNamed n, loc), k))
|
adamc@3
|
232 | L.CApp (c1, c2) =>
|
adamc@3
|
233 let
|
adamc@3
|
234 val (c1', k1) = elabCon env c1
|
adamc@3
|
235 val (c2', k2) = elabCon env c2
|
adamc@3
|
236 val dom = kunif ()
|
adamc@3
|
237 val ran = kunif ()
|
adamc@3
|
238 in
|
adamc@5
|
239 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
|
adamc@5
|
240 checkKind env c2' k2 dom;
|
adamc@3
|
241 ((L'.CApp (c1', c2'), loc), ran)
|
adamc@3
|
242 end
|
adamc@8
|
243 | L.CAbs (x, k, t) =>
|
adamc@3
|
244 let
|
adamc@3
|
245 val k' = elabKind k
|
adamc@3
|
246 val env' = E.pushCRel env x k'
|
adamc@3
|
247 val (t', tk) = elabCon env' t
|
adamc@3
|
248 in
|
adamc@8
|
249 ((L'.CAbs (x, k', t'), loc),
|
adamc@3
|
250 (L'.KArrow (k', tk), loc))
|
adamc@3
|
251 end
|
adamc@3
|
252
|
adamc@3
|
253 | L.CName s =>
|
adamc@3
|
254 ((L'.CName s, loc), kname)
|
adamc@3
|
255
|
adamc@3
|
256 | L.CRecord xcs =>
|
adamc@3
|
257 let
|
adamc@3
|
258 val k = kunif ()
|
adamc@3
|
259
|
adamc@3
|
260 val xcs' = map (fn (x, c) =>
|
adamc@3
|
261 let
|
adamc@3
|
262 val (x', xk) = elabCon env x
|
adamc@3
|
263 val (c', ck) = elabCon env c
|
adamc@3
|
264 in
|
adamc@5
|
265 checkKind env x' xk kname;
|
adamc@5
|
266 checkKind env c' ck k;
|
adamc@3
|
267 (x', c')
|
adamc@3
|
268 end) xcs
|
adamc@3
|
269 in
|
adamc@5
|
270 ((L'.CRecord (k, xcs'), loc), (L'.KRecord k, loc))
|
adamc@3
|
271 end
|
adamc@3
|
272 | L.CConcat (c1, c2) =>
|
adamc@3
|
273 let
|
adamc@3
|
274 val (c1', k1) = elabCon env c1
|
adamc@3
|
275 val (c2', k2) = elabCon env c2
|
adamc@3
|
276 val ku = kunif ()
|
adamc@3
|
277 val k = (L'.KRecord ku, loc)
|
adamc@3
|
278 in
|
adamc@5
|
279 checkKind env c1' k1 k;
|
adamc@5
|
280 checkKind env c2' k2 k;
|
adamc@3
|
281 ((L'.CConcat (c1', c2'), loc), k)
|
adamc@3
|
282 end
|
adamc@3
|
283
|
adamc@6
|
284 fun kunifsRemain k =
|
adamc@6
|
285 case k of
|
adamc@6
|
286 L'.KUnif (_, ref NONE) => true
|
adamc@6
|
287 | _ => false
|
adamc@10
|
288 fun cunifsRemain c =
|
adamc@10
|
289 case c of
|
adamc@10
|
290 L'.CUnif (_, _, ref NONE) => true
|
adamc@10
|
291 | _ => false
|
adamc@6
|
292
|
adamc@6
|
293 val kunifsInKind = U.Kind.exists kunifsRemain
|
adamc@6
|
294 val kunifsInCon = U.Con.exists {kind = kunifsRemain,
|
adamc@6
|
295 con = fn _ => false}
|
adamc@10
|
296 val kunifsInExp = U.Exp.exists {kind = kunifsRemain,
|
adamc@10
|
297 con = fn _ => false,
|
adamc@10
|
298 exp = fn _ => false}
|
adamc@10
|
299
|
adamc@10
|
300 val cunifsInCon = U.Con.exists {kind = fn _ => false,
|
adamc@10
|
301 con = cunifsRemain}
|
adamc@10
|
302 val cunifsInExp = U.Exp.exists {kind = fn _ => false,
|
adamc@10
|
303 con = cunifsRemain,
|
adamc@10
|
304 exp = fn _ => false}
|
adamc@10
|
305
|
adamc@10
|
306 fun occursCon r =
|
adamc@10
|
307 U.Con.exists {kind = fn _ => false,
|
adamc@10
|
308 con = fn L'.CUnif (_, _, r') => r = r'
|
adamc@10
|
309 | _ => false}
|
adamc@10
|
310
|
adamc@10
|
311 datatype cunify_error =
|
adamc@10
|
312 CKind of L'.kind * L'.kind * kunify_error
|
adamc@10
|
313 | COccursCheckFailed of L'.con * L'.con
|
adamc@10
|
314 | CIncompatible of L'.con * L'.con
|
adamc@10
|
315 | CExplicitness of L'.con * L'.con
|
adamc@10
|
316
|
adamc@10
|
317 exception CUnify' of cunify_error
|
adamc@10
|
318
|
adamc@10
|
319 fun cunifyError env err =
|
adamc@10
|
320 case err of
|
adamc@10
|
321 CKind (k1, k2, kerr) =>
|
adamc@10
|
322 (eprefaces "Kind unification failure"
|
adamc@10
|
323 [("Kind 1", p_kind k1),
|
adamc@10
|
324 ("Kind 2", p_kind k2)];
|
adamc@10
|
325 kunifyError kerr)
|
adamc@10
|
326 | COccursCheckFailed (c1, c2) =>
|
adamc@10
|
327 eprefaces "Constructor occurs check failed"
|
adamc@10
|
328 [("Con 1", p_con env c1),
|
adamc@10
|
329 ("Con 2", p_con env c2)]
|
adamc@10
|
330 | CIncompatible (c1, c2) =>
|
adamc@10
|
331 eprefaces "Incompatible constructors"
|
adamc@10
|
332 [("Con 1", p_con env c1),
|
adamc@10
|
333 ("Con 2", p_con env c2)]
|
adamc@10
|
334 | CExplicitness (c1, c2) =>
|
adamc@10
|
335 eprefaces "Differing constructor function explicitness"
|
adamc@10
|
336 [("Con 1", p_con env c1),
|
adamc@10
|
337 ("Con 2", p_con env c2)]
|
adamc@10
|
338
|
adamc@11
|
339 fun hnormCon env (cAll as (c, _)) =
|
adamc@11
|
340 case c of
|
adamc@11
|
341 L'.CUnif (_, _, ref (SOME c)) => hnormCon env c
|
adamc@11
|
342
|
adamc@11
|
343 | L'.CNamed xn =>
|
adamc@11
|
344 (case E.lookupCNamed env xn of
|
adamc@11
|
345 (_, _, SOME c') => hnormCon env c'
|
adamc@11
|
346 | _ => cAll)
|
adamc@11
|
347
|
adamc@11
|
348 | _ => cAll
|
adamc@11
|
349
|
adamc@11
|
350 fun unifyCons' env c1 c2 =
|
adamc@11
|
351 unifyCons'' env (hnormCon env c1) (hnormCon env c2)
|
adamc@11
|
352
|
adamc@11
|
353 and unifyCons'' env (c1All as (c1, _)) (c2All as (c2, _)) =
|
adamc@10
|
354 let
|
adamc@10
|
355 fun err f = raise CUnify' (f (c1All, c2All))
|
adamc@10
|
356 in
|
adamc@10
|
357 case (c1, c2) of
|
adamc@10
|
358 (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
|
adamc@10
|
359 (unifyCons' env d1 d2;
|
adamc@10
|
360 unifyCons' env r1 r2)
|
adamc@10
|
361 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
|
adamc@10
|
362 if expl1 <> expl2 then
|
adamc@10
|
363 err CExplicitness
|
adamc@10
|
364 else
|
adamc@10
|
365 (unifyKinds d1 d2;
|
adamc@10
|
366 unifyCons' (E.pushCRel env x1 d1) r1 r2)
|
adamc@10
|
367 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env r1 r2
|
adamc@10
|
368
|
adamc@10
|
369 | (L'.CRel n1, L'.CRel n2) =>
|
adamc@10
|
370 if n1 = n2 then
|
adamc@10
|
371 ()
|
adamc@10
|
372 else
|
adamc@10
|
373 err CIncompatible
|
adamc@10
|
374 | (L'.CNamed n1, L'.CNamed n2) =>
|
adamc@10
|
375 if n1 = n2 then
|
adamc@10
|
376 ()
|
adamc@10
|
377 else
|
adamc@10
|
378 err CIncompatible
|
adamc@10
|
379
|
adamc@10
|
380 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
|
adamc@10
|
381 (unifyCons' env d1 d2;
|
adamc@10
|
382 unifyCons' env r1 r2)
|
adamc@10
|
383 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
|
adamc@10
|
384 (unifyKinds k1 k2;
|
adamc@10
|
385 unifyCons' (E.pushCRel env x1 k1) c1 c2)
|
adamc@10
|
386
|
adamc@10
|
387 | (L'.CName n1, L'.CName n2) =>
|
adamc@10
|
388 if n1 = n2 then
|
adamc@10
|
389 ()
|
adamc@10
|
390 else
|
adamc@10
|
391 err CIncompatible
|
adamc@10
|
392
|
adamc@10
|
393 | (L'.CRecord (k1, rs1), L'.CRecord (k2, rs2)) =>
|
adamc@10
|
394 (unifyKinds k1 k2;
|
adamc@10
|
395 ((ListPair.appEq (fn ((n1, v1), (n2, v2)) =>
|
adamc@10
|
396 (unifyCons' env n1 n2;
|
adamc@10
|
397 unifyCons' env v1 v2)) (rs1, rs2))
|
adamc@10
|
398 handle ListPair.UnequalLengths => err CIncompatible))
|
adamc@10
|
399 | (L'.CConcat (d1, r1), L'.CConcat (d2, r2)) =>
|
adamc@10
|
400 (unifyCons' env d1 d2;
|
adamc@10
|
401 unifyCons' env r1 r2)
|
adamc@10
|
402
|
adamc@10
|
403
|
adamc@10
|
404 | (L'.CError, _) => ()
|
adamc@10
|
405 | (_, L'.CError) => ()
|
adamc@10
|
406
|
adamc@10
|
407 | (L'.CUnif (_, _, ref (SOME c1All)), _) => unifyCons' env c1All c2All
|
adamc@10
|
408 | (_, L'.CUnif (_, _, ref (SOME c2All))) => unifyCons' env c1All c2All
|
adamc@10
|
409
|
adamc@10
|
410 | (L'.CUnif (k1, _, r1), L'.CUnif (k2, _, r2)) =>
|
adamc@10
|
411 if r1 = r2 then
|
adamc@10
|
412 ()
|
adamc@10
|
413 else
|
adamc@10
|
414 (unifyKinds k1 k2;
|
adamc@10
|
415 r1 := SOME c2All)
|
adamc@10
|
416
|
adamc@10
|
417 | (L'.CUnif (_, _, r), _) =>
|
adamc@10
|
418 if occursCon r c2All then
|
adamc@10
|
419 err COccursCheckFailed
|
adamc@10
|
420 else
|
adamc@10
|
421 r := SOME c2All
|
adamc@10
|
422 | (_, L'.CUnif (_, _, r)) =>
|
adamc@10
|
423 if occursCon r c1All then
|
adamc@10
|
424 err COccursCheckFailed
|
adamc@10
|
425 else
|
adamc@10
|
426 r := SOME c1All
|
adamc@10
|
427
|
adamc@10
|
428 | _ => err CIncompatible
|
adamc@10
|
429 end
|
adamc@10
|
430
|
adamc@10
|
431 exception CUnify of L'.con * L'.con * cunify_error
|
adamc@10
|
432
|
adamc@10
|
433 fun unifyCons env c1 c2 =
|
adamc@10
|
434 unifyCons' env c1 c2
|
adamc@10
|
435 handle CUnify' err => raise CUnify (c1, c2, err)
|
adamc@10
|
436 | KUnify args => raise CUnify (c1, c2, CKind args)
|
adamc@10
|
437
|
adamc@10
|
438 datatype exp_error =
|
adamc@10
|
439 UnboundExp of ErrorMsg.span * string
|
adamc@10
|
440 | Unify of L'.exp * L'.con * L'.con * cunify_error
|
adamc@11
|
441 | Unif of string * L'.con
|
adamc@11
|
442 | WrongForm of string * L'.exp * L'.con
|
adamc@10
|
443
|
adamc@10
|
444 fun expError env err =
|
adamc@10
|
445 case err of
|
adamc@10
|
446 UnboundExp (loc, s) =>
|
adamc@10
|
447 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
|
adamc@10
|
448 | Unify (e, c1, c2, uerr) =>
|
adamc@10
|
449 (ErrorMsg.errorAt (#2 e) "Unification failure";
|
adamc@10
|
450 eprefaces' [("Expression", p_exp env e),
|
adamc@10
|
451 ("Have con", p_con env c1),
|
adamc@10
|
452 ("Need con", p_con env c2)];
|
adamc@10
|
453 cunifyError env uerr)
|
adamc@11
|
454 | Unif (action, c) =>
|
adamc@11
|
455 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
|
adamc@11
|
456 eprefaces' [("Con", p_con env c)])
|
adamc@11
|
457 | WrongForm (variety, e, t) =>
|
adamc@11
|
458 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
|
adamc@11
|
459 eprefaces' [("Expression", p_exp env e),
|
adamc@11
|
460 ("Type", p_con env t)])
|
adamc@10
|
461
|
adamc@10
|
462 fun checkCon env e c1 c2 =
|
adamc@10
|
463 unifyCons env c1 c2
|
adamc@10
|
464 handle CUnify (c1, c2, err) =>
|
adamc@10
|
465 expError env (Unify (e, c1, c2, err))
|
adamc@10
|
466
|
adamc@11
|
467 exception SynUnif
|
adamc@11
|
468
|
adamc@11
|
469 val liftConInCon =
|
adamc@11
|
470 U.Con.mapB {kind = fn k => k,
|
adamc@11
|
471 con = fn bound => fn c =>
|
adamc@11
|
472 case c of
|
adamc@11
|
473 L'.CRel xn =>
|
adamc@11
|
474 if xn < bound then
|
adamc@11
|
475 c
|
adamc@11
|
476 else
|
adamc@11
|
477 L'.CRel (xn + 1)
|
adamc@11
|
478 | L'.CUnif _ => raise SynUnif
|
adamc@11
|
479 | _ => c,
|
adamc@11
|
480 bind = fn (bound, U.Con.Rel _) => bound + 1
|
adamc@11
|
481 | (bound, _) => bound}
|
adamc@11
|
482
|
adamc@11
|
483 val subConInCon =
|
adamc@11
|
484 U.Con.mapB {kind = fn k => k,
|
adamc@11
|
485 con = fn (xn, rep) => fn c =>
|
adamc@11
|
486 case c of
|
adamc@11
|
487 L'.CRel xn' =>
|
adamc@11
|
488 if xn = xn' then
|
adamc@11
|
489 #1 rep
|
adamc@11
|
490 else
|
adamc@11
|
491 c
|
adamc@11
|
492 | L'.CUnif _ => raise SynUnif
|
adamc@11
|
493 | _ => c,
|
adamc@11
|
494 bind = fn ((xn, rep), U.Con.Rel _) => (xn+1, liftConInCon 0 rep)
|
adamc@11
|
495 | (ctx, _) => ctx}
|
adamc@11
|
496
|
adamc@10
|
497 fun elabExp env (e, loc) =
|
adamc@10
|
498 case e of
|
adamc@10
|
499 L.EAnnot (e, t) =>
|
adamc@10
|
500 let
|
adamc@10
|
501 val (e', et) = elabExp env e
|
adamc@10
|
502 val (t', _) = elabCon env t
|
adamc@10
|
503 in
|
adamc@10
|
504 checkCon env e' et t';
|
adamc@10
|
505 (e', t')
|
adamc@10
|
506 end
|
adamc@10
|
507
|
adamc@10
|
508 | L.EVar s =>
|
adamc@10
|
509 (case E.lookupE env s of
|
adamc@10
|
510 E.NotBound =>
|
adamc@10
|
511 (expError env (UnboundExp (loc, s));
|
adamc@10
|
512 (eerror, cerror))
|
adamc@10
|
513 | E.Rel (n, t) => ((L'.ERel n, loc), t)
|
adamc@10
|
514 | E.Named (n, t) => ((L'.ENamed n, loc), t))
|
adamc@10
|
515 | L.EApp (e1, e2) =>
|
adamc@10
|
516 let
|
adamc@10
|
517 val (e1', t1) = elabExp env e1
|
adamc@10
|
518 val (e2', t2) = elabExp env e2
|
adamc@10
|
519
|
adamc@10
|
520 val dom = cunif ktype
|
adamc@10
|
521 val ran = cunif ktype
|
adamc@10
|
522 val t = (L'.TFun (dom, ran), dummy)
|
adamc@10
|
523 in
|
adamc@10
|
524 checkCon env e1' t1 t;
|
adamc@10
|
525 checkCon env e2' t2 dom;
|
adamc@10
|
526 ((L'.EApp (e1', e2'), loc), ran)
|
adamc@10
|
527 end
|
adamc@10
|
528 | L.EAbs (x, to, e) =>
|
adamc@10
|
529 let
|
adamc@10
|
530 val t' = case to of
|
adamc@10
|
531 NONE => cunif ktype
|
adamc@10
|
532 | SOME t =>
|
adamc@10
|
533 let
|
adamc@10
|
534 val (t', tk) = elabCon env t
|
adamc@10
|
535 in
|
adamc@10
|
536 checkKind env t' tk ktype;
|
adamc@10
|
537 t'
|
adamc@10
|
538 end
|
adamc@10
|
539 val (e', et) = elabExp (E.pushERel env x t') e
|
adamc@10
|
540 in
|
adamc@10
|
541 ((L'.EAbs (x, t', e'), loc),
|
adamc@10
|
542 (L'.TFun (t', et), loc))
|
adamc@10
|
543 end
|
adamc@10
|
544 | L.ECApp (e, c) =>
|
adamc@10
|
545 let
|
adamc@10
|
546 val (e', et) = elabExp env e
|
adamc@10
|
547 val (c', ck) = elabCon env c
|
adamc@10
|
548 in
|
adamc@11
|
549 case #1 (hnormCon env et) of
|
adamc@11
|
550 L'.CError => (eerror, cerror)
|
adamc@11
|
551 | L'.TCFun (_, _, k, eb) =>
|
adamc@11
|
552 let
|
adamc@11
|
553 val () = checkKind env c' ck k
|
adamc@11
|
554 val eb' = subConInCon (0, c') eb
|
adamc@11
|
555 handle SynUnif => (expError env (Unif ("substitution", eb));
|
adamc@11
|
556 cerror)
|
adamc@11
|
557 in
|
adamc@11
|
558 ((L'.ECApp (e', c'), loc), eb')
|
adamc@11
|
559 end
|
adamc@11
|
560
|
adamc@11
|
561 | L'.CUnif _ =>
|
adamc@11
|
562 (expError env (Unif ("application", et));
|
adamc@11
|
563 (eerror, cerror))
|
adamc@11
|
564
|
adamc@11
|
565 | _ =>
|
adamc@11
|
566 (expError env (WrongForm ("constructor function", e', et));
|
adamc@11
|
567 (eerror, cerror))
|
adamc@10
|
568 end
|
adamc@11
|
569 | L.ECAbs (expl, x, k, e) =>
|
adamc@11
|
570 let
|
adamc@11
|
571 val expl' = elabExplicitness expl
|
adamc@11
|
572 val k' = elabKind k
|
adamc@11
|
573 val (e', et) = elabExp (E.pushCRel env x k') e
|
adamc@11
|
574 in
|
adamc@11
|
575 ((L'.ECAbs (expl', x, k', e'), loc),
|
adamc@11
|
576 (L'.TCFun (expl', x, k', et), loc))
|
adamc@11
|
577 end
|
adamc@6
|
578
|
adamc@6
|
579 datatype decl_error =
|
adamc@6
|
580 KunifsRemainKind of ErrorMsg.span * L'.kind
|
adamc@6
|
581 | KunifsRemainCon of ErrorMsg.span * L'.con
|
adamc@10
|
582 | KunifsRemainExp of ErrorMsg.span * L'.exp
|
adamc@10
|
583 | CunifsRemainCon of ErrorMsg.span * L'.con
|
adamc@10
|
584 | CunifsRemainExp of ErrorMsg.span * L'.exp
|
adamc@6
|
585
|
adamc@6
|
586 fun declError env err =
|
adamc@6
|
587 case err of
|
adamc@6
|
588 KunifsRemainKind (loc, k) =>
|
adamc@6
|
589 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in kind";
|
adamc@6
|
590 eprefaces' [("Kind", p_kind k)])
|
adamc@6
|
591 | KunifsRemainCon (loc, c) =>
|
adamc@6
|
592 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in constructor";
|
adamc@6
|
593 eprefaces' [("Constructor", p_con env c)])
|
adamc@10
|
594 | KunifsRemainExp (loc, e) =>
|
adamc@10
|
595 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in expression";
|
adamc@10
|
596 eprefaces' [("Expression", p_exp env e)])
|
adamc@10
|
597 | CunifsRemainCon (loc, c) =>
|
adamc@10
|
598 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in constructor";
|
adamc@10
|
599 eprefaces' [("Constructor", p_con env c)])
|
adamc@10
|
600 | CunifsRemainExp (loc, e) =>
|
adamc@10
|
601 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in expression";
|
adamc@10
|
602 eprefaces' [("Expression", p_exp env e)])
|
adamc@6
|
603
|
adamc@3
|
604 fun elabDecl env (d, loc) =
|
adamc@5
|
605 (resetKunif ();
|
adamc@5
|
606 case d of
|
adamc@5
|
607 L.DCon (x, ko, c) =>
|
adamc@5
|
608 let
|
adamc@5
|
609 val k' = case ko of
|
adamc@5
|
610 NONE => kunif ()
|
adamc@5
|
611 | SOME k => elabKind k
|
adamc@3
|
612
|
adamc@5
|
613 val (c', ck) = elabCon env c
|
adamc@11
|
614 val (env', n) = E.pushCNamed env x k' (SOME c')
|
adamc@5
|
615 in
|
adamc@5
|
616 checkKind env c' ck k';
|
adamc@6
|
617
|
adamc@6
|
618 if kunifsInKind k' then
|
adamc@6
|
619 declError env (KunifsRemainKind (loc, k'))
|
adamc@6
|
620 else
|
adamc@6
|
621 ();
|
adamc@6
|
622
|
adamc@6
|
623 if kunifsInCon c' then
|
adamc@6
|
624 declError env (KunifsRemainCon (loc, c'))
|
adamc@6
|
625 else
|
adamc@6
|
626 ();
|
adamc@6
|
627
|
adamc@5
|
628 (env',
|
adamc@5
|
629 (L'.DCon (x, n, k', c'), loc))
|
adamc@10
|
630 end
|
adamc@10
|
631 | L.DVal (x, co, e) =>
|
adamc@10
|
632 let
|
adamc@10
|
633 val (c', ck) = case co of
|
adamc@10
|
634 NONE => (cunif ktype, ktype)
|
adamc@10
|
635 | SOME c => elabCon env c
|
adamc@10
|
636
|
adamc@10
|
637 val (e', et) = elabExp env e
|
adamc@10
|
638 val (env', n) = E.pushENamed env x c'
|
adamc@10
|
639 in
|
adamc@10
|
640 checkCon env e' et c';
|
adamc@10
|
641
|
adamc@10
|
642 if kunifsInCon c' then
|
adamc@10
|
643 declError env (KunifsRemainCon (loc, c'))
|
adamc@10
|
644 else
|
adamc@10
|
645 ();
|
adamc@10
|
646
|
adamc@10
|
647 if cunifsInCon c' then
|
adamc@10
|
648 declError env (CunifsRemainCon (loc, c'))
|
adamc@10
|
649 else
|
adamc@10
|
650 ();
|
adamc@10
|
651
|
adamc@10
|
652 if kunifsInExp e' then
|
adamc@10
|
653 declError env (KunifsRemainExp (loc, e'))
|
adamc@10
|
654 else
|
adamc@10
|
655 ();
|
adamc@10
|
656
|
adamc@10
|
657 if cunifsInExp e' then
|
adamc@10
|
658 declError env (CunifsRemainExp (loc, e'))
|
adamc@10
|
659 else
|
adamc@10
|
660 ();
|
adamc@10
|
661
|
adamc@10
|
662 (env',
|
adamc@10
|
663 (L'.DVal (x, n, c', e'), loc))
|
adamc@5
|
664 end)
|
adamc@3
|
665
|
adamc@5
|
666 fun elabFile env ds =
|
adamc@5
|
667 ListUtil.mapfoldl (fn (d, env) => elabDecl env d) env ds
|
adamc@2
|
668
|
adamc@2
|
669 end
|