X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=code%2Fcalculator%2Fcalc5.ml;h=2f533af3a2aad60def4c827e72336c61b7dc84a2;hp=85727060c8cc3877898e272ff6586df47ae47106;hb=ee6e8e390ac5ffa57dc95531939222efda5d37ff;hpb=12c7bba12e14ab2e1ea72a4b95f1aee0c114a188 diff --git a/code/calculator/calc5.ml b/code/calculator/calc5.ml index 85727060..2f533af3 100644 --- a/code/calculator/calc5.ml +++ b/code/calculator/calc5.ml @@ -1,95 +1,95 @@ (* calc3,ml, enhanced with Mutable Pairs *) - type term = - Intconstant of int - | Multiplication of (term * term) - | Addition of (term * term) - | Variable of char - | Let of (char * term * term) - | Iszero of term - | If of (term * term * term) - | Makepair of (term * term) - | First of term - | Lambda of (char * term) - | Apply of (term * term) - | Letrec of (char * term * term) - | Setfirst of (term * term) - ;; + type term = + Intconstant of int + | Multiplication of (term * term) + | Addition of (term * term) + | Variable of char + | Let of (char * term * term) + | Iszero of term + | If of (term * term * term) + | Makepair of (term * term) + | First of term + | Lambda of (char * term) + | Apply of (term * term) + | Letrec of (char * term * term) + | Setfirst of (term * term) + ;; - type index = int;; + type index = int;; type bound_value = Nonrecursive of expressed_value | Recursive_Closure of char * char * term * assignment - and assignment = (char * bound_value) list + and assignment = (char * bound_value) list and expressed_value = Int of int | Bool of bool | Pair of index * index | Closure of char * term * assignment;; - type store = expressed_value list;; + type store = expressed_value list;; - let rec eval (t : term) (g : assignment) (s : store) = match t with - Intconstant x -> (Int x, s) - | Multiplication (t1, t2) -> - (* we don't handle cases where the subterms don't evaluate to Ints *) - let (Int i1, s') = eval t1 g s - in let (Int i2, s'') = eval t2 g s' - (* Multiplication (t1, t2) should evaluate to an Int *) - in (Int (i1 * i2), s'') - | Addition (t1, t2) -> - let (Int i1, s') = eval t1 g s - in let (Int i2, s'') = eval t2 g s' - in (Int (i1 + i2), s'') - | Variable (var) -> ( - (* we don't handle cases where g doesn't bind var to any value *) - match List.assoc var g with + let rec eval (t : term) (g : assignment) (s : store) = match t with + Intconstant x -> (Int x, s) + | Multiplication (t1, t2) -> + (* we don't handle cases where the subterms don't evaluate to Ints *) + let (Int i1, s') = eval t1 g s + in let (Int i2, s'') = eval t2 g s' + (* Multiplication (t1, t2) should evaluate to an Int *) + in (Int (i1 * i2), s'') + | Addition (t1, t2) -> + let (Int i1, s') = eval t1 g s + in let (Int i2, s'') = eval t2 g s' + in (Int (i1 + i2), s'') + | Variable (var) -> (( + (* we don't handle cases where g doesn't bind var to any value *) + match List.assoc var g with | Nonrecursive value -> value | Recursive_Closure (self_var, arg_var, body, savedg) as rec_closure -> - (* we update savedg to bind self_var to rec_closure here *) + (* we update savedg to bind self_var to rec_closure here *) let savedg' = (self_var, rec_closure) :: savedg in Closure (arg_var, body, savedg') - ), s - | Let (var_to_bind, t2, t3) -> - (* evaluate t3 under a new assignment where var_to_bind has been bound to + ), s) + | Let (var_to_bind, t2, t3) -> + (* evaluate t3 under a new assignment where var_to_bind has been bound to the result of evaluating t2 under the current assignment *) - let (value2, s') = eval t2 g s - (* we have to wrap value2 in Nonrecursive *) - in let g' = (var_to_bind, Nonrecursive value2) :: g - in eval t3 g' s' - | Iszero (t1) -> - (* we don't handle cases where t1 doesn't evaluate to an Int *) - let (Int i1, s') = eval t1 g s - (* Iszero t1 should evaluate to a Bool *) - in (Bool (i1 = 0), s') - | If (t1, t2, t3) -> - (* we don't handle cases where t1 doesn't evaluate to a boolean *) - let (Bool b1, s') = eval t1 g s + let (value2, s') = eval t2 g s + (* we have to wrap value2 in Nonrecursive *) + in let g' = (var_to_bind, Nonrecursive value2) :: g + in eval t3 g' s' + | Iszero (t1) -> + (* we don't handle cases where t1 doesn't evaluate to an Int *) + let (Int i1, s') = eval t1 g s + (* Iszero t1 should evaluate to a Bool *) + in (Bool (i1 = 0), s') + | If (t1, t2, t3) -> + (* we don't handle cases where t1 doesn't evaluate to a boolean *) + let (Bool b1, s') = eval t1 g s (* note we thread s' through only one of the then/else clauses *) - in if b1 then eval t2 g s' - else eval t3 g s' - | Makepair (t1, t2) -> - let (value1, s') = eval t1 g s - in let (value2, s'') = eval t2 g s' + in if b1 then eval t2 g s' + else eval t3 g s' + | Makepair (t1, t2) -> + let (value1, s') = eval t1 g s + in let (value2, s'') = eval t2 g s' (* now we want to retrieve the next free index in s'' *) in let new_index = List.length s'' (* now we want to insert value1 and value2 there; the following is an easy but inefficient way to do it *) in let s''' = List.append s'' [value1; value2] - in (Pair (new_index, new_index + 1), s''') - | First (t1) -> - (* we don't handle cases where t1 doesn't evaluate to a Pair *) - let (Pair (index1, index2), s') = eval t1 g s + in (Pair (new_index, new_index + 1), s''') + | First (t1) -> + (* we don't handle cases where t1 doesn't evaluate to a Pair *) + let (Pair (index1, index2), s') = eval t1 g s (* note that s' may be different from s, if t1 itself contained any mutation operations *) in (List.nth s' index1, s') - | Lambda (arg_var, t2) -> (Closure (arg_var, t2, g), s) - | Apply (t1, t2) -> - (* we don't handle cases where t1 doesn't evaluate to a function value *) - let (Closure (arg_var, body, savedg), s') = eval t1 g s - in let (value2, s'') = eval t2 g s' - (* evaluate body under savedg, except with arg_var bound to Nonrecursive value2 *) - in let savedg' = (arg_var, Nonrecursive value2) :: savedg - in eval body savedg' s'' - | Letrec (var_to_bind, t2, t3) -> - (* we don't handle cases where t2 doesn't evaluate to a function value *) - let (Closure (arg_var, body, savedg), s') = eval t2 g s + | Lambda (arg_var, t2) -> (Closure (arg_var, t2, g), s) + | Apply (t1, t2) -> + (* we don't handle cases where t1 doesn't evaluate to a function value *) + let (Closure (arg_var, body, savedg), s') = eval t1 g s + in let (value2, s'') = eval t2 g s' + (* evaluate body under savedg, except with arg_var bound to Nonrecursive value2 *) + in let savedg' = (arg_var, Nonrecursive value2) :: savedg + in eval body savedg' s'' + | Letrec (var_to_bind, t2, t3) -> + (* we don't handle cases where t2 doesn't evaluate to a function value *) + let (Closure (arg_var, body, savedg), s') = eval t2 g s (* evaluate t3 under a new assignment where var_to_bind has been recursively bound to that function value *) - in let g' = (var_to_bind, Recursive_Closure (var_to_bind, arg_var, body, savedg)) :: g - in eval t3 g' s' + in let g' = (var_to_bind, Recursive_Closure (var_to_bind, arg_var, body, savedg)) :: g + in eval t3 g' s' | Setfirst (t1, t2) -> (* we don't handle cases where t1 doesn't evaluate to a Pair *) let (Pair (index1, index2), s') = eval t1 g s