adam@1303
|
1 (* Copyright (c) 2008-2010, 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
|
adam@1639
|
78 | KUnif (_, _, ref (KKnown k)) => mfk' ctx k
|
adamc@2
|
79 | KUnif _ => S.return2 kAll
|
adamc@623
|
80
|
adam@1639
|
81 | KTupleUnif (_, _, ref (KKnown k)) => mfk' ctx k
|
adam@1302
|
82 | KTupleUnif (loc, nks, r) =>
|
adam@1302
|
83 S.map2 (ListUtil.mapfold (fn (n, k) =>
|
adam@1302
|
84 S.map2 (mfk ctx k,
|
adam@1302
|
85 fn k' =>
|
adam@1302
|
86 (n, k'))) nks,
|
adam@1302
|
87 fn nks' =>
|
adam@1302
|
88 (KTupleUnif (loc, nks', r), loc))
|
adam@1302
|
89
|
adam@1302
|
90
|
adamc@623
|
91 | KRel _ => S.return2 kAll
|
adamc@623
|
92 | KFun (x, k) =>
|
adamc@623
|
93 S.map2 (mfk (bind (ctx, x)) k,
|
adamc@623
|
94 fn k' =>
|
adamc@623
|
95 (KFun (x, k'), loc))
|
adamc@2
|
96 in
|
adamc@2
|
97 mfk
|
adamc@2
|
98 end
|
adamc@2
|
99
|
adamc@623
|
100 fun mapfold fk =
|
adamc@623
|
101 mapfoldB {kind = fn () => fk,
|
adamc@623
|
102 bind = fn ((), _) => ()} ()
|
adamc@623
|
103
|
adamc@623
|
104 fun mapB {kind, bind} ctx k =
|
adamc@623
|
105 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
|
adamc@623
|
106 bind = bind} ctx k () of
|
adamc@623
|
107 S.Continue (k, ()) => k
|
adamc@623
|
108 | S.Return _ => raise Fail "ElabUtil.Kind.mapB: Impossible"
|
adamc@623
|
109
|
adamc@2
|
110 fun exists f k =
|
adamc@6
|
111 case mapfold (fn k => fn () =>
|
adamc@6
|
112 if f k then
|
adamc@6
|
113 S.Return ()
|
adamc@6
|
114 else
|
adamc@6
|
115 S.Continue (k, ())) k () of
|
adamc@6
|
116 S.Return _ => true
|
adamc@6
|
117 | S.Continue _ => false
|
adamc@6
|
118
|
adam@2201
|
119 fun foldB {kind, bind} ctx st k =
|
adam@2201
|
120 case mapfoldB {kind = fn ctx => fn k => fn st => S.Continue (k, kind (ctx, k, st)),
|
adam@2201
|
121 bind = bind} ctx k st of
|
adam@2201
|
122 S.Continue (_, st) => st
|
adam@2201
|
123 | S.Return _ => raise Fail "ElabUtil.Kind.foldB: Impossible"
|
adam@2201
|
124
|
adamc@6
|
125 end
|
adamc@6
|
126
|
adam@1303
|
127 val mliftConInCon = ref (fn n : int => fn c : con => (raise Fail "You didn't set ElabUtil.mliftConInCon!") : con)
|
adam@1303
|
128
|
adamc@6
|
129 structure Con = struct
|
adamc@6
|
130
|
adamc@11
|
131 datatype binder =
|
adamc@623
|
132 RelK of string
|
adamc@623
|
133 | RelC of string * Elab.kind
|
adamc@792
|
134 | NamedC of string * int * Elab.kind * Elab.con option
|
adamc@11
|
135
|
adamc@11
|
136 fun mapfoldB {kind = fk, con = fc, bind} =
|
adamc@6
|
137 let
|
adamc@623
|
138 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, s) => bind (ctx, RelK s)}
|
adamc@6
|
139
|
adamc@11
|
140 fun mfc ctx c acc =
|
adamc@11
|
141 S.bindP (mfc' ctx c acc, fc ctx)
|
adamc@6
|
142
|
adamc@11
|
143 and mfc' ctx (cAll as (c, loc)) =
|
adamc@6
|
144 case c of
|
adamc@6
|
145 TFun (c1, c2) =>
|
adamc@11
|
146 S.bind2 (mfc ctx c1,
|
adamc@6
|
147 fn c1' =>
|
adamc@11
|
148 S.map2 (mfc ctx c2,
|
adamc@6
|
149 fn c2' =>
|
adamc@6
|
150 (TFun (c1', c2'), loc)))
|
adamc@6
|
151 | TCFun (e, x, k, c) =>
|
adamc@623
|
152 S.bind2 (mfk ctx k,
|
adamc@6
|
153 fn k' =>
|
adamc@623
|
154 S.map2 (mfc (bind (ctx, RelC (x, k))) c,
|
adamc@6
|
155 fn c' =>
|
adamc@6
|
156 (TCFun (e, x, k', c'), loc)))
|
adamc@628
|
157 | TDisjoint (c1, c2, c3) =>
|
adamc@85
|
158 S.bind2 (mfc ctx c1,
|
adamc@85
|
159 fn c1' =>
|
adamc@85
|
160 S.bind2 (mfc ctx c2,
|
adamc@85
|
161 fn c2' =>
|
adamc@85
|
162 S.map2 (mfc ctx c3,
|
adamc@85
|
163 fn c3' =>
|
adamc@628
|
164 (TDisjoint (c1', c2', c3'), loc))))
|
adamc@6
|
165 | TRecord c =>
|
adamc@11
|
166 S.map2 (mfc ctx c,
|
adamc@6
|
167 fn c' =>
|
adamc@6
|
168 (TRecord c', loc))
|
adamc@6
|
169
|
adamc@6
|
170 | CRel _ => S.return2 cAll
|
adamc@6
|
171 | CNamed _ => S.return2 cAll
|
adamc@34
|
172 | CModProj _ => S.return2 cAll
|
adamc@6
|
173 | CApp (c1, c2) =>
|
adamc@11
|
174 S.bind2 (mfc ctx c1,
|
adamc@6
|
175 fn c1' =>
|
adamc@11
|
176 S.map2 (mfc ctx c2,
|
adamc@6
|
177 fn c2' =>
|
adamc@6
|
178 (CApp (c1', c2'), loc)))
|
adamc@8
|
179 | CAbs (x, k, c) =>
|
adamc@623
|
180 S.bind2 (mfk ctx k,
|
adamc@6
|
181 fn k' =>
|
adamc@623
|
182 S.map2 (mfc (bind (ctx, RelC (x, k))) c,
|
adamc@6
|
183 fn c' =>
|
adamc@8
|
184 (CAbs (x, k', c'), loc)))
|
adamc@6
|
185
|
adamc@6
|
186 | CName _ => S.return2 cAll
|
adamc@6
|
187
|
adamc@6
|
188 | CRecord (k, xcs) =>
|
adamc@623
|
189 S.bind2 (mfk ctx k,
|
adamc@6
|
190 fn k' =>
|
adamc@6
|
191 S.map2 (ListUtil.mapfold (fn (x, c) =>
|
adamc@11
|
192 S.bind2 (mfc ctx x,
|
adamc@6
|
193 fn x' =>
|
adamc@11
|
194 S.map2 (mfc ctx c,
|
adamc@6
|
195 fn c' =>
|
adamc@6
|
196 (x', c'))))
|
adamc@6
|
197 xcs,
|
adamc@6
|
198 fn xcs' =>
|
adamc@6
|
199 (CRecord (k', xcs'), loc)))
|
adamc@6
|
200 | CConcat (c1, c2) =>
|
adamc@11
|
201 S.bind2 (mfc ctx c1,
|
adamc@6
|
202 fn c1' =>
|
adamc@11
|
203 S.map2 (mfc ctx c2,
|
adamc@6
|
204 fn c2' =>
|
adamc@6
|
205 (CConcat (c1', c2'), loc)))
|
adamc@621
|
206 | CMap (k1, k2) =>
|
adamc@623
|
207 S.bind2 (mfk ctx k1,
|
adamc@67
|
208 fn k1' =>
|
adamc@623
|
209 S.map2 (mfk ctx k2,
|
adamc@67
|
210 fn k2' =>
|
adamc@621
|
211 (CMap (k1', k2'), loc)))
|
adamc@6
|
212
|
adamc@82
|
213 | CUnit => S.return2 cAll
|
adamc@82
|
214
|
adamc@207
|
215 | CTuple cs =>
|
adamc@207
|
216 S.map2 (ListUtil.mapfold (mfc ctx) cs,
|
adamc@207
|
217 fn cs' =>
|
adamc@207
|
218 (CTuple cs', loc))
|
adamc@207
|
219
|
adamc@207
|
220 | CProj (c, n) =>
|
adamc@207
|
221 S.map2 (mfc ctx c,
|
adamc@207
|
222 fn c' =>
|
adamc@207
|
223 (CProj (c', n), loc))
|
adamc@207
|
224
|
adamc@6
|
225 | CError => S.return2 cAll
|
adam@1639
|
226 | CUnif (nl, _, _, _, ref (Known c)) => mfc' ctx (!mliftConInCon nl c)
|
adamc@6
|
227 | CUnif _ => S.return2 cAll
|
adam@1302
|
228
|
adamc@623
|
229 | CKAbs (x, c) =>
|
adamc@623
|
230 S.map2 (mfc (bind (ctx, RelK x)) c,
|
adamc@623
|
231 fn c' =>
|
adamc@623
|
232 (CKAbs (x, c'), loc))
|
adamc@623
|
233 | CKApp (c, k) =>
|
adamc@623
|
234 S.bind2 (mfc ctx c,
|
adamc@623
|
235 fn c' =>
|
adamc@623
|
236 S.map2 (mfk ctx k,
|
adamc@623
|
237 fn k' =>
|
adamc@623
|
238 (CKApp (c', k'), loc)))
|
adamc@623
|
239 | TKFun (x, c) =>
|
adamc@623
|
240 S.map2 (mfc (bind (ctx, RelK x)) c,
|
adamc@623
|
241 fn c' =>
|
adamc@623
|
242 (TKFun (x, c'), loc))
|
adamc@6
|
243 in
|
adamc@6
|
244 mfc
|
adamc@6
|
245 end
|
adamc@6
|
246
|
adamc@11
|
247 fun mapfold {kind = fk, con = fc} =
|
adamc@623
|
248 mapfoldB {kind = fn () => fk,
|
adamc@11
|
249 con = fn () => fc,
|
adamc@11
|
250 bind = fn ((), _) => ()} ()
|
adamc@11
|
251
|
adamc@11
|
252 fun mapB {kind, con, bind} ctx c =
|
adamc@623
|
253 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
|
adamc@11
|
254 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@11
|
255 bind = bind} ctx c () of
|
adamc@11
|
256 S.Continue (c, ()) => c
|
adamc@34
|
257 | S.Return _ => raise Fail "ElabUtil.Con.mapB: Impossible"
|
adamc@34
|
258
|
adamc@34
|
259 fun map {kind, con} s =
|
adamc@34
|
260 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@34
|
261 con = fn c => fn () => S.Continue (con c, ())} s () of
|
adamc@34
|
262 S.Return () => raise Fail "ElabUtil.Con.map: Impossible"
|
adamc@34
|
263 | S.Continue (s, ()) => s
|
adamc@11
|
264
|
adam@1639
|
265 fun appB {kind, con, bind} ctx c =
|
adam@1639
|
266 case mapfoldB {kind = fn ctx => fn k => fn () => (kind ctx k; S.Continue (k, ())),
|
adam@1639
|
267 con = fn ctx => fn c => fn () => (con ctx c; S.Continue (c, ())),
|
adam@1639
|
268 bind = bind} ctx c () of
|
adam@1639
|
269 S.Continue _ => ()
|
adam@1639
|
270 | S.Return _ => raise Fail "ElabUtil.Con.appB: Impossible"
|
adam@1639
|
271
|
adam@1639
|
272 fun app {kind, con} s =
|
adam@1639
|
273 case mapfold {kind = fn k => fn () => (kind k; S.Continue (k, ())),
|
adam@1639
|
274 con = fn c => fn () => (con c; S.Continue (c, ()))} s () of
|
adam@1639
|
275 S.Return () => raise Fail "ElabUtil.Con.app: Impossible"
|
adam@1639
|
276 | S.Continue _ => ()
|
adam@1639
|
277
|
adamc@711
|
278 fun existsB {kind, con, bind} ctx c =
|
adamc@711
|
279 case mapfoldB {kind = fn ctx => fn k => fn () =>
|
adamc@711
|
280 if kind (ctx, k) then
|
adamc@711
|
281 S.Return ()
|
adamc@711
|
282 else
|
adamc@711
|
283 S.Continue (k, ()),
|
adamc@711
|
284 con = fn ctx => fn c => fn () =>
|
adamc@711
|
285 if con (ctx, c) then
|
adamc@711
|
286 S.Return ()
|
adamc@711
|
287 else
|
adamc@711
|
288 S.Continue (c, ()),
|
adamc@711
|
289 bind = bind} ctx c () of
|
adamc@711
|
290 S.Return _ => true
|
adamc@711
|
291 | S.Continue _ => false
|
adamc@711
|
292
|
adamc@711
|
293 fun exists {kind, con} c =
|
adamc@6
|
294 case mapfold {kind = fn k => fn () =>
|
adamc@6
|
295 if kind k then
|
adamc@6
|
296 S.Return ()
|
adamc@6
|
297 else
|
adamc@6
|
298 S.Continue (k, ()),
|
adamc@6
|
299 con = fn c => fn () =>
|
adamc@6
|
300 if con c then
|
adamc@6
|
301 S.Return ()
|
adamc@6
|
302 else
|
adamc@711
|
303 S.Continue (c, ())} c () of
|
adamc@2
|
304 S.Return _ => true
|
adamc@2
|
305 | S.Continue _ => false
|
adamc@2
|
306
|
adamc@448
|
307 fun foldB {kind, con, bind} ctx st c =
|
adamc@623
|
308 case mapfoldB {kind = fn ctx => fn k => fn st => S.Continue (k, kind (ctx, k, st)),
|
adamc@448
|
309 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
|
adamc@448
|
310 bind = bind} ctx c st of
|
adamc@448
|
311 S.Continue (_, st) => st
|
adamc@448
|
312 | S.Return _ => raise Fail "ElabUtil.Con.foldB: Impossible"
|
adamc@448
|
313
|
adamc@839
|
314 fun fold {kind, con} st c =
|
adamc@839
|
315 case mapfoldB {kind = fn () => fn k => fn st => S.Continue (k, kind (k, st)),
|
adamc@839
|
316 con = fn () => fn c => fn st => S.Continue (c, con (c, st)),
|
adamc@839
|
317 bind = fn ((), _) => ()} () c st of
|
adamc@839
|
318 S.Continue (_, st) => st
|
adamc@839
|
319 | S.Return _ => raise Fail "ElabUtil.Con.fold: Impossible"
|
adamc@839
|
320
|
adamc@2
|
321 end
|
adamc@2
|
322
|
adamc@10
|
323 structure Exp = struct
|
adamc@10
|
324
|
adamc@11
|
325 datatype binder =
|
adamc@623
|
326 RelK of string
|
adamc@623
|
327 | RelC of string * Elab.kind
|
adamc@792
|
328 | NamedC of string * int * Elab.kind * Elab.con option
|
adamc@11
|
329 | RelE of string * Elab.con
|
adamc@11
|
330 | NamedE of string * Elab.con
|
adamc@11
|
331
|
adamc@11
|
332 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
|
adamc@10
|
333 let
|
adamc@623
|
334 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
|
adamc@10
|
335
|
adamc@11
|
336 fun bind' (ctx, b) =
|
adamc@11
|
337 let
|
adamc@11
|
338 val b' = case b of
|
adamc@623
|
339 Con.RelK x => RelK x
|
adamc@623
|
340 | Con.RelC x => RelC x
|
adamc@623
|
341 | Con.NamedC x => NamedC x
|
adamc@11
|
342 in
|
adamc@11
|
343 bind (ctx, b')
|
adamc@11
|
344 end
|
adamc@11
|
345 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@10
|
346
|
adamc@825
|
347 fun doVars ((p, _), ctx) =
|
adamc@825
|
348 case p of
|
adamc@825
|
349 PWild => ctx
|
adamc@825
|
350 | PVar xt => bind (ctx, RelE xt)
|
adamc@825
|
351 | PPrim _ => ctx
|
adamc@825
|
352 | PCon (_, _, _, NONE) => ctx
|
adamc@825
|
353 | PCon (_, _, _, SOME p) => doVars (p, ctx)
|
adamc@825
|
354 | PRecord xpcs =>
|
adamc@825
|
355 foldl (fn ((_, p, _), ctx) => doVars (p, ctx))
|
adamc@825
|
356 ctx xpcs
|
adamc@825
|
357
|
adamc@11
|
358 fun mfe ctx e acc =
|
adamc@11
|
359 S.bindP (mfe' ctx e acc, fe ctx)
|
adamc@11
|
360
|
adamc@11
|
361 and mfe' ctx (eAll as (e, loc)) =
|
adamc@10
|
362 case e of
|
adamc@14
|
363 EPrim _ => S.return2 eAll
|
adamc@14
|
364 | ERel _ => S.return2 eAll
|
adamc@10
|
365 | ENamed _ => S.return2 eAll
|
adamc@34
|
366 | EModProj _ => S.return2 eAll
|
adamc@10
|
367 | EApp (e1, e2) =>
|
adamc@11
|
368 S.bind2 (mfe ctx e1,
|
adamc@10
|
369 fn e1' =>
|
adamc@11
|
370 S.map2 (mfe ctx e2,
|
adamc@10
|
371 fn e2' =>
|
adamc@10
|
372 (EApp (e1', e2'), loc)))
|
adamc@26
|
373 | EAbs (x, dom, ran, e) =>
|
adamc@26
|
374 S.bind2 (mfc ctx dom,
|
adamc@26
|
375 fn dom' =>
|
adamc@26
|
376 S.bind2 (mfc ctx ran,
|
adamc@26
|
377 fn ran' =>
|
adamc@26
|
378 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
|
adamc@26
|
379 fn e' =>
|
adamc@26
|
380 (EAbs (x, dom', ran', e'), loc))))
|
adamc@26
|
381
|
adamc@10
|
382 | ECApp (e, c) =>
|
adamc@11
|
383 S.bind2 (mfe ctx e,
|
adamc@10
|
384 fn e' =>
|
adamc@11
|
385 S.map2 (mfc ctx c,
|
adamc@10
|
386 fn c' =>
|
adamc@10
|
387 (ECApp (e', c'), loc)))
|
adamc@10
|
388 | ECAbs (expl, x, k, e) =>
|
adamc@623
|
389 S.bind2 (mfk ctx k,
|
adamc@10
|
390 fn k' =>
|
adamc@11
|
391 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
|
adamc@10
|
392 fn e' =>
|
adamc@10
|
393 (ECAbs (expl, x, k', e'), loc)))
|
adamc@10
|
394
|
adamc@12
|
395 | ERecord xes =>
|
adamc@29
|
396 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
|
adamc@12
|
397 S.bind2 (mfc ctx x,
|
adamc@12
|
398 fn x' =>
|
adamc@29
|
399 S.bind2 (mfe ctx e,
|
adamc@12
|
400 fn e' =>
|
adamc@29
|
401 S.map2 (mfc ctx t,
|
adamc@29
|
402 fn t' =>
|
adamc@29
|
403 (x', e', t')))))
|
adamc@12
|
404 xes,
|
adamc@12
|
405 fn xes' =>
|
adamc@12
|
406 (ERecord xes', loc))
|
adamc@12
|
407 | EField (e, c, {field, rest}) =>
|
adamc@12
|
408 S.bind2 (mfe ctx e,
|
adamc@12
|
409 fn e' =>
|
adamc@12
|
410 S.bind2 (mfc ctx c,
|
adamc@12
|
411 fn c' =>
|
adamc@12
|
412 S.bind2 (mfc ctx field,
|
adamc@12
|
413 fn field' =>
|
adamc@12
|
414 S.map2 (mfc ctx rest,
|
adamc@12
|
415 fn rest' =>
|
adamc@12
|
416 (EField (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@445
|
417 | EConcat (e1, c1, e2, c2) =>
|
adamc@339
|
418 S.bind2 (mfe ctx e1,
|
adamc@339
|
419 fn e1' =>
|
adamc@445
|
420 S.bind2 (mfc ctx c1,
|
adamc@445
|
421 fn c1' =>
|
adamc@339
|
422 S.bind2 (mfe ctx e2,
|
adamc@339
|
423 fn e2' =>
|
adamc@445
|
424 S.map2 (mfc ctx c2,
|
adamc@445
|
425 fn c2' =>
|
adamc@445
|
426 (EConcat (e1', c1', e2', c2'),
|
adamc@445
|
427 loc)))))
|
adamc@149
|
428 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
429 S.bind2 (mfe ctx e,
|
adamc@149
|
430 fn e' =>
|
adamc@149
|
431 S.bind2 (mfc ctx c,
|
adamc@149
|
432 fn c' =>
|
adamc@149
|
433 S.bind2 (mfc ctx field,
|
adamc@149
|
434 fn field' =>
|
adamc@149
|
435 S.map2 (mfc ctx rest,
|
adamc@149
|
436 fn rest' =>
|
adamc@149
|
437 (ECut (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@12
|
438
|
adamc@493
|
439 | ECutMulti (e, c, {rest}) =>
|
adamc@493
|
440 S.bind2 (mfe ctx e,
|
adamc@493
|
441 fn e' =>
|
adamc@493
|
442 S.bind2 (mfc ctx c,
|
adamc@493
|
443 fn c' =>
|
adamc@493
|
444 S.map2 (mfc ctx rest,
|
adamc@493
|
445 fn rest' =>
|
adamc@493
|
446 (ECutMulti (e', c', {rest = rest'}), loc))))
|
adamc@493
|
447
|
adamc@182
|
448 | ECase (e, pes, {disc, result}) =>
|
adamc@171
|
449 S.bind2 (mfe ctx e,
|
adamc@171
|
450 fn e' =>
|
adamc@171
|
451 S.bind2 (ListUtil.mapfold (fn (p, e) =>
|
adamc@448
|
452 let
|
adamc@448
|
453 fun pb ((p, _), ctx) =
|
adamc@448
|
454 case p of
|
adamc@448
|
455 PWild => ctx
|
adamc@448
|
456 | PVar (x, t) => bind (ctx, RelE (x, t))
|
adamc@448
|
457 | PPrim _ => ctx
|
adamc@448
|
458 | PCon (_, _, _, NONE) => ctx
|
adamc@448
|
459 | PCon (_, _, _, SOME p) => pb (p, ctx)
|
adamc@448
|
460 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
|
adamc@448
|
461 pb (p, ctx)) ctx xps
|
adamc@448
|
462 in
|
adamc@1272
|
463 S.bind2 (mfp ctx p,
|
adamc@1272
|
464 fn p' =>
|
adamc@1272
|
465 S.map2 (mfe (pb (p', ctx)) e,
|
adamc@1272
|
466 fn e' => (p', e')))
|
adamc@448
|
467 end) pes,
|
adamc@171
|
468 fn pes' =>
|
adamc@182
|
469 S.bind2 (mfc ctx disc,
|
adamc@182
|
470 fn disc' =>
|
adamc@182
|
471 S.map2 (mfc ctx result,
|
adamc@182
|
472 fn result' =>
|
adamc@182
|
473 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
|
adamc@171
|
474
|
adamc@10
|
475 | EError => S.return2 eAll
|
adamc@228
|
476 | EUnif (ref (SOME e)) => mfe ctx e
|
adamc@228
|
477 | EUnif _ => S.return2 eAll
|
adamc@447
|
478
|
adamc@825
|
479 | ELet (des, e, t) =>
|
adamc@447
|
480 let
|
adamc@826
|
481 val (des, ctx') = foldl (fn (ed, (des, ctx)) =>
|
adamc@826
|
482 let
|
adamc@826
|
483 val ctx' =
|
adamc@826
|
484 case #1 ed of
|
adamc@826
|
485 EDVal (p, _, _) => doVars (p, ctx)
|
adamc@826
|
486 | EDValRec vis =>
|
adamc@826
|
487 foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t)))
|
adamc@826
|
488 ctx vis
|
adamc@826
|
489 in
|
adamc@826
|
490 (S.bind2 (des,
|
adamc@826
|
491 fn des' =>
|
adamc@826
|
492 S.map2 (mfed ctx ed,
|
adamc@826
|
493 fn ed' => ed' :: des')),
|
adamc@826
|
494 ctx')
|
adamc@826
|
495 end)
|
adamc@447
|
496 (S.return2 [], ctx) des
|
adamc@447
|
497 in
|
adamc@447
|
498 S.bind2 (des,
|
adamc@447
|
499 fn des' =>
|
adamc@826
|
500 S.bind2 (mfe ctx' e,
|
adamc@447
|
501 fn e' =>
|
adamc@825
|
502 S.map2 (mfc ctx t,
|
adamc@825
|
503 fn t' =>
|
adamc@826
|
504 (ELet (rev des', e', t'), loc))))
|
adamc@447
|
505 end
|
adamc@447
|
506
|
adamc@623
|
507 | EKAbs (x, e) =>
|
adamc@623
|
508 S.map2 (mfe (bind (ctx, RelK x)) e,
|
adamc@623
|
509 fn e' =>
|
adamc@623
|
510 (EKAbs (x, e'), loc))
|
adamc@623
|
511 | EKApp (e, k) =>
|
adamc@623
|
512 S.bind2 (mfe ctx e,
|
adamc@623
|
513 fn e' =>
|
adamc@623
|
514 S.map2 (mfk ctx k,
|
adamc@623
|
515 fn k' =>
|
adamc@623
|
516 (EKApp (e', k'), loc)))
|
adamc@623
|
517
|
adamc@1272
|
518 and mfp ctx (pAll as (p, loc)) =
|
adamc@1272
|
519 case p of
|
adamc@1272
|
520 PWild => S.return2 pAll
|
adamc@1272
|
521 | PVar (x, t) =>
|
adamc@1272
|
522 S.map2 (mfc ctx t,
|
adamc@1272
|
523 fn t' =>
|
adamc@1272
|
524 (PVar (x, t'), loc))
|
adamc@1272
|
525 | PPrim _ => S.return2 pAll
|
adamc@1272
|
526 | PCon (dk, pc, args, po) =>
|
adamc@1272
|
527 S.bind2 (ListUtil.mapfold (mfc ctx) args,
|
adamc@1272
|
528 fn args' =>
|
adamc@1272
|
529 S.map2 ((case po of
|
adamc@1272
|
530 NONE => S.return2 NONE
|
adamc@1272
|
531 | SOME p => S.map2 (mfp ctx p, SOME)),
|
adamc@1272
|
532 fn po' =>
|
adamc@1272
|
533 (PCon (dk, pc, args', po'), loc)))
|
adamc@1272
|
534 | PRecord xps =>
|
adamc@1272
|
535 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
|
adamc@1272
|
536 S.bind2 (mfp ctx p,
|
adamc@1272
|
537 fn p' =>
|
adamc@1272
|
538 S.map2 (mfc ctx c,
|
adamc@1272
|
539 fn c' =>
|
adamc@1272
|
540 (x, p', c')))) xps,
|
adamc@1272
|
541 fn xps' =>
|
adamc@1272
|
542 (PRecord xps', loc))
|
adamc@1272
|
543
|
adamc@447
|
544 and mfed ctx (dAll as (d, loc)) =
|
adamc@447
|
545 case d of
|
adamc@825
|
546 EDVal (p, t, e) =>
|
adamc@825
|
547 S.bind2 (mfc ctx t,
|
adamc@825
|
548 fn t' =>
|
adamc@826
|
549 S.map2 (mfe ctx e,
|
adamc@825
|
550 fn e' =>
|
adamc@825
|
551 (EDVal (p, t', e'), loc)))
|
adamc@447
|
552 | EDValRec vis =>
|
adamc@447
|
553 let
|
adamc@453
|
554 val ctx = foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t))) ctx vis
|
adamc@447
|
555 in
|
adamc@447
|
556 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@447
|
557 fn vis' =>
|
adamc@447
|
558 (EDValRec vis', loc))
|
adamc@447
|
559 end
|
adamc@447
|
560
|
adamc@447
|
561 and mfvi ctx (x, c, e) =
|
adamc@447
|
562 S.bind2 (mfc ctx c,
|
adamc@447
|
563 fn c' =>
|
adamc@447
|
564 S.map2 (mfe ctx e,
|
adamc@447
|
565 fn e' =>
|
adamc@447
|
566 (x, c', e')))
|
adamc@10
|
567 in
|
adamc@10
|
568 mfe
|
adamc@10
|
569 end
|
adamc@10
|
570
|
adamc@11
|
571 fun mapfold {kind = fk, con = fc, exp = fe} =
|
adamc@623
|
572 mapfoldB {kind = fn () => fk,
|
adamc@11
|
573 con = fn () => fc,
|
adamc@11
|
574 exp = fn () => fe,
|
adamc@11
|
575 bind = fn ((), _) => ()} ()
|
adamc@11
|
576
|
adam@1795
|
577 fun existsB {kind, con, exp, bind} ctx e =
|
adam@1795
|
578 case mapfoldB {kind = fn ctx => fn k => fn () =>
|
adam@1795
|
579 if kind (ctx, k) then
|
adam@1795
|
580 S.Return ()
|
adam@1795
|
581 else
|
adam@1795
|
582 S.Continue (k, ()),
|
adam@1795
|
583 con = fn ctx => fn c => fn () =>
|
adam@1795
|
584 if con (ctx, c) then
|
adam@1795
|
585 S.Return ()
|
adam@1795
|
586 else
|
adam@1795
|
587 S.Continue (c, ()),
|
adam@1795
|
588 exp = fn ctx => fn e => fn () =>
|
adam@1795
|
589 if exp (ctx, e) then
|
adam@1795
|
590 S.Return ()
|
adam@1795
|
591 else
|
adam@1795
|
592 S.Continue (e, ()),
|
adam@1795
|
593 bind = bind} ctx e () of
|
adam@1795
|
594 S.Return _ => true
|
adam@1795
|
595 | S.Continue _ => false
|
adam@1795
|
596
|
adamc@10
|
597 fun exists {kind, con, exp} k =
|
adamc@10
|
598 case mapfold {kind = fn k => fn () =>
|
adamc@10
|
599 if kind k then
|
adamc@10
|
600 S.Return ()
|
adamc@10
|
601 else
|
adamc@10
|
602 S.Continue (k, ()),
|
adamc@10
|
603 con = fn c => fn () =>
|
adamc@10
|
604 if con c then
|
adamc@10
|
605 S.Return ()
|
adamc@10
|
606 else
|
adamc@10
|
607 S.Continue (c, ()),
|
adamc@10
|
608 exp = fn e => fn () =>
|
adamc@10
|
609 if exp e then
|
adamc@10
|
610 S.Return ()
|
adamc@10
|
611 else
|
adamc@10
|
612 S.Continue (e, ())} k () of
|
adamc@10
|
613 S.Return _ => true
|
adamc@10
|
614 | S.Continue _ => false
|
adamc@10
|
615
|
adamc@211
|
616 fun mapB {kind, con, exp, bind} ctx e =
|
adamc@623
|
617 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
|
adamc@211
|
618 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@211
|
619 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
|
adamc@211
|
620 bind = bind} ctx e () of
|
adamc@211
|
621 S.Continue (e, ()) => e
|
adamc@211
|
622 | S.Return _ => raise Fail "ElabUtil.Exp.mapB: Impossible"
|
adamc@211
|
623
|
adamc@448
|
624 fun foldB {kind, con, exp, bind} ctx st e =
|
adamc@623
|
625 case mapfoldB {kind = fn ctx => fn k => fn st => S.Continue (k, kind (ctx, k, st)),
|
adamc@448
|
626 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
|
adamc@448
|
627 exp = fn ctx => fn e => fn st => S.Continue (e, exp (ctx, e, st)),
|
adamc@448
|
628 bind = bind} ctx e st of
|
adamc@448
|
629 S.Continue (_, st) => st
|
adamc@448
|
630 | S.Return _ => raise Fail "ElabUtil.Exp.foldB: Impossible"
|
adamc@448
|
631
|
adamc@10
|
632 end
|
adamc@10
|
633
|
adamc@34
|
634 structure Sgn = struct
|
adamc@34
|
635
|
adamc@34
|
636 datatype binder =
|
adamc@623
|
637 RelK of string
|
adamc@623
|
638 | RelC of string * Elab.kind
|
adamc@792
|
639 | NamedC of string * int * Elab.kind * Elab.con option
|
adamc@792
|
640 | Str of string * int * Elab.sgn
|
adamc@792
|
641 | Sgn of string * int * Elab.sgn
|
adamc@34
|
642
|
adamc@34
|
643 fun mapfoldB {kind, con, sgn_item, sgn, bind} =
|
adamc@34
|
644 let
|
adamc@34
|
645 fun bind' (ctx, b) =
|
adamc@34
|
646 let
|
adamc@34
|
647 val b' = case b of
|
adamc@623
|
648 Con.RelK x => RelK x
|
adamc@623
|
649 | Con.RelC x => RelC x
|
adamc@623
|
650 | Con.NamedC x => NamedC x
|
adamc@34
|
651 in
|
adamc@34
|
652 bind (ctx, b')
|
adamc@34
|
653 end
|
adamc@34
|
654 val con = Con.mapfoldB {kind = kind, con = con, bind = bind'}
|
adamc@34
|
655
|
adamc@623
|
656 val kind = Kind.mapfoldB {kind = kind, bind = fn (ctx, x) => bind (ctx, RelK x)}
|
adamc@34
|
657
|
adamc@34
|
658 fun sgi ctx si acc =
|
adamc@34
|
659 S.bindP (sgi' ctx si acc, sgn_item ctx)
|
adamc@34
|
660
|
adamc@156
|
661 and sgi' ctx (siAll as (si, loc)) =
|
adamc@34
|
662 case si of
|
adamc@34
|
663 SgiConAbs (x, n, k) =>
|
adamc@623
|
664 S.map2 (kind ctx k,
|
adamc@34
|
665 fn k' =>
|
adamc@34
|
666 (SgiConAbs (x, n, k'), loc))
|
adamc@34
|
667 | SgiCon (x, n, k, c) =>
|
adamc@623
|
668 S.bind2 (kind ctx k,
|
adamc@34
|
669 fn k' =>
|
adamc@34
|
670 S.map2 (con ctx c,
|
adamc@34
|
671 fn c' =>
|
adamc@34
|
672 (SgiCon (x, n, k', c'), loc)))
|
adamc@805
|
673 | SgiDatatype dts =>
|
adamc@805
|
674 S.map2 (ListUtil.mapfold (fn (x, n, xs, xncs) =>
|
adamc@805
|
675 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@805
|
676 case c of
|
adamc@805
|
677 NONE => S.return2 (x, n, c)
|
adamc@805
|
678 | SOME c =>
|
adamc@805
|
679 S.map2 (con ctx c,
|
adamc@805
|
680 fn c' => (x, n, SOME c'))) xncs,
|
adamc@805
|
681 fn xncs' => (x, n, xs, xncs'))) dts,
|
adamc@805
|
682 fn dts' =>
|
adamc@805
|
683 (SgiDatatype dts', loc))
|
adamc@191
|
684 | SgiDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@162
|
685 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
686 case c of
|
adamc@162
|
687 NONE => S.return2 (x, n, c)
|
adamc@162
|
688 | SOME c =>
|
adamc@162
|
689 S.map2 (con ctx c,
|
adamc@162
|
690 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
691 fn xncs' =>
|
adamc@191
|
692 (SgiDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
|
adamc@34
|
693 | SgiVal (x, n, c) =>
|
adamc@34
|
694 S.map2 (con ctx c,
|
adamc@34
|
695 fn c' =>
|
adamc@34
|
696 (SgiVal (x, n, c'), loc))
|
adam@2190
|
697 | SgiStr (im, x, n, s) =>
|
adamc@34
|
698 S.map2 (sg ctx s,
|
adamc@34
|
699 fn s' =>
|
adam@2190
|
700 (SgiStr (im, x, n, s'), loc))
|
adamc@59
|
701 | SgiSgn (x, n, s) =>
|
adamc@59
|
702 S.map2 (sg ctx s,
|
adamc@59
|
703 fn s' =>
|
adamc@59
|
704 (SgiSgn (x, n, s'), loc))
|
adamc@88
|
705 | SgiConstraint (c1, c2) =>
|
adamc@88
|
706 S.bind2 (con ctx c1,
|
adamc@88
|
707 fn c1' =>
|
adamc@88
|
708 S.map2 (con ctx c2,
|
adamc@88
|
709 fn c2' =>
|
adamc@88
|
710 (SgiConstraint (c1', c2'), loc)))
|
adamc@563
|
711 | SgiClassAbs (x, n, k) =>
|
adamc@623
|
712 S.map2 (kind ctx k,
|
adamc@563
|
713 fn k' =>
|
adamc@563
|
714 (SgiClassAbs (x, n, k'), loc))
|
adamc@563
|
715 | SgiClass (x, n, k, c) =>
|
adamc@623
|
716 S.bind2 (kind ctx k,
|
adamc@563
|
717 fn k' =>
|
adamc@563
|
718 S.map2 (con ctx c,
|
adamc@563
|
719 fn c' =>
|
adamc@563
|
720 (SgiClass (x, n, k', c'), loc)))
|
adamc@34
|
721
|
adamc@34
|
722 and sg ctx s acc =
|
adamc@34
|
723 S.bindP (sg' ctx s acc, sgn ctx)
|
adamc@34
|
724
|
adamc@34
|
725 and sg' ctx (sAll as (s, loc)) =
|
adamc@34
|
726 case s of
|
adamc@34
|
727 SgnConst sgis =>
|
adamc@34
|
728 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
|
adamc@34
|
729 (case #1 si of
|
adamc@329
|
730 SgiConAbs (x, n, k) =>
|
adamc@792
|
731 bind (ctx, NamedC (x, n, k, NONE))
|
adamc@792
|
732 | SgiCon (x, n, k, c) =>
|
adamc@792
|
733 bind (ctx, NamedC (x, n, k, SOME c))
|
adamc@805
|
734 | SgiDatatype dts =>
|
adamc@805
|
735 foldl (fn ((x, n, ks, _), ctx) =>
|
adamc@805
|
736 let
|
adamc@805
|
737 val k' = (KType, loc)
|
adamc@805
|
738 val k = foldl (fn (_, k) => (KArrow (k', k), loc))
|
adamc@805
|
739 k' ks
|
adamc@805
|
740 in
|
adamc@805
|
741 bind (ctx, NamedC (x, n, k, NONE))
|
adamc@805
|
742 end) ctx dts
|
adamc@792
|
743 | SgiDatatypeImp (x, n, m1, ms, s, _, _) =>
|
adamc@792
|
744 bind (ctx, NamedC (x, n, (KType, loc),
|
adamc@792
|
745 SOME (CModProj (m1, ms, s), loc)))
|
adamc@34
|
746 | SgiVal _ => ctx
|
adam@2190
|
747 | SgiStr (_, x, n, sgn) =>
|
adamc@792
|
748 bind (ctx, Str (x, n, sgn))
|
adamc@792
|
749 | SgiSgn (x, n, sgn) =>
|
adamc@792
|
750 bind (ctx, Sgn (x, n, sgn))
|
adamc@203
|
751 | SgiConstraint _ => ctx
|
adamc@563
|
752 | SgiClassAbs (x, n, k) =>
|
adamc@792
|
753 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), NONE))
|
adamc@792
|
754 | SgiClass (x, n, k, c) =>
|
adamc@792
|
755 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), SOME c)),
|
adamc@34
|
756 sgi ctx si)) ctx sgis,
|
adamc@34
|
757 fn sgis' =>
|
adamc@34
|
758 (SgnConst sgis', loc))
|
adamc@34
|
759
|
adamc@34
|
760 | SgnVar _ => S.return2 sAll
|
adamc@41
|
761 | SgnFun (m, n, s1, s2) =>
|
adamc@41
|
762 S.bind2 (sg ctx s1,
|
adamc@41
|
763 fn s1' =>
|
adamc@792
|
764 S.map2 (sg (bind (ctx, Str (m, n, s1'))) s2,
|
adamc@41
|
765 fn s2' =>
|
adamc@41
|
766 (SgnFun (m, n, s1', s2'), loc)))
|
adamc@59
|
767 | SgnProj _ => S.return2 sAll
|
adam@1864
|
768 | SgnWhere (sgn, ms, x, c) =>
|
adamc@42
|
769 S.bind2 (sg ctx sgn,
|
adamc@42
|
770 fn sgn' =>
|
adamc@42
|
771 S.map2 (con ctx c,
|
adamc@42
|
772 fn c' =>
|
adam@1864
|
773 (SgnWhere (sgn', ms, x, c'), loc)))
|
adamc@34
|
774 | SgnError => S.return2 sAll
|
adamc@34
|
775 in
|
adamc@34
|
776 sg
|
adamc@34
|
777 end
|
adamc@34
|
778
|
adamc@34
|
779 fun mapfold {kind, con, sgn_item, sgn} =
|
adamc@623
|
780 mapfoldB {kind = fn () => kind,
|
adamc@34
|
781 con = fn () => con,
|
adamc@34
|
782 sgn_item = fn () => sgn_item,
|
adamc@34
|
783 sgn = fn () => sgn,
|
adamc@34
|
784 bind = fn ((), _) => ()} ()
|
adamc@34
|
785
|
adamc@792
|
786 fun mapB {kind, con, sgn_item, sgn, bind} ctx s =
|
adamc@792
|
787 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
|
adamc@792
|
788 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@792
|
789 sgn_item = fn ctx => fn sgi => fn () => S.Continue (sgn_item ctx sgi, ()),
|
adamc@792
|
790 sgn = fn ctx => fn s => fn () => S.Continue (sgn ctx s, ()),
|
adamc@792
|
791 bind = bind} ctx s () of
|
adamc@792
|
792 S.Continue (s, ()) => s
|
adamc@792
|
793 | S.Return _ => raise Fail "ElabUtil.Sgn.mapB: Impossible"
|
adamc@792
|
794
|
adamc@34
|
795 fun map {kind, con, sgn_item, sgn} s =
|
adamc@34
|
796 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@34
|
797 con = fn c => fn () => S.Continue (con c, ()),
|
adamc@34
|
798 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
|
adamc@34
|
799 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
|
adamc@34
|
800 S.Return () => raise Fail "Elab_util.Sgn.map"
|
adamc@34
|
801 | S.Continue (s, ()) => s
|
adamc@34
|
802
|
adamc@2
|
803 end
|
adamc@34
|
804
|
adamc@76
|
805 structure Decl = struct
|
adamc@76
|
806
|
adamc@76
|
807 datatype binder =
|
adamc@623
|
808 RelK of string
|
adamc@623
|
809 | RelC of string * Elab.kind
|
adamc@792
|
810 | NamedC of string * int * Elab.kind * Elab.con option
|
adamc@76
|
811 | RelE of string * Elab.con
|
adamc@76
|
812 | NamedE of string * Elab.con
|
adam@1345
|
813 | Str of string * int * Elab.sgn
|
adam@1345
|
814 | Sgn of string * int * Elab.sgn
|
adamc@76
|
815
|
adamc@76
|
816 fun mapfoldB {kind = fk, con = fc, exp = fe, sgn_item = fsgi, sgn = fsg, str = fst, decl = fd, bind} =
|
adamc@76
|
817 let
|
adamc@623
|
818 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
|
adamc@76
|
819
|
adamc@76
|
820 fun bind' (ctx, b) =
|
adamc@76
|
821 let
|
adamc@76
|
822 val b' = case b of
|
adamc@623
|
823 Con.RelK x => RelK x
|
adamc@623
|
824 | Con.RelC x => RelC x
|
adamc@623
|
825 | Con.NamedC x => NamedC x
|
adamc@76
|
826 in
|
adamc@76
|
827 bind (ctx, b')
|
adamc@76
|
828 end
|
adamc@76
|
829 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@76
|
830
|
adamc@76
|
831 fun bind' (ctx, b) =
|
adamc@76
|
832 let
|
adamc@76
|
833 val b' = case b of
|
adamc@623
|
834 Exp.RelK x => RelK x
|
adamc@623
|
835 | Exp.RelC x => RelC x
|
adamc@76
|
836 | Exp.NamedC x => NamedC x
|
adamc@76
|
837 | Exp.RelE x => RelE x
|
adamc@76
|
838 | Exp.NamedE x => NamedE x
|
adamc@76
|
839 in
|
adamc@76
|
840 bind (ctx, b')
|
adamc@76
|
841 end
|
adamc@76
|
842 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind'}
|
adamc@76
|
843
|
adamc@76
|
844 fun bind' (ctx, b) =
|
adamc@76
|
845 let
|
adamc@76
|
846 val b' = case b of
|
adamc@623
|
847 Sgn.RelK x => RelK x
|
adamc@623
|
848 | Sgn.RelC x => RelC x
|
adamc@76
|
849 | Sgn.NamedC x => NamedC x
|
adam@1345
|
850 | Sgn.Sgn x => Sgn x
|
adam@1345
|
851 | Sgn.Str x => Str x
|
adamc@76
|
852 in
|
adamc@76
|
853 bind (ctx, b')
|
adamc@76
|
854 end
|
adamc@76
|
855 val mfsg = Sgn.mapfoldB {kind = fk, con = fc, sgn_item = fsgi, sgn = fsg, bind = bind'}
|
adamc@76
|
856
|
adamc@76
|
857 fun mfst ctx str acc =
|
adamc@76
|
858 S.bindP (mfst' ctx str acc, fst ctx)
|
adamc@76
|
859
|
adamc@76
|
860 and mfst' ctx (strAll as (str, loc)) =
|
adamc@76
|
861 case str of
|
adamc@76
|
862 StrConst ds =>
|
adamc@76
|
863 S.map2 (ListUtil.mapfoldB (fn (ctx, d) =>
|
adamc@76
|
864 (case #1 d of
|
adamc@792
|
865 DCon (x, n, k, c) =>
|
adamc@792
|
866 bind (ctx, NamedC (x, n, k, SOME c))
|
adamc@805
|
867 | DDatatype dts =>
|
adamc@156
|
868 let
|
adamc@805
|
869 fun doOne ((x, n, xs, xncs), ctx) =
|
adamc@805
|
870 let
|
adamc@805
|
871 val ctx = bind (ctx, NamedC (x, n, (KType, loc), NONE))
|
adamc@805
|
872 in
|
adamc@805
|
873 foldl (fn ((x, _, co), ctx) =>
|
adamc@805
|
874 let
|
adamc@805
|
875 val t =
|
adamc@805
|
876 case co of
|
adamc@805
|
877 NONE => CNamed n
|
adamc@805
|
878 | SOME t => TFun (t, (CNamed n, loc))
|
adamc@805
|
879
|
adamc@805
|
880 val k = (KType, loc)
|
adamc@805
|
881 val t = (t, loc)
|
adamc@805
|
882 val t = foldr (fn (x, t) =>
|
adamc@805
|
883 (TCFun (Explicit,
|
adamc@805
|
884 x,
|
adamc@805
|
885 k,
|
adamc@805
|
886 t), loc))
|
adamc@805
|
887 t xs
|
adamc@805
|
888 in
|
adamc@805
|
889 bind (ctx, NamedE (x, t))
|
adamc@805
|
890 end)
|
adamc@805
|
891 ctx xncs
|
adamc@805
|
892 end
|
adamc@156
|
893 in
|
adamc@805
|
894 foldl doOne ctx dts
|
adamc@156
|
895 end
|
adamc@191
|
896 | DDatatypeImp (x, n, m, ms, x', _, _) =>
|
adamc@792
|
897 bind (ctx, NamedC (x, n, (KType, loc),
|
adamc@792
|
898 SOME (CModProj (m, ms, x'), loc)))
|
adamc@76
|
899 | DVal (x, _, c, _) =>
|
adamc@76
|
900 bind (ctx, NamedE (x, c))
|
adamc@123
|
901 | DValRec vis =>
|
adamc@123
|
902 foldl (fn ((x, _, c, _), ctx) => bind (ctx, NamedE (x, c))) ctx vis
|
adam@1345
|
903 | DSgn (x, n, sgn) =>
|
adam@1345
|
904 bind (ctx, Sgn (x, n, sgn))
|
adam@1345
|
905 | DStr (x, n, sgn, _) =>
|
adam@1345
|
906 bind (ctx, Str (x, n, sgn))
|
adam@1345
|
907 | DFfiStr (x, n, sgn) =>
|
adam@1345
|
908 bind (ctx, Str (x, n, sgn))
|
adamc@100
|
909 | DConstraint _ => ctx
|
adamc@203
|
910 | DExport _ => ctx
|
adamc@707
|
911 | DTable (tn, x, n, c, _, pc, _, cc) =>
|
adamc@705
|
912 let
|
adamc@705
|
913 val ct = (CModProj (n, [], "sql_table"), loc)
|
adamc@705
|
914 val ct = (CApp (ct, c), loc)
|
adamc@707
|
915 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
|
adamc@705
|
916 in
|
adamc@705
|
917 bind (ctx, NamedE (x, ct))
|
adamc@705
|
918 end
|
adamc@338
|
919 | DSequence (tn, x, n) =>
|
adamc@338
|
920 bind (ctx, NamedE (x, (CModProj (n, [], "sql_sequence"), loc)))
|
adamc@754
|
921 | DView (tn, x, n, _, c) =>
|
adamc@754
|
922 let
|
adamc@754
|
923 val ct = (CModProj (n, [], "sql_view"), loc)
|
adamc@754
|
924 val ct = (CApp (ct, c), loc)
|
adamc@754
|
925 in
|
adamc@754
|
926 bind (ctx, NamedE (x, ct))
|
adamc@754
|
927 end
|
adamc@459
|
928 | DDatabase _ => ctx
|
adamc@459
|
929 | DCookie (tn, x, n, c) =>
|
adamc@459
|
930 bind (ctx, NamedE (x, (CApp ((CModProj (n, [], "cookie"), loc),
|
adamc@718
|
931 c), loc)))
|
adamc@720
|
932 | DStyle (tn, x, n) =>
|
adamc@1073
|
933 bind (ctx, NamedE (x, (CModProj (n, [], "css_class"), loc)))
|
adamc@1199
|
934 | DTask _ => ctx
|
adam@1294
|
935 | DPolicy _ => ctx
|
adam@2010
|
936 | DOnError _ => ctx
|
adam@2010
|
937 | DFfi (x, _, _, t) => bind (ctx, NamedE (x, t)),
|
adamc@76
|
938 mfd ctx d)) ctx ds,
|
adamc@76
|
939 fn ds' => (StrConst ds', loc))
|
adamc@76
|
940 | StrVar _ => S.return2 strAll
|
adamc@76
|
941 | StrProj (str, x) =>
|
adamc@76
|
942 S.map2 (mfst ctx str,
|
adamc@76
|
943 fn str' =>
|
adamc@76
|
944 (StrProj (str', x), loc))
|
adamc@76
|
945 | StrFun (x, n, sgn1, sgn2, str) =>
|
adamc@76
|
946 S.bind2 (mfsg ctx sgn1,
|
adamc@76
|
947 fn sgn1' =>
|
adamc@76
|
948 S.bind2 (mfsg ctx sgn2,
|
adamc@76
|
949 fn sgn2' =>
|
adamc@76
|
950 S.map2 (mfst ctx str,
|
adamc@76
|
951 fn str' =>
|
adamc@76
|
952 (StrFun (x, n, sgn1', sgn2', str'), loc))))
|
adamc@76
|
953 | StrApp (str1, str2) =>
|
adamc@76
|
954 S.bind2 (mfst ctx str1,
|
adamc@76
|
955 fn str1' =>
|
adamc@76
|
956 S.map2 (mfst ctx str2,
|
adamc@76
|
957 fn str2' =>
|
adamc@76
|
958 (StrApp (str1', str2'), loc)))
|
adamc@76
|
959 | StrError => S.return2 strAll
|
adamc@76
|
960
|
adamc@76
|
961 and mfd ctx d acc =
|
adamc@76
|
962 S.bindP (mfd' ctx d acc, fd ctx)
|
adamc@76
|
963
|
adamc@76
|
964 and mfd' ctx (dAll as (d, loc)) =
|
adamc@76
|
965 case d of
|
adamc@76
|
966 DCon (x, n, k, c) =>
|
adamc@623
|
967 S.bind2 (mfk ctx k,
|
adamc@76
|
968 fn k' =>
|
adamc@76
|
969 S.map2 (mfc ctx c,
|
adamc@76
|
970 fn c' =>
|
adamc@76
|
971 (DCon (x, n, k', c'), loc)))
|
adamc@805
|
972 | DDatatype dts =>
|
adamc@805
|
973 S.map2 (ListUtil.mapfold (fn (x, n, xs, xncs) =>
|
adamc@805
|
974 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@805
|
975 case c of
|
adamc@805
|
976 NONE => S.return2 (x, n, c)
|
adamc@805
|
977 | SOME c =>
|
adamc@805
|
978 S.map2 (mfc ctx c,
|
adamc@805
|
979 fn c' => (x, n, SOME c'))) xncs,
|
adamc@805
|
980 fn xncs' =>
|
adamc@805
|
981 (x, n, xs, xncs'))) dts,
|
adamc@805
|
982 fn dts' =>
|
adamc@805
|
983 (DDatatype dts', loc))
|
adamc@191
|
984 | DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@162
|
985 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
986 case c of
|
adamc@162
|
987 NONE => S.return2 (x, n, c)
|
adamc@162
|
988 | SOME c =>
|
adamc@162
|
989 S.map2 (mfc ctx c,
|
adamc@162
|
990 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
991 fn xncs' =>
|
adamc@191
|
992 (DDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
|
adamc@123
|
993 | DVal vi =>
|
adamc@123
|
994 S.map2 (mfvi ctx vi,
|
adamc@123
|
995 fn vi' =>
|
adamc@123
|
996 (DVal vi', loc))
|
adamc@123
|
997 | DValRec vis =>
|
adamc@123
|
998 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@123
|
999 fn vis' =>
|
adamc@123
|
1000 (DValRec vis', loc))
|
adamc@76
|
1001 | DSgn (x, n, sgn) =>
|
adamc@76
|
1002 S.map2 (mfsg ctx sgn,
|
adamc@76
|
1003 fn sgn' =>
|
adamc@76
|
1004 (DSgn (x, n, sgn'), loc))
|
adamc@76
|
1005 | DStr (x, n, sgn, str) =>
|
adamc@76
|
1006 S.bind2 (mfsg ctx sgn,
|
adamc@76
|
1007 fn sgn' =>
|
adamc@76
|
1008 S.map2 (mfst ctx str,
|
adamc@76
|
1009 fn str' =>
|
adamc@76
|
1010 (DStr (x, n, sgn', str'), loc)))
|
adamc@76
|
1011 | DFfiStr (x, n, sgn) =>
|
adamc@76
|
1012 S.map2 (mfsg ctx sgn,
|
adamc@76
|
1013 fn sgn' =>
|
adamc@76
|
1014 (DFfiStr (x, n, sgn'), loc))
|
adamc@88
|
1015 | DConstraint (c1, c2) =>
|
adamc@88
|
1016 S.bind2 (mfc ctx c1,
|
adamc@88
|
1017 fn c1' =>
|
adamc@88
|
1018 S.map2 (mfc ctx c2,
|
adamc@88
|
1019 fn c2' =>
|
adamc@88
|
1020 (DConstraint (c1', c2'), loc)))
|
adamc@109
|
1021 | DExport (en, sgn, str) =>
|
adamc@109
|
1022 S.bind2 (mfsg ctx sgn,
|
adamc@109
|
1023 fn sgn' =>
|
adamc@109
|
1024 S.map2 (mfst ctx str,
|
adamc@109
|
1025 fn str' =>
|
adamc@109
|
1026 (DExport (en, sgn', str'), loc)))
|
adamc@123
|
1027
|
adamc@707
|
1028 | DTable (tn, x, n, c, pe, pc, ce, cc) =>
|
adamc@704
|
1029 S.bind2 (mfc ctx c,
|
adamc@203
|
1030 fn c' =>
|
adamc@707
|
1031 S.bind2 (mfe ctx pe,
|
adamc@707
|
1032 fn pe' =>
|
adamc@707
|
1033 S.bind2 (mfc ctx pc,
|
adamc@707
|
1034 fn pc' =>
|
adamc@707
|
1035 S.bind2 (mfe ctx ce,
|
adamc@707
|
1036 fn ce' =>
|
adamc@707
|
1037 S.map2 (mfc ctx cc,
|
adamc@707
|
1038 fn cc' =>
|
adamc@707
|
1039 (DTable (tn, x, n, c', pe', pc', ce', cc'), loc))))))
|
adamc@338
|
1040 | DSequence _ => S.return2 dAll
|
adamc@754
|
1041 | DView (tn, x, n, e, c) =>
|
adamc@754
|
1042 S.bind2 (mfe ctx e,
|
adamc@754
|
1043 fn e' =>
|
adamc@754
|
1044 S.map2 (mfc ctx c,
|
adamc@754
|
1045 fn c' =>
|
adamc@754
|
1046 (DView (tn, x, n, e', c'), loc)))
|
adamc@203
|
1047
|
adamc@338
|
1048 | DDatabase _ => S.return2 dAll
|
adamc@271
|
1049
|
adamc@459
|
1050 | DCookie (tn, x, n, c) =>
|
adamc@459
|
1051 S.map2 (mfc ctx c,
|
adamc@459
|
1052 fn c' =>
|
adamc@459
|
1053 (DCookie (tn, x, n, c'), loc))
|
adamc@720
|
1054 | DStyle _ => S.return2 dAll
|
adamc@1075
|
1055 | DTask (e1, e2) =>
|
adamc@1075
|
1056 S.bind2 (mfe ctx e1,
|
adamc@1075
|
1057 fn e1' =>
|
adamc@1075
|
1058 S.map2 (mfe ctx e2,
|
adamc@1075
|
1059 fn e2' =>
|
adamc@1075
|
1060 (DTask (e1', e2'), loc)))
|
adamc@1199
|
1061 | DPolicy e1 =>
|
adamc@1199
|
1062 S.map2 (mfe ctx e1,
|
adamc@1199
|
1063 fn e1' =>
|
adamc@1199
|
1064 (DPolicy e1', loc))
|
adam@1294
|
1065 | DOnError _ => S.return2 dAll
|
adam@2010
|
1066 | DFfi (x, n, modes, t) =>
|
adam@2010
|
1067 S.map2 (mfc ctx t,
|
adam@2010
|
1068 fn t' =>
|
adam@2010
|
1069 (DFfi (x, n, modes, t'), loc))
|
adamc@459
|
1070
|
adamc@123
|
1071 and mfvi ctx (x, n, c, e) =
|
adamc@123
|
1072 S.bind2 (mfc ctx c,
|
adamc@123
|
1073 fn c' =>
|
adamc@123
|
1074 S.map2 (mfe ctx e,
|
adamc@123
|
1075 fn e' =>
|
adamc@123
|
1076 (x, n, c', e')))
|
adamc@76
|
1077 in
|
adamc@76
|
1078 mfd
|
adamc@76
|
1079 end
|
adamc@76
|
1080
|
adamc@76
|
1081 fun mapfold {kind, con, exp, sgn_item, sgn, str, decl} =
|
adamc@623
|
1082 mapfoldB {kind = fn () => kind,
|
adamc@76
|
1083 con = fn () => con,
|
adamc@76
|
1084 exp = fn () => exp,
|
adamc@76
|
1085 sgn_item = fn () => sgn_item,
|
adamc@76
|
1086 sgn = fn () => sgn,
|
adamc@76
|
1087 str = fn () => str,
|
adamc@76
|
1088 decl = fn () => decl,
|
adamc@76
|
1089 bind = fn ((), _) => ()} ()
|
adamc@76
|
1090
|
adamc@76
|
1091 fun exists {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
1092 case mapfold {kind = fn k => fn () =>
|
adamc@76
|
1093 if kind k then
|
adamc@76
|
1094 S.Return ()
|
adamc@76
|
1095 else
|
adamc@76
|
1096 S.Continue (k, ()),
|
adamc@76
|
1097 con = fn c => fn () =>
|
adamc@76
|
1098 if con c then
|
adamc@76
|
1099 S.Return ()
|
adamc@76
|
1100 else
|
adamc@76
|
1101 S.Continue (c, ()),
|
adamc@76
|
1102 exp = fn e => fn () =>
|
adamc@76
|
1103 if exp e then
|
adamc@76
|
1104 S.Return ()
|
adamc@76
|
1105 else
|
adamc@76
|
1106 S.Continue (e, ()),
|
adamc@76
|
1107 sgn_item = fn sgi => fn () =>
|
adamc@76
|
1108 if sgn_item sgi then
|
adamc@76
|
1109 S.Return ()
|
adamc@76
|
1110 else
|
adamc@76
|
1111 S.Continue (sgi, ()),
|
adamc@76
|
1112 sgn = fn x => fn () =>
|
adamc@76
|
1113 if sgn x then
|
adamc@76
|
1114 S.Return ()
|
adamc@76
|
1115 else
|
adamc@76
|
1116 S.Continue (x, ()),
|
adamc@76
|
1117 str = fn x => fn () =>
|
adamc@76
|
1118 if str x then
|
adamc@76
|
1119 S.Return ()
|
adamc@76
|
1120 else
|
adamc@76
|
1121 S.Continue (x, ()),
|
adamc@76
|
1122 decl = fn x => fn () =>
|
adamc@76
|
1123 if decl x then
|
adamc@76
|
1124 S.Return ()
|
adamc@76
|
1125 else
|
adamc@76
|
1126 S.Continue (x, ())} k () of
|
adamc@76
|
1127 S.Return _ => true
|
adamc@76
|
1128 | S.Continue _ => false
|
adamc@76
|
1129
|
adamc@76
|
1130 fun search {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
1131 case mapfold {kind = fn x => fn () =>
|
adamc@76
|
1132 case kind x of
|
adamc@76
|
1133 NONE => S.Continue (x, ())
|
adamc@76
|
1134 | SOME v => S.Return v,
|
adamc@76
|
1135
|
adamc@76
|
1136 con = fn x => fn () =>
|
adamc@76
|
1137 case con x of
|
adamc@76
|
1138 NONE => S.Continue (x, ())
|
adamc@76
|
1139 | SOME v => S.Return v,
|
adamc@76
|
1140
|
adamc@76
|
1141 exp = fn x => fn () =>
|
adamc@76
|
1142 case exp x of
|
adamc@76
|
1143 NONE => S.Continue (x, ())
|
adamc@76
|
1144 | SOME v => S.Return v,
|
adamc@76
|
1145
|
adamc@76
|
1146 sgn_item = fn x => fn () =>
|
adamc@76
|
1147 case sgn_item x of
|
adamc@76
|
1148 NONE => S.Continue (x, ())
|
adamc@76
|
1149 | SOME v => S.Return v,
|
adamc@76
|
1150
|
adamc@76
|
1151 sgn = fn x => fn () =>
|
adamc@76
|
1152 case sgn x of
|
adamc@76
|
1153 NONE => S.Continue (x, ())
|
adamc@76
|
1154 | SOME v => S.Return v,
|
adamc@76
|
1155
|
adamc@76
|
1156 str = fn x => fn () =>
|
adamc@76
|
1157 case str x of
|
adamc@76
|
1158 NONE => S.Continue (x, ())
|
adamc@76
|
1159 | SOME v => S.Return v,
|
adamc@76
|
1160
|
adamc@76
|
1161 decl = fn x => fn () =>
|
adamc@76
|
1162 case decl x of
|
adamc@76
|
1163 NONE => S.Continue (x, ())
|
adamc@76
|
1164 | SOME v => S.Return v
|
adamc@76
|
1165
|
adamc@76
|
1166 } k () of
|
adamc@76
|
1167 S.Return x => SOME x
|
adamc@76
|
1168 | S.Continue _ => NONE
|
adamc@76
|
1169
|
adamc@448
|
1170 fun foldMapB {kind, con, exp, sgn_item, sgn, str, decl, bind} ctx st d =
|
adamc@623
|
1171 case mapfoldB {kind = fn ctx => fn x => fn st => S.Continue (kind (ctx, x, st)),
|
adamc@448
|
1172 con = fn ctx => fn x => fn st => S.Continue (con (ctx, x, st)),
|
adamc@448
|
1173 exp = fn ctx => fn x => fn st => S.Continue (exp (ctx, x, st)),
|
adamc@448
|
1174 sgn_item = fn ctx => fn x => fn st => S.Continue (sgn_item (ctx, x, st)),
|
adamc@448
|
1175 sgn = fn ctx => fn x => fn st => S.Continue (sgn (ctx, x, st)),
|
adamc@448
|
1176 str = fn ctx => fn x => fn st => S.Continue (str (ctx, x, st)),
|
adamc@448
|
1177 decl = fn ctx => fn x => fn st => S.Continue (decl (ctx, x, st)),
|
adamc@448
|
1178 bind = bind} ctx d st of
|
adamc@448
|
1179 S.Continue x => x
|
adamc@448
|
1180 | S.Return _ => raise Fail "ElabUtil.Decl.foldMapB: Impossible"
|
adamc@448
|
1181
|
adam@1345
|
1182 fun map {kind, con, exp, sgn_item, sgn, str, decl} s =
|
adam@1345
|
1183 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adam@1345
|
1184 con = fn c => fn () => S.Continue (con c, ()),
|
adam@1345
|
1185 exp = fn e => fn () => S.Continue (exp e, ()),
|
adam@1345
|
1186 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
|
adam@1345
|
1187 sgn = fn s => fn () => S.Continue (sgn s, ()),
|
adam@1345
|
1188 str = fn si => fn () => S.Continue (str si, ()),
|
adam@1345
|
1189 decl = fn s => fn () => S.Continue (decl s, ())} s () of
|
adam@1345
|
1190 S.Return () => raise Fail "Elab_util.Decl.map"
|
adam@1345
|
1191 | S.Continue (s, ()) => s
|
adam@1345
|
1192
|
adam@1345
|
1193 fun mapB {kind, con, exp, sgn_item, sgn, str, decl, bind} ctx s =
|
adam@1345
|
1194 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
|
adam@1345
|
1195 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adam@1345
|
1196 exp = fn ctx => fn c => fn () => S.Continue (exp ctx c, ()),
|
adam@1345
|
1197 sgn_item = fn ctx => fn sgi => fn () => S.Continue (sgn_item ctx sgi, ()),
|
adam@1345
|
1198 sgn = fn ctx => fn s => fn () => S.Continue (sgn ctx s, ()),
|
adam@1345
|
1199 str = fn ctx => fn sgi => fn () => S.Continue (str ctx sgi, ()),
|
adam@1345
|
1200 decl = fn ctx => fn s => fn () => S.Continue (decl ctx s, ()),
|
adam@1345
|
1201 bind = bind} ctx s () of
|
adam@1345
|
1202 S.Continue (s, ()) => s
|
adam@1345
|
1203 | S.Return _ => raise Fail "ElabUtil.Decl.mapB: Impossible"
|
adam@1345
|
1204
|
adam@1732
|
1205 fun fold {kind, con, exp, sgn_item, sgn, str, decl} (st : 'a) d : 'a =
|
adam@1732
|
1206 case mapfold {kind = fn k => fn st => S.Continue (k, kind (k, st)),
|
adam@1732
|
1207 con = fn c => fn st => S.Continue (c, con (c, st)),
|
adam@1732
|
1208 exp = fn e => fn st => S.Continue (e, exp (e, st)),
|
adam@1732
|
1209 sgn_item = fn sgi => fn st => S.Continue (sgi, sgn_item (sgi, st)),
|
adam@1732
|
1210 sgn = fn s => fn st => S.Continue (s, sgn (s, st)),
|
adam@1732
|
1211 str = fn str' => fn st => S.Continue (str', str (str', st)),
|
adam@1732
|
1212 decl = fn d => fn st => S.Continue (d, decl (d, st))} d st of
|
adam@1732
|
1213 S.Continue (_, st) => st
|
adam@1732
|
1214 | S.Return _ => raise Fail "ElabUtil.Decl.fold: Impossible"
|
adam@1732
|
1215
|
adamc@448
|
1216 end
|
adamc@448
|
1217
|
adamc@448
|
1218 structure File = struct
|
adamc@448
|
1219
|
adamc@448
|
1220 fun maxName ds = foldl (fn (d, count) => Int.max (maxNameDecl d, count)) 0 ds
|
adamc@448
|
1221
|
adamc@448
|
1222 and maxNameDecl (d, _) =
|
adamc@448
|
1223 case d of
|
adamc@448
|
1224 DCon (_, n, _, _) => n
|
adamc@805
|
1225 | DDatatype dts =>
|
adamc@805
|
1226 foldl (fn ((_, n, _, ns), max) =>
|
adamc@448
|
1227 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@805
|
1228 (Int.max (n, max)) ns) 0 dts
|
adamc@448
|
1229 | DDatatypeImp (_, n1, n2, _, _, _, ns) =>
|
adamc@448
|
1230 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@448
|
1231 (Int.max (n1, n2)) ns
|
adamc@448
|
1232 | DVal (_, n, _, _) => n
|
adamc@448
|
1233 | DValRec vis => foldl (fn ((_, n, _, _), count) => Int.max (n, count)) 0 vis
|
adamc@448
|
1234 | DStr (_, n, sgn, str) => Int.max (n, Int.max (maxNameSgn sgn, maxNameStr str))
|
adamc@448
|
1235 | DSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
1236 | DFfiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
1237 | DConstraint _ => 0
|
adamc@448
|
1238 | DExport _ => 0
|
adamc@707
|
1239 | DTable (n1, _, n2, _, _, _, _, _) => Int.max (n1, n2)
|
adamc@459
|
1240 | DSequence (n1, _, n2) => Int.max (n1, n2)
|
adamc@754
|
1241 | DView (n1, _, n2, _, _) => Int.max (n1, n2)
|
adamc@448
|
1242 | DDatabase _ => 0
|
adamc@459
|
1243 | DCookie (n1, _, n2, _) => Int.max (n1, n2)
|
adamc@720
|
1244 | DStyle (n1, _, n2) => Int.max (n1, n2)
|
adamc@1075
|
1245 | DTask _ => 0
|
adamc@1199
|
1246 | DPolicy _ => 0
|
adam@1294
|
1247 | DOnError _ => 0
|
adam@2010
|
1248 | DFfi (_, n, _, _) => n
|
adamc@448
|
1249 and maxNameStr (str, _) =
|
adamc@448
|
1250 case str of
|
adamc@448
|
1251 StrConst ds => maxName ds
|
adamc@448
|
1252 | StrVar n => n
|
adamc@448
|
1253 | StrProj (str, _) => maxNameStr str
|
adamc@448
|
1254 | StrFun (_, n, dom, ran, str) => foldl Int.max n [maxNameSgn dom, maxNameSgn ran, maxNameStr str]
|
adamc@448
|
1255 | StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
|
adamc@448
|
1256 | StrError => 0
|
adamc@448
|
1257
|
adamc@448
|
1258 and maxNameSgn (sgn, _) =
|
adamc@448
|
1259 case sgn of
|
adamc@448
|
1260 SgnConst sgis => foldl (fn (sgi, count) => Int.max (maxNameSgi sgi, count)) 0 sgis
|
adamc@448
|
1261 | SgnVar n => n
|
adamc@448
|
1262 | SgnFun (_, n, dom, ran) => Int.max (n, Int.max (maxNameSgn dom, maxNameSgn ran))
|
adam@1864
|
1263 | SgnWhere (sgn, _, _, _) => maxNameSgn sgn
|
adamc@448
|
1264 | SgnProj (n, _, _) => n
|
adamc@448
|
1265 | SgnError => 0
|
adamc@448
|
1266
|
adamc@448
|
1267 and maxNameSgi (sgi, _) =
|
adamc@448
|
1268 case sgi of
|
adamc@448
|
1269 SgiConAbs (_, n, _) => n
|
adamc@448
|
1270 | SgiCon (_, n, _, _) => n
|
adamc@805
|
1271 | SgiDatatype dts =>
|
adamc@805
|
1272 foldl (fn ((_, n, _, ns), max) =>
|
adamc@805
|
1273 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@805
|
1274 (Int.max (n, max)) ns) 0 dts
|
adamc@448
|
1275 | SgiDatatypeImp (_, n1, n2, _, _, _, ns) =>
|
adamc@448
|
1276 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@448
|
1277 (Int.max (n1, n2)) ns
|
adamc@448
|
1278 | SgiVal (_, n, _) => n
|
adam@2190
|
1279 | SgiStr (_, _, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
1280 | SgiSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
|
adamc@448
|
1281 | SgiConstraint _ => 0
|
adamc@563
|
1282 | SgiClassAbs (_, n, _) => n
|
adamc@563
|
1283 | SgiClass (_, n, _, _) => n
|
adam@1584
|
1284
|
adam@1584
|
1285 fun findDecl pred file =
|
adam@1584
|
1286 let
|
adam@1584
|
1287 fun decl d =
|
adam@1584
|
1288 let
|
adam@1584
|
1289 val r = case #1 d of
|
adam@1584
|
1290 DStr (_, _, _, s) => str s
|
adam@1584
|
1291 | _ => NONE
|
adam@1584
|
1292 in
|
adam@1584
|
1293 case r of
|
adam@1584
|
1294 NONE => if pred d then SOME d else NONE
|
adam@1584
|
1295 | _ => r
|
adam@1584
|
1296 end
|
adam@1584
|
1297
|
adam@1584
|
1298 and str s =
|
adam@1584
|
1299 case #1 s of
|
adam@1584
|
1300 StrConst ds => ListUtil.search decl ds
|
adam@1584
|
1301 | StrFun (_, _, _, _, s) => str s
|
adam@1584
|
1302 | StrApp (s1, s2) =>
|
adam@1584
|
1303 (case str s1 of
|
adam@1584
|
1304 NONE => str s2
|
adam@1584
|
1305 | r => r)
|
adam@1584
|
1306 | _ => NONE
|
adam@1584
|
1307 in
|
adam@1584
|
1308 ListUtil.search decl file
|
adam@1584
|
1309 end
|
adamc@448
|
1310
|
adamc@34
|
1311 end
|
adamc@76
|
1312
|
adamc@76
|
1313 end
|