annotate src/core_env.sml @ 179:3bbed533fbd2

Cases through monoize
author Adam Chlipala <adamc@hcoop.net>
date Sun, 03 Aug 2008 10:48:36 -0400
parents 5d030ee143e2
children d11754ffe252
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@16 54
adamc@16 55 (* Back to environments *)
adamc@16 56
adamc@16 57 exception UnboundRel of int
adamc@16 58 exception UnboundNamed of int
adamc@16 59
adamc@16 60 type env = {
adamc@16 61 relC : (string * kind) list,
adamc@16 62 namedC : (string * kind * con option) IM.map,
adamc@16 63
adamc@168 64 datatypes : (string * (string * int * con option) list) IM.map,
adamc@177 65 constructors : (string * con option * int) IM.map,
adamc@168 66
adamc@16 67 relE : (string * con) list,
adamc@109 68 namedE : (string * con * exp option * string) IM.map
adamc@16 69 }
adamc@16 70
adamc@16 71 val empty = {
adamc@16 72 relC = [],
adamc@16 73 namedC = IM.empty,
adamc@16 74
adamc@168 75 datatypes = IM.empty,
adamc@177 76 constructors = IM.empty,
adamc@168 77
adamc@16 78 relE = [],
adamc@16 79 namedE = IM.empty
adamc@16 80 }
adamc@16 81
adamc@16 82 fun pushCRel (env : env) x k =
adamc@16 83 {relC = (x, k) :: #relC env,
adamc@16 84 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
adamc@16 85
adamc@168 86 datatypes = #datatypes env,
adamc@177 87 constructors = #constructors env,
adamc@168 88
adamc@16 89 relE = map (fn (x, c) => (x, lift c)) (#relE env),
adamc@109 90 namedE = IM.map (fn (x, c, eo, s) => (x, lift c, eo, s)) (#namedE env)}
adamc@16 91
adamc@16 92 fun lookupCRel (env : env) n =
adamc@16 93 (List.nth (#relC env, n))
adamc@16 94 handle Subscript => raise UnboundRel n
adamc@16 95
adamc@16 96 fun pushCNamed (env : env) x n k co =
adamc@16 97 {relC = #relC env,
adamc@16 98 namedC = IM.insert (#namedC env, n, (x, k, co)),
adamc@16 99
adamc@168 100 datatypes = #datatypes env,
adamc@177 101 constructors = #constructors env,
adamc@168 102
adamc@16 103 relE = #relE env,
adamc@16 104 namedE = #namedE env}
adamc@16 105
adamc@16 106 fun lookupCNamed (env : env) n =
adamc@16 107 case IM.find (#namedC env, n) of
adamc@16 108 NONE => raise UnboundNamed n
adamc@16 109 | SOME x => x
adamc@16 110
adamc@168 111 fun pushDatatype (env : env) x n xncs =
adamc@168 112 {relC = #relC env,
adamc@168 113 namedC = #namedC env,
adamc@168 114
adamc@168 115 datatypes = IM.insert (#datatypes env, n, (x, xncs)),
adamc@177 116 constructors = foldl (fn ((x, n, to), constructors) =>
adamc@177 117 IM.insert (constructors, n, (x, to, n)))
adamc@177 118 (#constructors env) xncs,
adamc@168 119
adamc@168 120 relE = #relE env,
adamc@168 121 namedE = #namedE env}
adamc@168 122
adamc@168 123 fun lookupDatatype (env : env) n =
adamc@168 124 case IM.find (#datatypes env, n) of
adamc@168 125 NONE => raise UnboundNamed n
adamc@168 126 | SOME x => x
adamc@168 127
adamc@177 128 fun lookupConstructor (env : env) n =
adamc@177 129 case IM.find (#constructors env, n) of
adamc@177 130 NONE => raise UnboundNamed n
adamc@177 131 | SOME x => x
adamc@177 132
adamc@16 133 fun pushERel (env : env) x t =
adamc@16 134 {relC = #relC env,
adamc@16 135 namedC = #namedC env,
adamc@16 136
adamc@168 137 datatypes = #datatypes env,
adamc@177 138 constructors = #constructors env,
adamc@168 139
adamc@16 140 relE = (x, t) :: #relE env,
adamc@16 141 namedE = #namedE env}
adamc@16 142
adamc@16 143 fun lookupERel (env : env) n =
adamc@16 144 (List.nth (#relE env, n))
adamc@16 145 handle Subscript => raise UnboundRel n
adamc@16 146
adamc@109 147 fun pushENamed (env : env) x n t eo s =
adamc@16 148 {relC = #relC env,
adamc@16 149 namedC = #namedC env,
adamc@16 150
adamc@168 151 datatypes = #datatypes env,
adamc@177 152 constructors = #constructors env,
adamc@168 153
adamc@16 154 relE = #relE env,
adamc@109 155 namedE = IM.insert (#namedE env, n, (x, t, eo, s))}
adamc@16 156
adamc@16 157 fun lookupENamed (env : env) n =
adamc@16 158 case IM.find (#namedE env, n) of
adamc@16 159 NONE => raise UnboundNamed n
adamc@16 160 | SOME x => x
adamc@16 161
adamc@163 162 fun declBinds env (d, loc) =
adamc@16 163 case d of
adamc@16 164 DCon (x, n, k, c) => pushCNamed env x n k (SOME c)
adamc@163 165 | DDatatype (x, n, xncs) =>
adamc@163 166 let
adamc@168 167 val env = pushDatatype env x n xncs
adamc@163 168 val env = pushCNamed env x n (KType, loc) NONE
adamc@163 169 in
adamc@163 170 foldl (fn ((x', n', NONE), env) => pushENamed env x' n' (CNamed n, loc) NONE ""
adamc@163 171 | ((x', n', SOME t), env) => pushENamed env x' n' (TFun (t, (CNamed n, loc)), loc) NONE "")
adamc@163 172 env xncs
adamc@163 173 end
adamc@109 174 | DVal (x, n, t, e, s) => pushENamed env x n t (SOME e) s
adamc@126 175 | DValRec vis => foldl (fn ((x, n, t, e, s), env) => pushENamed env x n t NONE s) env vis
adamc@109 176 | DExport _ => env
adamc@16 177
adamc@16 178 end