annotate src/elab_env.sml @ 160:870e8abbe3b9

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