adamc@26
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@26
|
2 * All rights reserved.
|
adamc@26
|
3 *
|
adamc@26
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@26
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@26
|
6 *
|
adamc@26
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@26
|
8 * this list of conditions and the following disclaimer.
|
adamc@26
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@26
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@26
|
11 * and/or other materials provided with the distribution.
|
adamc@26
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@26
|
13 * derived from this software without specific prior written permission.
|
adamc@26
|
14 *
|
adamc@26
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@26
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@26
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@26
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@26
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@26
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@26
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@26
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@26
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@26
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@26
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@26
|
26 *)
|
adamc@26
|
27
|
adamc@26
|
28 structure MonoUtil :> MONO_UTIL = struct
|
adamc@26
|
29
|
adamc@26
|
30 open Mono
|
adamc@26
|
31
|
adamc@26
|
32 structure S = Search
|
adamc@26
|
33
|
adamc@267
|
34 val dummyt = (TRecord [], ErrorMsg.dummySpan)
|
adamc@267
|
35
|
adamc@26
|
36 structure Typ = struct
|
adamc@26
|
37
|
adamc@193
|
38 open Order
|
adamc@109
|
39
|
adamc@109
|
40 fun compare ((t1, _), (t2, _)) =
|
adamc@109
|
41 case (t1, t2) of
|
adamc@109
|
42 (TFun (d1, r1), TFun (d2, r2)) =>
|
adamc@109
|
43 join (compare (d1, d2), fn () => compare (r1, r2))
|
adamc@109
|
44 | (TRecord xts1, TRecord xts2) =>
|
adamc@109
|
45 let
|
adamc@109
|
46 val xts1 = sortFields xts1
|
adamc@109
|
47 val xts2 = sortFields xts2
|
adamc@109
|
48 in
|
adamc@109
|
49 joinL compareFields (xts1, xts2)
|
adamc@109
|
50 end
|
adamc@196
|
51 | (TDatatype (n1, _), TDatatype (n2, _)) => Int.compare (n1, n2)
|
adamc@109
|
52 | (TFfi (m1, x1), TFfi (m2, x2)) => join (String.compare (m1, m2), fn () => String.compare (x1, x2))
|
adamc@288
|
53 | (TOption t1, TOption t2) => compare (t1, t2)
|
adamc@109
|
54
|
adamc@109
|
55 | (TFun _, _) => LESS
|
adamc@109
|
56 | (_, TFun _) => GREATER
|
adamc@109
|
57
|
adamc@109
|
58 | (TRecord _, _) => LESS
|
adamc@109
|
59 | (_, TRecord _) => GREATER
|
adamc@109
|
60
|
adamc@168
|
61 | (TDatatype _, _) => LESS
|
adamc@168
|
62 | (_, TDatatype _) => GREATER
|
adamc@109
|
63
|
adamc@288
|
64 | (TFfi _, _) => LESS
|
adamc@288
|
65 | (_, TFfi _) => GREATER
|
adamc@288
|
66
|
adamc@109
|
67 and compareFields ((x1, t1), (x2, t2)) =
|
adamc@109
|
68 join (String.compare (x1, x2),
|
adamc@109
|
69 fn () => compare (t1, t2))
|
adamc@109
|
70
|
adamc@109
|
71 and sortFields xts = ListMergeSort.sort (fn (x, y) => compareFields (x, y) = GREATER) xts
|
adamc@109
|
72
|
adamc@26
|
73 fun mapfold fc =
|
adamc@26
|
74 let
|
adamc@26
|
75 fun mft c acc =
|
adamc@26
|
76 S.bindP (mft' c acc, fc)
|
adamc@26
|
77
|
adamc@26
|
78 and mft' (cAll as (c, loc)) =
|
adamc@26
|
79 case c of
|
adamc@26
|
80 TFun (t1, t2) =>
|
adamc@26
|
81 S.bind2 (mft t1,
|
adamc@26
|
82 fn t1' =>
|
adamc@26
|
83 S.map2 (mft t2,
|
adamc@26
|
84 fn t2' =>
|
adamc@26
|
85 (TFun (t1', t2'), loc)))
|
adamc@26
|
86 | TRecord xts =>
|
adamc@26
|
87 S.map2 (ListUtil.mapfold (fn (x, t) =>
|
adamc@26
|
88 S.map2 (mft t,
|
adamc@26
|
89 fn t' =>
|
adamc@26
|
90 (x, t')))
|
adamc@26
|
91 xts,
|
adamc@26
|
92 fn xts' => (TRecord xts', loc))
|
adamc@168
|
93 | TDatatype _ => S.return2 cAll
|
adamc@51
|
94 | TFfi _ => S.return2 cAll
|
adamc@288
|
95 | TOption t =>
|
adamc@288
|
96 S.map2 (mft t,
|
adamc@288
|
97 fn t' =>
|
adamc@288
|
98 (TOption t, loc))
|
adamc@26
|
99 in
|
adamc@26
|
100 mft
|
adamc@26
|
101 end
|
adamc@26
|
102
|
adamc@26
|
103 fun map typ c =
|
adamc@26
|
104 case mapfold (fn c => fn () => S.Continue (typ c, ())) c () of
|
adamc@26
|
105 S.Return () => raise Fail "Mono_util.Typ.map"
|
adamc@26
|
106 | S.Continue (c, ()) => c
|
adamc@26
|
107
|
adamc@26
|
108 fun fold typ s c =
|
adamc@26
|
109 case mapfold (fn c => fn s => S.Continue (c, typ (c, s))) c s of
|
adamc@26
|
110 S.Continue (_, s) => s
|
adamc@26
|
111 | S.Return _ => raise Fail "MonoUtil.Typ.fold: Impossible"
|
adamc@26
|
112
|
adamc@26
|
113 fun exists typ k =
|
adamc@26
|
114 case mapfold (fn c => fn () =>
|
adamc@26
|
115 if typ c then
|
adamc@26
|
116 S.Return ()
|
adamc@26
|
117 else
|
adamc@26
|
118 S.Continue (c, ())) k () of
|
adamc@26
|
119 S.Return _ => true
|
adamc@26
|
120 | S.Continue _ => false
|
adamc@26
|
121
|
adamc@26
|
122 end
|
adamc@26
|
123
|
adamc@26
|
124 structure Exp = struct
|
adamc@26
|
125
|
adamc@26
|
126 datatype binder =
|
adamc@168
|
127 Datatype of string * int * (string * int * typ option) list
|
adamc@26
|
128 | RelE of string * typ
|
adamc@109
|
129 | NamedE of string * int * typ * exp option * string
|
adamc@26
|
130
|
adamc@26
|
131 fun mapfoldB {typ = fc, exp = fe, bind} =
|
adamc@26
|
132 let
|
adamc@26
|
133 val mft = Typ.mapfold fc
|
adamc@26
|
134
|
adamc@26
|
135 fun mfe ctx e acc =
|
adamc@26
|
136 S.bindP (mfe' ctx e acc, fe ctx)
|
adamc@26
|
137
|
adamc@26
|
138 and mfe' ctx (eAll as (e, loc)) =
|
adamc@26
|
139 case e of
|
adamc@26
|
140 EPrim _ => S.return2 eAll
|
adamc@26
|
141 | ERel _ => S.return2 eAll
|
adamc@26
|
142 | ENamed _ => S.return2 eAll
|
adamc@188
|
143 | ECon (_, _, NONE) => S.return2 eAll
|
adamc@188
|
144 | ECon (dk, n, SOME e) =>
|
adamc@178
|
145 S.map2 (mfe ctx e,
|
adamc@178
|
146 fn e' =>
|
adamc@188
|
147 (ECon (dk, n, SOME e'), loc))
|
adamc@290
|
148 | ESome (t, e) =>
|
adamc@290
|
149 S.bind2 (mft t,
|
adamc@290
|
150 fn t' =>
|
adamc@290
|
151 S.map2 (mfe ctx e,
|
adamc@290
|
152 fn e' =>
|
adamc@290
|
153 (ESome (t', e'), loc)))
|
adamc@51
|
154 | EFfi _ => S.return2 eAll
|
adamc@51
|
155 | EFfiApp (m, x, es) =>
|
adamc@51
|
156 S.map2 (ListUtil.mapfold (fn e => mfe ctx e) es,
|
adamc@51
|
157 fn es' =>
|
adamc@51
|
158 (EFfiApp (m, x, es'), loc))
|
adamc@26
|
159 | EApp (e1, e2) =>
|
adamc@26
|
160 S.bind2 (mfe ctx e1,
|
adamc@26
|
161 fn e1' =>
|
adamc@26
|
162 S.map2 (mfe ctx e2,
|
adamc@26
|
163 fn e2' =>
|
adamc@26
|
164 (EApp (e1', e2'), loc)))
|
adamc@26
|
165 | EAbs (x, dom, ran, e) =>
|
adamc@26
|
166 S.bind2 (mft dom,
|
adamc@26
|
167 fn dom' =>
|
adamc@26
|
168 S.bind2 (mft ran,
|
adamc@26
|
169 fn ran' =>
|
adamc@26
|
170 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
|
adamc@26
|
171 fn e' =>
|
adamc@26
|
172 (EAbs (x, dom', ran', e'), loc))))
|
adamc@26
|
173
|
adamc@26
|
174 | ERecord xes =>
|
adamc@29
|
175 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
|
adamc@29
|
176 S.bind2 (mfe ctx e,
|
adamc@26
|
177 fn e' =>
|
adamc@29
|
178 S.map2 (mft t,
|
adamc@29
|
179 fn t' =>
|
adamc@29
|
180 (x, e', t'))))
|
adamc@26
|
181 xes,
|
adamc@26
|
182 fn xes' =>
|
adamc@26
|
183 (ERecord xes', loc))
|
adamc@26
|
184 | EField (e, x) =>
|
adamc@26
|
185 S.map2 (mfe ctx e,
|
adamc@26
|
186 fn e' =>
|
adamc@26
|
187 (EField (e', x), loc))
|
adamc@94
|
188
|
adamc@182
|
189 | ECase (e, pes, {disc, result}) =>
|
adamc@178
|
190 S.bind2 (mfe ctx e,
|
adamc@178
|
191 fn e' =>
|
adamc@178
|
192 S.bind2 (ListUtil.mapfold (fn (p, e) =>
|
adamc@179
|
193 let
|
adamc@179
|
194 fun pb ((p, _), ctx) =
|
adamc@179
|
195 case p of
|
adamc@179
|
196 PWild => ctx
|
adamc@182
|
197 | PVar (x, t) => bind (ctx, RelE (x, t))
|
adamc@179
|
198 | PPrim _ => ctx
|
adamc@188
|
199 | PCon (_, _, NONE) => ctx
|
adamc@188
|
200 | PCon (_, _, SOME p) => pb (p, ctx)
|
adamc@182
|
201 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
|
adamc@179
|
202 pb (p, ctx)) ctx xps
|
adamc@288
|
203 | PNone _ => ctx
|
adamc@288
|
204 | PSome (_, p) => pb (p, ctx)
|
adamc@179
|
205 in
|
adamc@179
|
206 S.map2 (mfe (pb (p, ctx)) e,
|
adamc@179
|
207 fn e' => (p, e'))
|
adamc@179
|
208 end) pes,
|
adamc@178
|
209 fn pes' =>
|
adamc@182
|
210 S.bind2 (mft disc,
|
adamc@182
|
211 fn disc' =>
|
adamc@182
|
212 S.map2 (mft result,
|
adamc@182
|
213 fn result' =>
|
adamc@182
|
214 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
|
adamc@178
|
215
|
adamc@283
|
216 | EError (e, t) =>
|
adamc@283
|
217 S.bind2 (mfe ctx e,
|
adamc@283
|
218 fn e' =>
|
adamc@283
|
219 S.map2 (mft t,
|
adamc@283
|
220 fn t' =>
|
adamc@283
|
221 (EError (e', t'), loc)))
|
adamc@283
|
222
|
adamc@94
|
223 | EStrcat (e1, e2) =>
|
adamc@94
|
224 S.bind2 (mfe ctx e1,
|
adamc@94
|
225 fn e1' =>
|
adamc@94
|
226 S.map2 (mfe ctx e2,
|
adamc@94
|
227 fn e2' =>
|
adamc@94
|
228 (EStrcat (e1', e2'), loc)))
|
adamc@102
|
229
|
adamc@102
|
230 | EWrite e =>
|
adamc@102
|
231 S.map2 (mfe ctx e,
|
adamc@102
|
232 fn e' =>
|
adamc@102
|
233 (EWrite e', loc))
|
adamc@106
|
234
|
adamc@106
|
235 | ESeq (e1, e2) =>
|
adamc@106
|
236 S.bind2 (mfe ctx e1,
|
adamc@106
|
237 fn e1' =>
|
adamc@106
|
238 S.map2 (mfe ctx e2,
|
adamc@106
|
239 fn e2' =>
|
adamc@106
|
240 (ESeq (e1', e2'), loc)))
|
adamc@251
|
241 | ELet (x, t, e1, e2) =>
|
adamc@251
|
242 S.bind2 (mft t,
|
adamc@251
|
243 fn t' =>
|
adamc@251
|
244 S.bind2 (mfe ctx e1,
|
adamc@251
|
245 fn e1' =>
|
adamc@252
|
246 S.map2 (mfe (bind (ctx, RelE (x, t'))) e2,
|
adamc@251
|
247 fn e2' =>
|
adamc@251
|
248 (ELet (x, t', e1', e2'), loc))))
|
adamc@111
|
249
|
adamc@111
|
250 | EClosure (n, es) =>
|
adamc@111
|
251 S.map2 (ListUtil.mapfold (mfe ctx) es,
|
adamc@111
|
252 fn es' =>
|
adamc@111
|
253 (EClosure (n, es'), loc))
|
adamc@252
|
254
|
adamc@252
|
255 | EQuery {exps, tables, state, query, body, initial} =>
|
adamc@252
|
256 S.bind2 (ListUtil.mapfold (fn (x, t) =>
|
adamc@252
|
257 S.map2 (mft t,
|
adamc@252
|
258 fn t' => (x, t'))) exps,
|
adamc@252
|
259 fn exps' =>
|
adamc@252
|
260 S.bind2 (ListUtil.mapfold (fn (x, xts) =>
|
adamc@252
|
261 S.map2 (ListUtil.mapfold
|
adamc@252
|
262 (fn (x, t) =>
|
adamc@252
|
263 S.map2 (mft t,
|
adamc@252
|
264 fn t' => (x, t'))) xts,
|
adamc@252
|
265 fn xts' => (x, xts'))) tables,
|
adamc@252
|
266 fn tables' =>
|
adamc@252
|
267 S.bind2 (mft state,
|
adamc@252
|
268 fn state' =>
|
adamc@252
|
269 S.bind2 (mfe ctx query,
|
adamc@252
|
270 fn query' =>
|
adamc@267
|
271 S.bind2 (mfe (bind (bind (ctx, RelE ("r", dummyt)),
|
adamc@267
|
272 RelE ("acc", dummyt)))
|
adamc@267
|
273 body,
|
adamc@267
|
274 fn body' =>
|
adamc@267
|
275 S.map2 (mfe ctx initial,
|
adamc@267
|
276 fn initial' =>
|
adamc@267
|
277 (EQuery {exps = exps',
|
adamc@267
|
278 tables = tables',
|
adamc@267
|
279 state = state',
|
adamc@267
|
280 query = query',
|
adamc@267
|
281 body = body',
|
adamc@267
|
282 initial = initial'},
|
adamc@267
|
283 loc)))))))
|
adamc@26
|
284 in
|
adamc@26
|
285 mfe
|
adamc@26
|
286 end
|
adamc@26
|
287
|
adamc@26
|
288 fun mapfold {typ = fc, exp = fe} =
|
adamc@26
|
289 mapfoldB {typ = fc,
|
adamc@26
|
290 exp = fn () => fe,
|
adamc@26
|
291 bind = fn ((), _) => ()} ()
|
adamc@26
|
292
|
adamc@26
|
293 fun mapB {typ, exp, bind} ctx e =
|
adamc@26
|
294 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
|
adamc@26
|
295 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
|
adamc@26
|
296 bind = bind} ctx e () of
|
adamc@26
|
297 S.Continue (e, ()) => e
|
adamc@26
|
298 | S.Return _ => raise Fail "MonoUtil.Exp.mapB: Impossible"
|
adamc@26
|
299
|
adamc@26
|
300 fun map {typ, exp} e =
|
adamc@26
|
301 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
|
adamc@26
|
302 exp = fn e => fn () => S.Continue (exp e, ())} e () of
|
adamc@26
|
303 S.Return () => raise Fail "Mono_util.Exp.map"
|
adamc@26
|
304 | S.Continue (e, ()) => e
|
adamc@26
|
305
|
adamc@26
|
306 fun fold {typ, exp} s e =
|
adamc@26
|
307 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
|
adamc@26
|
308 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
|
adamc@26
|
309 S.Continue (_, s) => s
|
adamc@26
|
310 | S.Return _ => raise Fail "MonoUtil.Exp.fold: Impossible"
|
adamc@26
|
311
|
adamc@26
|
312 fun exists {typ, exp} k =
|
adamc@26
|
313 case mapfold {typ = fn c => fn () =>
|
adamc@26
|
314 if typ c then
|
adamc@26
|
315 S.Return ()
|
adamc@26
|
316 else
|
adamc@26
|
317 S.Continue (c, ()),
|
adamc@26
|
318 exp = fn e => fn () =>
|
adamc@26
|
319 if exp e then
|
adamc@26
|
320 S.Return ()
|
adamc@26
|
321 else
|
adamc@26
|
322 S.Continue (e, ())} k () of
|
adamc@26
|
323 S.Return _ => true
|
adamc@26
|
324 | S.Continue _ => false
|
adamc@26
|
325
|
adamc@26
|
326 end
|
adamc@26
|
327
|
adamc@26
|
328 structure Decl = struct
|
adamc@26
|
329
|
adamc@26
|
330 datatype binder = datatype Exp.binder
|
adamc@26
|
331
|
adamc@26
|
332 fun mapfoldB {typ = fc, exp = fe, decl = fd, bind} =
|
adamc@26
|
333 let
|
adamc@26
|
334 val mft = Typ.mapfold fc
|
adamc@26
|
335
|
adamc@26
|
336 val mfe = Exp.mapfoldB {typ = fc, exp = fe, bind = bind}
|
adamc@26
|
337
|
adamc@26
|
338 fun mfd ctx d acc =
|
adamc@26
|
339 S.bindP (mfd' ctx d acc, fd ctx)
|
adamc@26
|
340
|
adamc@26
|
341 and mfd' ctx (dAll as (d, loc)) =
|
adamc@26
|
342 case d of
|
adamc@164
|
343 DDatatype (x, n, xncs) =>
|
adamc@164
|
344 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@164
|
345 case c of
|
adamc@164
|
346 NONE => S.return2 (x, n, c)
|
adamc@164
|
347 | SOME c =>
|
adamc@164
|
348 S.map2 (mft c,
|
adamc@164
|
349 fn c' => (x, n, SOME c'))) xncs,
|
adamc@164
|
350 fn xncs' =>
|
adamc@164
|
351 (DDatatype (x, n, xncs'), loc))
|
adamc@164
|
352 | DVal vi =>
|
adamc@126
|
353 S.map2 (mfvi ctx vi,
|
adamc@126
|
354 fn vi' =>
|
adamc@126
|
355 (DVal vi', loc))
|
adamc@126
|
356 | DValRec vis =>
|
adamc@196
|
357 let
|
adamc@196
|
358 val ctx' = foldl (fn ((x, n, t, _, s), ctx') => bind (ctx', NamedE (x, n, t, NONE, s))) ctx vis
|
adamc@196
|
359 in
|
adamc@196
|
360 S.map2 (ListUtil.mapfold (mfvi ctx') vis,
|
adamc@196
|
361 fn vis' =>
|
adamc@196
|
362 (DValRec vis', loc))
|
adamc@196
|
363 end
|
adamc@144
|
364 | DExport (ek, s, n, ts) =>
|
adamc@120
|
365 S.map2 (ListUtil.mapfold mft ts,
|
adamc@120
|
366 fn ts' =>
|
adamc@144
|
367 (DExport (ek, s, n, ts'), loc))
|
adamc@273
|
368 | DTable _ => S.return2 dAll
|
adamc@271
|
369 | DDatabase _ => S.return2 dAll
|
adamc@126
|
370
|
adamc@126
|
371 and mfvi ctx (x, n, t, e, s) =
|
adamc@126
|
372 S.bind2 (mft t,
|
adamc@126
|
373 fn t' =>
|
adamc@126
|
374 S.map2 (mfe ctx e,
|
adamc@126
|
375 fn e' =>
|
adamc@126
|
376 (x, n, t', e', s)))
|
adamc@26
|
377 in
|
adamc@26
|
378 mfd
|
adamc@26
|
379 end
|
adamc@26
|
380
|
adamc@26
|
381 fun mapfold {typ = fc, exp = fe, decl = fd} =
|
adamc@26
|
382 mapfoldB {typ = fc,
|
adamc@26
|
383 exp = fn () => fe,
|
adamc@26
|
384 decl = fn () => fd,
|
adamc@26
|
385 bind = fn ((), _) => ()} ()
|
adamc@26
|
386
|
adamc@26
|
387 fun fold {typ, exp, decl} s d =
|
adamc@26
|
388 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
|
adamc@26
|
389 exp = fn e => fn s => S.Continue (e, exp (e, s)),
|
adamc@26
|
390 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
|
adamc@26
|
391 S.Continue (_, s) => s
|
adamc@26
|
392 | S.Return _ => raise Fail "MonoUtil.Decl.fold: Impossible"
|
adamc@26
|
393
|
adamc@26
|
394 end
|
adamc@26
|
395
|
adamc@26
|
396 structure File = struct
|
adamc@26
|
397
|
adamc@26
|
398 datatype binder = datatype Exp.binder
|
adamc@26
|
399
|
adamc@26
|
400 fun mapfoldB (all as {bind, ...}) =
|
adamc@26
|
401 let
|
adamc@26
|
402 val mfd = Decl.mapfoldB all
|
adamc@26
|
403
|
adamc@26
|
404 fun mff ctx ds =
|
adamc@26
|
405 case ds of
|
adamc@26
|
406 nil => S.return2 nil
|
adamc@26
|
407 | d :: ds' =>
|
adamc@26
|
408 S.bind2 (mfd ctx d,
|
adamc@26
|
409 fn d' =>
|
adamc@26
|
410 let
|
adamc@100
|
411 val ctx' =
|
adamc@26
|
412 case #1 d' of
|
adamc@164
|
413 DDatatype (x, n, xncs) =>
|
adamc@164
|
414 let
|
adamc@168
|
415 val ctx = bind (ctx, Datatype (x, n, xncs))
|
adamc@198
|
416 val t = (TDatatype (n, ref (ElabUtil.classifyDatatype xncs, xncs)), #2 d')
|
adamc@164
|
417 in
|
adamc@164
|
418 foldl (fn ((x, n, to), ctx) =>
|
adamc@164
|
419 let
|
adamc@164
|
420 val t = case to of
|
adamc@164
|
421 NONE => t
|
adamc@164
|
422 | SOME t' => (TFun (t', t), #2 d')
|
adamc@164
|
423 in
|
adamc@164
|
424 bind (ctx, NamedE (x, n, t, NONE, ""))
|
adamc@164
|
425 end)
|
adamc@164
|
426 ctx xncs
|
adamc@164
|
427 end
|
adamc@164
|
428 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
|
adamc@126
|
429 | DValRec vis => foldl (fn ((x, n, t, e, s), ctx) =>
|
adamc@196
|
430 bind (ctx, NamedE (x, n, t, NONE, s))) ctx vis
|
adamc@109
|
431 | DExport _ => ctx
|
adamc@273
|
432 | DTable _ => ctx
|
adamc@271
|
433 | DDatabase _ => ctx
|
adamc@26
|
434 in
|
adamc@26
|
435 S.map2 (mff ctx' ds',
|
adamc@26
|
436 fn ds' =>
|
adamc@26
|
437 d' :: ds')
|
adamc@26
|
438 end)
|
adamc@26
|
439 in
|
adamc@26
|
440 mff
|
adamc@26
|
441 end
|
adamc@26
|
442
|
adamc@26
|
443 fun mapfold {typ = fc, exp = fe, decl = fd} =
|
adamc@26
|
444 mapfoldB {typ = fc,
|
adamc@26
|
445 exp = fn () => fe,
|
adamc@26
|
446 decl = fn () => fd,
|
adamc@26
|
447 bind = fn ((), _) => ()} ()
|
adamc@26
|
448
|
adamc@26
|
449 fun mapB {typ, exp, decl, bind} ctx ds =
|
adamc@26
|
450 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
|
adamc@26
|
451 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
|
adamc@26
|
452 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
|
adamc@26
|
453 bind = bind} ctx ds () of
|
adamc@26
|
454 S.Continue (ds, ()) => ds
|
adamc@26
|
455 | S.Return _ => raise Fail "MonoUtil.File.mapB: Impossible"
|
adamc@26
|
456
|
adamc@96
|
457 fun map {typ, exp, decl} e =
|
adamc@96
|
458 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
|
adamc@96
|
459 exp = fn e => fn () => S.Continue (exp e, ()),
|
adamc@96
|
460 decl = fn d => fn () => S.Continue (decl d, ())} e () of
|
adamc@96
|
461 S.Return () => raise Fail "Mono_util.File.map"
|
adamc@96
|
462 | S.Continue (e, ()) => e
|
adamc@96
|
463
|
adamc@26
|
464 fun fold {typ, exp, decl} s d =
|
adamc@26
|
465 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
|
adamc@26
|
466 exp = fn e => fn s => S.Continue (e, exp (e, s)),
|
adamc@26
|
467 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
|
adamc@26
|
468 S.Continue (_, s) => s
|
adamc@26
|
469 | S.Return _ => raise Fail "MonoUtil.File.fold: Impossible"
|
adamc@26
|
470
|
adamc@26
|
471 end
|
adamc@26
|
472
|
adamc@26
|
473 end
|