annotate src/core_util.sml @ 411:06fcddcd20d3

Sum demo, minus inference of {Unit}s
author Adam Chlipala <adamc@hcoop.net>
date Tue, 21 Oct 2008 19:24:39 -0400
parents ab3177746c78
children bd9ee9aeca2f
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@339 427 | EWith (e1, c, e2, {field, rest}) =>
adamc@339 428 S.bind2 (mfe ctx e1,
adamc@339 429 fn e1' =>
adamc@339 430 S.bind2 (mfc ctx c,
adamc@339 431 fn c' =>
adamc@339 432 S.bind2 (mfe ctx e2,
adamc@339 433 fn e2' =>
adamc@339 434 S.bind2 (mfc ctx field,
adamc@339 435 fn field' =>
adamc@339 436 S.map2 (mfc ctx rest,
adamc@339 437 fn rest' =>
adamc@339 438 (EWith (e1', c', e2', {field = field', rest = rest'}),
adamc@339 439 loc))))))
adamc@149 440 | ECut (e, c, {field, rest}) =>
adamc@149 441 S.bind2 (mfe ctx e,
adamc@149 442 fn e' =>
adamc@149 443 S.bind2 (mfc ctx c,
adamc@149 444 fn c' =>
adamc@149 445 S.bind2 (mfc ctx field,
adamc@149 446 fn field' =>
adamc@149 447 S.map2 (mfc ctx rest,
adamc@149 448 fn rest' =>
adamc@149 449 (ECut (e', c', {field = field', rest = rest'}), loc)))))
adamc@73 450 | EFold k =>
adamc@73 451 S.map2 (mfk k,
adamc@73 452 fn k' =>
adamc@73 453 (EFold k', loc))
adamc@102 454
adamc@182 455 | ECase (e, pes, {disc, result}) =>
adamc@177 456 S.bind2 (mfe ctx e,
adamc@177 457 fn e' =>
adamc@177 458 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@193 459 let
adamc@193 460 fun pb ((p, _), ctx) =
adamc@193 461 case p of
adamc@193 462 PWild => ctx
adamc@193 463 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@193 464 | PPrim _ => ctx
adamc@193 465 | PCon (_, _, _, NONE) => ctx
adamc@193 466 | PCon (_, _, _, SOME p) => pb (p, ctx)
adamc@193 467 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@193 468 pb (p, ctx)) ctx xps
adamc@193 469 in
adamc@193 470 S.bind2 (mfp ctx p,
adamc@193 471 fn p' =>
adamc@193 472 S.map2 (mfe (pb (p', ctx)) e,
adamc@193 473 fn e' => (p', e')))
adamc@193 474 end) pes,
adamc@177 475 fn pes' =>
adamc@182 476 S.bind2 (mfc ctx disc,
adamc@182 477 fn disc' =>
adamc@182 478 S.map2 (mfc ctx result,
adamc@182 479 fn result' =>
adamc@182 480 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@177 481
adamc@102 482 | EWrite e =>
adamc@102 483 S.map2 (mfe ctx e,
adamc@102 484 fn e' =>
adamc@102 485 (EWrite e', loc))
adamc@110 486
adamc@110 487 | EClosure (n, es) =>
adamc@110 488 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@110 489 fn es' =>
adamc@110 490 (EClosure (n, es'), loc))
adamc@193 491
adamc@193 492 and mfp ctx (pAll as (p, loc)) =
adamc@193 493 case p of
adamc@193 494 PWild => S.return2 pAll
adamc@193 495 | PVar (x, t) =>
adamc@193 496 S.map2 (mfc ctx t,
adamc@193 497 fn t' =>
adamc@193 498 (PVar (x, t'), loc))
adamc@193 499 | PPrim _ => S.return2 pAll
adamc@193 500 | PCon (dk, pc, args, po) =>
adamc@193 501 S.bind2 (mfpc ctx pc,
adamc@193 502 fn pc' =>
adamc@193 503 S.bind2 (ListUtil.mapfold (mfc ctx) args,
adamc@193 504 fn args' =>
adamc@193 505 S.map2 ((case po of
adamc@193 506 NONE => S.return2 NONE
adamc@193 507 | SOME p => S.map2 (mfp ctx p, SOME)),
adamc@193 508 fn po' =>
adamc@193 509 (PCon (dk, pc', args', po'), loc))))
adamc@193 510 | PRecord xps =>
adamc@193 511 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
adamc@193 512 S.bind2 (mfp ctx p,
adamc@193 513 fn p' =>
adamc@193 514 S.map2 (mfc ctx c,
adamc@193 515 fn c' =>
adamc@193 516 (x, p', c')))) xps,
adamc@193 517 fn xps' =>
adamc@193 518 (PRecord xps', loc))
adamc@193 519
adamc@193 520 and mfpc ctx pc =
adamc@193 521 case pc of
adamc@193 522 PConVar _ => S.return2 pc
adamc@193 523 | PConFfi {mod = m, datatyp, params, con, arg, kind} =>
adamc@193 524 S.map2 ((case arg of
adamc@193 525 NONE => S.return2 NONE
adamc@193 526 | SOME c => S.map2 (mfc ctx c, SOME)),
adamc@193 527 fn arg' =>
adamc@193 528 PConFfi {mod = m, datatyp = datatyp, params = params,
adamc@193 529 con = con, arg = arg', kind = kind})
adamc@16 530 in
adamc@16 531 mfe
adamc@16 532 end
adamc@16 533
adamc@16 534 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@16 535 mapfoldB {kind = fk,
adamc@16 536 con = fn () => fc,
adamc@16 537 exp = fn () => fe,
adamc@16 538 bind = fn ((), _) => ()} ()
adamc@16 539
adamc@21 540 fun mapB {kind, con, exp, bind} ctx e =
adamc@21 541 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@21 542 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@21 543 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@21 544 bind = bind} ctx e () of
adamc@21 545 S.Continue (e, ()) => e
adamc@21 546 | S.Return _ => raise Fail "CoreUtil.Exp.mapB: Impossible"
adamc@21 547
adamc@16 548 fun map {kind, con, exp} e =
adamc@16 549 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 550 con = fn c => fn () => S.Continue (con c, ()),
adamc@16 551 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@16 552 S.Return () => raise Fail "Core_util.Exp.map"
adamc@16 553 | S.Continue (e, ()) => e
adamc@16 554
adamc@23 555 fun fold {kind, con, exp} s e =
adamc@23 556 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 557 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 558 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@23 559 S.Continue (_, s) => s
adamc@23 560 | S.Return _ => raise Fail "CoreUtil.Exp.fold: Impossible"
adamc@23 561
adamc@16 562 fun exists {kind, con, exp} k =
adamc@16 563 case mapfold {kind = fn k => fn () =>
adamc@16 564 if kind k then
adamc@16 565 S.Return ()
adamc@16 566 else
adamc@16 567 S.Continue (k, ()),
adamc@16 568 con = fn c => fn () =>
adamc@16 569 if con c then
adamc@16 570 S.Return ()
adamc@16 571 else
adamc@16 572 S.Continue (c, ()),
adamc@16 573 exp = fn e => fn () =>
adamc@16 574 if exp e then
adamc@16 575 S.Return ()
adamc@16 576 else
adamc@16 577 S.Continue (e, ())} k () of
adamc@16 578 S.Return _ => true
adamc@16 579 | S.Continue _ => false
adamc@16 580
adamc@16 581 end
adamc@16 582
adamc@20 583 structure Decl = struct
adamc@20 584
adamc@20 585 datatype binder = datatype Exp.binder
adamc@20 586
adamc@20 587 fun mapfoldB {kind = fk, con = fc, exp = fe, decl = fd, bind} =
adamc@20 588 let
adamc@20 589 val mfk = Kind.mapfold fk
adamc@20 590
adamc@20 591 fun bind' (ctx, b) =
adamc@20 592 let
adamc@20 593 val b' = case b of
adamc@20 594 Con.Rel x => RelC x
adamc@20 595 | Con.Named x => NamedC x
adamc@20 596 in
adamc@20 597 bind (ctx, b')
adamc@20 598 end
adamc@20 599 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@20 600
adamc@20 601 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind}
adamc@20 602
adamc@20 603 fun mfd ctx d acc =
adamc@20 604 S.bindP (mfd' ctx d acc, fd ctx)
adamc@20 605
adamc@20 606 and mfd' ctx (dAll as (d, loc)) =
adamc@20 607 case d of
adamc@20 608 DCon (x, n, k, c) =>
adamc@20 609 S.bind2 (mfk k,
adamc@20 610 fn k' =>
adamc@20 611 S.map2 (mfc ctx c,
adamc@20 612 fn c' =>
adamc@20 613 (DCon (x, n, k', c'), loc)))
adamc@192 614 | DDatatype (x, n, xs, xncs) =>
adamc@196 615 let
adamc@196 616 val k = (KType, loc)
adamc@196 617 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@196 618 val ctx' = bind (ctx, NamedC (x, n, k', NONE))
adamc@196 619 in
adamc@196 620 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@196 621 case c of
adamc@196 622 NONE => S.return2 (x, n, c)
adamc@196 623 | SOME c =>
adamc@196 624 S.map2 (mfc ctx' c,
adamc@196 625 fn c' => (x, n, SOME c'))) xncs,
adamc@196 626 fn xncs' =>
adamc@196 627 (DDatatype (x, n, xs, xncs'), loc))
adamc@196 628 end
adamc@125 629 | DVal vi =>
adamc@125 630 S.map2 (mfvi ctx vi,
adamc@125 631 fn vi' =>
adamc@125 632 (DVal vi', loc))
adamc@125 633 | DValRec vis =>
adamc@127 634 let
adamc@127 635 val ctx = foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@127 636 ctx vis
adamc@127 637 in
adamc@127 638 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@127 639 fn vis' =>
adamc@127 640 (DValRec vis', loc))
adamc@127 641 end
adamc@109 642 | DExport _ => S.return2 dAll
adamc@247 643 | DTable (x, n, c, s) =>
adamc@247 644 S.map2 (mfc ctx c,
adamc@247 645 fn c' =>
adamc@247 646 (DTable (x, n, c', s), loc))
adamc@338 647 | DSequence _ => S.return2 dAll
adamc@271 648 | DDatabase _ => S.return2 dAll
adamc@125 649
adamc@125 650 and mfvi ctx (x, n, t, e, s) =
adamc@125 651 S.bind2 (mfc ctx t,
adamc@125 652 fn t' =>
adamc@125 653 S.map2 (mfe ctx e,
adamc@125 654 fn e' =>
adamc@125 655 (x, n, t', e', s)))
adamc@20 656 in
adamc@20 657 mfd
adamc@20 658 end
adamc@20 659
adamc@23 660 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 661 mapfoldB {kind = fk,
adamc@23 662 con = fn () => fc,
adamc@23 663 exp = fn () => fe,
adamc@23 664 decl = fn () => fd,
adamc@23 665 bind = fn ((), _) => ()} ()
adamc@23 666
adamc@23 667 fun fold {kind, con, exp, decl} s d =
adamc@23 668 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 669 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 670 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 671 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 672 S.Continue (_, s) => s
adamc@23 673 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
adamc@23 674
adamc@110 675 fun foldMap {kind, con, exp, decl} s d =
adamc@110 676 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 677 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 678 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 679 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 680 S.Continue v => v
adamc@110 681 | S.Return _ => raise Fail "CoreUtil.Decl.foldMap: Impossible"
adamc@110 682
adamc@16 683 end
adamc@20 684
adamc@20 685 structure File = struct
adamc@20 686
adamc@20 687 datatype binder = datatype Exp.binder
adamc@20 688
adamc@20 689 fun mapfoldB (all as {bind, ...}) =
adamc@20 690 let
adamc@20 691 val mfd = Decl.mapfoldB all
adamc@20 692
adamc@20 693 fun mff ctx ds =
adamc@20 694 case ds of
adamc@20 695 nil => S.return2 nil
adamc@20 696 | d :: ds' =>
adamc@20 697 S.bind2 (mfd ctx d,
adamc@20 698 fn d' =>
adamc@20 699 let
adamc@100 700 val ctx' =
adamc@20 701 case #1 d' of
adamc@100 702 DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c))
adamc@192 703 | DDatatype (x, n, xs, xncs) =>
adamc@163 704 let
adamc@192 705 val loc = #2 d'
adamc@192 706 val k = (KType, loc)
adamc@192 707 val k' = foldl (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@192 708
adamc@192 709 val ctx = bind (ctx, NamedC (x, n, k', NONE))
adamc@163 710 val t = (CNamed n, #2 d')
adamc@192 711 val t = ListUtil.foldli (fn (i, _, t) => (CApp (t, (CRel i, loc)), loc))
adamc@192 712 t xs
adamc@163 713 in
adamc@163 714 foldl (fn ((x, n, to), ctx) =>
adamc@163 715 let
adamc@163 716 val t = case to of
adamc@163 717 NONE => t
adamc@163 718 | SOME t' => (TFun (t', t), #2 d')
adamc@192 719 val t = foldr (fn (x, t) => (TCFun (x, k, t), loc))
adamc@192 720 t xs
adamc@163 721 in
adamc@163 722 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@163 723 end)
adamc@163 724 ctx xncs
adamc@163 725 end
adamc@109 726 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@125 727 | DValRec vis =>
adamc@127 728 foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s)))
adamc@125 729 ctx vis
adamc@109 730 | DExport _ => ctx
adamc@247 731 | DTable (x, n, c, s) =>
adamc@247 732 let
adamc@338 733 val t = (CApp ((CFfi ("Basis", "sql_table"), #2 d'), c), #2 d')
adamc@338 734 in
adamc@338 735 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@338 736 end
adamc@338 737 | DSequence (x, n, s) =>
adamc@338 738 let
adamc@338 739 val t = (CFfi ("Basis", "sql_sequence"), #2 d')
adamc@247 740 in
adamc@247 741 bind (ctx, NamedE (x, n, t, NONE, s))
adamc@247 742 end
adamc@271 743 | DDatabase _ => ctx
adamc@20 744 in
adamc@20 745 S.map2 (mff ctx' ds',
adamc@20 746 fn ds' =>
adamc@20 747 d' :: ds')
adamc@20 748 end)
adamc@20 749 in
adamc@20 750 mff
adamc@20 751 end
adamc@20 752
adamc@23 753 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 754 mapfoldB {kind = fk,
adamc@23 755 con = fn () => fc,
adamc@23 756 exp = fn () => fe,
adamc@23 757 decl = fn () => fd,
adamc@23 758 bind = fn ((), _) => ()} ()
adamc@23 759
adamc@20 760 fun mapB {kind, con, exp, decl, bind} ctx ds =
adamc@20 761 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@20 762 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@20 763 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@20 764 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@20 765 bind = bind} ctx ds () of
adamc@20 766 S.Continue (ds, ()) => ds
adamc@20 767 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
adamc@20 768
adamc@23 769 fun fold {kind, con, exp, decl} s d =
adamc@23 770 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 771 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 772 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 773 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 774 S.Continue (_, s) => s
adamc@23 775 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
adamc@23 776
adamc@110 777 fun foldMap {kind, con, exp, decl} s d =
adamc@110 778 case mapfold {kind = fn k => fn s => S.Continue (kind (k, s)),
adamc@110 779 con = fn c => fn s => S.Continue (con (c, s)),
adamc@110 780 exp = fn e => fn s => S.Continue (exp (e, s)),
adamc@110 781 decl = fn d => fn s => S.Continue (decl (d, s))} d s of
adamc@110 782 S.Continue v => v
adamc@110 783 | S.Return _ => raise Fail "CoreUtil.File.foldMap: Impossible"
adamc@110 784
adamc@179 785 val maxName = foldl (fn ((d, _) : decl, count) =>
adamc@179 786 case d of
adamc@179 787 DCon (_, n, _, _) => Int.max (n, count)
adamc@398 788 | DDatatype (_, n, _, ns) =>
adamc@398 789 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@398 790 (Int.max (n, count)) ns
adamc@179 791 | DVal (_, n, _, _, _) => Int.max (n, count)
adamc@179 792 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
adamc@247 793 | DExport _ => count
adamc@271 794 | DTable (_, n, _, _) => Int.max (n, count)
adamc@338 795 | DSequence (_, n, _) => Int.max (n, count)
adamc@271 796 | DDatabase _ => count) 0
adamc@179 797
adamc@20 798 end
adamc@20 799
adamc@20 800 end