annotate src/elab_util.sml @ 1289:3b22c3c67f35

Reduce: Inline let-bound variables whose types involve functions
author Adam Chlipala <adam@chlipala.net>
date Sun, 22 Aug 2010 13:43:46 -0400
parents 56bd4a4f6e66
children b4480a56cab7
rev   line source
adamc@2 1 (* Copyright (c) 2008, Adam Chlipala
adamc@2 2 * All rights reserved.
adamc@2 3 *
adamc@2 4 * Redistribution and use in source and binary forms, with or without
adamc@2 5 * modification, are permitted provided that the following conditions are met:
adamc@2 6 *
adamc@2 7 * - Redistributions of source code must retain the above copyright notice,
adamc@2 8 * this list of conditions and the following disclaimer.
adamc@2 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@2 10 * this list of conditions and the following disclaimer in the documentation
adamc@2 11 * and/or other materials provided with the distribution.
adamc@2 12 * - The names of contributors may not be used to endorse or promote products
adamc@2 13 * derived from this software without specific prior written permission.
adamc@2 14 *
adamc@2 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@2 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@2 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@2 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@2 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@2 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@2 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@2 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@2 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@2 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@2 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@2 26 *)
adamc@2 27
adamc@2 28 structure ElabUtil :> ELAB_UTIL = struct
adamc@2 29
adamc@2 30 open Elab
adamc@2 31
adamc@188 32 fun classifyDatatype xncs =
adamc@198 33 case xncs of
adamc@198 34 [(_, _, NONE), (_, _, SOME _)] => Option
adamc@198 35 | [(_, _, SOME _), (_, _, NONE)] => Option
adamc@198 36 | _ =>
adamc@198 37 if List.all (fn (_, _, NONE) => true | _ => false) xncs then
adamc@198 38 Enum
adamc@198 39 else
adamc@198 40 Default
adamc@188 41
adamc@2 42 structure S = Search
adamc@2 43
adamc@2 44 structure Kind = struct
adamc@2 45
adamc@623 46 fun mapfoldB {kind, bind} =
adamc@2 47 let
adamc@623 48 fun mfk ctx k acc =
adamc@623 49 S.bindP (mfk' ctx k acc, kind ctx)
adamc@2 50
adamc@623 51 and mfk' ctx (kAll as (k, loc)) =
adamc@2 52 case k of
adamc@2 53 KType => S.return2 kAll
adamc@2 54
adamc@2 55 | KArrow (k1, k2) =>
adamc@623 56 S.bind2 (mfk ctx k1,
adamc@2 57 fn k1' =>
adamc@623 58 S.map2 (mfk ctx k2,
adamc@2 59 fn k2' =>
adamc@2 60 (KArrow (k1', k2'), loc)))
adamc@2 61
adamc@2 62 | KName => S.return2 kAll
adamc@2 63
adamc@2 64 | KRecord k =>
adamc@623 65 S.map2 (mfk ctx k,
adamc@2 66 fn k' =>
adamc@2 67 (KRecord k', loc))
adamc@2 68
adamc@82 69 | KUnit => S.return2 kAll
adamc@82 70
adamc@207 71 | KTuple ks =>
adamc@623 72 S.map2 (ListUtil.mapfold (mfk ctx) ks,
adamc@207 73 fn ks' =>
adamc@207 74 (KTuple ks', loc))
adamc@207 75
adamc@2 76 | KError => S.return2 kAll
adamc@2 77
adamc@623 78 | KUnif (_, _, ref (SOME k)) => mfk' ctx k
adamc@2 79 | KUnif _ => S.return2 kAll
adamc@623 80
adamc@623 81 | KRel _ => S.return2 kAll
adamc@623 82 | KFun (x, k) =>
adamc@623 83 S.map2 (mfk (bind (ctx, x)) k,
adamc@623 84 fn k' =>
adamc@623 85 (KFun (x, k'), loc))
adamc@2 86 in
adamc@2 87 mfk
adamc@2 88 end
adamc@2 89
adamc@623 90 fun mapfold fk =
adamc@623 91 mapfoldB {kind = fn () => fk,
adamc@623 92 bind = fn ((), _) => ()} ()
adamc@623 93
adamc@623 94 fun mapB {kind, bind} ctx k =
adamc@623 95 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@623 96 bind = bind} ctx k () of
adamc@623 97 S.Continue (k, ()) => k
adamc@623 98 | S.Return _ => raise Fail "ElabUtil.Kind.mapB: Impossible"
adamc@623 99
adamc@2 100 fun exists f k =
adamc@6 101 case mapfold (fn k => fn () =>
adamc@6 102 if f k then
adamc@6 103 S.Return ()
adamc@6 104 else
adamc@6 105 S.Continue (k, ())) k () of
adamc@6 106 S.Return _ => true
adamc@6 107 | S.Continue _ => false
adamc@6 108
adamc@6 109 end
adamc@6 110
adamc@6 111 structure Con = struct
adamc@6 112
adamc@11 113 datatype binder =
adamc@623 114 RelK of string
adamc@623 115 | RelC of string * Elab.kind
adamc@792 116 | NamedC of string * int * Elab.kind * Elab.con option
adamc@11 117
adamc@11 118 fun mapfoldB {kind = fk, con = fc, bind} =
adamc@6 119 let
adamc@623 120 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, s) => bind (ctx, RelK s)}
adamc@6 121
adamc@11 122 fun mfc ctx c acc =
adamc@11 123 S.bindP (mfc' ctx c acc, fc ctx)
adamc@6 124
adamc@11 125 and mfc' ctx (cAll as (c, loc)) =
adamc@6 126 case c of
adamc@6 127 TFun (c1, c2) =>
adamc@11 128 S.bind2 (mfc ctx c1,
adamc@6 129 fn c1' =>
adamc@11 130 S.map2 (mfc ctx c2,
adamc@6 131 fn c2' =>
adamc@6 132 (TFun (c1', c2'), loc)))
adamc@6 133 | TCFun (e, x, k, c) =>
adamc@623 134 S.bind2 (mfk ctx k,
adamc@6 135 fn k' =>
adamc@623 136 S.map2 (mfc (bind (ctx, RelC (x, k))) c,
adamc@6 137 fn c' =>
adamc@6 138 (TCFun (e, x, k', c'), loc)))
adamc@628 139 | TDisjoint (c1, c2, c3) =>
adamc@85 140 S.bind2 (mfc ctx c1,
adamc@85 141 fn c1' =>
adamc@85 142 S.bind2 (mfc ctx c2,
adamc@85 143 fn c2' =>
adamc@85 144 S.map2 (mfc ctx c3,
adamc@85 145 fn c3' =>
adamc@628 146 (TDisjoint (c1', c2', c3'), loc))))
adamc@6 147 | TRecord c =>
adamc@11 148 S.map2 (mfc ctx c,
adamc@6 149 fn c' =>
adamc@6 150 (TRecord c', loc))
adamc@6 151
adamc@6 152 | CRel _ => S.return2 cAll
adamc@6 153 | CNamed _ => S.return2 cAll
adamc@34 154 | CModProj _ => S.return2 cAll
adamc@6 155 | CApp (c1, c2) =>
adamc@11 156 S.bind2 (mfc ctx c1,
adamc@6 157 fn c1' =>
adamc@11 158 S.map2 (mfc ctx c2,
adamc@6 159 fn c2' =>
adamc@6 160 (CApp (c1', c2'), loc)))
adamc@8 161 | CAbs (x, k, c) =>
adamc@623 162 S.bind2 (mfk ctx k,
adamc@6 163 fn k' =>
adamc@623 164 S.map2 (mfc (bind (ctx, RelC (x, k))) c,
adamc@6 165 fn c' =>
adamc@8 166 (CAbs (x, k', c'), loc)))
adamc@6 167
adamc@6 168 | CName _ => S.return2 cAll
adamc@6 169
adamc@6 170 | CRecord (k, xcs) =>
adamc@623 171 S.bind2 (mfk ctx k,
adamc@6 172 fn k' =>
adamc@6 173 S.map2 (ListUtil.mapfold (fn (x, c) =>
adamc@11 174 S.bind2 (mfc ctx x,
adamc@6 175 fn x' =>
adamc@11 176 S.map2 (mfc ctx c,
adamc@6 177 fn c' =>
adamc@6 178 (x', c'))))
adamc@6 179 xcs,
adamc@6 180 fn xcs' =>
adamc@6 181 (CRecord (k', xcs'), loc)))
adamc@6 182 | CConcat (c1, c2) =>
adamc@11 183 S.bind2 (mfc ctx c1,
adamc@6 184 fn c1' =>
adamc@11 185 S.map2 (mfc ctx c2,
adamc@6 186 fn c2' =>
adamc@6 187 (CConcat (c1', c2'), loc)))
adamc@621 188 | CMap (k1, k2) =>
adamc@623 189 S.bind2 (mfk ctx k1,
adamc@67 190 fn k1' =>
adamc@623 191 S.map2 (mfk ctx k2,
adamc@67 192 fn k2' =>
adamc@621 193 (CMap (k1', k2'), loc)))
adamc@6 194
adamc@82 195 | CUnit => S.return2 cAll
adamc@82 196
adamc@207 197 | CTuple cs =>
adamc@207 198 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@207 199 fn cs' =>
adamc@207 200 (CTuple cs', loc))
adamc@207 201
adamc@207 202 | CProj (c, n) =>
adamc@207 203 S.map2 (mfc ctx c,
adamc@207 204 fn c' =>
adamc@207 205 (CProj (c', n), loc))
adamc@207 206
adamc@6 207 | CError => S.return2 cAll
adamc@76 208 | CUnif (_, _, _, ref (SOME c)) => mfc' ctx c
adamc@6 209 | CUnif _ => S.return2 cAll
adamc@623 210
adamc@623 211 | CKAbs (x, c) =>
adamc@623 212 S.map2 (mfc (bind (ctx, RelK x)) c,
adamc@623 213 fn c' =>
adamc@623 214 (CKAbs (x, c'), loc))
adamc@623 215 | CKApp (c, k) =>
adamc@623 216 S.bind2 (mfc ctx c,
adamc@623 217 fn c' =>
adamc@623 218 S.map2 (mfk ctx k,
adamc@623 219 fn k' =>
adamc@623 220 (CKApp (c', k'), loc)))
adamc@623 221 | TKFun (x, c) =>
adamc@623 222 S.map2 (mfc (bind (ctx, RelK x)) c,
adamc@623 223 fn c' =>
adamc@623 224 (TKFun (x, c'), loc))
adamc@6 225 in
adamc@6 226 mfc
adamc@6 227 end
adamc@6 228
adamc@11 229 fun mapfold {kind = fk, con = fc} =
adamc@623 230 mapfoldB {kind = fn () => fk,
adamc@11 231 con = fn () => fc,
adamc@11 232 bind = fn ((), _) => ()} ()
adamc@11 233
adamc@11 234 fun mapB {kind, con, bind} ctx c =
adamc@623 235 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@11 236 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@11 237 bind = bind} ctx c () of
adamc@11 238 S.Continue (c, ()) => c
adamc@34 239 | S.Return _ => raise Fail "ElabUtil.Con.mapB: Impossible"
adamc@34 240
adamc@34 241 fun map {kind, con} s =
adamc@34 242 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@34 243 con = fn c => fn () => S.Continue (con c, ())} s () of
adamc@34 244 S.Return () => raise Fail "ElabUtil.Con.map: Impossible"
adamc@34 245 | S.Continue (s, ()) => s
adamc@11 246
adamc@711 247 fun existsB {kind, con, bind} ctx c =
adamc@711 248 case mapfoldB {kind = fn ctx => fn k => fn () =>
adamc@711 249 if kind (ctx, k) then
adamc@711 250 S.Return ()
adamc@711 251 else
adamc@711 252 S.Continue (k, ()),
adamc@711 253 con = fn ctx => fn c => fn () =>
adamc@711 254 if con (ctx, c) then
adamc@711 255 S.Return ()
adamc@711 256 else
adamc@711 257 S.Continue (c, ()),
adamc@711 258 bind = bind} ctx c () of
adamc@711 259 S.Return _ => true
adamc@711 260 | S.Continue _ => false
adamc@711 261
adamc@711 262 fun exists {kind, con} c =
adamc@6 263 case mapfold {kind = fn k => fn () =>
adamc@6 264 if kind k then
adamc@6 265 S.Return ()
adamc@6 266 else
adamc@6 267 S.Continue (k, ()),
adamc@6 268 con = fn c => fn () =>
adamc@6 269 if con c then
adamc@6 270 S.Return ()
adamc@6 271 else
adamc@711 272 S.Continue (c, ())} c () of
adamc@2 273 S.Return _ => true
adamc@2 274 | S.Continue _ => false
adamc@2 275
adamc@448 276 fun foldB {kind, con, bind} ctx st c =
adamc@623 277 case mapfoldB {kind = fn ctx => fn k => fn st => S.Continue (k, kind (ctx, k, st)),
adamc@448 278 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
adamc@448 279 bind = bind} ctx c st of
adamc@448 280 S.Continue (_, st) => st
adamc@448 281 | S.Return _ => raise Fail "ElabUtil.Con.foldB: Impossible"
adamc@448 282
adamc@839 283 fun fold {kind, con} st c =
adamc@839 284 case mapfoldB {kind = fn () => fn k => fn st => S.Continue (k, kind (k, st)),
adamc@839 285 con = fn () => fn c => fn st => S.Continue (c, con (c, st)),
adamc@839 286 bind = fn ((), _) => ()} () c st of
adamc@839 287 S.Continue (_, st) => st
adamc@839 288 | S.Return _ => raise Fail "ElabUtil.Con.fold: Impossible"
adamc@839 289
adamc@2 290 end
adamc@2 291
adamc@10 292 structure Exp = struct
adamc@10 293
adamc@11 294 datatype binder =
adamc@623 295 RelK of string
adamc@623 296 | RelC of string * Elab.kind
adamc@792 297 | NamedC of string * int * Elab.kind * Elab.con option
adamc@11 298 | RelE of string * Elab.con
adamc@11 299 | NamedE of string * Elab.con
adamc@11 300
adamc@11 301 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@10 302 let
adamc@623 303 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@10 304
adamc@11 305 fun bind' (ctx, b) =
adamc@11 306 let
adamc@11 307 val b' = case b of
adamc@623 308 Con.RelK x => RelK x
adamc@623 309 | Con.RelC x => RelC x
adamc@623 310 | Con.NamedC x => NamedC x
adamc@11 311 in
adamc@11 312 bind (ctx, b')
adamc@11 313 end
adamc@11 314 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@10 315
adamc@825 316 fun doVars ((p, _), ctx) =
adamc@825 317 case p of
adamc@825 318 PWild => ctx
adamc@825 319 | PVar xt => bind (ctx, RelE xt)
adamc@825 320 | PPrim _ => ctx
adamc@825 321 | PCon (_, _, _, NONE) => ctx
adamc@825 322 | PCon (_, _, _, SOME p) => doVars (p, ctx)
adamc@825 323 | PRecord xpcs =>
adamc@825 324 foldl (fn ((_, p, _), ctx) => doVars (p, ctx))
adamc@825 325 ctx xpcs
adamc@825 326
adamc@11 327 fun mfe ctx e acc =
adamc@11 328 S.bindP (mfe' ctx e acc, fe ctx)
adamc@11 329
adamc@11 330 and mfe' ctx (eAll as (e, loc)) =
adamc@10 331 case e of
adamc@14 332 EPrim _ => S.return2 eAll
adamc@14 333 | ERel _ => S.return2 eAll
adamc@10 334 | ENamed _ => S.return2 eAll
adamc@34 335 | EModProj _ => S.return2 eAll
adamc@10 336 | EApp (e1, e2) =>
adamc@11 337 S.bind2 (mfe ctx e1,
adamc@10 338 fn e1' =>
adamc@11 339 S.map2 (mfe ctx e2,
adamc@10 340 fn e2' =>
adamc@10 341 (EApp (e1', e2'), loc)))
adamc@26 342 | EAbs (x, dom, ran, e) =>
adamc@26 343 S.bind2 (mfc ctx dom,
adamc@26 344 fn dom' =>
adamc@26 345 S.bind2 (mfc ctx ran,
adamc@26 346 fn ran' =>
adamc@26 347 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 348 fn e' =>
adamc@26 349 (EAbs (x, dom', ran', e'), loc))))
adamc@26 350
adamc@10 351 | ECApp (e, c) =>
adamc@11 352 S.bind2 (mfe ctx e,
adamc@10 353 fn e' =>
adamc@11 354 S.map2 (mfc ctx c,
adamc@10 355 fn c' =>
adamc@10 356 (ECApp (e', c'), loc)))
adamc@10 357 | ECAbs (expl, x, k, e) =>
adamc@623 358 S.bind2 (mfk ctx k,
adamc@10 359 fn k' =>
adamc@11 360 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@10 361 fn e' =>
adamc@10 362 (ECAbs (expl, x, k', e'), loc)))
adamc@10 363
adamc@12 364 | ERecord xes =>
adamc@29 365 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@12 366 S.bind2 (mfc ctx x,
adamc@12 367 fn x' =>
adamc@29 368 S.bind2 (mfe ctx e,
adamc@12 369 fn e' =>
adamc@29 370 S.map2 (mfc ctx t,
adamc@29 371 fn t' =>
adamc@29 372 (x', e', t')))))
adamc@12 373 xes,
adamc@12 374 fn xes' =>
adamc@12 375 (ERecord xes', loc))
adamc@12 376 | EField (e, c, {field, rest}) =>
adamc@12 377 S.bind2 (mfe ctx e,
adamc@12 378 fn e' =>
adamc@12 379 S.bind2 (mfc ctx c,
adamc@12 380 fn c' =>
adamc@12 381 S.bind2 (mfc ctx field,
adamc@12 382 fn field' =>
adamc@12 383 S.map2 (mfc ctx rest,
adamc@12 384 fn rest' =>
adamc@12 385 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@445 386 | EConcat (e1, c1, e2, c2) =>
adamc@339 387 S.bind2 (mfe ctx e1,
adamc@339 388 fn e1' =>
adamc@445 389 S.bind2 (mfc ctx c1,
adamc@445 390 fn c1' =>
adamc@339 391 S.bind2 (mfe ctx e2,
adamc@339 392 fn e2' =>
adamc@445 393 S.map2 (mfc ctx c2,
adamc@445 394 fn c2' =>
adamc@445 395 (EConcat (e1', c1', e2', c2'),
adamc@445 396 loc)))))
adamc@149 397 | ECut (e, c, {field, rest}) =>
adamc@149 398 S.bind2 (mfe ctx e,
adamc@149 399 fn e' =>
adamc@149 400 S.bind2 (mfc ctx c,
adamc@149 401 fn c' =>
adamc@149 402 S.bind2 (mfc ctx field,
adamc@149 403 fn field' =>
adamc@149 404 S.map2 (mfc ctx rest,
adamc@149 405 fn rest' =>
adamc@149 406 (ECut (e', c', {field = field', rest = rest'}), loc)))))
adamc@12 407
adamc@493 408 | ECutMulti (e, c, {rest}) =>
adamc@493 409 S.bind2 (mfe ctx e,
adamc@493 410 fn e' =>
adamc@493 411 S.bind2 (mfc ctx c,
adamc@493 412 fn c' =>
adamc@493 413 S.map2 (mfc ctx rest,
adamc@493 414 fn rest' =>
adamc@493 415 (ECutMulti (e', c', {rest = rest'}), loc))))
adamc@493 416
adamc@182 417 | ECase (e, pes, {disc, result}) =>
adamc@171 418 S.bind2 (mfe ctx e,
adamc@171 419 fn e' =>
adamc@171 420 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@448 421 let
adamc@448 422 fun pb ((p, _), ctx) =
adamc@448 423 case p of
adamc@448 424 PWild => ctx
adamc@448 425 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@448 426 | PPrim _ => ctx
adamc@448 427 | PCon (_, _, _, NONE) => ctx
adamc@448 428 | PCon (_, _, _, SOME p) => pb (p, ctx)
adamc@448 429 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@448 430 pb (p, ctx)) ctx xps
adamc@448 431 in
adamc@1272 432 S.bind2 (mfp ctx p,
adamc@1272 433 fn p' =>
adamc@1272 434 S.map2 (mfe (pb (p', ctx)) e,
adamc@1272 435 fn e' => (p', e')))
adamc@448 436 end) pes,
adamc@171 437 fn pes' =>
adamc@182 438 S.bind2 (mfc ctx disc,
adamc@182 439 fn disc' =>
adamc@182 440 S.map2 (mfc ctx result,
adamc@182 441 fn result' =>
adamc@182 442 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@171 443
adamc@10 444 | EError => S.return2 eAll
adamc@228 445 | EUnif (ref (SOME e)) => mfe ctx e
adamc@228 446 | EUnif _ => S.return2 eAll
adamc@447 447
adamc@825 448 | ELet (des, e, t) =>
adamc@447 449 let
adamc@826 450 val (des, ctx') = foldl (fn (ed, (des, ctx)) =>
adamc@826 451 let
adamc@826 452 val ctx' =
adamc@826 453 case #1 ed of
adamc@826 454 EDVal (p, _, _) => doVars (p, ctx)
adamc@826 455 | EDValRec vis =>
adamc@826 456 foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t)))
adamc@826 457 ctx vis
adamc@826 458 in
adamc@826 459 (S.bind2 (des,
adamc@826 460 fn des' =>
adamc@826 461 S.map2 (mfed ctx ed,
adamc@826 462 fn ed' => ed' :: des')),
adamc@826 463 ctx')
adamc@826 464 end)
adamc@447 465 (S.return2 [], ctx) des
adamc@447 466 in
adamc@447 467 S.bind2 (des,
adamc@447 468 fn des' =>
adamc@826 469 S.bind2 (mfe ctx' e,
adamc@447 470 fn e' =>
adamc@825 471 S.map2 (mfc ctx t,
adamc@825 472 fn t' =>
adamc@826 473 (ELet (rev des', e', t'), loc))))
adamc@447 474 end
adamc@447 475
adamc@623 476 | EKAbs (x, e) =>
adamc@623 477 S.map2 (mfe (bind (ctx, RelK x)) e,
adamc@623 478 fn e' =>
adamc@623 479 (EKAbs (x, e'), loc))
adamc@623 480 | EKApp (e, k) =>
adamc@623 481 S.bind2 (mfe ctx e,
adamc@623 482 fn e' =>
adamc@623 483 S.map2 (mfk ctx k,
adamc@623 484 fn k' =>
adamc@623 485 (EKApp (e', k'), loc)))
adamc@623 486
adamc@1272 487 and mfp ctx (pAll as (p, loc)) =
adamc@1272 488 case p of
adamc@1272 489 PWild => S.return2 pAll
adamc@1272 490 | PVar (x, t) =>
adamc@1272 491 S.map2 (mfc ctx t,
adamc@1272 492 fn t' =>
adamc@1272 493 (PVar (x, t'), loc))
adamc@1272 494 | PPrim _ => S.return2 pAll
adamc@1272 495 | PCon (dk, pc, args, po) =>
adamc@1272 496 S.bind2 (ListUtil.mapfold (mfc ctx) args,
adamc@1272 497 fn args' =>
adamc@1272 498 S.map2 ((case po of
adamc@1272 499 NONE => S.return2 NONE
adamc@1272 500 | SOME p => S.map2 (mfp ctx p, SOME)),
adamc@1272 501 fn po' =>
adamc@1272 502 (PCon (dk, pc, args', po'), loc)))
adamc@1272 503 | PRecord xps =>
adamc@1272 504 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
adamc@1272 505 S.bind2 (mfp ctx p,
adamc@1272 506 fn p' =>
adamc@1272 507 S.map2 (mfc ctx c,
adamc@1272 508 fn c' =>
adamc@1272 509 (x, p', c')))) xps,
adamc@1272 510 fn xps' =>
adamc@1272 511 (PRecord xps', loc))
adamc@1272 512
adamc@447 513 and mfed ctx (dAll as (d, loc)) =
adamc@447 514 case d of
adamc@825 515 EDVal (p, t, e) =>
adamc@825 516 S.bind2 (mfc ctx t,
adamc@825 517 fn t' =>
adamc@826 518 S.map2 (mfe ctx e,
adamc@825 519 fn e' =>
adamc@825 520 (EDVal (p, t', e'), loc)))
adamc@447 521 | EDValRec vis =>
adamc@447 522 let
adamc@453 523 val ctx = foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t))) ctx vis
adamc@447 524 in
adamc@447 525 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@447 526 fn vis' =>
adamc@447 527 (EDValRec vis', loc))
adamc@447 528 end
adamc@447 529
adamc@447 530 and mfvi ctx (x, c, e) =
adamc@447 531 S.bind2 (mfc ctx c,
adamc@447 532 fn c' =>
adamc@447 533 S.map2 (mfe ctx e,
adamc@447 534 fn e' =>
adamc@447 535 (x, c', e')))
adamc@10 536 in
adamc@10 537 mfe
adamc@10 538 end
adamc@10 539
adamc@11 540 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@623 541 mapfoldB {kind = fn () => fk,
adamc@11 542 con = fn () => fc,
adamc@11 543 exp = fn () => fe,
adamc@11 544 bind = fn ((), _) => ()} ()
adamc@11 545
adamc@10 546 fun exists {kind, con, exp} k =
adamc@10 547 case mapfold {kind = fn k => fn () =>
adamc@10 548 if kind k then
adamc@10 549 S.Return ()
adamc@10 550 else
adamc@10 551 S.Continue (k, ()),
adamc@10 552 con = fn c => fn () =>
adamc@10 553 if con c then
adamc@10 554 S.Return ()
adamc@10 555 else
adamc@10 556 S.Continue (c, ()),
adamc@10 557 exp = fn e => fn () =>
adamc@10 558 if exp e then
adamc@10 559 S.Return ()
adamc@10 560 else
adamc@10 561 S.Continue (e, ())} k () of
adamc@10 562 S.Return _ => true
adamc@10 563 | S.Continue _ => false
adamc@10 564
adamc@211 565 fun mapB {kind, con, exp, bind} ctx e =
adamc@623 566 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@211 567 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@211 568 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@211 569 bind = bind} ctx e () of
adamc@211 570 S.Continue (e, ()) => e
adamc@211 571 | S.Return _ => raise Fail "ElabUtil.Exp.mapB: Impossible"
adamc@211 572
adamc@448 573 fun foldB {kind, con, exp, bind} ctx st e =
adamc@623 574 case mapfoldB {kind = fn ctx => fn k => fn st => S.Continue (k, kind (ctx, k, st)),
adamc@448 575 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
adamc@448 576 exp = fn ctx => fn e => fn st => S.Continue (e, exp (ctx, e, st)),
adamc@448 577 bind = bind} ctx e st of
adamc@448 578 S.Continue (_, st) => st
adamc@448 579 | S.Return _ => raise Fail "ElabUtil.Exp.foldB: Impossible"
adamc@448 580
adamc@10 581 end
adamc@10 582
adamc@34 583 structure Sgn = struct
adamc@34 584
adamc@34 585 datatype binder =
adamc@623 586 RelK of string
adamc@623 587 | RelC of string * Elab.kind
adamc@792 588 | NamedC of string * int * Elab.kind * Elab.con option
adamc@792 589 | Str of string * int * Elab.sgn
adamc@792 590 | Sgn of string * int * Elab.sgn
adamc@34 591
adamc@34 592 fun mapfoldB {kind, con, sgn_item, sgn, bind} =
adamc@34 593 let
adamc@34 594 fun bind' (ctx, b) =
adamc@34 595 let
adamc@34 596 val b' = case b of
adamc@623 597 Con.RelK x => RelK x
adamc@623 598 | Con.RelC x => RelC x
adamc@623 599 | Con.NamedC x => NamedC x
adamc@34 600 in
adamc@34 601 bind (ctx, b')
adamc@34 602 end
adamc@34 603 val con = Con.mapfoldB {kind = kind, con = con, bind = bind'}
adamc@34 604
adamc@623 605 val kind = Kind.mapfoldB {kind = kind, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@34 606
adamc@34 607 fun sgi ctx si acc =
adamc@34 608 S.bindP (sgi' ctx si acc, sgn_item ctx)
adamc@34 609
adamc@156 610 and sgi' ctx (siAll as (si, loc)) =
adamc@34 611 case si of
adamc@34 612 SgiConAbs (x, n, k) =>
adamc@623 613 S.map2 (kind ctx k,
adamc@34 614 fn k' =>
adamc@34 615 (SgiConAbs (x, n, k'), loc))
adamc@34 616 | SgiCon (x, n, k, c) =>
adamc@623 617 S.bind2 (kind ctx k,
adamc@34 618 fn k' =>
adamc@34 619 S.map2 (con ctx c,
adamc@34 620 fn c' =>
adamc@34 621 (SgiCon (x, n, k', c'), loc)))
adamc@805 622 | SgiDatatype dts =>
adamc@805 623 S.map2 (ListUtil.mapfold (fn (x, n, xs, xncs) =>
adamc@805 624 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@805 625 case c of
adamc@805 626 NONE => S.return2 (x, n, c)
adamc@805 627 | SOME c =>
adamc@805 628 S.map2 (con ctx c,
adamc@805 629 fn c' => (x, n, SOME c'))) xncs,
adamc@805 630 fn xncs' => (x, n, xs, xncs'))) dts,
adamc@805 631 fn dts' =>
adamc@805 632 (SgiDatatype dts', loc))
adamc@191 633 | SgiDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
adamc@162 634 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@162 635 case c of
adamc@162 636 NONE => S.return2 (x, n, c)
adamc@162 637 | SOME c =>
adamc@162 638 S.map2 (con ctx c,
adamc@162 639 fn c' => (x, n, SOME c'))) xncs,
adamc@162 640 fn xncs' =>
adamc@191 641 (SgiDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
adamc@34 642 | SgiVal (x, n, c) =>
adamc@34 643 S.map2 (con ctx c,
adamc@34 644 fn c' =>
adamc@34 645 (SgiVal (x, n, c'), loc))
adamc@34 646 | SgiStr (x, n, s) =>
adamc@34 647 S.map2 (sg ctx s,
adamc@34 648 fn s' =>
adamc@34 649 (SgiStr (x, n, s'), loc))
adamc@59 650 | SgiSgn (x, n, s) =>
adamc@59 651 S.map2 (sg ctx s,
adamc@59 652 fn s' =>
adamc@59 653 (SgiSgn (x, n, s'), loc))
adamc@88 654 | SgiConstraint (c1, c2) =>
adamc@88 655 S.bind2 (con ctx c1,
adamc@88 656 fn c1' =>
adamc@88 657 S.map2 (con ctx c2,
adamc@88 658 fn c2' =>
adamc@88 659 (SgiConstraint (c1', c2'), loc)))
adamc@563 660 | SgiClassAbs (x, n, k) =>
adamc@623 661 S.map2 (kind ctx k,
adamc@563 662 fn k' =>
adamc@563 663 (SgiClassAbs (x, n, k'), loc))
adamc@563 664 | SgiClass (x, n, k, c) =>
adamc@623 665 S.bind2 (kind ctx k,
adamc@563 666 fn k' =>
adamc@563 667 S.map2 (con ctx c,
adamc@563 668 fn c' =>
adamc@563 669 (SgiClass (x, n, k', c'), loc)))
adamc@34 670
adamc@34 671 and sg ctx s acc =
adamc@34 672 S.bindP (sg' ctx s acc, sgn ctx)
adamc@34 673
adamc@34 674 and sg' ctx (sAll as (s, loc)) =
adamc@34 675 case s of
adamc@34 676 SgnConst sgis =>
adamc@34 677 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
adamc@34 678 (case #1 si of
adamc@329 679 SgiConAbs (x, n, k) =>
adamc@792 680 bind (ctx, NamedC (x, n, k, NONE))
adamc@792 681 | SgiCon (x, n, k, c) =>
adamc@792 682 bind (ctx, NamedC (x, n, k, SOME c))
adamc@805 683 | SgiDatatype dts =>
adamc@805 684 foldl (fn ((x, n, ks, _), ctx) =>
adamc@805 685 let
adamc@805 686 val k' = (KType, loc)
adamc@805 687 val k = foldl (fn (_, k) => (KArrow (k', k), loc))
adamc@805 688 k' ks
adamc@805 689 in
adamc@805 690 bind (ctx, NamedC (x, n, k, NONE))
adamc@805 691 end) ctx dts
adamc@792 692 | SgiDatatypeImp (x, n, m1, ms, s, _, _) =>
adamc@792 693 bind (ctx, NamedC (x, n, (KType, loc),
adamc@792 694 SOME (CModProj (m1, ms, s), loc)))
adamc@34 695 | SgiVal _ => ctx
adamc@792 696 | SgiStr (x, n, sgn) =>
adamc@792 697 bind (ctx, Str (x, n, sgn))
adamc@792 698 | SgiSgn (x, n, sgn) =>
adamc@792 699 bind (ctx, Sgn (x, n, sgn))
adamc@203 700 | SgiConstraint _ => ctx
adamc@563 701 | SgiClassAbs (x, n, k) =>
adamc@792 702 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), NONE))
adamc@792 703 | SgiClass (x, n, k, c) =>
adamc@792 704 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), SOME c)),
adamc@34 705 sgi ctx si)) ctx sgis,
adamc@34 706 fn sgis' =>
adamc@34 707 (SgnConst sgis', loc))
adamc@34 708
adamc@34 709 | SgnVar _ => S.return2 sAll
adamc@41 710 | SgnFun (m, n, s1, s2) =>
adamc@41 711 S.bind2 (sg ctx s1,
adamc@41 712 fn s1' =>
adamc@792 713 S.map2 (sg (bind (ctx, Str (m, n, s1'))) s2,
adamc@41 714 fn s2' =>
adamc@41 715 (SgnFun (m, n, s1', s2'), loc)))
adamc@59 716 | SgnProj _ => S.return2 sAll
adamc@42 717 | SgnWhere (sgn, x, c) =>
adamc@42 718 S.bind2 (sg ctx sgn,
adamc@42 719 fn sgn' =>
adamc@42 720 S.map2 (con ctx c,
adamc@42 721 fn c' =>
adamc@42 722 (SgnWhere (sgn', x, c'), loc)))
adamc@34 723 | SgnError => S.return2 sAll
adamc@34 724 in
adamc@34 725 sg
adamc@34 726 end
adamc@34 727
adamc@34 728 fun mapfold {kind, con, sgn_item, sgn} =
adamc@623 729 mapfoldB {kind = fn () => kind,
adamc@34 730 con = fn () => con,
adamc@34 731 sgn_item = fn () => sgn_item,
adamc@34 732 sgn = fn () => sgn,
adamc@34 733 bind = fn ((), _) => ()} ()
adamc@34 734
adamc@792 735 fun mapB {kind, con, sgn_item, sgn, bind} ctx s =
adamc@792 736 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@792 737 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@792 738 sgn_item = fn ctx => fn sgi => fn () => S.Continue (sgn_item ctx sgi, ()),
adamc@792 739 sgn = fn ctx => fn s => fn () => S.Continue (sgn ctx s, ()),
adamc@792 740 bind = bind} ctx s () of
adamc@792 741 S.Continue (s, ()) => s
adamc@792 742 | S.Return _ => raise Fail "ElabUtil.Sgn.mapB: Impossible"
adamc@792 743
adamc@34 744 fun map {kind, con, sgn_item, sgn} s =
adamc@34 745 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@34 746 con = fn c => fn () => S.Continue (con c, ()),
adamc@34 747 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
adamc@34 748 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
adamc@34 749 S.Return () => raise Fail "Elab_util.Sgn.map"
adamc@34 750 | S.Continue (s, ()) => s
adamc@34 751
adamc@2 752 end
adamc@34 753
adamc@76 754 structure Decl = struct
adamc@76 755
adamc@76 756 datatype binder =
adamc@623 757 RelK of string
adamc@623 758 | RelC of string * Elab.kind
adamc@792 759 | NamedC of string * int * Elab.kind * Elab.con option
adamc@76 760 | RelE of string * Elab.con
adamc@76 761 | NamedE of string * Elab.con
adamc@76 762 | Str of string * Elab.sgn
adamc@76 763 | Sgn of string * Elab.sgn
adamc@76 764
adamc@76 765 fun mapfoldB {kind = fk, con = fc, exp = fe, sgn_item = fsgi, sgn = fsg, str = fst, decl = fd, bind} =
adamc@76 766 let
adamc@623 767 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@76 768
adamc@76 769 fun bind' (ctx, b) =
adamc@76 770 let
adamc@76 771 val b' = case b of
adamc@623 772 Con.RelK x => RelK x
adamc@623 773 | Con.RelC x => RelC x
adamc@623 774 | Con.NamedC x => NamedC x
adamc@76 775 in
adamc@76 776 bind (ctx, b')
adamc@76 777 end
adamc@76 778 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@76 779
adamc@76 780 fun bind' (ctx, b) =
adamc@76 781 let
adamc@76 782 val b' = case b of
adamc@623 783 Exp.RelK x => RelK x
adamc@623 784 | Exp.RelC x => RelC x
adamc@76 785 | Exp.NamedC x => NamedC x
adamc@76 786 | Exp.RelE x => RelE x
adamc@76 787 | Exp.NamedE x => NamedE x
adamc@76 788 in
adamc@76 789 bind (ctx, b')
adamc@76 790 end
adamc@76 791 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind'}
adamc@76 792
adamc@76 793 fun bind' (ctx, b) =
adamc@76 794 let
adamc@76 795 val b' = case b of
adamc@623 796 Sgn.RelK x => RelK x
adamc@623 797 | Sgn.RelC x => RelC x
adamc@76 798 | Sgn.NamedC x => NamedC x
adamc@792 799 | Sgn.Sgn (x, _, y) => Sgn (x, y)
adamc@792 800 | Sgn.Str (x, _, y) => Str (x, y)
adamc@76 801 in
adamc@76 802 bind (ctx, b')
adamc@76 803 end
adamc@76 804 val mfsg = Sgn.mapfoldB {kind = fk, con = fc, sgn_item = fsgi, sgn = fsg, bind = bind'}
adamc@76 805
adamc@76 806 fun mfst ctx str acc =
adamc@76 807 S.bindP (mfst' ctx str acc, fst ctx)
adamc@76 808
adamc@76 809 and mfst' ctx (strAll as (str, loc)) =
adamc@76 810 case str of
adamc@76 811 StrConst ds =>
adamc@76 812 S.map2 (ListUtil.mapfoldB (fn (ctx, d) =>
adamc@76 813 (case #1 d of
adamc@792 814 DCon (x, n, k, c) =>
adamc@792 815 bind (ctx, NamedC (x, n, k, SOME c))
adamc@805 816 | DDatatype dts =>
adamc@156 817 let
adamc@805 818 fun doOne ((x, n, xs, xncs), ctx) =
adamc@805 819 let
adamc@805 820 val ctx = bind (ctx, NamedC (x, n, (KType, loc), NONE))
adamc@805 821 in
adamc@805 822 foldl (fn ((x, _, co), ctx) =>
adamc@805 823 let
adamc@805 824 val t =
adamc@805 825 case co of
adamc@805 826 NONE => CNamed n
adamc@805 827 | SOME t => TFun (t, (CNamed n, loc))
adamc@805 828
adamc@805 829 val k = (KType, loc)
adamc@805 830 val t = (t, loc)
adamc@805 831 val t = foldr (fn (x, t) =>
adamc@805 832 (TCFun (Explicit,
adamc@805 833 x,
adamc@805 834 k,
adamc@805 835 t), loc))
adamc@805 836 t xs
adamc@805 837 in
adamc@805 838 bind (ctx, NamedE (x, t))
adamc@805 839 end)
adamc@805 840 ctx xncs
adamc@805 841 end
adamc@156 842 in
adamc@805 843 foldl doOne ctx dts
adamc@156 844 end
adamc@191 845 | DDatatypeImp (x, n, m, ms, x', _, _) =>
adamc@792 846 bind (ctx, NamedC (x, n, (KType, loc),
adamc@792 847 SOME (CModProj (m, ms, x'), loc)))
adamc@76 848 | DVal (x, _, c, _) =>
adamc@76 849 bind (ctx, NamedE (x, c))
adamc@123 850 | DValRec vis =>
adamc@123 851 foldl (fn ((x, _, c, _), ctx) => bind (ctx, NamedE (x, c))) ctx vis
adamc@76 852 | DSgn (x, _, sgn) =>
adamc@76 853 bind (ctx, Sgn (x, sgn))
adamc@76 854 | DStr (x, _, sgn, _) =>
adamc@76 855 bind (ctx, Str (x, sgn))
adamc@76 856 | DFfiStr (x, _, sgn) =>
adamc@88 857 bind (ctx, Str (x, sgn))
adamc@100 858 | DConstraint _ => ctx
adamc@203 859 | DExport _ => ctx
adamc@707 860 | DTable (tn, x, n, c, _, pc, _, cc) =>
adamc@705 861 let
adamc@705 862 val ct = (CModProj (n, [], "sql_table"), loc)
adamc@705 863 val ct = (CApp (ct, c), loc)
adamc@707 864 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
adamc@705 865 in
adamc@705 866 bind (ctx, NamedE (x, ct))
adamc@705 867 end
adamc@338 868 | DSequence (tn, x, n) =>
adamc@338 869 bind (ctx, NamedE (x, (CModProj (n, [], "sql_sequence"), loc)))
adamc@754 870 | DView (tn, x, n, _, c) =>
adamc@754 871 let
adamc@754 872 val ct = (CModProj (n, [], "sql_view"), loc)
adamc@754 873 val ct = (CApp (ct, c), loc)
adamc@754 874 in
adamc@754 875 bind (ctx, NamedE (x, ct))
adamc@754 876 end
adamc@792 877 | DClass (x, n, k, c) =>
adamc@792 878 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), SOME c))
adamc@459 879 | DDatabase _ => ctx
adamc@459 880 | DCookie (tn, x, n, c) =>
adamc@459 881 bind (ctx, NamedE (x, (CApp ((CModProj (n, [], "cookie"), loc),
adamc@718 882 c), loc)))
adamc@720 883 | DStyle (tn, x, n) =>
adamc@1073 884 bind (ctx, NamedE (x, (CModProj (n, [], "css_class"), loc)))
adamc@1199 885 | DTask _ => ctx
adamc@1199 886 | DPolicy _ => ctx,
adamc@76 887 mfd ctx d)) ctx ds,
adamc@76 888 fn ds' => (StrConst ds', loc))
adamc@76 889 | StrVar _ => S.return2 strAll
adamc@76 890 | StrProj (str, x) =>
adamc@76 891 S.map2 (mfst ctx str,
adamc@76 892 fn str' =>
adamc@76 893 (StrProj (str', x), loc))
adamc@76 894 | StrFun (x, n, sgn1, sgn2, str) =>
adamc@76 895 S.bind2 (mfsg ctx sgn1,
adamc@76 896 fn sgn1' =>
adamc@76 897 S.bind2 (mfsg ctx sgn2,
adamc@76 898 fn sgn2' =>
adamc@76 899 S.map2 (mfst ctx str,
adamc@76 900 fn str' =>
adamc@76 901 (StrFun (x, n, sgn1', sgn2', str'), loc))))
adamc@76 902 | StrApp (str1, str2) =>
adamc@76 903 S.bind2 (mfst ctx str1,
adamc@76 904 fn str1' =>
adamc@76 905 S.map2 (mfst ctx str2,
adamc@76 906 fn str2' =>
adamc@76 907 (StrApp (str1', str2'), loc)))
adamc@76 908 | StrError => S.return2 strAll
adamc@76 909
adamc@76 910 and mfd ctx d acc =
adamc@76 911 S.bindP (mfd' ctx d acc, fd ctx)
adamc@76 912
adamc@76 913 and mfd' ctx (dAll as (d, loc)) =
adamc@76 914 case d of
adamc@76 915 DCon (x, n, k, c) =>
adamc@623 916 S.bind2 (mfk ctx k,
adamc@76 917 fn k' =>
adamc@76 918 S.map2 (mfc ctx c,
adamc@76 919 fn c' =>
adamc@76 920 (DCon (x, n, k', c'), loc)))
adamc@805 921 | DDatatype dts =>
adamc@805 922 S.map2 (ListUtil.mapfold (fn (x, n, xs, xncs) =>
adamc@805 923 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@805 924 case c of
adamc@805 925 NONE => S.return2 (x, n, c)
adamc@805 926 | SOME c =>
adamc@805 927 S.map2 (mfc ctx c,
adamc@805 928 fn c' => (x, n, SOME c'))) xncs,
adamc@805 929 fn xncs' =>
adamc@805 930 (x, n, xs, xncs'))) dts,
adamc@805 931 fn dts' =>
adamc@805 932 (DDatatype dts', loc))
adamc@191 933 | DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
adamc@162 934 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@162 935 case c of
adamc@162 936 NONE => S.return2 (x, n, c)
adamc@162 937 | SOME c =>
adamc@162 938 S.map2 (mfc ctx c,
adamc@162 939 fn c' => (x, n, SOME c'))) xncs,
adamc@162 940 fn xncs' =>
adamc@191 941 (DDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
adamc@123 942 | DVal vi =>
adamc@123 943 S.map2 (mfvi ctx vi,
adamc@123 944 fn vi' =>
adamc@123 945 (DVal vi', loc))
adamc@123 946 | DValRec vis =>
adamc@123 947 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@123 948 fn vis' =>
adamc@123 949 (DValRec vis', loc))
adamc@76 950 | DSgn (x, n, sgn) =>
adamc@76 951 S.map2 (mfsg ctx sgn,
adamc@76 952 fn sgn' =>
adamc@76 953 (DSgn (x, n, sgn'), loc))
adamc@76 954 | DStr (x, n, sgn, str) =>
adamc@76 955 S.bind2 (mfsg ctx sgn,
adamc@76 956 fn sgn' =>
adamc@76 957 S.map2 (mfst ctx str,
adamc@76 958 fn str' =>
adamc@76 959 (DStr (x, n, sgn', str'), loc)))
adamc@76 960 | DFfiStr (x, n, sgn) =>
adamc@76 961 S.map2 (mfsg ctx sgn,
adamc@76 962 fn sgn' =>
adamc@76 963 (DFfiStr (x, n, sgn'), loc))
adamc@88 964 | DConstraint (c1, c2) =>
adamc@88 965 S.bind2 (mfc ctx c1,
adamc@88 966 fn c1' =>
adamc@88 967 S.map2 (mfc ctx c2,
adamc@88 968 fn c2' =>
adamc@88 969 (DConstraint (c1', c2'), loc)))
adamc@109 970 | DExport (en, sgn, str) =>
adamc@109 971 S.bind2 (mfsg ctx sgn,
adamc@109 972 fn sgn' =>
adamc@109 973 S.map2 (mfst ctx str,
adamc@109 974 fn str' =>
adamc@109 975 (DExport (en, sgn', str'), loc)))
adamc@123 976
adamc@707 977 | DTable (tn, x, n, c, pe, pc, ce, cc) =>
adamc@704 978 S.bind2 (mfc ctx c,
adamc@203 979 fn c' =>
adamc@707 980 S.bind2 (mfe ctx pe,
adamc@707 981 fn pe' =>
adamc@707 982 S.bind2 (mfc ctx pc,
adamc@707 983 fn pc' =>
adamc@707 984 S.bind2 (mfe ctx ce,
adamc@707 985 fn ce' =>
adamc@707 986 S.map2 (mfc ctx cc,
adamc@707 987 fn cc' =>
adamc@707 988 (DTable (tn, x, n, c', pe', pc', ce', cc'), loc))))))
adamc@338 989 | DSequence _ => S.return2 dAll
adamc@754 990 | DView (tn, x, n, e, c) =>
adamc@754 991 S.bind2 (mfe ctx e,
adamc@754 992 fn e' =>
adamc@754 993 S.map2 (mfc ctx c,
adamc@754 994 fn c' =>
adamc@754 995 (DView (tn, x, n, e', c'), loc)))
adamc@203 996
adamc@563 997 | DClass (x, n, k, c) =>
adamc@623 998 S.bind2 (mfk ctx k,
adamc@563 999 fn k' =>
adamc@563 1000 S.map2 (mfc ctx c,
adamc@563 1001 fn c' =>
adamc@563 1002 (DClass (x, n, k', c'), loc)))
adamc@213 1003
adamc@338 1004 | DDatabase _ => S.return2 dAll
adamc@271 1005
adamc@459 1006 | DCookie (tn, x, n, c) =>
adamc@459 1007 S.map2 (mfc ctx c,
adamc@459 1008 fn c' =>
adamc@459 1009 (DCookie (tn, x, n, c'), loc))
adamc@720 1010 | DStyle _ => S.return2 dAll
adamc@1075 1011 | DTask (e1, e2) =>
adamc@1075 1012 S.bind2 (mfe ctx e1,
adamc@1075 1013 fn e1' =>
adamc@1075 1014 S.map2 (mfe ctx e2,
adamc@1075 1015 fn e2' =>
adamc@1075 1016 (DTask (e1', e2'), loc)))
adamc@1199 1017 | DPolicy e1 =>
adamc@1199 1018 S.map2 (mfe ctx e1,
adamc@1199 1019 fn e1' =>
adamc@1199 1020 (DPolicy e1', loc))
adamc@459 1021
adamc@123 1022 and mfvi ctx (x, n, c, e) =
adamc@123 1023 S.bind2 (mfc ctx c,
adamc@123 1024 fn c' =>
adamc@123 1025 S.map2 (mfe ctx e,
adamc@123 1026 fn e' =>
adamc@123 1027 (x, n, c', e')))
adamc@76 1028 in
adamc@76 1029 mfd
adamc@76 1030 end
adamc@76 1031
adamc@76 1032 fun mapfold {kind, con, exp, sgn_item, sgn, str, decl} =
adamc@623 1033 mapfoldB {kind = fn () => kind,
adamc@76 1034 con = fn () => con,
adamc@76 1035 exp = fn () => exp,
adamc@76 1036 sgn_item = fn () => sgn_item,
adamc@76 1037 sgn = fn () => sgn,
adamc@76 1038 str = fn () => str,
adamc@76 1039 decl = fn () => decl,
adamc@76 1040 bind = fn ((), _) => ()} ()
adamc@76 1041
adamc@76 1042 fun exists {kind, con, exp, sgn_item, sgn, str, decl} k =
adamc@76 1043 case mapfold {kind = fn k => fn () =>
adamc@76 1044 if kind k then
adamc@76 1045 S.Return ()
adamc@76 1046 else
adamc@76 1047 S.Continue (k, ()),
adamc@76 1048 con = fn c => fn () =>
adamc@76 1049 if con c then
adamc@76 1050 S.Return ()
adamc@76 1051 else
adamc@76 1052 S.Continue (c, ()),
adamc@76 1053 exp = fn e => fn () =>
adamc@76 1054 if exp e then
adamc@76 1055 S.Return ()
adamc@76 1056 else
adamc@76 1057 S.Continue (e, ()),
adamc@76 1058 sgn_item = fn sgi => fn () =>
adamc@76 1059 if sgn_item sgi then
adamc@76 1060 S.Return ()
adamc@76 1061 else
adamc@76 1062 S.Continue (sgi, ()),
adamc@76 1063 sgn = fn x => fn () =>
adamc@76 1064 if sgn x then
adamc@76 1065 S.Return ()
adamc@76 1066 else
adamc@76 1067 S.Continue (x, ()),
adamc@76 1068 str = fn x => fn () =>
adamc@76 1069 if str x then
adamc@76 1070 S.Return ()
adamc@76 1071 else
adamc@76 1072 S.Continue (x, ()),
adamc@76 1073 decl = fn x => fn () =>
adamc@76 1074 if decl x then
adamc@76 1075 S.Return ()
adamc@76 1076 else
adamc@76 1077 S.Continue (x, ())} k () of
adamc@76 1078 S.Return _ => true
adamc@76 1079 | S.Continue _ => false
adamc@76 1080
adamc@76 1081 fun search {kind, con, exp, sgn_item, sgn, str, decl} k =
adamc@76 1082 case mapfold {kind = fn x => fn () =>
adamc@76 1083 case kind x of
adamc@76 1084 NONE => S.Continue (x, ())
adamc@76 1085 | SOME v => S.Return v,
adamc@76 1086
adamc@76 1087 con = fn x => fn () =>
adamc@76 1088 case con x of
adamc@76 1089 NONE => S.Continue (x, ())
adamc@76 1090 | SOME v => S.Return v,
adamc@76 1091
adamc@76 1092 exp = fn x => fn () =>
adamc@76 1093 case exp x of
adamc@76 1094 NONE => S.Continue (x, ())
adamc@76 1095 | SOME v => S.Return v,
adamc@76 1096
adamc@76 1097 sgn_item = fn x => fn () =>
adamc@76 1098 case sgn_item x of
adamc@76 1099 NONE => S.Continue (x, ())
adamc@76 1100 | SOME v => S.Return v,
adamc@76 1101
adamc@76 1102 sgn = fn x => fn () =>
adamc@76 1103 case sgn x of
adamc@76 1104 NONE => S.Continue (x, ())
adamc@76 1105 | SOME v => S.Return v,
adamc@76 1106
adamc@76 1107 str = fn x => fn () =>
adamc@76 1108 case str x of
adamc@76 1109 NONE => S.Continue (x, ())
adamc@76 1110 | SOME v => S.Return v,
adamc@76 1111
adamc@76 1112 decl = fn x => fn () =>
adamc@76 1113 case decl x of
adamc@76 1114 NONE => S.Continue (x, ())
adamc@76 1115 | SOME v => S.Return v
adamc@76 1116
adamc@76 1117 } k () of
adamc@76 1118 S.Return x => SOME x
adamc@76 1119 | S.Continue _ => NONE
adamc@76 1120
adamc@448 1121 fun foldMapB {kind, con, exp, sgn_item, sgn, str, decl, bind} ctx st d =
adamc@623 1122 case mapfoldB {kind = fn ctx => fn x => fn st => S.Continue (kind (ctx, x, st)),
adamc@448 1123 con = fn ctx => fn x => fn st => S.Continue (con (ctx, x, st)),
adamc@448 1124 exp = fn ctx => fn x => fn st => S.Continue (exp (ctx, x, st)),
adamc@448 1125 sgn_item = fn ctx => fn x => fn st => S.Continue (sgn_item (ctx, x, st)),
adamc@448 1126 sgn = fn ctx => fn x => fn st => S.Continue (sgn (ctx, x, st)),
adamc@448 1127 str = fn ctx => fn x => fn st => S.Continue (str (ctx, x, st)),
adamc@448 1128 decl = fn ctx => fn x => fn st => S.Continue (decl (ctx, x, st)),
adamc@448 1129 bind = bind} ctx d st of
adamc@448 1130 S.Continue x => x
adamc@448 1131 | S.Return _ => raise Fail "ElabUtil.Decl.foldMapB: Impossible"
adamc@448 1132
adamc@448 1133 end
adamc@448 1134
adamc@448 1135 structure File = struct
adamc@448 1136
adamc@448 1137 fun maxName ds = foldl (fn (d, count) => Int.max (maxNameDecl d, count)) 0 ds
adamc@448 1138
adamc@448 1139 and maxNameDecl (d, _) =
adamc@448 1140 case d of
adamc@448 1141 DCon (_, n, _, _) => n
adamc@805 1142 | DDatatype dts =>
adamc@805 1143 foldl (fn ((_, n, _, ns), max) =>
adamc@448 1144 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@805 1145 (Int.max (n, max)) ns) 0 dts
adamc@448 1146 | DDatatypeImp (_, n1, n2, _, _, _, ns) =>
adamc@448 1147 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@448 1148 (Int.max (n1, n2)) ns
adamc@448 1149 | DVal (_, n, _, _) => n
adamc@448 1150 | DValRec vis => foldl (fn ((_, n, _, _), count) => Int.max (n, count)) 0 vis
adamc@448 1151 | DStr (_, n, sgn, str) => Int.max (n, Int.max (maxNameSgn sgn, maxNameStr str))
adamc@448 1152 | DSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1153 | DFfiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1154 | DConstraint _ => 0
adamc@563 1155 | DClass (_, n, _, _) => n
adamc@448 1156 | DExport _ => 0
adamc@707 1157 | DTable (n1, _, n2, _, _, _, _, _) => Int.max (n1, n2)
adamc@459 1158 | DSequence (n1, _, n2) => Int.max (n1, n2)
adamc@754 1159 | DView (n1, _, n2, _, _) => Int.max (n1, n2)
adamc@448 1160 | DDatabase _ => 0
adamc@459 1161 | DCookie (n1, _, n2, _) => Int.max (n1, n2)
adamc@720 1162 | DStyle (n1, _, n2) => Int.max (n1, n2)
adamc@1075 1163 | DTask _ => 0
adamc@1199 1164 | DPolicy _ => 0
adamc@448 1165 and maxNameStr (str, _) =
adamc@448 1166 case str of
adamc@448 1167 StrConst ds => maxName ds
adamc@448 1168 | StrVar n => n
adamc@448 1169 | StrProj (str, _) => maxNameStr str
adamc@448 1170 | StrFun (_, n, dom, ran, str) => foldl Int.max n [maxNameSgn dom, maxNameSgn ran, maxNameStr str]
adamc@448 1171 | StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
adamc@448 1172 | StrError => 0
adamc@448 1173
adamc@448 1174 and maxNameSgn (sgn, _) =
adamc@448 1175 case sgn of
adamc@448 1176 SgnConst sgis => foldl (fn (sgi, count) => Int.max (maxNameSgi sgi, count)) 0 sgis
adamc@448 1177 | SgnVar n => n
adamc@448 1178 | SgnFun (_, n, dom, ran) => Int.max (n, Int.max (maxNameSgn dom, maxNameSgn ran))
adamc@448 1179 | SgnWhere (sgn, _, _) => maxNameSgn sgn
adamc@448 1180 | SgnProj (n, _, _) => n
adamc@448 1181 | SgnError => 0
adamc@448 1182
adamc@448 1183 and maxNameSgi (sgi, _) =
adamc@448 1184 case sgi of
adamc@448 1185 SgiConAbs (_, n, _) => n
adamc@448 1186 | SgiCon (_, n, _, _) => n
adamc@805 1187 | SgiDatatype dts =>
adamc@805 1188 foldl (fn ((_, n, _, ns), max) =>
adamc@805 1189 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@805 1190 (Int.max (n, max)) ns) 0 dts
adamc@448 1191 | SgiDatatypeImp (_, n1, n2, _, _, _, ns) =>
adamc@448 1192 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@448 1193 (Int.max (n1, n2)) ns
adamc@448 1194 | SgiVal (_, n, _) => n
adamc@448 1195 | SgiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1196 | SgiSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1197 | SgiConstraint _ => 0
adamc@563 1198 | SgiClassAbs (_, n, _) => n
adamc@563 1199 | SgiClass (_, n, _, _) => n
adamc@448 1200
adamc@34 1201 end
adamc@76 1202
adamc@76 1203 end