annotate src/elab_env.sml @ 13:6049e2193bf2

Lifting cons in ElabEnv
author Adam Chlipala <adamc@hcoop.net>
date Sun, 08 Jun 2008 11:32:48 -0400
parents e97c6d335869
children f1c36df29ed7
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 L' = Elab
adamc@13 33 structure U = ElabUtil
adamc@13 34
adamc@2 35 structure IM = IntBinaryMap
adamc@2 36 structure SM = BinaryMapFn(struct
adamc@2 37 type ord_key = string
adamc@2 38 val compare = String.compare
adamc@2 39 end)
adamc@2 40
adamc@2 41 exception UnboundRel of int
adamc@2 42 exception UnboundNamed of int
adamc@2 43
adamc@13 44
adamc@13 45 (* AST utility functions *)
adamc@13 46
adamc@13 47 exception SynUnif
adamc@13 48
adamc@13 49 val liftConInCon =
adamc@13 50 U.Con.mapB {kind = fn k => k,
adamc@13 51 con = fn bound => fn c =>
adamc@13 52 case c of
adamc@13 53 L'.CRel xn =>
adamc@13 54 if xn < bound then
adamc@13 55 c
adamc@13 56 else
adamc@13 57 L'.CRel (xn + 1)
adamc@13 58 | L'.CUnif _ => raise SynUnif
adamc@13 59 | _ => c,
adamc@13 60 bind = fn (bound, U.Con.Rel _) => bound + 1
adamc@13 61 | (bound, _) => bound}
adamc@13 62
adamc@13 63 val lift = liftConInCon 0
adamc@13 64
adamc@13 65
adamc@13 66 (* Back to environments *)
adamc@13 67
adamc@9 68 datatype 'a var' =
adamc@9 69 Rel' of int * 'a
adamc@9 70 | Named' of int * 'a
adamc@2 71
adamc@9 72 datatype 'a var =
adamc@9 73 NotBound
adamc@9 74 | Rel of int * 'a
adamc@9 75 | Named of int * 'a
adamc@2 76
adamc@2 77 type env = {
adamc@9 78 renameC : kind var' SM.map,
adamc@2 79 relC : (string * kind) list,
adamc@11 80 namedC : (string * kind * con option) IM.map,
adamc@9 81
adamc@9 82 renameE : con var' SM.map,
adamc@9 83 relE : (string * con) list,
adamc@9 84 namedE : (string * con) IM.map
adamc@2 85 }
adamc@2 86
adamc@2 87 val namedCounter = ref 0
adamc@2 88
adamc@2 89 val empty = {
adamc@2 90 renameC = SM.empty,
adamc@2 91 relC = [],
adamc@9 92 namedC = IM.empty,
adamc@9 93
adamc@9 94 renameE = SM.empty,
adamc@9 95 relE = [],
adamc@9 96 namedE = IM.empty
adamc@2 97 }
adamc@2 98
adamc@2 99 fun pushCRel (env : env) x k =
adamc@2 100 let
adamc@9 101 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
adamc@2 102 | x => x) (#renameC env)
adamc@2 103 in
adamc@9 104 {renameC = SM.insert (renameC, x, Rel' (0, k)),
adamc@2 105 relC = (x, k) :: #relC env,
adamc@13 106 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
adamc@9 107
adamc@9 108 renameE = #renameE env,
adamc@13 109 relE = map (fn (x, c) => (x, lift c)) (#relE env),
adamc@13 110 namedE = IM.map (fn (x, c) => (x, lift c)) (#namedE env)}
adamc@2 111 end
adamc@2 112
adamc@2 113 fun lookupCRel (env : env) n =
adamc@2 114 (List.nth (#relC env, n))
adamc@2 115 handle Subscript => raise UnboundRel n
adamc@2 116
adamc@11 117 fun pushCNamedAs (env : env) x n k co =
adamc@9 118 {renameC = SM.insert (#renameC env, x, Named' (n, k)),
adamc@5 119 relC = #relC env,
adamc@11 120 namedC = IM.insert (#namedC env, n, (x, k, co)),
adamc@9 121
adamc@9 122 renameE = #renameE env,
adamc@9 123 relE = #relE env,
adamc@9 124 namedE = #namedE env}
adamc@5 125
adamc@11 126 fun pushCNamed env x k co =
adamc@2 127 let
adamc@2 128 val n = !namedCounter
adamc@2 129 in
adamc@2 130 namedCounter := n + 1;
adamc@11 131 (pushCNamedAs env x n k co, n)
adamc@2 132 end
adamc@2 133
adamc@2 134 fun lookupCNamed (env : env) n =
adamc@2 135 case IM.find (#namedC env, n) of
adamc@2 136 NONE => raise UnboundNamed n
adamc@2 137 | SOME x => x
adamc@2 138
adamc@2 139 fun lookupC (env : env) x =
adamc@2 140 case SM.find (#renameC env, x) of
adamc@9 141 NONE => NotBound
adamc@9 142 | SOME (Rel' x) => Rel x
adamc@9 143 | SOME (Named' x) => Named x
adamc@9 144
adamc@9 145 fun pushERel (env : env) x t =
adamc@9 146 let
adamc@9 147 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
adamc@9 148 | x => x) (#renameE env)
adamc@9 149 in
adamc@9 150 {renameC = #renameC env,
adamc@9 151 relC = #relC env,
adamc@9 152 namedC = #namedC env,
adamc@9 153
adamc@9 154 renameE = SM.insert (renameE, x, Rel' (0, t)),
adamc@9 155 relE = (x, t) :: #relE env,
adamc@9 156 namedE = #namedE env}
adamc@9 157 end
adamc@9 158
adamc@9 159 fun lookupERel (env : env) n =
adamc@9 160 (List.nth (#relE env, n))
adamc@9 161 handle Subscript => raise UnboundRel n
adamc@9 162
adamc@9 163 fun pushENamedAs (env : env) x n t =
adamc@9 164 {renameC = #renameC env,
adamc@9 165 relC = #relC env,
adamc@9 166 namedC = #namedC env,
adamc@9 167
adamc@9 168 renameE = SM.insert (#renameE env, x, Named' (n, t)),
adamc@9 169 relE = #relE env,
adamc@9 170 namedE = IM.insert (#namedE env, n, (x, t))}
adamc@9 171
adamc@9 172 fun pushENamed env x t =
adamc@9 173 let
adamc@9 174 val n = !namedCounter
adamc@9 175 in
adamc@9 176 namedCounter := n + 1;
adamc@9 177 (pushENamedAs env x n t, n)
adamc@9 178 end
adamc@9 179
adamc@9 180 fun lookupENamed (env : env) n =
adamc@9 181 case IM.find (#namedE env, n) of
adamc@9 182 NONE => raise UnboundNamed n
adamc@9 183 | SOME x => x
adamc@9 184
adamc@9 185 fun lookupE (env : env) x =
adamc@9 186 case SM.find (#renameE 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@2 190
adamc@13 191 fun declBinds env (d, _) =
adamc@13 192 case d of
adamc@13 193 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@13 194 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@13 195
adamc@2 196 end