annotate src/elab_util.sml @ 1302:d008c4c43a0a

Flex kinds for type-level tuples; ::_ notation
author Adam Chlipala <adam@chlipala.net>
date Sun, 10 Oct 2010 13:07:38 -0400
parents b4480a56cab7
children c7b9a33c26c8
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
adam@1302 81 | KTupleUnif (_, _, ref (SOME k)) => mfk' ctx k
adam@1302 82 | KTupleUnif (loc, nks, r) =>
adam@1302 83 S.map2 (ListUtil.mapfold (fn (n, k) =>
adam@1302 84 S.map2 (mfk ctx k,
adam@1302 85 fn k' =>
adam@1302 86 (n, k'))) nks,
adam@1302 87 fn nks' =>
adam@1302 88 (KTupleUnif (loc, nks', r), loc))
adam@1302 89
adam@1302 90
adamc@623 91 | KRel _ => S.return2 kAll
adamc@623 92 | KFun (x, k) =>
adamc@623 93 S.map2 (mfk (bind (ctx, x)) k,
adamc@623 94 fn k' =>
adamc@623 95 (KFun (x, k'), loc))
adamc@2 96 in
adamc@2 97 mfk
adamc@2 98 end
adamc@2 99
adamc@623 100 fun mapfold fk =
adamc@623 101 mapfoldB {kind = fn () => fk,
adamc@623 102 bind = fn ((), _) => ()} ()
adamc@623 103
adamc@623 104 fun mapB {kind, bind} ctx k =
adamc@623 105 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@623 106 bind = bind} ctx k () of
adamc@623 107 S.Continue (k, ()) => k
adamc@623 108 | S.Return _ => raise Fail "ElabUtil.Kind.mapB: Impossible"
adamc@623 109
adamc@2 110 fun exists f k =
adamc@6 111 case mapfold (fn k => fn () =>
adamc@6 112 if f k then
adamc@6 113 S.Return ()
adamc@6 114 else
adamc@6 115 S.Continue (k, ())) k () of
adamc@6 116 S.Return _ => true
adamc@6 117 | S.Continue _ => false
adamc@6 118
adamc@6 119 end
adamc@6 120
adamc@6 121 structure Con = struct
adamc@6 122
adamc@11 123 datatype binder =
adamc@623 124 RelK of string
adamc@623 125 | RelC of string * Elab.kind
adamc@792 126 | NamedC of string * int * Elab.kind * Elab.con option
adamc@11 127
adamc@11 128 fun mapfoldB {kind = fk, con = fc, bind} =
adamc@6 129 let
adamc@623 130 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, s) => bind (ctx, RelK s)}
adamc@6 131
adamc@11 132 fun mfc ctx c acc =
adamc@11 133 S.bindP (mfc' ctx c acc, fc ctx)
adamc@6 134
adamc@11 135 and mfc' ctx (cAll as (c, loc)) =
adamc@6 136 case c of
adamc@6 137 TFun (c1, c2) =>
adamc@11 138 S.bind2 (mfc ctx c1,
adamc@6 139 fn c1' =>
adamc@11 140 S.map2 (mfc ctx c2,
adamc@6 141 fn c2' =>
adamc@6 142 (TFun (c1', c2'), loc)))
adamc@6 143 | TCFun (e, x, k, c) =>
adamc@623 144 S.bind2 (mfk ctx k,
adamc@6 145 fn k' =>
adamc@623 146 S.map2 (mfc (bind (ctx, RelC (x, k))) c,
adamc@6 147 fn c' =>
adamc@6 148 (TCFun (e, x, k', c'), loc)))
adamc@628 149 | TDisjoint (c1, c2, c3) =>
adamc@85 150 S.bind2 (mfc ctx c1,
adamc@85 151 fn c1' =>
adamc@85 152 S.bind2 (mfc ctx c2,
adamc@85 153 fn c2' =>
adamc@85 154 S.map2 (mfc ctx c3,
adamc@85 155 fn c3' =>
adamc@628 156 (TDisjoint (c1', c2', c3'), loc))))
adamc@6 157 | TRecord c =>
adamc@11 158 S.map2 (mfc ctx c,
adamc@6 159 fn c' =>
adamc@6 160 (TRecord c', loc))
adamc@6 161
adamc@6 162 | CRel _ => S.return2 cAll
adamc@6 163 | CNamed _ => S.return2 cAll
adamc@34 164 | CModProj _ => S.return2 cAll
adamc@6 165 | CApp (c1, c2) =>
adamc@11 166 S.bind2 (mfc ctx c1,
adamc@6 167 fn c1' =>
adamc@11 168 S.map2 (mfc ctx c2,
adamc@6 169 fn c2' =>
adamc@6 170 (CApp (c1', c2'), loc)))
adamc@8 171 | CAbs (x, k, c) =>
adamc@623 172 S.bind2 (mfk ctx k,
adamc@6 173 fn k' =>
adamc@623 174 S.map2 (mfc (bind (ctx, RelC (x, k))) c,
adamc@6 175 fn c' =>
adamc@8 176 (CAbs (x, k', c'), loc)))
adamc@6 177
adamc@6 178 | CName _ => S.return2 cAll
adamc@6 179
adamc@6 180 | CRecord (k, xcs) =>
adamc@623 181 S.bind2 (mfk ctx k,
adamc@6 182 fn k' =>
adamc@6 183 S.map2 (ListUtil.mapfold (fn (x, c) =>
adamc@11 184 S.bind2 (mfc ctx x,
adamc@6 185 fn x' =>
adamc@11 186 S.map2 (mfc ctx c,
adamc@6 187 fn c' =>
adamc@6 188 (x', c'))))
adamc@6 189 xcs,
adamc@6 190 fn xcs' =>
adamc@6 191 (CRecord (k', xcs'), loc)))
adamc@6 192 | CConcat (c1, c2) =>
adamc@11 193 S.bind2 (mfc ctx c1,
adamc@6 194 fn c1' =>
adamc@11 195 S.map2 (mfc ctx c2,
adamc@6 196 fn c2' =>
adamc@6 197 (CConcat (c1', c2'), loc)))
adamc@621 198 | CMap (k1, k2) =>
adamc@623 199 S.bind2 (mfk ctx k1,
adamc@67 200 fn k1' =>
adamc@623 201 S.map2 (mfk ctx k2,
adamc@67 202 fn k2' =>
adamc@621 203 (CMap (k1', k2'), loc)))
adamc@6 204
adamc@82 205 | CUnit => S.return2 cAll
adamc@82 206
adamc@207 207 | CTuple cs =>
adamc@207 208 S.map2 (ListUtil.mapfold (mfc ctx) cs,
adamc@207 209 fn cs' =>
adamc@207 210 (CTuple cs', loc))
adamc@207 211
adamc@207 212 | CProj (c, n) =>
adamc@207 213 S.map2 (mfc ctx c,
adamc@207 214 fn c' =>
adamc@207 215 (CProj (c', n), loc))
adamc@207 216
adamc@6 217 | CError => S.return2 cAll
adamc@76 218 | CUnif (_, _, _, ref (SOME c)) => mfc' ctx c
adamc@6 219 | CUnif _ => S.return2 cAll
adam@1302 220
adamc@623 221 | CKAbs (x, c) =>
adamc@623 222 S.map2 (mfc (bind (ctx, RelK x)) c,
adamc@623 223 fn c' =>
adamc@623 224 (CKAbs (x, c'), loc))
adamc@623 225 | CKApp (c, k) =>
adamc@623 226 S.bind2 (mfc ctx c,
adamc@623 227 fn c' =>
adamc@623 228 S.map2 (mfk ctx k,
adamc@623 229 fn k' =>
adamc@623 230 (CKApp (c', k'), loc)))
adamc@623 231 | TKFun (x, c) =>
adamc@623 232 S.map2 (mfc (bind (ctx, RelK x)) c,
adamc@623 233 fn c' =>
adamc@623 234 (TKFun (x, c'), loc))
adamc@6 235 in
adamc@6 236 mfc
adamc@6 237 end
adamc@6 238
adamc@11 239 fun mapfold {kind = fk, con = fc} =
adamc@623 240 mapfoldB {kind = fn () => fk,
adamc@11 241 con = fn () => fc,
adamc@11 242 bind = fn ((), _) => ()} ()
adamc@11 243
adamc@11 244 fun mapB {kind, con, bind} ctx c =
adamc@623 245 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@11 246 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@11 247 bind = bind} ctx c () of
adamc@11 248 S.Continue (c, ()) => c
adamc@34 249 | S.Return _ => raise Fail "ElabUtil.Con.mapB: Impossible"
adamc@34 250
adamc@34 251 fun map {kind, con} s =
adamc@34 252 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@34 253 con = fn c => fn () => S.Continue (con c, ())} s () of
adamc@34 254 S.Return () => raise Fail "ElabUtil.Con.map: Impossible"
adamc@34 255 | S.Continue (s, ()) => s
adamc@11 256
adamc@711 257 fun existsB {kind, con, bind} ctx c =
adamc@711 258 case mapfoldB {kind = fn ctx => fn k => fn () =>
adamc@711 259 if kind (ctx, k) then
adamc@711 260 S.Return ()
adamc@711 261 else
adamc@711 262 S.Continue (k, ()),
adamc@711 263 con = fn ctx => fn c => fn () =>
adamc@711 264 if con (ctx, c) then
adamc@711 265 S.Return ()
adamc@711 266 else
adamc@711 267 S.Continue (c, ()),
adamc@711 268 bind = bind} ctx c () of
adamc@711 269 S.Return _ => true
adamc@711 270 | S.Continue _ => false
adamc@711 271
adamc@711 272 fun exists {kind, con} c =
adamc@6 273 case mapfold {kind = fn k => fn () =>
adamc@6 274 if kind k then
adamc@6 275 S.Return ()
adamc@6 276 else
adamc@6 277 S.Continue (k, ()),
adamc@6 278 con = fn c => fn () =>
adamc@6 279 if con c then
adamc@6 280 S.Return ()
adamc@6 281 else
adamc@711 282 S.Continue (c, ())} c () of
adamc@2 283 S.Return _ => true
adamc@2 284 | S.Continue _ => false
adamc@2 285
adamc@448 286 fun foldB {kind, con, bind} ctx st c =
adamc@623 287 case mapfoldB {kind = fn ctx => fn k => fn st => S.Continue (k, kind (ctx, k, st)),
adamc@448 288 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
adamc@448 289 bind = bind} ctx c st of
adamc@448 290 S.Continue (_, st) => st
adamc@448 291 | S.Return _ => raise Fail "ElabUtil.Con.foldB: Impossible"
adamc@448 292
adamc@839 293 fun fold {kind, con} st c =
adamc@839 294 case mapfoldB {kind = fn () => fn k => fn st => S.Continue (k, kind (k, st)),
adamc@839 295 con = fn () => fn c => fn st => S.Continue (c, con (c, st)),
adamc@839 296 bind = fn ((), _) => ()} () c st of
adamc@839 297 S.Continue (_, st) => st
adamc@839 298 | S.Return _ => raise Fail "ElabUtil.Con.fold: Impossible"
adamc@839 299
adamc@2 300 end
adamc@2 301
adamc@10 302 structure Exp = struct
adamc@10 303
adamc@11 304 datatype binder =
adamc@623 305 RelK of string
adamc@623 306 | RelC of string * Elab.kind
adamc@792 307 | NamedC of string * int * Elab.kind * Elab.con option
adamc@11 308 | RelE of string * Elab.con
adamc@11 309 | NamedE of string * Elab.con
adamc@11 310
adamc@11 311 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@10 312 let
adamc@623 313 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@10 314
adamc@11 315 fun bind' (ctx, b) =
adamc@11 316 let
adamc@11 317 val b' = case b of
adamc@623 318 Con.RelK x => RelK x
adamc@623 319 | Con.RelC x => RelC x
adamc@623 320 | Con.NamedC x => NamedC x
adamc@11 321 in
adamc@11 322 bind (ctx, b')
adamc@11 323 end
adamc@11 324 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@10 325
adamc@825 326 fun doVars ((p, _), ctx) =
adamc@825 327 case p of
adamc@825 328 PWild => ctx
adamc@825 329 | PVar xt => bind (ctx, RelE xt)
adamc@825 330 | PPrim _ => ctx
adamc@825 331 | PCon (_, _, _, NONE) => ctx
adamc@825 332 | PCon (_, _, _, SOME p) => doVars (p, ctx)
adamc@825 333 | PRecord xpcs =>
adamc@825 334 foldl (fn ((_, p, _), ctx) => doVars (p, ctx))
adamc@825 335 ctx xpcs
adamc@825 336
adamc@11 337 fun mfe ctx e acc =
adamc@11 338 S.bindP (mfe' ctx e acc, fe ctx)
adamc@11 339
adamc@11 340 and mfe' ctx (eAll as (e, loc)) =
adamc@10 341 case e of
adamc@14 342 EPrim _ => S.return2 eAll
adamc@14 343 | ERel _ => S.return2 eAll
adamc@10 344 | ENamed _ => S.return2 eAll
adamc@34 345 | EModProj _ => S.return2 eAll
adamc@10 346 | EApp (e1, e2) =>
adamc@11 347 S.bind2 (mfe ctx e1,
adamc@10 348 fn e1' =>
adamc@11 349 S.map2 (mfe ctx e2,
adamc@10 350 fn e2' =>
adamc@10 351 (EApp (e1', e2'), loc)))
adamc@26 352 | EAbs (x, dom, ran, e) =>
adamc@26 353 S.bind2 (mfc ctx dom,
adamc@26 354 fn dom' =>
adamc@26 355 S.bind2 (mfc ctx ran,
adamc@26 356 fn ran' =>
adamc@26 357 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 358 fn e' =>
adamc@26 359 (EAbs (x, dom', ran', e'), loc))))
adamc@26 360
adamc@10 361 | ECApp (e, c) =>
adamc@11 362 S.bind2 (mfe ctx e,
adamc@10 363 fn e' =>
adamc@11 364 S.map2 (mfc ctx c,
adamc@10 365 fn c' =>
adamc@10 366 (ECApp (e', c'), loc)))
adamc@10 367 | ECAbs (expl, x, k, e) =>
adamc@623 368 S.bind2 (mfk ctx k,
adamc@10 369 fn k' =>
adamc@11 370 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@10 371 fn e' =>
adamc@10 372 (ECAbs (expl, x, k', e'), loc)))
adamc@10 373
adamc@12 374 | ERecord xes =>
adamc@29 375 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@12 376 S.bind2 (mfc ctx x,
adamc@12 377 fn x' =>
adamc@29 378 S.bind2 (mfe ctx e,
adamc@12 379 fn e' =>
adamc@29 380 S.map2 (mfc ctx t,
adamc@29 381 fn t' =>
adamc@29 382 (x', e', t')))))
adamc@12 383 xes,
adamc@12 384 fn xes' =>
adamc@12 385 (ERecord xes', loc))
adamc@12 386 | EField (e, c, {field, rest}) =>
adamc@12 387 S.bind2 (mfe ctx e,
adamc@12 388 fn e' =>
adamc@12 389 S.bind2 (mfc ctx c,
adamc@12 390 fn c' =>
adamc@12 391 S.bind2 (mfc ctx field,
adamc@12 392 fn field' =>
adamc@12 393 S.map2 (mfc ctx rest,
adamc@12 394 fn rest' =>
adamc@12 395 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@445 396 | EConcat (e1, c1, e2, c2) =>
adamc@339 397 S.bind2 (mfe ctx e1,
adamc@339 398 fn e1' =>
adamc@445 399 S.bind2 (mfc ctx c1,
adamc@445 400 fn c1' =>
adamc@339 401 S.bind2 (mfe ctx e2,
adamc@339 402 fn e2' =>
adamc@445 403 S.map2 (mfc ctx c2,
adamc@445 404 fn c2' =>
adamc@445 405 (EConcat (e1', c1', e2', c2'),
adamc@445 406 loc)))))
adamc@149 407 | ECut (e, c, {field, rest}) =>
adamc@149 408 S.bind2 (mfe ctx e,
adamc@149 409 fn e' =>
adamc@149 410 S.bind2 (mfc ctx c,
adamc@149 411 fn c' =>
adamc@149 412 S.bind2 (mfc ctx field,
adamc@149 413 fn field' =>
adamc@149 414 S.map2 (mfc ctx rest,
adamc@149 415 fn rest' =>
adamc@149 416 (ECut (e', c', {field = field', rest = rest'}), loc)))))
adamc@12 417
adamc@493 418 | ECutMulti (e, c, {rest}) =>
adamc@493 419 S.bind2 (mfe ctx e,
adamc@493 420 fn e' =>
adamc@493 421 S.bind2 (mfc ctx c,
adamc@493 422 fn c' =>
adamc@493 423 S.map2 (mfc ctx rest,
adamc@493 424 fn rest' =>
adamc@493 425 (ECutMulti (e', c', {rest = rest'}), loc))))
adamc@493 426
adamc@182 427 | ECase (e, pes, {disc, result}) =>
adamc@171 428 S.bind2 (mfe ctx e,
adamc@171 429 fn e' =>
adamc@171 430 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@448 431 let
adamc@448 432 fun pb ((p, _), ctx) =
adamc@448 433 case p of
adamc@448 434 PWild => ctx
adamc@448 435 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@448 436 | PPrim _ => ctx
adamc@448 437 | PCon (_, _, _, NONE) => ctx
adamc@448 438 | PCon (_, _, _, SOME p) => pb (p, ctx)
adamc@448 439 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@448 440 pb (p, ctx)) ctx xps
adamc@448 441 in
adamc@1272 442 S.bind2 (mfp ctx p,
adamc@1272 443 fn p' =>
adamc@1272 444 S.map2 (mfe (pb (p', ctx)) e,
adamc@1272 445 fn e' => (p', e')))
adamc@448 446 end) pes,
adamc@171 447 fn pes' =>
adamc@182 448 S.bind2 (mfc ctx disc,
adamc@182 449 fn disc' =>
adamc@182 450 S.map2 (mfc ctx result,
adamc@182 451 fn result' =>
adamc@182 452 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@171 453
adamc@10 454 | EError => S.return2 eAll
adamc@228 455 | EUnif (ref (SOME e)) => mfe ctx e
adamc@228 456 | EUnif _ => S.return2 eAll
adamc@447 457
adamc@825 458 | ELet (des, e, t) =>
adamc@447 459 let
adamc@826 460 val (des, ctx') = foldl (fn (ed, (des, ctx)) =>
adamc@826 461 let
adamc@826 462 val ctx' =
adamc@826 463 case #1 ed of
adamc@826 464 EDVal (p, _, _) => doVars (p, ctx)
adamc@826 465 | EDValRec vis =>
adamc@826 466 foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t)))
adamc@826 467 ctx vis
adamc@826 468 in
adamc@826 469 (S.bind2 (des,
adamc@826 470 fn des' =>
adamc@826 471 S.map2 (mfed ctx ed,
adamc@826 472 fn ed' => ed' :: des')),
adamc@826 473 ctx')
adamc@826 474 end)
adamc@447 475 (S.return2 [], ctx) des
adamc@447 476 in
adamc@447 477 S.bind2 (des,
adamc@447 478 fn des' =>
adamc@826 479 S.bind2 (mfe ctx' e,
adamc@447 480 fn e' =>
adamc@825 481 S.map2 (mfc ctx t,
adamc@825 482 fn t' =>
adamc@826 483 (ELet (rev des', e', t'), loc))))
adamc@447 484 end
adamc@447 485
adamc@623 486 | EKAbs (x, e) =>
adamc@623 487 S.map2 (mfe (bind (ctx, RelK x)) e,
adamc@623 488 fn e' =>
adamc@623 489 (EKAbs (x, e'), loc))
adamc@623 490 | EKApp (e, k) =>
adamc@623 491 S.bind2 (mfe ctx e,
adamc@623 492 fn e' =>
adamc@623 493 S.map2 (mfk ctx k,
adamc@623 494 fn k' =>
adamc@623 495 (EKApp (e', k'), loc)))
adamc@623 496
adamc@1272 497 and mfp ctx (pAll as (p, loc)) =
adamc@1272 498 case p of
adamc@1272 499 PWild => S.return2 pAll
adamc@1272 500 | PVar (x, t) =>
adamc@1272 501 S.map2 (mfc ctx t,
adamc@1272 502 fn t' =>
adamc@1272 503 (PVar (x, t'), loc))
adamc@1272 504 | PPrim _ => S.return2 pAll
adamc@1272 505 | PCon (dk, pc, args, po) =>
adamc@1272 506 S.bind2 (ListUtil.mapfold (mfc ctx) args,
adamc@1272 507 fn args' =>
adamc@1272 508 S.map2 ((case po of
adamc@1272 509 NONE => S.return2 NONE
adamc@1272 510 | SOME p => S.map2 (mfp ctx p, SOME)),
adamc@1272 511 fn po' =>
adamc@1272 512 (PCon (dk, pc, args', po'), loc)))
adamc@1272 513 | PRecord xps =>
adamc@1272 514 S.map2 (ListUtil.mapfold (fn (x, p, c) =>
adamc@1272 515 S.bind2 (mfp ctx p,
adamc@1272 516 fn p' =>
adamc@1272 517 S.map2 (mfc ctx c,
adamc@1272 518 fn c' =>
adamc@1272 519 (x, p', c')))) xps,
adamc@1272 520 fn xps' =>
adamc@1272 521 (PRecord xps', loc))
adamc@1272 522
adamc@447 523 and mfed ctx (dAll as (d, loc)) =
adamc@447 524 case d of
adamc@825 525 EDVal (p, t, e) =>
adamc@825 526 S.bind2 (mfc ctx t,
adamc@825 527 fn t' =>
adamc@826 528 S.map2 (mfe ctx e,
adamc@825 529 fn e' =>
adamc@825 530 (EDVal (p, t', e'), loc)))
adamc@447 531 | EDValRec vis =>
adamc@447 532 let
adamc@453 533 val ctx = foldl (fn ((x, t, _), ctx) => bind (ctx, RelE (x, t))) ctx vis
adamc@447 534 in
adamc@447 535 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@447 536 fn vis' =>
adamc@447 537 (EDValRec vis', loc))
adamc@447 538 end
adamc@447 539
adamc@447 540 and mfvi ctx (x, c, e) =
adamc@447 541 S.bind2 (mfc ctx c,
adamc@447 542 fn c' =>
adamc@447 543 S.map2 (mfe ctx e,
adamc@447 544 fn e' =>
adamc@447 545 (x, c', e')))
adamc@10 546 in
adamc@10 547 mfe
adamc@10 548 end
adamc@10 549
adamc@11 550 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@623 551 mapfoldB {kind = fn () => fk,
adamc@11 552 con = fn () => fc,
adamc@11 553 exp = fn () => fe,
adamc@11 554 bind = fn ((), _) => ()} ()
adamc@11 555
adamc@10 556 fun exists {kind, con, exp} k =
adamc@10 557 case mapfold {kind = fn k => fn () =>
adamc@10 558 if kind k then
adamc@10 559 S.Return ()
adamc@10 560 else
adamc@10 561 S.Continue (k, ()),
adamc@10 562 con = fn c => fn () =>
adamc@10 563 if con c then
adamc@10 564 S.Return ()
adamc@10 565 else
adamc@10 566 S.Continue (c, ()),
adamc@10 567 exp = fn e => fn () =>
adamc@10 568 if exp e then
adamc@10 569 S.Return ()
adamc@10 570 else
adamc@10 571 S.Continue (e, ())} k () of
adamc@10 572 S.Return _ => true
adamc@10 573 | S.Continue _ => false
adamc@10 574
adamc@211 575 fun mapB {kind, con, exp, bind} ctx e =
adamc@623 576 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@211 577 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@211 578 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@211 579 bind = bind} ctx e () of
adamc@211 580 S.Continue (e, ()) => e
adamc@211 581 | S.Return _ => raise Fail "ElabUtil.Exp.mapB: Impossible"
adamc@211 582
adamc@448 583 fun foldB {kind, con, exp, bind} ctx st e =
adamc@623 584 case mapfoldB {kind = fn ctx => fn k => fn st => S.Continue (k, kind (ctx, k, st)),
adamc@448 585 con = fn ctx => fn c => fn st => S.Continue (c, con (ctx, c, st)),
adamc@448 586 exp = fn ctx => fn e => fn st => S.Continue (e, exp (ctx, e, st)),
adamc@448 587 bind = bind} ctx e st of
adamc@448 588 S.Continue (_, st) => st
adamc@448 589 | S.Return _ => raise Fail "ElabUtil.Exp.foldB: Impossible"
adamc@448 590
adamc@10 591 end
adamc@10 592
adamc@34 593 structure Sgn = struct
adamc@34 594
adamc@34 595 datatype binder =
adamc@623 596 RelK of string
adamc@623 597 | RelC of string * Elab.kind
adamc@792 598 | NamedC of string * int * Elab.kind * Elab.con option
adamc@792 599 | Str of string * int * Elab.sgn
adamc@792 600 | Sgn of string * int * Elab.sgn
adamc@34 601
adamc@34 602 fun mapfoldB {kind, con, sgn_item, sgn, bind} =
adamc@34 603 let
adamc@34 604 fun bind' (ctx, b) =
adamc@34 605 let
adamc@34 606 val b' = case b of
adamc@623 607 Con.RelK x => RelK x
adamc@623 608 | Con.RelC x => RelC x
adamc@623 609 | Con.NamedC x => NamedC x
adamc@34 610 in
adamc@34 611 bind (ctx, b')
adamc@34 612 end
adamc@34 613 val con = Con.mapfoldB {kind = kind, con = con, bind = bind'}
adamc@34 614
adamc@623 615 val kind = Kind.mapfoldB {kind = kind, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@34 616
adamc@34 617 fun sgi ctx si acc =
adamc@34 618 S.bindP (sgi' ctx si acc, sgn_item ctx)
adamc@34 619
adamc@156 620 and sgi' ctx (siAll as (si, loc)) =
adamc@34 621 case si of
adamc@34 622 SgiConAbs (x, n, k) =>
adamc@623 623 S.map2 (kind ctx k,
adamc@34 624 fn k' =>
adamc@34 625 (SgiConAbs (x, n, k'), loc))
adamc@34 626 | SgiCon (x, n, k, c) =>
adamc@623 627 S.bind2 (kind ctx k,
adamc@34 628 fn k' =>
adamc@34 629 S.map2 (con ctx c,
adamc@34 630 fn c' =>
adamc@34 631 (SgiCon (x, n, k', c'), loc)))
adamc@805 632 | SgiDatatype dts =>
adamc@805 633 S.map2 (ListUtil.mapfold (fn (x, n, xs, xncs) =>
adamc@805 634 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@805 635 case c of
adamc@805 636 NONE => S.return2 (x, n, c)
adamc@805 637 | SOME c =>
adamc@805 638 S.map2 (con ctx c,
adamc@805 639 fn c' => (x, n, SOME c'))) xncs,
adamc@805 640 fn xncs' => (x, n, xs, xncs'))) dts,
adamc@805 641 fn dts' =>
adamc@805 642 (SgiDatatype dts', loc))
adamc@191 643 | SgiDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
adamc@162 644 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@162 645 case c of
adamc@162 646 NONE => S.return2 (x, n, c)
adamc@162 647 | SOME c =>
adamc@162 648 S.map2 (con ctx c,
adamc@162 649 fn c' => (x, n, SOME c'))) xncs,
adamc@162 650 fn xncs' =>
adamc@191 651 (SgiDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
adamc@34 652 | SgiVal (x, n, c) =>
adamc@34 653 S.map2 (con ctx c,
adamc@34 654 fn c' =>
adamc@34 655 (SgiVal (x, n, c'), loc))
adamc@34 656 | SgiStr (x, n, s) =>
adamc@34 657 S.map2 (sg ctx s,
adamc@34 658 fn s' =>
adamc@34 659 (SgiStr (x, n, s'), loc))
adamc@59 660 | SgiSgn (x, n, s) =>
adamc@59 661 S.map2 (sg ctx s,
adamc@59 662 fn s' =>
adamc@59 663 (SgiSgn (x, n, s'), loc))
adamc@88 664 | SgiConstraint (c1, c2) =>
adamc@88 665 S.bind2 (con ctx c1,
adamc@88 666 fn c1' =>
adamc@88 667 S.map2 (con ctx c2,
adamc@88 668 fn c2' =>
adamc@88 669 (SgiConstraint (c1', c2'), loc)))
adamc@563 670 | SgiClassAbs (x, n, k) =>
adamc@623 671 S.map2 (kind ctx k,
adamc@563 672 fn k' =>
adamc@563 673 (SgiClassAbs (x, n, k'), loc))
adamc@563 674 | SgiClass (x, n, k, c) =>
adamc@623 675 S.bind2 (kind ctx k,
adamc@563 676 fn k' =>
adamc@563 677 S.map2 (con ctx c,
adamc@563 678 fn c' =>
adamc@563 679 (SgiClass (x, n, k', c'), loc)))
adamc@34 680
adamc@34 681 and sg ctx s acc =
adamc@34 682 S.bindP (sg' ctx s acc, sgn ctx)
adamc@34 683
adamc@34 684 and sg' ctx (sAll as (s, loc)) =
adamc@34 685 case s of
adamc@34 686 SgnConst sgis =>
adamc@34 687 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
adamc@34 688 (case #1 si of
adamc@329 689 SgiConAbs (x, n, k) =>
adamc@792 690 bind (ctx, NamedC (x, n, k, NONE))
adamc@792 691 | SgiCon (x, n, k, c) =>
adamc@792 692 bind (ctx, NamedC (x, n, k, SOME c))
adamc@805 693 | SgiDatatype dts =>
adamc@805 694 foldl (fn ((x, n, ks, _), ctx) =>
adamc@805 695 let
adamc@805 696 val k' = (KType, loc)
adamc@805 697 val k = foldl (fn (_, k) => (KArrow (k', k), loc))
adamc@805 698 k' ks
adamc@805 699 in
adamc@805 700 bind (ctx, NamedC (x, n, k, NONE))
adamc@805 701 end) ctx dts
adamc@792 702 | SgiDatatypeImp (x, n, m1, ms, s, _, _) =>
adamc@792 703 bind (ctx, NamedC (x, n, (KType, loc),
adamc@792 704 SOME (CModProj (m1, ms, s), loc)))
adamc@34 705 | SgiVal _ => ctx
adamc@792 706 | SgiStr (x, n, sgn) =>
adamc@792 707 bind (ctx, Str (x, n, sgn))
adamc@792 708 | SgiSgn (x, n, sgn) =>
adamc@792 709 bind (ctx, Sgn (x, n, sgn))
adamc@203 710 | SgiConstraint _ => ctx
adamc@563 711 | SgiClassAbs (x, n, k) =>
adamc@792 712 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), NONE))
adamc@792 713 | SgiClass (x, n, k, c) =>
adamc@792 714 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), SOME c)),
adamc@34 715 sgi ctx si)) ctx sgis,
adamc@34 716 fn sgis' =>
adamc@34 717 (SgnConst sgis', loc))
adamc@34 718
adamc@34 719 | SgnVar _ => S.return2 sAll
adamc@41 720 | SgnFun (m, n, s1, s2) =>
adamc@41 721 S.bind2 (sg ctx s1,
adamc@41 722 fn s1' =>
adamc@792 723 S.map2 (sg (bind (ctx, Str (m, n, s1'))) s2,
adamc@41 724 fn s2' =>
adamc@41 725 (SgnFun (m, n, s1', s2'), loc)))
adamc@59 726 | SgnProj _ => S.return2 sAll
adamc@42 727 | SgnWhere (sgn, x, c) =>
adamc@42 728 S.bind2 (sg ctx sgn,
adamc@42 729 fn sgn' =>
adamc@42 730 S.map2 (con ctx c,
adamc@42 731 fn c' =>
adamc@42 732 (SgnWhere (sgn', x, c'), loc)))
adamc@34 733 | SgnError => S.return2 sAll
adamc@34 734 in
adamc@34 735 sg
adamc@34 736 end
adamc@34 737
adamc@34 738 fun mapfold {kind, con, sgn_item, sgn} =
adamc@623 739 mapfoldB {kind = fn () => kind,
adamc@34 740 con = fn () => con,
adamc@34 741 sgn_item = fn () => sgn_item,
adamc@34 742 sgn = fn () => sgn,
adamc@34 743 bind = fn ((), _) => ()} ()
adamc@34 744
adamc@792 745 fun mapB {kind, con, sgn_item, sgn, bind} ctx s =
adamc@792 746 case mapfoldB {kind = fn ctx => fn k => fn () => S.Continue (kind ctx k, ()),
adamc@792 747 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@792 748 sgn_item = fn ctx => fn sgi => fn () => S.Continue (sgn_item ctx sgi, ()),
adamc@792 749 sgn = fn ctx => fn s => fn () => S.Continue (sgn ctx s, ()),
adamc@792 750 bind = bind} ctx s () of
adamc@792 751 S.Continue (s, ()) => s
adamc@792 752 | S.Return _ => raise Fail "ElabUtil.Sgn.mapB: Impossible"
adamc@792 753
adamc@34 754 fun map {kind, con, sgn_item, sgn} s =
adamc@34 755 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@34 756 con = fn c => fn () => S.Continue (con c, ()),
adamc@34 757 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
adamc@34 758 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
adamc@34 759 S.Return () => raise Fail "Elab_util.Sgn.map"
adamc@34 760 | S.Continue (s, ()) => s
adamc@34 761
adamc@2 762 end
adamc@34 763
adamc@76 764 structure Decl = struct
adamc@76 765
adamc@76 766 datatype binder =
adamc@623 767 RelK of string
adamc@623 768 | RelC of string * Elab.kind
adamc@792 769 | NamedC of string * int * Elab.kind * Elab.con option
adamc@76 770 | RelE of string * Elab.con
adamc@76 771 | NamedE of string * Elab.con
adamc@76 772 | Str of string * Elab.sgn
adamc@76 773 | Sgn of string * Elab.sgn
adamc@76 774
adamc@76 775 fun mapfoldB {kind = fk, con = fc, exp = fe, sgn_item = fsgi, sgn = fsg, str = fst, decl = fd, bind} =
adamc@76 776 let
adamc@623 777 val mfk = Kind.mapfoldB {kind = fk, bind = fn (ctx, x) => bind (ctx, RelK x)}
adamc@76 778
adamc@76 779 fun bind' (ctx, b) =
adamc@76 780 let
adamc@76 781 val b' = case b of
adamc@623 782 Con.RelK x => RelK x
adamc@623 783 | Con.RelC x => RelC x
adamc@623 784 | Con.NamedC x => NamedC x
adamc@76 785 in
adamc@76 786 bind (ctx, b')
adamc@76 787 end
adamc@76 788 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@76 789
adamc@76 790 fun bind' (ctx, b) =
adamc@76 791 let
adamc@76 792 val b' = case b of
adamc@623 793 Exp.RelK x => RelK x
adamc@623 794 | Exp.RelC x => RelC x
adamc@76 795 | Exp.NamedC x => NamedC x
adamc@76 796 | Exp.RelE x => RelE x
adamc@76 797 | Exp.NamedE x => NamedE x
adamc@76 798 in
adamc@76 799 bind (ctx, b')
adamc@76 800 end
adamc@76 801 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind'}
adamc@76 802
adamc@76 803 fun bind' (ctx, b) =
adamc@76 804 let
adamc@76 805 val b' = case b of
adamc@623 806 Sgn.RelK x => RelK x
adamc@623 807 | Sgn.RelC x => RelC x
adamc@76 808 | Sgn.NamedC x => NamedC x
adamc@792 809 | Sgn.Sgn (x, _, y) => Sgn (x, y)
adamc@792 810 | Sgn.Str (x, _, y) => Str (x, y)
adamc@76 811 in
adamc@76 812 bind (ctx, b')
adamc@76 813 end
adamc@76 814 val mfsg = Sgn.mapfoldB {kind = fk, con = fc, sgn_item = fsgi, sgn = fsg, bind = bind'}
adamc@76 815
adamc@76 816 fun mfst ctx str acc =
adamc@76 817 S.bindP (mfst' ctx str acc, fst ctx)
adamc@76 818
adamc@76 819 and mfst' ctx (strAll as (str, loc)) =
adamc@76 820 case str of
adamc@76 821 StrConst ds =>
adamc@76 822 S.map2 (ListUtil.mapfoldB (fn (ctx, d) =>
adamc@76 823 (case #1 d of
adamc@792 824 DCon (x, n, k, c) =>
adamc@792 825 bind (ctx, NamedC (x, n, k, SOME c))
adamc@805 826 | DDatatype dts =>
adamc@156 827 let
adamc@805 828 fun doOne ((x, n, xs, xncs), ctx) =
adamc@805 829 let
adamc@805 830 val ctx = bind (ctx, NamedC (x, n, (KType, loc), NONE))
adamc@805 831 in
adamc@805 832 foldl (fn ((x, _, co), ctx) =>
adamc@805 833 let
adamc@805 834 val t =
adamc@805 835 case co of
adamc@805 836 NONE => CNamed n
adamc@805 837 | SOME t => TFun (t, (CNamed n, loc))
adamc@805 838
adamc@805 839 val k = (KType, loc)
adamc@805 840 val t = (t, loc)
adamc@805 841 val t = foldr (fn (x, t) =>
adamc@805 842 (TCFun (Explicit,
adamc@805 843 x,
adamc@805 844 k,
adamc@805 845 t), loc))
adamc@805 846 t xs
adamc@805 847 in
adamc@805 848 bind (ctx, NamedE (x, t))
adamc@805 849 end)
adamc@805 850 ctx xncs
adamc@805 851 end
adamc@156 852 in
adamc@805 853 foldl doOne ctx dts
adamc@156 854 end
adamc@191 855 | DDatatypeImp (x, n, m, ms, x', _, _) =>
adamc@792 856 bind (ctx, NamedC (x, n, (KType, loc),
adamc@792 857 SOME (CModProj (m, ms, x'), loc)))
adamc@76 858 | DVal (x, _, c, _) =>
adamc@76 859 bind (ctx, NamedE (x, c))
adamc@123 860 | DValRec vis =>
adamc@123 861 foldl (fn ((x, _, c, _), ctx) => bind (ctx, NamedE (x, c))) ctx vis
adamc@76 862 | DSgn (x, _, sgn) =>
adamc@76 863 bind (ctx, Sgn (x, sgn))
adamc@76 864 | DStr (x, _, sgn, _) =>
adamc@76 865 bind (ctx, Str (x, sgn))
adamc@76 866 | DFfiStr (x, _, sgn) =>
adamc@88 867 bind (ctx, Str (x, sgn))
adamc@100 868 | DConstraint _ => ctx
adamc@203 869 | DExport _ => ctx
adamc@707 870 | DTable (tn, x, n, c, _, pc, _, cc) =>
adamc@705 871 let
adamc@705 872 val ct = (CModProj (n, [], "sql_table"), loc)
adamc@705 873 val ct = (CApp (ct, c), loc)
adamc@707 874 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
adamc@705 875 in
adamc@705 876 bind (ctx, NamedE (x, ct))
adamc@705 877 end
adamc@338 878 | DSequence (tn, x, n) =>
adamc@338 879 bind (ctx, NamedE (x, (CModProj (n, [], "sql_sequence"), loc)))
adamc@754 880 | DView (tn, x, n, _, c) =>
adamc@754 881 let
adamc@754 882 val ct = (CModProj (n, [], "sql_view"), loc)
adamc@754 883 val ct = (CApp (ct, c), loc)
adamc@754 884 in
adamc@754 885 bind (ctx, NamedE (x, ct))
adamc@754 886 end
adamc@792 887 | DClass (x, n, k, c) =>
adamc@792 888 bind (ctx, NamedC (x, n, (KArrow (k, (KType, loc)), loc), SOME c))
adamc@459 889 | DDatabase _ => ctx
adamc@459 890 | DCookie (tn, x, n, c) =>
adamc@459 891 bind (ctx, NamedE (x, (CApp ((CModProj (n, [], "cookie"), loc),
adamc@718 892 c), loc)))
adamc@720 893 | DStyle (tn, x, n) =>
adamc@1073 894 bind (ctx, NamedE (x, (CModProj (n, [], "css_class"), loc)))
adamc@1199 895 | DTask _ => ctx
adam@1294 896 | DPolicy _ => ctx
adam@1294 897 | DOnError _ => ctx,
adamc@76 898 mfd ctx d)) ctx ds,
adamc@76 899 fn ds' => (StrConst ds', loc))
adamc@76 900 | StrVar _ => S.return2 strAll
adamc@76 901 | StrProj (str, x) =>
adamc@76 902 S.map2 (mfst ctx str,
adamc@76 903 fn str' =>
adamc@76 904 (StrProj (str', x), loc))
adamc@76 905 | StrFun (x, n, sgn1, sgn2, str) =>
adamc@76 906 S.bind2 (mfsg ctx sgn1,
adamc@76 907 fn sgn1' =>
adamc@76 908 S.bind2 (mfsg ctx sgn2,
adamc@76 909 fn sgn2' =>
adamc@76 910 S.map2 (mfst ctx str,
adamc@76 911 fn str' =>
adamc@76 912 (StrFun (x, n, sgn1', sgn2', str'), loc))))
adamc@76 913 | StrApp (str1, str2) =>
adamc@76 914 S.bind2 (mfst ctx str1,
adamc@76 915 fn str1' =>
adamc@76 916 S.map2 (mfst ctx str2,
adamc@76 917 fn str2' =>
adamc@76 918 (StrApp (str1', str2'), loc)))
adamc@76 919 | StrError => S.return2 strAll
adamc@76 920
adamc@76 921 and mfd ctx d acc =
adamc@76 922 S.bindP (mfd' ctx d acc, fd ctx)
adamc@76 923
adamc@76 924 and mfd' ctx (dAll as (d, loc)) =
adamc@76 925 case d of
adamc@76 926 DCon (x, n, k, c) =>
adamc@623 927 S.bind2 (mfk ctx k,
adamc@76 928 fn k' =>
adamc@76 929 S.map2 (mfc ctx c,
adamc@76 930 fn c' =>
adamc@76 931 (DCon (x, n, k', c'), loc)))
adamc@805 932 | DDatatype dts =>
adamc@805 933 S.map2 (ListUtil.mapfold (fn (x, n, xs, xncs) =>
adamc@805 934 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@805 935 case c of
adamc@805 936 NONE => S.return2 (x, n, c)
adamc@805 937 | SOME c =>
adamc@805 938 S.map2 (mfc ctx c,
adamc@805 939 fn c' => (x, n, SOME c'))) xncs,
adamc@805 940 fn xncs' =>
adamc@805 941 (x, n, xs, xncs'))) dts,
adamc@805 942 fn dts' =>
adamc@805 943 (DDatatype dts', loc))
adamc@191 944 | DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
adamc@162 945 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@162 946 case c of
adamc@162 947 NONE => S.return2 (x, n, c)
adamc@162 948 | SOME c =>
adamc@162 949 S.map2 (mfc ctx c,
adamc@162 950 fn c' => (x, n, SOME c'))) xncs,
adamc@162 951 fn xncs' =>
adamc@191 952 (DDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
adamc@123 953 | DVal vi =>
adamc@123 954 S.map2 (mfvi ctx vi,
adamc@123 955 fn vi' =>
adamc@123 956 (DVal vi', loc))
adamc@123 957 | DValRec vis =>
adamc@123 958 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
adamc@123 959 fn vis' =>
adamc@123 960 (DValRec vis', loc))
adamc@76 961 | DSgn (x, n, sgn) =>
adamc@76 962 S.map2 (mfsg ctx sgn,
adamc@76 963 fn sgn' =>
adamc@76 964 (DSgn (x, n, sgn'), loc))
adamc@76 965 | DStr (x, n, sgn, str) =>
adamc@76 966 S.bind2 (mfsg ctx sgn,
adamc@76 967 fn sgn' =>
adamc@76 968 S.map2 (mfst ctx str,
adamc@76 969 fn str' =>
adamc@76 970 (DStr (x, n, sgn', str'), loc)))
adamc@76 971 | DFfiStr (x, n, sgn) =>
adamc@76 972 S.map2 (mfsg ctx sgn,
adamc@76 973 fn sgn' =>
adamc@76 974 (DFfiStr (x, n, sgn'), loc))
adamc@88 975 | DConstraint (c1, c2) =>
adamc@88 976 S.bind2 (mfc ctx c1,
adamc@88 977 fn c1' =>
adamc@88 978 S.map2 (mfc ctx c2,
adamc@88 979 fn c2' =>
adamc@88 980 (DConstraint (c1', c2'), loc)))
adamc@109 981 | DExport (en, sgn, str) =>
adamc@109 982 S.bind2 (mfsg ctx sgn,
adamc@109 983 fn sgn' =>
adamc@109 984 S.map2 (mfst ctx str,
adamc@109 985 fn str' =>
adamc@109 986 (DExport (en, sgn', str'), loc)))
adamc@123 987
adamc@707 988 | DTable (tn, x, n, c, pe, pc, ce, cc) =>
adamc@704 989 S.bind2 (mfc ctx c,
adamc@203 990 fn c' =>
adamc@707 991 S.bind2 (mfe ctx pe,
adamc@707 992 fn pe' =>
adamc@707 993 S.bind2 (mfc ctx pc,
adamc@707 994 fn pc' =>
adamc@707 995 S.bind2 (mfe ctx ce,
adamc@707 996 fn ce' =>
adamc@707 997 S.map2 (mfc ctx cc,
adamc@707 998 fn cc' =>
adamc@707 999 (DTable (tn, x, n, c', pe', pc', ce', cc'), loc))))))
adamc@338 1000 | DSequence _ => S.return2 dAll
adamc@754 1001 | DView (tn, x, n, e, c) =>
adamc@754 1002 S.bind2 (mfe ctx e,
adamc@754 1003 fn e' =>
adamc@754 1004 S.map2 (mfc ctx c,
adamc@754 1005 fn c' =>
adamc@754 1006 (DView (tn, x, n, e', c'), loc)))
adamc@203 1007
adamc@563 1008 | DClass (x, n, k, c) =>
adamc@623 1009 S.bind2 (mfk ctx k,
adamc@563 1010 fn k' =>
adamc@563 1011 S.map2 (mfc ctx c,
adamc@563 1012 fn c' =>
adamc@563 1013 (DClass (x, n, k', c'), loc)))
adamc@213 1014
adamc@338 1015 | DDatabase _ => S.return2 dAll
adamc@271 1016
adamc@459 1017 | DCookie (tn, x, n, c) =>
adamc@459 1018 S.map2 (mfc ctx c,
adamc@459 1019 fn c' =>
adamc@459 1020 (DCookie (tn, x, n, c'), loc))
adamc@720 1021 | DStyle _ => S.return2 dAll
adamc@1075 1022 | DTask (e1, e2) =>
adamc@1075 1023 S.bind2 (mfe ctx e1,
adamc@1075 1024 fn e1' =>
adamc@1075 1025 S.map2 (mfe ctx e2,
adamc@1075 1026 fn e2' =>
adamc@1075 1027 (DTask (e1', e2'), loc)))
adamc@1199 1028 | DPolicy e1 =>
adamc@1199 1029 S.map2 (mfe ctx e1,
adamc@1199 1030 fn e1' =>
adamc@1199 1031 (DPolicy e1', loc))
adam@1294 1032 | DOnError _ => S.return2 dAll
adamc@459 1033
adamc@123 1034 and mfvi ctx (x, n, c, e) =
adamc@123 1035 S.bind2 (mfc ctx c,
adamc@123 1036 fn c' =>
adamc@123 1037 S.map2 (mfe ctx e,
adamc@123 1038 fn e' =>
adamc@123 1039 (x, n, c', e')))
adamc@76 1040 in
adamc@76 1041 mfd
adamc@76 1042 end
adamc@76 1043
adamc@76 1044 fun mapfold {kind, con, exp, sgn_item, sgn, str, decl} =
adamc@623 1045 mapfoldB {kind = fn () => kind,
adamc@76 1046 con = fn () => con,
adamc@76 1047 exp = fn () => exp,
adamc@76 1048 sgn_item = fn () => sgn_item,
adamc@76 1049 sgn = fn () => sgn,
adamc@76 1050 str = fn () => str,
adamc@76 1051 decl = fn () => decl,
adamc@76 1052 bind = fn ((), _) => ()} ()
adamc@76 1053
adamc@76 1054 fun exists {kind, con, exp, sgn_item, sgn, str, decl} k =
adamc@76 1055 case mapfold {kind = fn k => fn () =>
adamc@76 1056 if kind k then
adamc@76 1057 S.Return ()
adamc@76 1058 else
adamc@76 1059 S.Continue (k, ()),
adamc@76 1060 con = fn c => fn () =>
adamc@76 1061 if con c then
adamc@76 1062 S.Return ()
adamc@76 1063 else
adamc@76 1064 S.Continue (c, ()),
adamc@76 1065 exp = fn e => fn () =>
adamc@76 1066 if exp e then
adamc@76 1067 S.Return ()
adamc@76 1068 else
adamc@76 1069 S.Continue (e, ()),
adamc@76 1070 sgn_item = fn sgi => fn () =>
adamc@76 1071 if sgn_item sgi then
adamc@76 1072 S.Return ()
adamc@76 1073 else
adamc@76 1074 S.Continue (sgi, ()),
adamc@76 1075 sgn = fn x => fn () =>
adamc@76 1076 if sgn x then
adamc@76 1077 S.Return ()
adamc@76 1078 else
adamc@76 1079 S.Continue (x, ()),
adamc@76 1080 str = fn x => fn () =>
adamc@76 1081 if str x then
adamc@76 1082 S.Return ()
adamc@76 1083 else
adamc@76 1084 S.Continue (x, ()),
adamc@76 1085 decl = fn x => fn () =>
adamc@76 1086 if decl x then
adamc@76 1087 S.Return ()
adamc@76 1088 else
adamc@76 1089 S.Continue (x, ())} k () of
adamc@76 1090 S.Return _ => true
adamc@76 1091 | S.Continue _ => false
adamc@76 1092
adamc@76 1093 fun search {kind, con, exp, sgn_item, sgn, str, decl} k =
adamc@76 1094 case mapfold {kind = fn x => fn () =>
adamc@76 1095 case kind x of
adamc@76 1096 NONE => S.Continue (x, ())
adamc@76 1097 | SOME v => S.Return v,
adamc@76 1098
adamc@76 1099 con = fn x => fn () =>
adamc@76 1100 case con x of
adamc@76 1101 NONE => S.Continue (x, ())
adamc@76 1102 | SOME v => S.Return v,
adamc@76 1103
adamc@76 1104 exp = fn x => fn () =>
adamc@76 1105 case exp x of
adamc@76 1106 NONE => S.Continue (x, ())
adamc@76 1107 | SOME v => S.Return v,
adamc@76 1108
adamc@76 1109 sgn_item = fn x => fn () =>
adamc@76 1110 case sgn_item x of
adamc@76 1111 NONE => S.Continue (x, ())
adamc@76 1112 | SOME v => S.Return v,
adamc@76 1113
adamc@76 1114 sgn = fn x => fn () =>
adamc@76 1115 case sgn x of
adamc@76 1116 NONE => S.Continue (x, ())
adamc@76 1117 | SOME v => S.Return v,
adamc@76 1118
adamc@76 1119 str = fn x => fn () =>
adamc@76 1120 case str x of
adamc@76 1121 NONE => S.Continue (x, ())
adamc@76 1122 | SOME v => S.Return v,
adamc@76 1123
adamc@76 1124 decl = fn x => fn () =>
adamc@76 1125 case decl x of
adamc@76 1126 NONE => S.Continue (x, ())
adamc@76 1127 | SOME v => S.Return v
adamc@76 1128
adamc@76 1129 } k () of
adamc@76 1130 S.Return x => SOME x
adamc@76 1131 | S.Continue _ => NONE
adamc@76 1132
adamc@448 1133 fun foldMapB {kind, con, exp, sgn_item, sgn, str, decl, bind} ctx st d =
adamc@623 1134 case mapfoldB {kind = fn ctx => fn x => fn st => S.Continue (kind (ctx, x, st)),
adamc@448 1135 con = fn ctx => fn x => fn st => S.Continue (con (ctx, x, st)),
adamc@448 1136 exp = fn ctx => fn x => fn st => S.Continue (exp (ctx, x, st)),
adamc@448 1137 sgn_item = fn ctx => fn x => fn st => S.Continue (sgn_item (ctx, x, st)),
adamc@448 1138 sgn = fn ctx => fn x => fn st => S.Continue (sgn (ctx, x, st)),
adamc@448 1139 str = fn ctx => fn x => fn st => S.Continue (str (ctx, x, st)),
adamc@448 1140 decl = fn ctx => fn x => fn st => S.Continue (decl (ctx, x, st)),
adamc@448 1141 bind = bind} ctx d st of
adamc@448 1142 S.Continue x => x
adamc@448 1143 | S.Return _ => raise Fail "ElabUtil.Decl.foldMapB: Impossible"
adamc@448 1144
adamc@448 1145 end
adamc@448 1146
adamc@448 1147 structure File = struct
adamc@448 1148
adamc@448 1149 fun maxName ds = foldl (fn (d, count) => Int.max (maxNameDecl d, count)) 0 ds
adamc@448 1150
adamc@448 1151 and maxNameDecl (d, _) =
adamc@448 1152 case d of
adamc@448 1153 DCon (_, n, _, _) => n
adamc@805 1154 | DDatatype dts =>
adamc@805 1155 foldl (fn ((_, n, _, ns), max) =>
adamc@448 1156 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@805 1157 (Int.max (n, max)) ns) 0 dts
adamc@448 1158 | DDatatypeImp (_, n1, n2, _, _, _, ns) =>
adamc@448 1159 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@448 1160 (Int.max (n1, n2)) ns
adamc@448 1161 | DVal (_, n, _, _) => n
adamc@448 1162 | DValRec vis => foldl (fn ((_, n, _, _), count) => Int.max (n, count)) 0 vis
adamc@448 1163 | DStr (_, n, sgn, str) => Int.max (n, Int.max (maxNameSgn sgn, maxNameStr str))
adamc@448 1164 | DSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1165 | DFfiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1166 | DConstraint _ => 0
adamc@563 1167 | DClass (_, n, _, _) => n
adamc@448 1168 | DExport _ => 0
adamc@707 1169 | DTable (n1, _, n2, _, _, _, _, _) => Int.max (n1, n2)
adamc@459 1170 | DSequence (n1, _, n2) => Int.max (n1, n2)
adamc@754 1171 | DView (n1, _, n2, _, _) => Int.max (n1, n2)
adamc@448 1172 | DDatabase _ => 0
adamc@459 1173 | DCookie (n1, _, n2, _) => Int.max (n1, n2)
adamc@720 1174 | DStyle (n1, _, n2) => Int.max (n1, n2)
adamc@1075 1175 | DTask _ => 0
adamc@1199 1176 | DPolicy _ => 0
adam@1294 1177 | DOnError _ => 0
adamc@448 1178 and maxNameStr (str, _) =
adamc@448 1179 case str of
adamc@448 1180 StrConst ds => maxName ds
adamc@448 1181 | StrVar n => n
adamc@448 1182 | StrProj (str, _) => maxNameStr str
adamc@448 1183 | StrFun (_, n, dom, ran, str) => foldl Int.max n [maxNameSgn dom, maxNameSgn ran, maxNameStr str]
adamc@448 1184 | StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
adamc@448 1185 | StrError => 0
adamc@448 1186
adamc@448 1187 and maxNameSgn (sgn, _) =
adamc@448 1188 case sgn of
adamc@448 1189 SgnConst sgis => foldl (fn (sgi, count) => Int.max (maxNameSgi sgi, count)) 0 sgis
adamc@448 1190 | SgnVar n => n
adamc@448 1191 | SgnFun (_, n, dom, ran) => Int.max (n, Int.max (maxNameSgn dom, maxNameSgn ran))
adamc@448 1192 | SgnWhere (sgn, _, _) => maxNameSgn sgn
adamc@448 1193 | SgnProj (n, _, _) => n
adamc@448 1194 | SgnError => 0
adamc@448 1195
adamc@448 1196 and maxNameSgi (sgi, _) =
adamc@448 1197 case sgi of
adamc@448 1198 SgiConAbs (_, n, _) => n
adamc@448 1199 | SgiCon (_, n, _, _) => n
adamc@805 1200 | SgiDatatype dts =>
adamc@805 1201 foldl (fn ((_, n, _, ns), max) =>
adamc@805 1202 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@805 1203 (Int.max (n, max)) ns) 0 dts
adamc@448 1204 | SgiDatatypeImp (_, n1, n2, _, _, _, ns) =>
adamc@448 1205 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@448 1206 (Int.max (n1, n2)) ns
adamc@448 1207 | SgiVal (_, n, _) => n
adamc@448 1208 | SgiStr (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1209 | SgiSgn (_, n, sgn) => Int.max (n, maxNameSgn sgn)
adamc@448 1210 | SgiConstraint _ => 0
adamc@563 1211 | SgiClassAbs (_, n, _) => n
adamc@563 1212 | SgiClass (_, n, _, _) => n
adamc@448 1213
adamc@34 1214 end
adamc@76 1215
adamc@76 1216 end