-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypechecker_types.ml
More file actions
204 lines (186 loc) · 4.25 KB
/
typechecker_types.ml
File metadata and controls
204 lines (186 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
type types =
| Var of string (* Var "a" *)
| Imp of types * types (* Imp(a,b) = a -> b *)
| Tup of types * types (* Tup(a,b) = (a,b) *)
| Lis of types (* Lis a = [a] *)
| Enum of (string * types option) list
| Int | Bool | Char | Void;;
exception Already_known of string;;
exception Not_in_env of string;;
module SS = Set.Make(String);;
type env_type = {
id : string;
t : types;
};;
module TypeSet =
Set.Make(
struct
type t = env_type
let compare x y = compare x.id y.id
end
);;
module Env_type =
struct
include TypeSet
let stub x : env_type =
{id = x; t = Void}
let find_safe x set =
try find x set
with
| Not_found -> raise (Not_in_env x.id)
let add_safe x set =
try
let _ = find x set in
raise (Already_known x.id)
with
| Not_found -> add x set
let update x set =
let y = find_safe x set in
let set' = remove y set in
add x set'
let union_safe set1 set2 =
fold (fun el beginset -> add_safe el beginset) set1 set2
end
;;
type env_var = {
id : string;
t : types;
};;
module VarSet =
Set.Make(
struct
type t = env_var
let compare x y = compare x.id y.id
end
);;
module Env_var =
struct
include VarSet
let stub x : env_var =
{id = x; t = Void}
let find_safe x set =
try find x set
with
| Not_found -> raise (Not_in_env x.id)
let add_safe x set =
try
let _ = find x set in
raise (Already_known x.id)
with
| Not_found -> add x set
let update x set =
let y = find_safe x set in
let set' = remove y set in
add x set'
let add_locals x set =
fold (fun el beginset ->
try
update el beginset
with
| Not_in_env e -> add_safe el beginset) x set
let exclude x set =
let y = find_safe {id = x; t = Void} set in
remove y set
let union_safe set1 set2 =
fold (fun el beginset -> add_safe el beginset) set1 set2
end;;
type env_fun = {
id : string;
bound : SS.t;
t : types;
mutable locals : Env_var.t;
};;
module FunSet =
Set.Make(
struct
type t = env_fun
let compare x y = compare x.id y.id
end
);;
module Env_fun =
struct
include FunSet
let stub x =
{id = x; bound = SS.empty; t = Void; locals = Env_var.empty}
let find_safe x set =
try find x set
with
| Not_found -> raise (Not_in_env x.id)
let add_safe x set =
try
let _ = find x set in
raise (Already_known x.id)
with
| Not_found -> add x set
let update x set =
let y = find_safe x set in
let set' = remove y set in
add x set'
let union_safe set1 set2 =
fold (fun el beginset -> add_safe el beginset) set1 set2
end;;
type environment = {
types : Env_type.t;
vars : Env_var.t;
funs : Env_fun.t;};;
module Env =
struct
type t = environment
let empty = {
types = Env_type.empty;
vars = Env_var.empty;
funs = Env_fun.empty;}
let union x y = {
types = Env_type.union x.types y.types;
vars = Env_var.union x.vars y.vars;
funs = Env_fun.union x.funs y.funs;}
let diff x y = {
types = Env_type.diff x.types y.types;
vars = Env_var.diff x.vars y.vars;
funs = Env_fun.diff x.funs y.funs;}
let elements env =
Env_type.elements env.types,
Env_var.elements env.vars,
Env_fun.elements env.funs
let find_type x env =
Env_type.find_safe (Env_type.stub x) env.types
let add_type x env = {
env with types =
Env_type.add_safe x env.types}
let update_var x env = {
env with types =
Env_type.update x env.types}
let find_var x env =
Env_var.find_safe (Env_var.stub x) env.vars
let add_var x env = {
env with vars =
Env_var.add_safe x env.vars}
let update_var x env = {
env with vars =
Env_var.update x env.vars}
let find_fun x env =
Env_fun.find_safe (Env_fun.stub x) env.funs
let add_fun x env = {
env with funs =
Env_fun.add_safe x env.funs}
let update_fun x env = {
env with funs =
Env_fun.update x env.funs}
let add_locals locals env = {
env with vars =
Env_var.add_locals locals env.vars}
let exclude x env = {
env with vars =
Env_var.exclude x env.vars}
let exists_cons x env =
try
let _ = find_type x env in
true
with
| Not_in_env _ -> false
end;;
module RW = Set.Make(
struct
type t = string * types
let compare x y = compare (fst x) (fst y)
end)