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@162
|
356 | SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
|
adamc@162
|
357 let
|
adamc@162
|
358 val env = pushCNamedAs env x n (KType, loc) (SOME (CModProj (m1, ms, x'), loc))
|
adamc@162
|
359 in
|
adamc@162
|
360 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' (CNamed n, loc)
|
adamc@162
|
361 | ((x', n', SOME t), env) => pushENamedAs env x' n' (TFun (t, (CNamed n, loc)), loc))
|
adamc@162
|
362 env xncs
|
adamc@162
|
363 end
|
adamc@31
|
364 | SgiVal (x, n, t) => pushENamedAs env x n t
|
adamc@31
|
365 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@59
|
366 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@88
|
367 | SgiConstraint _ => env
|
adamc@31
|
368
|
adamc@34
|
369 fun sgnSeek f sgis =
|
adamc@34
|
370 let
|
adamc@59
|
371 fun seek (sgis, sgns, strs, cons) =
|
adamc@34
|
372 case sgis of
|
adamc@34
|
373 [] => NONE
|
adamc@34
|
374 | (sgi, _) :: sgis =>
|
adamc@34
|
375 case f sgi of
|
adamc@158
|
376 SOME v =>
|
adamc@158
|
377 let
|
adamc@158
|
378 val cons =
|
adamc@158
|
379 case sgi of
|
adamc@158
|
380 SgiDatatype (x, n, _) => IM.insert (cons, n, x)
|
adamc@162
|
381 | SgiDatatypeImp (x, n, _, _, _, _) => IM.insert (cons, n, x)
|
adamc@158
|
382 | _ => cons
|
adamc@158
|
383 in
|
adamc@158
|
384 SOME (v, (sgns, strs, cons))
|
adamc@158
|
385 end
|
adamc@88
|
386 | NONE =>
|
adamc@88
|
387 case sgi of
|
adamc@88
|
388 SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@88
|
389 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@156
|
390 | SgiDatatype (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@162
|
391 | SgiDatatypeImp (x, n, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
|
adamc@88
|
392 | SgiVal _ => seek (sgis, sgns, strs, cons)
|
adamc@88
|
393 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons)
|
adamc@88
|
394 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons)
|
adamc@88
|
395 | SgiConstraint _ => seek (sgis, sgns, strs, cons)
|
adamc@34
|
396 in
|
adamc@59
|
397 seek (sgis, IM.empty, IM.empty, IM.empty)
|
adamc@34
|
398 end
|
adamc@34
|
399
|
adamc@34
|
400 fun id x = x
|
adamc@34
|
401
|
adamc@34
|
402 fun unravelStr (str, _) =
|
adamc@34
|
403 case str of
|
adamc@34
|
404 StrVar x => (x, [])
|
adamc@34
|
405 | StrProj (str, m) =>
|
adamc@34
|
406 let
|
adamc@34
|
407 val (x, ms) = unravelStr str
|
adamc@34
|
408 in
|
adamc@34
|
409 (x, ms @ [m])
|
adamc@34
|
410 end
|
adamc@34
|
411 | _ => raise Fail "unravelStr"
|
adamc@34
|
412
|
adamc@59
|
413 fun sgnS_con (str, (sgns, strs, cons)) c =
|
adamc@34
|
414 case c of
|
adamc@34
|
415 CModProj (m1, ms, x) =>
|
adamc@34
|
416 (case IM.find (strs, m1) of
|
adamc@34
|
417 NONE => c
|
adamc@34
|
418 | SOME m1x =>
|
adamc@34
|
419 let
|
adamc@34
|
420 val (m1, ms') = unravelStr str
|
adamc@34
|
421 in
|
adamc@34
|
422 CModProj (m1, ms' @ m1x :: ms, x)
|
adamc@34
|
423 end)
|
adamc@34
|
424 | CNamed n =>
|
adamc@34
|
425 (case IM.find (cons, n) of
|
adamc@34
|
426 NONE => c
|
adamc@34
|
427 | SOME nx =>
|
adamc@34
|
428 let
|
adamc@34
|
429 val (m1, ms) = unravelStr str
|
adamc@34
|
430 in
|
adamc@34
|
431 CModProj (m1, ms, nx)
|
adamc@34
|
432 end)
|
adamc@34
|
433 | _ => c
|
adamc@34
|
434
|
adamc@59
|
435 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
|
adamc@59
|
436 case sgn of
|
adamc@59
|
437 SgnProj (m1, ms, x) =>
|
adamc@59
|
438 (case IM.find (strs, m1) of
|
adamc@59
|
439 NONE => sgn
|
adamc@59
|
440 | SOME m1x =>
|
adamc@59
|
441 let
|
adamc@59
|
442 val (m1, ms') = unravelStr str
|
adamc@59
|
443 in
|
adamc@59
|
444 SgnProj (m1, ms' @ m1x :: ms, x)
|
adamc@59
|
445 end)
|
adamc@59
|
446 | SgnVar n =>
|
adamc@59
|
447 (case IM.find (sgns, n) of
|
adamc@59
|
448 NONE => sgn
|
adamc@59
|
449 | SOME nx =>
|
adamc@59
|
450 let
|
adamc@59
|
451 val (m1, ms) = unravelStr str
|
adamc@59
|
452 in
|
adamc@59
|
453 SgnProj (m1, ms, nx)
|
adamc@59
|
454 end)
|
adamc@59
|
455 | _ => sgn
|
adamc@59
|
456
|
adamc@59
|
457 fun sgnSubCon x =
|
adamc@34
|
458 ElabUtil.Con.map {kind = id,
|
adamc@59
|
459 con = sgnS_con x}
|
adamc@34
|
460
|
adamc@59
|
461 fun sgnSubSgn x =
|
adamc@34
|
462 ElabUtil.Sgn.map {kind = id,
|
adamc@59
|
463 con = sgnS_con x,
|
adamc@34
|
464 sgn_item = id,
|
adamc@59
|
465 sgn = sgnS_sgn x}
|
adamc@34
|
466
|
adamc@42
|
467 fun hnormSgn env (all as (sgn, loc)) =
|
adamc@34
|
468 case sgn of
|
adamc@42
|
469 SgnError => all
|
adamc@42
|
470 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
|
adamc@42
|
471 | SgnConst _ => all
|
adamc@42
|
472 | SgnFun _ => all
|
adamc@59
|
473 | SgnProj (m, ms, x) =>
|
adamc@59
|
474 let
|
adamc@59
|
475 val (_, sgn) = lookupStrNamed env m
|
adamc@59
|
476 in
|
adamc@59
|
477 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
|
adamc@59
|
478 sgn = sgn,
|
adamc@59
|
479 field = x} of
|
adamc@59
|
480 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
|
adamc@59
|
481 | SOME sgn => sgn
|
adamc@59
|
482 end
|
adamc@42
|
483 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
484 case #1 (hnormSgn env sgn) of
|
adamc@42
|
485 SgnError => (SgnError, loc)
|
adamc@42
|
486 | SgnConst sgis =>
|
adamc@42
|
487 let
|
adamc@42
|
488 fun traverse (pre, post) =
|
adamc@42
|
489 case post of
|
adamc@42
|
490 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
|
adamc@42
|
491 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
|
adamc@42
|
492 if x = x' then
|
adamc@42
|
493 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
|
adamc@42
|
494 else
|
adamc@42
|
495 traverse (sgi :: pre, rest)
|
adamc@42
|
496 | sgi :: rest => traverse (sgi :: pre, rest)
|
adamc@42
|
497
|
adamc@42
|
498 val sgis = traverse ([], sgis)
|
adamc@42
|
499 in
|
adamc@42
|
500 (SgnConst sgis, loc)
|
adamc@42
|
501 end
|
adamc@42
|
502 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
|
adamc@42
|
503
|
adamc@59
|
504 and projectSgn env {sgn, str, field} =
|
adamc@59
|
505 case #1 (hnormSgn env sgn) of
|
adamc@59
|
506 SgnConst sgis =>
|
adamc@59
|
507 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
508 NONE => NONE
|
adamc@59
|
509 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
510 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
511 | _ => NONE
|
adamc@59
|
512
|
adamc@59
|
513 fun projectStr env {sgn, str, field} =
|
adamc@59
|
514 case #1 (hnormSgn env sgn) of
|
adamc@59
|
515 SgnConst sgis =>
|
adamc@59
|
516 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
517 NONE => NONE
|
adamc@59
|
518 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
519 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
520 | _ => NONE
|
adamc@59
|
521
|
adamc@158
|
522 fun chaseMpath env (n, ms) =
|
adamc@158
|
523 let
|
adamc@158
|
524 val (_, sgn) = lookupStrNamed env n
|
adamc@158
|
525 in
|
adamc@158
|
526 foldl (fn (m, (str, sgn)) =>
|
adamc@158
|
527 case projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@158
|
528 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@158
|
529 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
|
adamc@158
|
530 ((StrVar n, #2 sgn), sgn) ms
|
adamc@158
|
531 end
|
adamc@158
|
532
|
adamc@42
|
533 fun projectCon env {sgn, str, field} =
|
adamc@42
|
534 case #1 (hnormSgn env sgn) of
|
adamc@34
|
535 SgnConst sgis =>
|
adamc@34
|
536 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
|
adamc@34
|
537 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
|
adamc@157
|
538 | SgiDatatype (x, _, _) => if x = field then SOME ((KType, #2 sgn), NONE) else NONE
|
adamc@162
|
539 | SgiDatatypeImp (x, _, m1, ms, x', _) =>
|
adamc@158
|
540 if x = field then
|
adamc@158
|
541 SOME ((KType, #2 sgn), SOME (CModProj (m1, ms, x'), #2 sgn))
|
adamc@158
|
542 else
|
adamc@158
|
543 NONE
|
adamc@34
|
544 | _ => NONE) sgis of
|
adamc@34
|
545 NONE => NONE
|
adamc@34
|
546 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
|
adamc@34
|
547 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
|
adamc@42
|
548 | _ => NONE
|
adamc@34
|
549
|
adamc@157
|
550 fun projectDatatype env {sgn, str, field} =
|
adamc@157
|
551 case #1 (hnormSgn env sgn) of
|
adamc@157
|
552 SgnConst sgis =>
|
adamc@157
|
553 (case sgnSeek (fn SgiDatatype (x, _, xncs) => if x = field then SOME xncs else NONE
|
adamc@162
|
554 | SgiDatatypeImp (x, _, _, _, _, xncs) => if x = field then SOME xncs else NONE
|
adamc@157
|
555 | _ => NONE) sgis of
|
adamc@157
|
556 NONE => NONE
|
adamc@157
|
557 | SOME (xncs, subs) => SOME (map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
|
adamc@157
|
558 | _ => NONE
|
adamc@157
|
559
|
adamc@42
|
560 fun projectVal env {sgn, str, field} =
|
adamc@42
|
561 case #1 (hnormSgn env sgn) of
|
adamc@34
|
562 SgnConst sgis =>
|
adamc@162
|
563 let
|
adamc@162
|
564 fun seek (n, xncs) =
|
adamc@162
|
565 ListUtil.search (fn (x, _, to) =>
|
adamc@162
|
566 if x = field then
|
adamc@162
|
567 SOME (case to of
|
adamc@162
|
568 NONE => (CNamed n, #2 sgn)
|
adamc@162
|
569 | SOME t => (TFun (t, (CNamed n, #2 sgn)), #2 sgn))
|
adamc@162
|
570 else
|
adamc@162
|
571 NONE) xncs
|
adamc@162
|
572 in
|
adamc@162
|
573 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
|
adamc@162
|
574 | SgiDatatype (_, n, xncs) => seek (n, xncs)
|
adamc@162
|
575 | SgiDatatypeImp (_, n, _, _, _, xncs) => seek (n, xncs)
|
adamc@162
|
576 | _ => NONE) sgis of
|
adamc@162
|
577 NONE => NONE
|
adamc@162
|
578 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
|
adamc@162
|
579 end
|
adamc@34
|
580 | SgnError => SOME (CError, ErrorMsg.dummySpan)
|
adamc@42
|
581 | _ => NONE
|
adamc@34
|
582
|
adamc@88
|
583 fun sgnSeekConstraints (str, sgis) =
|
adamc@88
|
584 let
|
adamc@88
|
585 fun seek (sgis, sgns, strs, cons, acc) =
|
adamc@88
|
586 case sgis of
|
adamc@88
|
587 [] => acc
|
adamc@88
|
588 | (sgi, _) :: sgis =>
|
adamc@88
|
589 case sgi of
|
adamc@88
|
590 SgiConstraint (c1, c2) =>
|
adamc@88
|
591 let
|
adamc@88
|
592 val sub = sgnSubCon (str, (sgns, strs, cons))
|
adamc@88
|
593 in
|
adamc@88
|
594 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
|
adamc@88
|
595 end
|
adamc@88
|
596 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
597 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@156
|
598 | SgiDatatype (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@162
|
599 | SgiDatatypeImp (x, n, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
600 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@88
|
601 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
|
adamc@88
|
602 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
|
adamc@88
|
603 in
|
adamc@88
|
604 seek (sgis, IM.empty, IM.empty, IM.empty, [])
|
adamc@88
|
605 end
|
adamc@88
|
606
|
adamc@88
|
607 fun projectConstraints env {sgn, str} =
|
adamc@88
|
608 case #1 (hnormSgn env sgn) of
|
adamc@88
|
609 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
|
adamc@88
|
610 | SgnError => SOME []
|
adamc@88
|
611 | _ => NONE
|
adamc@34
|
612
|
adamc@157
|
613 fun declBinds env (d, loc) =
|
adamc@157
|
614 case d of
|
adamc@157
|
615 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@157
|
616 | DDatatype (x, n, xncs) =>
|
adamc@157
|
617 let
|
adamc@157
|
618 val env = pushCNamedAs env x n (KType, loc) NONE
|
adamc@157
|
619 in
|
adamc@157
|
620 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' (CNamed n, loc)
|
adamc@157
|
621 | ((x', n', SOME t), env) => pushENamedAs env x' n' (TFun (t, (CNamed n, loc)), loc))
|
adamc@157
|
622 env xncs
|
adamc@157
|
623 end
|
adamc@162
|
624 | DDatatypeImp (x, n, m, ms, x', xncs) =>
|
adamc@157
|
625 let
|
adamc@157
|
626 val t = (CModProj (m, ms, x'), loc)
|
adamc@157
|
627 val env = pushCNamedAs env x n (KType, loc) (SOME t)
|
adamc@157
|
628
|
adamc@157
|
629 val t = (CNamed n, loc)
|
adamc@157
|
630 in
|
adamc@157
|
631 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' t
|
adamc@157
|
632 | ((x', n', SOME t'), env) => pushENamedAs env x' n' (TFun (t', t), loc))
|
adamc@157
|
633 env xncs
|
adamc@157
|
634 end
|
adamc@157
|
635 | DVal (x, n, t, _) => pushENamedAs env x n t
|
adamc@157
|
636 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
|
adamc@157
|
637 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@157
|
638 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
|
adamc@157
|
639 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@157
|
640 | DConstraint _ => env
|
adamc@157
|
641 | DExport _ => env
|
adamc@157
|
642
|
adamc@2
|
643 end
|