1 (* calc6.ml: calc3.ml enhanced with Mutable Variables *)
5 | Multiplication of (term * term)
6 | Addition of (term * term)
8 | Let of (char * term * term)
10 | If of (term * term * term)
11 | Makepair of (term * term)
13 | Lambda of (char * term)
14 | Apply of (term * term)
15 | Letrec of (char * term * term)
16 | Change of (char * term * term)
21 type bound_value = index;;
22 type assignment = (char * bound_value) list;;
23 type expressed_value = Int of int | Bool of bool | Pair of expressed_value * expressed_value | Closure of char * term * assignment;;
25 type store = expressed_value list;;
27 let rec eval (t : term) (g : assignment) (s : store) = match t with
28 Intconstant x -> (Int x, s)
29 | Multiplication (t1, t2) ->
30 (* we don't handle cases where the subterms don't evaluate to Ints *)
31 let (Int i1, s') = eval t1 g s
32 in let (Int i2, s'') = eval t2 g s'
33 (* Multiplication (t1, t2) should evaluate to an Int *)
34 in (Int (i1 * i2), s'')
35 | Addition (t1, t2) ->
36 let (Int i1, s') = eval t1 g s
37 in let (Int i2, s'') = eval t2 g s'
38 in (Int (i1 + i2), s'')
40 (* we don't handle cases where g doesn't bind var to any value *)
41 let index = List.assoc var g
42 (* get value stored at location index in s *)
43 in let value = List.nth s index
45 | Let (var_to_bind, t2, t3) ->
46 let (value2, s') = eval t2 g s
47 (* note that s' may be different from s, if t2 itself contained any mutation operations *)
48 (* get next free index in s' *)
49 in let new_index = List.length s'
50 (* now we want to insert value2 there; the following is an easy but inefficient way to do it *)
51 in let s'' = List.append s' [value2]
52 (* bind var_to_bind to location new_index in the store *)
53 in let g' = ((var_to_bind, new_index) :: g)
56 (* we don't handle cases where t1 doesn't evaluate to an Int *)
57 let (Int i1, s') = eval t1 g s
58 (* Iszero t1 should evaluate to a Bool *)
59 in (Bool (i1 = 0), s')
61 (* we don't handle cases where t1 doesn't evaluate to a boolean *)
62 let (Bool b1, s') = eval t1 g s
63 (* note we thread s' through only one of the then/else clauses *)
64 in if b1 then eval t2 g s'
66 | Makepair (t1, t2) ->
67 let (value1, s') = eval t1 g s
68 in let (value2, s'') = eval t2 g s'
69 in (Pair (value1, value2), s'')
71 (* we don't handle cases where t1 doesn't evaluate to a Pair *)
72 let (Pair (value1, value2), s') = eval t1 g s
74 | Lambda (arg_var, t2) -> (Closure (arg_var, t2, g), s)
76 (* we don't handle cases where t1 doesn't evaluate to a function value *)
77 let (Closure (arg_var, body, savedg), s') = eval t1 g s
78 in let (value2, s'') = eval t2 g s'
79 (* evaluate body under savedg, except with arg_var bound to a new location containing value2 *)
80 in let new_index = List.length s''
81 in let s''' = List.append s'' [value2]
82 in let savedg' = (arg_var, new_index) :: savedg
83 in eval body savedg' s'''
84 | Letrec (var_to_bind, t2, t3) ->
85 (* we don't handle cases where t2 doesn't evaluate to a function value *)
86 let (Closure (arg_var, body, savedg), s') = eval t2 g s
87 in let new_index = List.length s'
88 in let savedg' = (var_to_bind, new_index) :: savedg
89 in let new_closure = Closure (arg_var, body, savedg')
90 in let s'' = List.append s' [new_closure]
91 in let g' = (var_to_bind, new_index) :: g
93 | Change (var, t2, t3) ->
94 (* we don't handle cases where g doesn't bind var to any value *)
95 let index = List.assoc var g
96 in let (value2, s') = eval t2 g s
97 (* note that s' may be different from s, if t2 itself contained any mutation operations *)
98 (* now we create a list which is just like s' except it has value2 at index *)
99 in let rec replace_nth lst m =
101 | [] -> failwith "list too short"
102 | x::xs when m = 0 -> value2 :: xs
103 | x::xs -> x :: replace_nth xs (m - 1)
104 in let s'' = replace_nth s' index
105 (* evaluate t3 using original assignment function and new store *)