annotate src/flat_env.sml @ 26:4ab19c19665f

Closure conversion
author Adam Chlipala <adamc@hcoop.net>
date Tue, 10 Jun 2008 15:56:33 -0400
parents
children d3cc191cb25f
rev   line source
adamc@26 1 (* Copyright (c) 2008, Adam Chlipala
adamc@26 2 * All rights reserved.
adamc@26 3 *
adamc@26 4 * Redistribution and use in source and binary forms, with or without
adamc@26 5 * modification, are permitted provided that the following conditions are met:
adamc@26 6 *
adamc@26 7 * - Redistributions of source code must retain the above copyright notice,
adamc@26 8 * this list of conditions and the following disclaimer.
adamc@26 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@26 10 * this list of conditions and the following disclaimer in the documentation
adamc@26 11 * and/or other materials provided with the distribution.
adamc@26 12 * - The names of contributors may not be used to endorse or promote products
adamc@26 13 * derived from this software without specific prior written permission.
adamc@26 14 *
adamc@26 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@26 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@26 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@26 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@26 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@26 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@26 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@26 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@26 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@26 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@26 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@26 26 *)
adamc@26 27
adamc@26 28 structure FlatEnv :> FLAT_ENV = struct
adamc@26 29
adamc@26 30 open Flat
adamc@26 31
adamc@26 32 structure IM = IntBinaryMap
adamc@26 33
adamc@26 34
adamc@26 35 exception UnboundRel of int
adamc@26 36 exception UnboundNamed of int
adamc@26 37 exception UnboundF of int
adamc@26 38
adamc@26 39 type env = {
adamc@26 40 namedT : (string * typ option) IM.map,
adamc@26 41
adamc@26 42 relE : (string * typ) list,
adamc@26 43 namedE : (string * typ) IM.map,
adamc@26 44
adamc@26 45 F : (string * typ * typ) IM.map
adamc@26 46 }
adamc@26 47
adamc@26 48 val empty = {
adamc@26 49 namedT = IM.empty,
adamc@26 50
adamc@26 51 relE = [],
adamc@26 52 namedE = IM.empty,
adamc@26 53
adamc@26 54 F = IM.empty
adamc@26 55 }
adamc@26 56
adamc@26 57 fun pushTNamed (env : env) x n co =
adamc@26 58 {namedT = IM.insert (#namedT env, n, (x, co)),
adamc@26 59
adamc@26 60 relE = #relE env,
adamc@26 61 namedE = #namedE env,
adamc@26 62
adamc@26 63 F = #F env}
adamc@26 64
adamc@26 65 fun lookupTNamed (env : env) n =
adamc@26 66 case IM.find (#namedT env, n) of
adamc@26 67 NONE => raise UnboundNamed n
adamc@26 68 | SOME x => x
adamc@26 69
adamc@26 70 fun pushERel (env : env) x t =
adamc@26 71 {namedT = #namedT env,
adamc@26 72
adamc@26 73 relE = (x, t) :: #relE env,
adamc@26 74 namedE = #namedE env,
adamc@26 75
adamc@26 76 F = #F env}
adamc@26 77
adamc@26 78 fun lookupERel (env : env) n =
adamc@26 79 (List.nth (#relE env, n))
adamc@26 80 handle Subscript => raise UnboundRel n
adamc@26 81
adamc@26 82 fun listERels (env : env) = #relE env
adamc@26 83
adamc@26 84 fun pushENamed (env : env) x n t =
adamc@26 85 {namedT = #namedT env,
adamc@26 86
adamc@26 87 relE = #relE env,
adamc@26 88 namedE = IM.insert (#namedE env, n, (x, t)),
adamc@26 89
adamc@26 90 F = #F env}
adamc@26 91
adamc@26 92 fun lookupENamed (env : env) n =
adamc@26 93 case IM.find (#namedE env, n) of
adamc@26 94 NONE => raise UnboundNamed n
adamc@26 95 | SOME x => x
adamc@26 96
adamc@26 97 fun pushF (env : env) n x dom ran =
adamc@26 98 {namedT = #namedT env,
adamc@26 99
adamc@26 100 relE = #relE env,
adamc@26 101 namedE = #namedE env,
adamc@26 102
adamc@26 103 F = IM.insert (#F env, n, (x, dom, ran))}
adamc@26 104
adamc@26 105 fun lookupF (env : env) n =
adamc@26 106 case IM.find (#F env, n) of
adamc@26 107 NONE => raise UnboundF n
adamc@26 108 | SOME x => x
adamc@26 109
adamc@26 110 fun declBinds env (d, _) =
adamc@26 111 case d of
adamc@26 112 DVal (x, n, t, _) => pushENamed env x n t
adamc@26 113 | DFun (n, x, dom, ran, _) => pushF env n x dom ran
adamc@26 114
adamc@26 115 fun bbind env x =
adamc@26 116 case ElabEnv.lookupC ElabEnv.basis x of
adamc@26 117 ElabEnv.NotBound => raise Fail "FlatEnv.bbind: Not bound"
adamc@26 118 | ElabEnv.Rel _ => raise Fail "FlatEnv.bbind: Rel"
adamc@26 119 | ElabEnv.Named (n, _) => pushTNamed env x n NONE
adamc@26 120
adamc@26 121 val basis = empty
adamc@26 122 val basis = bbind basis "int"
adamc@26 123 val basis = bbind basis "float"
adamc@26 124 val basis = bbind basis "string"
adamc@26 125
adamc@26 126 end