annotate src/core_env.sml @ 594:55829473f6a7

Injected an option
author Adam Chlipala <adamc@hcoop.net>
date Fri, 02 Jan 2009 13:03:22 -0500
parents 5c9606deacb6
children 230654093b51
rev   line source
adamc@16 1 (* Copyright (c) 2008, Adam Chlipala
adamc@16 2 * All rights reserved.
adamc@16 3 *
adamc@16 4 * Redistribution and use in source and binary forms, with or without
adamc@16 5 * modification, are permitted provided that the following conditions are met:
adamc@16 6 *
adamc@16 7 * - Redistributions of source code must retain the above copyright notice,
adamc@16 8 * this list of conditions and the following disclaimer.
adamc@16 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@16 10 * this list of conditions and the following disclaimer in the documentation
adamc@16 11 * and/or other materials provided with the distribution.
adamc@16 12 * - The names of contributors may not be used to endorse or promote products
adamc@16 13 * derived from this software without specific prior written permission.
adamc@16 14 *
adamc@16 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@16 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@16 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@16 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@16 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@16 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@16 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@16 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@16 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@16 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@16 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@16 26 *)
adamc@16 27
adamc@16 28 structure CoreEnv :> CORE_ENV = struct
adamc@16 29
adamc@16 30 open Core
adamc@16 31
adamc@16 32 structure U = CoreUtil
adamc@16 33
adamc@16 34 structure IM = IntBinaryMap
adamc@16 35
adamc@16 36
adamc@16 37 (* AST utility functions *)
adamc@16 38
adamc@16 39 val liftConInCon =
adamc@16 40 U.Con.mapB {kind = fn k => k,
adamc@16 41 con = fn bound => fn c =>
adamc@16 42 case c of
adamc@16 43 CRel xn =>
adamc@16 44 if xn < bound then
adamc@16 45 c
adamc@16 46 else
adamc@16 47 CRel (xn + 1)
adamc@16 48 | _ => c,
adamc@16 49 bind = fn (bound, U.Con.Rel _) => bound + 1
adamc@16 50 | (bound, _) => bound}
adamc@16 51
adamc@16 52 val lift = liftConInCon 0
adamc@16 53
adamc@193 54 val subConInCon =
adamc@193 55 U.Con.mapB {kind = fn k => k,
adamc@193 56 con = fn (xn, rep) => fn c =>
adamc@193 57 case c of
adamc@193 58 CRel xn' =>
adamc@193 59 (case Int.compare (xn', xn) of
adamc@193 60 EQUAL => #1 rep
adamc@193 61 | GREATER => CRel (xn' - 1)
adamc@193 62 | LESS => c)
adamc@193 63 | _ => c,
adamc@193 64 bind = fn ((xn, rep), U.Con.Rel _) => (xn+1, liftConInCon 0 rep)
adamc@193 65 | (ctx, _) => ctx}
adamc@193 66
adamc@16 67
adamc@315 68 val liftConInExp =
adamc@315 69 U.Exp.mapB {kind = fn k => k,
adamc@315 70 con = fn bound => fn c =>
adamc@315 71 case c of
adamc@315 72 CRel xn =>
adamc@315 73 if xn < bound then
adamc@315 74 c
adamc@315 75 else
adamc@315 76 CRel (xn + 1)
adamc@315 77 | _ => c,
adamc@315 78 exp = fn _ => fn e => e,
adamc@315 79 bind = fn (bound, U.Exp.RelC _) => bound + 1
adamc@315 80 | (bound, _) => bound}
adamc@315 81
adamc@315 82 val subConInExp =
adamc@315 83 U.Exp.mapB {kind = fn k => k,
adamc@315 84 con = fn (xn, rep) => fn c =>
adamc@417 85 case c of
adamc@417 86 CRel xn' =>
adamc@417 87 (case Int.compare (xn', xn) of
adamc@417 88 EQUAL => #1 rep
adamc@417 89 | GREATER => CRel (xn' - 1)
adamc@417 90 | LESS => c)
adamc@417 91 | _ => c,
adamc@315 92 exp = fn _ => fn e => e,
adamc@315 93 bind = fn ((xn, rep), U.Exp.RelC _) => (xn+1, liftConInCon 0 rep)
adamc@315 94 | (ctx, _) => ctx}
adamc@315 95
adamc@443 96 val liftExpInExp =
adamc@443 97 U.Exp.mapB {kind = fn k => k,
adamc@443 98 con = fn _ => fn c => c,
adamc@443 99 exp = fn bound => fn e =>
adamc@443 100 case e of
adamc@443 101 ERel xn =>
adamc@443 102 if xn < bound then
adamc@443 103 e
adamc@443 104 else
adamc@443 105 ERel (xn + 1)
adamc@443 106 | _ => e,
adamc@443 107 bind = fn (bound, U.Exp.RelE _) => bound + 1
adamc@443 108 | (bound, _) => bound}
adamc@443 109
adamc@443 110 val subExpInExp =
adamc@443 111 U.Exp.mapB {kind = fn k => k,
adamc@443 112 con = fn _ => fn c => c,
adamc@443 113 exp = fn (xn, rep) => fn e =>
adamc@443 114 case e of
adamc@443 115 ERel xn' =>
adamc@443 116 (case Int.compare (xn', xn) of
adamc@443 117 EQUAL => #1 rep
adamc@443 118 | GREATER=> ERel (xn' - 1)
adamc@443 119 | LESS => e)
adamc@443 120 | _ => e,
adamc@443 121 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
adamc@443 122 | ((xn, rep), U.Exp.RelC _) => (xn, liftConInExp 0 rep)
adamc@443 123 | (ctx, _) => ctx}
adamc@443 124
adamc@16 125 (* Back to environments *)
adamc@16 126
adamc@16 127 exception UnboundRel of int
adamc@16 128 exception UnboundNamed of int
adamc@16 129
adamc@16 130 type env = {
adamc@16 131 relC : (string * kind) list,
adamc@16 132 namedC : (string * kind * con option) IM.map,
adamc@16 133
adamc@192 134 datatypes : (string * string list * (string * int * con option) list) IM.map,
adamc@192 135 constructors : (string * string list * con option * int) IM.map,
adamc@168 136
adamc@16 137 relE : (string * con) list,
adamc@109 138 namedE : (string * con * exp option * string) IM.map
adamc@16 139 }
adamc@16 140
adamc@16 141 val empty = {
adamc@16 142 relC = [],
adamc@16 143 namedC = IM.empty,
adamc@16 144
adamc@168 145 datatypes = IM.empty,
adamc@177 146 constructors = IM.empty,
adamc@168 147
adamc@16 148 relE = [],
adamc@16 149 namedE = IM.empty
adamc@16 150 }
adamc@16 151
adamc@16 152 fun pushCRel (env : env) x k =
adamc@16 153 {relC = (x, k) :: #relC env,
adamc@16 154 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
adamc@16 155
adamc@168 156 datatypes = #datatypes env,
adamc@177 157 constructors = #constructors env,
adamc@168 158
adamc@16 159 relE = map (fn (x, c) => (x, lift c)) (#relE env),
adamc@109 160 namedE = IM.map (fn (x, c, eo, s) => (x, lift c, eo, s)) (#namedE env)}
adamc@16 161
adamc@16 162 fun lookupCRel (env : env) n =
adamc@16 163 (List.nth (#relC env, n))
adamc@16 164 handle Subscript => raise UnboundRel n
adamc@16 165
adamc@16 166 fun pushCNamed (env : env) x n k co =
adamc@16 167 {relC = #relC env,
adamc@16 168 namedC = IM.insert (#namedC env, n, (x, k, co)),
adamc@16 169
adamc@168 170 datatypes = #datatypes env,
adamc@177 171 constructors = #constructors env,
adamc@168 172
adamc@16 173 relE = #relE env,
adamc@16 174 namedE = #namedE env}
adamc@16 175
adamc@16 176 fun lookupCNamed (env : env) n =
adamc@16 177 case IM.find (#namedC env, n) of
adamc@16 178 NONE => raise UnboundNamed n
adamc@16 179 | SOME x => x
adamc@16 180
adamc@192 181 fun pushDatatype (env : env) x n xs xncs =
adamc@168 182 {relC = #relC env,
adamc@168 183 namedC = #namedC env,
adamc@168 184
adamc@192 185 datatypes = IM.insert (#datatypes env, n, (x, xs, xncs)),
adamc@182 186 constructors = foldl (fn ((x, n', to), constructors) =>
adamc@192 187 IM.insert (constructors, n', (x, xs, to, n)))
adamc@177 188 (#constructors env) xncs,
adamc@168 189
adamc@168 190 relE = #relE env,
adamc@168 191 namedE = #namedE env}
adamc@168 192
adamc@168 193 fun lookupDatatype (env : env) n =
adamc@168 194 case IM.find (#datatypes env, n) of
adamc@168 195 NONE => raise UnboundNamed n
adamc@168 196 | SOME x => x
adamc@168 197
adamc@177 198 fun lookupConstructor (env : env) n =
adamc@177 199 case IM.find (#constructors env, n) of
adamc@177 200 NONE => raise UnboundNamed n
adamc@177 201 | SOME x => x
adamc@177 202
adamc@16 203 fun pushERel (env : env) x t =
adamc@16 204 {relC = #relC env,
adamc@16 205 namedC = #namedC env,
adamc@16 206
adamc@168 207 datatypes = #datatypes env,
adamc@177 208 constructors = #constructors env,
adamc@168 209
adamc@16 210 relE = (x, t) :: #relE env,
adamc@16 211 namedE = #namedE env}
adamc@16 212
adamc@16 213 fun lookupERel (env : env) n =
adamc@16 214 (List.nth (#relE env, n))
adamc@16 215 handle Subscript => raise UnboundRel n
adamc@16 216
adamc@109 217 fun pushENamed (env : env) x n t eo s =
adamc@16 218 {relC = #relC env,
adamc@16 219 namedC = #namedC env,
adamc@16 220
adamc@168 221 datatypes = #datatypes env,
adamc@177 222 constructors = #constructors env,
adamc@168 223
adamc@16 224 relE = #relE env,
adamc@109 225 namedE = IM.insert (#namedE env, n, (x, t, eo, s))}
adamc@16 226
adamc@16 227 fun lookupENamed (env : env) n =
adamc@16 228 case IM.find (#namedE env, n) of
adamc@16 229 NONE => raise UnboundNamed n
adamc@16 230 | SOME x => x
adamc@16 231
adamc@163 232 fun declBinds env (d, loc) =
adamc@16 233 case d of
adamc@16 234 DCon (x, n, k, c) => pushCNamed env x n k (SOME c)
adamc@192 235 | DDatatype (x, n, xs, xncs) =>
adamc@163 236 let
adamc@192 237 val env = pushDatatype env x n xs xncs
adamc@163 238 val env = pushCNamed env x n (KType, loc) NONE
adamc@163 239 in
adamc@163 240 foldl (fn ((x', n', NONE), env) => pushENamed env x' n' (CNamed n, loc) NONE ""
adamc@163 241 | ((x', n', SOME t), env) => pushENamed env x' n' (TFun (t, (CNamed n, loc)), loc) NONE "")
adamc@163 242 env xncs
adamc@163 243 end
adamc@109 244 | DVal (x, n, t, e, s) => pushENamed env x n t (SOME e) s
adamc@126 245 | DValRec vis => foldl (fn ((x, n, t, e, s), env) => pushENamed env x n t NONE s) env vis
adamc@109 246 | DExport _ => env
adamc@247 247 | DTable (x, n, c, s) =>
adamc@247 248 let
adamc@338 249 val t = (CApp ((CFfi ("Basis", "sql_table"), loc), c), loc)
adamc@338 250 in
adamc@338 251 pushENamed env x n t NONE s
adamc@338 252 end
adamc@338 253 | DSequence (x, n, s) =>
adamc@338 254 let
adamc@338 255 val t = (CFfi ("Basis", "sql_sequence"), loc)
adamc@247 256 in
adamc@247 257 pushENamed env x n t NONE s
adamc@247 258 end
adamc@271 259 | DDatabase _ => env
adamc@461 260 | DCookie (x, n, c, s) =>
adamc@461 261 let
adamc@461 262 val t = (CApp ((CFfi ("Basis", "http_cookie"), loc), c), loc)
adamc@461 263 in
adamc@461 264 pushENamed env x n t NONE s
adamc@461 265 end
adamc@16 266
adamc@193 267 fun patBinds env (p, loc) =
adamc@193 268 case p of
adamc@193 269 PWild => env
adamc@193 270 | PVar (x, t) => pushERel env x t
adamc@193 271 | PPrim _ => env
adamc@193 272 | PCon (_, _, _, NONE) => env
adamc@193 273 | PCon (_, _, _, SOME p) => patBinds env p
adamc@193 274 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
adamc@193 275
adamc@16 276 end