adamc@26: (* Copyright (c) 2008, Adam Chlipala adamc@26: * All rights reserved. adamc@26: * adamc@26: * Redistribution and use in source and binary forms, with or without adamc@26: * modification, are permitted provided that the following conditions are met: adamc@26: * adamc@26: * - Redistributions of source code must retain the above copyright notice, adamc@26: * this list of conditions and the following disclaimer. adamc@26: * - Redistributions in binary form must reproduce the above copyright notice, adamc@26: * this list of conditions and the following disclaimer in the documentation adamc@26: * and/or other materials provided with the distribution. adamc@26: * - The names of contributors may not be used to endorse or promote products adamc@26: * derived from this software without specific prior written permission. adamc@26: * adamc@26: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@26: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@26: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@26: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@26: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@26: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@26: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@26: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@26: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@26: * POSSIBILITY OF SUCH DAMAGE. adamc@26: *) adamc@26: adamc@26: structure FlatEnv :> FLAT_ENV = struct adamc@26: adamc@26: open Flat adamc@26: adamc@26: structure IM = IntBinaryMap adamc@26: adamc@26: adamc@26: exception UnboundRel of int adamc@26: exception UnboundNamed of int adamc@26: exception UnboundF of int adamc@26: adamc@26: type env = { adamc@26: namedT : (string * typ option) IM.map, adamc@26: adamc@26: relE : (string * typ) list, adamc@26: namedE : (string * typ) IM.map, adamc@26: adamc@26: F : (string * typ * typ) IM.map adamc@26: } adamc@26: adamc@26: val empty = { adamc@26: namedT = IM.empty, adamc@26: adamc@26: relE = [], adamc@26: namedE = IM.empty, adamc@26: adamc@26: F = IM.empty adamc@26: } adamc@26: adamc@26: fun pushTNamed (env : env) x n co = adamc@26: {namedT = IM.insert (#namedT env, n, (x, co)), adamc@26: adamc@26: relE = #relE env, adamc@26: namedE = #namedE env, adamc@26: adamc@26: F = #F env} adamc@26: adamc@26: fun lookupTNamed (env : env) n = adamc@26: case IM.find (#namedT env, n) of adamc@26: NONE => raise UnboundNamed n adamc@26: | SOME x => x adamc@26: adamc@26: fun pushERel (env : env) x t = adamc@26: {namedT = #namedT env, adamc@26: adamc@26: relE = (x, t) :: #relE env, adamc@26: namedE = #namedE env, adamc@26: adamc@26: F = #F env} adamc@26: adamc@26: fun lookupERel (env : env) n = adamc@26: (List.nth (#relE env, n)) adamc@26: handle Subscript => raise UnboundRel n adamc@26: adamc@26: fun listERels (env : env) = #relE env adamc@26: adamc@26: fun pushENamed (env : env) x n t = adamc@26: {namedT = #namedT env, adamc@26: adamc@26: relE = #relE env, adamc@26: namedE = IM.insert (#namedE env, n, (x, t)), adamc@26: adamc@26: F = #F env} adamc@26: adamc@26: fun lookupENamed (env : env) n = adamc@26: case IM.find (#namedE env, n) of adamc@26: NONE => raise UnboundNamed n adamc@26: | SOME x => x adamc@26: adamc@26: fun pushF (env : env) n x dom ran = adamc@26: {namedT = #namedT env, adamc@26: adamc@26: relE = #relE env, adamc@26: namedE = #namedE env, adamc@26: adamc@26: F = IM.insert (#F env, n, (x, dom, ran))} adamc@26: adamc@26: fun lookupF (env : env) n = adamc@26: case IM.find (#F env, n) of adamc@26: NONE => raise UnboundF n adamc@26: | SOME x => x adamc@26: adamc@26: fun declBinds env (d, _) = adamc@26: case d of adamc@26: DVal (x, n, t, _) => pushENamed env x n t adamc@26: | DFun (n, x, dom, ran, _) => pushF env n x dom ran adamc@26: adamc@26: fun bbind env x = adamc@26: case ElabEnv.lookupC ElabEnv.basis x of adamc@26: ElabEnv.NotBound => raise Fail "FlatEnv.bbind: Not bound" adamc@26: | ElabEnv.Rel _ => raise Fail "FlatEnv.bbind: Rel" adamc@26: | ElabEnv.Named (n, _) => pushTNamed env x n NONE adamc@26: adamc@26: val basis = empty adamc@26: val basis = bbind basis "int" adamc@26: val basis = bbind basis "float" adamc@26: val basis = bbind basis "string" adamc@26: adamc@26: end