X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=advanced_topics%2Fcalculator_improvements.mdwn;h=541e6958e1c1bb54783f5691e886aa9801c93ce7;hp=280604f4677ced7e9a1af39e93ac336f1013c4c2;hb=afae1eb5d1dc7633dd5ac8f0e2535c22cd9cfaea;hpb=38f178b5967f0875383d87a92330ecd58979f83e diff --git a/advanced_topics/calculator_improvements.mdwn b/advanced_topics/calculator_improvements.mdwn index 280604f4..541e6958 100644 --- a/advanced_topics/calculator_improvements.mdwn +++ b/advanced_topics/calculator_improvements.mdwn @@ -448,12 +448,12 @@ Now we need to formulate the clauses for evaluating the new forms `Newref (...)` ... | Newref (t1) -> - let (starting_val, s') = eval t1 g s + let (value1, s') = eval t1 g s (* note that s' may be different from s, if t1 itself contained any mutation operations *) (* now we want to retrieve the next free index in s' *) in let new_index = List.length s' - (* now we want to insert starting_val there; the following is an easy but inefficient way to do it *) - in let s'' = List.append s' [starting_val] + (* now we want to insert value1 there; the following is an easy but inefficient way to do it *) + in let s'' = List.append s' [value1] (* now we return a pair of a wrapped new_index, and the new store *) in (Mutcell new_index, s'') | Deref (t1) -> @@ -465,12 +465,12 @@ Now we need to formulate the clauses for evaluating the new forms `Newref (...)` (* we don't handle cases where t1 doesn't evaluate to a Mutcell *) let (Mutcell index1, s') = eval t1 g s (* note that s' may be different from s, if t1 itself contained any mutation operations *) - in let (new_value, s'') = eval t2 g s' - (* now we create a list which is just like s'' except it has new_value in index1 *) + in let (value2, s'') = eval t2 g s' + (* now we create a list which is just like s'' except it has value2 in index1 *) in let rec replace_nth lst m = match lst with | [] -> failwith "list too short" - | x::xs when m = 0 -> new_value :: xs + | x::xs when m = 0 -> value2 :: xs | x::xs -> x :: replace_nth xs (m - 1) in let s''' = replace_nth s'' index1 (* we'll arbitrarily return Int 42 as the expressed_value of a Setref operation *) @@ -538,12 +538,12 @@ Finally, here are the changed or added clauses to the evaluation function: (* 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 let (new_value, s'') = eval t2 g s' - (* now we create a list which is just like s'' except it has new_value in index1 *) + in let (value2, s'') = eval t2 g s' + (* now we create a list which is just like s'' except it has value2 in index1 *) in let rec replace_nth lst m = match lst with | [] -> failwith "list too short" - | x::xs when m = 0 -> new_value :: xs + | x::xs when m = 0 -> value2 :: xs | x::xs -> x :: replace_nth xs (m - 1) in let s''' = replace_nth s'' index1 in (Int 42, s''') @@ -585,7 +585,7 @@ In the present implementation, we separate the roles of the `bound_value` and `e type store = expressed_value list;; -Our evaluation function still interacts with a `store` argument in much the same way it did with explicit-style mutation. The clause for `Variable ...` works differently, because all `expressed_value`s now need to be retrieved from the `store`: +Our evaluation function still interacts with a `store` argument in much the same way it did with explicit-style mutation. The clause for `Variable (...)` works differently, because all `expressed_value`s now need to be retrieved from the `store`: let rec eval (t : term) (g : assignment) (s : store) = match t with ... @@ -655,7 +655,7 @@ Finally, here is the clause for `Change (...)`, which takes over the role earlie in eval t3 g s'' ;; -Note that because the `savedg` component of a `Closure` keeps track of which `index`es in the store free variables were bound to, the values at those `index`es can later be changed, and later applications of the `Closure` will use the changed values. +Note that because the `savedg` component of a `Closure` keeps track of which `index`es in the store---rather than which values---free variables were bound to, the values at those `index`es can later be changed, and later applications of the `Closure` will use the changed values. The complete code is available [here](/code/calculator/calc6.ml).