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@6
|
46 fun mapfold f =
|
adamc@2
|
47 let
|
adamc@2
|
48 fun mfk k acc =
|
adamc@2
|
49 S.bindP (mfk' k acc, f)
|
adamc@2
|
50
|
adamc@2
|
51 and mfk' (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@2
|
56 S.bind2 (mfk k1,
|
adamc@2
|
57 fn k1' =>
|
adamc@2
|
58 S.map2 (mfk 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@2
|
65 S.map2 (mfk 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@207
|
72 S.map2 (ListUtil.mapfold mfk 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@76
|
78 | KUnif (_, _, ref (SOME k)) => mfk' k
|
adamc@2
|
79 | KUnif _ => S.return2 kAll
|
adamc@2
|
80 in
|
adamc@2
|
81 mfk
|
adamc@2
|
82 end
|
adamc@2
|
83
|
adamc@2
|
84 fun exists f k =
|
adamc@6
|
85 case mapfold (fn k => fn () =>
|
adamc@6
|
86 if f k then
|
adamc@6
|
87 S.Return ()
|
adamc@6
|
88 else
|
adamc@6
|
89 S.Continue (k, ())) k () of
|
adamc@6
|
90 S.Return _ => true
|
adamc@6
|
91 | S.Continue _ => false
|
adamc@6
|
92
|
adamc@6
|
93 end
|
adamc@6
|
94
|
adamc@6
|
95 structure Con = struct
|
adamc@6
|
96
|
adamc@11
|
97 datatype binder =
|
adamc@11
|
98 Rel of string * Elab.kind
|
adamc@329
|
99 | Named of string * int * Elab.kind
|
adamc@11
|
100
|
adamc@11
|
101 fun mapfoldB {kind = fk, con = fc, bind} =
|
adamc@6
|
102 let
|
adamc@6
|
103 val mfk = Kind.mapfold fk
|
adamc@6
|
104
|
adamc@11
|
105 fun mfc ctx c acc =
|
adamc@11
|
106 S.bindP (mfc' ctx c acc, fc ctx)
|
adamc@6
|
107
|
adamc@11
|
108 and mfc' ctx (cAll as (c, loc)) =
|
adamc@6
|
109 case c of
|
adamc@6
|
110 TFun (c1, c2) =>
|
adamc@11
|
111 S.bind2 (mfc ctx c1,
|
adamc@6
|
112 fn c1' =>
|
adamc@11
|
113 S.map2 (mfc ctx c2,
|
adamc@6
|
114 fn c2' =>
|
adamc@6
|
115 (TFun (c1', c2'), loc)))
|
adamc@6
|
116 | TCFun (e, x, k, c) =>
|
adamc@6
|
117 S.bind2 (mfk k,
|
adamc@6
|
118 fn k' =>
|
adamc@11
|
119 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
|
adamc@6
|
120 fn c' =>
|
adamc@6
|
121 (TCFun (e, x, k', c'), loc)))
|
adamc@345
|
122 | CDisjoint (ai, c1, c2, c3) =>
|
adamc@85
|
123 S.bind2 (mfc ctx c1,
|
adamc@85
|
124 fn c1' =>
|
adamc@85
|
125 S.bind2 (mfc ctx c2,
|
adamc@85
|
126 fn c2' =>
|
adamc@85
|
127 S.map2 (mfc ctx c3,
|
adamc@85
|
128 fn c3' =>
|
adamc@345
|
129 (CDisjoint (ai, c1', c2', c3'), loc))))
|
adamc@6
|
130 | TRecord c =>
|
adamc@11
|
131 S.map2 (mfc ctx c,
|
adamc@6
|
132 fn c' =>
|
adamc@6
|
133 (TRecord c', loc))
|
adamc@6
|
134
|
adamc@6
|
135 | CRel _ => S.return2 cAll
|
adamc@6
|
136 | CNamed _ => S.return2 cAll
|
adamc@34
|
137 | CModProj _ => S.return2 cAll
|
adamc@6
|
138 | CApp (c1, c2) =>
|
adamc@11
|
139 S.bind2 (mfc ctx c1,
|
adamc@6
|
140 fn c1' =>
|
adamc@11
|
141 S.map2 (mfc ctx c2,
|
adamc@6
|
142 fn c2' =>
|
adamc@6
|
143 (CApp (c1', c2'), loc)))
|
adamc@8
|
144 | CAbs (x, k, c) =>
|
adamc@6
|
145 S.bind2 (mfk k,
|
adamc@6
|
146 fn k' =>
|
adamc@11
|
147 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
|
adamc@6
|
148 fn c' =>
|
adamc@8
|
149 (CAbs (x, k', c'), loc)))
|
adamc@6
|
150
|
adamc@6
|
151 | CName _ => S.return2 cAll
|
adamc@6
|
152
|
adamc@6
|
153 | CRecord (k, xcs) =>
|
adamc@6
|
154 S.bind2 (mfk k,
|
adamc@6
|
155 fn k' =>
|
adamc@6
|
156 S.map2 (ListUtil.mapfold (fn (x, c) =>
|
adamc@11
|
157 S.bind2 (mfc ctx x,
|
adamc@6
|
158 fn x' =>
|
adamc@11
|
159 S.map2 (mfc ctx c,
|
adamc@6
|
160 fn c' =>
|
adamc@6
|
161 (x', c'))))
|
adamc@6
|
162 xcs,
|
adamc@6
|
163 fn xcs' =>
|
adamc@6
|
164 (CRecord (k', xcs'), loc)))
|
adamc@6
|
165 | CConcat (c1, c2) =>
|
adamc@11
|
166 S.bind2 (mfc ctx c1,
|
adamc@6
|
167 fn c1' =>
|
adamc@11
|
168 S.map2 (mfc ctx c2,
|
adamc@6
|
169 fn c2' =>
|
adamc@6
|
170 (CConcat (c1', c2'), loc)))
|
adamc@67
|
171 | CFold (k1, k2) =>
|
adamc@67
|
172 S.bind2 (mfk k1,
|
adamc@67
|
173 fn k1' =>
|
adamc@67
|
174 S.map2 (mfk k2,
|
adamc@67
|
175 fn k2' =>
|
adamc@67
|
176 (CFold (k1', k2'), loc)))
|
adamc@6
|
177
|
adamc@82
|
178 | CUnit => S.return2 cAll
|
adamc@82
|
179
|
adamc@207
|
180 | CTuple cs =>
|
adamc@207
|
181 S.map2 (ListUtil.mapfold (mfc ctx) cs,
|
adamc@207
|
182 fn cs' =>
|
adamc@207
|
183 (CTuple cs', loc))
|
adamc@207
|
184
|
adamc@207
|
185 | CProj (c, n) =>
|
adamc@207
|
186 S.map2 (mfc ctx c,
|
adamc@207
|
187 fn c' =>
|
adamc@207
|
188 (CProj (c', n), loc))
|
adamc@207
|
189
|
adamc@6
|
190 | CError => S.return2 cAll
|
adamc@76
|
191 | CUnif (_, _, _, ref (SOME c)) => mfc' ctx c
|
adamc@6
|
192 | CUnif _ => S.return2 cAll
|
adamc@6
|
193 in
|
adamc@6
|
194 mfc
|
adamc@6
|
195 end
|
adamc@6
|
196
|
adamc@11
|
197 fun mapfold {kind = fk, con = fc} =
|
adamc@11
|
198 mapfoldB {kind = fk,
|
adamc@11
|
199 con = fn () => fc,
|
adamc@11
|
200 bind = fn ((), _) => ()} ()
|
adamc@11
|
201
|
adamc@11
|
202 fun mapB {kind, con, bind} ctx c =
|
adamc@11
|
203 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@11
|
204 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@11
|
205 bind = bind} ctx c () of
|
adamc@11
|
206 S.Continue (c, ()) => c
|
adamc@34
|
207 | S.Return _ => raise Fail "ElabUtil.Con.mapB: Impossible"
|
adamc@34
|
208
|
adamc@34
|
209 fun map {kind, con} s =
|
adamc@34
|
210 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@34
|
211 con = fn c => fn () => S.Continue (con c, ())} s () of
|
adamc@34
|
212 S.Return () => raise Fail "ElabUtil.Con.map: Impossible"
|
adamc@34
|
213 | S.Continue (s, ()) => s
|
adamc@11
|
214
|
adamc@6
|
215 fun exists {kind, con} k =
|
adamc@6
|
216 case mapfold {kind = fn k => fn () =>
|
adamc@6
|
217 if kind k then
|
adamc@6
|
218 S.Return ()
|
adamc@6
|
219 else
|
adamc@6
|
220 S.Continue (k, ()),
|
adamc@6
|
221 con = fn c => fn () =>
|
adamc@6
|
222 if con c then
|
adamc@6
|
223 S.Return ()
|
adamc@6
|
224 else
|
adamc@6
|
225 S.Continue (c, ())} k () of
|
adamc@2
|
226 S.Return _ => true
|
adamc@2
|
227 | S.Continue _ => false
|
adamc@2
|
228
|
adamc@448
|
229 fun foldB {kind, con, bind} ctx st c =
|
adamc@448
|
230 case mapfoldB {kind = fn k => fn st => S.Continue (k, kind (k, st)),
|
adamc@448
|
231 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
|
adamc@448
|
232 bind = bind} ctx c st of
|
adamc@448
|
233 S.Continue (_, st) => st
|
adamc@448
|
234 | S.Return _ => raise Fail "ElabUtil.Con.foldB: Impossible"
|
adamc@448
|
235
|
adamc@2
|
236 end
|
adamc@2
|
237
|
adamc@10
|
238 structure Exp = struct
|
adamc@10
|
239
|
adamc@11
|
240 datatype binder =
|
adamc@11
|
241 RelC of string * Elab.kind
|
adamc@329
|
242 | NamedC of string * int * Elab.kind
|
adamc@11
|
243 | RelE of string * Elab.con
|
adamc@11
|
244 | NamedE of string * Elab.con
|
adamc@11
|
245
|
adamc@11
|
246 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
|
adamc@10
|
247 let
|
adamc@10
|
248 val mfk = Kind.mapfold fk
|
adamc@10
|
249
|
adamc@11
|
250 fun bind' (ctx, b) =
|
adamc@11
|
251 let
|
adamc@11
|
252 val b' = case b of
|
adamc@11
|
253 Con.Rel x => RelC x
|
adamc@11
|
254 | Con.Named x => NamedC x
|
adamc@11
|
255 in
|
adamc@11
|
256 bind (ctx, b')
|
adamc@11
|
257 end
|
adamc@11
|
258 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@10
|
259
|
adamc@11
|
260 fun mfe ctx e acc =
|
adamc@11
|
261 S.bindP (mfe' ctx e acc, fe ctx)
|
adamc@11
|
262
|
adamc@11
|
263 and mfe' ctx (eAll as (e, loc)) =
|
adamc@10
|
264 case e of
|
adamc@14
|
265 EPrim _ => S.return2 eAll
|
adamc@14
|
266 | ERel _ => S.return2 eAll
|
adamc@10
|
267 | ENamed _ => S.return2 eAll
|
adamc@34
|
268 | EModProj _ => S.return2 eAll
|
adamc@10
|
269 | EApp (e1, e2) =>
|
adamc@11
|
270 S.bind2 (mfe ctx e1,
|
adamc@10
|
271 fn e1' =>
|
adamc@11
|
272 S.map2 (mfe ctx e2,
|
adamc@10
|
273 fn e2' =>
|
adamc@10
|
274 (EApp (e1', e2'), loc)))
|
adamc@26
|
275 | EAbs (x, dom, ran, e) =>
|
adamc@26
|
276 S.bind2 (mfc ctx dom,
|
adamc@26
|
277 fn dom' =>
|
adamc@26
|
278 S.bind2 (mfc ctx ran,
|
adamc@26
|
279 fn ran' =>
|
adamc@26
|
280 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
|
adamc@26
|
281 fn e' =>
|
adamc@26
|
282 (EAbs (x, dom', ran', e'), loc))))
|
adamc@26
|
283
|
adamc@10
|
284 | ECApp (e, c) =>
|
adamc@11
|
285 S.bind2 (mfe ctx e,
|
adamc@10
|
286 fn e' =>
|
adamc@11
|
287 S.map2 (mfc ctx c,
|
adamc@10
|
288 fn c' =>
|
adamc@10
|
289 (ECApp (e', c'), loc)))
|
adamc@10
|
290 | ECAbs (expl, x, k, e) =>
|
adamc@10
|
291 S.bind2 (mfk k,
|
adamc@10
|
292 fn k' =>
|
adamc@11
|
293 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
|
adamc@10
|
294 fn e' =>
|
adamc@10
|
295 (ECAbs (expl, x, k', e'), loc)))
|
adamc@10
|
296
|
adamc@12
|
297 | ERecord xes =>
|
adamc@29
|
298 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
|
adamc@12
|
299 S.bind2 (mfc ctx x,
|
adamc@12
|
300 fn x' =>
|
adamc@29
|
301 S.bind2 (mfe ctx e,
|
adamc@12
|
302 fn e' =>
|
adamc@29
|
303 S.map2 (mfc ctx t,
|
adamc@29
|
304 fn t' =>
|
adamc@29
|
305 (x', e', t')))))
|
adamc@12
|
306 xes,
|
adamc@12
|
307 fn xes' =>
|
adamc@12
|
308 (ERecord xes', loc))
|
adamc@12
|
309 | EField (e, c, {field, rest}) =>
|
adamc@12
|
310 S.bind2 (mfe ctx e,
|
adamc@12
|
311 fn e' =>
|
adamc@12
|
312 S.bind2 (mfc ctx c,
|
adamc@12
|
313 fn c' =>
|
adamc@12
|
314 S.bind2 (mfc ctx field,
|
adamc@12
|
315 fn field' =>
|
adamc@12
|
316 S.map2 (mfc ctx rest,
|
adamc@12
|
317 fn rest' =>
|
adamc@12
|
318 (EField (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@445
|
319 | EConcat (e1, c1, e2, c2) =>
|
adamc@339
|
320 S.bind2 (mfe ctx e1,
|
adamc@339
|
321 fn e1' =>
|
adamc@445
|
322 S.bind2 (mfc ctx c1,
|
adamc@445
|
323 fn c1' =>
|
adamc@339
|
324 S.bind2 (mfe ctx e2,
|
adamc@339
|
325 fn e2' =>
|
adamc@445
|
326 S.map2 (mfc ctx c2,
|
adamc@445
|
327 fn c2' =>
|
adamc@445
|
328 (EConcat (e1', c1', e2', c2'),
|
adamc@445
|
329 loc)))))
|
adamc@149
|
330 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
331 S.bind2 (mfe ctx e,
|
adamc@149
|
332 fn e' =>
|
adamc@149
|
333 S.bind2 (mfc ctx c,
|
adamc@149
|
334 fn c' =>
|
adamc@149
|
335 S.bind2 (mfc ctx field,
|
adamc@149
|
336 fn field' =>
|
adamc@149
|
337 S.map2 (mfc ctx rest,
|
adamc@149
|
338 fn rest' =>
|
adamc@149
|
339 (ECut (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@12
|
340
|
adamc@71
|
341 | EFold k =>
|
adamc@71
|
342 S.map2 (mfk k,
|
adamc@71
|
343 fn k' =>
|
adamc@71
|
344 (EFold k', loc))
|
adamc@71
|
345
|
adamc@182
|
346 | ECase (e, pes, {disc, result}) =>
|
adamc@171
|
347 S.bind2 (mfe ctx e,
|
adamc@171
|
348 fn e' =>
|
adamc@171
|
349 S.bind2 (ListUtil.mapfold (fn (p, e) =>
|
adamc@448
|
350 let
|
adamc@448
|
351 fun pb ((p, _), ctx) =
|
adamc@448
|
352 case p of
|
adamc@448
|
353 PWild => ctx
|
adamc@448
|
354 | PVar (x, t) => bind (ctx, RelE (x, t))
|
adamc@448
|
355 | PPrim _ => ctx
|
adamc@448
|
356 | PCon (_, _, _, NONE) => ctx
|
adamc@448
|
357 | PCon (_, _, _, SOME p) => pb (p, ctx)
|
adamc@448
|
358 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
|
adamc@448
|
359 pb (p, ctx)) ctx xps
|
adamc@448
|
360 in
|
adamc@448
|
361 S.map2 (mfe (pb (p, ctx)) e,
|
adamc@448
|
362 fn e' => (p, e'))
|
adamc@448
|
363 end) pes,
|
adamc@171
|
364 fn pes' =>
|
adamc@182
|
365 S.bind2 (mfc ctx disc,
|
adamc@182
|
366 fn disc' =>
|
adamc@182
|
367 S.map2 (mfc ctx result,
|
adamc@182
|
368 fn result' =>
|
adamc@182
|
369 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
|
adamc@171
|
370
|
adamc@10
|
371 | EError => S.return2 eAll
|
adamc@228
|
372 | EUnif (ref (SOME e)) => mfe ctx e
|
adamc@228
|
373 | EUnif _ => S.return2 eAll
|
adamc@447
|
374
|
adamc@447
|
375 | ELet (des, e) =>
|
adamc@447
|
376 let
|
adamc@447
|
377 val (des, ctx) = foldl (fn (ed, (des, ctx)) =>
|
adamc@453
|
378 let
|
adamc@453
|
379 val ctx' =
|
adamc@453
|
380 case #1 ed of
|
adamc@453
|
381 EDVal (x, t, _) => bind (ctx, RelE (x, t))
|
adamc@453
|
382 | EDValRec vis =>
|
adamc@453
|
383 foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t))) ctx vis
|
adamc@453
|
384 in
|
adamc@453
|
385 (S.bind2 (des,
|
adamc@453
|
386 fn des' =>
|
adamc@453
|
387 S.map2 (mfed ctx ed,
|
adamc@447
|
388 fn ed' => des' @ [ed'])),
|
adamc@453
|
389 ctx')
|
adamc@453
|
390 end)
|
adamc@447
|
391 (S.return2 [], ctx) des
|
adamc@447
|
392 in
|
adamc@447
|
393 S.bind2 (des,
|
adamc@447
|
394 fn des' =>
|
adamc@447
|
395 S.map2 (mfe ctx e,
|
adamc@447
|
396 fn e' =>
|
adamc@447
|
397 (ELet (des', e'), loc)))
|
adamc@447
|
398 end
|
adamc@447
|
399
|
adamc@447
|
400 and mfed ctx (dAll as (d, loc)) =
|
adamc@447
|
401 case d of
|
adamc@447
|
402 EDVal vi =>
|
adamc@447
|
403 S.map2 (mfvi ctx vi,
|
adamc@447
|
404 fn vi' =>
|
adamc@447
|
405 (EDVal vi', loc))
|
adamc@447
|
406 | EDValRec vis =>
|
adamc@447
|
407 let
|
adamc@453
|
408 val ctx = foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t))) ctx vis
|
adamc@447
|
409 in
|
adamc@447
|
410 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@447
|
411 fn vis' =>
|
adamc@447
|
412 (EDValRec vis', loc))
|
adamc@447
|
413 end
|
adamc@447
|
414
|
adamc@447
|
415 and mfvi ctx (x, c, e) =
|
adamc@447
|
416 S.bind2 (mfc ctx c,
|
adamc@447
|
417 fn c' =>
|
adamc@447
|
418 S.map2 (mfe ctx e,
|
adamc@447
|
419 fn e' =>
|
adamc@447
|
420 (x, c', e')))
|
adamc@10
|
421 in
|
adamc@10
|
422 mfe
|
adamc@10
|
423 end
|
adamc@10
|
424
|
adamc@11
|
425 fun mapfold {kind = fk, con = fc, exp = fe} =
|
adamc@11
|
426 mapfoldB {kind = fk,
|
adamc@11
|
427 con = fn () => fc,
|
adamc@11
|
428 exp = fn () => fe,
|
adamc@11
|
429 bind = fn ((), _) => ()} ()
|
adamc@11
|
430
|
adamc@10
|
431 fun exists {kind, con, exp} k =
|
adamc@10
|
432 case mapfold {kind = fn k => fn () =>
|
adamc@10
|
433 if kind k then
|
adamc@10
|
434 S.Return ()
|
adamc@10
|
435 else
|
adamc@10
|
436 S.Continue (k, ()),
|
adamc@10
|
437 con = fn c => fn () =>
|
adamc@10
|
438 if con c then
|
adamc@10
|
439 S.Return ()
|
adamc@10
|
440 else
|
adamc@10
|
441 S.Continue (c, ()),
|
adamc@10
|
442 exp = fn e => fn () =>
|
adamc@10
|
443 if exp e then
|
adamc@10
|
444 S.Return ()
|
adamc@10
|
445 else
|
adamc@10
|
446 S.Continue (e, ())} k () of
|
adamc@10
|
447 S.Return _ => true
|
adamc@10
|
448 | S.Continue _ => false
|
adamc@10
|
449
|
adamc@211
|
450 fun mapB {kind, con, exp, bind} ctx e =
|
adamc@211
|
451 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@211
|
452 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@211
|
453 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
|
adamc@211
|
454 bind = bind} ctx e () of
|
adamc@211
|
455 S.Continue (e, ()) => e
|
adamc@211
|
456 | S.Return _ => raise Fail "ElabUtil.Exp.mapB: Impossible"
|
adamc@211
|
457
|
adamc@448
|
458 fun foldB {kind, con, exp, bind} ctx st e =
|
adamc@448
|
459 case mapfoldB {kind = fn k => fn st => S.Continue (k, kind (k, st)),
|
adamc@448
|
460 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
|
adamc@448
|
461 exp = fn ctx => fn e => fn st => S.Continue (e, exp (ctx, e, st)),
|
adamc@448
|
462 bind = bind} ctx e st of
|
adamc@448
|
463 S.Continue (_, st) => st
|
adamc@448
|
464 | S.Return _ => raise Fail "ElabUtil.Exp.foldB: Impossible"
|
adamc@448
|
465
|
adamc@10
|
466 end
|
adamc@10
|
467
|
adamc@34
|
468 structure Sgn = struct
|
adamc@34
|
469
|
adamc@34
|
470 datatype binder =
|
adamc@34
|
471 RelC of string * Elab.kind
|
adamc@329
|
472 | NamedC of string * int * Elab.kind
|
adamc@34
|
473 | Str of string * Elab.sgn
|
adamc@59
|
474 | Sgn of string * Elab.sgn
|
adamc@34
|
475
|
adamc@34
|
476 fun mapfoldB {kind, con, sgn_item, sgn, bind} =
|
adamc@34
|
477 let
|
adamc@34
|
478 fun bind' (ctx, b) =
|
adamc@34
|
479 let
|
adamc@34
|
480 val b' = case b of
|
adamc@34
|
481 Con.Rel x => RelC x
|
adamc@34
|
482 | Con.Named x => NamedC x
|
adamc@34
|
483 in
|
adamc@34
|
484 bind (ctx, b')
|
adamc@34
|
485 end
|
adamc@34
|
486 val con = Con.mapfoldB {kind = kind, con = con, bind = bind'}
|
adamc@34
|
487
|
adamc@34
|
488 val kind = Kind.mapfold kind
|
adamc@34
|
489
|
adamc@34
|
490 fun sgi ctx si acc =
|
adamc@34
|
491 S.bindP (sgi' ctx si acc, sgn_item ctx)
|
adamc@34
|
492
|
adamc@156
|
493 and sgi' ctx (siAll as (si, loc)) =
|
adamc@34
|
494 case si of
|
adamc@34
|
495 SgiConAbs (x, n, k) =>
|
adamc@34
|
496 S.map2 (kind k,
|
adamc@34
|
497 fn k' =>
|
adamc@34
|
498 (SgiConAbs (x, n, k'), loc))
|
adamc@34
|
499 | SgiCon (x, n, k, c) =>
|
adamc@34
|
500 S.bind2 (kind k,
|
adamc@34
|
501 fn k' =>
|
adamc@34
|
502 S.map2 (con ctx c,
|
adamc@34
|
503 fn c' =>
|
adamc@34
|
504 (SgiCon (x, n, k', c'), loc)))
|
adamc@191
|
505 | SgiDatatype (x, n, xs, xncs) =>
|
adamc@156
|
506 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@156
|
507 case c of
|
adamc@156
|
508 NONE => S.return2 (x, n, c)
|
adamc@156
|
509 | SOME c =>
|
adamc@156
|
510 S.map2 (con ctx c,
|
adamc@156
|
511 fn c' => (x, n, SOME c'))) xncs,
|
adamc@156
|
512 fn xncs' =>
|
adamc@191
|
513 (SgiDatatype (x, n, xs, xncs'), loc))
|
adamc@191
|
514 | SgiDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@162
|
515 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
516 case c of
|
adamc@162
|
517 NONE => S.return2 (x, n, c)
|
adamc@162
|
518 | SOME c =>
|
adamc@162
|
519 S.map2 (con ctx c,
|
adamc@162
|
520 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
521 fn xncs' =>
|
adamc@191
|
522 (SgiDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
|
adamc@34
|
523 | SgiVal (x, n, c) =>
|
adamc@34
|
524 S.map2 (con ctx c,
|
adamc@34
|
525 fn c' =>
|
adamc@34
|
526 (SgiVal (x, n, c'), loc))
|
adamc@34
|
527 | SgiStr (x, n, s) =>
|
adamc@34
|
528 S.map2 (sg ctx s,
|
adamc@34
|
529 fn s' =>
|
adamc@34
|
530 (SgiStr (x, n, s'), loc))
|
adamc@59
|
531 | SgiSgn (x, n, s) =>
|
adamc@59
|
532 S.map2 (sg ctx s,
|
adamc@59
|
533 fn s' =>
|
adamc@59
|
534 (SgiSgn (x, n, s'), loc))
|
adamc@88
|
535 | SgiConstraint (c1, c2) =>
|
adamc@88
|
536 S.bind2 (con ctx c1,
|
adamc@88
|
537 fn c1' =>
|
adamc@88
|
538 S.map2 (con ctx c2,
|
adamc@88
|
539 fn c2' =>
|
adamc@88
|
540 (SgiConstraint (c1', c2'), loc)))
|
adamc@205
|
541 | SgiTable (tn, x, n, c) =>
|
adamc@203
|
542 S.map2 (con ctx c,
|
adamc@203
|
543 fn c' =>
|
adamc@205
|
544 (SgiTable (tn, x, n, c'), loc))
|
adamc@338
|
545 | SgiSequence _ => S.return2 siAll
|
adamc@211
|
546 | SgiClassAbs _ => S.return2 siAll
|
adamc@211
|
547 | SgiClass (x, n, c) =>
|
adamc@211
|
548 S.map2 (con ctx c,
|
adamc@211
|
549 fn c' =>
|
adamc@211
|
550 (SgiClass (x, n, c'), loc))
|
adamc@459
|
551 | SgiCookie (tn, x, n, c) =>
|
adamc@459
|
552 S.map2 (con ctx c,
|
adamc@459
|
553 fn c' =>
|
adamc@459
|
554 (SgiCookie (tn, x, n, c'), loc))
|
adamc@34
|
555
|
adamc@34
|
556 and sg ctx s acc =
|
adamc@34
|
557 S.bindP (sg' ctx s acc, sgn ctx)
|
adamc@34
|
558
|
adamc@34
|
559 and sg' ctx (sAll as (s, loc)) =
|
adamc@34
|
560 case s of
|
adamc@34
|
561 SgnConst sgis =>
|
adamc@34
|
562 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
|
adamc@34
|
563 (case #1 si of
|
adamc@329
|
564 SgiConAbs (x, n, k) =>
|
adamc@329
|
565 bind (ctx, NamedC (x, n, k))
|
adamc@329
|
566 | SgiCon (x, n, k, _) =>
|
adamc@329
|
567 bind (ctx, NamedC (x, n, k))
|
adamc@191
|
568 | SgiDatatype (x, n, _, xncs) =>
|
adamc@329
|
569 bind (ctx, NamedC (x, n, (KType, loc)))
|
adamc@329
|
570 | SgiDatatypeImp (x, n, _, _, _, _, _) =>
|
adamc@329
|
571 bind (ctx, NamedC (x, n, (KType, loc)))
|
adamc@34
|
572 | SgiVal _ => ctx
|
adamc@34
|
573 | SgiStr (x, _, sgn) =>
|
adamc@59
|
574 bind (ctx, Str (x, sgn))
|
adamc@59
|
575 | SgiSgn (x, _, sgn) =>
|
adamc@88
|
576 bind (ctx, Sgn (x, sgn))
|
adamc@203
|
577 | SgiConstraint _ => ctx
|
adamc@211
|
578 | SgiTable _ => ctx
|
adamc@338
|
579 | SgiSequence _ => ctx
|
adamc@329
|
580 | SgiClassAbs (x, n) =>
|
adamc@329
|
581 bind (ctx, NamedC (x, n, (KArrow ((KType, loc), (KType, loc)), loc)))
|
adamc@329
|
582 | SgiClass (x, n, _) =>
|
adamc@459
|
583 bind (ctx, NamedC (x, n, (KArrow ((KType, loc), (KType, loc)), loc)))
|
adamc@459
|
584 | SgiCookie _ => ctx,
|
adamc@34
|
585 sgi ctx si)) ctx sgis,
|
adamc@34
|
586 fn sgis' =>
|
adamc@34
|
587 (SgnConst sgis', loc))
|
adamc@34
|
588
|
adamc@34
|
589 | SgnVar _ => S.return2 sAll
|
adamc@41
|
590 | SgnFun (m, n, s1, s2) =>
|
adamc@41
|
591 S.bind2 (sg ctx s1,
|
adamc@41
|
592 fn s1' =>
|
adamc@41
|
593 S.map2 (sg (bind (ctx, Str (m, s1'))) s2,
|
adamc@41
|
594 fn s2' =>
|
adamc@41
|
595 (SgnFun (m, n, s1', s2'), loc)))
|
adamc@59
|
596 | SgnProj _ => S.return2 sAll
|
adamc@42
|
597 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
598 S.bind2 (sg ctx sgn,
|
adamc@42
|
599 fn sgn' =>
|
adamc@42
|
600 S.map2 (con ctx c,
|
adamc@42
|
601 fn c' =>
|
adamc@42
|
602 (SgnWhere (sgn', x, c'), loc)))
|
adamc@34
|
603 | SgnError => S.return2 sAll
|
adamc@34
|
604 in
|
adamc@34
|
605 sg
|
adamc@34
|
606 end
|
adamc@34
|
607
|
adamc@34
|
608 fun mapfold {kind, con, sgn_item, sgn} =
|
adamc@34
|
609 mapfoldB {kind = kind,
|
adamc@34
|
610 con = fn () => con,
|
adamc@34
|
611 sgn_item = fn () => sgn_item,
|
adamc@34
|
612 sgn = fn () => sgn,
|
adamc@34
|
613 bind = fn ((), _) => ()} ()
|
adamc@34
|
614
|
adamc@34
|
615 fun map {kind, con, sgn_item, sgn} s =
|
adamc@34
|
616 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@34
|
617 con = fn c => fn () => S.Continue (con c, ()),
|
adamc@34
|
618 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
|
adamc@34
|
619 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
|
adamc@34
|
620 S.Return () => raise Fail "Elab_util.Sgn.map"
|
adamc@34
|
621 | S.Continue (s, ()) => s
|
adamc@34
|
622
|
adamc@2
|
623 end
|
adamc@34
|
624
|
adamc@76
|
625 structure Decl = struct
|
adamc@76
|
626
|
adamc@76
|
627 datatype binder =
|
adamc@76
|
628 RelC of string * Elab.kind
|
adamc@329
|
629 | NamedC of string * int * Elab.kind
|
adamc@76
|
630 | RelE of string * Elab.con
|
adamc@76
|
631 | NamedE of string * Elab.con
|
adamc@76
|
632 | Str of string * Elab.sgn
|
adamc@76
|
633 | Sgn of string * Elab.sgn
|
adamc@76
|
634
|
adamc@76
|
635 fun mapfoldB {kind = fk, con = fc, exp = fe, sgn_item = fsgi, sgn = fsg, str = fst, decl = fd, bind} =
|
adamc@76
|
636 let
|
adamc@76
|
637 val mfk = Kind.mapfold fk
|
adamc@76
|
638
|
adamc@76
|
639 fun bind' (ctx, b) =
|
adamc@76
|
640 let
|
adamc@76
|
641 val b' = case b of
|
adamc@76
|
642 Con.Rel x => RelC x
|
adamc@76
|
643 | Con.Named x => NamedC x
|
adamc@76
|
644 in
|
adamc@76
|
645 bind (ctx, b')
|
adamc@76
|
646 end
|
adamc@76
|
647 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@76
|
648
|
adamc@76
|
649 fun bind' (ctx, b) =
|
adamc@76
|
650 let
|
adamc@76
|
651 val b' = case b of
|
adamc@76
|
652 Exp.RelC x => RelC x
|
adamc@76
|
653 | Exp.NamedC x => NamedC x
|
adamc@76
|
654 | Exp.RelE x => RelE x
|
adamc@76
|
655 | Exp.NamedE x => NamedE x
|
adamc@76
|
656 in
|
adamc@76
|
657 bind (ctx, b')
|
adamc@76
|
658 end
|
adamc@76
|
659 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind'}
|
adamc@76
|
660
|
adamc@76
|
661 fun bind' (ctx, b) =
|
adamc@76
|
662 let
|
adamc@76
|
663 val b' = case b of
|
adamc@76
|
664 Sgn.RelC x => RelC x
|
adamc@76
|
665 | Sgn.NamedC x => NamedC x
|
adamc@76
|
666 | Sgn.Sgn x => Sgn x
|
adamc@76
|
667 | Sgn.Str x => Str x
|
adamc@76
|
668 in
|
adamc@76
|
669 bind (ctx, b')
|
adamc@76
|
670 end
|
adamc@76
|
671 val mfsg = Sgn.mapfoldB {kind = fk, con = fc, sgn_item = fsgi, sgn = fsg, bind = bind'}
|
adamc@76
|
672
|
adamc@76
|
673 fun mfst ctx str acc =
|
adamc@76
|
674 S.bindP (mfst' ctx str acc, fst ctx)
|
adamc@76
|
675
|
adamc@76
|
676 and mfst' ctx (strAll as (str, loc)) =
|
adamc@76
|
677 case str of
|
adamc@76
|
678 StrConst ds =>
|
adamc@76
|
679 S.map2 (ListUtil.mapfoldB (fn (ctx, d) =>
|
adamc@76
|
680 (case #1 d of
|
adamc@329
|
681 DCon (x, n, k, _) =>
|
adamc@329
|
682 bind (ctx, NamedC (x, n, k))
|
adamc@191
|
683 | DDatatype (x, n, xs, xncs) =>
|
adamc@156
|
684 let
|
adamc@329
|
685 val ctx = bind (ctx, NamedC (x, n, (KType, loc)))
|
adamc@156
|
686 in
|
adamc@156
|
687 foldl (fn ((x, _, co), ctx) =>
|
adamc@156
|
688 let
|
adamc@156
|
689 val t =
|
adamc@156
|
690 case co of
|
adamc@156
|
691 NONE => CNamed n
|
adamc@156
|
692 | SOME t => TFun (t, (CNamed n, loc))
|
adamc@191
|
693
|
adamc@191
|
694 val k = (KType, loc)
|
adamc@191
|
695 val t = (t, loc)
|
adamc@191
|
696 val t = foldr (fn (x, t) =>
|
adamc@191
|
697 (TCFun (Explicit,
|
adamc@191
|
698 x,
|
adamc@191
|
699 k,
|
adamc@191
|
700 t), loc))
|
adamc@191
|
701 t xs
|
adamc@156
|
702 in
|
adamc@191
|
703 bind (ctx, NamedE (x, t))
|
adamc@156
|
704 end)
|
adamc@156
|
705 ctx xncs
|
adamc@156
|
706 end
|
adamc@191
|
707 | DDatatypeImp (x, n, m, ms, x', _, _) =>
|
adamc@329
|
708 bind (ctx, NamedC (x, n, (KType, loc)))
|
adamc@76
|
709 | DVal (x, _, c, _) =>
|
adamc@76
|
710 bind (ctx, NamedE (x, c))
|
adamc@123
|
711 | DValRec vis =>
|
adamc@123
|
712 foldl (fn ((x, _, c, _), ctx) => bind (ctx, NamedE (x, c))) ctx vis
|
adamc@76
|
713 | DSgn (x, _, sgn) =>
|
adamc@76
|
714 bind (ctx, Sgn (x, sgn))
|
adamc@76
|
715 | DStr (x, _, sgn, _) =>
|
adamc@76
|
716 bind (ctx, Str (x, sgn))
|
adamc@76
|
717 | DFfiStr (x, _, sgn) =>
|
adamc@88
|
718 bind (ctx, Str (x, sgn))
|
adamc@100
|
719 | DConstraint _ => ctx
|
adamc@203
|
720 | DExport _ => ctx
|
adamc@205
|
721 | DTable (tn, x, n, c) =>
|
adamc@338
|
722 bind (ctx, NamedE (x, (CApp ((CModProj (n, [], "sql_table"), loc),
|
adamc@213
|
723 c), loc)))
|
adamc@338
|
724 | DSequence (tn, x, n) =>
|
adamc@338
|
725 bind (ctx, NamedE (x, (CModProj (n, [], "sql_sequence"), loc)))
|
adamc@329
|
726 | DClass (x, n, _) =>
|
adamc@329
|
727 bind (ctx, NamedC (x, n, (KArrow ((KType, loc), (KType, loc)), loc)))
|
adamc@459
|
728 | DDatabase _ => ctx
|
adamc@459
|
729 | DCookie (tn, x, n, c) =>
|
adamc@459
|
730 bind (ctx, NamedE (x, (CApp ((CModProj (n, [], "cookie"), loc),
|
adamc@459
|
731 c), loc))),
|
adamc@76
|
732 mfd ctx d)) ctx ds,
|
adamc@76
|
733 fn ds' => (StrConst ds', loc))
|
adamc@76
|
734 | StrVar _ => S.return2 strAll
|
adamc@76
|
735 | StrProj (str, x) =>
|
adamc@76
|
736 S.map2 (mfst ctx str,
|
adamc@76
|
737 fn str' =>
|
adamc@76
|
738 (StrProj (str', x), loc))
|
adamc@76
|
739 | StrFun (x, n, sgn1, sgn2, str) =>
|
adamc@76
|
740 S.bind2 (mfsg ctx sgn1,
|
adamc@76
|
741 fn sgn1' =>
|
adamc@76
|
742 S.bind2 (mfsg ctx sgn2,
|
adamc@76
|
743 fn sgn2' =>
|
adamc@76
|
744 S.map2 (mfst ctx str,
|
adamc@76
|
745 fn str' =>
|
adamc@76
|
746 (StrFun (x, n, sgn1', sgn2', str'), loc))))
|
adamc@76
|
747 | StrApp (str1, str2) =>
|
adamc@76
|
748 S.bind2 (mfst ctx str1,
|
adamc@76
|
749 fn str1' =>
|
adamc@76
|
750 S.map2 (mfst ctx str2,
|
adamc@76
|
751 fn str2' =>
|
adamc@76
|
752 (StrApp (str1', str2'), loc)))
|
adamc@76
|
753 | StrError => S.return2 strAll
|
adamc@76
|
754
|
adamc@76
|
755 and mfd ctx d acc =
|
adamc@76
|
756 S.bindP (mfd' ctx d acc, fd ctx)
|
adamc@76
|
757
|
adamc@76
|
758 and mfd' ctx (dAll as (d, loc)) =
|
adamc@76
|
759 case d of
|
adamc@76
|
760 DCon (x, n, k, c) =>
|
adamc@76
|
761 S.bind2 (mfk k,
|
adamc@76
|
762 fn k' =>
|
adamc@76
|
763 S.map2 (mfc ctx c,
|
adamc@76
|
764 fn c' =>
|
adamc@76
|
765 (DCon (x, n, k', c'), loc)))
|
adamc@191
|
766 | DDatatype (x, n, xs, xncs) =>
|
adamc@156
|
767 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@156
|
768 case c of
|
adamc@156
|
769 NONE => S.return2 (x, n, c)
|
adamc@156
|
770 | SOME c =>
|
adamc@156
|
771 S.map2 (mfc ctx c,
|
adamc@156
|
772 fn c' => (x, n, SOME c'))) xncs,
|
adamc@156
|
773 fn xncs' =>
|
adamc@191
|
774 (DDatatype (x, n, xs, xncs'), loc))
|
adamc@191
|
775 | DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@162
|
776 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
777 case c of
|
adamc@162
|
778 NONE => S.return2 (x, n, c)
|
adamc@162
|
779 | SOME c =>
|
adamc@162
|
780 S.map2 (mfc ctx c,
|
adamc@162
|
781 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
782 fn xncs' =>
|
adamc@191
|
783 (DDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
|
adamc@123
|
784 | DVal vi =>
|
adamc@123
|
785 S.map2 (mfvi ctx vi,
|
adamc@123
|
786 fn vi' =>
|
adamc@123
|
787 (DVal vi', loc))
|
adamc@123
|
788 | DValRec vis =>
|
adamc@123
|
789 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@123
|
790 fn vis' =>
|
adamc@123
|
791 (DValRec vis', loc))
|
adamc@76
|
792 | DSgn (x, n, sgn) =>
|
adamc@76
|
793 S.map2 (mfsg ctx sgn,
|
adamc@76
|
794 fn sgn' =>
|
adamc@76
|
795 (DSgn (x, n, sgn'), loc))
|
adamc@76
|
796 | DStr (x, n, sgn, str) =>
|
adamc@76
|
797 S.bind2 (mfsg ctx sgn,
|
adamc@76
|
798 fn sgn' =>
|
adamc@76
|
799 S.map2 (mfst ctx str,
|
adamc@76
|
800 fn str' =>
|
adamc@76
|
801 (DStr (x, n, sgn', str'), loc)))
|
adamc@76
|
802 | DFfiStr (x, n, sgn) =>
|
adamc@76
|
803 S.map2 (mfsg ctx sgn,
|
adamc@76
|
804 fn sgn' =>
|
adamc@76
|
805 (DFfiStr (x, n, sgn'), loc))
|
adamc@88
|
806 | DConstraint (c1, c2) =>
|
adamc@88
|
807 S.bind2 (mfc ctx c1,
|
adamc@88
|
808 fn c1' =>
|
adamc@88
|
809 S.map2 (mfc ctx c2,
|
adamc@88
|
810 fn c2' =>
|
adamc@88
|
811 (DConstraint (c1', c2'), loc)))
|
adamc@109
|
812 | DExport (en, sgn, str) =>
|
adamc@109
|
813 S.bind2 (mfsg ctx sgn,
|
adamc@109
|
814 fn sgn' =>
|
adamc@109
|
815 S.map2 (mfst ctx str,
|
adamc@109
|
816 fn str' =>
|
adamc@109
|
817 (DExport (en, sgn', str'), loc)))
|
adamc@123
|
818
|
adamc@205
|
819 | DTable (tn, x, n, c) =>
|
adamc@203
|
820 S.map2 (mfc ctx c,
|
adamc@203
|
821 fn c' =>
|
adamc@205
|
822 (DTable (tn, x, n, c'), loc))
|
adamc@338
|
823 | DSequence _ => S.return2 dAll
|
adamc@203
|
824
|
adamc@338
|
825 | DClass (x, n, c) =>
|
adamc@213
|
826 S.map2 (mfc ctx c,
|
adamc@338
|
827 fn c' =>
|
adamc@338
|
828 (DClass (x, n, c'), loc))
|
adamc@213
|
829
|
adamc@338
|
830 | DDatabase _ => S.return2 dAll
|
adamc@271
|
831
|
adamc@459
|
832 | DCookie (tn, x, n, c) =>
|
adamc@459
|
833 S.map2 (mfc ctx c,
|
adamc@459
|
834 fn c' =>
|
adamc@459
|
835 (DCookie (tn, x, n, c'), loc))
|
adamc@459
|
836
|
adamc@123
|
837 and mfvi ctx (x, n, c, e) =
|
adamc@123
|
838 S.bind2 (mfc ctx c,
|
adamc@123
|
839 fn c' =>
|
adamc@123
|
840 S.map2 (mfe ctx e,
|
adamc@123
|
841 fn e' =>
|
adamc@123
|
842 (x, n, c', e')))
|
adamc@76
|
843 in
|
adamc@76
|
844 mfd
|
adamc@76
|
845 end
|
adamc@76
|
846
|
adamc@76
|
847 fun mapfold {kind, con, exp, sgn_item, sgn, str, decl} =
|
adamc@76
|
848 mapfoldB {kind = kind,
|
adamc@76
|
849 con = fn () => con,
|
adamc@76
|
850 exp = fn () => exp,
|
adamc@76
|
851 sgn_item = fn () => sgn_item,
|
adamc@76
|
852 sgn = fn () => sgn,
|
adamc@76
|
853 str = fn () => str,
|
adamc@76
|
854 decl = fn () => decl,
|
adamc@76
|
855 bind = fn ((), _) => ()} ()
|
adamc@76
|
856
|
adamc@76
|
857 fun exists {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
858 case mapfold {kind = fn k => fn () =>
|
adamc@76
|
859 if kind k then
|
adamc@76
|
860 S.Return ()
|
adamc@76
|
861 else
|
adamc@76
|
862 S.Continue (k, ()),
|
adamc@76
|
863 con = fn c => fn () =>
|
adamc@76
|
864 if con c then
|
adamc@76
|
865 S.Return ()
|
adamc@76
|
866 else
|
adamc@76
|
867 S.Continue (c, ()),
|
adamc@76
|
868 exp = fn e => fn () =>
|
adamc@76
|
869 if exp e then
|
adamc@76
|
870 S.Return ()
|
adamc@76
|
871 else
|
adamc@76
|
872 S.Continue (e, ()),
|
adamc@76
|
873 sgn_item = fn sgi => fn () =>
|
adamc@76
|
874 if sgn_item sgi then
|
adamc@76
|
875 S.Return ()
|
adamc@76
|
876 else
|
adamc@76
|
877 S.Continue (sgi, ()),
|
adamc@76
|
878 sgn = fn x => fn () =>
|
adamc@76
|
879 if sgn x then
|
adamc@76
|
880 S.Return ()
|
adamc@76
|
881 else
|
adamc@76
|
882 S.Continue (x, ()),
|
adamc@76
|
883 str = fn x => fn () =>
|
adamc@76
|
884 if str x then
|
adamc@76
|
885 S.Return ()
|
adamc@76
|
886 else
|
adamc@76
|
887 S.Continue (x, ()),
|
adamc@76
|
888 decl = fn x => fn () =>
|
adamc@76
|
889 if decl x then
|
adamc@76
|
890 S.Return ()
|
adamc@76
|
891 else
|
adamc@76
|
892 S.Continue (x, ())} k () of
|
adamc@76
|
893 S.Return _ => true
|
adamc@76
|
894 | S.Continue _ => false
|
adamc@76
|
895
|
adamc@76
|
896 fun search {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
897 case mapfold {kind = fn x => fn () =>
|
adamc@76
|
898 case kind x of
|
adamc@76
|
899 NONE => S.Continue (x, ())
|
adamc@76
|
900 | SOME v => S.Return v,
|
adamc@76
|
901
|
adamc@76
|
902 con = fn x => fn () =>
|
adamc@76
|
903 case con x of
|
adamc@76
|
904 NONE => S.Continue (x, ())
|
adamc@76
|
905 | SOME v => S.Return v,
|
adamc@76
|
906
|
adamc@76
|
907 exp = fn x => fn () =>
|
adamc@76
|
908 case exp x of
|
adamc@76
|
909 NONE => S.Continue (x, ())
|
adamc@76
|
910 | SOME v => S.Return v,
|
adamc@76
|
911
|
adamc@76
|
912 sgn_item = fn x => fn () =>
|
adamc@76
|
913 case sgn_item x of
|
adamc@76
|
914 NONE => S.Continue (x, ())
|
adamc@76
|
915 | SOME v => S.Return v,
|
adamc@76
|
916
|
adamc@76
|
917 sgn = fn x => fn () =>
|
adamc@76
|
918 case sgn x of
|
adamc@76
|
919 NONE => S.Continue (x, ())
|
adamc@76
|
920 | SOME v => S.Return v,
|
adamc@76
|
921
|
adamc@76
|
922 str = fn x => fn () =>
|
adamc@76
|
923 case str x of
|
adamc@76
|
924 NONE => S.Continue (x, ())
|
adamc@76
|
925 | SOME v => S.Return v,
|
adamc@76
|
926
|
adamc@76
|
927 decl = fn x => fn () =>
|
adamc@76
|
928 case decl x of
|
adamc@76
|
929 NONE => S.Continue (x, ())
|
adamc@76
|
930 | SOME v => S.Return v
|
adamc@76
|
931
|
adamc@76
|
932 } k () of
|
adamc@76
|
933 S.Return x => SOME x
|
adamc@76
|
934 | S.Continue _ => NONE
|
adamc@76
|
935
|
adamc@448
|
936 fun foldMapB {kind, con, exp, sgn_item, sgn, str, decl, bind} ctx st d =
|
adamc@448
|
937 case mapfoldB {kind = fn x => fn st => S.Continue (kind (x, st)),
|
adamc@448
|
938 con = fn ctx => fn x => fn st => S.Continue (con (ctx, x, st)),
|
adamc@448
|
939 exp = fn ctx => fn x => fn st => S.Continue (exp (ctx, x, st)),
|
adamc@448
|
940 sgn_item = fn ctx => fn x => fn st => S.Continue (sgn_item (ctx, x, st)),
|
adamc@448
|
941 sgn = fn ctx => fn x => fn st => S.Continue (sgn (ctx, x, st)),
|
adamc@448
|
942 str = fn ctx => fn x => fn st => S.Continue (str (ctx, x, st)),
|
adamc@448
|
943 decl = fn ctx => fn x => fn st => S.Continue (decl (ctx, x, st)),
|
adamc@448
|
944 bind = bind} ctx d st of
|
adamc@448
|
945 S.Continue x => x
|
adamc@448
|
946 | S.Return _ => raise Fail "ElabUtil.Decl.foldMapB: Impossible"
|
adamc@448
|
947
|
adamc@448
|
948 end
|
adamc@448
|
949
|
adamc@448
|
950 structure File = struct
|
adamc@448
|
951
|
adamc@448
|
952 fun maxName ds = foldl (fn (d, count) => Int.max (maxNameDecl d, count)) 0 ds
|
adamc@448
|
953
|
adamc@448
|
954 and maxNameDecl (d, _) =
|
adamc@448
|
955 case d of
|
adamc@448
|
956 DCon (_, n, _, _) => n
|
adamc@448
|
957 | DDatatype (_, n, _, ns) =>
|
adamc@448
|
958 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@448
|
959 n ns
|
adamc@448
|
960 | DDatatypeImp (_, n1, n2, _, _, _, ns) =>
|
adamc@448
|
961 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@448
|
962 (Int.max (n1, n2)) ns
|
adamc@448
|
963 | DVal (_, n, _, _) => n
|
adamc@448
|
964 | DValRec vis => foldl (fn ((_, n, _, _), count) => Int.max (n, count)) 0 vis
|
adamc@448
|
965 | DStr (_, n, sgn, str) => Int.max (n, Int.max (maxNameSgn sgn, maxNameStr str))
|
adamc@448
|
966 | DSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
967 | DFfiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
968 | DConstraint _ => 0
|
adamc@448
|
969 | DClass (_, n, _) => n
|
adamc@448
|
970 | DExport _ => 0
|
adamc@459
|
971 | DTable (n1, _, n2, _) => Int.max (n1, n2)
|
adamc@459
|
972 | DSequence (n1, _, n2) => Int.max (n1, n2)
|
adamc@448
|
973 | DDatabase _ => 0
|
adamc@459
|
974 | DCookie (n1, _, n2, _) => Int.max (n1, n2)
|
adamc@448
|
975
|
adamc@448
|
976 and maxNameStr (str, _) =
|
adamc@448
|
977 case str of
|
adamc@448
|
978 StrConst ds => maxName ds
|
adamc@448
|
979 | StrVar n => n
|
adamc@448
|
980 | StrProj (str, _) => maxNameStr str
|
adamc@448
|
981 | StrFun (_, n, dom, ran, str) => foldl Int.max n [maxNameSgn dom, maxNameSgn ran, maxNameStr str]
|
adamc@448
|
982 | StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
|
adamc@448
|
983 | StrError => 0
|
adamc@448
|
984
|
adamc@448
|
985 and maxNameSgn (sgn, _) =
|
adamc@448
|
986 case sgn of
|
adamc@448
|
987 SgnConst sgis => foldl (fn (sgi, count) => Int.max (maxNameSgi sgi, count)) 0 sgis
|
adamc@448
|
988 | SgnVar n => n
|
adamc@448
|
989 | SgnFun (_, n, dom, ran) => Int.max (n, Int.max (maxNameSgn dom, maxNameSgn ran))
|
adamc@448
|
990 | SgnWhere (sgn, _, _) => maxNameSgn sgn
|
adamc@448
|
991 | SgnProj (n, _, _) => n
|
adamc@448
|
992 | SgnError => 0
|
adamc@448
|
993
|
adamc@448
|
994 and maxNameSgi (sgi, _) =
|
adamc@448
|
995 case sgi of
|
adamc@448
|
996 SgiConAbs (_, n, _) => n
|
adamc@448
|
997 | SgiCon (_, n, _, _) => n
|
adamc@448
|
998 | SgiDatatype (_, n, _, ns) =>
|
adamc@448
|
999 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@448
|
1000 n ns
|
adamc@448
|
1001 | SgiDatatypeImp (_, n1, n2, _, _, _, ns) =>
|
adamc@448
|
1002 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@448
|
1003 (Int.max (n1, n2)) ns
|
adamc@448
|
1004 | SgiVal (_, n, _) => n
|
adamc@448
|
1005 | SgiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
1006 | SgiSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
1007 | SgiConstraint _ => 0
|
adamc@459
|
1008 | SgiTable (n1, _, n2, _) => Int.max (n1, n2)
|
adamc@459
|
1009 | SgiSequence (n1, _, n2) => Int.max (n1, n2)
|
adamc@448
|
1010 | SgiClassAbs (_, n) => n
|
adamc@448
|
1011 | SgiClass (_, n, _) => n
|
adamc@459
|
1012 | SgiCookie (n1, _, n2, _) => Int.max (n1, n2)
|
adamc@448
|
1013
|
adamc@34
|
1014 end
|
adamc@76
|
1015
|
adamc@76
|
1016 end
|