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@2
|
229 end
|
adamc@2
|
230
|
adamc@10
|
231 structure Exp = struct
|
adamc@10
|
232
|
adamc@11
|
233 datatype binder =
|
adamc@11
|
234 RelC of string * Elab.kind
|
adamc@329
|
235 | NamedC of string * int * Elab.kind
|
adamc@11
|
236 | RelE of string * Elab.con
|
adamc@11
|
237 | NamedE of string * Elab.con
|
adamc@11
|
238
|
adamc@11
|
239 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
|
adamc@10
|
240 let
|
adamc@10
|
241 val mfk = Kind.mapfold fk
|
adamc@10
|
242
|
adamc@11
|
243 fun bind' (ctx, b) =
|
adamc@11
|
244 let
|
adamc@11
|
245 val b' = case b of
|
adamc@11
|
246 Con.Rel x => RelC x
|
adamc@11
|
247 | Con.Named x => NamedC x
|
adamc@11
|
248 in
|
adamc@11
|
249 bind (ctx, b')
|
adamc@11
|
250 end
|
adamc@11
|
251 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@10
|
252
|
adamc@11
|
253 fun mfe ctx e acc =
|
adamc@11
|
254 S.bindP (mfe' ctx e acc, fe ctx)
|
adamc@11
|
255
|
adamc@11
|
256 and mfe' ctx (eAll as (e, loc)) =
|
adamc@10
|
257 case e of
|
adamc@14
|
258 EPrim _ => S.return2 eAll
|
adamc@14
|
259 | ERel _ => S.return2 eAll
|
adamc@10
|
260 | ENamed _ => S.return2 eAll
|
adamc@34
|
261 | EModProj _ => S.return2 eAll
|
adamc@10
|
262 | EApp (e1, e2) =>
|
adamc@11
|
263 S.bind2 (mfe ctx e1,
|
adamc@10
|
264 fn e1' =>
|
adamc@11
|
265 S.map2 (mfe ctx e2,
|
adamc@10
|
266 fn e2' =>
|
adamc@10
|
267 (EApp (e1', e2'), loc)))
|
adamc@26
|
268 | EAbs (x, dom, ran, e) =>
|
adamc@26
|
269 S.bind2 (mfc ctx dom,
|
adamc@26
|
270 fn dom' =>
|
adamc@26
|
271 S.bind2 (mfc ctx ran,
|
adamc@26
|
272 fn ran' =>
|
adamc@26
|
273 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
|
adamc@26
|
274 fn e' =>
|
adamc@26
|
275 (EAbs (x, dom', ran', e'), loc))))
|
adamc@26
|
276
|
adamc@10
|
277 | ECApp (e, c) =>
|
adamc@11
|
278 S.bind2 (mfe ctx e,
|
adamc@10
|
279 fn e' =>
|
adamc@11
|
280 S.map2 (mfc ctx c,
|
adamc@10
|
281 fn c' =>
|
adamc@10
|
282 (ECApp (e', c'), loc)))
|
adamc@10
|
283 | ECAbs (expl, x, k, e) =>
|
adamc@10
|
284 S.bind2 (mfk k,
|
adamc@10
|
285 fn k' =>
|
adamc@11
|
286 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
|
adamc@10
|
287 fn e' =>
|
adamc@10
|
288 (ECAbs (expl, x, k', e'), loc)))
|
adamc@10
|
289
|
adamc@12
|
290 | ERecord xes =>
|
adamc@29
|
291 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
|
adamc@12
|
292 S.bind2 (mfc ctx x,
|
adamc@12
|
293 fn x' =>
|
adamc@29
|
294 S.bind2 (mfe ctx e,
|
adamc@12
|
295 fn e' =>
|
adamc@29
|
296 S.map2 (mfc ctx t,
|
adamc@29
|
297 fn t' =>
|
adamc@29
|
298 (x', e', t')))))
|
adamc@12
|
299 xes,
|
adamc@12
|
300 fn xes' =>
|
adamc@12
|
301 (ERecord xes', loc))
|
adamc@12
|
302 | EField (e, c, {field, rest}) =>
|
adamc@12
|
303 S.bind2 (mfe ctx e,
|
adamc@12
|
304 fn e' =>
|
adamc@12
|
305 S.bind2 (mfc ctx c,
|
adamc@12
|
306 fn c' =>
|
adamc@12
|
307 S.bind2 (mfc ctx field,
|
adamc@12
|
308 fn field' =>
|
adamc@12
|
309 S.map2 (mfc ctx rest,
|
adamc@12
|
310 fn rest' =>
|
adamc@12
|
311 (EField (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@445
|
312 | EConcat (e1, c1, e2, c2) =>
|
adamc@339
|
313 S.bind2 (mfe ctx e1,
|
adamc@339
|
314 fn e1' =>
|
adamc@445
|
315 S.bind2 (mfc ctx c1,
|
adamc@445
|
316 fn c1' =>
|
adamc@339
|
317 S.bind2 (mfe ctx e2,
|
adamc@339
|
318 fn e2' =>
|
adamc@445
|
319 S.map2 (mfc ctx c2,
|
adamc@445
|
320 fn c2' =>
|
adamc@445
|
321 (EConcat (e1', c1', e2', c2'),
|
adamc@445
|
322 loc)))))
|
adamc@149
|
323 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
324 S.bind2 (mfe ctx e,
|
adamc@149
|
325 fn e' =>
|
adamc@149
|
326 S.bind2 (mfc ctx c,
|
adamc@149
|
327 fn c' =>
|
adamc@149
|
328 S.bind2 (mfc ctx field,
|
adamc@149
|
329 fn field' =>
|
adamc@149
|
330 S.map2 (mfc ctx rest,
|
adamc@149
|
331 fn rest' =>
|
adamc@149
|
332 (ECut (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@12
|
333
|
adamc@71
|
334 | EFold k =>
|
adamc@71
|
335 S.map2 (mfk k,
|
adamc@71
|
336 fn k' =>
|
adamc@71
|
337 (EFold k', loc))
|
adamc@71
|
338
|
adamc@182
|
339 | ECase (e, pes, {disc, result}) =>
|
adamc@171
|
340 S.bind2 (mfe ctx e,
|
adamc@171
|
341 fn e' =>
|
adamc@171
|
342 S.bind2 (ListUtil.mapfold (fn (p, e) =>
|
adamc@171
|
343 S.map2 (mfe ctx e,
|
adamc@171
|
344 fn e' => (p, e'))) pes,
|
adamc@171
|
345 fn pes' =>
|
adamc@182
|
346 S.bind2 (mfc ctx disc,
|
adamc@182
|
347 fn disc' =>
|
adamc@182
|
348 S.map2 (mfc ctx result,
|
adamc@182
|
349 fn result' =>
|
adamc@182
|
350 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
|
adamc@171
|
351
|
adamc@10
|
352 | EError => S.return2 eAll
|
adamc@228
|
353 | EUnif (ref (SOME e)) => mfe ctx e
|
adamc@228
|
354 | EUnif _ => S.return2 eAll
|
adamc@447
|
355
|
adamc@447
|
356 | ELet (des, e) =>
|
adamc@447
|
357 let
|
adamc@447
|
358 val (des, ctx) = foldl (fn (ed, (des, ctx)) =>
|
adamc@447
|
359 (S.bind2 (des,
|
adamc@447
|
360 fn des' =>
|
adamc@447
|
361 S.map2 (mfed ctx ed,
|
adamc@447
|
362 fn ed' => des' @ [ed'])),
|
adamc@447
|
363 case #1 ed of
|
adamc@447
|
364 EDVal (x, t, _) => bind (ctx, RelE (x, t))
|
adamc@447
|
365 | EDValRec vis =>
|
adamc@447
|
366 foldl (fn ((x, t, _), env) => bind (ctx, RelE (x, t))) ctx vis))
|
adamc@447
|
367 (S.return2 [], ctx) des
|
adamc@447
|
368 in
|
adamc@447
|
369 S.bind2 (des,
|
adamc@447
|
370 fn des' =>
|
adamc@447
|
371 S.map2 (mfe ctx e,
|
adamc@447
|
372 fn e' =>
|
adamc@447
|
373 (ELet (des', e'), loc)))
|
adamc@447
|
374 end
|
adamc@447
|
375
|
adamc@447
|
376 and mfed ctx (dAll as (d, loc)) =
|
adamc@447
|
377 case d of
|
adamc@447
|
378 EDVal vi =>
|
adamc@447
|
379 S.map2 (mfvi ctx vi,
|
adamc@447
|
380 fn vi' =>
|
adamc@447
|
381 (EDVal vi', loc))
|
adamc@447
|
382 | EDValRec vis =>
|
adamc@447
|
383 let
|
adamc@447
|
384 val ctx = foldl (fn ((x, t, _), env) => bind (ctx, RelE (x, t))) ctx vis
|
adamc@447
|
385 in
|
adamc@447
|
386 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@447
|
387 fn vis' =>
|
adamc@447
|
388 (EDValRec vis', loc))
|
adamc@447
|
389 end
|
adamc@447
|
390
|
adamc@447
|
391 and mfvi ctx (x, c, e) =
|
adamc@447
|
392 S.bind2 (mfc ctx c,
|
adamc@447
|
393 fn c' =>
|
adamc@447
|
394 S.map2 (mfe ctx e,
|
adamc@447
|
395 fn e' =>
|
adamc@447
|
396 (x, c', e')))
|
adamc@10
|
397 in
|
adamc@10
|
398 mfe
|
adamc@10
|
399 end
|
adamc@10
|
400
|
adamc@11
|
401 fun mapfold {kind = fk, con = fc, exp = fe} =
|
adamc@11
|
402 mapfoldB {kind = fk,
|
adamc@11
|
403 con = fn () => fc,
|
adamc@11
|
404 exp = fn () => fe,
|
adamc@11
|
405 bind = fn ((), _) => ()} ()
|
adamc@11
|
406
|
adamc@10
|
407 fun exists {kind, con, exp} k =
|
adamc@10
|
408 case mapfold {kind = fn k => fn () =>
|
adamc@10
|
409 if kind k then
|
adamc@10
|
410 S.Return ()
|
adamc@10
|
411 else
|
adamc@10
|
412 S.Continue (k, ()),
|
adamc@10
|
413 con = fn c => fn () =>
|
adamc@10
|
414 if con c then
|
adamc@10
|
415 S.Return ()
|
adamc@10
|
416 else
|
adamc@10
|
417 S.Continue (c, ()),
|
adamc@10
|
418 exp = fn e => fn () =>
|
adamc@10
|
419 if exp e then
|
adamc@10
|
420 S.Return ()
|
adamc@10
|
421 else
|
adamc@10
|
422 S.Continue (e, ())} k () of
|
adamc@10
|
423 S.Return _ => true
|
adamc@10
|
424 | S.Continue _ => false
|
adamc@10
|
425
|
adamc@211
|
426 fun mapB {kind, con, exp, bind} ctx e =
|
adamc@211
|
427 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@211
|
428 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@211
|
429 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
|
adamc@211
|
430 bind = bind} ctx e () of
|
adamc@211
|
431 S.Continue (e, ()) => e
|
adamc@211
|
432 | S.Return _ => raise Fail "ElabUtil.Exp.mapB: Impossible"
|
adamc@211
|
433
|
adamc@10
|
434 end
|
adamc@10
|
435
|
adamc@34
|
436 structure Sgn = struct
|
adamc@34
|
437
|
adamc@34
|
438 datatype binder =
|
adamc@34
|
439 RelC of string * Elab.kind
|
adamc@329
|
440 | NamedC of string * int * Elab.kind
|
adamc@34
|
441 | Str of string * Elab.sgn
|
adamc@59
|
442 | Sgn of string * Elab.sgn
|
adamc@34
|
443
|
adamc@34
|
444 fun mapfoldB {kind, con, sgn_item, sgn, bind} =
|
adamc@34
|
445 let
|
adamc@34
|
446 fun bind' (ctx, b) =
|
adamc@34
|
447 let
|
adamc@34
|
448 val b' = case b of
|
adamc@34
|
449 Con.Rel x => RelC x
|
adamc@34
|
450 | Con.Named x => NamedC x
|
adamc@34
|
451 in
|
adamc@34
|
452 bind (ctx, b')
|
adamc@34
|
453 end
|
adamc@34
|
454 val con = Con.mapfoldB {kind = kind, con = con, bind = bind'}
|
adamc@34
|
455
|
adamc@34
|
456 val kind = Kind.mapfold kind
|
adamc@34
|
457
|
adamc@34
|
458 fun sgi ctx si acc =
|
adamc@34
|
459 S.bindP (sgi' ctx si acc, sgn_item ctx)
|
adamc@34
|
460
|
adamc@156
|
461 and sgi' ctx (siAll as (si, loc)) =
|
adamc@34
|
462 case si of
|
adamc@34
|
463 SgiConAbs (x, n, k) =>
|
adamc@34
|
464 S.map2 (kind k,
|
adamc@34
|
465 fn k' =>
|
adamc@34
|
466 (SgiConAbs (x, n, k'), loc))
|
adamc@34
|
467 | SgiCon (x, n, k, c) =>
|
adamc@34
|
468 S.bind2 (kind k,
|
adamc@34
|
469 fn k' =>
|
adamc@34
|
470 S.map2 (con ctx c,
|
adamc@34
|
471 fn c' =>
|
adamc@34
|
472 (SgiCon (x, n, k', c'), loc)))
|
adamc@191
|
473 | SgiDatatype (x, n, xs, xncs) =>
|
adamc@156
|
474 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@156
|
475 case c of
|
adamc@156
|
476 NONE => S.return2 (x, n, c)
|
adamc@156
|
477 | SOME c =>
|
adamc@156
|
478 S.map2 (con ctx c,
|
adamc@156
|
479 fn c' => (x, n, SOME c'))) xncs,
|
adamc@156
|
480 fn xncs' =>
|
adamc@191
|
481 (SgiDatatype (x, n, xs, xncs'), loc))
|
adamc@191
|
482 | SgiDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@162
|
483 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
484 case c of
|
adamc@162
|
485 NONE => S.return2 (x, n, c)
|
adamc@162
|
486 | SOME c =>
|
adamc@162
|
487 S.map2 (con ctx c,
|
adamc@162
|
488 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
489 fn xncs' =>
|
adamc@191
|
490 (SgiDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
|
adamc@34
|
491 | SgiVal (x, n, c) =>
|
adamc@34
|
492 S.map2 (con ctx c,
|
adamc@34
|
493 fn c' =>
|
adamc@34
|
494 (SgiVal (x, n, c'), loc))
|
adamc@34
|
495 | SgiStr (x, n, s) =>
|
adamc@34
|
496 S.map2 (sg ctx s,
|
adamc@34
|
497 fn s' =>
|
adamc@34
|
498 (SgiStr (x, n, s'), loc))
|
adamc@59
|
499 | SgiSgn (x, n, s) =>
|
adamc@59
|
500 S.map2 (sg ctx s,
|
adamc@59
|
501 fn s' =>
|
adamc@59
|
502 (SgiSgn (x, n, s'), loc))
|
adamc@88
|
503 | SgiConstraint (c1, c2) =>
|
adamc@88
|
504 S.bind2 (con ctx c1,
|
adamc@88
|
505 fn c1' =>
|
adamc@88
|
506 S.map2 (con ctx c2,
|
adamc@88
|
507 fn c2' =>
|
adamc@88
|
508 (SgiConstraint (c1', c2'), loc)))
|
adamc@205
|
509 | SgiTable (tn, x, n, c) =>
|
adamc@203
|
510 S.map2 (con ctx c,
|
adamc@203
|
511 fn c' =>
|
adamc@205
|
512 (SgiTable (tn, x, n, c'), loc))
|
adamc@338
|
513 | SgiSequence _ => S.return2 siAll
|
adamc@211
|
514 | SgiClassAbs _ => S.return2 siAll
|
adamc@211
|
515 | SgiClass (x, n, c) =>
|
adamc@211
|
516 S.map2 (con ctx c,
|
adamc@211
|
517 fn c' =>
|
adamc@211
|
518 (SgiClass (x, n, c'), loc))
|
adamc@34
|
519
|
adamc@34
|
520 and sg ctx s acc =
|
adamc@34
|
521 S.bindP (sg' ctx s acc, sgn ctx)
|
adamc@34
|
522
|
adamc@34
|
523 and sg' ctx (sAll as (s, loc)) =
|
adamc@34
|
524 case s of
|
adamc@34
|
525 SgnConst sgis =>
|
adamc@34
|
526 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
|
adamc@34
|
527 (case #1 si of
|
adamc@329
|
528 SgiConAbs (x, n, k) =>
|
adamc@329
|
529 bind (ctx, NamedC (x, n, k))
|
adamc@329
|
530 | SgiCon (x, n, k, _) =>
|
adamc@329
|
531 bind (ctx, NamedC (x, n, k))
|
adamc@191
|
532 | SgiDatatype (x, n, _, xncs) =>
|
adamc@329
|
533 bind (ctx, NamedC (x, n, (KType, loc)))
|
adamc@329
|
534 | SgiDatatypeImp (x, n, _, _, _, _, _) =>
|
adamc@329
|
535 bind (ctx, NamedC (x, n, (KType, loc)))
|
adamc@34
|
536 | SgiVal _ => ctx
|
adamc@34
|
537 | SgiStr (x, _, sgn) =>
|
adamc@59
|
538 bind (ctx, Str (x, sgn))
|
adamc@59
|
539 | SgiSgn (x, _, sgn) =>
|
adamc@88
|
540 bind (ctx, Sgn (x, sgn))
|
adamc@203
|
541 | SgiConstraint _ => ctx
|
adamc@211
|
542 | SgiTable _ => ctx
|
adamc@338
|
543 | SgiSequence _ => ctx
|
adamc@329
|
544 | SgiClassAbs (x, n) =>
|
adamc@329
|
545 bind (ctx, NamedC (x, n, (KArrow ((KType, loc), (KType, loc)), loc)))
|
adamc@329
|
546 | SgiClass (x, n, _) =>
|
adamc@329
|
547 bind (ctx, NamedC (x, n, (KArrow ((KType, loc), (KType, loc)), loc))),
|
adamc@34
|
548 sgi ctx si)) ctx sgis,
|
adamc@34
|
549 fn sgis' =>
|
adamc@34
|
550 (SgnConst sgis', loc))
|
adamc@34
|
551
|
adamc@34
|
552 | SgnVar _ => S.return2 sAll
|
adamc@41
|
553 | SgnFun (m, n, s1, s2) =>
|
adamc@41
|
554 S.bind2 (sg ctx s1,
|
adamc@41
|
555 fn s1' =>
|
adamc@41
|
556 S.map2 (sg (bind (ctx, Str (m, s1'))) s2,
|
adamc@41
|
557 fn s2' =>
|
adamc@41
|
558 (SgnFun (m, n, s1', s2'), loc)))
|
adamc@59
|
559 | SgnProj _ => S.return2 sAll
|
adamc@42
|
560 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
561 S.bind2 (sg ctx sgn,
|
adamc@42
|
562 fn sgn' =>
|
adamc@42
|
563 S.map2 (con ctx c,
|
adamc@42
|
564 fn c' =>
|
adamc@42
|
565 (SgnWhere (sgn', x, c'), loc)))
|
adamc@34
|
566 | SgnError => S.return2 sAll
|
adamc@34
|
567 in
|
adamc@34
|
568 sg
|
adamc@34
|
569 end
|
adamc@34
|
570
|
adamc@34
|
571 fun mapfold {kind, con, sgn_item, sgn} =
|
adamc@34
|
572 mapfoldB {kind = kind,
|
adamc@34
|
573 con = fn () => con,
|
adamc@34
|
574 sgn_item = fn () => sgn_item,
|
adamc@34
|
575 sgn = fn () => sgn,
|
adamc@34
|
576 bind = fn ((), _) => ()} ()
|
adamc@34
|
577
|
adamc@34
|
578 fun map {kind, con, sgn_item, sgn} s =
|
adamc@34
|
579 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@34
|
580 con = fn c => fn () => S.Continue (con c, ()),
|
adamc@34
|
581 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
|
adamc@34
|
582 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
|
adamc@34
|
583 S.Return () => raise Fail "Elab_util.Sgn.map"
|
adamc@34
|
584 | S.Continue (s, ()) => s
|
adamc@34
|
585
|
adamc@2
|
586 end
|
adamc@34
|
587
|
adamc@76
|
588 structure Decl = struct
|
adamc@76
|
589
|
adamc@76
|
590 datatype binder =
|
adamc@76
|
591 RelC of string * Elab.kind
|
adamc@329
|
592 | NamedC of string * int * Elab.kind
|
adamc@76
|
593 | RelE of string * Elab.con
|
adamc@76
|
594 | NamedE of string * Elab.con
|
adamc@76
|
595 | Str of string * Elab.sgn
|
adamc@76
|
596 | Sgn of string * Elab.sgn
|
adamc@76
|
597
|
adamc@76
|
598 fun mapfoldB {kind = fk, con = fc, exp = fe, sgn_item = fsgi, sgn = fsg, str = fst, decl = fd, bind} =
|
adamc@76
|
599 let
|
adamc@76
|
600 val mfk = Kind.mapfold fk
|
adamc@76
|
601
|
adamc@76
|
602 fun bind' (ctx, b) =
|
adamc@76
|
603 let
|
adamc@76
|
604 val b' = case b of
|
adamc@76
|
605 Con.Rel x => RelC x
|
adamc@76
|
606 | Con.Named x => NamedC x
|
adamc@76
|
607 in
|
adamc@76
|
608 bind (ctx, b')
|
adamc@76
|
609 end
|
adamc@76
|
610 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@76
|
611
|
adamc@76
|
612 fun bind' (ctx, b) =
|
adamc@76
|
613 let
|
adamc@76
|
614 val b' = case b of
|
adamc@76
|
615 Exp.RelC x => RelC x
|
adamc@76
|
616 | Exp.NamedC x => NamedC x
|
adamc@76
|
617 | Exp.RelE x => RelE x
|
adamc@76
|
618 | Exp.NamedE x => NamedE x
|
adamc@76
|
619 in
|
adamc@76
|
620 bind (ctx, b')
|
adamc@76
|
621 end
|
adamc@76
|
622 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind'}
|
adamc@76
|
623
|
adamc@76
|
624 fun bind' (ctx, b) =
|
adamc@76
|
625 let
|
adamc@76
|
626 val b' = case b of
|
adamc@76
|
627 Sgn.RelC x => RelC x
|
adamc@76
|
628 | Sgn.NamedC x => NamedC x
|
adamc@76
|
629 | Sgn.Sgn x => Sgn x
|
adamc@76
|
630 | Sgn.Str x => Str x
|
adamc@76
|
631 in
|
adamc@76
|
632 bind (ctx, b')
|
adamc@76
|
633 end
|
adamc@76
|
634 val mfsg = Sgn.mapfoldB {kind = fk, con = fc, sgn_item = fsgi, sgn = fsg, bind = bind'}
|
adamc@76
|
635
|
adamc@76
|
636 fun mfst ctx str acc =
|
adamc@76
|
637 S.bindP (mfst' ctx str acc, fst ctx)
|
adamc@76
|
638
|
adamc@76
|
639 and mfst' ctx (strAll as (str, loc)) =
|
adamc@76
|
640 case str of
|
adamc@76
|
641 StrConst ds =>
|
adamc@76
|
642 S.map2 (ListUtil.mapfoldB (fn (ctx, d) =>
|
adamc@76
|
643 (case #1 d of
|
adamc@329
|
644 DCon (x, n, k, _) =>
|
adamc@329
|
645 bind (ctx, NamedC (x, n, k))
|
adamc@191
|
646 | DDatatype (x, n, xs, xncs) =>
|
adamc@156
|
647 let
|
adamc@329
|
648 val ctx = bind (ctx, NamedC (x, n, (KType, loc)))
|
adamc@156
|
649 in
|
adamc@156
|
650 foldl (fn ((x, _, co), ctx) =>
|
adamc@156
|
651 let
|
adamc@156
|
652 val t =
|
adamc@156
|
653 case co of
|
adamc@156
|
654 NONE => CNamed n
|
adamc@156
|
655 | SOME t => TFun (t, (CNamed n, loc))
|
adamc@191
|
656
|
adamc@191
|
657 val k = (KType, loc)
|
adamc@191
|
658 val t = (t, loc)
|
adamc@191
|
659 val t = foldr (fn (x, t) =>
|
adamc@191
|
660 (TCFun (Explicit,
|
adamc@191
|
661 x,
|
adamc@191
|
662 k,
|
adamc@191
|
663 t), loc))
|
adamc@191
|
664 t xs
|
adamc@156
|
665 in
|
adamc@191
|
666 bind (ctx, NamedE (x, t))
|
adamc@156
|
667 end)
|
adamc@156
|
668 ctx xncs
|
adamc@156
|
669 end
|
adamc@191
|
670 | DDatatypeImp (x, n, m, ms, x', _, _) =>
|
adamc@329
|
671 bind (ctx, NamedC (x, n, (KType, loc)))
|
adamc@76
|
672 | DVal (x, _, c, _) =>
|
adamc@76
|
673 bind (ctx, NamedE (x, c))
|
adamc@123
|
674 | DValRec vis =>
|
adamc@123
|
675 foldl (fn ((x, _, c, _), ctx) => bind (ctx, NamedE (x, c))) ctx vis
|
adamc@76
|
676 | DSgn (x, _, sgn) =>
|
adamc@76
|
677 bind (ctx, Sgn (x, sgn))
|
adamc@76
|
678 | DStr (x, _, sgn, _) =>
|
adamc@76
|
679 bind (ctx, Str (x, sgn))
|
adamc@76
|
680 | DFfiStr (x, _, sgn) =>
|
adamc@88
|
681 bind (ctx, Str (x, sgn))
|
adamc@100
|
682 | DConstraint _ => ctx
|
adamc@203
|
683 | DExport _ => ctx
|
adamc@205
|
684 | DTable (tn, x, n, c) =>
|
adamc@338
|
685 bind (ctx, NamedE (x, (CApp ((CModProj (n, [], "sql_table"), loc),
|
adamc@213
|
686 c), loc)))
|
adamc@338
|
687 | DSequence (tn, x, n) =>
|
adamc@338
|
688 bind (ctx, NamedE (x, (CModProj (n, [], "sql_sequence"), loc)))
|
adamc@329
|
689 | DClass (x, n, _) =>
|
adamc@329
|
690 bind (ctx, NamedC (x, n, (KArrow ((KType, loc), (KType, loc)), loc)))
|
adamc@271
|
691 | DDatabase _ => ctx,
|
adamc@76
|
692 mfd ctx d)) ctx ds,
|
adamc@76
|
693 fn ds' => (StrConst ds', loc))
|
adamc@76
|
694 | StrVar _ => S.return2 strAll
|
adamc@76
|
695 | StrProj (str, x) =>
|
adamc@76
|
696 S.map2 (mfst ctx str,
|
adamc@76
|
697 fn str' =>
|
adamc@76
|
698 (StrProj (str', x), loc))
|
adamc@76
|
699 | StrFun (x, n, sgn1, sgn2, str) =>
|
adamc@76
|
700 S.bind2 (mfsg ctx sgn1,
|
adamc@76
|
701 fn sgn1' =>
|
adamc@76
|
702 S.bind2 (mfsg ctx sgn2,
|
adamc@76
|
703 fn sgn2' =>
|
adamc@76
|
704 S.map2 (mfst ctx str,
|
adamc@76
|
705 fn str' =>
|
adamc@76
|
706 (StrFun (x, n, sgn1', sgn2', str'), loc))))
|
adamc@76
|
707 | StrApp (str1, str2) =>
|
adamc@76
|
708 S.bind2 (mfst ctx str1,
|
adamc@76
|
709 fn str1' =>
|
adamc@76
|
710 S.map2 (mfst ctx str2,
|
adamc@76
|
711 fn str2' =>
|
adamc@76
|
712 (StrApp (str1', str2'), loc)))
|
adamc@76
|
713 | StrError => S.return2 strAll
|
adamc@76
|
714
|
adamc@76
|
715 and mfd ctx d acc =
|
adamc@76
|
716 S.bindP (mfd' ctx d acc, fd ctx)
|
adamc@76
|
717
|
adamc@76
|
718 and mfd' ctx (dAll as (d, loc)) =
|
adamc@76
|
719 case d of
|
adamc@76
|
720 DCon (x, n, k, c) =>
|
adamc@76
|
721 S.bind2 (mfk k,
|
adamc@76
|
722 fn k' =>
|
adamc@76
|
723 S.map2 (mfc ctx c,
|
adamc@76
|
724 fn c' =>
|
adamc@76
|
725 (DCon (x, n, k', c'), loc)))
|
adamc@191
|
726 | DDatatype (x, n, xs, xncs) =>
|
adamc@156
|
727 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@156
|
728 case c of
|
adamc@156
|
729 NONE => S.return2 (x, n, c)
|
adamc@156
|
730 | SOME c =>
|
adamc@156
|
731 S.map2 (mfc ctx c,
|
adamc@156
|
732 fn c' => (x, n, SOME c'))) xncs,
|
adamc@156
|
733 fn xncs' =>
|
adamc@191
|
734 (DDatatype (x, n, xs, xncs'), loc))
|
adamc@191
|
735 | DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@162
|
736 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
737 case c of
|
adamc@162
|
738 NONE => S.return2 (x, n, c)
|
adamc@162
|
739 | SOME c =>
|
adamc@162
|
740 S.map2 (mfc ctx c,
|
adamc@162
|
741 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
742 fn xncs' =>
|
adamc@191
|
743 (DDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
|
adamc@123
|
744 | DVal vi =>
|
adamc@123
|
745 S.map2 (mfvi ctx vi,
|
adamc@123
|
746 fn vi' =>
|
adamc@123
|
747 (DVal vi', loc))
|
adamc@123
|
748 | DValRec vis =>
|
adamc@123
|
749 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@123
|
750 fn vis' =>
|
adamc@123
|
751 (DValRec vis', loc))
|
adamc@76
|
752 | DSgn (x, n, sgn) =>
|
adamc@76
|
753 S.map2 (mfsg ctx sgn,
|
adamc@76
|
754 fn sgn' =>
|
adamc@76
|
755 (DSgn (x, n, sgn'), loc))
|
adamc@76
|
756 | DStr (x, n, sgn, str) =>
|
adamc@76
|
757 S.bind2 (mfsg ctx sgn,
|
adamc@76
|
758 fn sgn' =>
|
adamc@76
|
759 S.map2 (mfst ctx str,
|
adamc@76
|
760 fn str' =>
|
adamc@76
|
761 (DStr (x, n, sgn', str'), loc)))
|
adamc@76
|
762 | DFfiStr (x, n, sgn) =>
|
adamc@76
|
763 S.map2 (mfsg ctx sgn,
|
adamc@76
|
764 fn sgn' =>
|
adamc@76
|
765 (DFfiStr (x, n, sgn'), loc))
|
adamc@88
|
766 | DConstraint (c1, c2) =>
|
adamc@88
|
767 S.bind2 (mfc ctx c1,
|
adamc@88
|
768 fn c1' =>
|
adamc@88
|
769 S.map2 (mfc ctx c2,
|
adamc@88
|
770 fn c2' =>
|
adamc@88
|
771 (DConstraint (c1', c2'), loc)))
|
adamc@109
|
772 | DExport (en, sgn, str) =>
|
adamc@109
|
773 S.bind2 (mfsg ctx sgn,
|
adamc@109
|
774 fn sgn' =>
|
adamc@109
|
775 S.map2 (mfst ctx str,
|
adamc@109
|
776 fn str' =>
|
adamc@109
|
777 (DExport (en, sgn', str'), loc)))
|
adamc@123
|
778
|
adamc@205
|
779 | DTable (tn, x, n, c) =>
|
adamc@203
|
780 S.map2 (mfc ctx c,
|
adamc@203
|
781 fn c' =>
|
adamc@205
|
782 (DTable (tn, x, n, c'), loc))
|
adamc@338
|
783 | DSequence _ => S.return2 dAll
|
adamc@203
|
784
|
adamc@338
|
785 | DClass (x, n, c) =>
|
adamc@213
|
786 S.map2 (mfc ctx c,
|
adamc@338
|
787 fn c' =>
|
adamc@338
|
788 (DClass (x, n, c'), loc))
|
adamc@213
|
789
|
adamc@338
|
790 | DDatabase _ => S.return2 dAll
|
adamc@271
|
791
|
adamc@123
|
792 and mfvi ctx (x, n, c, e) =
|
adamc@123
|
793 S.bind2 (mfc ctx c,
|
adamc@123
|
794 fn c' =>
|
adamc@123
|
795 S.map2 (mfe ctx e,
|
adamc@123
|
796 fn e' =>
|
adamc@123
|
797 (x, n, c', e')))
|
adamc@76
|
798 in
|
adamc@76
|
799 mfd
|
adamc@76
|
800 end
|
adamc@76
|
801
|
adamc@76
|
802 fun mapfold {kind, con, exp, sgn_item, sgn, str, decl} =
|
adamc@76
|
803 mapfoldB {kind = kind,
|
adamc@76
|
804 con = fn () => con,
|
adamc@76
|
805 exp = fn () => exp,
|
adamc@76
|
806 sgn_item = fn () => sgn_item,
|
adamc@76
|
807 sgn = fn () => sgn,
|
adamc@76
|
808 str = fn () => str,
|
adamc@76
|
809 decl = fn () => decl,
|
adamc@76
|
810 bind = fn ((), _) => ()} ()
|
adamc@76
|
811
|
adamc@76
|
812 fun exists {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
813 case mapfold {kind = fn k => fn () =>
|
adamc@76
|
814 if kind k then
|
adamc@76
|
815 S.Return ()
|
adamc@76
|
816 else
|
adamc@76
|
817 S.Continue (k, ()),
|
adamc@76
|
818 con = fn c => fn () =>
|
adamc@76
|
819 if con c then
|
adamc@76
|
820 S.Return ()
|
adamc@76
|
821 else
|
adamc@76
|
822 S.Continue (c, ()),
|
adamc@76
|
823 exp = fn e => fn () =>
|
adamc@76
|
824 if exp e then
|
adamc@76
|
825 S.Return ()
|
adamc@76
|
826 else
|
adamc@76
|
827 S.Continue (e, ()),
|
adamc@76
|
828 sgn_item = fn sgi => fn () =>
|
adamc@76
|
829 if sgn_item sgi then
|
adamc@76
|
830 S.Return ()
|
adamc@76
|
831 else
|
adamc@76
|
832 S.Continue (sgi, ()),
|
adamc@76
|
833 sgn = fn x => fn () =>
|
adamc@76
|
834 if sgn x then
|
adamc@76
|
835 S.Return ()
|
adamc@76
|
836 else
|
adamc@76
|
837 S.Continue (x, ()),
|
adamc@76
|
838 str = fn x => fn () =>
|
adamc@76
|
839 if str x then
|
adamc@76
|
840 S.Return ()
|
adamc@76
|
841 else
|
adamc@76
|
842 S.Continue (x, ()),
|
adamc@76
|
843 decl = fn x => fn () =>
|
adamc@76
|
844 if decl x then
|
adamc@76
|
845 S.Return ()
|
adamc@76
|
846 else
|
adamc@76
|
847 S.Continue (x, ())} k () of
|
adamc@76
|
848 S.Return _ => true
|
adamc@76
|
849 | S.Continue _ => false
|
adamc@76
|
850
|
adamc@76
|
851 fun search {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
852 case mapfold {kind = fn x => fn () =>
|
adamc@76
|
853 case kind x of
|
adamc@76
|
854 NONE => S.Continue (x, ())
|
adamc@76
|
855 | SOME v => S.Return v,
|
adamc@76
|
856
|
adamc@76
|
857 con = fn x => fn () =>
|
adamc@76
|
858 case con x of
|
adamc@76
|
859 NONE => S.Continue (x, ())
|
adamc@76
|
860 | SOME v => S.Return v,
|
adamc@76
|
861
|
adamc@76
|
862 exp = fn x => fn () =>
|
adamc@76
|
863 case exp x of
|
adamc@76
|
864 NONE => S.Continue (x, ())
|
adamc@76
|
865 | SOME v => S.Return v,
|
adamc@76
|
866
|
adamc@76
|
867 sgn_item = fn x => fn () =>
|
adamc@76
|
868 case sgn_item x of
|
adamc@76
|
869 NONE => S.Continue (x, ())
|
adamc@76
|
870 | SOME v => S.Return v,
|
adamc@76
|
871
|
adamc@76
|
872 sgn = fn x => fn () =>
|
adamc@76
|
873 case sgn x of
|
adamc@76
|
874 NONE => S.Continue (x, ())
|
adamc@76
|
875 | SOME v => S.Return v,
|
adamc@76
|
876
|
adamc@76
|
877 str = fn x => fn () =>
|
adamc@76
|
878 case str x of
|
adamc@76
|
879 NONE => S.Continue (x, ())
|
adamc@76
|
880 | SOME v => S.Return v,
|
adamc@76
|
881
|
adamc@76
|
882 decl = fn x => fn () =>
|
adamc@76
|
883 case decl x of
|
adamc@76
|
884 NONE => S.Continue (x, ())
|
adamc@76
|
885 | SOME v => S.Return v
|
adamc@76
|
886
|
adamc@76
|
887 } k () of
|
adamc@76
|
888 S.Return x => SOME x
|
adamc@76
|
889 | S.Continue _ => NONE
|
adamc@76
|
890
|
adamc@34
|
891 end
|
adamc@76
|
892
|
adamc@76
|
893 end
|