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@3
|
142
|
adamc@3
|
143 local
|
adamc@3
|
144 val count = ref 0
|
adamc@3
|
145 in
|
adamc@3
|
146
|
adamc@3
|
147 fun resetKunif () = count := 0
|
adamc@3
|
148
|
adamc@3
|
149 fun kunif () =
|
adamc@3
|
150 let
|
adamc@3
|
151 val n = !count
|
adamc@3
|
152 val s = if n <= 26 then
|
adamc@3
|
153 str (chr (ord #"A" + n))
|
adamc@3
|
154 else
|
adamc@3
|
155 "U" ^ Int.toString (n - 26)
|
adamc@3
|
156 in
|
adamc@3
|
157 count := n + 1;
|
adamc@3
|
158 (L'.KUnif (s, ref NONE), dummy)
|
adamc@3
|
159 end
|
adamc@3
|
160
|
adamc@3
|
161 end
|
adamc@3
|
162
|
adamc@3
|
163 fun elabCon env (c, loc) =
|
adamc@3
|
164 case c of
|
adamc@3
|
165 L.CAnnot (c, k) =>
|
adamc@3
|
166 let
|
adamc@3
|
167 val k' = elabKind k
|
adamc@3
|
168 val (c', ck) = elabCon env c
|
adamc@3
|
169 in
|
adamc@5
|
170 checkKind env c' ck k';
|
adamc@3
|
171 (c', k')
|
adamc@3
|
172 end
|
adamc@3
|
173
|
adamc@3
|
174 | L.TFun (t1, t2) =>
|
adamc@3
|
175 let
|
adamc@3
|
176 val (t1', k1) = elabCon env t1
|
adamc@3
|
177 val (t2', k2) = elabCon env t2
|
adamc@3
|
178 in
|
adamc@5
|
179 checkKind env t1' k1 ktype;
|
adamc@5
|
180 checkKind env t2' k2 ktype;
|
adamc@3
|
181 ((L'.TFun (t1', t2'), loc), ktype)
|
adamc@3
|
182 end
|
adamc@3
|
183 | L.TCFun (e, x, k, t) =>
|
adamc@3
|
184 let
|
adamc@3
|
185 val e' = elabExplicitness e
|
adamc@3
|
186 val k' = elabKind k
|
adamc@3
|
187 val env' = E.pushCRel env x k'
|
adamc@3
|
188 val (t', tk) = elabCon env' t
|
adamc@3
|
189 in
|
adamc@5
|
190 checkKind env t' tk ktype;
|
adamc@3
|
191 ((L'.TCFun (e', x, k', t'), loc), ktype)
|
adamc@3
|
192 end
|
adamc@3
|
193 | L.TRecord c =>
|
adamc@3
|
194 let
|
adamc@3
|
195 val (c', ck) = elabCon env c
|
adamc@3
|
196 val k = (L'.KRecord ktype, loc)
|
adamc@3
|
197 in
|
adamc@5
|
198 checkKind env c' ck k;
|
adamc@3
|
199 ((L'.TRecord c', loc), ktype)
|
adamc@3
|
200 end
|
adamc@3
|
201
|
adamc@3
|
202 | L.CVar s =>
|
adamc@3
|
203 (case E.lookupC env s of
|
adamc@3
|
204 E.CNotBound =>
|
adamc@5
|
205 (conError env (UnboundCon (loc, s));
|
adamc@3
|
206 (cerror, kerror))
|
adamc@3
|
207 | E.CRel (n, k) =>
|
adamc@3
|
208 ((L'.CRel n, loc), k)
|
adamc@3
|
209 | E.CNamed (n, k) =>
|
adamc@3
|
210 ((L'.CNamed n, loc), k))
|
adamc@3
|
211 | L.CApp (c1, c2) =>
|
adamc@3
|
212 let
|
adamc@3
|
213 val (c1', k1) = elabCon env c1
|
adamc@3
|
214 val (c2', k2) = elabCon env c2
|
adamc@3
|
215 val dom = kunif ()
|
adamc@3
|
216 val ran = kunif ()
|
adamc@3
|
217 in
|
adamc@5
|
218 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
|
adamc@5
|
219 checkKind env c2' k2 dom;
|
adamc@3
|
220 ((L'.CApp (c1', c2'), loc), ran)
|
adamc@3
|
221 end
|
adamc@8
|
222 | L.CAbs (x, k, t) =>
|
adamc@3
|
223 let
|
adamc@3
|
224 val k' = elabKind k
|
adamc@3
|
225 val env' = E.pushCRel env x k'
|
adamc@3
|
226 val (t', tk) = elabCon env' t
|
adamc@3
|
227 in
|
adamc@8
|
228 ((L'.CAbs (x, k', t'), loc),
|
adamc@3
|
229 (L'.KArrow (k', tk), loc))
|
adamc@3
|
230 end
|
adamc@3
|
231
|
adamc@3
|
232 | L.CName s =>
|
adamc@3
|
233 ((L'.CName s, loc), kname)
|
adamc@3
|
234
|
adamc@3
|
235 | L.CRecord xcs =>
|
adamc@3
|
236 let
|
adamc@3
|
237 val k = kunif ()
|
adamc@3
|
238
|
adamc@3
|
239 val xcs' = map (fn (x, c) =>
|
adamc@3
|
240 let
|
adamc@3
|
241 val (x', xk) = elabCon env x
|
adamc@3
|
242 val (c', ck) = elabCon env c
|
adamc@3
|
243 in
|
adamc@5
|
244 checkKind env x' xk kname;
|
adamc@5
|
245 checkKind env c' ck k;
|
adamc@3
|
246 (x', c')
|
adamc@3
|
247 end) xcs
|
adamc@3
|
248 in
|
adamc@5
|
249 ((L'.CRecord (k, xcs'), loc), (L'.KRecord k, loc))
|
adamc@3
|
250 end
|
adamc@3
|
251 | L.CConcat (c1, c2) =>
|
adamc@3
|
252 let
|
adamc@3
|
253 val (c1', k1) = elabCon env c1
|
adamc@3
|
254 val (c2', k2) = elabCon env c2
|
adamc@3
|
255 val ku = kunif ()
|
adamc@3
|
256 val k = (L'.KRecord ku, loc)
|
adamc@3
|
257 in
|
adamc@5
|
258 checkKind env c1' k1 k;
|
adamc@5
|
259 checkKind env c2' k2 k;
|
adamc@3
|
260 ((L'.CConcat (c1', c2'), loc), k)
|
adamc@3
|
261 end
|
adamc@3
|
262
|
adamc@6
|
263 fun kunifsRemain k =
|
adamc@6
|
264 case k of
|
adamc@6
|
265 L'.KUnif (_, ref NONE) => true
|
adamc@6
|
266 | _ => false
|
adamc@6
|
267
|
adamc@6
|
268 val kunifsInKind = U.Kind.exists kunifsRemain
|
adamc@6
|
269 val kunifsInCon = U.Con.exists {kind = kunifsRemain,
|
adamc@6
|
270 con = fn _ => false}
|
adamc@6
|
271
|
adamc@6
|
272 datatype decl_error =
|
adamc@6
|
273 KunifsRemainKind of ErrorMsg.span * L'.kind
|
adamc@6
|
274 | KunifsRemainCon of ErrorMsg.span * L'.con
|
adamc@6
|
275
|
adamc@6
|
276 fun declError env err =
|
adamc@6
|
277 case err of
|
adamc@6
|
278 KunifsRemainKind (loc, k) =>
|
adamc@6
|
279 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in kind";
|
adamc@6
|
280 eprefaces' [("Kind", p_kind k)])
|
adamc@6
|
281 | KunifsRemainCon (loc, c) =>
|
adamc@6
|
282 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in constructor";
|
adamc@6
|
283 eprefaces' [("Constructor", p_con env c)])
|
adamc@6
|
284
|
adamc@3
|
285 fun elabDecl env (d, loc) =
|
adamc@5
|
286 (resetKunif ();
|
adamc@5
|
287 case d of
|
adamc@5
|
288 L.DCon (x, ko, c) =>
|
adamc@5
|
289 let
|
adamc@5
|
290 val k' = case ko of
|
adamc@5
|
291 NONE => kunif ()
|
adamc@5
|
292 | SOME k => elabKind k
|
adamc@3
|
293
|
adamc@5
|
294 val (c', ck) = elabCon env c
|
adamc@5
|
295 val (env', n) = E.pushCNamed env x k'
|
adamc@5
|
296 in
|
adamc@5
|
297 checkKind env c' ck k';
|
adamc@6
|
298
|
adamc@6
|
299 if kunifsInKind k' then
|
adamc@6
|
300 declError env (KunifsRemainKind (loc, k'))
|
adamc@6
|
301 else
|
adamc@6
|
302 ();
|
adamc@6
|
303
|
adamc@6
|
304 if kunifsInCon c' then
|
adamc@6
|
305 declError env (KunifsRemainCon (loc, c'))
|
adamc@6
|
306 else
|
adamc@6
|
307 ();
|
adamc@6
|
308
|
adamc@5
|
309 (env',
|
adamc@5
|
310 (L'.DCon (x, n, k', c'), loc))
|
adamc@5
|
311 end)
|
adamc@3
|
312
|
adamc@5
|
313 fun elabFile env ds =
|
adamc@5
|
314 ListUtil.mapfoldl (fn (d, env) => elabDecl env d) env ds
|
adamc@2
|
315
|
adamc@2
|
316 end
|