X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=week9.mdwn;h=2b80483989efca1fd91845b1a022eee173b2ee36;hp=f5beebb4aa78e74e462a90bd9f5ce58869e37bdb;hb=40b5ff7bf5c1f0fe0843ca938af8ced1ec5c9976;hpb=54f07a87abd25524b0e1599cc5208c0c88b7a8c1 diff --git a/week9.mdwn b/week9.mdwn index f5beebb4..2b804839 100644 --- a/week9.mdwn +++ b/week9.mdwn @@ -291,7 +291,11 @@ Now we're going to relativize our interpretations not only to the assignment fun > \[[expression]]g s = (value, s') -For expressions we already know how to interpret, `s'` will usually just be `s`. One exception is complex expressions like `let var = expr1 in expr2`. Part of interpreting this will be to interpret the sub-expression `expr1`, and we have to allow that in doing that, the store may have already been updated. We want to use that possibly updated store when interpreting `expr2`. Like this: +For expressions we already know how to interpret, expect `s'` to just be `s`. +An exception is complex expressions like `let var = expr1 in expr2`. Part of +interpreting this will be to interpret the sub-expression `expr1`, and we have +to allow that in doing that, the store may have already been updated. We want +to use that possibly updated store when interpreting `expr2`. Like this: let rec eval expression g s = match expression with @@ -304,6 +308,15 @@ For expressions we already know how to interpret, `s'` will usually just be `s`. eval expr2 ((c, value) :: g) s' ... +Similarly: + + ... + | Addition (expr1, expr2) -> + let (value1, s') = eval expr1 g s + in let (value2, s'') = eval expr2 g s' + in (value1 + value2, s'') + ... + Let's consider how to interpet our new syntactic forms `newref`, `deref`, and `setref`: @@ -332,7 +345,7 @@ Let's consider how to interpet our new syntactic forms `newref`, `deref`, and `s in (Index new_index, s'') ... -2. When `expr` evaluates to a `store_index`, then `deref expr` should evaluate to whatever value is at that index in the current store. (If `expr` evaluates to a value of another type, `deref expr` is undefined.) In this operation, we don't change the store at all; we're just reading from it. So we'll return the same store back unchanged. +2. When `expr` evaluates to a `store_index`, then `deref expr` should evaluate to whatever value is at that index in the current store. (If `expr` evaluates to a value of another type, `deref expr` is undefined.) In this operation, we don't change the store at all; we're just reading from it. So we'll return the same store back unchanged (assuming it wasn't changed during the evaluation of `expr`). let rec eval expression g s = match expression with @@ -350,7 +363,7 @@ Let's consider how to interpet our new syntactic forms `newref`, `deref`, and `s ... | Setref (expr1, expr2) -> let (Index n, s') = eval expr1 g s - (* note that s' may be different from s, if expr itself contained any mutation operations *) + (* note that s' may be different from s, if expr1 itself contained any mutation operations *) in let (new_value, s'') = eval expr2 g s' (* now we create a list which is just like s'' except it has new_value in index n *) in let rec replace_nth lst m =