annotate src/core_util.sml @ 985:28bf725de7f3

New release
author Adam Chlipala <adamc@hcoop.net>
date Sat, 26 Sep 2009 12:57:01 -0400
parents 2a50da66ffd8
children dfe34fad749d
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@626 61 | (KTuple _, _) => LESS
adamc@626 62 | (_, KTuple _) => GREATER
adamc@193 63
adamc@626 64 | (KRel n1, KRel n2) => Int.compare (n1, n2)
adamc@626 65 | (KRel _, _) => LESS
adamc@626 66 | (_, KRel _) => GREATER
adamc@626 67
adamc@626 68 | (KFun (_, k1), KFun (_, k2)) => compare (k1, k2)
adamc@626 69
adamc@626 70 fun mapfoldB {kind = f, bind} =
adamc@16 71 let
adamc@626 72 fun mfk ctx k acc =
adamc@626 73 S.bindP (mfk' ctx k acc, f ctx)
adamc@16 74
adamc@626 75 and mfk' ctx (kAll as (k, loc)) =
adamc@16 76 case k of
adamc@16 77 KType => S.return2 kAll
adamc@16 78
adamc@16 79 | KArrow (k1, k2) =>
adamc@626 80 S.bind2 (mfk ctx k1,
adamc@16 81 fn k1' =>
adamc@626 82 S.map2 (mfk ctx k2,
adamc@16 83 fn k2' =>
adamc@16 84 (KArrow (k1', k2'), loc)))
adamc@16 85
adamc@16 86 | KName => S.return2 kAll
adamc@16 87
adamc@16 88 | KRecord k =>
adamc@626 89 S.map2 (mfk ctx k,
adamc@16 90 fn k' =>
adamc@16 91 (KRecord k', loc))
adamc@87 92
adamc@87 93 | KUnit => S.return2 kAll
adamc@214 94
adamc@214 95 | KTuple ks =>
adamc@626 96 S.map2 (ListUtil.mapfold (mfk ctx) ks,
adamc@214 97 fn ks' =>
adamc@214 98 (KTuple ks', loc))
adamc@626 99
adamc@626 100 | KRel _ => S.return2 kAll
adamc@626 101 | KFun (x, k) =>
adamc@626 102 S.map2 (mfk (bind (ctx, x)) k,
adamc@626 103 fn k' =>
adamc@626 104 (KFun (x, k'), loc))
adamc@16 105 in
adamc@16 106 mfk
adamc@16 107 end
adamc@16 108
adamc@626 109 fun mapfold fk =
adamc@626 110 mapfoldB {kind = fn () => fk,
adamc@626 111 bind = fn ((), _) => ()} ()
adamc@626 112
adamc@16 113 fun map f k =
adamc@16 114 case mapfold (fn k => fn () => S.Continue (f k, ())) k () of
adamc@626 115 S.Return () => raise Fail "CoreUtil.Kind.map"
adamc@16 116 | S.Continue (k, ()) => k
adamc@16 117
adamc@626 118 fun mapB {kind, bind} ctx k =
adamc@626 119 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@626 120 bind = bind} ctx k () of
adamc@626 121 S.Continue (k, ()) => k
adamc@626 122 | S.Return _ => raise Fail "CoreUtil.Kind.mapB: Impossible"
adamc@626 123
adamc@16 124 fun exists f k =
adamc@16 125 case mapfold (fn k => fn () =>
adamc@16 126 if f k then
adamc@16 127 S.Return ()
adamc@16 128 else
adamc@16 129 S.Continue (k, ())) k () of
adamc@16 130 S.Return _ => true
adamc@16 131 | S.Continue _ => false
adamc@16 132
adamc@16 133 end
adamc@16 134
adamc@16 135 structure Con = struct
adamc@16 136
adamc@193 137 open Order
adamc@193 138
adamc@193 139 fun compare ((c1, _), (c2, _)) =
adamc@193 140 case (c1, c2) of
adamc@193 141 (TFun (d1, r1), TFun (d2, r2)) => join (compare (d1, d2), fn () => compare (r1, r2))
adamc@193 142 | (TFun _, _) => LESS
adamc@193 143 | (_, TFun _) => GREATER
adamc@193 144
adamc@193 145 | (TCFun (x1, k1, r1), TCFun (x2, k2, r2)) =>
adamc@193 146 join (String.compare (x1, x2),
adamc@193 147 fn () => join (Kind.compare (k1, k2),
adamc@193 148 fn () => compare (r1, r2)))
adamc@193 149 | (TCFun _, _) => LESS
adamc@193 150 | (_, TCFun _) => GREATER
adamc@193 151
adamc@193 152 | (TRecord c1, TRecord c2) => compare (c1, c2)
adamc@193 153 | (TRecord _, _) => LESS
adamc@193 154 | (_, TRecord _) => GREATER
adamc@193 155
adamc@193 156 | (CRel n1, CRel n2) => Int.compare (n1, n2)
adamc@193 157 | (CRel _, _) => LESS
adamc@193 158 | (_, CRel _) => GREATER
adamc@193 159
adamc@193 160 | (CNamed n1, CNamed n2) => Int.compare (n1, n2)
adamc@193 161 | (CNamed _, _) => LESS
adamc@193 162 | (_, CNamed _) => GREATER
adamc@193 163
adamc@193 164 | (CFfi (m1, s1), CFfi (m2, s2)) => join (String.compare (m1, m2),
adamc@193 165 fn () => String.compare (s1, s2))
adamc@193 166 | (CFfi _, _) => LESS
adamc@193 167 | (_, CFfi _) => GREATER
adamc@193 168
adamc@193 169 | (CApp (f1, x1), CApp (f2, x2)) => join (compare (f1, f2),
adamc@193 170 fn () => compare (x1, x2))
adamc@193 171 | (CApp _, _) => LESS
adamc@193 172 | (_, CApp _) => GREATER
adamc@193 173
adamc@193 174 | (CAbs (x1, k1, b1), CAbs (x2, k2, b2)) =>
adamc@193 175 join (String.compare (x1, x2),
adamc@193 176 fn () => join (Kind.compare (k1, k2),
adamc@193 177 fn () => compare (b1, b2)))
adamc@193 178 | (CAbs _, _) => LESS
adamc@193 179 | (_, CAbs _) => GREATER
adamc@193 180
adamc@193 181 | (CName s1, CName s2) => String.compare (s1, s2)
adamc@193 182 | (CName _, _) => LESS
adamc@193 183 | (_, CName _) => GREATER
adamc@193 184
adamc@193 185 | (CRecord (k1, xvs1), CRecord (k2, xvs2)) =>
adamc@193 186 join (Kind.compare (k1, k2),
adamc@250 187 fn () =>
adamc@250 188 let
adamc@250 189 val sort = ListMergeSort.sort (fn ((x1, _), (x2, _)) =>
adamc@250 190 compare (x1, x2) = GREATER)
adamc@250 191 in
adamc@250 192 joinL (fn ((x1, v1), (x2, v2)) =>
adamc@250 193 join (compare (x1, x2),
adamc@250 194 fn () => compare (v1, v2))) (sort xvs1, sort xvs2)
adamc@250 195 end)
adamc@193 196 | (CRecord _, _) => LESS
adamc@193 197 | (_, CRecord _) => GREATER
adamc@193 198
adamc@193 199 | (CConcat (f1, s1), CConcat (f2, s2)) =>
adamc@193 200 join (compare (f1, f2),
adamc@193 201 fn () => compare (s1, s2))
adamc@193 202 | (CConcat _, _) => LESS
adamc@193 203 | (_, CConcat _) => GREATER
adamc@193 204
adamc@621 205 | (CMap (d1, r1), CMap (d2, r2)) =>
adamc@193 206 join (Kind.compare (d1, r2),
adamc@193 207 fn () => Kind.compare (r1, r2))
adamc@621 208 | (CMap _, _) => LESS
adamc@621 209 | (_, CMap _) => GREATER
adamc@193 210
adamc@193 211 | (CUnit, CUnit) => EQUAL
adamc@214 212 | (CUnit, _) => LESS
adamc@214 213 | (_, CUnit) => GREATER
adamc@214 214
adamc@214 215 | (CTuple cs1, CTuple cs2) => joinL compare (cs1, cs2)
adamc@214 216 | (CTuple _, _) => LESS
adamc@214 217 | (_, CTuple _) => GREATER
adamc@214 218
adamc@214 219 | (CProj (c1, n1), CProj (c2, n2)) => join (Int.compare (n1, n2),
adamc@214 220 fn () => compare (c1, c2))
adamc@626 221 | (CProj _, _) => LESS
adamc@626 222 | (_, CProj _) => GREATER
adamc@626 223
adamc@626 224 | (CKAbs (_, c1), CKAbs (_, c2)) => compare (c1, c2)
adamc@626 225 | (CKAbs _, _) => LESS
adamc@626 226 | (_, CKAbs _) => GREATER
adamc@626 227
adamc@626 228 | (CKApp (c1, k1), CKApp (c2, k2)) =>
adamc@626 229 join (compare (c1, c2),
adamc@626 230 fn () => Kind.compare (k1, k2))
adamc@626 231 | (CKApp _, _) => LESS
adamc@626 232 | (_, CKApp _) => GREATER
adamc@626 233
adamc@626 234 | (TKFun (_, c1), TKFun (_, c2)) => compare (c1, c2)
adamc@193 235
adamc@16 236 datatype binder =
adamc@626 237 RelK of string
adamc@626 238 | RelC of string * kind
adamc@626 239 | NamedC of string * int * kind * con option
adamc@16 240
adamc@16 241 fun mapfoldB {kind = fk, con = fc, bind} =
adamc@16 242 let
adamc@626 243 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@16 244
adamc@16 245 fun mfc ctx c acc =
adamc@16 246 S.bindP (mfc' ctx c acc, fc ctx)
adamc@16 247
adamc@16 248 and mfc' ctx (cAll as (c, loc)) =
adamc@16 249 case c of
adamc@16 250 TFun (c1, c2) =>
adamc@16 251 S.bind2 (mfc ctx c1,
adamc@16 252 fn c1' =>
adamc@16 253 S.map2 (mfc ctx c2,
adamc@16 254 fn c2' =>
adamc@16 255 (TFun (c1', c2'), loc)))
adamc@16 256 | TCFun (x, k, c) =>
adamc@626 257 S.bind2 (mfk ctx k,
adamc@16 258 fn k' =>
adamc@626 259 S.map2 (mfc (bind (ctx, RelC (x, k))) c,
adamc@16 260 fn c' =>
adamc@16 261 (TCFun (x, k', c'), loc)))
adamc@16 262 | TRecord c =>
adamc@16 263 S.map2 (mfc ctx c,
adamc@16 264 fn c' =>
adamc@16 265 (TRecord c', loc))
adamc@16 266
adamc@16 267 | CRel _ => S.return2 cAll
adamc@16 268 | CNamed _ => S.return2 cAll
adamc@48 269 | CFfi _ => S.return2 cAll
adamc@16 270 | CApp (c1, c2) =>
adamc@16 271 S.bind2 (mfc ctx c1,
adamc@16 272 fn c1' =>
adamc@16 273 S.map2 (mfc ctx c2,
adamc@16 274 fn c2' =>
adamc@16 275 (CApp (c1', c2'), loc)))
adamc@16 276 | CAbs (x, k, c) =>
adamc@626 277 S.bind2 (mfk ctx k,
adamc@16 278 fn k' =>
adamc@626 279 S.map2 (mfc (bind (ctx, RelC (x, k))) c,
adamc@16 280 fn c' =>
adamc@16 281 (CAbs (x, k', c'), loc)))
adamc@16 282
adamc@16 283 | CName _ => S.return2 cAll
adamc@16 284
adamc@16 285 | CRecord (k, xcs) =>
adamc@626 286 S.bind2 (mfk ctx k,
adamc@16 287 fn k' =>
adamc@16 288 S.map2 (ListUtil.mapfold (fn (x, c) =>
adamc@16 289 S.bind2 (mfc ctx x,
adamc@16 290 fn x' =>
adamc@16 291 S.map2 (mfc ctx c,
adamc@16 292 fn c' =>
adamc@16 293 (x', c'))))
adamc@16 294 xcs,
adamc@16 295 fn xcs' =>
adamc@16 296 (CRecord (k', xcs'), loc)))
adamc@16 297 | CConcat (c1, c2) =>
adamc@16 298 S.bind2 (mfc ctx c1,
adamc@16 299 fn c1' =>
adamc@16 300 S.map2 (mfc ctx c2,
adamc@16 301 fn c2' =>
adamc@16 302 (CConcat (c1', c2'), loc)))
adamc@621 303 | CMap (k1, k2) =>
adamc@626 304 S.bind2 (mfk ctx k1,
adamc@69 305 fn k1' =>
adamc@626 306 S.map2 (mfk ctx k2,
adamc@69 307 fn k2' =>
adamc@621 308 (CMap (k1', k2'), loc)))
adamc@87 309
adamc@87 310 | CUnit => S.return2 cAll
adamc@214 311
adamc@214 312 | CTuple cs =>
adamc@214 313 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@214 314 fn cs' =>
adamc@214 315 (CTuple cs', loc))
adamc@214 316
adamc@214 317 | CProj (c, n) =>
adamc@214 318 S.map2 (mfc ctx c,
adamc@214 319 fn c' =>
adamc@214 320 (CProj (c', n), loc))
adamc@626 321
adamc@626 322 | CKAbs (x, c) =>
adamc@626 323 S.map2 (mfc (bind (ctx, RelK x)) c,
adamc@626 324 fn c' =>
adamc@626 325 (CKAbs (x, c'), loc))
adamc@626 326 | CKApp (c, k) =>
adamc@626 327 S.bind2 (mfc ctx c,
adamc@626 328 fn c' =>
adamc@626 329 S.map2 (mfk ctx k,
adamc@626 330 fn k' =>
adamc@626 331 (CKApp (c', k'), loc)))
adamc@626 332 | TKFun (x, c) =>
adamc@626 333 S.map2 (mfc (bind (ctx, RelK x)) c,
adamc@626 334 fn c' =>
adamc@626 335 (TKFun (x, c'), loc))
adamc@16 336 in
adamc@16 337 mfc
adamc@16 338 end
adamc@16 339
adamc@16 340 fun mapfold {kind = fk, con = fc} =
adamc@626 341 mapfoldB {kind = fn () => fk,
adamc@16 342 con = fn () => fc,
adamc@16 343 bind = fn ((), _) => ()} ()
adamc@16 344
adamc@16 345 fun map {kind, con} c =
adamc@16 346 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 347 con = fn c => fn () => S.Continue (con c, ())} c () of
adamc@16 348 S.Return () => raise Fail "Core_util.Con.map"
adamc@16 349 | S.Continue (c, ()) => c
adamc@16 350
adamc@16 351 fun mapB {kind, con, bind} ctx c =
adamc@626 352 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@16 353 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@16 354 bind = bind} ctx c () of
adamc@16 355 S.Continue (c, ()) => c
adamc@20 356 | S.Return _ => raise Fail "CoreUtil.Con.mapB: Impossible"
adamc@16 357
adamc@23 358 fun fold {kind, con} s c =
adamc@23 359 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 360 con = fn c => fn s => S.Continue (c, con (c, s))} c s of
adamc@23 361 S.Continue (_, s) => s
adamc@23 362 | S.Return _ => raise Fail "CoreUtil.Con.fold: Impossible"
adamc@23 363
adamc@16 364 fun exists {kind, con} k =
adamc@16 365 case mapfold {kind = fn k => fn () =>
adamc@16 366 if kind k then
adamc@16 367 S.Return ()
adamc@16 368 else
adamc@16 369 S.Continue (k, ()),
adamc@16 370 con = fn c => fn () =>
adamc@16 371 if con c then
adamc@16 372 S.Return ()
adamc@16 373 else
adamc@16 374 S.Continue (c, ())} k () of
adamc@16 375 S.Return _ => true
adamc@16 376 | S.Continue _ => false
adamc@16 377
adamc@193 378 fun foldMap {kind, con} s c =
adamc@193 379 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@193 380 con = fn c => fn s => S.Continue (con (c, s))} c s of
adamc@193 381 S.Continue v => v
adamc@193 382 | S.Return _ => raise Fail "CoreUtil.Con.foldMap: Impossible"
adamc@193 383
adamc@16 384 end
adamc@16 385
adamc@16 386 structure Exp = struct
adamc@16 387
adamc@479 388 open Order
adamc@479 389
adamc@479 390 fun pcCompare (pc1, pc2) =
adamc@479 391 case (pc1, pc2) of
adamc@479 392 (PConVar n1, PConVar n2) => Int.compare (n1, n2)
adamc@479 393 | (PConVar _, _) => LESS
adamc@479 394 | (_, PConVar _) => GREATER
adamc@479 395
adamc@479 396 | (PConFfi {mod = m1, datatyp = d1, con = c1, ...},
adamc@479 397 PConFfi {mod = m2, datatyp = d2, con = c2, ...}) =>
adamc@479 398 join (String.compare (m1, m2),
adamc@479 399 fn () => join (String.compare (d1, d2),
adamc@479 400 fn () => String.compare (c1, c2)))
adamc@479 401
adamc@479 402 fun pCompare ((p1, _), (p2, _)) =
adamc@479 403 case (p1, p2) of
adamc@479 404 (PWild, PWild) => EQUAL
adamc@479 405 | (PWild, _) => LESS
adamc@479 406 | (_, PWild) => GREATER
adamc@479 407
adamc@479 408 | (PVar _, PVar _) => EQUAL
adamc@479 409 | (PVar _, _) => LESS
adamc@479 410 | (_, PVar _) => GREATER
adamc@479 411
adamc@479 412 | (PPrim p1, PPrim p2) => Prim.compare (p1, p2)
adamc@479 413 | (PPrim _, _) => LESS
adamc@479 414 | (_, PPrim _) => GREATER
adamc@479 415
adamc@479 416 | (PCon (_, pc1, _, po1), PCon (_, pc2, _, po2)) =>
adamc@479 417 join (pcCompare (pc1, pc2),
adamc@479 418 fn () => joinO pCompare (po1, po2))
adamc@479 419 | (PCon _, _) => LESS
adamc@479 420 | (_, PCon _) => GREATER
adamc@479 421
adamc@479 422 | (PRecord xps1, PRecord xps2) =>
adamc@479 423 joinL (fn ((x1, p1, _), (x2, p2, _)) =>
adamc@479 424 join (String.compare (x1, x2),
adamc@479 425 fn () => pCompare (p1, p2))) (xps1, xps2)
adamc@479 426
adamc@479 427 fun compare ((e1, _), (e2, _)) =
adamc@479 428 case (e1, e2) of
adamc@479 429 (EPrim p1, EPrim p2) => Prim.compare (p1, p2)
adamc@479 430 | (EPrim _, _) => LESS
adamc@479 431 | (_, EPrim _) => GREATER
adamc@479 432
adamc@479 433 | (ERel n1, ERel n2) => Int.compare (n1, n2)
adamc@479 434 | (ERel _, _) => LESS
adamc@479 435 | (_, ERel _) => GREATER
adamc@479 436
adamc@479 437 | (ENamed n1, ENamed n2) => Int.compare (n1, n2)
adamc@479 438 | (ENamed _, _) => LESS
adamc@479 439 | (_, ENamed _) => GREATER
adamc@479 440
adamc@479 441 | (ECon (_, pc1, _, eo1), ECon (_, pc2, _, eo2)) =>
adamc@479 442 join (pcCompare (pc1, pc2),
adamc@479 443 fn () => joinO compare (eo1, eo2))
adamc@479 444 | (ECon _, _) => LESS
adamc@479 445 | (_, ECon _) => GREATER
adamc@479 446
adamc@479 447 | (EFfi (f1, x1), EFfi (f2, x2)) =>
adamc@479 448 join (String.compare (f1, f2),
adamc@479 449 fn () => String.compare (x1, x2))
adamc@479 450 | (EFfi _, _) => LESS
adamc@479 451 | (_, EFfi _) => GREATER
adamc@479 452
adamc@479 453 | (EFfiApp (f1, x1, es1), EFfiApp (f2, x2, es2)) =>
adamc@479 454 join (String.compare (f1, f2),
adamc@479 455 fn () => join (String.compare (x1, x2),
adamc@479 456 fn () => joinL compare (es1, es2)))
adamc@479 457 | (EFfiApp _, _) => LESS
adamc@479 458 | (_, EFfiApp _) => GREATER
adamc@479 459
adamc@479 460 | (EApp (f1, x1), EApp (f2, x2)) =>
adamc@479 461 join (compare (f1, f2),
adamc@479 462 fn () => compare (x1, x2))
adamc@479 463 | (EApp _, _) => LESS
adamc@479 464 | (_, EApp _) => GREATER
adamc@479 465
adamc@479 466 | (EAbs (_, _, _, e1), EAbs (_, _, _, e2)) => compare (e1, e2)
adamc@479 467 | (EAbs _, _) => LESS
adamc@479 468 | (_, EAbs _) => GREATER
adamc@479 469
adamc@479 470 | (ECApp (f1, x1), ECApp (f2, x2)) =>
adamc@479 471 join (compare (f1, f2),
adamc@479 472 fn () => Con.compare (x1, x2))
adamc@479 473 | (ECApp _, _) => LESS
adamc@479 474 | (_, ECApp _) => GREATER
adamc@479 475
adamc@479 476 | (ECAbs (_, _, e1), ECAbs (_, _, e2)) => compare (e1, e2)
adamc@479 477 | (ECAbs _, _) => LESS
adamc@479 478 | (_, ECAbs _) => GREATER
adamc@479 479
adamc@479 480 | (ERecord xes1, ERecord xes2) =>
adamc@479 481 joinL (fn ((x1, e1, _), (x2, e2, _)) =>
adamc@479 482 join (Con.compare (x1, x2),
adamc@479 483 fn () => compare (e1, e2))) (xes1, xes2)
adamc@479 484 | (ERecord _, _) => LESS
adamc@479 485 | (_, ERecord _) => GREATER
adamc@479 486
adamc@479 487 | (EField (e1, c1, _), EField (e2, c2, _)) =>
adamc@479 488 join (compare (e1, e2),
adamc@479 489 fn () => Con.compare (c1, c2))
adamc@479 490 | (EField _, _) => LESS
adamc@479 491 | (_, EField _) => GREATER
adamc@479 492
adamc@479 493 | (EConcat (x1, _, y1, _), EConcat (x2, _, y2, _)) =>
adamc@479 494 join (compare (x1, x2),
adamc@479 495 fn () => compare (y1, y2))
adamc@479 496 | (EConcat _, _) => LESS
adamc@479 497 | (_, EConcat _) => GREATER
adamc@479 498
adamc@479 499 | (ECut (e1, c1, _), ECut (e2, c2, _)) =>
adamc@479 500 join (compare (e1, e2),
adamc@493 501 fn () => Con.compare (c1, c2))
adamc@479 502 | (ECut _, _) => LESS
adamc@479 503 | (_, ECut _) => GREATER
adamc@479 504
adamc@493 505 | (ECutMulti (e1, c1, _), ECutMulti (e2, c2, _)) =>
adamc@493 506 join (compare (e1, e2),
adamc@493 507 fn () => Con.compare (c1, c2))
adamc@493 508 | (ECutMulti _, _) => LESS
adamc@493 509 | (_, ECutMulti _) => GREATER
adamc@493 510
adamc@479 511 | (ECase (e1, pes1, _), ECase (e2, pes2, _)) =>
adamc@479 512 join (compare (e1, e2),
adamc@479 513 fn () => joinL (fn ((p1, e1), (p2, e2)) =>
adamc@479 514 join (pCompare (p1, p2),
adamc@479 515 fn () => compare (e1, e2))) (pes1, pes2))
adamc@479 516 | (ECase _, _) => LESS
adamc@479 517 | (_, ECase _) => GREATER
adamc@479 518
adamc@479 519 | (EWrite e1, EWrite e2) => compare (e1, e2)
adamc@479 520 | (EWrite _, _) => LESS
adamc@479 521 | (_, EWrite _) => GREATER
adamc@479 522
adamc@479 523 | (EClosure (n1, es1), EClosure (n2, es2)) =>
adamc@479 524 join (Int.compare (n1, n2),
adamc@479 525 fn () => joinL compare (es1, es2))
adamc@479 526 | (EClosure _, _) => LESS
adamc@479 527 | (_, EClosure _) => GREATER
adamc@479 528
adamc@479 529 | (ELet (_, _, x1, e1), ELet (_, _, x2, e2)) =>
adamc@479 530 join (compare (x1, x2),
adamc@479 531 fn () => compare (e1, e2))
adamc@607 532 | (ELet _, _) => LESS
adamc@607 533 | (_, ELet _) => GREATER
adamc@607 534
adamc@908 535 | (EServerCall (n1, es1, e1, _, _), EServerCall (n2, es2, e2, _, _)) =>
adamc@607 536 join (Int.compare (n1, n2),
adamc@607 537 fn () => join (joinL compare (es1, es2),
adamc@607 538 fn () => compare (e1, e2)))
adamc@626 539 | (EServerCall _, _) => LESS
adamc@626 540 | (_, EServerCall _) => GREATER
adamc@626 541
adamc@954 542 | (ETailCall (n1, es1, e1, _, _), ETailCall (n2, es2, e2, _, _)) =>
adamc@954 543 join (Int.compare (n1, n2),
adamc@954 544 fn () => join (joinL compare (es1, es2),
adamc@954 545 fn () => compare (e1, e2)))
adamc@954 546 | (ETailCall _, _) => LESS
adamc@954 547 | (_, ETailCall _) => GREATER
adamc@954 548
adamc@626 549 | (EKAbs (_, e1), EKAbs (_, e2)) => compare (e1, e2)
adamc@626 550 | (EKAbs _, _) => LESS
adamc@626 551 | (_, EKAbs _) => GREATER
adamc@626 552
adamc@626 553 | (EKApp (e1, k1), EKApp (e2, k2)) =>
adamc@626 554 join (compare (e1, e2),
adamc@626 555 fn () => Kind.compare (k1, k2))
adamc@479 556
adamc@16 557 datatype binder =
adamc@626 558 RelK of string
adamc@626 559 | RelC of string * kind
adamc@20 560 | NamedC of string * int * kind * con option
adamc@16 561 | RelE of string * con
adamc@109 562 | NamedE of string * int * con * exp option * string
adamc@16 563
adamc@16 564 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@16 565 let
adamc@626 566 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@16 567
adamc@16 568 fun bind' (ctx, b) =
adamc@16 569 let
adamc@16 570 val b' = case b of
adamc@626 571 Con.RelK x => RelK x
adamc@626 572 | Con.RelC x => RelC x
adamc@626 573 | Con.NamedC x => NamedC x
adamc@16 574 in
adamc@16 575 bind (ctx, b')
adamc@16 576 end
adamc@16 577 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@16 578
adamc@16 579 fun mfe ctx e acc =
adamc@16 580 S.bindP (mfe' ctx e acc, fe ctx)
adamc@16 581
adamc@16 582 and mfe' ctx (eAll as (e, loc)) =
adamc@16 583 case e of
adamc@16 584 EPrim _ => S.return2 eAll
adamc@16 585 | ERel _ => S.return2 eAll
adamc@16 586 | ENamed _ => S.return2 eAll
adamc@192 587 | ECon (dk, pc, cs, NONE) =>
adamc@192 588 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 589 fn cs' =>
adamc@192 590 (ECon (dk, pc, cs', NONE), loc))
adamc@192 591 | ECon (dk, n, cs, SOME e) =>
adamc@192 592 S.bind2 (mfe ctx e,
adamc@192 593 fn e' =>
adamc@192 594 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 595 fn cs' =>
adamc@192 596 (ECon (dk, n, cs', SOME e'), loc)))
adamc@48 597 | EFfi _ => S.return2 eAll
adamc@48 598 | EFfiApp (m, x, es) =>
adamc@192 599 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@48 600 fn es' =>
adamc@48 601 (EFfiApp (m, x, es'), loc))
adamc@16 602 | EApp (e1, e2) =>
adamc@16 603 S.bind2 (mfe ctx e1,
adamc@16 604 fn e1' =>
adamc@16 605 S.map2 (mfe ctx e2,
adamc@16 606 fn e2' =>
adamc@16 607 (EApp (e1', e2'), loc)))
adamc@26 608 | EAbs (x, dom, ran, e) =>
adamc@26 609 S.bind2 (mfc ctx dom,
adamc@26 610 fn dom' =>
adamc@26 611 S.bind2 (mfc ctx ran,
adamc@26 612 fn ran' =>
adamc@26 613 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 614 fn e' =>
adamc@26 615 (EAbs (x, dom', ran', e'), loc))))
adamc@16 616
adamc@16 617 | ECApp (e, c) =>
adamc@16 618 S.bind2 (mfe ctx e,
adamc@16 619 fn e' =>
adamc@16 620 S.map2 (mfc ctx c,
adamc@16 621 fn c' =>
adamc@16 622 (ECApp (e', c'), loc)))
adamc@16 623 | ECAbs (x, k, e) =>
adamc@626 624 S.bind2 (mfk ctx k,
adamc@16 625 fn k' =>
adamc@16 626 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@16 627 fn e' =>
adamc@16 628 (ECAbs (x, k', e'), loc)))
adamc@16 629
adamc@16 630 | ERecord xes =>
adamc@29 631 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@16 632 S.bind2 (mfc ctx x,
adamc@16 633 fn x' =>
adamc@29 634 S.bind2 (mfe ctx e,
adamc@29 635 fn e' =>
adamc@29 636 S.map2 (mfc ctx t,
adamc@29 637 fn t' =>
adamc@29 638 (x', e', t')))))
adamc@16 639 xes,
adamc@16 640 fn xes' =>
adamc@16 641 (ERecord xes', loc))
adamc@16 642 | EField (e, c, {field, rest}) =>
adamc@16 643 S.bind2 (mfe ctx e,
adamc@16 644 fn e' =>
adamc@16 645 S.bind2 (mfc ctx c,
adamc@16 646 fn c' =>
adamc@16 647 S.bind2 (mfc ctx field,
adamc@16 648 fn field' =>
adamc@16 649 S.map2 (mfc ctx rest,
adamc@16 650 fn rest' =>
adamc@16 651 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@445 652 | EConcat (e1, c1, e2, c2) =>
adamc@339 653 S.bind2 (mfe ctx e1,
adamc@339 654 fn e1' =>
adamc@445 655 S.bind2 (mfc ctx c1,
adamc@445 656 fn c1' =>
adamc@339 657 S.bind2 (mfe ctx e2,
adamc@339 658 fn e2' =>
adamc@445 659 S.map2 (mfc ctx c2,
adamc@445 660 fn c2' =>
adamc@445 661 (EConcat (e1', c1', e2', c2'),
adamc@445 662 loc)))))
adamc@149 663 | ECut (e, c, {field, rest}) =>
adamc@149 664 S.bind2 (mfe ctx e,
adamc@149 665 fn e' =>
adamc@149 666 S.bind2 (mfc ctx c,
adamc@149 667 fn c' =>
adamc@149 668 S.bind2 (mfc ctx field,
adamc@149 669 fn field' =>
adamc@149 670 S.map2 (mfc ctx rest,
adamc@149 671 fn rest' =>
adamc@149 672 (ECut (e', c', {field = field', rest = rest'}), loc)))))
adamc@493 673 | ECutMulti (e, c, {rest}) =>
adamc@493 674 S.bind2 (mfe ctx e,
adamc@493 675 fn e' =>
adamc@493 676 S.bind2 (mfc ctx c,
adamc@493 677 fn c' =>
adamc@493 678 S.map2 (mfc ctx rest,
adamc@493 679 fn rest' =>
adamc@493 680 (ECutMulti (e', c', {rest = rest'}), loc))))
adamc@102 681
adamc@182 682 | ECase (e, pes, {disc, result}) =>
adamc@177 683 S.bind2 (mfe ctx e,
adamc@177 684 fn e' =>
adamc@177 685 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@193 686 let
adamc@193 687 fun pb ((p, _), ctx) =
adamc@193 688 case p of
adamc@193 689 PWild => ctx
adamc@193 690 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@193 691 | PPrim _ => ctx
adamc@193 692 | PCon (_, _, _, NONE) => ctx
adamc@193 693 | PCon (_, _, _, SOME p) => pb (p, ctx)
adamc@193 694 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@193 695 pb (p, ctx)) ctx xps
adamc@193 696 in
adamc@193 697 S.bind2 (mfp ctx p,
adamc@193 698 fn p' =>
adamc@193 699 S.map2 (mfe (pb (p', ctx)) e,
adamc@193 700 fn e' => (p', e')))
adamc@193 701 end) pes,
adamc@177 702 fn pes' =>
adamc@182 703 S.bind2 (mfc ctx disc,
adamc@182 704 fn disc' =>
adamc@182 705 S.map2 (mfc ctx result,
adamc@182 706 fn result' =>
adamc@182 707 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@177 708
adamc@102 709 | EWrite e =>
adamc@102 710 S.map2 (mfe ctx e,
adamc@102 711 fn e' =>
adamc@102 712 (EWrite e', loc))
adamc@110 713
adamc@110 714 | EClosure (n, es) =>
adamc@110 715 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@110 716 fn es' =>
adamc@110 717 (EClosure (n, es'), loc))
adamc@193 718
adamc@450 719 | ELet (x, t, e1, e2) =>
adamc@450 720 S.bind2 (mfc ctx t,
adamc@450 721 fn t' =>
adamc@450 722 S.bind2 (mfe ctx e1,
adamc@450 723 fn e1' =>
adamc@453 724 S.map2 (mfe (bind (ctx, RelE (x, t'))) e2,
adamc@450 725 fn e2' =>
adamc@450 726 (ELet (x, t', e1', e2'), loc))))
adamc@450 727
adamc@908 728 | EServerCall (n, es, e, t1, t2) =>
adamc@607 729 S.bind2 (ListUtil.mapfold (mfe ctx) es,
adamc@607 730 fn es' =>
adamc@609 731 S.bind2 (mfe ctx e,
adamc@607 732 fn e' =>
adamc@908 733 S.bind2 (mfc ctx t1,
adamc@908 734 fn t1' =>
adamc@908 735 S.map2 (mfc ctx t2,
adamc@908 736 fn t2' =>
adamc@908 737 (EServerCall (n, es', e', t1', t2'), loc)))))
adamc@626 738
adamc@954 739 | ETailCall (n, es, e, t1, t2) =>
adamc@954 740 S.bind2 (ListUtil.mapfold (mfe ctx) es,
adamc@954 741 fn es' =>
adamc@954 742 S.bind2 (mfe ctx e,
adamc@954 743 fn e' =>
adamc@954 744 S.bind2 (mfc ctx t1,
adamc@954 745 fn t1' =>
adamc@954 746 S.map2 (mfc ctx t2,
adamc@954 747 fn t2' =>
adamc@954 748 (ETailCall (n, es', e', t1', t2'), loc)))))
adamc@954 749
adamc@626 750 | EKAbs (x, e) =>
adamc@626 751 S.map2 (mfe (bind (ctx, RelK x)) e,
adamc@626 752 fn e' =>
adamc@626 753 (EKAbs (x, e'), loc))
adamc@626 754 | EKApp (e, k) =>
adamc@626 755 S.bind2 (mfe ctx e,
adamc@626 756 fn e' =>
adamc@626 757 S.map2 (mfk ctx k,
adamc@626 758 fn k' =>
adamc@626 759 (EKApp (e', k'), loc)))
adamc@607 760
adamc@193 761 and mfp ctx (pAll as (p, loc)) =
adamc@193 762 case p of
adamc@193 763 PWild => S.return2 pAll
adamc@193 764 | PVar (x, t) =>
adamc@193 765 S.map2 (mfc ctx t,
adamc@193 766 fn t' =>
adamc@193 767 (PVar (x, t'), loc))
adamc@193 768 | PPrim _ => S.return2 pAll
adamc@193 769 | PCon (dk, pc, args, po) =>
adamc@193 770 S.bind2 (mfpc ctx pc,
adamc@193 771 fn pc' =>
adamc@193 772 S.bind2 (ListUtil.mapfold (mfc ctx) args,
adamc@193 773 fn args' =>
adamc@193 774 S.map2 ((case po of
adamc@193 775 NONE => S.return2 NONE
adamc@193 776 | SOME p => S.map2 (mfp ctx p, SOME)),
adamc@193 777 fn po' =>
adamc@193 778 (PCon (dk, pc', args', po'), loc))))
adamc@193 779 | PRecord xps =>
adamc@193 780 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
adamc@193 781 S.bind2 (mfp ctx p,
adamc@193 782 fn p' =>
adamc@193 783 S.map2 (mfc ctx c,
adamc@193 784 fn c' =>
adamc@193 785 (x, p', c')))) xps,
adamc@193 786 fn xps' =>
adamc@193 787 (PRecord xps', loc))
adamc@193 788
adamc@193 789 and mfpc ctx pc =
adamc@193 790 case pc of
adamc@193 791 PConVar _ => S.return2 pc
adamc@193 792 | PConFfi {mod = m, datatyp, params, con, arg, kind} =>
adamc@193 793 S.map2 ((case arg of
adamc@193 794 NONE => S.return2 NONE
adamc@796 795 | SOME c =>
adamc@796 796 let
adamc@796 797 val k = (KType, ErrorMsg.dummySpan)
adamc@796 798 val ctx' = foldl (fn (x, ctx) => bind (ctx, RelC (x, k))) ctx params
adamc@796 799 in
adamc@796 800 S.map2 (mfc ctx' c, SOME)
adamc@796 801 end),
adamc@193 802 fn arg' =>
adamc@193 803 PConFfi {mod = m, datatyp = datatyp, params = params,
adamc@193 804 con = con, arg = arg', kind = kind})
adamc@16 805 in
adamc@16 806 mfe
adamc@16 807 end
adamc@16 808
adamc@16 809 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@626 810 mapfoldB {kind = fn () => fk,
adamc@16 811 con = fn () => fc,
adamc@16 812 exp = fn () => fe,
adamc@16 813 bind = fn ((), _) => ()} ()
adamc@16 814
adamc@21 815 fun mapB {kind, con, exp, bind} ctx e =
adamc@626 816 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@21 817 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@21 818 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@21 819 bind = bind} ctx e () of
adamc@21 820 S.Continue (e, ()) => e
adamc@21 821 | S.Return _ => raise Fail "CoreUtil.Exp.mapB: Impossible"
adamc@21 822
adamc@16 823 fun map {kind, con, exp} e =
adamc@16 824 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 825 con = fn c => fn () => S.Continue (con c, ()),
adamc@16 826 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@16 827 S.Return () => raise Fail "Core_util.Exp.map"
adamc@16 828 | S.Continue (e, ()) => e
adamc@16 829
adamc@23 830 fun fold {kind, con, exp} s e =
adamc@23 831 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 832 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 833 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@23 834 S.Continue (_, s) => s
adamc@23 835 | S.Return _ => raise Fail "CoreUtil.Exp.fold: Impossible"
adamc@23 836
adamc@484 837 fun foldB {kind, con, exp, bind} ctx s e =
adamc@626 838 case mapfoldB {kind = fn ctx => fn k => fn s => S.Continue (k, kind (ctx, k, s)),
adamc@484 839 con = fn ctx => fn c => fn s => S.Continue (c, con (ctx, c, s)),
adamc@484 840 exp = fn ctx => fn e => fn s => S.Continue (e, exp (ctx, e, s)),
adamc@484 841 bind = bind} ctx e s of
adamc@484 842 S.Continue (_, s) => s
adamc@484 843 | S.Return _ => raise Fail "CoreUtil.Exp.foldB: Impossible"
adamc@484 844
adamc@16 845 fun exists {kind, con, exp} k =
adamc@16 846 case mapfold {kind = fn k => fn () =>
adamc@16 847 if kind k then
adamc@16 848 S.Return ()
adamc@16 849 else
adamc@16 850 S.Continue (k, ()),
adamc@16 851 con = fn c => fn () =>
adamc@16 852 if con c then
adamc@16 853 S.Return ()
adamc@16 854 else
adamc@16 855 S.Continue (c, ()),
adamc@16 856 exp = fn e => fn () =>
adamc@16 857 if exp e then
adamc@16 858 S.Return ()
adamc@16 859 else
adamc@16 860 S.Continue (e, ())} k () of
adamc@16 861 S.Return _ => true
adamc@16 862 | S.Continue _ => false
adamc@16 863
adamc@479 864 fun existsB {kind, con, exp, bind} ctx k =
adamc@626 865 case mapfoldB {kind = fn ctx => fn k => fn () =>
adamc@626 866 if kind (ctx, k) then
adamc@626 867 S.Return ()
adamc@626 868 else
adamc@626 869 S.Continue (k, ()),
adamc@479 870 con = fn ctx => fn c => fn () =>
adamc@479 871 if con (ctx, c) then
adamc@479 872 S.Return ()
adamc@479 873 else
adamc@479 874 S.Continue (c, ()),
adamc@479 875 exp = fn ctx => fn e => fn () =>
adamc@479 876 if exp (ctx, e) then
adamc@479 877 S.Return ()
adamc@479 878 else
adamc@479 879 S.Continue (e, ()),
adamc@479 880 bind = bind} ctx k () of
adamc@479 881 S.Return _ => true
adamc@479 882 | S.Continue _ => false
adamc@479 883
adamc@443 884 fun foldMap {kind, con, exp} s e =
adamc@443 885 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@443 886 con = fn c => fn s => S.Continue (con (c, s)),
adamc@443 887 exp = fn e => fn s => S.Continue (exp (e, s))} e s of
adamc@443 888 S.Continue v => v
adamc@443 889 | S.Return _ => raise Fail "CoreUtil.Exp.foldMap: Impossible"
adamc@443 890
adamc@488 891 fun foldMapB {kind, con, exp, bind} ctx s e =
adamc@626 892 case mapfoldB {kind = fn ctx => fn k => fn s => S.Continue (kind (ctx, k, s)),
adamc@488 893 con = fn ctx => fn c => fn s => S.Continue (con (ctx, c, s)),
adamc@488 894 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
adamc@488 895 bind = bind} ctx e s of
adamc@488 896 S.Continue v => v
adamc@488 897 | S.Return _ => raise Fail "CoreUtil.Exp.foldMapB: Impossible"
adamc@488 898
adamc@16 899 end
adamc@16 900
adamc@20 901 structure Decl = struct
adamc@20 902
adamc@20 903 datatype binder = datatype Exp.binder
adamc@20 904
adamc@20 905 fun mapfoldB {kind = fk, con = fc, exp = fe, decl = fd, bind} =
adamc@20 906 let
adamc@626 907 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@20 908
adamc@20 909 fun bind' (ctx, b) =
adamc@20 910 let
adamc@20 911 val b' = case b of
adamc@626 912 Con.RelK x => RelK x
adamc@626 913 | Con.RelC x => RelC x
adamc@626 914 | Con.NamedC x => NamedC x
adamc@20 915 in
adamc@20 916 bind (ctx, b')
adamc@20 917 end
adamc@20 918 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@20 919
adamc@20 920 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind}
adamc@20 921
adamc@20 922 fun mfd ctx d acc =
adamc@20 923 S.bindP (mfd' ctx d acc, fd ctx)
adamc@20 924
adamc@20 925 and mfd' ctx (dAll as (d, loc)) =
adamc@20 926 case d of
adamc@20 927 DCon (x, n, k, c) =>
adamc@626 928 S.bind2 (mfk ctx k,
adamc@20 929 fn k' =>
adamc@20 930 S.map2 (mfc ctx c,
adamc@20 931 fn c' =>
adamc@20 932 (DCon (x, n, k', c'), loc)))
adamc@807 933 | DDatatype dts =>
adamc@807 934 S.map2 (ListUtil.mapfold (fn (x, n, xs, xncs) =>
adamc@807 935 let
adamc@807 936 val k = (KType, loc)
adamc@807 937 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@807 938 val ctx' = bind (ctx, NamedC (x, n, k', NONE))
adamc@807 939 in
adamc@807 940 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@807 941 case c of
adamc@807 942 NONE => S.return2 (x, n, c)
adamc@807 943 | SOME c =>
adamc@807 944 S.map2 (mfc ctx' c,
adamc@807 945 fn c' => (x, n, SOME c'))) xncs,
adamc@807 946 fn xncs' => (x, n, xs, xncs'))
adamc@807 947 end) dts,
adamc@807 948 fn dts' =>
adamc@807 949 (DDatatype dts', loc))
adamc@125 950 | DVal vi =>
adamc@125 951 S.map2 (mfvi ctx vi,
adamc@125 952 fn vi' =>
adamc@125 953 (DVal vi', loc))
adamc@125 954 | DValRec vis =>
adamc@127 955 let
adamc@127 956 val ctx = foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@127 957 ctx vis
adamc@127 958 in
adamc@127 959 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@127 960 fn vis' =>
adamc@127 961 (DValRec vis', loc))
adamc@127 962 end
adamc@109 963 | DExport _ => S.return2 dAll
adamc@707 964 | DTable (x, n, c, s, pe, pc, ce, cc) =>
adamc@802 965 let
adamc@802 966 val loc = #2 ce
adamc@802 967 val ct = (CFfi ("Basis", "sql_table"), loc)
adamc@802 968 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
adamc@802 969 val ct = (CApp (ct, cc), loc)
adamc@802 970 val ctx' = bind (ctx, NamedE (x, n, ct, NONE, s))
adamc@802 971 in
adamc@802 972 S.bind2 (mfc ctx c,
adamc@802 973 fn c' =>
adamc@802 974 S.bind2 (mfe ctx' pe,
adamc@802 975 fn pe' =>
adamc@802 976 S.bind2 (mfc ctx pc,
adamc@802 977 fn pc' =>
adamc@802 978 S.bind2 (mfe ctx' ce,
adamc@802 979 fn ce' =>
adamc@802 980 S.map2 (mfc ctx cc,
adamc@802 981 fn cc' =>
adamc@802 982 (DTable (x, n, c', s, pe', pc', ce', cc'), loc))))))
adamc@802 983 end
adamc@338 984 | DSequence _ => S.return2 dAll
adamc@754 985 | DView (x, n, s, e, c) =>
adamc@754 986 S.bind2 (mfe ctx e,
adamc@754 987 fn e' =>
adamc@754 988 S.map2 (mfc ctx c,
adamc@754 989 fn c' =>
adamc@754 990 (DView (x, n, s, e', c'), loc)))
adamc@271 991 | DDatabase _ => S.return2 dAll
adamc@461 992 | DCookie (x, n, c, s) =>
adamc@461 993 S.map2 (mfc ctx c,
adamc@461 994 fn c' =>
adamc@461 995 (DCookie (x, n, c', s), loc))
adamc@720 996 | DStyle _ => S.return2 dAll
adamc@125 997
adamc@125 998 and mfvi ctx (x, n, t, e, s) =
adamc@125 999 S.bind2 (mfc ctx t,
adamc@125 1000 fn t' =>
adamc@125 1001 S.map2 (mfe ctx e,
adamc@125 1002 fn e' =>
adamc@125 1003 (x, n, t', e', s)))
adamc@20 1004 in
adamc@20 1005 mfd
adamc@20 1006 end
adamc@20 1007
adamc@23 1008 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@626 1009 mapfoldB {kind = fn () => fk,
adamc@23 1010 con = fn () => fc,
adamc@23 1011 exp = fn () => fe,
adamc@23 1012 decl = fn () => fd,
adamc@23 1013 bind = fn ((), _) => ()} ()
adamc@23 1014
adamc@23 1015 fun fold {kind, con, exp, decl} s d =
adamc@23 1016 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 1017 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 1018 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 1019 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 1020 S.Continue (_, s) => s
adamc@23 1021 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
adamc@23 1022
adamc@110 1023 fun foldMap {kind, con, exp, decl} s d =
adamc@110 1024 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 1025 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 1026 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 1027 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 1028 S.Continue v => v
adamc@110 1029 | S.Return _ => raise Fail "CoreUtil.Decl.foldMap: Impossible"
adamc@110 1030
adamc@484 1031 fun foldMapB {kind, con, exp, decl, bind} ctx s d =
adamc@626 1032 case mapfoldB {kind = fn ctx => fn k => fn s => S.Continue (kind (ctx, k, s)),
adamc@484 1033 con = fn ctx => fn c => fn s => S.Continue (con (ctx, c, s)),
adamc@484 1034 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
adamc@484 1035 decl = fn ctx => fn d => fn s => S.Continue (decl (ctx, d, s)),
adamc@484 1036 bind = bind} ctx d s of
adamc@484 1037 S.Continue v => v
adamc@484 1038 | S.Return _ => raise Fail "CoreUtil.Decl.foldMapB: Impossible"
adamc@484 1039
adamc@522 1040 fun exists {kind, con, exp, decl} d =
adamc@522 1041 case mapfold {kind = fn k => fn () =>
adamc@522 1042 if kind k then
adamc@522 1043 S.Return ()
adamc@522 1044 else
adamc@522 1045 S.Continue (k, ()),
adamc@522 1046 con = fn c => fn () =>
adamc@522 1047 if con c then
adamc@522 1048 S.Return ()
adamc@522 1049 else
adamc@522 1050 S.Continue (c, ()),
adamc@522 1051 exp = fn e => fn () =>
adamc@522 1052 if exp e then
adamc@522 1053 S.Return ()
adamc@522 1054 else
adamc@522 1055 S.Continue (e, ()),
adamc@522 1056 decl = fn d => fn () =>
adamc@522 1057 if decl d then
adamc@522 1058 S.Return ()
adamc@522 1059 else
adamc@522 1060 S.Continue (d, ())} d () of
adamc@522 1061 S.Return _ => true
adamc@522 1062 | S.Continue _ => false
adamc@522 1063
adamc@16 1064 end
adamc@20 1065
adamc@20 1066 structure File = struct
adamc@20 1067
adamc@20 1068 datatype binder = datatype Exp.binder
adamc@20 1069
adamc@20 1070 fun mapfoldB (all as {bind, ...}) =
adamc@20 1071 let
adamc@20 1072 val mfd = Decl.mapfoldB all
adamc@20 1073
adamc@20 1074 fun mff ctx ds =
adamc@20 1075 case ds of
adamc@20 1076 nil => S.return2 nil
adamc@20 1077 | d :: ds' =>
adamc@20 1078 S.bind2 (mfd ctx d,
adamc@20 1079 fn d' =>
adamc@20 1080 let
adamc@100 1081 val ctx' =
adamc@20 1082 case #1 d' of
adamc@100 1083 DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c))
adamc@807 1084 | DDatatype dts =>
adamc@807 1085 foldl (fn ((x, n, xs, xncs), ctx) =>
adamc@807 1086 let
adamc@807 1087 val loc = #2 d'
adamc@807 1088 val k = (KType, loc)
adamc@807 1089 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@192 1090
adamc@807 1091 val ctx = bind (ctx, NamedC (x, n, k', NONE))
adamc@807 1092 val t = (CNamed n, #2 d')
adamc@807 1093 val t = ListUtil.foldli (fn (i, _, t) =>
adamc@807 1094 (CApp (t, (CRel i, loc)), loc))
adamc@807 1095 t xs
adamc@807 1096 in
adamc@807 1097 foldl (fn ((x, n, to), ctx) =>
adamc@807 1098 let
adamc@807 1099 val t = case to of
adamc@807 1100 NONE => t
adamc@807 1101 | SOME t' => (TFun (t', t), #2 d')
adamc@807 1102 val t = foldr (fn (x, t) => (TCFun (x, k, t), loc))
adamc@807 1103 t xs
adamc@807 1104 in
adamc@807 1105 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@807 1106 end)
adamc@807 1107 ctx xncs
adamc@807 1108 end)
adamc@807 1109 ctx dts
adamc@109 1110 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@125 1111 | DValRec vis =>
adamc@127 1112 foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@125 1113 ctx vis
adamc@109 1114 | DExport _ => ctx
adamc@707 1115 | DTable (x, n, c, s, _, pc, _, cc) =>
adamc@247 1116 let
adamc@705 1117 val loc = #2 d'
adamc@705 1118 val ct = (CFfi ("Basis", "sql_table"), loc)
adamc@707 1119 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
adamc@705 1120 val ct = (CApp (ct, cc), loc)
adamc@338 1121 in
adamc@705 1122 bind (ctx, NamedE (x, n, ct, NONE, s))
adamc@338 1123 end
adamc@338 1124 | DSequence (x, n, s) =>
adamc@338 1125 let
adamc@338 1126 val t = (CFfi ("Basis", "sql_sequence"), #2 d')
adamc@247 1127 in
adamc@247 1128 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@247 1129 end
adamc@754 1130 | DView (x, n, s, _, c) =>
adamc@754 1131 let
adamc@754 1132 val loc = #2 d'
adamc@754 1133 val ct = (CFfi ("Basis", "sql_view"), loc)
adamc@754 1134 val ct = (CApp (ct, c), loc)
adamc@754 1135 in
adamc@754 1136 bind (ctx, NamedE (x, n, ct, NONE, s))
adamc@754 1137 end
adamc@271 1138 | DDatabase _ => ctx
adamc@461 1139 | DCookie (x, n, c, s) =>
adamc@461 1140 let
adamc@461 1141 val t = (CApp ((CFfi ("Basis", "http_cookie"), #2 d'), c), #2 d')
adamc@461 1142 in
adamc@461 1143 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@461 1144 end
adamc@720 1145 | DStyle (x, n, s) =>
adamc@718 1146 let
adamc@720 1147 val t = (CFfi ("Basis", "css_class"), #2 d')
adamc@718 1148 in
adamc@718 1149 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@718 1150 end
adamc@20 1151 in
adamc@20 1152 S.map2 (mff ctx' ds',
adamc@20 1153 fn ds' =>
adamc@20 1154 d' :: ds')
adamc@20 1155 end)
adamc@20 1156 in
adamc@20 1157 mff
adamc@20 1158 end
adamc@20 1159
adamc@23 1160 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@626 1161 mapfoldB {kind = fn () => fk,
adamc@23 1162 con = fn () => fc,
adamc@23 1163 exp = fn () => fe,
adamc@23 1164 decl = fn () => fd,
adamc@23 1165 bind = fn ((), _) => ()} ()
adamc@23 1166
adamc@20 1167 fun mapB {kind, con, exp, decl, bind} ctx ds =
adamc@626 1168 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@20 1169 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@20 1170 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@20 1171 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@20 1172 bind = bind} ctx ds () of
adamc@20 1173 S.Continue (ds, ()) => ds
adamc@20 1174 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
adamc@20 1175
adamc@482 1176 fun map {kind, con, exp, decl} ds =
adamc@626 1177 mapB {kind = fn () => kind,
adamc@482 1178 con = fn () => con,
adamc@482 1179 exp = fn () => exp,
adamc@482 1180 decl = fn () => decl,
adamc@482 1181 bind = fn _ => ()} () ds
adamc@482 1182
adamc@23 1183 fun fold {kind, con, exp, decl} s d =
adamc@23 1184 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 1185 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 1186 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 1187 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 1188 S.Continue (_, s) => s
adamc@23 1189 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
adamc@23 1190
adamc@110 1191 fun foldMap {kind, con, exp, decl} s d =
adamc@110 1192 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 1193 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 1194 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 1195 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 1196 S.Continue v => v
adamc@110 1197 | S.Return _ => raise Fail "CoreUtil.File.foldMap: Impossible"
adamc@110 1198
adamc@179 1199 val maxName = foldl (fn ((d, _) : decl, count) =>
adamc@179 1200 case d of
adamc@179 1201 DCon (_, n, _, _) => Int.max (n, count)
adamc@807 1202 | DDatatype dts => foldl (fn ((_, n, _, ns), count) =>
adamc@807 1203 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@807 1204 (Int.max (n, count)) ns) count dts
adamc@179 1205 | DVal (_, n, _, _, _) => Int.max (n, count)
adamc@179 1206 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
adamc@247 1207 | DExport _ => count
adamc@707 1208 | DTable (_, n, _, _, _, _, _, _) => Int.max (n, count)
adamc@338 1209 | DSequence (_, n, _) => Int.max (n, count)
adamc@754 1210 | DView (_, n, _, _, _) => Int.max (n, count)
adamc@461 1211 | DDatabase _ => count
adamc@718 1212 | DCookie (_, n, _, _) => Int.max (n, count)
adamc@720 1213 | DStyle (_, n, _) => Int.max (n, count)) 0
adamc@179 1214
adamc@20 1215 end
adamc@20 1216
adamc@20 1217 end