annotate src/elab_env.sml @ 45:3c1ce1b4eb3d

Explifying functors
author Adam Chlipala <adamc@hcoop.net>
date Thu, 19 Jun 2008 17:11:24 -0400
parents b3fbbc6cb1e5
children 0a5c312de09a
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@2 76 type env = {
adamc@9 77 renameC : kind var' SM.map,
adamc@2 78 relC : (string * kind) list,
adamc@11 79 namedC : (string * kind * con option) IM.map,
adamc@9 80
adamc@9 81 renameE : con var' SM.map,
adamc@9 82 relE : (string * con) list,
adamc@31 83 namedE : (string * con) IM.map,
adamc@31 84
adamc@31 85 renameSgn : (int * sgn) SM.map,
adamc@31 86 sgn : (string * sgn) IM.map,
adamc@31 87
adamc@31 88 renameStr : (int * sgn) SM.map,
adamc@31 89 str : (string * sgn) IM.map
adamc@2 90 }
adamc@2 91
adamc@2 92 val namedCounter = ref 0
adamc@2 93
adamc@2 94 val empty = {
adamc@2 95 renameC = SM.empty,
adamc@2 96 relC = [],
adamc@9 97 namedC = IM.empty,
adamc@9 98
adamc@9 99 renameE = SM.empty,
adamc@9 100 relE = [],
adamc@31 101 namedE = IM.empty,
adamc@31 102
adamc@31 103 renameSgn = SM.empty,
adamc@31 104 sgn = IM.empty,
adamc@31 105
adamc@31 106 renameStr = SM.empty,
adamc@31 107 str = IM.empty
adamc@2 108 }
adamc@2 109
adamc@2 110 fun pushCRel (env : env) x k =
adamc@2 111 let
adamc@9 112 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
adamc@2 113 | x => x) (#renameC env)
adamc@2 114 in
adamc@9 115 {renameC = SM.insert (renameC, x, Rel' (0, k)),
adamc@2 116 relC = (x, k) :: #relC env,
adamc@13 117 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
adamc@9 118
adamc@9 119 renameE = #renameE env,
adamc@13 120 relE = map (fn (x, c) => (x, lift c)) (#relE env),
adamc@31 121 namedE = IM.map (fn (x, c) => (x, lift c)) (#namedE env),
adamc@31 122
adamc@31 123 renameSgn = #renameSgn env,
adamc@31 124 sgn = #sgn env,
adamc@31 125
adamc@31 126 renameStr = #renameStr env,
adamc@31 127 str = #str env
adamc@31 128 }
adamc@2 129 end
adamc@2 130
adamc@2 131 fun lookupCRel (env : env) n =
adamc@2 132 (List.nth (#relC env, n))
adamc@2 133 handle Subscript => raise UnboundRel n
adamc@2 134
adamc@11 135 fun pushCNamedAs (env : env) x n k co =
adamc@9 136 {renameC = SM.insert (#renameC env, x, Named' (n, k)),
adamc@5 137 relC = #relC env,
adamc@11 138 namedC = IM.insert (#namedC env, n, (x, k, co)),
adamc@9 139
adamc@9 140 renameE = #renameE env,
adamc@9 141 relE = #relE env,
adamc@31 142 namedE = #namedE env,
adamc@31 143
adamc@31 144 renameSgn = #renameSgn env,
adamc@31 145 sgn = #sgn env,
adamc@31 146
adamc@31 147 renameStr = #renameStr env,
adamc@31 148 str = #str env}
adamc@5 149
adamc@11 150 fun pushCNamed env x k co =
adamc@2 151 let
adamc@2 152 val n = !namedCounter
adamc@2 153 in
adamc@2 154 namedCounter := n + 1;
adamc@11 155 (pushCNamedAs env x n k co, n)
adamc@2 156 end
adamc@2 157
adamc@2 158 fun lookupCNamed (env : env) n =
adamc@2 159 case IM.find (#namedC env, n) of
adamc@2 160 NONE => raise UnboundNamed n
adamc@2 161 | SOME x => x
adamc@2 162
adamc@2 163 fun lookupC (env : env) x =
adamc@2 164 case SM.find (#renameC env, x) of
adamc@9 165 NONE => NotBound
adamc@9 166 | SOME (Rel' x) => Rel x
adamc@9 167 | SOME (Named' x) => Named x
adamc@9 168
adamc@9 169 fun pushERel (env : env) x t =
adamc@9 170 let
adamc@9 171 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
adamc@9 172 | x => x) (#renameE env)
adamc@9 173 in
adamc@9 174 {renameC = #renameC env,
adamc@9 175 relC = #relC env,
adamc@9 176 namedC = #namedC env,
adamc@9 177
adamc@9 178 renameE = SM.insert (renameE, x, Rel' (0, t)),
adamc@9 179 relE = (x, t) :: #relE env,
adamc@31 180 namedE = #namedE env,
adamc@31 181
adamc@31 182 renameSgn = #renameSgn env,
adamc@31 183 sgn = #sgn env,
adamc@31 184
adamc@31 185 renameStr = #renameStr env,
adamc@31 186 str = #str env}
adamc@9 187 end
adamc@9 188
adamc@9 189 fun lookupERel (env : env) n =
adamc@9 190 (List.nth (#relE env, n))
adamc@9 191 handle Subscript => raise UnboundRel n
adamc@9 192
adamc@9 193 fun pushENamedAs (env : env) x n t =
adamc@9 194 {renameC = #renameC env,
adamc@9 195 relC = #relC env,
adamc@9 196 namedC = #namedC env,
adamc@9 197
adamc@9 198 renameE = SM.insert (#renameE env, x, Named' (n, t)),
adamc@9 199 relE = #relE env,
adamc@31 200 namedE = IM.insert (#namedE env, n, (x, t)),
adamc@31 201
adamc@31 202 renameSgn = #renameSgn env,
adamc@31 203 sgn = #sgn env,
adamc@31 204
adamc@31 205 renameStr = #renameStr env,
adamc@31 206 str = #str env}
adamc@9 207
adamc@9 208 fun pushENamed env x t =
adamc@9 209 let
adamc@9 210 val n = !namedCounter
adamc@9 211 in
adamc@9 212 namedCounter := n + 1;
adamc@9 213 (pushENamedAs env x n t, n)
adamc@9 214 end
adamc@9 215
adamc@9 216 fun lookupENamed (env : env) n =
adamc@9 217 case IM.find (#namedE env, n) of
adamc@9 218 NONE => raise UnboundNamed n
adamc@9 219 | SOME x => x
adamc@9 220
adamc@9 221 fun lookupE (env : env) x =
adamc@9 222 case SM.find (#renameE env, x) of
adamc@9 223 NONE => NotBound
adamc@9 224 | SOME (Rel' x) => Rel x
adamc@9 225 | SOME (Named' x) => Named x
adamc@2 226
adamc@31 227 fun pushSgnNamedAs (env : env) x n sgis =
adamc@31 228 {renameC = #renameC env,
adamc@31 229 relC = #relC env,
adamc@31 230 namedC = #namedC env,
adamc@31 231
adamc@31 232 renameE = #renameE env,
adamc@31 233 relE = #relE env,
adamc@31 234 namedE = #namedE env,
adamc@31 235
adamc@31 236 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
adamc@31 237 sgn = IM.insert (#sgn env, n, (x, sgis)),
adamc@31 238
adamc@31 239 renameStr = #renameStr env,
adamc@31 240 str = #str env}
adamc@31 241
adamc@31 242 fun pushSgnNamed env x sgis =
adamc@31 243 let
adamc@31 244 val n = !namedCounter
adamc@31 245 in
adamc@31 246 namedCounter := n + 1;
adamc@31 247 (pushSgnNamedAs env x n sgis, n)
adamc@31 248 end
adamc@31 249
adamc@31 250 fun lookupSgnNamed (env : env) n =
adamc@31 251 case IM.find (#sgn env, n) of
adamc@31 252 NONE => raise UnboundNamed n
adamc@31 253 | SOME x => x
adamc@31 254
adamc@31 255 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
adamc@31 256
adamc@31 257 fun pushStrNamedAs (env : env) x n sgis =
adamc@31 258 {renameC = #renameC env,
adamc@31 259 relC = #relC env,
adamc@31 260 namedC = #namedC env,
adamc@31 261
adamc@31 262 renameE = #renameE env,
adamc@31 263 relE = #relE env,
adamc@31 264 namedE = #namedE env,
adamc@31 265
adamc@31 266 renameSgn = #renameSgn env,
adamc@31 267 sgn = #sgn env,
adamc@31 268
adamc@31 269 renameStr = SM.insert (#renameStr env, x, (n, sgis)),
adamc@31 270 str = IM.insert (#str env, n, (x, sgis))}
adamc@31 271
adamc@31 272 fun pushStrNamed env x sgis =
adamc@31 273 let
adamc@31 274 val n = !namedCounter
adamc@31 275 in
adamc@31 276 namedCounter := n + 1;
adamc@31 277 (pushStrNamedAs env x n sgis, n)
adamc@31 278 end
adamc@31 279
adamc@31 280 fun lookupStrNamed (env : env) n =
adamc@31 281 case IM.find (#str env, n) of
adamc@31 282 NONE => raise UnboundNamed n
adamc@31 283 | SOME x => x
adamc@31 284
adamc@31 285 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
adamc@31 286
adamc@13 287 fun declBinds env (d, _) =
adamc@13 288 case d of
adamc@13 289 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@13 290 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@31 291 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@31 292 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
adamc@31 293
adamc@31 294 fun sgiBinds env (sgi, _) =
adamc@31 295 case sgi of
adamc@31 296 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@31 297 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@31 298 | SgiVal (x, n, t) => pushENamedAs env x n t
adamc@31 299 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@31 300
adamc@34 301 fun sgnSeek f sgis =
adamc@34 302 let
adamc@34 303 fun seek (sgis, strs, cons) =
adamc@34 304 case sgis of
adamc@34 305 [] => NONE
adamc@34 306 | (sgi, _) :: sgis =>
adamc@34 307 case f sgi of
adamc@34 308 SOME v => SOME (v, (strs, cons))
adamc@34 309 | NONE =>
adamc@34 310 case sgi of
adamc@34 311 SgiConAbs (x, n, _) => seek (sgis, strs, IM.insert (cons, n, x))
adamc@34 312 | SgiCon (x, n, _, _) => seek (sgis, strs, IM.insert (cons, n, x))
adamc@34 313 | SgiVal _ => seek (sgis, strs, cons)
adamc@34 314 | SgiStr (x, n, _) => seek (sgis, IM.insert (strs, n, x), cons)
adamc@34 315 in
adamc@34 316 seek (sgis, IM.empty, IM.empty)
adamc@34 317 end
adamc@34 318
adamc@34 319 fun id x = x
adamc@34 320
adamc@34 321 fun unravelStr (str, _) =
adamc@34 322 case str of
adamc@34 323 StrVar x => (x, [])
adamc@34 324 | StrProj (str, m) =>
adamc@34 325 let
adamc@34 326 val (x, ms) = unravelStr str
adamc@34 327 in
adamc@34 328 (x, ms @ [m])
adamc@34 329 end
adamc@34 330 | _ => raise Fail "unravelStr"
adamc@34 331
adamc@34 332 fun sgnS_con (str, (strs, cons)) c =
adamc@34 333 case c of
adamc@34 334 CModProj (m1, ms, x) =>
adamc@34 335 (case IM.find (strs, m1) of
adamc@34 336 NONE => c
adamc@34 337 | SOME m1x =>
adamc@34 338 let
adamc@34 339 val (m1, ms') = unravelStr str
adamc@34 340 in
adamc@34 341 CModProj (m1, ms' @ m1x :: ms, x)
adamc@34 342 end)
adamc@34 343 | CNamed n =>
adamc@34 344 (case IM.find (cons, n) of
adamc@34 345 NONE => c
adamc@34 346 | SOME nx =>
adamc@34 347 let
adamc@34 348 val (m1, ms) = unravelStr str
adamc@34 349 in
adamc@34 350 CModProj (m1, ms, nx)
adamc@34 351 end)
adamc@34 352 | _ => c
adamc@34 353
adamc@34 354 fun sgnSubCon (str, (strs, cons)) =
adamc@34 355 ElabUtil.Con.map {kind = id,
adamc@34 356 con = sgnS_con (str, (strs, cons))}
adamc@34 357
adamc@34 358 fun sgnSubSgn (str, (strs, cons)) =
adamc@34 359 ElabUtil.Sgn.map {kind = id,
adamc@34 360 con = sgnS_con (str, (strs, cons)),
adamc@34 361 sgn_item = id,
adamc@34 362 sgn = id}
adamc@34 363
adamc@42 364 fun hnormSgn env (all as (sgn, loc)) =
adamc@34 365 case sgn of
adamc@42 366 SgnError => all
adamc@42 367 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
adamc@42 368 | SgnConst _ => all
adamc@42 369 | SgnFun _ => all
adamc@42 370 | SgnWhere (sgn, x, c) =>
adamc@42 371 case #1 (hnormSgn env sgn) of
adamc@42 372 SgnError => (SgnError, loc)
adamc@42 373 | SgnConst sgis =>
adamc@42 374 let
adamc@42 375 fun traverse (pre, post) =
adamc@42 376 case post of
adamc@42 377 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
adamc@42 378 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
adamc@42 379 if x = x' then
adamc@42 380 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
adamc@42 381 else
adamc@42 382 traverse (sgi :: pre, rest)
adamc@42 383 | sgi :: rest => traverse (sgi :: pre, rest)
adamc@42 384
adamc@42 385 val sgis = traverse ([], sgis)
adamc@42 386 in
adamc@42 387 (SgnConst sgis, loc)
adamc@42 388 end
adamc@42 389 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
adamc@42 390
adamc@42 391 fun projectCon env {sgn, str, field} =
adamc@42 392 case #1 (hnormSgn env sgn) of
adamc@34 393 SgnConst sgis =>
adamc@34 394 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
adamc@34 395 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
adamc@34 396 | _ => NONE) sgis of
adamc@34 397 NONE => NONE
adamc@34 398 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
adamc@34 399 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
adamc@42 400 | _ => NONE
adamc@34 401
adamc@42 402 fun projectVal env {sgn, str, field} =
adamc@42 403 case #1 (hnormSgn env sgn) of
adamc@34 404 SgnConst sgis =>
adamc@34 405 (case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE | _ => NONE) sgis of
adamc@34 406 NONE => NONE
adamc@34 407 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c))
adamc@34 408 | SgnError => SOME (CError, ErrorMsg.dummySpan)
adamc@42 409 | _ => NONE
adamc@34 410
adamc@42 411 fun projectStr env {sgn, str, field} =
adamc@42 412 case #1 (hnormSgn env sgn) of
adamc@34 413 SgnConst sgis =>
adamc@34 414 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@34 415 NONE => NONE
adamc@34 416 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@34 417 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@42 418 | _ => NONE
adamc@34 419
adamc@13 420
adamc@14 421 val ktype = (KType, ErrorMsg.dummySpan)
adamc@14 422
adamc@14 423 fun bbind env x = #1 (pushCNamed env x ktype NONE)
adamc@14 424
adamc@14 425 val basis = empty
adamc@14 426 val basis = bbind basis "int"
adamc@14 427 val basis = bbind basis "float"
adamc@14 428 val basis = bbind basis "string"
adamc@14 429
adamc@2 430 end