annotate src/core_util.sml @ 198:ab86aa858e6c

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