annotate src/elab_util.sml @ 79:37847b504cc6

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