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