annotate src/elab_env.sml @ 207:cc68da3801bc

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