annotate src/elab_env.sml @ 184:98c29e3986d3

Push write inside case
author Adam Chlipala <adamc@hcoop.net>
date Sun, 03 Aug 2008 16:53:13 -0400
parents 7ee424760d2f
children 88d46972de53
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 ElabEnv :> ELAB_ENV = struct
adamc@2 29
adamc@2 30 open Elab
adamc@2 31
adamc@13 32 structure U = ElabUtil
adamc@13 33
adamc@2 34 structure IM = IntBinaryMap
adamc@2 35 structure SM = BinaryMapFn(struct
adamc@2 36 type ord_key = string
adamc@2 37 val compare = String.compare
adamc@2 38 end)
adamc@2 39
adamc@2 40 exception UnboundRel of int
adamc@2 41 exception UnboundNamed of int
adamc@2 42
adamc@13 43
adamc@13 44 (* AST utility functions *)
adamc@13 45
adamc@13 46 exception SynUnif
adamc@13 47
adamc@13 48 val liftConInCon =
adamc@13 49 U.Con.mapB {kind = fn k => k,
adamc@13 50 con = fn bound => fn c =>
adamc@13 51 case c of
adamc@16 52 CRel xn =>
adamc@13 53 if xn < bound then
adamc@13 54 c
adamc@13 55 else
adamc@16 56 CRel (xn + 1)
adamc@17 57 (*| CUnif _ => raise SynUnif*)
adamc@13 58 | _ => c,
adamc@13 59 bind = fn (bound, U.Con.Rel _) => bound + 1
adamc@13 60 | (bound, _) => bound}
adamc@13 61
adamc@13 62 val lift = liftConInCon 0
adamc@13 63
adamc@13 64
adamc@13 65 (* Back to environments *)
adamc@13 66
adamc@9 67 datatype 'a var' =
adamc@9 68 Rel' of int * 'a
adamc@9 69 | Named' of int * 'a
adamc@2 70
adamc@9 71 datatype 'a var =
adamc@9 72 NotBound
adamc@9 73 | Rel of int * 'a
adamc@9 74 | Named of int * 'a
adamc@2 75
adamc@157 76 type datatyp = (string * con option) IM.map
adamc@157 77
adamc@2 78 type env = {
adamc@9 79 renameC : kind var' SM.map,
adamc@2 80 relC : (string * kind) list,
adamc@11 81 namedC : (string * kind * con option) IM.map,
adamc@9 82
adamc@157 83 datatypes : datatyp IM.map,
adamc@171 84 constructors : (int * con option * int) SM.map,
adamc@157 85
adamc@9 86 renameE : con var' SM.map,
adamc@9 87 relE : (string * con) list,
adamc@31 88 namedE : (string * con) IM.map,
adamc@31 89
adamc@31 90 renameSgn : (int * sgn) SM.map,
adamc@31 91 sgn : (string * sgn) IM.map,
adamc@31 92
adamc@31 93 renameStr : (int * sgn) SM.map,
adamc@31 94 str : (string * sgn) IM.map
adamc@2 95 }
adamc@2 96
adamc@2 97 val namedCounter = ref 0
adamc@2 98
adamc@109 99 fun newNamed () =
adamc@109 100 let
adamc@109 101 val r = !namedCounter
adamc@109 102 in
adamc@109 103 namedCounter := r + 1;
adamc@109 104 r
adamc@109 105 end
adamc@109 106
adamc@2 107 val empty = {
adamc@2 108 renameC = SM.empty,
adamc@2 109 relC = [],
adamc@9 110 namedC = IM.empty,
adamc@9 111
adamc@157 112 datatypes = IM.empty,
adamc@171 113 constructors = SM.empty,
adamc@157 114
adamc@9 115 renameE = SM.empty,
adamc@9 116 relE = [],
adamc@31 117 namedE = IM.empty,
adamc@31 118
adamc@31 119 renameSgn = SM.empty,
adamc@31 120 sgn = IM.empty,
adamc@31 121
adamc@31 122 renameStr = SM.empty,
adamc@31 123 str = IM.empty
adamc@2 124 }
adamc@2 125
adamc@2 126 fun pushCRel (env : env) x k =
adamc@2 127 let
adamc@9 128 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
adamc@2 129 | x => x) (#renameC env)
adamc@2 130 in
adamc@9 131 {renameC = SM.insert (renameC, x, Rel' (0, k)),
adamc@2 132 relC = (x, k) :: #relC env,
adamc@13 133 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
adamc@9 134
adamc@157 135 datatypes = #datatypes env,
adamc@171 136 constructors = #constructors env,
adamc@157 137
adamc@9 138 renameE = #renameE env,
adamc@13 139 relE = map (fn (x, c) => (x, lift c)) (#relE env),
adamc@31 140 namedE = IM.map (fn (x, c) => (x, lift c)) (#namedE env),
adamc@31 141
adamc@31 142 renameSgn = #renameSgn env,
adamc@31 143 sgn = #sgn env,
adamc@31 144
adamc@31 145 renameStr = #renameStr env,
adamc@31 146 str = #str env
adamc@31 147 }
adamc@2 148 end
adamc@2 149
adamc@2 150 fun lookupCRel (env : env) n =
adamc@2 151 (List.nth (#relC env, n))
adamc@2 152 handle Subscript => raise UnboundRel n
adamc@2 153
adamc@11 154 fun pushCNamedAs (env : env) x n k co =
adamc@9 155 {renameC = SM.insert (#renameC env, x, Named' (n, k)),
adamc@5 156 relC = #relC env,
adamc@11 157 namedC = IM.insert (#namedC env, n, (x, k, co)),
adamc@9 158
adamc@157 159 datatypes = #datatypes env,
adamc@171 160 constructors = #constructors env,
adamc@157 161
adamc@9 162 renameE = #renameE env,
adamc@9 163 relE = #relE env,
adamc@31 164 namedE = #namedE env,
adamc@31 165
adamc@31 166 renameSgn = #renameSgn env,
adamc@31 167 sgn = #sgn env,
adamc@31 168
adamc@31 169 renameStr = #renameStr env,
adamc@31 170 str = #str env}
adamc@5 171
adamc@11 172 fun pushCNamed env x k co =
adamc@2 173 let
adamc@2 174 val n = !namedCounter
adamc@2 175 in
adamc@2 176 namedCounter := n + 1;
adamc@11 177 (pushCNamedAs env x n k co, n)
adamc@2 178 end
adamc@2 179
adamc@2 180 fun lookupCNamed (env : env) n =
adamc@2 181 case IM.find (#namedC env, n) of
adamc@2 182 NONE => raise UnboundNamed n
adamc@2 183 | SOME x => x
adamc@2 184
adamc@2 185 fun lookupC (env : env) x =
adamc@2 186 case SM.find (#renameC env, x) of
adamc@9 187 NONE => NotBound
adamc@9 188 | SOME (Rel' x) => Rel x
adamc@9 189 | SOME (Named' x) => Named x
adamc@9 190
adamc@157 191 fun pushDatatype (env : env) n xncs =
adamc@157 192 {renameC = #renameC env,
adamc@157 193 relC = #relC env,
adamc@157 194 namedC = #namedC env,
adamc@157 195
adamc@157 196 datatypes = IM.insert (#datatypes env, n,
adamc@157 197 foldl (fn ((x, n, to), cons) =>
adamc@157 198 IM.insert (cons, n, (x, to))) IM.empty xncs),
adamc@171 199 constructors = foldl (fn ((x, n', to), cmap) =>
adamc@171 200 SM.insert (cmap, x, (n', to, n)))
adamc@171 201 (#constructors env) xncs,
adamc@157 202
adamc@157 203 renameE = #renameE env,
adamc@157 204 relE = #relE env,
adamc@157 205 namedE = #namedE env,
adamc@157 206
adamc@157 207 renameSgn = #renameSgn env,
adamc@157 208 sgn = #sgn env,
adamc@157 209
adamc@157 210 renameStr = #renameStr env,
adamc@157 211 str = #str env}
adamc@157 212
adamc@157 213 fun lookupDatatype (env : env) n =
adamc@157 214 case IM.find (#datatypes env, n) of
adamc@157 215 NONE => raise UnboundNamed n
adamc@157 216 | SOME x => x
adamc@157 217
adamc@171 218 fun lookupDatatypeConstructor dt n =
adamc@157 219 case IM.find (dt, n) of
adamc@157 220 NONE => raise UnboundNamed n
adamc@157 221 | SOME x => x
adamc@157 222
adamc@171 223 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
adamc@171 224
adamc@157 225 fun constructors dt = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
adamc@157 226
adamc@9 227 fun pushERel (env : env) x t =
adamc@9 228 let
adamc@9 229 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
adamc@9 230 | x => x) (#renameE env)
adamc@9 231 in
adamc@9 232 {renameC = #renameC env,
adamc@9 233 relC = #relC env,
adamc@9 234 namedC = #namedC env,
adamc@9 235
adamc@157 236 datatypes = #datatypes env,
adamc@171 237 constructors = #constructors env,
adamc@157 238
adamc@9 239 renameE = SM.insert (renameE, x, Rel' (0, t)),
adamc@9 240 relE = (x, t) :: #relE env,
adamc@31 241 namedE = #namedE env,
adamc@31 242
adamc@31 243 renameSgn = #renameSgn env,
adamc@31 244 sgn = #sgn env,
adamc@31 245
adamc@31 246 renameStr = #renameStr env,
adamc@31 247 str = #str env}
adamc@9 248 end
adamc@9 249
adamc@9 250 fun lookupERel (env : env) n =
adamc@9 251 (List.nth (#relE env, n))
adamc@9 252 handle Subscript => raise UnboundRel n
adamc@9 253
adamc@9 254 fun pushENamedAs (env : env) x n t =
adamc@9 255 {renameC = #renameC env,
adamc@9 256 relC = #relC env,
adamc@9 257 namedC = #namedC env,
adamc@9 258
adamc@157 259 datatypes = #datatypes env,
adamc@171 260 constructors = #constructors env,
adamc@157 261
adamc@9 262 renameE = SM.insert (#renameE env, x, Named' (n, t)),
adamc@9 263 relE = #relE env,
adamc@31 264 namedE = IM.insert (#namedE env, n, (x, t)),
adamc@31 265
adamc@31 266 renameSgn = #renameSgn env,
adamc@31 267 sgn = #sgn env,
adamc@31 268
adamc@31 269 renameStr = #renameStr env,
adamc@31 270 str = #str env}
adamc@9 271
adamc@9 272 fun pushENamed env x t =
adamc@9 273 let
adamc@9 274 val n = !namedCounter
adamc@9 275 in
adamc@9 276 namedCounter := n + 1;
adamc@9 277 (pushENamedAs env x n t, n)
adamc@9 278 end
adamc@9 279
adamc@9 280 fun lookupENamed (env : env) n =
adamc@9 281 case IM.find (#namedE env, n) of
adamc@9 282 NONE => raise UnboundNamed n
adamc@9 283 | SOME x => x
adamc@9 284
adamc@9 285 fun lookupE (env : env) x =
adamc@9 286 case SM.find (#renameE env, x) of
adamc@9 287 NONE => NotBound
adamc@9 288 | SOME (Rel' x) => Rel x
adamc@9 289 | SOME (Named' x) => Named x
adamc@2 290
adamc@31 291 fun pushSgnNamedAs (env : env) x n sgis =
adamc@31 292 {renameC = #renameC env,
adamc@31 293 relC = #relC env,
adamc@31 294 namedC = #namedC env,
adamc@31 295
adamc@157 296 datatypes = #datatypes env,
adamc@171 297 constructors = #constructors env,
adamc@157 298
adamc@31 299 renameE = #renameE env,
adamc@31 300 relE = #relE env,
adamc@31 301 namedE = #namedE env,
adamc@31 302
adamc@31 303 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
adamc@31 304 sgn = IM.insert (#sgn env, n, (x, sgis)),
adamc@31 305
adamc@31 306 renameStr = #renameStr env,
adamc@31 307 str = #str env}
adamc@31 308
adamc@31 309 fun pushSgnNamed env x sgis =
adamc@31 310 let
adamc@31 311 val n = !namedCounter
adamc@31 312 in
adamc@31 313 namedCounter := n + 1;
adamc@31 314 (pushSgnNamedAs env x n sgis, n)
adamc@31 315 end
adamc@31 316
adamc@31 317 fun lookupSgnNamed (env : env) n =
adamc@31 318 case IM.find (#sgn env, n) of
adamc@31 319 NONE => raise UnboundNamed n
adamc@31 320 | SOME x => x
adamc@31 321
adamc@31 322 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
adamc@31 323
adamc@31 324 fun pushStrNamedAs (env : env) x n sgis =
adamc@31 325 {renameC = #renameC env,
adamc@31 326 relC = #relC env,
adamc@31 327 namedC = #namedC env,
adamc@31 328
adamc@157 329 datatypes = #datatypes env,
adamc@171 330 constructors = #constructors env,
adamc@157 331
adamc@31 332 renameE = #renameE env,
adamc@31 333 relE = #relE env,
adamc@31 334 namedE = #namedE env,
adamc@31 335
adamc@31 336 renameSgn = #renameSgn env,
adamc@31 337 sgn = #sgn env,
adamc@31 338
adamc@31 339 renameStr = SM.insert (#renameStr env, x, (n, sgis)),
adamc@31 340 str = IM.insert (#str env, n, (x, sgis))}
adamc@31 341
adamc@31 342 fun pushStrNamed env x sgis =
adamc@31 343 let
adamc@31 344 val n = !namedCounter
adamc@31 345 in
adamc@31 346 namedCounter := n + 1;
adamc@31 347 (pushStrNamedAs env x n sgis, n)
adamc@31 348 end
adamc@31 349
adamc@31 350 fun lookupStrNamed (env : env) n =
adamc@31 351 case IM.find (#str env, n) of
adamc@31 352 NONE => raise UnboundNamed n
adamc@31 353 | SOME x => x
adamc@31 354
adamc@31 355 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
adamc@31 356
adamc@156 357 fun sgiBinds env (sgi, loc) =
adamc@31 358 case sgi of
adamc@31 359 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@31 360 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@156 361 | SgiDatatype (x, n, xncs) =>
adamc@156 362 let
adamc@156 363 val env = pushCNamedAs env x n (KType, loc) NONE
adamc@156 364 in
adamc@156 365 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' (CNamed n, loc)
adamc@156 366 | ((x', n', SOME t), env) => pushENamedAs env x' n' (TFun (t, (CNamed n, loc)), loc))
adamc@156 367 env xncs
adamc@156 368 end
adamc@162 369 | SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
adamc@162 370 let
adamc@162 371 val env = pushCNamedAs env x n (KType, loc) (SOME (CModProj (m1, ms, x'), loc))
adamc@162 372 in
adamc@162 373 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' (CNamed n, loc)
adamc@162 374 | ((x', n', SOME t), env) => pushENamedAs env x' n' (TFun (t, (CNamed n, loc)), loc))
adamc@162 375 env xncs
adamc@162 376 end
adamc@31 377 | SgiVal (x, n, t) => pushENamedAs env x n t
adamc@31 378 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@59 379 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@88 380 | SgiConstraint _ => env
adamc@31 381
adamc@34 382 fun sgnSeek f sgis =
adamc@34 383 let
adamc@59 384 fun seek (sgis, sgns, strs, cons) =
adamc@34 385 case sgis of
adamc@34 386 [] => NONE
adamc@34 387 | (sgi, _) :: sgis =>
adamc@34 388 case f sgi of
adamc@158 389 SOME v =>
adamc@158 390 let
adamc@158 391 val cons =
adamc@158 392 case sgi of
adamc@158 393 SgiDatatype (x, n, _) => IM.insert (cons, n, x)
adamc@162 394 | SgiDatatypeImp (x, n, _, _, _, _) => IM.insert (cons, n, x)
adamc@158 395 | _ => cons
adamc@158 396 in
adamc@158 397 SOME (v, (sgns, strs, cons))
adamc@158 398 end
adamc@88 399 | NONE =>
adamc@88 400 case sgi of
adamc@88 401 SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@88 402 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@156 403 | SgiDatatype (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@162 404 | SgiDatatypeImp (x, n, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@88 405 | SgiVal _ => seek (sgis, sgns, strs, cons)
adamc@88 406 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons)
adamc@88 407 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons)
adamc@88 408 | SgiConstraint _ => seek (sgis, sgns, strs, cons)
adamc@34 409 in
adamc@59 410 seek (sgis, IM.empty, IM.empty, IM.empty)
adamc@34 411 end
adamc@34 412
adamc@34 413 fun id x = x
adamc@34 414
adamc@34 415 fun unravelStr (str, _) =
adamc@34 416 case str of
adamc@34 417 StrVar x => (x, [])
adamc@34 418 | StrProj (str, m) =>
adamc@34 419 let
adamc@34 420 val (x, ms) = unravelStr str
adamc@34 421 in
adamc@34 422 (x, ms @ [m])
adamc@34 423 end
adamc@34 424 | _ => raise Fail "unravelStr"
adamc@34 425
adamc@59 426 fun sgnS_con (str, (sgns, strs, cons)) c =
adamc@34 427 case c of
adamc@34 428 CModProj (m1, ms, x) =>
adamc@34 429 (case IM.find (strs, m1) of
adamc@34 430 NONE => c
adamc@34 431 | SOME m1x =>
adamc@34 432 let
adamc@34 433 val (m1, ms') = unravelStr str
adamc@34 434 in
adamc@34 435 CModProj (m1, ms' @ m1x :: ms, x)
adamc@34 436 end)
adamc@34 437 | CNamed n =>
adamc@34 438 (case IM.find (cons, n) of
adamc@34 439 NONE => c
adamc@34 440 | SOME nx =>
adamc@34 441 let
adamc@34 442 val (m1, ms) = unravelStr str
adamc@34 443 in
adamc@34 444 CModProj (m1, ms, nx)
adamc@34 445 end)
adamc@34 446 | _ => c
adamc@34 447
adamc@59 448 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
adamc@59 449 case sgn of
adamc@59 450 SgnProj (m1, ms, x) =>
adamc@59 451 (case IM.find (strs, m1) of
adamc@59 452 NONE => sgn
adamc@59 453 | SOME m1x =>
adamc@59 454 let
adamc@59 455 val (m1, ms') = unravelStr str
adamc@59 456 in
adamc@59 457 SgnProj (m1, ms' @ m1x :: ms, x)
adamc@59 458 end)
adamc@59 459 | SgnVar n =>
adamc@59 460 (case IM.find (sgns, n) of
adamc@59 461 NONE => sgn
adamc@59 462 | SOME nx =>
adamc@59 463 let
adamc@59 464 val (m1, ms) = unravelStr str
adamc@59 465 in
adamc@59 466 SgnProj (m1, ms, nx)
adamc@59 467 end)
adamc@59 468 | _ => sgn
adamc@59 469
adamc@59 470 fun sgnSubCon x =
adamc@34 471 ElabUtil.Con.map {kind = id,
adamc@59 472 con = sgnS_con x}
adamc@34 473
adamc@59 474 fun sgnSubSgn x =
adamc@34 475 ElabUtil.Sgn.map {kind = id,
adamc@59 476 con = sgnS_con x,
adamc@34 477 sgn_item = id,
adamc@59 478 sgn = sgnS_sgn x}
adamc@34 479
adamc@42 480 fun hnormSgn env (all as (sgn, loc)) =
adamc@34 481 case sgn of
adamc@42 482 SgnError => all
adamc@42 483 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
adamc@42 484 | SgnConst _ => all
adamc@42 485 | SgnFun _ => all
adamc@59 486 | SgnProj (m, ms, x) =>
adamc@59 487 let
adamc@59 488 val (_, sgn) = lookupStrNamed env m
adamc@59 489 in
adamc@59 490 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
adamc@59 491 sgn = sgn,
adamc@59 492 field = x} of
adamc@59 493 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
adamc@59 494 | SOME sgn => sgn
adamc@59 495 end
adamc@42 496 | SgnWhere (sgn, x, c) =>
adamc@42 497 case #1 (hnormSgn env sgn) of
adamc@42 498 SgnError => (SgnError, loc)
adamc@42 499 | SgnConst sgis =>
adamc@42 500 let
adamc@42 501 fun traverse (pre, post) =
adamc@42 502 case post of
adamc@42 503 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
adamc@42 504 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
adamc@42 505 if x = x' then
adamc@42 506 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
adamc@42 507 else
adamc@42 508 traverse (sgi :: pre, rest)
adamc@42 509 | sgi :: rest => traverse (sgi :: pre, rest)
adamc@42 510
adamc@42 511 val sgis = traverse ([], sgis)
adamc@42 512 in
adamc@42 513 (SgnConst sgis, loc)
adamc@42 514 end
adamc@42 515 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
adamc@42 516
adamc@59 517 and projectSgn env {sgn, str, field} =
adamc@59 518 case #1 (hnormSgn env sgn) of
adamc@59 519 SgnConst sgis =>
adamc@59 520 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 521 NONE => NONE
adamc@59 522 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 523 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 524 | _ => NONE
adamc@59 525
adamc@59 526 fun projectStr env {sgn, str, field} =
adamc@59 527 case #1 (hnormSgn env sgn) of
adamc@59 528 SgnConst sgis =>
adamc@59 529 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 530 NONE => NONE
adamc@59 531 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 532 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 533 | _ => NONE
adamc@59 534
adamc@158 535 fun chaseMpath env (n, ms) =
adamc@158 536 let
adamc@158 537 val (_, sgn) = lookupStrNamed env n
adamc@158 538 in
adamc@158 539 foldl (fn (m, (str, sgn)) =>
adamc@158 540 case projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 541 NONE => raise Fail "kindof: Unknown substructure"
adamc@158 542 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
adamc@158 543 ((StrVar n, #2 sgn), sgn) ms
adamc@158 544 end
adamc@158 545
adamc@42 546 fun projectCon env {sgn, str, field} =
adamc@42 547 case #1 (hnormSgn env sgn) of
adamc@34 548 SgnConst sgis =>
adamc@34 549 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
adamc@34 550 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
adamc@157 551 | SgiDatatype (x, _, _) => if x = field then SOME ((KType, #2 sgn), NONE) else NONE
adamc@162 552 | SgiDatatypeImp (x, _, m1, ms, x', _) =>
adamc@158 553 if x = field then
adamc@158 554 SOME ((KType, #2 sgn), SOME (CModProj (m1, ms, x'), #2 sgn))
adamc@158 555 else
adamc@158 556 NONE
adamc@34 557 | _ => NONE) sgis of
adamc@34 558 NONE => NONE
adamc@34 559 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
adamc@34 560 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
adamc@42 561 | _ => NONE
adamc@34 562
adamc@157 563 fun projectDatatype env {sgn, str, field} =
adamc@157 564 case #1 (hnormSgn env sgn) of
adamc@157 565 SgnConst sgis =>
adamc@157 566 (case sgnSeek (fn SgiDatatype (x, _, xncs) => if x = field then SOME xncs else NONE
adamc@162 567 | SgiDatatypeImp (x, _, _, _, _, xncs) => if x = field then SOME xncs else NONE
adamc@157 568 | _ => NONE) sgis of
adamc@157 569 NONE => NONE
adamc@157 570 | SOME (xncs, subs) => SOME (map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
adamc@157 571 | _ => NONE
adamc@157 572
adamc@174 573 fun projectConstructor env {sgn, str, field} =
adamc@174 574 case #1 (hnormSgn env sgn) of
adamc@174 575 SgnConst sgis =>
adamc@174 576 let
adamc@174 577 fun consider (n, xncs) =
adamc@174 578 ListUtil.search (fn (x, n', to) =>
adamc@174 579 if x <> field then
adamc@174 580 NONE
adamc@174 581 else
adamc@174 582 SOME (n', to, (CNamed n, #2 str))) xncs
adamc@174 583 in
adamc@174 584 case sgnSeek (fn SgiDatatype (_, n, xncs) => consider (n, xncs)
adamc@174 585 | SgiDatatypeImp (_, n, _, _, _, xncs) => consider (n, xncs)
adamc@174 586 | _ => NONE) sgis of
adamc@174 587 NONE => NONE
adamc@174 588 | SOME ((n, to, t), subs) => SOME (n, Option.map (sgnSubCon (str, subs)) to, sgnSubCon (str, subs) t)
adamc@174 589 end
adamc@174 590 | _ => NONE
adamc@174 591
adamc@42 592 fun projectVal env {sgn, str, field} =
adamc@42 593 case #1 (hnormSgn env sgn) of
adamc@34 594 SgnConst sgis =>
adamc@162 595 let
adamc@162 596 fun seek (n, xncs) =
adamc@162 597 ListUtil.search (fn (x, _, to) =>
adamc@162 598 if x = field then
adamc@162 599 SOME (case to of
adamc@162 600 NONE => (CNamed n, #2 sgn)
adamc@162 601 | SOME t => (TFun (t, (CNamed n, #2 sgn)), #2 sgn))
adamc@162 602 else
adamc@162 603 NONE) xncs
adamc@162 604 in
adamc@162 605 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
adamc@162 606 | SgiDatatype (_, n, xncs) => seek (n, xncs)
adamc@162 607 | SgiDatatypeImp (_, n, _, _, _, xncs) => seek (n, xncs)
adamc@162 608 | _ => NONE) sgis of
adamc@162 609 NONE => NONE
adamc@162 610 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
adamc@162 611 end
adamc@34 612 | SgnError => SOME (CError, ErrorMsg.dummySpan)
adamc@42 613 | _ => NONE
adamc@34 614
adamc@88 615 fun sgnSeekConstraints (str, sgis) =
adamc@88 616 let
adamc@88 617 fun seek (sgis, sgns, strs, cons, acc) =
adamc@88 618 case sgis of
adamc@88 619 [] => acc
adamc@88 620 | (sgi, _) :: sgis =>
adamc@88 621 case sgi of
adamc@88 622 SgiConstraint (c1, c2) =>
adamc@88 623 let
adamc@88 624 val sub = sgnSubCon (str, (sgns, strs, cons))
adamc@88 625 in
adamc@88 626 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
adamc@88 627 end
adamc@88 628 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 629 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@156 630 | SgiDatatype (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@162 631 | SgiDatatypeImp (x, n, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 632 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
adamc@88 633 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
adamc@88 634 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
adamc@88 635 in
adamc@88 636 seek (sgis, IM.empty, IM.empty, IM.empty, [])
adamc@88 637 end
adamc@88 638
adamc@88 639 fun projectConstraints env {sgn, str} =
adamc@88 640 case #1 (hnormSgn env sgn) of
adamc@88 641 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
adamc@88 642 | SgnError => SOME []
adamc@88 643 | _ => NONE
adamc@34 644
adamc@157 645 fun declBinds env (d, loc) =
adamc@157 646 case d of
adamc@157 647 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@157 648 | DDatatype (x, n, xncs) =>
adamc@157 649 let
adamc@157 650 val env = pushCNamedAs env x n (KType, loc) NONE
adamc@157 651 in
adamc@157 652 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' (CNamed n, loc)
adamc@157 653 | ((x', n', SOME t), env) => pushENamedAs env x' n' (TFun (t, (CNamed n, loc)), loc))
adamc@157 654 env xncs
adamc@157 655 end
adamc@162 656 | DDatatypeImp (x, n, m, ms, x', xncs) =>
adamc@157 657 let
adamc@157 658 val t = (CModProj (m, ms, x'), loc)
adamc@157 659 val env = pushCNamedAs env x n (KType, loc) (SOME t)
adamc@157 660
adamc@157 661 val t = (CNamed n, loc)
adamc@157 662 in
adamc@157 663 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' t
adamc@157 664 | ((x', n', SOME t'), env) => pushENamedAs env x' n' (TFun (t', t), loc))
adamc@157 665 env xncs
adamc@157 666 end
adamc@157 667 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@157 668 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
adamc@157 669 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@157 670 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
adamc@157 671 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@157 672 | DConstraint _ => env
adamc@157 673 | DExport _ => env
adamc@157 674
adamc@2 675 end