annotate src/core_util.sml @ 275:73456bfde988

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