annotate src/core_util.sml @ 479:ffa18975e661

Broaden set of possible especializations
author Adam Chlipala <adamc@hcoop.net>
date Sat, 08 Nov 2008 14:42:52 -0500
parents 5c9606deacb6
children 9117a7bf229c
rev   line source
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@479 447 fn () => Con.compare (c1, c2))
adamc@479 448 | (ECut _, _) => LESS
adamc@479 449 | (_, ECut _) => GREATER
adamc@479 450
adamc@479 451 | (EFold _, EFold _) => EQUAL
adamc@479 452 | (EFold _, _) => LESS
adamc@479 453 | (_, EFold _) => GREATER
adamc@479 454
adamc@479 455 | (ECase (e1, pes1, _), ECase (e2, pes2, _)) =>
adamc@479 456 join (compare (e1, e2),
adamc@479 457 fn () => joinL (fn ((p1, e1), (p2, e2)) =>
adamc@479 458 join (pCompare (p1, p2),
adamc@479 459 fn () => compare (e1, e2))) (pes1, pes2))
adamc@479 460 | (ECase _, _) => LESS
adamc@479 461 | (_, ECase _) => GREATER
adamc@479 462
adamc@479 463 | (EWrite e1, EWrite e2) => compare (e1, e2)
adamc@479 464 | (EWrite _, _) => LESS
adamc@479 465 | (_, EWrite _) => GREATER
adamc@479 466
adamc@479 467 | (EClosure (n1, es1), EClosure (n2, es2)) =>
adamc@479 468 join (Int.compare (n1, n2),
adamc@479 469 fn () => joinL compare (es1, es2))
adamc@479 470 | (EClosure _, _) => LESS
adamc@479 471 | (_, EClosure _) => GREATER
adamc@479 472
adamc@479 473 | (ELet (_, _, x1, e1), ELet (_, _, x2, e2)) =>
adamc@479 474 join (compare (x1, x2),
adamc@479 475 fn () => compare (e1, e2))
adamc@479 476
adamc@16 477 datatype binder =
adamc@16 478 RelC of string * kind
adamc@20 479 | NamedC of string * int * kind * con option
adamc@16 480 | RelE of string * con
adamc@109 481 | NamedE of string * int * con * exp option * string
adamc@16 482
adamc@16 483 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@16 484 let
adamc@16 485 val mfk = Kind.mapfold fk
adamc@16 486
adamc@16 487 fun bind' (ctx, b) =
adamc@16 488 let
adamc@16 489 val b' = case b of
adamc@16 490 Con.Rel x => RelC x
adamc@16 491 | Con.Named x => NamedC x
adamc@16 492 in
adamc@16 493 bind (ctx, b')
adamc@16 494 end
adamc@16 495 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@16 496
adamc@16 497 fun mfe ctx e acc =
adamc@16 498 S.bindP (mfe' ctx e acc, fe ctx)
adamc@16 499
adamc@16 500 and mfe' ctx (eAll as (e, loc)) =
adamc@16 501 case e of
adamc@16 502 EPrim _ => S.return2 eAll
adamc@16 503 | ERel _ => S.return2 eAll
adamc@16 504 | ENamed _ => S.return2 eAll
adamc@192 505 | ECon (dk, pc, cs, NONE) =>
adamc@192 506 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 507 fn cs' =>
adamc@192 508 (ECon (dk, pc, cs', NONE), loc))
adamc@192 509 | ECon (dk, n, cs, SOME e) =>
adamc@192 510 S.bind2 (mfe ctx e,
adamc@192 511 fn e' =>
adamc@192 512 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 513 fn cs' =>
adamc@192 514 (ECon (dk, n, cs', SOME e'), loc)))
adamc@48 515 | EFfi _ => S.return2 eAll
adamc@48 516 | EFfiApp (m, x, es) =>
adamc@192 517 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@48 518 fn es' =>
adamc@48 519 (EFfiApp (m, x, es'), loc))
adamc@16 520 | EApp (e1, e2) =>
adamc@16 521 S.bind2 (mfe ctx e1,
adamc@16 522 fn e1' =>
adamc@16 523 S.map2 (mfe ctx e2,
adamc@16 524 fn e2' =>
adamc@16 525 (EApp (e1', e2'), loc)))
adamc@26 526 | EAbs (x, dom, ran, e) =>
adamc@26 527 S.bind2 (mfc ctx dom,
adamc@26 528 fn dom' =>
adamc@26 529 S.bind2 (mfc ctx ran,
adamc@26 530 fn ran' =>
adamc@26 531 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 532 fn e' =>
adamc@26 533 (EAbs (x, dom', ran', e'), loc))))
adamc@16 534
adamc@16 535 | ECApp (e, c) =>
adamc@16 536 S.bind2 (mfe ctx e,
adamc@16 537 fn e' =>
adamc@16 538 S.map2 (mfc ctx c,
adamc@16 539 fn c' =>
adamc@16 540 (ECApp (e', c'), loc)))
adamc@16 541 | ECAbs (x, k, e) =>
adamc@16 542 S.bind2 (mfk k,
adamc@16 543 fn k' =>
adamc@16 544 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@16 545 fn e' =>
adamc@16 546 (ECAbs (x, k', e'), loc)))
adamc@16 547
adamc@16 548 | ERecord xes =>
adamc@29 549 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@16 550 S.bind2 (mfc ctx x,
adamc@16 551 fn x' =>
adamc@29 552 S.bind2 (mfe ctx e,
adamc@29 553 fn e' =>
adamc@29 554 S.map2 (mfc ctx t,
adamc@29 555 fn t' =>
adamc@29 556 (x', e', t')))))
adamc@16 557 xes,
adamc@16 558 fn xes' =>
adamc@16 559 (ERecord xes', loc))
adamc@16 560 | EField (e, c, {field, rest}) =>
adamc@16 561 S.bind2 (mfe ctx e,
adamc@16 562 fn e' =>
adamc@16 563 S.bind2 (mfc ctx c,
adamc@16 564 fn c' =>
adamc@16 565 S.bind2 (mfc ctx field,
adamc@16 566 fn field' =>
adamc@16 567 S.map2 (mfc ctx rest,
adamc@16 568 fn rest' =>
adamc@16 569 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@445 570 | EConcat (e1, c1, e2, c2) =>
adamc@339 571 S.bind2 (mfe ctx e1,
adamc@339 572 fn e1' =>
adamc@445 573 S.bind2 (mfc ctx c1,
adamc@445 574 fn c1' =>
adamc@339 575 S.bind2 (mfe ctx e2,
adamc@339 576 fn e2' =>
adamc@445 577 S.map2 (mfc ctx c2,
adamc@445 578 fn c2' =>
adamc@445 579 (EConcat (e1', c1', e2', c2'),
adamc@445 580 loc)))))
adamc@149 581 | ECut (e, c, {field, rest}) =>
adamc@149 582 S.bind2 (mfe ctx e,
adamc@149 583 fn e' =>
adamc@149 584 S.bind2 (mfc ctx c,
adamc@149 585 fn c' =>
adamc@149 586 S.bind2 (mfc ctx field,
adamc@149 587 fn field' =>
adamc@149 588 S.map2 (mfc ctx rest,
adamc@149 589 fn rest' =>
adamc@149 590 (ECut (e', c', {field = field', rest = rest'}), loc)))))
adamc@73 591 | EFold k =>
adamc@73 592 S.map2 (mfk k,
adamc@73 593 fn k' =>
adamc@73 594 (EFold k', loc))
adamc@102 595
adamc@182 596 | ECase (e, pes, {disc, result}) =>
adamc@177 597 S.bind2 (mfe ctx e,
adamc@177 598 fn e' =>
adamc@177 599 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@193 600 let
adamc@193 601 fun pb ((p, _), ctx) =
adamc@193 602 case p of
adamc@193 603 PWild => ctx
adamc@193 604 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@193 605 | PPrim _ => ctx
adamc@193 606 | PCon (_, _, _, NONE) => ctx
adamc@193 607 | PCon (_, _, _, SOME p) => pb (p, ctx)
adamc@193 608 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@193 609 pb (p, ctx)) ctx xps
adamc@193 610 in
adamc@193 611 S.bind2 (mfp ctx p,
adamc@193 612 fn p' =>
adamc@193 613 S.map2 (mfe (pb (p', ctx)) e,
adamc@193 614 fn e' => (p', e')))
adamc@193 615 end) pes,
adamc@177 616 fn pes' =>
adamc@182 617 S.bind2 (mfc ctx disc,
adamc@182 618 fn disc' =>
adamc@182 619 S.map2 (mfc ctx result,
adamc@182 620 fn result' =>
adamc@182 621 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@177 622
adamc@102 623 | EWrite e =>
adamc@102 624 S.map2 (mfe ctx e,
adamc@102 625 fn e' =>
adamc@102 626 (EWrite e', loc))
adamc@110 627
adamc@110 628 | EClosure (n, es) =>
adamc@110 629 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@110 630 fn es' =>
adamc@110 631 (EClosure (n, es'), loc))
adamc@193 632
adamc@450 633 | ELet (x, t, e1, e2) =>
adamc@450 634 S.bind2 (mfc ctx t,
adamc@450 635 fn t' =>
adamc@450 636 S.bind2 (mfe ctx e1,
adamc@450 637 fn e1' =>
adamc@453 638 S.map2 (mfe (bind (ctx, RelE (x, t'))) e2,
adamc@450 639 fn e2' =>
adamc@450 640 (ELet (x, t', e1', e2'), loc))))
adamc@450 641
adamc@193 642 and mfp ctx (pAll as (p, loc)) =
adamc@193 643 case p of
adamc@193 644 PWild => S.return2 pAll
adamc@193 645 | PVar (x, t) =>
adamc@193 646 S.map2 (mfc ctx t,
adamc@193 647 fn t' =>
adamc@193 648 (PVar (x, t'), loc))
adamc@193 649 | PPrim _ => S.return2 pAll
adamc@193 650 | PCon (dk, pc, args, po) =>
adamc@193 651 S.bind2 (mfpc ctx pc,
adamc@193 652 fn pc' =>
adamc@193 653 S.bind2 (ListUtil.mapfold (mfc ctx) args,
adamc@193 654 fn args' =>
adamc@193 655 S.map2 ((case po of
adamc@193 656 NONE => S.return2 NONE
adamc@193 657 | SOME p => S.map2 (mfp ctx p, SOME)),
adamc@193 658 fn po' =>
adamc@193 659 (PCon (dk, pc', args', po'), loc))))
adamc@193 660 | PRecord xps =>
adamc@193 661 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
adamc@193 662 S.bind2 (mfp ctx p,
adamc@193 663 fn p' =>
adamc@193 664 S.map2 (mfc ctx c,
adamc@193 665 fn c' =>
adamc@193 666 (x, p', c')))) xps,
adamc@193 667 fn xps' =>
adamc@193 668 (PRecord xps', loc))
adamc@193 669
adamc@193 670 and mfpc ctx pc =
adamc@193 671 case pc of
adamc@193 672 PConVar _ => S.return2 pc
adamc@193 673 | PConFfi {mod = m, datatyp, params, con, arg, kind} =>
adamc@193 674 S.map2 ((case arg of
adamc@193 675 NONE => S.return2 NONE
adamc@193 676 | SOME c => S.map2 (mfc ctx c, SOME)),
adamc@193 677 fn arg' =>
adamc@193 678 PConFfi {mod = m, datatyp = datatyp, params = params,
adamc@193 679 con = con, arg = arg', kind = kind})
adamc@16 680 in
adamc@16 681 mfe
adamc@16 682 end
adamc@16 683
adamc@16 684 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@16 685 mapfoldB {kind = fk,
adamc@16 686 con = fn () => fc,
adamc@16 687 exp = fn () => fe,
adamc@16 688 bind = fn ((), _) => ()} ()
adamc@16 689
adamc@21 690 fun mapB {kind, con, exp, bind} ctx e =
adamc@21 691 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@21 692 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@21 693 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@21 694 bind = bind} ctx e () of
adamc@21 695 S.Continue (e, ()) => e
adamc@21 696 | S.Return _ => raise Fail "CoreUtil.Exp.mapB: Impossible"
adamc@21 697
adamc@16 698 fun map {kind, con, exp} e =
adamc@16 699 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 700 con = fn c => fn () => S.Continue (con c, ()),
adamc@16 701 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@16 702 S.Return () => raise Fail "Core_util.Exp.map"
adamc@16 703 | S.Continue (e, ()) => e
adamc@16 704
adamc@23 705 fun fold {kind, con, exp} s e =
adamc@23 706 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 707 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 708 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@23 709 S.Continue (_, s) => s
adamc@23 710 | S.Return _ => raise Fail "CoreUtil.Exp.fold: Impossible"
adamc@23 711
adamc@16 712 fun exists {kind, con, exp} k =
adamc@16 713 case mapfold {kind = fn k => fn () =>
adamc@16 714 if kind k then
adamc@16 715 S.Return ()
adamc@16 716 else
adamc@16 717 S.Continue (k, ()),
adamc@16 718 con = fn c => fn () =>
adamc@16 719 if con c then
adamc@16 720 S.Return ()
adamc@16 721 else
adamc@16 722 S.Continue (c, ()),
adamc@16 723 exp = fn e => fn () =>
adamc@16 724 if exp e then
adamc@16 725 S.Return ()
adamc@16 726 else
adamc@16 727 S.Continue (e, ())} k () of
adamc@16 728 S.Return _ => true
adamc@16 729 | S.Continue _ => false
adamc@16 730
adamc@479 731 fun existsB {kind, con, exp, bind} ctx k =
adamc@479 732 case mapfoldB {kind = fn k => fn () =>
adamc@479 733 if kind k then
adamc@479 734 S.Return ()
adamc@479 735 else
adamc@479 736 S.Continue (k, ()),
adamc@479 737 con = fn ctx => fn c => fn () =>
adamc@479 738 if con (ctx, c) then
adamc@479 739 S.Return ()
adamc@479 740 else
adamc@479 741 S.Continue (c, ()),
adamc@479 742 exp = fn ctx => fn e => fn () =>
adamc@479 743 if exp (ctx, e) then
adamc@479 744 S.Return ()
adamc@479 745 else
adamc@479 746 S.Continue (e, ()),
adamc@479 747 bind = bind} ctx k () of
adamc@479 748 S.Return _ => true
adamc@479 749 | S.Continue _ => false
adamc@479 750
adamc@443 751 fun foldMap {kind, con, exp} s e =
adamc@443 752 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@443 753 con = fn c => fn s => S.Continue (con (c, s)),
adamc@443 754 exp = fn e => fn s => S.Continue (exp (e, s))} e s of
adamc@443 755 S.Continue v => v
adamc@443 756 | S.Return _ => raise Fail "CoreUtil.Exp.foldMap: Impossible"
adamc@443 757
adamc@16 758 end
adamc@16 759
adamc@20 760 structure Decl = struct
adamc@20 761
adamc@20 762 datatype binder = datatype Exp.binder
adamc@20 763
adamc@20 764 fun mapfoldB {kind = fk, con = fc, exp = fe, decl = fd, bind} =
adamc@20 765 let
adamc@20 766 val mfk = Kind.mapfold fk
adamc@20 767
adamc@20 768 fun bind' (ctx, b) =
adamc@20 769 let
adamc@20 770 val b' = case b of
adamc@20 771 Con.Rel x => RelC x
adamc@20 772 | Con.Named x => NamedC x
adamc@20 773 in
adamc@20 774 bind (ctx, b')
adamc@20 775 end
adamc@20 776 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@20 777
adamc@20 778 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind}
adamc@20 779
adamc@20 780 fun mfd ctx d acc =
adamc@20 781 S.bindP (mfd' ctx d acc, fd ctx)
adamc@20 782
adamc@20 783 and mfd' ctx (dAll as (d, loc)) =
adamc@20 784 case d of
adamc@20 785 DCon (x, n, k, c) =>
adamc@20 786 S.bind2 (mfk k,
adamc@20 787 fn k' =>
adamc@20 788 S.map2 (mfc ctx c,
adamc@20 789 fn c' =>
adamc@20 790 (DCon (x, n, k', c'), loc)))
adamc@192 791 | DDatatype (x, n, xs, xncs) =>
adamc@196 792 let
adamc@196 793 val k = (KType, loc)
adamc@196 794 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@196 795 val ctx' = bind (ctx, NamedC (x, n, k', NONE))
adamc@196 796 in
adamc@196 797 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@196 798 case c of
adamc@196 799 NONE => S.return2 (x, n, c)
adamc@196 800 | SOME c =>
adamc@196 801 S.map2 (mfc ctx' c,
adamc@196 802 fn c' => (x, n, SOME c'))) xncs,
adamc@196 803 fn xncs' =>
adamc@196 804 (DDatatype (x, n, xs, xncs'), loc))
adamc@196 805 end
adamc@125 806 | DVal vi =>
adamc@125 807 S.map2 (mfvi ctx vi,
adamc@125 808 fn vi' =>
adamc@125 809 (DVal vi', loc))
adamc@125 810 | DValRec vis =>
adamc@127 811 let
adamc@127 812 val ctx = foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@127 813 ctx vis
adamc@127 814 in
adamc@127 815 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@127 816 fn vis' =>
adamc@127 817 (DValRec vis', loc))
adamc@127 818 end
adamc@109 819 | DExport _ => S.return2 dAll
adamc@247 820 | DTable (x, n, c, s) =>
adamc@247 821 S.map2 (mfc ctx c,
adamc@461 822 fn c' =>
adamc@461 823 (DTable (x, n, c', s), loc))
adamc@338 824 | DSequence _ => S.return2 dAll
adamc@271 825 | DDatabase _ => S.return2 dAll
adamc@461 826 | DCookie (x, n, c, s) =>
adamc@461 827 S.map2 (mfc ctx c,
adamc@461 828 fn c' =>
adamc@461 829 (DCookie (x, n, c', s), loc))
adamc@125 830
adamc@125 831 and mfvi ctx (x, n, t, e, s) =
adamc@125 832 S.bind2 (mfc ctx t,
adamc@125 833 fn t' =>
adamc@125 834 S.map2 (mfe ctx e,
adamc@125 835 fn e' =>
adamc@125 836 (x, n, t', e', s)))
adamc@20 837 in
adamc@20 838 mfd
adamc@20 839 end
adamc@20 840
adamc@23 841 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 842 mapfoldB {kind = fk,
adamc@23 843 con = fn () => fc,
adamc@23 844 exp = fn () => fe,
adamc@23 845 decl = fn () => fd,
adamc@23 846 bind = fn ((), _) => ()} ()
adamc@23 847
adamc@23 848 fun fold {kind, con, exp, decl} s d =
adamc@23 849 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 850 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 851 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 852 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 853 S.Continue (_, s) => s
adamc@23 854 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
adamc@23 855
adamc@110 856 fun foldMap {kind, con, exp, decl} s d =
adamc@110 857 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 858 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 859 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 860 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 861 S.Continue v => v
adamc@110 862 | S.Return _ => raise Fail "CoreUtil.Decl.foldMap: Impossible"
adamc@110 863
adamc@16 864 end
adamc@20 865
adamc@20 866 structure File = struct
adamc@20 867
adamc@20 868 datatype binder = datatype Exp.binder
adamc@20 869
adamc@20 870 fun mapfoldB (all as {bind, ...}) =
adamc@20 871 let
adamc@20 872 val mfd = Decl.mapfoldB all
adamc@20 873
adamc@20 874 fun mff ctx ds =
adamc@20 875 case ds of
adamc@20 876 nil => S.return2 nil
adamc@20 877 | d :: ds' =>
adamc@20 878 S.bind2 (mfd ctx d,
adamc@20 879 fn d' =>
adamc@20 880 let
adamc@100 881 val ctx' =
adamc@20 882 case #1 d' of
adamc@100 883 DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c))
adamc@192 884 | DDatatype (x, n, xs, xncs) =>
adamc@163 885 let
adamc@192 886 val loc = #2 d'
adamc@192 887 val k = (KType, loc)
adamc@192 888 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@192 889
adamc@192 890 val ctx = bind (ctx, NamedC (x, n, k', NONE))
adamc@163 891 val t = (CNamed n, #2 d')
adamc@192 892 val t = ListUtil.foldli (fn (i, _, t) => (CApp (t, (CRel i, loc)), loc))
adamc@192 893 t xs
adamc@163 894 in
adamc@163 895 foldl (fn ((x, n, to), ctx) =>
adamc@163 896 let
adamc@163 897 val t = case to of
adamc@163 898 NONE => t
adamc@163 899 | SOME t' => (TFun (t', t), #2 d')
adamc@192 900 val t = foldr (fn (x, t) => (TCFun (x, k, t), loc))
adamc@192 901 t xs
adamc@163 902 in
adamc@163 903 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@163 904 end)
adamc@163 905 ctx xncs
adamc@163 906 end
adamc@109 907 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@125 908 | DValRec vis =>
adamc@127 909 foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@125 910 ctx vis
adamc@109 911 | DExport _ => ctx
adamc@247 912 | DTable (x, n, c, s) =>
adamc@247 913 let
adamc@338 914 val t = (CApp ((CFfi ("Basis", "sql_table"), #2 d'), c), #2 d')
adamc@338 915 in
adamc@338 916 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@338 917 end
adamc@338 918 | DSequence (x, n, s) =>
adamc@338 919 let
adamc@338 920 val t = (CFfi ("Basis", "sql_sequence"), #2 d')
adamc@247 921 in
adamc@247 922 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@247 923 end
adamc@271 924 | DDatabase _ => ctx
adamc@461 925 | DCookie (x, n, c, s) =>
adamc@461 926 let
adamc@461 927 val t = (CApp ((CFfi ("Basis", "http_cookie"), #2 d'), c), #2 d')
adamc@461 928 in
adamc@461 929 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@461 930 end
adamc@20 931 in
adamc@20 932 S.map2 (mff ctx' ds',
adamc@20 933 fn ds' =>
adamc@20 934 d' :: ds')
adamc@20 935 end)
adamc@20 936 in
adamc@20 937 mff
adamc@20 938 end
adamc@20 939
adamc@23 940 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 941 mapfoldB {kind = fk,
adamc@23 942 con = fn () => fc,
adamc@23 943 exp = fn () => fe,
adamc@23 944 decl = fn () => fd,
adamc@23 945 bind = fn ((), _) => ()} ()
adamc@23 946
adamc@20 947 fun mapB {kind, con, exp, decl, bind} ctx ds =
adamc@20 948 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@20 949 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@20 950 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@20 951 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@20 952 bind = bind} ctx ds () of
adamc@20 953 S.Continue (ds, ()) => ds
adamc@20 954 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
adamc@20 955
adamc@23 956 fun fold {kind, con, exp, decl} s d =
adamc@23 957 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 958 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 959 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 960 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 961 S.Continue (_, s) => s
adamc@23 962 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
adamc@23 963
adamc@110 964 fun foldMap {kind, con, exp, decl} s d =
adamc@110 965 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 966 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 967 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 968 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 969 S.Continue v => v
adamc@110 970 | S.Return _ => raise Fail "CoreUtil.File.foldMap: Impossible"
adamc@110 971
adamc@179 972 val maxName = foldl (fn ((d, _) : decl, count) =>
adamc@179 973 case d of
adamc@179 974 DCon (_, n, _, _) => Int.max (n, count)
adamc@398 975 | DDatatype (_, n, _, ns) =>
adamc@398 976 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@398 977 (Int.max (n, count)) ns
adamc@179 978 | DVal (_, n, _, _, _) => Int.max (n, count)
adamc@179 979 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
adamc@247 980 | DExport _ => count
adamc@271 981 | DTable (_, n, _, _) => Int.max (n, count)
adamc@338 982 | DSequence (_, n, _) => Int.max (n, count)
adamc@461 983 | DDatabase _ => count
adamc@461 984 | DCookie (_, n, _, _) => Int.max (n, count)) 0
adamc@179 985
adamc@20 986 end
adamc@20 987
adamc@20 988 end