annotate src/core_util.sml @ 502:8875ff2e85dc

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