annotate src/elab_util.sml @ 84:e86370850c30

Disjointness assumptions
author Adam Chlipala <adamc@hcoop.net>
date Tue, 01 Jul 2008 12:10:46 -0400
parents b4f2a258e52c
children 1f85890c9846
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@2 32 structure S = Search
adamc@2 33
adamc@2 34 structure Kind = struct
adamc@2 35
adamc@6 36 fun mapfold f =
adamc@2 37 let
adamc@2 38 fun mfk k acc =
adamc@2 39 S.bindP (mfk' k acc, f)
adamc@2 40
adamc@2 41 and mfk' (kAll as (k, loc)) =
adamc@2 42 case k of
adamc@2 43 KType => S.return2 kAll
adamc@2 44
adamc@2 45 | KArrow (k1, k2) =>
adamc@2 46 S.bind2 (mfk k1,
adamc@2 47 fn k1' =>
adamc@2 48 S.map2 (mfk k2,
adamc@2 49 fn k2' =>
adamc@2 50 (KArrow (k1', k2'), loc)))
adamc@2 51
adamc@2 52 | KName => S.return2 kAll
adamc@2 53
adamc@2 54 | KRecord k =>
adamc@2 55 S.map2 (mfk k,
adamc@2 56 fn k' =>
adamc@2 57 (KRecord k', loc))
adamc@2 58
adamc@82 59 | KUnit => S.return2 kAll
adamc@82 60
adamc@2 61 | KError => S.return2 kAll
adamc@2 62
adamc@76 63 | KUnif (_, _, ref (SOME k)) => mfk' k
adamc@2 64 | KUnif _ => S.return2 kAll
adamc@2 65 in
adamc@2 66 mfk
adamc@2 67 end
adamc@2 68
adamc@2 69 fun exists f k =
adamc@6 70 case mapfold (fn k => fn () =>
adamc@6 71 if f k then
adamc@6 72 S.Return ()
adamc@6 73 else
adamc@6 74 S.Continue (k, ())) k () of
adamc@6 75 S.Return _ => true
adamc@6 76 | S.Continue _ => false
adamc@6 77
adamc@6 78 end
adamc@6 79
adamc@6 80 structure Con = struct
adamc@6 81
adamc@11 82 datatype binder =
adamc@11 83 Rel of string * Elab.kind
adamc@11 84 | Named of string * Elab.kind
adamc@11 85
adamc@11 86 fun mapfoldB {kind = fk, con = fc, bind} =
adamc@6 87 let
adamc@6 88 val mfk = Kind.mapfold fk
adamc@6 89
adamc@11 90 fun mfc ctx c acc =
adamc@11 91 S.bindP (mfc' ctx c acc, fc ctx)
adamc@6 92
adamc@11 93 and mfc' ctx (cAll as (c, loc)) =
adamc@6 94 case c of
adamc@6 95 TFun (c1, c2) =>
adamc@11 96 S.bind2 (mfc ctx c1,
adamc@6 97 fn c1' =>
adamc@11 98 S.map2 (mfc ctx c2,
adamc@6 99 fn c2' =>
adamc@6 100 (TFun (c1', c2'), loc)))
adamc@6 101 | TCFun (e, x, k, c) =>
adamc@6 102 S.bind2 (mfk k,
adamc@6 103 fn k' =>
adamc@11 104 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
adamc@6 105 fn c' =>
adamc@6 106 (TCFun (e, x, k', c'), loc)))
adamc@6 107 | TRecord c =>
adamc@11 108 S.map2 (mfc ctx c,
adamc@6 109 fn c' =>
adamc@6 110 (TRecord c', loc))
adamc@6 111
adamc@6 112 | CRel _ => S.return2 cAll
adamc@6 113 | CNamed _ => S.return2 cAll
adamc@34 114 | CModProj _ => S.return2 cAll
adamc@6 115 | CApp (c1, c2) =>
adamc@11 116 S.bind2 (mfc ctx c1,
adamc@6 117 fn c1' =>
adamc@11 118 S.map2 (mfc ctx c2,
adamc@6 119 fn c2' =>
adamc@6 120 (CApp (c1', c2'), loc)))
adamc@8 121 | CAbs (x, k, c) =>
adamc@6 122 S.bind2 (mfk k,
adamc@6 123 fn k' =>
adamc@11 124 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
adamc@6 125 fn c' =>
adamc@8 126 (CAbs (x, k', c'), loc)))
adamc@84 127 | CDisjoint (c1, c2, c3) =>
adamc@84 128 S.bind2 (mfc ctx c1,
adamc@84 129 fn c1' =>
adamc@84 130 S.bind2 (mfc ctx c2,
adamc@84 131 fn c2' =>
adamc@84 132 S.map2 (mfc ctx c3,
adamc@84 133 fn c3' =>
adamc@84 134 (CDisjoint (c1', c2', c3'), loc))))
adamc@6 135
adamc@6 136 | CName _ => S.return2 cAll
adamc@6 137
adamc@6 138 | CRecord (k, xcs) =>
adamc@6 139 S.bind2 (mfk k,
adamc@6 140 fn k' =>
adamc@6 141 S.map2 (ListUtil.mapfold (fn (x, c) =>
adamc@11 142 S.bind2 (mfc ctx x,
adamc@6 143 fn x' =>
adamc@11 144 S.map2 (mfc ctx c,
adamc@6 145 fn c' =>
adamc@6 146 (x', c'))))
adamc@6 147 xcs,
adamc@6 148 fn xcs' =>
adamc@6 149 (CRecord (k', xcs'), loc)))
adamc@6 150 | CConcat (c1, c2) =>
adamc@11 151 S.bind2 (mfc ctx c1,
adamc@6 152 fn c1' =>
adamc@11 153 S.map2 (mfc ctx c2,
adamc@6 154 fn c2' =>
adamc@6 155 (CConcat (c1', c2'), loc)))
adamc@67 156 | CFold (k1, k2) =>
adamc@67 157 S.bind2 (mfk k1,
adamc@67 158 fn k1' =>
adamc@67 159 S.map2 (mfk k2,
adamc@67 160 fn k2' =>
adamc@67 161 (CFold (k1', k2'), loc)))
adamc@6 162
adamc@82 163 | CUnit => S.return2 cAll
adamc@82 164
adamc@6 165 | CError => S.return2 cAll
adamc@76 166 | CUnif (_, _, _, ref (SOME c)) => mfc' ctx c
adamc@6 167 | CUnif _ => S.return2 cAll
adamc@6 168 in
adamc@6 169 mfc
adamc@6 170 end
adamc@6 171
adamc@11 172 fun mapfold {kind = fk, con = fc} =
adamc@11 173 mapfoldB {kind = fk,
adamc@11 174 con = fn () => fc,
adamc@11 175 bind = fn ((), _) => ()} ()
adamc@11 176
adamc@11 177 fun mapB {kind, con, bind} ctx c =
adamc@11 178 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@11 179 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@11 180 bind = bind} ctx c () of
adamc@11 181 S.Continue (c, ()) => c
adamc@34 182 | S.Return _ => raise Fail "ElabUtil.Con.mapB: Impossible"
adamc@34 183
adamc@34 184 fun map {kind, con} s =
adamc@34 185 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@34 186 con = fn c => fn () => S.Continue (con c, ())} s () of
adamc@34 187 S.Return () => raise Fail "ElabUtil.Con.map: Impossible"
adamc@34 188 | S.Continue (s, ()) => s
adamc@11 189
adamc@6 190 fun exists {kind, con} k =
adamc@6 191 case mapfold {kind = fn k => fn () =>
adamc@6 192 if kind k then
adamc@6 193 S.Return ()
adamc@6 194 else
adamc@6 195 S.Continue (k, ()),
adamc@6 196 con = fn c => fn () =>
adamc@6 197 if con c then
adamc@6 198 S.Return ()
adamc@6 199 else
adamc@6 200 S.Continue (c, ())} k () of
adamc@2 201 S.Return _ => true
adamc@2 202 | S.Continue _ => false
adamc@2 203
adamc@2 204 end
adamc@2 205
adamc@10 206 structure Exp = struct
adamc@10 207
adamc@11 208 datatype binder =
adamc@11 209 RelC of string * Elab.kind
adamc@11 210 | NamedC of string * Elab.kind
adamc@11 211 | RelE of string * Elab.con
adamc@11 212 | NamedE of string * Elab.con
adamc@11 213
adamc@11 214 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@10 215 let
adamc@10 216 val mfk = Kind.mapfold fk
adamc@10 217
adamc@11 218 fun bind' (ctx, b) =
adamc@11 219 let
adamc@11 220 val b' = case b of
adamc@11 221 Con.Rel x => RelC x
adamc@11 222 | Con.Named x => NamedC x
adamc@11 223 in
adamc@11 224 bind (ctx, b')
adamc@11 225 end
adamc@11 226 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@10 227
adamc@11 228 fun mfe ctx e acc =
adamc@11 229 S.bindP (mfe' ctx e acc, fe ctx)
adamc@11 230
adamc@11 231 and mfe' ctx (eAll as (e, loc)) =
adamc@10 232 case e of
adamc@14 233 EPrim _ => S.return2 eAll
adamc@14 234 | ERel _ => S.return2 eAll
adamc@10 235 | ENamed _ => S.return2 eAll
adamc@34 236 | EModProj _ => S.return2 eAll
adamc@10 237 | EApp (e1, e2) =>
adamc@11 238 S.bind2 (mfe ctx e1,
adamc@10 239 fn e1' =>
adamc@11 240 S.map2 (mfe ctx e2,
adamc@10 241 fn e2' =>
adamc@10 242 (EApp (e1', e2'), loc)))
adamc@26 243 | EAbs (x, dom, ran, e) =>
adamc@26 244 S.bind2 (mfc ctx dom,
adamc@26 245 fn dom' =>
adamc@26 246 S.bind2 (mfc ctx ran,
adamc@26 247 fn ran' =>
adamc@26 248 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 249 fn e' =>
adamc@26 250 (EAbs (x, dom', ran', e'), loc))))
adamc@26 251
adamc@10 252 | ECApp (e, c) =>
adamc@11 253 S.bind2 (mfe ctx e,
adamc@10 254 fn e' =>
adamc@11 255 S.map2 (mfc ctx c,
adamc@10 256 fn c' =>
adamc@10 257 (ECApp (e', c'), loc)))
adamc@10 258 | ECAbs (expl, x, k, e) =>
adamc@10 259 S.bind2 (mfk k,
adamc@10 260 fn k' =>
adamc@11 261 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@10 262 fn e' =>
adamc@10 263 (ECAbs (expl, x, k', e'), loc)))
adamc@10 264
adamc@12 265 | ERecord xes =>
adamc@29 266 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@12 267 S.bind2 (mfc ctx x,
adamc@12 268 fn x' =>
adamc@29 269 S.bind2 (mfe ctx e,
adamc@12 270 fn e' =>
adamc@29 271 S.map2 (mfc ctx t,
adamc@29 272 fn t' =>
adamc@29 273 (x', e', t')))))
adamc@12 274 xes,
adamc@12 275 fn xes' =>
adamc@12 276 (ERecord xes', loc))
adamc@12 277 | EField (e, c, {field, rest}) =>
adamc@12 278 S.bind2 (mfe ctx e,
adamc@12 279 fn e' =>
adamc@12 280 S.bind2 (mfc ctx c,
adamc@12 281 fn c' =>
adamc@12 282 S.bind2 (mfc ctx field,
adamc@12 283 fn field' =>
adamc@12 284 S.map2 (mfc ctx rest,
adamc@12 285 fn rest' =>
adamc@12 286 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@12 287
adamc@71 288 | EFold k =>
adamc@71 289 S.map2 (mfk k,
adamc@71 290 fn k' =>
adamc@71 291 (EFold k', loc))
adamc@71 292
adamc@10 293 | EError => S.return2 eAll
adamc@10 294 in
adamc@10 295 mfe
adamc@10 296 end
adamc@10 297
adamc@11 298 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@11 299 mapfoldB {kind = fk,
adamc@11 300 con = fn () => fc,
adamc@11 301 exp = fn () => fe,
adamc@11 302 bind = fn ((), _) => ()} ()
adamc@11 303
adamc@10 304 fun exists {kind, con, exp} k =
adamc@10 305 case mapfold {kind = fn k => fn () =>
adamc@10 306 if kind k then
adamc@10 307 S.Return ()
adamc@10 308 else
adamc@10 309 S.Continue (k, ()),
adamc@10 310 con = fn c => fn () =>
adamc@10 311 if con c then
adamc@10 312 S.Return ()
adamc@10 313 else
adamc@10 314 S.Continue (c, ()),
adamc@10 315 exp = fn e => fn () =>
adamc@10 316 if exp e then
adamc@10 317 S.Return ()
adamc@10 318 else
adamc@10 319 S.Continue (e, ())} k () of
adamc@10 320 S.Return _ => true
adamc@10 321 | S.Continue _ => false
adamc@10 322
adamc@10 323 end
adamc@10 324
adamc@34 325 structure Sgn = struct
adamc@34 326
adamc@34 327 datatype binder =
adamc@34 328 RelC of string * Elab.kind
adamc@34 329 | NamedC of string * Elab.kind
adamc@34 330 | Str of string * Elab.sgn
adamc@59 331 | Sgn of string * Elab.sgn
adamc@34 332
adamc@34 333 fun mapfoldB {kind, con, sgn_item, sgn, bind} =
adamc@34 334 let
adamc@34 335 fun bind' (ctx, b) =
adamc@34 336 let
adamc@34 337 val b' = case b of
adamc@34 338 Con.Rel x => RelC x
adamc@34 339 | Con.Named x => NamedC x
adamc@34 340 in
adamc@34 341 bind (ctx, b')
adamc@34 342 end
adamc@34 343 val con = Con.mapfoldB {kind = kind, con = con, bind = bind'}
adamc@34 344
adamc@34 345 val kind = Kind.mapfold kind
adamc@34 346
adamc@34 347 fun sgi ctx si acc =
adamc@34 348 S.bindP (sgi' ctx si acc, sgn_item ctx)
adamc@34 349
adamc@34 350 and sgi' ctx (si, loc) =
adamc@34 351 case si of
adamc@34 352 SgiConAbs (x, n, k) =>
adamc@34 353 S.map2 (kind k,
adamc@34 354 fn k' =>
adamc@34 355 (SgiConAbs (x, n, k'), loc))
adamc@34 356 | SgiCon (x, n, k, c) =>
adamc@34 357 S.bind2 (kind k,
adamc@34 358 fn k' =>
adamc@34 359 S.map2 (con ctx c,
adamc@34 360 fn c' =>
adamc@34 361 (SgiCon (x, n, k', c'), loc)))
adamc@34 362 | SgiVal (x, n, c) =>
adamc@34 363 S.map2 (con ctx c,
adamc@34 364 fn c' =>
adamc@34 365 (SgiVal (x, n, c'), loc))
adamc@34 366 | SgiStr (x, n, s) =>
adamc@34 367 S.map2 (sg ctx s,
adamc@34 368 fn s' =>
adamc@34 369 (SgiStr (x, n, s'), loc))
adamc@59 370 | SgiSgn (x, n, s) =>
adamc@59 371 S.map2 (sg ctx s,
adamc@59 372 fn s' =>
adamc@59 373 (SgiSgn (x, n, s'), loc))
adamc@34 374
adamc@34 375 and sg ctx s acc =
adamc@34 376 S.bindP (sg' ctx s acc, sgn ctx)
adamc@34 377
adamc@34 378 and sg' ctx (sAll as (s, loc)) =
adamc@34 379 case s of
adamc@34 380 SgnConst sgis =>
adamc@34 381 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
adamc@34 382 (case #1 si of
adamc@34 383 SgiConAbs (x, _, k) =>
adamc@34 384 bind (ctx, NamedC (x, k))
adamc@34 385 | SgiCon (x, _, k, _) =>
adamc@34 386 bind (ctx, NamedC (x, k))
adamc@34 387 | SgiVal _ => ctx
adamc@34 388 | SgiStr (x, _, sgn) =>
adamc@59 389 bind (ctx, Str (x, sgn))
adamc@59 390 | SgiSgn (x, _, sgn) =>
adamc@59 391 bind (ctx, Sgn (x, sgn)),
adamc@34 392 sgi ctx si)) ctx sgis,
adamc@34 393 fn sgis' =>
adamc@34 394 (SgnConst sgis', loc))
adamc@34 395
adamc@34 396 | SgnVar _ => S.return2 sAll
adamc@41 397 | SgnFun (m, n, s1, s2) =>
adamc@41 398 S.bind2 (sg ctx s1,
adamc@41 399 fn s1' =>
adamc@41 400 S.map2 (sg (bind (ctx, Str (m, s1'))) s2,
adamc@41 401 fn s2' =>
adamc@41 402 (SgnFun (m, n, s1', s2'), loc)))
adamc@59 403 | SgnProj _ => S.return2 sAll
adamc@42 404 | SgnWhere (sgn, x, c) =>
adamc@42 405 S.bind2 (sg ctx sgn,
adamc@42 406 fn sgn' =>
adamc@42 407 S.map2 (con ctx c,
adamc@42 408 fn c' =>
adamc@42 409 (SgnWhere (sgn', x, c'), loc)))
adamc@34 410 | SgnError => S.return2 sAll
adamc@34 411 in
adamc@34 412 sg
adamc@34 413 end
adamc@34 414
adamc@34 415 fun mapfold {kind, con, sgn_item, sgn} =
adamc@34 416 mapfoldB {kind = kind,
adamc@34 417 con = fn () => con,
adamc@34 418 sgn_item = fn () => sgn_item,
adamc@34 419 sgn = fn () => sgn,
adamc@34 420 bind = fn ((), _) => ()} ()
adamc@34 421
adamc@34 422 fun map {kind, con, sgn_item, sgn} s =
adamc@34 423 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@34 424 con = fn c => fn () => S.Continue (con c, ()),
adamc@34 425 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
adamc@34 426 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
adamc@34 427 S.Return () => raise Fail "Elab_util.Sgn.map"
adamc@34 428 | S.Continue (s, ()) => s
adamc@34 429
adamc@2 430 end
adamc@34 431
adamc@76 432 structure Decl = struct
adamc@76 433
adamc@76 434 datatype binder =
adamc@76 435 RelC of string * Elab.kind
adamc@76 436 | NamedC of string * Elab.kind
adamc@76 437 | RelE of string * Elab.con
adamc@76 438 | NamedE of string * Elab.con
adamc@76 439 | Str of string * Elab.sgn
adamc@76 440 | Sgn of string * Elab.sgn
adamc@76 441
adamc@76 442 fun mapfoldB {kind = fk, con = fc, exp = fe, sgn_item = fsgi, sgn = fsg, str = fst, decl = fd, bind} =
adamc@76 443 let
adamc@76 444 val mfk = Kind.mapfold fk
adamc@76 445
adamc@76 446 fun bind' (ctx, b) =
adamc@76 447 let
adamc@76 448 val b' = case b of
adamc@76 449 Con.Rel x => RelC x
adamc@76 450 | Con.Named x => NamedC x
adamc@76 451 in
adamc@76 452 bind (ctx, b')
adamc@76 453 end
adamc@76 454 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@76 455
adamc@76 456 fun bind' (ctx, b) =
adamc@76 457 let
adamc@76 458 val b' = case b of
adamc@76 459 Exp.RelC x => RelC x
adamc@76 460 | Exp.NamedC x => NamedC x
adamc@76 461 | Exp.RelE x => RelE x
adamc@76 462 | Exp.NamedE x => NamedE x
adamc@76 463 in
adamc@76 464 bind (ctx, b')
adamc@76 465 end
adamc@76 466 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind'}
adamc@76 467
adamc@76 468 fun bind' (ctx, b) =
adamc@76 469 let
adamc@76 470 val b' = case b of
adamc@76 471 Sgn.RelC x => RelC x
adamc@76 472 | Sgn.NamedC x => NamedC x
adamc@76 473 | Sgn.Sgn x => Sgn x
adamc@76 474 | Sgn.Str x => Str x
adamc@76 475 in
adamc@76 476 bind (ctx, b')
adamc@76 477 end
adamc@76 478 val mfsg = Sgn.mapfoldB {kind = fk, con = fc, sgn_item = fsgi, sgn = fsg, bind = bind'}
adamc@76 479
adamc@76 480 fun mfst ctx str acc =
adamc@76 481 S.bindP (mfst' ctx str acc, fst ctx)
adamc@76 482
adamc@76 483 and mfst' ctx (strAll as (str, loc)) =
adamc@76 484 case str of
adamc@76 485 StrConst ds =>
adamc@76 486 S.map2 (ListUtil.mapfoldB (fn (ctx, d) =>
adamc@76 487 (case #1 d of
adamc@76 488 DCon (x, _, k, _) =>
adamc@76 489 bind (ctx, NamedC (x, k))
adamc@76 490 | DVal (x, _, c, _) =>
adamc@76 491 bind (ctx, NamedE (x, c))
adamc@76 492 | DSgn (x, _, sgn) =>
adamc@76 493 bind (ctx, Sgn (x, sgn))
adamc@76 494 | DStr (x, _, sgn, _) =>
adamc@76 495 bind (ctx, Str (x, sgn))
adamc@76 496 | DFfiStr (x, _, sgn) =>
adamc@76 497 bind (ctx, Str (x, sgn)),
adamc@76 498 mfd ctx d)) ctx ds,
adamc@76 499 fn ds' => (StrConst ds', loc))
adamc@76 500 | StrVar _ => S.return2 strAll
adamc@76 501 | StrProj (str, x) =>
adamc@76 502 S.map2 (mfst ctx str,
adamc@76 503 fn str' =>
adamc@76 504 (StrProj (str', x), loc))
adamc@76 505 | StrFun (x, n, sgn1, sgn2, str) =>
adamc@76 506 S.bind2 (mfsg ctx sgn1,
adamc@76 507 fn sgn1' =>
adamc@76 508 S.bind2 (mfsg ctx sgn2,
adamc@76 509 fn sgn2' =>
adamc@76 510 S.map2 (mfst ctx str,
adamc@76 511 fn str' =>
adamc@76 512 (StrFun (x, n, sgn1', sgn2', str'), loc))))
adamc@76 513 | StrApp (str1, str2) =>
adamc@76 514 S.bind2 (mfst ctx str1,
adamc@76 515 fn str1' =>
adamc@76 516 S.map2 (mfst ctx str2,
adamc@76 517 fn str2' =>
adamc@76 518 (StrApp (str1', str2'), loc)))
adamc@76 519 | StrError => S.return2 strAll
adamc@76 520
adamc@76 521 and mfd ctx d acc =
adamc@76 522 S.bindP (mfd' ctx d acc, fd ctx)
adamc@76 523
adamc@76 524 and mfd' ctx (dAll as (d, loc)) =
adamc@76 525 case d of
adamc@76 526 DCon (x, n, k, c) =>
adamc@76 527 S.bind2 (mfk k,
adamc@76 528 fn k' =>
adamc@76 529 S.map2 (mfc ctx c,
adamc@76 530 fn c' =>
adamc@76 531 (DCon (x, n, k', c'), loc)))
adamc@76 532 | DVal (x, n, c, e) =>
adamc@76 533 S.bind2 (mfc ctx c,
adamc@76 534 fn c' =>
adamc@76 535 S.map2 (mfe ctx e,
adamc@76 536 fn e' =>
adamc@76 537 (DVal (x, n, c', e'), loc)))
adamc@76 538 | DSgn (x, n, sgn) =>
adamc@76 539 S.map2 (mfsg ctx sgn,
adamc@76 540 fn sgn' =>
adamc@76 541 (DSgn (x, n, sgn'), loc))
adamc@76 542 | DStr (x, n, sgn, str) =>
adamc@76 543 S.bind2 (mfsg ctx sgn,
adamc@76 544 fn sgn' =>
adamc@76 545 S.map2 (mfst ctx str,
adamc@76 546 fn str' =>
adamc@76 547 (DStr (x, n, sgn', str'), loc)))
adamc@76 548 | DFfiStr (x, n, sgn) =>
adamc@76 549 S.map2 (mfsg ctx sgn,
adamc@76 550 fn sgn' =>
adamc@76 551 (DFfiStr (x, n, sgn'), loc))
adamc@76 552 in
adamc@76 553 mfd
adamc@76 554 end
adamc@76 555
adamc@76 556 fun mapfold {kind, con, exp, sgn_item, sgn, str, decl} =
adamc@76 557 mapfoldB {kind = kind,
adamc@76 558 con = fn () => con,
adamc@76 559 exp = fn () => exp,
adamc@76 560 sgn_item = fn () => sgn_item,
adamc@76 561 sgn = fn () => sgn,
adamc@76 562 str = fn () => str,
adamc@76 563 decl = fn () => decl,
adamc@76 564 bind = fn ((), _) => ()} ()
adamc@76 565
adamc@76 566 fun exists {kind, con, exp, sgn_item, sgn, str, decl} k =
adamc@76 567 case mapfold {kind = fn k => fn () =>
adamc@76 568 if kind k then
adamc@76 569 S.Return ()
adamc@76 570 else
adamc@76 571 S.Continue (k, ()),
adamc@76 572 con = fn c => fn () =>
adamc@76 573 if con c then
adamc@76 574 S.Return ()
adamc@76 575 else
adamc@76 576 S.Continue (c, ()),
adamc@76 577 exp = fn e => fn () =>
adamc@76 578 if exp e then
adamc@76 579 S.Return ()
adamc@76 580 else
adamc@76 581 S.Continue (e, ()),
adamc@76 582 sgn_item = fn sgi => fn () =>
adamc@76 583 if sgn_item sgi then
adamc@76 584 S.Return ()
adamc@76 585 else
adamc@76 586 S.Continue (sgi, ()),
adamc@76 587 sgn = fn x => fn () =>
adamc@76 588 if sgn x then
adamc@76 589 S.Return ()
adamc@76 590 else
adamc@76 591 S.Continue (x, ()),
adamc@76 592 str = fn x => fn () =>
adamc@76 593 if str x then
adamc@76 594 S.Return ()
adamc@76 595 else
adamc@76 596 S.Continue (x, ()),
adamc@76 597 decl = fn x => fn () =>
adamc@76 598 if decl x then
adamc@76 599 S.Return ()
adamc@76 600 else
adamc@76 601 S.Continue (x, ())} k () of
adamc@76 602 S.Return _ => true
adamc@76 603 | S.Continue _ => false
adamc@76 604
adamc@76 605 fun search {kind, con, exp, sgn_item, sgn, str, decl} k =
adamc@76 606 case mapfold {kind = fn x => fn () =>
adamc@76 607 case kind x of
adamc@76 608 NONE => S.Continue (x, ())
adamc@76 609 | SOME v => S.Return v,
adamc@76 610
adamc@76 611 con = fn x => fn () =>
adamc@76 612 case con x of
adamc@76 613 NONE => S.Continue (x, ())
adamc@76 614 | SOME v => S.Return v,
adamc@76 615
adamc@76 616 exp = fn x => fn () =>
adamc@76 617 case exp x of
adamc@76 618 NONE => S.Continue (x, ())
adamc@76 619 | SOME v => S.Return v,
adamc@76 620
adamc@76 621 sgn_item = fn x => fn () =>
adamc@76 622 case sgn_item x of
adamc@76 623 NONE => S.Continue (x, ())
adamc@76 624 | SOME v => S.Return v,
adamc@76 625
adamc@76 626 sgn = fn x => fn () =>
adamc@76 627 case sgn x of
adamc@76 628 NONE => S.Continue (x, ())
adamc@76 629 | SOME v => S.Return v,
adamc@76 630
adamc@76 631 str = fn x => fn () =>
adamc@76 632 case str x of
adamc@76 633 NONE => S.Continue (x, ())
adamc@76 634 | SOME v => S.Return v,
adamc@76 635
adamc@76 636 decl = fn x => fn () =>
adamc@76 637 case decl x of
adamc@76 638 NONE => S.Continue (x, ())
adamc@76 639 | SOME v => S.Return v
adamc@76 640
adamc@76 641 } k () of
adamc@76 642 S.Return x => SOME x
adamc@76 643 | S.Continue _ => NONE
adamc@76 644
adamc@34 645 end
adamc@76 646
adamc@76 647 end