week9 implicit-style
[lambda.git] / week9.mdwn
index abff587..e992bf4 100644 (file)
@@ -2,7 +2,7 @@
 
 The seminar is now going to begin talking about more **imperatival** or **effect**-like elements in programming languages. The only effect-like element we've encountered so far is the possibility of divergence, in languages that permit fixed point combinators and so have the full power of recursion. What it means for something to be effect-like, and why this counts as an example of such, will emerge.
 
-Other effect-like elements in a language include: printing (recall the [[damn]] example at the start of term); continuations (also foreshadowed in the [[damn]] example) and exceptions (foreshadowed in our discussion of abortable list traversals in [[week4]]); and **mutation**. This last notion is our first topic.
+Other effect-like elements in a language include: printing (recall the [[damn]] example at the start of term); continuations (also foreshadowed in the [[damn]] example) and exceptions (foreshadowed in our discussion of abortable list traversals in [[week4]]); and **mutation**. This last notion is our topic this week.
 
 
 ## Mutation##
@@ -37,7 +37,7 @@ If the expression that evaluates to a function value has a free variable in it,
 
 Other choices about how to interpret free variables are also possible (you can read about "lexical scope" versus "dynamic scope"), but what we do here is the norm in functional programming languages, and seems to be easiest for programmers to reason about.
 
-In our next fragement, we re-use a variable that had been bound to another value in a wider context:
+In our next fragment, we re-use a variable that had been bound to another value in a wider context:
 
        [E] let x be 4 in
                  let x be 3 in
@@ -99,9 +99,9 @@ When dealing with explicit-style mutation, there's a difference between the type
 
 ##Controlling order##
 
-When we're dealing with mutable variables (or any other kind of effect), order matters. For example, it would make a big difference whether I evaluated "let z = !ycell" before or after evaluating "ycell := !ycell + 1". Before this point, order never mattered except with respect to sometimes avoiding divergence.
+When we're dealing with mutable variables (or any other kind of effect), order matters. For example, it would make a big difference whether I evaluated `let z = !ycell` before or after evaluating `ycell := !ycell + 1`. Before this point, order never mattered except sometimes it played a role in avoiding divergence.
 
-OCaml does not however guarantee what order expressions will be evaluated in arbitrary contexts. For example, in the following fragment, you cannot rely on `expression_a` being evaluated before `expression_b` before `expression_c`:
+OCaml does *not* guarantee what order expressions will be evaluated in arbitrary contexts. For example, in the following fragment, you cannot rely on `expression_a` being evaluated before `expression_b` before `expression_c`:
 
        let triple = (expression_a, expression_b, expression_c)
 
@@ -161,7 +161,7 @@ How could such functions be useful? Well, as always, the context in which you bu
        in f ()
        in z;;
 
-We don't apply (or call or execute or however you want to say it) the function `f` until after we've extracted `ycell`'s value and assigned it to `z`. So `z` will get assigned to 1. If on the other hand we called `f ()` before evaluating `let z = !ycell`, then `z` would have gotten assigned a different value.
+We don't apply (or call or execute or however you want to say it) the function `f` until after we've extracted `ycell`'s value and assigned it to `z`. So `z` will get assigned 1. If on the other hand we called `f ()` before evaluating `let z = !ycell`, then `z` would have gotten assigned a different value.
 
 In languages with mutable variables, the free variables in a function definition are usually taken to refer back to the same *reference cells* they had in their lexical contexts, and not just their original value. So if we do this for instance:
 
@@ -172,15 +172,15 @@ In languages with mutable variables, the free variables in a function definition
                in let setter (new_value : int) =
                        free_var := new_value
                in (getter, setter)
-       in let (getter1, setter1) = factory 1
-       in let first = getter1 ()
-       in let () = setter1 2
-       in let second = getter1 ()
-       in let () = setter1 3
-       in let third = getter1 ()
+       in let (getter, setter) = factory 1
+       in let first = getter ()
+       in let () = setter 2
+       in let second = getter ()
+       in let () = setter 3
+       in let third = getter ()
        in (first, second, third)
        
-At the end, we'll get `(1, 2, 3)`. The reference cell that gets updated when we call `setter1` is the same one that gets fetched from when we call `getter1`. This should seem very intuitive here, since we're working with explicit-style mutation. When working with a language with implicit-style mutation, it can be more surprising. For instance, here's the same fragment in Python, which has implicit-style mutation:
+At the end, we'll get `(1, 2, 3)`. The reference cell that gets updated when we call `setter` is the same one that gets fetched from when we call `getter`. This should seem very intuitive here, since we're working with explicit-style mutation. When working with a language with implicit-style mutation, it can be more surprising. For instance, here's the same fragment in Python, which has implicit-style mutation:
 
        def factory (starting_value):
                free_var = starting_value
@@ -193,26 +193,26 @@ At the end, we'll get `(1, 2, 3)`. The reference cell that gets updated when we
                        nonlocal free_var
                        free_var = new_value
                return getter, setter
-       getter1, setter1 = factory (1)
-       first = getter1 ()
-       setter1 (2)
-       second = getter1 ()
-       setter1 (3)
-       third = getter1 ()
+       getter, setter = factory (1)
+       first = getter ()
+       setter (2)
+       second = getter ()
+       setter (3)
+       third = getter ()
        (first, second, third)
 
-Here, too, just as in the OCaml fragment, all the calls to getter1 and setter1 are working with a single mutable variable `free_var`.
+Here, too, just as in the OCaml fragment, all the calls to getter and setter are working with a single mutable variable `free_var`.
 
 If however you called `factory` twice, you'd have different `getter`/`setter` pairs, each of which had their own, independent `free_var`. In OCaml:
 
        let factory (starting_val : int) =
        ... (* as above *)
-       in let (getter1, setter1) = factory 1
-       in let (getter2, setter2) = factory 1
-       in let () = setter1 2
-       in getter2 ()
+       in let (getter, setter) = factory 1
+       in let (getter', setter') = factory 1
+       in let () = setter 2
+       in getter' ()
 
-Here, the call to `setter1` only mutated the reference cell associated with the `getter1`/`setter1` pair. The reference cell associated with `getter2` hasn't changed, and so `getter2 ()` will still evaluate to 1.
+Here, the call to `setter` only mutated the reference cell associated with the `getter`/`setter` pair. The reference cell associated with `getter'` hasn't changed, and so `getter' ()` will still evaluate to 1.
 
 Notice in these fragments that once we return from inside the call to `factory`, the `free_var` mutable variable is no longer accessible, except through the helper functions `getter` and `setter` that we've provided. This is another way in which a thunk like `getter` can be useful: it still has access to the `free_var` reference cell that was created when it was, because its free variables are interpreted relative to the context in which `getter` was built, even if that context is otherwise no longer accessible. What `getter ()` evaluates to, however, will very much depend on *when* we evaluate it---in particular, it will depend on which calls to the corresponding `setter` were evaluated first.
 
@@ -244,11 +244,120 @@ Notice, however, that when mutable variables are present, the same substitution
 
 ##How to implement explicit-style mutable variables##
 
--- FIXME --
+We'll think about how to implement explicit-style mutation first. We suppose that we add some new syntactic forms to a language, let's call them `newref`, `deref`, and `setref`. And now we want to expand the semantics for the language so as to interpret these new forms.
+
+Well, part of our semantic machinery will be an assignment function, call it `g`. Somehow we'd have to keep track of the types of the variables and values we're working with, but we won't pay much attention to that now. In fact, we won't even both much at this point with the assignment function. Below we'll pay more attention to it.
+
+In addition to the assignment function, we'll also need a way to keep track of how many reference cells have been "allocated" (using `newref`), and what their current values are. We'll suppose all the reference cells are organized in a single data structure we'll call a **store**. This might be a big heap of memory. For our purposes, we'll suppose that reference cells only ever contain `int`s, and we'll let the store be a list of `int`s.
+
+In many languages, including OCaml, the first position in a list is indexed `0`, the second is indexed `1` and so on. If a list has length 2, then there won't be any value at index `2`; that will be the "next free location" in the list.
+
+Before we brought mutation on the scene, our language's semantics will have looked something like this:
+
+>      \[[expression]]<sub>g</sub> = value
+
+Now we're going to relativize our interpretations not only to the assignment function `g`, but also to the current store, which I'll label `s`. Additionally, we're going to want to allow that evaluating some functions might *change* the store, perhaps by allocating new reference cells or perhaps by updating the contents of some existing cells. So the interpretation of an expression won't just return a value; it will also return a possibly updated store. We'll suppose that our interpretation function does this quite generally, even though for many expressions in the language, the store that's returned will be the same one that the interpretation function started with:
+
+>      \[[expression]]<sub>g s</sub> = (value, s')
+
+With that kind of framework, we can interpret `newref`, `deref`, and `setref` as follows.
+
+1.     \[[newref starting_val]] should allocate a new reference cell in the store and insert `starting_val` into that cell. It should return some "key" or "index" or "pointer" to the newly created reference cell, so that we can do things like:
+
+               let ycell = newref 1
+               in ...
+
+       and be able to refer back to that cell later by using the value that we assigned to the variable `ycell`. In our simple implementation, we're letting the store just be an `int list`, and we can let the "keys" be indexes in that list, which are just `int`s. Somehow we'd have to keep track of which variables are assigned `int`s as `int`s and which are assigned `int`s as indexes into the store. So we'll create a special type to wrap the latter:
+
+               type store_index = Index of int;;
+               
+               let rec eval expression g s =
+                       match expression with
+                       ...
+                       | Newref expr ->
+                               let (starting_val, s') = eval expr g s
+                               (* note that s' may be different from s, if expr 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 return a pair of a wrapped new_index, and the new store *)
+                               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.
+
+               let rec eval expression g s =
+                       match expression with
+                       ...
+                       | Deref expr ->
+                               let (Index n, s') = eval expr g s
+                               (* note that s' may be different from s, if expr itself contained any mutation operations *)
+                               in (List.nth s' n, s')
+                       ...
+
+3.     When `expr1` evaluates to a `store_index` and `expr2` evaluates to an `int`, then `setref expr1 expr2` should have the effect of changing the store so that the reference cell at that index now contains that `int`. We have to make a decision about what value the `setref ...` call should itself evaluate to; OCaml makes this `()` but other choices are also possible. Here I'll just suppose we've got some appropriate value in the variable `dummy`.
+
+               let rec eval expression g s =
+                       match expression with
+                       ...
+                       | 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 *)
+                               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 =
+                                       match lst with
+                                       | [] -> failwith "list too short"
+                                       | x::xs when m = 0 -> new_value :: xs
+                                       | x::xs -> x :: replace_nth xs (m - 1)
+                               in let s''' = replace_nth s'' n
+                               in (dummy, s''')
+                       ...
+
 
 ##How to implement implicit-style mutable variables##
 
--- FIXME --
+With implicit-style mutation, we don't have new syntactic forms like `newref` and `deref`. Instead, we just treat ordinary variables as being mutable. You could if you wanted to have some variables be mutable and others not; perhaps the first sort are written in Greek and the second in Latin. But we will suppose all variables in our language are mutable.
+
+We will still need a store to keep track of reference cells and their current values, just as in the explicit-style implementation. This time, every variable will be associated with an index into the store. So this is what we'll have our assignment function keep track of. The assignment function will bind variables to indexes into the store, rather than to the variables' current values. The variables will only indirectly be associated with those values by virtue of the joint work of the assignment function and the store.
+
+This brings up an interesting conceptual novelty. Formerly, we'd naturally think that a variable `x` is associated with only one type, and that that's the type that the expression `x` would *evaluate to*, and also the type of value that the assignment function *bound* `x` to. However, in the current framework these two types can come apart. The assignment function binds `x` to an index into the store, and what the expression `x` evaluates to will be the value at that location in the store, which might be some other type, such as a `bool` or a `string`.
+
+To handle implicit-style mutation, we'll need to re-implement the way we interpret expressions like `x`, `let x = expr1 in expr2`. We will have just one new syntactic form, `change x to expr1 then expr2`.
+
+Here's how to implement these. We'll suppose that our assignment function is list of pairs, as in [week6](/reader_monad_for_variable_binding).
+
+       let rec eval expression g s =
+               match expression with
+               ...
+               | Var (c : char) ->
+                       let index = List.assoc c g
+                       in List.nth s index
+
+               | Let (c : char) expr1 expr2 ->
+                       let (starting_value, s') = eval expr1 g s
+                       (* get next free index in s' *)
+                       in let new_index = List.length s'
+                       (* insert starting_value there *)
+                       in let s'' = List.append s' [starting_value]
+                       (* evaluate expr2 using a new assignment function and store *)
+                       in eval expr2 ((c, new_index) :: g) s''
+
+               | Change (c : char) expr1 expr2 ->
+                       let (new_value, s') = eval expr1 g s
+                       (* lookup which index is associated with Var c *)
+                       in let n = List.assoc c g
+                       (* 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 =
+                               match lst with
+                               | [] -> failwith "list too short"
+                               | x::xs when m = 0 -> new_value :: xs
+                               | x::xs -> x :: replace_nth xs (m - 1)
+                       in let s'' = replace_nth s' n
+                       (* evaluate expr2 using original assignment function and new store *)
+                       in eval expr2 g s''
+
 
 ##How to implicit mutation with a State monad##
 
@@ -373,7 +482,7 @@ Notice, however, that when mutable variables are present, the same substitution
                in let () = fact_cell := Some factorial
                in ...
 
-       We use the `None`/`Some factorial` option type here just as a way to ensure that the contents of fact_cell are of the same type both at the start and the end of the block.
+       We use the `None`/`Some factorial` option type here just as a way to ensure that the contents of `fact_cell` are of the same type both at the start and the end of the block.
 
 
 <!--