adamc@16
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@16
|
2 * All rights reserved.
|
adamc@16
|
3 *
|
adamc@16
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@16
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@16
|
6 *
|
adamc@16
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@16
|
8 * this list of conditions and the following disclaimer.
|
adamc@16
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@16
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@16
|
11 * and/or other materials provided with the distribution.
|
adamc@16
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@16
|
13 * derived from this software without specific prior written permission.
|
adamc@16
|
14 *
|
adamc@16
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@16
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@16
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@16
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@16
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@16
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@16
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@16
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@16
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@16
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@16
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@16
|
26 *)
|
adamc@16
|
27
|
adamc@16
|
28 structure CoreUtil :> CORE_UTIL = struct
|
adamc@16
|
29
|
adamc@16
|
30 open Core
|
adamc@16
|
31
|
adamc@16
|
32 structure S = Search
|
adamc@16
|
33
|
adamc@16
|
34 structure Kind = struct
|
adamc@16
|
35
|
adamc@193
|
36 open Order
|
adamc@193
|
37
|
adamc@193
|
38 fun compare ((k1, _), (k2, _)) =
|
adamc@193
|
39 case (k1, k2) of
|
adamc@193
|
40 (KType, KType) => EQUAL
|
adamc@193
|
41 | (KType, _) => LESS
|
adamc@193
|
42 | (_, KType) => GREATER
|
adamc@193
|
43
|
adamc@193
|
44 | (KArrow (d1, r1), KArrow (d2, r2)) => join (compare (d1, d2), fn () => compare (r1, r2))
|
adamc@193
|
45 | (KArrow _, _) => LESS
|
adamc@193
|
46 | (_, KArrow _) => GREATER
|
adamc@193
|
47
|
adamc@193
|
48 | (KName, KName) => EQUAL
|
adamc@193
|
49 | (KName, _) => LESS
|
adamc@193
|
50 | (_, KName) => GREATER
|
adamc@193
|
51
|
adamc@193
|
52 | (KRecord k1, KRecord k2) => compare (k1, k2)
|
adamc@193
|
53 | (KRecord _, _) => LESS
|
adamc@193
|
54 | (_, KRecord _) => GREATER
|
adamc@193
|
55
|
adamc@193
|
56 | (KUnit, KUnit) => EQUAL
|
adamc@214
|
57 | (KUnit, _) => LESS
|
adamc@214
|
58 | (_, KUnit) => GREATER
|
adamc@214
|
59
|
adamc@214
|
60 | (KTuple ks1, KTuple ks2) => joinL compare (ks1, ks2)
|
adamc@193
|
61
|
adamc@16
|
62 fun mapfold f =
|
adamc@16
|
63 let
|
adamc@16
|
64 fun mfk k acc =
|
adamc@16
|
65 S.bindP (mfk' k acc, f)
|
adamc@16
|
66
|
adamc@16
|
67 and mfk' (kAll as (k, loc)) =
|
adamc@16
|
68 case k of
|
adamc@16
|
69 KType => S.return2 kAll
|
adamc@16
|
70
|
adamc@16
|
71 | KArrow (k1, k2) =>
|
adamc@16
|
72 S.bind2 (mfk k1,
|
adamc@16
|
73 fn k1' =>
|
adamc@16
|
74 S.map2 (mfk k2,
|
adamc@16
|
75 fn k2' =>
|
adamc@16
|
76 (KArrow (k1', k2'), loc)))
|
adamc@16
|
77
|
adamc@16
|
78 | KName => S.return2 kAll
|
adamc@16
|
79
|
adamc@16
|
80 | KRecord k =>
|
adamc@16
|
81 S.map2 (mfk k,
|
adamc@16
|
82 fn k' =>
|
adamc@16
|
83 (KRecord k', loc))
|
adamc@87
|
84
|
adamc@87
|
85 | KUnit => S.return2 kAll
|
adamc@214
|
86
|
adamc@214
|
87 | KTuple ks =>
|
adamc@214
|
88 S.map2 (ListUtil.mapfold mfk ks,
|
adamc@214
|
89 fn ks' =>
|
adamc@214
|
90 (KTuple ks', loc))
|
adamc@16
|
91 in
|
adamc@16
|
92 mfk
|
adamc@16
|
93 end
|
adamc@16
|
94
|
adamc@16
|
95 fun map f k =
|
adamc@16
|
96 case mapfold (fn k => fn () => S.Continue (f k, ())) k () of
|
adamc@16
|
97 S.Return () => raise Fail "Core_util.Kind.map"
|
adamc@16
|
98 | S.Continue (k, ()) => k
|
adamc@16
|
99
|
adamc@16
|
100 fun exists f k =
|
adamc@16
|
101 case mapfold (fn k => fn () =>
|
adamc@16
|
102 if f k then
|
adamc@16
|
103 S.Return ()
|
adamc@16
|
104 else
|
adamc@16
|
105 S.Continue (k, ())) k () of
|
adamc@16
|
106 S.Return _ => true
|
adamc@16
|
107 | S.Continue _ => false
|
adamc@16
|
108
|
adamc@16
|
109 end
|
adamc@16
|
110
|
adamc@16
|
111 structure Con = struct
|
adamc@16
|
112
|
adamc@193
|
113 open Order
|
adamc@193
|
114
|
adamc@193
|
115 fun compare ((c1, _), (c2, _)) =
|
adamc@193
|
116 case (c1, c2) of
|
adamc@193
|
117 (TFun (d1, r1), TFun (d2, r2)) => join (compare (d1, d2), fn () => compare (r1, r2))
|
adamc@193
|
118 | (TFun _, _) => LESS
|
adamc@193
|
119 | (_, TFun _) => GREATER
|
adamc@193
|
120
|
adamc@193
|
121 | (TCFun (x1, k1, r1), TCFun (x2, k2, r2)) =>
|
adamc@193
|
122 join (String.compare (x1, x2),
|
adamc@193
|
123 fn () => join (Kind.compare (k1, k2),
|
adamc@193
|
124 fn () => compare (r1, r2)))
|
adamc@193
|
125 | (TCFun _, _) => LESS
|
adamc@193
|
126 | (_, TCFun _) => GREATER
|
adamc@193
|
127
|
adamc@193
|
128 | (TRecord c1, TRecord c2) => compare (c1, c2)
|
adamc@193
|
129 | (TRecord _, _) => LESS
|
adamc@193
|
130 | (_, TRecord _) => GREATER
|
adamc@193
|
131
|
adamc@193
|
132 | (CRel n1, CRel n2) => Int.compare (n1, n2)
|
adamc@193
|
133 | (CRel _, _) => LESS
|
adamc@193
|
134 | (_, CRel _) => GREATER
|
adamc@193
|
135
|
adamc@193
|
136 | (CNamed n1, CNamed n2) => Int.compare (n1, n2)
|
adamc@193
|
137 | (CNamed _, _) => LESS
|
adamc@193
|
138 | (_, CNamed _) => GREATER
|
adamc@193
|
139
|
adamc@193
|
140 | (CFfi (m1, s1), CFfi (m2, s2)) => join (String.compare (m1, m2),
|
adamc@193
|
141 fn () => String.compare (s1, s2))
|
adamc@193
|
142 | (CFfi _, _) => LESS
|
adamc@193
|
143 | (_, CFfi _) => GREATER
|
adamc@193
|
144
|
adamc@193
|
145 | (CApp (f1, x1), CApp (f2, x2)) => join (compare (f1, f2),
|
adamc@193
|
146 fn () => compare (x1, x2))
|
adamc@193
|
147 | (CApp _, _) => LESS
|
adamc@193
|
148 | (_, CApp _) => GREATER
|
adamc@193
|
149
|
adamc@193
|
150 | (CAbs (x1, k1, b1), CAbs (x2, k2, b2)) =>
|
adamc@193
|
151 join (String.compare (x1, x2),
|
adamc@193
|
152 fn () => join (Kind.compare (k1, k2),
|
adamc@193
|
153 fn () => compare (b1, b2)))
|
adamc@193
|
154 | (CAbs _, _) => LESS
|
adamc@193
|
155 | (_, CAbs _) => GREATER
|
adamc@193
|
156
|
adamc@193
|
157 | (CName s1, CName s2) => String.compare (s1, s2)
|
adamc@193
|
158 | (CName _, _) => LESS
|
adamc@193
|
159 | (_, CName _) => GREATER
|
adamc@193
|
160
|
adamc@193
|
161 | (CRecord (k1, xvs1), CRecord (k2, xvs2)) =>
|
adamc@193
|
162 join (Kind.compare (k1, k2),
|
adamc@250
|
163 fn () =>
|
adamc@250
|
164 let
|
adamc@250
|
165 val sort = ListMergeSort.sort (fn ((x1, _), (x2, _)) =>
|
adamc@250
|
166 compare (x1, x2) = GREATER)
|
adamc@250
|
167 in
|
adamc@250
|
168 joinL (fn ((x1, v1), (x2, v2)) =>
|
adamc@250
|
169 join (compare (x1, x2),
|
adamc@250
|
170 fn () => compare (v1, v2))) (sort xvs1, sort xvs2)
|
adamc@250
|
171 end)
|
adamc@193
|
172 | (CRecord _, _) => LESS
|
adamc@193
|
173 | (_, CRecord _) => GREATER
|
adamc@193
|
174
|
adamc@193
|
175 | (CConcat (f1, s1), CConcat (f2, s2)) =>
|
adamc@193
|
176 join (compare (f1, f2),
|
adamc@193
|
177 fn () => compare (s1, s2))
|
adamc@193
|
178 | (CConcat _, _) => LESS
|
adamc@193
|
179 | (_, CConcat _) => GREATER
|
adamc@193
|
180
|
adamc@193
|
181 | (CFold (d1, r1), CFold (d2, r2)) =>
|
adamc@193
|
182 join (Kind.compare (d1, r2),
|
adamc@193
|
183 fn () => Kind.compare (r1, r2))
|
adamc@193
|
184 | (CFold _, _) => LESS
|
adamc@193
|
185 | (_, CFold _) => GREATER
|
adamc@193
|
186
|
adamc@193
|
187 | (CUnit, CUnit) => EQUAL
|
adamc@214
|
188 | (CUnit, _) => LESS
|
adamc@214
|
189 | (_, CUnit) => GREATER
|
adamc@214
|
190
|
adamc@214
|
191 | (CTuple cs1, CTuple cs2) => joinL compare (cs1, cs2)
|
adamc@214
|
192 | (CTuple _, _) => LESS
|
adamc@214
|
193 | (_, CTuple _) => GREATER
|
adamc@214
|
194
|
adamc@214
|
195 | (CProj (c1, n1), CProj (c2, n2)) => join (Int.compare (n1, n2),
|
adamc@214
|
196 fn () => compare (c1, c2))
|
adamc@193
|
197
|
adamc@16
|
198 datatype binder =
|
adamc@16
|
199 Rel of string * kind
|
adamc@20
|
200 | Named of string * int * kind * con option
|
adamc@16
|
201
|
adamc@16
|
202 fun mapfoldB {kind = fk, con = fc, bind} =
|
adamc@16
|
203 let
|
adamc@16
|
204 val mfk = Kind.mapfold fk
|
adamc@16
|
205
|
adamc@16
|
206 fun mfc ctx c acc =
|
adamc@16
|
207 S.bindP (mfc' ctx c acc, fc ctx)
|
adamc@16
|
208
|
adamc@16
|
209 and mfc' ctx (cAll as (c, loc)) =
|
adamc@16
|
210 case c of
|
adamc@16
|
211 TFun (c1, c2) =>
|
adamc@16
|
212 S.bind2 (mfc ctx c1,
|
adamc@16
|
213 fn c1' =>
|
adamc@16
|
214 S.map2 (mfc ctx c2,
|
adamc@16
|
215 fn c2' =>
|
adamc@16
|
216 (TFun (c1', c2'), loc)))
|
adamc@16
|
217 | TCFun (x, k, c) =>
|
adamc@16
|
218 S.bind2 (mfk k,
|
adamc@16
|
219 fn k' =>
|
adamc@16
|
220 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
|
adamc@16
|
221 fn c' =>
|
adamc@16
|
222 (TCFun (x, k', c'), loc)))
|
adamc@16
|
223 | TRecord c =>
|
adamc@16
|
224 S.map2 (mfc ctx c,
|
adamc@16
|
225 fn c' =>
|
adamc@16
|
226 (TRecord c', loc))
|
adamc@16
|
227
|
adamc@16
|
228 | CRel _ => S.return2 cAll
|
adamc@16
|
229 | CNamed _ => S.return2 cAll
|
adamc@48
|
230 | CFfi _ => S.return2 cAll
|
adamc@16
|
231 | CApp (c1, c2) =>
|
adamc@16
|
232 S.bind2 (mfc ctx c1,
|
adamc@16
|
233 fn c1' =>
|
adamc@16
|
234 S.map2 (mfc ctx c2,
|
adamc@16
|
235 fn c2' =>
|
adamc@16
|
236 (CApp (c1', c2'), loc)))
|
adamc@16
|
237 | CAbs (x, k, c) =>
|
adamc@16
|
238 S.bind2 (mfk k,
|
adamc@16
|
239 fn k' =>
|
adamc@16
|
240 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
|
adamc@16
|
241 fn c' =>
|
adamc@16
|
242 (CAbs (x, k', c'), loc)))
|
adamc@16
|
243
|
adamc@16
|
244 | CName _ => S.return2 cAll
|
adamc@16
|
245
|
adamc@16
|
246 | CRecord (k, xcs) =>
|
adamc@16
|
247 S.bind2 (mfk k,
|
adamc@16
|
248 fn k' =>
|
adamc@16
|
249 S.map2 (ListUtil.mapfold (fn (x, c) =>
|
adamc@16
|
250 S.bind2 (mfc ctx x,
|
adamc@16
|
251 fn x' =>
|
adamc@16
|
252 S.map2 (mfc ctx c,
|
adamc@16
|
253 fn c' =>
|
adamc@16
|
254 (x', c'))))
|
adamc@16
|
255 xcs,
|
adamc@16
|
256 fn xcs' =>
|
adamc@16
|
257 (CRecord (k', xcs'), loc)))
|
adamc@16
|
258 | CConcat (c1, c2) =>
|
adamc@16
|
259 S.bind2 (mfc ctx c1,
|
adamc@16
|
260 fn c1' =>
|
adamc@16
|
261 S.map2 (mfc ctx c2,
|
adamc@16
|
262 fn c2' =>
|
adamc@16
|
263 (CConcat (c1', c2'), loc)))
|
adamc@69
|
264 | CFold (k1, k2) =>
|
adamc@69
|
265 S.bind2 (mfk k1,
|
adamc@69
|
266 fn k1' =>
|
adamc@69
|
267 S.map2 (mfk k2,
|
adamc@69
|
268 fn k2' =>
|
adamc@69
|
269 (CFold (k1', k2'), loc)))
|
adamc@87
|
270
|
adamc@87
|
271 | CUnit => S.return2 cAll
|
adamc@214
|
272
|
adamc@214
|
273 | CTuple cs =>
|
adamc@214
|
274 S.map2 (ListUtil.mapfold (mfc ctx) cs,
|
adamc@214
|
275 fn cs' =>
|
adamc@214
|
276 (CTuple cs', loc))
|
adamc@214
|
277
|
adamc@214
|
278 | CProj (c, n) =>
|
adamc@214
|
279 S.map2 (mfc ctx c,
|
adamc@214
|
280 fn c' =>
|
adamc@214
|
281 (CProj (c', n), loc))
|
adamc@16
|
282 in
|
adamc@16
|
283 mfc
|
adamc@16
|
284 end
|
adamc@16
|
285
|
adamc@16
|
286 fun mapfold {kind = fk, con = fc} =
|
adamc@16
|
287 mapfoldB {kind = fk,
|
adamc@16
|
288 con = fn () => fc,
|
adamc@16
|
289 bind = fn ((), _) => ()} ()
|
adamc@16
|
290
|
adamc@16
|
291 fun map {kind, con} c =
|
adamc@16
|
292 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@16
|
293 con = fn c => fn () => S.Continue (con c, ())} c () of
|
adamc@16
|
294 S.Return () => raise Fail "Core_util.Con.map"
|
adamc@16
|
295 | S.Continue (c, ()) => c
|
adamc@16
|
296
|
adamc@16
|
297 fun mapB {kind, con, bind} ctx c =
|
adamc@16
|
298 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@16
|
299 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@16
|
300 bind = bind} ctx c () of
|
adamc@16
|
301 S.Continue (c, ()) => c
|
adamc@20
|
302 | S.Return _ => raise Fail "CoreUtil.Con.mapB: Impossible"
|
adamc@16
|
303
|
adamc@23
|
304 fun fold {kind, con} s c =
|
adamc@23
|
305 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
|
adamc@23
|
306 con = fn c => fn s => S.Continue (c, con (c, s))} c s of
|
adamc@23
|
307 S.Continue (_, s) => s
|
adamc@23
|
308 | S.Return _ => raise Fail "CoreUtil.Con.fold: Impossible"
|
adamc@23
|
309
|
adamc@16
|
310 fun exists {kind, con} k =
|
adamc@16
|
311 case mapfold {kind = fn k => fn () =>
|
adamc@16
|
312 if kind k then
|
adamc@16
|
313 S.Return ()
|
adamc@16
|
314 else
|
adamc@16
|
315 S.Continue (k, ()),
|
adamc@16
|
316 con = fn c => fn () =>
|
adamc@16
|
317 if con c then
|
adamc@16
|
318 S.Return ()
|
adamc@16
|
319 else
|
adamc@16
|
320 S.Continue (c, ())} k () of
|
adamc@16
|
321 S.Return _ => true
|
adamc@16
|
322 | S.Continue _ => false
|
adamc@16
|
323
|
adamc@193
|
324 fun foldMap {kind, con} s c =
|
adamc@193
|
325 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
|
adamc@193
|
326 con = fn c => fn s => S.Continue (con (c, s))} c s of
|
adamc@193
|
327 S.Continue v => v
|
adamc@193
|
328 | S.Return _ => raise Fail "CoreUtil.Con.foldMap: Impossible"
|
adamc@193
|
329
|
adamc@16
|
330 end
|
adamc@16
|
331
|
adamc@16
|
332 structure Exp = struct
|
adamc@16
|
333
|
adamc@479
|
334 open Order
|
adamc@479
|
335
|
adamc@479
|
336 fun pcCompare (pc1, pc2) =
|
adamc@479
|
337 case (pc1, pc2) of
|
adamc@479
|
338 (PConVar n1, PConVar n2) => Int.compare (n1, n2)
|
adamc@479
|
339 | (PConVar _, _) => LESS
|
adamc@479
|
340 | (_, PConVar _) => GREATER
|
adamc@479
|
341
|
adamc@479
|
342 | (PConFfi {mod = m1, datatyp = d1, con = c1, ...},
|
adamc@479
|
343 PConFfi {mod = m2, datatyp = d2, con = c2, ...}) =>
|
adamc@479
|
344 join (String.compare (m1, m2),
|
adamc@479
|
345 fn () => join (String.compare (d1, d2),
|
adamc@479
|
346 fn () => String.compare (c1, c2)))
|
adamc@479
|
347
|
adamc@479
|
348 fun pCompare ((p1, _), (p2, _)) =
|
adamc@479
|
349 case (p1, p2) of
|
adamc@479
|
350 (PWild, PWild) => EQUAL
|
adamc@479
|
351 | (PWild, _) => LESS
|
adamc@479
|
352 | (_, PWild) => GREATER
|
adamc@479
|
353
|
adamc@479
|
354 | (PVar _, PVar _) => EQUAL
|
adamc@479
|
355 | (PVar _, _) => LESS
|
adamc@479
|
356 | (_, PVar _) => GREATER
|
adamc@479
|
357
|
adamc@479
|
358 | (PPrim p1, PPrim p2) => Prim.compare (p1, p2)
|
adamc@479
|
359 | (PPrim _, _) => LESS
|
adamc@479
|
360 | (_, PPrim _) => GREATER
|
adamc@479
|
361
|
adamc@479
|
362 | (PCon (_, pc1, _, po1), PCon (_, pc2, _, po2)) =>
|
adamc@479
|
363 join (pcCompare (pc1, pc2),
|
adamc@479
|
364 fn () => joinO pCompare (po1, po2))
|
adamc@479
|
365 | (PCon _, _) => LESS
|
adamc@479
|
366 | (_, PCon _) => GREATER
|
adamc@479
|
367
|
adamc@479
|
368 | (PRecord xps1, PRecord xps2) =>
|
adamc@479
|
369 joinL (fn ((x1, p1, _), (x2, p2, _)) =>
|
adamc@479
|
370 join (String.compare (x1, x2),
|
adamc@479
|
371 fn () => pCompare (p1, p2))) (xps1, xps2)
|
adamc@479
|
372
|
adamc@479
|
373 fun compare ((e1, _), (e2, _)) =
|
adamc@479
|
374 case (e1, e2) of
|
adamc@479
|
375 (EPrim p1, EPrim p2) => Prim.compare (p1, p2)
|
adamc@479
|
376 | (EPrim _, _) => LESS
|
adamc@479
|
377 | (_, EPrim _) => GREATER
|
adamc@479
|
378
|
adamc@479
|
379 | (ERel n1, ERel n2) => Int.compare (n1, n2)
|
adamc@479
|
380 | (ERel _, _) => LESS
|
adamc@479
|
381 | (_, ERel _) => GREATER
|
adamc@479
|
382
|
adamc@479
|
383 | (ENamed n1, ENamed n2) => Int.compare (n1, n2)
|
adamc@479
|
384 | (ENamed _, _) => LESS
|
adamc@479
|
385 | (_, ENamed _) => GREATER
|
adamc@479
|
386
|
adamc@479
|
387 | (ECon (_, pc1, _, eo1), ECon (_, pc2, _, eo2)) =>
|
adamc@479
|
388 join (pcCompare (pc1, pc2),
|
adamc@479
|
389 fn () => joinO compare (eo1, eo2))
|
adamc@479
|
390 | (ECon _, _) => LESS
|
adamc@479
|
391 | (_, ECon _) => GREATER
|
adamc@479
|
392
|
adamc@479
|
393 | (EFfi (f1, x1), EFfi (f2, x2)) =>
|
adamc@479
|
394 join (String.compare (f1, f2),
|
adamc@479
|
395 fn () => String.compare (x1, x2))
|
adamc@479
|
396 | (EFfi _, _) => LESS
|
adamc@479
|
397 | (_, EFfi _) => GREATER
|
adamc@479
|
398
|
adamc@479
|
399 | (EFfiApp (f1, x1, es1), EFfiApp (f2, x2, es2)) =>
|
adamc@479
|
400 join (String.compare (f1, f2),
|
adamc@479
|
401 fn () => join (String.compare (x1, x2),
|
adamc@479
|
402 fn () => joinL compare (es1, es2)))
|
adamc@479
|
403 | (EFfiApp _, _) => LESS
|
adamc@479
|
404 | (_, EFfiApp _) => GREATER
|
adamc@479
|
405
|
adamc@479
|
406 | (EApp (f1, x1), EApp (f2, x2)) =>
|
adamc@479
|
407 join (compare (f1, f2),
|
adamc@479
|
408 fn () => compare (x1, x2))
|
adamc@479
|
409 | (EApp _, _) => LESS
|
adamc@479
|
410 | (_, EApp _) => GREATER
|
adamc@479
|
411
|
adamc@479
|
412 | (EAbs (_, _, _, e1), EAbs (_, _, _, e2)) => compare (e1, e2)
|
adamc@479
|
413 | (EAbs _, _) => LESS
|
adamc@479
|
414 | (_, EAbs _) => GREATER
|
adamc@479
|
415
|
adamc@479
|
416 | (ECApp (f1, x1), ECApp (f2, x2)) =>
|
adamc@479
|
417 join (compare (f1, f2),
|
adamc@479
|
418 fn () => Con.compare (x1, x2))
|
adamc@479
|
419 | (ECApp _, _) => LESS
|
adamc@479
|
420 | (_, ECApp _) => GREATER
|
adamc@479
|
421
|
adamc@479
|
422 | (ECAbs (_, _, e1), ECAbs (_, _, e2)) => compare (e1, e2)
|
adamc@479
|
423 | (ECAbs _, _) => LESS
|
adamc@479
|
424 | (_, ECAbs _) => GREATER
|
adamc@479
|
425
|
adamc@479
|
426 | (ERecord xes1, ERecord xes2) =>
|
adamc@479
|
427 joinL (fn ((x1, e1, _), (x2, e2, _)) =>
|
adamc@479
|
428 join (Con.compare (x1, x2),
|
adamc@479
|
429 fn () => compare (e1, e2))) (xes1, xes2)
|
adamc@479
|
430 | (ERecord _, _) => LESS
|
adamc@479
|
431 | (_, ERecord _) => GREATER
|
adamc@479
|
432
|
adamc@479
|
433 | (EField (e1, c1, _), EField (e2, c2, _)) =>
|
adamc@479
|
434 join (compare (e1, e2),
|
adamc@479
|
435 fn () => Con.compare (c1, c2))
|
adamc@479
|
436 | (EField _, _) => LESS
|
adamc@479
|
437 | (_, EField _) => GREATER
|
adamc@479
|
438
|
adamc@479
|
439 | (EConcat (x1, _, y1, _), EConcat (x2, _, y2, _)) =>
|
adamc@479
|
440 join (compare (x1, x2),
|
adamc@479
|
441 fn () => compare (y1, y2))
|
adamc@479
|
442 | (EConcat _, _) => LESS
|
adamc@479
|
443 | (_, EConcat _) => GREATER
|
adamc@479
|
444
|
adamc@479
|
445 | (ECut (e1, c1, _), ECut (e2, c2, _)) =>
|
adamc@479
|
446 join (compare (e1, e2),
|
adamc@493
|
447 fn () => Con.compare (c1, c2))
|
adamc@479
|
448 | (ECut _, _) => LESS
|
adamc@479
|
449 | (_, ECut _) => GREATER
|
adamc@479
|
450
|
adamc@493
|
451 | (ECutMulti (e1, c1, _), ECutMulti (e2, c2, _)) =>
|
adamc@493
|
452 join (compare (e1, e2),
|
adamc@493
|
453 fn () => Con.compare (c1, c2))
|
adamc@493
|
454 | (ECutMulti _, _) => LESS
|
adamc@493
|
455 | (_, ECutMulti _) => GREATER
|
adamc@493
|
456
|
adamc@479
|
457 | (EFold _, EFold _) => EQUAL
|
adamc@479
|
458 | (EFold _, _) => LESS
|
adamc@479
|
459 | (_, EFold _) => GREATER
|
adamc@479
|
460
|
adamc@479
|
461 | (ECase (e1, pes1, _), ECase (e2, pes2, _)) =>
|
adamc@479
|
462 join (compare (e1, e2),
|
adamc@479
|
463 fn () => joinL (fn ((p1, e1), (p2, e2)) =>
|
adamc@479
|
464 join (pCompare (p1, p2),
|
adamc@479
|
465 fn () => compare (e1, e2))) (pes1, pes2))
|
adamc@479
|
466 | (ECase _, _) => LESS
|
adamc@479
|
467 | (_, ECase _) => GREATER
|
adamc@479
|
468
|
adamc@479
|
469 | (EWrite e1, EWrite e2) => compare (e1, e2)
|
adamc@479
|
470 | (EWrite _, _) => LESS
|
adamc@479
|
471 | (_, EWrite _) => GREATER
|
adamc@479
|
472
|
adamc@479
|
473 | (EClosure (n1, es1), EClosure (n2, es2)) =>
|
adamc@479
|
474 join (Int.compare (n1, n2),
|
adamc@479
|
475 fn () => joinL compare (es1, es2))
|
adamc@479
|
476 | (EClosure _, _) => LESS
|
adamc@479
|
477 | (_, EClosure _) => GREATER
|
adamc@479
|
478
|
adamc@479
|
479 | (ELet (_, _, x1, e1), ELet (_, _, x2, e2)) =>
|
adamc@479
|
480 join (compare (x1, x2),
|
adamc@479
|
481 fn () => compare (e1, e2))
|
adamc@607
|
482 | (ELet _, _) => LESS
|
adamc@607
|
483 | (_, ELet _) => GREATER
|
adamc@607
|
484
|
adamc@607
|
485 | (EServerCall (n1, es1, e1), EServerCall (n2, es2, e2)) =>
|
adamc@607
|
486 join (Int.compare (n1, n2),
|
adamc@607
|
487 fn () => join (joinL compare (es1, es2),
|
adamc@607
|
488 fn () => compare (e1, e2)))
|
adamc@479
|
489
|
adamc@16
|
490 datatype binder =
|
adamc@16
|
491 RelC of string * kind
|
adamc@20
|
492 | NamedC of string * int * kind * con option
|
adamc@16
|
493 | RelE of string * con
|
adamc@109
|
494 | NamedE of string * int * con * exp option * string
|
adamc@16
|
495
|
adamc@16
|
496 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
|
adamc@16
|
497 let
|
adamc@16
|
498 val mfk = Kind.mapfold fk
|
adamc@16
|
499
|
adamc@16
|
500 fun bind' (ctx, b) =
|
adamc@16
|
501 let
|
adamc@16
|
502 val b' = case b of
|
adamc@16
|
503 Con.Rel x => RelC x
|
adamc@16
|
504 | Con.Named x => NamedC x
|
adamc@16
|
505 in
|
adamc@16
|
506 bind (ctx, b')
|
adamc@16
|
507 end
|
adamc@16
|
508 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@16
|
509
|
adamc@16
|
510 fun mfe ctx e acc =
|
adamc@16
|
511 S.bindP (mfe' ctx e acc, fe ctx)
|
adamc@16
|
512
|
adamc@16
|
513 and mfe' ctx (eAll as (e, loc)) =
|
adamc@16
|
514 case e of
|
adamc@16
|
515 EPrim _ => S.return2 eAll
|
adamc@16
|
516 | ERel _ => S.return2 eAll
|
adamc@16
|
517 | ENamed _ => S.return2 eAll
|
adamc@192
|
518 | ECon (dk, pc, cs, NONE) =>
|
adamc@192
|
519 S.map2 (ListUtil.mapfold (mfc ctx) cs,
|
adamc@192
|
520 fn cs' =>
|
adamc@192
|
521 (ECon (dk, pc, cs', NONE), loc))
|
adamc@192
|
522 | ECon (dk, n, cs, SOME e) =>
|
adamc@192
|
523 S.bind2 (mfe ctx e,
|
adamc@192
|
524 fn e' =>
|
adamc@192
|
525 S.map2 (ListUtil.mapfold (mfc ctx) cs,
|
adamc@192
|
526 fn cs' =>
|
adamc@192
|
527 (ECon (dk, n, cs', SOME e'), loc)))
|
adamc@48
|
528 | EFfi _ => S.return2 eAll
|
adamc@48
|
529 | EFfiApp (m, x, es) =>
|
adamc@192
|
530 S.map2 (ListUtil.mapfold (mfe ctx) es,
|
adamc@48
|
531 fn es' =>
|
adamc@48
|
532 (EFfiApp (m, x, es'), loc))
|
adamc@16
|
533 | EApp (e1, e2) =>
|
adamc@16
|
534 S.bind2 (mfe ctx e1,
|
adamc@16
|
535 fn e1' =>
|
adamc@16
|
536 S.map2 (mfe ctx e2,
|
adamc@16
|
537 fn e2' =>
|
adamc@16
|
538 (EApp (e1', e2'), loc)))
|
adamc@26
|
539 | EAbs (x, dom, ran, e) =>
|
adamc@26
|
540 S.bind2 (mfc ctx dom,
|
adamc@26
|
541 fn dom' =>
|
adamc@26
|
542 S.bind2 (mfc ctx ran,
|
adamc@26
|
543 fn ran' =>
|
adamc@26
|
544 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
|
adamc@26
|
545 fn e' =>
|
adamc@26
|
546 (EAbs (x, dom', ran', e'), loc))))
|
adamc@16
|
547
|
adamc@16
|
548 | ECApp (e, c) =>
|
adamc@16
|
549 S.bind2 (mfe ctx e,
|
adamc@16
|
550 fn e' =>
|
adamc@16
|
551 S.map2 (mfc ctx c,
|
adamc@16
|
552 fn c' =>
|
adamc@16
|
553 (ECApp (e', c'), loc)))
|
adamc@16
|
554 | ECAbs (x, k, e) =>
|
adamc@16
|
555 S.bind2 (mfk k,
|
adamc@16
|
556 fn k' =>
|
adamc@16
|
557 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
|
adamc@16
|
558 fn e' =>
|
adamc@16
|
559 (ECAbs (x, k', e'), loc)))
|
adamc@16
|
560
|
adamc@16
|
561 | ERecord xes =>
|
adamc@29
|
562 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
|
adamc@16
|
563 S.bind2 (mfc ctx x,
|
adamc@16
|
564 fn x' =>
|
adamc@29
|
565 S.bind2 (mfe ctx e,
|
adamc@29
|
566 fn e' =>
|
adamc@29
|
567 S.map2 (mfc ctx t,
|
adamc@29
|
568 fn t' =>
|
adamc@29
|
569 (x', e', t')))))
|
adamc@16
|
570 xes,
|
adamc@16
|
571 fn xes' =>
|
adamc@16
|
572 (ERecord xes', loc))
|
adamc@16
|
573 | EField (e, c, {field, rest}) =>
|
adamc@16
|
574 S.bind2 (mfe ctx e,
|
adamc@16
|
575 fn e' =>
|
adamc@16
|
576 S.bind2 (mfc ctx c,
|
adamc@16
|
577 fn c' =>
|
adamc@16
|
578 S.bind2 (mfc ctx field,
|
adamc@16
|
579 fn field' =>
|
adamc@16
|
580 S.map2 (mfc ctx rest,
|
adamc@16
|
581 fn rest' =>
|
adamc@16
|
582 (EField (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@445
|
583 | EConcat (e1, c1, e2, c2) =>
|
adamc@339
|
584 S.bind2 (mfe ctx e1,
|
adamc@339
|
585 fn e1' =>
|
adamc@445
|
586 S.bind2 (mfc ctx c1,
|
adamc@445
|
587 fn c1' =>
|
adamc@339
|
588 S.bind2 (mfe ctx e2,
|
adamc@339
|
589 fn e2' =>
|
adamc@445
|
590 S.map2 (mfc ctx c2,
|
adamc@445
|
591 fn c2' =>
|
adamc@445
|
592 (EConcat (e1', c1', e2', c2'),
|
adamc@445
|
593 loc)))))
|
adamc@149
|
594 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
595 S.bind2 (mfe ctx e,
|
adamc@149
|
596 fn e' =>
|
adamc@149
|
597 S.bind2 (mfc ctx c,
|
adamc@149
|
598 fn c' =>
|
adamc@149
|
599 S.bind2 (mfc ctx field,
|
adamc@149
|
600 fn field' =>
|
adamc@149
|
601 S.map2 (mfc ctx rest,
|
adamc@149
|
602 fn rest' =>
|
adamc@149
|
603 (ECut (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@493
|
604 | ECutMulti (e, c, {rest}) =>
|
adamc@493
|
605 S.bind2 (mfe ctx e,
|
adamc@493
|
606 fn e' =>
|
adamc@493
|
607 S.bind2 (mfc ctx c,
|
adamc@493
|
608 fn c' =>
|
adamc@493
|
609 S.map2 (mfc ctx rest,
|
adamc@493
|
610 fn rest' =>
|
adamc@493
|
611 (ECutMulti (e', c', {rest = rest'}), loc))))
|
adamc@73
|
612 | EFold k =>
|
adamc@73
|
613 S.map2 (mfk k,
|
adamc@73
|
614 fn k' =>
|
adamc@73
|
615 (EFold k', loc))
|
adamc@102
|
616
|
adamc@182
|
617 | ECase (e, pes, {disc, result}) =>
|
adamc@177
|
618 S.bind2 (mfe ctx e,
|
adamc@177
|
619 fn e' =>
|
adamc@177
|
620 S.bind2 (ListUtil.mapfold (fn (p, e) =>
|
adamc@193
|
621 let
|
adamc@193
|
622 fun pb ((p, _), ctx) =
|
adamc@193
|
623 case p of
|
adamc@193
|
624 PWild => ctx
|
adamc@193
|
625 | PVar (x, t) => bind (ctx, RelE (x, t))
|
adamc@193
|
626 | PPrim _ => ctx
|
adamc@193
|
627 | PCon (_, _, _, NONE) => ctx
|
adamc@193
|
628 | PCon (_, _, _, SOME p) => pb (p, ctx)
|
adamc@193
|
629 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
|
adamc@193
|
630 pb (p, ctx)) ctx xps
|
adamc@193
|
631 in
|
adamc@193
|
632 S.bind2 (mfp ctx p,
|
adamc@193
|
633 fn p' =>
|
adamc@193
|
634 S.map2 (mfe (pb (p', ctx)) e,
|
adamc@193
|
635 fn e' => (p', e')))
|
adamc@193
|
636 end) pes,
|
adamc@177
|
637 fn pes' =>
|
adamc@182
|
638 S.bind2 (mfc ctx disc,
|
adamc@182
|
639 fn disc' =>
|
adamc@182
|
640 S.map2 (mfc ctx result,
|
adamc@182
|
641 fn result' =>
|
adamc@182
|
642 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
|
adamc@177
|
643
|
adamc@102
|
644 | EWrite e =>
|
adamc@102
|
645 S.map2 (mfe ctx e,
|
adamc@102
|
646 fn e' =>
|
adamc@102
|
647 (EWrite e', loc))
|
adamc@110
|
648
|
adamc@110
|
649 | EClosure (n, es) =>
|
adamc@110
|
650 S.map2 (ListUtil.mapfold (mfe ctx) es,
|
adamc@110
|
651 fn es' =>
|
adamc@110
|
652 (EClosure (n, es'), loc))
|
adamc@193
|
653
|
adamc@450
|
654 | ELet (x, t, e1, e2) =>
|
adamc@450
|
655 S.bind2 (mfc ctx t,
|
adamc@450
|
656 fn t' =>
|
adamc@450
|
657 S.bind2 (mfe ctx e1,
|
adamc@450
|
658 fn e1' =>
|
adamc@453
|
659 S.map2 (mfe (bind (ctx, RelE (x, t'))) e2,
|
adamc@450
|
660 fn e2' =>
|
adamc@450
|
661 (ELet (x, t', e1', e2'), loc))))
|
adamc@450
|
662
|
adamc@607
|
663 | EServerCall (n, es, e) =>
|
adamc@607
|
664 S.bind2 (ListUtil.mapfold (mfe ctx) es,
|
adamc@607
|
665 fn es' =>
|
adamc@607
|
666 S.map2 (mfe ctx e,
|
adamc@607
|
667 fn e' =>
|
adamc@607
|
668 (EServerCall (n, es', e'), loc)))
|
adamc@607
|
669
|
adamc@193
|
670 and mfp ctx (pAll as (p, loc)) =
|
adamc@193
|
671 case p of
|
adamc@193
|
672 PWild => S.return2 pAll
|
adamc@193
|
673 | PVar (x, t) =>
|
adamc@193
|
674 S.map2 (mfc ctx t,
|
adamc@193
|
675 fn t' =>
|
adamc@193
|
676 (PVar (x, t'), loc))
|
adamc@193
|
677 | PPrim _ => S.return2 pAll
|
adamc@193
|
678 | PCon (dk, pc, args, po) =>
|
adamc@193
|
679 S.bind2 (mfpc ctx pc,
|
adamc@193
|
680 fn pc' =>
|
adamc@193
|
681 S.bind2 (ListUtil.mapfold (mfc ctx) args,
|
adamc@193
|
682 fn args' =>
|
adamc@193
|
683 S.map2 ((case po of
|
adamc@193
|
684 NONE => S.return2 NONE
|
adamc@193
|
685 | SOME p => S.map2 (mfp ctx p, SOME)),
|
adamc@193
|
686 fn po' =>
|
adamc@193
|
687 (PCon (dk, pc', args', po'), loc))))
|
adamc@193
|
688 | PRecord xps =>
|
adamc@193
|
689 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
|
adamc@193
|
690 S.bind2 (mfp ctx p,
|
adamc@193
|
691 fn p' =>
|
adamc@193
|
692 S.map2 (mfc ctx c,
|
adamc@193
|
693 fn c' =>
|
adamc@193
|
694 (x, p', c')))) xps,
|
adamc@193
|
695 fn xps' =>
|
adamc@193
|
696 (PRecord xps', loc))
|
adamc@193
|
697
|
adamc@193
|
698 and mfpc ctx pc =
|
adamc@193
|
699 case pc of
|
adamc@193
|
700 PConVar _ => S.return2 pc
|
adamc@193
|
701 | PConFfi {mod = m, datatyp, params, con, arg, kind} =>
|
adamc@193
|
702 S.map2 ((case arg of
|
adamc@193
|
703 NONE => S.return2 NONE
|
adamc@193
|
704 | SOME c => S.map2 (mfc ctx c, SOME)),
|
adamc@193
|
705 fn arg' =>
|
adamc@193
|
706 PConFfi {mod = m, datatyp = datatyp, params = params,
|
adamc@193
|
707 con = con, arg = arg', kind = kind})
|
adamc@16
|
708 in
|
adamc@16
|
709 mfe
|
adamc@16
|
710 end
|
adamc@16
|
711
|
adamc@16
|
712 fun mapfold {kind = fk, con = fc, exp = fe} =
|
adamc@16
|
713 mapfoldB {kind = fk,
|
adamc@16
|
714 con = fn () => fc,
|
adamc@16
|
715 exp = fn () => fe,
|
adamc@16
|
716 bind = fn ((), _) => ()} ()
|
adamc@16
|
717
|
adamc@21
|
718 fun mapB {kind, con, exp, bind} ctx e =
|
adamc@21
|
719 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@21
|
720 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@21
|
721 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
|
adamc@21
|
722 bind = bind} ctx e () of
|
adamc@21
|
723 S.Continue (e, ()) => e
|
adamc@21
|
724 | S.Return _ => raise Fail "CoreUtil.Exp.mapB: Impossible"
|
adamc@21
|
725
|
adamc@16
|
726 fun map {kind, con, exp} e =
|
adamc@16
|
727 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@16
|
728 con = fn c => fn () => S.Continue (con c, ()),
|
adamc@16
|
729 exp = fn e => fn () => S.Continue (exp e, ())} e () of
|
adamc@16
|
730 S.Return () => raise Fail "Core_util.Exp.map"
|
adamc@16
|
731 | S.Continue (e, ()) => e
|
adamc@16
|
732
|
adamc@23
|
733 fun fold {kind, con, exp} s e =
|
adamc@23
|
734 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
|
adamc@23
|
735 con = fn c => fn s => S.Continue (c, con (c, s)),
|
adamc@23
|
736 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
|
adamc@23
|
737 S.Continue (_, s) => s
|
adamc@23
|
738 | S.Return _ => raise Fail "CoreUtil.Exp.fold: Impossible"
|
adamc@23
|
739
|
adamc@484
|
740 fun foldB {kind, con, exp, bind} ctx s e =
|
adamc@484
|
741 case mapfoldB {kind = fn k => fn s => S.Continue (k, kind (k, s)),
|
adamc@484
|
742 con = fn ctx => fn c => fn s => S.Continue (c, con (ctx, c, s)),
|
adamc@484
|
743 exp = fn ctx => fn e => fn s => S.Continue (e, exp (ctx, e, s)),
|
adamc@484
|
744 bind = bind} ctx e s of
|
adamc@484
|
745 S.Continue (_, s) => s
|
adamc@484
|
746 | S.Return _ => raise Fail "CoreUtil.Exp.foldB: Impossible"
|
adamc@484
|
747
|
adamc@16
|
748 fun exists {kind, con, exp} k =
|
adamc@16
|
749 case mapfold {kind = fn k => fn () =>
|
adamc@16
|
750 if kind k then
|
adamc@16
|
751 S.Return ()
|
adamc@16
|
752 else
|
adamc@16
|
753 S.Continue (k, ()),
|
adamc@16
|
754 con = fn c => fn () =>
|
adamc@16
|
755 if con c then
|
adamc@16
|
756 S.Return ()
|
adamc@16
|
757 else
|
adamc@16
|
758 S.Continue (c, ()),
|
adamc@16
|
759 exp = fn e => fn () =>
|
adamc@16
|
760 if exp e then
|
adamc@16
|
761 S.Return ()
|
adamc@16
|
762 else
|
adamc@16
|
763 S.Continue (e, ())} k () of
|
adamc@16
|
764 S.Return _ => true
|
adamc@16
|
765 | S.Continue _ => false
|
adamc@16
|
766
|
adamc@479
|
767 fun existsB {kind, con, exp, bind} ctx k =
|
adamc@479
|
768 case mapfoldB {kind = fn k => fn () =>
|
adamc@479
|
769 if kind k then
|
adamc@479
|
770 S.Return ()
|
adamc@479
|
771 else
|
adamc@479
|
772 S.Continue (k, ()),
|
adamc@479
|
773 con = fn ctx => fn c => fn () =>
|
adamc@479
|
774 if con (ctx, c) then
|
adamc@479
|
775 S.Return ()
|
adamc@479
|
776 else
|
adamc@479
|
777 S.Continue (c, ()),
|
adamc@479
|
778 exp = fn ctx => fn e => fn () =>
|
adamc@479
|
779 if exp (ctx, e) then
|
adamc@479
|
780 S.Return ()
|
adamc@479
|
781 else
|
adamc@479
|
782 S.Continue (e, ()),
|
adamc@479
|
783 bind = bind} ctx k () of
|
adamc@479
|
784 S.Return _ => true
|
adamc@479
|
785 | S.Continue _ => false
|
adamc@479
|
786
|
adamc@443
|
787 fun foldMap {kind, con, exp} s e =
|
adamc@443
|
788 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
|
adamc@443
|
789 con = fn c => fn s => S.Continue (con (c, s)),
|
adamc@443
|
790 exp = fn e => fn s => S.Continue (exp (e, s))} e s of
|
adamc@443
|
791 S.Continue v => v
|
adamc@443
|
792 | S.Return _ => raise Fail "CoreUtil.Exp.foldMap: Impossible"
|
adamc@443
|
793
|
adamc@488
|
794 fun foldMapB {kind, con, exp, bind} ctx s e =
|
adamc@488
|
795 case mapfoldB {kind = fn k => fn s => S.Continue (kind (k, s)),
|
adamc@488
|
796 con = fn ctx => fn c => fn s => S.Continue (con (ctx, c, s)),
|
adamc@488
|
797 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
|
adamc@488
|
798 bind = bind} ctx e s of
|
adamc@488
|
799 S.Continue v => v
|
adamc@488
|
800 | S.Return _ => raise Fail "CoreUtil.Exp.foldMapB: Impossible"
|
adamc@488
|
801
|
adamc@16
|
802 end
|
adamc@16
|
803
|
adamc@20
|
804 structure Decl = struct
|
adamc@20
|
805
|
adamc@20
|
806 datatype binder = datatype Exp.binder
|
adamc@20
|
807
|
adamc@20
|
808 fun mapfoldB {kind = fk, con = fc, exp = fe, decl = fd, bind} =
|
adamc@20
|
809 let
|
adamc@20
|
810 val mfk = Kind.mapfold fk
|
adamc@20
|
811
|
adamc@20
|
812 fun bind' (ctx, b) =
|
adamc@20
|
813 let
|
adamc@20
|
814 val b' = case b of
|
adamc@20
|
815 Con.Rel x => RelC x
|
adamc@20
|
816 | Con.Named x => NamedC x
|
adamc@20
|
817 in
|
adamc@20
|
818 bind (ctx, b')
|
adamc@20
|
819 end
|
adamc@20
|
820 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@20
|
821
|
adamc@20
|
822 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind}
|
adamc@20
|
823
|
adamc@20
|
824 fun mfd ctx d acc =
|
adamc@20
|
825 S.bindP (mfd' ctx d acc, fd ctx)
|
adamc@20
|
826
|
adamc@20
|
827 and mfd' ctx (dAll as (d, loc)) =
|
adamc@20
|
828 case d of
|
adamc@20
|
829 DCon (x, n, k, c) =>
|
adamc@20
|
830 S.bind2 (mfk k,
|
adamc@20
|
831 fn k' =>
|
adamc@20
|
832 S.map2 (mfc ctx c,
|
adamc@20
|
833 fn c' =>
|
adamc@20
|
834 (DCon (x, n, k', c'), loc)))
|
adamc@192
|
835 | DDatatype (x, n, xs, xncs) =>
|
adamc@196
|
836 let
|
adamc@196
|
837 val k = (KType, loc)
|
adamc@196
|
838 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
|
adamc@196
|
839 val ctx' = bind (ctx, NamedC (x, n, k', NONE))
|
adamc@196
|
840 in
|
adamc@196
|
841 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@196
|
842 case c of
|
adamc@196
|
843 NONE => S.return2 (x, n, c)
|
adamc@196
|
844 | SOME c =>
|
adamc@196
|
845 S.map2 (mfc ctx' c,
|
adamc@196
|
846 fn c' => (x, n, SOME c'))) xncs,
|
adamc@196
|
847 fn xncs' =>
|
adamc@196
|
848 (DDatatype (x, n, xs, xncs'), loc))
|
adamc@196
|
849 end
|
adamc@125
|
850 | DVal vi =>
|
adamc@125
|
851 S.map2 (mfvi ctx vi,
|
adamc@125
|
852 fn vi' =>
|
adamc@125
|
853 (DVal vi', loc))
|
adamc@125
|
854 | DValRec vis =>
|
adamc@127
|
855 let
|
adamc@127
|
856 val ctx = foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
|
adamc@127
|
857 ctx vis
|
adamc@127
|
858 in
|
adamc@127
|
859 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@127
|
860 fn vis' =>
|
adamc@127
|
861 (DValRec vis', loc))
|
adamc@127
|
862 end
|
adamc@109
|
863 | DExport _ => S.return2 dAll
|
adamc@247
|
864 | DTable (x, n, c, s) =>
|
adamc@247
|
865 S.map2 (mfc ctx c,
|
adamc@461
|
866 fn c' =>
|
adamc@461
|
867 (DTable (x, n, c', s), loc))
|
adamc@338
|
868 | DSequence _ => S.return2 dAll
|
adamc@271
|
869 | DDatabase _ => S.return2 dAll
|
adamc@461
|
870 | DCookie (x, n, c, s) =>
|
adamc@461
|
871 S.map2 (mfc ctx c,
|
adamc@461
|
872 fn c' =>
|
adamc@461
|
873 (DCookie (x, n, c', s), loc))
|
adamc@125
|
874
|
adamc@125
|
875 and mfvi ctx (x, n, t, e, s) =
|
adamc@125
|
876 S.bind2 (mfc ctx t,
|
adamc@125
|
877 fn t' =>
|
adamc@125
|
878 S.map2 (mfe ctx e,
|
adamc@125
|
879 fn e' =>
|
adamc@125
|
880 (x, n, t', e', s)))
|
adamc@20
|
881 in
|
adamc@20
|
882 mfd
|
adamc@20
|
883 end
|
adamc@20
|
884
|
adamc@23
|
885 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
|
adamc@23
|
886 mapfoldB {kind = fk,
|
adamc@23
|
887 con = fn () => fc,
|
adamc@23
|
888 exp = fn () => fe,
|
adamc@23
|
889 decl = fn () => fd,
|
adamc@23
|
890 bind = fn ((), _) => ()} ()
|
adamc@23
|
891
|
adamc@23
|
892 fun fold {kind, con, exp, decl} s d =
|
adamc@23
|
893 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
|
adamc@23
|
894 con = fn c => fn s => S.Continue (c, con (c, s)),
|
adamc@23
|
895 exp = fn e => fn s => S.Continue (e, exp (e, s)),
|
adamc@23
|
896 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
|
adamc@23
|
897 S.Continue (_, s) => s
|
adamc@23
|
898 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
|
adamc@23
|
899
|
adamc@110
|
900 fun foldMap {kind, con, exp, decl} s d =
|
adamc@110
|
901 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
|
adamc@110
|
902 con = fn c => fn s => S.Continue (con (c, s)),
|
adamc@110
|
903 exp = fn e => fn s => S.Continue (exp (e, s)),
|
adamc@110
|
904 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
|
adamc@110
|
905 S.Continue v => v
|
adamc@110
|
906 | S.Return _ => raise Fail "CoreUtil.Decl.foldMap: Impossible"
|
adamc@110
|
907
|
adamc@484
|
908 fun foldMapB {kind, con, exp, decl, bind} ctx s d =
|
adamc@484
|
909 case mapfoldB {kind = fn k => fn s => S.Continue (kind (k, s)),
|
adamc@484
|
910 con = fn ctx => fn c => fn s => S.Continue (con (ctx, c, s)),
|
adamc@484
|
911 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
|
adamc@484
|
912 decl = fn ctx => fn d => fn s => S.Continue (decl (ctx, d, s)),
|
adamc@484
|
913 bind = bind} ctx d s of
|
adamc@484
|
914 S.Continue v => v
|
adamc@484
|
915 | S.Return _ => raise Fail "CoreUtil.Decl.foldMapB: Impossible"
|
adamc@484
|
916
|
adamc@522
|
917 fun exists {kind, con, exp, decl} d =
|
adamc@522
|
918 case mapfold {kind = fn k => fn () =>
|
adamc@522
|
919 if kind k then
|
adamc@522
|
920 S.Return ()
|
adamc@522
|
921 else
|
adamc@522
|
922 S.Continue (k, ()),
|
adamc@522
|
923 con = fn c => fn () =>
|
adamc@522
|
924 if con c then
|
adamc@522
|
925 S.Return ()
|
adamc@522
|
926 else
|
adamc@522
|
927 S.Continue (c, ()),
|
adamc@522
|
928 exp = fn e => fn () =>
|
adamc@522
|
929 if exp e then
|
adamc@522
|
930 S.Return ()
|
adamc@522
|
931 else
|
adamc@522
|
932 S.Continue (e, ()),
|
adamc@522
|
933 decl = fn d => fn () =>
|
adamc@522
|
934 if decl d then
|
adamc@522
|
935 S.Return ()
|
adamc@522
|
936 else
|
adamc@522
|
937 S.Continue (d, ())} d () of
|
adamc@522
|
938 S.Return _ => true
|
adamc@522
|
939 | S.Continue _ => false
|
adamc@522
|
940
|
adamc@16
|
941 end
|
adamc@20
|
942
|
adamc@20
|
943 structure File = struct
|
adamc@20
|
944
|
adamc@20
|
945 datatype binder = datatype Exp.binder
|
adamc@20
|
946
|
adamc@20
|
947 fun mapfoldB (all as {bind, ...}) =
|
adamc@20
|
948 let
|
adamc@20
|
949 val mfd = Decl.mapfoldB all
|
adamc@20
|
950
|
adamc@20
|
951 fun mff ctx ds =
|
adamc@20
|
952 case ds of
|
adamc@20
|
953 nil => S.return2 nil
|
adamc@20
|
954 | d :: ds' =>
|
adamc@20
|
955 S.bind2 (mfd ctx d,
|
adamc@20
|
956 fn d' =>
|
adamc@20
|
957 let
|
adamc@100
|
958 val ctx' =
|
adamc@20
|
959 case #1 d' of
|
adamc@100
|
960 DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c))
|
adamc@192
|
961 | DDatatype (x, n, xs, xncs) =>
|
adamc@163
|
962 let
|
adamc@192
|
963 val loc = #2 d'
|
adamc@192
|
964 val k = (KType, loc)
|
adamc@192
|
965 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
|
adamc@192
|
966
|
adamc@192
|
967 val ctx = bind (ctx, NamedC (x, n, k', NONE))
|
adamc@163
|
968 val t = (CNamed n, #2 d')
|
adamc@192
|
969 val t = ListUtil.foldli (fn (i, _, t) => (CApp (t, (CRel i, loc)), loc))
|
adamc@192
|
970 t xs
|
adamc@163
|
971 in
|
adamc@163
|
972 foldl (fn ((x, n, to), ctx) =>
|
adamc@163
|
973 let
|
adamc@163
|
974 val t = case to of
|
adamc@163
|
975 NONE => t
|
adamc@163
|
976 | SOME t' => (TFun (t', t), #2 d')
|
adamc@192
|
977 val t = foldr (fn (x, t) => (TCFun (x, k, t), loc))
|
adamc@192
|
978 t xs
|
adamc@163
|
979 in
|
adamc@163
|
980 bind (ctx, NamedE (x, n, t, NONE, ""))
|
adamc@163
|
981 end)
|
adamc@163
|
982 ctx xncs
|
adamc@163
|
983 end
|
adamc@109
|
984 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
|
adamc@125
|
985 | DValRec vis =>
|
adamc@127
|
986 foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
|
adamc@125
|
987 ctx vis
|
adamc@109
|
988 | DExport _ => ctx
|
adamc@247
|
989 | DTable (x, n, c, s) =>
|
adamc@247
|
990 let
|
adamc@338
|
991 val t = (CApp ((CFfi ("Basis", "sql_table"), #2 d'), c), #2 d')
|
adamc@338
|
992 in
|
adamc@338
|
993 bind (ctx, NamedE (x, n, t, NONE, s))
|
adamc@338
|
994 end
|
adamc@338
|
995 | DSequence (x, n, s) =>
|
adamc@338
|
996 let
|
adamc@338
|
997 val t = (CFfi ("Basis", "sql_sequence"), #2 d')
|
adamc@247
|
998 in
|
adamc@247
|
999 bind (ctx, NamedE (x, n, t, NONE, s))
|
adamc@247
|
1000 end
|
adamc@271
|
1001 | DDatabase _ => ctx
|
adamc@461
|
1002 | DCookie (x, n, c, s) =>
|
adamc@461
|
1003 let
|
adamc@461
|
1004 val t = (CApp ((CFfi ("Basis", "http_cookie"), #2 d'), c), #2 d')
|
adamc@461
|
1005 in
|
adamc@461
|
1006 bind (ctx, NamedE (x, n, t, NONE, s))
|
adamc@461
|
1007 end
|
adamc@20
|
1008 in
|
adamc@20
|
1009 S.map2 (mff ctx' ds',
|
adamc@20
|
1010 fn ds' =>
|
adamc@20
|
1011 d' :: ds')
|
adamc@20
|
1012 end)
|
adamc@20
|
1013 in
|
adamc@20
|
1014 mff
|
adamc@20
|
1015 end
|
adamc@20
|
1016
|
adamc@23
|
1017 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
|
adamc@23
|
1018 mapfoldB {kind = fk,
|
adamc@23
|
1019 con = fn () => fc,
|
adamc@23
|
1020 exp = fn () => fe,
|
adamc@23
|
1021 decl = fn () => fd,
|
adamc@23
|
1022 bind = fn ((), _) => ()} ()
|
adamc@23
|
1023
|
adamc@20
|
1024 fun mapB {kind, con, exp, decl, bind} ctx ds =
|
adamc@20
|
1025 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@20
|
1026 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@20
|
1027 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
|
adamc@20
|
1028 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
|
adamc@20
|
1029 bind = bind} ctx ds () of
|
adamc@20
|
1030 S.Continue (ds, ()) => ds
|
adamc@20
|
1031 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
|
adamc@20
|
1032
|
adamc@482
|
1033 fun map {kind, con, exp, decl} ds =
|
adamc@482
|
1034 mapB {kind = kind,
|
adamc@482
|
1035 con = fn () => con,
|
adamc@482
|
1036 exp = fn () => exp,
|
adamc@482
|
1037 decl = fn () => decl,
|
adamc@482
|
1038 bind = fn _ => ()} () ds
|
adamc@482
|
1039
|
adamc@23
|
1040 fun fold {kind, con, exp, decl} s d =
|
adamc@23
|
1041 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
|
adamc@23
|
1042 con = fn c => fn s => S.Continue (c, con (c, s)),
|
adamc@23
|
1043 exp = fn e => fn s => S.Continue (e, exp (e, s)),
|
adamc@23
|
1044 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
|
adamc@23
|
1045 S.Continue (_, s) => s
|
adamc@23
|
1046 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
|
adamc@23
|
1047
|
adamc@110
|
1048 fun foldMap {kind, con, exp, decl} s d =
|
adamc@110
|
1049 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
|
adamc@110
|
1050 con = fn c => fn s => S.Continue (con (c, s)),
|
adamc@110
|
1051 exp = fn e => fn s => S.Continue (exp (e, s)),
|
adamc@110
|
1052 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
|
adamc@110
|
1053 S.Continue v => v
|
adamc@110
|
1054 | S.Return _ => raise Fail "CoreUtil.File.foldMap: Impossible"
|
adamc@110
|
1055
|
adamc@179
|
1056 val maxName = foldl (fn ((d, _) : decl, count) =>
|
adamc@179
|
1057 case d of
|
adamc@179
|
1058 DCon (_, n, _, _) => Int.max (n, count)
|
adamc@398
|
1059 | DDatatype (_, n, _, ns) =>
|
adamc@398
|
1060 foldl (fn ((_, n', _), m) => Int.max (n', m))
|
adamc@398
|
1061 (Int.max (n, count)) ns
|
adamc@179
|
1062 | DVal (_, n, _, _, _) => Int.max (n, count)
|
adamc@179
|
1063 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
|
adamc@247
|
1064 | DExport _ => count
|
adamc@271
|
1065 | DTable (_, n, _, _) => Int.max (n, count)
|
adamc@338
|
1066 | DSequence (_, n, _) => Int.max (n, count)
|
adamc@461
|
1067 | DDatabase _ => count
|
adamc@461
|
1068 | DCookie (_, n, _, _) => Int.max (n, count)) 0
|
adamc@179
|
1069
|
adamc@20
|
1070 end
|
adamc@20
|
1071
|
adamc@20
|
1072 end
|