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@757
|
54 | (TList t1, TList t2) => compare (t1, t2)
|
adamc@577
|
55 | (TSource, TSource) => EQUAL
|
adamc@568
|
56 | (TSignal t1, TSignal t2) => compare (t1, t2)
|
adamc@109
|
57
|
adamc@109
|
58 | (TFun _, _) => LESS
|
adamc@109
|
59 | (_, TFun _) => GREATER
|
adamc@109
|
60
|
adamc@109
|
61 | (TRecord _, _) => LESS
|
adamc@109
|
62 | (_, TRecord _) => GREATER
|
adamc@109
|
63
|
adamc@168
|
64 | (TDatatype _, _) => LESS
|
adamc@168
|
65 | (_, TDatatype _) => GREATER
|
adamc@109
|
66
|
adamc@288
|
67 | (TFfi _, _) => LESS
|
adamc@288
|
68 | (_, TFfi _) => GREATER
|
adamc@288
|
69
|
adamc@568
|
70 | (TOption _, _) => LESS
|
adamc@568
|
71 | (_, TOption _) => GREATER
|
adamc@568
|
72
|
adamc@757
|
73 | (TList _, _) => LESS
|
adamc@757
|
74 | (_, TList _) => GREATER
|
adamc@757
|
75
|
adamc@577
|
76 | (TSource, _) => LESS
|
adamc@577
|
77 | (_, TSource) => GREATER
|
adamc@577
|
78
|
adamc@109
|
79 and compareFields ((x1, t1), (x2, t2)) =
|
adamc@109
|
80 join (String.compare (x1, x2),
|
adamc@109
|
81 fn () => compare (t1, t2))
|
adamc@109
|
82
|
adamc@109
|
83 and sortFields xts = ListMergeSort.sort (fn (x, y) => compareFields (x, y) = GREATER) xts
|
adamc@109
|
84
|
adamc@26
|
85 fun mapfold fc =
|
adamc@26
|
86 let
|
adamc@26
|
87 fun mft c acc =
|
adamc@26
|
88 S.bindP (mft' c acc, fc)
|
adamc@26
|
89
|
adamc@26
|
90 and mft' (cAll as (c, loc)) =
|
adamc@26
|
91 case c of
|
adamc@26
|
92 TFun (t1, t2) =>
|
adamc@26
|
93 S.bind2 (mft t1,
|
adamc@26
|
94 fn t1' =>
|
adamc@26
|
95 S.map2 (mft t2,
|
adamc@26
|
96 fn t2' =>
|
adamc@26
|
97 (TFun (t1', t2'), loc)))
|
adamc@26
|
98 | TRecord xts =>
|
adamc@26
|
99 S.map2 (ListUtil.mapfold (fn (x, t) =>
|
adamc@26
|
100 S.map2 (mft t,
|
adamc@26
|
101 fn t' =>
|
adamc@26
|
102 (x, t')))
|
adamc@26
|
103 xts,
|
adamc@26
|
104 fn xts' => (TRecord xts', loc))
|
adamc@168
|
105 | TDatatype _ => S.return2 cAll
|
adamc@51
|
106 | TFfi _ => S.return2 cAll
|
adamc@288
|
107 | TOption t =>
|
adamc@288
|
108 S.map2 (mft t,
|
adamc@288
|
109 fn t' =>
|
adamc@288
|
110 (TOption t, loc))
|
adamc@757
|
111 | TList t =>
|
adamc@757
|
112 S.map2 (mft t,
|
adamc@757
|
113 fn t' =>
|
adamc@757
|
114 (TList t, loc))
|
adamc@577
|
115 | TSource => S.return2 cAll
|
adamc@568
|
116 | TSignal t =>
|
adamc@568
|
117 S.map2 (mft t,
|
adamc@568
|
118 fn t' =>
|
adamc@568
|
119 (TSignal t, loc))
|
adamc@26
|
120 in
|
adamc@26
|
121 mft
|
adamc@26
|
122 end
|
adamc@26
|
123
|
adamc@26
|
124 fun map typ c =
|
adamc@26
|
125 case mapfold (fn c => fn () => S.Continue (typ c, ())) c () of
|
adamc@26
|
126 S.Return () => raise Fail "Mono_util.Typ.map"
|
adamc@26
|
127 | S.Continue (c, ()) => c
|
adamc@26
|
128
|
adamc@26
|
129 fun fold typ s c =
|
adamc@26
|
130 case mapfold (fn c => fn s => S.Continue (c, typ (c, s))) c s of
|
adamc@26
|
131 S.Continue (_, s) => s
|
adamc@26
|
132 | S.Return _ => raise Fail "MonoUtil.Typ.fold: Impossible"
|
adamc@26
|
133
|
adamc@26
|
134 fun exists typ k =
|
adamc@26
|
135 case mapfold (fn c => fn () =>
|
adamc@26
|
136 if typ c then
|
adamc@26
|
137 S.Return ()
|
adamc@26
|
138 else
|
adamc@26
|
139 S.Continue (c, ())) k () of
|
adamc@26
|
140 S.Return _ => true
|
adamc@26
|
141 | S.Continue _ => false
|
adamc@26
|
142
|
adamc@26
|
143 end
|
adamc@26
|
144
|
adamc@26
|
145 structure Exp = struct
|
adamc@26
|
146
|
adamc@26
|
147 datatype binder =
|
adamc@168
|
148 Datatype of string * int * (string * int * typ option) list
|
adamc@26
|
149 | RelE of string * typ
|
adamc@109
|
150 | NamedE of string * int * typ * exp option * string
|
adamc@26
|
151
|
adamc@26
|
152 fun mapfoldB {typ = fc, exp = fe, bind} =
|
adamc@26
|
153 let
|
adamc@26
|
154 val mft = Typ.mapfold fc
|
adamc@26
|
155
|
adamc@26
|
156 fun mfe ctx e acc =
|
adamc@26
|
157 S.bindP (mfe' ctx e acc, fe ctx)
|
adamc@26
|
158
|
adamc@26
|
159 and mfe' ctx (eAll as (e, loc)) =
|
adamc@26
|
160 case e of
|
adamc@26
|
161 EPrim _ => S.return2 eAll
|
adamc@26
|
162 | ERel _ => S.return2 eAll
|
adamc@26
|
163 | ENamed _ => S.return2 eAll
|
adamc@188
|
164 | ECon (_, _, NONE) => S.return2 eAll
|
adamc@188
|
165 | ECon (dk, n, SOME e) =>
|
adamc@178
|
166 S.map2 (mfe ctx e,
|
adamc@178
|
167 fn e' =>
|
adamc@188
|
168 (ECon (dk, n, SOME e'), loc))
|
adamc@297
|
169 | ENone t =>
|
adamc@297
|
170 S.map2 (mft t,
|
adamc@297
|
171 fn t' =>
|
adamc@297
|
172 (ENone t', loc))
|
adamc@290
|
173 | ESome (t, e) =>
|
adamc@290
|
174 S.bind2 (mft t,
|
adamc@290
|
175 fn t' =>
|
adamc@290
|
176 S.map2 (mfe ctx e,
|
adamc@290
|
177 fn e' =>
|
adamc@290
|
178 (ESome (t', e'), loc)))
|
adamc@51
|
179 | EFfi _ => S.return2 eAll
|
adamc@51
|
180 | EFfiApp (m, x, es) =>
|
adamc@51
|
181 S.map2 (ListUtil.mapfold (fn e => mfe ctx e) es,
|
adamc@51
|
182 fn es' =>
|
adamc@51
|
183 (EFfiApp (m, x, es'), loc))
|
adamc@26
|
184 | EApp (e1, e2) =>
|
adamc@26
|
185 S.bind2 (mfe ctx e1,
|
adamc@26
|
186 fn e1' =>
|
adamc@26
|
187 S.map2 (mfe ctx e2,
|
adamc@26
|
188 fn e2' =>
|
adamc@26
|
189 (EApp (e1', e2'), loc)))
|
adamc@26
|
190 | EAbs (x, dom, ran, e) =>
|
adamc@26
|
191 S.bind2 (mft dom,
|
adamc@26
|
192 fn dom' =>
|
adamc@26
|
193 S.bind2 (mft ran,
|
adamc@26
|
194 fn ran' =>
|
adamc@26
|
195 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
|
adamc@26
|
196 fn e' =>
|
adamc@26
|
197 (EAbs (x, dom', ran', e'), loc))))
|
adamc@26
|
198
|
adamc@387
|
199 | EUnop (s, e) =>
|
adamc@387
|
200 S.map2 (mfe ctx e,
|
adamc@387
|
201 fn e' =>
|
adamc@387
|
202 (EUnop (s, e'), loc))
|
adamc@387
|
203 | EBinop (s, e1, e2) =>
|
adamc@387
|
204 S.bind2 (mfe ctx e1,
|
adamc@387
|
205 fn e1' =>
|
adamc@387
|
206 S.map2 (mfe ctx e2,
|
adamc@387
|
207 fn e2' =>
|
adamc@387
|
208 (EBinop (s, e1', e2'), loc)))
|
adamc@387
|
209
|
adamc@26
|
210 | ERecord xes =>
|
adamc@29
|
211 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
|
adamc@29
|
212 S.bind2 (mfe ctx e,
|
adamc@26
|
213 fn e' =>
|
adamc@29
|
214 S.map2 (mft t,
|
adamc@29
|
215 fn t' =>
|
adamc@29
|
216 (x, e', t'))))
|
adamc@26
|
217 xes,
|
adamc@26
|
218 fn xes' =>
|
adamc@26
|
219 (ERecord xes', loc))
|
adamc@26
|
220 | EField (e, x) =>
|
adamc@26
|
221 S.map2 (mfe ctx e,
|
adamc@26
|
222 fn e' =>
|
adamc@26
|
223 (EField (e', x), loc))
|
adamc@94
|
224
|
adamc@182
|
225 | ECase (e, pes, {disc, result}) =>
|
adamc@178
|
226 S.bind2 (mfe ctx e,
|
adamc@178
|
227 fn e' =>
|
adamc@178
|
228 S.bind2 (ListUtil.mapfold (fn (p, e) =>
|
adamc@179
|
229 let
|
adamc@179
|
230 fun pb ((p, _), ctx) =
|
adamc@179
|
231 case p of
|
adamc@179
|
232 PWild => ctx
|
adamc@182
|
233 | PVar (x, t) => bind (ctx, RelE (x, t))
|
adamc@179
|
234 | PPrim _ => ctx
|
adamc@188
|
235 | PCon (_, _, NONE) => ctx
|
adamc@188
|
236 | PCon (_, _, SOME p) => pb (p, ctx)
|
adamc@182
|
237 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
|
adamc@179
|
238 pb (p, ctx)) ctx xps
|
adamc@288
|
239 | PNone _ => ctx
|
adamc@288
|
240 | PSome (_, p) => pb (p, ctx)
|
adamc@179
|
241 in
|
adamc@179
|
242 S.map2 (mfe (pb (p, ctx)) e,
|
adamc@179
|
243 fn e' => (p, e'))
|
adamc@179
|
244 end) pes,
|
adamc@178
|
245 fn pes' =>
|
adamc@182
|
246 S.bind2 (mft disc,
|
adamc@182
|
247 fn disc' =>
|
adamc@182
|
248 S.map2 (mft result,
|
adamc@182
|
249 fn result' =>
|
adamc@182
|
250 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
|
adamc@178
|
251
|
adamc@283
|
252 | EError (e, t) =>
|
adamc@283
|
253 S.bind2 (mfe ctx e,
|
adamc@283
|
254 fn e' =>
|
adamc@283
|
255 S.map2 (mft t,
|
adamc@283
|
256 fn t' =>
|
adamc@283
|
257 (EError (e', t'), loc)))
|
adamc@741
|
258 | EReturnBlob {blob, mimeType, t} =>
|
adamc@741
|
259 S.bind2 (mfe ctx blob,
|
adamc@741
|
260 fn blob' =>
|
adamc@741
|
261 S.bind2 (mfe ctx mimeType,
|
adamc@741
|
262 fn mimeType' =>
|
adamc@741
|
263 S.map2 (mft t,
|
adamc@741
|
264 fn t' =>
|
adamc@741
|
265 (EReturnBlob {blob = blob', mimeType = mimeType', t = t'}, loc))))
|
adamc@741
|
266
|
adamc@94
|
267 | EStrcat (e1, e2) =>
|
adamc@94
|
268 S.bind2 (mfe ctx e1,
|
adamc@94
|
269 fn e1' =>
|
adamc@94
|
270 S.map2 (mfe ctx e2,
|
adamc@94
|
271 fn e2' =>
|
adamc@94
|
272 (EStrcat (e1', e2'), loc)))
|
adamc@102
|
273
|
adamc@102
|
274 | EWrite e =>
|
adamc@102
|
275 S.map2 (mfe ctx e,
|
adamc@102
|
276 fn e' =>
|
adamc@102
|
277 (EWrite e', loc))
|
adamc@106
|
278
|
adamc@106
|
279 | ESeq (e1, e2) =>
|
adamc@106
|
280 S.bind2 (mfe ctx e1,
|
adamc@106
|
281 fn e1' =>
|
adamc@106
|
282 S.map2 (mfe ctx e2,
|
adamc@106
|
283 fn e2' =>
|
adamc@106
|
284 (ESeq (e1', e2'), loc)))
|
adamc@251
|
285 | ELet (x, t, e1, e2) =>
|
adamc@251
|
286 S.bind2 (mft t,
|
adamc@251
|
287 fn t' =>
|
adamc@251
|
288 S.bind2 (mfe ctx e1,
|
adamc@251
|
289 fn e1' =>
|
adamc@252
|
290 S.map2 (mfe (bind (ctx, RelE (x, t'))) e2,
|
adamc@251
|
291 fn e2' =>
|
adamc@251
|
292 (ELet (x, t', e1', e2'), loc))))
|
adamc@111
|
293
|
adamc@111
|
294 | EClosure (n, es) =>
|
adamc@111
|
295 S.map2 (ListUtil.mapfold (mfe ctx) es,
|
adamc@111
|
296 fn es' =>
|
adamc@111
|
297 (EClosure (n, es'), loc))
|
adamc@252
|
298
|
adamc@252
|
299 | EQuery {exps, tables, state, query, body, initial} =>
|
adamc@252
|
300 S.bind2 (ListUtil.mapfold (fn (x, t) =>
|
adamc@252
|
301 S.map2 (mft t,
|
adamc@252
|
302 fn t' => (x, t'))) exps,
|
adamc@252
|
303 fn exps' =>
|
adamc@252
|
304 S.bind2 (ListUtil.mapfold (fn (x, xts) =>
|
adamc@252
|
305 S.map2 (ListUtil.mapfold
|
adamc@252
|
306 (fn (x, t) =>
|
adamc@252
|
307 S.map2 (mft t,
|
adamc@252
|
308 fn t' => (x, t'))) xts,
|
adamc@252
|
309 fn xts' => (x, xts'))) tables,
|
adamc@252
|
310 fn tables' =>
|
adamc@252
|
311 S.bind2 (mft state,
|
adamc@252
|
312 fn state' =>
|
adamc@252
|
313 S.bind2 (mfe ctx query,
|
adamc@252
|
314 fn query' =>
|
adamc@267
|
315 S.bind2 (mfe (bind (bind (ctx, RelE ("r", dummyt)),
|
adamc@267
|
316 RelE ("acc", dummyt)))
|
adamc@267
|
317 body,
|
adamc@267
|
318 fn body' =>
|
adamc@267
|
319 S.map2 (mfe ctx initial,
|
adamc@267
|
320 fn initial' =>
|
adamc@267
|
321 (EQuery {exps = exps',
|
adamc@267
|
322 tables = tables',
|
adamc@267
|
323 state = state',
|
adamc@267
|
324 query = query',
|
adamc@267
|
325 body = body',
|
adamc@267
|
326 initial = initial'},
|
adamc@267
|
327 loc)))))))
|
adamc@307
|
328
|
adamc@307
|
329 | EDml e =>
|
adamc@307
|
330 S.map2 (mfe ctx e,
|
adamc@307
|
331 fn e' =>
|
adamc@307
|
332 (EDml e', loc))
|
adamc@338
|
333 | ENextval e =>
|
adamc@338
|
334 S.map2 (mfe ctx e,
|
adamc@338
|
335 fn e' =>
|
adamc@338
|
336 (ENextval e', loc))
|
adamc@463
|
337 | EUnurlify (e, t) =>
|
adamc@463
|
338 S.bind2 (mfe ctx e,
|
adamc@463
|
339 fn e' =>
|
adamc@463
|
340 S.map2 (mft t,
|
adamc@463
|
341 fn t' =>
|
adamc@463
|
342 (EUnurlify (e', t'), loc)))
|
adamc@815
|
343 | EJavaScript (m, e) =>
|
adamc@729
|
344 S.bind2 (mfmode ctx m,
|
adamc@729
|
345 fn m' =>
|
adamc@729
|
346 S.map2 (mfe ctx e,
|
adamc@729
|
347 fn e' =>
|
adamc@815
|
348 (EJavaScript (m', e'), loc)))
|
adamc@569
|
349
|
adamc@568
|
350 | ESignalReturn e =>
|
adamc@568
|
351 S.map2 (mfe ctx e,
|
adamc@568
|
352 fn e' =>
|
adamc@568
|
353 (ESignalReturn e', loc))
|
adamc@572
|
354 | ESignalBind (e1, e2) =>
|
adamc@572
|
355 S.bind2 (mfe ctx e1,
|
adamc@572
|
356 fn e1' =>
|
adamc@572
|
357 S.map2 (mfe ctx e2,
|
adamc@572
|
358 fn e2' =>
|
adamc@572
|
359 (ESignalBind (e1', e2'), loc)))
|
adamc@574
|
360 | ESignalSource e =>
|
adamc@574
|
361 S.map2 (mfe ctx e,
|
adamc@574
|
362 fn e' =>
|
adamc@574
|
363 (ESignalSource e', loc))
|
adamc@608
|
364
|
adamc@905
|
365 | EServerCall (s, ek, t, eff, ue) =>
|
adamc@642
|
366 S.bind2 (mfe ctx s,
|
adamc@642
|
367 fn s' =>
|
adamc@642
|
368 S.bind2 (mfe ctx ek,
|
adamc@642
|
369 fn ek' =>
|
adamc@905
|
370 S.bind2 (mft t,
|
adamc@642
|
371 fn t' =>
|
adamc@905
|
372 S.map2 (mfe ctx ue,
|
adamc@905
|
373 fn ue' =>
|
adamc@905
|
374 (EServerCall (s', ek', t', eff, ue'), loc)))))
|
adamc@670
|
375 | ERecv (s, ek, t) =>
|
adamc@670
|
376 S.bind2 (mfe ctx s,
|
adamc@695
|
377 fn s' =>
|
adamc@695
|
378 S.bind2 (mfe ctx ek,
|
adamc@695
|
379 fn ek' =>
|
adamc@695
|
380 S.map2 (mft t,
|
adamc@695
|
381 fn t' =>
|
adamc@695
|
382 (ERecv (s', ek', t'), loc))))
|
adamc@695
|
383 | ESleep (s, ek) =>
|
adamc@695
|
384 S.bind2 (mfe ctx s,
|
adamc@695
|
385 fn s' =>
|
adamc@695
|
386 S.map2 (mfe ctx ek,
|
adamc@695
|
387 fn ek' =>
|
adamc@695
|
388 (ESleep (s', ek'), loc)))
|
adamc@729
|
389
|
adamc@729
|
390 and mfmode ctx mode =
|
adamc@729
|
391 case mode of
|
adamc@729
|
392 Attribute => S.return2 mode
|
adamc@729
|
393 | Script => S.return2 mode
|
adamc@729
|
394 | Source t =>
|
adamc@729
|
395 S.map2 (mft t,
|
adamc@729
|
396 fn t' => Source t')
|
adamc@26
|
397 in
|
adamc@26
|
398 mfe
|
adamc@26
|
399 end
|
adamc@26
|
400
|
adamc@26
|
401 fun mapfold {typ = fc, exp = fe} =
|
adamc@26
|
402 mapfoldB {typ = fc,
|
adamc@26
|
403 exp = fn () => fe,
|
adamc@26
|
404 bind = fn ((), _) => ()} ()
|
adamc@26
|
405
|
adamc@26
|
406 fun mapB {typ, exp, bind} ctx e =
|
adamc@26
|
407 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
|
adamc@26
|
408 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
|
adamc@26
|
409 bind = bind} ctx e () of
|
adamc@26
|
410 S.Continue (e, ()) => e
|
adamc@26
|
411 | S.Return _ => raise Fail "MonoUtil.Exp.mapB: Impossible"
|
adamc@26
|
412
|
adamc@26
|
413 fun map {typ, exp} e =
|
adamc@26
|
414 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
|
adamc@26
|
415 exp = fn e => fn () => S.Continue (exp e, ())} e () of
|
adamc@26
|
416 S.Return () => raise Fail "Mono_util.Exp.map"
|
adamc@26
|
417 | S.Continue (e, ()) => e
|
adamc@26
|
418
|
adamc@26
|
419 fun fold {typ, exp} s e =
|
adamc@26
|
420 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
|
adamc@26
|
421 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
|
adamc@26
|
422 S.Continue (_, s) => s
|
adamc@26
|
423 | S.Return _ => raise Fail "MonoUtil.Exp.fold: Impossible"
|
adamc@26
|
424
|
adamc@26
|
425 fun exists {typ, exp} k =
|
adamc@26
|
426 case mapfold {typ = fn c => fn () =>
|
adamc@26
|
427 if typ c then
|
adamc@26
|
428 S.Return ()
|
adamc@26
|
429 else
|
adamc@26
|
430 S.Continue (c, ()),
|
adamc@26
|
431 exp = fn e => fn () =>
|
adamc@26
|
432 if exp e then
|
adamc@26
|
433 S.Return ()
|
adamc@26
|
434 else
|
adamc@26
|
435 S.Continue (e, ())} k () of
|
adamc@26
|
436 S.Return _ => true
|
adamc@26
|
437 | S.Continue _ => false
|
adamc@26
|
438
|
adamc@567
|
439 fun foldB {typ, exp, bind} ctx s e =
|
adamc@567
|
440 case mapfoldB {typ = fn t => fn s => S.Continue (t, typ (t, s)),
|
adamc@567
|
441 exp = fn ctx => fn e => fn s => S.Continue (e, exp (ctx, e, s)),
|
adamc@567
|
442 bind = bind} ctx e s of
|
adamc@567
|
443 S.Continue (_, s) => s
|
adamc@567
|
444 | S.Return _ => raise Fail "MonoUtil.Exp.foldB: Impossible"
|
adamc@567
|
445
|
adamc@26
|
446 end
|
adamc@26
|
447
|
adamc@26
|
448 structure Decl = struct
|
adamc@26
|
449
|
adamc@26
|
450 datatype binder = datatype Exp.binder
|
adamc@26
|
451
|
adamc@26
|
452 fun mapfoldB {typ = fc, exp = fe, decl = fd, bind} =
|
adamc@26
|
453 let
|
adamc@26
|
454 val mft = Typ.mapfold fc
|
adamc@26
|
455
|
adamc@26
|
456 val mfe = Exp.mapfoldB {typ = fc, exp = fe, bind = bind}
|
adamc@26
|
457
|
adamc@26
|
458 fun mfd ctx d acc =
|
adamc@26
|
459 S.bindP (mfd' ctx d acc, fd ctx)
|
adamc@26
|
460
|
adamc@26
|
461 and mfd' ctx (dAll as (d, loc)) =
|
adamc@26
|
462 case d of
|
adamc@808
|
463 DDatatype dts =>
|
adamc@808
|
464 S.map2 (ListUtil.mapfold (fn (x, n, xncs) =>
|
adamc@808
|
465 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@808
|
466 case c of
|
adamc@808
|
467 NONE => S.return2 (x, n, c)
|
adamc@808
|
468 | SOME c =>
|
adamc@808
|
469 S.map2 (mft c,
|
adamc@808
|
470 fn c' => (x, n, SOME c'))) xncs,
|
adamc@808
|
471 fn xncs' => (x, n, xncs'))) dts,
|
adamc@808
|
472 fn dts' =>
|
adamc@808
|
473 (DDatatype dts', loc))
|
adamc@164
|
474 | DVal vi =>
|
adamc@126
|
475 S.map2 (mfvi ctx vi,
|
adamc@126
|
476 fn vi' =>
|
adamc@126
|
477 (DVal vi', loc))
|
adamc@126
|
478 | DValRec vis =>
|
adamc@196
|
479 let
|
adamc@196
|
480 val ctx' = foldl (fn ((x, n, t, _, s), ctx') => bind (ctx', NamedE (x, n, t, NONE, s))) ctx vis
|
adamc@196
|
481 in
|
adamc@196
|
482 S.map2 (ListUtil.mapfold (mfvi ctx') vis,
|
adamc@196
|
483 fn vis' =>
|
adamc@196
|
484 (DValRec vis', loc))
|
adamc@196
|
485 end
|
adamc@609
|
486 | DExport (ek, s, n, ts, t) =>
|
adamc@609
|
487 S.bind2 (ListUtil.mapfold mft ts,
|
adamc@120
|
488 fn ts' =>
|
adamc@609
|
489 S.map2 (mft t,
|
adamc@609
|
490 fn t' =>
|
adamc@609
|
491 (DExport (ek, s, n, ts', t'), loc)))
|
adamc@707
|
492 | DTable (s, xts, pe, ce) =>
|
adamc@707
|
493 S.bind2 (mfe ctx pe,
|
adamc@707
|
494 fn pe' =>
|
adamc@707
|
495 S.map2 (mfe ctx ce,
|
adamc@707
|
496 fn ce' =>
|
adamc@707
|
497 (DTable (s, xts, pe', ce'), loc)))
|
adamc@338
|
498 | DSequence _ => S.return2 dAll
|
adamc@754
|
499 | DView (s, xts, e) =>
|
adamc@754
|
500 S.map2 (mfe ctx e,
|
adamc@754
|
501 fn e' =>
|
adamc@754
|
502 (DView (s, xts, e'), loc))
|
adamc@271
|
503 | DDatabase _ => S.return2 dAll
|
adamc@569
|
504 | DJavaScript _ => S.return2 dAll
|
adamc@725
|
505 | DCookie _ => S.return2 dAll
|
adamc@718
|
506 | DStyle _ => S.return2 dAll
|
adamc@126
|
507
|
adamc@126
|
508 and mfvi ctx (x, n, t, e, s) =
|
adamc@126
|
509 S.bind2 (mft t,
|
adamc@126
|
510 fn t' =>
|
adamc@126
|
511 S.map2 (mfe ctx e,
|
adamc@126
|
512 fn e' =>
|
adamc@126
|
513 (x, n, t', e', s)))
|
adamc@26
|
514 in
|
adamc@26
|
515 mfd
|
adamc@26
|
516 end
|
adamc@26
|
517
|
adamc@26
|
518 fun mapfold {typ = fc, exp = fe, decl = fd} =
|
adamc@26
|
519 mapfoldB {typ = fc,
|
adamc@26
|
520 exp = fn () => fe,
|
adamc@26
|
521 decl = fn () => fd,
|
adamc@26
|
522 bind = fn ((), _) => ()} ()
|
adamc@26
|
523
|
adamc@26
|
524 fun fold {typ, exp, decl} s d =
|
adamc@26
|
525 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
|
adamc@26
|
526 exp = fn e => fn s => S.Continue (e, exp (e, s)),
|
adamc@26
|
527 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
|
adamc@26
|
528 S.Continue (_, s) => s
|
adamc@26
|
529 | S.Return _ => raise Fail "MonoUtil.Decl.fold: Impossible"
|
adamc@26
|
530
|
adamc@506
|
531 fun map {typ, exp, decl} e =
|
adamc@506
|
532 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
|
adamc@506
|
533 exp = fn e => fn () => S.Continue (exp e, ()),
|
adamc@506
|
534 decl = fn d => fn () => S.Continue (decl d, ())} e () of
|
adamc@506
|
535 S.Return () => raise Fail "MonoUtil.Decl.map: Impossible"
|
adamc@506
|
536 | S.Continue (e, ()) => e
|
adamc@506
|
537
|
adamc@567
|
538 fun foldMapB {typ, exp, decl, bind} ctx s d =
|
adamc@567
|
539 case mapfoldB {typ = fn c => fn s => S.Continue (typ (c, s)),
|
adamc@567
|
540 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
|
adamc@567
|
541 decl = fn ctx => fn d => fn s => S.Continue (decl (ctx, d, s)),
|
adamc@567
|
542 bind = bind} ctx d s of
|
adamc@567
|
543 S.Continue v => v
|
adamc@567
|
544 | S.Return _ => raise Fail "MonoUtil.Decl.foldMapB: Impossible"
|
adamc@567
|
545
|
adamc@26
|
546 end
|
adamc@26
|
547
|
adamc@26
|
548 structure File = struct
|
adamc@26
|
549
|
adamc@26
|
550 datatype binder = datatype Exp.binder
|
adamc@26
|
551
|
adamc@26
|
552 fun mapfoldB (all as {bind, ...}) =
|
adamc@26
|
553 let
|
adamc@26
|
554 val mfd = Decl.mapfoldB all
|
adamc@26
|
555
|
adamc@26
|
556 fun mff ctx ds =
|
adamc@26
|
557 case ds of
|
adamc@26
|
558 nil => S.return2 nil
|
adamc@26
|
559 | d :: ds' =>
|
adamc@26
|
560 S.bind2 (mfd ctx d,
|
adamc@26
|
561 fn d' =>
|
adamc@26
|
562 let
|
adamc@100
|
563 val ctx' =
|
adamc@26
|
564 case #1 d' of
|
adamc@808
|
565 DDatatype dts =>
|
adamc@808
|
566 foldl (fn ((x, n, xncs), ctx) =>
|
adamc@808
|
567 let
|
adamc@808
|
568 val ctx = bind (ctx, Datatype (x, n, xncs))
|
adamc@808
|
569 val t = (TDatatype (n, ref (ElabUtil.classifyDatatype xncs, xncs)),
|
adamc@808
|
570 #2 d')
|
adamc@808
|
571 in
|
adamc@808
|
572 foldl (fn ((x, n, to), ctx) =>
|
adamc@808
|
573 let
|
adamc@808
|
574 val t = case to of
|
adamc@808
|
575 NONE => t
|
adamc@808
|
576 | SOME t' => (TFun (t', t), #2 d')
|
adamc@808
|
577 in
|
adamc@808
|
578 bind (ctx, NamedE (x, n, t, NONE, ""))
|
adamc@808
|
579 end)
|
adamc@808
|
580 ctx xncs
|
adamc@808
|
581 end) ctx dts
|
adamc@164
|
582 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
|
adamc@126
|
583 | DValRec vis => foldl (fn ((x, n, t, e, s), ctx) =>
|
adamc@196
|
584 bind (ctx, NamedE (x, n, t, NONE, s))) ctx vis
|
adamc@109
|
585 | DExport _ => ctx
|
adamc@273
|
586 | DTable _ => ctx
|
adamc@338
|
587 | DSequence _ => ctx
|
adamc@754
|
588 | DView _ => ctx
|
adamc@271
|
589 | DDatabase _ => ctx
|
adamc@569
|
590 | DJavaScript _ => ctx
|
adamc@725
|
591 | DCookie _ => ctx
|
adamc@718
|
592 | DStyle _ => ctx
|
adamc@26
|
593 in
|
adamc@26
|
594 S.map2 (mff ctx' ds',
|
adamc@26
|
595 fn ds' =>
|
adamc@26
|
596 d' :: ds')
|
adamc@26
|
597 end)
|
adamc@26
|
598 in
|
adamc@26
|
599 mff
|
adamc@26
|
600 end
|
adamc@26
|
601
|
adamc@26
|
602 fun mapfold {typ = fc, exp = fe, decl = fd} =
|
adamc@26
|
603 mapfoldB {typ = fc,
|
adamc@26
|
604 exp = fn () => fe,
|
adamc@26
|
605 decl = fn () => fd,
|
adamc@26
|
606 bind = fn ((), _) => ()} ()
|
adamc@26
|
607
|
adamc@26
|
608 fun mapB {typ, exp, decl, bind} ctx ds =
|
adamc@26
|
609 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
|
adamc@26
|
610 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
|
adamc@26
|
611 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
|
adamc@26
|
612 bind = bind} ctx ds () of
|
adamc@26
|
613 S.Continue (ds, ()) => ds
|
adamc@26
|
614 | S.Return _ => raise Fail "MonoUtil.File.mapB: Impossible"
|
adamc@26
|
615
|
adamc@96
|
616 fun map {typ, exp, decl} e =
|
adamc@96
|
617 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
|
adamc@96
|
618 exp = fn e => fn () => S.Continue (exp e, ()),
|
adamc@96
|
619 decl = fn d => fn () => S.Continue (decl d, ())} e () of
|
adamc@506
|
620 S.Return () => raise Fail "MonoUtil.File.map: Impossible"
|
adamc@96
|
621 | S.Continue (e, ()) => e
|
adamc@96
|
622
|
adamc@26
|
623 fun fold {typ, exp, decl} s d =
|
adamc@26
|
624 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
|
adamc@26
|
625 exp = fn e => fn s => S.Continue (e, exp (e, s)),
|
adamc@26
|
626 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
|
adamc@26
|
627 S.Continue (_, s) => s
|
adamc@26
|
628 | S.Return _ => raise Fail "MonoUtil.File.fold: Impossible"
|
adamc@26
|
629
|
adamc@506
|
630 val maxName = foldl (fn ((d, _) : decl, count) =>
|
adamc@506
|
631 case d of
|
adamc@808
|
632 DDatatype dts =>
|
adamc@808
|
633 foldl (fn ((_, n, ns), count) =>
|
adamc@808
|
634 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@808
|
635 (Int.max (n, count)) ns) count dts
|
adamc@506
|
636 | DVal (_, n, _, _, _) => Int.max (n, count)
|
adamc@506
|
637 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
|
adamc@506
|
638 | DExport _ => count
|
adamc@506
|
639 | DTable _ => count
|
adamc@506
|
640 | DSequence _ => count
|
adamc@754
|
641 | DView _ => count
|
adamc@569
|
642 | DDatabase _ => count
|
adamc@718
|
643 | DJavaScript _ => count
|
adamc@725
|
644 | DCookie _ => count
|
adamc@718
|
645 | DStyle _ => count) 0
|
adamc@506
|
646
|
adamc@26
|
647 end
|
adamc@26
|
648
|
adamc@26
|
649 end
|