1 (* calc4.ml: calc3.ml enhanced with Mutable Cells *)
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)
18 | Setref of (term * term)
23 type bound_value = Nonrecursive of expressed_value | Recursive_Closure of char * char * term * assignment
24 and assignment = (char * bound_value) list
25 and expressed_value = Int of int | Bool of bool | Pair of expressed_value * expressed_value | Closure of char * term * assignment | Mutcell of index;;
27 type store = expressed_value list;;
29 let rec eval (t : term) (g : assignment) (s : store) = match t with
30 Intconstant x -> (Int x, s)
31 | Multiplication (t1, t2) ->
32 (* we don't handle cases where the subterms don't evaluate to Ints *)
33 let (Int i1, s') = eval t1 g s
34 in let (Int i2, s'') = eval t2 g s'
35 (* Multiplication (t1, t2) should evaluate to an Int *)
36 in (Int (i1 * i2), s'')
37 | Addition (t1, t2) ->
38 let (Int i1, s') = eval t1 g s
39 in let (Int i2, s'') = eval t2 g s'
40 in (Int (i1 + i2), s'')
41 | Variable (var) -> ((
42 (* we don't handle cases where g doesn't bind var to any value *)
43 match List.assoc var g with
44 | Nonrecursive value -> value
45 | Recursive_Closure (self_var, arg_var, body, savedg) as rec_closure ->
46 (* we update savedg to bind self_var to rec_closure here *)
47 let savedg' = (self_var, rec_closure) :: savedg
48 in Closure (arg_var, body, savedg')
50 | Let (var_to_bind, t2, t3) ->
51 (* evaluate t3 under a new assignment where var_to_bind has been bound to
52 the result of evaluating t2 under the current assignment *)
53 let (value2, s') = eval t2 g s
54 (* we have to wrap value2 in Nonrecursive *)
55 in let g' = (var_to_bind, Nonrecursive value2) :: g
58 (* we don't handle cases where t1 doesn't evaluate to an Int *)
59 let (Int i1, s') = eval t1 g s
60 (* Iszero t1 should evaluate to a Bool *)
61 in (Bool (i1 = 0), s')
63 (* we don't handle cases where t1 doesn't evaluate to a boolean *)
64 let (Bool b1, s') = eval t1 g s
65 (* note we thread s' through only one of the then/else clauses *)
66 in if b1 then eval t2 g s'
68 | Makepair (t1, t2) ->
69 let (value1, s') = eval t1 g s
70 in let (value2, s'') = eval t2 g s'
71 in (Pair (value1, value2), s'')
73 (* we don't handle cases where t1 doesn't evaluate to a Pair *)
74 let (Pair (value1, value2), s') = eval t1 g s
76 | Lambda (arg_var, t2) -> (Closure (arg_var, t2, g), s)
78 (* we don't handle cases where t1 doesn't evaluate to a function value *)
79 let (Closure (arg_var, body, savedg), s') = eval t1 g s
80 in let (value2, s'') = eval t2 g s'
81 (* evaluate body under savedg, except with arg_var bound to Nonrecursive value2 *)
82 in let savedg' = (arg_var, Nonrecursive value2) :: 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 (* evaluate t3 under a new assignment where var_to_bind has been recursively bound to that function value *)
88 in let g' = (var_to_bind, Recursive_Closure (var_to_bind, arg_var, body, savedg)) :: g
91 let (value1, s') = eval t1 g s
92 (* note that s' may be different from s, if t1 itself contained any mutation operations *)
93 (* now we want to retrieve the next free index in s' *)
94 in let new_index = List.length s'
95 (* now we want to insert value1 there; the following is an easy but inefficient way to do it *)
96 in let s'' = List.append s' [value1]
97 (* now we return a pair of a wrapped new_index, and the new store *)
98 in (Mutcell new_index, s'')
100 (* we don't handle cases where t1 doesn't evaluate to a Mutcell *)
101 let (Mutcell index1, s') = eval t1 g s
102 (* note that s' may be different from s, if t1 itself contained any mutation operations *)
103 in (List.nth s' index1, s')
105 (* we don't handle cases where t1 doesn't evaluate to a Mutcell *)
106 let (Mutcell index1, s') = eval t1 g s
107 (* note that s' may be different from s, if t1 itself contained any mutation operations *)
108 in let (value2, s'') = eval t2 g s'
109 (* now we create a list which is just like s'' except it has value2 in index1 *)
110 in let rec replace_nth lst m =
112 | [] -> failwith "list too short"
113 | x::xs when m = 0 -> value2 :: xs
114 | x::xs -> x :: replace_nth xs (m - 1)
115 in let s''' = replace_nth s'' index1
116 (* we'll arbitrarily return Int 42 as the expressed_value of a Setref operation *)