Lecture #15, Tuesday, February 25th =================================== - Implementing `rec` Using Cyclic Structures - Boxes and Mutation ------------------------------------------------------------------------ # Implementing `rec` Using Cyclic Structures > [PLAI ยง10] Looking at the arrows in the environment diagrams, what we're really looking for is a closure that has an environment pointer which is the same environment in which it was defined. This will make it possible for `fact` to be bound to a closure that can refer to *itself* since its environment is the same one in which it is defined. However, so far we have no tools that makes it possible to do this. What we need is to create a "cycle of pointers", and so far we do not have a way of achieving that: when we create a closure, we begin with an environment which is saved in the slot's environment slot, but we want that closure to be the value of a binding in that same environment. ------------------------------------------------------------------------ # Boxes and Mutation To actually implement a circular structure, we will now use *side-effects*, using a new kind of Racket value which supports mutation: a box. A box value is built with the `box` constructor: (define my-thing (box 7)) the value is retrieved with the `unbox' function, (* 6 (unbox my-thing)) and finally, the value can be changed with the `set-box!` function. (set-box! my-thing 17) (* 6 (unbox my-thing)) An important thing to note is that `set-box!` is much like `display` etc, it returns a value that is not printed in the Racket REPL, because there is no point in using the result of a `set-box!`, it is called for the side-effect it generates. (Languages like C blur this distinction between returning a value and a side-effect with its assignment statement.) As a side note, we now have side effects of two kinds: mutation of state, and I/O (at least the O part). (Actually, there is also infinite looping that can be viewed as another form of a side effect.) This means that we're now in a completely different world, and lots of new things can make sense now. A few things that you should know about: * We never used more than one expression in a function body because there was no point in it, but now there is. To evaluate a sequence of Racket expressions, you wrap them in a `begin` expression. * In most places you don't actually need to use `begin` --- these are places that are said to have an *implicit* `begin`: the body of a function (or any lambda expression), the body of a `let` (and `let`-relatives), the consequence positions in `cond`, `match`, and `cases` clauses and more. One of the common places where a `begin` is used is in an `if` expression (and some people prefer using `cond` instead when there is more than a single expression). * `cond` without an `else` in the end can make sense, if all you're using it it for is side-effects. * `if` could get a single expression which is executed when the condition is true (and an unspecified value is used otherwise), but our language (as well as the default Racket language) always forbids this --- there are convenient special forms for a one-sided `if`s: `when` & `unless`, and they can have any number of expressions (they have an implicit `begin`). They have an advantage of saying "this code does some side-effects here" more explicit. * There is a function called `for-each` which is just like `map`, except that it doesn't collect the list of results, it is used only for performing side effects. * Aliasing and the concept of "object equality": `equal?` vs `eq?`. For example: (: foo : (Boxof ...) (Boxof ...) -> ...) (define (foo a b) (set-box! a 1)) ;*** this might change b, can check `eq?` When any one of these things is used (in Racket or other languages), you can tell that side-effects are involved, because there is no point in any of them otherwise. In addition, any name that ends with a `!` ("bang") is used to mark a function that changes state (usually a function that only changes state). So how do we create a cycle? Simple, boxes can have any value, and they can be put in other values like lists, so we can do this: #lang pl untyped (define foo (list 1 (box 3))) (set-box! (second foo) foo) and we get a circular value. (Note how it is printed.) And with types: #lang pl (: foo : (List Number (Boxof Any))) (define foo (list 1 (box 3))) (set-box! (second foo) foo)