adamc@2
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@2
|
2 * All rights reserved.
|
adamc@2
|
3 *
|
adamc@2
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@2
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@2
|
6 *
|
adamc@2
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@2
|
8 * this list of conditions and the following disclaimer.
|
adamc@2
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@2
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@2
|
11 * and/or other materials provided with the distribution.
|
adamc@2
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@2
|
13 * derived from this software without specific prior written permission.
|
adamc@2
|
14 *
|
adamc@2
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@2
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@2
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@2
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@2
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@2
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@2
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@2
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@2
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@2
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@2
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@2
|
26 *)
|
adamc@2
|
27
|
adamc@2
|
28 structure 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@13
|
48 val liftConInCon =
|
adamc@13
|
49 U.Con.mapB {kind = fn k => k,
|
adamc@13
|
50 con = fn bound => fn c =>
|
adamc@13
|
51 case c of
|
adamc@16
|
52 CRel xn =>
|
adamc@13
|
53 if xn < bound then
|
adamc@13
|
54 c
|
adamc@13
|
55 else
|
adamc@16
|
56 CRel (xn + 1)
|
adamc@17
|
57 (*| CUnif _ => raise SynUnif*)
|
adamc@13
|
58 | _ => c,
|
adamc@13
|
59 bind = fn (bound, U.Con.Rel _) => bound + 1
|
adamc@13
|
60 | (bound, _) => bound}
|
adamc@13
|
61
|
adamc@13
|
62 val lift = liftConInCon 0
|
adamc@13
|
63
|
adamc@448
|
64 val liftConInExp =
|
adamc@448
|
65 U.Exp.mapB {kind = fn k => k,
|
adamc@448
|
66 con = fn bound => fn c =>
|
adamc@448
|
67 case c of
|
adamc@448
|
68 CRel xn =>
|
adamc@448
|
69 if xn < bound then
|
adamc@448
|
70 c
|
adamc@448
|
71 else
|
adamc@448
|
72 CRel (xn + 1)
|
adamc@448
|
73 | _ => c,
|
adamc@448
|
74 exp = fn _ => fn e => e,
|
adamc@448
|
75 bind = fn (bound, U.Exp.RelC _) => bound + 1
|
adamc@448
|
76 | (bound, _) => bound}
|
adamc@448
|
77
|
adamc@211
|
78 val liftExpInExp =
|
adamc@211
|
79 U.Exp.mapB {kind = fn k => k,
|
adamc@211
|
80 con = fn _ => fn c => c,
|
adamc@211
|
81 exp = fn bound => fn e =>
|
adamc@211
|
82 case e of
|
adamc@211
|
83 ERel xn =>
|
adamc@211
|
84 if xn < bound then
|
adamc@211
|
85 e
|
adamc@211
|
86 else
|
adamc@211
|
87 ERel (xn + 1)
|
adamc@211
|
88 | _ => e,
|
adamc@211
|
89 bind = fn (bound, U.Exp.RelE _) => bound + 1
|
adamc@211
|
90 | (bound, _) => bound}
|
adamc@211
|
91
|
adamc@211
|
92
|
adamc@211
|
93 val liftExp = liftExpInExp 0
|
adamc@13
|
94
|
adamc@448
|
95 val subExpInExp =
|
adamc@448
|
96 U.Exp.mapB {kind = fn k => k,
|
adamc@448
|
97 con = fn _ => fn c => c,
|
adamc@448
|
98 exp = fn (xn, rep) => fn e =>
|
adamc@448
|
99 case e of
|
adamc@448
|
100 ERel xn' =>
|
adamc@448
|
101 (case Int.compare (xn', xn) of
|
adamc@448
|
102 EQUAL => #1 rep
|
adamc@448
|
103 | GREATER=> ERel (xn' - 1)
|
adamc@448
|
104 | LESS => e)
|
adamc@448
|
105 | _ => e,
|
adamc@448
|
106 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
|
adamc@448
|
107 | ((xn, rep), U.Exp.RelC _) => (xn, liftConInExp 0 rep)
|
adamc@448
|
108 | (ctx, _) => ctx}
|
adamc@448
|
109
|
adamc@13
|
110 (* Back to environments *)
|
adamc@13
|
111
|
adamc@9
|
112 datatype 'a var' =
|
adamc@9
|
113 Rel' of int * 'a
|
adamc@9
|
114 | Named' of int * 'a
|
adamc@2
|
115
|
adamc@9
|
116 datatype 'a var =
|
adamc@9
|
117 NotBound
|
adamc@9
|
118 | Rel of int * 'a
|
adamc@9
|
119 | Named of int * 'a
|
adamc@2
|
120
|
adamc@191
|
121 type datatyp = string list * (string * con option) IM.map
|
adamc@157
|
122
|
adamc@211
|
123 datatype class_name =
|
adamc@211
|
124 ClNamed of int
|
adamc@211
|
125 | ClProj of int * string list * string
|
adamc@211
|
126
|
adamc@216
|
127 fun cn2s cn =
|
adamc@216
|
128 case cn of
|
adamc@216
|
129 ClNamed n => "Named(" ^ Int.toString n ^ ")"
|
adamc@216
|
130 | ClProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
|
adamc@216
|
131
|
adamc@211
|
132 structure CK = struct
|
adamc@211
|
133 type ord_key = class_name
|
adamc@211
|
134 open Order
|
adamc@211
|
135 fun compare x =
|
adamc@211
|
136 case x of
|
adamc@211
|
137 (ClNamed n1, ClNamed n2) => Int.compare (n1, n2)
|
adamc@211
|
138 | (ClNamed _, _) => LESS
|
adamc@211
|
139 | (_, ClNamed _) => GREATER
|
adamc@211
|
140
|
adamc@211
|
141 | (ClProj (m1, ms1, x1), ClProj (m2, ms2, x2)) =>
|
adamc@211
|
142 join (Int.compare (m1, m2),
|
adamc@211
|
143 fn () => join (joinL String.compare (ms1, ms2),
|
adamc@211
|
144 fn () => String.compare (x1, x2)))
|
adamc@211
|
145 end
|
adamc@211
|
146
|
adamc@211
|
147 structure CM = BinaryMapFn(CK)
|
adamc@211
|
148
|
adamc@211
|
149 datatype class_key =
|
adamc@211
|
150 CkNamed of int
|
adamc@211
|
151 | CkRel of int
|
adamc@211
|
152 | CkProj of int * string list * string
|
adamc@211
|
153
|
adamc@216
|
154 fun ck2s ck =
|
adamc@216
|
155 case ck of
|
adamc@216
|
156 CkNamed n => "Named(" ^ Int.toString n ^ ")"
|
adamc@216
|
157 | CkRel n => "Rel(" ^ Int.toString n ^ ")"
|
adamc@216
|
158 | CkProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
|
adamc@216
|
159
|
adamc@216
|
160 fun cp2s (cn, ck) = "(" ^ cn2s cn ^ "," ^ ck2s ck ^ ")"
|
adamc@216
|
161
|
adamc@211
|
162 structure KK = struct
|
adamc@211
|
163 type ord_key = class_key
|
adamc@211
|
164 open Order
|
adamc@211
|
165 fun compare x =
|
adamc@211
|
166 case x of
|
adamc@211
|
167 (CkNamed n1, CkNamed n2) => Int.compare (n1, n2)
|
adamc@211
|
168 | (CkNamed _, _) => LESS
|
adamc@211
|
169 | (_, CkNamed _) => GREATER
|
adamc@211
|
170
|
adamc@211
|
171 | (CkRel n1, CkRel n2) => Int.compare (n1, n2)
|
adamc@211
|
172 | (CkRel _, _) => LESS
|
adamc@211
|
173 | (_, CkRel _) => GREATER
|
adamc@211
|
174
|
adamc@211
|
175 | (CkProj (m1, ms1, x1), CkProj (m2, ms2, x2)) =>
|
adamc@211
|
176 join (Int.compare (m1, m2),
|
adamc@211
|
177 fn () => join (joinL String.compare (ms1, ms2),
|
adamc@211
|
178 fn () => String.compare (x1, x2)))
|
adamc@211
|
179 end
|
adamc@211
|
180
|
adamc@211
|
181 structure KM = BinaryMapFn(KK)
|
adamc@211
|
182
|
adamc@211
|
183 type class = {
|
adamc@211
|
184 ground : exp KM.map
|
adamc@211
|
185 }
|
adamc@211
|
186
|
adamc@211
|
187 val empty_class = {
|
adamc@211
|
188 ground = KM.empty
|
adamc@211
|
189 }
|
adamc@211
|
190
|
adamc@419
|
191 fun printClasses cs = (print "Classes:\n";
|
adamc@419
|
192 CM.appi (fn (cn, {ground = km}) =>
|
adamc@419
|
193 (print (cn2s cn ^ ":");
|
adamc@419
|
194 KM.appi (fn (ck, _) => print (" " ^ ck2s ck)) km;
|
adamc@419
|
195 print "\n")) cs)
|
adamc@216
|
196
|
adamc@2
|
197 type env = {
|
adamc@9
|
198 renameC : kind var' SM.map,
|
adamc@2
|
199 relC : (string * kind) list,
|
adamc@11
|
200 namedC : (string * kind * con option) IM.map,
|
adamc@9
|
201
|
adamc@157
|
202 datatypes : datatyp IM.map,
|
adamc@191
|
203 constructors : (datatype_kind * int * string list * con option * int) SM.map,
|
adamc@157
|
204
|
adamc@211
|
205 classes : class CM.map,
|
adamc@211
|
206
|
adamc@9
|
207 renameE : con var' SM.map,
|
adamc@9
|
208 relE : (string * con) list,
|
adamc@31
|
209 namedE : (string * con) IM.map,
|
adamc@31
|
210
|
adamc@31
|
211 renameSgn : (int * sgn) SM.map,
|
adamc@31
|
212 sgn : (string * sgn) IM.map,
|
adamc@31
|
213
|
adamc@31
|
214 renameStr : (int * sgn) SM.map,
|
adamc@31
|
215 str : (string * sgn) IM.map
|
adamc@2
|
216 }
|
adamc@2
|
217
|
adamc@2
|
218 val namedCounter = ref 0
|
adamc@2
|
219
|
adamc@109
|
220 fun newNamed () =
|
adamc@109
|
221 let
|
adamc@109
|
222 val r = !namedCounter
|
adamc@109
|
223 in
|
adamc@109
|
224 namedCounter := r + 1;
|
adamc@109
|
225 r
|
adamc@109
|
226 end
|
adamc@109
|
227
|
adamc@2
|
228 val empty = {
|
adamc@2
|
229 renameC = SM.empty,
|
adamc@2
|
230 relC = [],
|
adamc@9
|
231 namedC = IM.empty,
|
adamc@9
|
232
|
adamc@157
|
233 datatypes = IM.empty,
|
adamc@171
|
234 constructors = SM.empty,
|
adamc@157
|
235
|
adamc@211
|
236 classes = CM.empty,
|
adamc@211
|
237
|
adamc@9
|
238 renameE = SM.empty,
|
adamc@9
|
239 relE = [],
|
adamc@31
|
240 namedE = IM.empty,
|
adamc@31
|
241
|
adamc@31
|
242 renameSgn = SM.empty,
|
adamc@31
|
243 sgn = IM.empty,
|
adamc@31
|
244
|
adamc@31
|
245 renameStr = SM.empty,
|
adamc@31
|
246 str = IM.empty
|
adamc@2
|
247 }
|
adamc@2
|
248
|
adamc@211
|
249 fun liftClassKey ck =
|
adamc@211
|
250 case ck of
|
adamc@211
|
251 CkNamed _ => ck
|
adamc@211
|
252 | CkRel n => CkRel (n + 1)
|
adamc@211
|
253 | CkProj _ => ck
|
adamc@211
|
254
|
adamc@2
|
255 fun pushCRel (env : env) x k =
|
adamc@2
|
256 let
|
adamc@9
|
257 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
|
adamc@2
|
258 | x => x) (#renameC env)
|
adamc@2
|
259 in
|
adamc@9
|
260 {renameC = SM.insert (renameC, x, Rel' (0, k)),
|
adamc@2
|
261 relC = (x, k) :: #relC env,
|
adamc@13
|
262 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
|
adamc@9
|
263
|
adamc@157
|
264 datatypes = #datatypes env,
|
adamc@171
|
265 constructors = #constructors env,
|
adamc@157
|
266
|
adamc@211
|
267 classes = CM.map (fn class => {
|
adamc@211
|
268 ground = KM.foldli (fn (ck, e, km) =>
|
adamc@211
|
269 KM.insert (km, liftClassKey ck, e))
|
adamc@211
|
270 KM.empty (#ground class)
|
adamc@211
|
271 })
|
adamc@211
|
272 (#classes env),
|
adamc@211
|
273
|
adamc@328
|
274 renameE = SM.map (fn Rel' (n, c) => Rel' (n, lift c)
|
adamc@328
|
275 | Named' (n, c) => Named' (n, lift c)) (#renameE env),
|
adamc@13
|
276 relE = map (fn (x, c) => (x, lift c)) (#relE env),
|
adamc@31
|
277 namedE = IM.map (fn (x, c) => (x, lift c)) (#namedE env),
|
adamc@31
|
278
|
adamc@31
|
279 renameSgn = #renameSgn env,
|
adamc@31
|
280 sgn = #sgn env,
|
adamc@31
|
281
|
adamc@31
|
282 renameStr = #renameStr env,
|
adamc@31
|
283 str = #str env
|
adamc@31
|
284 }
|
adamc@2
|
285 end
|
adamc@2
|
286
|
adamc@2
|
287 fun lookupCRel (env : env) n =
|
adamc@2
|
288 (List.nth (#relC env, n))
|
adamc@2
|
289 handle Subscript => raise UnboundRel n
|
adamc@2
|
290
|
adamc@11
|
291 fun pushCNamedAs (env : env) x n k co =
|
adamc@9
|
292 {renameC = SM.insert (#renameC env, x, Named' (n, k)),
|
adamc@5
|
293 relC = #relC env,
|
adamc@11
|
294 namedC = IM.insert (#namedC env, n, (x, k, co)),
|
adamc@9
|
295
|
adamc@157
|
296 datatypes = #datatypes env,
|
adamc@171
|
297 constructors = #constructors env,
|
adamc@157
|
298
|
adamc@211
|
299 classes = #classes env,
|
adamc@211
|
300
|
adamc@9
|
301 renameE = #renameE env,
|
adamc@9
|
302 relE = #relE env,
|
adamc@31
|
303 namedE = #namedE env,
|
adamc@31
|
304
|
adamc@31
|
305 renameSgn = #renameSgn env,
|
adamc@31
|
306 sgn = #sgn env,
|
adamc@31
|
307
|
adamc@31
|
308 renameStr = #renameStr env,
|
adamc@31
|
309 str = #str env}
|
adamc@5
|
310
|
adamc@11
|
311 fun pushCNamed env x k co =
|
adamc@2
|
312 let
|
adamc@2
|
313 val n = !namedCounter
|
adamc@2
|
314 in
|
adamc@2
|
315 namedCounter := n + 1;
|
adamc@11
|
316 (pushCNamedAs env x n k co, n)
|
adamc@2
|
317 end
|
adamc@2
|
318
|
adamc@2
|
319 fun lookupCNamed (env : env) n =
|
adamc@2
|
320 case IM.find (#namedC env, n) of
|
adamc@2
|
321 NONE => raise UnboundNamed n
|
adamc@2
|
322 | SOME x => x
|
adamc@2
|
323
|
adamc@2
|
324 fun lookupC (env : env) x =
|
adamc@2
|
325 case SM.find (#renameC env, x) of
|
adamc@9
|
326 NONE => NotBound
|
adamc@9
|
327 | SOME (Rel' x) => Rel x
|
adamc@9
|
328 | SOME (Named' x) => Named x
|
adamc@9
|
329
|
adamc@191
|
330 fun pushDatatype (env : env) n xs xncs =
|
adamc@188
|
331 let
|
adamc@188
|
332 val dk = U.classifyDatatype xncs
|
adamc@188
|
333 in
|
adamc@188
|
334 {renameC = #renameC env,
|
adamc@188
|
335 relC = #relC env,
|
adamc@188
|
336 namedC = #namedC env,
|
adamc@157
|
337
|
adamc@188
|
338 datatypes = IM.insert (#datatypes env, n,
|
adamc@191
|
339 (xs, foldl (fn ((x, n, to), cons) =>
|
adamc@191
|
340 IM.insert (cons, n, (x, to))) IM.empty xncs)),
|
adamc@188
|
341 constructors = foldl (fn ((x, n', to), cmap) =>
|
adamc@191
|
342 SM.insert (cmap, x, (dk, n', xs, to, n)))
|
adamc@188
|
343 (#constructors env) xncs,
|
adamc@157
|
344
|
adamc@211
|
345 classes = #classes env,
|
adamc@211
|
346
|
adamc@188
|
347 renameE = #renameE env,
|
adamc@188
|
348 relE = #relE env,
|
adamc@188
|
349 namedE = #namedE env,
|
adamc@157
|
350
|
adamc@188
|
351 renameSgn = #renameSgn env,
|
adamc@188
|
352 sgn = #sgn env,
|
adamc@157
|
353
|
adamc@188
|
354 renameStr = #renameStr env,
|
adamc@188
|
355 str = #str env}
|
adamc@188
|
356 end
|
adamc@157
|
357
|
adamc@157
|
358 fun lookupDatatype (env : env) n =
|
adamc@157
|
359 case IM.find (#datatypes env, n) of
|
adamc@157
|
360 NONE => raise UnboundNamed n
|
adamc@157
|
361 | SOME x => x
|
adamc@157
|
362
|
adamc@191
|
363 fun lookupDatatypeConstructor (_, dt) n =
|
adamc@157
|
364 case IM.find (dt, n) of
|
adamc@157
|
365 NONE => raise UnboundNamed n
|
adamc@157
|
366 | SOME x => x
|
adamc@157
|
367
|
adamc@171
|
368 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
|
adamc@171
|
369
|
adamc@191
|
370 fun datatypeArgs (xs, _) = xs
|
adamc@191
|
371 fun constructors (_, dt) = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
|
adamc@157
|
372
|
adamc@211
|
373 fun pushClass (env : env) n =
|
adamc@211
|
374 {renameC = #renameC env,
|
adamc@211
|
375 relC = #relC env,
|
adamc@211
|
376 namedC = #namedC env,
|
adamc@211
|
377
|
adamc@211
|
378 datatypes = #datatypes env,
|
adamc@211
|
379 constructors = #constructors env,
|
adamc@211
|
380
|
adamc@211
|
381 classes = CM.insert (#classes env, ClNamed n, {ground = KM.empty}),
|
adamc@211
|
382
|
adamc@211
|
383 renameE = #renameE env,
|
adamc@211
|
384 relE = #relE env,
|
adamc@211
|
385 namedE = #namedE env,
|
adamc@211
|
386
|
adamc@211
|
387 renameSgn = #renameSgn env,
|
adamc@211
|
388 sgn = #sgn env,
|
adamc@211
|
389
|
adamc@211
|
390 renameStr = #renameStr env,
|
adamc@403
|
391 str = #str env}
|
adamc@211
|
392
|
adamc@211
|
393 fun class_name_in (c, _) =
|
adamc@211
|
394 case c of
|
adamc@211
|
395 CNamed n => SOME (ClNamed n)
|
adamc@211
|
396 | CModProj x => SOME (ClProj x)
|
adamc@228
|
397 | CUnif (_, _, _, ref (SOME c)) => class_name_in c
|
adamc@211
|
398 | _ => NONE
|
adamc@211
|
399
|
adamc@403
|
400 fun isClass (env : env) c =
|
adamc@403
|
401 let
|
adamc@403
|
402 fun find NONE = false
|
adamc@403
|
403 | find (SOME c) = Option.isSome (CM.find (#classes env, c))
|
adamc@403
|
404 in
|
adamc@403
|
405 find (class_name_in c)
|
adamc@403
|
406 end
|
adamc@403
|
407
|
adamc@211
|
408 fun class_key_in (c, _) =
|
adamc@211
|
409 case c of
|
adamc@211
|
410 CRel n => SOME (CkRel n)
|
adamc@211
|
411 | CNamed n => SOME (CkNamed n)
|
adamc@211
|
412 | CModProj x => SOME (CkProj x)
|
adamc@228
|
413 | CUnif (_, _, _, ref (SOME c)) => class_key_in c
|
adamc@211
|
414 | _ => NONE
|
adamc@211
|
415
|
adamc@211
|
416 fun class_pair_in (c, _) =
|
adamc@211
|
417 case c of
|
adamc@211
|
418 CApp (f, x) =>
|
adamc@211
|
419 (case (class_name_in f, class_key_in x) of
|
adamc@211
|
420 (SOME f, SOME x) => SOME (f, x)
|
adamc@211
|
421 | _ => NONE)
|
adamc@452
|
422 | CUnif (_, _, _, ref (SOME c)) => class_pair_in c
|
adamc@211
|
423 | _ => NONE
|
adamc@211
|
424
|
adamc@211
|
425 fun resolveClass (env : env) c =
|
adamc@211
|
426 case class_pair_in c of
|
adamc@211
|
427 SOME (f, x) =>
|
adamc@211
|
428 (case CM.find (#classes env, f) of
|
adamc@211
|
429 NONE => NONE
|
adamc@211
|
430 | SOME class =>
|
adamc@211
|
431 case KM.find (#ground class, x) of
|
adamc@211
|
432 NONE => NONE
|
adamc@211
|
433 | SOME e => SOME e)
|
adamc@211
|
434 | _ => NONE
|
adamc@211
|
435
|
adamc@9
|
436 fun pushERel (env : env) x t =
|
adamc@9
|
437 let
|
adamc@9
|
438 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
|
adamc@9
|
439 | x => x) (#renameE env)
|
adamc@211
|
440
|
adamc@211
|
441 val classes = CM.map (fn class => {
|
adamc@211
|
442 ground = KM.map liftExp (#ground class)
|
adamc@211
|
443 }) (#classes env)
|
adamc@211
|
444 val classes = case class_pair_in t of
|
adamc@211
|
445 NONE => classes
|
adamc@211
|
446 | SOME (f, x) =>
|
adamc@403
|
447 case CM.find (classes, f) of
|
adamc@403
|
448 NONE => classes
|
adamc@403
|
449 | SOME class =>
|
adamc@403
|
450 let
|
adamc@403
|
451 val class = {
|
adamc@403
|
452 ground = KM.insert (#ground class, x, (ERel 0, #2 t))
|
adamc@403
|
453 }
|
adamc@403
|
454 in
|
adamc@403
|
455 CM.insert (classes, f, class)
|
adamc@403
|
456 end
|
adamc@9
|
457 in
|
adamc@9
|
458 {renameC = #renameC env,
|
adamc@9
|
459 relC = #relC env,
|
adamc@9
|
460 namedC = #namedC env,
|
adamc@9
|
461
|
adamc@157
|
462 datatypes = #datatypes env,
|
adamc@171
|
463 constructors = #constructors env,
|
adamc@157
|
464
|
adamc@211
|
465 classes = classes,
|
adamc@211
|
466
|
adamc@9
|
467 renameE = SM.insert (renameE, x, Rel' (0, t)),
|
adamc@9
|
468 relE = (x, t) :: #relE env,
|
adamc@31
|
469 namedE = #namedE env,
|
adamc@31
|
470
|
adamc@31
|
471 renameSgn = #renameSgn env,
|
adamc@31
|
472 sgn = #sgn env,
|
adamc@31
|
473
|
adamc@31
|
474 renameStr = #renameStr env,
|
adamc@31
|
475 str = #str env}
|
adamc@9
|
476 end
|
adamc@9
|
477
|
adamc@9
|
478 fun lookupERel (env : env) n =
|
adamc@9
|
479 (List.nth (#relE env, n))
|
adamc@9
|
480 handle Subscript => raise UnboundRel n
|
adamc@9
|
481
|
adamc@9
|
482 fun pushENamedAs (env : env) x n t =
|
adamc@211
|
483 let
|
adamc@211
|
484 val classes = #classes env
|
adamc@211
|
485 val classes = case class_pair_in t of
|
adamc@211
|
486 NONE => classes
|
adamc@211
|
487 | SOME (f, x) =>
|
adamc@403
|
488 case CM.find (classes, f) of
|
adamc@403
|
489 NONE => classes
|
adamc@403
|
490 | SOME class =>
|
adamc@403
|
491 let
|
adamc@403
|
492 val class = {
|
adamc@403
|
493 ground = KM.insert (#ground class, x, (ENamed n, #2 t))
|
adamc@403
|
494 }
|
adamc@403
|
495 in
|
adamc@403
|
496 CM.insert (classes, f, class)
|
adamc@403
|
497 end
|
adamc@211
|
498 in
|
adamc@211
|
499 {renameC = #renameC env,
|
adamc@211
|
500 relC = #relC env,
|
adamc@211
|
501 namedC = #namedC env,
|
adamc@9
|
502
|
adamc@211
|
503 datatypes = #datatypes env,
|
adamc@211
|
504 constructors = #constructors env,
|
adamc@157
|
505
|
adamc@211
|
506 classes = classes,
|
adamc@31
|
507
|
adamc@211
|
508 renameE = SM.insert (#renameE env, x, Named' (n, t)),
|
adamc@211
|
509 relE = #relE env,
|
adamc@211
|
510 namedE = IM.insert (#namedE env, n, (x, t)),
|
adamc@211
|
511
|
adamc@211
|
512 renameSgn = #renameSgn env,
|
adamc@211
|
513 sgn = #sgn env,
|
adamc@211
|
514
|
adamc@211
|
515 renameStr = #renameStr env,
|
adamc@211
|
516 str = #str env}
|
adamc@211
|
517 end
|
adamc@9
|
518
|
adamc@9
|
519 fun pushENamed env x t =
|
adamc@9
|
520 let
|
adamc@9
|
521 val n = !namedCounter
|
adamc@9
|
522 in
|
adamc@9
|
523 namedCounter := n + 1;
|
adamc@9
|
524 (pushENamedAs env x n t, n)
|
adamc@9
|
525 end
|
adamc@9
|
526
|
adamc@9
|
527 fun lookupENamed (env : env) n =
|
adamc@9
|
528 case IM.find (#namedE env, n) of
|
adamc@9
|
529 NONE => raise UnboundNamed n
|
adamc@9
|
530 | SOME x => x
|
adamc@9
|
531
|
adamc@9
|
532 fun lookupE (env : env) x =
|
adamc@9
|
533 case SM.find (#renameE env, x) of
|
adamc@9
|
534 NONE => NotBound
|
adamc@9
|
535 | SOME (Rel' x) => Rel x
|
adamc@9
|
536 | SOME (Named' x) => Named x
|
adamc@2
|
537
|
adamc@31
|
538 fun pushSgnNamedAs (env : env) x n sgis =
|
adamc@31
|
539 {renameC = #renameC env,
|
adamc@31
|
540 relC = #relC env,
|
adamc@31
|
541 namedC = #namedC env,
|
adamc@31
|
542
|
adamc@157
|
543 datatypes = #datatypes env,
|
adamc@171
|
544 constructors = #constructors env,
|
adamc@157
|
545
|
adamc@211
|
546 classes = #classes env,
|
adamc@211
|
547
|
adamc@31
|
548 renameE = #renameE env,
|
adamc@31
|
549 relE = #relE env,
|
adamc@31
|
550 namedE = #namedE env,
|
adamc@31
|
551
|
adamc@31
|
552 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
|
adamc@31
|
553 sgn = IM.insert (#sgn env, n, (x, sgis)),
|
adamc@31
|
554
|
adamc@31
|
555 renameStr = #renameStr env,
|
adamc@31
|
556 str = #str env}
|
adamc@31
|
557
|
adamc@31
|
558 fun pushSgnNamed env x sgis =
|
adamc@31
|
559 let
|
adamc@31
|
560 val n = !namedCounter
|
adamc@31
|
561 in
|
adamc@31
|
562 namedCounter := n + 1;
|
adamc@31
|
563 (pushSgnNamedAs env x n sgis, n)
|
adamc@31
|
564 end
|
adamc@31
|
565
|
adamc@31
|
566 fun lookupSgnNamed (env : env) n =
|
adamc@31
|
567 case IM.find (#sgn env, n) of
|
adamc@31
|
568 NONE => raise UnboundNamed n
|
adamc@31
|
569 | SOME x => x
|
adamc@31
|
570
|
adamc@31
|
571 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
|
adamc@31
|
572
|
adamc@31
|
573 fun lookupStrNamed (env : env) n =
|
adamc@31
|
574 case IM.find (#str env, n) of
|
adamc@31
|
575 NONE => raise UnboundNamed n
|
adamc@31
|
576 | SOME x => x
|
adamc@31
|
577
|
adamc@31
|
578 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
|
adamc@31
|
579
|
adamc@203
|
580
|
adamc@216
|
581 fun sgiSeek (sgi, (sgns, strs, cons)) =
|
adamc@216
|
582 case sgi of
|
adamc@216
|
583 SgiConAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
584 | SgiCon (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
585 | SgiDatatype (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
586 | SgiDatatypeImp (x, n, _, _, _, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
587 | SgiVal _ => (sgns, strs, cons)
|
adamc@216
|
588 | SgiSgn (x, n, _) => (IM.insert (sgns, n, x), strs, cons)
|
adamc@216
|
589 | SgiStr (x, n, _) => (sgns, IM.insert (strs, n, x), cons)
|
adamc@216
|
590 | SgiConstraint _ => (sgns, strs, cons)
|
adamc@216
|
591 | SgiTable _ => (sgns, strs, cons)
|
adamc@338
|
592 | SgiSequence _ => (sgns, strs, cons)
|
adamc@216
|
593 | SgiClassAbs (x, n) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
594 | SgiClass (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
595
|
adamc@34
|
596 fun sgnSeek f sgis =
|
adamc@34
|
597 let
|
adamc@59
|
598 fun seek (sgis, sgns, strs, cons) =
|
adamc@34
|
599 case sgis of
|
adamc@34
|
600 [] => NONE
|
adamc@34
|
601 | (sgi, _) :: sgis =>
|
adamc@34
|
602 case f sgi of
|
adamc@158
|
603 SOME v =>
|
adamc@158
|
604 let
|
adamc@158
|
605 val cons =
|
adamc@158
|
606 case sgi of
|
adamc@191
|
607 SgiDatatype (x, n, _, _) => IM.insert (cons, n, x)
|
adamc@191
|
608 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
|
adamc@158
|
609 | _ => cons
|
adamc@158
|
610 in
|
adamc@158
|
611 SOME (v, (sgns, strs, cons))
|
adamc@158
|
612 end
|
adamc@88
|
613 | NONE =>
|
adamc@216
|
614 let
|
adamc@216
|
615 val (sgns, strs, cons) = sgiSeek (sgi, (sgns, strs, cons))
|
adamc@216
|
616 in
|
adamc@216
|
617 seek (sgis, sgns, strs, cons)
|
adamc@216
|
618 end
|
adamc@34
|
619 in
|
adamc@59
|
620 seek (sgis, IM.empty, IM.empty, IM.empty)
|
adamc@34
|
621 end
|
adamc@34
|
622
|
adamc@34
|
623 fun id x = x
|
adamc@34
|
624
|
adamc@34
|
625 fun unravelStr (str, _) =
|
adamc@34
|
626 case str of
|
adamc@34
|
627 StrVar x => (x, [])
|
adamc@34
|
628 | StrProj (str, m) =>
|
adamc@34
|
629 let
|
adamc@34
|
630 val (x, ms) = unravelStr str
|
adamc@34
|
631 in
|
adamc@34
|
632 (x, ms @ [m])
|
adamc@34
|
633 end
|
adamc@34
|
634 | _ => raise Fail "unravelStr"
|
adamc@34
|
635
|
adamc@59
|
636 fun sgnS_con (str, (sgns, strs, cons)) c =
|
adamc@34
|
637 case c of
|
adamc@34
|
638 CModProj (m1, ms, x) =>
|
adamc@34
|
639 (case IM.find (strs, m1) of
|
adamc@34
|
640 NONE => c
|
adamc@34
|
641 | SOME m1x =>
|
adamc@34
|
642 let
|
adamc@34
|
643 val (m1, ms') = unravelStr str
|
adamc@34
|
644 in
|
adamc@34
|
645 CModProj (m1, ms' @ m1x :: ms, x)
|
adamc@34
|
646 end)
|
adamc@34
|
647 | CNamed n =>
|
adamc@34
|
648 (case IM.find (cons, n) of
|
adamc@34
|
649 NONE => c
|
adamc@34
|
650 | SOME nx =>
|
adamc@34
|
651 let
|
adamc@34
|
652 val (m1, ms) = unravelStr str
|
adamc@34
|
653 in
|
adamc@34
|
654 CModProj (m1, ms, nx)
|
adamc@34
|
655 end)
|
adamc@34
|
656 | _ => c
|
adamc@34
|
657
|
adamc@216
|
658 fun sgnS_con' (m1, ms', (sgns, strs, cons)) c =
|
adamc@216
|
659 case c of
|
adamc@216
|
660 CModProj (m1, ms, x) =>
|
adamc@216
|
661 (case IM.find (strs, m1) of
|
adamc@216
|
662 NONE => c
|
adamc@216
|
663 | SOME m1x => CModProj (m1, ms' @ m1x :: ms, x))
|
adamc@216
|
664 | CNamed n =>
|
adamc@216
|
665 (case IM.find (cons, n) of
|
adamc@216
|
666 NONE => c
|
adamc@216
|
667 | SOME nx => CModProj (m1, ms', nx))
|
adamc@216
|
668 | _ => c
|
adamc@216
|
669
|
adamc@59
|
670 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
|
adamc@59
|
671 case sgn of
|
adamc@59
|
672 SgnProj (m1, ms, x) =>
|
adamc@59
|
673 (case IM.find (strs, m1) of
|
adamc@59
|
674 NONE => sgn
|
adamc@59
|
675 | SOME m1x =>
|
adamc@59
|
676 let
|
adamc@59
|
677 val (m1, ms') = unravelStr str
|
adamc@59
|
678 in
|
adamc@59
|
679 SgnProj (m1, ms' @ m1x :: ms, x)
|
adamc@59
|
680 end)
|
adamc@59
|
681 | SgnVar n =>
|
adamc@59
|
682 (case IM.find (sgns, n) of
|
adamc@59
|
683 NONE => sgn
|
adamc@59
|
684 | SOME nx =>
|
adamc@59
|
685 let
|
adamc@59
|
686 val (m1, ms) = unravelStr str
|
adamc@59
|
687 in
|
adamc@59
|
688 SgnProj (m1, ms, nx)
|
adamc@59
|
689 end)
|
adamc@59
|
690 | _ => sgn
|
adamc@59
|
691
|
adamc@59
|
692 fun sgnSubSgn x =
|
adamc@34
|
693 ElabUtil.Sgn.map {kind = id,
|
adamc@59
|
694 con = sgnS_con x,
|
adamc@34
|
695 sgn_item = id,
|
adamc@59
|
696 sgn = sgnS_sgn x}
|
adamc@34
|
697
|
adamc@211
|
698
|
adamc@211
|
699
|
adamc@211
|
700 and projectSgn env {sgn, str, field} =
|
adamc@211
|
701 case #1 (hnormSgn env sgn) of
|
adamc@211
|
702 SgnConst sgis =>
|
adamc@211
|
703 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@211
|
704 NONE => NONE
|
adamc@211
|
705 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@211
|
706 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@211
|
707 | _ => NONE
|
adamc@211
|
708
|
adamc@211
|
709 and hnormSgn env (all as (sgn, loc)) =
|
adamc@34
|
710 case sgn of
|
adamc@42
|
711 SgnError => all
|
adamc@42
|
712 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
|
adamc@42
|
713 | SgnConst _ => all
|
adamc@42
|
714 | SgnFun _ => all
|
adamc@59
|
715 | SgnProj (m, ms, x) =>
|
adamc@59
|
716 let
|
adamc@59
|
717 val (_, sgn) = lookupStrNamed env m
|
adamc@59
|
718 in
|
adamc@59
|
719 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
|
adamc@59
|
720 sgn = sgn,
|
adamc@59
|
721 field = x} of
|
adamc@59
|
722 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
|
adamc@59
|
723 | SOME sgn => sgn
|
adamc@59
|
724 end
|
adamc@42
|
725 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
726 case #1 (hnormSgn env sgn) of
|
adamc@42
|
727 SgnError => (SgnError, loc)
|
adamc@42
|
728 | SgnConst sgis =>
|
adamc@42
|
729 let
|
adamc@42
|
730 fun traverse (pre, post) =
|
adamc@42
|
731 case post of
|
adamc@42
|
732 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
|
adamc@42
|
733 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
|
adamc@42
|
734 if x = x' then
|
adamc@42
|
735 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
|
adamc@42
|
736 else
|
adamc@42
|
737 traverse (sgi :: pre, rest)
|
adamc@42
|
738 | sgi :: rest => traverse (sgi :: pre, rest)
|
adamc@42
|
739
|
adamc@42
|
740 val sgis = traverse ([], sgis)
|
adamc@42
|
741 in
|
adamc@42
|
742 (SgnConst sgis, loc)
|
adamc@42
|
743 end
|
adamc@42
|
744 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
|
adamc@42
|
745
|
adamc@211
|
746 fun enrichClasses env classes (m1, ms) sgn =
|
adamc@59
|
747 case #1 (hnormSgn env sgn) of
|
adamc@59
|
748 SgnConst sgis =>
|
adamc@211
|
749 let
|
adamc@218
|
750 val (classes, _, _, _) =
|
adamc@218
|
751 foldl (fn (sgi, (classes, newClasses, fmap, env)) =>
|
adamc@211
|
752 let
|
adamc@211
|
753 fun found (x, n) =
|
adamc@211
|
754 (CM.insert (classes,
|
adamc@211
|
755 ClProj (m1, ms, x),
|
adamc@211
|
756 empty_class),
|
adamc@216
|
757 IM.insert (newClasses, n, x),
|
adamc@218
|
758 sgiSeek (#1 sgi, fmap),
|
adamc@218
|
759 env)
|
adamc@216
|
760
|
adamc@218
|
761 fun default () = (classes, newClasses, sgiSeek (#1 sgi, fmap), env)
|
adamc@211
|
762 in
|
adamc@211
|
763 case #1 sgi of
|
adamc@217
|
764 SgiStr (x, _, sgn) =>
|
adamc@217
|
765 (enrichClasses env classes (m1, ms @ [x]) sgn,
|
adamc@217
|
766 newClasses,
|
adamc@218
|
767 sgiSeek (#1 sgi, fmap),
|
adamc@218
|
768 env)
|
adamc@218
|
769 | SgiSgn (x, n, sgn) =>
|
adamc@218
|
770 (classes,
|
adamc@218
|
771 newClasses,
|
adamc@218
|
772 fmap,
|
adamc@218
|
773 pushSgnNamedAs env x n sgn)
|
adamc@218
|
774
|
adamc@217
|
775 | SgiClassAbs xn => found xn
|
adamc@211
|
776 | SgiClass (x, n, _) => found (x, n)
|
adamc@419
|
777 | SgiVal (x, n, (CApp (f, a), _)) =>
|
adamc@419
|
778 let
|
adamc@419
|
779 fun unravel c =
|
adamc@419
|
780 case #1 c of
|
adamc@419
|
781 CUnif (_, _, _, ref (SOME c)) => unravel c
|
adamc@419
|
782 | CNamed n =>
|
adamc@419
|
783 ((case lookupCNamed env n of
|
adamc@419
|
784 (_, _, SOME c) => unravel c
|
adamc@419
|
785 | _ => c)
|
adamc@419
|
786 handle UnboundNamed _ => c)
|
adamc@419
|
787 | _ => c
|
adamc@216
|
788
|
adamc@419
|
789 val nc =
|
adamc@419
|
790 case f of
|
adamc@419
|
791 (CNamed f, _) => IM.find (newClasses, f)
|
adamc@419
|
792 | _ => NONE
|
adamc@419
|
793 in
|
adamc@419
|
794 case nc of
|
adamc@419
|
795 NONE =>
|
adamc@419
|
796 (case (class_name_in (unravel f),
|
adamc@419
|
797 class_key_in (sgnS_con' (m1, ms, fmap) (#1 a), #2 a)) of
|
adamc@419
|
798 (SOME cn, SOME ck) =>
|
adamc@419
|
799 let
|
adamc@419
|
800 val classes =
|
adamc@419
|
801 case CM.find (classes, cn) of
|
adamc@419
|
802 NONE => classes
|
adamc@419
|
803 | SOME class =>
|
adamc@419
|
804 let
|
adamc@419
|
805 val class = {
|
adamc@419
|
806 ground = KM.insert (#ground class, ck,
|
adamc@419
|
807 (EModProj (m1, ms, x), #2 sgn))
|
adamc@419
|
808 }
|
adamc@419
|
809 in
|
adamc@419
|
810 CM.insert (classes, cn, class)
|
adamc@419
|
811 end
|
adamc@419
|
812 in
|
adamc@419
|
813 (classes,
|
adamc@419
|
814 newClasses,
|
adamc@419
|
815 fmap,
|
adamc@419
|
816 env)
|
adamc@419
|
817 end
|
adamc@419
|
818 | _ => default ())
|
adamc@419
|
819 | SOME fx =>
|
adamc@419
|
820 case class_key_in (sgnS_con' (m1, ms, fmap) (#1 a), #2 a) of
|
adamc@419
|
821 NONE => default ()
|
adamc@419
|
822 | SOME ck =>
|
adamc@419
|
823 let
|
adamc@419
|
824 val cn = ClProj (m1, ms, fx)
|
adamc@419
|
825
|
adamc@419
|
826 val classes =
|
adamc@419
|
827 case CM.find (classes, cn) of
|
adamc@419
|
828 NONE => classes
|
adamc@419
|
829 | SOME class =>
|
adamc@419
|
830 let
|
adamc@419
|
831 val class = {
|
adamc@419
|
832 ground = KM.insert (#ground class, ck,
|
adamc@419
|
833 (EModProj (m1, ms, x), #2 sgn))
|
adamc@419
|
834 }
|
adamc@419
|
835 in
|
adamc@419
|
836 CM.insert (classes, cn, class)
|
adamc@419
|
837 end
|
adamc@419
|
838 in
|
adamc@419
|
839 (classes,
|
adamc@419
|
840 newClasses,
|
adamc@419
|
841 fmap,
|
adamc@419
|
842 env)
|
adamc@419
|
843 end
|
adamc@419
|
844 end
|
adamc@217
|
845 | SgiVal _ => default ()
|
adamc@216
|
846 | _ => default ()
|
adamc@211
|
847 end)
|
adamc@218
|
848 (classes, IM.empty, (IM.empty, IM.empty, IM.empty), env) sgis
|
adamc@211
|
849 in
|
adamc@211
|
850 classes
|
adamc@211
|
851 end
|
adamc@211
|
852 | _ => classes
|
adamc@211
|
853
|
adamc@211
|
854 fun pushStrNamedAs (env : env) x n sgn =
|
adamc@211
|
855 {renameC = #renameC env,
|
adamc@211
|
856 relC = #relC env,
|
adamc@211
|
857 namedC = #namedC env,
|
adamc@211
|
858
|
adamc@211
|
859 datatypes = #datatypes env,
|
adamc@211
|
860 constructors = #constructors env,
|
adamc@211
|
861
|
adamc@211
|
862 classes = enrichClasses env (#classes env) (n, []) sgn,
|
adamc@211
|
863
|
adamc@211
|
864 renameE = #renameE env,
|
adamc@211
|
865 relE = #relE env,
|
adamc@211
|
866 namedE = #namedE env,
|
adamc@211
|
867
|
adamc@211
|
868 renameSgn = #renameSgn env,
|
adamc@211
|
869 sgn = #sgn env,
|
adamc@211
|
870
|
adamc@211
|
871 renameStr = SM.insert (#renameStr env, x, (n, sgn)),
|
adamc@211
|
872 str = IM.insert (#str env, n, (x, sgn))}
|
adamc@211
|
873
|
adamc@211
|
874 fun pushStrNamed env x sgn =
|
adamc@211
|
875 let
|
adamc@211
|
876 val n = !namedCounter
|
adamc@211
|
877 in
|
adamc@211
|
878 namedCounter := n + 1;
|
adamc@211
|
879 (pushStrNamedAs env x n sgn, n)
|
adamc@211
|
880 end
|
adamc@211
|
881
|
adamc@211
|
882 fun sgiBinds env (sgi, loc) =
|
adamc@211
|
883 case sgi of
|
adamc@211
|
884 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
|
adamc@211
|
885 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@211
|
886 | SgiDatatype (x, n, xs, xncs) =>
|
adamc@211
|
887 let
|
adamc@341
|
888 val k = (KType, loc)
|
adamc@341
|
889 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
|
adamc@341
|
890
|
adamc@341
|
891 val env = pushCNamedAs env x n k' NONE
|
adamc@211
|
892 in
|
adamc@211
|
893 foldl (fn ((x', n', to), env) =>
|
adamc@211
|
894 let
|
adamc@211
|
895 val t =
|
adamc@211
|
896 case to of
|
adamc@211
|
897 NONE => (CNamed n, loc)
|
adamc@211
|
898 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@211
|
899
|
adamc@211
|
900 val k = (KType, loc)
|
adamc@211
|
901 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@211
|
902 in
|
adamc@211
|
903 pushENamedAs env x' n' t
|
adamc@211
|
904 end)
|
adamc@211
|
905 env xncs
|
adamc@211
|
906 end
|
adamc@211
|
907 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
|
adamc@211
|
908 let
|
adamc@341
|
909 val k = (KType, loc)
|
adamc@341
|
910 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
|
adamc@341
|
911
|
adamc@341
|
912 val env = pushCNamedAs env x n k' (SOME (CModProj (m1, ms, x'), loc))
|
adamc@211
|
913 in
|
adamc@211
|
914 foldl (fn ((x', n', to), env) =>
|
adamc@211
|
915 let
|
adamc@211
|
916 val t =
|
adamc@211
|
917 case to of
|
adamc@211
|
918 NONE => (CNamed n, loc)
|
adamc@211
|
919 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@211
|
920
|
adamc@211
|
921 val k = (KType, loc)
|
adamc@211
|
922 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@211
|
923 in
|
adamc@211
|
924 pushENamedAs env x' n' t
|
adamc@211
|
925 end)
|
adamc@211
|
926 env xncs
|
adamc@211
|
927 end
|
adamc@211
|
928 | SgiVal (x, n, t) => pushENamedAs env x n t
|
adamc@211
|
929 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@211
|
930 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@211
|
931 | SgiConstraint _ => env
|
adamc@211
|
932
|
adamc@211
|
933 | SgiTable (tn, x, n, c) =>
|
adamc@211
|
934 let
|
adamc@338
|
935 val t = (CApp ((CModProj (tn, [], "sql_table"), loc), c), loc)
|
adamc@338
|
936 in
|
adamc@338
|
937 pushENamedAs env x n t
|
adamc@338
|
938 end
|
adamc@338
|
939 | SgiSequence (tn, x, n) =>
|
adamc@338
|
940 let
|
adamc@338
|
941 val t = (CModProj (tn, [], "sql_sequence"), loc)
|
adamc@211
|
942 in
|
adamc@211
|
943 pushENamedAs env x n t
|
adamc@211
|
944 end
|
adamc@211
|
945
|
adamc@211
|
946 | SgiClassAbs (x, n) => pushCNamedAs env x n (KArrow ((KType, loc), (KType, loc)), loc) NONE
|
adamc@211
|
947 | SgiClass (x, n, c) => pushCNamedAs env x n (KArrow ((KType, loc), (KType, loc)), loc) (SOME c)
|
adamc@211
|
948
|
adamc@211
|
949
|
adamc@211
|
950 fun sgnSubCon x =
|
adamc@211
|
951 ElabUtil.Con.map {kind = id,
|
adamc@211
|
952 con = sgnS_con x}
|
adamc@59
|
953
|
adamc@59
|
954 fun projectStr env {sgn, str, field} =
|
adamc@59
|
955 case #1 (hnormSgn env sgn) of
|
adamc@59
|
956 SgnConst sgis =>
|
adamc@59
|
957 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
958 NONE => NONE
|
adamc@59
|
959 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
960 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
961 | _ => NONE
|
adamc@59
|
962
|
adamc@158
|
963 fun chaseMpath env (n, ms) =
|
adamc@158
|
964 let
|
adamc@158
|
965 val (_, sgn) = lookupStrNamed env n
|
adamc@158
|
966 in
|
adamc@158
|
967 foldl (fn (m, (str, sgn)) =>
|
adamc@158
|
968 case projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@158
|
969 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@158
|
970 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
|
adamc@158
|
971 ((StrVar n, #2 sgn), sgn) ms
|
adamc@158
|
972 end
|
adamc@158
|
973
|
adamc@42
|
974 fun projectCon env {sgn, str, field} =
|
adamc@42
|
975 case #1 (hnormSgn env sgn) of
|
adamc@34
|
976 SgnConst sgis =>
|
adamc@34
|
977 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
|
adamc@34
|
978 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
|
adamc@341
|
979 | SgiDatatype (x, _, xs, _) =>
|
adamc@158
|
980 if x = field then
|
adamc@341
|
981 let
|
adamc@341
|
982 val k = (KType, #2 sgn)
|
adamc@341
|
983 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
|
adamc@341
|
984 in
|
adamc@341
|
985 SOME (k', NONE)
|
adamc@341
|
986 end
|
adamc@341
|
987 else
|
adamc@341
|
988 NONE
|
adamc@341
|
989 | SgiDatatypeImp (x, _, m1, ms, x', xs, _) =>
|
adamc@341
|
990 if x = field then
|
adamc@341
|
991 let
|
adamc@341
|
992 val k = (KType, #2 sgn)
|
adamc@341
|
993 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
|
adamc@341
|
994 in
|
adamc@341
|
995 SOME (k', SOME (CModProj (m1, ms, x'), #2 sgn))
|
adamc@341
|
996 end
|
adamc@158
|
997 else
|
adamc@158
|
998 NONE
|
adamc@216
|
999 | SgiClassAbs (x, _) => if x = field then
|
adamc@216
|
1000 SOME ((KArrow ((KType, #2 sgn), (KType, #2 sgn)), #2 sgn), NONE)
|
adamc@216
|
1001 else
|
adamc@216
|
1002 NONE
|
adamc@216
|
1003 | SgiClass (x, _, c) => if x = field then
|
adamc@216
|
1004 SOME ((KArrow ((KType, #2 sgn), (KType, #2 sgn)), #2 sgn), SOME c)
|
adamc@216
|
1005 else
|
adamc@216
|
1006 NONE
|
adamc@34
|
1007 | _ => NONE) sgis of
|
adamc@34
|
1008 NONE => NONE
|
adamc@34
|
1009 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
|
adamc@34
|
1010 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
|
adamc@42
|
1011 | _ => NONE
|
adamc@34
|
1012
|
adamc@157
|
1013 fun projectDatatype env {sgn, str, field} =
|
adamc@157
|
1014 case #1 (hnormSgn env sgn) of
|
adamc@157
|
1015 SgnConst sgis =>
|
adamc@191
|
1016 (case sgnSeek (fn SgiDatatype (x, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
|
adamc@191
|
1017 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
|
adamc@157
|
1018 | _ => NONE) sgis of
|
adamc@157
|
1019 NONE => NONE
|
adamc@191
|
1020 | SOME ((xs, xncs), subs) => SOME (xs,
|
adamc@191
|
1021 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
|
adamc@157
|
1022 | _ => NONE
|
adamc@157
|
1023
|
adamc@174
|
1024 fun projectConstructor env {sgn, str, field} =
|
adamc@174
|
1025 case #1 (hnormSgn env sgn) of
|
adamc@174
|
1026 SgnConst sgis =>
|
adamc@174
|
1027 let
|
adamc@191
|
1028 fun consider (n, xs, xncs) =
|
adamc@174
|
1029 ListUtil.search (fn (x, n', to) =>
|
adamc@174
|
1030 if x <> field then
|
adamc@174
|
1031 NONE
|
adamc@174
|
1032 else
|
adamc@191
|
1033 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
|
adamc@174
|
1034 in
|
adamc@191
|
1035 case sgnSeek (fn SgiDatatype (_, n, xs, xncs) => consider (n, xs, xncs)
|
adamc@191
|
1036 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
|
adamc@174
|
1037 | _ => NONE) sgis of
|
adamc@174
|
1038 NONE => NONE
|
adamc@191
|
1039 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
|
adamc@191
|
1040 sgnSubCon (str, subs) t)
|
adamc@174
|
1041 end
|
adamc@174
|
1042 | _ => NONE
|
adamc@174
|
1043
|
adamc@42
|
1044 fun projectVal env {sgn, str, field} =
|
adamc@42
|
1045 case #1 (hnormSgn env sgn) of
|
adamc@34
|
1046 SgnConst sgis =>
|
adamc@162
|
1047 let
|
adamc@191
|
1048 fun seek (n, xs, xncs) =
|
adamc@162
|
1049 ListUtil.search (fn (x, _, to) =>
|
adamc@162
|
1050 if x = field then
|
adamc@191
|
1051 SOME (let
|
adamc@191
|
1052 val t =
|
adamc@191
|
1053 case to of
|
adamc@191
|
1054 NONE => (CNamed n, #2 sgn)
|
adamc@191
|
1055 | SOME t => (TFun (t, (CNamed n, #2 sgn)), #2 sgn)
|
adamc@191
|
1056 val k = (KType, #2 sgn)
|
adamc@191
|
1057 in
|
adamc@191
|
1058 foldr (fn (x, t) => (TCFun (Explicit, x, k, t), #2 sgn))
|
adamc@191
|
1059 t xs
|
adamc@191
|
1060 end)
|
adamc@162
|
1061 else
|
adamc@162
|
1062 NONE) xncs
|
adamc@162
|
1063 in
|
adamc@162
|
1064 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
|
adamc@191
|
1065 | SgiDatatype (_, n, xs, xncs) => seek (n, xs, xncs)
|
adamc@191
|
1066 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
|
adamc@162
|
1067 | _ => NONE) sgis of
|
adamc@162
|
1068 NONE => NONE
|
adamc@162
|
1069 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
|
adamc@162
|
1070 end
|
adamc@34
|
1071 | SgnError => SOME (CError, ErrorMsg.dummySpan)
|
adamc@42
|
1072 | _ => NONE
|
adamc@34
|
1073
|
adamc@88
|
1074 fun sgnSeekConstraints (str, sgis) =
|
adamc@88
|
1075 let
|
adamc@88
|
1076 fun seek (sgis, sgns, strs, cons, acc) =
|
adamc@88
|
1077 case sgis of
|
adamc@88
|
1078 [] => acc
|
adamc@88
|
1079 | (sgi, _) :: sgis =>
|
adamc@88
|
1080 case sgi of
|
adamc@88
|
1081 SgiConstraint (c1, c2) =>
|
adamc@88
|
1082 let
|
adamc@88
|
1083 val sub = sgnSubCon (str, (sgns, strs, cons))
|
adamc@88
|
1084 in
|
adamc@88
|
1085 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
|
adamc@88
|
1086 end
|
adamc@88
|
1087 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1088 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@191
|
1089 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@191
|
1090 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1091 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@88
|
1092 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
|
adamc@88
|
1093 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
|
adamc@203
|
1094 | SgiTable _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@338
|
1095 | SgiSequence _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@211
|
1096 | SgiClassAbs (x, n) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@211
|
1097 | SgiClass (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1098 in
|
adamc@88
|
1099 seek (sgis, IM.empty, IM.empty, IM.empty, [])
|
adamc@88
|
1100 end
|
adamc@88
|
1101
|
adamc@88
|
1102 fun projectConstraints env {sgn, str} =
|
adamc@88
|
1103 case #1 (hnormSgn env sgn) of
|
adamc@88
|
1104 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
|
adamc@88
|
1105 | SgnError => SOME []
|
adamc@88
|
1106 | _ => NONE
|
adamc@34
|
1107
|
adamc@447
|
1108 fun edeclBinds env (d, loc) =
|
adamc@447
|
1109 case d of
|
adamc@447
|
1110 EDVal (x, t, _) => pushERel env x t
|
adamc@447
|
1111 | EDValRec vis => foldl (fn ((x, t, _), env) => pushERel env x t) env vis
|
adamc@447
|
1112
|
adamc@157
|
1113 fun declBinds env (d, loc) =
|
adamc@157
|
1114 case d of
|
adamc@157
|
1115 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@191
|
1116 | DDatatype (x, n, xs, xncs) =>
|
adamc@157
|
1117 let
|
adamc@297
|
1118 val k = (KType, loc)
|
adamc@297
|
1119 val nxs = length xs
|
adamc@297
|
1120 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
|
adamc@297
|
1121 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
|
adamc@297
|
1122 (KArrow (k, kb), loc)))
|
adamc@297
|
1123 ((CNamed n, loc), k) xs
|
adamc@297
|
1124
|
adamc@297
|
1125 val env = pushCNamedAs env x n kb NONE
|
adamc@191
|
1126 val env = pushDatatype env n xs xncs
|
adamc@157
|
1127 in
|
adamc@191
|
1128 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
1129 let
|
adamc@191
|
1130 val t =
|
adamc@191
|
1131 case to of
|
adamc@297
|
1132 NONE => tb
|
adamc@297
|
1133 | SOME t => (TFun (t, tb), loc)
|
adamc@297
|
1134 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
|
adamc@191
|
1135 in
|
adamc@191
|
1136 pushENamedAs env x' n' t
|
adamc@191
|
1137 end)
|
adamc@191
|
1138 env xncs
|
adamc@157
|
1139 end
|
adamc@191
|
1140 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
|
adamc@157
|
1141 let
|
adamc@157
|
1142 val t = (CModProj (m, ms, x'), loc)
|
adamc@297
|
1143 val k = (KType, loc)
|
adamc@297
|
1144 val nxs = length xs
|
adamc@297
|
1145 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
|
adamc@297
|
1146 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
|
adamc@297
|
1147 (KArrow (k, kb), loc)))
|
adamc@297
|
1148 ((CNamed n, loc), k) xs
|
adamc@297
|
1149
|
adamc@341
|
1150 val env = pushCNamedAs env x n kb (SOME t)
|
adamc@191
|
1151 val env = pushDatatype env n xs xncs
|
adamc@157
|
1152 in
|
adamc@191
|
1153 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
1154 let
|
adamc@191
|
1155 val t =
|
adamc@191
|
1156 case to of
|
adamc@297
|
1157 NONE => tb
|
adamc@297
|
1158 | SOME t => (TFun (t, tb), loc)
|
adamc@297
|
1159 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
|
adamc@191
|
1160 in
|
adamc@191
|
1161 pushENamedAs env x' n' t
|
adamc@191
|
1162 end)
|
adamc@191
|
1163 env xncs
|
adamc@157
|
1164 end
|
adamc@157
|
1165 | DVal (x, n, t, _) => pushENamedAs env x n t
|
adamc@157
|
1166 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
|
adamc@157
|
1167 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@157
|
1168 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
|
adamc@157
|
1169 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@157
|
1170 | DConstraint _ => env
|
adamc@157
|
1171 | DExport _ => env
|
adamc@205
|
1172 | DTable (tn, x, n, c) =>
|
adamc@205
|
1173 let
|
adamc@338
|
1174 val t = (CApp ((CModProj (tn, [], "sql_table"), loc), c), loc)
|
adamc@338
|
1175 in
|
adamc@338
|
1176 pushENamedAs env x n t
|
adamc@338
|
1177 end
|
adamc@338
|
1178 | DSequence (tn, x, n) =>
|
adamc@338
|
1179 let
|
adamc@338
|
1180 val t = (CModProj (tn, [], "sql_sequence"), loc)
|
adamc@205
|
1181 in
|
adamc@205
|
1182 pushENamedAs env x n t
|
adamc@205
|
1183 end
|
adamc@213
|
1184 | DClass (x, n, c) =>
|
adamc@213
|
1185 let
|
adamc@213
|
1186 val k = (KArrow ((KType, loc), (KType, loc)), loc)
|
adamc@213
|
1187 val env = pushCNamedAs env x n k (SOME c)
|
adamc@213
|
1188 in
|
adamc@213
|
1189 pushClass env n
|
adamc@213
|
1190 end
|
adamc@271
|
1191 | DDatabase _ => env
|
adamc@157
|
1192
|
adamc@243
|
1193 fun patBinds env (p, loc) =
|
adamc@243
|
1194 case p of
|
adamc@243
|
1195 PWild => env
|
adamc@243
|
1196 | PVar (x, t) => pushERel env x t
|
adamc@243
|
1197 | PPrim _ => env
|
adamc@243
|
1198 | PCon (_, _, _, NONE) => env
|
adamc@243
|
1199 | PCon (_, _, _, SOME p) => patBinds env p
|
adamc@243
|
1200 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
|
adamc@243
|
1201
|
adamc@2
|
1202 end
|