annotate src/elab_util.sml @ 191:aa54250f58ac

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