annotate src/core_util.sml @ 623:588b9d16b00a

Start of kind polymorphism, up to the point where demo/hello elaborates with updated Basis/Top
author Adam Chlipala <adamc@hcoop.net>
date Sun, 22 Feb 2009 16:10:25 -0500
parents 8998114760c1
children 230654093b51
rev   line source
adamc@16 1 (* Copyright (c) 2008, Adam Chlipala
adamc@16 2 * All rights reserved.
adamc@16 3 *
adamc@16 4 * Redistribution and use in source and binary forms, with or without
adamc@16 5 * modification, are permitted provided that the following conditions are met:
adamc@16 6 *
adamc@16 7 * - Redistributions of source code must retain the above copyright notice,
adamc@16 8 * this list of conditions and the following disclaimer.
adamc@16 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@16 10 * this list of conditions and the following disclaimer in the documentation
adamc@16 11 * and/or other materials provided with the distribution.
adamc@16 12 * - The names of contributors may not be used to endorse or promote products
adamc@16 13 * derived from this software without specific prior written permission.
adamc@16 14 *
adamc@16 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@16 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@16 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@16 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@16 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@16 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@16 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@16 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@16 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@16 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@16 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@16 26 *)
adamc@16 27
adamc@16 28 structure CoreUtil :> CORE_UTIL = struct
adamc@16 29
adamc@16 30 open Core
adamc@16 31
adamc@16 32 structure S = Search
adamc@16 33
adamc@16 34 structure Kind = struct
adamc@16 35
adamc@193 36 open Order
adamc@193 37
adamc@193 38 fun compare ((k1, _), (k2, _)) =
adamc@193 39 case (k1, k2) of
adamc@193 40 (KType, KType) => EQUAL
adamc@193 41 | (KType, _) => LESS
adamc@193 42 | (_, KType) => GREATER
adamc@193 43
adamc@193 44 | (KArrow (d1, r1), KArrow (d2, r2)) => join (compare (d1, d2), fn () => compare (r1, r2))
adamc@193 45 | (KArrow _, _) => LESS
adamc@193 46 | (_, KArrow _) => GREATER
adamc@193 47
adamc@193 48 | (KName, KName) => EQUAL
adamc@193 49 | (KName, _) => LESS
adamc@193 50 | (_, KName) => GREATER
adamc@193 51
adamc@193 52 | (KRecord k1, KRecord k2) => compare (k1, k2)
adamc@193 53 | (KRecord _, _) => LESS
adamc@193 54 | (_, KRecord _) => GREATER
adamc@193 55
adamc@193 56 | (KUnit, KUnit) => EQUAL
adamc@214 57 | (KUnit, _) => LESS
adamc@214 58 | (_, KUnit) => GREATER
adamc@214 59
adamc@214 60 | (KTuple ks1, KTuple ks2) => joinL compare (ks1, ks2)
adamc@193 61
adamc@16 62 fun mapfold f =
adamc@16 63 let
adamc@16 64 fun mfk k acc =
adamc@16 65 S.bindP (mfk' k acc, f)
adamc@16 66
adamc@16 67 and mfk' (kAll as (k, loc)) =
adamc@16 68 case k of
adamc@16 69 KType => S.return2 kAll
adamc@16 70
adamc@16 71 | KArrow (k1, k2) =>
adamc@16 72 S.bind2 (mfk k1,
adamc@16 73 fn k1' =>
adamc@16 74 S.map2 (mfk k2,
adamc@16 75 fn k2' =>
adamc@16 76 (KArrow (k1', k2'), loc)))
adamc@16 77
adamc@16 78 | KName => S.return2 kAll
adamc@16 79
adamc@16 80 | KRecord k =>
adamc@16 81 S.map2 (mfk k,
adamc@16 82 fn k' =>
adamc@16 83 (KRecord k', loc))
adamc@87 84
adamc@87 85 | KUnit => S.return2 kAll
adamc@214 86
adamc@214 87 | KTuple ks =>
adamc@214 88 S.map2 (ListUtil.mapfold mfk ks,
adamc@214 89 fn ks' =>
adamc@214 90 (KTuple ks', loc))
adamc@16 91 in
adamc@16 92 mfk
adamc@16 93 end
adamc@16 94
adamc@16 95 fun map f k =
adamc@16 96 case mapfold (fn k => fn () => S.Continue (f k, ())) k () of
adamc@16 97 S.Return () => raise Fail "Core_util.Kind.map"
adamc@16 98 | S.Continue (k, ()) => k
adamc@16 99
adamc@16 100 fun exists f k =
adamc@16 101 case mapfold (fn k => fn () =>
adamc@16 102 if f k then
adamc@16 103 S.Return ()
adamc@16 104 else
adamc@16 105 S.Continue (k, ())) k () of
adamc@16 106 S.Return _ => true
adamc@16 107 | S.Continue _ => false
adamc@16 108
adamc@16 109 end
adamc@16 110
adamc@16 111 structure Con = struct
adamc@16 112
adamc@193 113 open Order
adamc@193 114
adamc@193 115 fun compare ((c1, _), (c2, _)) =
adamc@193 116 case (c1, c2) of
adamc@193 117 (TFun (d1, r1), TFun (d2, r2)) => join (compare (d1, d2), fn () => compare (r1, r2))
adamc@193 118 | (TFun _, _) => LESS
adamc@193 119 | (_, TFun _) => GREATER
adamc@193 120
adamc@193 121 | (TCFun (x1, k1, r1), TCFun (x2, k2, r2)) =>
adamc@193 122 join (String.compare (x1, x2),
adamc@193 123 fn () => join (Kind.compare (k1, k2),
adamc@193 124 fn () => compare (r1, r2)))
adamc@193 125 | (TCFun _, _) => LESS
adamc@193 126 | (_, TCFun _) => GREATER
adamc@193 127
adamc@193 128 | (TRecord c1, TRecord c2) => compare (c1, c2)
adamc@193 129 | (TRecord _, _) => LESS
adamc@193 130 | (_, TRecord _) => GREATER
adamc@193 131
adamc@193 132 | (CRel n1, CRel n2) => Int.compare (n1, n2)
adamc@193 133 | (CRel _, _) => LESS
adamc@193 134 | (_, CRel _) => GREATER
adamc@193 135
adamc@193 136 | (CNamed n1, CNamed n2) => Int.compare (n1, n2)
adamc@193 137 | (CNamed _, _) => LESS
adamc@193 138 | (_, CNamed _) => GREATER
adamc@193 139
adamc@193 140 | (CFfi (m1, s1), CFfi (m2, s2)) => join (String.compare (m1, m2),
adamc@193 141 fn () => String.compare (s1, s2))
adamc@193 142 | (CFfi _, _) => LESS
adamc@193 143 | (_, CFfi _) => GREATER
adamc@193 144
adamc@193 145 | (CApp (f1, x1), CApp (f2, x2)) => join (compare (f1, f2),
adamc@193 146 fn () => compare (x1, x2))
adamc@193 147 | (CApp _, _) => LESS
adamc@193 148 | (_, CApp _) => GREATER
adamc@193 149
adamc@193 150 | (CAbs (x1, k1, b1), CAbs (x2, k2, b2)) =>
adamc@193 151 join (String.compare (x1, x2),
adamc@193 152 fn () => join (Kind.compare (k1, k2),
adamc@193 153 fn () => compare (b1, b2)))
adamc@193 154 | (CAbs _, _) => LESS
adamc@193 155 | (_, CAbs _) => GREATER
adamc@193 156
adamc@193 157 | (CName s1, CName s2) => String.compare (s1, s2)
adamc@193 158 | (CName _, _) => LESS
adamc@193 159 | (_, CName _) => GREATER
adamc@193 160
adamc@193 161 | (CRecord (k1, xvs1), CRecord (k2, xvs2)) =>
adamc@193 162 join (Kind.compare (k1, k2),
adamc@250 163 fn () =>
adamc@250 164 let
adamc@250 165 val sort = ListMergeSort.sort (fn ((x1, _), (x2, _)) =>
adamc@250 166 compare (x1, x2) = GREATER)
adamc@250 167 in
adamc@250 168 joinL (fn ((x1, v1), (x2, v2)) =>
adamc@250 169 join (compare (x1, x2),
adamc@250 170 fn () => compare (v1, v2))) (sort xvs1, sort xvs2)
adamc@250 171 end)
adamc@193 172 | (CRecord _, _) => LESS
adamc@193 173 | (_, CRecord _) => GREATER
adamc@193 174
adamc@193 175 | (CConcat (f1, s1), CConcat (f2, s2)) =>
adamc@193 176 join (compare (f1, f2),
adamc@193 177 fn () => compare (s1, s2))
adamc@193 178 | (CConcat _, _) => LESS
adamc@193 179 | (_, CConcat _) => GREATER
adamc@193 180
adamc@621 181 | (CMap (d1, r1), CMap (d2, r2)) =>
adamc@193 182 join (Kind.compare (d1, r2),
adamc@193 183 fn () => Kind.compare (r1, r2))
adamc@621 184 | (CMap _, _) => LESS
adamc@621 185 | (_, CMap _) => GREATER
adamc@193 186
adamc@193 187 | (CUnit, CUnit) => EQUAL
adamc@214 188 | (CUnit, _) => LESS
adamc@214 189 | (_, CUnit) => GREATER
adamc@214 190
adamc@214 191 | (CTuple cs1, CTuple cs2) => joinL compare (cs1, cs2)
adamc@214 192 | (CTuple _, _) => LESS
adamc@214 193 | (_, CTuple _) => GREATER
adamc@214 194
adamc@214 195 | (CProj (c1, n1), CProj (c2, n2)) => join (Int.compare (n1, n2),
adamc@214 196 fn () => compare (c1, c2))
adamc@193 197
adamc@16 198 datatype binder =
adamc@16 199 Rel of string * kind
adamc@20 200 | Named of string * int * kind * con option
adamc@16 201
adamc@16 202 fun mapfoldB {kind = fk, con = fc, bind} =
adamc@16 203 let
adamc@16 204 val mfk = Kind.mapfold fk
adamc@16 205
adamc@16 206 fun mfc ctx c acc =
adamc@16 207 S.bindP (mfc' ctx c acc, fc ctx)
adamc@16 208
adamc@16 209 and mfc' ctx (cAll as (c, loc)) =
adamc@16 210 case c of
adamc@16 211 TFun (c1, c2) =>
adamc@16 212 S.bind2 (mfc ctx c1,
adamc@16 213 fn c1' =>
adamc@16 214 S.map2 (mfc ctx c2,
adamc@16 215 fn c2' =>
adamc@16 216 (TFun (c1', c2'), loc)))
adamc@16 217 | TCFun (x, k, c) =>
adamc@16 218 S.bind2 (mfk k,
adamc@16 219 fn k' =>
adamc@16 220 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
adamc@16 221 fn c' =>
adamc@16 222 (TCFun (x, k', c'), loc)))
adamc@16 223 | TRecord c =>
adamc@16 224 S.map2 (mfc ctx c,
adamc@16 225 fn c' =>
adamc@16 226 (TRecord c', loc))
adamc@16 227
adamc@16 228 | CRel _ => S.return2 cAll
adamc@16 229 | CNamed _ => S.return2 cAll
adamc@48 230 | CFfi _ => S.return2 cAll
adamc@16 231 | CApp (c1, c2) =>
adamc@16 232 S.bind2 (mfc ctx c1,
adamc@16 233 fn c1' =>
adamc@16 234 S.map2 (mfc ctx c2,
adamc@16 235 fn c2' =>
adamc@16 236 (CApp (c1', c2'), loc)))
adamc@16 237 | CAbs (x, k, c) =>
adamc@16 238 S.bind2 (mfk k,
adamc@16 239 fn k' =>
adamc@16 240 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
adamc@16 241 fn c' =>
adamc@16 242 (CAbs (x, k', c'), loc)))
adamc@16 243
adamc@16 244 | CName _ => S.return2 cAll
adamc@16 245
adamc@16 246 | CRecord (k, xcs) =>
adamc@16 247 S.bind2 (mfk k,
adamc@16 248 fn k' =>
adamc@16 249 S.map2 (ListUtil.mapfold (fn (x, c) =>
adamc@16 250 S.bind2 (mfc ctx x,
adamc@16 251 fn x' =>
adamc@16 252 S.map2 (mfc ctx c,
adamc@16 253 fn c' =>
adamc@16 254 (x', c'))))
adamc@16 255 xcs,
adamc@16 256 fn xcs' =>
adamc@16 257 (CRecord (k', xcs'), loc)))
adamc@16 258 | CConcat (c1, c2) =>
adamc@16 259 S.bind2 (mfc ctx c1,
adamc@16 260 fn c1' =>
adamc@16 261 S.map2 (mfc ctx c2,
adamc@16 262 fn c2' =>
adamc@16 263 (CConcat (c1', c2'), loc)))
adamc@621 264 | CMap (k1, k2) =>
adamc@69 265 S.bind2 (mfk k1,
adamc@69 266 fn k1' =>
adamc@69 267 S.map2 (mfk k2,
adamc@69 268 fn k2' =>
adamc@621 269 (CMap (k1', k2'), loc)))
adamc@87 270
adamc@87 271 | CUnit => S.return2 cAll
adamc@214 272
adamc@214 273 | CTuple cs =>
adamc@214 274 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@214 275 fn cs' =>
adamc@214 276 (CTuple cs', loc))
adamc@214 277
adamc@214 278 | CProj (c, n) =>
adamc@214 279 S.map2 (mfc ctx c,
adamc@214 280 fn c' =>
adamc@214 281 (CProj (c', n), loc))
adamc@16 282 in
adamc@16 283 mfc
adamc@16 284 end
adamc@16 285
adamc@16 286 fun mapfold {kind = fk, con = fc} =
adamc@16 287 mapfoldB {kind = fk,
adamc@16 288 con = fn () => fc,
adamc@16 289 bind = fn ((), _) => ()} ()
adamc@16 290
adamc@16 291 fun map {kind, con} c =
adamc@16 292 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 293 con = fn c => fn () => S.Continue (con c, ())} c () of
adamc@16 294 S.Return () => raise Fail "Core_util.Con.map"
adamc@16 295 | S.Continue (c, ()) => c
adamc@16 296
adamc@16 297 fun mapB {kind, con, bind} ctx c =
adamc@16 298 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 299 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@16 300 bind = bind} ctx c () of
adamc@16 301 S.Continue (c, ()) => c
adamc@20 302 | S.Return _ => raise Fail "CoreUtil.Con.mapB: Impossible"
adamc@16 303
adamc@23 304 fun fold {kind, con} s c =
adamc@23 305 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 306 con = fn c => fn s => S.Continue (c, con (c, s))} c s of
adamc@23 307 S.Continue (_, s) => s
adamc@23 308 | S.Return _ => raise Fail "CoreUtil.Con.fold: Impossible"
adamc@23 309
adamc@16 310 fun exists {kind, con} k =
adamc@16 311 case mapfold {kind = fn k => fn () =>
adamc@16 312 if kind k then
adamc@16 313 S.Return ()
adamc@16 314 else
adamc@16 315 S.Continue (k, ()),
adamc@16 316 con = fn c => fn () =>
adamc@16 317 if con c then
adamc@16 318 S.Return ()
adamc@16 319 else
adamc@16 320 S.Continue (c, ())} k () of
adamc@16 321 S.Return _ => true
adamc@16 322 | S.Continue _ => false
adamc@16 323
adamc@193 324 fun foldMap {kind, con} s c =
adamc@193 325 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@193 326 con = fn c => fn s => S.Continue (con (c, s))} c s of
adamc@193 327 S.Continue v => v
adamc@193 328 | S.Return _ => raise Fail "CoreUtil.Con.foldMap: Impossible"
adamc@193 329
adamc@16 330 end
adamc@16 331
adamc@16 332 structure Exp = struct
adamc@16 333
adamc@479 334 open Order
adamc@479 335
adamc@479 336 fun pcCompare (pc1, pc2) =
adamc@479 337 case (pc1, pc2) of
adamc@479 338 (PConVar n1, PConVar n2) => Int.compare (n1, n2)
adamc@479 339 | (PConVar _, _) => LESS
adamc@479 340 | (_, PConVar _) => GREATER
adamc@479 341
adamc@479 342 | (PConFfi {mod = m1, datatyp = d1, con = c1, ...},
adamc@479 343 PConFfi {mod = m2, datatyp = d2, con = c2, ...}) =>
adamc@479 344 join (String.compare (m1, m2),
adamc@479 345 fn () => join (String.compare (d1, d2),
adamc@479 346 fn () => String.compare (c1, c2)))
adamc@479 347
adamc@479 348 fun pCompare ((p1, _), (p2, _)) =
adamc@479 349 case (p1, p2) of
adamc@479 350 (PWild, PWild) => EQUAL
adamc@479 351 | (PWild, _) => LESS
adamc@479 352 | (_, PWild) => GREATER
adamc@479 353
adamc@479 354 | (PVar _, PVar _) => EQUAL
adamc@479 355 | (PVar _, _) => LESS
adamc@479 356 | (_, PVar _) => GREATER
adamc@479 357
adamc@479 358 | (PPrim p1, PPrim p2) => Prim.compare (p1, p2)
adamc@479 359 | (PPrim _, _) => LESS
adamc@479 360 | (_, PPrim _) => GREATER
adamc@479 361
adamc@479 362 | (PCon (_, pc1, _, po1), PCon (_, pc2, _, po2)) =>
adamc@479 363 join (pcCompare (pc1, pc2),
adamc@479 364 fn () => joinO pCompare (po1, po2))
adamc@479 365 | (PCon _, _) => LESS
adamc@479 366 | (_, PCon _) => GREATER
adamc@479 367
adamc@479 368 | (PRecord xps1, PRecord xps2) =>
adamc@479 369 joinL (fn ((x1, p1, _), (x2, p2, _)) =>
adamc@479 370 join (String.compare (x1, x2),
adamc@479 371 fn () => pCompare (p1, p2))) (xps1, xps2)
adamc@479 372
adamc@479 373 fun compare ((e1, _), (e2, _)) =
adamc@479 374 case (e1, e2) of
adamc@479 375 (EPrim p1, EPrim p2) => Prim.compare (p1, p2)
adamc@479 376 | (EPrim _, _) => LESS
adamc@479 377 | (_, EPrim _) => GREATER
adamc@479 378
adamc@479 379 | (ERel n1, ERel n2) => Int.compare (n1, n2)
adamc@479 380 | (ERel _, _) => LESS
adamc@479 381 | (_, ERel _) => GREATER
adamc@479 382
adamc@479 383 | (ENamed n1, ENamed n2) => Int.compare (n1, n2)
adamc@479 384 | (ENamed _, _) => LESS
adamc@479 385 | (_, ENamed _) => GREATER
adamc@479 386
adamc@479 387 | (ECon (_, pc1, _, eo1), ECon (_, pc2, _, eo2)) =>
adamc@479 388 join (pcCompare (pc1, pc2),
adamc@479 389 fn () => joinO compare (eo1, eo2))
adamc@479 390 | (ECon _, _) => LESS
adamc@479 391 | (_, ECon _) => GREATER
adamc@479 392
adamc@479 393 | (EFfi (f1, x1), EFfi (f2, x2)) =>
adamc@479 394 join (String.compare (f1, f2),
adamc@479 395 fn () => String.compare (x1, x2))
adamc@479 396 | (EFfi _, _) => LESS
adamc@479 397 | (_, EFfi _) => GREATER
adamc@479 398
adamc@479 399 | (EFfiApp (f1, x1, es1), EFfiApp (f2, x2, es2)) =>
adamc@479 400 join (String.compare (f1, f2),
adamc@479 401 fn () => join (String.compare (x1, x2),
adamc@479 402 fn () => joinL compare (es1, es2)))
adamc@479 403 | (EFfiApp _, _) => LESS
adamc@479 404 | (_, EFfiApp _) => GREATER
adamc@479 405
adamc@479 406 | (EApp (f1, x1), EApp (f2, x2)) =>
adamc@479 407 join (compare (f1, f2),
adamc@479 408 fn () => compare (x1, x2))
adamc@479 409 | (EApp _, _) => LESS
adamc@479 410 | (_, EApp _) => GREATER
adamc@479 411
adamc@479 412 | (EAbs (_, _, _, e1), EAbs (_, _, _, e2)) => compare (e1, e2)
adamc@479 413 | (EAbs _, _) => LESS
adamc@479 414 | (_, EAbs _) => GREATER
adamc@479 415
adamc@479 416 | (ECApp (f1, x1), ECApp (f2, x2)) =>
adamc@479 417 join (compare (f1, f2),
adamc@479 418 fn () => Con.compare (x1, x2))
adamc@479 419 | (ECApp _, _) => LESS
adamc@479 420 | (_, ECApp _) => GREATER
adamc@479 421
adamc@479 422 | (ECAbs (_, _, e1), ECAbs (_, _, e2)) => compare (e1, e2)
adamc@479 423 | (ECAbs _, _) => LESS
adamc@479 424 | (_, ECAbs _) => GREATER
adamc@479 425
adamc@479 426 | (ERecord xes1, ERecord xes2) =>
adamc@479 427 joinL (fn ((x1, e1, _), (x2, e2, _)) =>
adamc@479 428 join (Con.compare (x1, x2),
adamc@479 429 fn () => compare (e1, e2))) (xes1, xes2)
adamc@479 430 | (ERecord _, _) => LESS
adamc@479 431 | (_, ERecord _) => GREATER
adamc@479 432
adamc@479 433 | (EField (e1, c1, _), EField (e2, c2, _)) =>
adamc@479 434 join (compare (e1, e2),
adamc@479 435 fn () => Con.compare (c1, c2))
adamc@479 436 | (EField _, _) => LESS
adamc@479 437 | (_, EField _) => GREATER
adamc@479 438
adamc@479 439 | (EConcat (x1, _, y1, _), EConcat (x2, _, y2, _)) =>
adamc@479 440 join (compare (x1, x2),
adamc@479 441 fn () => compare (y1, y2))
adamc@479 442 | (EConcat _, _) => LESS
adamc@479 443 | (_, EConcat _) => GREATER
adamc@479 444
adamc@479 445 | (ECut (e1, c1, _), ECut (e2, c2, _)) =>
adamc@479 446 join (compare (e1, e2),
adamc@493 447 fn () => Con.compare (c1, c2))
adamc@479 448 | (ECut _, _) => LESS
adamc@479 449 | (_, ECut _) => GREATER
adamc@479 450
adamc@493 451 | (ECutMulti (e1, c1, _), ECutMulti (e2, c2, _)) =>
adamc@493 452 join (compare (e1, e2),
adamc@493 453 fn () => Con.compare (c1, c2))
adamc@493 454 | (ECutMulti _, _) => LESS
adamc@493 455 | (_, ECutMulti _) => GREATER
adamc@493 456
adamc@479 457 | (ECase (e1, pes1, _), ECase (e2, pes2, _)) =>
adamc@479 458 join (compare (e1, e2),
adamc@479 459 fn () => joinL (fn ((p1, e1), (p2, e2)) =>
adamc@479 460 join (pCompare (p1, p2),
adamc@479 461 fn () => compare (e1, e2))) (pes1, pes2))
adamc@479 462 | (ECase _, _) => LESS
adamc@479 463 | (_, ECase _) => GREATER
adamc@479 464
adamc@479 465 | (EWrite e1, EWrite e2) => compare (e1, e2)
adamc@479 466 | (EWrite _, _) => LESS
adamc@479 467 | (_, EWrite _) => GREATER
adamc@479 468
adamc@479 469 | (EClosure (n1, es1), EClosure (n2, es2)) =>
adamc@479 470 join (Int.compare (n1, n2),
adamc@479 471 fn () => joinL compare (es1, es2))
adamc@479 472 | (EClosure _, _) => LESS
adamc@479 473 | (_, EClosure _) => GREATER
adamc@479 474
adamc@479 475 | (ELet (_, _, x1, e1), ELet (_, _, x2, e2)) =>
adamc@479 476 join (compare (x1, x2),
adamc@479 477 fn () => compare (e1, e2))
adamc@607 478 | (ELet _, _) => LESS
adamc@607 479 | (_, ELet _) => GREATER
adamc@607 480
adamc@609 481 | (EServerCall (n1, es1, e1, _), EServerCall (n2, es2, e2, _)) =>
adamc@607 482 join (Int.compare (n1, n2),
adamc@607 483 fn () => join (joinL compare (es1, es2),
adamc@607 484 fn () => compare (e1, e2)))
adamc@479 485
adamc@16 486 datatype binder =
adamc@16 487 RelC of string * kind
adamc@20 488 | NamedC of string * int * kind * con option
adamc@16 489 | RelE of string * con
adamc@109 490 | NamedE of string * int * con * exp option * string
adamc@16 491
adamc@16 492 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@16 493 let
adamc@16 494 val mfk = Kind.mapfold fk
adamc@16 495
adamc@16 496 fun bind' (ctx, b) =
adamc@16 497 let
adamc@16 498 val b' = case b of
adamc@16 499 Con.Rel x => RelC x
adamc@16 500 | Con.Named x => NamedC x
adamc@16 501 in
adamc@16 502 bind (ctx, b')
adamc@16 503 end
adamc@16 504 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@16 505
adamc@16 506 fun mfe ctx e acc =
adamc@16 507 S.bindP (mfe' ctx e acc, fe ctx)
adamc@16 508
adamc@16 509 and mfe' ctx (eAll as (e, loc)) =
adamc@16 510 case e of
adamc@16 511 EPrim _ => S.return2 eAll
adamc@16 512 | ERel _ => S.return2 eAll
adamc@16 513 | ENamed _ => S.return2 eAll
adamc@192 514 | ECon (dk, pc, cs, NONE) =>
adamc@192 515 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 516 fn cs' =>
adamc@192 517 (ECon (dk, pc, cs', NONE), loc))
adamc@192 518 | ECon (dk, n, cs, SOME e) =>
adamc@192 519 S.bind2 (mfe ctx e,
adamc@192 520 fn e' =>
adamc@192 521 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 522 fn cs' =>
adamc@192 523 (ECon (dk, n, cs', SOME e'), loc)))
adamc@48 524 | EFfi _ => S.return2 eAll
adamc@48 525 | EFfiApp (m, x, es) =>
adamc@192 526 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@48 527 fn es' =>
adamc@48 528 (EFfiApp (m, x, es'), loc))
adamc@16 529 | EApp (e1, e2) =>
adamc@16 530 S.bind2 (mfe ctx e1,
adamc@16 531 fn e1' =>
adamc@16 532 S.map2 (mfe ctx e2,
adamc@16 533 fn e2' =>
adamc@16 534 (EApp (e1', e2'), loc)))
adamc@26 535 | EAbs (x, dom, ran, e) =>
adamc@26 536 S.bind2 (mfc ctx dom,
adamc@26 537 fn dom' =>
adamc@26 538 S.bind2 (mfc ctx ran,
adamc@26 539 fn ran' =>
adamc@26 540 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 541 fn e' =>
adamc@26 542 (EAbs (x, dom', ran', e'), loc))))
adamc@16 543
adamc@16 544 | ECApp (e, c) =>
adamc@16 545 S.bind2 (mfe ctx e,
adamc@16 546 fn e' =>
adamc@16 547 S.map2 (mfc ctx c,
adamc@16 548 fn c' =>
adamc@16 549 (ECApp (e', c'), loc)))
adamc@16 550 | ECAbs (x, k, e) =>
adamc@16 551 S.bind2 (mfk k,
adamc@16 552 fn k' =>
adamc@16 553 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@16 554 fn e' =>
adamc@16 555 (ECAbs (x, k', e'), loc)))
adamc@16 556
adamc@16 557 | ERecord xes =>
adamc@29 558 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@16 559 S.bind2 (mfc ctx x,
adamc@16 560 fn x' =>
adamc@29 561 S.bind2 (mfe ctx e,
adamc@29 562 fn e' =>
adamc@29 563 S.map2 (mfc ctx t,
adamc@29 564 fn t' =>
adamc@29 565 (x', e', t')))))
adamc@16 566 xes,
adamc@16 567 fn xes' =>
adamc@16 568 (ERecord xes', loc))
adamc@16 569 | EField (e, c, {field, rest}) =>
adamc@16 570 S.bind2 (mfe ctx e,
adamc@16 571 fn e' =>
adamc@16 572 S.bind2 (mfc ctx c,
adamc@16 573 fn c' =>
adamc@16 574 S.bind2 (mfc ctx field,
adamc@16 575 fn field' =>
adamc@16 576 S.map2 (mfc ctx rest,
adamc@16 577 fn rest' =>
adamc@16 578 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@445 579 | EConcat (e1, c1, e2, c2) =>
adamc@339 580 S.bind2 (mfe ctx e1,
adamc@339 581 fn e1' =>
adamc@445 582 S.bind2 (mfc ctx c1,
adamc@445 583 fn c1' =>
adamc@339 584 S.bind2 (mfe ctx e2,
adamc@339 585 fn e2' =>
adamc@445 586 S.map2 (mfc ctx c2,
adamc@445 587 fn c2' =>
adamc@445 588 (EConcat (e1', c1', e2', c2'),
adamc@445 589 loc)))))
adamc@149 590 | ECut (e, c, {field, rest}) =>
adamc@149 591 S.bind2 (mfe ctx e,
adamc@149 592 fn e' =>
adamc@149 593 S.bind2 (mfc ctx c,
adamc@149 594 fn c' =>
adamc@149 595 S.bind2 (mfc ctx field,
adamc@149 596 fn field' =>
adamc@149 597 S.map2 (mfc ctx rest,
adamc@149 598 fn rest' =>
adamc@149 599 (ECut (e', c', {field = field', rest = rest'}), loc)))))
adamc@493 600 | ECutMulti (e, c, {rest}) =>
adamc@493 601 S.bind2 (mfe ctx e,
adamc@493 602 fn e' =>
adamc@493 603 S.bind2 (mfc ctx c,
adamc@493 604 fn c' =>
adamc@493 605 S.map2 (mfc ctx rest,
adamc@493 606 fn rest' =>
adamc@493 607 (ECutMulti (e', c', {rest = rest'}), loc))))
adamc@102 608
adamc@182 609 | ECase (e, pes, {disc, result}) =>
adamc@177 610 S.bind2 (mfe ctx e,
adamc@177 611 fn e' =>
adamc@177 612 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@193 613 let
adamc@193 614 fun pb ((p, _), ctx) =
adamc@193 615 case p of
adamc@193 616 PWild => ctx
adamc@193 617 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@193 618 | PPrim _ => ctx
adamc@193 619 | PCon (_, _, _, NONE) => ctx
adamc@193 620 | PCon (_, _, _, SOME p) => pb (p, ctx)
adamc@193 621 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@193 622 pb (p, ctx)) ctx xps
adamc@193 623 in
adamc@193 624 S.bind2 (mfp ctx p,
adamc@193 625 fn p' =>
adamc@193 626 S.map2 (mfe (pb (p', ctx)) e,
adamc@193 627 fn e' => (p', e')))
adamc@193 628 end) pes,
adamc@177 629 fn pes' =>
adamc@182 630 S.bind2 (mfc ctx disc,
adamc@182 631 fn disc' =>
adamc@182 632 S.map2 (mfc ctx result,
adamc@182 633 fn result' =>
adamc@182 634 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@177 635
adamc@102 636 | EWrite e =>
adamc@102 637 S.map2 (mfe ctx e,
adamc@102 638 fn e' =>
adamc@102 639 (EWrite e', loc))
adamc@110 640
adamc@110 641 | EClosure (n, es) =>
adamc@110 642 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@110 643 fn es' =>
adamc@110 644 (EClosure (n, es'), loc))
adamc@193 645
adamc@450 646 | ELet (x, t, e1, e2) =>
adamc@450 647 S.bind2 (mfc ctx t,
adamc@450 648 fn t' =>
adamc@450 649 S.bind2 (mfe ctx e1,
adamc@450 650 fn e1' =>
adamc@453 651 S.map2 (mfe (bind (ctx, RelE (x, t'))) e2,
adamc@450 652 fn e2' =>
adamc@450 653 (ELet (x, t', e1', e2'), loc))))
adamc@450 654
adamc@609 655 | EServerCall (n, es, e, t) =>
adamc@607 656 S.bind2 (ListUtil.mapfold (mfe ctx) es,
adamc@607 657 fn es' =>
adamc@609 658 S.bind2 (mfe ctx e,
adamc@607 659 fn e' =>
adamc@609 660 S.map2 (mfc ctx t,
adamc@609 661 fn t' =>
adamc@609 662 (EServerCall (n, es', e', t'), loc))))
adamc@607 663
adamc@193 664 and mfp ctx (pAll as (p, loc)) =
adamc@193 665 case p of
adamc@193 666 PWild => S.return2 pAll
adamc@193 667 | PVar (x, t) =>
adamc@193 668 S.map2 (mfc ctx t,
adamc@193 669 fn t' =>
adamc@193 670 (PVar (x, t'), loc))
adamc@193 671 | PPrim _ => S.return2 pAll
adamc@193 672 | PCon (dk, pc, args, po) =>
adamc@193 673 S.bind2 (mfpc ctx pc,
adamc@193 674 fn pc' =>
adamc@193 675 S.bind2 (ListUtil.mapfold (mfc ctx) args,
adamc@193 676 fn args' =>
adamc@193 677 S.map2 ((case po of
adamc@193 678 NONE => S.return2 NONE
adamc@193 679 | SOME p => S.map2 (mfp ctx p, SOME)),
adamc@193 680 fn po' =>
adamc@193 681 (PCon (dk, pc', args', po'), loc))))
adamc@193 682 | PRecord xps =>
adamc@193 683 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
adamc@193 684 S.bind2 (mfp ctx p,
adamc@193 685 fn p' =>
adamc@193 686 S.map2 (mfc ctx c,
adamc@193 687 fn c' =>
adamc@193 688 (x, p', c')))) xps,
adamc@193 689 fn xps' =>
adamc@193 690 (PRecord xps', loc))
adamc@193 691
adamc@193 692 and mfpc ctx pc =
adamc@193 693 case pc of
adamc@193 694 PConVar _ => S.return2 pc
adamc@193 695 | PConFfi {mod = m, datatyp, params, con, arg, kind} =>
adamc@193 696 S.map2 ((case arg of
adamc@193 697 NONE => S.return2 NONE
adamc@193 698 | SOME c => S.map2 (mfc ctx c, SOME)),
adamc@193 699 fn arg' =>
adamc@193 700 PConFfi {mod = m, datatyp = datatyp, params = params,
adamc@193 701 con = con, arg = arg', kind = kind})
adamc@16 702 in
adamc@16 703 mfe
adamc@16 704 end
adamc@16 705
adamc@16 706 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@16 707 mapfoldB {kind = fk,
adamc@16 708 con = fn () => fc,
adamc@16 709 exp = fn () => fe,
adamc@16 710 bind = fn ((), _) => ()} ()
adamc@16 711
adamc@21 712 fun mapB {kind, con, exp, bind} ctx e =
adamc@21 713 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@21 714 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@21 715 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@21 716 bind = bind} ctx e () of
adamc@21 717 S.Continue (e, ()) => e
adamc@21 718 | S.Return _ => raise Fail "CoreUtil.Exp.mapB: Impossible"
adamc@21 719
adamc@16 720 fun map {kind, con, exp} e =
adamc@16 721 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 722 con = fn c => fn () => S.Continue (con c, ()),
adamc@16 723 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@16 724 S.Return () => raise Fail "Core_util.Exp.map"
adamc@16 725 | S.Continue (e, ()) => e
adamc@16 726
adamc@23 727 fun fold {kind, con, exp} s e =
adamc@23 728 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 729 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 730 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@23 731 S.Continue (_, s) => s
adamc@23 732 | S.Return _ => raise Fail "CoreUtil.Exp.fold: Impossible"
adamc@23 733
adamc@484 734 fun foldB {kind, con, exp, bind} ctx s e =
adamc@484 735 case mapfoldB {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@484 736 con = fn ctx => fn c => fn s => S.Continue (c, con (ctx, c, s)),
adamc@484 737 exp = fn ctx => fn e => fn s => S.Continue (e, exp (ctx, e, s)),
adamc@484 738 bind = bind} ctx e s of
adamc@484 739 S.Continue (_, s) => s
adamc@484 740 | S.Return _ => raise Fail "CoreUtil.Exp.foldB: Impossible"
adamc@484 741
adamc@16 742 fun exists {kind, con, exp} k =
adamc@16 743 case mapfold {kind = fn k => fn () =>
adamc@16 744 if kind k then
adamc@16 745 S.Return ()
adamc@16 746 else
adamc@16 747 S.Continue (k, ()),
adamc@16 748 con = fn c => fn () =>
adamc@16 749 if con c then
adamc@16 750 S.Return ()
adamc@16 751 else
adamc@16 752 S.Continue (c, ()),
adamc@16 753 exp = fn e => fn () =>
adamc@16 754 if exp e then
adamc@16 755 S.Return ()
adamc@16 756 else
adamc@16 757 S.Continue (e, ())} k () of
adamc@16 758 S.Return _ => true
adamc@16 759 | S.Continue _ => false
adamc@16 760
adamc@479 761 fun existsB {kind, con, exp, bind} ctx k =
adamc@479 762 case mapfoldB {kind = fn k => fn () =>
adamc@479 763 if kind k then
adamc@479 764 S.Return ()
adamc@479 765 else
adamc@479 766 S.Continue (k, ()),
adamc@479 767 con = fn ctx => fn c => fn () =>
adamc@479 768 if con (ctx, c) then
adamc@479 769 S.Return ()
adamc@479 770 else
adamc@479 771 S.Continue (c, ()),
adamc@479 772 exp = fn ctx => fn e => fn () =>
adamc@479 773 if exp (ctx, e) then
adamc@479 774 S.Return ()
adamc@479 775 else
adamc@479 776 S.Continue (e, ()),
adamc@479 777 bind = bind} ctx k () of
adamc@479 778 S.Return _ => true
adamc@479 779 | S.Continue _ => false
adamc@479 780
adamc@443 781 fun foldMap {kind, con, exp} s e =
adamc@443 782 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@443 783 con = fn c => fn s => S.Continue (con (c, s)),
adamc@443 784 exp = fn e => fn s => S.Continue (exp (e, s))} e s of
adamc@443 785 S.Continue v => v
adamc@443 786 | S.Return _ => raise Fail "CoreUtil.Exp.foldMap: Impossible"
adamc@443 787
adamc@488 788 fun foldMapB {kind, con, exp, bind} ctx s e =
adamc@488 789 case mapfoldB {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@488 790 con = fn ctx => fn c => fn s => S.Continue (con (ctx, c, s)),
adamc@488 791 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
adamc@488 792 bind = bind} ctx e s of
adamc@488 793 S.Continue v => v
adamc@488 794 | S.Return _ => raise Fail "CoreUtil.Exp.foldMapB: Impossible"
adamc@488 795
adamc@16 796 end
adamc@16 797
adamc@20 798 structure Decl = struct
adamc@20 799
adamc@20 800 datatype binder = datatype Exp.binder
adamc@20 801
adamc@20 802 fun mapfoldB {kind = fk, con = fc, exp = fe, decl = fd, bind} =
adamc@20 803 let
adamc@20 804 val mfk = Kind.mapfold fk
adamc@20 805
adamc@20 806 fun bind' (ctx, b) =
adamc@20 807 let
adamc@20 808 val b' = case b of
adamc@20 809 Con.Rel x => RelC x
adamc@20 810 | Con.Named x => NamedC x
adamc@20 811 in
adamc@20 812 bind (ctx, b')
adamc@20 813 end
adamc@20 814 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@20 815
adamc@20 816 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind}
adamc@20 817
adamc@20 818 fun mfd ctx d acc =
adamc@20 819 S.bindP (mfd' ctx d acc, fd ctx)
adamc@20 820
adamc@20 821 and mfd' ctx (dAll as (d, loc)) =
adamc@20 822 case d of
adamc@20 823 DCon (x, n, k, c) =>
adamc@20 824 S.bind2 (mfk k,
adamc@20 825 fn k' =>
adamc@20 826 S.map2 (mfc ctx c,
adamc@20 827 fn c' =>
adamc@20 828 (DCon (x, n, k', c'), loc)))
adamc@192 829 | DDatatype (x, n, xs, xncs) =>
adamc@196 830 let
adamc@196 831 val k = (KType, loc)
adamc@196 832 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@196 833 val ctx' = bind (ctx, NamedC (x, n, k', NONE))
adamc@196 834 in
adamc@196 835 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@196 836 case c of
adamc@196 837 NONE => S.return2 (x, n, c)
adamc@196 838 | SOME c =>
adamc@196 839 S.map2 (mfc ctx' c,
adamc@196 840 fn c' => (x, n, SOME c'))) xncs,
adamc@196 841 fn xncs' =>
adamc@196 842 (DDatatype (x, n, xs, xncs'), loc))
adamc@196 843 end
adamc@125 844 | DVal vi =>
adamc@125 845 S.map2 (mfvi ctx vi,
adamc@125 846 fn vi' =>
adamc@125 847 (DVal vi', loc))
adamc@125 848 | DValRec vis =>
adamc@127 849 let
adamc@127 850 val ctx = foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@127 851 ctx vis
adamc@127 852 in
adamc@127 853 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@127 854 fn vis' =>
adamc@127 855 (DValRec vis', loc))
adamc@127 856 end
adamc@109 857 | DExport _ => S.return2 dAll
adamc@247 858 | DTable (x, n, c, s) =>
adamc@247 859 S.map2 (mfc ctx c,
adamc@461 860 fn c' =>
adamc@461 861 (DTable (x, n, c', s), loc))
adamc@338 862 | DSequence _ => S.return2 dAll
adamc@271 863 | DDatabase _ => S.return2 dAll
adamc@461 864 | DCookie (x, n, c, s) =>
adamc@461 865 S.map2 (mfc ctx c,
adamc@461 866 fn c' =>
adamc@461 867 (DCookie (x, n, c', s), loc))
adamc@125 868
adamc@125 869 and mfvi ctx (x, n, t, e, s) =
adamc@125 870 S.bind2 (mfc ctx t,
adamc@125 871 fn t' =>
adamc@125 872 S.map2 (mfe ctx e,
adamc@125 873 fn e' =>
adamc@125 874 (x, n, t', e', s)))
adamc@20 875 in
adamc@20 876 mfd
adamc@20 877 end
adamc@20 878
adamc@23 879 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 880 mapfoldB {kind = fk,
adamc@23 881 con = fn () => fc,
adamc@23 882 exp = fn () => fe,
adamc@23 883 decl = fn () => fd,
adamc@23 884 bind = fn ((), _) => ()} ()
adamc@23 885
adamc@23 886 fun fold {kind, con, exp, decl} s d =
adamc@23 887 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 888 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 889 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 890 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 891 S.Continue (_, s) => s
adamc@23 892 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
adamc@23 893
adamc@110 894 fun foldMap {kind, con, exp, decl} s d =
adamc@110 895 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 896 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 897 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 898 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 899 S.Continue v => v
adamc@110 900 | S.Return _ => raise Fail "CoreUtil.Decl.foldMap: Impossible"
adamc@110 901
adamc@484 902 fun foldMapB {kind, con, exp, decl, bind} ctx s d =
adamc@484 903 case mapfoldB {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@484 904 con = fn ctx => fn c => fn s => S.Continue (con (ctx, c, s)),
adamc@484 905 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
adamc@484 906 decl = fn ctx => fn d => fn s => S.Continue (decl (ctx, d, s)),
adamc@484 907 bind = bind} ctx d s of
adamc@484 908 S.Continue v => v
adamc@484 909 | S.Return _ => raise Fail "CoreUtil.Decl.foldMapB: Impossible"
adamc@484 910
adamc@522 911 fun exists {kind, con, exp, decl} d =
adamc@522 912 case mapfold {kind = fn k => fn () =>
adamc@522 913 if kind k then
adamc@522 914 S.Return ()
adamc@522 915 else
adamc@522 916 S.Continue (k, ()),
adamc@522 917 con = fn c => fn () =>
adamc@522 918 if con c then
adamc@522 919 S.Return ()
adamc@522 920 else
adamc@522 921 S.Continue (c, ()),
adamc@522 922 exp = fn e => fn () =>
adamc@522 923 if exp e then
adamc@522 924 S.Return ()
adamc@522 925 else
adamc@522 926 S.Continue (e, ()),
adamc@522 927 decl = fn d => fn () =>
adamc@522 928 if decl d then
adamc@522 929 S.Return ()
adamc@522 930 else
adamc@522 931 S.Continue (d, ())} d () of
adamc@522 932 S.Return _ => true
adamc@522 933 | S.Continue _ => false
adamc@522 934
adamc@16 935 end
adamc@20 936
adamc@20 937 structure File = struct
adamc@20 938
adamc@20 939 datatype binder = datatype Exp.binder
adamc@20 940
adamc@20 941 fun mapfoldB (all as {bind, ...}) =
adamc@20 942 let
adamc@20 943 val mfd = Decl.mapfoldB all
adamc@20 944
adamc@20 945 fun mff ctx ds =
adamc@20 946 case ds of
adamc@20 947 nil => S.return2 nil
adamc@20 948 | d :: ds' =>
adamc@20 949 S.bind2 (mfd ctx d,
adamc@20 950 fn d' =>
adamc@20 951 let
adamc@100 952 val ctx' =
adamc@20 953 case #1 d' of
adamc@100 954 DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c))
adamc@192 955 | DDatatype (x, n, xs, xncs) =>
adamc@163 956 let
adamc@192 957 val loc = #2 d'
adamc@192 958 val k = (KType, loc)
adamc@192 959 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@192 960
adamc@192 961 val ctx = bind (ctx, NamedC (x, n, k', NONE))
adamc@163 962 val t = (CNamed n, #2 d')
adamc@192 963 val t = ListUtil.foldli (fn (i, _, t) => (CApp (t, (CRel i, loc)), loc))
adamc@192 964 t xs
adamc@163 965 in
adamc@163 966 foldl (fn ((x, n, to), ctx) =>
adamc@163 967 let
adamc@163 968 val t = case to of
adamc@163 969 NONE => t
adamc@163 970 | SOME t' => (TFun (t', t), #2 d')
adamc@192 971 val t = foldr (fn (x, t) => (TCFun (x, k, t), loc))
adamc@192 972 t xs
adamc@163 973 in
adamc@163 974 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@163 975 end)
adamc@163 976 ctx xncs
adamc@163 977 end
adamc@109 978 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@125 979 | DValRec vis =>
adamc@127 980 foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@125 981 ctx vis
adamc@109 982 | DExport _ => ctx
adamc@247 983 | DTable (x, n, c, s) =>
adamc@247 984 let
adamc@338 985 val t = (CApp ((CFfi ("Basis", "sql_table"), #2 d'), c), #2 d')
adamc@338 986 in
adamc@338 987 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@338 988 end
adamc@338 989 | DSequence (x, n, s) =>
adamc@338 990 let
adamc@338 991 val t = (CFfi ("Basis", "sql_sequence"), #2 d')
adamc@247 992 in
adamc@247 993 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@247 994 end
adamc@271 995 | DDatabase _ => ctx
adamc@461 996 | DCookie (x, n, c, s) =>
adamc@461 997 let
adamc@461 998 val t = (CApp ((CFfi ("Basis", "http_cookie"), #2 d'), c), #2 d')
adamc@461 999 in
adamc@461 1000 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@461 1001 end
adamc@20 1002 in
adamc@20 1003 S.map2 (mff ctx' ds',
adamc@20 1004 fn ds' =>
adamc@20 1005 d' :: ds')
adamc@20 1006 end)
adamc@20 1007 in
adamc@20 1008 mff
adamc@20 1009 end
adamc@20 1010
adamc@23 1011 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 1012 mapfoldB {kind = fk,
adamc@23 1013 con = fn () => fc,
adamc@23 1014 exp = fn () => fe,
adamc@23 1015 decl = fn () => fd,
adamc@23 1016 bind = fn ((), _) => ()} ()
adamc@23 1017
adamc@20 1018 fun mapB {kind, con, exp, decl, bind} ctx ds =
adamc@20 1019 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@20 1020 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@20 1021 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@20 1022 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@20 1023 bind = bind} ctx ds () of
adamc@20 1024 S.Continue (ds, ()) => ds
adamc@20 1025 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
adamc@20 1026
adamc@482 1027 fun map {kind, con, exp, decl} ds =
adamc@482 1028 mapB {kind = kind,
adamc@482 1029 con = fn () => con,
adamc@482 1030 exp = fn () => exp,
adamc@482 1031 decl = fn () => decl,
adamc@482 1032 bind = fn _ => ()} () ds
adamc@482 1033
adamc@23 1034 fun fold {kind, con, exp, decl} s d =
adamc@23 1035 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 1036 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 1037 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 1038 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 1039 S.Continue (_, s) => s
adamc@23 1040 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
adamc@23 1041
adamc@110 1042 fun foldMap {kind, con, exp, decl} s d =
adamc@110 1043 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 1044 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 1045 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 1046 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 1047 S.Continue v => v
adamc@110 1048 | S.Return _ => raise Fail "CoreUtil.File.foldMap: Impossible"
adamc@110 1049
adamc@179 1050 val maxName = foldl (fn ((d, _) : decl, count) =>
adamc@179 1051 case d of
adamc@179 1052 DCon (_, n, _, _) => Int.max (n, count)
adamc@398 1053 | DDatatype (_, n, _, ns) =>
adamc@398 1054 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@398 1055 (Int.max (n, count)) ns
adamc@179 1056 | DVal (_, n, _, _, _) => Int.max (n, count)
adamc@179 1057 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
adamc@247 1058 | DExport _ => count
adamc@271 1059 | DTable (_, n, _, _) => Int.max (n, count)
adamc@338 1060 | DSequence (_, n, _) => Int.max (n, count)
adamc@461 1061 | DDatabase _ => count
adamc@461 1062 | DCookie (_, n, _, _) => Int.max (n, count)) 0
adamc@179 1063
adamc@20 1064 end
adamc@20 1065
adamc@20 1066 end