annotate src/core_util.sml @ 453:787d4931fb07

Almost have that nested save function compiling
author Adam Chlipala <adamc@hcoop.net>
date Sat, 01 Nov 2008 21:19:43 -0400
parents 07f6576aeb0a
children 5c9606deacb6
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@193 181 | (CFold (d1, r1), CFold (d2, r2)) =>
adamc@193 182 join (Kind.compare (d1, r2),
adamc@193 183 fn () => Kind.compare (r1, r2))
adamc@193 184 | (CFold _, _) => LESS
adamc@193 185 | (_, CFold _) => 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@69 264 | CFold (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@69 269 (CFold (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@16 334 datatype binder =
adamc@16 335 RelC of string * kind
adamc@20 336 | NamedC of string * int * kind * con option
adamc@16 337 | RelE of string * con
adamc@109 338 | NamedE of string * int * con * exp option * string
adamc@16 339
adamc@16 340 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@16 341 let
adamc@16 342 val mfk = Kind.mapfold fk
adamc@16 343
adamc@16 344 fun bind' (ctx, b) =
adamc@16 345 let
adamc@16 346 val b' = case b of
adamc@16 347 Con.Rel x => RelC x
adamc@16 348 | Con.Named x => NamedC x
adamc@16 349 in
adamc@16 350 bind (ctx, b')
adamc@16 351 end
adamc@16 352 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@16 353
adamc@16 354 fun mfe ctx e acc =
adamc@16 355 S.bindP (mfe' ctx e acc, fe ctx)
adamc@16 356
adamc@16 357 and mfe' ctx (eAll as (e, loc)) =
adamc@16 358 case e of
adamc@16 359 EPrim _ => S.return2 eAll
adamc@16 360 | ERel _ => S.return2 eAll
adamc@16 361 | ENamed _ => S.return2 eAll
adamc@192 362 | ECon (dk, pc, cs, NONE) =>
adamc@192 363 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 364 fn cs' =>
adamc@192 365 (ECon (dk, pc, cs', NONE), loc))
adamc@192 366 | ECon (dk, n, cs, SOME e) =>
adamc@192 367 S.bind2 (mfe ctx e,
adamc@192 368 fn e' =>
adamc@192 369 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@192 370 fn cs' =>
adamc@192 371 (ECon (dk, n, cs', SOME e'), loc)))
adamc@48 372 | EFfi _ => S.return2 eAll
adamc@48 373 | EFfiApp (m, x, es) =>
adamc@192 374 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@48 375 fn es' =>
adamc@48 376 (EFfiApp (m, x, es'), loc))
adamc@16 377 | EApp (e1, e2) =>
adamc@16 378 S.bind2 (mfe ctx e1,
adamc@16 379 fn e1' =>
adamc@16 380 S.map2 (mfe ctx e2,
adamc@16 381 fn e2' =>
adamc@16 382 (EApp (e1', e2'), loc)))
adamc@26 383 | EAbs (x, dom, ran, e) =>
adamc@26 384 S.bind2 (mfc ctx dom,
adamc@26 385 fn dom' =>
adamc@26 386 S.bind2 (mfc ctx ran,
adamc@26 387 fn ran' =>
adamc@26 388 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 389 fn e' =>
adamc@26 390 (EAbs (x, dom', ran', e'), loc))))
adamc@16 391
adamc@16 392 | ECApp (e, c) =>
adamc@16 393 S.bind2 (mfe ctx e,
adamc@16 394 fn e' =>
adamc@16 395 S.map2 (mfc ctx c,
adamc@16 396 fn c' =>
adamc@16 397 (ECApp (e', c'), loc)))
adamc@16 398 | ECAbs (x, k, e) =>
adamc@16 399 S.bind2 (mfk k,
adamc@16 400 fn k' =>
adamc@16 401 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@16 402 fn e' =>
adamc@16 403 (ECAbs (x, k', e'), loc)))
adamc@16 404
adamc@16 405 | ERecord xes =>
adamc@29 406 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@16 407 S.bind2 (mfc ctx x,
adamc@16 408 fn x' =>
adamc@29 409 S.bind2 (mfe ctx e,
adamc@29 410 fn e' =>
adamc@29 411 S.map2 (mfc ctx t,
adamc@29 412 fn t' =>
adamc@29 413 (x', e', t')))))
adamc@16 414 xes,
adamc@16 415 fn xes' =>
adamc@16 416 (ERecord xes', loc))
adamc@16 417 | EField (e, c, {field, rest}) =>
adamc@16 418 S.bind2 (mfe ctx e,
adamc@16 419 fn e' =>
adamc@16 420 S.bind2 (mfc ctx c,
adamc@16 421 fn c' =>
adamc@16 422 S.bind2 (mfc ctx field,
adamc@16 423 fn field' =>
adamc@16 424 S.map2 (mfc ctx rest,
adamc@16 425 fn rest' =>
adamc@16 426 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@445 427 | EConcat (e1, c1, e2, c2) =>
adamc@339 428 S.bind2 (mfe ctx e1,
adamc@339 429 fn e1' =>
adamc@445 430 S.bind2 (mfc ctx c1,
adamc@445 431 fn c1' =>
adamc@339 432 S.bind2 (mfe ctx e2,
adamc@339 433 fn e2' =>
adamc@445 434 S.map2 (mfc ctx c2,
adamc@445 435 fn c2' =>
adamc@445 436 (EConcat (e1', c1', e2', c2'),
adamc@445 437 loc)))))
adamc@149 438 | ECut (e, c, {field, rest}) =>
adamc@149 439 S.bind2 (mfe ctx e,
adamc@149 440 fn e' =>
adamc@149 441 S.bind2 (mfc ctx c,
adamc@149 442 fn c' =>
adamc@149 443 S.bind2 (mfc ctx field,
adamc@149 444 fn field' =>
adamc@149 445 S.map2 (mfc ctx rest,
adamc@149 446 fn rest' =>
adamc@149 447 (ECut (e', c', {field = field', rest = rest'}), loc)))))
adamc@73 448 | EFold k =>
adamc@73 449 S.map2 (mfk k,
adamc@73 450 fn k' =>
adamc@73 451 (EFold k', loc))
adamc@102 452
adamc@182 453 | ECase (e, pes, {disc, result}) =>
adamc@177 454 S.bind2 (mfe ctx e,
adamc@177 455 fn e' =>
adamc@177 456 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@193 457 let
adamc@193 458 fun pb ((p, _), ctx) =
adamc@193 459 case p of
adamc@193 460 PWild => ctx
adamc@193 461 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@193 462 | PPrim _ => ctx
adamc@193 463 | PCon (_, _, _, NONE) => ctx
adamc@193 464 | PCon (_, _, _, SOME p) => pb (p, ctx)
adamc@193 465 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@193 466 pb (p, ctx)) ctx xps
adamc@193 467 in
adamc@193 468 S.bind2 (mfp ctx p,
adamc@193 469 fn p' =>
adamc@193 470 S.map2 (mfe (pb (p', ctx)) e,
adamc@193 471 fn e' => (p', e')))
adamc@193 472 end) pes,
adamc@177 473 fn pes' =>
adamc@182 474 S.bind2 (mfc ctx disc,
adamc@182 475 fn disc' =>
adamc@182 476 S.map2 (mfc ctx result,
adamc@182 477 fn result' =>
adamc@182 478 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@177 479
adamc@102 480 | EWrite e =>
adamc@102 481 S.map2 (mfe ctx e,
adamc@102 482 fn e' =>
adamc@102 483 (EWrite e', loc))
adamc@110 484
adamc@110 485 | EClosure (n, es) =>
adamc@110 486 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@110 487 fn es' =>
adamc@110 488 (EClosure (n, es'), loc))
adamc@193 489
adamc@450 490 | ELet (x, t, e1, e2) =>
adamc@450 491 S.bind2 (mfc ctx t,
adamc@450 492 fn t' =>
adamc@450 493 S.bind2 (mfe ctx e1,
adamc@450 494 fn e1' =>
adamc@453 495 S.map2 (mfe (bind (ctx, RelE (x, t'))) e2,
adamc@450 496 fn e2' =>
adamc@450 497 (ELet (x, t', e1', e2'), loc))))
adamc@450 498
adamc@193 499 and mfp ctx (pAll as (p, loc)) =
adamc@193 500 case p of
adamc@193 501 PWild => S.return2 pAll
adamc@193 502 | PVar (x, t) =>
adamc@193 503 S.map2 (mfc ctx t,
adamc@193 504 fn t' =>
adamc@193 505 (PVar (x, t'), loc))
adamc@193 506 | PPrim _ => S.return2 pAll
adamc@193 507 | PCon (dk, pc, args, po) =>
adamc@193 508 S.bind2 (mfpc ctx pc,
adamc@193 509 fn pc' =>
adamc@193 510 S.bind2 (ListUtil.mapfold (mfc ctx) args,
adamc@193 511 fn args' =>
adamc@193 512 S.map2 ((case po of
adamc@193 513 NONE => S.return2 NONE
adamc@193 514 | SOME p => S.map2 (mfp ctx p, SOME)),
adamc@193 515 fn po' =>
adamc@193 516 (PCon (dk, pc', args', po'), loc))))
adamc@193 517 | PRecord xps =>
adamc@193 518 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
adamc@193 519 S.bind2 (mfp ctx p,
adamc@193 520 fn p' =>
adamc@193 521 S.map2 (mfc ctx c,
adamc@193 522 fn c' =>
adamc@193 523 (x, p', c')))) xps,
adamc@193 524 fn xps' =>
adamc@193 525 (PRecord xps', loc))
adamc@193 526
adamc@193 527 and mfpc ctx pc =
adamc@193 528 case pc of
adamc@193 529 PConVar _ => S.return2 pc
adamc@193 530 | PConFfi {mod = m, datatyp, params, con, arg, kind} =>
adamc@193 531 S.map2 ((case arg of
adamc@193 532 NONE => S.return2 NONE
adamc@193 533 | SOME c => S.map2 (mfc ctx c, SOME)),
adamc@193 534 fn arg' =>
adamc@193 535 PConFfi {mod = m, datatyp = datatyp, params = params,
adamc@193 536 con = con, arg = arg', kind = kind})
adamc@16 537 in
adamc@16 538 mfe
adamc@16 539 end
adamc@16 540
adamc@16 541 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@16 542 mapfoldB {kind = fk,
adamc@16 543 con = fn () => fc,
adamc@16 544 exp = fn () => fe,
adamc@16 545 bind = fn ((), _) => ()} ()
adamc@16 546
adamc@21 547 fun mapB {kind, con, exp, bind} ctx e =
adamc@21 548 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@21 549 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@21 550 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@21 551 bind = bind} ctx e () of
adamc@21 552 S.Continue (e, ()) => e
adamc@21 553 | S.Return _ => raise Fail "CoreUtil.Exp.mapB: Impossible"
adamc@21 554
adamc@16 555 fun map {kind, con, exp} e =
adamc@16 556 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 557 con = fn c => fn () => S.Continue (con c, ()),
adamc@16 558 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@16 559 S.Return () => raise Fail "Core_util.Exp.map"
adamc@16 560 | S.Continue (e, ()) => e
adamc@16 561
adamc@23 562 fun fold {kind, con, exp} s e =
adamc@23 563 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 564 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 565 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@23 566 S.Continue (_, s) => s
adamc@23 567 | S.Return _ => raise Fail "CoreUtil.Exp.fold: Impossible"
adamc@23 568
adamc@16 569 fun exists {kind, con, exp} k =
adamc@16 570 case mapfold {kind = fn k => fn () =>
adamc@16 571 if kind k then
adamc@16 572 S.Return ()
adamc@16 573 else
adamc@16 574 S.Continue (k, ()),
adamc@16 575 con = fn c => fn () =>
adamc@16 576 if con c then
adamc@16 577 S.Return ()
adamc@16 578 else
adamc@16 579 S.Continue (c, ()),
adamc@16 580 exp = fn e => fn () =>
adamc@16 581 if exp e then
adamc@16 582 S.Return ()
adamc@16 583 else
adamc@16 584 S.Continue (e, ())} k () of
adamc@16 585 S.Return _ => true
adamc@16 586 | S.Continue _ => false
adamc@16 587
adamc@443 588 fun foldMap {kind, con, exp} s e =
adamc@443 589 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@443 590 con = fn c => fn s => S.Continue (con (c, s)),
adamc@443 591 exp = fn e => fn s => S.Continue (exp (e, s))} e s of
adamc@443 592 S.Continue v => v
adamc@443 593 | S.Return _ => raise Fail "CoreUtil.Exp.foldMap: Impossible"
adamc@443 594
adamc@16 595 end
adamc@16 596
adamc@20 597 structure Decl = struct
adamc@20 598
adamc@20 599 datatype binder = datatype Exp.binder
adamc@20 600
adamc@20 601 fun mapfoldB {kind = fk, con = fc, exp = fe, decl = fd, bind} =
adamc@20 602 let
adamc@20 603 val mfk = Kind.mapfold fk
adamc@20 604
adamc@20 605 fun bind' (ctx, b) =
adamc@20 606 let
adamc@20 607 val b' = case b of
adamc@20 608 Con.Rel x => RelC x
adamc@20 609 | Con.Named x => NamedC x
adamc@20 610 in
adamc@20 611 bind (ctx, b')
adamc@20 612 end
adamc@20 613 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@20 614
adamc@20 615 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind}
adamc@20 616
adamc@20 617 fun mfd ctx d acc =
adamc@20 618 S.bindP (mfd' ctx d acc, fd ctx)
adamc@20 619
adamc@20 620 and mfd' ctx (dAll as (d, loc)) =
adamc@20 621 case d of
adamc@20 622 DCon (x, n, k, c) =>
adamc@20 623 S.bind2 (mfk k,
adamc@20 624 fn k' =>
adamc@20 625 S.map2 (mfc ctx c,
adamc@20 626 fn c' =>
adamc@20 627 (DCon (x, n, k', c'), loc)))
adamc@192 628 | DDatatype (x, n, xs, xncs) =>
adamc@196 629 let
adamc@196 630 val k = (KType, loc)
adamc@196 631 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@196 632 val ctx' = bind (ctx, NamedC (x, n, k', NONE))
adamc@196 633 in
adamc@196 634 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@196 635 case c of
adamc@196 636 NONE => S.return2 (x, n, c)
adamc@196 637 | SOME c =>
adamc@196 638 S.map2 (mfc ctx' c,
adamc@196 639 fn c' => (x, n, SOME c'))) xncs,
adamc@196 640 fn xncs' =>
adamc@196 641 (DDatatype (x, n, xs, xncs'), loc))
adamc@196 642 end
adamc@125 643 | DVal vi =>
adamc@125 644 S.map2 (mfvi ctx vi,
adamc@125 645 fn vi' =>
adamc@125 646 (DVal vi', loc))
adamc@125 647 | DValRec vis =>
adamc@127 648 let
adamc@127 649 val ctx = foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@127 650 ctx vis
adamc@127 651 in
adamc@127 652 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@127 653 fn vis' =>
adamc@127 654 (DValRec vis', loc))
adamc@127 655 end
adamc@109 656 | DExport _ => S.return2 dAll
adamc@247 657 | DTable (x, n, c, s) =>
adamc@247 658 S.map2 (mfc ctx c,
adamc@247 659 fn c' =>
adamc@247 660 (DTable (x, n, c', s), loc))
adamc@338 661 | DSequence _ => S.return2 dAll
adamc@271 662 | DDatabase _ => S.return2 dAll
adamc@125 663
adamc@125 664 and mfvi ctx (x, n, t, e, s) =
adamc@125 665 S.bind2 (mfc ctx t,
adamc@125 666 fn t' =>
adamc@125 667 S.map2 (mfe ctx e,
adamc@125 668 fn e' =>
adamc@125 669 (x, n, t', e', s)))
adamc@20 670 in
adamc@20 671 mfd
adamc@20 672 end
adamc@20 673
adamc@23 674 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 675 mapfoldB {kind = fk,
adamc@23 676 con = fn () => fc,
adamc@23 677 exp = fn () => fe,
adamc@23 678 decl = fn () => fd,
adamc@23 679 bind = fn ((), _) => ()} ()
adamc@23 680
adamc@23 681 fun fold {kind, con, exp, decl} s d =
adamc@23 682 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 683 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 684 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 685 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 686 S.Continue (_, s) => s
adamc@23 687 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
adamc@23 688
adamc@110 689 fun foldMap {kind, con, exp, decl} s d =
adamc@110 690 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 691 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 692 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 693 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 694 S.Continue v => v
adamc@110 695 | S.Return _ => raise Fail "CoreUtil.Decl.foldMap: Impossible"
adamc@110 696
adamc@16 697 end
adamc@20 698
adamc@20 699 structure File = struct
adamc@20 700
adamc@20 701 datatype binder = datatype Exp.binder
adamc@20 702
adamc@20 703 fun mapfoldB (all as {bind, ...}) =
adamc@20 704 let
adamc@20 705 val mfd = Decl.mapfoldB all
adamc@20 706
adamc@20 707 fun mff ctx ds =
adamc@20 708 case ds of
adamc@20 709 nil => S.return2 nil
adamc@20 710 | d :: ds' =>
adamc@20 711 S.bind2 (mfd ctx d,
adamc@20 712 fn d' =>
adamc@20 713 let
adamc@100 714 val ctx' =
adamc@20 715 case #1 d' of
adamc@100 716 DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c))
adamc@192 717 | DDatatype (x, n, xs, xncs) =>
adamc@163 718 let
adamc@192 719 val loc = #2 d'
adamc@192 720 val k = (KType, loc)
adamc@192 721 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@192 722
adamc@192 723 val ctx = bind (ctx, NamedC (x, n, k', NONE))
adamc@163 724 val t = (CNamed n, #2 d')
adamc@192 725 val t = ListUtil.foldli (fn (i, _, t) => (CApp (t, (CRel i, loc)), loc))
adamc@192 726 t xs
adamc@163 727 in
adamc@163 728 foldl (fn ((x, n, to), ctx) =>
adamc@163 729 let
adamc@163 730 val t = case to of
adamc@163 731 NONE => t
adamc@163 732 | SOME t' => (TFun (t', t), #2 d')
adamc@192 733 val t = foldr (fn (x, t) => (TCFun (x, k, t), loc))
adamc@192 734 t xs
adamc@163 735 in
adamc@163 736 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@163 737 end)
adamc@163 738 ctx xncs
adamc@163 739 end
adamc@109 740 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@125 741 | DValRec vis =>
adamc@127 742 foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@125 743 ctx vis
adamc@109 744 | DExport _ => ctx
adamc@247 745 | DTable (x, n, c, s) =>
adamc@247 746 let
adamc@338 747 val t = (CApp ((CFfi ("Basis", "sql_table"), #2 d'), c), #2 d')
adamc@338 748 in
adamc@338 749 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@338 750 end
adamc@338 751 | DSequence (x, n, s) =>
adamc@338 752 let
adamc@338 753 val t = (CFfi ("Basis", "sql_sequence"), #2 d')
adamc@247 754 in
adamc@247 755 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@247 756 end
adamc@271 757 | DDatabase _ => ctx
adamc@20 758 in
adamc@20 759 S.map2 (mff ctx' ds',
adamc@20 760 fn ds' =>
adamc@20 761 d' :: ds')
adamc@20 762 end)
adamc@20 763 in
adamc@20 764 mff
adamc@20 765 end
adamc@20 766
adamc@23 767 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 768 mapfoldB {kind = fk,
adamc@23 769 con = fn () => fc,
adamc@23 770 exp = fn () => fe,
adamc@23 771 decl = fn () => fd,
adamc@23 772 bind = fn ((), _) => ()} ()
adamc@23 773
adamc@20 774 fun mapB {kind, con, exp, decl, bind} ctx ds =
adamc@20 775 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@20 776 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@20 777 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@20 778 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@20 779 bind = bind} ctx ds () of
adamc@20 780 S.Continue (ds, ()) => ds
adamc@20 781 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
adamc@20 782
adamc@23 783 fun fold {kind, con, exp, decl} s d =
adamc@23 784 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 785 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 786 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 787 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 788 S.Continue (_, s) => s
adamc@23 789 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
adamc@23 790
adamc@110 791 fun foldMap {kind, con, exp, decl} s d =
adamc@110 792 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 793 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 794 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 795 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 796 S.Continue v => v
adamc@110 797 | S.Return _ => raise Fail "CoreUtil.File.foldMap: Impossible"
adamc@110 798
adamc@179 799 val maxName = foldl (fn ((d, _) : decl, count) =>
adamc@179 800 case d of
adamc@179 801 DCon (_, n, _, _) => Int.max (n, count)
adamc@398 802 | DDatatype (_, n, _, ns) =>
adamc@398 803 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@398 804 (Int.max (n, count)) ns
adamc@179 805 | DVal (_, n, _, _, _) => Int.max (n, count)
adamc@179 806 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
adamc@247 807 | DExport _ => count
adamc@271 808 | DTable (_, n, _, _) => Int.max (n, count)
adamc@338 809 | DSequence (_, n, _) => Int.max (n, count)
adamc@271 810 | DDatabase _ => count) 0
adamc@179 811
adamc@20 812 end
adamc@20 813
adamc@20 814 end