annotate src/core_util.sml @ 246:3aa010e97db9

Explify tables
author Adam Chlipala <adamc@hcoop.net>
date Sun, 31 Aug 2008 08:46:22 -0400
parents 766b5475477f
children 5c50b17f5e4a
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@125 624
adamc@125 625 and mfvi ctx (x, n, t, e, s) =
adamc@125 626 S.bind2 (mfc ctx t,
adamc@125 627 fn t' =>
adamc@125 628 S.map2 (mfe ctx e,
adamc@125 629 fn e' =>
adamc@125 630 (x, n, t', e', s)))
adamc@20 631 in
adamc@20 632 mfd
adamc@20 633 end
adamc@20 634
adamc@23 635 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 636 mapfoldB {kind = fk,
adamc@23 637 con = fn () => fc,
adamc@23 638 exp = fn () => fe,
adamc@23 639 decl = fn () => fd,
adamc@23 640 bind = fn ((), _) => ()} ()
adamc@23 641
adamc@23 642 fun fold {kind, con, exp, decl} s d =
adamc@23 643 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 644 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 645 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 646 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 647 S.Continue (_, s) => s
adamc@23 648 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
adamc@23 649
adamc@110 650 fun foldMap {kind, con, exp, decl} s d =
adamc@110 651 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 652 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 653 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 654 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 655 S.Continue v => v
adamc@110 656 | S.Return _ => raise Fail "CoreUtil.Decl.foldMap: Impossible"
adamc@110 657
adamc@16 658 end
adamc@20 659
adamc@20 660 structure File = struct
adamc@20 661
adamc@20 662 datatype binder = datatype Exp.binder
adamc@20 663
adamc@20 664 fun mapfoldB (all as {bind, ...}) =
adamc@20 665 let
adamc@20 666 val mfd = Decl.mapfoldB all
adamc@20 667
adamc@20 668 fun mff ctx ds =
adamc@20 669 case ds of
adamc@20 670 nil => S.return2 nil
adamc@20 671 | d :: ds' =>
adamc@20 672 S.bind2 (mfd ctx d,
adamc@20 673 fn d' =>
adamc@20 674 let
adamc@100 675 val ctx' =
adamc@20 676 case #1 d' of
adamc@100 677 DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c))
adamc@192 678 | DDatatype (x, n, xs, xncs) =>
adamc@163 679 let
adamc@192 680 val loc = #2 d'
adamc@192 681 val k = (KType, loc)
adamc@192 682 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@192 683
adamc@192 684 val ctx = bind (ctx, NamedC (x, n, k', NONE))
adamc@163 685 val t = (CNamed n, #2 d')
adamc@192 686 val t = ListUtil.foldli (fn (i, _, t) => (CApp (t, (CRel i, loc)), loc))
adamc@192 687 t xs
adamc@163 688 in
adamc@163 689 foldl (fn ((x, n, to), ctx) =>
adamc@163 690 let
adamc@163 691 val t = case to of
adamc@163 692 NONE => t
adamc@163 693 | SOME t' => (TFun (t', t), #2 d')
adamc@192 694 val t = foldr (fn (x, t) => (TCFun (x, k, t), loc))
adamc@192 695 t xs
adamc@163 696 in
adamc@163 697 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@163 698 end)
adamc@163 699 ctx xncs
adamc@163 700 end
adamc@109 701 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@125 702 | DValRec vis =>
adamc@127 703 foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@125 704 ctx vis
adamc@109 705 | DExport _ => ctx
adamc@20 706 in
adamc@20 707 S.map2 (mff ctx' ds',
adamc@20 708 fn ds' =>
adamc@20 709 d' :: ds')
adamc@20 710 end)
adamc@20 711 in
adamc@20 712 mff
adamc@20 713 end
adamc@20 714
adamc@23 715 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 716 mapfoldB {kind = fk,
adamc@23 717 con = fn () => fc,
adamc@23 718 exp = fn () => fe,
adamc@23 719 decl = fn () => fd,
adamc@23 720 bind = fn ((), _) => ()} ()
adamc@23 721
adamc@20 722 fun mapB {kind, con, exp, decl, bind} ctx ds =
adamc@20 723 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@20 724 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@20 725 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@20 726 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@20 727 bind = bind} ctx ds () of
adamc@20 728 S.Continue (ds, ()) => ds
adamc@20 729 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
adamc@20 730
adamc@23 731 fun fold {kind, con, exp, decl} s d =
adamc@23 732 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 733 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 734 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 735 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 736 S.Continue (_, s) => s
adamc@23 737 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
adamc@23 738
adamc@110 739 fun foldMap {kind, con, exp, decl} s d =
adamc@110 740 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 741 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 742 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 743 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 744 S.Continue v => v
adamc@110 745 | S.Return _ => raise Fail "CoreUtil.File.foldMap: Impossible"
adamc@110 746
adamc@179 747 val maxName = foldl (fn ((d, _) : decl, count) =>
adamc@179 748 case d of
adamc@179 749 DCon (_, n, _, _) => Int.max (n, count)
adamc@192 750 | DDatatype (_, n, _, _) => Int.max (n, count)
adamc@179 751 | DVal (_, n, _, _, _) => Int.max (n, count)
adamc@179 752 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
adamc@179 753 | DExport _ => count) 0
adamc@179 754
adamc@20 755 end
adamc@20 756
adamc@20 757 end