annotate src/cjr_env.sml @ 167:2be573fec9a6

Unurlifying a datatype; longjmp-based error signaling mechanism
author Adam Chlipala <adamc@hcoop.net>
date Tue, 29 Jul 2008 15:25:42 -0400
parents a991431b77eb
children 25b169416ea8
rev   line source
adamc@29 1 (* Copyright (c) 2008, Adam Chlipala
adamc@29 2 * All rights reserved.
adamc@29 3 *
adamc@29 4 * Redistribution and use in source and binary forms, with or without
adamc@29 5 * modification, are permitted provided that the following conditions are met:
adamc@29 6 *
adamc@29 7 * - Redistributions of source code must retain the above copyright notice,
adamc@29 8 * this list of conditions and the following disclaimer.
adamc@29 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@29 10 * this list of conditions and the following disclaimer in the documentation
adamc@29 11 * and/or other materials provided with the distribution.
adamc@29 12 * - The names of contributors may not be used to endorse or promote products
adamc@29 13 * derived from this software without specific prior written permission.
adamc@29 14 *
adamc@29 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@29 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@29 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@29 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@29 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@29 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@29 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@29 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@29 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@29 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@29 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@29 26 *)
adamc@29 27
adamc@29 28 structure CjrEnv :> CJR_ENV = struct
adamc@29 29
adamc@29 30 open Cjr
adamc@29 31
adamc@29 32 structure IM = IntBinaryMap
adamc@29 33
adamc@29 34
adamc@29 35 exception UnboundRel of int
adamc@29 36 exception UnboundNamed of int
adamc@29 37 exception UnboundF of int
adamc@101 38 exception UnboundStruct of int
adamc@29 39
adamc@29 40 type env = {
adamc@166 41 datatypes : (string * (string * int * typ option) list) IM.map,
adamc@29 42
adamc@29 43 numRelE : int,
adamc@29 44 relE : (string * typ) list,
adamc@29 45 namedE : (string * typ) IM.map,
adamc@29 46
adamc@101 47 structs : (string * typ) list IM.map
adamc@29 48 }
adamc@29 49
adamc@29 50 val empty = {
adamc@166 51 datatypes = IM.empty,
adamc@29 52
adamc@29 53 numRelE = 0,
adamc@29 54 relE = [],
adamc@29 55 namedE = IM.empty,
adamc@29 56
adamc@101 57 structs = IM.empty
adamc@29 58 }
adamc@29 59
adamc@166 60 fun pushDatatype (env : env) x n xncs =
adamc@166 61 {datatypes = IM.insert (#datatypes env, n, (x, xncs)),
adamc@29 62
adamc@29 63 numRelE = #numRelE env,
adamc@29 64 relE = #relE env,
adamc@29 65 namedE = #namedE env,
adamc@29 66
adamc@101 67 structs = #structs env}
adamc@29 68
adamc@166 69 fun lookupDatatype (env : env) n =
adamc@166 70 case IM.find (#datatypes env, n) of
adamc@29 71 NONE => raise UnboundNamed n
adamc@29 72 | SOME x => x
adamc@29 73
adamc@29 74 fun pushERel (env : env) x t =
adamc@166 75 {datatypes = #datatypes env,
adamc@29 76
adamc@29 77 numRelE = #numRelE env + 1,
adamc@29 78 relE = (x, t) :: #relE env,
adamc@29 79 namedE = #namedE env,
adamc@29 80
adamc@101 81 structs = #structs env}
adamc@29 82
adamc@29 83 fun lookupERel (env : env) n =
adamc@29 84 (List.nth (#relE env, n))
adamc@29 85 handle Subscript => raise UnboundRel n
adamc@29 86
adamc@29 87 fun countERels (env : env) = #numRelE env
adamc@29 88
adamc@29 89 fun listERels (env : env) = #relE env
adamc@29 90
adamc@29 91 fun pushENamed (env : env) x n t =
adamc@166 92 {datatypes = #datatypes env,
adamc@29 93
adamc@29 94 numRelE = #numRelE env,
adamc@29 95 relE = #relE env,
adamc@29 96 namedE = IM.insert (#namedE env, n, (x, t)),
adamc@29 97
adamc@101 98 structs = #structs env}
adamc@29 99
adamc@29 100 fun lookupENamed (env : env) n =
adamc@29 101 case IM.find (#namedE env, n) of
adamc@29 102 NONE => raise UnboundNamed n
adamc@29 103 | SOME x => x
adamc@29 104
adamc@101 105 fun pushStruct (env : env) n xts =
adamc@166 106 {datatypes = #datatypes env,
adamc@101 107
adamc@101 108 numRelE = #numRelE env,
adamc@101 109 relE = #relE env,
adamc@101 110 namedE = #namedE env,
adamc@101 111
adamc@101 112 structs = IM.insert (#structs env, n, xts)}
adamc@101 113
adamc@101 114 fun lookupStruct (env : env) n =
adamc@101 115 case IM.find (#structs env, n) of
adamc@101 116 NONE => raise UnboundStruct n
adamc@101 117 | SOME x => x
adamc@101 118
adamc@109 119 fun declBinds env (d, loc) =
adamc@29 120 case d of
adamc@165 121 DDatatype (x, n, xncs) =>
adamc@165 122 let
adamc@166 123 val env = pushDatatype env x n xncs
adamc@165 124 in
adamc@166 125 foldl (fn ((x', n', NONE), env) => pushENamed env x' n' (TDatatype n, loc)
adamc@166 126 | ((x', n', SOME t), env) => pushENamed env x' n' (TFun (t, (TDatatype n, loc)), loc))
adamc@165 127 env xncs
adamc@165 128 end
adamc@165 129 | DStruct (n, xts) => pushStruct env n xts
adamc@165 130 | DVal (x, n, t, _) => pushENamed env x n t
adamc@121 131 | DFun (fx, n, args, ran, _) =>
adamc@121 132 let
adamc@121 133 val t = foldl (fn ((_, arg), t) => (TFun (arg, t), loc)) ran args
adamc@121 134 in
adamc@121 135 pushENamed env fx n t
adamc@121 136 end
adamc@129 137 | DFunRec vis =>
adamc@129 138 foldl (fn ((fx, n, args, ran, _), env) =>
adamc@129 139 let
adamc@129 140 val t = foldl (fn ((_, arg), t) => (TFun (arg, t), loc)) ran args
adamc@129 141 in
adamc@129 142 pushENamed env fx n t
adamc@129 143 end) env vis
adamc@165 144
adamc@29 145
adamc@29 146 end