-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.ml
36 lines (29 loc) · 1000 Bytes
/
env.ml
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
module SMap = Map.Make(String)
let empty = SMap.empty::[]
let push env = SMap.empty::env
let pop env = match env with
| [] -> failwith "Trying to pop an empty env"
| _::tl -> tl
let add ref value env = match env with
| [] -> failwith "Trying to bind on an empty env"
| cur::tl -> (SMap.add ref value cur)::tl
let bind assoc env = match env with
| [] -> failwith "Trying to bind on an empty env"
| cur::tl ->
let rec bind_rec assoc acc = match assoc with
| [] -> acc
| (ref, value)::tl -> bind_rec tl (SMap.add ref value acc)
in
(bind_rec assoc cur)::tl
let rec find ref env = match env with
| [] -> failwith (Printf.sprintf "Unbound variable %s" ref)
| cur::tl ->
try SMap.find ref cur
with Not_found -> find ref tl
let rec update ref value env = match env with
| [] -> failwith (Printf.sprintf "Unbound variable %s" ref)
| cur::tl ->
if SMap.mem ref cur then
(SMap.add ref value cur)::tl
else
cur::(update ref value tl)