annotate src/elab_env.sml @ 195:85b5f663bb86

Tuples syntactic sugar
author Adam Chlipala <adamc@hcoop.net>
date Sat, 09 Aug 2008 12:50:49 -0400
parents aa54250f58ac
children dd82457fda82
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@34 407 fun sgnSeek f sgis =
adamc@34 408 let
adamc@59 409 fun seek (sgis, sgns, strs, cons) =
adamc@34 410 case sgis of
adamc@34 411 [] => NONE
adamc@34 412 | (sgi, _) :: sgis =>
adamc@34 413 case f sgi of
adamc@158 414 SOME v =>
adamc@158 415 let
adamc@158 416 val cons =
adamc@158 417 case sgi of
adamc@191 418 SgiDatatype (x, n, _, _) => IM.insert (cons, n, x)
adamc@191 419 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
adamc@158 420 | _ => cons
adamc@158 421 in
adamc@158 422 SOME (v, (sgns, strs, cons))
adamc@158 423 end
adamc@88 424 | NONE =>
adamc@88 425 case sgi of
adamc@88 426 SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@88 427 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@191 428 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@191 429 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x))
adamc@88 430 | SgiVal _ => seek (sgis, sgns, strs, cons)
adamc@88 431 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons)
adamc@88 432 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons)
adamc@88 433 | SgiConstraint _ => seek (sgis, sgns, strs, cons)
adamc@34 434 in
adamc@59 435 seek (sgis, IM.empty, IM.empty, IM.empty)
adamc@34 436 end
adamc@34 437
adamc@34 438 fun id x = x
adamc@34 439
adamc@34 440 fun unravelStr (str, _) =
adamc@34 441 case str of
adamc@34 442 StrVar x => (x, [])
adamc@34 443 | StrProj (str, m) =>
adamc@34 444 let
adamc@34 445 val (x, ms) = unravelStr str
adamc@34 446 in
adamc@34 447 (x, ms @ [m])
adamc@34 448 end
adamc@34 449 | _ => raise Fail "unravelStr"
adamc@34 450
adamc@59 451 fun sgnS_con (str, (sgns, strs, cons)) c =
adamc@34 452 case c of
adamc@34 453 CModProj (m1, ms, x) =>
adamc@34 454 (case IM.find (strs, m1) of
adamc@34 455 NONE => c
adamc@34 456 | SOME m1x =>
adamc@34 457 let
adamc@34 458 val (m1, ms') = unravelStr str
adamc@34 459 in
adamc@34 460 CModProj (m1, ms' @ m1x :: ms, x)
adamc@34 461 end)
adamc@34 462 | CNamed n =>
adamc@34 463 (case IM.find (cons, n) of
adamc@34 464 NONE => c
adamc@34 465 | SOME nx =>
adamc@34 466 let
adamc@34 467 val (m1, ms) = unravelStr str
adamc@34 468 in
adamc@34 469 CModProj (m1, ms, nx)
adamc@34 470 end)
adamc@34 471 | _ => c
adamc@34 472
adamc@59 473 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
adamc@59 474 case sgn of
adamc@59 475 SgnProj (m1, ms, x) =>
adamc@59 476 (case IM.find (strs, m1) of
adamc@59 477 NONE => sgn
adamc@59 478 | SOME m1x =>
adamc@59 479 let
adamc@59 480 val (m1, ms') = unravelStr str
adamc@59 481 in
adamc@59 482 SgnProj (m1, ms' @ m1x :: ms, x)
adamc@59 483 end)
adamc@59 484 | SgnVar n =>
adamc@59 485 (case IM.find (sgns, n) of
adamc@59 486 NONE => sgn
adamc@59 487 | SOME nx =>
adamc@59 488 let
adamc@59 489 val (m1, ms) = unravelStr str
adamc@59 490 in
adamc@59 491 SgnProj (m1, ms, nx)
adamc@59 492 end)
adamc@59 493 | _ => sgn
adamc@59 494
adamc@59 495 fun sgnSubCon x =
adamc@34 496 ElabUtil.Con.map {kind = id,
adamc@59 497 con = sgnS_con x}
adamc@34 498
adamc@59 499 fun sgnSubSgn x =
adamc@34 500 ElabUtil.Sgn.map {kind = id,
adamc@59 501 con = sgnS_con x,
adamc@34 502 sgn_item = id,
adamc@59 503 sgn = sgnS_sgn x}
adamc@34 504
adamc@42 505 fun hnormSgn env (all as (sgn, loc)) =
adamc@34 506 case sgn of
adamc@42 507 SgnError => all
adamc@42 508 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
adamc@42 509 | SgnConst _ => all
adamc@42 510 | SgnFun _ => all
adamc@59 511 | SgnProj (m, ms, x) =>
adamc@59 512 let
adamc@59 513 val (_, sgn) = lookupStrNamed env m
adamc@59 514 in
adamc@59 515 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
adamc@59 516 sgn = sgn,
adamc@59 517 field = x} of
adamc@59 518 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
adamc@59 519 | SOME sgn => sgn
adamc@59 520 end
adamc@42 521 | SgnWhere (sgn, x, c) =>
adamc@42 522 case #1 (hnormSgn env sgn) of
adamc@42 523 SgnError => (SgnError, loc)
adamc@42 524 | SgnConst sgis =>
adamc@42 525 let
adamc@42 526 fun traverse (pre, post) =
adamc@42 527 case post of
adamc@42 528 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
adamc@42 529 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
adamc@42 530 if x = x' then
adamc@42 531 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
adamc@42 532 else
adamc@42 533 traverse (sgi :: pre, rest)
adamc@42 534 | sgi :: rest => traverse (sgi :: pre, rest)
adamc@42 535
adamc@42 536 val sgis = traverse ([], sgis)
adamc@42 537 in
adamc@42 538 (SgnConst sgis, loc)
adamc@42 539 end
adamc@42 540 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
adamc@42 541
adamc@59 542 and projectSgn env {sgn, str, field} =
adamc@59 543 case #1 (hnormSgn env sgn) of
adamc@59 544 SgnConst sgis =>
adamc@59 545 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 546 NONE => NONE
adamc@59 547 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 548 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 549 | _ => NONE
adamc@59 550
adamc@59 551 fun projectStr env {sgn, str, field} =
adamc@59 552 case #1 (hnormSgn env sgn) of
adamc@59 553 SgnConst sgis =>
adamc@59 554 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 555 NONE => NONE
adamc@59 556 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 557 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 558 | _ => NONE
adamc@59 559
adamc@158 560 fun chaseMpath env (n, ms) =
adamc@158 561 let
adamc@158 562 val (_, sgn) = lookupStrNamed env n
adamc@158 563 in
adamc@158 564 foldl (fn (m, (str, sgn)) =>
adamc@158 565 case projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 566 NONE => raise Fail "kindof: Unknown substructure"
adamc@158 567 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
adamc@158 568 ((StrVar n, #2 sgn), sgn) ms
adamc@158 569 end
adamc@158 570
adamc@42 571 fun projectCon env {sgn, str, field} =
adamc@42 572 case #1 (hnormSgn env sgn) of
adamc@34 573 SgnConst sgis =>
adamc@34 574 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
adamc@34 575 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
adamc@191 576 | SgiDatatype (x, _, _, _) => if x = field then SOME ((KType, #2 sgn), NONE) else NONE
adamc@191 577 | SgiDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@158 578 if x = field then
adamc@158 579 SOME ((KType, #2 sgn), SOME (CModProj (m1, ms, x'), #2 sgn))
adamc@158 580 else
adamc@158 581 NONE
adamc@34 582 | _ => NONE) sgis of
adamc@34 583 NONE => NONE
adamc@34 584 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
adamc@34 585 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
adamc@42 586 | _ => NONE
adamc@34 587
adamc@157 588 fun projectDatatype env {sgn, str, field} =
adamc@157 589 case #1 (hnormSgn env sgn) of
adamc@157 590 SgnConst sgis =>
adamc@191 591 (case sgnSeek (fn SgiDatatype (x, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@191 592 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@157 593 | _ => NONE) sgis of
adamc@157 594 NONE => NONE
adamc@191 595 | SOME ((xs, xncs), subs) => SOME (xs,
adamc@191 596 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
adamc@157 597 | _ => NONE
adamc@157 598
adamc@174 599 fun projectConstructor env {sgn, str, field} =
adamc@174 600 case #1 (hnormSgn env sgn) of
adamc@174 601 SgnConst sgis =>
adamc@174 602 let
adamc@191 603 fun consider (n, xs, xncs) =
adamc@174 604 ListUtil.search (fn (x, n', to) =>
adamc@174 605 if x <> field then
adamc@174 606 NONE
adamc@174 607 else
adamc@191 608 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
adamc@174 609 in
adamc@191 610 case sgnSeek (fn SgiDatatype (_, n, xs, xncs) => consider (n, xs, xncs)
adamc@191 611 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
adamc@174 612 | _ => NONE) sgis of
adamc@174 613 NONE => NONE
adamc@191 614 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
adamc@191 615 sgnSubCon (str, subs) t)
adamc@174 616 end
adamc@174 617 | _ => NONE
adamc@174 618
adamc@42 619 fun projectVal env {sgn, str, field} =
adamc@42 620 case #1 (hnormSgn env sgn) of
adamc@34 621 SgnConst sgis =>
adamc@162 622 let
adamc@191 623 fun seek (n, xs, xncs) =
adamc@162 624 ListUtil.search (fn (x, _, to) =>
adamc@162 625 if x = field then
adamc@191 626 SOME (let
adamc@191 627 val t =
adamc@191 628 case to of
adamc@191 629 NONE => (CNamed n, #2 sgn)
adamc@191 630 | SOME t => (TFun (t, (CNamed n, #2 sgn)), #2 sgn)
adamc@191 631 val k = (KType, #2 sgn)
adamc@191 632 in
adamc@191 633 foldr (fn (x, t) => (TCFun (Explicit, x, k, t), #2 sgn))
adamc@191 634 t xs
adamc@191 635 end)
adamc@162 636 else
adamc@162 637 NONE) xncs
adamc@162 638 in
adamc@162 639 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
adamc@191 640 | SgiDatatype (_, n, xs, xncs) => seek (n, xs, xncs)
adamc@191 641 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
adamc@162 642 | _ => NONE) sgis of
adamc@162 643 NONE => NONE
adamc@162 644 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
adamc@162 645 end
adamc@34 646 | SgnError => SOME (CError, ErrorMsg.dummySpan)
adamc@42 647 | _ => NONE
adamc@34 648
adamc@88 649 fun sgnSeekConstraints (str, sgis) =
adamc@88 650 let
adamc@88 651 fun seek (sgis, sgns, strs, cons, acc) =
adamc@88 652 case sgis of
adamc@88 653 [] => acc
adamc@88 654 | (sgi, _) :: sgis =>
adamc@88 655 case sgi of
adamc@88 656 SgiConstraint (c1, c2) =>
adamc@88 657 let
adamc@88 658 val sub = sgnSubCon (str, (sgns, strs, cons))
adamc@88 659 in
adamc@88 660 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
adamc@88 661 end
adamc@88 662 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 663 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@191 664 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@191 665 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 666 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
adamc@88 667 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
adamc@88 668 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
adamc@88 669 in
adamc@88 670 seek (sgis, IM.empty, IM.empty, IM.empty, [])
adamc@88 671 end
adamc@88 672
adamc@88 673 fun projectConstraints env {sgn, str} =
adamc@88 674 case #1 (hnormSgn env sgn) of
adamc@88 675 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
adamc@88 676 | SgnError => SOME []
adamc@88 677 | _ => NONE
adamc@34 678
adamc@157 679 fun declBinds env (d, loc) =
adamc@157 680 case d of
adamc@157 681 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@191 682 | DDatatype (x, n, xs, xncs) =>
adamc@157 683 let
adamc@157 684 val env = pushCNamedAs env x n (KType, loc) NONE
adamc@191 685 val env = pushDatatype env n xs xncs
adamc@157 686 in
adamc@191 687 foldl (fn ((x', n', to), env) =>
adamc@191 688 let
adamc@191 689 val t =
adamc@191 690 case to of
adamc@191 691 NONE => (CNamed n, loc)
adamc@191 692 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@191 693 val k = (KType, loc)
adamc@191 694 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@191 695 in
adamc@191 696 pushENamedAs env x' n' t
adamc@191 697 end)
adamc@191 698 env xncs
adamc@157 699 end
adamc@191 700 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
adamc@157 701 let
adamc@157 702 val t = (CModProj (m, ms, x'), loc)
adamc@157 703 val env = pushCNamedAs env x n (KType, loc) (SOME t)
adamc@191 704 val env = pushDatatype env n xs xncs
adamc@157 705
adamc@157 706 val t = (CNamed n, loc)
adamc@157 707 in
adamc@191 708 foldl (fn ((x', n', to), env) =>
adamc@191 709 let
adamc@191 710 val t =
adamc@191 711 case to of
adamc@191 712 NONE => (CNamed n, loc)
adamc@191 713 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@191 714 val k = (KType, loc)
adamc@191 715 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@191 716 in
adamc@191 717 pushENamedAs env x' n' t
adamc@191 718 end)
adamc@191 719 env xncs
adamc@157 720 end
adamc@157 721 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@157 722 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
adamc@157 723 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@157 724 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
adamc@157 725 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@157 726 | DConstraint _ => env
adamc@157 727 | DExport _ => env
adamc@157 728
adamc@2 729 end