annotate src/core_util.sml @ 249:b6b75e6e0898

Corify transaction wrappers
author Adam Chlipala <adamc@hcoop.net>
date Sun, 31 Aug 2008 09:45:23 -0400
parents 5c50b17f5e4a
children 98f551ddd54b
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@193 163 fn () => joinL (fn ((x1, v1), (x2, v2)) =>
adamc@193 164 join (compare (x1, x2),
adamc@193 165 fn () => compare (v1, v2))) (xvs1, xvs2))
adamc@193 166 | (CRecord _, _) => LESS
adamc@193 167 | (_, CRecord _) => GREATER
adamc@193 168
adamc@193 169 | (CConcat (f1, s1), CConcat (f2, s2)) =>
adamc@193 170 join (compare (f1, f2),
adamc@193 171 fn () => compare (s1, s2))
adamc@193 172 | (CConcat _, _) => LESS
adamc@193 173 | (_, CConcat _) => GREATER
adamc@193 174
adamc@193 175 | (CFold (d1, r1), CFold (d2, r2)) =>
adamc@193 176 join (Kind.compare (d1, r2),
adamc@193 177 fn () => Kind.compare (r1, r2))
adamc@193 178 | (CFold _, _) => LESS
adamc@193 179 | (_, CFold _) => GREATER
adamc@193 180
adamc@193 181 | (CUnit, CUnit) => EQUAL
adamc@214 182 | (CUnit, _) => LESS
adamc@214 183 | (_, CUnit) => GREATER
adamc@214 184
adamc@214 185 | (CTuple cs1, CTuple cs2) => joinL compare (cs1, cs2)
adamc@214 186 | (CTuple _, _) => LESS
adamc@214 187 | (_, CTuple _) => GREATER
adamc@214 188
adamc@214 189 | (CProj (c1, n1), CProj (c2, n2)) => join (Int.compare (n1, n2),
adamc@214 190 fn () => compare (c1, c2))
adamc@193 191
adamc@16 192 datatype binder =
adamc@16 193 Rel of string * kind
adamc@20 194 | Named of string * int * kind * con option
adamc@16 195
adamc@16 196 fun mapfoldB {kind = fk, con = fc, bind} =
adamc@16 197 let
adamc@16 198 val mfk = Kind.mapfold fk
adamc@16 199
adamc@16 200 fun mfc ctx c acc =
adamc@16 201 S.bindP (mfc' ctx c acc, fc ctx)
adamc@16 202
adamc@16 203 and mfc' ctx (cAll as (c, loc)) =
adamc@16 204 case c of
adamc@16 205 TFun (c1, c2) =>
adamc@16 206 S.bind2 (mfc ctx c1,
adamc@16 207 fn c1' =>
adamc@16 208 S.map2 (mfc ctx c2,
adamc@16 209 fn c2' =>
adamc@16 210 (TFun (c1', c2'), loc)))
adamc@16 211 | TCFun (x, k, c) =>
adamc@16 212 S.bind2 (mfk k,
adamc@16 213 fn k' =>
adamc@16 214 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
adamc@16 215 fn c' =>
adamc@16 216 (TCFun (x, k', c'), loc)))
adamc@16 217 | TRecord c =>
adamc@16 218 S.map2 (mfc ctx c,
adamc@16 219 fn c' =>
adamc@16 220 (TRecord c', loc))
adamc@16 221
adamc@16 222 | CRel _ => S.return2 cAll
adamc@16 223 | CNamed _ => S.return2 cAll
adamc@48 224 | CFfi _ => S.return2 cAll
adamc@16 225 | CApp (c1, c2) =>
adamc@16 226 S.bind2 (mfc ctx c1,
adamc@16 227 fn c1' =>
adamc@16 228 S.map2 (mfc ctx c2,
adamc@16 229 fn c2' =>
adamc@16 230 (CApp (c1', c2'), loc)))
adamc@16 231 | CAbs (x, k, c) =>
adamc@16 232 S.bind2 (mfk k,
adamc@16 233 fn k' =>
adamc@16 234 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
adamc@16 235 fn c' =>
adamc@16 236 (CAbs (x, k', c'), loc)))
adamc@16 237
adamc@16 238 | CName _ => S.return2 cAll
adamc@16 239
adamc@16 240 | CRecord (k, xcs) =>
adamc@16 241 S.bind2 (mfk k,
adamc@16 242 fn k' =>
adamc@16 243 S.map2 (ListUtil.mapfold (fn (x, c) =>
adamc@16 244 S.bind2 (mfc ctx x,
adamc@16 245 fn x' =>
adamc@16 246 S.map2 (mfc ctx c,
adamc@16 247 fn c' =>
adamc@16 248 (x', c'))))
adamc@16 249 xcs,
adamc@16 250 fn xcs' =>
adamc@16 251 (CRecord (k', xcs'), loc)))
adamc@16 252 | CConcat (c1, c2) =>
adamc@16 253 S.bind2 (mfc ctx c1,
adamc@16 254 fn c1' =>
adamc@16 255 S.map2 (mfc ctx c2,
adamc@16 256 fn c2' =>
adamc@16 257 (CConcat (c1', c2'), loc)))
adamc@69 258 | CFold (k1, k2) =>
adamc@69 259 S.bind2 (mfk k1,
adamc@69 260 fn k1' =>
adamc@69 261 S.map2 (mfk k2,
adamc@69 262 fn k2' =>
adamc@69 263 (CFold (k1', k2'), loc)))
adamc@87 264
adamc@87 265 | CUnit => S.return2 cAll
adamc@214 266
adamc@214 267 | CTuple cs =>
adamc@214 268 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@214 269 fn cs' =>
adamc@214 270 (CTuple cs', loc))
adamc@214 271
adamc@214 272 | CProj (c, n) =>
adamc@214 273 S.map2 (mfc ctx c,
adamc@214 274 fn c' =>
adamc@214 275 (CProj (c', n), loc))
adamc@16 276 in
adamc@16 277 mfc
adamc@16 278 end
adamc@16 279
adamc@16 280 fun mapfold {kind = fk, con = fc} =
adamc@16 281 mapfoldB {kind = fk,
adamc@16 282 con = fn () => fc,
adamc@16 283 bind = fn ((), _) => ()} ()
adamc@16 284
adamc@16 285 fun map {kind, con} c =
adamc@16 286 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 287 con = fn c => fn () => S.Continue (con c, ())} c () of
adamc@16 288 S.Return () => raise Fail "Core_util.Con.map"
adamc@16 289 | S.Continue (c, ()) => c
adamc@16 290
adamc@16 291 fun mapB {kind, con, bind} ctx c =
adamc@16 292 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 293 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@16 294 bind = bind} ctx c () of
adamc@16 295 S.Continue (c, ()) => c
adamc@20 296 | S.Return _ => raise Fail "CoreUtil.Con.mapB: Impossible"
adamc@16 297
adamc@23 298 fun fold {kind, con} s c =
adamc@23 299 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 300 con = fn c => fn s => S.Continue (c, con (c, s))} c s of
adamc@23 301 S.Continue (_, s) => s
adamc@23 302 | S.Return _ => raise Fail "CoreUtil.Con.fold: Impossible"
adamc@23 303
adamc@16 304 fun exists {kind, con} k =
adamc@16 305 case mapfold {kind = fn k => fn () =>
adamc@16 306 if kind k then
adamc@16 307 S.Return ()
adamc@16 308 else
adamc@16 309 S.Continue (k, ()),
adamc@16 310 con = fn c => fn () =>
adamc@16 311 if con c then
adamc@16 312 S.Return ()
adamc@16 313 else
adamc@16 314 S.Continue (c, ())} k () of
adamc@16 315 S.Return _ => true
adamc@16 316 | S.Continue _ => false
adamc@16 317
adamc@193 318 fun foldMap {kind, con} s c =
adamc@193 319 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@193 320 con = fn c => fn s => S.Continue (con (c, s))} c s of
adamc@193 321 S.Continue v => v
adamc@193 322 | S.Return _ => raise Fail "CoreUtil.Con.foldMap: Impossible"
adamc@193 323
adamc@16 324 end
adamc@16 325
adamc@16 326 structure Exp = struct
adamc@16 327
adamc@16 328 datatype binder =
adamc@16 329 RelC of string * kind
adamc@20 330 | NamedC of string * int * kind * con option
adamc@16 331 | RelE of string * con
adamc@109 332 | NamedE of string * int * con * exp option * string
adamc@16 333
adamc@16 334 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@16 335 let
adamc@16 336 val mfk = Kind.mapfold fk
adamc@16 337
adamc@16 338 fun bind' (ctx, b) =
adamc@16 339 let
adamc@16 340 val b' = case b of
adamc@16 341 Con.Rel x => RelC x
adamc@16 342 | Con.Named x => NamedC x
adamc@16 343 in
adamc@16 344 bind (ctx, b')
adamc@16 345 end
adamc@16 346 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@16 347
adamc@16 348 fun mfe ctx e acc =
adamc@16 349 S.bindP (mfe' ctx e acc, fe ctx)
adamc@16 350
adamc@16 351 and mfe' ctx (eAll as (e, loc)) =
adamc@16 352 case e of
adamc@16 353 EPrim _ => S.return2 eAll
adamc@16 354 | ERel _ => S.return2 eAll
adamc@16 355 | ENamed _ => S.return2 eAll
adamc@192 356 | ECon (dk, pc, cs, NONE) =>
adamc@192 357 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 358 fn cs' =>
adamc@192 359 (ECon (dk, pc, cs', NONE), loc))
adamc@192 360 | ECon (dk, n, cs, SOME e) =>
adamc@192 361 S.bind2 (mfe ctx e,
adamc@192 362 fn e' =>
adamc@192 363 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 364 fn cs' =>
adamc@192 365 (ECon (dk, n, cs', SOME e'), loc)))
adamc@48 366 | EFfi _ => S.return2 eAll
adamc@48 367 | EFfiApp (m, x, es) =>
adamc@192 368 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@48 369 fn es' =>
adamc@48 370 (EFfiApp (m, x, es'), loc))
adamc@16 371 | EApp (e1, e2) =>
adamc@16 372 S.bind2 (mfe ctx e1,
adamc@16 373 fn e1' =>
adamc@16 374 S.map2 (mfe ctx e2,
adamc@16 375 fn e2' =>
adamc@16 376 (EApp (e1', e2'), loc)))
adamc@26 377 | EAbs (x, dom, ran, e) =>
adamc@26 378 S.bind2 (mfc ctx dom,
adamc@26 379 fn dom' =>
adamc@26 380 S.bind2 (mfc ctx ran,
adamc@26 381 fn ran' =>
adamc@26 382 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 383 fn e' =>
adamc@26 384 (EAbs (x, dom', ran', e'), loc))))
adamc@16 385
adamc@16 386 | ECApp (e, c) =>
adamc@16 387 S.bind2 (mfe ctx e,
adamc@16 388 fn e' =>
adamc@16 389 S.map2 (mfc ctx c,
adamc@16 390 fn c' =>
adamc@16 391 (ECApp (e', c'), loc)))
adamc@16 392 | ECAbs (x, k, e) =>
adamc@16 393 S.bind2 (mfk k,
adamc@16 394 fn k' =>
adamc@16 395 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@16 396 fn e' =>
adamc@16 397 (ECAbs (x, k', e'), loc)))
adamc@16 398
adamc@16 399 | ERecord xes =>
adamc@29 400 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@16 401 S.bind2 (mfc ctx x,
adamc@16 402 fn x' =>
adamc@29 403 S.bind2 (mfe ctx e,
adamc@29 404 fn e' =>
adamc@29 405 S.map2 (mfc ctx t,
adamc@29 406 fn t' =>
adamc@29 407 (x', e', t')))))
adamc@16 408 xes,
adamc@16 409 fn xes' =>
adamc@16 410 (ERecord xes', loc))
adamc@16 411 | EField (e, c, {field, rest}) =>
adamc@16 412 S.bind2 (mfe ctx e,
adamc@16 413 fn e' =>
adamc@16 414 S.bind2 (mfc ctx c,
adamc@16 415 fn c' =>
adamc@16 416 S.bind2 (mfc ctx field,
adamc@16 417 fn field' =>
adamc@16 418 S.map2 (mfc ctx rest,
adamc@16 419 fn rest' =>
adamc@16 420 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@149 421 | ECut (e, c, {field, rest}) =>
adamc@149 422 S.bind2 (mfe ctx e,
adamc@149 423 fn e' =>
adamc@149 424 S.bind2 (mfc ctx c,
adamc@149 425 fn c' =>
adamc@149 426 S.bind2 (mfc ctx field,
adamc@149 427 fn field' =>
adamc@149 428 S.map2 (mfc ctx rest,
adamc@149 429 fn rest' =>
adamc@149 430 (ECut (e', c', {field = field', rest = rest'}), loc)))))
adamc@73 431 | EFold k =>
adamc@73 432 S.map2 (mfk k,
adamc@73 433 fn k' =>
adamc@73 434 (EFold k', loc))
adamc@102 435
adamc@182 436 | ECase (e, pes, {disc, result}) =>
adamc@177 437 S.bind2 (mfe ctx e,
adamc@177 438 fn e' =>
adamc@177 439 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@193 440 let
adamc@193 441 fun pb ((p, _), ctx) =
adamc@193 442 case p of
adamc@193 443 PWild => ctx
adamc@193 444 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@193 445 | PPrim _ => ctx
adamc@193 446 | PCon (_, _, _, NONE) => ctx
adamc@193 447 | PCon (_, _, _, SOME p) => pb (p, ctx)
adamc@193 448 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@193 449 pb (p, ctx)) ctx xps
adamc@193 450 in
adamc@193 451 S.bind2 (mfp ctx p,
adamc@193 452 fn p' =>
adamc@193 453 S.map2 (mfe (pb (p', ctx)) e,
adamc@193 454 fn e' => (p', e')))
adamc@193 455 end) pes,
adamc@177 456 fn pes' =>
adamc@182 457 S.bind2 (mfc ctx disc,
adamc@182 458 fn disc' =>
adamc@182 459 S.map2 (mfc ctx result,
adamc@182 460 fn result' =>
adamc@182 461 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@177 462
adamc@102 463 | EWrite e =>
adamc@102 464 S.map2 (mfe ctx e,
adamc@102 465 fn e' =>
adamc@102 466 (EWrite e', loc))
adamc@110 467
adamc@110 468 | EClosure (n, es) =>
adamc@110 469 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@110 470 fn es' =>
adamc@110 471 (EClosure (n, es'), loc))
adamc@193 472
adamc@193 473 and mfp ctx (pAll as (p, loc)) =
adamc@193 474 case p of
adamc@193 475 PWild => S.return2 pAll
adamc@193 476 | PVar (x, t) =>
adamc@193 477 S.map2 (mfc ctx t,
adamc@193 478 fn t' =>
adamc@193 479 (PVar (x, t'), loc))
adamc@193 480 | PPrim _ => S.return2 pAll
adamc@193 481 | PCon (dk, pc, args, po) =>
adamc@193 482 S.bind2 (mfpc ctx pc,
adamc@193 483 fn pc' =>
adamc@193 484 S.bind2 (ListUtil.mapfold (mfc ctx) args,
adamc@193 485 fn args' =>
adamc@193 486 S.map2 ((case po of
adamc@193 487 NONE => S.return2 NONE
adamc@193 488 | SOME p => S.map2 (mfp ctx p, SOME)),
adamc@193 489 fn po' =>
adamc@193 490 (PCon (dk, pc', args', po'), loc))))
adamc@193 491 | PRecord xps =>
adamc@193 492 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
adamc@193 493 S.bind2 (mfp ctx p,
adamc@193 494 fn p' =>
adamc@193 495 S.map2 (mfc ctx c,
adamc@193 496 fn c' =>
adamc@193 497 (x, p', c')))) xps,
adamc@193 498 fn xps' =>
adamc@193 499 (PRecord xps', loc))
adamc@193 500
adamc@193 501 and mfpc ctx pc =
adamc@193 502 case pc of
adamc@193 503 PConVar _ => S.return2 pc
adamc@193 504 | PConFfi {mod = m, datatyp, params, con, arg, kind} =>
adamc@193 505 S.map2 ((case arg of
adamc@193 506 NONE => S.return2 NONE
adamc@193 507 | SOME c => S.map2 (mfc ctx c, SOME)),
adamc@193 508 fn arg' =>
adamc@193 509 PConFfi {mod = m, datatyp = datatyp, params = params,
adamc@193 510 con = con, arg = arg', kind = kind})
adamc@16 511 in
adamc@16 512 mfe
adamc@16 513 end
adamc@16 514
adamc@16 515 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@16 516 mapfoldB {kind = fk,
adamc@16 517 con = fn () => fc,
adamc@16 518 exp = fn () => fe,
adamc@16 519 bind = fn ((), _) => ()} ()
adamc@16 520
adamc@21 521 fun mapB {kind, con, exp, bind} ctx e =
adamc@21 522 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@21 523 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@21 524 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@21 525 bind = bind} ctx e () of
adamc@21 526 S.Continue (e, ()) => e
adamc@21 527 | S.Return _ => raise Fail "CoreUtil.Exp.mapB: Impossible"
adamc@21 528
adamc@16 529 fun map {kind, con, exp} e =
adamc@16 530 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 531 con = fn c => fn () => S.Continue (con c, ()),
adamc@16 532 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@16 533 S.Return () => raise Fail "Core_util.Exp.map"
adamc@16 534 | S.Continue (e, ()) => e
adamc@16 535
adamc@23 536 fun fold {kind, con, exp} s e =
adamc@23 537 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 538 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 539 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@23 540 S.Continue (_, s) => s
adamc@23 541 | S.Return _ => raise Fail "CoreUtil.Exp.fold: Impossible"
adamc@23 542
adamc@16 543 fun exists {kind, con, exp} k =
adamc@16 544 case mapfold {kind = fn k => fn () =>
adamc@16 545 if kind k then
adamc@16 546 S.Return ()
adamc@16 547 else
adamc@16 548 S.Continue (k, ()),
adamc@16 549 con = fn c => fn () =>
adamc@16 550 if con c then
adamc@16 551 S.Return ()
adamc@16 552 else
adamc@16 553 S.Continue (c, ()),
adamc@16 554 exp = fn e => fn () =>
adamc@16 555 if exp e then
adamc@16 556 S.Return ()
adamc@16 557 else
adamc@16 558 S.Continue (e, ())} k () of
adamc@16 559 S.Return _ => true
adamc@16 560 | S.Continue _ => false
adamc@16 561
adamc@16 562 end
adamc@16 563
adamc@20 564 structure Decl = struct
adamc@20 565
adamc@20 566 datatype binder = datatype Exp.binder
adamc@20 567
adamc@20 568 fun mapfoldB {kind = fk, con = fc, exp = fe, decl = fd, bind} =
adamc@20 569 let
adamc@20 570 val mfk = Kind.mapfold fk
adamc@20 571
adamc@20 572 fun bind' (ctx, b) =
adamc@20 573 let
adamc@20 574 val b' = case b of
adamc@20 575 Con.Rel x => RelC x
adamc@20 576 | Con.Named x => NamedC x
adamc@20 577 in
adamc@20 578 bind (ctx, b')
adamc@20 579 end
adamc@20 580 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@20 581
adamc@20 582 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind}
adamc@20 583
adamc@20 584 fun mfd ctx d acc =
adamc@20 585 S.bindP (mfd' ctx d acc, fd ctx)
adamc@20 586
adamc@20 587 and mfd' ctx (dAll as (d, loc)) =
adamc@20 588 case d of
adamc@20 589 DCon (x, n, k, c) =>
adamc@20 590 S.bind2 (mfk k,
adamc@20 591 fn k' =>
adamc@20 592 S.map2 (mfc ctx c,
adamc@20 593 fn c' =>
adamc@20 594 (DCon (x, n, k', c'), loc)))
adamc@192 595 | DDatatype (x, n, xs, xncs) =>
adamc@196 596 let
adamc@196 597 val k = (KType, loc)
adamc@196 598 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@196 599 val ctx' = bind (ctx, NamedC (x, n, k', NONE))
adamc@196 600 in
adamc@196 601 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@196 602 case c of
adamc@196 603 NONE => S.return2 (x, n, c)
adamc@196 604 | SOME c =>
adamc@196 605 S.map2 (mfc ctx' c,
adamc@196 606 fn c' => (x, n, SOME c'))) xncs,
adamc@196 607 fn xncs' =>
adamc@196 608 (DDatatype (x, n, xs, xncs'), loc))
adamc@196 609 end
adamc@125 610 | DVal vi =>
adamc@125 611 S.map2 (mfvi ctx vi,
adamc@125 612 fn vi' =>
adamc@125 613 (DVal vi', loc))
adamc@125 614 | DValRec vis =>
adamc@127 615 let
adamc@127 616 val ctx = foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@127 617 ctx vis
adamc@127 618 in
adamc@127 619 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@127 620 fn vis' =>
adamc@127 621 (DValRec vis', loc))
adamc@127 622 end
adamc@109 623 | DExport _ => S.return2 dAll
adamc@247 624 | DTable (x, n, c, s) =>
adamc@247 625 S.map2 (mfc ctx c,
adamc@247 626 fn c' =>
adamc@247 627 (DTable (x, n, c', s), loc))
adamc@125 628
adamc@125 629 and mfvi ctx (x, n, t, e, s) =
adamc@125 630 S.bind2 (mfc ctx t,
adamc@125 631 fn t' =>
adamc@125 632 S.map2 (mfe ctx e,
adamc@125 633 fn e' =>
adamc@125 634 (x, n, t', e', s)))
adamc@20 635 in
adamc@20 636 mfd
adamc@20 637 end
adamc@20 638
adamc@23 639 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 640 mapfoldB {kind = fk,
adamc@23 641 con = fn () => fc,
adamc@23 642 exp = fn () => fe,
adamc@23 643 decl = fn () => fd,
adamc@23 644 bind = fn ((), _) => ()} ()
adamc@23 645
adamc@23 646 fun fold {kind, con, exp, decl} s d =
adamc@23 647 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 648 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 649 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 650 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 651 S.Continue (_, s) => s
adamc@23 652 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
adamc@23 653
adamc@110 654 fun foldMap {kind, con, exp, decl} s d =
adamc@110 655 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 656 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 657 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 658 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 659 S.Continue v => v
adamc@110 660 | S.Return _ => raise Fail "CoreUtil.Decl.foldMap: Impossible"
adamc@110 661
adamc@16 662 end
adamc@20 663
adamc@20 664 structure File = struct
adamc@20 665
adamc@20 666 datatype binder = datatype Exp.binder
adamc@20 667
adamc@20 668 fun mapfoldB (all as {bind, ...}) =
adamc@20 669 let
adamc@20 670 val mfd = Decl.mapfoldB all
adamc@20 671
adamc@20 672 fun mff ctx ds =
adamc@20 673 case ds of
adamc@20 674 nil => S.return2 nil
adamc@20 675 | d :: ds' =>
adamc@20 676 S.bind2 (mfd ctx d,
adamc@20 677 fn d' =>
adamc@20 678 let
adamc@100 679 val ctx' =
adamc@20 680 case #1 d' of
adamc@100 681 DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c))
adamc@192 682 | DDatatype (x, n, xs, xncs) =>
adamc@163 683 let
adamc@192 684 val loc = #2 d'
adamc@192 685 val k = (KType, loc)
adamc@192 686 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@192 687
adamc@192 688 val ctx = bind (ctx, NamedC (x, n, k', NONE))
adamc@163 689 val t = (CNamed n, #2 d')
adamc@192 690 val t = ListUtil.foldli (fn (i, _, t) => (CApp (t, (CRel i, loc)), loc))
adamc@192 691 t xs
adamc@163 692 in
adamc@163 693 foldl (fn ((x, n, to), ctx) =>
adamc@163 694 let
adamc@163 695 val t = case to of
adamc@163 696 NONE => t
adamc@163 697 | SOME t' => (TFun (t', t), #2 d')
adamc@192 698 val t = foldr (fn (x, t) => (TCFun (x, k, t), loc))
adamc@192 699 t xs
adamc@163 700 in
adamc@163 701 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@163 702 end)
adamc@163 703 ctx xncs
adamc@163 704 end
adamc@109 705 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@125 706 | DValRec vis =>
adamc@127 707 foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@125 708 ctx vis
adamc@109 709 | DExport _ => ctx
adamc@247 710 | DTable (x, n, c, s) =>
adamc@247 711 let
adamc@247 712 val t = (CApp ((CFfi ("Basis", "table"), #2 d'), c), #2 d')
adamc@247 713 in
adamc@247 714 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@247 715 end
adamc@20 716 in
adamc@20 717 S.map2 (mff ctx' ds',
adamc@20 718 fn ds' =>
adamc@20 719 d' :: ds')
adamc@20 720 end)
adamc@20 721 in
adamc@20 722 mff
adamc@20 723 end
adamc@20 724
adamc@23 725 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 726 mapfoldB {kind = fk,
adamc@23 727 con = fn () => fc,
adamc@23 728 exp = fn () => fe,
adamc@23 729 decl = fn () => fd,
adamc@23 730 bind = fn ((), _) => ()} ()
adamc@23 731
adamc@20 732 fun mapB {kind, con, exp, decl, bind} ctx ds =
adamc@20 733 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@20 734 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@20 735 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@20 736 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@20 737 bind = bind} ctx ds () of
adamc@20 738 S.Continue (ds, ()) => ds
adamc@20 739 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
adamc@20 740
adamc@23 741 fun fold {kind, con, exp, decl} s d =
adamc@23 742 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 743 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 744 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 745 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 746 S.Continue (_, s) => s
adamc@23 747 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
adamc@23 748
adamc@110 749 fun foldMap {kind, con, exp, decl} s d =
adamc@110 750 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 751 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 752 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 753 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 754 S.Continue v => v
adamc@110 755 | S.Return _ => raise Fail "CoreUtil.File.foldMap: Impossible"
adamc@110 756
adamc@179 757 val maxName = foldl (fn ((d, _) : decl, count) =>
adamc@179 758 case d of
adamc@179 759 DCon (_, n, _, _) => Int.max (n, count)
adamc@192 760 | DDatatype (_, n, _, _) => Int.max (n, count)
adamc@179 761 | DVal (_, n, _, _, _) => Int.max (n, count)
adamc@179 762 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
adamc@247 763 | DExport _ => count
adamc@247 764 | DTable (_, n, _, _) => Int.max (n, count)) 0
adamc@179 765
adamc@20 766 end
adamc@20 767
adamc@20 768 end