annotate src/elab_util.sml @ 628:12b73f3c108e

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