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@157
|
76 type datatyp = (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@157
|
84
|
adamc@9
|
85 renameE : con var' SM.map,
|
adamc@9
|
86 relE : (string * con) list,
|
adamc@31
|
87 namedE : (string * con) IM.map,
|
adamc@31
|
88
|
adamc@31
|
89 renameSgn : (int * sgn) SM.map,
|
adamc@31
|
90 sgn : (string * sgn) IM.map,
|
adamc@31
|
91
|
adamc@31
|
92 renameStr : (int * sgn) SM.map,
|
adamc@31
|
93 str : (string * sgn) IM.map
|
adamc@2
|
94 }
|
adamc@2
|
95
|
adamc@2
|
96 val namedCounter = ref 0
|
adamc@2
|
97
|
adamc@109
|
98 fun newNamed () =
|
adamc@109
|
99 let
|
adamc@109
|
100 val r = !namedCounter
|
adamc@109
|
101 in
|
adamc@109
|
102 namedCounter := r + 1;
|
adamc@109
|
103 r
|
adamc@109
|
104 end
|
adamc@109
|
105
|
adamc@2
|
106 val empty = {
|
adamc@2
|
107 renameC = SM.empty,
|
adamc@2
|
108 relC = [],
|
adamc@9
|
109 namedC = IM.empty,
|
adamc@9
|
110
|
adamc@157
|
111 datatypes = IM.empty,
|
adamc@157
|
112
|
adamc@9
|
113 renameE = SM.empty,
|
adamc@9
|
114 relE = [],
|
adamc@31
|
115 namedE = IM.empty,
|
adamc@31
|
116
|
adamc@31
|
117 renameSgn = SM.empty,
|
adamc@31
|
118 sgn = IM.empty,
|
adamc@31
|
119
|
adamc@31
|
120 renameStr = SM.empty,
|
adamc@31
|
121 str = IM.empty
|
adamc@2
|
122 }
|
adamc@2
|
123
|
adamc@2
|
124 fun pushCRel (env : env) x k =
|
adamc@2
|
125 let
|
adamc@9
|
126 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
|
adamc@2
|
127 | x => x) (#renameC env)
|
adamc@2
|
128 in
|
adamc@9
|
129 {renameC = SM.insert (renameC, x, Rel' (0, k)),
|
adamc@2
|
130 relC = (x, k) :: #relC env,
|
adamc@13
|
131 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
|
adamc@9
|
132
|
adamc@157
|
133 datatypes = #datatypes env,
|
adamc@157
|
134
|
adamc@9
|
135 renameE = #renameE env,
|
adamc@13
|
136 relE = map (fn (x, c) => (x, lift c)) (#relE env),
|
adamc@31
|
137 namedE = IM.map (fn (x, c) => (x, lift c)) (#namedE env),
|
adamc@31
|
138
|
adamc@31
|
139 renameSgn = #renameSgn env,
|
adamc@31
|
140 sgn = #sgn env,
|
adamc@31
|
141
|
adamc@31
|
142 renameStr = #renameStr env,
|
adamc@31
|
143 str = #str env
|
adamc@31
|
144 }
|
adamc@2
|
145 end
|
adamc@2
|
146
|
adamc@2
|
147 fun lookupCRel (env : env) n =
|
adamc@2
|
148 (List.nth (#relC env, n))
|
adamc@2
|
149 handle Subscript => raise UnboundRel n
|
adamc@2
|
150
|
adamc@11
|
151 fun pushCNamedAs (env : env) x n k co =
|
adamc@9
|
152 {renameC = SM.insert (#renameC env, x, Named' (n, k)),
|
adamc@5
|
153 relC = #relC env,
|
adamc@11
|
154 namedC = IM.insert (#namedC env, n, (x, k, co)),
|
adamc@9
|
155
|
adamc@157
|
156 datatypes = #datatypes env,
|
adamc@157
|
157
|
adamc@9
|
158 renameE = #renameE env,
|
adamc@9
|
159 relE = #relE env,
|
adamc@31
|
160 namedE = #namedE env,
|
adamc@31
|
161
|
adamc@31
|
162 renameSgn = #renameSgn env,
|
adamc@31
|
163 sgn = #sgn env,
|
adamc@31
|
164
|
adamc@31
|
165 renameStr = #renameStr env,
|
adamc@31
|
166 str = #str env}
|
adamc@5
|
167
|
adamc@11
|
168 fun pushCNamed env x k co =
|
adamc@2
|
169 let
|
adamc@2
|
170 val n = !namedCounter
|
adamc@2
|
171 in
|
adamc@2
|
172 namedCounter := n + 1;
|
adamc@11
|
173 (pushCNamedAs env x n k co, n)
|
adamc@2
|
174 end
|
adamc@2
|
175
|
adamc@2
|
176 fun lookupCNamed (env : env) n =
|
adamc@2
|
177 case IM.find (#namedC env, n) of
|
adamc@2
|
178 NONE => raise UnboundNamed n
|
adamc@2
|
179 | SOME x => x
|
adamc@2
|
180
|
adamc@2
|
181 fun lookupC (env : env) x =
|
adamc@2
|
182 case SM.find (#renameC env, x) of
|
adamc@9
|
183 NONE => NotBound
|
adamc@9
|
184 | SOME (Rel' x) => Rel x
|
adamc@9
|
185 | SOME (Named' x) => Named x
|
adamc@9
|
186
|
adamc@157
|
187 fun pushDatatype (env : env) n xncs =
|
adamc@157
|
188 {renameC = #renameC env,
|
adamc@157
|
189 relC = #relC env,
|
adamc@157
|
190 namedC = #namedC env,
|
adamc@157
|
191
|
adamc@157
|
192 datatypes = IM.insert (#datatypes env, n,
|
adamc@157
|
193 foldl (fn ((x, n, to), cons) =>
|
adamc@157
|
194 IM.insert (cons, n, (x, to))) IM.empty xncs),
|
adamc@157
|
195
|
adamc@157
|
196 renameE = #renameE env,
|
adamc@157
|
197 relE = #relE env,
|
adamc@157
|
198 namedE = #namedE env,
|
adamc@157
|
199
|
adamc@157
|
200 renameSgn = #renameSgn env,
|
adamc@157
|
201 sgn = #sgn env,
|
adamc@157
|
202
|
adamc@157
|
203 renameStr = #renameStr env,
|
adamc@157
|
204 str = #str env}
|
adamc@157
|
205
|
adamc@157
|
206 fun lookupDatatype (env : env) n =
|
adamc@157
|
207 case IM.find (#datatypes env, n) of
|
adamc@157
|
208 NONE => raise UnboundNamed n
|
adamc@157
|
209 | SOME x => x
|
adamc@157
|
210
|
adamc@157
|
211 fun lookupConstructor dt n =
|
adamc@157
|
212 case IM.find (dt, n) of
|
adamc@157
|
213 NONE => raise UnboundNamed n
|
adamc@157
|
214 | SOME x => x
|
adamc@157
|
215
|
adamc@157
|
216 fun constructors dt = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
|
adamc@157
|
217
|
adamc@9
|
218 fun pushERel (env : env) x t =
|
adamc@9
|
219 let
|
adamc@9
|
220 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
|
adamc@9
|
221 | x => x) (#renameE env)
|
adamc@9
|
222 in
|
adamc@9
|
223 {renameC = #renameC env,
|
adamc@9
|
224 relC = #relC env,
|
adamc@9
|
225 namedC = #namedC env,
|
adamc@9
|
226
|
adamc@157
|
227 datatypes = #datatypes env,
|
adamc@157
|
228
|
adamc@9
|
229 renameE = SM.insert (renameE, x, Rel' (0, t)),
|
adamc@9
|
230 relE = (x, t) :: #relE env,
|
adamc@31
|
231 namedE = #namedE env,
|
adamc@31
|
232
|
adamc@31
|
233 renameSgn = #renameSgn env,
|
adamc@31
|
234 sgn = #sgn env,
|
adamc@31
|
235
|
adamc@31
|
236 renameStr = #renameStr env,
|
adamc@31
|
237 str = #str env}
|
adamc@9
|
238 end
|
adamc@9
|
239
|
adamc@9
|
240 fun lookupERel (env : env) n =
|
adamc@9
|
241 (List.nth (#relE env, n))
|
adamc@9
|
242 handle Subscript => raise UnboundRel n
|
adamc@9
|
243
|
adamc@9
|
244 fun pushENamedAs (env : env) x n t =
|
adamc@9
|
245 {renameC = #renameC env,
|
adamc@9
|
246 relC = #relC env,
|
adamc@9
|
247 namedC = #namedC env,
|
adamc@9
|
248
|
adamc@157
|
249 datatypes = #datatypes env,
|
adamc@157
|
250
|
adamc@9
|
251 renameE = SM.insert (#renameE env, x, Named' (n, t)),
|
adamc@9
|
252 relE = #relE env,
|
adamc@31
|
253 namedE = IM.insert (#namedE env, n, (x, t)),
|
adamc@31
|
254
|
adamc@31
|
255 renameSgn = #renameSgn env,
|
adamc@31
|
256 sgn = #sgn env,
|
adamc@31
|
257
|
adamc@31
|
258 renameStr = #renameStr env,
|
adamc@31
|
259 str = #str env}
|
adamc@9
|
260
|
adamc@9
|
261 fun pushENamed env x t =
|
adamc@9
|
262 let
|
adamc@9
|
263 val n = !namedCounter
|
adamc@9
|
264 in
|
adamc@9
|
265 namedCounter := n + 1;
|
adamc@9
|
266 (pushENamedAs env x n t, n)
|
adamc@9
|
267 end
|
adamc@9
|
268
|
adamc@9
|
269 fun lookupENamed (env : env) n =
|
adamc@9
|
270 case IM.find (#namedE env, n) of
|
adamc@9
|
271 NONE => raise UnboundNamed n
|
adamc@9
|
272 | SOME x => x
|
adamc@9
|
273
|
adamc@9
|
274 fun lookupE (env : env) x =
|
adamc@9
|
275 case SM.find (#renameE env, x) of
|
adamc@9
|
276 NONE => NotBound
|
adamc@9
|
277 | SOME (Rel' x) => Rel x
|
adamc@9
|
278 | SOME (Named' x) => Named x
|
adamc@2
|
279
|
adamc@31
|
280 fun pushSgnNamedAs (env : env) x n sgis =
|
adamc@31
|
281 {renameC = #renameC env,
|
adamc@31
|
282 relC = #relC env,
|
adamc@31
|
283 namedC = #namedC env,
|
adamc@31
|
284
|
adamc@157
|
285 datatypes = #datatypes env,
|
adamc@157
|
286
|
adamc@31
|
287 renameE = #renameE env,
|
adamc@31
|
288 relE = #relE env,
|
adamc@31
|
289 namedE = #namedE env,
|
adamc@31
|
290
|
adamc@31
|
291 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
|
adamc@31
|
292 sgn = IM.insert (#sgn env, n, (x, sgis)),
|
adamc@31
|
293
|
adamc@31
|
294 renameStr = #renameStr env,
|
adamc@31
|
295 str = #str env}
|
adamc@31
|
296
|
adamc@31
|
297 fun pushSgnNamed env x sgis =
|
adamc@31
|
298 let
|
adamc@31
|
299 val n = !namedCounter
|
adamc@31
|
300 in
|
adamc@31
|
301 namedCounter := n + 1;
|
adamc@31
|
302 (pushSgnNamedAs env x n sgis, n)
|
adamc@31
|
303 end
|
adamc@31
|
304
|
adamc@31
|
305 fun lookupSgnNamed (env : env) n =
|
adamc@31
|
306 case IM.find (#sgn env, n) of
|
adamc@31
|
307 NONE => raise UnboundNamed n
|
adamc@31
|
308 | SOME x => x
|
adamc@31
|
309
|
adamc@31
|
310 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
|
adamc@31
|
311
|
adamc@31
|
312 fun pushStrNamedAs (env : env) x n sgis =
|
adamc@31
|
313 {renameC = #renameC env,
|
adamc@31
|
314 relC = #relC env,
|
adamc@31
|
315 namedC = #namedC env,
|
adamc@31
|
316
|
adamc@157
|
317 datatypes = #datatypes env,
|
adamc@157
|
318
|
adamc@31
|
319 renameE = #renameE env,
|
adamc@31
|
320 relE = #relE env,
|
adamc@31
|
321 namedE = #namedE env,
|
adamc@31
|
322
|
adamc@31
|
323 renameSgn = #renameSgn env,
|
adamc@31
|
324 sgn = #sgn env,
|
adamc@31
|
325
|
adamc@31
|
326 renameStr = SM.insert (#renameStr env, x, (n, sgis)),
|
adamc@31
|
327 str = IM.insert (#str env, n, (x, sgis))}
|
adamc@31
|
328
|
adamc@31
|
329 fun pushStrNamed env x sgis =
|
adamc@31
|
330 let
|
adamc@31
|
331 val n = !namedCounter
|
adamc@31
|
332 in
|
adamc@31
|
333 namedCounter := n + 1;
|
adamc@31
|
334 (pushStrNamedAs env x n sgis, n)
|
adamc@31
|
335 end
|
adamc@31
|
336
|
adamc@31
|
337 fun lookupStrNamed (env : env) n =
|
adamc@31
|
338 case IM.find (#str env, n) of
|
adamc@31
|
339 NONE => raise UnboundNamed n
|
adamc@31
|
340 | SOME x => x
|
adamc@31
|
341
|
adamc@31
|
342 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
|
adamc@31
|
343
|
adamc@156
|
344 fun sgiBinds env (sgi, loc) =
|
adamc@31
|
345 case sgi of
|
adamc@31
|
346 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
|
adamc@31
|
347 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@156
|
348 | SgiDatatype (x, n, xncs) =>
|
adamc@156
|
349 let
|
adamc@156
|
350 val env = pushCNamedAs env x n (KType, loc) NONE
|
adamc@156
|
351 in
|
adamc@156
|
352 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' (CNamed n, loc)
|
adamc@156
|
353 | ((x', n', SOME t), env) => pushENamedAs env x' n' (TFun (t, (CNamed n, loc)), loc))
|
adamc@156
|
354 env xncs
|
adamc@156
|
355 end
|
adamc@156
|
356 | SgiDatatypeImp (x, n, m1, ms, x') => pushCNamedAs env x n (KType, loc) (SOME (CModProj (m1, ms, x'), loc))
|
adamc@31
|
357 | SgiVal (x, n, t) => pushENamedAs env x n t
|
adamc@31
|
358 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@59
|
359 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@88
|
360 | SgiConstraint _ => env
|
adamc@31
|
361
|
adamc@34
|
362 fun sgnSeek f sgis =
|
adamc@34
|
363 let
|
adamc@59
|
364 fun seek (sgis, sgns, strs, cons) =
|
adamc@34
|
365 case sgis of
|
adamc@34
|
366 [] => NONE
|
adamc@34
|
367 | (sgi, _) :: sgis =>
|
adamc@34
|
368 case f sgi of
|
adamc@59
|
369 SOME v => SOME (v, (sgns, strs, cons))
|
adamc@88
|
370 | NONE =>
|
adamc@88
|
371 case sgi of
|
adamc@88
|
372 SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@88
|
373 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@156
|
374 | SgiDatatype (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@156
|
375 | SgiDatatypeImp (x, n, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@88
|
376 | SgiVal _ => seek (sgis, sgns, strs, cons)
|
adamc@88
|
377 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons)
|
adamc@88
|
378 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons)
|
adamc@88
|
379 | SgiConstraint _ => seek (sgis, sgns, strs, cons)
|
adamc@34
|
380 in
|
adamc@59
|
381 seek (sgis, IM.empty, IM.empty, IM.empty)
|
adamc@34
|
382 end
|
adamc@34
|
383
|
adamc@34
|
384 fun id x = x
|
adamc@34
|
385
|
adamc@34
|
386 fun unravelStr (str, _) =
|
adamc@34
|
387 case str of
|
adamc@34
|
388 StrVar x => (x, [])
|
adamc@34
|
389 | StrProj (str, m) =>
|
adamc@34
|
390 let
|
adamc@34
|
391 val (x, ms) = unravelStr str
|
adamc@34
|
392 in
|
adamc@34
|
393 (x, ms @ [m])
|
adamc@34
|
394 end
|
adamc@34
|
395 | _ => raise Fail "unravelStr"
|
adamc@34
|
396
|
adamc@59
|
397 fun sgnS_con (str, (sgns, strs, cons)) c =
|
adamc@34
|
398 case c of
|
adamc@34
|
399 CModProj (m1, ms, x) =>
|
adamc@34
|
400 (case IM.find (strs, m1) of
|
adamc@34
|
401 NONE => c
|
adamc@34
|
402 | SOME m1x =>
|
adamc@34
|
403 let
|
adamc@34
|
404 val (m1, ms') = unravelStr str
|
adamc@34
|
405 in
|
adamc@34
|
406 CModProj (m1, ms' @ m1x :: ms, x)
|
adamc@34
|
407 end)
|
adamc@34
|
408 | CNamed n =>
|
adamc@34
|
409 (case IM.find (cons, n) of
|
adamc@34
|
410 NONE => c
|
adamc@34
|
411 | SOME nx =>
|
adamc@34
|
412 let
|
adamc@34
|
413 val (m1, ms) = unravelStr str
|
adamc@34
|
414 in
|
adamc@34
|
415 CModProj (m1, ms, nx)
|
adamc@34
|
416 end)
|
adamc@34
|
417 | _ => c
|
adamc@34
|
418
|
adamc@59
|
419 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
|
adamc@59
|
420 case sgn of
|
adamc@59
|
421 SgnProj (m1, ms, x) =>
|
adamc@59
|
422 (case IM.find (strs, m1) of
|
adamc@59
|
423 NONE => sgn
|
adamc@59
|
424 | SOME m1x =>
|
adamc@59
|
425 let
|
adamc@59
|
426 val (m1, ms') = unravelStr str
|
adamc@59
|
427 in
|
adamc@59
|
428 SgnProj (m1, ms' @ m1x :: ms, x)
|
adamc@59
|
429 end)
|
adamc@59
|
430 | SgnVar n =>
|
adamc@59
|
431 (case IM.find (sgns, n) of
|
adamc@59
|
432 NONE => sgn
|
adamc@59
|
433 | SOME nx =>
|
adamc@59
|
434 let
|
adamc@59
|
435 val (m1, ms) = unravelStr str
|
adamc@59
|
436 in
|
adamc@59
|
437 SgnProj (m1, ms, nx)
|
adamc@59
|
438 end)
|
adamc@59
|
439 | _ => sgn
|
adamc@59
|
440
|
adamc@59
|
441 fun sgnSubCon x =
|
adamc@34
|
442 ElabUtil.Con.map {kind = id,
|
adamc@59
|
443 con = sgnS_con x}
|
adamc@34
|
444
|
adamc@59
|
445 fun sgnSubSgn x =
|
adamc@34
|
446 ElabUtil.Sgn.map {kind = id,
|
adamc@59
|
447 con = sgnS_con x,
|
adamc@34
|
448 sgn_item = id,
|
adamc@59
|
449 sgn = sgnS_sgn x}
|
adamc@34
|
450
|
adamc@42
|
451 fun hnormSgn env (all as (sgn, loc)) =
|
adamc@34
|
452 case sgn of
|
adamc@42
|
453 SgnError => all
|
adamc@42
|
454 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
|
adamc@42
|
455 | SgnConst _ => all
|
adamc@42
|
456 | SgnFun _ => all
|
adamc@59
|
457 | SgnProj (m, ms, x) =>
|
adamc@59
|
458 let
|
adamc@59
|
459 val (_, sgn) = lookupStrNamed env m
|
adamc@59
|
460 in
|
adamc@59
|
461 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
|
adamc@59
|
462 sgn = sgn,
|
adamc@59
|
463 field = x} of
|
adamc@59
|
464 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
|
adamc@59
|
465 | SOME sgn => sgn
|
adamc@59
|
466 end
|
adamc@42
|
467 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
468 case #1 (hnormSgn env sgn) of
|
adamc@42
|
469 SgnError => (SgnError, loc)
|
adamc@42
|
470 | SgnConst sgis =>
|
adamc@42
|
471 let
|
adamc@42
|
472 fun traverse (pre, post) =
|
adamc@42
|
473 case post of
|
adamc@42
|
474 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
|
adamc@42
|
475 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
|
adamc@42
|
476 if x = x' then
|
adamc@42
|
477 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
|
adamc@42
|
478 else
|
adamc@42
|
479 traverse (sgi :: pre, rest)
|
adamc@42
|
480 | sgi :: rest => traverse (sgi :: pre, rest)
|
adamc@42
|
481
|
adamc@42
|
482 val sgis = traverse ([], sgis)
|
adamc@42
|
483 in
|
adamc@42
|
484 (SgnConst sgis, loc)
|
adamc@42
|
485 end
|
adamc@42
|
486 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
|
adamc@42
|
487
|
adamc@59
|
488 and projectSgn env {sgn, str, field} =
|
adamc@59
|
489 case #1 (hnormSgn env sgn) of
|
adamc@59
|
490 SgnConst sgis =>
|
adamc@59
|
491 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
492 NONE => NONE
|
adamc@59
|
493 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
494 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
495 | _ => NONE
|
adamc@59
|
496
|
adamc@59
|
497 fun projectStr env {sgn, str, field} =
|
adamc@59
|
498 case #1 (hnormSgn env sgn) of
|
adamc@59
|
499 SgnConst sgis =>
|
adamc@59
|
500 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
501 NONE => NONE
|
adamc@59
|
502 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
503 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
504 | _ => NONE
|
adamc@59
|
505
|
adamc@42
|
506 fun projectCon env {sgn, str, field} =
|
adamc@42
|
507 case #1 (hnormSgn env sgn) of
|
adamc@34
|
508 SgnConst sgis =>
|
adamc@34
|
509 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
|
adamc@34
|
510 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
|
adamc@157
|
511 | SgiDatatype (x, _, _) => if x = field then SOME ((KType, #2 sgn), NONE) else NONE
|
adamc@34
|
512 | _ => NONE) sgis of
|
adamc@34
|
513 NONE => NONE
|
adamc@34
|
514 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
|
adamc@34
|
515 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
|
adamc@42
|
516 | _ => NONE
|
adamc@34
|
517
|
adamc@157
|
518 fun projectDatatype env {sgn, str, field} =
|
adamc@157
|
519 case #1 (hnormSgn env sgn) of
|
adamc@157
|
520 SgnConst sgis =>
|
adamc@157
|
521 (case sgnSeek (fn SgiDatatype (x, _, xncs) => if x = field then SOME xncs else NONE
|
adamc@157
|
522 | _ => NONE) sgis of
|
adamc@157
|
523 NONE => NONE
|
adamc@157
|
524 | SOME (xncs, subs) => SOME (map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
|
adamc@157
|
525 | _ => NONE
|
adamc@157
|
526
|
adamc@42
|
527 fun projectVal env {sgn, str, field} =
|
adamc@42
|
528 case #1 (hnormSgn env sgn) of
|
adamc@34
|
529 SgnConst sgis =>
|
adamc@34
|
530 (case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE | _ => NONE) sgis of
|
adamc@34
|
531 NONE => NONE
|
adamc@34
|
532 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c))
|
adamc@34
|
533 | SgnError => SOME (CError, ErrorMsg.dummySpan)
|
adamc@42
|
534 | _ => NONE
|
adamc@34
|
535
|
adamc@88
|
536 fun sgnSeekConstraints (str, sgis) =
|
adamc@88
|
537 let
|
adamc@88
|
538 fun seek (sgis, sgns, strs, cons, acc) =
|
adamc@88
|
539 case sgis of
|
adamc@88
|
540 [] => acc
|
adamc@88
|
541 | (sgi, _) :: sgis =>
|
adamc@88
|
542 case sgi of
|
adamc@88
|
543 SgiConstraint (c1, c2) =>
|
adamc@88
|
544 let
|
adamc@88
|
545 val sub = sgnSubCon (str, (sgns, strs, cons))
|
adamc@88
|
546 in
|
adamc@88
|
547 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
|
adamc@88
|
548 end
|
adamc@88
|
549 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
550 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@156
|
551 | SgiDatatype (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@156
|
552 | SgiDatatypeImp (x, n, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
553 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@88
|
554 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
|
adamc@88
|
555 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
|
adamc@88
|
556 in
|
adamc@88
|
557 seek (sgis, IM.empty, IM.empty, IM.empty, [])
|
adamc@88
|
558 end
|
adamc@88
|
559
|
adamc@88
|
560 fun projectConstraints env {sgn, str} =
|
adamc@88
|
561 case #1 (hnormSgn env sgn) of
|
adamc@88
|
562 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
|
adamc@88
|
563 | SgnError => SOME []
|
adamc@88
|
564 | _ => NONE
|
adamc@34
|
565
|
adamc@157
|
566 fun declBinds env (d, loc) =
|
adamc@157
|
567 case d of
|
adamc@157
|
568 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@157
|
569 | DDatatype (x, n, xncs) =>
|
adamc@157
|
570 let
|
adamc@157
|
571 val env = pushCNamedAs env x n (KType, loc) NONE
|
adamc@157
|
572 in
|
adamc@157
|
573 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' (CNamed n, loc)
|
adamc@157
|
574 | ((x', n', SOME t), env) => pushENamedAs env x' n' (TFun (t, (CNamed n, loc)), loc))
|
adamc@157
|
575 env xncs
|
adamc@157
|
576 end
|
adamc@157
|
577 | DDatatypeImp (x, n, m, ms, x') =>
|
adamc@157
|
578 let
|
adamc@157
|
579 val t = (CModProj (m, ms, x'), loc)
|
adamc@157
|
580 val env = pushCNamedAs env x n (KType, loc) (SOME t)
|
adamc@157
|
581 val (_, sgn) = lookupStrNamed env m
|
adamc@157
|
582 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@157
|
583 case projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@157
|
584 NONE => raise Fail "ElabEnv.declBinds: Couldn't projectStr"
|
adamc@157
|
585 | SOME sgn => ((StrProj (str, m), loc), sgn))
|
adamc@157
|
586 ((StrVar m, loc), sgn) ms
|
adamc@157
|
587 val xncs = case projectDatatype env {sgn = sgn, str = str, field = x'} of
|
adamc@157
|
588 NONE => raise Fail "ElabEnv.declBinds: Couldn't projectDatatype"
|
adamc@157
|
589 | SOME xncs => xncs
|
adamc@157
|
590
|
adamc@157
|
591 val t = (CNamed n, loc)
|
adamc@157
|
592 in
|
adamc@157
|
593 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' t
|
adamc@157
|
594 | ((x', n', SOME t'), env) => pushENamedAs env x' n' (TFun (t', t), loc))
|
adamc@157
|
595 env xncs
|
adamc@157
|
596 end
|
adamc@157
|
597 | DVal (x, n, t, _) => pushENamedAs env x n t
|
adamc@157
|
598 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
|
adamc@157
|
599 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@157
|
600 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
|
adamc@157
|
601 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@157
|
602 | DConstraint _ => env
|
adamc@157
|
603 | DExport _ => env
|
adamc@157
|
604
|
adamc@2
|
605 end
|