1 (* calc5.ml: calc3,ml enhanced with Mutable Pairs *)
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 | Setfirst of (term * term)
21 type bound_value = Nonrecursive of expressed_value | Recursive_Closure of char * char * term * assignment
22 and assignment = (char * bound_value) list
23 and expressed_value = Int of int | Bool of bool | Pair of index * index | 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'')
39 | Variable (var) -> ((
40 (* we don't handle cases where g doesn't bind var to any value *)
41 match List.assoc var g with
42 | Nonrecursive value -> value
43 | Recursive_Closure (self_var, arg_var, body, savedg) as rec_closure ->
44 (* we update savedg to bind self_var to rec_closure here *)
45 let savedg' = (self_var, rec_closure) :: savedg
46 in Closure (arg_var, body, savedg')
48 | Let (var_to_bind, t2, t3) ->
49 (* evaluate t3 under a new assignment where var_to_bind has been bound to
50 the result of evaluating t2 under the current assignment *)
51 let (value2, s') = eval t2 g s
52 (* we have to wrap value2 in Nonrecursive *)
53 in let g' = (var_to_bind, Nonrecursive value2) :: 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 (* now we want to retrieve the next free index in s'' *)
70 in let new_index = List.length s''
71 (* now we want to insert value1 and value2 there; the following is an easy but inefficient way to do it *)
72 in let s''' = List.append s'' [value1; value2]
73 in (Pair (new_index, new_index + 1), s''')
75 (* we don't handle cases where t1 doesn't evaluate to a Pair *)
76 let (Pair (index1, index2), s') = eval t1 g s
77 (* note that s' may be different from s, if t1 itself contained any mutation operations *)
78 in (List.nth s' index1, s')
79 | Lambda (arg_var, t2) -> (Closure (arg_var, t2, g), s)
81 (* we don't handle cases where t1 doesn't evaluate to a function value *)
82 let (Closure (arg_var, body, savedg), s') = eval t1 g s
83 in let (value2, s'') = eval t2 g s'
84 (* evaluate body under savedg, except with arg_var bound to Nonrecursive value2 *)
85 in let savedg' = (arg_var, Nonrecursive value2) :: savedg
86 in eval body savedg' s''
87 | Letrec (var_to_bind, t2, t3) ->
88 (* we don't handle cases where t2 doesn't evaluate to a function value *)
89 let (Closure (arg_var, body, savedg), s') = eval t2 g s
90 (* evaluate t3 under a new assignment where var_to_bind has been recursively bound to that function value *)
91 in let g' = (var_to_bind, Recursive_Closure (var_to_bind, arg_var, body, savedg)) :: g
93 | Setfirst (t1, t2) ->
94 (* we don't handle cases where t1 doesn't evaluate to a Pair *)
95 let (Pair (index1, index2), s') = eval t1 g s
96 (* note that s' may be different from s, if t1 itself contained any mutation operations *)
97 in let (value2, s'') = eval t2 g s'
98 (* now we create a list which is just like s'' except it has value2 in index1 *)
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'' index1