annotate src/elab_util.sml @ 825:7f871c03e3a1

Destructing local let, to the point where demo compiles
author Adam Chlipala <adamc@hcoop.net>
date Thu, 28 May 2009 12:07:05 -0400
parents e2780d2f4afc
children 78504d97410b
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@2 283 end
adamc@2 284
adamc@10 285 structure Exp = struct
adamc@10 286
adamc@11 287 datatype binder =
adamc@623 288 RelK of string
adamc@623 289 | RelC of string * Elab.kind
adamc@792 290 | NamedC of string * int * Elab.kind * Elab.con option
adamc@11 291 | RelE of string * Elab.con
adamc@11 292 | NamedE of string * Elab.con
adamc@11 293
adamc@11 294 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@10 295 let
adamc@623 296 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@10 297
adamc@11 298 fun bind' (ctx, b) =
adamc@11 299 let
adamc@11 300 val b' = case b of
adamc@623 301 Con.RelK x => RelK x
adamc@623 302 | Con.RelC x => RelC x
adamc@623 303 | Con.NamedC x => NamedC x
adamc@11 304 in
adamc@11 305 bind (ctx, b')
adamc@11 306 end
adamc@11 307 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@10 308
adamc@825 309 fun doVars ((p, _), ctx) =
adamc@825 310 case p of
adamc@825 311 PWild => ctx
adamc@825 312 | PVar xt => bind (ctx, RelE xt)
adamc@825 313 | PPrim _ => ctx
adamc@825 314 | PCon (_, _, _, NONE) => ctx
adamc@825 315 | PCon (_, _, _, SOME p) => doVars (p, ctx)
adamc@825 316 | PRecord xpcs =>
adamc@825 317 foldl (fn ((_, p, _), ctx) => doVars (p, ctx))
adamc@825 318 ctx xpcs
adamc@825 319
adamc@11 320 fun mfe ctx e acc =
adamc@11 321 S.bindP (mfe' ctx e acc, fe ctx)
adamc@11 322
adamc@11 323 and mfe' ctx (eAll as (e, loc)) =
adamc@10 324 case e of
adamc@14 325 EPrim _ => S.return2 eAll
adamc@14 326 | ERel _ => S.return2 eAll
adamc@10 327 | ENamed _ => S.return2 eAll
adamc@34 328 | EModProj _ => S.return2 eAll
adamc@10 329 | EApp (e1, e2) =>
adamc@11 330 S.bind2 (mfe ctx e1,
adamc@10 331 fn e1' =>
adamc@11 332 S.map2 (mfe ctx e2,
adamc@10 333 fn e2' =>
adamc@10 334 (EApp (e1', e2'), loc)))
adamc@26 335 | EAbs (x, dom, ran, e) =>
adamc@26 336 S.bind2 (mfc ctx dom,
adamc@26 337 fn dom' =>
adamc@26 338 S.bind2 (mfc ctx ran,
adamc@26 339 fn ran' =>
adamc@26 340 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 341 fn e' =>
adamc@26 342 (EAbs (x, dom', ran', e'), loc))))
adamc@26 343
adamc@10 344 | ECApp (e, c) =>
adamc@11 345 S.bind2 (mfe ctx e,
adamc@10 346 fn e' =>
adamc@11 347 S.map2 (mfc ctx c,
adamc@10 348 fn c' =>
adamc@10 349 (ECApp (e', c'), loc)))
adamc@10 350 | ECAbs (expl, x, k, e) =>
adamc@623 351 S.bind2 (mfk ctx k,
adamc@10 352 fn k' =>
adamc@11 353 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@10 354 fn e' =>
adamc@10 355 (ECAbs (expl, x, k', e'), loc)))
adamc@10 356
adamc@12 357 | ERecord xes =>
adamc@29 358 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@12 359 S.bind2 (mfc ctx x,
adamc@12 360 fn x' =>
adamc@29 361 S.bind2 (mfe ctx e,
adamc@12 362 fn e' =>
adamc@29 363 S.map2 (mfc ctx t,
adamc@29 364 fn t' =>
adamc@29 365 (x', e', t')))))
adamc@12 366 xes,
adamc@12 367 fn xes' =>
adamc@12 368 (ERecord xes', loc))
adamc@12 369 | EField (e, c, {field, rest}) =>
adamc@12 370 S.bind2 (mfe ctx e,
adamc@12 371 fn e' =>
adamc@12 372 S.bind2 (mfc ctx c,
adamc@12 373 fn c' =>
adamc@12 374 S.bind2 (mfc ctx field,
adamc@12 375 fn field' =>
adamc@12 376 S.map2 (mfc ctx rest,
adamc@12 377 fn rest' =>
adamc@12 378 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@445 379 | EConcat (e1, c1, e2, c2) =>
adamc@339 380 S.bind2 (mfe ctx e1,
adamc@339 381 fn e1' =>
adamc@445 382 S.bind2 (mfc ctx c1,
adamc@445 383 fn c1' =>
adamc@339 384 S.bind2 (mfe ctx e2,
adamc@339 385 fn e2' =>
adamc@445 386 S.map2 (mfc ctx c2,
adamc@445 387 fn c2' =>
adamc@445 388 (EConcat (e1', c1', e2', c2'),
adamc@445 389 loc)))))
adamc@149 390 | ECut (e, c, {field, rest}) =>
adamc@149 391 S.bind2 (mfe ctx e,
adamc@149 392 fn e' =>
adamc@149 393 S.bind2 (mfc ctx c,
adamc@149 394 fn c' =>
adamc@149 395 S.bind2 (mfc ctx field,
adamc@149 396 fn field' =>
adamc@149 397 S.map2 (mfc ctx rest,
adamc@149 398 fn rest' =>
adamc@149 399 (ECut (e', c', {field = field', rest = rest'}), loc)))))
adamc@12 400
adamc@493 401 | ECutMulti (e, c, {rest}) =>
adamc@493 402 S.bind2 (mfe ctx e,
adamc@493 403 fn e' =>
adamc@493 404 S.bind2 (mfc ctx c,
adamc@493 405 fn c' =>
adamc@493 406 S.map2 (mfc ctx rest,
adamc@493 407 fn rest' =>
adamc@493 408 (ECutMulti (e', c', {rest = rest'}), loc))))
adamc@493 409
adamc@182 410 | ECase (e, pes, {disc, result}) =>
adamc@171 411 S.bind2 (mfe ctx e,
adamc@171 412 fn e' =>
adamc@171 413 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@448 414 let
adamc@448 415 fun pb ((p, _), ctx) =
adamc@448 416 case p of
adamc@448 417 PWild => ctx
adamc@448 418 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@448 419 | PPrim _ => ctx
adamc@448 420 | PCon (_, _, _, NONE) => ctx
adamc@448 421 | PCon (_, _, _, SOME p) => pb (p, ctx)
adamc@448 422 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@448 423 pb (p, ctx)) ctx xps
adamc@448 424 in
adamc@448 425 S.map2 (mfe (pb (p, ctx)) e,
adamc@448 426 fn e' => (p, e'))
adamc@448 427 end) pes,
adamc@171 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@171 434
adamc@10 435 | EError => S.return2 eAll
adamc@228 436 | EUnif (ref (SOME e)) => mfe ctx e
adamc@228 437 | EUnif _ => S.return2 eAll
adamc@447 438
adamc@825 439 | ELet (des, e, t) =>
adamc@447 440 let
adamc@447 441 val (des, ctx) = foldl (fn (ed, (des, ctx)) =>
adamc@453 442 let
adamc@453 443 val ctx' =
adamc@453 444 case #1 ed of
adamc@825 445 EDVal (p, _, _) => doVars (p, ctx)
adamc@453 446 | EDValRec vis =>
adamc@453 447 foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t))) ctx vis
adamc@453 448 in
adamc@453 449 (S.bind2 (des,
adamc@453 450 fn des' =>
adamc@453 451 S.map2 (mfed ctx ed,
adamc@447 452 fn ed' => des' @ [ed'])),
adamc@453 453 ctx')
adamc@453 454 end)
adamc@447 455 (S.return2 [], ctx) des
adamc@447 456 in
adamc@447 457 S.bind2 (des,
adamc@447 458 fn des' =>
adamc@825 459 S.bind2 (mfe ctx e,
adamc@447 460 fn e' =>
adamc@825 461 S.map2 (mfc ctx t,
adamc@825 462 fn t' =>
adamc@825 463 (ELet (des', e', t'), loc))))
adamc@447 464 end
adamc@447 465
adamc@623 466 | EKAbs (x, e) =>
adamc@623 467 S.map2 (mfe (bind (ctx, RelK x)) e,
adamc@623 468 fn e' =>
adamc@623 469 (EKAbs (x, e'), loc))
adamc@623 470 | EKApp (e, k) =>
adamc@623 471 S.bind2 (mfe ctx e,
adamc@623 472 fn e' =>
adamc@623 473 S.map2 (mfk ctx k,
adamc@623 474 fn k' =>
adamc@623 475 (EKApp (e', k'), loc)))
adamc@623 476
adamc@447 477 and mfed ctx (dAll as (d, loc)) =
adamc@447 478 case d of
adamc@825 479 EDVal (p, t, e) =>
adamc@825 480 S.bind2 (mfc ctx t,
adamc@825 481 fn t' =>
adamc@825 482 S.map2 (mfe (doVars (p, ctx)) e,
adamc@825 483 fn e' =>
adamc@825 484 (EDVal (p, t', e'), loc)))
adamc@447 485 | EDValRec vis =>
adamc@447 486 let
adamc@453 487 val ctx = foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t))) ctx vis
adamc@447 488 in
adamc@447 489 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@447 490 fn vis' =>
adamc@447 491 (EDValRec vis', loc))
adamc@447 492 end
adamc@447 493
adamc@447 494 and mfvi ctx (x, c, e) =
adamc@447 495 S.bind2 (mfc ctx c,
adamc@447 496 fn c' =>
adamc@447 497 S.map2 (mfe ctx e,
adamc@447 498 fn e' =>
adamc@447 499 (x, c', e')))
adamc@10 500 in
adamc@10 501 mfe
adamc@10 502 end
adamc@10 503
adamc@11 504 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@623 505 mapfoldB {kind = fn () => fk,
adamc@11 506 con = fn () => fc,
adamc@11 507 exp = fn () => fe,
adamc@11 508 bind = fn ((), _) => ()} ()
adamc@11 509
adamc@10 510 fun exists {kind, con, exp} k =
adamc@10 511 case mapfold {kind = fn k => fn () =>
adamc@10 512 if kind k then
adamc@10 513 S.Return ()
adamc@10 514 else
adamc@10 515 S.Continue (k, ()),
adamc@10 516 con = fn c => fn () =>
adamc@10 517 if con c then
adamc@10 518 S.Return ()
adamc@10 519 else
adamc@10 520 S.Continue (c, ()),
adamc@10 521 exp = fn e => fn () =>
adamc@10 522 if exp e then
adamc@10 523 S.Return ()
adamc@10 524 else
adamc@10 525 S.Continue (e, ())} k () of
adamc@10 526 S.Return _ => true
adamc@10 527 | S.Continue _ => false
adamc@10 528
adamc@211 529 fun mapB {kind, con, exp, bind} ctx e =
adamc@623 530 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@211 531 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@211 532 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@211 533 bind = bind} ctx e () of
adamc@211 534 S.Continue (e, ()) => e
adamc@211 535 | S.Return _ => raise Fail "ElabUtil.Exp.mapB: Impossible"
adamc@211 536
adamc@448 537 fun foldB {kind, con, exp, bind} ctx st e =
adamc@623 538 case mapfoldB {kind = fn ctx => fn k => fn st => S.Continue (k, kind (ctx, k, st)),
adamc@448 539 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
adamc@448 540 exp = fn ctx => fn e => fn st => S.Continue (e, exp (ctx, e, st)),
adamc@448 541 bind = bind} ctx e st of
adamc@448 542 S.Continue (_, st) => st
adamc@448 543 | S.Return _ => raise Fail "ElabUtil.Exp.foldB: Impossible"
adamc@448 544
adamc@10 545 end
adamc@10 546
adamc@34 547 structure Sgn = struct
adamc@34 548
adamc@34 549 datatype binder =
adamc@623 550 RelK of string
adamc@623 551 | RelC of string * Elab.kind
adamc@792 552 | NamedC of string * int * Elab.kind * Elab.con option
adamc@792 553 | Str of string * int * Elab.sgn
adamc@792 554 | Sgn of string * int * Elab.sgn
adamc@34 555
adamc@34 556 fun mapfoldB {kind, con, sgn_item, sgn, bind} =
adamc@34 557 let
adamc@34 558 fun bind' (ctx, b) =
adamc@34 559 let
adamc@34 560 val b' = case b of
adamc@623 561 Con.RelK x => RelK x
adamc@623 562 | Con.RelC x => RelC x
adamc@623 563 | Con.NamedC x => NamedC x
adamc@34 564 in
adamc@34 565 bind (ctx, b')
adamc@34 566 end
adamc@34 567 val con = Con.mapfoldB {kind = kind, con = con, bind = bind'}
adamc@34 568
adamc@623 569 val kind = Kind.mapfoldB {kind = kind, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@34 570
adamc@34 571 fun sgi ctx si acc =
adamc@34 572 S.bindP (sgi' ctx si acc, sgn_item ctx)
adamc@34 573
adamc@156 574 and sgi' ctx (siAll as (si, loc)) =
adamc@34 575 case si of
adamc@34 576 SgiConAbs (x, n, k) =>
adamc@623 577 S.map2 (kind ctx k,
adamc@34 578 fn k' =>
adamc@34 579 (SgiConAbs (x, n, k'), loc))
adamc@34 580 | SgiCon (x, n, k, c) =>
adamc@623 581 S.bind2 (kind ctx k,
adamc@34 582 fn k' =>
adamc@34 583 S.map2 (con ctx c,
adamc@34 584 fn c' =>
adamc@34 585 (SgiCon (x, n, k', c'), loc)))
adamc@805 586 | SgiDatatype dts =>
adamc@805 587 S.map2 (ListUtil.mapfold (fn (x, n, xs, xncs) =>
adamc@805 588 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@805 589 case c of
adamc@805 590 NONE => S.return2 (x, n, c)
adamc@805 591 | SOME c =>
adamc@805 592 S.map2 (con ctx c,
adamc@805 593 fn c' => (x, n, SOME c'))) xncs,
adamc@805 594 fn xncs' => (x, n, xs, xncs'))) dts,
adamc@805 595 fn dts' =>
adamc@805 596 (SgiDatatype dts', loc))
adamc@191 597 | SgiDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
adamc@162 598 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@162 599 case c of
adamc@162 600 NONE => S.return2 (x, n, c)
adamc@162 601 | SOME c =>
adamc@162 602 S.map2 (con ctx c,
adamc@162 603 fn c' => (x, n, SOME c'))) xncs,
adamc@162 604 fn xncs' =>
adamc@191 605 (SgiDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
adamc@34 606 | SgiVal (x, n, c) =>
adamc@34 607 S.map2 (con ctx c,
adamc@34 608 fn c' =>
adamc@34 609 (SgiVal (x, n, c'), loc))
adamc@34 610 | SgiStr (x, n, s) =>
adamc@34 611 S.map2 (sg ctx s,
adamc@34 612 fn s' =>
adamc@34 613 (SgiStr (x, n, s'), loc))
adamc@59 614 | SgiSgn (x, n, s) =>
adamc@59 615 S.map2 (sg ctx s,
adamc@59 616 fn s' =>
adamc@59 617 (SgiSgn (x, n, s'), loc))
adamc@88 618 | SgiConstraint (c1, c2) =>
adamc@88 619 S.bind2 (con ctx c1,
adamc@88 620 fn c1' =>
adamc@88 621 S.map2 (con ctx c2,
adamc@88 622 fn c2' =>
adamc@88 623 (SgiConstraint (c1', c2'), loc)))
adamc@563 624 | SgiClassAbs (x, n, k) =>
adamc@623 625 S.map2 (kind ctx k,
adamc@563 626 fn k' =>
adamc@563 627 (SgiClassAbs (x, n, k'), loc))
adamc@563 628 | SgiClass (x, n, k, c) =>
adamc@623 629 S.bind2 (kind ctx k,
adamc@563 630 fn k' =>
adamc@563 631 S.map2 (con ctx c,
adamc@563 632 fn c' =>
adamc@563 633 (SgiClass (x, n, k', c'), loc)))
adamc@34 634
adamc@34 635 and sg ctx s acc =
adamc@34 636 S.bindP (sg' ctx s acc, sgn ctx)
adamc@34 637
adamc@34 638 and sg' ctx (sAll as (s, loc)) =
adamc@34 639 case s of
adamc@34 640 SgnConst sgis =>
adamc@34 641 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
adamc@34 642 (case #1 si of
adamc@329 643 SgiConAbs (x, n, k) =>
adamc@792 644 bind (ctx, NamedC (x, n, k, NONE))
adamc@792 645 | SgiCon (x, n, k, c) =>
adamc@792 646 bind (ctx, NamedC (x, n, k, SOME c))
adamc@805 647 | SgiDatatype dts =>
adamc@805 648 foldl (fn ((x, n, ks, _), ctx) =>
adamc@805 649 let
adamc@805 650 val k' = (KType, loc)
adamc@805 651 val k = foldl (fn (_, k) => (KArrow (k', k), loc))
adamc@805 652 k' ks
adamc@805 653 in
adamc@805 654 bind (ctx, NamedC (x, n, k, NONE))
adamc@805 655 end) ctx dts
adamc@792 656 | SgiDatatypeImp (x, n, m1, ms, s, _, _) =>
adamc@792 657 bind (ctx, NamedC (x, n, (KType, loc),
adamc@792 658 SOME (CModProj (m1, ms, s), loc)))
adamc@34 659 | SgiVal _ => ctx
adamc@792 660 | SgiStr (x, n, sgn) =>
adamc@792 661 bind (ctx, Str (x, n, sgn))
adamc@792 662 | SgiSgn (x, n, sgn) =>
adamc@792 663 bind (ctx, Sgn (x, n, sgn))
adamc@203 664 | SgiConstraint _ => ctx
adamc@563 665 | SgiClassAbs (x, n, k) =>
adamc@792 666 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), NONE))
adamc@792 667 | SgiClass (x, n, k, c) =>
adamc@792 668 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), SOME c)),
adamc@34 669 sgi ctx si)) ctx sgis,
adamc@34 670 fn sgis' =>
adamc@34 671 (SgnConst sgis', loc))
adamc@34 672
adamc@34 673 | SgnVar _ => S.return2 sAll
adamc@41 674 | SgnFun (m, n, s1, s2) =>
adamc@41 675 S.bind2 (sg ctx s1,
adamc@41 676 fn s1' =>
adamc@792 677 S.map2 (sg (bind (ctx, Str (m, n, s1'))) s2,
adamc@41 678 fn s2' =>
adamc@41 679 (SgnFun (m, n, s1', s2'), loc)))
adamc@59 680 | SgnProj _ => S.return2 sAll
adamc@42 681 | SgnWhere (sgn, x, c) =>
adamc@42 682 S.bind2 (sg ctx sgn,
adamc@42 683 fn sgn' =>
adamc@42 684 S.map2 (con ctx c,
adamc@42 685 fn c' =>
adamc@42 686 (SgnWhere (sgn', x, c'), loc)))
adamc@34 687 | SgnError => S.return2 sAll
adamc@34 688 in
adamc@34 689 sg
adamc@34 690 end
adamc@34 691
adamc@34 692 fun mapfold {kind, con, sgn_item, sgn} =
adamc@623 693 mapfoldB {kind = fn () => kind,
adamc@34 694 con = fn () => con,
adamc@34 695 sgn_item = fn () => sgn_item,
adamc@34 696 sgn = fn () => sgn,
adamc@34 697 bind = fn ((), _) => ()} ()
adamc@34 698
adamc@792 699 fun mapB {kind, con, sgn_item, sgn, bind} ctx s =
adamc@792 700 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@792 701 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@792 702 sgn_item = fn ctx => fn sgi => fn () => S.Continue (sgn_item ctx sgi, ()),
adamc@792 703 sgn = fn ctx => fn s => fn () => S.Continue (sgn ctx s, ()),
adamc@792 704 bind = bind} ctx s () of
adamc@792 705 S.Continue (s, ()) => s
adamc@792 706 | S.Return _ => raise Fail "ElabUtil.Sgn.mapB: Impossible"
adamc@792 707
adamc@34 708 fun map {kind, con, sgn_item, sgn} s =
adamc@34 709 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@34 710 con = fn c => fn () => S.Continue (con c, ()),
adamc@34 711 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
adamc@34 712 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
adamc@34 713 S.Return () => raise Fail "Elab_util.Sgn.map"
adamc@34 714 | S.Continue (s, ()) => s
adamc@34 715
adamc@2 716 end
adamc@34 717
adamc@76 718 structure Decl = struct
adamc@76 719
adamc@76 720 datatype binder =
adamc@623 721 RelK of string
adamc@623 722 | RelC of string * Elab.kind
adamc@792 723 | NamedC of string * int * Elab.kind * Elab.con option
adamc@76 724 | RelE of string * Elab.con
adamc@76 725 | NamedE of string * Elab.con
adamc@76 726 | Str of string * Elab.sgn
adamc@76 727 | Sgn of string * Elab.sgn
adamc@76 728
adamc@76 729 fun mapfoldB {kind = fk, con = fc, exp = fe, sgn_item = fsgi, sgn = fsg, str = fst, decl = fd, bind} =
adamc@76 730 let
adamc@623 731 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@76 732
adamc@76 733 fun bind' (ctx, b) =
adamc@76 734 let
adamc@76 735 val b' = case b of
adamc@623 736 Con.RelK x => RelK x
adamc@623 737 | Con.RelC x => RelC x
adamc@623 738 | Con.NamedC x => NamedC x
adamc@76 739 in
adamc@76 740 bind (ctx, b')
adamc@76 741 end
adamc@76 742 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@76 743
adamc@76 744 fun bind' (ctx, b) =
adamc@76 745 let
adamc@76 746 val b' = case b of
adamc@623 747 Exp.RelK x => RelK x
adamc@623 748 | Exp.RelC x => RelC x
adamc@76 749 | Exp.NamedC x => NamedC x
adamc@76 750 | Exp.RelE x => RelE x
adamc@76 751 | Exp.NamedE x => NamedE x
adamc@76 752 in
adamc@76 753 bind (ctx, b')
adamc@76 754 end
adamc@76 755 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind'}
adamc@76 756
adamc@76 757 fun bind' (ctx, b) =
adamc@76 758 let
adamc@76 759 val b' = case b of
adamc@623 760 Sgn.RelK x => RelK x
adamc@623 761 | Sgn.RelC x => RelC x
adamc@76 762 | Sgn.NamedC x => NamedC x
adamc@792 763 | Sgn.Sgn (x, _, y) => Sgn (x, y)
adamc@792 764 | Sgn.Str (x, _, y) => Str (x, y)
adamc@76 765 in
adamc@76 766 bind (ctx, b')
adamc@76 767 end
adamc@76 768 val mfsg = Sgn.mapfoldB {kind = fk, con = fc, sgn_item = fsgi, sgn = fsg, bind = bind'}
adamc@76 769
adamc@76 770 fun mfst ctx str acc =
adamc@76 771 S.bindP (mfst' ctx str acc, fst ctx)
adamc@76 772
adamc@76 773 and mfst' ctx (strAll as (str, loc)) =
adamc@76 774 case str of
adamc@76 775 StrConst ds =>
adamc@76 776 S.map2 (ListUtil.mapfoldB (fn (ctx, d) =>
adamc@76 777 (case #1 d of
adamc@792 778 DCon (x, n, k, c) =>
adamc@792 779 bind (ctx, NamedC (x, n, k, SOME c))
adamc@805 780 | DDatatype dts =>
adamc@156 781 let
adamc@805 782 fun doOne ((x, n, xs, xncs), ctx) =
adamc@805 783 let
adamc@805 784 val ctx = bind (ctx, NamedC (x, n, (KType, loc), NONE))
adamc@805 785 in
adamc@805 786 foldl (fn ((x, _, co), ctx) =>
adamc@805 787 let
adamc@805 788 val t =
adamc@805 789 case co of
adamc@805 790 NONE => CNamed n
adamc@805 791 | SOME t => TFun (t, (CNamed n, loc))
adamc@805 792
adamc@805 793 val k = (KType, loc)
adamc@805 794 val t = (t, loc)
adamc@805 795 val t = foldr (fn (x, t) =>
adamc@805 796 (TCFun (Explicit,
adamc@805 797 x,
adamc@805 798 k,
adamc@805 799 t), loc))
adamc@805 800 t xs
adamc@805 801 in
adamc@805 802 bind (ctx, NamedE (x, t))
adamc@805 803 end)
adamc@805 804 ctx xncs
adamc@805 805 end
adamc@156 806 in
adamc@805 807 foldl doOne ctx dts
adamc@156 808 end
adamc@191 809 | DDatatypeImp (x, n, m, ms, x', _, _) =>
adamc@792 810 bind (ctx, NamedC (x, n, (KType, loc),
adamc@792 811 SOME (CModProj (m, ms, x'), loc)))
adamc@76 812 | DVal (x, _, c, _) =>
adamc@76 813 bind (ctx, NamedE (x, c))
adamc@123 814 | DValRec vis =>
adamc@123 815 foldl (fn ((x, _, c, _), ctx) => bind (ctx, NamedE (x, c))) ctx vis
adamc@76 816 | DSgn (x, _, sgn) =>
adamc@76 817 bind (ctx, Sgn (x, sgn))
adamc@76 818 | DStr (x, _, sgn, _) =>
adamc@76 819 bind (ctx, Str (x, sgn))
adamc@76 820 | DFfiStr (x, _, sgn) =>
adamc@88 821 bind (ctx, Str (x, sgn))
adamc@100 822 | DConstraint _ => ctx
adamc@203 823 | DExport _ => ctx
adamc@707 824 | DTable (tn, x, n, c, _, pc, _, cc) =>
adamc@705 825 let
adamc@705 826 val ct = (CModProj (n, [], "sql_table"), loc)
adamc@705 827 val ct = (CApp (ct, c), loc)
adamc@707 828 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
adamc@705 829 in
adamc@705 830 bind (ctx, NamedE (x, ct))
adamc@705 831 end
adamc@338 832 | DSequence (tn, x, n) =>
adamc@338 833 bind (ctx, NamedE (x, (CModProj (n, [], "sql_sequence"), loc)))
adamc@754 834 | DView (tn, x, n, _, c) =>
adamc@754 835 let
adamc@754 836 val ct = (CModProj (n, [], "sql_view"), loc)
adamc@754 837 val ct = (CApp (ct, c), loc)
adamc@754 838 in
adamc@754 839 bind (ctx, NamedE (x, ct))
adamc@754 840 end
adamc@792 841 | DClass (x, n, k, c) =>
adamc@792 842 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), SOME c))
adamc@459 843 | DDatabase _ => ctx
adamc@459 844 | DCookie (tn, x, n, c) =>
adamc@459 845 bind (ctx, NamedE (x, (CApp ((CModProj (n, [], "cookie"), loc),
adamc@718 846 c), loc)))
adamc@720 847 | DStyle (tn, x, n) =>
adamc@720 848 bind (ctx, NamedE (x, (CModProj (n, [], "css_class"), loc))),
adamc@76 849 mfd ctx d)) ctx ds,
adamc@76 850 fn ds' => (StrConst ds', loc))
adamc@76 851 | StrVar _ => S.return2 strAll
adamc@76 852 | StrProj (str, x) =>
adamc@76 853 S.map2 (mfst ctx str,
adamc@76 854 fn str' =>
adamc@76 855 (StrProj (str', x), loc))
adamc@76 856 | StrFun (x, n, sgn1, sgn2, str) =>
adamc@76 857 S.bind2 (mfsg ctx sgn1,
adamc@76 858 fn sgn1' =>
adamc@76 859 S.bind2 (mfsg ctx sgn2,
adamc@76 860 fn sgn2' =>
adamc@76 861 S.map2 (mfst ctx str,
adamc@76 862 fn str' =>
adamc@76 863 (StrFun (x, n, sgn1', sgn2', str'), loc))))
adamc@76 864 | StrApp (str1, str2) =>
adamc@76 865 S.bind2 (mfst ctx str1,
adamc@76 866 fn str1' =>
adamc@76 867 S.map2 (mfst ctx str2,
adamc@76 868 fn str2' =>
adamc@76 869 (StrApp (str1', str2'), loc)))
adamc@76 870 | StrError => S.return2 strAll
adamc@76 871
adamc@76 872 and mfd ctx d acc =
adamc@76 873 S.bindP (mfd' ctx d acc, fd ctx)
adamc@76 874
adamc@76 875 and mfd' ctx (dAll as (d, loc)) =
adamc@76 876 case d of
adamc@76 877 DCon (x, n, k, c) =>
adamc@623 878 S.bind2 (mfk ctx k,
adamc@76 879 fn k' =>
adamc@76 880 S.map2 (mfc ctx c,
adamc@76 881 fn c' =>
adamc@76 882 (DCon (x, n, k', c'), loc)))
adamc@805 883 | DDatatype dts =>
adamc@805 884 S.map2 (ListUtil.mapfold (fn (x, n, xs, xncs) =>
adamc@805 885 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@805 886 case c of
adamc@805 887 NONE => S.return2 (x, n, c)
adamc@805 888 | SOME c =>
adamc@805 889 S.map2 (mfc ctx c,
adamc@805 890 fn c' => (x, n, SOME c'))) xncs,
adamc@805 891 fn xncs' =>
adamc@805 892 (x, n, xs, xncs'))) dts,
adamc@805 893 fn dts' =>
adamc@805 894 (DDatatype dts', loc))
adamc@191 895 | DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
adamc@162 896 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@162 897 case c of
adamc@162 898 NONE => S.return2 (x, n, c)
adamc@162 899 | SOME c =>
adamc@162 900 S.map2 (mfc ctx c,
adamc@162 901 fn c' => (x, n, SOME c'))) xncs,
adamc@162 902 fn xncs' =>
adamc@191 903 (DDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
adamc@123 904 | DVal vi =>
adamc@123 905 S.map2 (mfvi ctx vi,
adamc@123 906 fn vi' =>
adamc@123 907 (DVal vi', loc))
adamc@123 908 | DValRec vis =>
adamc@123 909 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@123 910 fn vis' =>
adamc@123 911 (DValRec vis', loc))
adamc@76 912 | DSgn (x, n, sgn) =>
adamc@76 913 S.map2 (mfsg ctx sgn,
adamc@76 914 fn sgn' =>
adamc@76 915 (DSgn (x, n, sgn'), loc))
adamc@76 916 | DStr (x, n, sgn, str) =>
adamc@76 917 S.bind2 (mfsg ctx sgn,
adamc@76 918 fn sgn' =>
adamc@76 919 S.map2 (mfst ctx str,
adamc@76 920 fn str' =>
adamc@76 921 (DStr (x, n, sgn', str'), loc)))
adamc@76 922 | DFfiStr (x, n, sgn) =>
adamc@76 923 S.map2 (mfsg ctx sgn,
adamc@76 924 fn sgn' =>
adamc@76 925 (DFfiStr (x, n, sgn'), loc))
adamc@88 926 | DConstraint (c1, c2) =>
adamc@88 927 S.bind2 (mfc ctx c1,
adamc@88 928 fn c1' =>
adamc@88 929 S.map2 (mfc ctx c2,
adamc@88 930 fn c2' =>
adamc@88 931 (DConstraint (c1', c2'), loc)))
adamc@109 932 | DExport (en, sgn, str) =>
adamc@109 933 S.bind2 (mfsg ctx sgn,
adamc@109 934 fn sgn' =>
adamc@109 935 S.map2 (mfst ctx str,
adamc@109 936 fn str' =>
adamc@109 937 (DExport (en, sgn', str'), loc)))
adamc@123 938
adamc@707 939 | DTable (tn, x, n, c, pe, pc, ce, cc) =>
adamc@704 940 S.bind2 (mfc ctx c,
adamc@203 941 fn c' =>
adamc@707 942 S.bind2 (mfe ctx pe,
adamc@707 943 fn pe' =>
adamc@707 944 S.bind2 (mfc ctx pc,
adamc@707 945 fn pc' =>
adamc@707 946 S.bind2 (mfe ctx ce,
adamc@707 947 fn ce' =>
adamc@707 948 S.map2 (mfc ctx cc,
adamc@707 949 fn cc' =>
adamc@707 950 (DTable (tn, x, n, c', pe', pc', ce', cc'), loc))))))
adamc@338 951 | DSequence _ => S.return2 dAll
adamc@754 952 | DView (tn, x, n, e, c) =>
adamc@754 953 S.bind2 (mfe ctx e,
adamc@754 954 fn e' =>
adamc@754 955 S.map2 (mfc ctx c,
adamc@754 956 fn c' =>
adamc@754 957 (DView (tn, x, n, e', c'), loc)))
adamc@203 958
adamc@563 959 | DClass (x, n, k, c) =>
adamc@623 960 S.bind2 (mfk ctx k,
adamc@563 961 fn k' =>
adamc@563 962 S.map2 (mfc ctx c,
adamc@563 963 fn c' =>
adamc@563 964 (DClass (x, n, k', c'), loc)))
adamc@213 965
adamc@338 966 | DDatabase _ => S.return2 dAll
adamc@271 967
adamc@459 968 | DCookie (tn, x, n, c) =>
adamc@459 969 S.map2 (mfc ctx c,
adamc@459 970 fn c' =>
adamc@459 971 (DCookie (tn, x, n, c'), loc))
adamc@720 972 | DStyle _ => S.return2 dAll
adamc@459 973
adamc@123 974 and mfvi ctx (x, n, c, e) =
adamc@123 975 S.bind2 (mfc ctx c,
adamc@123 976 fn c' =>
adamc@123 977 S.map2 (mfe ctx e,
adamc@123 978 fn e' =>
adamc@123 979 (x, n, c', e')))
adamc@76 980 in
adamc@76 981 mfd
adamc@76 982 end
adamc@76 983
adamc@76 984 fun mapfold {kind, con, exp, sgn_item, sgn, str, decl} =
adamc@623 985 mapfoldB {kind = fn () => kind,
adamc@76 986 con = fn () => con,
adamc@76 987 exp = fn () => exp,
adamc@76 988 sgn_item = fn () => sgn_item,
adamc@76 989 sgn = fn () => sgn,
adamc@76 990 str = fn () => str,
adamc@76 991 decl = fn () => decl,
adamc@76 992 bind = fn ((), _) => ()} ()
adamc@76 993
adamc@76 994 fun exists {kind, con, exp, sgn_item, sgn, str, decl} k =
adamc@76 995 case mapfold {kind = fn k => fn () =>
adamc@76 996 if kind k then
adamc@76 997 S.Return ()
adamc@76 998 else
adamc@76 999 S.Continue (k, ()),
adamc@76 1000 con = fn c => fn () =>
adamc@76 1001 if con c then
adamc@76 1002 S.Return ()
adamc@76 1003 else
adamc@76 1004 S.Continue (c, ()),
adamc@76 1005 exp = fn e => fn () =>
adamc@76 1006 if exp e then
adamc@76 1007 S.Return ()
adamc@76 1008 else
adamc@76 1009 S.Continue (e, ()),
adamc@76 1010 sgn_item = fn sgi => fn () =>
adamc@76 1011 if sgn_item sgi then
adamc@76 1012 S.Return ()
adamc@76 1013 else
adamc@76 1014 S.Continue (sgi, ()),
adamc@76 1015 sgn = fn x => fn () =>
adamc@76 1016 if sgn x then
adamc@76 1017 S.Return ()
adamc@76 1018 else
adamc@76 1019 S.Continue (x, ()),
adamc@76 1020 str = fn x => fn () =>
adamc@76 1021 if str x then
adamc@76 1022 S.Return ()
adamc@76 1023 else
adamc@76 1024 S.Continue (x, ()),
adamc@76 1025 decl = fn x => fn () =>
adamc@76 1026 if decl x then
adamc@76 1027 S.Return ()
adamc@76 1028 else
adamc@76 1029 S.Continue (x, ())} k () of
adamc@76 1030 S.Return _ => true
adamc@76 1031 | S.Continue _ => false
adamc@76 1032
adamc@76 1033 fun search {kind, con, exp, sgn_item, sgn, str, decl} k =
adamc@76 1034 case mapfold {kind = fn x => fn () =>
adamc@76 1035 case kind x of
adamc@76 1036 NONE => S.Continue (x, ())
adamc@76 1037 | SOME v => S.Return v,
adamc@76 1038
adamc@76 1039 con = fn x => fn () =>
adamc@76 1040 case con x of
adamc@76 1041 NONE => S.Continue (x, ())
adamc@76 1042 | SOME v => S.Return v,
adamc@76 1043
adamc@76 1044 exp = fn x => fn () =>
adamc@76 1045 case exp x of
adamc@76 1046 NONE => S.Continue (x, ())
adamc@76 1047 | SOME v => S.Return v,
adamc@76 1048
adamc@76 1049 sgn_item = fn x => fn () =>
adamc@76 1050 case sgn_item x of
adamc@76 1051 NONE => S.Continue (x, ())
adamc@76 1052 | SOME v => S.Return v,
adamc@76 1053
adamc@76 1054 sgn = fn x => fn () =>
adamc@76 1055 case sgn x of
adamc@76 1056 NONE => S.Continue (x, ())
adamc@76 1057 | SOME v => S.Return v,
adamc@76 1058
adamc@76 1059 str = fn x => fn () =>
adamc@76 1060 case str x of
adamc@76 1061 NONE => S.Continue (x, ())
adamc@76 1062 | SOME v => S.Return v,
adamc@76 1063
adamc@76 1064 decl = fn x => fn () =>
adamc@76 1065 case decl x of
adamc@76 1066 NONE => S.Continue (x, ())
adamc@76 1067 | SOME v => S.Return v
adamc@76 1068
adamc@76 1069 } k () of
adamc@76 1070 S.Return x => SOME x
adamc@76 1071 | S.Continue _ => NONE
adamc@76 1072
adamc@448 1073 fun foldMapB {kind, con, exp, sgn_item, sgn, str, decl, bind} ctx st d =
adamc@623 1074 case mapfoldB {kind = fn ctx => fn x => fn st => S.Continue (kind (ctx, x, st)),
adamc@448 1075 con = fn ctx => fn x => fn st => S.Continue (con (ctx, x, st)),
adamc@448 1076 exp = fn ctx => fn x => fn st => S.Continue (exp (ctx, x, st)),
adamc@448 1077 sgn_item = fn ctx => fn x => fn st => S.Continue (sgn_item (ctx, x, st)),
adamc@448 1078 sgn = fn ctx => fn x => fn st => S.Continue (sgn (ctx, x, st)),
adamc@448 1079 str = fn ctx => fn x => fn st => S.Continue (str (ctx, x, st)),
adamc@448 1080 decl = fn ctx => fn x => fn st => S.Continue (decl (ctx, x, st)),
adamc@448 1081 bind = bind} ctx d st of
adamc@448 1082 S.Continue x => x
adamc@448 1083 | S.Return _ => raise Fail "ElabUtil.Decl.foldMapB: Impossible"
adamc@448 1084
adamc@448 1085 end
adamc@448 1086
adamc@448 1087 structure File = struct
adamc@448 1088
adamc@448 1089 fun maxName ds = foldl (fn (d, count) => Int.max (maxNameDecl d, count)) 0 ds
adamc@448 1090
adamc@448 1091 and maxNameDecl (d, _) =
adamc@448 1092 case d of
adamc@448 1093 DCon (_, n, _, _) => n
adamc@805 1094 | DDatatype dts =>
adamc@805 1095 foldl (fn ((_, n, _, ns), max) =>
adamc@448 1096 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@805 1097 (Int.max (n, max)) ns) 0 dts
adamc@448 1098 | DDatatypeImp (_, n1, n2, _, _, _, ns) =>
adamc@448 1099 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@448 1100 (Int.max (n1, n2)) ns
adamc@448 1101 | DVal (_, n, _, _) => n
adamc@448 1102 | DValRec vis => foldl (fn ((_, n, _, _), count) => Int.max (n, count)) 0 vis
adamc@448 1103 | DStr (_, n, sgn, str) => Int.max (n, Int.max (maxNameSgn sgn, maxNameStr str))
adamc@448 1104 | DSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1105 | DFfiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1106 | DConstraint _ => 0
adamc@563 1107 | DClass (_, n, _, _) => n
adamc@448 1108 | DExport _ => 0
adamc@707 1109 | DTable (n1, _, n2, _, _, _, _, _) => Int.max (n1, n2)
adamc@459 1110 | DSequence (n1, _, n2) => Int.max (n1, n2)
adamc@754 1111 | DView (n1, _, n2, _, _) => Int.max (n1, n2)
adamc@448 1112 | DDatabase _ => 0
adamc@459 1113 | DCookie (n1, _, n2, _) => Int.max (n1, n2)
adamc@720 1114 | DStyle (n1, _, n2) => Int.max (n1, n2)
adamc@448 1115 and maxNameStr (str, _) =
adamc@448 1116 case str of
adamc@448 1117 StrConst ds => maxName ds
adamc@448 1118 | StrVar n => n
adamc@448 1119 | StrProj (str, _) => maxNameStr str
adamc@448 1120 | StrFun (_, n, dom, ran, str) => foldl Int.max n [maxNameSgn dom, maxNameSgn ran, maxNameStr str]
adamc@448 1121 | StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
adamc@448 1122 | StrError => 0
adamc@448 1123
adamc@448 1124 and maxNameSgn (sgn, _) =
adamc@448 1125 case sgn of
adamc@448 1126 SgnConst sgis => foldl (fn (sgi, count) => Int.max (maxNameSgi sgi, count)) 0 sgis
adamc@448 1127 | SgnVar n => n
adamc@448 1128 | SgnFun (_, n, dom, ran) => Int.max (n, Int.max (maxNameSgn dom, maxNameSgn ran))
adamc@448 1129 | SgnWhere (sgn, _, _) => maxNameSgn sgn
adamc@448 1130 | SgnProj (n, _, _) => n
adamc@448 1131 | SgnError => 0
adamc@448 1132
adamc@448 1133 and maxNameSgi (sgi, _) =
adamc@448 1134 case sgi of
adamc@448 1135 SgiConAbs (_, n, _) => n
adamc@448 1136 | SgiCon (_, n, _, _) => n
adamc@805 1137 | SgiDatatype dts =>
adamc@805 1138 foldl (fn ((_, n, _, ns), max) =>
adamc@805 1139 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@805 1140 (Int.max (n, max)) ns) 0 dts
adamc@448 1141 | SgiDatatypeImp (_, n1, n2, _, _, _, ns) =>
adamc@448 1142 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@448 1143 (Int.max (n1, n2)) ns
adamc@448 1144 | SgiVal (_, n, _) => n
adamc@448 1145 | SgiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1146 | SgiSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1147 | SgiConstraint _ => 0
adamc@563 1148 | SgiClassAbs (_, n, _) => n
adamc@563 1149 | SgiClass (_, n, _, _) => n
adamc@448 1150
adamc@34 1151 end
adamc@76 1152
adamc@76 1153 end