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@211
|
64 val liftExpInExp =
|
adamc@211
|
65 U.Exp.mapB {kind = fn k => k,
|
adamc@211
|
66 con = fn _ => fn c => c,
|
adamc@211
|
67 exp = fn bound => fn e =>
|
adamc@211
|
68 case e of
|
adamc@211
|
69 ERel xn =>
|
adamc@211
|
70 if xn < bound then
|
adamc@211
|
71 e
|
adamc@211
|
72 else
|
adamc@211
|
73 ERel (xn + 1)
|
adamc@211
|
74 | _ => e,
|
adamc@211
|
75 bind = fn (bound, U.Exp.RelE _) => bound + 1
|
adamc@211
|
76 | (bound, _) => bound}
|
adamc@211
|
77
|
adamc@211
|
78
|
adamc@211
|
79 val liftExp = liftExpInExp 0
|
adamc@13
|
80
|
adamc@13
|
81 (* Back to environments *)
|
adamc@13
|
82
|
adamc@9
|
83 datatype 'a var' =
|
adamc@9
|
84 Rel' of int * 'a
|
adamc@9
|
85 | Named' of int * 'a
|
adamc@2
|
86
|
adamc@9
|
87 datatype 'a var =
|
adamc@9
|
88 NotBound
|
adamc@9
|
89 | Rel of int * 'a
|
adamc@9
|
90 | Named of int * 'a
|
adamc@2
|
91
|
adamc@191
|
92 type datatyp = string list * (string * con option) IM.map
|
adamc@157
|
93
|
adamc@211
|
94 datatype class_name =
|
adamc@211
|
95 ClNamed of int
|
adamc@211
|
96 | ClProj of int * string list * string
|
adamc@211
|
97
|
adamc@216
|
98 fun cn2s cn =
|
adamc@216
|
99 case cn of
|
adamc@216
|
100 ClNamed n => "Named(" ^ Int.toString n ^ ")"
|
adamc@216
|
101 | ClProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
|
adamc@216
|
102
|
adamc@211
|
103 structure CK = struct
|
adamc@211
|
104 type ord_key = class_name
|
adamc@211
|
105 open Order
|
adamc@211
|
106 fun compare x =
|
adamc@211
|
107 case x of
|
adamc@211
|
108 (ClNamed n1, ClNamed n2) => Int.compare (n1, n2)
|
adamc@211
|
109 | (ClNamed _, _) => LESS
|
adamc@211
|
110 | (_, ClNamed _) => GREATER
|
adamc@211
|
111
|
adamc@211
|
112 | (ClProj (m1, ms1, x1), ClProj (m2, ms2, x2)) =>
|
adamc@211
|
113 join (Int.compare (m1, m2),
|
adamc@211
|
114 fn () => join (joinL String.compare (ms1, ms2),
|
adamc@211
|
115 fn () => String.compare (x1, x2)))
|
adamc@211
|
116 end
|
adamc@211
|
117
|
adamc@211
|
118 structure CM = BinaryMapFn(CK)
|
adamc@211
|
119
|
adamc@211
|
120 datatype class_key =
|
adamc@211
|
121 CkNamed of int
|
adamc@211
|
122 | CkRel of int
|
adamc@211
|
123 | CkProj of int * string list * string
|
adamc@211
|
124
|
adamc@216
|
125 fun ck2s ck =
|
adamc@216
|
126 case ck of
|
adamc@216
|
127 CkNamed n => "Named(" ^ Int.toString n ^ ")"
|
adamc@216
|
128 | CkRel n => "Rel(" ^ Int.toString n ^ ")"
|
adamc@216
|
129 | CkProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
|
adamc@216
|
130
|
adamc@216
|
131 fun cp2s (cn, ck) = "(" ^ cn2s cn ^ "," ^ ck2s ck ^ ")"
|
adamc@216
|
132
|
adamc@211
|
133 structure KK = struct
|
adamc@211
|
134 type ord_key = class_key
|
adamc@211
|
135 open Order
|
adamc@211
|
136 fun compare x =
|
adamc@211
|
137 case x of
|
adamc@211
|
138 (CkNamed n1, CkNamed n2) => Int.compare (n1, n2)
|
adamc@211
|
139 | (CkNamed _, _) => LESS
|
adamc@211
|
140 | (_, CkNamed _) => GREATER
|
adamc@211
|
141
|
adamc@211
|
142 | (CkRel n1, CkRel n2) => Int.compare (n1, n2)
|
adamc@211
|
143 | (CkRel _, _) => LESS
|
adamc@211
|
144 | (_, CkRel _) => GREATER
|
adamc@211
|
145
|
adamc@211
|
146 | (CkProj (m1, ms1, x1), CkProj (m2, ms2, x2)) =>
|
adamc@211
|
147 join (Int.compare (m1, m2),
|
adamc@211
|
148 fn () => join (joinL String.compare (ms1, ms2),
|
adamc@211
|
149 fn () => String.compare (x1, x2)))
|
adamc@211
|
150 end
|
adamc@211
|
151
|
adamc@211
|
152 structure KM = BinaryMapFn(KK)
|
adamc@211
|
153
|
adamc@211
|
154 type class = {
|
adamc@211
|
155 ground : exp KM.map
|
adamc@211
|
156 }
|
adamc@211
|
157
|
adamc@211
|
158 val empty_class = {
|
adamc@211
|
159 ground = KM.empty
|
adamc@211
|
160 }
|
adamc@211
|
161
|
adamc@216
|
162 fun printClasses cs = CM.appi (fn (cn, {ground = km}) =>
|
adamc@216
|
163 (print (cn2s cn ^ ":");
|
adamc@216
|
164 KM.appi (fn (ck, _) => print (" " ^ ck2s ck)) km;
|
adamc@216
|
165 print "\n")) cs
|
adamc@216
|
166
|
adamc@2
|
167 type env = {
|
adamc@9
|
168 renameC : kind var' SM.map,
|
adamc@2
|
169 relC : (string * kind) list,
|
adamc@11
|
170 namedC : (string * kind * con option) IM.map,
|
adamc@9
|
171
|
adamc@157
|
172 datatypes : datatyp IM.map,
|
adamc@191
|
173 constructors : (datatype_kind * int * string list * con option * int) SM.map,
|
adamc@157
|
174
|
adamc@211
|
175 classes : class CM.map,
|
adamc@211
|
176
|
adamc@9
|
177 renameE : con var' SM.map,
|
adamc@9
|
178 relE : (string * con) list,
|
adamc@31
|
179 namedE : (string * con) IM.map,
|
adamc@31
|
180
|
adamc@31
|
181 renameSgn : (int * sgn) SM.map,
|
adamc@31
|
182 sgn : (string * sgn) IM.map,
|
adamc@31
|
183
|
adamc@31
|
184 renameStr : (int * sgn) SM.map,
|
adamc@31
|
185 str : (string * sgn) IM.map
|
adamc@2
|
186 }
|
adamc@2
|
187
|
adamc@2
|
188 val namedCounter = ref 0
|
adamc@2
|
189
|
adamc@109
|
190 fun newNamed () =
|
adamc@109
|
191 let
|
adamc@109
|
192 val r = !namedCounter
|
adamc@109
|
193 in
|
adamc@109
|
194 namedCounter := r + 1;
|
adamc@109
|
195 r
|
adamc@109
|
196 end
|
adamc@109
|
197
|
adamc@2
|
198 val empty = {
|
adamc@2
|
199 renameC = SM.empty,
|
adamc@2
|
200 relC = [],
|
adamc@9
|
201 namedC = IM.empty,
|
adamc@9
|
202
|
adamc@157
|
203 datatypes = IM.empty,
|
adamc@171
|
204 constructors = SM.empty,
|
adamc@157
|
205
|
adamc@211
|
206 classes = CM.empty,
|
adamc@211
|
207
|
adamc@9
|
208 renameE = SM.empty,
|
adamc@9
|
209 relE = [],
|
adamc@31
|
210 namedE = IM.empty,
|
adamc@31
|
211
|
adamc@31
|
212 renameSgn = SM.empty,
|
adamc@31
|
213 sgn = IM.empty,
|
adamc@31
|
214
|
adamc@31
|
215 renameStr = SM.empty,
|
adamc@31
|
216 str = IM.empty
|
adamc@2
|
217 }
|
adamc@2
|
218
|
adamc@211
|
219 fun liftClassKey ck =
|
adamc@211
|
220 case ck of
|
adamc@211
|
221 CkNamed _ => ck
|
adamc@211
|
222 | CkRel n => CkRel (n + 1)
|
adamc@211
|
223 | CkProj _ => ck
|
adamc@211
|
224
|
adamc@2
|
225 fun pushCRel (env : env) x k =
|
adamc@2
|
226 let
|
adamc@9
|
227 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
|
adamc@2
|
228 | x => x) (#renameC env)
|
adamc@2
|
229 in
|
adamc@9
|
230 {renameC = SM.insert (renameC, x, Rel' (0, k)),
|
adamc@2
|
231 relC = (x, k) :: #relC env,
|
adamc@13
|
232 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
|
adamc@9
|
233
|
adamc@157
|
234 datatypes = #datatypes env,
|
adamc@171
|
235 constructors = #constructors env,
|
adamc@157
|
236
|
adamc@211
|
237 classes = CM.map (fn class => {
|
adamc@211
|
238 ground = KM.foldli (fn (ck, e, km) =>
|
adamc@211
|
239 KM.insert (km, liftClassKey ck, e))
|
adamc@211
|
240 KM.empty (#ground class)
|
adamc@211
|
241 })
|
adamc@211
|
242 (#classes env),
|
adamc@211
|
243
|
adamc@9
|
244 renameE = #renameE env,
|
adamc@13
|
245 relE = map (fn (x, c) => (x, lift c)) (#relE env),
|
adamc@31
|
246 namedE = IM.map (fn (x, c) => (x, lift c)) (#namedE env),
|
adamc@31
|
247
|
adamc@31
|
248 renameSgn = #renameSgn env,
|
adamc@31
|
249 sgn = #sgn env,
|
adamc@31
|
250
|
adamc@31
|
251 renameStr = #renameStr env,
|
adamc@31
|
252 str = #str env
|
adamc@31
|
253 }
|
adamc@2
|
254 end
|
adamc@2
|
255
|
adamc@2
|
256 fun lookupCRel (env : env) n =
|
adamc@2
|
257 (List.nth (#relC env, n))
|
adamc@2
|
258 handle Subscript => raise UnboundRel n
|
adamc@2
|
259
|
adamc@11
|
260 fun pushCNamedAs (env : env) x n k co =
|
adamc@9
|
261 {renameC = SM.insert (#renameC env, x, Named' (n, k)),
|
adamc@5
|
262 relC = #relC env,
|
adamc@11
|
263 namedC = IM.insert (#namedC env, n, (x, k, co)),
|
adamc@9
|
264
|
adamc@157
|
265 datatypes = #datatypes env,
|
adamc@171
|
266 constructors = #constructors env,
|
adamc@157
|
267
|
adamc@211
|
268 classes = #classes env,
|
adamc@211
|
269
|
adamc@9
|
270 renameE = #renameE env,
|
adamc@9
|
271 relE = #relE env,
|
adamc@31
|
272 namedE = #namedE env,
|
adamc@31
|
273
|
adamc@31
|
274 renameSgn = #renameSgn env,
|
adamc@31
|
275 sgn = #sgn env,
|
adamc@31
|
276
|
adamc@31
|
277 renameStr = #renameStr env,
|
adamc@31
|
278 str = #str env}
|
adamc@5
|
279
|
adamc@11
|
280 fun pushCNamed env x k co =
|
adamc@2
|
281 let
|
adamc@2
|
282 val n = !namedCounter
|
adamc@2
|
283 in
|
adamc@2
|
284 namedCounter := n + 1;
|
adamc@11
|
285 (pushCNamedAs env x n k co, n)
|
adamc@2
|
286 end
|
adamc@2
|
287
|
adamc@2
|
288 fun lookupCNamed (env : env) n =
|
adamc@2
|
289 case IM.find (#namedC env, n) of
|
adamc@2
|
290 NONE => raise UnboundNamed n
|
adamc@2
|
291 | SOME x => x
|
adamc@2
|
292
|
adamc@2
|
293 fun lookupC (env : env) x =
|
adamc@2
|
294 case SM.find (#renameC env, x) of
|
adamc@9
|
295 NONE => NotBound
|
adamc@9
|
296 | SOME (Rel' x) => Rel x
|
adamc@9
|
297 | SOME (Named' x) => Named x
|
adamc@9
|
298
|
adamc@191
|
299 fun pushDatatype (env : env) n xs xncs =
|
adamc@188
|
300 let
|
adamc@188
|
301 val dk = U.classifyDatatype xncs
|
adamc@188
|
302 in
|
adamc@188
|
303 {renameC = #renameC env,
|
adamc@188
|
304 relC = #relC env,
|
adamc@188
|
305 namedC = #namedC env,
|
adamc@157
|
306
|
adamc@188
|
307 datatypes = IM.insert (#datatypes env, n,
|
adamc@191
|
308 (xs, foldl (fn ((x, n, to), cons) =>
|
adamc@191
|
309 IM.insert (cons, n, (x, to))) IM.empty xncs)),
|
adamc@188
|
310 constructors = foldl (fn ((x, n', to), cmap) =>
|
adamc@191
|
311 SM.insert (cmap, x, (dk, n', xs, to, n)))
|
adamc@188
|
312 (#constructors env) xncs,
|
adamc@157
|
313
|
adamc@211
|
314 classes = #classes env,
|
adamc@211
|
315
|
adamc@188
|
316 renameE = #renameE env,
|
adamc@188
|
317 relE = #relE env,
|
adamc@188
|
318 namedE = #namedE env,
|
adamc@157
|
319
|
adamc@188
|
320 renameSgn = #renameSgn env,
|
adamc@188
|
321 sgn = #sgn env,
|
adamc@157
|
322
|
adamc@188
|
323 renameStr = #renameStr env,
|
adamc@188
|
324 str = #str env}
|
adamc@188
|
325 end
|
adamc@157
|
326
|
adamc@157
|
327 fun lookupDatatype (env : env) n =
|
adamc@157
|
328 case IM.find (#datatypes env, n) of
|
adamc@157
|
329 NONE => raise UnboundNamed n
|
adamc@157
|
330 | SOME x => x
|
adamc@157
|
331
|
adamc@191
|
332 fun lookupDatatypeConstructor (_, dt) n =
|
adamc@157
|
333 case IM.find (dt, n) of
|
adamc@157
|
334 NONE => raise UnboundNamed n
|
adamc@157
|
335 | SOME x => x
|
adamc@157
|
336
|
adamc@171
|
337 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
|
adamc@171
|
338
|
adamc@191
|
339 fun datatypeArgs (xs, _) = xs
|
adamc@191
|
340 fun constructors (_, dt) = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
|
adamc@157
|
341
|
adamc@211
|
342 fun pushClass (env : env) n =
|
adamc@211
|
343 {renameC = #renameC env,
|
adamc@211
|
344 relC = #relC env,
|
adamc@211
|
345 namedC = #namedC env,
|
adamc@211
|
346
|
adamc@211
|
347 datatypes = #datatypes env,
|
adamc@211
|
348 constructors = #constructors env,
|
adamc@211
|
349
|
adamc@211
|
350 classes = CM.insert (#classes env, ClNamed n, {ground = KM.empty}),
|
adamc@211
|
351
|
adamc@211
|
352 renameE = #renameE env,
|
adamc@211
|
353 relE = #relE env,
|
adamc@211
|
354 namedE = #namedE env,
|
adamc@211
|
355
|
adamc@211
|
356 renameSgn = #renameSgn env,
|
adamc@211
|
357 sgn = #sgn env,
|
adamc@211
|
358
|
adamc@211
|
359 renameStr = #renameStr env,
|
adamc@211
|
360 str = #str env}
|
adamc@211
|
361
|
adamc@211
|
362 fun class_name_in (c, _) =
|
adamc@211
|
363 case c of
|
adamc@211
|
364 CNamed n => SOME (ClNamed n)
|
adamc@211
|
365 | CModProj x => SOME (ClProj x)
|
adamc@228
|
366 | CUnif (_, _, _, ref (SOME c)) => class_name_in c
|
adamc@211
|
367 | _ => NONE
|
adamc@211
|
368
|
adamc@211
|
369 fun class_key_in (c, _) =
|
adamc@211
|
370 case c of
|
adamc@211
|
371 CRel n => SOME (CkRel n)
|
adamc@211
|
372 | CNamed n => SOME (CkNamed n)
|
adamc@211
|
373 | CModProj x => SOME (CkProj x)
|
adamc@228
|
374 | CUnif (_, _, _, ref (SOME c)) => class_key_in c
|
adamc@211
|
375 | _ => NONE
|
adamc@211
|
376
|
adamc@211
|
377 fun class_pair_in (c, _) =
|
adamc@211
|
378 case c of
|
adamc@211
|
379 CApp (f, x) =>
|
adamc@211
|
380 (case (class_name_in f, class_key_in x) of
|
adamc@211
|
381 (SOME f, SOME x) => SOME (f, x)
|
adamc@211
|
382 | _ => NONE)
|
adamc@211
|
383 | _ => NONE
|
adamc@211
|
384
|
adamc@211
|
385 fun resolveClass (env : env) c =
|
adamc@211
|
386 case class_pair_in c of
|
adamc@211
|
387 SOME (f, x) =>
|
adamc@211
|
388 (case CM.find (#classes env, f) of
|
adamc@211
|
389 NONE => NONE
|
adamc@211
|
390 | SOME class =>
|
adamc@211
|
391 case KM.find (#ground class, x) of
|
adamc@211
|
392 NONE => NONE
|
adamc@211
|
393 | SOME e => SOME e)
|
adamc@211
|
394 | _ => NONE
|
adamc@211
|
395
|
adamc@9
|
396 fun pushERel (env : env) x t =
|
adamc@9
|
397 let
|
adamc@9
|
398 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
|
adamc@9
|
399 | x => x) (#renameE env)
|
adamc@211
|
400
|
adamc@211
|
401 val classes = CM.map (fn class => {
|
adamc@211
|
402 ground = KM.map liftExp (#ground class)
|
adamc@211
|
403 }) (#classes env)
|
adamc@211
|
404 val classes = case class_pair_in t of
|
adamc@211
|
405 NONE => classes
|
adamc@211
|
406 | SOME (f, x) =>
|
adamc@211
|
407 let
|
adamc@211
|
408 val class = Option.getOpt (CM.find (classes, f), empty_class)
|
adamc@211
|
409 val class = {
|
adamc@211
|
410 ground = KM.insert (#ground class, x, (ERel 0, #2 t))
|
adamc@211
|
411 }
|
adamc@211
|
412 in
|
adamc@211
|
413 CM.insert (classes, f, class)
|
adamc@211
|
414 end
|
adamc@9
|
415 in
|
adamc@9
|
416 {renameC = #renameC env,
|
adamc@9
|
417 relC = #relC env,
|
adamc@9
|
418 namedC = #namedC env,
|
adamc@9
|
419
|
adamc@157
|
420 datatypes = #datatypes env,
|
adamc@171
|
421 constructors = #constructors env,
|
adamc@157
|
422
|
adamc@211
|
423 classes = classes,
|
adamc@211
|
424
|
adamc@9
|
425 renameE = SM.insert (renameE, x, Rel' (0, t)),
|
adamc@9
|
426 relE = (x, t) :: #relE env,
|
adamc@31
|
427 namedE = #namedE env,
|
adamc@31
|
428
|
adamc@31
|
429 renameSgn = #renameSgn env,
|
adamc@31
|
430 sgn = #sgn env,
|
adamc@31
|
431
|
adamc@31
|
432 renameStr = #renameStr env,
|
adamc@31
|
433 str = #str env}
|
adamc@9
|
434 end
|
adamc@9
|
435
|
adamc@9
|
436 fun lookupERel (env : env) n =
|
adamc@9
|
437 (List.nth (#relE env, n))
|
adamc@9
|
438 handle Subscript => raise UnboundRel n
|
adamc@9
|
439
|
adamc@9
|
440 fun pushENamedAs (env : env) x n t =
|
adamc@211
|
441 let
|
adamc@211
|
442 val classes = #classes env
|
adamc@211
|
443 val classes = case class_pair_in t of
|
adamc@211
|
444 NONE => classes
|
adamc@211
|
445 | SOME (f, x) =>
|
adamc@211
|
446 let
|
adamc@211
|
447 val class = Option.getOpt (CM.find (classes, f), empty_class)
|
adamc@211
|
448 val class = {
|
adamc@211
|
449 ground = KM.insert (#ground class, x, (ENamed n, #2 t))
|
adamc@211
|
450 }
|
adamc@211
|
451 in
|
adamc@211
|
452 CM.insert (classes, f, class)
|
adamc@211
|
453 end
|
adamc@211
|
454 in
|
adamc@211
|
455 {renameC = #renameC env,
|
adamc@211
|
456 relC = #relC env,
|
adamc@211
|
457 namedC = #namedC env,
|
adamc@9
|
458
|
adamc@211
|
459 datatypes = #datatypes env,
|
adamc@211
|
460 constructors = #constructors env,
|
adamc@157
|
461
|
adamc@211
|
462 classes = classes,
|
adamc@31
|
463
|
adamc@211
|
464 renameE = SM.insert (#renameE env, x, Named' (n, t)),
|
adamc@211
|
465 relE = #relE env,
|
adamc@211
|
466 namedE = IM.insert (#namedE env, n, (x, t)),
|
adamc@211
|
467
|
adamc@211
|
468 renameSgn = #renameSgn env,
|
adamc@211
|
469 sgn = #sgn env,
|
adamc@211
|
470
|
adamc@211
|
471 renameStr = #renameStr env,
|
adamc@211
|
472 str = #str env}
|
adamc@211
|
473 end
|
adamc@9
|
474
|
adamc@9
|
475 fun pushENamed env x t =
|
adamc@9
|
476 let
|
adamc@9
|
477 val n = !namedCounter
|
adamc@9
|
478 in
|
adamc@9
|
479 namedCounter := n + 1;
|
adamc@9
|
480 (pushENamedAs env x n t, n)
|
adamc@9
|
481 end
|
adamc@9
|
482
|
adamc@9
|
483 fun lookupENamed (env : env) n =
|
adamc@9
|
484 case IM.find (#namedE env, n) of
|
adamc@9
|
485 NONE => raise UnboundNamed n
|
adamc@9
|
486 | SOME x => x
|
adamc@9
|
487
|
adamc@9
|
488 fun lookupE (env : env) x =
|
adamc@9
|
489 case SM.find (#renameE env, x) of
|
adamc@9
|
490 NONE => NotBound
|
adamc@9
|
491 | SOME (Rel' x) => Rel x
|
adamc@9
|
492 | SOME (Named' x) => Named x
|
adamc@2
|
493
|
adamc@31
|
494 fun pushSgnNamedAs (env : env) x n sgis =
|
adamc@31
|
495 {renameC = #renameC env,
|
adamc@31
|
496 relC = #relC env,
|
adamc@31
|
497 namedC = #namedC env,
|
adamc@31
|
498
|
adamc@157
|
499 datatypes = #datatypes env,
|
adamc@171
|
500 constructors = #constructors env,
|
adamc@157
|
501
|
adamc@211
|
502 classes = #classes env,
|
adamc@211
|
503
|
adamc@31
|
504 renameE = #renameE env,
|
adamc@31
|
505 relE = #relE env,
|
adamc@31
|
506 namedE = #namedE env,
|
adamc@31
|
507
|
adamc@31
|
508 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
|
adamc@31
|
509 sgn = IM.insert (#sgn env, n, (x, sgis)),
|
adamc@31
|
510
|
adamc@31
|
511 renameStr = #renameStr env,
|
adamc@31
|
512 str = #str env}
|
adamc@31
|
513
|
adamc@31
|
514 fun pushSgnNamed env x sgis =
|
adamc@31
|
515 let
|
adamc@31
|
516 val n = !namedCounter
|
adamc@31
|
517 in
|
adamc@31
|
518 namedCounter := n + 1;
|
adamc@31
|
519 (pushSgnNamedAs env x n sgis, n)
|
adamc@31
|
520 end
|
adamc@31
|
521
|
adamc@31
|
522 fun lookupSgnNamed (env : env) n =
|
adamc@31
|
523 case IM.find (#sgn env, n) of
|
adamc@31
|
524 NONE => raise UnboundNamed n
|
adamc@31
|
525 | SOME x => x
|
adamc@31
|
526
|
adamc@31
|
527 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
|
adamc@31
|
528
|
adamc@31
|
529 fun lookupStrNamed (env : env) n =
|
adamc@31
|
530 case IM.find (#str env, n) of
|
adamc@31
|
531 NONE => raise UnboundNamed n
|
adamc@31
|
532 | SOME x => x
|
adamc@31
|
533
|
adamc@31
|
534 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
|
adamc@31
|
535
|
adamc@203
|
536
|
adamc@216
|
537 fun sgiSeek (sgi, (sgns, strs, cons)) =
|
adamc@216
|
538 case sgi of
|
adamc@216
|
539 SgiConAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
540 | SgiCon (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
541 | SgiDatatype (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
542 | SgiDatatypeImp (x, n, _, _, _, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
543 | SgiVal _ => (sgns, strs, cons)
|
adamc@216
|
544 | SgiSgn (x, n, _) => (IM.insert (sgns, n, x), strs, cons)
|
adamc@216
|
545 | SgiStr (x, n, _) => (sgns, IM.insert (strs, n, x), cons)
|
adamc@216
|
546 | SgiConstraint _ => (sgns, strs, cons)
|
adamc@216
|
547 | SgiTable _ => (sgns, strs, cons)
|
adamc@216
|
548 | SgiClassAbs (x, n) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
549 | SgiClass (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
550
|
adamc@34
|
551 fun sgnSeek f sgis =
|
adamc@34
|
552 let
|
adamc@59
|
553 fun seek (sgis, sgns, strs, cons) =
|
adamc@34
|
554 case sgis of
|
adamc@34
|
555 [] => NONE
|
adamc@34
|
556 | (sgi, _) :: sgis =>
|
adamc@34
|
557 case f sgi of
|
adamc@158
|
558 SOME v =>
|
adamc@158
|
559 let
|
adamc@158
|
560 val cons =
|
adamc@158
|
561 case sgi of
|
adamc@191
|
562 SgiDatatype (x, n, _, _) => IM.insert (cons, n, x)
|
adamc@191
|
563 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
|
adamc@158
|
564 | _ => cons
|
adamc@158
|
565 in
|
adamc@158
|
566 SOME (v, (sgns, strs, cons))
|
adamc@158
|
567 end
|
adamc@88
|
568 | NONE =>
|
adamc@216
|
569 let
|
adamc@216
|
570 val (sgns, strs, cons) = sgiSeek (sgi, (sgns, strs, cons))
|
adamc@216
|
571 in
|
adamc@216
|
572 seek (sgis, sgns, strs, cons)
|
adamc@216
|
573 end
|
adamc@34
|
574 in
|
adamc@59
|
575 seek (sgis, IM.empty, IM.empty, IM.empty)
|
adamc@34
|
576 end
|
adamc@34
|
577
|
adamc@34
|
578 fun id x = x
|
adamc@34
|
579
|
adamc@34
|
580 fun unravelStr (str, _) =
|
adamc@34
|
581 case str of
|
adamc@34
|
582 StrVar x => (x, [])
|
adamc@34
|
583 | StrProj (str, m) =>
|
adamc@34
|
584 let
|
adamc@34
|
585 val (x, ms) = unravelStr str
|
adamc@34
|
586 in
|
adamc@34
|
587 (x, ms @ [m])
|
adamc@34
|
588 end
|
adamc@34
|
589 | _ => raise Fail "unravelStr"
|
adamc@34
|
590
|
adamc@59
|
591 fun sgnS_con (str, (sgns, strs, cons)) c =
|
adamc@34
|
592 case c of
|
adamc@34
|
593 CModProj (m1, ms, x) =>
|
adamc@34
|
594 (case IM.find (strs, m1) of
|
adamc@34
|
595 NONE => c
|
adamc@34
|
596 | SOME m1x =>
|
adamc@34
|
597 let
|
adamc@34
|
598 val (m1, ms') = unravelStr str
|
adamc@34
|
599 in
|
adamc@34
|
600 CModProj (m1, ms' @ m1x :: ms, x)
|
adamc@34
|
601 end)
|
adamc@34
|
602 | CNamed n =>
|
adamc@34
|
603 (case IM.find (cons, n) of
|
adamc@34
|
604 NONE => c
|
adamc@34
|
605 | SOME nx =>
|
adamc@34
|
606 let
|
adamc@34
|
607 val (m1, ms) = unravelStr str
|
adamc@34
|
608 in
|
adamc@34
|
609 CModProj (m1, ms, nx)
|
adamc@34
|
610 end)
|
adamc@34
|
611 | _ => c
|
adamc@34
|
612
|
adamc@216
|
613 fun sgnS_con' (m1, ms', (sgns, strs, cons)) c =
|
adamc@216
|
614 case c of
|
adamc@216
|
615 CModProj (m1, ms, x) =>
|
adamc@216
|
616 (case IM.find (strs, m1) of
|
adamc@216
|
617 NONE => c
|
adamc@216
|
618 | SOME m1x => CModProj (m1, ms' @ m1x :: ms, x))
|
adamc@216
|
619 | CNamed n =>
|
adamc@216
|
620 (case IM.find (cons, n) of
|
adamc@216
|
621 NONE => c
|
adamc@216
|
622 | SOME nx => CModProj (m1, ms', nx))
|
adamc@216
|
623 | _ => c
|
adamc@216
|
624
|
adamc@59
|
625 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
|
adamc@59
|
626 case sgn of
|
adamc@59
|
627 SgnProj (m1, ms, x) =>
|
adamc@59
|
628 (case IM.find (strs, m1) of
|
adamc@59
|
629 NONE => sgn
|
adamc@59
|
630 | SOME m1x =>
|
adamc@59
|
631 let
|
adamc@59
|
632 val (m1, ms') = unravelStr str
|
adamc@59
|
633 in
|
adamc@59
|
634 SgnProj (m1, ms' @ m1x :: ms, x)
|
adamc@59
|
635 end)
|
adamc@59
|
636 | SgnVar n =>
|
adamc@59
|
637 (case IM.find (sgns, n) of
|
adamc@59
|
638 NONE => sgn
|
adamc@59
|
639 | SOME nx =>
|
adamc@59
|
640 let
|
adamc@59
|
641 val (m1, ms) = unravelStr str
|
adamc@59
|
642 in
|
adamc@59
|
643 SgnProj (m1, ms, nx)
|
adamc@59
|
644 end)
|
adamc@59
|
645 | _ => sgn
|
adamc@59
|
646
|
adamc@59
|
647 fun sgnSubSgn x =
|
adamc@34
|
648 ElabUtil.Sgn.map {kind = id,
|
adamc@59
|
649 con = sgnS_con x,
|
adamc@34
|
650 sgn_item = id,
|
adamc@59
|
651 sgn = sgnS_sgn x}
|
adamc@34
|
652
|
adamc@211
|
653
|
adamc@211
|
654
|
adamc@211
|
655 and projectSgn env {sgn, str, field} =
|
adamc@211
|
656 case #1 (hnormSgn env sgn) of
|
adamc@211
|
657 SgnConst sgis =>
|
adamc@211
|
658 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@211
|
659 NONE => NONE
|
adamc@211
|
660 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@211
|
661 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@211
|
662 | _ => NONE
|
adamc@211
|
663
|
adamc@211
|
664 and hnormSgn env (all as (sgn, loc)) =
|
adamc@34
|
665 case sgn of
|
adamc@42
|
666 SgnError => all
|
adamc@42
|
667 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
|
adamc@42
|
668 | SgnConst _ => all
|
adamc@42
|
669 | SgnFun _ => all
|
adamc@59
|
670 | SgnProj (m, ms, x) =>
|
adamc@59
|
671 let
|
adamc@59
|
672 val (_, sgn) = lookupStrNamed env m
|
adamc@59
|
673 in
|
adamc@59
|
674 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
|
adamc@59
|
675 sgn = sgn,
|
adamc@59
|
676 field = x} of
|
adamc@59
|
677 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
|
adamc@59
|
678 | SOME sgn => sgn
|
adamc@59
|
679 end
|
adamc@42
|
680 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
681 case #1 (hnormSgn env sgn) of
|
adamc@42
|
682 SgnError => (SgnError, loc)
|
adamc@42
|
683 | SgnConst sgis =>
|
adamc@42
|
684 let
|
adamc@42
|
685 fun traverse (pre, post) =
|
adamc@42
|
686 case post of
|
adamc@42
|
687 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
|
adamc@42
|
688 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
|
adamc@42
|
689 if x = x' then
|
adamc@42
|
690 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
|
adamc@42
|
691 else
|
adamc@42
|
692 traverse (sgi :: pre, rest)
|
adamc@42
|
693 | sgi :: rest => traverse (sgi :: pre, rest)
|
adamc@42
|
694
|
adamc@42
|
695 val sgis = traverse ([], sgis)
|
adamc@42
|
696 in
|
adamc@42
|
697 (SgnConst sgis, loc)
|
adamc@42
|
698 end
|
adamc@42
|
699 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
|
adamc@42
|
700
|
adamc@211
|
701 fun enrichClasses env classes (m1, ms) sgn =
|
adamc@59
|
702 case #1 (hnormSgn env sgn) of
|
adamc@59
|
703 SgnConst sgis =>
|
adamc@211
|
704 let
|
adamc@218
|
705 val (classes, _, _, _) =
|
adamc@218
|
706 foldl (fn (sgi, (classes, newClasses, fmap, env)) =>
|
adamc@211
|
707 let
|
adamc@211
|
708 fun found (x, n) =
|
adamc@211
|
709 (CM.insert (classes,
|
adamc@211
|
710 ClProj (m1, ms, x),
|
adamc@211
|
711 empty_class),
|
adamc@216
|
712 IM.insert (newClasses, n, x),
|
adamc@218
|
713 sgiSeek (#1 sgi, fmap),
|
adamc@218
|
714 env)
|
adamc@216
|
715
|
adamc@218
|
716 fun default () = (classes, newClasses, sgiSeek (#1 sgi, fmap), env)
|
adamc@211
|
717 in
|
adamc@211
|
718 case #1 sgi of
|
adamc@217
|
719 SgiStr (x, _, sgn) =>
|
adamc@217
|
720 (enrichClasses env classes (m1, ms @ [x]) sgn,
|
adamc@217
|
721 newClasses,
|
adamc@218
|
722 sgiSeek (#1 sgi, fmap),
|
adamc@218
|
723 env)
|
adamc@218
|
724 | SgiSgn (x, n, sgn) =>
|
adamc@218
|
725 (classes,
|
adamc@218
|
726 newClasses,
|
adamc@218
|
727 fmap,
|
adamc@218
|
728 pushSgnNamedAs env x n sgn)
|
adamc@218
|
729
|
adamc@217
|
730 | SgiClassAbs xn => found xn
|
adamc@211
|
731 | SgiClass (x, n, _) => found (x, n)
|
adamc@216
|
732 | SgiVal (x, n, (CApp ((CNamed f, _), a), _)) =>
|
adamc@216
|
733 (case IM.find (newClasses, f) of
|
adamc@216
|
734 NONE => default ()
|
adamc@216
|
735 | SOME fx =>
|
adamc@216
|
736 case class_key_in (sgnS_con' (m1, ms, fmap) (#1 a), #2 a) of
|
adamc@216
|
737 NONE => default ()
|
adamc@216
|
738 | SOME ck =>
|
adamc@216
|
739 let
|
adamc@216
|
740 val cn = ClProj (m1, ms, fx)
|
adamc@216
|
741 val class = Option.getOpt (CM.find (classes, cn), empty_class)
|
adamc@216
|
742 val class = {
|
adamc@216
|
743 ground = KM.insert (#ground class, ck,
|
adamc@216
|
744 (EModProj (m1, ms, x), #2 sgn))
|
adamc@216
|
745 }
|
adamc@216
|
746
|
adamc@216
|
747 in
|
adamc@216
|
748 (CM.insert (classes, cn, class),
|
adamc@216
|
749 newClasses,
|
adamc@218
|
750 fmap,
|
adamc@218
|
751 env)
|
adamc@216
|
752 end)
|
adamc@217
|
753 | SgiVal _ => default ()
|
adamc@216
|
754 | _ => default ()
|
adamc@211
|
755 end)
|
adamc@218
|
756 (classes, IM.empty, (IM.empty, IM.empty, IM.empty), env) sgis
|
adamc@211
|
757 in
|
adamc@211
|
758 classes
|
adamc@211
|
759 end
|
adamc@211
|
760 | _ => classes
|
adamc@211
|
761
|
adamc@211
|
762 fun pushStrNamedAs (env : env) x n sgn =
|
adamc@211
|
763 {renameC = #renameC env,
|
adamc@211
|
764 relC = #relC env,
|
adamc@211
|
765 namedC = #namedC env,
|
adamc@211
|
766
|
adamc@211
|
767 datatypes = #datatypes env,
|
adamc@211
|
768 constructors = #constructors env,
|
adamc@211
|
769
|
adamc@211
|
770 classes = enrichClasses env (#classes env) (n, []) sgn,
|
adamc@211
|
771
|
adamc@211
|
772 renameE = #renameE env,
|
adamc@211
|
773 relE = #relE env,
|
adamc@211
|
774 namedE = #namedE env,
|
adamc@211
|
775
|
adamc@211
|
776 renameSgn = #renameSgn env,
|
adamc@211
|
777 sgn = #sgn env,
|
adamc@211
|
778
|
adamc@211
|
779 renameStr = SM.insert (#renameStr env, x, (n, sgn)),
|
adamc@211
|
780 str = IM.insert (#str env, n, (x, sgn))}
|
adamc@211
|
781
|
adamc@211
|
782 fun pushStrNamed env x sgn =
|
adamc@211
|
783 let
|
adamc@211
|
784 val n = !namedCounter
|
adamc@211
|
785 in
|
adamc@211
|
786 namedCounter := n + 1;
|
adamc@211
|
787 (pushStrNamedAs env x n sgn, n)
|
adamc@211
|
788 end
|
adamc@211
|
789
|
adamc@211
|
790 fun sgiBinds env (sgi, loc) =
|
adamc@211
|
791 case sgi of
|
adamc@211
|
792 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
|
adamc@211
|
793 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@211
|
794 | SgiDatatype (x, n, xs, xncs) =>
|
adamc@211
|
795 let
|
adamc@211
|
796 val env = pushCNamedAs env x n (KType, loc) NONE
|
adamc@211
|
797 in
|
adamc@211
|
798 foldl (fn ((x', n', to), env) =>
|
adamc@211
|
799 let
|
adamc@211
|
800 val t =
|
adamc@211
|
801 case to of
|
adamc@211
|
802 NONE => (CNamed n, loc)
|
adamc@211
|
803 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@211
|
804
|
adamc@211
|
805 val k = (KType, loc)
|
adamc@211
|
806 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@211
|
807 in
|
adamc@211
|
808 pushENamedAs env x' n' t
|
adamc@211
|
809 end)
|
adamc@211
|
810 env xncs
|
adamc@211
|
811 end
|
adamc@211
|
812 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
|
adamc@211
|
813 let
|
adamc@211
|
814 val env = pushCNamedAs env x n (KType, loc) (SOME (CModProj (m1, ms, x'), loc))
|
adamc@211
|
815 in
|
adamc@211
|
816 foldl (fn ((x', n', to), env) =>
|
adamc@211
|
817 let
|
adamc@211
|
818 val t =
|
adamc@211
|
819 case to of
|
adamc@211
|
820 NONE => (CNamed n, loc)
|
adamc@211
|
821 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@211
|
822
|
adamc@211
|
823 val k = (KType, loc)
|
adamc@211
|
824 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@211
|
825 in
|
adamc@211
|
826 pushENamedAs env x' n' t
|
adamc@211
|
827 end)
|
adamc@211
|
828 env xncs
|
adamc@211
|
829 end
|
adamc@211
|
830 | SgiVal (x, n, t) => pushENamedAs env x n t
|
adamc@211
|
831 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@211
|
832 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@211
|
833 | SgiConstraint _ => env
|
adamc@211
|
834
|
adamc@211
|
835 | SgiTable (tn, x, n, c) =>
|
adamc@211
|
836 let
|
adamc@211
|
837 val t = (CApp ((CModProj (tn, [], "table"), loc), c), loc)
|
adamc@211
|
838 in
|
adamc@211
|
839 pushENamedAs env x n t
|
adamc@211
|
840 end
|
adamc@211
|
841
|
adamc@211
|
842 | SgiClassAbs (x, n) => pushCNamedAs env x n (KArrow ((KType, loc), (KType, loc)), loc) NONE
|
adamc@211
|
843 | SgiClass (x, n, c) => pushCNamedAs env x n (KArrow ((KType, loc), (KType, loc)), loc) (SOME c)
|
adamc@211
|
844
|
adamc@211
|
845
|
adamc@211
|
846 fun sgnSubCon x =
|
adamc@211
|
847 ElabUtil.Con.map {kind = id,
|
adamc@211
|
848 con = sgnS_con x}
|
adamc@59
|
849
|
adamc@59
|
850 fun projectStr env {sgn, str, field} =
|
adamc@59
|
851 case #1 (hnormSgn env sgn) of
|
adamc@59
|
852 SgnConst sgis =>
|
adamc@59
|
853 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
854 NONE => NONE
|
adamc@59
|
855 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
856 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
857 | _ => NONE
|
adamc@59
|
858
|
adamc@158
|
859 fun chaseMpath env (n, ms) =
|
adamc@158
|
860 let
|
adamc@158
|
861 val (_, sgn) = lookupStrNamed env n
|
adamc@158
|
862 in
|
adamc@158
|
863 foldl (fn (m, (str, sgn)) =>
|
adamc@158
|
864 case projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@158
|
865 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@158
|
866 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
|
adamc@158
|
867 ((StrVar n, #2 sgn), sgn) ms
|
adamc@158
|
868 end
|
adamc@158
|
869
|
adamc@42
|
870 fun projectCon env {sgn, str, field} =
|
adamc@42
|
871 case #1 (hnormSgn env sgn) of
|
adamc@34
|
872 SgnConst sgis =>
|
adamc@34
|
873 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
|
adamc@34
|
874 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
|
adamc@191
|
875 | SgiDatatype (x, _, _, _) => if x = field then SOME ((KType, #2 sgn), NONE) else NONE
|
adamc@191
|
876 | SgiDatatypeImp (x, _, m1, ms, x', _, _) =>
|
adamc@158
|
877 if x = field then
|
adamc@158
|
878 SOME ((KType, #2 sgn), SOME (CModProj (m1, ms, x'), #2 sgn))
|
adamc@158
|
879 else
|
adamc@158
|
880 NONE
|
adamc@216
|
881 | SgiClassAbs (x, _) => if x = field then
|
adamc@216
|
882 SOME ((KArrow ((KType, #2 sgn), (KType, #2 sgn)), #2 sgn), NONE)
|
adamc@216
|
883 else
|
adamc@216
|
884 NONE
|
adamc@216
|
885 | SgiClass (x, _, c) => if x = field then
|
adamc@216
|
886 SOME ((KArrow ((KType, #2 sgn), (KType, #2 sgn)), #2 sgn), SOME c)
|
adamc@216
|
887 else
|
adamc@216
|
888 NONE
|
adamc@34
|
889 | _ => NONE) sgis of
|
adamc@34
|
890 NONE => NONE
|
adamc@34
|
891 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
|
adamc@34
|
892 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
|
adamc@42
|
893 | _ => NONE
|
adamc@34
|
894
|
adamc@157
|
895 fun projectDatatype env {sgn, str, field} =
|
adamc@157
|
896 case #1 (hnormSgn env sgn) of
|
adamc@157
|
897 SgnConst sgis =>
|
adamc@191
|
898 (case sgnSeek (fn SgiDatatype (x, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
|
adamc@191
|
899 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
|
adamc@157
|
900 | _ => NONE) sgis of
|
adamc@157
|
901 NONE => NONE
|
adamc@191
|
902 | SOME ((xs, xncs), subs) => SOME (xs,
|
adamc@191
|
903 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
|
adamc@157
|
904 | _ => NONE
|
adamc@157
|
905
|
adamc@174
|
906 fun projectConstructor env {sgn, str, field} =
|
adamc@174
|
907 case #1 (hnormSgn env sgn) of
|
adamc@174
|
908 SgnConst sgis =>
|
adamc@174
|
909 let
|
adamc@191
|
910 fun consider (n, xs, xncs) =
|
adamc@174
|
911 ListUtil.search (fn (x, n', to) =>
|
adamc@174
|
912 if x <> field then
|
adamc@174
|
913 NONE
|
adamc@174
|
914 else
|
adamc@191
|
915 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
|
adamc@174
|
916 in
|
adamc@191
|
917 case sgnSeek (fn SgiDatatype (_, n, xs, xncs) => consider (n, xs, xncs)
|
adamc@191
|
918 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
|
adamc@174
|
919 | _ => NONE) sgis of
|
adamc@174
|
920 NONE => NONE
|
adamc@191
|
921 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
|
adamc@191
|
922 sgnSubCon (str, subs) t)
|
adamc@174
|
923 end
|
adamc@174
|
924 | _ => NONE
|
adamc@174
|
925
|
adamc@42
|
926 fun projectVal env {sgn, str, field} =
|
adamc@42
|
927 case #1 (hnormSgn env sgn) of
|
adamc@34
|
928 SgnConst sgis =>
|
adamc@162
|
929 let
|
adamc@191
|
930 fun seek (n, xs, xncs) =
|
adamc@162
|
931 ListUtil.search (fn (x, _, to) =>
|
adamc@162
|
932 if x = field then
|
adamc@191
|
933 SOME (let
|
adamc@191
|
934 val t =
|
adamc@191
|
935 case to of
|
adamc@191
|
936 NONE => (CNamed n, #2 sgn)
|
adamc@191
|
937 | SOME t => (TFun (t, (CNamed n, #2 sgn)), #2 sgn)
|
adamc@191
|
938 val k = (KType, #2 sgn)
|
adamc@191
|
939 in
|
adamc@191
|
940 foldr (fn (x, t) => (TCFun (Explicit, x, k, t), #2 sgn))
|
adamc@191
|
941 t xs
|
adamc@191
|
942 end)
|
adamc@162
|
943 else
|
adamc@162
|
944 NONE) xncs
|
adamc@162
|
945 in
|
adamc@162
|
946 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
|
adamc@191
|
947 | SgiDatatype (_, n, xs, xncs) => seek (n, xs, xncs)
|
adamc@191
|
948 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
|
adamc@162
|
949 | _ => NONE) sgis of
|
adamc@162
|
950 NONE => NONE
|
adamc@162
|
951 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
|
adamc@162
|
952 end
|
adamc@34
|
953 | SgnError => SOME (CError, ErrorMsg.dummySpan)
|
adamc@42
|
954 | _ => NONE
|
adamc@34
|
955
|
adamc@88
|
956 fun sgnSeekConstraints (str, sgis) =
|
adamc@88
|
957 let
|
adamc@88
|
958 fun seek (sgis, sgns, strs, cons, acc) =
|
adamc@88
|
959 case sgis of
|
adamc@88
|
960 [] => acc
|
adamc@88
|
961 | (sgi, _) :: sgis =>
|
adamc@88
|
962 case sgi of
|
adamc@88
|
963 SgiConstraint (c1, c2) =>
|
adamc@88
|
964 let
|
adamc@88
|
965 val sub = sgnSubCon (str, (sgns, strs, cons))
|
adamc@88
|
966 in
|
adamc@88
|
967 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
|
adamc@88
|
968 end
|
adamc@88
|
969 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
970 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@191
|
971 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@191
|
972 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
973 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@88
|
974 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
|
adamc@88
|
975 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
|
adamc@203
|
976 | SgiTable _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@211
|
977 | SgiClassAbs (x, n) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@211
|
978 | SgiClass (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
979 in
|
adamc@88
|
980 seek (sgis, IM.empty, IM.empty, IM.empty, [])
|
adamc@88
|
981 end
|
adamc@88
|
982
|
adamc@88
|
983 fun projectConstraints env {sgn, str} =
|
adamc@88
|
984 case #1 (hnormSgn env sgn) of
|
adamc@88
|
985 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
|
adamc@88
|
986 | SgnError => SOME []
|
adamc@88
|
987 | _ => NONE
|
adamc@34
|
988
|
adamc@157
|
989 fun declBinds env (d, loc) =
|
adamc@157
|
990 case d of
|
adamc@157
|
991 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@191
|
992 | DDatatype (x, n, xs, xncs) =>
|
adamc@157
|
993 let
|
adamc@157
|
994 val env = pushCNamedAs env x n (KType, loc) NONE
|
adamc@191
|
995 val env = pushDatatype env n xs xncs
|
adamc@157
|
996 in
|
adamc@191
|
997 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
998 let
|
adamc@191
|
999 val t =
|
adamc@191
|
1000 case to of
|
adamc@191
|
1001 NONE => (CNamed n, loc)
|
adamc@191
|
1002 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@191
|
1003 val k = (KType, loc)
|
adamc@191
|
1004 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@191
|
1005 in
|
adamc@191
|
1006 pushENamedAs env x' n' t
|
adamc@191
|
1007 end)
|
adamc@191
|
1008 env xncs
|
adamc@157
|
1009 end
|
adamc@191
|
1010 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
|
adamc@157
|
1011 let
|
adamc@157
|
1012 val t = (CModProj (m, ms, x'), loc)
|
adamc@157
|
1013 val env = pushCNamedAs env x n (KType, loc) (SOME t)
|
adamc@191
|
1014 val env = pushDatatype env n xs xncs
|
adamc@157
|
1015
|
adamc@157
|
1016 val t = (CNamed n, loc)
|
adamc@157
|
1017 in
|
adamc@191
|
1018 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
1019 let
|
adamc@191
|
1020 val t =
|
adamc@191
|
1021 case to of
|
adamc@191
|
1022 NONE => (CNamed n, loc)
|
adamc@191
|
1023 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@191
|
1024 val k = (KType, loc)
|
adamc@191
|
1025 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@191
|
1026 in
|
adamc@191
|
1027 pushENamedAs env x' n' t
|
adamc@191
|
1028 end)
|
adamc@191
|
1029 env xncs
|
adamc@157
|
1030 end
|
adamc@157
|
1031 | DVal (x, n, t, _) => pushENamedAs env x n t
|
adamc@157
|
1032 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
|
adamc@157
|
1033 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@157
|
1034 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
|
adamc@157
|
1035 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@157
|
1036 | DConstraint _ => env
|
adamc@157
|
1037 | DExport _ => env
|
adamc@205
|
1038 | DTable (tn, x, n, c) =>
|
adamc@205
|
1039 let
|
adamc@205
|
1040 val t = (CApp ((CModProj (tn, [], "table"), loc), c), loc)
|
adamc@205
|
1041 in
|
adamc@205
|
1042 pushENamedAs env x n t
|
adamc@205
|
1043 end
|
adamc@213
|
1044 | DClass (x, n, c) =>
|
adamc@213
|
1045 let
|
adamc@213
|
1046 val k = (KArrow ((KType, loc), (KType, loc)), loc)
|
adamc@213
|
1047 val env = pushCNamedAs env x n k (SOME c)
|
adamc@213
|
1048 in
|
adamc@213
|
1049 pushClass env n
|
adamc@213
|
1050 end
|
adamc@271
|
1051 | DDatabase _ => env
|
adamc@157
|
1052
|
adamc@243
|
1053 fun patBinds env (p, loc) =
|
adamc@243
|
1054 case p of
|
adamc@243
|
1055 PWild => env
|
adamc@243
|
1056 | PVar (x, t) => pushERel env x t
|
adamc@243
|
1057 | PPrim _ => env
|
adamc@243
|
1058 | PCon (_, _, _, NONE) => env
|
adamc@243
|
1059 | PCon (_, _, _, SOME p) => patBinds env p
|
adamc@243
|
1060 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
|
adamc@243
|
1061
|
adamc@2
|
1062 end
|