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@750
|
510 | (CUnif _, _) => ()
|
adamc@750
|
511
|
adamc@711
|
512 | (c1', CRel n2) =>
|
adamc@711
|
513 if n2 < d then
|
adamc@711
|
514 case c1' of
|
adamc@711
|
515 CRel n1 => if n1 = n2 then () else raise Unify
|
adamc@711
|
516 | _ => raise Unify
|
adamc@711
|
517 else if n2 - d >= length rs then
|
adamc@711
|
518 case c1' of
|
adamc@711
|
519 CRel n1 => if n1 = n2 - length rs then () else raise Unify
|
adamc@711
|
520 | _ => raise Unify
|
adamc@711
|
521 else
|
adamc@711
|
522 let
|
adamc@711
|
523 val r = List.nth (rs, n2 - d)
|
adamc@711
|
524 in
|
adamc@711
|
525 case !r of
|
adamc@711
|
526 NONE => r := SOME c1
|
adamc@711
|
527 | SOME c2 => unify d (c1, c2)
|
adamc@711
|
528 end
|
adamc@711
|
529
|
adamc@711
|
530 | (TFun (d1, r1), TFun (d2, r2)) => (unify d (d1, d2); unify d (r1, r2))
|
adamc@711
|
531 | (TCFun (_, _, k1, r1), TCFun (_, _, k2, r2)) => (unifyKinds (k1, k2); unify (d + 1) (r1, r2))
|
adamc@711
|
532 | (TRecord c1, TRecord c2) => unify d (c1, c2)
|
adamc@711
|
533 | (TDisjoint (a1, b1, c1), TDisjoint (a2, b2, c2)) =>
|
adamc@711
|
534 (unify d (a1, a2); unify d (b1, b2); unify d (c1, c2))
|
adamc@711
|
535
|
adamc@711
|
536 | (CNamed n1, CNamed n2) => if n1 = n2 then () else raise Unify
|
adamc@711
|
537 | (CModProj (n1, ms1, x1), CModProj (n2, ms2, x2)) =>
|
adamc@711
|
538 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then () else raise Unify
|
adamc@711
|
539 | (CApp (f1, x1), CApp (f2, x2)) => (unify d (f1, f2); unify d (x1, x2))
|
adamc@711
|
540 | (CAbs (_, k1, b1), CAbs (_, k2, b2)) => (unifyKinds (k1, k2); unify (d + 1) (b1, b2))
|
adamc@711
|
541
|
adamc@711
|
542 | (CKAbs (_, b1), CKAbs (_, b2)) => unify d (b1, b2)
|
adamc@711
|
543 | (CKApp (c1, k1), CKApp (c2, k2)) => (unify d (c1, c2); unifyKinds (k1, k2))
|
adamc@711
|
544 | (TKFun (_, c1), TKFun (_, c2)) => unify d (c1, c2)
|
adamc@711
|
545
|
adamc@711
|
546 | (CName s1, CName s2) => if s1 = s2 then () else raise Unify
|
adamc@711
|
547
|
adamc@711
|
548 | (CRecord (k1, xcs1), CRecord (k2, xcs2)) =>
|
adamc@711
|
549 (unifyKinds (k1, k2);
|
adamc@711
|
550 ListPair.appEq (fn ((x1, c1), (x2, c2)) => (unify d (x1, x2); unify d (c1, c2))) (xcs1, xcs2)
|
adamc@711
|
551 handle ListPair.UnequalLengths => raise Unify)
|
adamc@711
|
552 | (CConcat (f1, x1), CConcat (f2, x2)) => (unify d (f1, f2); unify d (x1, x2))
|
adamc@711
|
553 | (CMap (d1, r1), CMap (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
|
adamc@711
|
554
|
adamc@711
|
555 | (CUnit, CUnit) => ()
|
adamc@711
|
556
|
adamc@711
|
557 | (CTuple cs1, CTuple cs2) => (ListPair.appEq (unify d) (cs1, cs2)
|
adamc@711
|
558 handle ListPair.UnequalLengths => raise Unify)
|
adamc@711
|
559 | (CProj (c1, n1), CProj (c2, n2)) => (unify d (c1, c2);
|
adamc@711
|
560 if n1 = n2 then () else raise Unify)
|
adamc@711
|
561
|
adamc@711
|
562 | _ => raise Unify
|
adamc@674
|
563 in
|
adamc@711
|
564 unify
|
adamc@674
|
565 end
|
adamc@674
|
566
|
adamc@711
|
567 fun tryUnify nRs (c1, c2) =
|
adamc@674
|
568 let
|
adamc@711
|
569 val rs = List.tabulate (nRs, fn _ => ref NONE)
|
adamc@674
|
570 in
|
adamc@711
|
571 (unifyCons rs 0 (c1, c2);
|
adamc@711
|
572 SOME (map (fn r => case !r of
|
adamc@711
|
573 NONE => raise Unify
|
adamc@711
|
574 | SOME c => c) rs))
|
adamc@711
|
575 handle Unify => NONE
|
adamc@674
|
576 end
|
adamc@674
|
577
|
adamc@711
|
578 fun unifySubst (rs : con list) =
|
adamc@711
|
579 U.Con.mapB {kind = fn _ => fn k => k,
|
adamc@711
|
580 con = fn d => fn c =>
|
adamc@711
|
581 case c of
|
adamc@711
|
582 CRel n =>
|
adamc@711
|
583 if n < d then
|
adamc@711
|
584 c
|
adamc@711
|
585 else
|
adamc@711
|
586 #1 (List.nth (rs, n - d))
|
adamc@711
|
587 | _ => c,
|
adamc@711
|
588 bind = fn (d, U.Con.RelC _) => d + 1
|
adamc@711
|
589 | (d, _) => d}
|
adamc@711
|
590 0
|
adamc@711
|
591
|
adamc@753
|
592 exception Bad of con * con
|
adamc@750
|
593
|
adamc@754
|
594 val hasUnif = U.Con.exists {kind = fn _ => false,
|
adamc@754
|
595 con = fn CUnif (_, _, _, ref NONE) => true
|
adamc@754
|
596 | _ => false}
|
adamc@754
|
597
|
adamc@754
|
598 fun startsWithUnif c =
|
adamc@754
|
599 let
|
adamc@754
|
600 fun firstArg (c, acc) =
|
adamc@754
|
601 case #1 c of
|
adamc@754
|
602 CApp (f, x) => firstArg (f, SOME x)
|
adamc@754
|
603 | _ => acc
|
adamc@754
|
604 in
|
adamc@754
|
605 case firstArg (c, NONE) of
|
adamc@754
|
606 NONE => false
|
adamc@754
|
607 | SOME x => hasUnif x
|
adamc@754
|
608 end
|
adamc@754
|
609
|
adamc@753
|
610 fun resolveClass (hnorm : con -> con) (consEq : con * con -> bool) (env : env) =
|
adamc@674
|
611 let
|
adamc@711
|
612 fun resolve c =
|
adamc@711
|
613 let
|
adamc@711
|
614 fun doHead f =
|
adamc@711
|
615 case CM.find (#classes env, f) of
|
adamc@711
|
616 NONE => NONE
|
adamc@711
|
617 | SOME class =>
|
adamc@711
|
618 let
|
adamc@711
|
619 val loc = #2 c
|
adamc@675
|
620
|
adamc@753
|
621 fun generalize (c as (_, loc)) =
|
adamc@753
|
622 case #1 c of
|
adamc@753
|
623 CApp (f, x) =>
|
adamc@753
|
624 let
|
adamc@753
|
625 val (f, equate) = generalize f
|
adamc@753
|
626
|
adamc@753
|
627 fun isRecord () =
|
adamc@753
|
628 let
|
adamc@753
|
629 val rk = ref NONE
|
adamc@753
|
630 val k = (KUnif (loc, "k", rk), loc)
|
adamc@753
|
631 val r = ref NONE
|
adamc@753
|
632 val rc = (CUnif (loc, k, "x", r), loc)
|
adamc@753
|
633 in
|
adamc@753
|
634 ((CApp (f, rc), loc),
|
adamc@753
|
635 fn () => (if consEq (rc, x) then
|
adamc@753
|
636 true
|
adamc@753
|
637 else
|
adamc@753
|
638 (raise Bad (rc, x);
|
adamc@753
|
639 false))
|
adamc@753
|
640 andalso equate ())
|
adamc@753
|
641 end
|
adamc@753
|
642 in
|
adamc@753
|
643 case #1 x of
|
adamc@753
|
644 CConcat _ => isRecord ()
|
adamc@753
|
645 | CRecord _ => isRecord ()
|
adamc@753
|
646 | _ => ((CApp (f, x), loc), equate)
|
adamc@753
|
647 end
|
adamc@753
|
648 | _ => (c, fn () => true)
|
adamc@753
|
649
|
adamc@753
|
650 val (c, equate) = generalize c
|
adamc@753
|
651
|
adamc@711
|
652 fun tryRules rules =
|
adamc@711
|
653 case rules of
|
adamc@675
|
654 [] => NONE
|
adamc@711
|
655 | (nRs, cs, c', e) :: rules' =>
|
adamc@711
|
656 case tryUnify nRs (c, c') of
|
adamc@711
|
657 NONE => tryRules rules'
|
adamc@711
|
658 | SOME rs =>
|
adamc@675
|
659 let
|
adamc@711
|
660 val eos = map (resolve o unifySubst rs) cs
|
adamc@675
|
661 in
|
adamc@750
|
662 if List.exists (not o Option.isSome) eos
|
adamc@753
|
663 orelse not (equate ())
|
adamc@753
|
664 orelse not (consEq (c, unifySubst rs c')) then
|
adamc@711
|
665 tryRules rules'
|
adamc@711
|
666 else
|
adamc@711
|
667 let
|
adamc@711
|
668 val es = List.mapPartial (fn x => x) eos
|
adamc@711
|
669
|
adamc@711
|
670 val e = foldr (fn (c, e) => (ECApp (e, c), loc)) e rs
|
adamc@711
|
671 val e = foldl (fn (e', e) => (EApp (e, e'), loc)) e es
|
adamc@711
|
672 in
|
adamc@711
|
673 SOME e
|
adamc@711
|
674 end
|
adamc@675
|
675 end
|
adamc@711
|
676
|
adamc@711
|
677 fun rules () = tryRules (#rules class)
|
adamc@711
|
678
|
adamc@711
|
679 fun tryGrounds ces =
|
adamc@711
|
680 case ces of
|
adamc@711
|
681 [] => rules ()
|
adamc@711
|
682 | (c', e) :: ces' =>
|
adamc@711
|
683 case tryUnify 0 (c, c') of
|
adamc@711
|
684 NONE => tryGrounds ces'
|
adamc@711
|
685 | SOME _ => SOME e
|
adamc@675
|
686 in
|
adamc@711
|
687 tryGrounds (#ground class)
|
adamc@675
|
688 end
|
adamc@711
|
689 in
|
adamc@754
|
690 if startsWithUnif c then
|
adamc@754
|
691 NONE
|
adamc@754
|
692 else
|
adamc@754
|
693 case #1 c of
|
adamc@754
|
694 TRecord c =>
|
adamc@754
|
695 (case #1 (hnorm c) of
|
adamc@754
|
696 CRecord (_, xts) =>
|
adamc@754
|
697 let
|
adamc@754
|
698 fun resolver (xts, acc) =
|
adamc@754
|
699 case xts of
|
adamc@754
|
700 [] => SOME (ERecord acc, #2 c)
|
adamc@754
|
701 | (x, t) :: xts =>
|
adamc@754
|
702 let
|
adamc@754
|
703 val t = hnorm t
|
adamc@750
|
704
|
adamc@754
|
705 val t = case t of
|
adamc@754
|
706 (CApp (f, x), loc) => (CApp (hnorm f, hnorm x), loc)
|
adamc@754
|
707 | _ => t
|
adamc@754
|
708 in
|
adamc@754
|
709 case resolve t of
|
adamc@754
|
710 NONE => NONE
|
adamc@754
|
711 | SOME e => resolver (xts, (x, e, t) :: acc)
|
adamc@754
|
712 end
|
adamc@754
|
713 in
|
adamc@754
|
714 resolver (xts, [])
|
adamc@754
|
715 end
|
adamc@754
|
716 | _ => NONE)
|
adamc@754
|
717 | _ =>
|
adamc@754
|
718 case class_head_in c of
|
adamc@754
|
719 SOME f => doHead f
|
adamc@754
|
720 | _ => NONE
|
adamc@711
|
721 end
|
adamc@674
|
722 in
|
adamc@711
|
723 resolve
|
adamc@674
|
724 end
|
adamc@211
|
725
|
adamc@9
|
726 fun pushERel (env : env) x t =
|
adamc@9
|
727 let
|
adamc@9
|
728 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
|
adamc@9
|
729 | x => x) (#renameE env)
|
adamc@211
|
730
|
adamc@675
|
731 val classes = CM.map (fn class =>
|
adamc@711
|
732 {ground = map (fn (c, e) => (c, liftExp e)) (#ground class),
|
adamc@711
|
733 rules = #rules class}) (#classes env)
|
adamc@711
|
734 val classes = case class_head_in t of
|
adamc@211
|
735 NONE => classes
|
adamc@711
|
736 | SOME f =>
|
adamc@403
|
737 case CM.find (classes, f) of
|
adamc@403
|
738 NONE => classes
|
adamc@403
|
739 | SOME class =>
|
adamc@403
|
740 let
|
adamc@711
|
741 val class = {ground = (t, (ERel 0, #2 t)) :: #ground class,
|
adamc@711
|
742 rules = #rules class}
|
adamc@403
|
743 in
|
adamc@403
|
744 CM.insert (classes, f, class)
|
adamc@403
|
745 end
|
adamc@9
|
746 in
|
adamc@623
|
747 {renameK = #renameK env,
|
adamc@623
|
748 relK = #relK env,
|
adamc@623
|
749
|
adamc@623
|
750 renameC = #renameC env,
|
adamc@9
|
751 relC = #relC env,
|
adamc@9
|
752 namedC = #namedC env,
|
adamc@9
|
753
|
adamc@157
|
754 datatypes = #datatypes env,
|
adamc@171
|
755 constructors = #constructors env,
|
adamc@157
|
756
|
adamc@211
|
757 classes = classes,
|
adamc@211
|
758
|
adamc@9
|
759 renameE = SM.insert (renameE, x, Rel' (0, t)),
|
adamc@9
|
760 relE = (x, t) :: #relE env,
|
adamc@31
|
761 namedE = #namedE env,
|
adamc@31
|
762
|
adamc@31
|
763 renameSgn = #renameSgn env,
|
adamc@31
|
764 sgn = #sgn env,
|
adamc@31
|
765
|
adamc@31
|
766 renameStr = #renameStr env,
|
adamc@31
|
767 str = #str env}
|
adamc@9
|
768 end
|
adamc@9
|
769
|
adamc@9
|
770 fun lookupERel (env : env) n =
|
adamc@9
|
771 (List.nth (#relE env, n))
|
adamc@9
|
772 handle Subscript => raise UnboundRel n
|
adamc@9
|
773
|
adamc@674
|
774 fun rule_in c =
|
adamc@674
|
775 let
|
adamc@674
|
776 fun quantifiers (c, nvars) =
|
adamc@674
|
777 case #1 c of
|
adamc@674
|
778 TCFun (_, _, _, c) => quantifiers (c, nvars + 1)
|
adamc@674
|
779 | _ =>
|
adamc@674
|
780 let
|
adamc@674
|
781 fun clauses (c, hyps) =
|
adamc@674
|
782 case #1 c of
|
adamc@674
|
783 TFun (hyp, c) =>
|
adamc@711
|
784 (case class_head_in hyp of
|
adamc@711
|
785 SOME _ => clauses (c, hyp :: hyps)
|
adamc@711
|
786 | NONE => NONE)
|
adamc@674
|
787 | _ =>
|
adamc@711
|
788 case class_head_in c of
|
adamc@674
|
789 NONE => NONE
|
adamc@711
|
790 | SOME f => SOME (f, nvars, rev hyps, c)
|
adamc@674
|
791 in
|
adamc@674
|
792 clauses (c, [])
|
adamc@674
|
793 end
|
adamc@674
|
794 in
|
adamc@711
|
795 quantifiers (c, 0)
|
adamc@677
|
796 end
|
adamc@677
|
797
|
adamc@9
|
798 fun pushENamedAs (env : env) x n t =
|
adamc@211
|
799 let
|
adamc@211
|
800 val classes = #classes env
|
adamc@674
|
801 val classes = case rule_in t of
|
adamc@211
|
802 NONE => classes
|
adamc@711
|
803 | SOME (f, nvs, cs, c) =>
|
adamc@403
|
804 case CM.find (classes, f) of
|
adamc@403
|
805 NONE => classes
|
adamc@403
|
806 | SOME class =>
|
adamc@403
|
807 let
|
adamc@675
|
808 val e = (ENamed n, #2 t)
|
adamc@675
|
809
|
adamc@675
|
810 val class =
|
adamc@711
|
811 {ground = #ground class,
|
adamc@711
|
812 rules = (nvs, cs, c, e) :: #rules class}
|
adamc@403
|
813 in
|
adamc@403
|
814 CM.insert (classes, f, class)
|
adamc@403
|
815 end
|
adamc@211
|
816 in
|
adamc@623
|
817 {renameK = #renameK env,
|
adamc@623
|
818 relK = #relK env,
|
adamc@623
|
819
|
adamc@623
|
820 renameC = #renameC env,
|
adamc@211
|
821 relC = #relC env,
|
adamc@211
|
822 namedC = #namedC env,
|
adamc@9
|
823
|
adamc@211
|
824 datatypes = #datatypes env,
|
adamc@211
|
825 constructors = #constructors env,
|
adamc@157
|
826
|
adamc@211
|
827 classes = classes,
|
adamc@31
|
828
|
adamc@211
|
829 renameE = SM.insert (#renameE env, x, Named' (n, t)),
|
adamc@211
|
830 relE = #relE env,
|
adamc@211
|
831 namedE = IM.insert (#namedE env, n, (x, t)),
|
adamc@211
|
832
|
adamc@211
|
833 renameSgn = #renameSgn env,
|
adamc@211
|
834 sgn = #sgn env,
|
adamc@211
|
835
|
adamc@211
|
836 renameStr = #renameStr env,
|
adamc@211
|
837 str = #str env}
|
adamc@211
|
838 end
|
adamc@9
|
839
|
adamc@9
|
840 fun pushENamed env x t =
|
adamc@9
|
841 let
|
adamc@9
|
842 val n = !namedCounter
|
adamc@9
|
843 in
|
adamc@9
|
844 namedCounter := n + 1;
|
adamc@9
|
845 (pushENamedAs env x n t, n)
|
adamc@9
|
846 end
|
adamc@9
|
847
|
adamc@9
|
848 fun lookupENamed (env : env) n =
|
adamc@9
|
849 case IM.find (#namedE env, n) of
|
adamc@9
|
850 NONE => raise UnboundNamed n
|
adamc@9
|
851 | SOME x => x
|
adamc@9
|
852
|
adamc@471
|
853 fun checkENamed (env : env) n =
|
adamc@471
|
854 Option.isSome (IM.find (#namedE env, n))
|
adamc@471
|
855
|
adamc@9
|
856 fun lookupE (env : env) x =
|
adamc@9
|
857 case SM.find (#renameE env, x) of
|
adamc@9
|
858 NONE => NotBound
|
adamc@9
|
859 | SOME (Rel' x) => Rel x
|
adamc@9
|
860 | SOME (Named' x) => Named x
|
adamc@2
|
861
|
adamc@31
|
862 fun pushSgnNamedAs (env : env) x n sgis =
|
adamc@623
|
863 {renameK = #renameK env,
|
adamc@623
|
864 relK = #relK env,
|
adamc@623
|
865
|
adamc@623
|
866 renameC = #renameC env,
|
adamc@31
|
867 relC = #relC env,
|
adamc@31
|
868 namedC = #namedC env,
|
adamc@31
|
869
|
adamc@157
|
870 datatypes = #datatypes env,
|
adamc@171
|
871 constructors = #constructors env,
|
adamc@157
|
872
|
adamc@211
|
873 classes = #classes env,
|
adamc@211
|
874
|
adamc@31
|
875 renameE = #renameE env,
|
adamc@31
|
876 relE = #relE env,
|
adamc@31
|
877 namedE = #namedE env,
|
adamc@31
|
878
|
adamc@31
|
879 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
|
adamc@31
|
880 sgn = IM.insert (#sgn env, n, (x, sgis)),
|
adamc@31
|
881
|
adamc@31
|
882 renameStr = #renameStr env,
|
adamc@31
|
883 str = #str env}
|
adamc@31
|
884
|
adamc@31
|
885 fun pushSgnNamed env x sgis =
|
adamc@31
|
886 let
|
adamc@31
|
887 val n = !namedCounter
|
adamc@31
|
888 in
|
adamc@31
|
889 namedCounter := n + 1;
|
adamc@31
|
890 (pushSgnNamedAs env x n sgis, n)
|
adamc@31
|
891 end
|
adamc@31
|
892
|
adamc@31
|
893 fun lookupSgnNamed (env : env) n =
|
adamc@31
|
894 case IM.find (#sgn env, n) of
|
adamc@31
|
895 NONE => raise UnboundNamed n
|
adamc@31
|
896 | SOME x => x
|
adamc@31
|
897
|
adamc@31
|
898 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
|
adamc@31
|
899
|
adamc@31
|
900 fun lookupStrNamed (env : env) n =
|
adamc@31
|
901 case IM.find (#str env, n) of
|
adamc@31
|
902 NONE => raise UnboundNamed n
|
adamc@31
|
903 | SOME x => x
|
adamc@31
|
904
|
adamc@31
|
905 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
|
adamc@31
|
906
|
adamc@203
|
907
|
adamc@216
|
908 fun sgiSeek (sgi, (sgns, strs, cons)) =
|
adamc@216
|
909 case sgi of
|
adamc@216
|
910 SgiConAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
911 | SgiCon (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@805
|
912 | SgiDatatype dts => (sgns, strs, foldl (fn ((x, n, _, _), cons) => IM.insert (cons, n, x)) cons dts)
|
adamc@216
|
913 | SgiDatatypeImp (x, n, _, _, _, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
914 | SgiVal _ => (sgns, strs, cons)
|
adamc@216
|
915 | SgiSgn (x, n, _) => (IM.insert (sgns, n, x), strs, cons)
|
adamc@216
|
916 | SgiStr (x, n, _) => (sgns, IM.insert (strs, n, x), cons)
|
adamc@216
|
917 | SgiConstraint _ => (sgns, strs, cons)
|
adamc@563
|
918 | SgiClassAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@563
|
919 | SgiClass (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
|
adamc@216
|
920
|
adamc@34
|
921 fun sgnSeek f sgis =
|
adamc@34
|
922 let
|
adamc@59
|
923 fun seek (sgis, sgns, strs, cons) =
|
adamc@34
|
924 case sgis of
|
adamc@34
|
925 [] => NONE
|
adamc@34
|
926 | (sgi, _) :: sgis =>
|
adamc@34
|
927 case f sgi of
|
adamc@158
|
928 SOME v =>
|
adamc@158
|
929 let
|
adamc@158
|
930 val cons =
|
adamc@158
|
931 case sgi of
|
adamc@805
|
932 SgiDatatype dts => foldl (fn ((x, n, _, _), cons) => IM.insert (cons, n, x)) cons dts
|
adamc@191
|
933 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
|
adamc@158
|
934 | _ => cons
|
adamc@158
|
935 in
|
adamc@158
|
936 SOME (v, (sgns, strs, cons))
|
adamc@158
|
937 end
|
adamc@88
|
938 | NONE =>
|
adamc@216
|
939 let
|
adamc@216
|
940 val (sgns, strs, cons) = sgiSeek (sgi, (sgns, strs, cons))
|
adamc@216
|
941 in
|
adamc@216
|
942 seek (sgis, sgns, strs, cons)
|
adamc@216
|
943 end
|
adamc@34
|
944 in
|
adamc@59
|
945 seek (sgis, IM.empty, IM.empty, IM.empty)
|
adamc@34
|
946 end
|
adamc@34
|
947
|
adamc@34
|
948 fun id x = x
|
adamc@34
|
949
|
adamc@34
|
950 fun unravelStr (str, _) =
|
adamc@34
|
951 case str of
|
adamc@34
|
952 StrVar x => (x, [])
|
adamc@34
|
953 | StrProj (str, m) =>
|
adamc@34
|
954 let
|
adamc@34
|
955 val (x, ms) = unravelStr str
|
adamc@34
|
956 in
|
adamc@34
|
957 (x, ms @ [m])
|
adamc@34
|
958 end
|
adamc@34
|
959 | _ => raise Fail "unravelStr"
|
adamc@34
|
960
|
adamc@59
|
961 fun sgnS_con (str, (sgns, strs, cons)) c =
|
adamc@34
|
962 case c of
|
adamc@34
|
963 CModProj (m1, ms, x) =>
|
adamc@34
|
964 (case IM.find (strs, m1) of
|
adamc@34
|
965 NONE => c
|
adamc@34
|
966 | SOME m1x =>
|
adamc@34
|
967 let
|
adamc@34
|
968 val (m1, ms') = unravelStr str
|
adamc@34
|
969 in
|
adamc@34
|
970 CModProj (m1, ms' @ m1x :: ms, x)
|
adamc@34
|
971 end)
|
adamc@34
|
972 | CNamed n =>
|
adamc@34
|
973 (case IM.find (cons, n) of
|
adamc@34
|
974 NONE => c
|
adamc@34
|
975 | SOME nx =>
|
adamc@34
|
976 let
|
adamc@34
|
977 val (m1, ms) = unravelStr str
|
adamc@34
|
978 in
|
adamc@34
|
979 CModProj (m1, ms, nx)
|
adamc@34
|
980 end)
|
adamc@34
|
981 | _ => c
|
adamc@34
|
982
|
adamc@721
|
983 fun sgnS_con' (m1, ms', (sgns, strs, cons)) =
|
adamc@721
|
984 U.Con.map {kind = fn x => x,
|
adamc@721
|
985 con = fn c =>
|
adamc@721
|
986 case c of
|
adamc@721
|
987 CModProj (m1, ms, x) =>
|
adamc@721
|
988 (case IM.find (strs, m1) of
|
adamc@721
|
989 NONE => c
|
adamc@721
|
990 | SOME m1x => CModProj (m1, ms' @ m1x :: ms, x))
|
adamc@721
|
991 | CNamed n =>
|
adamc@721
|
992 (case IM.find (cons, n) of
|
adamc@721
|
993 NONE => c
|
adamc@721
|
994 | SOME nx => CModProj (m1, ms', nx))
|
adamc@721
|
995 | _ => c}
|
adamc@216
|
996
|
adamc@59
|
997 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
|
adamc@59
|
998 case sgn of
|
adamc@59
|
999 SgnProj (m1, ms, x) =>
|
adamc@59
|
1000 (case IM.find (strs, m1) of
|
adamc@59
|
1001 NONE => sgn
|
adamc@59
|
1002 | SOME m1x =>
|
adamc@59
|
1003 let
|
adamc@59
|
1004 val (m1, ms') = unravelStr str
|
adamc@59
|
1005 in
|
adamc@59
|
1006 SgnProj (m1, ms' @ m1x :: ms, x)
|
adamc@59
|
1007 end)
|
adamc@59
|
1008 | SgnVar n =>
|
adamc@59
|
1009 (case IM.find (sgns, n) of
|
adamc@59
|
1010 NONE => sgn
|
adamc@59
|
1011 | SOME nx =>
|
adamc@59
|
1012 let
|
adamc@59
|
1013 val (m1, ms) = unravelStr str
|
adamc@59
|
1014 in
|
adamc@59
|
1015 SgnProj (m1, ms, nx)
|
adamc@59
|
1016 end)
|
adamc@59
|
1017 | _ => sgn
|
adamc@59
|
1018
|
adamc@59
|
1019 fun sgnSubSgn x =
|
adamc@34
|
1020 ElabUtil.Sgn.map {kind = id,
|
adamc@59
|
1021 con = sgnS_con x,
|
adamc@34
|
1022 sgn_item = id,
|
adamc@59
|
1023 sgn = sgnS_sgn x}
|
adamc@34
|
1024
|
adamc@211
|
1025
|
adamc@211
|
1026
|
adamc@211
|
1027 and projectSgn env {sgn, str, field} =
|
adamc@211
|
1028 case #1 (hnormSgn env sgn) of
|
adamc@211
|
1029 SgnConst sgis =>
|
adamc@211
|
1030 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@211
|
1031 NONE => NONE
|
adamc@211
|
1032 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@211
|
1033 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@211
|
1034 | _ => NONE
|
adamc@211
|
1035
|
adamc@211
|
1036 and hnormSgn env (all as (sgn, loc)) =
|
adamc@34
|
1037 case sgn of
|
adamc@42
|
1038 SgnError => all
|
adamc@42
|
1039 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
|
adamc@42
|
1040 | SgnConst _ => all
|
adamc@42
|
1041 | SgnFun _ => all
|
adamc@59
|
1042 | SgnProj (m, ms, x) =>
|
adamc@59
|
1043 let
|
adamc@59
|
1044 val (_, sgn) = lookupStrNamed env m
|
adamc@59
|
1045 in
|
adamc@59
|
1046 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
|
adamc@59
|
1047 sgn = sgn,
|
adamc@59
|
1048 field = x} of
|
adamc@59
|
1049 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
|
adamc@59
|
1050 | SOME sgn => sgn
|
adamc@59
|
1051 end
|
adamc@42
|
1052 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
1053 case #1 (hnormSgn env sgn) of
|
adamc@42
|
1054 SgnError => (SgnError, loc)
|
adamc@42
|
1055 | SgnConst sgis =>
|
adamc@42
|
1056 let
|
adamc@42
|
1057 fun traverse (pre, post) =
|
adamc@42
|
1058 case post of
|
adamc@42
|
1059 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
|
adamc@42
|
1060 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
|
adamc@42
|
1061 if x = x' then
|
adamc@42
|
1062 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
|
adamc@42
|
1063 else
|
adamc@42
|
1064 traverse (sgi :: pre, rest)
|
adamc@42
|
1065 | sgi :: rest => traverse (sgi :: pre, rest)
|
adamc@42
|
1066
|
adamc@42
|
1067 val sgis = traverse ([], sgis)
|
adamc@42
|
1068 in
|
adamc@42
|
1069 (SgnConst sgis, loc)
|
adamc@42
|
1070 end
|
adamc@42
|
1071 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
|
adamc@42
|
1072
|
adamc@211
|
1073 fun enrichClasses env classes (m1, ms) sgn =
|
adamc@59
|
1074 case #1 (hnormSgn env sgn) of
|
adamc@59
|
1075 SgnConst sgis =>
|
adamc@211
|
1076 let
|
adamc@218
|
1077 val (classes, _, _, _) =
|
adamc@218
|
1078 foldl (fn (sgi, (classes, newClasses, fmap, env)) =>
|
adamc@211
|
1079 let
|
adamc@211
|
1080 fun found (x, n) =
|
adamc@211
|
1081 (CM.insert (classes,
|
adamc@211
|
1082 ClProj (m1, ms, x),
|
adamc@211
|
1083 empty_class),
|
adamc@216
|
1084 IM.insert (newClasses, n, x),
|
adamc@218
|
1085 sgiSeek (#1 sgi, fmap),
|
adamc@218
|
1086 env)
|
adamc@216
|
1087
|
adamc@218
|
1088 fun default () = (classes, newClasses, sgiSeek (#1 sgi, fmap), env)
|
adamc@211
|
1089 in
|
adamc@211
|
1090 case #1 sgi of
|
adamc@217
|
1091 SgiStr (x, _, sgn) =>
|
adamc@217
|
1092 (enrichClasses env classes (m1, ms @ [x]) sgn,
|
adamc@217
|
1093 newClasses,
|
adamc@218
|
1094 sgiSeek (#1 sgi, fmap),
|
adamc@218
|
1095 env)
|
adamc@218
|
1096 | SgiSgn (x, n, sgn) =>
|
adamc@218
|
1097 (classes,
|
adamc@218
|
1098 newClasses,
|
adamc@218
|
1099 fmap,
|
adamc@218
|
1100 pushSgnNamedAs env x n sgn)
|
adamc@218
|
1101
|
adamc@563
|
1102 | SgiClassAbs (x, n, _) => found (x, n)
|
adamc@563
|
1103 | SgiClass (x, n, _, _) => found (x, n)
|
adamc@674
|
1104 | SgiVal (x, n, c) =>
|
adamc@674
|
1105 (case rule_in c of
|
adamc@674
|
1106 NONE => default ()
|
adamc@711
|
1107 | SOME (cn, nvs, cs, c) =>
|
adamc@674
|
1108 let
|
adamc@711
|
1109 val loc = #2 c
|
adamc@721
|
1110 val globalize = sgnS_con' (m1, ms, fmap)
|
adamc@674
|
1111
|
adamc@674
|
1112 val nc =
|
adamc@674
|
1113 case cn of
|
adamc@674
|
1114 ClNamed f => IM.find (newClasses, f)
|
adamc@674
|
1115 | _ => NONE
|
adamc@674
|
1116 in
|
adamc@674
|
1117 case nc of
|
adamc@674
|
1118 NONE =>
|
adamc@419
|
1119 let
|
adamc@419
|
1120 val classes =
|
adamc@419
|
1121 case CM.find (classes, cn) of
|
adamc@419
|
1122 NONE => classes
|
adamc@419
|
1123 | SOME class =>
|
adamc@419
|
1124 let
|
adamc@711
|
1125 val e = (EModProj (m1, ms, x), #2 sgn)
|
adamc@675
|
1126
|
adamc@675
|
1127 val class =
|
adamc@711
|
1128 {ground = #ground class,
|
adamc@711
|
1129 rules = (nvs,
|
adamc@711
|
1130 map globalize cs,
|
adamc@711
|
1131 globalize c,
|
adamc@711
|
1132 e) :: #rules class}
|
adamc@419
|
1133 in
|
adamc@419
|
1134 CM.insert (classes, cn, class)
|
adamc@419
|
1135 end
|
adamc@419
|
1136 in
|
adamc@419
|
1137 (classes,
|
adamc@419
|
1138 newClasses,
|
adamc@419
|
1139 fmap,
|
adamc@419
|
1140 env)
|
adamc@419
|
1141 end
|
adamc@674
|
1142 | SOME fx =>
|
adamc@674
|
1143 let
|
adamc@674
|
1144 val cn = ClProj (m1, ms, fx)
|
adamc@419
|
1145
|
adamc@674
|
1146 val classes =
|
adamc@674
|
1147 case CM.find (classes, cn) of
|
adamc@674
|
1148 NONE => classes
|
adamc@674
|
1149 | SOME class =>
|
adamc@674
|
1150 let
|
adamc@675
|
1151 val e = (EModProj (m1, ms, x), #2 sgn)
|
adamc@675
|
1152
|
adamc@675
|
1153 val class =
|
adamc@711
|
1154 {ground = #ground class,
|
adamc@711
|
1155 rules = (nvs,
|
adamc@711
|
1156 map globalize cs,
|
adamc@711
|
1157 globalize c,
|
adamc@711
|
1158 e) :: #rules class}
|
adamc@674
|
1159 in
|
adamc@674
|
1160 CM.insert (classes, cn, class)
|
adamc@674
|
1161 end
|
adamc@674
|
1162 in
|
adamc@674
|
1163 (classes,
|
adamc@674
|
1164 newClasses,
|
adamc@674
|
1165 fmap,
|
adamc@674
|
1166 env)
|
adamc@674
|
1167 end
|
adamc@674
|
1168 end)
|
adamc@216
|
1169 | _ => default ()
|
adamc@211
|
1170 end)
|
adamc@218
|
1171 (classes, IM.empty, (IM.empty, IM.empty, IM.empty), env) sgis
|
adamc@211
|
1172 in
|
adamc@211
|
1173 classes
|
adamc@211
|
1174 end
|
adamc@211
|
1175 | _ => classes
|
adamc@211
|
1176
|
adamc@211
|
1177 fun pushStrNamedAs (env : env) x n sgn =
|
adamc@623
|
1178 {renameK = #renameK env,
|
adamc@623
|
1179 relK = #relK env,
|
adamc@623
|
1180
|
adamc@623
|
1181 renameC = #renameC env,
|
adamc@211
|
1182 relC = #relC env,
|
adamc@211
|
1183 namedC = #namedC env,
|
adamc@211
|
1184
|
adamc@211
|
1185 datatypes = #datatypes env,
|
adamc@211
|
1186 constructors = #constructors env,
|
adamc@211
|
1187
|
adamc@211
|
1188 classes = enrichClasses env (#classes env) (n, []) sgn,
|
adamc@211
|
1189
|
adamc@211
|
1190 renameE = #renameE env,
|
adamc@211
|
1191 relE = #relE env,
|
adamc@211
|
1192 namedE = #namedE env,
|
adamc@211
|
1193
|
adamc@211
|
1194 renameSgn = #renameSgn env,
|
adamc@211
|
1195 sgn = #sgn env,
|
adamc@211
|
1196
|
adamc@211
|
1197 renameStr = SM.insert (#renameStr env, x, (n, sgn)),
|
adamc@211
|
1198 str = IM.insert (#str env, n, (x, sgn))}
|
adamc@211
|
1199
|
adamc@211
|
1200 fun pushStrNamed env x sgn =
|
adamc@211
|
1201 let
|
adamc@211
|
1202 val n = !namedCounter
|
adamc@211
|
1203 in
|
adamc@211
|
1204 namedCounter := n + 1;
|
adamc@211
|
1205 (pushStrNamedAs env x n sgn, n)
|
adamc@211
|
1206 end
|
adamc@211
|
1207
|
adamc@211
|
1208 fun sgiBinds env (sgi, loc) =
|
adamc@211
|
1209 case sgi of
|
adamc@211
|
1210 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
|
adamc@211
|
1211 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@805
|
1212 | SgiDatatype dts =>
|
adamc@211
|
1213 let
|
adamc@805
|
1214 fun doOne ((x, n, xs, xncs), env) =
|
adamc@805
|
1215 let
|
adamc@805
|
1216 val k = (KType, loc)
|
adamc@805
|
1217 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
|
adamc@341
|
1218
|
adamc@805
|
1219 val env = pushCNamedAs env x n k' NONE
|
adamc@805
|
1220 in
|
adamc@805
|
1221 foldl (fn ((x', n', to), env) =>
|
adamc@805
|
1222 let
|
adamc@805
|
1223 val t =
|
adamc@805
|
1224 case to of
|
adamc@805
|
1225 NONE => (CNamed n, loc)
|
adamc@805
|
1226 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@805
|
1227
|
adamc@805
|
1228 val k = (KType, loc)
|
adamc@805
|
1229 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@805
|
1230 in
|
adamc@805
|
1231 pushENamedAs env x' n' t
|
adamc@805
|
1232 end)
|
adamc@805
|
1233 env xncs
|
adamc@805
|
1234 end
|
adamc@211
|
1235 in
|
adamc@805
|
1236 foldl doOne env dts
|
adamc@211
|
1237 end
|
adamc@211
|
1238 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
|
adamc@211
|
1239 let
|
adamc@341
|
1240 val k = (KType, loc)
|
adamc@341
|
1241 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
|
adamc@341
|
1242
|
adamc@341
|
1243 val env = pushCNamedAs env x n k' (SOME (CModProj (m1, ms, x'), loc))
|
adamc@211
|
1244 in
|
adamc@211
|
1245 foldl (fn ((x', n', to), env) =>
|
adamc@211
|
1246 let
|
adamc@211
|
1247 val t =
|
adamc@211
|
1248 case to of
|
adamc@211
|
1249 NONE => (CNamed n, loc)
|
adamc@211
|
1250 | SOME t => (TFun (t, (CNamed n, loc)), loc)
|
adamc@211
|
1251
|
adamc@211
|
1252 val k = (KType, loc)
|
adamc@211
|
1253 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
|
adamc@211
|
1254 in
|
adamc@211
|
1255 pushENamedAs env x' n' t
|
adamc@211
|
1256 end)
|
adamc@211
|
1257 env xncs
|
adamc@211
|
1258 end
|
adamc@211
|
1259 | SgiVal (x, n, t) => pushENamedAs env x n t
|
adamc@211
|
1260 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@211
|
1261 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@211
|
1262 | SgiConstraint _ => env
|
adamc@211
|
1263
|
adamc@711
|
1264 | SgiClassAbs (x, n, k) => pushCNamedAs env x n k NONE
|
adamc@711
|
1265 | SgiClass (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@459
|
1266
|
adamc@211
|
1267 fun sgnSubCon x =
|
adamc@211
|
1268 ElabUtil.Con.map {kind = id,
|
adamc@211
|
1269 con = sgnS_con x}
|
adamc@59
|
1270
|
adamc@59
|
1271 fun projectStr env {sgn, str, field} =
|
adamc@59
|
1272 case #1 (hnormSgn env sgn) of
|
adamc@59
|
1273 SgnConst sgis =>
|
adamc@59
|
1274 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
|
adamc@59
|
1275 NONE => NONE
|
adamc@59
|
1276 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
|
adamc@59
|
1277 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
|
adamc@59
|
1278 | _ => NONE
|
adamc@59
|
1279
|
adamc@158
|
1280 fun chaseMpath env (n, ms) =
|
adamc@158
|
1281 let
|
adamc@158
|
1282 val (_, sgn) = lookupStrNamed env n
|
adamc@158
|
1283 in
|
adamc@158
|
1284 foldl (fn (m, (str, sgn)) =>
|
adamc@158
|
1285 case projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@158
|
1286 NONE => raise Fail "kindof: Unknown substructure"
|
adamc@158
|
1287 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
|
adamc@158
|
1288 ((StrVar n, #2 sgn), sgn) ms
|
adamc@158
|
1289 end
|
adamc@158
|
1290
|
adamc@42
|
1291 fun projectCon env {sgn, str, field} =
|
adamc@42
|
1292 case #1 (hnormSgn env sgn) of
|
adamc@34
|
1293 SgnConst sgis =>
|
adamc@34
|
1294 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
|
adamc@34
|
1295 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
|
adamc@805
|
1296 | SgiDatatype dts =>
|
adamc@805
|
1297 (case List.find (fn (x, _, xs, _) => x = field) dts of
|
adamc@805
|
1298 SOME (_, _, xs, _) =>
|
adamc@805
|
1299 let
|
adamc@805
|
1300 val k = (KType, #2 sgn)
|
adamc@805
|
1301 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
|
adamc@805
|
1302 in
|
adamc@805
|
1303 SOME (k', NONE)
|
adamc@805
|
1304 end
|
adamc@805
|
1305 | NONE => NONE)
|
adamc@341
|
1306 | SgiDatatypeImp (x, _, m1, ms, x', xs, _) =>
|
adamc@341
|
1307 if x = field then
|
adamc@341
|
1308 let
|
adamc@341
|
1309 val k = (KType, #2 sgn)
|
adamc@341
|
1310 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
|
adamc@341
|
1311 in
|
adamc@341
|
1312 SOME (k', SOME (CModProj (m1, ms, x'), #2 sgn))
|
adamc@341
|
1313 end
|
adamc@158
|
1314 else
|
adamc@158
|
1315 NONE
|
adamc@563
|
1316 | SgiClassAbs (x, _, k) => if x = field then
|
adamc@563
|
1317 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), NONE)
|
adamc@563
|
1318 else
|
adamc@563
|
1319 NONE
|
adamc@563
|
1320 | SgiClass (x, _, k, c) => if x = field then
|
adamc@563
|
1321 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), SOME c)
|
adamc@563
|
1322 else
|
adamc@563
|
1323 NONE
|
adamc@34
|
1324 | _ => NONE) sgis of
|
adamc@34
|
1325 NONE => NONE
|
adamc@34
|
1326 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
|
adamc@34
|
1327 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
|
adamc@42
|
1328 | _ => NONE
|
adamc@34
|
1329
|
adamc@157
|
1330 fun projectDatatype env {sgn, str, field} =
|
adamc@157
|
1331 case #1 (hnormSgn env sgn) of
|
adamc@157
|
1332 SgnConst sgis =>
|
adamc@805
|
1333 (case sgnSeek (fn SgiDatatype dts =>
|
adamc@805
|
1334 (case List.find (fn (x, _, _, _) => x = field) dts of
|
adamc@805
|
1335 SOME (_, _, xs, xncs) => SOME (xs, xncs)
|
adamc@805
|
1336 | NONE => NONE)
|
adamc@191
|
1337 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
|
adamc@157
|
1338 | _ => NONE) sgis of
|
adamc@157
|
1339 NONE => NONE
|
adamc@191
|
1340 | SOME ((xs, xncs), subs) => SOME (xs,
|
adamc@191
|
1341 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
|
adamc@157
|
1342 | _ => NONE
|
adamc@157
|
1343
|
adamc@174
|
1344 fun projectConstructor env {sgn, str, field} =
|
adamc@174
|
1345 case #1 (hnormSgn env sgn) of
|
adamc@174
|
1346 SgnConst sgis =>
|
adamc@174
|
1347 let
|
adamc@191
|
1348 fun consider (n, xs, xncs) =
|
adamc@174
|
1349 ListUtil.search (fn (x, n', to) =>
|
adamc@174
|
1350 if x <> field then
|
adamc@174
|
1351 NONE
|
adamc@174
|
1352 else
|
adamc@191
|
1353 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
|
adamc@174
|
1354 in
|
adamc@805
|
1355 case sgnSeek (fn SgiDatatype dts =>
|
adamc@805
|
1356 let
|
adamc@805
|
1357 fun search dts =
|
adamc@805
|
1358 case dts of
|
adamc@805
|
1359 [] => NONE
|
adamc@805
|
1360 | (_, n, xs, xncs) :: dts =>
|
adamc@805
|
1361 case consider (n, xs, xncs) of
|
adamc@805
|
1362 NONE => search dts
|
adamc@805
|
1363 | v => v
|
adamc@805
|
1364 in
|
adamc@805
|
1365 search dts
|
adamc@805
|
1366 end
|
adamc@191
|
1367 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
|
adamc@174
|
1368 | _ => NONE) sgis of
|
adamc@174
|
1369 NONE => NONE
|
adamc@191
|
1370 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
|
adamc@191
|
1371 sgnSubCon (str, subs) t)
|
adamc@174
|
1372 end
|
adamc@174
|
1373 | _ => NONE
|
adamc@174
|
1374
|
adamc@42
|
1375 fun projectVal env {sgn, str, field} =
|
adamc@42
|
1376 case #1 (hnormSgn env sgn) of
|
adamc@34
|
1377 SgnConst sgis =>
|
adamc@162
|
1378 let
|
adamc@191
|
1379 fun seek (n, xs, xncs) =
|
adamc@162
|
1380 ListUtil.search (fn (x, _, to) =>
|
adamc@162
|
1381 if x = field then
|
adamc@191
|
1382 SOME (let
|
adamc@467
|
1383 val base = (CNamed n, #2 sgn)
|
adamc@467
|
1384 val nxs = length xs
|
adamc@467
|
1385 val base = ListUtil.foldli (fn (i, _, base) =>
|
adamc@467
|
1386 (CApp (base,
|
adamc@467
|
1387 (CRel (nxs - i - 1), #2 sgn)),
|
adamc@467
|
1388 #2 sgn))
|
adamc@467
|
1389 base xs
|
adamc@467
|
1390
|
adamc@191
|
1391 val t =
|
adamc@191
|
1392 case to of
|
adamc@467
|
1393 NONE => base
|
adamc@467
|
1394 | SOME t => (TFun (t, base), #2 sgn)
|
adamc@191
|
1395 val k = (KType, #2 sgn)
|
adamc@191
|
1396 in
|
adamc@467
|
1397 foldr (fn (x, t) => (TCFun (Implicit, x, k, t), #2 sgn))
|
adamc@191
|
1398 t xs
|
adamc@191
|
1399 end)
|
adamc@162
|
1400 else
|
adamc@162
|
1401 NONE) xncs
|
adamc@162
|
1402 in
|
adamc@162
|
1403 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
|
adamc@805
|
1404 | SgiDatatype dts =>
|
adamc@805
|
1405 let
|
adamc@805
|
1406 fun search dts =
|
adamc@805
|
1407 case dts of
|
adamc@805
|
1408 [] => NONE
|
adamc@805
|
1409 | (_, n, xs, xncs) :: dts =>
|
adamc@805
|
1410 case seek (n, xs, xncs) of
|
adamc@805
|
1411 NONE => search dts
|
adamc@805
|
1412 | v => v
|
adamc@805
|
1413 in
|
adamc@805
|
1414 search dts
|
adamc@805
|
1415 end
|
adamc@191
|
1416 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
|
adamc@162
|
1417 | _ => NONE) sgis of
|
adamc@162
|
1418 NONE => NONE
|
adamc@162
|
1419 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
|
adamc@162
|
1420 end
|
adamc@34
|
1421 | SgnError => SOME (CError, ErrorMsg.dummySpan)
|
adamc@42
|
1422 | _ => NONE
|
adamc@34
|
1423
|
adamc@88
|
1424 fun sgnSeekConstraints (str, sgis) =
|
adamc@88
|
1425 let
|
adamc@88
|
1426 fun seek (sgis, sgns, strs, cons, acc) =
|
adamc@88
|
1427 case sgis of
|
adamc@88
|
1428 [] => acc
|
adamc@88
|
1429 | (sgi, _) :: sgis =>
|
adamc@88
|
1430 case sgi of
|
adamc@88
|
1431 SgiConstraint (c1, c2) =>
|
adamc@88
|
1432 let
|
adamc@88
|
1433 val sub = sgnSubCon (str, (sgns, strs, cons))
|
adamc@88
|
1434 in
|
adamc@88
|
1435 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
|
adamc@88
|
1436 end
|
adamc@88
|
1437 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1438 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@805
|
1439 | SgiDatatype dts => seek (sgis, sgns, strs,
|
adamc@805
|
1440 foldl (fn ((x, n, _, _), cons) => IM.insert (cons, n, x)) cons dts, acc)
|
adamc@191
|
1441 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1442 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
|
adamc@88
|
1443 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
|
adamc@88
|
1444 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
|
adamc@563
|
1445 | SgiClassAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@563
|
1446 | SgiClass (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
|
adamc@88
|
1447 in
|
adamc@88
|
1448 seek (sgis, IM.empty, IM.empty, IM.empty, [])
|
adamc@88
|
1449 end
|
adamc@88
|
1450
|
adamc@88
|
1451 fun projectConstraints env {sgn, str} =
|
adamc@88
|
1452 case #1 (hnormSgn env sgn) of
|
adamc@88
|
1453 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
|
adamc@88
|
1454 | SgnError => SOME []
|
adamc@88
|
1455 | _ => NONE
|
adamc@34
|
1456
|
adamc@447
|
1457 fun edeclBinds env (d, loc) =
|
adamc@447
|
1458 case d of
|
adamc@447
|
1459 EDVal (x, t, _) => pushERel env x t
|
adamc@447
|
1460 | EDValRec vis => foldl (fn ((x, t, _), env) => pushERel env x t) env vis
|
adamc@447
|
1461
|
adamc@157
|
1462 fun declBinds env (d, loc) =
|
adamc@157
|
1463 case d of
|
adamc@157
|
1464 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
|
adamc@805
|
1465 | DDatatype dts =>
|
adamc@157
|
1466 let
|
adamc@805
|
1467 fun doOne ((x, n, xs, xncs), env) =
|
adamc@805
|
1468 let
|
adamc@805
|
1469 val k = (KType, loc)
|
adamc@805
|
1470 val nxs = length xs
|
adamc@805
|
1471 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
|
adamc@805
|
1472 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
|
adamc@805
|
1473 (KArrow (k, kb), loc)))
|
adamc@805
|
1474 ((CNamed n, loc), k) xs
|
adamc@805
|
1475
|
adamc@805
|
1476 val env = pushCNamedAs env x n kb NONE
|
adamc@805
|
1477 val env = pushDatatype env n xs xncs
|
adamc@805
|
1478 in
|
adamc@805
|
1479 foldl (fn ((x', n', to), env) =>
|
adamc@805
|
1480 let
|
adamc@805
|
1481 val t =
|
adamc@805
|
1482 case to of
|
adamc@805
|
1483 NONE => tb
|
adamc@805
|
1484 | SOME t => (TFun (t, tb), loc)
|
adamc@805
|
1485 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
|
adamc@805
|
1486 in
|
adamc@805
|
1487 pushENamedAs env x' n' t
|
adamc@805
|
1488 end)
|
adamc@805
|
1489 env xncs
|
adamc@805
|
1490 end
|
adamc@157
|
1491 in
|
adamc@805
|
1492 foldl doOne env dts
|
adamc@157
|
1493 end
|
adamc@191
|
1494 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
|
adamc@157
|
1495 let
|
adamc@157
|
1496 val t = (CModProj (m, ms, x'), loc)
|
adamc@297
|
1497 val k = (KType, loc)
|
adamc@297
|
1498 val nxs = length xs
|
adamc@297
|
1499 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
|
adamc@297
|
1500 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
|
adamc@297
|
1501 (KArrow (k, kb), loc)))
|
adamc@297
|
1502 ((CNamed n, loc), k) xs
|
adamc@297
|
1503
|
adamc@341
|
1504 val env = pushCNamedAs env x n kb (SOME t)
|
adamc@191
|
1505 val env = pushDatatype env n xs xncs
|
adamc@157
|
1506 in
|
adamc@191
|
1507 foldl (fn ((x', n', to), env) =>
|
adamc@191
|
1508 let
|
adamc@191
|
1509 val t =
|
adamc@191
|
1510 case to of
|
adamc@297
|
1511 NONE => tb
|
adamc@297
|
1512 | SOME t => (TFun (t, tb), loc)
|
adamc@297
|
1513 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
|
adamc@191
|
1514 in
|
adamc@191
|
1515 pushENamedAs env x' n' t
|
adamc@191
|
1516 end)
|
adamc@191
|
1517 env xncs
|
adamc@157
|
1518 end
|
adamc@157
|
1519 | DVal (x, n, t, _) => pushENamedAs env x n t
|
adamc@157
|
1520 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
|
adamc@157
|
1521 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
|
adamc@157
|
1522 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
|
adamc@157
|
1523 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
|
adamc@157
|
1524 | DConstraint _ => env
|
adamc@157
|
1525 | DExport _ => env
|
adamc@707
|
1526 | DTable (tn, x, n, c, _, pc, _, cc) =>
|
adamc@205
|
1527 let
|
adamc@705
|
1528 val ct = (CModProj (tn, [], "sql_table"), loc)
|
adamc@705
|
1529 val ct = (CApp (ct, c), loc)
|
adamc@707
|
1530 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
|
adamc@338
|
1531 in
|
adamc@705
|
1532 pushENamedAs env x n ct
|
adamc@338
|
1533 end
|
adamc@338
|
1534 | DSequence (tn, x, n) =>
|
adamc@338
|
1535 let
|
adamc@338
|
1536 val t = (CModProj (tn, [], "sql_sequence"), loc)
|
adamc@205
|
1537 in
|
adamc@205
|
1538 pushENamedAs env x n t
|
adamc@205
|
1539 end
|
adamc@754
|
1540 | DView (tn, x, n, _, c) =>
|
adamc@754
|
1541 let
|
adamc@754
|
1542 val ct = (CModProj (tn, [], "sql_view"), loc)
|
adamc@754
|
1543 val ct = (CApp (ct, c), loc)
|
adamc@754
|
1544 in
|
adamc@754
|
1545 pushENamedAs env x n ct
|
adamc@754
|
1546 end
|
adamc@563
|
1547 | DClass (x, n, k, c) =>
|
adamc@213
|
1548 let
|
adamc@563
|
1549 val k = (KArrow (k, (KType, loc)), loc)
|
adamc@213
|
1550 val env = pushCNamedAs env x n k (SOME c)
|
adamc@213
|
1551 in
|
adamc@213
|
1552 pushClass env n
|
adamc@213
|
1553 end
|
adamc@271
|
1554 | DDatabase _ => env
|
adamc@459
|
1555 | DCookie (tn, x, n, c) =>
|
adamc@459
|
1556 let
|
adamc@459
|
1557 val t = (CApp ((CModProj (tn, [], "cookie"), loc), c), loc)
|
adamc@459
|
1558 in
|
adamc@459
|
1559 pushENamedAs env x n t
|
adamc@459
|
1560 end
|
adamc@720
|
1561 | DStyle (tn, x, n) =>
|
adamc@718
|
1562 let
|
adamc@720
|
1563 val t = (CModProj (tn, [], "css_class"), loc)
|
adamc@718
|
1564 in
|
adamc@718
|
1565 pushENamedAs env x n t
|
adamc@718
|
1566 end
|
adamc@157
|
1567
|
adamc@243
|
1568 fun patBinds env (p, loc) =
|
adamc@243
|
1569 case p of
|
adamc@243
|
1570 PWild => env
|
adamc@243
|
1571 | PVar (x, t) => pushERel env x t
|
adamc@243
|
1572 | PPrim _ => env
|
adamc@243
|
1573 | PCon (_, _, _, NONE) => env
|
adamc@243
|
1574 | PCon (_, _, _, SOME p) => patBinds env p
|
adamc@243
|
1575 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
|
adamc@243
|
1576
|
adamc@2
|
1577 end
|