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@29
|
41 namedT : (string * typ option) 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@29
|
51 namedT = 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@29
|
60 fun pushTNamed (env : env) x n co =
|
adamc@29
|
61 {namedT = IM.insert (#namedT env, n, (x, co)),
|
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@29
|
69 fun lookupTNamed (env : env) n =
|
adamc@29
|
70 case IM.find (#namedT 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@29
|
75 {namedT = #namedT 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@29
|
92 {namedT = #namedT 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@101
|
106 {namedT = #namedT 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@29
|
121 DVal (x, n, t, _) => pushENamed env x n t
|
adamc@121
|
122 | DFun (fx, n, args, ran, _) =>
|
adamc@121
|
123 let
|
adamc@121
|
124 val t = foldl (fn ((_, arg), t) => (TFun (arg, t), loc)) ran args
|
adamc@121
|
125 in
|
adamc@121
|
126 pushENamed env fx n t
|
adamc@121
|
127 end
|
adamc@129
|
128 | DFunRec vis =>
|
adamc@129
|
129 foldl (fn ((fx, n, args, ran, _), env) =>
|
adamc@129
|
130 let
|
adamc@129
|
131 val t = foldl (fn ((_, arg), t) => (TFun (arg, t), loc)) ran args
|
adamc@129
|
132 in
|
adamc@129
|
133 pushENamed env fx n t
|
adamc@129
|
134 end) env vis
|
adamc@101
|
135 | DStruct (n, xts) => pushStruct env n xts
|
adamc@29
|
136
|
adamc@29
|
137 end
|