annotate src/core_util.sml @ 1783:5bc4fbf9c0fe

New event records for key and mouse handlers
author Adam Chlipala <adam@chlipala.net>
date Sat, 21 Jul 2012 10:02:53 -0400
parents 0577be31a435
children e15234fbb163
rev   line source
adamc@1185 1 (* Copyright (c) 2008-2010, 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@1185 378 fun existsB {kind, con, bind} ctx c =
adamc@1185 379 case mapfoldB {kind = fn ctx => fn k => fn () =>
adamc@1185 380 if kind (ctx, k) then
adamc@1185 381 S.Return ()
adamc@1185 382 else
adamc@1185 383 S.Continue (k, ()),
adamc@1185 384 con = fn ctx => fn c => fn () =>
adamc@1185 385 if con (ctx, c) then
adamc@1185 386 S.Return ()
adamc@1185 387 else
adamc@1185 388 S.Continue (c, ()),
adamc@1185 389 bind = bind} ctx c () of
adamc@1185 390 S.Return _ => true
adamc@1185 391 | S.Continue _ => false
adamc@1185 392
adamc@193 393 fun foldMap {kind, con} s c =
adamc@193 394 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@193 395 con = fn c => fn s => S.Continue (con (c, s))} c s of
adamc@193 396 S.Continue v => v
adamc@193 397 | S.Return _ => raise Fail "CoreUtil.Con.foldMap: Impossible"
adamc@193 398
adamc@16 399 end
adamc@16 400
adamc@16 401 structure Exp = struct
adamc@16 402
adamc@479 403 open Order
adamc@479 404
adamc@479 405 fun pcCompare (pc1, pc2) =
adamc@479 406 case (pc1, pc2) of
adamc@479 407 (PConVar n1, PConVar n2) => Int.compare (n1, n2)
adamc@479 408 | (PConVar _, _) => LESS
adamc@479 409 | (_, PConVar _) => GREATER
adamc@479 410
adamc@479 411 | (PConFfi {mod = m1, datatyp = d1, con = c1, ...},
adamc@479 412 PConFfi {mod = m2, datatyp = d2, con = c2, ...}) =>
adamc@479 413 join (String.compare (m1, m2),
adamc@479 414 fn () => join (String.compare (d1, d2),
adamc@479 415 fn () => String.compare (c1, c2)))
adamc@479 416
adamc@479 417 fun pCompare ((p1, _), (p2, _)) =
adamc@479 418 case (p1, p2) of
adamc@479 419 (PWild, PWild) => EQUAL
adamc@479 420 | (PWild, _) => LESS
adamc@479 421 | (_, PWild) => GREATER
adamc@479 422
adamc@479 423 | (PVar _, PVar _) => EQUAL
adamc@479 424 | (PVar _, _) => LESS
adamc@479 425 | (_, PVar _) => GREATER
adamc@479 426
adamc@479 427 | (PPrim p1, PPrim p2) => Prim.compare (p1, p2)
adamc@479 428 | (PPrim _, _) => LESS
adamc@479 429 | (_, PPrim _) => GREATER
adamc@479 430
adamc@479 431 | (PCon (_, pc1, _, po1), PCon (_, pc2, _, po2)) =>
adamc@479 432 join (pcCompare (pc1, pc2),
adamc@479 433 fn () => joinO pCompare (po1, po2))
adamc@479 434 | (PCon _, _) => LESS
adamc@479 435 | (_, PCon _) => GREATER
adamc@479 436
adamc@479 437 | (PRecord xps1, PRecord xps2) =>
adamc@479 438 joinL (fn ((x1, p1, _), (x2, p2, _)) =>
adamc@479 439 join (String.compare (x1, x2),
adamc@479 440 fn () => pCompare (p1, p2))) (xps1, xps2)
adamc@479 441
adamc@479 442 fun compare ((e1, _), (e2, _)) =
adamc@479 443 case (e1, e2) of
adamc@479 444 (EPrim p1, EPrim p2) => Prim.compare (p1, p2)
adamc@479 445 | (EPrim _, _) => LESS
adamc@479 446 | (_, EPrim _) => GREATER
adamc@479 447
adamc@479 448 | (ERel n1, ERel n2) => Int.compare (n1, n2)
adamc@479 449 | (ERel _, _) => LESS
adamc@479 450 | (_, ERel _) => GREATER
adamc@479 451
adamc@479 452 | (ENamed n1, ENamed n2) => Int.compare (n1, n2)
adamc@479 453 | (ENamed _, _) => LESS
adamc@479 454 | (_, ENamed _) => GREATER
adamc@479 455
adamc@479 456 | (ECon (_, pc1, _, eo1), ECon (_, pc2, _, eo2)) =>
adamc@479 457 join (pcCompare (pc1, pc2),
adamc@479 458 fn () => joinO compare (eo1, eo2))
adamc@479 459 | (ECon _, _) => LESS
adamc@479 460 | (_, ECon _) => GREATER
adamc@479 461
adamc@479 462 | (EFfi (f1, x1), EFfi (f2, x2)) =>
adamc@479 463 join (String.compare (f1, f2),
adamc@479 464 fn () => String.compare (x1, x2))
adamc@479 465 | (EFfi _, _) => LESS
adamc@479 466 | (_, EFfi _) => GREATER
adamc@479 467
adamc@479 468 | (EFfiApp (f1, x1, es1), EFfiApp (f2, x2, es2)) =>
adamc@479 469 join (String.compare (f1, f2),
adamc@479 470 fn () => join (String.compare (x1, x2),
adam@1663 471 fn () => joinL (fn ((e1, _), (e2, _)) => compare (e1, e2))(es1, es2)))
adamc@479 472 | (EFfiApp _, _) => LESS
adamc@479 473 | (_, EFfiApp _) => GREATER
adamc@479 474
adamc@479 475 | (EApp (f1, x1), EApp (f2, x2)) =>
adamc@479 476 join (compare (f1, f2),
adamc@479 477 fn () => compare (x1, x2))
adamc@479 478 | (EApp _, _) => LESS
adamc@479 479 | (_, EApp _) => GREATER
adamc@479 480
adamc@479 481 | (EAbs (_, _, _, e1), EAbs (_, _, _, e2)) => compare (e1, e2)
adamc@479 482 | (EAbs _, _) => LESS
adamc@479 483 | (_, EAbs _) => GREATER
adamc@479 484
adamc@479 485 | (ECApp (f1, x1), ECApp (f2, x2)) =>
adamc@479 486 join (compare (f1, f2),
adamc@479 487 fn () => Con.compare (x1, x2))
adamc@479 488 | (ECApp _, _) => LESS
adamc@479 489 | (_, ECApp _) => GREATER
adamc@479 490
adamc@479 491 | (ECAbs (_, _, e1), ECAbs (_, _, e2)) => compare (e1, e2)
adamc@479 492 | (ECAbs _, _) => LESS
adamc@479 493 | (_, ECAbs _) => GREATER
adamc@479 494
adamc@479 495 | (ERecord xes1, ERecord xes2) =>
adamc@479 496 joinL (fn ((x1, e1, _), (x2, e2, _)) =>
adamc@479 497 join (Con.compare (x1, x2),
adamc@479 498 fn () => compare (e1, e2))) (xes1, xes2)
adamc@479 499 | (ERecord _, _) => LESS
adamc@479 500 | (_, ERecord _) => GREATER
adamc@479 501
adamc@479 502 | (EField (e1, c1, _), EField (e2, c2, _)) =>
adamc@479 503 join (compare (e1, e2),
adamc@479 504 fn () => Con.compare (c1, c2))
adamc@479 505 | (EField _, _) => LESS
adamc@479 506 | (_, EField _) => GREATER
adamc@479 507
adamc@479 508 | (EConcat (x1, _, y1, _), EConcat (x2, _, y2, _)) =>
adamc@479 509 join (compare (x1, x2),
adamc@479 510 fn () => compare (y1, y2))
adamc@479 511 | (EConcat _, _) => LESS
adamc@479 512 | (_, EConcat _) => GREATER
adamc@479 513
adamc@479 514 | (ECut (e1, c1, _), ECut (e2, c2, _)) =>
adamc@479 515 join (compare (e1, e2),
adamc@493 516 fn () => Con.compare (c1, c2))
adamc@479 517 | (ECut _, _) => LESS
adamc@479 518 | (_, ECut _) => GREATER
adamc@479 519
adamc@493 520 | (ECutMulti (e1, c1, _), ECutMulti (e2, c2, _)) =>
adamc@493 521 join (compare (e1, e2),
adamc@493 522 fn () => Con.compare (c1, c2))
adamc@493 523 | (ECutMulti _, _) => LESS
adamc@493 524 | (_, ECutMulti _) => GREATER
adamc@493 525
adamc@479 526 | (ECase (e1, pes1, _), ECase (e2, pes2, _)) =>
adamc@479 527 join (compare (e1, e2),
adamc@479 528 fn () => joinL (fn ((p1, e1), (p2, e2)) =>
adamc@479 529 join (pCompare (p1, p2),
adamc@479 530 fn () => compare (e1, e2))) (pes1, pes2))
adamc@479 531 | (ECase _, _) => LESS
adamc@479 532 | (_, ECase _) => GREATER
adamc@479 533
adamc@479 534 | (EWrite e1, EWrite e2) => compare (e1, e2)
adamc@479 535 | (EWrite _, _) => LESS
adamc@479 536 | (_, EWrite _) => GREATER
adamc@479 537
adamc@479 538 | (EClosure (n1, es1), EClosure (n2, es2)) =>
adamc@479 539 join (Int.compare (n1, n2),
adamc@479 540 fn () => joinL compare (es1, es2))
adamc@479 541 | (EClosure _, _) => LESS
adamc@479 542 | (_, EClosure _) => GREATER
adamc@479 543
adamc@479 544 | (ELet (_, _, x1, e1), ELet (_, _, x2, e2)) =>
adamc@479 545 join (compare (x1, x2),
adamc@479 546 fn () => compare (e1, e2))
adamc@607 547 | (ELet _, _) => LESS
adamc@607 548 | (_, ELet _) => GREATER
adamc@607 549
adamc@1020 550 | (EServerCall (n1, es1, _), EServerCall (n2, es2, _)) =>
adamc@607 551 join (Int.compare (n1, n2),
adamc@1020 552 fn () => joinL compare (es1, es2))
adamc@626 553 | (EServerCall _, _) => LESS
adamc@626 554 | (_, EServerCall _) => GREATER
adamc@626 555
adamc@626 556 | (EKAbs (_, e1), EKAbs (_, e2)) => compare (e1, e2)
adamc@626 557 | (EKAbs _, _) => LESS
adamc@626 558 | (_, EKAbs _) => GREATER
adamc@626 559
adamc@626 560 | (EKApp (e1, k1), EKApp (e2, k2)) =>
adamc@626 561 join (compare (e1, e2),
adamc@626 562 fn () => Kind.compare (k1, k2))
adamc@479 563
adamc@16 564 datatype binder =
adamc@626 565 RelK of string
adamc@626 566 | RelC of string * kind
adamc@20 567 | NamedC of string * int * kind * con option
adamc@16 568 | RelE of string * con
adamc@109 569 | NamedE of string * int * con * exp option * string
adamc@16 570
adamc@16 571 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@16 572 let
adamc@626 573 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@16 574
adamc@16 575 fun bind' (ctx, b) =
adamc@16 576 let
adamc@16 577 val b' = case b of
adamc@626 578 Con.RelK x => RelK x
adamc@626 579 | Con.RelC x => RelC x
adamc@626 580 | Con.NamedC x => NamedC x
adamc@16 581 in
adamc@16 582 bind (ctx, b')
adamc@16 583 end
adamc@16 584 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@16 585
adamc@16 586 fun mfe ctx e acc =
adamc@16 587 S.bindP (mfe' ctx e acc, fe ctx)
adamc@16 588
adam@1663 589 and mfet ctx (e, t) =
adam@1663 590 S.bind2 (mfe ctx e,
adam@1663 591 fn e' =>
adam@1663 592 S.map2 (mfc ctx t,
adam@1663 593 fn t' => (e', t')))
adam@1663 594
adamc@16 595 and mfe' ctx (eAll as (e, loc)) =
adamc@16 596 case e of
adamc@16 597 EPrim _ => S.return2 eAll
adamc@16 598 | ERel _ => S.return2 eAll
adamc@16 599 | ENamed _ => S.return2 eAll
adamc@192 600 | ECon (dk, pc, cs, NONE) =>
adamc@192 601 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 602 fn cs' =>
adamc@192 603 (ECon (dk, pc, cs', NONE), loc))
adamc@192 604 | ECon (dk, n, cs, SOME e) =>
adamc@192 605 S.bind2 (mfe ctx e,
adamc@192 606 fn e' =>
adamc@192 607 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 608 fn cs' =>
adamc@192 609 (ECon (dk, n, cs', SOME e'), loc)))
adamc@48 610 | EFfi _ => S.return2 eAll
adamc@48 611 | EFfiApp (m, x, es) =>
adam@1663 612 S.map2 (ListUtil.mapfold (mfet ctx) es,
adamc@48 613 fn es' =>
adamc@48 614 (EFfiApp (m, x, es'), loc))
adamc@16 615 | EApp (e1, e2) =>
adamc@16 616 S.bind2 (mfe ctx e1,
adamc@16 617 fn e1' =>
adamc@16 618 S.map2 (mfe ctx e2,
adamc@16 619 fn e2' =>
adamc@16 620 (EApp (e1', e2'), loc)))
adamc@26 621 | EAbs (x, dom, ran, e) =>
adamc@26 622 S.bind2 (mfc ctx dom,
adamc@26 623 fn dom' =>
adamc@26 624 S.bind2 (mfc ctx ran,
adamc@26 625 fn ran' =>
adamc@26 626 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 627 fn e' =>
adamc@26 628 (EAbs (x, dom', ran', e'), loc))))
adamc@16 629
adamc@16 630 | ECApp (e, c) =>
adamc@16 631 S.bind2 (mfe ctx e,
adamc@16 632 fn e' =>
adamc@16 633 S.map2 (mfc ctx c,
adamc@16 634 fn c' =>
adamc@16 635 (ECApp (e', c'), loc)))
adamc@16 636 | ECAbs (x, k, e) =>
adamc@626 637 S.bind2 (mfk ctx k,
adamc@16 638 fn k' =>
adamc@16 639 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@16 640 fn e' =>
adamc@16 641 (ECAbs (x, k', e'), loc)))
adamc@16 642
adamc@16 643 | ERecord xes =>
adamc@29 644 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@16 645 S.bind2 (mfc ctx x,
adamc@16 646 fn x' =>
adamc@29 647 S.bind2 (mfe ctx e,
adamc@29 648 fn e' =>
adamc@29 649 S.map2 (mfc ctx t,
adamc@29 650 fn t' =>
adamc@29 651 (x', e', t')))))
adamc@16 652 xes,
adamc@16 653 fn xes' =>
adamc@16 654 (ERecord xes', loc))
adamc@16 655 | EField (e, c, {field, rest}) =>
adamc@16 656 S.bind2 (mfe ctx e,
adamc@16 657 fn e' =>
adamc@16 658 S.bind2 (mfc ctx c,
adamc@16 659 fn c' =>
adamc@16 660 S.bind2 (mfc ctx field,
adamc@16 661 fn field' =>
adamc@16 662 S.map2 (mfc ctx rest,
adamc@16 663 fn rest' =>
adamc@16 664 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@445 665 | EConcat (e1, c1, e2, c2) =>
adamc@339 666 S.bind2 (mfe ctx e1,
adamc@339 667 fn e1' =>
adamc@445 668 S.bind2 (mfc ctx c1,
adamc@445 669 fn c1' =>
adamc@339 670 S.bind2 (mfe ctx e2,
adamc@339 671 fn e2' =>
adamc@445 672 S.map2 (mfc ctx c2,
adamc@445 673 fn c2' =>
adamc@445 674 (EConcat (e1', c1', e2', c2'),
adamc@445 675 loc)))))
adamc@149 676 | ECut (e, c, {field, rest}) =>
adamc@149 677 S.bind2 (mfe ctx e,
adamc@149 678 fn e' =>
adamc@149 679 S.bind2 (mfc ctx c,
adamc@149 680 fn c' =>
adamc@149 681 S.bind2 (mfc ctx field,
adamc@149 682 fn field' =>
adamc@149 683 S.map2 (mfc ctx rest,
adamc@149 684 fn rest' =>
adamc@149 685 (ECut (e', c', {field = field', rest = rest'}), loc)))))
adamc@493 686 | ECutMulti (e, c, {rest}) =>
adamc@493 687 S.bind2 (mfe ctx e,
adamc@493 688 fn e' =>
adamc@493 689 S.bind2 (mfc ctx c,
adamc@493 690 fn c' =>
adamc@493 691 S.map2 (mfc ctx rest,
adamc@493 692 fn rest' =>
adamc@493 693 (ECutMulti (e', c', {rest = rest'}), loc))))
adamc@102 694
adamc@182 695 | ECase (e, pes, {disc, result}) =>
adamc@177 696 S.bind2 (mfe ctx e,
adamc@177 697 fn e' =>
adamc@177 698 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@193 699 let
adamc@193 700 fun pb ((p, _), ctx) =
adamc@193 701 case p of
adamc@193 702 PWild => ctx
adamc@193 703 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@193 704 | PPrim _ => ctx
adamc@193 705 | PCon (_, _, _, NONE) => ctx
adamc@193 706 | PCon (_, _, _, SOME p) => pb (p, ctx)
adamc@193 707 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@193 708 pb (p, ctx)) ctx xps
adamc@193 709 in
adamc@193 710 S.bind2 (mfp ctx p,
adamc@193 711 fn p' =>
adamc@193 712 S.map2 (mfe (pb (p', ctx)) e,
adamc@193 713 fn e' => (p', e')))
adamc@193 714 end) pes,
adamc@177 715 fn pes' =>
adamc@182 716 S.bind2 (mfc ctx disc,
adamc@182 717 fn disc' =>
adamc@182 718 S.map2 (mfc ctx result,
adamc@182 719 fn result' =>
adamc@182 720 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@177 721
adamc@102 722 | EWrite e =>
adamc@102 723 S.map2 (mfe ctx e,
adamc@102 724 fn e' =>
adamc@102 725 (EWrite e', loc))
adamc@110 726
adamc@110 727 | EClosure (n, es) =>
adamc@110 728 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@110 729 fn es' =>
adamc@110 730 (EClosure (n, es'), loc))
adamc@193 731
adamc@450 732 | ELet (x, t, e1, e2) =>
adamc@450 733 S.bind2 (mfc ctx t,
adamc@450 734 fn t' =>
adamc@450 735 S.bind2 (mfe ctx e1,
adamc@450 736 fn e1' =>
adamc@453 737 S.map2 (mfe (bind (ctx, RelE (x, t'))) e2,
adamc@450 738 fn e2' =>
adamc@450 739 (ELet (x, t', e1', e2'), loc))))
adamc@450 740
adamc@1020 741 | EServerCall (n, es, t) =>
adamc@607 742 S.bind2 (ListUtil.mapfold (mfe ctx) es,
adamc@607 743 fn es' =>
adamc@1020 744 S.map2 (mfc ctx t,
adamc@1020 745 fn t' =>
adamc@1020 746 (EServerCall (n, es', t'), loc)))
adamc@954 747
adamc@626 748 | EKAbs (x, e) =>
adamc@626 749 S.map2 (mfe (bind (ctx, RelK x)) e,
adamc@626 750 fn e' =>
adamc@626 751 (EKAbs (x, e'), loc))
adamc@626 752 | EKApp (e, k) =>
adamc@626 753 S.bind2 (mfe ctx e,
adamc@626 754 fn e' =>
adamc@626 755 S.map2 (mfk ctx k,
adamc@626 756 fn k' =>
adamc@626 757 (EKApp (e', k'), loc)))
adamc@607 758
adamc@193 759 and mfp ctx (pAll as (p, loc)) =
adamc@193 760 case p of
adamc@193 761 PWild => S.return2 pAll
adamc@193 762 | PVar (x, t) =>
adamc@193 763 S.map2 (mfc ctx t,
adamc@193 764 fn t' =>
adamc@193 765 (PVar (x, t'), loc))
adamc@193 766 | PPrim _ => S.return2 pAll
adamc@193 767 | PCon (dk, pc, args, po) =>
adamc@193 768 S.bind2 (mfpc ctx pc,
adamc@193 769 fn pc' =>
adamc@193 770 S.bind2 (ListUtil.mapfold (mfc ctx) args,
adamc@193 771 fn args' =>
adamc@193 772 S.map2 ((case po of
adamc@193 773 NONE => S.return2 NONE
adamc@193 774 | SOME p => S.map2 (mfp ctx p, SOME)),
adamc@193 775 fn po' =>
adamc@193 776 (PCon (dk, pc', args', po'), loc))))
adamc@193 777 | PRecord xps =>
adamc@193 778 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
adamc@193 779 S.bind2 (mfp ctx p,
adamc@193 780 fn p' =>
adamc@193 781 S.map2 (mfc ctx c,
adamc@193 782 fn c' =>
adamc@193 783 (x, p', c')))) xps,
adamc@193 784 fn xps' =>
adamc@193 785 (PRecord xps', loc))
adamc@193 786
adamc@193 787 and mfpc ctx pc =
adamc@193 788 case pc of
adamc@193 789 PConVar _ => S.return2 pc
adamc@193 790 | PConFfi {mod = m, datatyp, params, con, arg, kind} =>
adamc@193 791 S.map2 ((case arg of
adamc@193 792 NONE => S.return2 NONE
adamc@796 793 | SOME c =>
adamc@796 794 let
adamc@796 795 val k = (KType, ErrorMsg.dummySpan)
adamc@796 796 val ctx' = foldl (fn (x, ctx) => bind (ctx, RelC (x, k))) ctx params
adamc@796 797 in
adamc@796 798 S.map2 (mfc ctx' c, SOME)
adamc@796 799 end),
adamc@193 800 fn arg' =>
adamc@193 801 PConFfi {mod = m, datatyp = datatyp, params = params,
adamc@193 802 con = con, arg = arg', kind = kind})
adamc@16 803 in
adamc@16 804 mfe
adamc@16 805 end
adamc@16 806
adamc@16 807 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@626 808 mapfoldB {kind = fn () => fk,
adamc@16 809 con = fn () => fc,
adamc@16 810 exp = fn () => fe,
adamc@16 811 bind = fn ((), _) => ()} ()
adamc@16 812
adamc@21 813 fun mapB {kind, con, exp, bind} ctx e =
adamc@626 814 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@21 815 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@21 816 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@21 817 bind = bind} ctx e () of
adamc@21 818 S.Continue (e, ()) => e
adamc@21 819 | S.Return _ => raise Fail "CoreUtil.Exp.mapB: Impossible"
adamc@21 820
adamc@16 821 fun map {kind, con, exp} e =
adamc@16 822 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 823 con = fn c => fn () => S.Continue (con c, ()),
adamc@16 824 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@16 825 S.Return () => raise Fail "Core_util.Exp.map"
adamc@16 826 | S.Continue (e, ()) => e
adamc@16 827
adamc@23 828 fun fold {kind, con, exp} s e =
adamc@23 829 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 830 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 831 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@23 832 S.Continue (_, s) => s
adamc@23 833 | S.Return _ => raise Fail "CoreUtil.Exp.fold: Impossible"
adamc@23 834
adamc@484 835 fun foldB {kind, con, exp, bind} ctx s e =
adamc@626 836 case mapfoldB {kind = fn ctx => fn k => fn s => S.Continue (k, kind (ctx, k, s)),
adamc@484 837 con = fn ctx => fn c => fn s => S.Continue (c, con (ctx, c, s)),
adamc@484 838 exp = fn ctx => fn e => fn s => S.Continue (e, exp (ctx, e, s)),
adamc@484 839 bind = bind} ctx e s of
adamc@484 840 S.Continue (_, s) => s
adamc@484 841 | S.Return _ => raise Fail "CoreUtil.Exp.foldB: Impossible"
adamc@484 842
adamc@16 843 fun exists {kind, con, exp} k =
adamc@16 844 case mapfold {kind = fn k => fn () =>
adamc@16 845 if kind k then
adamc@16 846 S.Return ()
adamc@16 847 else
adamc@16 848 S.Continue (k, ()),
adamc@16 849 con = fn c => fn () =>
adamc@16 850 if con c then
adamc@16 851 S.Return ()
adamc@16 852 else
adamc@16 853 S.Continue (c, ()),
adamc@16 854 exp = fn e => fn () =>
adamc@16 855 if exp e then
adamc@16 856 S.Return ()
adamc@16 857 else
adamc@16 858 S.Continue (e, ())} k () of
adamc@16 859 S.Return _ => true
adamc@16 860 | S.Continue _ => false
adamc@16 861
adamc@479 862 fun existsB {kind, con, exp, bind} ctx k =
adamc@626 863 case mapfoldB {kind = fn ctx => fn k => fn () =>
adamc@626 864 if kind (ctx, k) then
adamc@626 865 S.Return ()
adamc@626 866 else
adamc@626 867 S.Continue (k, ()),
adamc@479 868 con = fn ctx => fn c => fn () =>
adamc@479 869 if con (ctx, c) then
adamc@479 870 S.Return ()
adamc@479 871 else
adamc@479 872 S.Continue (c, ()),
adamc@479 873 exp = fn ctx => fn e => fn () =>
adamc@479 874 if exp (ctx, e) then
adamc@479 875 S.Return ()
adamc@479 876 else
adamc@479 877 S.Continue (e, ()),
adamc@479 878 bind = bind} ctx k () of
adamc@479 879 S.Return _ => true
adamc@479 880 | S.Continue _ => false
adamc@479 881
adamc@443 882 fun foldMap {kind, con, exp} s e =
adamc@443 883 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@443 884 con = fn c => fn s => S.Continue (con (c, s)),
adamc@443 885 exp = fn e => fn s => S.Continue (exp (e, s))} e s of
adamc@443 886 S.Continue v => v
adamc@443 887 | S.Return _ => raise Fail "CoreUtil.Exp.foldMap: Impossible"
adamc@443 888
adamc@488 889 fun foldMapB {kind, con, exp, bind} ctx s e =
adamc@626 890 case mapfoldB {kind = fn ctx => fn k => fn s => S.Continue (kind (ctx, k, s)),
adamc@488 891 con = fn ctx => fn c => fn s => S.Continue (con (ctx, c, s)),
adamc@488 892 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
adamc@488 893 bind = bind} ctx e s of
adamc@488 894 S.Continue v => v
adamc@488 895 | S.Return _ => raise Fail "CoreUtil.Exp.foldMapB: Impossible"
adamc@488 896
adamc@16 897 end
adamc@16 898
adamc@20 899 structure Decl = struct
adamc@20 900
adamc@20 901 datatype binder = datatype Exp.binder
adamc@20 902
adamc@20 903 fun mapfoldB {kind = fk, con = fc, exp = fe, decl = fd, bind} =
adamc@20 904 let
adamc@626 905 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@20 906
adamc@20 907 fun bind' (ctx, b) =
adamc@20 908 let
adamc@20 909 val b' = case b of
adamc@626 910 Con.RelK x => RelK x
adamc@626 911 | Con.RelC x => RelC x
adamc@626 912 | Con.NamedC x => NamedC x
adamc@20 913 in
adamc@20 914 bind (ctx, b')
adamc@20 915 end
adamc@20 916 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@20 917
adamc@20 918 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind}
adamc@20 919
adamc@20 920 fun mfd ctx d acc =
adamc@20 921 S.bindP (mfd' ctx d acc, fd ctx)
adamc@20 922
adamc@20 923 and mfd' ctx (dAll as (d, loc)) =
adamc@20 924 case d of
adamc@20 925 DCon (x, n, k, c) =>
adamc@626 926 S.bind2 (mfk ctx k,
adamc@20 927 fn k' =>
adamc@20 928 S.map2 (mfc ctx c,
adamc@20 929 fn c' =>
adamc@20 930 (DCon (x, n, k', c'), loc)))
adamc@807 931 | DDatatype dts =>
adamc@807 932 S.map2 (ListUtil.mapfold (fn (x, n, xs, xncs) =>
adamc@807 933 let
adamc@807 934 val k = (KType, loc)
adamc@807 935 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@807 936 val ctx' = bind (ctx, NamedC (x, n, k', NONE))
adamc@807 937 in
adamc@807 938 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@807 939 case c of
adamc@807 940 NONE => S.return2 (x, n, c)
adamc@807 941 | SOME c =>
adamc@807 942 S.map2 (mfc ctx' c,
adamc@807 943 fn c' => (x, n, SOME c'))) xncs,
adamc@807 944 fn xncs' => (x, n, xs, xncs'))
adamc@807 945 end) dts,
adamc@807 946 fn dts' =>
adamc@807 947 (DDatatype dts', loc))
adamc@125 948 | DVal vi =>
adamc@125 949 S.map2 (mfvi ctx vi,
adamc@125 950 fn vi' =>
adamc@125 951 (DVal vi', loc))
adamc@125 952 | DValRec vis =>
adamc@127 953 let
adamc@127 954 val ctx = foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@127 955 ctx vis
adamc@127 956 in
adamc@127 957 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@127 958 fn vis' =>
adamc@127 959 (DValRec vis', loc))
adamc@127 960 end
adamc@109 961 | DExport _ => S.return2 dAll
adamc@707 962 | DTable (x, n, c, s, pe, pc, ce, cc) =>
adamc@802 963 let
adamc@802 964 val loc = #2 ce
adamc@802 965 val ct = (CFfi ("Basis", "sql_table"), loc)
adamc@802 966 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
adamc@802 967 val ct = (CApp (ct, cc), loc)
adamc@802 968 val ctx' = bind (ctx, NamedE (x, n, ct, NONE, s))
adamc@802 969 in
adamc@802 970 S.bind2 (mfc ctx c,
adamc@802 971 fn c' =>
adamc@802 972 S.bind2 (mfe ctx' pe,
adamc@802 973 fn pe' =>
adamc@802 974 S.bind2 (mfc ctx pc,
adamc@802 975 fn pc' =>
adamc@802 976 S.bind2 (mfe ctx' ce,
adamc@802 977 fn ce' =>
adamc@802 978 S.map2 (mfc ctx cc,
adamc@802 979 fn cc' =>
adamc@802 980 (DTable (x, n, c', s, pe', pc', ce', cc'), loc))))))
adamc@802 981 end
adamc@338 982 | DSequence _ => S.return2 dAll
adamc@754 983 | DView (x, n, s, e, c) =>
adamc@754 984 S.bind2 (mfe ctx e,
adamc@754 985 fn e' =>
adamc@754 986 S.map2 (mfc ctx c,
adamc@754 987 fn c' =>
adamc@754 988 (DView (x, n, s, e', c'), loc)))
adamc@271 989 | DDatabase _ => S.return2 dAll
adamc@461 990 | DCookie (x, n, c, s) =>
adamc@461 991 S.map2 (mfc ctx c,
adamc@461 992 fn c' =>
adamc@461 993 (DCookie (x, n, c', s), loc))
adamc@720 994 | DStyle _ => S.return2 dAll
adamc@1075 995 | DTask (e1, e2) =>
adamc@1075 996 S.bind2 (mfe ctx e1,
adamc@1075 997 fn e1' =>
adamc@1075 998 S.map2 (mfe ctx e2,
adamc@1075 999 fn e2' =>
adamc@1075 1000 (DTask (e1', e2'), loc)))
adamc@1199 1001 | DPolicy e =>
adamc@1199 1002 S.map2 (mfe ctx e,
adamc@1199 1003 fn e' =>
adamc@1199 1004 (DPolicy e', loc))
adamc@125 1005
adam@1294 1006 | DOnError _ => S.return2 dAll
adam@1294 1007
adamc@125 1008 and mfvi ctx (x, n, t, e, s) =
adamc@125 1009 S.bind2 (mfc ctx t,
adamc@125 1010 fn t' =>
adamc@125 1011 S.map2 (mfe ctx e,
adamc@125 1012 fn e' =>
adamc@125 1013 (x, n, t', e', s)))
adamc@20 1014 in
adamc@20 1015 mfd
adamc@20 1016 end
adamc@20 1017
adamc@23 1018 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@626 1019 mapfoldB {kind = fn () => fk,
adamc@23 1020 con = fn () => fc,
adamc@23 1021 exp = fn () => fe,
adamc@23 1022 decl = fn () => fd,
adamc@23 1023 bind = fn ((), _) => ()} ()
adamc@23 1024
adamc@23 1025 fun fold {kind, con, exp, decl} s d =
adamc@23 1026 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 1027 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 1028 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 1029 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 1030 S.Continue (_, s) => s
adamc@23 1031 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
adamc@23 1032
adamc@110 1033 fun foldMap {kind, con, exp, decl} s d =
adamc@110 1034 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 1035 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 1036 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 1037 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 1038 S.Continue v => v
adamc@110 1039 | S.Return _ => raise Fail "CoreUtil.Decl.foldMap: Impossible"
adamc@110 1040
adamc@484 1041 fun foldMapB {kind, con, exp, decl, bind} ctx s d =
adamc@626 1042 case mapfoldB {kind = fn ctx => fn k => fn s => S.Continue (kind (ctx, k, s)),
adamc@484 1043 con = fn ctx => fn c => fn s => S.Continue (con (ctx, c, s)),
adamc@484 1044 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
adamc@484 1045 decl = fn ctx => fn d => fn s => S.Continue (decl (ctx, d, s)),
adamc@484 1046 bind = bind} ctx d s of
adamc@484 1047 S.Continue v => v
adamc@484 1048 | S.Return _ => raise Fail "CoreUtil.Decl.foldMapB: Impossible"
adamc@484 1049
adamc@522 1050 fun exists {kind, con, exp, decl} d =
adamc@522 1051 case mapfold {kind = fn k => fn () =>
adamc@522 1052 if kind k then
adamc@522 1053 S.Return ()
adamc@522 1054 else
adamc@522 1055 S.Continue (k, ()),
adamc@522 1056 con = fn c => fn () =>
adamc@522 1057 if con c then
adamc@522 1058 S.Return ()
adamc@522 1059 else
adamc@522 1060 S.Continue (c, ()),
adamc@522 1061 exp = fn e => fn () =>
adamc@522 1062 if exp e then
adamc@522 1063 S.Return ()
adamc@522 1064 else
adamc@522 1065 S.Continue (e, ()),
adamc@522 1066 decl = fn d => fn () =>
adamc@522 1067 if decl d then
adamc@522 1068 S.Return ()
adamc@522 1069 else
adamc@522 1070 S.Continue (d, ())} d () of
adamc@522 1071 S.Return _ => true
adamc@522 1072 | S.Continue _ => false
adamc@522 1073
adamc@16 1074 end
adamc@20 1075
adamc@20 1076 structure File = struct
adamc@20 1077
adamc@20 1078 datatype binder = datatype Exp.binder
adamc@20 1079
adamc@20 1080 fun mapfoldB (all as {bind, ...}) =
adamc@20 1081 let
adamc@20 1082 val mfd = Decl.mapfoldB all
adamc@20 1083
adamc@20 1084 fun mff ctx ds =
adamc@20 1085 case ds of
adamc@20 1086 nil => S.return2 nil
adamc@20 1087 | d :: ds' =>
adamc@20 1088 S.bind2 (mfd ctx d,
adamc@20 1089 fn d' =>
adamc@20 1090 let
adamc@100 1091 val ctx' =
adamc@20 1092 case #1 d' of
adamc@100 1093 DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c))
adamc@807 1094 | DDatatype dts =>
adamc@807 1095 foldl (fn ((x, n, xs, xncs), ctx) =>
adamc@807 1096 let
adamc@807 1097 val loc = #2 d'
adamc@807 1098 val k = (KType, loc)
adamc@807 1099 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@192 1100
adamc@807 1101 val ctx = bind (ctx, NamedC (x, n, k', NONE))
adamc@807 1102 val t = (CNamed n, #2 d')
adamc@807 1103 val t = ListUtil.foldli (fn (i, _, t) =>
adamc@807 1104 (CApp (t, (CRel i, loc)), loc))
adamc@807 1105 t xs
adamc@807 1106 in
adamc@807 1107 foldl (fn ((x, n, to), ctx) =>
adamc@807 1108 let
adamc@807 1109 val t = case to of
adamc@807 1110 NONE => t
adamc@807 1111 | SOME t' => (TFun (t', t), #2 d')
adamc@807 1112 val t = foldr (fn (x, t) => (TCFun (x, k, t), loc))
adamc@807 1113 t xs
adamc@807 1114 in
adamc@807 1115 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@807 1116 end)
adamc@807 1117 ctx xncs
adamc@807 1118 end)
adamc@807 1119 ctx dts
adamc@109 1120 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@125 1121 | DValRec vis =>
adamc@127 1122 foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@125 1123 ctx vis
adamc@109 1124 | DExport _ => ctx
adamc@707 1125 | DTable (x, n, c, s, _, pc, _, cc) =>
adamc@247 1126 let
adamc@705 1127 val loc = #2 d'
adamc@705 1128 val ct = (CFfi ("Basis", "sql_table"), loc)
adamc@707 1129 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
adamc@705 1130 val ct = (CApp (ct, cc), loc)
adamc@338 1131 in
adamc@705 1132 bind (ctx, NamedE (x, n, ct, NONE, s))
adamc@338 1133 end
adamc@338 1134 | DSequence (x, n, s) =>
adamc@338 1135 let
adamc@338 1136 val t = (CFfi ("Basis", "sql_sequence"), #2 d')
adamc@247 1137 in
adamc@247 1138 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@247 1139 end
adamc@754 1140 | DView (x, n, s, _, c) =>
adamc@754 1141 let
adamc@754 1142 val loc = #2 d'
adamc@754 1143 val ct = (CFfi ("Basis", "sql_view"), loc)
adamc@754 1144 val ct = (CApp (ct, c), loc)
adamc@754 1145 in
adamc@754 1146 bind (ctx, NamedE (x, n, ct, NONE, s))
adamc@754 1147 end
adamc@271 1148 | DDatabase _ => ctx
adamc@461 1149 | DCookie (x, n, c, s) =>
adamc@461 1150 let
adamc@461 1151 val t = (CApp ((CFfi ("Basis", "http_cookie"), #2 d'), c), #2 d')
adamc@461 1152 in
adamc@461 1153 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@461 1154 end
adamc@720 1155 | DStyle (x, n, s) =>
adamc@718 1156 let
adamc@720 1157 val t = (CFfi ("Basis", "css_class"), #2 d')
adamc@718 1158 in
adamc@718 1159 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@718 1160 end
adamc@1075 1161 | DTask _ => ctx
adamc@1199 1162 | DPolicy _ => ctx
adam@1294 1163 | DOnError _ => ctx
adamc@20 1164 in
adamc@20 1165 S.map2 (mff ctx' ds',
adamc@20 1166 fn ds' =>
adamc@20 1167 d' :: ds')
adamc@20 1168 end)
adamc@20 1169 in
adamc@20 1170 mff
adamc@20 1171 end
adamc@20 1172
adamc@23 1173 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@626 1174 mapfoldB {kind = fn () => fk,
adamc@23 1175 con = fn () => fc,
adamc@23 1176 exp = fn () => fe,
adamc@23 1177 decl = fn () => fd,
adamc@23 1178 bind = fn ((), _) => ()} ()
adamc@23 1179
adamc@20 1180 fun mapB {kind, con, exp, decl, bind} ctx ds =
adamc@626 1181 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@20 1182 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@20 1183 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@20 1184 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@20 1185 bind = bind} ctx ds () of
adamc@20 1186 S.Continue (ds, ()) => ds
adamc@20 1187 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
adamc@20 1188
adamc@482 1189 fun map {kind, con, exp, decl} ds =
adamc@626 1190 mapB {kind = fn () => kind,
adamc@482 1191 con = fn () => con,
adamc@482 1192 exp = fn () => exp,
adamc@482 1193 decl = fn () => decl,
adamc@482 1194 bind = fn _ => ()} () ds
adamc@482 1195
adamc@23 1196 fun fold {kind, con, exp, decl} s d =
adamc@23 1197 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 1198 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 1199 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 1200 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 1201 S.Continue (_, s) => s
adamc@23 1202 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
adamc@23 1203
adamc@110 1204 fun foldMap {kind, con, exp, decl} s d =
adamc@110 1205 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 1206 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 1207 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 1208 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 1209 S.Continue v => v
adamc@110 1210 | S.Return _ => raise Fail "CoreUtil.File.foldMap: Impossible"
adamc@110 1211
adamc@179 1212 val maxName = foldl (fn ((d, _) : decl, count) =>
adamc@179 1213 case d of
adamc@179 1214 DCon (_, n, _, _) => Int.max (n, count)
adamc@807 1215 | DDatatype dts => foldl (fn ((_, n, _, ns), count) =>
adamc@807 1216 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@807 1217 (Int.max (n, count)) ns) count dts
adamc@179 1218 | DVal (_, n, _, _, _) => Int.max (n, count)
adamc@179 1219 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
adamc@247 1220 | DExport _ => count
adamc@707 1221 | DTable (_, n, _, _, _, _, _, _) => Int.max (n, count)
adamc@338 1222 | DSequence (_, n, _) => Int.max (n, count)
adamc@754 1223 | DView (_, n, _, _, _) => Int.max (n, count)
adamc@461 1224 | DDatabase _ => count
adamc@718 1225 | DCookie (_, n, _, _) => Int.max (n, count)
adamc@1073 1226 | DStyle (_, n, _) => Int.max (n, count)
adamc@1199 1227 | DTask _ => count
adam@1294 1228 | DPolicy _ => count
adam@1294 1229 | DOnError _ => count) 0
adamc@179 1230
adamc@20 1231 end
adamc@20 1232
adamc@20 1233 end