comparison src/cjr_env.sml @ 29:537db4ee89f4

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