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