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@13
|
64
|
adamc@13
|
65 (* Back to environments *)
|
adamc@13
|
66
|
adamc@9
|
67 datatype 'a var' =
|
adamc@9
|
68 Rel' of int * 'a
|
adamc@9
|
69 | Named' of int * 'a
|
adamc@2
|
70
|
adamc@9
|
71 datatype 'a var =
|
adamc@9
|
72 NotBound
|
adamc@9
|
73 | Rel of int * 'a
|
adamc@9
|
74 | Named of int * 'a
|
adamc@2
|
75
|
adamc@191
|
76 type datatyp = string list * (string * con option) IM.map
|
adamc@157
|
77
|
adamc@2
|
78 type env = {
|
adamc@9
|
79 renameC : kind var' SM.map,
|
adamc@2
|
80 relC : (string * kind) list,
|
adamc@11
|
81 namedC : (string * kind * con option) IM.map,
|
adamc@9
|
82
|
adamc@157
|
83 datatypes : datatyp IM.map,
|
adamc@191
|
84 constructors : (datatype_kind * int * string list * con option * int) SM.map,
|
adamc@157
|
85
|
adamc@9
|
86 renameE : con var' SM.map,
|
adamc@9
|
87 relE : (string * con) list,
|
adamc@31
|
88 namedE : (string * con) IM.map,
|
adamc@31
|
89
|
adamc@31
|
90 renameSgn : (int * sgn) SM.map,
|
adamc@31
|
91 sgn : (string * sgn) IM.map,
|
adamc@31
|
92
|
adamc@31
|
93 renameStr : (int * sgn) SM.map,
|
adamc@31
|
94 str : (string * sgn) IM.map
|
adamc@2
|
95 }
|
adamc@2
|
96
|
adamc@2
|
97 val namedCounter = ref 0
|
adamc@2
|
98
|
adamc@109
|
99 fun newNamed () =
|
adamc@109
|
100 let
|
adamc@109
|
101 val r = !namedCounter
|
adamc@109
|
102 in
|
adamc@109
|
103 namedCounter := r + 1;
|
adamc@109
|
104 r
|
adamc@109
|
105 end
|
adamc@109
|
106
|
adamc@2
|
107 val empty = {
|
adamc@2
|
108 renameC = SM.empty,
|
adamc@2
|
109 relC = [],
|
adamc@9
|
110 namedC = IM.empty,
|
adamc@9
|
111
|
adamc@157
|
112 datatypes = IM.empty,
|
adamc@171
|
113 constructors = SM.empty,
|
adamc@157
|
114
|
adamc@9
|
115 renameE = SM.empty,
|
adamc@9
|
116 relE = [],
|
adamc@31
|
117 namedE = IM.empty,
|
adamc@31
|
118
|
adamc@31
|
119 renameSgn = SM.empty,
|
adamc@31
|
120 sgn = IM.empty,
|
adamc@31
|
121
|
adamc@31
|
122 renameStr = SM.empty,
|
adamc@31
|
123 str = IM.empty
|
adamc@2
|
124 }
|
adamc@2
|
125
|
adamc@2
|
126 fun pushCRel (env : env) x k =
|
adamc@2
|
127 let
|
adamc@9
|
128 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
|
adamc@2
|
129 | x => x) (#renameC env)
|
adamc@2
|
130 in
|
adamc@9
|
131 {renameC = SM.insert (renameC, x, Rel' (0, k)),
|
adamc@2
|
132 relC = (x, k) :: #relC env,
|
adamc@13
|
133 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
|
adamc@9
|
134
|
adamc@157
|
135 datatypes = #datatypes env,
|
adamc@171
|
136 constructors = #constructors env,
|
adamc@157
|
137
|
adamc@9
|
138 renameE = #renameE env,
|
adamc@13
|
139 relE = map (fn (x, c) => (x, lift c)) (#relE env),
|
adamc@31
|
140 namedE = IM.map (fn (x, c) => (x, lift c)) (#namedE env),
|
adamc@31
|
141
|
adamc@31
|
142 renameSgn = #renameSgn env,
|
adamc@31
|
143 sgn = #sgn env,
|
adamc@31
|
144
|
adamc@31
|
145 renameStr = #renameStr env,
|
adamc@31
|
146 str = #str env
|
adamc@31
|
147 }
|
adamc@2
|
148 end
|
adamc@2
|
149
|
adamc@2
|
150 fun lookupCRel (env : env) n =
|
adamc@2
|
151 (List.nth (#relC env, n))
|
adamc@2
|
152 handle Subscript => raise UnboundRel n
|
adamc@2
|
153
|
adamc@11
|
154 fun pushCNamedAs (env : env) x n k co =
|
adamc@9
|
155 {renameC = SM.insert (#renameC env, x, Named' (n, k)),
|
adamc@5
|
156 relC = #relC env,
|
adamc@11
|
157 namedC = IM.insert (#namedC env, n, (x, k, co)),
|
adamc@9
|
158
|
adamc@157
|
159 datatypes = #datatypes env,
|
adamc@171
|
160 constructors = #constructors env,
|
adamc@157
|
161
|
adamc@9
|
162 renameE = #renameE env,
|
adamc@9
|
163 relE = #relE env,
|
adamc@31
|
164 namedE = #namedE env,
|
adamc@31
|
165
|
adamc@31
|
166 renameSgn = #renameSgn env,
|
adamc@31
|
167 sgn = #sgn env,
|
adamc@31
|
168
|
adamc@31
|
169 renameStr = #renameStr env,
|
adamc@31
|
170 str = #str env}
|
adamc@5
|
171
|
adamc@11
|
172 fun pushCNamed env x k co =
|
adamc@2
|
173 let
|
adamc@2
|
174 val n = !namedCounter
|
adamc@2
|
175 in
|
adamc@2
|
176 namedCounter := n + 1;
|
adamc@11
|
177 (pushCNamedAs env x n k co, n)
|
adamc@2
|
178 end
|
adamc@2
|
179
|
adamc@2
|
180 fun lookupCNamed (env : env) n =
|
adamc@2
|
181 case IM.find (#namedC env, n) of
|
adamc@2
|
182 NONE => raise UnboundNamed n
|
adamc@2
|
183 | SOME x => x
|
adamc@2
|
184
|
adamc@2
|
185 fun lookupC (env : env) x =
|
adamc@2
|
186 case SM.find (#renameC env, x) of
|
adamc@9
|
187 NONE => NotBound
|
adamc@9
|
188 | SOME (Rel' x) => Rel x
|
adamc@9
|
189 | SOME (Named' x) => Named x
|
adamc@9
|
190
|
adamc@191
|
191 fun pushDatatype (env : env) n xs xncs =
|
adamc@188
|
192 let
|
adamc@188
|
193 val dk = U.classifyDatatype xncs
|
adamc@188
|
194 in
|
adamc@188
|
195 {renameC = #renameC env,
|
adamc@188
|
196 relC = #relC env,
|
adamc@188
|
197 namedC = #namedC env,
|
adamc@157
|
198
|
adamc@188
|
199 datatypes = IM.insert (#datatypes env, n,
|
adamc@191
|
200 (xs, foldl (fn ((x, n, to), cons) =>
|
adamc@191
|
201 IM.insert (cons, n, (x, to))) IM.empty xncs)),
|
adamc@188
|
202 constructors = foldl (fn ((x, n', to), cmap) =>
|
adamc@191
|
203 SM.insert (cmap, x, (dk, n', xs, to, n)))
|
adamc@188
|
204 (#constructors env) xncs,
|
adamc@157
|
205
|
adamc@188
|
206 renameE = #renameE env,
|
adamc@188
|
207 relE = #relE env,
|
adamc@188
|
208 namedE = #namedE env,
|
adamc@157
|
209
|
adamc@188
|
210 renameSgn = #renameSgn env,
|
adamc@188
|
211 sgn = #sgn env,
|
adamc@157
|
212
|
adamc@188
|
213 renameStr = #renameStr env,
|
adamc@188
|
214 str = #str env}
|
adamc@188
|
215 end
|
adamc@157
|
216
|
adamc@157
|
217 fun lookupDatatype (env : env) n =
|
adamc@157
|
218 case IM.find (#datatypes env, n) of
|
adamc@157
|
219 NONE => raise UnboundNamed n
|
adamc@157
|
220 | SOME x => x
|
adamc@157
|
221
|
adamc@191
|
222 fun lookupDatatypeConstructor (_, dt) n =
|
adamc@157
|
223 case IM.find (dt, n) of
|
adamc@157
|
224 NONE => raise UnboundNamed n
|
adamc@157
|
225 | SOME x => x
|
adamc@157
|
226
|
adamc@171
|
227 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
|
adamc@171
|
228
|
adamc@191
|
229 fun datatypeArgs (xs, _) = xs
|
adamc@191
|
230 fun constructors (_, dt) = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
|
adamc@157
|
231
|
adamc@9
|
232 fun pushERel (env : env) x t =
|
adamc@9
|
233 let
|
adamc@9
|
234 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
|
adamc@9
|
235 | x => x) (#renameE env)
|
adamc@9
|
236 in
|
adamc@9
|
237 {renameC = #renameC env,
|
adamc@9
|
238 relC = #relC env,
|
adamc@9
|
239 namedC = #namedC env,
|
adamc@9
|
240
|
adamc@157
|
241 datatypes = #datatypes env,
|
adamc@171
|
242 constructors = #constructors env,
|
adamc@157
|
243
|
adamc@9
|
244 renameE = SM.insert (renameE, x, Rel' (0, t)),
|
adamc@9
|
245 relE = (x, t) :: #relE env,
|
adamc@31
|
246 namedE = #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@9
|
253 end
|
adamc@9
|
254
|
adamc@9
|
255 fun lookupERel (env : env) n =
|
adamc@9
|
256 (List.nth (#relE env, n))
|
adamc@9
|
257 handle Subscript => raise UnboundRel n
|
adamc@9
|
258
|
adamc@9
|
259 fun pushENamedAs (env : env) x n t =
|
adamc@9
|
260 {renameC = #renameC env,
|
adamc@9
|
261 relC = #relC env,
|
adamc@9
|
262 namedC = #namedC env,
|
adamc@9
|
263
|
adamc@157
|
264 datatypes = #datatypes env,
|
adamc@171
|
265 constructors = #constructors env,
|
adamc@157
|
266
|
adamc@9
|
267 renameE = SM.insert (#renameE env, x, Named' (n, t)),
|
adamc@9
|
268 relE = #relE env,
|
adamc@31
|
269 namedE = IM.insert (#namedE env, n, (x, t)),
|
adamc@31
|
270
|
adamc@31
|
271 renameSgn = #renameSgn env,
|
adamc@31
|
272 sgn = #sgn env,
|
adamc@31
|
273
|
adamc@31
|
274 renameStr = #renameStr env,
|
adamc@31
|
275 str = #str env}
|
adamc@9
|
276
|
adamc@9
|
277 fun pushENamed env x t =
|
adamc@9
|
278 let
|
adamc@9
|
279 val n = !namedCounter
|
adamc@9
|
280 in
|
adamc@9
|
281 namedCounter := n + 1;
|
adamc@9
|
282 (pushENamedAs env x n t, n)
|
adamc@9
|
283 end
|
adamc@9
|
284
|
adamc@9
|
285 fun lookupENamed (env : env) n =
|
adamc@9
|
286 case IM.find (#namedE env, n) of
|
adamc@9
|
287 NONE => raise UnboundNamed n
|
adamc@9
|
288 | SOME x => x
|
adamc@9
|
289
|
adamc@9
|
290 fun lookupE (env : env) x =
|
adamc@9
|
291 case SM.find (#renameE env, x) of
|
adamc@9
|
292 NONE => NotBound
|
adamc@9
|
293 | SOME (Rel' x) => Rel x
|
adamc@9
|
294 | SOME (Named' x) => Named x
|
adamc@2
|
295
|
adamc@31
|
296 fun pushSgnNamedAs (env : env) x n sgis =
|
adamc@31
|
297 {renameC = #renameC env,
|
adamc@31
|
298 relC = #relC env,
|
adamc@31
|
299 namedC = #namedC env,
|
adamc@31
|
300
|
adamc@157
|
301 datatypes = #datatypes env,
|
adamc@171
|
302 constructors = #constructors env,
|
adamc@157
|
303
|
adamc@31
|
304 renameE = #renameE env,
|
adamc@31
|
305 relE = #relE env,
|
adamc@31
|
306 namedE = #namedE env,
|
adamc@31
|
307
|
adamc@31
|
308 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
|
adamc@31
|
309 sgn = IM.insert (#sgn env, n, (x, sgis)),
|
adamc@31
|
310
|
adamc@31
|
311 renameStr = #renameStr env,
|
adamc@31
|
312 str = #str env}
|
adamc@31
|
313
|
adamc@31
|
314 fun pushSgnNamed env x sgis =
|
adamc@31
|
315 let
|
adamc@31
|
316 val n = !namedCounter
|
adamc@31
|
317 in
|
adamc@31
|
318 namedCounter := n + 1;
|
adamc@31
|
319 (pushSgnNamedAs env x n sgis, n)
|
adamc@31
|
320 end
|
adamc@31
|
321
|
adamc@31
|
322 fun lookupSgnNamed (env : env) n =
|
adamc@31
|
323 case IM.find (#sgn env, n) of
|
adamc@31
|
324 NONE => raise UnboundNamed n
|
adamc@31
|
325 | SOME x => x
|
adamc@31
|
326
|
adamc@31
|
327 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
|
adamc@31
|
328
|
adamc@31
|
329 fun pushStrNamedAs (env : env) x n sgis =
|
adamc@31
|
330 {renameC = #renameC env,
|
adamc@31
|
331 relC = #relC env,
|
adamc@31
|
332 namedC = #namedC env,
|
adamc@31
|
333
|
adamc@157
|
334 datatypes = #datatypes env,
|
adamc@171
|
335 constructors = #constructors env,
|
adamc@157
|
336
|
adamc@31
|
337 renameE = #renameE env,
|
adamc@31
|
338 relE = #relE env,
|
adamc@31
|
339 namedE = #namedE env,
|
adamc@31
|
340
|
adamc@31
|
341 renameSgn = #renameSgn env,
|
adamc@31
|
342 sgn = #sgn env,
|
adamc@31
|
343
|
adamc@31
|
344 renameStr = SM.insert (#renameStr env, x, (n, sgis)),
|
adamc@31
|
345 str = IM.insert (#str env, n, (x, sgis))}
|
adamc@31
|
346
|
adamc@31
|
347 fun pushStrNamed env x sgis =
|
adamc@31
|
348 let
|
adamc@31
|
349 val n = !namedCounter
|
adamc@31
|
350 in
|
adamc@31
|
351 namedCounter := n + 1;
|
adamc@31
|
352 (pushStrNamedAs env x n sgis, n)
|
adamc@31
|
353 end
|
adamc@31
|
354
|
adamc@31
|
355 fun lookupStrNamed (env : env) n =
|
adamc@31
|
356 case IM.find (#str env, n) of
|
adamc@31
|
357 NONE => raise UnboundNamed n
|
adamc@31
|
358 | SOME x => x
|
adamc@31
|
359
|
adamc@31
|
360 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
|
adamc@31
|
361
|
adamc@156
|
362 fun sgiBinds env (sgi, loc) =
|
adamc@31
|
363 case sgi of
|
adamc@31
|
364 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
|
adamc@31
|
365 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@191
|
366 | SgiDatatype (x, n, xs, xncs) =>
|
adamc@156
|
367 let
|
adamc@156
|
368 val env = pushCNamedAs env x n (KType, loc) NONE
|
adamc@156
|
369 in
|
adamc@191
|
370 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
371 let
|
adamc@191
|
372 val t =
|
adamc@191
|
373 case to of
|
adamc@191
|
374 NONE => (CNamed n, loc)
|
adamc@191
|
375 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@191
|
376
|
adamc@191
|
377 val k = (KType, loc)
|
adamc@191
|
378 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@191
|
379 in
|
adamc@191
|
380 pushENamedAs env x' n' t
|
adamc@191
|
381 end)
|
adamc@156
|
382 env xncs
|
adamc@156
|
383 end
|
adamc@191
|
384 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
|
adamc@162
|
385 let
|
adamc@162
|
386 val env = pushCNamedAs env x n (KType, loc) (SOME (CModProj (m1, ms, x'), loc))
|
adamc@162
|
387 in
|
adamc@191
|
388 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
389 let
|
adamc@191
|
390 val t =
|
adamc@191
|
391 case to of
|
adamc@191
|
392 NONE => (CNamed n, loc)
|
adamc@191
|
393 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@191
|
394
|
adamc@191
|
395 val k = (KType, loc)
|
adamc@191
|
396 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@191
|
397 in
|
adamc@191
|
398 pushENamedAs env x' n' t
|
adamc@191
|
399 end)
|
adamc@162
|
400 env xncs
|
adamc@162
|
401 end
|
adamc@31
|
402 | SgiVal (x, n, t) => pushENamedAs env x n t
|
adamc@31
|
403 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@59
|
404 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@88
|
405 | SgiConstraint _ => env
|
adamc@31
|
406
|
adamc@203
|
407 | SgiTable (x, n, c) =>
|
adamc@203
|
408 (case lookupStr env "Basis" of
|
adamc@203
|
409 NONE => raise Fail "ElabEnv.sgiBinds: Can't find Basis"
|
adamc@203
|
410 | SOME (n, _) =>
|
adamc@203
|
411 let
|
adamc@203
|
412 val t = (CApp ((CModProj (n, [], "table"), loc), c), loc)
|
adamc@203
|
413 in
|
adamc@203
|
414 pushENamedAs env x n t
|
adamc@203
|
415 end)
|
adamc@203
|
416
|
adamc@34
|
417 fun sgnSeek f sgis =
|
adamc@34
|
418 let
|
adamc@59
|
419 fun seek (sgis, sgns, strs, cons) =
|
adamc@34
|
420 case sgis of
|
adamc@34
|
421 [] => NONE
|
adamc@34
|
422 | (sgi, _) :: sgis =>
|
adamc@34
|
423 case f sgi of
|
adamc@158
|
424 SOME v =>
|
adamc@158
|
425 let
|
adamc@158
|
426 val cons =
|
adamc@158
|
427 case sgi of
|
adamc@191
|
428 SgiDatatype (x, n, _, _) => IM.insert (cons, n, x)
|
adamc@191
|
429 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
|
adamc@158
|
430 | _ => cons
|
adamc@158
|
431 in
|
adamc@158
|
432 SOME (v, (sgns, strs, cons))
|
adamc@158
|
433 end
|
adamc@88
|
434 | NONE =>
|
adamc@88
|
435 case sgi of
|
adamc@88
|
436 SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@88
|
437 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@191
|
438 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@191
|
439 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@88
|
440 | SgiVal _ => seek (sgis, sgns, strs, cons)
|
adamc@88
|
441 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons)
|
adamc@88
|
442 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons)
|
adamc@88
|
443 | SgiConstraint _ => seek (sgis, sgns, strs, cons)
|
adamc@203
|
444 | SgiTable _ => seek (sgis, sgns, strs, cons)
|
adamc@34
|
445 in
|
adamc@59
|
446 seek (sgis, IM.empty, IM.empty, IM.empty)
|
adamc@34
|
447 end
|
adamc@34
|
448
|
adamc@34
|
449 fun id x = x
|
adamc@34
|
450
|
adamc@34
|
451 fun unravelStr (str, _) =
|
adamc@34
|
452 case str of
|
adamc@34
|
453 StrVar x => (x, [])
|
adamc@34
|
454 | StrProj (str, m) =>
|
adamc@34
|
455 let
|
adamc@34
|
456 val (x, ms) = unravelStr str
|
adamc@34
|
457 in
|
adamc@34
|
458 (x, ms @ [m])
|
adamc@34
|
459 end
|
adamc@34
|
460 | _ => raise Fail "unravelStr"
|
adamc@34
|
461
|
adamc@59
|
462 fun sgnS_con (str, (sgns, strs, cons)) c =
|
adamc@34
|
463 case c of
|
adamc@34
|
464 CModProj (m1, ms, x) =>
|
adamc@34
|
465 (case IM.find (strs, m1) of
|
adamc@34
|
466 NONE => c
|
adamc@34
|
467 | SOME m1x =>
|
adamc@34
|
468 let
|
adamc@34
|
469 val (m1, ms') = unravelStr str
|
adamc@34
|
470 in
|
adamc@34
|
471 CModProj (m1, ms' @ m1x :: ms, x)
|
adamc@34
|
472 end)
|
adamc@34
|
473 | CNamed n =>
|
adamc@34
|
474 (case IM.find (cons, n) of
|
adamc@34
|
475 NONE => c
|
adamc@34
|
476 | SOME nx =>
|
adamc@34
|
477 let
|
adamc@34
|
478 val (m1, ms) = unravelStr str
|
adamc@34
|
479 in
|
adamc@34
|
480 CModProj (m1, ms, nx)
|
adamc@34
|
481 end)
|
adamc@34
|
482 | _ => c
|
adamc@34
|
483
|
adamc@59
|
484 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
|
adamc@59
|
485 case sgn of
|
adamc@59
|
486 SgnProj (m1, ms, x) =>
|
adamc@59
|
487 (case IM.find (strs, m1) of
|
adamc@59
|
488 NONE => sgn
|
adamc@59
|
489 | SOME m1x =>
|
adamc@59
|
490 let
|
adamc@59
|
491 val (m1, ms') = unravelStr str
|
adamc@59
|
492 in
|
adamc@59
|
493 SgnProj (m1, ms' @ m1x :: ms, x)
|
adamc@59
|
494 end)
|
adamc@59
|
495 | SgnVar n =>
|
adamc@59
|
496 (case IM.find (sgns, n) of
|
adamc@59
|
497 NONE => sgn
|
adamc@59
|
498 | SOME nx =>
|
adamc@59
|
499 let
|
adamc@59
|
500 val (m1, ms) = unravelStr str
|
adamc@59
|
501 in
|
adamc@59
|
502 SgnProj (m1, ms, nx)
|
adamc@59
|
503 end)
|
adamc@59
|
504 | _ => sgn
|
adamc@59
|
505
|
adamc@59
|
506 fun sgnSubCon x =
|
adamc@34
|
507 ElabUtil.Con.map {kind = id,
|
adamc@59
|
508 con = sgnS_con x}
|
adamc@34
|
509
|
adamc@59
|
510 fun sgnSubSgn x =
|
adamc@34
|
511 ElabUtil.Sgn.map {kind = id,
|
adamc@59
|
512 con = sgnS_con x,
|
adamc@34
|
513 sgn_item = id,
|
adamc@59
|
514 sgn = sgnS_sgn x}
|
adamc@34
|
515
|
adamc@42
|
516 fun hnormSgn env (all as (sgn, loc)) =
|
adamc@34
|
517 case sgn of
|
adamc@42
|
518 SgnError => all
|
adamc@42
|
519 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
|
adamc@42
|
520 | SgnConst _ => all
|
adamc@42
|
521 | SgnFun _ => all
|
adamc@59
|
522 | SgnProj (m, ms, x) =>
|
adamc@59
|
523 let
|
adamc@59
|
524 val (_, sgn) = lookupStrNamed env m
|
adamc@59
|
525 in
|
adamc@59
|
526 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
|
adamc@59
|
527 sgn = sgn,
|
adamc@59
|
528 field = x} of
|
adamc@59
|
529 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
|
adamc@59
|
530 | SOME sgn => sgn
|
adamc@59
|
531 end
|
adamc@42
|
532 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
533 case #1 (hnormSgn env sgn) of
|
adamc@42
|
534 SgnError => (SgnError, loc)
|
adamc@42
|
535 | SgnConst sgis =>
|
adamc@42
|
536 let
|
adamc@42
|
537 fun traverse (pre, post) =
|
adamc@42
|
538 case post of
|
adamc@42
|
539 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
|
adamc@42
|
540 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
|
adamc@42
|
541 if x = x' then
|
adamc@42
|
542 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
|
adamc@42
|
543 else
|
adamc@42
|
544 traverse (sgi :: pre, rest)
|
adamc@42
|
545 | sgi :: rest => traverse (sgi :: pre, rest)
|
adamc@42
|
546
|
adamc@42
|
547 val sgis = traverse ([], sgis)
|
adamc@42
|
548 in
|
adamc@42
|
549 (SgnConst sgis, loc)
|
adamc@42
|
550 end
|
adamc@42
|
551 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
|
adamc@42
|
552
|
adamc@59
|
553 and projectSgn env {sgn, str, field} =
|
adamc@59
|
554 case #1 (hnormSgn env sgn) of
|
adamc@59
|
555 SgnConst sgis =>
|
adamc@59
|
556 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
557 NONE => NONE
|
adamc@59
|
558 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
559 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
560 | _ => NONE
|
adamc@59
|
561
|
adamc@59
|
562 fun projectStr env {sgn, str, field} =
|
adamc@59
|
563 case #1 (hnormSgn env sgn) of
|
adamc@59
|
564 SgnConst sgis =>
|
adamc@59
|
565 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
566 NONE => NONE
|
adamc@59
|
567 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
568 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
569 | _ => NONE
|
adamc@59
|
570
|
adamc@158
|
571 fun chaseMpath env (n, ms) =
|
adamc@158
|
572 let
|
adamc@158
|
573 val (_, sgn) = lookupStrNamed env n
|
adamc@158
|
574 in
|
adamc@158
|
575 foldl (fn (m, (str, sgn)) =>
|
adamc@158
|
576 case projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@158
|
577 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@158
|
578 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
|
adamc@158
|
579 ((StrVar n, #2 sgn), sgn) ms
|
adamc@158
|
580 end
|
adamc@158
|
581
|
adamc@42
|
582 fun projectCon env {sgn, str, field} =
|
adamc@42
|
583 case #1 (hnormSgn env sgn) of
|
adamc@34
|
584 SgnConst sgis =>
|
adamc@34
|
585 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
|
adamc@34
|
586 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
|
adamc@191
|
587 | SgiDatatype (x, _, _, _) => if x = field then SOME ((KType, #2 sgn), NONE) else NONE
|
adamc@191
|
588 | SgiDatatypeImp (x, _, m1, ms, x', _, _) =>
|
adamc@158
|
589 if x = field then
|
adamc@158
|
590 SOME ((KType, #2 sgn), SOME (CModProj (m1, ms, x'), #2 sgn))
|
adamc@158
|
591 else
|
adamc@158
|
592 NONE
|
adamc@34
|
593 | _ => NONE) sgis of
|
adamc@34
|
594 NONE => NONE
|
adamc@34
|
595 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
|
adamc@34
|
596 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
|
adamc@42
|
597 | _ => NONE
|
adamc@34
|
598
|
adamc@157
|
599 fun projectDatatype env {sgn, str, field} =
|
adamc@157
|
600 case #1 (hnormSgn env sgn) of
|
adamc@157
|
601 SgnConst sgis =>
|
adamc@191
|
602 (case sgnSeek (fn SgiDatatype (x, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
|
adamc@191
|
603 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
|
adamc@157
|
604 | _ => NONE) sgis of
|
adamc@157
|
605 NONE => NONE
|
adamc@191
|
606 | SOME ((xs, xncs), subs) => SOME (xs,
|
adamc@191
|
607 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
|
adamc@157
|
608 | _ => NONE
|
adamc@157
|
609
|
adamc@174
|
610 fun projectConstructor env {sgn, str, field} =
|
adamc@174
|
611 case #1 (hnormSgn env sgn) of
|
adamc@174
|
612 SgnConst sgis =>
|
adamc@174
|
613 let
|
adamc@191
|
614 fun consider (n, xs, xncs) =
|
adamc@174
|
615 ListUtil.search (fn (x, n', to) =>
|
adamc@174
|
616 if x <> field then
|
adamc@174
|
617 NONE
|
adamc@174
|
618 else
|
adamc@191
|
619 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
|
adamc@174
|
620 in
|
adamc@191
|
621 case sgnSeek (fn SgiDatatype (_, n, xs, xncs) => consider (n, xs, xncs)
|
adamc@191
|
622 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
|
adamc@174
|
623 | _ => NONE) sgis of
|
adamc@174
|
624 NONE => NONE
|
adamc@191
|
625 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
|
adamc@191
|
626 sgnSubCon (str, subs) t)
|
adamc@174
|
627 end
|
adamc@174
|
628 | _ => NONE
|
adamc@174
|
629
|
adamc@42
|
630 fun projectVal env {sgn, str, field} =
|
adamc@42
|
631 case #1 (hnormSgn env sgn) of
|
adamc@34
|
632 SgnConst sgis =>
|
adamc@162
|
633 let
|
adamc@191
|
634 fun seek (n, xs, xncs) =
|
adamc@162
|
635 ListUtil.search (fn (x, _, to) =>
|
adamc@162
|
636 if x = field then
|
adamc@191
|
637 SOME (let
|
adamc@191
|
638 val t =
|
adamc@191
|
639 case to of
|
adamc@191
|
640 NONE => (CNamed n, #2 sgn)
|
adamc@191
|
641 | SOME t => (TFun (t, (CNamed n, #2 sgn)), #2 sgn)
|
adamc@191
|
642 val k = (KType, #2 sgn)
|
adamc@191
|
643 in
|
adamc@191
|
644 foldr (fn (x, t) => (TCFun (Explicit, x, k, t), #2 sgn))
|
adamc@191
|
645 t xs
|
adamc@191
|
646 end)
|
adamc@162
|
647 else
|
adamc@162
|
648 NONE) xncs
|
adamc@162
|
649 in
|
adamc@162
|
650 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
|
adamc@191
|
651 | SgiDatatype (_, n, xs, xncs) => seek (n, xs, xncs)
|
adamc@191
|
652 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
|
adamc@162
|
653 | _ => NONE) sgis of
|
adamc@162
|
654 NONE => NONE
|
adamc@162
|
655 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
|
adamc@162
|
656 end
|
adamc@34
|
657 | SgnError => SOME (CError, ErrorMsg.dummySpan)
|
adamc@42
|
658 | _ => NONE
|
adamc@34
|
659
|
adamc@88
|
660 fun sgnSeekConstraints (str, sgis) =
|
adamc@88
|
661 let
|
adamc@88
|
662 fun seek (sgis, sgns, strs, cons, acc) =
|
adamc@88
|
663 case sgis of
|
adamc@88
|
664 [] => acc
|
adamc@88
|
665 | (sgi, _) :: sgis =>
|
adamc@88
|
666 case sgi of
|
adamc@88
|
667 SgiConstraint (c1, c2) =>
|
adamc@88
|
668 let
|
adamc@88
|
669 val sub = sgnSubCon (str, (sgns, strs, cons))
|
adamc@88
|
670 in
|
adamc@88
|
671 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
|
adamc@88
|
672 end
|
adamc@88
|
673 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
674 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@191
|
675 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@191
|
676 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
677 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@88
|
678 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
|
adamc@88
|
679 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
|
adamc@203
|
680 | SgiTable _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@88
|
681 in
|
adamc@88
|
682 seek (sgis, IM.empty, IM.empty, IM.empty, [])
|
adamc@88
|
683 end
|
adamc@88
|
684
|
adamc@88
|
685 fun projectConstraints env {sgn, str} =
|
adamc@88
|
686 case #1 (hnormSgn env sgn) of
|
adamc@88
|
687 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
|
adamc@88
|
688 | SgnError => SOME []
|
adamc@88
|
689 | _ => NONE
|
adamc@34
|
690
|
adamc@157
|
691 fun declBinds env (d, loc) =
|
adamc@157
|
692 case d of
|
adamc@157
|
693 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@191
|
694 | DDatatype (x, n, xs, xncs) =>
|
adamc@157
|
695 let
|
adamc@157
|
696 val env = pushCNamedAs env x n (KType, loc) NONE
|
adamc@191
|
697 val env = pushDatatype env n xs xncs
|
adamc@157
|
698 in
|
adamc@191
|
699 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
700 let
|
adamc@191
|
701 val t =
|
adamc@191
|
702 case to of
|
adamc@191
|
703 NONE => (CNamed n, loc)
|
adamc@191
|
704 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@191
|
705 val k = (KType, loc)
|
adamc@191
|
706 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@191
|
707 in
|
adamc@191
|
708 pushENamedAs env x' n' t
|
adamc@191
|
709 end)
|
adamc@191
|
710 env xncs
|
adamc@157
|
711 end
|
adamc@191
|
712 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
|
adamc@157
|
713 let
|
adamc@157
|
714 val t = (CModProj (m, ms, x'), loc)
|
adamc@157
|
715 val env = pushCNamedAs env x n (KType, loc) (SOME t)
|
adamc@191
|
716 val env = pushDatatype env n xs xncs
|
adamc@157
|
717
|
adamc@157
|
718 val t = (CNamed n, loc)
|
adamc@157
|
719 in
|
adamc@191
|
720 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
721 let
|
adamc@191
|
722 val t =
|
adamc@191
|
723 case to of
|
adamc@191
|
724 NONE => (CNamed n, loc)
|
adamc@191
|
725 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@191
|
726 val k = (KType, loc)
|
adamc@191
|
727 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@191
|
728 in
|
adamc@191
|
729 pushENamedAs env x' n' t
|
adamc@191
|
730 end)
|
adamc@191
|
731 env xncs
|
adamc@157
|
732 end
|
adamc@157
|
733 | DVal (x, n, t, _) => pushENamedAs env x n t
|
adamc@157
|
734 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
|
adamc@157
|
735 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@157
|
736 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
|
adamc@157
|
737 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@157
|
738 | DConstraint _ => env
|
adamc@157
|
739 | DExport _ => env
|
adamc@203
|
740 | DTable (x, n, c) =>
|
adamc@203
|
741 (case lookupStr env "Basis" of
|
adamc@203
|
742 NONE => raise Fail "ElabEnv.declBinds: Can't find Basis"
|
adamc@203
|
743 | SOME (n, _) =>
|
adamc@203
|
744 let
|
adamc@203
|
745 val t = (CApp ((CModProj (n, [], "table"), loc), c), loc)
|
adamc@203
|
746 in
|
adamc@203
|
747 pushENamedAs env x n t
|
adamc@203
|
748 end)
|
adamc@157
|
749
|
adamc@2
|
750 end
|