1 (* calc1.ml: original calculator from Week7, enhanced with Booleans and Immutable 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)
15 type expressed_value = Int of int | Bool of bool | Pair of expressed_value * expressed_value;;
16 type bound_value = expressed_value;;
17 type assignment = (char * bound_value) list;;
19 let rec eval (t : term) (g : assignment) = match t with
20 Intconstant x -> Int x
21 | Multiplication (t1, t2) ->
22 (* we don't handle cases where the subterms don't evaluate to Ints *)
23 let Int i1 = eval t1 g
24 in let Int i2 = eval t2 g
25 (* Multiplication (t1, t2) should evaluate to an Int *)
27 | Addition (t1, t2) ->
28 let Int i1 = eval t1 g
29 in let Int i2 = eval t2 g
32 (* we don't handle cases where g doesn't bind var to any value *)
34 | Let (var_to_bind, t2, t3) ->
35 (* evaluate t3 under a new assignment where var_to_bind has been bound to
36 the result of evaluating t2 under the current assignment *)
37 let value2 = eval t2 g
38 in let g' = (var_to_bind, value2) :: g
41 (* we don't handle cases where t1 doesn't evaluate to an Int *)
42 let Int i1 = eval t1 g
43 (* Iszero t1 should evaluate to a Bool *)
46 (* we don't handle cases where t1 doesn't evaluate to a boolean *)
47 let Bool b1 = eval t1 g
48 in if b1 then eval t2 g
50 | Makepair (t1, t2) ->
51 let value1 = eval t1 g
52 in let value2 = eval t2 g
53 in Pair (value1, value2)
55 (* we don't handle cases where t1 doesn't evaluate to a Pair *)
56 let Pair (value1, value2) = eval t1 g