annotate src/elab_env.sml @ 17:9bd8669d53c2

Further experiments with implicit arguments
author Adam Chlipala <adamc@hcoop.net>
date Sun, 08 Jun 2008 14:10:51 -0400
parents bc7b76ca57e0
children 1c91c5e6840f
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@9 83 namedE : (string * con) IM.map
adamc@2 84 }
adamc@2 85
adamc@2 86 val namedCounter = ref 0
adamc@2 87
adamc@2 88 val empty = {
adamc@2 89 renameC = SM.empty,
adamc@2 90 relC = [],
adamc@9 91 namedC = IM.empty,
adamc@9 92
adamc@9 93 renameE = SM.empty,
adamc@9 94 relE = [],
adamc@9 95 namedE = IM.empty
adamc@2 96 }
adamc@2 97
adamc@2 98 fun pushCRel (env : env) x k =
adamc@2 99 let
adamc@9 100 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
adamc@2 101 | x => x) (#renameC env)
adamc@2 102 in
adamc@9 103 {renameC = SM.insert (renameC, x, Rel' (0, k)),
adamc@2 104 relC = (x, k) :: #relC env,
adamc@13 105 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
adamc@9 106
adamc@9 107 renameE = #renameE env,
adamc@13 108 relE = map (fn (x, c) => (x, lift c)) (#relE env),
adamc@13 109 namedE = IM.map (fn (x, c) => (x, lift c)) (#namedE env)}
adamc@2 110 end
adamc@2 111
adamc@2 112 fun lookupCRel (env : env) n =
adamc@2 113 (List.nth (#relC env, n))
adamc@2 114 handle Subscript => raise UnboundRel n
adamc@2 115
adamc@11 116 fun pushCNamedAs (env : env) x n k co =
adamc@9 117 {renameC = SM.insert (#renameC env, x, Named' (n, k)),
adamc@5 118 relC = #relC env,
adamc@11 119 namedC = IM.insert (#namedC env, n, (x, k, co)),
adamc@9 120
adamc@9 121 renameE = #renameE env,
adamc@9 122 relE = #relE env,
adamc@9 123 namedE = #namedE env}
adamc@5 124
adamc@11 125 fun pushCNamed env x k co =
adamc@2 126 let
adamc@2 127 val n = !namedCounter
adamc@2 128 in
adamc@2 129 namedCounter := n + 1;
adamc@11 130 (pushCNamedAs env x n k co, n)
adamc@2 131 end
adamc@2 132
adamc@2 133 fun lookupCNamed (env : env) n =
adamc@2 134 case IM.find (#namedC env, n) of
adamc@2 135 NONE => raise UnboundNamed n
adamc@2 136 | SOME x => x
adamc@2 137
adamc@2 138 fun lookupC (env : env) x =
adamc@2 139 case SM.find (#renameC env, x) of
adamc@9 140 NONE => NotBound
adamc@9 141 | SOME (Rel' x) => Rel x
adamc@9 142 | SOME (Named' x) => Named x
adamc@9 143
adamc@9 144 fun pushERel (env : env) x t =
adamc@9 145 let
adamc@9 146 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
adamc@9 147 | x => x) (#renameE env)
adamc@9 148 in
adamc@9 149 {renameC = #renameC env,
adamc@9 150 relC = #relC env,
adamc@9 151 namedC = #namedC env,
adamc@9 152
adamc@9 153 renameE = SM.insert (renameE, x, Rel' (0, t)),
adamc@9 154 relE = (x, t) :: #relE env,
adamc@9 155 namedE = #namedE env}
adamc@9 156 end
adamc@9 157
adamc@9 158 fun lookupERel (env : env) n =
adamc@9 159 (List.nth (#relE env, n))
adamc@9 160 handle Subscript => raise UnboundRel n
adamc@9 161
adamc@9 162 fun pushENamedAs (env : env) x n t =
adamc@9 163 {renameC = #renameC env,
adamc@9 164 relC = #relC env,
adamc@9 165 namedC = #namedC env,
adamc@9 166
adamc@9 167 renameE = SM.insert (#renameE env, x, Named' (n, t)),
adamc@9 168 relE = #relE env,
adamc@9 169 namedE = IM.insert (#namedE env, n, (x, t))}
adamc@9 170
adamc@9 171 fun pushENamed env x t =
adamc@9 172 let
adamc@9 173 val n = !namedCounter
adamc@9 174 in
adamc@9 175 namedCounter := n + 1;
adamc@9 176 (pushENamedAs env x n t, n)
adamc@9 177 end
adamc@9 178
adamc@9 179 fun lookupENamed (env : env) n =
adamc@9 180 case IM.find (#namedE env, n) of
adamc@9 181 NONE => raise UnboundNamed n
adamc@9 182 | SOME x => x
adamc@9 183
adamc@9 184 fun lookupE (env : env) x =
adamc@9 185 case SM.find (#renameE env, x) of
adamc@9 186 NONE => NotBound
adamc@9 187 | SOME (Rel' x) => Rel x
adamc@9 188 | SOME (Named' x) => Named x
adamc@2 189
adamc@13 190 fun declBinds env (d, _) =
adamc@13 191 case d of
adamc@13 192 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@13 193 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@13 194
adamc@14 195 val ktype = (KType, ErrorMsg.dummySpan)
adamc@14 196
adamc@14 197 fun bbind env x = #1 (pushCNamed env x ktype NONE)
adamc@14 198
adamc@14 199 val basis = empty
adamc@14 200 val basis = bbind basis "int"
adamc@14 201 val basis = bbind basis "float"
adamc@14 202 val basis = bbind basis "string"
adamc@14 203
adamc@2 204 end