annotate src/elab_env.sml @ 189:20bf7487c370

Update bool to follow datatype representation change
author Adam Chlipala <adamc@hcoop.net>
date Sun, 03 Aug 2008 19:52:37 -0400
parents 8e9f97508f0d
children aa54250f58ac
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@188 84 constructors : (datatype_kind * 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@188 192 let
adamc@188 193 val dk = U.classifyDatatype xncs
adamc@188 194 in
adamc@188 195 {renameC = #renameC env,
adamc@188 196 relC = #relC env,
adamc@188 197 namedC = #namedC env,
adamc@157 198
adamc@188 199 datatypes = IM.insert (#datatypes env, n,
adamc@188 200 foldl (fn ((x, n, to), cons) =>
adamc@188 201 IM.insert (cons, n, (x, to))) IM.empty xncs),
adamc@188 202 constructors = foldl (fn ((x, n', to), cmap) =>
adamc@188 203 SM.insert (cmap, x, (dk, n', to, n)))
adamc@188 204 (#constructors env) xncs,
adamc@157 205
adamc@188 206 renameE = #renameE env,
adamc@188 207 relE = #relE env,
adamc@188 208 namedE = #namedE env,
adamc@157 209
adamc@188 210 renameSgn = #renameSgn env,
adamc@188 211 sgn = #sgn env,
adamc@157 212
adamc@188 213 renameStr = #renameStr env,
adamc@188 214 str = #str env}
adamc@188 215 end
adamc@157 216
adamc@157 217 fun lookupDatatype (env : env) n =
adamc@157 218 case IM.find (#datatypes env, n) of
adamc@157 219 NONE => raise UnboundNamed n
adamc@157 220 | SOME x => x
adamc@157 221
adamc@171 222 fun lookupDatatypeConstructor dt n =
adamc@157 223 case IM.find (dt, n) of
adamc@157 224 NONE => raise UnboundNamed n
adamc@157 225 | SOME x => x
adamc@157 226
adamc@171 227 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
adamc@171 228
adamc@157 229 fun constructors dt = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
adamc@157 230
adamc@9 231 fun pushERel (env : env) x t =
adamc@9 232 let
adamc@9 233 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
adamc@9 234 | x => x) (#renameE env)
adamc@9 235 in
adamc@9 236 {renameC = #renameC env,
adamc@9 237 relC = #relC env,
adamc@9 238 namedC = #namedC env,
adamc@9 239
adamc@157 240 datatypes = #datatypes env,
adamc@171 241 constructors = #constructors env,
adamc@157 242
adamc@9 243 renameE = SM.insert (renameE, x, Rel' (0, t)),
adamc@9 244 relE = (x, t) :: #relE env,
adamc@31 245 namedE = #namedE env,
adamc@31 246
adamc@31 247 renameSgn = #renameSgn env,
adamc@31 248 sgn = #sgn env,
adamc@31 249
adamc@31 250 renameStr = #renameStr env,
adamc@31 251 str = #str env}
adamc@9 252 end
adamc@9 253
adamc@9 254 fun lookupERel (env : env) n =
adamc@9 255 (List.nth (#relE env, n))
adamc@9 256 handle Subscript => raise UnboundRel n
adamc@9 257
adamc@9 258 fun pushENamedAs (env : env) x n t =
adamc@9 259 {renameC = #renameC env,
adamc@9 260 relC = #relC env,
adamc@9 261 namedC = #namedC env,
adamc@9 262
adamc@157 263 datatypes = #datatypes env,
adamc@171 264 constructors = #constructors env,
adamc@157 265
adamc@9 266 renameE = SM.insert (#renameE env, x, Named' (n, t)),
adamc@9 267 relE = #relE env,
adamc@31 268 namedE = IM.insert (#namedE env, n, (x, t)),
adamc@31 269
adamc@31 270 renameSgn = #renameSgn env,
adamc@31 271 sgn = #sgn env,
adamc@31 272
adamc@31 273 renameStr = #renameStr env,
adamc@31 274 str = #str env}
adamc@9 275
adamc@9 276 fun pushENamed env x t =
adamc@9 277 let
adamc@9 278 val n = !namedCounter
adamc@9 279 in
adamc@9 280 namedCounter := n + 1;
adamc@9 281 (pushENamedAs env x n t, n)
adamc@9 282 end
adamc@9 283
adamc@9 284 fun lookupENamed (env : env) n =
adamc@9 285 case IM.find (#namedE env, n) of
adamc@9 286 NONE => raise UnboundNamed n
adamc@9 287 | SOME x => x
adamc@9 288
adamc@9 289 fun lookupE (env : env) x =
adamc@9 290 case SM.find (#renameE env, x) of
adamc@9 291 NONE => NotBound
adamc@9 292 | SOME (Rel' x) => Rel x
adamc@9 293 | SOME (Named' x) => Named x
adamc@2 294
adamc@31 295 fun pushSgnNamedAs (env : env) x n sgis =
adamc@31 296 {renameC = #renameC env,
adamc@31 297 relC = #relC env,
adamc@31 298 namedC = #namedC env,
adamc@31 299
adamc@157 300 datatypes = #datatypes env,
adamc@171 301 constructors = #constructors env,
adamc@157 302
adamc@31 303 renameE = #renameE env,
adamc@31 304 relE = #relE env,
adamc@31 305 namedE = #namedE env,
adamc@31 306
adamc@31 307 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
adamc@31 308 sgn = IM.insert (#sgn env, n, (x, sgis)),
adamc@31 309
adamc@31 310 renameStr = #renameStr env,
adamc@31 311 str = #str env}
adamc@31 312
adamc@31 313 fun pushSgnNamed env x sgis =
adamc@31 314 let
adamc@31 315 val n = !namedCounter
adamc@31 316 in
adamc@31 317 namedCounter := n + 1;
adamc@31 318 (pushSgnNamedAs env x n sgis, n)
adamc@31 319 end
adamc@31 320
adamc@31 321 fun lookupSgnNamed (env : env) n =
adamc@31 322 case IM.find (#sgn env, n) of
adamc@31 323 NONE => raise UnboundNamed n
adamc@31 324 | SOME x => x
adamc@31 325
adamc@31 326 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
adamc@31 327
adamc@31 328 fun pushStrNamedAs (env : env) x n sgis =
adamc@31 329 {renameC = #renameC env,
adamc@31 330 relC = #relC env,
adamc@31 331 namedC = #namedC env,
adamc@31 332
adamc@157 333 datatypes = #datatypes env,
adamc@171 334 constructors = #constructors env,
adamc@157 335
adamc@31 336 renameE = #renameE env,
adamc@31 337 relE = #relE env,
adamc@31 338 namedE = #namedE env,
adamc@31 339
adamc@31 340 renameSgn = #renameSgn env,
adamc@31 341 sgn = #sgn env,
adamc@31 342
adamc@31 343 renameStr = SM.insert (#renameStr env, x, (n, sgis)),
adamc@31 344 str = IM.insert (#str env, n, (x, sgis))}
adamc@31 345
adamc@31 346 fun pushStrNamed env x sgis =
adamc@31 347 let
adamc@31 348 val n = !namedCounter
adamc@31 349 in
adamc@31 350 namedCounter := n + 1;
adamc@31 351 (pushStrNamedAs env x n sgis, n)
adamc@31 352 end
adamc@31 353
adamc@31 354 fun lookupStrNamed (env : env) n =
adamc@31 355 case IM.find (#str env, n) of
adamc@31 356 NONE => raise UnboundNamed n
adamc@31 357 | SOME x => x
adamc@31 358
adamc@31 359 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
adamc@31 360
adamc@156 361 fun sgiBinds env (sgi, loc) =
adamc@31 362 case sgi of
adamc@31 363 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@31 364 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@156 365 | SgiDatatype (x, n, xncs) =>
adamc@156 366 let
adamc@156 367 val env = pushCNamedAs env x n (KType, loc) NONE
adamc@156 368 in
adamc@156 369 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' (CNamed n, loc)
adamc@156 370 | ((x', n', SOME t), env) => pushENamedAs env x' n' (TFun (t, (CNamed n, loc)), loc))
adamc@156 371 env xncs
adamc@156 372 end
adamc@162 373 | SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
adamc@162 374 let
adamc@162 375 val env = pushCNamedAs env x n (KType, loc) (SOME (CModProj (m1, ms, x'), loc))
adamc@162 376 in
adamc@162 377 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' (CNamed n, loc)
adamc@162 378 | ((x', n', SOME t), env) => pushENamedAs env x' n' (TFun (t, (CNamed n, loc)), loc))
adamc@162 379 env xncs
adamc@162 380 end
adamc@31 381 | SgiVal (x, n, t) => pushENamedAs env x n t
adamc@31 382 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@59 383 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@88 384 | SgiConstraint _ => env
adamc@31 385
adamc@34 386 fun sgnSeek f sgis =
adamc@34 387 let
adamc@59 388 fun seek (sgis, sgns, strs, cons) =
adamc@34 389 case sgis of
adamc@34 390 [] => NONE
adamc@34 391 | (sgi, _) :: sgis =>
adamc@34 392 case f sgi of
adamc@158 393 SOME v =>
adamc@158 394 let
adamc@158 395 val cons =
adamc@158 396 case sgi of
adamc@158 397 SgiDatatype (x, n, _) => IM.insert (cons, n, x)
adamc@162 398 | SgiDatatypeImp (x, n, _, _, _, _) => IM.insert (cons, n, x)
adamc@158 399 | _ => cons
adamc@158 400 in
adamc@158 401 SOME (v, (sgns, strs, cons))
adamc@158 402 end
adamc@88 403 | NONE =>
adamc@88 404 case sgi of
adamc@88 405 SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@88 406 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@156 407 | SgiDatatype (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@162 408 | SgiDatatypeImp (x, n, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@88 409 | SgiVal _ => seek (sgis, sgns, strs, cons)
adamc@88 410 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons)
adamc@88 411 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons)
adamc@88 412 | SgiConstraint _ => seek (sgis, sgns, strs, cons)
adamc@34 413 in
adamc@59 414 seek (sgis, IM.empty, IM.empty, IM.empty)
adamc@34 415 end
adamc@34 416
adamc@34 417 fun id x = x
adamc@34 418
adamc@34 419 fun unravelStr (str, _) =
adamc@34 420 case str of
adamc@34 421 StrVar x => (x, [])
adamc@34 422 | StrProj (str, m) =>
adamc@34 423 let
adamc@34 424 val (x, ms) = unravelStr str
adamc@34 425 in
adamc@34 426 (x, ms @ [m])
adamc@34 427 end
adamc@34 428 | _ => raise Fail "unravelStr"
adamc@34 429
adamc@59 430 fun sgnS_con (str, (sgns, strs, cons)) c =
adamc@34 431 case c of
adamc@34 432 CModProj (m1, ms, x) =>
adamc@34 433 (case IM.find (strs, m1) of
adamc@34 434 NONE => c
adamc@34 435 | SOME m1x =>
adamc@34 436 let
adamc@34 437 val (m1, ms') = unravelStr str
adamc@34 438 in
adamc@34 439 CModProj (m1, ms' @ m1x :: ms, x)
adamc@34 440 end)
adamc@34 441 | CNamed n =>
adamc@34 442 (case IM.find (cons, n) of
adamc@34 443 NONE => c
adamc@34 444 | SOME nx =>
adamc@34 445 let
adamc@34 446 val (m1, ms) = unravelStr str
adamc@34 447 in
adamc@34 448 CModProj (m1, ms, nx)
adamc@34 449 end)
adamc@34 450 | _ => c
adamc@34 451
adamc@59 452 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
adamc@59 453 case sgn of
adamc@59 454 SgnProj (m1, ms, x) =>
adamc@59 455 (case IM.find (strs, m1) of
adamc@59 456 NONE => sgn
adamc@59 457 | SOME m1x =>
adamc@59 458 let
adamc@59 459 val (m1, ms') = unravelStr str
adamc@59 460 in
adamc@59 461 SgnProj (m1, ms' @ m1x :: ms, x)
adamc@59 462 end)
adamc@59 463 | SgnVar n =>
adamc@59 464 (case IM.find (sgns, n) of
adamc@59 465 NONE => sgn
adamc@59 466 | SOME nx =>
adamc@59 467 let
adamc@59 468 val (m1, ms) = unravelStr str
adamc@59 469 in
adamc@59 470 SgnProj (m1, ms, nx)
adamc@59 471 end)
adamc@59 472 | _ => sgn
adamc@59 473
adamc@59 474 fun sgnSubCon x =
adamc@34 475 ElabUtil.Con.map {kind = id,
adamc@59 476 con = sgnS_con x}
adamc@34 477
adamc@59 478 fun sgnSubSgn x =
adamc@34 479 ElabUtil.Sgn.map {kind = id,
adamc@59 480 con = sgnS_con x,
adamc@34 481 sgn_item = id,
adamc@59 482 sgn = sgnS_sgn x}
adamc@34 483
adamc@42 484 fun hnormSgn env (all as (sgn, loc)) =
adamc@34 485 case sgn of
adamc@42 486 SgnError => all
adamc@42 487 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
adamc@42 488 | SgnConst _ => all
adamc@42 489 | SgnFun _ => all
adamc@59 490 | SgnProj (m, ms, x) =>
adamc@59 491 let
adamc@59 492 val (_, sgn) = lookupStrNamed env m
adamc@59 493 in
adamc@59 494 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
adamc@59 495 sgn = sgn,
adamc@59 496 field = x} of
adamc@59 497 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
adamc@59 498 | SOME sgn => sgn
adamc@59 499 end
adamc@42 500 | SgnWhere (sgn, x, c) =>
adamc@42 501 case #1 (hnormSgn env sgn) of
adamc@42 502 SgnError => (SgnError, loc)
adamc@42 503 | SgnConst sgis =>
adamc@42 504 let
adamc@42 505 fun traverse (pre, post) =
adamc@42 506 case post of
adamc@42 507 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
adamc@42 508 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
adamc@42 509 if x = x' then
adamc@42 510 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
adamc@42 511 else
adamc@42 512 traverse (sgi :: pre, rest)
adamc@42 513 | sgi :: rest => traverse (sgi :: pre, rest)
adamc@42 514
adamc@42 515 val sgis = traverse ([], sgis)
adamc@42 516 in
adamc@42 517 (SgnConst sgis, loc)
adamc@42 518 end
adamc@42 519 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
adamc@42 520
adamc@59 521 and projectSgn env {sgn, str, field} =
adamc@59 522 case #1 (hnormSgn env sgn) of
adamc@59 523 SgnConst sgis =>
adamc@59 524 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 525 NONE => NONE
adamc@59 526 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 527 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 528 | _ => NONE
adamc@59 529
adamc@59 530 fun projectStr env {sgn, str, field} =
adamc@59 531 case #1 (hnormSgn env sgn) of
adamc@59 532 SgnConst sgis =>
adamc@59 533 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 534 NONE => NONE
adamc@59 535 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 536 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 537 | _ => NONE
adamc@59 538
adamc@158 539 fun chaseMpath env (n, ms) =
adamc@158 540 let
adamc@158 541 val (_, sgn) = lookupStrNamed env n
adamc@158 542 in
adamc@158 543 foldl (fn (m, (str, sgn)) =>
adamc@158 544 case projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 545 NONE => raise Fail "kindof: Unknown substructure"
adamc@158 546 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
adamc@158 547 ((StrVar n, #2 sgn), sgn) ms
adamc@158 548 end
adamc@158 549
adamc@42 550 fun projectCon env {sgn, str, field} =
adamc@42 551 case #1 (hnormSgn env sgn) of
adamc@34 552 SgnConst sgis =>
adamc@34 553 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
adamc@34 554 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
adamc@157 555 | SgiDatatype (x, _, _) => if x = field then SOME ((KType, #2 sgn), NONE) else NONE
adamc@162 556 | SgiDatatypeImp (x, _, m1, ms, x', _) =>
adamc@158 557 if x = field then
adamc@158 558 SOME ((KType, #2 sgn), SOME (CModProj (m1, ms, x'), #2 sgn))
adamc@158 559 else
adamc@158 560 NONE
adamc@34 561 | _ => NONE) sgis of
adamc@34 562 NONE => NONE
adamc@34 563 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
adamc@34 564 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
adamc@42 565 | _ => NONE
adamc@34 566
adamc@157 567 fun projectDatatype env {sgn, str, field} =
adamc@157 568 case #1 (hnormSgn env sgn) of
adamc@157 569 SgnConst sgis =>
adamc@157 570 (case sgnSeek (fn SgiDatatype (x, _, xncs) => if x = field then SOME xncs else NONE
adamc@162 571 | SgiDatatypeImp (x, _, _, _, _, xncs) => if x = field then SOME xncs else NONE
adamc@157 572 | _ => NONE) sgis of
adamc@157 573 NONE => NONE
adamc@157 574 | SOME (xncs, subs) => SOME (map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
adamc@157 575 | _ => NONE
adamc@157 576
adamc@174 577 fun projectConstructor env {sgn, str, field} =
adamc@174 578 case #1 (hnormSgn env sgn) of
adamc@174 579 SgnConst sgis =>
adamc@174 580 let
adamc@174 581 fun consider (n, xncs) =
adamc@174 582 ListUtil.search (fn (x, n', to) =>
adamc@174 583 if x <> field then
adamc@174 584 NONE
adamc@174 585 else
adamc@188 586 SOME (U.classifyDatatype xncs, n', to, (CNamed n, #2 str))) xncs
adamc@174 587 in
adamc@174 588 case sgnSeek (fn SgiDatatype (_, n, xncs) => consider (n, xncs)
adamc@174 589 | SgiDatatypeImp (_, n, _, _, _, xncs) => consider (n, xncs)
adamc@174 590 | _ => NONE) sgis of
adamc@174 591 NONE => NONE
adamc@188 592 | SOME ((dk, n, to, t), subs) => SOME (dk, n, Option.map (sgnSubCon (str, subs)) to,
adamc@188 593 sgnSubCon (str, subs) t)
adamc@174 594 end
adamc@174 595 | _ => NONE
adamc@174 596
adamc@42 597 fun projectVal env {sgn, str, field} =
adamc@42 598 case #1 (hnormSgn env sgn) of
adamc@34 599 SgnConst sgis =>
adamc@162 600 let
adamc@162 601 fun seek (n, xncs) =
adamc@162 602 ListUtil.search (fn (x, _, to) =>
adamc@162 603 if x = field then
adamc@162 604 SOME (case to of
adamc@162 605 NONE => (CNamed n, #2 sgn)
adamc@162 606 | SOME t => (TFun (t, (CNamed n, #2 sgn)), #2 sgn))
adamc@162 607 else
adamc@162 608 NONE) xncs
adamc@162 609 in
adamc@162 610 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
adamc@162 611 | SgiDatatype (_, n, xncs) => seek (n, xncs)
adamc@162 612 | SgiDatatypeImp (_, n, _, _, _, xncs) => seek (n, xncs)
adamc@162 613 | _ => NONE) sgis of
adamc@162 614 NONE => NONE
adamc@162 615 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
adamc@162 616 end
adamc@34 617 | SgnError => SOME (CError, ErrorMsg.dummySpan)
adamc@42 618 | _ => NONE
adamc@34 619
adamc@88 620 fun sgnSeekConstraints (str, sgis) =
adamc@88 621 let
adamc@88 622 fun seek (sgis, sgns, strs, cons, acc) =
adamc@88 623 case sgis of
adamc@88 624 [] => acc
adamc@88 625 | (sgi, _) :: sgis =>
adamc@88 626 case sgi of
adamc@88 627 SgiConstraint (c1, c2) =>
adamc@88 628 let
adamc@88 629 val sub = sgnSubCon (str, (sgns, strs, cons))
adamc@88 630 in
adamc@88 631 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
adamc@88 632 end
adamc@88 633 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 634 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@156 635 | SgiDatatype (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@162 636 | SgiDatatypeImp (x, n, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 637 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
adamc@88 638 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
adamc@88 639 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
adamc@88 640 in
adamc@88 641 seek (sgis, IM.empty, IM.empty, IM.empty, [])
adamc@88 642 end
adamc@88 643
adamc@88 644 fun projectConstraints env {sgn, str} =
adamc@88 645 case #1 (hnormSgn env sgn) of
adamc@88 646 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
adamc@88 647 | SgnError => SOME []
adamc@88 648 | _ => NONE
adamc@34 649
adamc@157 650 fun declBinds env (d, loc) =
adamc@157 651 case d of
adamc@157 652 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@157 653 | DDatatype (x, n, xncs) =>
adamc@157 654 let
adamc@157 655 val env = pushCNamedAs env x n (KType, loc) NONE
adamc@186 656 val env = pushDatatype env n xncs
adamc@157 657 in
adamc@157 658 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' (CNamed n, loc)
adamc@157 659 | ((x', n', SOME t), env) => pushENamedAs env x' n' (TFun (t, (CNamed n, loc)), loc))
adamc@157 660 env xncs
adamc@157 661 end
adamc@162 662 | DDatatypeImp (x, n, m, ms, x', xncs) =>
adamc@157 663 let
adamc@157 664 val t = (CModProj (m, ms, x'), loc)
adamc@157 665 val env = pushCNamedAs env x n (KType, loc) (SOME t)
adamc@186 666 val env = pushDatatype env n xncs
adamc@157 667
adamc@157 668 val t = (CNamed n, loc)
adamc@157 669 in
adamc@157 670 foldl (fn ((x', n', NONE), env) => pushENamedAs env x' n' t
adamc@157 671 | ((x', n', SOME t'), env) => pushENamedAs env x' n' (TFun (t', t), loc))
adamc@157 672 env xncs
adamc@157 673 end
adamc@157 674 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@157 675 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
adamc@157 676 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@157 677 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
adamc@157 678 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@157 679 | DConstraint _ => env
adamc@157 680 | DExport _ => env
adamc@157 681
adamc@2 682 end