adamc@674
|
1 (* Copyright (c) 2008-2009, 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 ElabEnv :> ELAB_ENV = struct
|
adamc@2
|
29
|
adamc@2
|
30 open Elab
|
adamc@2
|
31
|
adamc@13
|
32 structure U = ElabUtil
|
adamc@13
|
33
|
adamc@2
|
34 structure IM = IntBinaryMap
|
adamc@2
|
35 structure SM = BinaryMapFn(struct
|
adamc@2
|
36 type ord_key = string
|
adamc@2
|
37 val compare = String.compare
|
adamc@2
|
38 end)
|
adamc@2
|
39
|
adamc@2
|
40 exception UnboundRel of int
|
adamc@2
|
41 exception UnboundNamed of int
|
adamc@2
|
42
|
adamc@13
|
43
|
adamc@13
|
44 (* AST utility functions *)
|
adamc@13
|
45
|
adamc@13
|
46 exception SynUnif
|
adamc@13
|
47
|
adamc@623
|
48 val liftKindInKind =
|
adamc@623
|
49 U.Kind.mapB {kind = fn bound => fn k =>
|
adamc@623
|
50 case k of
|
adamc@623
|
51 KRel xn =>
|
adamc@623
|
52 if xn < bound then
|
adamc@623
|
53 k
|
adamc@623
|
54 else
|
adamc@623
|
55 KRel (xn + 1)
|
adamc@623
|
56 | _ => k,
|
adamc@623
|
57 bind = fn (bound, _) => bound + 1}
|
adamc@623
|
58
|
adamc@623
|
59 val liftKindInCon =
|
adamc@623
|
60 U.Con.mapB {kind = fn bound => fn k =>
|
adamc@623
|
61 case k of
|
adamc@623
|
62 KRel xn =>
|
adamc@623
|
63 if xn < bound then
|
adamc@623
|
64 k
|
adamc@623
|
65 else
|
adamc@623
|
66 KRel (xn + 1)
|
adamc@623
|
67 | _ => k,
|
adamc@623
|
68 con = fn _ => fn c => c,
|
adamc@623
|
69 bind = fn (bound, U.Con.RelK _) => bound + 1
|
adamc@623
|
70 | (bound, _) => bound}
|
adamc@623
|
71
|
adamc@13
|
72 val liftConInCon =
|
adamc@623
|
73 U.Con.mapB {kind = fn _ => fn k => k,
|
adamc@13
|
74 con = fn bound => fn c =>
|
adamc@13
|
75 case c of
|
adamc@16
|
76 CRel xn =>
|
adamc@13
|
77 if xn < bound then
|
adamc@13
|
78 c
|
adamc@13
|
79 else
|
adamc@16
|
80 CRel (xn + 1)
|
adamc@17
|
81 (*| CUnif _ => raise SynUnif*)
|
adamc@13
|
82 | _ => c,
|
adamc@623
|
83 bind = fn (bound, U.Con.RelC _) => bound + 1
|
adamc@13
|
84 | (bound, _) => bound}
|
adamc@13
|
85
|
adamc@13
|
86 val lift = liftConInCon 0
|
adamc@13
|
87
|
adamc@623
|
88 val liftKindInExp =
|
adamc@623
|
89 U.Exp.mapB {kind = fn bound => fn k =>
|
adamc@623
|
90 case k of
|
adamc@623
|
91 KRel xn =>
|
adamc@623
|
92 if xn < bound then
|
adamc@623
|
93 k
|
adamc@623
|
94 else
|
adamc@623
|
95 KRel (xn + 1)
|
adamc@623
|
96 | _ => k,
|
adamc@623
|
97 con = fn _ => fn c => c,
|
adamc@623
|
98 exp = fn _ => fn e => e,
|
adamc@623
|
99 bind = fn (bound, U.Exp.RelK _) => bound + 1
|
adamc@623
|
100 | (bound, _) => bound}
|
adamc@623
|
101
|
adamc@448
|
102 val liftConInExp =
|
adamc@623
|
103 U.Exp.mapB {kind = fn _ => fn k => k,
|
adamc@448
|
104 con = fn bound => fn c =>
|
adamc@448
|
105 case c of
|
adamc@448
|
106 CRel xn =>
|
adamc@448
|
107 if xn < bound then
|
adamc@448
|
108 c
|
adamc@448
|
109 else
|
adamc@448
|
110 CRel (xn + 1)
|
adamc@448
|
111 | _ => c,
|
adamc@448
|
112 exp = fn _ => fn e => e,
|
adamc@448
|
113 bind = fn (bound, U.Exp.RelC _) => bound + 1
|
adamc@448
|
114 | (bound, _) => bound}
|
adamc@448
|
115
|
adamc@211
|
116 val liftExpInExp =
|
adamc@623
|
117 U.Exp.mapB {kind = fn _ => fn k => k,
|
adamc@211
|
118 con = fn _ => fn c => c,
|
adamc@211
|
119 exp = fn bound => fn e =>
|
adamc@211
|
120 case e of
|
adamc@211
|
121 ERel xn =>
|
adamc@211
|
122 if xn < bound then
|
adamc@211
|
123 e
|
adamc@211
|
124 else
|
adamc@211
|
125 ERel (xn + 1)
|
adamc@211
|
126 | _ => e,
|
adamc@211
|
127 bind = fn (bound, U.Exp.RelE _) => bound + 1
|
adamc@211
|
128 | (bound, _) => bound}
|
adamc@211
|
129
|
adamc@211
|
130
|
adamc@211
|
131 val liftExp = liftExpInExp 0
|
adamc@13
|
132
|
adamc@448
|
133 val subExpInExp =
|
adamc@623
|
134 U.Exp.mapB {kind = fn _ => fn k => k,
|
adamc@448
|
135 con = fn _ => fn c => c,
|
adamc@448
|
136 exp = fn (xn, rep) => fn e =>
|
adamc@448
|
137 case e of
|
adamc@448
|
138 ERel xn' =>
|
adamc@448
|
139 (case Int.compare (xn', xn) of
|
adamc@448
|
140 EQUAL => #1 rep
|
adamc@448
|
141 | GREATER=> ERel (xn' - 1)
|
adamc@448
|
142 | LESS => e)
|
adamc@448
|
143 | _ => e,
|
adamc@448
|
144 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
|
adamc@448
|
145 | ((xn, rep), U.Exp.RelC _) => (xn, liftConInExp 0 rep)
|
adamc@448
|
146 | (ctx, _) => ctx}
|
adamc@448
|
147
|
adamc@13
|
148 (* Back to environments *)
|
adamc@13
|
149
|
adamc@9
|
150 datatype 'a var' =
|
adamc@9
|
151 Rel' of int * 'a
|
adamc@9
|
152 | Named' of int * 'a
|
adamc@2
|
153
|
adamc@9
|
154 datatype 'a var =
|
adamc@9
|
155 NotBound
|
adamc@9
|
156 | Rel of int * 'a
|
adamc@9
|
157 | Named of int * 'a
|
adamc@2
|
158
|
adamc@191
|
159 type datatyp = string list * (string * con option) IM.map
|
adamc@157
|
160
|
adamc@211
|
161 datatype class_name =
|
adamc@211
|
162 ClNamed of int
|
adamc@211
|
163 | ClProj of int * string list * string
|
adamc@211
|
164
|
adamc@711
|
165 fun class_name_out cn =
|
adamc@711
|
166 case cn of
|
adamc@711
|
167 ClNamed n => (CNamed n, ErrorMsg.dummySpan)
|
adamc@711
|
168 | ClProj x => (CModProj x, ErrorMsg.dummySpan)
|
adamc@711
|
169
|
adamc@216
|
170 fun cn2s cn =
|
adamc@216
|
171 case cn of
|
adamc@216
|
172 ClNamed n => "Named(" ^ Int.toString n ^ ")"
|
adamc@216
|
173 | ClProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
|
adamc@216
|
174
|
adamc@211
|
175 structure CK = struct
|
adamc@211
|
176 type ord_key = class_name
|
adamc@211
|
177 open Order
|
adamc@211
|
178 fun compare x =
|
adamc@211
|
179 case x of
|
adamc@211
|
180 (ClNamed n1, ClNamed n2) => Int.compare (n1, n2)
|
adamc@211
|
181 | (ClNamed _, _) => LESS
|
adamc@211
|
182 | (_, ClNamed _) => GREATER
|
adamc@211
|
183
|
adamc@211
|
184 | (ClProj (m1, ms1, x1), ClProj (m2, ms2, x2)) =>
|
adamc@211
|
185 join (Int.compare (m1, m2),
|
adamc@211
|
186 fn () => join (joinL String.compare (ms1, ms2),
|
adamc@211
|
187 fn () => String.compare (x1, x2)))
|
adamc@211
|
188 end
|
adamc@211
|
189
|
adamc@677
|
190 structure CS = BinarySetFn(CK)
|
adamc@211
|
191 structure CM = BinaryMapFn(CK)
|
adamc@211
|
192
|
adamc@711
|
193 type class = {ground : (con * exp) list,
|
adamc@711
|
194 rules : (int * con list * con * exp) list}
|
adamc@711
|
195 val empty_class = {ground = [],
|
adamc@711
|
196 rules = []}
|
adamc@216
|
197
|
adamc@2
|
198 type env = {
|
adamc@623
|
199 renameK : int SM.map,
|
adamc@623
|
200 relK : string list,
|
adamc@623
|
201
|
adamc@9
|
202 renameC : kind var' SM.map,
|
adamc@2
|
203 relC : (string * kind) list,
|
adamc@11
|
204 namedC : (string * kind * con option) IM.map,
|
adamc@9
|
205
|
adamc@157
|
206 datatypes : datatyp IM.map,
|
adamc@191
|
207 constructors : (datatype_kind * int * string list * con option * int) SM.map,
|
adamc@157
|
208
|
adamc@211
|
209 classes : class CM.map,
|
adamc@211
|
210
|
adamc@9
|
211 renameE : con var' SM.map,
|
adamc@9
|
212 relE : (string * con) list,
|
adamc@31
|
213 namedE : (string * con) IM.map,
|
adamc@31
|
214
|
adamc@31
|
215 renameSgn : (int * sgn) SM.map,
|
adamc@31
|
216 sgn : (string * sgn) IM.map,
|
adamc@31
|
217
|
adamc@31
|
218 renameStr : (int * sgn) SM.map,
|
adamc@31
|
219 str : (string * sgn) IM.map
|
adamc@2
|
220 }
|
adamc@2
|
221
|
adamc@2
|
222 val namedCounter = ref 0
|
adamc@2
|
223
|
adamc@109
|
224 fun newNamed () =
|
adamc@109
|
225 let
|
adamc@109
|
226 val r = !namedCounter
|
adamc@109
|
227 in
|
adamc@109
|
228 namedCounter := r + 1;
|
adamc@109
|
229 r
|
adamc@109
|
230 end
|
adamc@109
|
231
|
adamc@2
|
232 val empty = {
|
adamc@623
|
233 renameK = SM.empty,
|
adamc@623
|
234 relK = [],
|
adamc@623
|
235
|
adamc@2
|
236 renameC = SM.empty,
|
adamc@2
|
237 relC = [],
|
adamc@9
|
238 namedC = IM.empty,
|
adamc@9
|
239
|
adamc@157
|
240 datatypes = IM.empty,
|
adamc@171
|
241 constructors = SM.empty,
|
adamc@157
|
242
|
adamc@211
|
243 classes = CM.empty,
|
adamc@211
|
244
|
adamc@9
|
245 renameE = SM.empty,
|
adamc@9
|
246 relE = [],
|
adamc@31
|
247 namedE = IM.empty,
|
adamc@31
|
248
|
adamc@31
|
249 renameSgn = SM.empty,
|
adamc@31
|
250 sgn = IM.empty,
|
adamc@31
|
251
|
adamc@31
|
252 renameStr = SM.empty,
|
adamc@31
|
253 str = IM.empty
|
adamc@2
|
254 }
|
adamc@2
|
255
|
adamc@623
|
256 fun pushKRel (env : env) x =
|
adamc@623
|
257 let
|
adamc@623
|
258 val renameK = SM.map (fn n => n+1) (#renameK env)
|
adamc@623
|
259 in
|
adamc@623
|
260 {renameK = SM.insert (renameK, x, 0),
|
adamc@623
|
261 relK = x :: #relK env,
|
adamc@623
|
262
|
adamc@623
|
263 renameC = SM.map (fn Rel' (n, k) => Rel' (n, liftKindInKind 0 k)
|
adamc@623
|
264 | x => x) (#renameC env),
|
adamc@623
|
265 relC = map (fn (x, k) => (x, liftKindInKind 0 k)) (#relC env),
|
adamc@623
|
266 namedC = #namedC env,
|
adamc@623
|
267
|
adamc@623
|
268 datatypes = #datatypes env,
|
adamc@623
|
269 constructors = #constructors env,
|
adamc@623
|
270
|
adamc@711
|
271 classes = CM.map (fn cl => {ground = map (fn (c, e) =>
|
adamc@711
|
272 (liftKindInCon 0 c,
|
adamc@711
|
273 e))
|
adamc@711
|
274 (#ground cl),
|
adamc@711
|
275 rules = #rules cl})
|
adamc@711
|
276 (#classes env),
|
adamc@623
|
277
|
adamc@623
|
278 renameE = SM.map (fn Rel' (n, c) => Rel' (n, liftKindInCon 0 c)
|
adamc@623
|
279 | Named' (n, c) => Named' (n, c)) (#renameE env),
|
adamc@623
|
280 relE = map (fn (x, c) => (x, liftKindInCon 0 c)) (#relE env),
|
adamc@623
|
281 namedE = #namedE env,
|
adamc@623
|
282
|
adamc@623
|
283 renameSgn = #renameSgn env,
|
adamc@623
|
284 sgn = #sgn env,
|
adamc@623
|
285
|
adamc@623
|
286 renameStr = #renameStr env,
|
adamc@623
|
287 str = #str env
|
adamc@623
|
288 }
|
adamc@623
|
289 end
|
adamc@623
|
290
|
adamc@623
|
291 fun lookupKRel (env : env) n =
|
adamc@623
|
292 (List.nth (#relK env, n))
|
adamc@623
|
293 handle Subscript => raise UnboundRel n
|
adamc@623
|
294
|
adamc@623
|
295 fun lookupK (env : env) x = SM.find (#renameK env, x)
|
adamc@623
|
296
|
adamc@2
|
297 fun pushCRel (env : env) x k =
|
adamc@2
|
298 let
|
adamc@9
|
299 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
|
adamc@2
|
300 | x => x) (#renameC env)
|
adamc@2
|
301 in
|
adamc@623
|
302 {renameK = #renameK env,
|
adamc@623
|
303 relK = #relK env,
|
adamc@623
|
304
|
adamc@623
|
305 renameC = SM.insert (renameC, x, Rel' (0, k)),
|
adamc@2
|
306 relC = (x, k) :: #relC env,
|
adamc@514
|
307 namedC = #namedC env,
|
adamc@9
|
308
|
adamc@157
|
309 datatypes = #datatypes env,
|
adamc@171
|
310 constructors = #constructors env,
|
adamc@157
|
311
|
adamc@674
|
312 classes = CM.map (fn class =>
|
adamc@711
|
313 {ground = map (fn (c, e) =>
|
adamc@711
|
314 (liftConInCon 0 c,
|
adamc@711
|
315 e))
|
adamc@711
|
316 (#ground class),
|
adamc@711
|
317 rules = #rules class})
|
adamc@211
|
318 (#classes env),
|
adamc@211
|
319
|
adamc@328
|
320 renameE = SM.map (fn Rel' (n, c) => Rel' (n, lift c)
|
adamc@517
|
321 | Named' (n, c) => Named' (n, c)) (#renameE env),
|
adamc@13
|
322 relE = map (fn (x, c) => (x, lift c)) (#relE env),
|
adamc@514
|
323 namedE = #namedE env,
|
adamc@31
|
324
|
adamc@31
|
325 renameSgn = #renameSgn env,
|
adamc@31
|
326 sgn = #sgn env,
|
adamc@31
|
327
|
adamc@31
|
328 renameStr = #renameStr env,
|
adamc@31
|
329 str = #str env
|
adamc@31
|
330 }
|
adamc@2
|
331 end
|
adamc@2
|
332
|
adamc@2
|
333 fun lookupCRel (env : env) n =
|
adamc@2
|
334 (List.nth (#relC env, n))
|
adamc@2
|
335 handle Subscript => raise UnboundRel n
|
adamc@2
|
336
|
adamc@11
|
337 fun pushCNamedAs (env : env) x n k co =
|
adamc@623
|
338 {renameK = #renameK env,
|
adamc@623
|
339 relK = #relK env,
|
adamc@623
|
340
|
adamc@623
|
341 renameC = SM.insert (#renameC env, x, Named' (n, k)),
|
adamc@5
|
342 relC = #relC env,
|
adamc@11
|
343 namedC = IM.insert (#namedC env, n, (x, k, co)),
|
adamc@9
|
344
|
adamc@157
|
345 datatypes = #datatypes env,
|
adamc@171
|
346 constructors = #constructors env,
|
adamc@157
|
347
|
adamc@211
|
348 classes = #classes env,
|
adamc@211
|
349
|
adamc@9
|
350 renameE = #renameE env,
|
adamc@9
|
351 relE = #relE env,
|
adamc@31
|
352 namedE = #namedE env,
|
adamc@31
|
353
|
adamc@31
|
354 renameSgn = #renameSgn env,
|
adamc@31
|
355 sgn = #sgn env,
|
adamc@31
|
356
|
adamc@31
|
357 renameStr = #renameStr env,
|
adamc@31
|
358 str = #str env}
|
adamc@5
|
359
|
adamc@11
|
360 fun pushCNamed env x k co =
|
adamc@2
|
361 let
|
adamc@2
|
362 val n = !namedCounter
|
adamc@2
|
363 in
|
adamc@2
|
364 namedCounter := n + 1;
|
adamc@11
|
365 (pushCNamedAs env x n k co, n)
|
adamc@2
|
366 end
|
adamc@2
|
367
|
adamc@2
|
368 fun lookupCNamed (env : env) n =
|
adamc@2
|
369 case IM.find (#namedC env, n) of
|
adamc@2
|
370 NONE => raise UnboundNamed n
|
adamc@2
|
371 | SOME x => x
|
adamc@2
|
372
|
adamc@2
|
373 fun lookupC (env : env) x =
|
adamc@2
|
374 case SM.find (#renameC env, x) of
|
adamc@9
|
375 NONE => NotBound
|
adamc@9
|
376 | SOME (Rel' x) => Rel x
|
adamc@9
|
377 | SOME (Named' x) => Named x
|
adamc@9
|
378
|
adamc@191
|
379 fun pushDatatype (env : env) n xs xncs =
|
adamc@188
|
380 let
|
adamc@188
|
381 val dk = U.classifyDatatype xncs
|
adamc@188
|
382 in
|
adamc@623
|
383 {renameK = #renameK env,
|
adamc@623
|
384 relK = #relK env,
|
adamc@623
|
385
|
adamc@623
|
386 renameC = #renameC env,
|
adamc@188
|
387 relC = #relC env,
|
adamc@188
|
388 namedC = #namedC env,
|
adamc@157
|
389
|
adamc@188
|
390 datatypes = IM.insert (#datatypes env, n,
|
adamc@191
|
391 (xs, foldl (fn ((x, n, to), cons) =>
|
adamc@191
|
392 IM.insert (cons, n, (x, to))) IM.empty xncs)),
|
adamc@188
|
393 constructors = foldl (fn ((x, n', to), cmap) =>
|
adamc@191
|
394 SM.insert (cmap, x, (dk, n', xs, to, n)))
|
adamc@188
|
395 (#constructors env) xncs,
|
adamc@157
|
396
|
adamc@211
|
397 classes = #classes env,
|
adamc@211
|
398
|
adamc@188
|
399 renameE = #renameE env,
|
adamc@188
|
400 relE = #relE env,
|
adamc@188
|
401 namedE = #namedE env,
|
adamc@157
|
402
|
adamc@188
|
403 renameSgn = #renameSgn env,
|
adamc@188
|
404 sgn = #sgn env,
|
adamc@157
|
405
|
adamc@188
|
406 renameStr = #renameStr env,
|
adamc@188
|
407 str = #str env}
|
adamc@188
|
408 end
|
adamc@157
|
409
|
adamc@157
|
410 fun lookupDatatype (env : env) n =
|
adamc@157
|
411 case IM.find (#datatypes env, n) of
|
adamc@157
|
412 NONE => raise UnboundNamed n
|
adamc@157
|
413 | SOME x => x
|
adamc@157
|
414
|
adamc@191
|
415 fun lookupDatatypeConstructor (_, dt) n =
|
adamc@157
|
416 case IM.find (dt, n) of
|
adamc@157
|
417 NONE => raise UnboundNamed n
|
adamc@157
|
418 | SOME x => x
|
adamc@157
|
419
|
adamc@171
|
420 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
|
adamc@171
|
421
|
adamc@191
|
422 fun datatypeArgs (xs, _) = xs
|
adamc@191
|
423 fun constructors (_, dt) = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
|
adamc@157
|
424
|
adamc@711
|
425 fun listClasses (env : env) =
|
adamc@711
|
426 map (fn (cn, {ground, rules}) =>
|
adamc@711
|
427 (class_name_out cn,
|
adamc@711
|
428 ground
|
adamc@711
|
429 @ map (fn (nvs, cs, c, e) =>
|
adamc@711
|
430 let
|
adamc@711
|
431 val loc = #2 c
|
adamc@711
|
432 val c = foldr (fn (c', c) => (TFun (c', c), loc)) c cs
|
adamc@711
|
433 val c = ListUtil.foldli (fn (n, (), c) => (TCFun (Explicit,
|
adamc@711
|
434 "x" ^ Int.toString n,
|
adamc@711
|
435 (KError, loc),
|
adamc@711
|
436 c), loc))
|
adamc@711
|
437 c (List.tabulate (nvs, fn _ => ()))
|
adamc@711
|
438 in
|
adamc@711
|
439 (c, e)
|
adamc@711
|
440 end) rules)) (CM.listItemsi (#classes env))
|
adamc@711
|
441
|
adamc@211
|
442 fun pushClass (env : env) n =
|
adamc@623
|
443 {renameK = #renameK env,
|
adamc@623
|
444 relK = #relK env,
|
adamc@623
|
445
|
adamc@623
|
446 renameC = #renameC env,
|
adamc@211
|
447 relC = #relC env,
|
adamc@211
|
448 namedC = #namedC env,
|
adamc@211
|
449
|
adamc@211
|
450 datatypes = #datatypes env,
|
adamc@211
|
451 constructors = #constructors env,
|
adamc@211
|
452
|
adamc@675
|
453 classes = CM.insert (#classes env, ClNamed n, empty_class),
|
adamc@211
|
454
|
adamc@211
|
455 renameE = #renameE env,
|
adamc@211
|
456 relE = #relE env,
|
adamc@211
|
457 namedE = #namedE env,
|
adamc@211
|
458
|
adamc@211
|
459 renameSgn = #renameSgn env,
|
adamc@211
|
460 sgn = #sgn env,
|
adamc@211
|
461
|
adamc@211
|
462 renameStr = #renameStr env,
|
adamc@403
|
463 str = #str env}
|
adamc@211
|
464
|
adamc@211
|
465 fun class_name_in (c, _) =
|
adamc@211
|
466 case c of
|
adamc@211
|
467 CNamed n => SOME (ClNamed n)
|
adamc@211
|
468 | CModProj x => SOME (ClProj x)
|
adamc@228
|
469 | CUnif (_, _, _, ref (SOME c)) => class_name_in c
|
adamc@211
|
470 | _ => NONE
|
adamc@211
|
471
|
adamc@403
|
472 fun isClass (env : env) c =
|
adamc@403
|
473 let
|
adamc@403
|
474 fun find NONE = false
|
adamc@403
|
475 | find (SOME c) = Option.isSome (CM.find (#classes env, c))
|
adamc@403
|
476 in
|
adamc@403
|
477 find (class_name_in c)
|
adamc@403
|
478 end
|
adamc@403
|
479
|
adamc@711
|
480 fun class_head_in c =
|
adamc@711
|
481 case #1 c of
|
adamc@711
|
482 CApp (f, _) => class_head_in f
|
adamc@711
|
483 | CUnif (_, _, _, ref (SOME c)) => class_head_in c
|
adamc@711
|
484 | _ => class_name_in c
|
adamc@211
|
485
|
adamc@711
|
486 exception Unify
|
adamc@711
|
487
|
adamc@711
|
488 fun unifyKinds (k1, k2) =
|
adamc@711
|
489 case (#1 k1, #1 k2) of
|
adamc@711
|
490 (KType, KType) => ()
|
adamc@711
|
491 | (KArrow (d1, r1), KArrow (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
|
adamc@711
|
492 | (KName, KName) => ()
|
adamc@711
|
493 | (KRecord k1, KRecord k2) => unifyKinds (k1, k2)
|
adamc@711
|
494 | (KUnit, KUnit) => ()
|
adamc@711
|
495 | (KTuple ks1, KTuple ks2) => (ListPair.appEq unifyKinds (ks1, ks2)
|
adamc@711
|
496 handle ListPair.UnequalLengths => raise Unify)
|
adamc@711
|
497 | (KUnif (_, _, ref (SOME k1)), _) => unifyKinds (k1, k2)
|
adamc@711
|
498 | (_, KUnif (_, _, ref (SOME k2))) => unifyKinds (k1, k2)
|
adamc@711
|
499 | (KRel n1, KRel n2) => if n1 = n2 then () else raise Unify
|
adamc@711
|
500 | (KFun (_, k1), KFun (_, k2)) => unifyKinds (k1, k2)
|
adamc@711
|
501 | _ => raise Unify
|
adamc@711
|
502
|
adamc@711
|
503 fun unifyCons rs =
|
adamc@674
|
504 let
|
adamc@711
|
505 fun unify d (c1, c2) =
|
adamc@711
|
506 case (#1 c1, #1 c2) of
|
adamc@711
|
507 (CUnif (_, _, _, ref (SOME c1)), _) => unify d (c1, c2)
|
adamc@711
|
508 | (_, CUnif (_, _, _, ref (SOME c2))) => unify d (c1, c2)
|
adamc@711
|
509
|
adamc@750
|
510 | (CUnif _, _) => ()
|
adamc@750
|
511
|
adamc@711
|
512 | (c1', CRel n2) =>
|
adamc@711
|
513 if n2 < d then
|
adamc@711
|
514 case c1' of
|
adamc@711
|
515 CRel n1 => if n1 = n2 then () else raise Unify
|
adamc@711
|
516 | _ => raise Unify
|
adamc@711
|
517 else if n2 - d >= length rs then
|
adamc@711
|
518 case c1' of
|
adamc@711
|
519 CRel n1 => if n1 = n2 - length rs then () else raise Unify
|
adamc@711
|
520 | _ => raise Unify
|
adamc@711
|
521 else
|
adamc@711
|
522 let
|
adamc@711
|
523 val r = List.nth (rs, n2 - d)
|
adamc@711
|
524 in
|
adamc@711
|
525 case !r of
|
adamc@711
|
526 NONE => r := SOME c1
|
adamc@711
|
527 | SOME c2 => unify d (c1, c2)
|
adamc@711
|
528 end
|
adamc@711
|
529
|
adamc@711
|
530 | (TFun (d1, r1), TFun (d2, r2)) => (unify d (d1, d2); unify d (r1, r2))
|
adamc@711
|
531 | (TCFun (_, _, k1, r1), TCFun (_, _, k2, r2)) => (unifyKinds (k1, k2); unify (d + 1) (r1, r2))
|
adamc@711
|
532 | (TRecord c1, TRecord c2) => unify d (c1, c2)
|
adamc@711
|
533 | (TDisjoint (a1, b1, c1), TDisjoint (a2, b2, c2)) =>
|
adamc@711
|
534 (unify d (a1, a2); unify d (b1, b2); unify d (c1, c2))
|
adamc@711
|
535
|
adamc@711
|
536 | (CNamed n1, CNamed n2) => if n1 = n2 then () else raise Unify
|
adamc@711
|
537 | (CModProj (n1, ms1, x1), CModProj (n2, ms2, x2)) =>
|
adamc@711
|
538 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then () else raise Unify
|
adamc@711
|
539 | (CApp (f1, x1), CApp (f2, x2)) => (unify d (f1, f2); unify d (x1, x2))
|
adamc@711
|
540 | (CAbs (_, k1, b1), CAbs (_, k2, b2)) => (unifyKinds (k1, k2); unify (d + 1) (b1, b2))
|
adamc@711
|
541
|
adamc@711
|
542 | (CKAbs (_, b1), CKAbs (_, b2)) => unify d (b1, b2)
|
adamc@711
|
543 | (CKApp (c1, k1), CKApp (c2, k2)) => (unify d (c1, c2); unifyKinds (k1, k2))
|
adamc@711
|
544 | (TKFun (_, c1), TKFun (_, c2)) => unify d (c1, c2)
|
adamc@711
|
545
|
adamc@711
|
546 | (CName s1, CName s2) => if s1 = s2 then () else raise Unify
|
adamc@711
|
547
|
adamc@711
|
548 | (CRecord (k1, xcs1), CRecord (k2, xcs2)) =>
|
adamc@711
|
549 (unifyKinds (k1, k2);
|
adamc@711
|
550 ListPair.appEq (fn ((x1, c1), (x2, c2)) => (unify d (x1, x2); unify d (c1, c2))) (xcs1, xcs2)
|
adamc@711
|
551 handle ListPair.UnequalLengths => raise Unify)
|
adamc@711
|
552 | (CConcat (f1, x1), CConcat (f2, x2)) => (unify d (f1, f2); unify d (x1, x2))
|
adamc@711
|
553 | (CMap (d1, r1), CMap (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
|
adamc@711
|
554
|
adamc@711
|
555 | (CUnit, CUnit) => ()
|
adamc@711
|
556
|
adamc@711
|
557 | (CTuple cs1, CTuple cs2) => (ListPair.appEq (unify d) (cs1, cs2)
|
adamc@711
|
558 handle ListPair.UnequalLengths => raise Unify)
|
adamc@711
|
559 | (CProj (c1, n1), CProj (c2, n2)) => (unify d (c1, c2);
|
adamc@711
|
560 if n1 = n2 then () else raise Unify)
|
adamc@711
|
561
|
adamc@711
|
562 | _ => raise Unify
|
adamc@674
|
563 in
|
adamc@711
|
564 unify
|
adamc@674
|
565 end
|
adamc@674
|
566
|
adamc@711
|
567 fun tryUnify nRs (c1, c2) =
|
adamc@674
|
568 let
|
adamc@711
|
569 val rs = List.tabulate (nRs, fn _ => ref NONE)
|
adamc@674
|
570 in
|
adamc@711
|
571 (unifyCons rs 0 (c1, c2);
|
adamc@711
|
572 SOME (map (fn r => case !r of
|
adamc@711
|
573 NONE => raise Unify
|
adamc@711
|
574 | SOME c => c) rs))
|
adamc@711
|
575 handle Unify => NONE
|
adamc@674
|
576 end
|
adamc@674
|
577
|
adamc@711
|
578 fun unifySubst (rs : con list) =
|
adamc@711
|
579 U.Con.mapB {kind = fn _ => fn k => k,
|
adamc@711
|
580 con = fn d => fn c =>
|
adamc@711
|
581 case c of
|
adamc@711
|
582 CRel n =>
|
adamc@711
|
583 if n < d then
|
adamc@711
|
584 c
|
adamc@711
|
585 else
|
adamc@711
|
586 #1 (List.nth (rs, n - d))
|
adamc@711
|
587 | _ => c,
|
adamc@711
|
588 bind = fn (d, U.Con.RelC _) => d + 1
|
adamc@711
|
589 | (d, _) => d}
|
adamc@711
|
590 0
|
adamc@711
|
591
|
adamc@753
|
592 exception Bad of con * con
|
adamc@750
|
593
|
adamc@754
|
594 val hasUnif = U.Con.exists {kind = fn _ => false,
|
adamc@754
|
595 con = fn CUnif (_, _, _, ref NONE) => true
|
adamc@754
|
596 | _ => false}
|
adamc@754
|
597
|
adamc@754
|
598 fun startsWithUnif c =
|
adamc@754
|
599 let
|
adamc@754
|
600 fun firstArg (c, acc) =
|
adamc@754
|
601 case #1 c of
|
adamc@754
|
602 CApp (f, x) => firstArg (f, SOME x)
|
adamc@754
|
603 | _ => acc
|
adamc@754
|
604 in
|
adamc@754
|
605 case firstArg (c, NONE) of
|
adamc@754
|
606 NONE => false
|
adamc@754
|
607 | SOME x => hasUnif x
|
adamc@754
|
608 end
|
adamc@754
|
609
|
adamc@753
|
610 fun resolveClass (hnorm : con -> con) (consEq : con * con -> bool) (env : env) =
|
adamc@674
|
611 let
|
adamc@711
|
612 fun resolve c =
|
adamc@711
|
613 let
|
adamc@711
|
614 fun doHead f =
|
adamc@711
|
615 case CM.find (#classes env, f) of
|
adamc@711
|
616 NONE => NONE
|
adamc@711
|
617 | SOME class =>
|
adamc@711
|
618 let
|
adamc@711
|
619 val loc = #2 c
|
adamc@675
|
620
|
adamc@753
|
621 fun generalize (c as (_, loc)) =
|
adamc@753
|
622 case #1 c of
|
adamc@753
|
623 CApp (f, x) =>
|
adamc@753
|
624 let
|
adamc@753
|
625 val (f, equate) = generalize f
|
adamc@753
|
626
|
adamc@753
|
627 fun isRecord () =
|
adamc@753
|
628 let
|
adamc@753
|
629 val rk = ref NONE
|
adamc@753
|
630 val k = (KUnif (loc, "k", rk), loc)
|
adamc@753
|
631 val r = ref NONE
|
adamc@753
|
632 val rc = (CUnif (loc, k, "x", r), loc)
|
adamc@753
|
633 in
|
adamc@753
|
634 ((CApp (f, rc), loc),
|
adamc@753
|
635 fn () => (if consEq (rc, x) then
|
adamc@753
|
636 true
|
adamc@753
|
637 else
|
adamc@753
|
638 (raise Bad (rc, x);
|
adamc@753
|
639 false))
|
adamc@753
|
640 andalso equate ())
|
adamc@753
|
641 end
|
adamc@753
|
642 in
|
adamc@753
|
643 case #1 x of
|
adamc@753
|
644 CConcat _ => isRecord ()
|
adamc@753
|
645 | CRecord _ => isRecord ()
|
adamc@753
|
646 | _ => ((CApp (f, x), loc), equate)
|
adamc@753
|
647 end
|
adamc@753
|
648 | _ => (c, fn () => true)
|
adamc@753
|
649
|
adamc@753
|
650 val (c, equate) = generalize c
|
adamc@753
|
651
|
adamc@711
|
652 fun tryRules rules =
|
adamc@711
|
653 case rules of
|
adamc@675
|
654 [] => NONE
|
adamc@711
|
655 | (nRs, cs, c', e) :: rules' =>
|
adamc@711
|
656 case tryUnify nRs (c, c') of
|
adamc@711
|
657 NONE => tryRules rules'
|
adamc@711
|
658 | SOME rs =>
|
adamc@675
|
659 let
|
adamc@711
|
660 val eos = map (resolve o unifySubst rs) cs
|
adamc@675
|
661 in
|
adamc@750
|
662 if List.exists (not o Option.isSome) eos
|
adamc@753
|
663 orelse not (equate ())
|
adamc@753
|
664 orelse not (consEq (c, unifySubst rs c')) then
|
adamc@711
|
665 tryRules rules'
|
adamc@711
|
666 else
|
adamc@711
|
667 let
|
adamc@711
|
668 val es = List.mapPartial (fn x => x) eos
|
adamc@711
|
669
|
adamc@711
|
670 val e = foldr (fn (c, e) => (ECApp (e, c), loc)) e rs
|
adamc@711
|
671 val e = foldl (fn (e', e) => (EApp (e, e'), loc)) e es
|
adamc@711
|
672 in
|
adamc@711
|
673 SOME e
|
adamc@711
|
674 end
|
adamc@675
|
675 end
|
adamc@711
|
676
|
adamc@711
|
677 fun rules () = tryRules (#rules class)
|
adamc@711
|
678
|
adamc@711
|
679 fun tryGrounds ces =
|
adamc@711
|
680 case ces of
|
adamc@711
|
681 [] => rules ()
|
adamc@711
|
682 | (c', e) :: ces' =>
|
adamc@711
|
683 case tryUnify 0 (c, c') of
|
adamc@711
|
684 NONE => tryGrounds ces'
|
adamc@711
|
685 | SOME _ => SOME e
|
adamc@675
|
686 in
|
adamc@711
|
687 tryGrounds (#ground class)
|
adamc@675
|
688 end
|
adamc@711
|
689 in
|
adamc@754
|
690 if startsWithUnif c then
|
adamc@754
|
691 NONE
|
adamc@754
|
692 else
|
adamc@754
|
693 case #1 c of
|
adamc@754
|
694 TRecord c =>
|
adamc@754
|
695 (case #1 (hnorm c) of
|
adamc@754
|
696 CRecord (_, xts) =>
|
adamc@754
|
697 let
|
adamc@754
|
698 fun resolver (xts, acc) =
|
adamc@754
|
699 case xts of
|
adamc@754
|
700 [] => SOME (ERecord acc, #2 c)
|
adamc@754
|
701 | (x, t) :: xts =>
|
adamc@754
|
702 let
|
adamc@754
|
703 val t = hnorm t
|
adamc@750
|
704
|
adamc@754
|
705 val t = case t of
|
adamc@754
|
706 (CApp (f, x), loc) => (CApp (hnorm f, hnorm x), loc)
|
adamc@754
|
707 | _ => t
|
adamc@754
|
708 in
|
adamc@754
|
709 case resolve t of
|
adamc@754
|
710 NONE => NONE
|
adamc@754
|
711 | SOME e => resolver (xts, (x, e, t) :: acc)
|
adamc@754
|
712 end
|
adamc@754
|
713 in
|
adamc@754
|
714 resolver (xts, [])
|
adamc@754
|
715 end
|
adamc@754
|
716 | _ => NONE)
|
adamc@754
|
717 | _ =>
|
adamc@754
|
718 case class_head_in c of
|
adamc@754
|
719 SOME f => doHead f
|
adamc@754
|
720 | _ => NONE
|
adamc@711
|
721 end
|
adamc@674
|
722 in
|
adamc@711
|
723 resolve
|
adamc@674
|
724 end
|
adamc@211
|
725
|
adamc@9
|
726 fun pushERel (env : env) x t =
|
adamc@9
|
727 let
|
adamc@9
|
728 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
|
adamc@9
|
729 | x => x) (#renameE env)
|
adamc@211
|
730
|
adamc@675
|
731 val classes = CM.map (fn class =>
|
adamc@711
|
732 {ground = map (fn (c, e) => (c, liftExp e)) (#ground class),
|
adamc@711
|
733 rules = #rules class}) (#classes env)
|
adamc@711
|
734 val classes = case class_head_in t of
|
adamc@211
|
735 NONE => classes
|
adamc@711
|
736 | SOME f =>
|
adamc@403
|
737 case CM.find (classes, f) of
|
adamc@403
|
738 NONE => classes
|
adamc@403
|
739 | SOME class =>
|
adamc@403
|
740 let
|
adamc@711
|
741 val class = {ground = (t, (ERel 0, #2 t)) :: #ground class,
|
adamc@711
|
742 rules = #rules class}
|
adamc@403
|
743 in
|
adamc@403
|
744 CM.insert (classes, f, class)
|
adamc@403
|
745 end
|
adamc@9
|
746 in
|
adamc@623
|
747 {renameK = #renameK env,
|
adamc@623
|
748 relK = #relK env,
|
adamc@623
|
749
|
adamc@623
|
750 renameC = #renameC env,
|
adamc@9
|
751 relC = #relC env,
|
adamc@9
|
752 namedC = #namedC env,
|
adamc@9
|
753
|
adamc@157
|
754 datatypes = #datatypes env,
|
adamc@171
|
755 constructors = #constructors env,
|
adamc@157
|
756
|
adamc@211
|
757 classes = classes,
|
adamc@211
|
758
|
adamc@9
|
759 renameE = SM.insert (renameE, x, Rel' (0, t)),
|
adamc@9
|
760 relE = (x, t) :: #relE env,
|
adamc@31
|
761 namedE = #namedE env,
|
adamc@31
|
762
|
adamc@31
|
763 renameSgn = #renameSgn env,
|
adamc@31
|
764 sgn = #sgn env,
|
adamc@31
|
765
|
adamc@31
|
766 renameStr = #renameStr env,
|
adamc@31
|
767 str = #str env}
|
adamc@9
|
768 end
|
adamc@9
|
769
|
adamc@9
|
770 fun lookupERel (env : env) n =
|
adamc@9
|
771 (List.nth (#relE env, n))
|
adamc@9
|
772 handle Subscript => raise UnboundRel n
|
adamc@9
|
773
|
adamc@674
|
774 fun rule_in c =
|
adamc@674
|
775 let
|
adamc@674
|
776 fun quantifiers (c, nvars) =
|
adamc@674
|
777 case #1 c of
|
adamc@674
|
778 TCFun (_, _, _, c) => quantifiers (c, nvars + 1)
|
adamc@674
|
779 | _ =>
|
adamc@674
|
780 let
|
adamc@674
|
781 fun clauses (c, hyps) =
|
adamc@674
|
782 case #1 c of
|
adamc@674
|
783 TFun (hyp, c) =>
|
adamc@711
|
784 (case class_head_in hyp of
|
adamc@711
|
785 SOME _ => clauses (c, hyp :: hyps)
|
adamc@711
|
786 | NONE => NONE)
|
adamc@674
|
787 | _ =>
|
adamc@711
|
788 case class_head_in c of
|
adamc@674
|
789 NONE => NONE
|
adamc@711
|
790 | SOME f => SOME (f, nvars, rev hyps, c)
|
adamc@674
|
791 in
|
adamc@674
|
792 clauses (c, [])
|
adamc@674
|
793 end
|
adamc@674
|
794 in
|
adamc@711
|
795 quantifiers (c, 0)
|
adamc@677
|
796 end
|
adamc@677
|
797
|
adamc@9
|
798 fun pushENamedAs (env : env) x n t =
|
adamc@211
|
799 let
|
adamc@211
|
800 val classes = #classes env
|
adamc@674
|
801 val classes = case rule_in t of
|
adamc@211
|
802 NONE => classes
|
adamc@711
|
803 | SOME (f, nvs, cs, c) =>
|
adamc@403
|
804 case CM.find (classes, f) of
|
adamc@403
|
805 NONE => classes
|
adamc@403
|
806 | SOME class =>
|
adamc@403
|
807 let
|
adamc@675
|
808 val e = (ENamed n, #2 t)
|
adamc@675
|
809
|
adamc@675
|
810 val class =
|
adamc@711
|
811 {ground = #ground class,
|
adamc@711
|
812 rules = (nvs, cs, c, e) :: #rules class}
|
adamc@403
|
813 in
|
adamc@403
|
814 CM.insert (classes, f, class)
|
adamc@403
|
815 end
|
adamc@211
|
816 in
|
adamc@623
|
817 {renameK = #renameK env,
|
adamc@623
|
818 relK = #relK env,
|
adamc@623
|
819
|
adamc@623
|
820 renameC = #renameC env,
|
adamc@211
|
821 relC = #relC env,
|
adamc@211
|
822 namedC = #namedC env,
|
adamc@9
|
823
|
adamc@211
|
824 datatypes = #datatypes env,
|
adamc@211
|
825 constructors = #constructors env,
|
adamc@157
|
826
|
adamc@211
|
827 classes = classes,
|
adamc@31
|
828
|
adamc@211
|
829 renameE = SM.insert (#renameE env, x, Named' (n, t)),
|
adamc@211
|
830 relE = #relE env,
|
adamc@211
|
831 namedE = IM.insert (#namedE env, n, (x, t)),
|
adamc@211
|
832
|
adamc@211
|
833 renameSgn = #renameSgn env,
|
adamc@211
|
834 sgn = #sgn env,
|
adamc@211
|
835
|
adamc@211
|
836 renameStr = #renameStr env,
|
adamc@211
|
837 str = #str env}
|
adamc@211
|
838 end
|
adamc@9
|
839
|
adamc@9
|
840 fun pushENamed env x t =
|
adamc@9
|
841 let
|
adamc@9
|
842 val n = !namedCounter
|
adamc@9
|
843 in
|
adamc@9
|
844 namedCounter := n + 1;
|
adamc@9
|
845 (pushENamedAs env x n t, n)
|
adamc@9
|
846 end
|
adamc@9
|
847
|
adamc@9
|
848 fun lookupENamed (env : env) n =
|
adamc@9
|
849 case IM.find (#namedE env, n) of
|
adamc@9
|
850 NONE => raise UnboundNamed n
|
adamc@9
|
851 | SOME x => x
|
adamc@9
|
852
|
adamc@471
|
853 fun checkENamed (env : env) n =
|
adamc@471
|
854 Option.isSome (IM.find (#namedE env, n))
|
adamc@471
|
855
|
adamc@9
|
856 fun lookupE (env : env) x =
|
adamc@9
|
857 case SM.find (#renameE env, x) of
|
adamc@9
|
858 NONE => NotBound
|
adamc@9
|
859 | SOME (Rel' x) => Rel x
|
adamc@9
|
860 | SOME (Named' x) => Named x
|
adamc@2
|
861
|
adamc@31
|
862 fun pushSgnNamedAs (env : env) x n sgis =
|
adamc@623
|
863 {renameK = #renameK env,
|
adamc@623
|
864 relK = #relK env,
|
adamc@623
|
865
|
adamc@623
|
866 renameC = #renameC env,
|
adamc@31
|
867 relC = #relC env,
|
adamc@31
|
868 namedC = #namedC env,
|
adamc@31
|
869
|
adamc@157
|
870 datatypes = #datatypes env,
|
adamc@171
|
871 constructors = #constructors env,
|
adamc@157
|
872
|
adamc@211
|
873 classes = #classes env,
|
adamc@211
|
874
|
adamc@31
|
875 renameE = #renameE env,
|
adamc@31
|
876 relE = #relE env,
|
adamc@31
|
877 namedE = #namedE env,
|
adamc@31
|
878
|
adamc@31
|
879 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
|
adamc@31
|
880 sgn = IM.insert (#sgn env, n, (x, sgis)),
|
adamc@31
|
881
|
adamc@31
|
882 renameStr = #renameStr env,
|
adamc@31
|
883 str = #str env}
|
adamc@31
|
884
|
adamc@31
|
885 fun pushSgnNamed env x sgis =
|
adamc@31
|
886 let
|
adamc@31
|
887 val n = !namedCounter
|
adamc@31
|
888 in
|
adamc@31
|
889 namedCounter := n + 1;
|
adamc@31
|
890 (pushSgnNamedAs env x n sgis, n)
|
adamc@31
|
891 end
|
adamc@31
|
892
|
adamc@31
|
893 fun lookupSgnNamed (env : env) n =
|
adamc@31
|
894 case IM.find (#sgn env, n) of
|
adamc@31
|
895 NONE => raise UnboundNamed n
|
adamc@31
|
896 | SOME x => x
|
adamc@31
|
897
|
adamc@31
|
898 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
|
adamc@31
|
899
|
adamc@31
|
900 fun lookupStrNamed (env : env) n =
|
adamc@31
|
901 case IM.find (#str env, n) of
|
adamc@31
|
902 NONE => raise UnboundNamed n
|
adamc@31
|
903 | SOME x => x
|
adamc@31
|
904
|
adamc@31
|
905 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
|
adamc@31
|
906
|
adamc@203
|
907
|
adamc@216
|
908 fun sgiSeek (sgi, (sgns, strs, cons)) =
|
adamc@216
|
909 case sgi of
|
adamc@216
|
910 SgiConAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
911 | SgiCon (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@805
|
912 | SgiDatatype dts => (sgns, strs, foldl (fn ((x, n, _, _), cons) => IM.insert (cons, n, x)) cons dts)
|
adamc@216
|
913 | SgiDatatypeImp (x, n, _, _, _, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
914 | SgiVal _ => (sgns, strs, cons)
|
adamc@216
|
915 | SgiSgn (x, n, _) => (IM.insert (sgns, n, x), strs, cons)
|
adamc@216
|
916 | SgiStr (x, n, _) => (sgns, IM.insert (strs, n, x), cons)
|
adamc@216
|
917 | SgiConstraint _ => (sgns, strs, cons)
|
adamc@563
|
918 | SgiClassAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@563
|
919 | SgiClass (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
920
|
adamc@34
|
921 fun sgnSeek f sgis =
|
adamc@34
|
922 let
|
adamc@59
|
923 fun seek (sgis, sgns, strs, cons) =
|
adamc@34
|
924 case sgis of
|
adamc@34
|
925 [] => NONE
|
adamc@34
|
926 | (sgi, _) :: sgis =>
|
adamc@34
|
927 case f sgi of
|
adamc@158
|
928 SOME v =>
|
adamc@158
|
929 let
|
adamc@158
|
930 val cons =
|
adamc@158
|
931 case sgi of
|
adamc@805
|
932 SgiDatatype dts => foldl (fn ((x, n, _, _), cons) => IM.insert (cons, n, x)) cons dts
|
adamc@191
|
933 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
|
adamc@158
|
934 | _ => cons
|
adamc@158
|
935 in
|
adamc@158
|
936 SOME (v, (sgns, strs, cons))
|
adamc@158
|
937 end
|
adamc@88
|
938 | NONE =>
|
adamc@216
|
939 let
|
adamc@216
|
940 val (sgns, strs, cons) = sgiSeek (sgi, (sgns, strs, cons))
|
adamc@216
|
941 in
|
adamc@216
|
942 seek (sgis, sgns, strs, cons)
|
adamc@216
|
943 end
|
adamc@34
|
944 in
|
adamc@59
|
945 seek (sgis, IM.empty, IM.empty, IM.empty)
|
adamc@34
|
946 end
|
adamc@34
|
947
|
adamc@34
|
948 fun id x = x
|
adamc@34
|
949
|
adamc@34
|
950 fun unravelStr (str, _) =
|
adamc@34
|
951 case str of
|
adamc@34
|
952 StrVar x => (x, [])
|
adamc@34
|
953 | StrProj (str, m) =>
|
adamc@34
|
954 let
|
adamc@34
|
955 val (x, ms) = unravelStr str
|
adamc@34
|
956 in
|
adamc@34
|
957 (x, ms @ [m])
|
adamc@34
|
958 end
|
adamc@34
|
959 | _ => raise Fail "unravelStr"
|
adamc@34
|
960
|
adamc@59
|
961 fun sgnS_con (str, (sgns, strs, cons)) c =
|
adamc@34
|
962 case c of
|
adamc@34
|
963 CModProj (m1, ms, x) =>
|
adamc@34
|
964 (case IM.find (strs, m1) of
|
adamc@34
|
965 NONE => c
|
adamc@34
|
966 | SOME m1x =>
|
adamc@34
|
967 let
|
adamc@34
|
968 val (m1, ms') = unravelStr str
|
adamc@34
|
969 in
|
adamc@34
|
970 CModProj (m1, ms' @ m1x :: ms, x)
|
adamc@34
|
971 end)
|
adamc@34
|
972 | CNamed n =>
|
adamc@34
|
973 (case IM.find (cons, n) of
|
adamc@34
|
974 NONE => c
|
adamc@34
|
975 | SOME nx =>
|
adamc@34
|
976 let
|
adamc@34
|
977 val (m1, ms) = unravelStr str
|
adamc@34
|
978 in
|
adamc@34
|
979 CModProj (m1, ms, nx)
|
adamc@34
|
980 end)
|
adamc@34
|
981 | _ => c
|
adamc@34
|
982
|
adamc@721
|
983 fun sgnS_con' (m1, ms', (sgns, strs, cons)) =
|
adamc@721
|
984 U.Con.map {kind = fn x => x,
|
adamc@721
|
985 con = fn c =>
|
adamc@721
|
986 case c of
|
adamc@721
|
987 CModProj (m1, ms, x) =>
|
adamc@721
|
988 (case IM.find (strs, m1) of
|
adamc@721
|
989 NONE => c
|
adamc@721
|
990 | SOME m1x => CModProj (m1, ms' @ m1x :: ms, x))
|
adamc@721
|
991 | CNamed n =>
|
adamc@721
|
992 (case IM.find (cons, n) of
|
adamc@721
|
993 NONE => c
|
adamc@721
|
994 | SOME nx => CModProj (m1, ms', nx))
|
adamc@721
|
995 | _ => c}
|
adamc@216
|
996
|
adamc@59
|
997 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
|
adamc@59
|
998 case sgn of
|
adamc@59
|
999 SgnProj (m1, ms, x) =>
|
adamc@59
|
1000 (case IM.find (strs, m1) of
|
adamc@59
|
1001 NONE => sgn
|
adamc@59
|
1002 | SOME m1x =>
|
adamc@59
|
1003 let
|
adamc@59
|
1004 val (m1, ms') = unravelStr str
|
adamc@59
|
1005 in
|
adamc@59
|
1006 SgnProj (m1, ms' @ m1x :: ms, x)
|
adamc@59
|
1007 end)
|
adamc@59
|
1008 | SgnVar n =>
|
adamc@59
|
1009 (case IM.find (sgns, n) of
|
adamc@59
|
1010 NONE => sgn
|
adamc@59
|
1011 | SOME nx =>
|
adamc@59
|
1012 let
|
adamc@59
|
1013 val (m1, ms) = unravelStr str
|
adamc@59
|
1014 in
|
adamc@59
|
1015 SgnProj (m1, ms, nx)
|
adamc@59
|
1016 end)
|
adamc@59
|
1017 | _ => sgn
|
adamc@59
|
1018
|
adamc@59
|
1019 fun sgnSubSgn x =
|
adamc@34
|
1020 ElabUtil.Sgn.map {kind = id,
|
adamc@59
|
1021 con = sgnS_con x,
|
adamc@34
|
1022 sgn_item = id,
|
adamc@59
|
1023 sgn = sgnS_sgn x}
|
adamc@34
|
1024
|
adamc@211
|
1025
|
adamc@211
|
1026
|
adamc@211
|
1027 and projectSgn env {sgn, str, field} =
|
adamc@211
|
1028 case #1 (hnormSgn env sgn) of
|
adamc@211
|
1029 SgnConst sgis =>
|
adamc@211
|
1030 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@211
|
1031 NONE => NONE
|
adamc@211
|
1032 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@211
|
1033 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@211
|
1034 | _ => NONE
|
adamc@211
|
1035
|
adamc@211
|
1036 and hnormSgn env (all as (sgn, loc)) =
|
adamc@34
|
1037 case sgn of
|
adamc@42
|
1038 SgnError => all
|
adamc@42
|
1039 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
|
adamc@42
|
1040 | SgnConst _ => all
|
adamc@42
|
1041 | SgnFun _ => all
|
adamc@59
|
1042 | SgnProj (m, ms, x) =>
|
adamc@59
|
1043 let
|
adamc@59
|
1044 val (_, sgn) = lookupStrNamed env m
|
adamc@59
|
1045 in
|
adamc@59
|
1046 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
|
adamc@59
|
1047 sgn = sgn,
|
adamc@59
|
1048 field = x} of
|
adamc@59
|
1049 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
|
adamc@59
|
1050 | SOME sgn => sgn
|
adamc@59
|
1051 end
|
adamc@42
|
1052 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
1053 case #1 (hnormSgn env sgn) of
|
adamc@42
|
1054 SgnError => (SgnError, loc)
|
adamc@42
|
1055 | SgnConst sgis =>
|
adamc@42
|
1056 let
|
adamc@42
|
1057 fun traverse (pre, post) =
|
adamc@42
|
1058 case post of
|
adamc@42
|
1059 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
|
adamc@42
|
1060 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
|
adamc@42
|
1061 if x = x' then
|
adamc@42
|
1062 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
|
adamc@42
|
1063 else
|
adamc@42
|
1064 traverse (sgi :: pre, rest)
|
adamc@42
|
1065 | sgi :: rest => traverse (sgi :: pre, rest)
|
adamc@42
|
1066
|
adamc@42
|
1067 val sgis = traverse ([], sgis)
|
adamc@42
|
1068 in
|
adamc@42
|
1069 (SgnConst sgis, loc)
|
adamc@42
|
1070 end
|
adamc@42
|
1071 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
|
adamc@42
|
1072
|
adamc@849
|
1073 fun manifest (m, ms, loc) =
|
adamc@849
|
1074 foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms
|
adamc@849
|
1075
|
adamc@211
|
1076 fun enrichClasses env classes (m1, ms) sgn =
|
adamc@59
|
1077 case #1 (hnormSgn env sgn) of
|
adamc@59
|
1078 SgnConst sgis =>
|
adamc@211
|
1079 let
|
adamc@218
|
1080 val (classes, _, _, _) =
|
adamc@218
|
1081 foldl (fn (sgi, (classes, newClasses, fmap, env)) =>
|
adamc@211
|
1082 let
|
adamc@211
|
1083 fun found (x, n) =
|
adamc@211
|
1084 (CM.insert (classes,
|
adamc@211
|
1085 ClProj (m1, ms, x),
|
adamc@211
|
1086 empty_class),
|
adamc@216
|
1087 IM.insert (newClasses, n, x),
|
adamc@218
|
1088 sgiSeek (#1 sgi, fmap),
|
adamc@218
|
1089 env)
|
adamc@216
|
1090
|
adamc@218
|
1091 fun default () = (classes, newClasses, sgiSeek (#1 sgi, fmap), env)
|
adamc@211
|
1092 in
|
adamc@211
|
1093 case #1 sgi of
|
adamc@217
|
1094 SgiStr (x, _, sgn) =>
|
adamc@849
|
1095 let
|
adamc@849
|
1096 val str = manifest (m1, ms, #2 sgi)
|
adamc@849
|
1097 val sgn' = sgnSubSgn (str, fmap) sgn
|
adamc@849
|
1098 in
|
adamc@849
|
1099 (enrichClasses env classes (m1, ms @ [x]) sgn',
|
adamc@849
|
1100 newClasses,
|
adamc@849
|
1101 sgiSeek (#1 sgi, fmap),
|
adamc@849
|
1102 env)
|
adamc@849
|
1103 end
|
adamc@218
|
1104 | SgiSgn (x, n, sgn) =>
|
adamc@218
|
1105 (classes,
|
adamc@218
|
1106 newClasses,
|
adamc@218
|
1107 fmap,
|
adamc@218
|
1108 pushSgnNamedAs env x n sgn)
|
adamc@218
|
1109
|
adamc@563
|
1110 | SgiClassAbs (x, n, _) => found (x, n)
|
adamc@563
|
1111 | SgiClass (x, n, _, _) => found (x, n)
|
adamc@674
|
1112 | SgiVal (x, n, c) =>
|
adamc@674
|
1113 (case rule_in c of
|
adamc@674
|
1114 NONE => default ()
|
adamc@711
|
1115 | SOME (cn, nvs, cs, c) =>
|
adamc@674
|
1116 let
|
adamc@711
|
1117 val loc = #2 c
|
adamc@721
|
1118 val globalize = sgnS_con' (m1, ms, fmap)
|
adamc@674
|
1119
|
adamc@674
|
1120 val nc =
|
adamc@674
|
1121 case cn of
|
adamc@674
|
1122 ClNamed f => IM.find (newClasses, f)
|
adamc@674
|
1123 | _ => NONE
|
adamc@674
|
1124 in
|
adamc@674
|
1125 case nc of
|
adamc@674
|
1126 NONE =>
|
adamc@419
|
1127 let
|
adamc@419
|
1128 val classes =
|
adamc@419
|
1129 case CM.find (classes, cn) of
|
adamc@419
|
1130 NONE => classes
|
adamc@419
|
1131 | SOME class =>
|
adamc@419
|
1132 let
|
adamc@711
|
1133 val e = (EModProj (m1, ms, x), #2 sgn)
|
adamc@675
|
1134
|
adamc@675
|
1135 val class =
|
adamc@711
|
1136 {ground = #ground class,
|
adamc@711
|
1137 rules = (nvs,
|
adamc@711
|
1138 map globalize cs,
|
adamc@711
|
1139 globalize c,
|
adamc@711
|
1140 e) :: #rules class}
|
adamc@419
|
1141 in
|
adamc@419
|
1142 CM.insert (classes, cn, class)
|
adamc@419
|
1143 end
|
adamc@419
|
1144 in
|
adamc@419
|
1145 (classes,
|
adamc@419
|
1146 newClasses,
|
adamc@419
|
1147 fmap,
|
adamc@419
|
1148 env)
|
adamc@419
|
1149 end
|
adamc@674
|
1150 | SOME fx =>
|
adamc@674
|
1151 let
|
adamc@674
|
1152 val cn = ClProj (m1, ms, fx)
|
adamc@419
|
1153
|
adamc@674
|
1154 val classes =
|
adamc@674
|
1155 case CM.find (classes, cn) of
|
adamc@674
|
1156 NONE => classes
|
adamc@674
|
1157 | SOME class =>
|
adamc@674
|
1158 let
|
adamc@675
|
1159 val e = (EModProj (m1, ms, x), #2 sgn)
|
adamc@675
|
1160
|
adamc@675
|
1161 val class =
|
adamc@711
|
1162 {ground = #ground class,
|
adamc@711
|
1163 rules = (nvs,
|
adamc@711
|
1164 map globalize cs,
|
adamc@711
|
1165 globalize c,
|
adamc@711
|
1166 e) :: #rules class}
|
adamc@674
|
1167 in
|
adamc@674
|
1168 CM.insert (classes, cn, class)
|
adamc@674
|
1169 end
|
adamc@674
|
1170 in
|
adamc@674
|
1171 (classes,
|
adamc@674
|
1172 newClasses,
|
adamc@674
|
1173 fmap,
|
adamc@674
|
1174 env)
|
adamc@674
|
1175 end
|
adamc@674
|
1176 end)
|
adamc@216
|
1177 | _ => default ()
|
adamc@211
|
1178 end)
|
adamc@218
|
1179 (classes, IM.empty, (IM.empty, IM.empty, IM.empty), env) sgis
|
adamc@211
|
1180 in
|
adamc@211
|
1181 classes
|
adamc@211
|
1182 end
|
adamc@211
|
1183 | _ => classes
|
adamc@211
|
1184
|
adamc@211
|
1185 fun pushStrNamedAs (env : env) x n sgn =
|
adamc@623
|
1186 {renameK = #renameK env,
|
adamc@623
|
1187 relK = #relK env,
|
adamc@623
|
1188
|
adamc@623
|
1189 renameC = #renameC env,
|
adamc@211
|
1190 relC = #relC env,
|
adamc@211
|
1191 namedC = #namedC env,
|
adamc@211
|
1192
|
adamc@211
|
1193 datatypes = #datatypes env,
|
adamc@211
|
1194 constructors = #constructors env,
|
adamc@211
|
1195
|
adamc@211
|
1196 classes = enrichClasses env (#classes env) (n, []) sgn,
|
adamc@211
|
1197
|
adamc@211
|
1198 renameE = #renameE env,
|
adamc@211
|
1199 relE = #relE env,
|
adamc@211
|
1200 namedE = #namedE env,
|
adamc@211
|
1201
|
adamc@211
|
1202 renameSgn = #renameSgn env,
|
adamc@211
|
1203 sgn = #sgn env,
|
adamc@211
|
1204
|
adamc@211
|
1205 renameStr = SM.insert (#renameStr env, x, (n, sgn)),
|
adamc@211
|
1206 str = IM.insert (#str env, n, (x, sgn))}
|
adamc@211
|
1207
|
adamc@211
|
1208 fun pushStrNamed env x sgn =
|
adamc@211
|
1209 let
|
adamc@211
|
1210 val n = !namedCounter
|
adamc@211
|
1211 in
|
adamc@211
|
1212 namedCounter := n + 1;
|
adamc@211
|
1213 (pushStrNamedAs env x n sgn, n)
|
adamc@211
|
1214 end
|
adamc@211
|
1215
|
adamc@211
|
1216 fun sgiBinds env (sgi, loc) =
|
adamc@211
|
1217 case sgi of
|
adamc@211
|
1218 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
|
adamc@211
|
1219 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@805
|
1220 | SgiDatatype dts =>
|
adamc@211
|
1221 let
|
adamc@805
|
1222 fun doOne ((x, n, xs, xncs), env) =
|
adamc@805
|
1223 let
|
adamc@805
|
1224 val k = (KType, loc)
|
adamc@805
|
1225 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
|
adamc@341
|
1226
|
adamc@805
|
1227 val env = pushCNamedAs env x n k' NONE
|
adamc@805
|
1228 in
|
adamc@805
|
1229 foldl (fn ((x', n', to), env) =>
|
adamc@805
|
1230 let
|
adamc@805
|
1231 val t =
|
adamc@805
|
1232 case to of
|
adamc@805
|
1233 NONE => (CNamed n, loc)
|
adamc@805
|
1234 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@805
|
1235
|
adamc@805
|
1236 val k = (KType, loc)
|
adamc@805
|
1237 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@805
|
1238 in
|
adamc@805
|
1239 pushENamedAs env x' n' t
|
adamc@805
|
1240 end)
|
adamc@805
|
1241 env xncs
|
adamc@805
|
1242 end
|
adamc@211
|
1243 in
|
adamc@805
|
1244 foldl doOne env dts
|
adamc@211
|
1245 end
|
adamc@211
|
1246 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
|
adamc@211
|
1247 let
|
adamc@341
|
1248 val k = (KType, loc)
|
adamc@341
|
1249 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
|
adamc@341
|
1250
|
adamc@341
|
1251 val env = pushCNamedAs env x n k' (SOME (CModProj (m1, ms, x'), loc))
|
adamc@211
|
1252 in
|
adamc@211
|
1253 foldl (fn ((x', n', to), env) =>
|
adamc@211
|
1254 let
|
adamc@211
|
1255 val t =
|
adamc@211
|
1256 case to of
|
adamc@211
|
1257 NONE => (CNamed n, loc)
|
adamc@211
|
1258 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@211
|
1259
|
adamc@211
|
1260 val k = (KType, loc)
|
adamc@211
|
1261 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@211
|
1262 in
|
adamc@211
|
1263 pushENamedAs env x' n' t
|
adamc@211
|
1264 end)
|
adamc@211
|
1265 env xncs
|
adamc@211
|
1266 end
|
adamc@211
|
1267 | SgiVal (x, n, t) => pushENamedAs env x n t
|
adamc@211
|
1268 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@211
|
1269 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@211
|
1270 | SgiConstraint _ => env
|
adamc@211
|
1271
|
adamc@711
|
1272 | SgiClassAbs (x, n, k) => pushCNamedAs env x n k NONE
|
adamc@711
|
1273 | SgiClass (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@459
|
1274
|
adamc@211
|
1275 fun sgnSubCon x =
|
adamc@211
|
1276 ElabUtil.Con.map {kind = id,
|
adamc@211
|
1277 con = sgnS_con x}
|
adamc@59
|
1278
|
adamc@59
|
1279 fun projectStr env {sgn, str, field} =
|
adamc@59
|
1280 case #1 (hnormSgn env sgn) of
|
adamc@59
|
1281 SgnConst sgis =>
|
adamc@59
|
1282 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
1283 NONE => NONE
|
adamc@59
|
1284 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
1285 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
1286 | _ => NONE
|
adamc@59
|
1287
|
adamc@158
|
1288 fun chaseMpath env (n, ms) =
|
adamc@158
|
1289 let
|
adamc@158
|
1290 val (_, sgn) = lookupStrNamed env n
|
adamc@158
|
1291 in
|
adamc@158
|
1292 foldl (fn (m, (str, sgn)) =>
|
adamc@158
|
1293 case projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@158
|
1294 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@158
|
1295 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
|
adamc@158
|
1296 ((StrVar n, #2 sgn), sgn) ms
|
adamc@158
|
1297 end
|
adamc@158
|
1298
|
adamc@42
|
1299 fun projectCon env {sgn, str, field} =
|
adamc@42
|
1300 case #1 (hnormSgn env sgn) of
|
adamc@34
|
1301 SgnConst sgis =>
|
adamc@34
|
1302 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
|
adamc@34
|
1303 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
|
adamc@805
|
1304 | SgiDatatype dts =>
|
adamc@805
|
1305 (case List.find (fn (x, _, xs, _) => x = field) dts of
|
adamc@805
|
1306 SOME (_, _, xs, _) =>
|
adamc@805
|
1307 let
|
adamc@805
|
1308 val k = (KType, #2 sgn)
|
adamc@805
|
1309 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
|
adamc@805
|
1310 in
|
adamc@805
|
1311 SOME (k', NONE)
|
adamc@805
|
1312 end
|
adamc@805
|
1313 | NONE => NONE)
|
adamc@341
|
1314 | SgiDatatypeImp (x, _, m1, ms, x', xs, _) =>
|
adamc@341
|
1315 if x = field then
|
adamc@341
|
1316 let
|
adamc@341
|
1317 val k = (KType, #2 sgn)
|
adamc@341
|
1318 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
|
adamc@341
|
1319 in
|
adamc@341
|
1320 SOME (k', SOME (CModProj (m1, ms, x'), #2 sgn))
|
adamc@341
|
1321 end
|
adamc@158
|
1322 else
|
adamc@158
|
1323 NONE
|
adamc@563
|
1324 | SgiClassAbs (x, _, k) => if x = field then
|
adamc@563
|
1325 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), NONE)
|
adamc@563
|
1326 else
|
adamc@563
|
1327 NONE
|
adamc@563
|
1328 | SgiClass (x, _, k, c) => if x = field then
|
adamc@563
|
1329 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), SOME c)
|
adamc@563
|
1330 else
|
adamc@563
|
1331 NONE
|
adamc@34
|
1332 | _ => NONE) sgis of
|
adamc@34
|
1333 NONE => NONE
|
adamc@34
|
1334 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
|
adamc@34
|
1335 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
|
adamc@42
|
1336 | _ => NONE
|
adamc@34
|
1337
|
adamc@157
|
1338 fun projectDatatype env {sgn, str, field} =
|
adamc@157
|
1339 case #1 (hnormSgn env sgn) of
|
adamc@157
|
1340 SgnConst sgis =>
|
adamc@805
|
1341 (case sgnSeek (fn SgiDatatype dts =>
|
adamc@805
|
1342 (case List.find (fn (x, _, _, _) => x = field) dts of
|
adamc@805
|
1343 SOME (_, _, xs, xncs) => SOME (xs, xncs)
|
adamc@805
|
1344 | NONE => NONE)
|
adamc@191
|
1345 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
|
adamc@157
|
1346 | _ => NONE) sgis of
|
adamc@157
|
1347 NONE => NONE
|
adamc@191
|
1348 | SOME ((xs, xncs), subs) => SOME (xs,
|
adamc@191
|
1349 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
|
adamc@157
|
1350 | _ => NONE
|
adamc@157
|
1351
|
adamc@174
|
1352 fun projectConstructor env {sgn, str, field} =
|
adamc@174
|
1353 case #1 (hnormSgn env sgn) of
|
adamc@174
|
1354 SgnConst sgis =>
|
adamc@174
|
1355 let
|
adamc@191
|
1356 fun consider (n, xs, xncs) =
|
adamc@174
|
1357 ListUtil.search (fn (x, n', to) =>
|
adamc@174
|
1358 if x <> field then
|
adamc@174
|
1359 NONE
|
adamc@174
|
1360 else
|
adamc@191
|
1361 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
|
adamc@174
|
1362 in
|
adamc@805
|
1363 case sgnSeek (fn SgiDatatype dts =>
|
adamc@805
|
1364 let
|
adamc@805
|
1365 fun search dts =
|
adamc@805
|
1366 case dts of
|
adamc@805
|
1367 [] => NONE
|
adamc@805
|
1368 | (_, n, xs, xncs) :: dts =>
|
adamc@805
|
1369 case consider (n, xs, xncs) of
|
adamc@805
|
1370 NONE => search dts
|
adamc@805
|
1371 | v => v
|
adamc@805
|
1372 in
|
adamc@805
|
1373 search dts
|
adamc@805
|
1374 end
|
adamc@191
|
1375 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
|
adamc@174
|
1376 | _ => NONE) sgis of
|
adamc@174
|
1377 NONE => NONE
|
adamc@191
|
1378 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
|
adamc@191
|
1379 sgnSubCon (str, subs) t)
|
adamc@174
|
1380 end
|
adamc@174
|
1381 | _ => NONE
|
adamc@174
|
1382
|
adamc@42
|
1383 fun projectVal env {sgn, str, field} =
|
adamc@42
|
1384 case #1 (hnormSgn env sgn) of
|
adamc@34
|
1385 SgnConst sgis =>
|
adamc@162
|
1386 let
|
adamc@191
|
1387 fun seek (n, xs, xncs) =
|
adamc@162
|
1388 ListUtil.search (fn (x, _, to) =>
|
adamc@162
|
1389 if x = field then
|
adamc@191
|
1390 SOME (let
|
adamc@467
|
1391 val base = (CNamed n, #2 sgn)
|
adamc@467
|
1392 val nxs = length xs
|
adamc@467
|
1393 val base = ListUtil.foldli (fn (i, _, base) =>
|
adamc@467
|
1394 (CApp (base,
|
adamc@467
|
1395 (CRel (nxs - i - 1), #2 sgn)),
|
adamc@467
|
1396 #2 sgn))
|
adamc@467
|
1397 base xs
|
adamc@467
|
1398
|
adamc@191
|
1399 val t =
|
adamc@191
|
1400 case to of
|
adamc@467
|
1401 NONE => base
|
adamc@467
|
1402 | SOME t => (TFun (t, base), #2 sgn)
|
adamc@191
|
1403 val k = (KType, #2 sgn)
|
adamc@191
|
1404 in
|
adamc@467
|
1405 foldr (fn (x, t) => (TCFun (Implicit, x, k, t), #2 sgn))
|
adamc@191
|
1406 t xs
|
adamc@191
|
1407 end)
|
adamc@162
|
1408 else
|
adamc@162
|
1409 NONE) xncs
|
adamc@162
|
1410 in
|
adamc@162
|
1411 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
|
adamc@805
|
1412 | SgiDatatype dts =>
|
adamc@805
|
1413 let
|
adamc@805
|
1414 fun search dts =
|
adamc@805
|
1415 case dts of
|
adamc@805
|
1416 [] => NONE
|
adamc@805
|
1417 | (_, n, xs, xncs) :: dts =>
|
adamc@805
|
1418 case seek (n, xs, xncs) of
|
adamc@805
|
1419 NONE => search dts
|
adamc@805
|
1420 | v => v
|
adamc@805
|
1421 in
|
adamc@805
|
1422 search dts
|
adamc@805
|
1423 end
|
adamc@191
|
1424 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
|
adamc@162
|
1425 | _ => NONE) sgis of
|
adamc@162
|
1426 NONE => NONE
|
adamc@162
|
1427 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
|
adamc@162
|
1428 end
|
adamc@34
|
1429 | SgnError => SOME (CError, ErrorMsg.dummySpan)
|
adamc@42
|
1430 | _ => NONE
|
adamc@34
|
1431
|
adamc@88
|
1432 fun sgnSeekConstraints (str, sgis) =
|
adamc@88
|
1433 let
|
adamc@88
|
1434 fun seek (sgis, sgns, strs, cons, acc) =
|
adamc@88
|
1435 case sgis of
|
adamc@88
|
1436 [] => acc
|
adamc@88
|
1437 | (sgi, _) :: sgis =>
|
adamc@88
|
1438 case sgi of
|
adamc@88
|
1439 SgiConstraint (c1, c2) =>
|
adamc@88
|
1440 let
|
adamc@88
|
1441 val sub = sgnSubCon (str, (sgns, strs, cons))
|
adamc@88
|
1442 in
|
adamc@88
|
1443 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
|
adamc@88
|
1444 end
|
adamc@88
|
1445 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1446 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@805
|
1447 | SgiDatatype dts => seek (sgis, sgns, strs,
|
adamc@805
|
1448 foldl (fn ((x, n, _, _), cons) => IM.insert (cons, n, x)) cons dts, acc)
|
adamc@191
|
1449 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1450 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@88
|
1451 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
|
adamc@88
|
1452 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
|
adamc@563
|
1453 | SgiClassAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@563
|
1454 | SgiClass (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1455 in
|
adamc@88
|
1456 seek (sgis, IM.empty, IM.empty, IM.empty, [])
|
adamc@88
|
1457 end
|
adamc@88
|
1458
|
adamc@88
|
1459 fun projectConstraints env {sgn, str} =
|
adamc@88
|
1460 case #1 (hnormSgn env sgn) of
|
adamc@88
|
1461 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
|
adamc@88
|
1462 | SgnError => SOME []
|
adamc@88
|
1463 | _ => NONE
|
adamc@34
|
1464
|
adamc@825
|
1465 fun patBinds env (p, loc) =
|
adamc@825
|
1466 case p of
|
adamc@825
|
1467 PWild => env
|
adamc@825
|
1468 | PVar (x, t) => pushERel env x t
|
adamc@825
|
1469 | PPrim _ => env
|
adamc@825
|
1470 | PCon (_, _, _, NONE) => env
|
adamc@825
|
1471 | PCon (_, _, _, SOME p) => patBinds env p
|
adamc@825
|
1472 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
|
adamc@825
|
1473
|
adamc@447
|
1474 fun edeclBinds env (d, loc) =
|
adamc@447
|
1475 case d of
|
adamc@825
|
1476 EDVal (p, _, _) => patBinds env p
|
adamc@447
|
1477 | EDValRec vis => foldl (fn ((x, t, _), env) => pushERel env x t) env vis
|
adamc@447
|
1478
|
adamc@157
|
1479 fun declBinds env (d, loc) =
|
adamc@157
|
1480 case d of
|
adamc@157
|
1481 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@805
|
1482 | DDatatype dts =>
|
adamc@157
|
1483 let
|
adamc@805
|
1484 fun doOne ((x, n, xs, xncs), env) =
|
adamc@805
|
1485 let
|
adamc@805
|
1486 val k = (KType, loc)
|
adamc@805
|
1487 val nxs = length xs
|
adamc@805
|
1488 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
|
adamc@805
|
1489 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
|
adamc@805
|
1490 (KArrow (k, kb), loc)))
|
adamc@805
|
1491 ((CNamed n, loc), k) xs
|
adamc@805
|
1492
|
adamc@805
|
1493 val env = pushCNamedAs env x n kb NONE
|
adamc@805
|
1494 val env = pushDatatype env n xs xncs
|
adamc@805
|
1495 in
|
adamc@805
|
1496 foldl (fn ((x', n', to), env) =>
|
adamc@805
|
1497 let
|
adamc@805
|
1498 val t =
|
adamc@805
|
1499 case to of
|
adamc@805
|
1500 NONE => tb
|
adamc@805
|
1501 | SOME t => (TFun (t, tb), loc)
|
adamc@805
|
1502 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
|
adamc@805
|
1503 in
|
adamc@805
|
1504 pushENamedAs env x' n' t
|
adamc@805
|
1505 end)
|
adamc@805
|
1506 env xncs
|
adamc@805
|
1507 end
|
adamc@157
|
1508 in
|
adamc@805
|
1509 foldl doOne env dts
|
adamc@157
|
1510 end
|
adamc@191
|
1511 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
|
adamc@157
|
1512 let
|
adamc@157
|
1513 val t = (CModProj (m, ms, x'), loc)
|
adamc@297
|
1514 val k = (KType, loc)
|
adamc@297
|
1515 val nxs = length xs
|
adamc@297
|
1516 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
|
adamc@297
|
1517 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
|
adamc@297
|
1518 (KArrow (k, kb), loc)))
|
adamc@297
|
1519 ((CNamed n, loc), k) xs
|
adamc@297
|
1520
|
adamc@341
|
1521 val env = pushCNamedAs env x n kb (SOME t)
|
adamc@191
|
1522 val env = pushDatatype env n xs xncs
|
adamc@157
|
1523 in
|
adamc@191
|
1524 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
1525 let
|
adamc@191
|
1526 val t =
|
adamc@191
|
1527 case to of
|
adamc@297
|
1528 NONE => tb
|
adamc@297
|
1529 | SOME t => (TFun (t, tb), loc)
|
adamc@297
|
1530 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
|
adamc@191
|
1531 in
|
adamc@191
|
1532 pushENamedAs env x' n' t
|
adamc@191
|
1533 end)
|
adamc@191
|
1534 env xncs
|
adamc@157
|
1535 end
|
adamc@157
|
1536 | DVal (x, n, t, _) => pushENamedAs env x n t
|
adamc@157
|
1537 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
|
adamc@157
|
1538 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@157
|
1539 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
|
adamc@157
|
1540 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@157
|
1541 | DConstraint _ => env
|
adamc@157
|
1542 | DExport _ => env
|
adamc@707
|
1543 | DTable (tn, x, n, c, _, pc, _, cc) =>
|
adamc@205
|
1544 let
|
adamc@705
|
1545 val ct = (CModProj (tn, [], "sql_table"), loc)
|
adamc@705
|
1546 val ct = (CApp (ct, c), loc)
|
adamc@707
|
1547 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
|
adamc@338
|
1548 in
|
adamc@705
|
1549 pushENamedAs env x n ct
|
adamc@338
|
1550 end
|
adamc@338
|
1551 | DSequence (tn, x, n) =>
|
adamc@338
|
1552 let
|
adamc@338
|
1553 val t = (CModProj (tn, [], "sql_sequence"), loc)
|
adamc@205
|
1554 in
|
adamc@205
|
1555 pushENamedAs env x n t
|
adamc@205
|
1556 end
|
adamc@754
|
1557 | DView (tn, x, n, _, c) =>
|
adamc@754
|
1558 let
|
adamc@754
|
1559 val ct = (CModProj (tn, [], "sql_view"), loc)
|
adamc@754
|
1560 val ct = (CApp (ct, c), loc)
|
adamc@754
|
1561 in
|
adamc@754
|
1562 pushENamedAs env x n ct
|
adamc@754
|
1563 end
|
adamc@563
|
1564 | DClass (x, n, k, c) =>
|
adamc@213
|
1565 let
|
adamc@563
|
1566 val k = (KArrow (k, (KType, loc)), loc)
|
adamc@213
|
1567 val env = pushCNamedAs env x n k (SOME c)
|
adamc@213
|
1568 in
|
adamc@213
|
1569 pushClass env n
|
adamc@213
|
1570 end
|
adamc@271
|
1571 | DDatabase _ => env
|
adamc@459
|
1572 | DCookie (tn, x, n, c) =>
|
adamc@459
|
1573 let
|
adamc@459
|
1574 val t = (CApp ((CModProj (tn, [], "cookie"), loc), c), loc)
|
adamc@459
|
1575 in
|
adamc@459
|
1576 pushENamedAs env x n t
|
adamc@459
|
1577 end
|
adamc@720
|
1578 | DStyle (tn, x, n) =>
|
adamc@718
|
1579 let
|
adamc@720
|
1580 val t = (CModProj (tn, [], "css_class"), loc)
|
adamc@718
|
1581 in
|
adamc@718
|
1582 pushENamedAs env x n t
|
adamc@718
|
1583 end
|
adamc@157
|
1584
|
adamc@2
|
1585 end
|