adamc@2
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@2
|
2 * All rights reserved.
|
adamc@2
|
3 *
|
adamc@2
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@2
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@2
|
6 *
|
adamc@2
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@2
|
8 * this list of conditions and the following disclaimer.
|
adamc@2
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@2
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@2
|
11 * and/or other materials provided with the distribution.
|
adamc@2
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@2
|
13 * derived from this software without specific prior written permission.
|
adamc@2
|
14 *
|
adamc@2
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@2
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@2
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@2
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@2
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@2
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@2
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@2
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@2
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@2
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@2
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@2
|
26 *)
|
adamc@2
|
27
|
adamc@2
|
28 structure ElabUtil :> ELAB_UTIL = struct
|
adamc@2
|
29
|
adamc@2
|
30 open Elab
|
adamc@2
|
31
|
adamc@188
|
32 fun classifyDatatype xncs =
|
adamc@198
|
33 case xncs of
|
adamc@198
|
34 [(_, _, NONE), (_, _, SOME _)] => Option
|
adamc@198
|
35 | [(_, _, SOME _), (_, _, NONE)] => Option
|
adamc@198
|
36 | _ =>
|
adamc@198
|
37 if List.all (fn (_, _, NONE) => true | _ => false) xncs then
|
adamc@198
|
38 Enum
|
adamc@198
|
39 else
|
adamc@198
|
40 Default
|
adamc@188
|
41
|
adamc@2
|
42 structure S = Search
|
adamc@2
|
43
|
adamc@2
|
44 structure Kind = struct
|
adamc@2
|
45
|
adamc@623
|
46 fun mapfoldB {kind, bind} =
|
adamc@2
|
47 let
|
adamc@623
|
48 fun mfk ctx k acc =
|
adamc@623
|
49 S.bindP (mfk' ctx k acc, kind ctx)
|
adamc@2
|
50
|
adamc@623
|
51 and mfk' ctx (kAll as (k, loc)) =
|
adamc@2
|
52 case k of
|
adamc@2
|
53 KType => S.return2 kAll
|
adamc@2
|
54
|
adamc@2
|
55 | KArrow (k1, k2) =>
|
adamc@623
|
56 S.bind2 (mfk ctx k1,
|
adamc@2
|
57 fn k1' =>
|
adamc@623
|
58 S.map2 (mfk ctx k2,
|
adamc@2
|
59 fn k2' =>
|
adamc@2
|
60 (KArrow (k1', k2'), loc)))
|
adamc@2
|
61
|
adamc@2
|
62 | KName => S.return2 kAll
|
adamc@2
|
63
|
adamc@2
|
64 | KRecord k =>
|
adamc@623
|
65 S.map2 (mfk ctx k,
|
adamc@2
|
66 fn k' =>
|
adamc@2
|
67 (KRecord k', loc))
|
adamc@2
|
68
|
adamc@82
|
69 | KUnit => S.return2 kAll
|
adamc@82
|
70
|
adamc@207
|
71 | KTuple ks =>
|
adamc@623
|
72 S.map2 (ListUtil.mapfold (mfk ctx) ks,
|
adamc@207
|
73 fn ks' =>
|
adamc@207
|
74 (KTuple ks', loc))
|
adamc@207
|
75
|
adamc@2
|
76 | KError => S.return2 kAll
|
adamc@2
|
77
|
adamc@623
|
78 | KUnif (_, _, ref (SOME k)) => mfk' ctx k
|
adamc@2
|
79 | KUnif _ => S.return2 kAll
|
adamc@623
|
80
|
adamc@623
|
81 | KRel _ => S.return2 kAll
|
adamc@623
|
82 | KFun (x, k) =>
|
adamc@623
|
83 S.map2 (mfk (bind (ctx, x)) k,
|
adamc@623
|
84 fn k' =>
|
adamc@623
|
85 (KFun (x, k'), loc))
|
adamc@2
|
86 in
|
adamc@2
|
87 mfk
|
adamc@2
|
88 end
|
adamc@2
|
89
|
adamc@623
|
90 fun mapfold fk =
|
adamc@623
|
91 mapfoldB {kind = fn () => fk,
|
adamc@623
|
92 bind = fn ((), _) => ()} ()
|
adamc@623
|
93
|
adamc@623
|
94 fun mapB {kind, bind} ctx k =
|
adamc@623
|
95 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
|
adamc@623
|
96 bind = bind} ctx k () of
|
adamc@623
|
97 S.Continue (k, ()) => k
|
adamc@623
|
98 | S.Return _ => raise Fail "ElabUtil.Kind.mapB: Impossible"
|
adamc@623
|
99
|
adamc@2
|
100 fun exists f k =
|
adamc@6
|
101 case mapfold (fn k => fn () =>
|
adamc@6
|
102 if f k then
|
adamc@6
|
103 S.Return ()
|
adamc@6
|
104 else
|
adamc@6
|
105 S.Continue (k, ())) k () of
|
adamc@6
|
106 S.Return _ => true
|
adamc@6
|
107 | S.Continue _ => false
|
adamc@6
|
108
|
adamc@6
|
109 end
|
adamc@6
|
110
|
adamc@6
|
111 structure Con = struct
|
adamc@6
|
112
|
adamc@11
|
113 datatype binder =
|
adamc@623
|
114 RelK of string
|
adamc@623
|
115 | RelC of string * Elab.kind
|
adamc@792
|
116 | NamedC of string * int * Elab.kind * Elab.con option
|
adamc@11
|
117
|
adamc@11
|
118 fun mapfoldB {kind = fk, con = fc, bind} =
|
adamc@6
|
119 let
|
adamc@623
|
120 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, s) => bind (ctx, RelK s)}
|
adamc@6
|
121
|
adamc@11
|
122 fun mfc ctx c acc =
|
adamc@11
|
123 S.bindP (mfc' ctx c acc, fc ctx)
|
adamc@6
|
124
|
adamc@11
|
125 and mfc' ctx (cAll as (c, loc)) =
|
adamc@6
|
126 case c of
|
adamc@6
|
127 TFun (c1, c2) =>
|
adamc@11
|
128 S.bind2 (mfc ctx c1,
|
adamc@6
|
129 fn c1' =>
|
adamc@11
|
130 S.map2 (mfc ctx c2,
|
adamc@6
|
131 fn c2' =>
|
adamc@6
|
132 (TFun (c1', c2'), loc)))
|
adamc@6
|
133 | TCFun (e, x, k, c) =>
|
adamc@623
|
134 S.bind2 (mfk ctx k,
|
adamc@6
|
135 fn k' =>
|
adamc@623
|
136 S.map2 (mfc (bind (ctx, RelC (x, k))) c,
|
adamc@6
|
137 fn c' =>
|
adamc@6
|
138 (TCFun (e, x, k', c'), loc)))
|
adamc@628
|
139 | TDisjoint (c1, c2, c3) =>
|
adamc@85
|
140 S.bind2 (mfc ctx c1,
|
adamc@85
|
141 fn c1' =>
|
adamc@85
|
142 S.bind2 (mfc ctx c2,
|
adamc@85
|
143 fn c2' =>
|
adamc@85
|
144 S.map2 (mfc ctx c3,
|
adamc@85
|
145 fn c3' =>
|
adamc@628
|
146 (TDisjoint (c1', c2', c3'), loc))))
|
adamc@6
|
147 | TRecord c =>
|
adamc@11
|
148 S.map2 (mfc ctx c,
|
adamc@6
|
149 fn c' =>
|
adamc@6
|
150 (TRecord c', loc))
|
adamc@6
|
151
|
adamc@6
|
152 | CRel _ => S.return2 cAll
|
adamc@6
|
153 | CNamed _ => S.return2 cAll
|
adamc@34
|
154 | CModProj _ => S.return2 cAll
|
adamc@6
|
155 | CApp (c1, c2) =>
|
adamc@11
|
156 S.bind2 (mfc ctx c1,
|
adamc@6
|
157 fn c1' =>
|
adamc@11
|
158 S.map2 (mfc ctx c2,
|
adamc@6
|
159 fn c2' =>
|
adamc@6
|
160 (CApp (c1', c2'), loc)))
|
adamc@8
|
161 | CAbs (x, k, c) =>
|
adamc@623
|
162 S.bind2 (mfk ctx k,
|
adamc@6
|
163 fn k' =>
|
adamc@623
|
164 S.map2 (mfc (bind (ctx, RelC (x, k))) c,
|
adamc@6
|
165 fn c' =>
|
adamc@8
|
166 (CAbs (x, k', c'), loc)))
|
adamc@6
|
167
|
adamc@6
|
168 | CName _ => S.return2 cAll
|
adamc@6
|
169
|
adamc@6
|
170 | CRecord (k, xcs) =>
|
adamc@623
|
171 S.bind2 (mfk ctx k,
|
adamc@6
|
172 fn k' =>
|
adamc@6
|
173 S.map2 (ListUtil.mapfold (fn (x, c) =>
|
adamc@11
|
174 S.bind2 (mfc ctx x,
|
adamc@6
|
175 fn x' =>
|
adamc@11
|
176 S.map2 (mfc ctx c,
|
adamc@6
|
177 fn c' =>
|
adamc@6
|
178 (x', c'))))
|
adamc@6
|
179 xcs,
|
adamc@6
|
180 fn xcs' =>
|
adamc@6
|
181 (CRecord (k', xcs'), loc)))
|
adamc@6
|
182 | CConcat (c1, c2) =>
|
adamc@11
|
183 S.bind2 (mfc ctx c1,
|
adamc@6
|
184 fn c1' =>
|
adamc@11
|
185 S.map2 (mfc ctx c2,
|
adamc@6
|
186 fn c2' =>
|
adamc@6
|
187 (CConcat (c1', c2'), loc)))
|
adamc@621
|
188 | CMap (k1, k2) =>
|
adamc@623
|
189 S.bind2 (mfk ctx k1,
|
adamc@67
|
190 fn k1' =>
|
adamc@623
|
191 S.map2 (mfk ctx k2,
|
adamc@67
|
192 fn k2' =>
|
adamc@621
|
193 (CMap (k1', k2'), loc)))
|
adamc@6
|
194
|
adamc@82
|
195 | CUnit => S.return2 cAll
|
adamc@82
|
196
|
adamc@207
|
197 | CTuple cs =>
|
adamc@207
|
198 S.map2 (ListUtil.mapfold (mfc ctx) cs,
|
adamc@207
|
199 fn cs' =>
|
adamc@207
|
200 (CTuple cs', loc))
|
adamc@207
|
201
|
adamc@207
|
202 | CProj (c, n) =>
|
adamc@207
|
203 S.map2 (mfc ctx c,
|
adamc@207
|
204 fn c' =>
|
adamc@207
|
205 (CProj (c', n), loc))
|
adamc@207
|
206
|
adamc@6
|
207 | CError => S.return2 cAll
|
adamc@76
|
208 | CUnif (_, _, _, ref (SOME c)) => mfc' ctx c
|
adamc@6
|
209 | CUnif _ => S.return2 cAll
|
adamc@623
|
210
|
adamc@623
|
211 | CKAbs (x, c) =>
|
adamc@623
|
212 S.map2 (mfc (bind (ctx, RelK x)) c,
|
adamc@623
|
213 fn c' =>
|
adamc@623
|
214 (CKAbs (x, c'), loc))
|
adamc@623
|
215 | CKApp (c, k) =>
|
adamc@623
|
216 S.bind2 (mfc ctx c,
|
adamc@623
|
217 fn c' =>
|
adamc@623
|
218 S.map2 (mfk ctx k,
|
adamc@623
|
219 fn k' =>
|
adamc@623
|
220 (CKApp (c', k'), loc)))
|
adamc@623
|
221 | TKFun (x, c) =>
|
adamc@623
|
222 S.map2 (mfc (bind (ctx, RelK x)) c,
|
adamc@623
|
223 fn c' =>
|
adamc@623
|
224 (TKFun (x, c'), loc))
|
adamc@6
|
225 in
|
adamc@6
|
226 mfc
|
adamc@6
|
227 end
|
adamc@6
|
228
|
adamc@11
|
229 fun mapfold {kind = fk, con = fc} =
|
adamc@623
|
230 mapfoldB {kind = fn () => fk,
|
adamc@11
|
231 con = fn () => fc,
|
adamc@11
|
232 bind = fn ((), _) => ()} ()
|
adamc@11
|
233
|
adamc@11
|
234 fun mapB {kind, con, bind} ctx c =
|
adamc@623
|
235 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
|
adamc@11
|
236 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@11
|
237 bind = bind} ctx c () of
|
adamc@11
|
238 S.Continue (c, ()) => c
|
adamc@34
|
239 | S.Return _ => raise Fail "ElabUtil.Con.mapB: Impossible"
|
adamc@34
|
240
|
adamc@34
|
241 fun map {kind, con} s =
|
adamc@34
|
242 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@34
|
243 con = fn c => fn () => S.Continue (con c, ())} s () of
|
adamc@34
|
244 S.Return () => raise Fail "ElabUtil.Con.map: Impossible"
|
adamc@34
|
245 | S.Continue (s, ()) => s
|
adamc@11
|
246
|
adamc@711
|
247 fun existsB {kind, con, bind} ctx c =
|
adamc@711
|
248 case mapfoldB {kind = fn ctx => fn k => fn () =>
|
adamc@711
|
249 if kind (ctx, k) then
|
adamc@711
|
250 S.Return ()
|
adamc@711
|
251 else
|
adamc@711
|
252 S.Continue (k, ()),
|
adamc@711
|
253 con = fn ctx => fn c => fn () =>
|
adamc@711
|
254 if con (ctx, c) then
|
adamc@711
|
255 S.Return ()
|
adamc@711
|
256 else
|
adamc@711
|
257 S.Continue (c, ()),
|
adamc@711
|
258 bind = bind} ctx c () of
|
adamc@711
|
259 S.Return _ => true
|
adamc@711
|
260 | S.Continue _ => false
|
adamc@711
|
261
|
adamc@711
|
262 fun exists {kind, con} c =
|
adamc@6
|
263 case mapfold {kind = fn k => fn () =>
|
adamc@6
|
264 if kind k then
|
adamc@6
|
265 S.Return ()
|
adamc@6
|
266 else
|
adamc@6
|
267 S.Continue (k, ()),
|
adamc@6
|
268 con = fn c => fn () =>
|
adamc@6
|
269 if con c then
|
adamc@6
|
270 S.Return ()
|
adamc@6
|
271 else
|
adamc@711
|
272 S.Continue (c, ())} c () of
|
adamc@2
|
273 S.Return _ => true
|
adamc@2
|
274 | S.Continue _ => false
|
adamc@2
|
275
|
adamc@448
|
276 fun foldB {kind, con, bind} ctx st c =
|
adamc@623
|
277 case mapfoldB {kind = fn ctx => fn k => fn st => S.Continue (k, kind (ctx, k, st)),
|
adamc@448
|
278 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
|
adamc@448
|
279 bind = bind} ctx c st of
|
adamc@448
|
280 S.Continue (_, st) => st
|
adamc@448
|
281 | S.Return _ => raise Fail "ElabUtil.Con.foldB: Impossible"
|
adamc@448
|
282
|
adamc@2
|
283 end
|
adamc@2
|
284
|
adamc@10
|
285 structure Exp = struct
|
adamc@10
|
286
|
adamc@11
|
287 datatype binder =
|
adamc@623
|
288 RelK of string
|
adamc@623
|
289 | RelC of string * Elab.kind
|
adamc@792
|
290 | NamedC of string * int * Elab.kind * Elab.con option
|
adamc@11
|
291 | RelE of string * Elab.con
|
adamc@11
|
292 | NamedE of string * Elab.con
|
adamc@11
|
293
|
adamc@11
|
294 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
|
adamc@10
|
295 let
|
adamc@623
|
296 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
|
adamc@10
|
297
|
adamc@11
|
298 fun bind' (ctx, b) =
|
adamc@11
|
299 let
|
adamc@11
|
300 val b' = case b of
|
adamc@623
|
301 Con.RelK x => RelK x
|
adamc@623
|
302 | Con.RelC x => RelC x
|
adamc@623
|
303 | Con.NamedC x => NamedC x
|
adamc@11
|
304 in
|
adamc@11
|
305 bind (ctx, b')
|
adamc@11
|
306 end
|
adamc@11
|
307 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@10
|
308
|
adamc@11
|
309 fun mfe ctx e acc =
|
adamc@11
|
310 S.bindP (mfe' ctx e acc, fe ctx)
|
adamc@11
|
311
|
adamc@11
|
312 and mfe' ctx (eAll as (e, loc)) =
|
adamc@10
|
313 case e of
|
adamc@14
|
314 EPrim _ => S.return2 eAll
|
adamc@14
|
315 | ERel _ => S.return2 eAll
|
adamc@10
|
316 | ENamed _ => S.return2 eAll
|
adamc@34
|
317 | EModProj _ => S.return2 eAll
|
adamc@10
|
318 | EApp (e1, e2) =>
|
adamc@11
|
319 S.bind2 (mfe ctx e1,
|
adamc@10
|
320 fn e1' =>
|
adamc@11
|
321 S.map2 (mfe ctx e2,
|
adamc@10
|
322 fn e2' =>
|
adamc@10
|
323 (EApp (e1', e2'), loc)))
|
adamc@26
|
324 | EAbs (x, dom, ran, e) =>
|
adamc@26
|
325 S.bind2 (mfc ctx dom,
|
adamc@26
|
326 fn dom' =>
|
adamc@26
|
327 S.bind2 (mfc ctx ran,
|
adamc@26
|
328 fn ran' =>
|
adamc@26
|
329 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
|
adamc@26
|
330 fn e' =>
|
adamc@26
|
331 (EAbs (x, dom', ran', e'), loc))))
|
adamc@26
|
332
|
adamc@10
|
333 | ECApp (e, c) =>
|
adamc@11
|
334 S.bind2 (mfe ctx e,
|
adamc@10
|
335 fn e' =>
|
adamc@11
|
336 S.map2 (mfc ctx c,
|
adamc@10
|
337 fn c' =>
|
adamc@10
|
338 (ECApp (e', c'), loc)))
|
adamc@10
|
339 | ECAbs (expl, x, k, e) =>
|
adamc@623
|
340 S.bind2 (mfk ctx k,
|
adamc@10
|
341 fn k' =>
|
adamc@11
|
342 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
|
adamc@10
|
343 fn e' =>
|
adamc@10
|
344 (ECAbs (expl, x, k', e'), loc)))
|
adamc@10
|
345
|
adamc@12
|
346 | ERecord xes =>
|
adamc@29
|
347 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
|
adamc@12
|
348 S.bind2 (mfc ctx x,
|
adamc@12
|
349 fn x' =>
|
adamc@29
|
350 S.bind2 (mfe ctx e,
|
adamc@12
|
351 fn e' =>
|
adamc@29
|
352 S.map2 (mfc ctx t,
|
adamc@29
|
353 fn t' =>
|
adamc@29
|
354 (x', e', t')))))
|
adamc@12
|
355 xes,
|
adamc@12
|
356 fn xes' =>
|
adamc@12
|
357 (ERecord xes', loc))
|
adamc@12
|
358 | EField (e, c, {field, rest}) =>
|
adamc@12
|
359 S.bind2 (mfe ctx e,
|
adamc@12
|
360 fn e' =>
|
adamc@12
|
361 S.bind2 (mfc ctx c,
|
adamc@12
|
362 fn c' =>
|
adamc@12
|
363 S.bind2 (mfc ctx field,
|
adamc@12
|
364 fn field' =>
|
adamc@12
|
365 S.map2 (mfc ctx rest,
|
adamc@12
|
366 fn rest' =>
|
adamc@12
|
367 (EField (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@445
|
368 | EConcat (e1, c1, e2, c2) =>
|
adamc@339
|
369 S.bind2 (mfe ctx e1,
|
adamc@339
|
370 fn e1' =>
|
adamc@445
|
371 S.bind2 (mfc ctx c1,
|
adamc@445
|
372 fn c1' =>
|
adamc@339
|
373 S.bind2 (mfe ctx e2,
|
adamc@339
|
374 fn e2' =>
|
adamc@445
|
375 S.map2 (mfc ctx c2,
|
adamc@445
|
376 fn c2' =>
|
adamc@445
|
377 (EConcat (e1', c1', e2', c2'),
|
adamc@445
|
378 loc)))))
|
adamc@149
|
379 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
380 S.bind2 (mfe ctx e,
|
adamc@149
|
381 fn e' =>
|
adamc@149
|
382 S.bind2 (mfc ctx c,
|
adamc@149
|
383 fn c' =>
|
adamc@149
|
384 S.bind2 (mfc ctx field,
|
adamc@149
|
385 fn field' =>
|
adamc@149
|
386 S.map2 (mfc ctx rest,
|
adamc@149
|
387 fn rest' =>
|
adamc@149
|
388 (ECut (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@12
|
389
|
adamc@493
|
390 | ECutMulti (e, c, {rest}) =>
|
adamc@493
|
391 S.bind2 (mfe ctx e,
|
adamc@493
|
392 fn e' =>
|
adamc@493
|
393 S.bind2 (mfc ctx c,
|
adamc@493
|
394 fn c' =>
|
adamc@493
|
395 S.map2 (mfc ctx rest,
|
adamc@493
|
396 fn rest' =>
|
adamc@493
|
397 (ECutMulti (e', c', {rest = rest'}), loc))))
|
adamc@493
|
398
|
adamc@182
|
399 | ECase (e, pes, {disc, result}) =>
|
adamc@171
|
400 S.bind2 (mfe ctx e,
|
adamc@171
|
401 fn e' =>
|
adamc@171
|
402 S.bind2 (ListUtil.mapfold (fn (p, e) =>
|
adamc@448
|
403 let
|
adamc@448
|
404 fun pb ((p, _), ctx) =
|
adamc@448
|
405 case p of
|
adamc@448
|
406 PWild => ctx
|
adamc@448
|
407 | PVar (x, t) => bind (ctx, RelE (x, t))
|
adamc@448
|
408 | PPrim _ => ctx
|
adamc@448
|
409 | PCon (_, _, _, NONE) => ctx
|
adamc@448
|
410 | PCon (_, _, _, SOME p) => pb (p, ctx)
|
adamc@448
|
411 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
|
adamc@448
|
412 pb (p, ctx)) ctx xps
|
adamc@448
|
413 in
|
adamc@448
|
414 S.map2 (mfe (pb (p, ctx)) e,
|
adamc@448
|
415 fn e' => (p, e'))
|
adamc@448
|
416 end) pes,
|
adamc@171
|
417 fn pes' =>
|
adamc@182
|
418 S.bind2 (mfc ctx disc,
|
adamc@182
|
419 fn disc' =>
|
adamc@182
|
420 S.map2 (mfc ctx result,
|
adamc@182
|
421 fn result' =>
|
adamc@182
|
422 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
|
adamc@171
|
423
|
adamc@10
|
424 | EError => S.return2 eAll
|
adamc@228
|
425 | EUnif (ref (SOME e)) => mfe ctx e
|
adamc@228
|
426 | EUnif _ => S.return2 eAll
|
adamc@447
|
427
|
adamc@447
|
428 | ELet (des, e) =>
|
adamc@447
|
429 let
|
adamc@447
|
430 val (des, ctx) = foldl (fn (ed, (des, ctx)) =>
|
adamc@453
|
431 let
|
adamc@453
|
432 val ctx' =
|
adamc@453
|
433 case #1 ed of
|
adamc@453
|
434 EDVal (x, t, _) => bind (ctx, RelE (x, t))
|
adamc@453
|
435 | EDValRec vis =>
|
adamc@453
|
436 foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t))) ctx vis
|
adamc@453
|
437 in
|
adamc@453
|
438 (S.bind2 (des,
|
adamc@453
|
439 fn des' =>
|
adamc@453
|
440 S.map2 (mfed ctx ed,
|
adamc@447
|
441 fn ed' => des' @ [ed'])),
|
adamc@453
|
442 ctx')
|
adamc@453
|
443 end)
|
adamc@447
|
444 (S.return2 [], ctx) des
|
adamc@447
|
445 in
|
adamc@447
|
446 S.bind2 (des,
|
adamc@447
|
447 fn des' =>
|
adamc@447
|
448 S.map2 (mfe ctx e,
|
adamc@447
|
449 fn e' =>
|
adamc@447
|
450 (ELet (des', e'), loc)))
|
adamc@447
|
451 end
|
adamc@447
|
452
|
adamc@623
|
453 | EKAbs (x, e) =>
|
adamc@623
|
454 S.map2 (mfe (bind (ctx, RelK x)) e,
|
adamc@623
|
455 fn e' =>
|
adamc@623
|
456 (EKAbs (x, e'), loc))
|
adamc@623
|
457 | EKApp (e, k) =>
|
adamc@623
|
458 S.bind2 (mfe ctx e,
|
adamc@623
|
459 fn e' =>
|
adamc@623
|
460 S.map2 (mfk ctx k,
|
adamc@623
|
461 fn k' =>
|
adamc@623
|
462 (EKApp (e', k'), loc)))
|
adamc@623
|
463
|
adamc@447
|
464 and mfed ctx (dAll as (d, loc)) =
|
adamc@447
|
465 case d of
|
adamc@447
|
466 EDVal vi =>
|
adamc@447
|
467 S.map2 (mfvi ctx vi,
|
adamc@447
|
468 fn vi' =>
|
adamc@447
|
469 (EDVal vi', loc))
|
adamc@447
|
470 | EDValRec vis =>
|
adamc@447
|
471 let
|
adamc@453
|
472 val ctx = foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t))) ctx vis
|
adamc@447
|
473 in
|
adamc@447
|
474 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@447
|
475 fn vis' =>
|
adamc@447
|
476 (EDValRec vis', loc))
|
adamc@447
|
477 end
|
adamc@447
|
478
|
adamc@447
|
479 and mfvi ctx (x, c, e) =
|
adamc@447
|
480 S.bind2 (mfc ctx c,
|
adamc@447
|
481 fn c' =>
|
adamc@447
|
482 S.map2 (mfe ctx e,
|
adamc@447
|
483 fn e' =>
|
adamc@447
|
484 (x, c', e')))
|
adamc@10
|
485 in
|
adamc@10
|
486 mfe
|
adamc@10
|
487 end
|
adamc@10
|
488
|
adamc@11
|
489 fun mapfold {kind = fk, con = fc, exp = fe} =
|
adamc@623
|
490 mapfoldB {kind = fn () => fk,
|
adamc@11
|
491 con = fn () => fc,
|
adamc@11
|
492 exp = fn () => fe,
|
adamc@11
|
493 bind = fn ((), _) => ()} ()
|
adamc@11
|
494
|
adamc@10
|
495 fun exists {kind, con, exp} k =
|
adamc@10
|
496 case mapfold {kind = fn k => fn () =>
|
adamc@10
|
497 if kind k then
|
adamc@10
|
498 S.Return ()
|
adamc@10
|
499 else
|
adamc@10
|
500 S.Continue (k, ()),
|
adamc@10
|
501 con = fn c => fn () =>
|
adamc@10
|
502 if con c then
|
adamc@10
|
503 S.Return ()
|
adamc@10
|
504 else
|
adamc@10
|
505 S.Continue (c, ()),
|
adamc@10
|
506 exp = fn e => fn () =>
|
adamc@10
|
507 if exp e then
|
adamc@10
|
508 S.Return ()
|
adamc@10
|
509 else
|
adamc@10
|
510 S.Continue (e, ())} k () of
|
adamc@10
|
511 S.Return _ => true
|
adamc@10
|
512 | S.Continue _ => false
|
adamc@10
|
513
|
adamc@211
|
514 fun mapB {kind, con, exp, bind} ctx e =
|
adamc@623
|
515 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
|
adamc@211
|
516 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@211
|
517 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
|
adamc@211
|
518 bind = bind} ctx e () of
|
adamc@211
|
519 S.Continue (e, ()) => e
|
adamc@211
|
520 | S.Return _ => raise Fail "ElabUtil.Exp.mapB: Impossible"
|
adamc@211
|
521
|
adamc@448
|
522 fun foldB {kind, con, exp, bind} ctx st e =
|
adamc@623
|
523 case mapfoldB {kind = fn ctx => fn k => fn st => S.Continue (k, kind (ctx, k, st)),
|
adamc@448
|
524 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
|
adamc@448
|
525 exp = fn ctx => fn e => fn st => S.Continue (e, exp (ctx, e, st)),
|
adamc@448
|
526 bind = bind} ctx e st of
|
adamc@448
|
527 S.Continue (_, st) => st
|
adamc@448
|
528 | S.Return _ => raise Fail "ElabUtil.Exp.foldB: Impossible"
|
adamc@448
|
529
|
adamc@10
|
530 end
|
adamc@10
|
531
|
adamc@34
|
532 structure Sgn = struct
|
adamc@34
|
533
|
adamc@34
|
534 datatype binder =
|
adamc@623
|
535 RelK of string
|
adamc@623
|
536 | RelC of string * Elab.kind
|
adamc@792
|
537 | NamedC of string * int * Elab.kind * Elab.con option
|
adamc@792
|
538 | Str of string * int * Elab.sgn
|
adamc@792
|
539 | Sgn of string * int * Elab.sgn
|
adamc@34
|
540
|
adamc@34
|
541 fun mapfoldB {kind, con, sgn_item, sgn, bind} =
|
adamc@34
|
542 let
|
adamc@34
|
543 fun bind' (ctx, b) =
|
adamc@34
|
544 let
|
adamc@34
|
545 val b' = case b of
|
adamc@623
|
546 Con.RelK x => RelK x
|
adamc@623
|
547 | Con.RelC x => RelC x
|
adamc@623
|
548 | Con.NamedC x => NamedC x
|
adamc@34
|
549 in
|
adamc@34
|
550 bind (ctx, b')
|
adamc@34
|
551 end
|
adamc@34
|
552 val con = Con.mapfoldB {kind = kind, con = con, bind = bind'}
|
adamc@34
|
553
|
adamc@623
|
554 val kind = Kind.mapfoldB {kind = kind, bind = fn (ctx, x) => bind (ctx, RelK x)}
|
adamc@34
|
555
|
adamc@34
|
556 fun sgi ctx si acc =
|
adamc@34
|
557 S.bindP (sgi' ctx si acc, sgn_item ctx)
|
adamc@34
|
558
|
adamc@156
|
559 and sgi' ctx (siAll as (si, loc)) =
|
adamc@34
|
560 case si of
|
adamc@34
|
561 SgiConAbs (x, n, k) =>
|
adamc@623
|
562 S.map2 (kind ctx k,
|
adamc@34
|
563 fn k' =>
|
adamc@34
|
564 (SgiConAbs (x, n, k'), loc))
|
adamc@34
|
565 | SgiCon (x, n, k, c) =>
|
adamc@623
|
566 S.bind2 (kind ctx k,
|
adamc@34
|
567 fn k' =>
|
adamc@34
|
568 S.map2 (con ctx c,
|
adamc@34
|
569 fn c' =>
|
adamc@34
|
570 (SgiCon (x, n, k', c'), loc)))
|
adamc@191
|
571 | SgiDatatype (x, n, xs, xncs) =>
|
adamc@156
|
572 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@156
|
573 case c of
|
adamc@156
|
574 NONE => S.return2 (x, n, c)
|
adamc@156
|
575 | SOME c =>
|
adamc@156
|
576 S.map2 (con ctx c,
|
adamc@156
|
577 fn c' => (x, n, SOME c'))) xncs,
|
adamc@156
|
578 fn xncs' =>
|
adamc@191
|
579 (SgiDatatype (x, n, xs, xncs'), loc))
|
adamc@191
|
580 | SgiDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@162
|
581 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
582 case c of
|
adamc@162
|
583 NONE => S.return2 (x, n, c)
|
adamc@162
|
584 | SOME c =>
|
adamc@162
|
585 S.map2 (con ctx c,
|
adamc@162
|
586 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
587 fn xncs' =>
|
adamc@191
|
588 (SgiDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
|
adamc@34
|
589 | SgiVal (x, n, c) =>
|
adamc@34
|
590 S.map2 (con ctx c,
|
adamc@34
|
591 fn c' =>
|
adamc@34
|
592 (SgiVal (x, n, c'), loc))
|
adamc@34
|
593 | SgiStr (x, n, s) =>
|
adamc@34
|
594 S.map2 (sg ctx s,
|
adamc@34
|
595 fn s' =>
|
adamc@34
|
596 (SgiStr (x, n, s'), loc))
|
adamc@59
|
597 | SgiSgn (x, n, s) =>
|
adamc@59
|
598 S.map2 (sg ctx s,
|
adamc@59
|
599 fn s' =>
|
adamc@59
|
600 (SgiSgn (x, n, s'), loc))
|
adamc@88
|
601 | SgiConstraint (c1, c2) =>
|
adamc@88
|
602 S.bind2 (con ctx c1,
|
adamc@88
|
603 fn c1' =>
|
adamc@88
|
604 S.map2 (con ctx c2,
|
adamc@88
|
605 fn c2' =>
|
adamc@88
|
606 (SgiConstraint (c1', c2'), loc)))
|
adamc@563
|
607 | SgiClassAbs (x, n, k) =>
|
adamc@623
|
608 S.map2 (kind ctx k,
|
adamc@563
|
609 fn k' =>
|
adamc@563
|
610 (SgiClassAbs (x, n, k'), loc))
|
adamc@563
|
611 | SgiClass (x, n, k, c) =>
|
adamc@623
|
612 S.bind2 (kind ctx k,
|
adamc@563
|
613 fn k' =>
|
adamc@563
|
614 S.map2 (con ctx c,
|
adamc@563
|
615 fn c' =>
|
adamc@563
|
616 (SgiClass (x, n, k', c'), loc)))
|
adamc@34
|
617
|
adamc@34
|
618 and sg ctx s acc =
|
adamc@34
|
619 S.bindP (sg' ctx s acc, sgn ctx)
|
adamc@34
|
620
|
adamc@34
|
621 and sg' ctx (sAll as (s, loc)) =
|
adamc@34
|
622 case s of
|
adamc@34
|
623 SgnConst sgis =>
|
adamc@34
|
624 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
|
adamc@34
|
625 (case #1 si of
|
adamc@329
|
626 SgiConAbs (x, n, k) =>
|
adamc@792
|
627 bind (ctx, NamedC (x, n, k, NONE))
|
adamc@792
|
628 | SgiCon (x, n, k, c) =>
|
adamc@792
|
629 bind (ctx, NamedC (x, n, k, SOME c))
|
adamc@191
|
630 | SgiDatatype (x, n, _, xncs) =>
|
adamc@792
|
631 bind (ctx, NamedC (x, n, (KType, loc), NONE))
|
adamc@792
|
632 | SgiDatatypeImp (x, n, m1, ms, s, _, _) =>
|
adamc@792
|
633 bind (ctx, NamedC (x, n, (KType, loc),
|
adamc@792
|
634 SOME (CModProj (m1, ms, s), loc)))
|
adamc@34
|
635 | SgiVal _ => ctx
|
adamc@792
|
636 | SgiStr (x, n, sgn) =>
|
adamc@792
|
637 bind (ctx, Str (x, n, sgn))
|
adamc@792
|
638 | SgiSgn (x, n, sgn) =>
|
adamc@792
|
639 bind (ctx, Sgn (x, n, sgn))
|
adamc@203
|
640 | SgiConstraint _ => ctx
|
adamc@563
|
641 | SgiClassAbs (x, n, k) =>
|
adamc@792
|
642 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), NONE))
|
adamc@792
|
643 | SgiClass (x, n, k, c) =>
|
adamc@792
|
644 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), SOME c)),
|
adamc@34
|
645 sgi ctx si)) ctx sgis,
|
adamc@34
|
646 fn sgis' =>
|
adamc@34
|
647 (SgnConst sgis', loc))
|
adamc@34
|
648
|
adamc@34
|
649 | SgnVar _ => S.return2 sAll
|
adamc@41
|
650 | SgnFun (m, n, s1, s2) =>
|
adamc@41
|
651 S.bind2 (sg ctx s1,
|
adamc@41
|
652 fn s1' =>
|
adamc@792
|
653 S.map2 (sg (bind (ctx, Str (m, n, s1'))) s2,
|
adamc@41
|
654 fn s2' =>
|
adamc@41
|
655 (SgnFun (m, n, s1', s2'), loc)))
|
adamc@59
|
656 | SgnProj _ => S.return2 sAll
|
adamc@42
|
657 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
658 S.bind2 (sg ctx sgn,
|
adamc@42
|
659 fn sgn' =>
|
adamc@42
|
660 S.map2 (con ctx c,
|
adamc@42
|
661 fn c' =>
|
adamc@42
|
662 (SgnWhere (sgn', x, c'), loc)))
|
adamc@34
|
663 | SgnError => S.return2 sAll
|
adamc@34
|
664 in
|
adamc@34
|
665 sg
|
adamc@34
|
666 end
|
adamc@34
|
667
|
adamc@34
|
668 fun mapfold {kind, con, sgn_item, sgn} =
|
adamc@623
|
669 mapfoldB {kind = fn () => kind,
|
adamc@34
|
670 con = fn () => con,
|
adamc@34
|
671 sgn_item = fn () => sgn_item,
|
adamc@34
|
672 sgn = fn () => sgn,
|
adamc@34
|
673 bind = fn ((), _) => ()} ()
|
adamc@34
|
674
|
adamc@792
|
675 fun mapB {kind, con, sgn_item, sgn, bind} ctx s =
|
adamc@792
|
676 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
|
adamc@792
|
677 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@792
|
678 sgn_item = fn ctx => fn sgi => fn () => S.Continue (sgn_item ctx sgi, ()),
|
adamc@792
|
679 sgn = fn ctx => fn s => fn () => S.Continue (sgn ctx s, ()),
|
adamc@792
|
680 bind = bind} ctx s () of
|
adamc@792
|
681 S.Continue (s, ()) => s
|
adamc@792
|
682 | S.Return _ => raise Fail "ElabUtil.Sgn.mapB: Impossible"
|
adamc@792
|
683
|
adamc@34
|
684 fun map {kind, con, sgn_item, sgn} s =
|
adamc@34
|
685 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@34
|
686 con = fn c => fn () => S.Continue (con c, ()),
|
adamc@34
|
687 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
|
adamc@34
|
688 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
|
adamc@34
|
689 S.Return () => raise Fail "Elab_util.Sgn.map"
|
adamc@34
|
690 | S.Continue (s, ()) => s
|
adamc@34
|
691
|
adamc@2
|
692 end
|
adamc@34
|
693
|
adamc@76
|
694 structure Decl = struct
|
adamc@76
|
695
|
adamc@76
|
696 datatype binder =
|
adamc@623
|
697 RelK of string
|
adamc@623
|
698 | RelC of string * Elab.kind
|
adamc@792
|
699 | NamedC of string * int * Elab.kind * Elab.con option
|
adamc@76
|
700 | RelE of string * Elab.con
|
adamc@76
|
701 | NamedE of string * Elab.con
|
adamc@76
|
702 | Str of string * Elab.sgn
|
adamc@76
|
703 | Sgn of string * Elab.sgn
|
adamc@76
|
704
|
adamc@76
|
705 fun mapfoldB {kind = fk, con = fc, exp = fe, sgn_item = fsgi, sgn = fsg, str = fst, decl = fd, bind} =
|
adamc@76
|
706 let
|
adamc@623
|
707 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
|
adamc@76
|
708
|
adamc@76
|
709 fun bind' (ctx, b) =
|
adamc@76
|
710 let
|
adamc@76
|
711 val b' = case b of
|
adamc@623
|
712 Con.RelK x => RelK x
|
adamc@623
|
713 | Con.RelC x => RelC x
|
adamc@623
|
714 | Con.NamedC x => NamedC x
|
adamc@76
|
715 in
|
adamc@76
|
716 bind (ctx, b')
|
adamc@76
|
717 end
|
adamc@76
|
718 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@76
|
719
|
adamc@76
|
720 fun bind' (ctx, b) =
|
adamc@76
|
721 let
|
adamc@76
|
722 val b' = case b of
|
adamc@623
|
723 Exp.RelK x => RelK x
|
adamc@623
|
724 | Exp.RelC x => RelC x
|
adamc@76
|
725 | Exp.NamedC x => NamedC x
|
adamc@76
|
726 | Exp.RelE x => RelE x
|
adamc@76
|
727 | Exp.NamedE x => NamedE x
|
adamc@76
|
728 in
|
adamc@76
|
729 bind (ctx, b')
|
adamc@76
|
730 end
|
adamc@76
|
731 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind'}
|
adamc@76
|
732
|
adamc@76
|
733 fun bind' (ctx, b) =
|
adamc@76
|
734 let
|
adamc@76
|
735 val b' = case b of
|
adamc@623
|
736 Sgn.RelK x => RelK x
|
adamc@623
|
737 | Sgn.RelC x => RelC x
|
adamc@76
|
738 | Sgn.NamedC x => NamedC x
|
adamc@792
|
739 | Sgn.Sgn (x, _, y) => Sgn (x, y)
|
adamc@792
|
740 | Sgn.Str (x, _, y) => Str (x, y)
|
adamc@76
|
741 in
|
adamc@76
|
742 bind (ctx, b')
|
adamc@76
|
743 end
|
adamc@76
|
744 val mfsg = Sgn.mapfoldB {kind = fk, con = fc, sgn_item = fsgi, sgn = fsg, bind = bind'}
|
adamc@76
|
745
|
adamc@76
|
746 fun mfst ctx str acc =
|
adamc@76
|
747 S.bindP (mfst' ctx str acc, fst ctx)
|
adamc@76
|
748
|
adamc@76
|
749 and mfst' ctx (strAll as (str, loc)) =
|
adamc@76
|
750 case str of
|
adamc@76
|
751 StrConst ds =>
|
adamc@76
|
752 S.map2 (ListUtil.mapfoldB (fn (ctx, d) =>
|
adamc@76
|
753 (case #1 d of
|
adamc@792
|
754 DCon (x, n, k, c) =>
|
adamc@792
|
755 bind (ctx, NamedC (x, n, k, SOME c))
|
adamc@191
|
756 | DDatatype (x, n, xs, xncs) =>
|
adamc@156
|
757 let
|
adamc@792
|
758 val ctx = bind (ctx, NamedC (x, n, (KType, loc), NONE))
|
adamc@156
|
759 in
|
adamc@156
|
760 foldl (fn ((x, _, co), ctx) =>
|
adamc@156
|
761 let
|
adamc@156
|
762 val t =
|
adamc@156
|
763 case co of
|
adamc@156
|
764 NONE => CNamed n
|
adamc@156
|
765 | SOME t => TFun (t, (CNamed n, loc))
|
adamc@191
|
766
|
adamc@191
|
767 val k = (KType, loc)
|
adamc@191
|
768 val t = (t, loc)
|
adamc@191
|
769 val t = foldr (fn (x, t) =>
|
adamc@191
|
770 (TCFun (Explicit,
|
adamc@191
|
771 x,
|
adamc@191
|
772 k,
|
adamc@191
|
773 t), loc))
|
adamc@191
|
774 t xs
|
adamc@156
|
775 in
|
adamc@191
|
776 bind (ctx, NamedE (x, t))
|
adamc@156
|
777 end)
|
adamc@156
|
778 ctx xncs
|
adamc@156
|
779 end
|
adamc@191
|
780 | DDatatypeImp (x, n, m, ms, x', _, _) =>
|
adamc@792
|
781 bind (ctx, NamedC (x, n, (KType, loc),
|
adamc@792
|
782 SOME (CModProj (m, ms, x'), loc)))
|
adamc@76
|
783 | DVal (x, _, c, _) =>
|
adamc@76
|
784 bind (ctx, NamedE (x, c))
|
adamc@123
|
785 | DValRec vis =>
|
adamc@123
|
786 foldl (fn ((x, _, c, _), ctx) => bind (ctx, NamedE (x, c))) ctx vis
|
adamc@76
|
787 | DSgn (x, _, sgn) =>
|
adamc@76
|
788 bind (ctx, Sgn (x, sgn))
|
adamc@76
|
789 | DStr (x, _, sgn, _) =>
|
adamc@76
|
790 bind (ctx, Str (x, sgn))
|
adamc@76
|
791 | DFfiStr (x, _, sgn) =>
|
adamc@88
|
792 bind (ctx, Str (x, sgn))
|
adamc@100
|
793 | DConstraint _ => ctx
|
adamc@203
|
794 | DExport _ => ctx
|
adamc@707
|
795 | DTable (tn, x, n, c, _, pc, _, cc) =>
|
adamc@705
|
796 let
|
adamc@705
|
797 val ct = (CModProj (n, [], "sql_table"), loc)
|
adamc@705
|
798 val ct = (CApp (ct, c), loc)
|
adamc@707
|
799 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
|
adamc@705
|
800 in
|
adamc@705
|
801 bind (ctx, NamedE (x, ct))
|
adamc@705
|
802 end
|
adamc@338
|
803 | DSequence (tn, x, n) =>
|
adamc@338
|
804 bind (ctx, NamedE (x, (CModProj (n, [], "sql_sequence"), loc)))
|
adamc@754
|
805 | DView (tn, x, n, _, c) =>
|
adamc@754
|
806 let
|
adamc@754
|
807 val ct = (CModProj (n, [], "sql_view"), loc)
|
adamc@754
|
808 val ct = (CApp (ct, c), loc)
|
adamc@754
|
809 in
|
adamc@754
|
810 bind (ctx, NamedE (x, ct))
|
adamc@754
|
811 end
|
adamc@792
|
812 | DClass (x, n, k, c) =>
|
adamc@792
|
813 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), SOME c))
|
adamc@459
|
814 | DDatabase _ => ctx
|
adamc@459
|
815 | DCookie (tn, x, n, c) =>
|
adamc@459
|
816 bind (ctx, NamedE (x, (CApp ((CModProj (n, [], "cookie"), loc),
|
adamc@718
|
817 c), loc)))
|
adamc@720
|
818 | DStyle (tn, x, n) =>
|
adamc@720
|
819 bind (ctx, NamedE (x, (CModProj (n, [], "css_class"), loc))),
|
adamc@76
|
820 mfd ctx d)) ctx ds,
|
adamc@76
|
821 fn ds' => (StrConst ds', loc))
|
adamc@76
|
822 | StrVar _ => S.return2 strAll
|
adamc@76
|
823 | StrProj (str, x) =>
|
adamc@76
|
824 S.map2 (mfst ctx str,
|
adamc@76
|
825 fn str' =>
|
adamc@76
|
826 (StrProj (str', x), loc))
|
adamc@76
|
827 | StrFun (x, n, sgn1, sgn2, str) =>
|
adamc@76
|
828 S.bind2 (mfsg ctx sgn1,
|
adamc@76
|
829 fn sgn1' =>
|
adamc@76
|
830 S.bind2 (mfsg ctx sgn2,
|
adamc@76
|
831 fn sgn2' =>
|
adamc@76
|
832 S.map2 (mfst ctx str,
|
adamc@76
|
833 fn str' =>
|
adamc@76
|
834 (StrFun (x, n, sgn1', sgn2', str'), loc))))
|
adamc@76
|
835 | StrApp (str1, str2) =>
|
adamc@76
|
836 S.bind2 (mfst ctx str1,
|
adamc@76
|
837 fn str1' =>
|
adamc@76
|
838 S.map2 (mfst ctx str2,
|
adamc@76
|
839 fn str2' =>
|
adamc@76
|
840 (StrApp (str1', str2'), loc)))
|
adamc@76
|
841 | StrError => S.return2 strAll
|
adamc@76
|
842
|
adamc@76
|
843 and mfd ctx d acc =
|
adamc@76
|
844 S.bindP (mfd' ctx d acc, fd ctx)
|
adamc@76
|
845
|
adamc@76
|
846 and mfd' ctx (dAll as (d, loc)) =
|
adamc@76
|
847 case d of
|
adamc@76
|
848 DCon (x, n, k, c) =>
|
adamc@623
|
849 S.bind2 (mfk ctx k,
|
adamc@76
|
850 fn k' =>
|
adamc@76
|
851 S.map2 (mfc ctx c,
|
adamc@76
|
852 fn c' =>
|
adamc@76
|
853 (DCon (x, n, k', c'), loc)))
|
adamc@191
|
854 | DDatatype (x, n, xs, xncs) =>
|
adamc@156
|
855 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@156
|
856 case c of
|
adamc@156
|
857 NONE => S.return2 (x, n, c)
|
adamc@156
|
858 | SOME c =>
|
adamc@156
|
859 S.map2 (mfc ctx c,
|
adamc@156
|
860 fn c' => (x, n, SOME c'))) xncs,
|
adamc@156
|
861 fn xncs' =>
|
adamc@191
|
862 (DDatatype (x, n, xs, xncs'), loc))
|
adamc@191
|
863 | DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@162
|
864 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
865 case c of
|
adamc@162
|
866 NONE => S.return2 (x, n, c)
|
adamc@162
|
867 | SOME c =>
|
adamc@162
|
868 S.map2 (mfc ctx c,
|
adamc@162
|
869 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
870 fn xncs' =>
|
adamc@191
|
871 (DDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
|
adamc@123
|
872 | DVal vi =>
|
adamc@123
|
873 S.map2 (mfvi ctx vi,
|
adamc@123
|
874 fn vi' =>
|
adamc@123
|
875 (DVal vi', loc))
|
adamc@123
|
876 | DValRec vis =>
|
adamc@123
|
877 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@123
|
878 fn vis' =>
|
adamc@123
|
879 (DValRec vis', loc))
|
adamc@76
|
880 | DSgn (x, n, sgn) =>
|
adamc@76
|
881 S.map2 (mfsg ctx sgn,
|
adamc@76
|
882 fn sgn' =>
|
adamc@76
|
883 (DSgn (x, n, sgn'), loc))
|
adamc@76
|
884 | DStr (x, n, sgn, str) =>
|
adamc@76
|
885 S.bind2 (mfsg ctx sgn,
|
adamc@76
|
886 fn sgn' =>
|
adamc@76
|
887 S.map2 (mfst ctx str,
|
adamc@76
|
888 fn str' =>
|
adamc@76
|
889 (DStr (x, n, sgn', str'), loc)))
|
adamc@76
|
890 | DFfiStr (x, n, sgn) =>
|
adamc@76
|
891 S.map2 (mfsg ctx sgn,
|
adamc@76
|
892 fn sgn' =>
|
adamc@76
|
893 (DFfiStr (x, n, sgn'), loc))
|
adamc@88
|
894 | DConstraint (c1, c2) =>
|
adamc@88
|
895 S.bind2 (mfc ctx c1,
|
adamc@88
|
896 fn c1' =>
|
adamc@88
|
897 S.map2 (mfc ctx c2,
|
adamc@88
|
898 fn c2' =>
|
adamc@88
|
899 (DConstraint (c1', c2'), loc)))
|
adamc@109
|
900 | DExport (en, sgn, str) =>
|
adamc@109
|
901 S.bind2 (mfsg ctx sgn,
|
adamc@109
|
902 fn sgn' =>
|
adamc@109
|
903 S.map2 (mfst ctx str,
|
adamc@109
|
904 fn str' =>
|
adamc@109
|
905 (DExport (en, sgn', str'), loc)))
|
adamc@123
|
906
|
adamc@707
|
907 | DTable (tn, x, n, c, pe, pc, ce, cc) =>
|
adamc@704
|
908 S.bind2 (mfc ctx c,
|
adamc@203
|
909 fn c' =>
|
adamc@707
|
910 S.bind2 (mfe ctx pe,
|
adamc@707
|
911 fn pe' =>
|
adamc@707
|
912 S.bind2 (mfc ctx pc,
|
adamc@707
|
913 fn pc' =>
|
adamc@707
|
914 S.bind2 (mfe ctx ce,
|
adamc@707
|
915 fn ce' =>
|
adamc@707
|
916 S.map2 (mfc ctx cc,
|
adamc@707
|
917 fn cc' =>
|
adamc@707
|
918 (DTable (tn, x, n, c', pe', pc', ce', cc'), loc))))))
|
adamc@338
|
919 | DSequence _ => S.return2 dAll
|
adamc@754
|
920 | DView (tn, x, n, e, c) =>
|
adamc@754
|
921 S.bind2 (mfe ctx e,
|
adamc@754
|
922 fn e' =>
|
adamc@754
|
923 S.map2 (mfc ctx c,
|
adamc@754
|
924 fn c' =>
|
adamc@754
|
925 (DView (tn, x, n, e', c'), loc)))
|
adamc@203
|
926
|
adamc@563
|
927 | DClass (x, n, k, c) =>
|
adamc@623
|
928 S.bind2 (mfk ctx k,
|
adamc@563
|
929 fn k' =>
|
adamc@563
|
930 S.map2 (mfc ctx c,
|
adamc@563
|
931 fn c' =>
|
adamc@563
|
932 (DClass (x, n, k', c'), loc)))
|
adamc@213
|
933
|
adamc@338
|
934 | DDatabase _ => S.return2 dAll
|
adamc@271
|
935
|
adamc@459
|
936 | DCookie (tn, x, n, c) =>
|
adamc@459
|
937 S.map2 (mfc ctx c,
|
adamc@459
|
938 fn c' =>
|
adamc@459
|
939 (DCookie (tn, x, n, c'), loc))
|
adamc@720
|
940 | DStyle _ => S.return2 dAll
|
adamc@459
|
941
|
adamc@123
|
942 and mfvi ctx (x, n, c, e) =
|
adamc@123
|
943 S.bind2 (mfc ctx c,
|
adamc@123
|
944 fn c' =>
|
adamc@123
|
945 S.map2 (mfe ctx e,
|
adamc@123
|
946 fn e' =>
|
adamc@123
|
947 (x, n, c', e')))
|
adamc@76
|
948 in
|
adamc@76
|
949 mfd
|
adamc@76
|
950 end
|
adamc@76
|
951
|
adamc@76
|
952 fun mapfold {kind, con, exp, sgn_item, sgn, str, decl} =
|
adamc@623
|
953 mapfoldB {kind = fn () => kind,
|
adamc@76
|
954 con = fn () => con,
|
adamc@76
|
955 exp = fn () => exp,
|
adamc@76
|
956 sgn_item = fn () => sgn_item,
|
adamc@76
|
957 sgn = fn () => sgn,
|
adamc@76
|
958 str = fn () => str,
|
adamc@76
|
959 decl = fn () => decl,
|
adamc@76
|
960 bind = fn ((), _) => ()} ()
|
adamc@76
|
961
|
adamc@76
|
962 fun exists {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
963 case mapfold {kind = fn k => fn () =>
|
adamc@76
|
964 if kind k then
|
adamc@76
|
965 S.Return ()
|
adamc@76
|
966 else
|
adamc@76
|
967 S.Continue (k, ()),
|
adamc@76
|
968 con = fn c => fn () =>
|
adamc@76
|
969 if con c then
|
adamc@76
|
970 S.Return ()
|
adamc@76
|
971 else
|
adamc@76
|
972 S.Continue (c, ()),
|
adamc@76
|
973 exp = fn e => fn () =>
|
adamc@76
|
974 if exp e then
|
adamc@76
|
975 S.Return ()
|
adamc@76
|
976 else
|
adamc@76
|
977 S.Continue (e, ()),
|
adamc@76
|
978 sgn_item = fn sgi => fn () =>
|
adamc@76
|
979 if sgn_item sgi then
|
adamc@76
|
980 S.Return ()
|
adamc@76
|
981 else
|
adamc@76
|
982 S.Continue (sgi, ()),
|
adamc@76
|
983 sgn = fn x => fn () =>
|
adamc@76
|
984 if sgn x then
|
adamc@76
|
985 S.Return ()
|
adamc@76
|
986 else
|
adamc@76
|
987 S.Continue (x, ()),
|
adamc@76
|
988 str = fn x => fn () =>
|
adamc@76
|
989 if str x then
|
adamc@76
|
990 S.Return ()
|
adamc@76
|
991 else
|
adamc@76
|
992 S.Continue (x, ()),
|
adamc@76
|
993 decl = fn x => fn () =>
|
adamc@76
|
994 if decl x then
|
adamc@76
|
995 S.Return ()
|
adamc@76
|
996 else
|
adamc@76
|
997 S.Continue (x, ())} k () of
|
adamc@76
|
998 S.Return _ => true
|
adamc@76
|
999 | S.Continue _ => false
|
adamc@76
|
1000
|
adamc@76
|
1001 fun search {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
1002 case mapfold {kind = fn x => fn () =>
|
adamc@76
|
1003 case kind x of
|
adamc@76
|
1004 NONE => S.Continue (x, ())
|
adamc@76
|
1005 | SOME v => S.Return v,
|
adamc@76
|
1006
|
adamc@76
|
1007 con = fn x => fn () =>
|
adamc@76
|
1008 case con x of
|
adamc@76
|
1009 NONE => S.Continue (x, ())
|
adamc@76
|
1010 | SOME v => S.Return v,
|
adamc@76
|
1011
|
adamc@76
|
1012 exp = fn x => fn () =>
|
adamc@76
|
1013 case exp x of
|
adamc@76
|
1014 NONE => S.Continue (x, ())
|
adamc@76
|
1015 | SOME v => S.Return v,
|
adamc@76
|
1016
|
adamc@76
|
1017 sgn_item = fn x => fn () =>
|
adamc@76
|
1018 case sgn_item x of
|
adamc@76
|
1019 NONE => S.Continue (x, ())
|
adamc@76
|
1020 | SOME v => S.Return v,
|
adamc@76
|
1021
|
adamc@76
|
1022 sgn = fn x => fn () =>
|
adamc@76
|
1023 case sgn x of
|
adamc@76
|
1024 NONE => S.Continue (x, ())
|
adamc@76
|
1025 | SOME v => S.Return v,
|
adamc@76
|
1026
|
adamc@76
|
1027 str = fn x => fn () =>
|
adamc@76
|
1028 case str x of
|
adamc@76
|
1029 NONE => S.Continue (x, ())
|
adamc@76
|
1030 | SOME v => S.Return v,
|
adamc@76
|
1031
|
adamc@76
|
1032 decl = fn x => fn () =>
|
adamc@76
|
1033 case decl x of
|
adamc@76
|
1034 NONE => S.Continue (x, ())
|
adamc@76
|
1035 | SOME v => S.Return v
|
adamc@76
|
1036
|
adamc@76
|
1037 } k () of
|
adamc@76
|
1038 S.Return x => SOME x
|
adamc@76
|
1039 | S.Continue _ => NONE
|
adamc@76
|
1040
|
adamc@448
|
1041 fun foldMapB {kind, con, exp, sgn_item, sgn, str, decl, bind} ctx st d =
|
adamc@623
|
1042 case mapfoldB {kind = fn ctx => fn x => fn st => S.Continue (kind (ctx, x, st)),
|
adamc@448
|
1043 con = fn ctx => fn x => fn st => S.Continue (con (ctx, x, st)),
|
adamc@448
|
1044 exp = fn ctx => fn x => fn st => S.Continue (exp (ctx, x, st)),
|
adamc@448
|
1045 sgn_item = fn ctx => fn x => fn st => S.Continue (sgn_item (ctx, x, st)),
|
adamc@448
|
1046 sgn = fn ctx => fn x => fn st => S.Continue (sgn (ctx, x, st)),
|
adamc@448
|
1047 str = fn ctx => fn x => fn st => S.Continue (str (ctx, x, st)),
|
adamc@448
|
1048 decl = fn ctx => fn x => fn st => S.Continue (decl (ctx, x, st)),
|
adamc@448
|
1049 bind = bind} ctx d st of
|
adamc@448
|
1050 S.Continue x => x
|
adamc@448
|
1051 | S.Return _ => raise Fail "ElabUtil.Decl.foldMapB: Impossible"
|
adamc@448
|
1052
|
adamc@448
|
1053 end
|
adamc@448
|
1054
|
adamc@448
|
1055 structure File = struct
|
adamc@448
|
1056
|
adamc@448
|
1057 fun maxName ds = foldl (fn (d, count) => Int.max (maxNameDecl d, count)) 0 ds
|
adamc@448
|
1058
|
adamc@448
|
1059 and maxNameDecl (d, _) =
|
adamc@448
|
1060 case d of
|
adamc@448
|
1061 DCon (_, n, _, _) => n
|
adamc@448
|
1062 | DDatatype (_, n, _, ns) =>
|
adamc@448
|
1063 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@448
|
1064 n ns
|
adamc@448
|
1065 | DDatatypeImp (_, n1, n2, _, _, _, ns) =>
|
adamc@448
|
1066 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@448
|
1067 (Int.max (n1, n2)) ns
|
adamc@448
|
1068 | DVal (_, n, _, _) => n
|
adamc@448
|
1069 | DValRec vis => foldl (fn ((_, n, _, _), count) => Int.max (n, count)) 0 vis
|
adamc@448
|
1070 | DStr (_, n, sgn, str) => Int.max (n, Int.max (maxNameSgn sgn, maxNameStr str))
|
adamc@448
|
1071 | DSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
1072 | DFfiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
1073 | DConstraint _ => 0
|
adamc@563
|
1074 | DClass (_, n, _, _) => n
|
adamc@448
|
1075 | DExport _ => 0
|
adamc@707
|
1076 | DTable (n1, _, n2, _, _, _, _, _) => Int.max (n1, n2)
|
adamc@459
|
1077 | DSequence (n1, _, n2) => Int.max (n1, n2)
|
adamc@754
|
1078 | DView (n1, _, n2, _, _) => Int.max (n1, n2)
|
adamc@448
|
1079 | DDatabase _ => 0
|
adamc@459
|
1080 | DCookie (n1, _, n2, _) => Int.max (n1, n2)
|
adamc@720
|
1081 | DStyle (n1, _, n2) => Int.max (n1, n2)
|
adamc@448
|
1082 and maxNameStr (str, _) =
|
adamc@448
|
1083 case str of
|
adamc@448
|
1084 StrConst ds => maxName ds
|
adamc@448
|
1085 | StrVar n => n
|
adamc@448
|
1086 | StrProj (str, _) => maxNameStr str
|
adamc@448
|
1087 | StrFun (_, n, dom, ran, str) => foldl Int.max n [maxNameSgn dom, maxNameSgn ran, maxNameStr str]
|
adamc@448
|
1088 | StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
|
adamc@448
|
1089 | StrError => 0
|
adamc@448
|
1090
|
adamc@448
|
1091 and maxNameSgn (sgn, _) =
|
adamc@448
|
1092 case sgn of
|
adamc@448
|
1093 SgnConst sgis => foldl (fn (sgi, count) => Int.max (maxNameSgi sgi, count)) 0 sgis
|
adamc@448
|
1094 | SgnVar n => n
|
adamc@448
|
1095 | SgnFun (_, n, dom, ran) => Int.max (n, Int.max (maxNameSgn dom, maxNameSgn ran))
|
adamc@448
|
1096 | SgnWhere (sgn, _, _) => maxNameSgn sgn
|
adamc@448
|
1097 | SgnProj (n, _, _) => n
|
adamc@448
|
1098 | SgnError => 0
|
adamc@448
|
1099
|
adamc@448
|
1100 and maxNameSgi (sgi, _) =
|
adamc@448
|
1101 case sgi of
|
adamc@448
|
1102 SgiConAbs (_, n, _) => n
|
adamc@448
|
1103 | SgiCon (_, n, _, _) => n
|
adamc@448
|
1104 | SgiDatatype (_, n, _, ns) =>
|
adamc@448
|
1105 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@448
|
1106 n ns
|
adamc@448
|
1107 | SgiDatatypeImp (_, n1, n2, _, _, _, ns) =>
|
adamc@448
|
1108 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@448
|
1109 (Int.max (n1, n2)) ns
|
adamc@448
|
1110 | SgiVal (_, n, _) => n
|
adamc@448
|
1111 | SgiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
1112 | SgiSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
1113 | SgiConstraint _ => 0
|
adamc@563
|
1114 | SgiClassAbs (_, n, _) => n
|
adamc@563
|
1115 | SgiClass (_, n, _, _) => n
|
adamc@448
|
1116
|
adamc@34
|
1117 end
|
adamc@76
|
1118
|
adamc@76
|
1119 end
|