adamc@674
|
1 (* Copyright (c) 2008-2009, 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@623
|
48 val liftKindInKind =
|
adamc@623
|
49 U.Kind.mapB {kind = fn bound => fn k =>
|
adamc@623
|
50 case k of
|
adamc@623
|
51 KRel xn =>
|
adamc@623
|
52 if xn < bound then
|
adamc@623
|
53 k
|
adamc@623
|
54 else
|
adamc@623
|
55 KRel (xn + 1)
|
adamc@623
|
56 | _ => k,
|
adamc@623
|
57 bind = fn (bound, _) => bound + 1}
|
adamc@623
|
58
|
adamc@623
|
59 val liftKindInCon =
|
adamc@623
|
60 U.Con.mapB {kind = fn bound => fn k =>
|
adamc@623
|
61 case k of
|
adamc@623
|
62 KRel xn =>
|
adamc@623
|
63 if xn < bound then
|
adamc@623
|
64 k
|
adamc@623
|
65 else
|
adamc@623
|
66 KRel (xn + 1)
|
adamc@623
|
67 | _ => k,
|
adamc@623
|
68 con = fn _ => fn c => c,
|
adamc@623
|
69 bind = fn (bound, U.Con.RelK _) => bound + 1
|
adamc@623
|
70 | (bound, _) => bound}
|
adamc@623
|
71
|
adamc@13
|
72 val liftConInCon =
|
adamc@623
|
73 U.Con.mapB {kind = fn _ => fn k => k,
|
adamc@13
|
74 con = fn bound => fn c =>
|
adamc@13
|
75 case c of
|
adamc@16
|
76 CRel xn =>
|
adamc@13
|
77 if xn < bound then
|
adamc@13
|
78 c
|
adamc@13
|
79 else
|
adamc@16
|
80 CRel (xn + 1)
|
adamc@17
|
81 (*| CUnif _ => raise SynUnif*)
|
adamc@13
|
82 | _ => c,
|
adamc@623
|
83 bind = fn (bound, U.Con.RelC _) => bound + 1
|
adamc@13
|
84 | (bound, _) => bound}
|
adamc@13
|
85
|
adamc@13
|
86 val lift = liftConInCon 0
|
adamc@13
|
87
|
adamc@623
|
88 val liftKindInExp =
|
adamc@623
|
89 U.Exp.mapB {kind = fn bound => fn k =>
|
adamc@623
|
90 case k of
|
adamc@623
|
91 KRel xn =>
|
adamc@623
|
92 if xn < bound then
|
adamc@623
|
93 k
|
adamc@623
|
94 else
|
adamc@623
|
95 KRel (xn + 1)
|
adamc@623
|
96 | _ => k,
|
adamc@623
|
97 con = fn _ => fn c => c,
|
adamc@623
|
98 exp = fn _ => fn e => e,
|
adamc@623
|
99 bind = fn (bound, U.Exp.RelK _) => bound + 1
|
adamc@623
|
100 | (bound, _) => bound}
|
adamc@623
|
101
|
adamc@448
|
102 val liftConInExp =
|
adamc@623
|
103 U.Exp.mapB {kind = fn _ => fn k => k,
|
adamc@448
|
104 con = fn bound => fn c =>
|
adamc@448
|
105 case c of
|
adamc@448
|
106 CRel xn =>
|
adamc@448
|
107 if xn < bound then
|
adamc@448
|
108 c
|
adamc@448
|
109 else
|
adamc@448
|
110 CRel (xn + 1)
|
adamc@448
|
111 | _ => c,
|
adamc@448
|
112 exp = fn _ => fn e => e,
|
adamc@448
|
113 bind = fn (bound, U.Exp.RelC _) => bound + 1
|
adamc@448
|
114 | (bound, _) => bound}
|
adamc@448
|
115
|
adamc@211
|
116 val liftExpInExp =
|
adamc@623
|
117 U.Exp.mapB {kind = fn _ => fn k => k,
|
adamc@211
|
118 con = fn _ => fn c => c,
|
adamc@211
|
119 exp = fn bound => fn e =>
|
adamc@211
|
120 case e of
|
adamc@211
|
121 ERel xn =>
|
adamc@211
|
122 if xn < bound then
|
adamc@211
|
123 e
|
adamc@211
|
124 else
|
adamc@211
|
125 ERel (xn + 1)
|
adamc@211
|
126 | _ => e,
|
adamc@211
|
127 bind = fn (bound, U.Exp.RelE _) => bound + 1
|
adamc@211
|
128 | (bound, _) => bound}
|
adamc@211
|
129
|
adamc@211
|
130
|
adamc@211
|
131 val liftExp = liftExpInExp 0
|
adamc@13
|
132
|
adamc@448
|
133 val subExpInExp =
|
adamc@623
|
134 U.Exp.mapB {kind = fn _ => fn k => k,
|
adamc@448
|
135 con = fn _ => fn c => c,
|
adamc@448
|
136 exp = fn (xn, rep) => fn e =>
|
adamc@448
|
137 case e of
|
adamc@448
|
138 ERel xn' =>
|
adamc@448
|
139 (case Int.compare (xn', xn) of
|
adamc@448
|
140 EQUAL => #1 rep
|
adamc@448
|
141 | GREATER=> ERel (xn' - 1)
|
adamc@448
|
142 | LESS => e)
|
adamc@448
|
143 | _ => e,
|
adamc@448
|
144 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
|
adamc@448
|
145 | ((xn, rep), U.Exp.RelC _) => (xn, liftConInExp 0 rep)
|
adamc@448
|
146 | (ctx, _) => ctx}
|
adamc@448
|
147
|
adamc@13
|
148 (* Back to environments *)
|
adamc@13
|
149
|
adamc@9
|
150 datatype 'a var' =
|
adamc@9
|
151 Rel' of int * 'a
|
adamc@9
|
152 | Named' of int * 'a
|
adamc@2
|
153
|
adamc@9
|
154 datatype 'a var =
|
adamc@9
|
155 NotBound
|
adamc@9
|
156 | Rel of int * 'a
|
adamc@9
|
157 | Named of int * 'a
|
adamc@2
|
158
|
adamc@191
|
159 type datatyp = string list * (string * con option) IM.map
|
adamc@157
|
160
|
adamc@211
|
161 datatype class_name =
|
adamc@211
|
162 ClNamed of int
|
adamc@211
|
163 | ClProj of int * string list * string
|
adamc@211
|
164
|
adamc@711
|
165 fun class_name_out cn =
|
adamc@711
|
166 case cn of
|
adamc@711
|
167 ClNamed n => (CNamed n, ErrorMsg.dummySpan)
|
adamc@711
|
168 | ClProj x => (CModProj x, ErrorMsg.dummySpan)
|
adamc@711
|
169
|
adamc@216
|
170 fun cn2s cn =
|
adamc@216
|
171 case cn of
|
adamc@216
|
172 ClNamed n => "Named(" ^ Int.toString n ^ ")"
|
adamc@216
|
173 | ClProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
|
adamc@216
|
174
|
adamc@211
|
175 structure CK = struct
|
adamc@211
|
176 type ord_key = class_name
|
adamc@211
|
177 open Order
|
adamc@211
|
178 fun compare x =
|
adamc@211
|
179 case x of
|
adamc@211
|
180 (ClNamed n1, ClNamed n2) => Int.compare (n1, n2)
|
adamc@211
|
181 | (ClNamed _, _) => LESS
|
adamc@211
|
182 | (_, ClNamed _) => GREATER
|
adamc@211
|
183
|
adamc@211
|
184 | (ClProj (m1, ms1, x1), ClProj (m2, ms2, x2)) =>
|
adamc@211
|
185 join (Int.compare (m1, m2),
|
adamc@211
|
186 fn () => join (joinL String.compare (ms1, ms2),
|
adamc@211
|
187 fn () => String.compare (x1, x2)))
|
adamc@211
|
188 end
|
adamc@211
|
189
|
adamc@677
|
190 structure CS = BinarySetFn(CK)
|
adamc@211
|
191 structure CM = BinaryMapFn(CK)
|
adamc@211
|
192
|
adamc@711
|
193 type class = {ground : (con * exp) list,
|
adamc@711
|
194 rules : (int * con list * con * exp) list}
|
adamc@711
|
195 val empty_class = {ground = [],
|
adamc@711
|
196 rules = []}
|
adamc@216
|
197
|
adamc@2
|
198 type env = {
|
adamc@623
|
199 renameK : int SM.map,
|
adamc@623
|
200 relK : string list,
|
adamc@623
|
201
|
adamc@9
|
202 renameC : kind var' SM.map,
|
adamc@2
|
203 relC : (string * kind) list,
|
adamc@11
|
204 namedC : (string * kind * con option) IM.map,
|
adamc@9
|
205
|
adamc@157
|
206 datatypes : datatyp IM.map,
|
adamc@191
|
207 constructors : (datatype_kind * int * string list * con option * int) SM.map,
|
adamc@157
|
208
|
adamc@211
|
209 classes : class CM.map,
|
adamc@211
|
210
|
adamc@9
|
211 renameE : con var' SM.map,
|
adamc@9
|
212 relE : (string * con) list,
|
adamc@31
|
213 namedE : (string * con) IM.map,
|
adamc@31
|
214
|
adamc@31
|
215 renameSgn : (int * sgn) SM.map,
|
adamc@31
|
216 sgn : (string * sgn) IM.map,
|
adamc@31
|
217
|
adamc@31
|
218 renameStr : (int * sgn) SM.map,
|
adamc@31
|
219 str : (string * sgn) IM.map
|
adamc@2
|
220 }
|
adamc@2
|
221
|
adamc@2
|
222 val namedCounter = ref 0
|
adamc@2
|
223
|
adamc@109
|
224 fun newNamed () =
|
adamc@109
|
225 let
|
adamc@109
|
226 val r = !namedCounter
|
adamc@109
|
227 in
|
adamc@109
|
228 namedCounter := r + 1;
|
adamc@109
|
229 r
|
adamc@109
|
230 end
|
adamc@109
|
231
|
adamc@2
|
232 val empty = {
|
adamc@623
|
233 renameK = SM.empty,
|
adamc@623
|
234 relK = [],
|
adamc@623
|
235
|
adamc@2
|
236 renameC = SM.empty,
|
adamc@2
|
237 relC = [],
|
adamc@9
|
238 namedC = IM.empty,
|
adamc@9
|
239
|
adamc@157
|
240 datatypes = IM.empty,
|
adamc@171
|
241 constructors = SM.empty,
|
adamc@157
|
242
|
adamc@211
|
243 classes = CM.empty,
|
adamc@211
|
244
|
adamc@9
|
245 renameE = SM.empty,
|
adamc@9
|
246 relE = [],
|
adamc@31
|
247 namedE = IM.empty,
|
adamc@31
|
248
|
adamc@31
|
249 renameSgn = SM.empty,
|
adamc@31
|
250 sgn = IM.empty,
|
adamc@31
|
251
|
adamc@31
|
252 renameStr = SM.empty,
|
adamc@31
|
253 str = IM.empty
|
adamc@2
|
254 }
|
adamc@2
|
255
|
adamc@623
|
256 fun pushKRel (env : env) x =
|
adamc@623
|
257 let
|
adamc@623
|
258 val renameK = SM.map (fn n => n+1) (#renameK env)
|
adamc@623
|
259 in
|
adamc@623
|
260 {renameK = SM.insert (renameK, x, 0),
|
adamc@623
|
261 relK = x :: #relK env,
|
adamc@623
|
262
|
adamc@623
|
263 renameC = SM.map (fn Rel' (n, k) => Rel' (n, liftKindInKind 0 k)
|
adamc@623
|
264 | x => x) (#renameC env),
|
adamc@623
|
265 relC = map (fn (x, k) => (x, liftKindInKind 0 k)) (#relC env),
|
adamc@623
|
266 namedC = #namedC env,
|
adamc@623
|
267
|
adamc@623
|
268 datatypes = #datatypes env,
|
adamc@623
|
269 constructors = #constructors env,
|
adamc@623
|
270
|
adamc@711
|
271 classes = CM.map (fn cl => {ground = map (fn (c, e) =>
|
adamc@711
|
272 (liftKindInCon 0 c,
|
adamc@711
|
273 e))
|
adamc@711
|
274 (#ground cl),
|
adamc@711
|
275 rules = #rules cl})
|
adamc@711
|
276 (#classes env),
|
adamc@623
|
277
|
adamc@623
|
278 renameE = SM.map (fn Rel' (n, c) => Rel' (n, liftKindInCon 0 c)
|
adamc@623
|
279 | Named' (n, c) => Named' (n, c)) (#renameE env),
|
adamc@623
|
280 relE = map (fn (x, c) => (x, liftKindInCon 0 c)) (#relE env),
|
adamc@623
|
281 namedE = #namedE env,
|
adamc@623
|
282
|
adamc@623
|
283 renameSgn = #renameSgn env,
|
adamc@623
|
284 sgn = #sgn env,
|
adamc@623
|
285
|
adamc@623
|
286 renameStr = #renameStr env,
|
adamc@623
|
287 str = #str env
|
adamc@623
|
288 }
|
adamc@623
|
289 end
|
adamc@623
|
290
|
adamc@623
|
291 fun lookupKRel (env : env) n =
|
adamc@623
|
292 (List.nth (#relK env, n))
|
adamc@623
|
293 handle Subscript => raise UnboundRel n
|
adamc@623
|
294
|
adamc@623
|
295 fun lookupK (env : env) x = SM.find (#renameK env, x)
|
adamc@623
|
296
|
adamc@2
|
297 fun pushCRel (env : env) x k =
|
adamc@2
|
298 let
|
adamc@9
|
299 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
|
adamc@2
|
300 | x => x) (#renameC env)
|
adamc@2
|
301 in
|
adamc@623
|
302 {renameK = #renameK env,
|
adamc@623
|
303 relK = #relK env,
|
adamc@623
|
304
|
adamc@623
|
305 renameC = SM.insert (renameC, x, Rel' (0, k)),
|
adamc@2
|
306 relC = (x, k) :: #relC env,
|
adamc@514
|
307 namedC = #namedC env,
|
adamc@9
|
308
|
adamc@157
|
309 datatypes = #datatypes env,
|
adamc@171
|
310 constructors = #constructors env,
|
adamc@157
|
311
|
adamc@674
|
312 classes = CM.map (fn class =>
|
adamc@711
|
313 {ground = map (fn (c, e) =>
|
adamc@711
|
314 (liftConInCon 0 c,
|
adamc@711
|
315 e))
|
adamc@711
|
316 (#ground class),
|
adamc@711
|
317 rules = #rules class})
|
adamc@211
|
318 (#classes env),
|
adamc@211
|
319
|
adamc@328
|
320 renameE = SM.map (fn Rel' (n, c) => Rel' (n, lift c)
|
adamc@517
|
321 | Named' (n, c) => Named' (n, c)) (#renameE env),
|
adamc@13
|
322 relE = map (fn (x, c) => (x, lift c)) (#relE env),
|
adamc@514
|
323 namedE = #namedE env,
|
adamc@31
|
324
|
adamc@31
|
325 renameSgn = #renameSgn env,
|
adamc@31
|
326 sgn = #sgn env,
|
adamc@31
|
327
|
adamc@31
|
328 renameStr = #renameStr env,
|
adamc@31
|
329 str = #str env
|
adamc@31
|
330 }
|
adamc@2
|
331 end
|
adamc@2
|
332
|
adamc@2
|
333 fun lookupCRel (env : env) n =
|
adamc@2
|
334 (List.nth (#relC env, n))
|
adamc@2
|
335 handle Subscript => raise UnboundRel n
|
adamc@2
|
336
|
adamc@11
|
337 fun pushCNamedAs (env : env) x n k co =
|
adamc@623
|
338 {renameK = #renameK env,
|
adamc@623
|
339 relK = #relK env,
|
adamc@623
|
340
|
adamc@623
|
341 renameC = SM.insert (#renameC env, x, Named' (n, k)),
|
adamc@5
|
342 relC = #relC env,
|
adamc@11
|
343 namedC = IM.insert (#namedC env, n, (x, k, co)),
|
adamc@9
|
344
|
adamc@157
|
345 datatypes = #datatypes env,
|
adamc@171
|
346 constructors = #constructors env,
|
adamc@157
|
347
|
adamc@211
|
348 classes = #classes env,
|
adamc@211
|
349
|
adamc@9
|
350 renameE = #renameE env,
|
adamc@9
|
351 relE = #relE env,
|
adamc@31
|
352 namedE = #namedE env,
|
adamc@31
|
353
|
adamc@31
|
354 renameSgn = #renameSgn env,
|
adamc@31
|
355 sgn = #sgn env,
|
adamc@31
|
356
|
adamc@31
|
357 renameStr = #renameStr env,
|
adamc@31
|
358 str = #str env}
|
adamc@5
|
359
|
adamc@11
|
360 fun pushCNamed env x k co =
|
adamc@2
|
361 let
|
adamc@2
|
362 val n = !namedCounter
|
adamc@2
|
363 in
|
adamc@2
|
364 namedCounter := n + 1;
|
adamc@11
|
365 (pushCNamedAs env x n k co, n)
|
adamc@2
|
366 end
|
adamc@2
|
367
|
adamc@2
|
368 fun lookupCNamed (env : env) n =
|
adamc@2
|
369 case IM.find (#namedC env, n) of
|
adamc@2
|
370 NONE => raise UnboundNamed n
|
adamc@2
|
371 | SOME x => x
|
adamc@2
|
372
|
adamc@2
|
373 fun lookupC (env : env) x =
|
adamc@2
|
374 case SM.find (#renameC env, x) of
|
adamc@9
|
375 NONE => NotBound
|
adamc@9
|
376 | SOME (Rel' x) => Rel x
|
adamc@9
|
377 | SOME (Named' x) => Named x
|
adamc@9
|
378
|
adamc@191
|
379 fun pushDatatype (env : env) n xs xncs =
|
adamc@188
|
380 let
|
adamc@188
|
381 val dk = U.classifyDatatype xncs
|
adamc@188
|
382 in
|
adamc@623
|
383 {renameK = #renameK env,
|
adamc@623
|
384 relK = #relK env,
|
adamc@623
|
385
|
adamc@623
|
386 renameC = #renameC env,
|
adamc@188
|
387 relC = #relC env,
|
adamc@188
|
388 namedC = #namedC env,
|
adamc@157
|
389
|
adamc@188
|
390 datatypes = IM.insert (#datatypes env, n,
|
adamc@191
|
391 (xs, foldl (fn ((x, n, to), cons) =>
|
adamc@191
|
392 IM.insert (cons, n, (x, to))) IM.empty xncs)),
|
adamc@188
|
393 constructors = foldl (fn ((x, n', to), cmap) =>
|
adamc@191
|
394 SM.insert (cmap, x, (dk, n', xs, to, n)))
|
adamc@188
|
395 (#constructors env) xncs,
|
adamc@157
|
396
|
adamc@211
|
397 classes = #classes env,
|
adamc@211
|
398
|
adamc@188
|
399 renameE = #renameE env,
|
adamc@188
|
400 relE = #relE env,
|
adamc@188
|
401 namedE = #namedE env,
|
adamc@157
|
402
|
adamc@188
|
403 renameSgn = #renameSgn env,
|
adamc@188
|
404 sgn = #sgn env,
|
adamc@157
|
405
|
adamc@188
|
406 renameStr = #renameStr env,
|
adamc@188
|
407 str = #str env}
|
adamc@188
|
408 end
|
adamc@157
|
409
|
adamc@157
|
410 fun lookupDatatype (env : env) n =
|
adamc@157
|
411 case IM.find (#datatypes env, n) of
|
adamc@157
|
412 NONE => raise UnboundNamed n
|
adamc@157
|
413 | SOME x => x
|
adamc@157
|
414
|
adamc@191
|
415 fun lookupDatatypeConstructor (_, dt) n =
|
adamc@157
|
416 case IM.find (dt, n) of
|
adamc@157
|
417 NONE => raise UnboundNamed n
|
adamc@157
|
418 | SOME x => x
|
adamc@157
|
419
|
adamc@171
|
420 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
|
adamc@171
|
421
|
adamc@191
|
422 fun datatypeArgs (xs, _) = xs
|
adamc@191
|
423 fun constructors (_, dt) = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
|
adamc@157
|
424
|
adamc@711
|
425 fun listClasses (env : env) =
|
adamc@711
|
426 map (fn (cn, {ground, rules}) =>
|
adamc@711
|
427 (class_name_out cn,
|
adamc@711
|
428 ground
|
adamc@711
|
429 @ map (fn (nvs, cs, c, e) =>
|
adamc@711
|
430 let
|
adamc@711
|
431 val loc = #2 c
|
adamc@711
|
432 val c = foldr (fn (c', c) => (TFun (c', c), loc)) c cs
|
adamc@711
|
433 val c = ListUtil.foldli (fn (n, (), c) => (TCFun (Explicit,
|
adamc@711
|
434 "x" ^ Int.toString n,
|
adamc@711
|
435 (KError, loc),
|
adamc@711
|
436 c), loc))
|
adamc@711
|
437 c (List.tabulate (nvs, fn _ => ()))
|
adamc@711
|
438 in
|
adamc@711
|
439 (c, e)
|
adamc@711
|
440 end) rules)) (CM.listItemsi (#classes env))
|
adamc@711
|
441
|
adamc@211
|
442 fun pushClass (env : env) n =
|
adamc@623
|
443 {renameK = #renameK env,
|
adamc@623
|
444 relK = #relK env,
|
adamc@623
|
445
|
adamc@623
|
446 renameC = #renameC env,
|
adamc@211
|
447 relC = #relC env,
|
adamc@211
|
448 namedC = #namedC env,
|
adamc@211
|
449
|
adamc@211
|
450 datatypes = #datatypes env,
|
adamc@211
|
451 constructors = #constructors env,
|
adamc@211
|
452
|
adamc@675
|
453 classes = CM.insert (#classes env, ClNamed n, empty_class),
|
adamc@211
|
454
|
adamc@211
|
455 renameE = #renameE env,
|
adamc@211
|
456 relE = #relE env,
|
adamc@211
|
457 namedE = #namedE env,
|
adamc@211
|
458
|
adamc@211
|
459 renameSgn = #renameSgn env,
|
adamc@211
|
460 sgn = #sgn env,
|
adamc@211
|
461
|
adamc@211
|
462 renameStr = #renameStr env,
|
adamc@403
|
463 str = #str env}
|
adamc@211
|
464
|
adamc@211
|
465 fun class_name_in (c, _) =
|
adamc@211
|
466 case c of
|
adamc@211
|
467 CNamed n => SOME (ClNamed n)
|
adamc@211
|
468 | CModProj x => SOME (ClProj x)
|
adamc@228
|
469 | CUnif (_, _, _, ref (SOME c)) => class_name_in c
|
adamc@211
|
470 | _ => NONE
|
adamc@211
|
471
|
adamc@403
|
472 fun isClass (env : env) c =
|
adamc@403
|
473 let
|
adamc@403
|
474 fun find NONE = false
|
adamc@403
|
475 | find (SOME c) = Option.isSome (CM.find (#classes env, c))
|
adamc@403
|
476 in
|
adamc@403
|
477 find (class_name_in c)
|
adamc@403
|
478 end
|
adamc@403
|
479
|
adamc@711
|
480 fun class_head_in c =
|
adamc@711
|
481 case #1 c of
|
adamc@711
|
482 CApp (f, _) => class_head_in f
|
adamc@711
|
483 | CUnif (_, _, _, ref (SOME c)) => class_head_in c
|
adamc@711
|
484 | _ => class_name_in c
|
adamc@211
|
485
|
adamc@711
|
486 exception Unify
|
adamc@711
|
487
|
adamc@711
|
488 fun unifyKinds (k1, k2) =
|
adamc@711
|
489 case (#1 k1, #1 k2) of
|
adamc@711
|
490 (KType, KType) => ()
|
adamc@711
|
491 | (KArrow (d1, r1), KArrow (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
|
adamc@711
|
492 | (KName, KName) => ()
|
adamc@711
|
493 | (KRecord k1, KRecord k2) => unifyKinds (k1, k2)
|
adamc@711
|
494 | (KUnit, KUnit) => ()
|
adamc@711
|
495 | (KTuple ks1, KTuple ks2) => (ListPair.appEq unifyKinds (ks1, ks2)
|
adamc@711
|
496 handle ListPair.UnequalLengths => raise Unify)
|
adamc@711
|
497 | (KUnif (_, _, ref (SOME k1)), _) => unifyKinds (k1, k2)
|
adamc@711
|
498 | (_, KUnif (_, _, ref (SOME k2))) => unifyKinds (k1, k2)
|
adamc@711
|
499 | (KRel n1, KRel n2) => if n1 = n2 then () else raise Unify
|
adamc@711
|
500 | (KFun (_, k1), KFun (_, k2)) => unifyKinds (k1, k2)
|
adamc@711
|
501 | _ => raise Unify
|
adamc@711
|
502
|
adamc@711
|
503 fun unifyCons rs =
|
adamc@674
|
504 let
|
adamc@711
|
505 fun unify d (c1, c2) =
|
adamc@711
|
506 case (#1 c1, #1 c2) of
|
adamc@711
|
507 (CUnif (_, _, _, ref (SOME c1)), _) => unify d (c1, c2)
|
adamc@711
|
508 | (_, CUnif (_, _, _, ref (SOME c2))) => unify d (c1, c2)
|
adamc@711
|
509
|
adamc@711
|
510 | (c1', CRel n2) =>
|
adamc@711
|
511 if n2 < d then
|
adamc@711
|
512 case c1' of
|
adamc@711
|
513 CRel n1 => if n1 = n2 then () else raise Unify
|
adamc@711
|
514 | _ => raise Unify
|
adamc@711
|
515 else if n2 - d >= length rs then
|
adamc@711
|
516 case c1' of
|
adamc@711
|
517 CRel n1 => if n1 = n2 - length rs then () else raise Unify
|
adamc@711
|
518 | _ => raise Unify
|
adamc@711
|
519 else
|
adamc@711
|
520 let
|
adamc@711
|
521 val r = List.nth (rs, n2 - d)
|
adamc@711
|
522 in
|
adamc@711
|
523 case !r of
|
adamc@711
|
524 NONE => r := SOME c1
|
adamc@711
|
525 | SOME c2 => unify d (c1, c2)
|
adamc@711
|
526 end
|
adamc@711
|
527
|
adamc@711
|
528 | (TFun (d1, r1), TFun (d2, r2)) => (unify d (d1, d2); unify d (r1, r2))
|
adamc@711
|
529 | (TCFun (_, _, k1, r1), TCFun (_, _, k2, r2)) => (unifyKinds (k1, k2); unify (d + 1) (r1, r2))
|
adamc@711
|
530 | (TRecord c1, TRecord c2) => unify d (c1, c2)
|
adamc@711
|
531 | (TDisjoint (a1, b1, c1), TDisjoint (a2, b2, c2)) =>
|
adamc@711
|
532 (unify d (a1, a2); unify d (b1, b2); unify d (c1, c2))
|
adamc@711
|
533
|
adamc@711
|
534 | (CNamed n1, CNamed n2) => if n1 = n2 then () else raise Unify
|
adamc@711
|
535 | (CModProj (n1, ms1, x1), CModProj (n2, ms2, x2)) =>
|
adamc@711
|
536 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then () else raise Unify
|
adamc@711
|
537 | (CApp (f1, x1), CApp (f2, x2)) => (unify d (f1, f2); unify d (x1, x2))
|
adamc@711
|
538 | (CAbs (_, k1, b1), CAbs (_, k2, b2)) => (unifyKinds (k1, k2); unify (d + 1) (b1, b2))
|
adamc@711
|
539
|
adamc@711
|
540 | (CKAbs (_, b1), CKAbs (_, b2)) => unify d (b1, b2)
|
adamc@711
|
541 | (CKApp (c1, k1), CKApp (c2, k2)) => (unify d (c1, c2); unifyKinds (k1, k2))
|
adamc@711
|
542 | (TKFun (_, c1), TKFun (_, c2)) => unify d (c1, c2)
|
adamc@711
|
543
|
adamc@711
|
544 | (CName s1, CName s2) => if s1 = s2 then () else raise Unify
|
adamc@711
|
545
|
adamc@711
|
546 | (CRecord (k1, xcs1), CRecord (k2, xcs2)) =>
|
adamc@711
|
547 (unifyKinds (k1, k2);
|
adamc@711
|
548 ListPair.appEq (fn ((x1, c1), (x2, c2)) => (unify d (x1, x2); unify d (c1, c2))) (xcs1, xcs2)
|
adamc@711
|
549 handle ListPair.UnequalLengths => raise Unify)
|
adamc@711
|
550 | (CConcat (f1, x1), CConcat (f2, x2)) => (unify d (f1, f2); unify d (x1, x2))
|
adamc@711
|
551 | (CMap (d1, r1), CMap (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
|
adamc@711
|
552
|
adamc@711
|
553 | (CUnit, CUnit) => ()
|
adamc@711
|
554
|
adamc@711
|
555 | (CTuple cs1, CTuple cs2) => (ListPair.appEq (unify d) (cs1, cs2)
|
adamc@711
|
556 handle ListPair.UnequalLengths => raise Unify)
|
adamc@711
|
557 | (CProj (c1, n1), CProj (c2, n2)) => (unify d (c1, c2);
|
adamc@711
|
558 if n1 = n2 then () else raise Unify)
|
adamc@711
|
559
|
adamc@711
|
560 | _ => raise Unify
|
adamc@674
|
561 in
|
adamc@711
|
562 unify
|
adamc@674
|
563 end
|
adamc@674
|
564
|
adamc@711
|
565 fun tryUnify nRs (c1, c2) =
|
adamc@674
|
566 let
|
adamc@711
|
567 val rs = List.tabulate (nRs, fn _ => ref NONE)
|
adamc@674
|
568 in
|
adamc@711
|
569 (unifyCons rs 0 (c1, c2);
|
adamc@711
|
570 SOME (map (fn r => case !r of
|
adamc@711
|
571 NONE => raise Unify
|
adamc@711
|
572 | SOME c => c) rs))
|
adamc@711
|
573 handle Unify => NONE
|
adamc@674
|
574 end
|
adamc@674
|
575
|
adamc@711
|
576 fun unifySubst (rs : con list) =
|
adamc@711
|
577 U.Con.mapB {kind = fn _ => fn k => k,
|
adamc@711
|
578 con = fn d => fn c =>
|
adamc@711
|
579 case c of
|
adamc@711
|
580 CRel n =>
|
adamc@711
|
581 if n < d then
|
adamc@711
|
582 c
|
adamc@711
|
583 else
|
adamc@711
|
584 #1 (List.nth (rs, n - d))
|
adamc@711
|
585 | _ => c,
|
adamc@711
|
586 bind = fn (d, U.Con.RelC _) => d + 1
|
adamc@711
|
587 | (d, _) => d}
|
adamc@711
|
588 0
|
adamc@711
|
589
|
adamc@711
|
590 fun resolveClass (env : env) =
|
adamc@674
|
591 let
|
adamc@711
|
592 fun resolve c =
|
adamc@711
|
593 let
|
adamc@711
|
594 fun doHead f =
|
adamc@711
|
595 case CM.find (#classes env, f) of
|
adamc@711
|
596 NONE => NONE
|
adamc@711
|
597 | SOME class =>
|
adamc@711
|
598 let
|
adamc@711
|
599 val loc = #2 c
|
adamc@675
|
600
|
adamc@711
|
601 fun tryRules rules =
|
adamc@711
|
602 case rules of
|
adamc@675
|
603 [] => NONE
|
adamc@711
|
604 | (nRs, cs, c', e) :: rules' =>
|
adamc@711
|
605 case tryUnify nRs (c, c') of
|
adamc@711
|
606 NONE => tryRules rules'
|
adamc@711
|
607 | SOME rs =>
|
adamc@675
|
608 let
|
adamc@711
|
609 val eos = map (resolve o unifySubst rs) cs
|
adamc@675
|
610 in
|
adamc@711
|
611 if List.exists (not o Option.isSome) eos then
|
adamc@711
|
612 tryRules rules'
|
adamc@711
|
613 else
|
adamc@711
|
614 let
|
adamc@711
|
615 val es = List.mapPartial (fn x => x) eos
|
adamc@711
|
616
|
adamc@711
|
617 val e = foldr (fn (c, e) => (ECApp (e, c), loc)) e rs
|
adamc@711
|
618 val e = foldl (fn (e', e) => (EApp (e, e'), loc)) e es
|
adamc@711
|
619 in
|
adamc@711
|
620 SOME e
|
adamc@711
|
621 end
|
adamc@675
|
622 end
|
adamc@711
|
623
|
adamc@711
|
624 fun rules () = tryRules (#rules class)
|
adamc@711
|
625
|
adamc@711
|
626 fun tryGrounds ces =
|
adamc@711
|
627 case ces of
|
adamc@711
|
628 [] => rules ()
|
adamc@711
|
629 | (c', e) :: ces' =>
|
adamc@711
|
630 case tryUnify 0 (c, c') of
|
adamc@711
|
631 NONE => tryGrounds ces'
|
adamc@711
|
632 | SOME _ => SOME e
|
adamc@675
|
633 in
|
adamc@711
|
634 tryGrounds (#ground class)
|
adamc@675
|
635 end
|
adamc@711
|
636 in
|
adamc@711
|
637 case class_head_in c of
|
adamc@711
|
638 SOME f => doHead f
|
adamc@711
|
639 | _ => NONE
|
adamc@711
|
640 end
|
adamc@674
|
641 in
|
adamc@711
|
642 resolve
|
adamc@674
|
643 end
|
adamc@211
|
644
|
adamc@9
|
645 fun pushERel (env : env) x t =
|
adamc@9
|
646 let
|
adamc@9
|
647 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
|
adamc@9
|
648 | x => x) (#renameE env)
|
adamc@211
|
649
|
adamc@675
|
650 val classes = CM.map (fn class =>
|
adamc@711
|
651 {ground = map (fn (c, e) => (c, liftExp e)) (#ground class),
|
adamc@711
|
652 rules = #rules class}) (#classes env)
|
adamc@711
|
653 val classes = case class_head_in t of
|
adamc@211
|
654 NONE => classes
|
adamc@711
|
655 | SOME f =>
|
adamc@403
|
656 case CM.find (classes, f) of
|
adamc@403
|
657 NONE => classes
|
adamc@403
|
658 | SOME class =>
|
adamc@403
|
659 let
|
adamc@711
|
660 val class = {ground = (t, (ERel 0, #2 t)) :: #ground class,
|
adamc@711
|
661 rules = #rules class}
|
adamc@403
|
662 in
|
adamc@403
|
663 CM.insert (classes, f, class)
|
adamc@403
|
664 end
|
adamc@9
|
665 in
|
adamc@623
|
666 {renameK = #renameK env,
|
adamc@623
|
667 relK = #relK env,
|
adamc@623
|
668
|
adamc@623
|
669 renameC = #renameC env,
|
adamc@9
|
670 relC = #relC env,
|
adamc@9
|
671 namedC = #namedC env,
|
adamc@9
|
672
|
adamc@157
|
673 datatypes = #datatypes env,
|
adamc@171
|
674 constructors = #constructors env,
|
adamc@157
|
675
|
adamc@211
|
676 classes = classes,
|
adamc@211
|
677
|
adamc@9
|
678 renameE = SM.insert (renameE, x, Rel' (0, t)),
|
adamc@9
|
679 relE = (x, t) :: #relE env,
|
adamc@31
|
680 namedE = #namedE env,
|
adamc@31
|
681
|
adamc@31
|
682 renameSgn = #renameSgn env,
|
adamc@31
|
683 sgn = #sgn env,
|
adamc@31
|
684
|
adamc@31
|
685 renameStr = #renameStr env,
|
adamc@31
|
686 str = #str env}
|
adamc@9
|
687 end
|
adamc@9
|
688
|
adamc@9
|
689 fun lookupERel (env : env) n =
|
adamc@9
|
690 (List.nth (#relE env, n))
|
adamc@9
|
691 handle Subscript => raise UnboundRel n
|
adamc@9
|
692
|
adamc@674
|
693 fun rule_in c =
|
adamc@674
|
694 let
|
adamc@674
|
695 fun quantifiers (c, nvars) =
|
adamc@674
|
696 case #1 c of
|
adamc@674
|
697 TCFun (_, _, _, c) => quantifiers (c, nvars + 1)
|
adamc@674
|
698 | _ =>
|
adamc@674
|
699 let
|
adamc@674
|
700 fun clauses (c, hyps) =
|
adamc@674
|
701 case #1 c of
|
adamc@674
|
702 TFun (hyp, c) =>
|
adamc@711
|
703 (case class_head_in hyp of
|
adamc@711
|
704 SOME _ => clauses (c, hyp :: hyps)
|
adamc@711
|
705 | NONE => NONE)
|
adamc@674
|
706 | _ =>
|
adamc@711
|
707 case class_head_in c of
|
adamc@674
|
708 NONE => NONE
|
adamc@711
|
709 | SOME f => SOME (f, nvars, rev hyps, c)
|
adamc@674
|
710 in
|
adamc@674
|
711 clauses (c, [])
|
adamc@674
|
712 end
|
adamc@674
|
713 in
|
adamc@711
|
714 quantifiers (c, 0)
|
adamc@677
|
715 end
|
adamc@677
|
716
|
adamc@9
|
717 fun pushENamedAs (env : env) x n t =
|
adamc@211
|
718 let
|
adamc@211
|
719 val classes = #classes env
|
adamc@674
|
720 val classes = case rule_in t of
|
adamc@211
|
721 NONE => classes
|
adamc@711
|
722 | SOME (f, nvs, cs, c) =>
|
adamc@403
|
723 case CM.find (classes, f) of
|
adamc@403
|
724 NONE => classes
|
adamc@403
|
725 | SOME class =>
|
adamc@403
|
726 let
|
adamc@675
|
727 val e = (ENamed n, #2 t)
|
adamc@675
|
728
|
adamc@675
|
729 val class =
|
adamc@711
|
730 {ground = #ground class,
|
adamc@711
|
731 rules = (nvs, cs, c, e) :: #rules class}
|
adamc@403
|
732 in
|
adamc@403
|
733 CM.insert (classes, f, class)
|
adamc@403
|
734 end
|
adamc@211
|
735 in
|
adamc@623
|
736 {renameK = #renameK env,
|
adamc@623
|
737 relK = #relK env,
|
adamc@623
|
738
|
adamc@623
|
739 renameC = #renameC env,
|
adamc@211
|
740 relC = #relC env,
|
adamc@211
|
741 namedC = #namedC env,
|
adamc@9
|
742
|
adamc@211
|
743 datatypes = #datatypes env,
|
adamc@211
|
744 constructors = #constructors env,
|
adamc@157
|
745
|
adamc@211
|
746 classes = classes,
|
adamc@31
|
747
|
adamc@211
|
748 renameE = SM.insert (#renameE env, x, Named' (n, t)),
|
adamc@211
|
749 relE = #relE env,
|
adamc@211
|
750 namedE = IM.insert (#namedE env, n, (x, t)),
|
adamc@211
|
751
|
adamc@211
|
752 renameSgn = #renameSgn env,
|
adamc@211
|
753 sgn = #sgn env,
|
adamc@211
|
754
|
adamc@211
|
755 renameStr = #renameStr env,
|
adamc@211
|
756 str = #str env}
|
adamc@211
|
757 end
|
adamc@9
|
758
|
adamc@9
|
759 fun pushENamed env x t =
|
adamc@9
|
760 let
|
adamc@9
|
761 val n = !namedCounter
|
adamc@9
|
762 in
|
adamc@9
|
763 namedCounter := n + 1;
|
adamc@9
|
764 (pushENamedAs env x n t, n)
|
adamc@9
|
765 end
|
adamc@9
|
766
|
adamc@9
|
767 fun lookupENamed (env : env) n =
|
adamc@9
|
768 case IM.find (#namedE env, n) of
|
adamc@9
|
769 NONE => raise UnboundNamed n
|
adamc@9
|
770 | SOME x => x
|
adamc@9
|
771
|
adamc@471
|
772 fun checkENamed (env : env) n =
|
adamc@471
|
773 Option.isSome (IM.find (#namedE env, n))
|
adamc@471
|
774
|
adamc@9
|
775 fun lookupE (env : env) x =
|
adamc@9
|
776 case SM.find (#renameE env, x) of
|
adamc@9
|
777 NONE => NotBound
|
adamc@9
|
778 | SOME (Rel' x) => Rel x
|
adamc@9
|
779 | SOME (Named' x) => Named x
|
adamc@2
|
780
|
adamc@31
|
781 fun pushSgnNamedAs (env : env) x n sgis =
|
adamc@623
|
782 {renameK = #renameK env,
|
adamc@623
|
783 relK = #relK env,
|
adamc@623
|
784
|
adamc@623
|
785 renameC = #renameC env,
|
adamc@31
|
786 relC = #relC env,
|
adamc@31
|
787 namedC = #namedC env,
|
adamc@31
|
788
|
adamc@157
|
789 datatypes = #datatypes env,
|
adamc@171
|
790 constructors = #constructors env,
|
adamc@157
|
791
|
adamc@211
|
792 classes = #classes env,
|
adamc@211
|
793
|
adamc@31
|
794 renameE = #renameE env,
|
adamc@31
|
795 relE = #relE env,
|
adamc@31
|
796 namedE = #namedE env,
|
adamc@31
|
797
|
adamc@31
|
798 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
|
adamc@31
|
799 sgn = IM.insert (#sgn env, n, (x, sgis)),
|
adamc@31
|
800
|
adamc@31
|
801 renameStr = #renameStr env,
|
adamc@31
|
802 str = #str env}
|
adamc@31
|
803
|
adamc@31
|
804 fun pushSgnNamed env x sgis =
|
adamc@31
|
805 let
|
adamc@31
|
806 val n = !namedCounter
|
adamc@31
|
807 in
|
adamc@31
|
808 namedCounter := n + 1;
|
adamc@31
|
809 (pushSgnNamedAs env x n sgis, n)
|
adamc@31
|
810 end
|
adamc@31
|
811
|
adamc@31
|
812 fun lookupSgnNamed (env : env) n =
|
adamc@31
|
813 case IM.find (#sgn env, n) of
|
adamc@31
|
814 NONE => raise UnboundNamed n
|
adamc@31
|
815 | SOME x => x
|
adamc@31
|
816
|
adamc@31
|
817 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
|
adamc@31
|
818
|
adamc@31
|
819 fun lookupStrNamed (env : env) n =
|
adamc@31
|
820 case IM.find (#str env, n) of
|
adamc@31
|
821 NONE => raise UnboundNamed n
|
adamc@31
|
822 | SOME x => x
|
adamc@31
|
823
|
adamc@31
|
824 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
|
adamc@31
|
825
|
adamc@203
|
826
|
adamc@216
|
827 fun sgiSeek (sgi, (sgns, strs, cons)) =
|
adamc@216
|
828 case sgi of
|
adamc@216
|
829 SgiConAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
830 | SgiCon (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
831 | SgiDatatype (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
832 | SgiDatatypeImp (x, n, _, _, _, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
833 | SgiVal _ => (sgns, strs, cons)
|
adamc@216
|
834 | SgiSgn (x, n, _) => (IM.insert (sgns, n, x), strs, cons)
|
adamc@216
|
835 | SgiStr (x, n, _) => (sgns, IM.insert (strs, n, x), cons)
|
adamc@216
|
836 | SgiConstraint _ => (sgns, strs, cons)
|
adamc@563
|
837 | SgiClassAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@563
|
838 | SgiClass (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
839
|
adamc@34
|
840 fun sgnSeek f sgis =
|
adamc@34
|
841 let
|
adamc@59
|
842 fun seek (sgis, sgns, strs, cons) =
|
adamc@34
|
843 case sgis of
|
adamc@34
|
844 [] => NONE
|
adamc@34
|
845 | (sgi, _) :: sgis =>
|
adamc@34
|
846 case f sgi of
|
adamc@158
|
847 SOME v =>
|
adamc@158
|
848 let
|
adamc@158
|
849 val cons =
|
adamc@158
|
850 case sgi of
|
adamc@191
|
851 SgiDatatype (x, n, _, _) => IM.insert (cons, n, x)
|
adamc@191
|
852 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
|
adamc@158
|
853 | _ => cons
|
adamc@158
|
854 in
|
adamc@158
|
855 SOME (v, (sgns, strs, cons))
|
adamc@158
|
856 end
|
adamc@88
|
857 | NONE =>
|
adamc@216
|
858 let
|
adamc@216
|
859 val (sgns, strs, cons) = sgiSeek (sgi, (sgns, strs, cons))
|
adamc@216
|
860 in
|
adamc@216
|
861 seek (sgis, sgns, strs, cons)
|
adamc@216
|
862 end
|
adamc@34
|
863 in
|
adamc@59
|
864 seek (sgis, IM.empty, IM.empty, IM.empty)
|
adamc@34
|
865 end
|
adamc@34
|
866
|
adamc@34
|
867 fun id x = x
|
adamc@34
|
868
|
adamc@34
|
869 fun unravelStr (str, _) =
|
adamc@34
|
870 case str of
|
adamc@34
|
871 StrVar x => (x, [])
|
adamc@34
|
872 | StrProj (str, m) =>
|
adamc@34
|
873 let
|
adamc@34
|
874 val (x, ms) = unravelStr str
|
adamc@34
|
875 in
|
adamc@34
|
876 (x, ms @ [m])
|
adamc@34
|
877 end
|
adamc@34
|
878 | _ => raise Fail "unravelStr"
|
adamc@34
|
879
|
adamc@59
|
880 fun sgnS_con (str, (sgns, strs, cons)) c =
|
adamc@34
|
881 case c of
|
adamc@34
|
882 CModProj (m1, ms, x) =>
|
adamc@34
|
883 (case IM.find (strs, m1) of
|
adamc@34
|
884 NONE => c
|
adamc@34
|
885 | SOME m1x =>
|
adamc@34
|
886 let
|
adamc@34
|
887 val (m1, ms') = unravelStr str
|
adamc@34
|
888 in
|
adamc@34
|
889 CModProj (m1, ms' @ m1x :: ms, x)
|
adamc@34
|
890 end)
|
adamc@34
|
891 | CNamed n =>
|
adamc@34
|
892 (case IM.find (cons, n) of
|
adamc@34
|
893 NONE => c
|
adamc@34
|
894 | SOME nx =>
|
adamc@34
|
895 let
|
adamc@34
|
896 val (m1, ms) = unravelStr str
|
adamc@34
|
897 in
|
adamc@34
|
898 CModProj (m1, ms, nx)
|
adamc@34
|
899 end)
|
adamc@34
|
900 | _ => c
|
adamc@34
|
901
|
adamc@721
|
902 fun sgnS_con' (m1, ms', (sgns, strs, cons)) =
|
adamc@721
|
903 U.Con.map {kind = fn x => x,
|
adamc@721
|
904 con = fn c =>
|
adamc@721
|
905 case c of
|
adamc@721
|
906 CModProj (m1, ms, x) =>
|
adamc@721
|
907 (case IM.find (strs, m1) of
|
adamc@721
|
908 NONE => c
|
adamc@721
|
909 | SOME m1x => CModProj (m1, ms' @ m1x :: ms, x))
|
adamc@721
|
910 | CNamed n =>
|
adamc@721
|
911 (case IM.find (cons, n) of
|
adamc@721
|
912 NONE => c
|
adamc@721
|
913 | SOME nx => CModProj (m1, ms', nx))
|
adamc@721
|
914 | _ => c}
|
adamc@216
|
915
|
adamc@59
|
916 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
|
adamc@59
|
917 case sgn of
|
adamc@59
|
918 SgnProj (m1, ms, x) =>
|
adamc@59
|
919 (case IM.find (strs, m1) of
|
adamc@59
|
920 NONE => sgn
|
adamc@59
|
921 | SOME m1x =>
|
adamc@59
|
922 let
|
adamc@59
|
923 val (m1, ms') = unravelStr str
|
adamc@59
|
924 in
|
adamc@59
|
925 SgnProj (m1, ms' @ m1x :: ms, x)
|
adamc@59
|
926 end)
|
adamc@59
|
927 | SgnVar n =>
|
adamc@59
|
928 (case IM.find (sgns, n) of
|
adamc@59
|
929 NONE => sgn
|
adamc@59
|
930 | SOME nx =>
|
adamc@59
|
931 let
|
adamc@59
|
932 val (m1, ms) = unravelStr str
|
adamc@59
|
933 in
|
adamc@59
|
934 SgnProj (m1, ms, nx)
|
adamc@59
|
935 end)
|
adamc@59
|
936 | _ => sgn
|
adamc@59
|
937
|
adamc@59
|
938 fun sgnSubSgn x =
|
adamc@34
|
939 ElabUtil.Sgn.map {kind = id,
|
adamc@59
|
940 con = sgnS_con x,
|
adamc@34
|
941 sgn_item = id,
|
adamc@59
|
942 sgn = sgnS_sgn x}
|
adamc@34
|
943
|
adamc@211
|
944
|
adamc@211
|
945
|
adamc@211
|
946 and projectSgn env {sgn, str, field} =
|
adamc@211
|
947 case #1 (hnormSgn env sgn) of
|
adamc@211
|
948 SgnConst sgis =>
|
adamc@211
|
949 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@211
|
950 NONE => NONE
|
adamc@211
|
951 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@211
|
952 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@211
|
953 | _ => NONE
|
adamc@211
|
954
|
adamc@211
|
955 and hnormSgn env (all as (sgn, loc)) =
|
adamc@34
|
956 case sgn of
|
adamc@42
|
957 SgnError => all
|
adamc@42
|
958 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
|
adamc@42
|
959 | SgnConst _ => all
|
adamc@42
|
960 | SgnFun _ => all
|
adamc@59
|
961 | SgnProj (m, ms, x) =>
|
adamc@59
|
962 let
|
adamc@59
|
963 val (_, sgn) = lookupStrNamed env m
|
adamc@59
|
964 in
|
adamc@59
|
965 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
|
adamc@59
|
966 sgn = sgn,
|
adamc@59
|
967 field = x} of
|
adamc@59
|
968 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
|
adamc@59
|
969 | SOME sgn => sgn
|
adamc@59
|
970 end
|
adamc@42
|
971 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
972 case #1 (hnormSgn env sgn) of
|
adamc@42
|
973 SgnError => (SgnError, loc)
|
adamc@42
|
974 | SgnConst sgis =>
|
adamc@42
|
975 let
|
adamc@42
|
976 fun traverse (pre, post) =
|
adamc@42
|
977 case post of
|
adamc@42
|
978 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
|
adamc@42
|
979 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
|
adamc@42
|
980 if x = x' then
|
adamc@42
|
981 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
|
adamc@42
|
982 else
|
adamc@42
|
983 traverse (sgi :: pre, rest)
|
adamc@42
|
984 | sgi :: rest => traverse (sgi :: pre, rest)
|
adamc@42
|
985
|
adamc@42
|
986 val sgis = traverse ([], sgis)
|
adamc@42
|
987 in
|
adamc@42
|
988 (SgnConst sgis, loc)
|
adamc@42
|
989 end
|
adamc@42
|
990 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
|
adamc@42
|
991
|
adamc@211
|
992 fun enrichClasses env classes (m1, ms) sgn =
|
adamc@59
|
993 case #1 (hnormSgn env sgn) of
|
adamc@59
|
994 SgnConst sgis =>
|
adamc@211
|
995 let
|
adamc@218
|
996 val (classes, _, _, _) =
|
adamc@218
|
997 foldl (fn (sgi, (classes, newClasses, fmap, env)) =>
|
adamc@211
|
998 let
|
adamc@211
|
999 fun found (x, n) =
|
adamc@211
|
1000 (CM.insert (classes,
|
adamc@211
|
1001 ClProj (m1, ms, x),
|
adamc@211
|
1002 empty_class),
|
adamc@216
|
1003 IM.insert (newClasses, n, x),
|
adamc@218
|
1004 sgiSeek (#1 sgi, fmap),
|
adamc@218
|
1005 env)
|
adamc@216
|
1006
|
adamc@218
|
1007 fun default () = (classes, newClasses, sgiSeek (#1 sgi, fmap), env)
|
adamc@211
|
1008 in
|
adamc@211
|
1009 case #1 sgi of
|
adamc@217
|
1010 SgiStr (x, _, sgn) =>
|
adamc@217
|
1011 (enrichClasses env classes (m1, ms @ [x]) sgn,
|
adamc@217
|
1012 newClasses,
|
adamc@218
|
1013 sgiSeek (#1 sgi, fmap),
|
adamc@218
|
1014 env)
|
adamc@218
|
1015 | SgiSgn (x, n, sgn) =>
|
adamc@218
|
1016 (classes,
|
adamc@218
|
1017 newClasses,
|
adamc@218
|
1018 fmap,
|
adamc@218
|
1019 pushSgnNamedAs env x n sgn)
|
adamc@218
|
1020
|
adamc@563
|
1021 | SgiClassAbs (x, n, _) => found (x, n)
|
adamc@563
|
1022 | SgiClass (x, n, _, _) => found (x, n)
|
adamc@674
|
1023 | SgiVal (x, n, c) =>
|
adamc@674
|
1024 (case rule_in c of
|
adamc@674
|
1025 NONE => default ()
|
adamc@711
|
1026 | SOME (cn, nvs, cs, c) =>
|
adamc@674
|
1027 let
|
adamc@711
|
1028 val loc = #2 c
|
adamc@721
|
1029 val globalize = sgnS_con' (m1, ms, fmap)
|
adamc@674
|
1030
|
adamc@674
|
1031 val nc =
|
adamc@674
|
1032 case cn of
|
adamc@674
|
1033 ClNamed f => IM.find (newClasses, f)
|
adamc@674
|
1034 | _ => NONE
|
adamc@674
|
1035 in
|
adamc@674
|
1036 case nc of
|
adamc@674
|
1037 NONE =>
|
adamc@419
|
1038 let
|
adamc@419
|
1039 val classes =
|
adamc@419
|
1040 case CM.find (classes, cn) of
|
adamc@419
|
1041 NONE => classes
|
adamc@419
|
1042 | SOME class =>
|
adamc@419
|
1043 let
|
adamc@711
|
1044 val e = (EModProj (m1, ms, x), #2 sgn)
|
adamc@675
|
1045
|
adamc@675
|
1046 val class =
|
adamc@711
|
1047 {ground = #ground class,
|
adamc@711
|
1048 rules = (nvs,
|
adamc@711
|
1049 map globalize cs,
|
adamc@711
|
1050 globalize c,
|
adamc@711
|
1051 e) :: #rules class}
|
adamc@419
|
1052 in
|
adamc@419
|
1053 CM.insert (classes, cn, class)
|
adamc@419
|
1054 end
|
adamc@419
|
1055 in
|
adamc@419
|
1056 (classes,
|
adamc@419
|
1057 newClasses,
|
adamc@419
|
1058 fmap,
|
adamc@419
|
1059 env)
|
adamc@419
|
1060 end
|
adamc@674
|
1061 | SOME fx =>
|
adamc@674
|
1062 let
|
adamc@674
|
1063 val cn = ClProj (m1, ms, fx)
|
adamc@419
|
1064
|
adamc@674
|
1065 val classes =
|
adamc@674
|
1066 case CM.find (classes, cn) of
|
adamc@674
|
1067 NONE => classes
|
adamc@674
|
1068 | SOME class =>
|
adamc@674
|
1069 let
|
adamc@675
|
1070 val e = (EModProj (m1, ms, x), #2 sgn)
|
adamc@675
|
1071
|
adamc@675
|
1072 val class =
|
adamc@711
|
1073 {ground = #ground class,
|
adamc@711
|
1074 rules = (nvs,
|
adamc@711
|
1075 map globalize cs,
|
adamc@711
|
1076 globalize c,
|
adamc@711
|
1077 e) :: #rules class}
|
adamc@674
|
1078 in
|
adamc@674
|
1079 CM.insert (classes, cn, class)
|
adamc@674
|
1080 end
|
adamc@674
|
1081 in
|
adamc@674
|
1082 (classes,
|
adamc@674
|
1083 newClasses,
|
adamc@674
|
1084 fmap,
|
adamc@674
|
1085 env)
|
adamc@674
|
1086 end
|
adamc@674
|
1087 end)
|
adamc@216
|
1088 | _ => default ()
|
adamc@211
|
1089 end)
|
adamc@218
|
1090 (classes, IM.empty, (IM.empty, IM.empty, IM.empty), env) sgis
|
adamc@211
|
1091 in
|
adamc@211
|
1092 classes
|
adamc@211
|
1093 end
|
adamc@211
|
1094 | _ => classes
|
adamc@211
|
1095
|
adamc@211
|
1096 fun pushStrNamedAs (env : env) x n sgn =
|
adamc@623
|
1097 {renameK = #renameK env,
|
adamc@623
|
1098 relK = #relK env,
|
adamc@623
|
1099
|
adamc@623
|
1100 renameC = #renameC env,
|
adamc@211
|
1101 relC = #relC env,
|
adamc@211
|
1102 namedC = #namedC env,
|
adamc@211
|
1103
|
adamc@211
|
1104 datatypes = #datatypes env,
|
adamc@211
|
1105 constructors = #constructors env,
|
adamc@211
|
1106
|
adamc@211
|
1107 classes = enrichClasses env (#classes env) (n, []) sgn,
|
adamc@211
|
1108
|
adamc@211
|
1109 renameE = #renameE env,
|
adamc@211
|
1110 relE = #relE env,
|
adamc@211
|
1111 namedE = #namedE env,
|
adamc@211
|
1112
|
adamc@211
|
1113 renameSgn = #renameSgn env,
|
adamc@211
|
1114 sgn = #sgn env,
|
adamc@211
|
1115
|
adamc@211
|
1116 renameStr = SM.insert (#renameStr env, x, (n, sgn)),
|
adamc@211
|
1117 str = IM.insert (#str env, n, (x, sgn))}
|
adamc@211
|
1118
|
adamc@211
|
1119 fun pushStrNamed env x sgn =
|
adamc@211
|
1120 let
|
adamc@211
|
1121 val n = !namedCounter
|
adamc@211
|
1122 in
|
adamc@211
|
1123 namedCounter := n + 1;
|
adamc@211
|
1124 (pushStrNamedAs env x n sgn, n)
|
adamc@211
|
1125 end
|
adamc@211
|
1126
|
adamc@211
|
1127 fun sgiBinds env (sgi, loc) =
|
adamc@211
|
1128 case sgi of
|
adamc@211
|
1129 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
|
adamc@211
|
1130 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@211
|
1131 | SgiDatatype (x, n, xs, xncs) =>
|
adamc@211
|
1132 let
|
adamc@341
|
1133 val k = (KType, loc)
|
adamc@341
|
1134 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
|
adamc@341
|
1135
|
adamc@341
|
1136 val env = pushCNamedAs env x n k' NONE
|
adamc@211
|
1137 in
|
adamc@211
|
1138 foldl (fn ((x', n', to), env) =>
|
adamc@211
|
1139 let
|
adamc@211
|
1140 val t =
|
adamc@211
|
1141 case to of
|
adamc@211
|
1142 NONE => (CNamed n, loc)
|
adamc@211
|
1143 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@211
|
1144
|
adamc@211
|
1145 val k = (KType, loc)
|
adamc@211
|
1146 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@211
|
1147 in
|
adamc@211
|
1148 pushENamedAs env x' n' t
|
adamc@211
|
1149 end)
|
adamc@211
|
1150 env xncs
|
adamc@211
|
1151 end
|
adamc@211
|
1152 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
|
adamc@211
|
1153 let
|
adamc@341
|
1154 val k = (KType, loc)
|
adamc@341
|
1155 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
|
adamc@341
|
1156
|
adamc@341
|
1157 val env = pushCNamedAs env x n k' (SOME (CModProj (m1, ms, x'), loc))
|
adamc@211
|
1158 in
|
adamc@211
|
1159 foldl (fn ((x', n', to), env) =>
|
adamc@211
|
1160 let
|
adamc@211
|
1161 val t =
|
adamc@211
|
1162 case to of
|
adamc@211
|
1163 NONE => (CNamed n, loc)
|
adamc@211
|
1164 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@211
|
1165
|
adamc@211
|
1166 val k = (KType, loc)
|
adamc@211
|
1167 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@211
|
1168 in
|
adamc@211
|
1169 pushENamedAs env x' n' t
|
adamc@211
|
1170 end)
|
adamc@211
|
1171 env xncs
|
adamc@211
|
1172 end
|
adamc@211
|
1173 | SgiVal (x, n, t) => pushENamedAs env x n t
|
adamc@211
|
1174 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@211
|
1175 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@211
|
1176 | SgiConstraint _ => env
|
adamc@211
|
1177
|
adamc@711
|
1178 | SgiClassAbs (x, n, k) => pushCNamedAs env x n k NONE
|
adamc@711
|
1179 | SgiClass (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@459
|
1180
|
adamc@211
|
1181 fun sgnSubCon x =
|
adamc@211
|
1182 ElabUtil.Con.map {kind = id,
|
adamc@211
|
1183 con = sgnS_con x}
|
adamc@59
|
1184
|
adamc@59
|
1185 fun projectStr env {sgn, str, field} =
|
adamc@59
|
1186 case #1 (hnormSgn env sgn) of
|
adamc@59
|
1187 SgnConst sgis =>
|
adamc@59
|
1188 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
1189 NONE => NONE
|
adamc@59
|
1190 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
1191 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
1192 | _ => NONE
|
adamc@59
|
1193
|
adamc@158
|
1194 fun chaseMpath env (n, ms) =
|
adamc@158
|
1195 let
|
adamc@158
|
1196 val (_, sgn) = lookupStrNamed env n
|
adamc@158
|
1197 in
|
adamc@158
|
1198 foldl (fn (m, (str, sgn)) =>
|
adamc@158
|
1199 case projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@158
|
1200 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@158
|
1201 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
|
adamc@158
|
1202 ((StrVar n, #2 sgn), sgn) ms
|
adamc@158
|
1203 end
|
adamc@158
|
1204
|
adamc@42
|
1205 fun projectCon env {sgn, str, field} =
|
adamc@42
|
1206 case #1 (hnormSgn env sgn) of
|
adamc@34
|
1207 SgnConst sgis =>
|
adamc@34
|
1208 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
|
adamc@34
|
1209 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
|
adamc@341
|
1210 | SgiDatatype (x, _, xs, _) =>
|
adamc@158
|
1211 if x = field then
|
adamc@341
|
1212 let
|
adamc@341
|
1213 val k = (KType, #2 sgn)
|
adamc@341
|
1214 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
|
adamc@341
|
1215 in
|
adamc@341
|
1216 SOME (k', NONE)
|
adamc@341
|
1217 end
|
adamc@341
|
1218 else
|
adamc@341
|
1219 NONE
|
adamc@341
|
1220 | SgiDatatypeImp (x, _, m1, ms, x', xs, _) =>
|
adamc@341
|
1221 if x = field then
|
adamc@341
|
1222 let
|
adamc@341
|
1223 val k = (KType, #2 sgn)
|
adamc@341
|
1224 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
|
adamc@341
|
1225 in
|
adamc@341
|
1226 SOME (k', SOME (CModProj (m1, ms, x'), #2 sgn))
|
adamc@341
|
1227 end
|
adamc@158
|
1228 else
|
adamc@158
|
1229 NONE
|
adamc@563
|
1230 | SgiClassAbs (x, _, k) => if x = field then
|
adamc@563
|
1231 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), NONE)
|
adamc@563
|
1232 else
|
adamc@563
|
1233 NONE
|
adamc@563
|
1234 | SgiClass (x, _, k, c) => if x = field then
|
adamc@563
|
1235 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), SOME c)
|
adamc@563
|
1236 else
|
adamc@563
|
1237 NONE
|
adamc@34
|
1238 | _ => NONE) sgis of
|
adamc@34
|
1239 NONE => NONE
|
adamc@34
|
1240 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
|
adamc@34
|
1241 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
|
adamc@42
|
1242 | _ => NONE
|
adamc@34
|
1243
|
adamc@157
|
1244 fun projectDatatype env {sgn, str, field} =
|
adamc@157
|
1245 case #1 (hnormSgn env sgn) of
|
adamc@157
|
1246 SgnConst sgis =>
|
adamc@191
|
1247 (case sgnSeek (fn SgiDatatype (x, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
|
adamc@191
|
1248 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
|
adamc@157
|
1249 | _ => NONE) sgis of
|
adamc@157
|
1250 NONE => NONE
|
adamc@191
|
1251 | SOME ((xs, xncs), subs) => SOME (xs,
|
adamc@191
|
1252 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
|
adamc@157
|
1253 | _ => NONE
|
adamc@157
|
1254
|
adamc@174
|
1255 fun projectConstructor env {sgn, str, field} =
|
adamc@174
|
1256 case #1 (hnormSgn env sgn) of
|
adamc@174
|
1257 SgnConst sgis =>
|
adamc@174
|
1258 let
|
adamc@191
|
1259 fun consider (n, xs, xncs) =
|
adamc@174
|
1260 ListUtil.search (fn (x, n', to) =>
|
adamc@174
|
1261 if x <> field then
|
adamc@174
|
1262 NONE
|
adamc@174
|
1263 else
|
adamc@191
|
1264 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
|
adamc@174
|
1265 in
|
adamc@191
|
1266 case sgnSeek (fn SgiDatatype (_, n, xs, xncs) => consider (n, xs, xncs)
|
adamc@191
|
1267 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
|
adamc@174
|
1268 | _ => NONE) sgis of
|
adamc@174
|
1269 NONE => NONE
|
adamc@191
|
1270 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
|
adamc@191
|
1271 sgnSubCon (str, subs) t)
|
adamc@174
|
1272 end
|
adamc@174
|
1273 | _ => NONE
|
adamc@174
|
1274
|
adamc@42
|
1275 fun projectVal env {sgn, str, field} =
|
adamc@42
|
1276 case #1 (hnormSgn env sgn) of
|
adamc@34
|
1277 SgnConst sgis =>
|
adamc@162
|
1278 let
|
adamc@191
|
1279 fun seek (n, xs, xncs) =
|
adamc@162
|
1280 ListUtil.search (fn (x, _, to) =>
|
adamc@162
|
1281 if x = field then
|
adamc@191
|
1282 SOME (let
|
adamc@467
|
1283 val base = (CNamed n, #2 sgn)
|
adamc@467
|
1284 val nxs = length xs
|
adamc@467
|
1285 val base = ListUtil.foldli (fn (i, _, base) =>
|
adamc@467
|
1286 (CApp (base,
|
adamc@467
|
1287 (CRel (nxs - i - 1), #2 sgn)),
|
adamc@467
|
1288 #2 sgn))
|
adamc@467
|
1289 base xs
|
adamc@467
|
1290
|
adamc@191
|
1291 val t =
|
adamc@191
|
1292 case to of
|
adamc@467
|
1293 NONE => base
|
adamc@467
|
1294 | SOME t => (TFun (t, base), #2 sgn)
|
adamc@191
|
1295 val k = (KType, #2 sgn)
|
adamc@191
|
1296 in
|
adamc@467
|
1297 foldr (fn (x, t) => (TCFun (Implicit, x, k, t), #2 sgn))
|
adamc@191
|
1298 t xs
|
adamc@191
|
1299 end)
|
adamc@162
|
1300 else
|
adamc@162
|
1301 NONE) xncs
|
adamc@162
|
1302 in
|
adamc@162
|
1303 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
|
adamc@191
|
1304 | SgiDatatype (_, n, xs, xncs) => seek (n, xs, xncs)
|
adamc@191
|
1305 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
|
adamc@162
|
1306 | _ => NONE) sgis of
|
adamc@162
|
1307 NONE => NONE
|
adamc@162
|
1308 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
|
adamc@162
|
1309 end
|
adamc@34
|
1310 | SgnError => SOME (CError, ErrorMsg.dummySpan)
|
adamc@42
|
1311 | _ => NONE
|
adamc@34
|
1312
|
adamc@88
|
1313 fun sgnSeekConstraints (str, sgis) =
|
adamc@88
|
1314 let
|
adamc@88
|
1315 fun seek (sgis, sgns, strs, cons, acc) =
|
adamc@88
|
1316 case sgis of
|
adamc@88
|
1317 [] => acc
|
adamc@88
|
1318 | (sgi, _) :: sgis =>
|
adamc@88
|
1319 case sgi of
|
adamc@88
|
1320 SgiConstraint (c1, c2) =>
|
adamc@88
|
1321 let
|
adamc@88
|
1322 val sub = sgnSubCon (str, (sgns, strs, cons))
|
adamc@88
|
1323 in
|
adamc@88
|
1324 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
|
adamc@88
|
1325 end
|
adamc@88
|
1326 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1327 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@191
|
1328 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@191
|
1329 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1330 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@88
|
1331 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
|
adamc@88
|
1332 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
|
adamc@563
|
1333 | SgiClassAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@563
|
1334 | SgiClass (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1335 in
|
adamc@88
|
1336 seek (sgis, IM.empty, IM.empty, IM.empty, [])
|
adamc@88
|
1337 end
|
adamc@88
|
1338
|
adamc@88
|
1339 fun projectConstraints env {sgn, str} =
|
adamc@88
|
1340 case #1 (hnormSgn env sgn) of
|
adamc@88
|
1341 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
|
adamc@88
|
1342 | SgnError => SOME []
|
adamc@88
|
1343 | _ => NONE
|
adamc@34
|
1344
|
adamc@447
|
1345 fun edeclBinds env (d, loc) =
|
adamc@447
|
1346 case d of
|
adamc@447
|
1347 EDVal (x, t, _) => pushERel env x t
|
adamc@447
|
1348 | EDValRec vis => foldl (fn ((x, t, _), env) => pushERel env x t) env vis
|
adamc@447
|
1349
|
adamc@157
|
1350 fun declBinds env (d, loc) =
|
adamc@157
|
1351 case d of
|
adamc@157
|
1352 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@191
|
1353 | DDatatype (x, n, xs, xncs) =>
|
adamc@157
|
1354 let
|
adamc@297
|
1355 val k = (KType, loc)
|
adamc@297
|
1356 val nxs = length xs
|
adamc@297
|
1357 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
|
adamc@297
|
1358 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
|
adamc@297
|
1359 (KArrow (k, kb), loc)))
|
adamc@297
|
1360 ((CNamed n, loc), k) xs
|
adamc@297
|
1361
|
adamc@297
|
1362 val env = pushCNamedAs env x n kb NONE
|
adamc@191
|
1363 val env = pushDatatype env n xs xncs
|
adamc@157
|
1364 in
|
adamc@191
|
1365 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
1366 let
|
adamc@191
|
1367 val t =
|
adamc@191
|
1368 case to of
|
adamc@297
|
1369 NONE => tb
|
adamc@297
|
1370 | SOME t => (TFun (t, tb), loc)
|
adamc@297
|
1371 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
|
adamc@191
|
1372 in
|
adamc@191
|
1373 pushENamedAs env x' n' t
|
adamc@191
|
1374 end)
|
adamc@191
|
1375 env xncs
|
adamc@157
|
1376 end
|
adamc@191
|
1377 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
|
adamc@157
|
1378 let
|
adamc@157
|
1379 val t = (CModProj (m, ms, x'), loc)
|
adamc@297
|
1380 val k = (KType, loc)
|
adamc@297
|
1381 val nxs = length xs
|
adamc@297
|
1382 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
|
adamc@297
|
1383 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
|
adamc@297
|
1384 (KArrow (k, kb), loc)))
|
adamc@297
|
1385 ((CNamed n, loc), k) xs
|
adamc@297
|
1386
|
adamc@341
|
1387 val env = pushCNamedAs env x n kb (SOME t)
|
adamc@191
|
1388 val env = pushDatatype env n xs xncs
|
adamc@157
|
1389 in
|
adamc@191
|
1390 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
1391 let
|
adamc@191
|
1392 val t =
|
adamc@191
|
1393 case to of
|
adamc@297
|
1394 NONE => tb
|
adamc@297
|
1395 | SOME t => (TFun (t, tb), loc)
|
adamc@297
|
1396 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
|
adamc@191
|
1397 in
|
adamc@191
|
1398 pushENamedAs env x' n' t
|
adamc@191
|
1399 end)
|
adamc@191
|
1400 env xncs
|
adamc@157
|
1401 end
|
adamc@157
|
1402 | DVal (x, n, t, _) => pushENamedAs env x n t
|
adamc@157
|
1403 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
|
adamc@157
|
1404 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@157
|
1405 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
|
adamc@157
|
1406 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@157
|
1407 | DConstraint _ => env
|
adamc@157
|
1408 | DExport _ => env
|
adamc@707
|
1409 | DTable (tn, x, n, c, _, pc, _, cc) =>
|
adamc@205
|
1410 let
|
adamc@705
|
1411 val ct = (CModProj (tn, [], "sql_table"), loc)
|
adamc@705
|
1412 val ct = (CApp (ct, c), loc)
|
adamc@707
|
1413 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
|
adamc@338
|
1414 in
|
adamc@705
|
1415 pushENamedAs env x n ct
|
adamc@338
|
1416 end
|
adamc@338
|
1417 | DSequence (tn, x, n) =>
|
adamc@338
|
1418 let
|
adamc@338
|
1419 val t = (CModProj (tn, [], "sql_sequence"), loc)
|
adamc@205
|
1420 in
|
adamc@205
|
1421 pushENamedAs env x n t
|
adamc@205
|
1422 end
|
adamc@563
|
1423 | DClass (x, n, k, c) =>
|
adamc@213
|
1424 let
|
adamc@563
|
1425 val k = (KArrow (k, (KType, loc)), loc)
|
adamc@213
|
1426 val env = pushCNamedAs env x n k (SOME c)
|
adamc@213
|
1427 in
|
adamc@213
|
1428 pushClass env n
|
adamc@213
|
1429 end
|
adamc@271
|
1430 | DDatabase _ => env
|
adamc@459
|
1431 | DCookie (tn, x, n, c) =>
|
adamc@459
|
1432 let
|
adamc@459
|
1433 val t = (CApp ((CModProj (tn, [], "cookie"), loc), c), loc)
|
adamc@459
|
1434 in
|
adamc@459
|
1435 pushENamedAs env x n t
|
adamc@459
|
1436 end
|
adamc@720
|
1437 | DStyle (tn, x, n) =>
|
adamc@718
|
1438 let
|
adamc@720
|
1439 val t = (CModProj (tn, [], "css_class"), loc)
|
adamc@718
|
1440 in
|
adamc@718
|
1441 pushENamedAs env x n t
|
adamc@718
|
1442 end
|
adamc@157
|
1443
|
adamc@243
|
1444 fun patBinds env (p, loc) =
|
adamc@243
|
1445 case p of
|
adamc@243
|
1446 PWild => env
|
adamc@243
|
1447 | PVar (x, t) => pushERel env x t
|
adamc@243
|
1448 | PPrim _ => env
|
adamc@243
|
1449 | PCon (_, _, _, NONE) => env
|
adamc@243
|
1450 | PCon (_, _, _, SOME p) => patBinds env p
|
adamc@243
|
1451 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
|
adamc@243
|
1452
|
adamc@2
|
1453 end
|