tweak calc improvements
[lambda.git] / code / calculator / calc7.ml
1 (* calc6.ml, enhanced with Aliases and Passing by Reference *)
2
3         type term =
4       Intconstant of int
5     | Multiplication of (term * term)
6     | Addition of (term * term)
7     | Variable of char
8     | Let of (char * term * term)
9     | Iszero of term
10     | If of (term * term * term)
11     | Makepair of (term * term)
12     | First of term
13     | Lambda of (char * term)
14     | Apply of (term * term)
15     | Letrec of (char * term * term)
16     | Change of (char * term * term)
17     | Alias of (char * char * term)
18     | Applyalias of (term * char)
19     ;;
20
21         type index = int;;
22
23     type bound_value = index;;
24     type assignment = (char * bound_value) list;;
25         type expressed_value = Int of int | Bool of bool | Pair of expressed_value * expressed_value | Closure of char * term * assignment;;
26
27         type store = expressed_value list;;
28
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         let index = List.assoc var g
44         (* get value stored at location index in s *)
45         in let value = List.nth s index
46         in (value, s)
47         | Let (var_to_bind, t2, t3) ->
48                 let (value2, s') = eval t2 g s
49                 (* note that s' may be different from s, if t2 itself contained any mutation operations *)
50         (* get next free index in s' *)
51                 in let new_index = List.length s'
52                 (* now we want to insert value2 there; the following is an easy but inefficient way to do it *)
53                 in let s'' = List.append s' [value2]
54         (* bind var_to_bind to location new_index in the store *)
55         in let g' = ((var_to_bind, new_index) :: g)
56         in eval t3 g' s''
57         | Iszero (t1) ->
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')
62         | If (t1, t2, t3) ->
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'
67                 else eval t3 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'')
72         | First (t1) ->
73                 (* we don't handle cases where t1 doesn't evaluate to a Pair *)
74                 let (Pair (value1, value2), s') = eval t1 g s
75                 in (value1, s')
76         | Lambda (arg_var, t2) -> (Closure (arg_var, t2, g), s)
77         | Apply (t1, t2) ->
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 a new location containing value2 *)
82         in let new_index = List.length s''
83         in let s''' = List.append s'' [value2]
84                 in let savedg' = (arg_var, new_index) :: savedg
85                 in eval body savedg' s'''
86         | Letrec (var_to_bind, t2, t3) ->
87                 (* we don't handle cases where t2 doesn't evaluate to a function value *)
88                 let (Closure (arg_var, body, savedg), s') = eval t2 g s
89         in let new_index = List.length s'
90         in let savedg' = (var_to_bind, new_index) :: savedg
91         in let new_closure = Closure (arg_var, body, savedg')
92         in let s'' = List.append s' [new_closure]
93         in let g' = (var_to_bind, new_index) :: g
94         in eval t3 g' s''
95         | Change (var, t2, t3) ->
96                 (* we don't handle cases where g doesn't bind var to any value *)
97         let index = List.assoc var g
98         in let (value2, s') = eval t2 g s
99                 (* note that s' may be different from s, if t2 itself contained any mutation operations *)
100                 (* now we create a list which is just like s' except it has value2 at index *)
101                 in let rec replace_nth lst m =
102                         match lst with
103                         | [] -> failwith "list too short"
104                         | x::xs when m = 0 -> value2 :: xs
105                         | x::xs -> x :: replace_nth xs (m - 1)
106         in let s'' = replace_nth s' index
107         (* evaluate t3 using original assignment function and new store *)
108         in eval t3 g s''
109         | Alias (var_to_bind, orig_var, t3) ->
110                 (* we don't handle cases where g doesn't bind orig_var to any value *)
111         let index = List.assoc orig_var g
112         (* bind var_to_bind to the same index in the store *)
113         in let g' = ((var_to_bind, index) :: g)
114         in eval t3 g' s
115         | Applyalias (t1, var) ->
116                 (* we don't handle cases where t1 doesn't evaluate to a function value *)
117                 let (Closure (arg_var, body, savedg), s') = eval t1 g s
118                 (* we don't handle cases where g doesn't bind var to any value *)
119         in let index = List.assoc var g
120                 (* evaluate body under savedg, except with arg_var bound to existing index *)
121                 in let savedg' = (arg_var, index) :: savedg
122                 in eval body savedg' s'
123     ;;
124