annotate src/core_util.sml @ 488:5521bb0b4014

Get preliminary ThreadedBlog working
author Adam Chlipala <adamc@hcoop.net>
date Tue, 11 Nov 2008 15:12:24 -0500
parents 685b41e85634
children ae03d09043c1
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@484 712 fun foldB {kind, con, exp, bind} ctx s e =
adamc@484 713 case mapfoldB {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@484 714 con = fn ctx => fn c => fn s => S.Continue (c, con (ctx, c, s)),
adamc@484 715 exp = fn ctx => fn e => fn s => S.Continue (e, exp (ctx, e, s)),
adamc@484 716 bind = bind} ctx e s of
adamc@484 717 S.Continue (_, s) => s
adamc@484 718 | S.Return _ => raise Fail "CoreUtil.Exp.foldB: Impossible"
adamc@484 719
adamc@16 720 fun exists {kind, con, exp} k =
adamc@16 721 case mapfold {kind = fn k => fn () =>
adamc@16 722 if kind k then
adamc@16 723 S.Return ()
adamc@16 724 else
adamc@16 725 S.Continue (k, ()),
adamc@16 726 con = fn c => fn () =>
adamc@16 727 if con c then
adamc@16 728 S.Return ()
adamc@16 729 else
adamc@16 730 S.Continue (c, ()),
adamc@16 731 exp = fn e => fn () =>
adamc@16 732 if exp e then
adamc@16 733 S.Return ()
adamc@16 734 else
adamc@16 735 S.Continue (e, ())} k () of
adamc@16 736 S.Return _ => true
adamc@16 737 | S.Continue _ => false
adamc@16 738
adamc@479 739 fun existsB {kind, con, exp, bind} ctx k =
adamc@479 740 case mapfoldB {kind = fn k => fn () =>
adamc@479 741 if kind k then
adamc@479 742 S.Return ()
adamc@479 743 else
adamc@479 744 S.Continue (k, ()),
adamc@479 745 con = fn ctx => fn c => fn () =>
adamc@479 746 if con (ctx, c) then
adamc@479 747 S.Return ()
adamc@479 748 else
adamc@479 749 S.Continue (c, ()),
adamc@479 750 exp = fn ctx => fn e => fn () =>
adamc@479 751 if exp (ctx, e) then
adamc@479 752 S.Return ()
adamc@479 753 else
adamc@479 754 S.Continue (e, ()),
adamc@479 755 bind = bind} ctx k () of
adamc@479 756 S.Return _ => true
adamc@479 757 | S.Continue _ => false
adamc@479 758
adamc@443 759 fun foldMap {kind, con, exp} s e =
adamc@443 760 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@443 761 con = fn c => fn s => S.Continue (con (c, s)),
adamc@443 762 exp = fn e => fn s => S.Continue (exp (e, s))} e s of
adamc@443 763 S.Continue v => v
adamc@443 764 | S.Return _ => raise Fail "CoreUtil.Exp.foldMap: Impossible"
adamc@443 765
adamc@488 766 fun foldMapB {kind, con, exp, bind} ctx s e =
adamc@488 767 case mapfoldB {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@488 768 con = fn ctx => fn c => fn s => S.Continue (con (ctx, c, s)),
adamc@488 769 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
adamc@488 770 bind = bind} ctx e s of
adamc@488 771 S.Continue v => v
adamc@488 772 | S.Return _ => raise Fail "CoreUtil.Exp.foldMapB: Impossible"
adamc@488 773
adamc@16 774 end
adamc@16 775
adamc@20 776 structure Decl = struct
adamc@20 777
adamc@20 778 datatype binder = datatype Exp.binder
adamc@20 779
adamc@20 780 fun mapfoldB {kind = fk, con = fc, exp = fe, decl = fd, bind} =
adamc@20 781 let
adamc@20 782 val mfk = Kind.mapfold fk
adamc@20 783
adamc@20 784 fun bind' (ctx, b) =
adamc@20 785 let
adamc@20 786 val b' = case b of
adamc@20 787 Con.Rel x => RelC x
adamc@20 788 | Con.Named x => NamedC x
adamc@20 789 in
adamc@20 790 bind (ctx, b')
adamc@20 791 end
adamc@20 792 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@20 793
adamc@20 794 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind}
adamc@20 795
adamc@20 796 fun mfd ctx d acc =
adamc@20 797 S.bindP (mfd' ctx d acc, fd ctx)
adamc@20 798
adamc@20 799 and mfd' ctx (dAll as (d, loc)) =
adamc@20 800 case d of
adamc@20 801 DCon (x, n, k, c) =>
adamc@20 802 S.bind2 (mfk k,
adamc@20 803 fn k' =>
adamc@20 804 S.map2 (mfc ctx c,
adamc@20 805 fn c' =>
adamc@20 806 (DCon (x, n, k', c'), loc)))
adamc@192 807 | DDatatype (x, n, xs, xncs) =>
adamc@196 808 let
adamc@196 809 val k = (KType, loc)
adamc@196 810 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@196 811 val ctx' = bind (ctx, NamedC (x, n, k', NONE))
adamc@196 812 in
adamc@196 813 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@196 814 case c of
adamc@196 815 NONE => S.return2 (x, n, c)
adamc@196 816 | SOME c =>
adamc@196 817 S.map2 (mfc ctx' c,
adamc@196 818 fn c' => (x, n, SOME c'))) xncs,
adamc@196 819 fn xncs' =>
adamc@196 820 (DDatatype (x, n, xs, xncs'), loc))
adamc@196 821 end
adamc@125 822 | DVal vi =>
adamc@125 823 S.map2 (mfvi ctx vi,
adamc@125 824 fn vi' =>
adamc@125 825 (DVal vi', loc))
adamc@125 826 | DValRec vis =>
adamc@127 827 let
adamc@127 828 val ctx = foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@127 829 ctx vis
adamc@127 830 in
adamc@127 831 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@127 832 fn vis' =>
adamc@127 833 (DValRec vis', loc))
adamc@127 834 end
adamc@109 835 | DExport _ => S.return2 dAll
adamc@247 836 | DTable (x, n, c, s) =>
adamc@247 837 S.map2 (mfc ctx c,
adamc@461 838 fn c' =>
adamc@461 839 (DTable (x, n, c', s), loc))
adamc@338 840 | DSequence _ => S.return2 dAll
adamc@271 841 | DDatabase _ => S.return2 dAll
adamc@461 842 | DCookie (x, n, c, s) =>
adamc@461 843 S.map2 (mfc ctx c,
adamc@461 844 fn c' =>
adamc@461 845 (DCookie (x, n, c', s), loc))
adamc@125 846
adamc@125 847 and mfvi ctx (x, n, t, e, s) =
adamc@125 848 S.bind2 (mfc ctx t,
adamc@125 849 fn t' =>
adamc@125 850 S.map2 (mfe ctx e,
adamc@125 851 fn e' =>
adamc@125 852 (x, n, t', e', s)))
adamc@20 853 in
adamc@20 854 mfd
adamc@20 855 end
adamc@20 856
adamc@23 857 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 858 mapfoldB {kind = fk,
adamc@23 859 con = fn () => fc,
adamc@23 860 exp = fn () => fe,
adamc@23 861 decl = fn () => fd,
adamc@23 862 bind = fn ((), _) => ()} ()
adamc@23 863
adamc@23 864 fun fold {kind, con, exp, decl} s d =
adamc@23 865 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 866 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 867 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 868 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 869 S.Continue (_, s) => s
adamc@23 870 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
adamc@23 871
adamc@110 872 fun foldMap {kind, con, exp, decl} s d =
adamc@110 873 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 874 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 875 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 876 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 877 S.Continue v => v
adamc@110 878 | S.Return _ => raise Fail "CoreUtil.Decl.foldMap: Impossible"
adamc@110 879
adamc@484 880 fun foldMapB {kind, con, exp, decl, bind} ctx s d =
adamc@484 881 case mapfoldB {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@484 882 con = fn ctx => fn c => fn s => S.Continue (con (ctx, c, s)),
adamc@484 883 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
adamc@484 884 decl = fn ctx => fn d => fn s => S.Continue (decl (ctx, d, s)),
adamc@484 885 bind = bind} ctx d s of
adamc@484 886 S.Continue v => v
adamc@484 887 | S.Return _ => raise Fail "CoreUtil.Decl.foldMapB: Impossible"
adamc@484 888
adamc@16 889 end
adamc@20 890
adamc@20 891 structure File = struct
adamc@20 892
adamc@20 893 datatype binder = datatype Exp.binder
adamc@20 894
adamc@20 895 fun mapfoldB (all as {bind, ...}) =
adamc@20 896 let
adamc@20 897 val mfd = Decl.mapfoldB all
adamc@20 898
adamc@20 899 fun mff ctx ds =
adamc@20 900 case ds of
adamc@20 901 nil => S.return2 nil
adamc@20 902 | d :: ds' =>
adamc@20 903 S.bind2 (mfd ctx d,
adamc@20 904 fn d' =>
adamc@20 905 let
adamc@100 906 val ctx' =
adamc@20 907 case #1 d' of
adamc@100 908 DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c))
adamc@192 909 | DDatatype (x, n, xs, xncs) =>
adamc@163 910 let
adamc@192 911 val loc = #2 d'
adamc@192 912 val k = (KType, loc)
adamc@192 913 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@192 914
adamc@192 915 val ctx = bind (ctx, NamedC (x, n, k', NONE))
adamc@163 916 val t = (CNamed n, #2 d')
adamc@192 917 val t = ListUtil.foldli (fn (i, _, t) => (CApp (t, (CRel i, loc)), loc))
adamc@192 918 t xs
adamc@163 919 in
adamc@163 920 foldl (fn ((x, n, to), ctx) =>
adamc@163 921 let
adamc@163 922 val t = case to of
adamc@163 923 NONE => t
adamc@163 924 | SOME t' => (TFun (t', t), #2 d')
adamc@192 925 val t = foldr (fn (x, t) => (TCFun (x, k, t), loc))
adamc@192 926 t xs
adamc@163 927 in
adamc@163 928 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@163 929 end)
adamc@163 930 ctx xncs
adamc@163 931 end
adamc@109 932 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@125 933 | DValRec vis =>
adamc@127 934 foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@125 935 ctx vis
adamc@109 936 | DExport _ => ctx
adamc@247 937 | DTable (x, n, c, s) =>
adamc@247 938 let
adamc@338 939 val t = (CApp ((CFfi ("Basis", "sql_table"), #2 d'), c), #2 d')
adamc@338 940 in
adamc@338 941 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@338 942 end
adamc@338 943 | DSequence (x, n, s) =>
adamc@338 944 let
adamc@338 945 val t = (CFfi ("Basis", "sql_sequence"), #2 d')
adamc@247 946 in
adamc@247 947 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@247 948 end
adamc@271 949 | DDatabase _ => ctx
adamc@461 950 | DCookie (x, n, c, s) =>
adamc@461 951 let
adamc@461 952 val t = (CApp ((CFfi ("Basis", "http_cookie"), #2 d'), c), #2 d')
adamc@461 953 in
adamc@461 954 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@461 955 end
adamc@20 956 in
adamc@20 957 S.map2 (mff ctx' ds',
adamc@20 958 fn ds' =>
adamc@20 959 d' :: ds')
adamc@20 960 end)
adamc@20 961 in
adamc@20 962 mff
adamc@20 963 end
adamc@20 964
adamc@23 965 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 966 mapfoldB {kind = fk,
adamc@23 967 con = fn () => fc,
adamc@23 968 exp = fn () => fe,
adamc@23 969 decl = fn () => fd,
adamc@23 970 bind = fn ((), _) => ()} ()
adamc@23 971
adamc@20 972 fun mapB {kind, con, exp, decl, bind} ctx ds =
adamc@20 973 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@20 974 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@20 975 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@20 976 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@20 977 bind = bind} ctx ds () of
adamc@20 978 S.Continue (ds, ()) => ds
adamc@20 979 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
adamc@20 980
adamc@482 981 fun map {kind, con, exp, decl} ds =
adamc@482 982 mapB {kind = kind,
adamc@482 983 con = fn () => con,
adamc@482 984 exp = fn () => exp,
adamc@482 985 decl = fn () => decl,
adamc@482 986 bind = fn _ => ()} () ds
adamc@482 987
adamc@23 988 fun fold {kind, con, exp, decl} s d =
adamc@23 989 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 990 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 991 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 992 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 993 S.Continue (_, s) => s
adamc@23 994 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
adamc@23 995
adamc@110 996 fun foldMap {kind, con, exp, decl} s d =
adamc@110 997 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 998 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 999 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 1000 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 1001 S.Continue v => v
adamc@110 1002 | S.Return _ => raise Fail "CoreUtil.File.foldMap: Impossible"
adamc@110 1003
adamc@179 1004 val maxName = foldl (fn ((d, _) : decl, count) =>
adamc@179 1005 case d of
adamc@179 1006 DCon (_, n, _, _) => Int.max (n, count)
adamc@398 1007 | DDatatype (_, n, _, ns) =>
adamc@398 1008 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@398 1009 (Int.max (n, count)) ns
adamc@179 1010 | DVal (_, n, _, _, _) => Int.max (n, count)
adamc@179 1011 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
adamc@247 1012 | DExport _ => count
adamc@271 1013 | DTable (_, n, _, _) => Int.max (n, count)
adamc@338 1014 | DSequence (_, n, _) => Int.max (n, count)
adamc@461 1015 | DDatabase _ => count
adamc@461 1016 | DCookie (_, n, _, _) => Int.max (n, count)) 0
adamc@179 1017
adamc@20 1018 end
adamc@20 1019
adamc@20 1020 end