week11 tweaks
[lambda.git] / zipper-lists-continuations.mdwn
1
2 [[!toc]]
3
4 Today we're going to encounter continuations.  We're going to come at
5 them from three different directions, and each time we're going to end
6 up at the same place: a particular monad, which we'll call the
7 continuation monad.
8
9 Much of this discussion benefited from detailed comments and
10 suggestions from Ken Shan.
11
12
13 Rethinking the list monad
14 -------------------------
15
16 To construct a monad, the key element is to settle on a type
17 constructor, and the monad naturally follows from that.  We'll remind
18 you of some examples of how monads follow from the type constructor in
19 a moment.  This will involve some review of familair material, but
20 it's worth doing for two reasons: it will set up a pattern for the new
21 discussion further below, and it will tie together some previously
22 unconnected elements of the course (more specifically, version 3 lists
23 and monads).
24
25 For instance, take the **Reader Monad**.  Once we decide that the type
26 constructor is
27
28     type 'a reader = env -> 'a
29
30 then the choice of unit and bind is natural:
31
32     let r_unit (a : 'a) : 'a reader = fun (e : env) -> a
33
34 Since the type of an `'a reader` is `env -> 'a` (by definition),
35 the type of the `r_unit` function is `'a -> env -> 'a`, which is a
36 specific case of the type of the *K* combinator.  So it makes sense
37 that *K* is the unit for the reader monad.
38
39 Since the type of the `bind` operator is required to be
40
41     r_bind : ('a reader) -> ('a -> 'b reader) -> ('b reader)
42
43 We can reason our way to the correct `bind` function as follows. We
44 start by declaring the types determined by the definition of a bind operation:
45
46     let r_bind (u : 'a reader) (f : 'a -> 'b reader) : ('b reader) = ...
47
48 Now we have to open up the `u` box and get out the `'a` object in order to
49 feed it to `f`.  Since `u` is a function from environments to
50 objects of type `'a`, the way we open a box in this monad is
51 by applying it to an environment:
52
53         ... f (u e) ...
54
55 This subexpression types to `'b reader`, which is good.  The only
56 problem is that we invented an environment `e` that we didn't already have ,
57 so we have to abstract over that variable to balance the books:
58
59         fun e -> f (u e) ...
60
61 This types to `env -> 'b reader`, but we want to end up with `env ->
62 'b`.  Once again, the easiest way to turn a `'b reader` into a `'b` is to apply it to an environment.  So we end up as follows:
63
64     r_bind (u : 'a reader) (f : 'a -> 'b reader) : ('b reader) =
65                 f (u e) e         
66
67 And we're done. This gives us a bind function of the right type. We can then check whether, in combination with the unit function we chose, it satisfies the monad laws, and behaves in the way we intend. And it does.
68
69 [The bind we cite here is a condensed version of the careful `let a = u e in ...`
70 constructions we provided in earlier lectures.  We use the condensed
71 version here in order to emphasize similarities of structure across
72 monads.]
73
74 The **State Monad** is similar.  Once we've decided to use the following type constructor:
75
76     type 'a state = store -> ('a, store)
77
78 Then our unit is naturally:
79
80     let s_unit (a : 'a) : ('a state) = fun (s : store) -> (a, s)
81
82 And we can reason our way to the bind function in a way similar to the reasoning given above. First, we need to apply `f` to the contents of the `u` box:
83
84     let s_bind (u : 'a state) (f : 'a -> 'b state) : 'b state = 
85                 ... f (...) ...
86
87 But unlocking the `u` box is a little more complicated.  As before, we
88 need to posit a state `s` that we can apply `u` to.  Once we do so,
89 however, we won't have an `'a`, we'll have a pair whose first element
90 is an `'a`.  So we have to unpack the pair:
91
92         ... let (a, s') = u s in ... (f a) ...
93
94 Abstracting over the `s` and adjusting the types gives the result:
95
96         let s_bind (u : 'a state) (f : 'a -> 'b state) : 'b state = 
97                 fun (s : store) -> let (a, s') = u s in f a s'
98
99 The **Option/Maybe Monad** doesn't follow the same pattern so closely, so we
100 won't pause to explore it here, though conceptually its unit and bind
101 follow just as naturally from its type constructor.
102
103 Our other familiar monad is the **List Monad**, which we were told
104 looks like this:
105
106     type 'a list = ['a];;
107     l_unit (a : 'a) = [a];;
108     l_bind u f = List.concat (List.map f u);;
109
110 Thinking through the list monad will take a little time, but doing so
111 will provide a connection with continuations.
112
113 Recall that `List.map` takes a function and a list and returns the
114 result to applying the function to the elements of the list:
115
116     List.map (fun i -> [i;i+1]) [1;2] ~~> [[1; 2]; [2; 3]]
117
118 and List.concat takes a list of lists and erases the embdded list
119 boundaries:
120
121     List.concat [[1; 2]; [2; 3]] ~~> [1; 2; 2; 3]
122
123 And sure enough, 
124
125     l_bind [1;2] (fun i -> [i, i+1]) ~~> [1; 2; 2; 3]
126
127 Now, why this unit, and why this bind?  Well, ideally a unit should
128 not throw away information, so we can rule out `fun x -> []` as an
129 ideal unit.  And units should not add more information than required,
130 so there's no obvious reason to prefer `fun x -> [x,x]`.  In other
131 words, `fun x -> [x]` is a reasonable choice for a unit.
132
133 As for bind, an `'a list` monadic object contains a lot of objects of
134 type `'a`, and we want to make some use of each of them (rather than
135 arbitrarily throwing some of them away).  The only
136 thing we know for sure we can do with an object of type `'a` is apply
137 the function of type `'a -> 'a list` to them.  Once we've done so, we
138 have a collection of lists, one for each of the `'a`'s.  One
139 possibility is that we could gather them all up in a list, so that
140 `bind' [1;2] (fun i -> [i;i]) ~~> [[1;1];[2;2]]`.  But that restricts
141 the object returned by the second argument of `bind` to always be of
142 type `'b list list`.  We can elimiate that restriction by flattening
143 the list of lists into a single list: this is
144 just List.concat applied to the output of List.map.  So there is some logic to the
145 choice of unit and bind for the list monad.  
146
147 Yet we can still desire to go deeper, and see if the appropriate bind
148 behavior emerges from the types, as it did for the previously
149 considered monads.  But we can't do that if we leave the list type 
150 as a primitive Ocaml type.  However, we know several ways of implementing
151 lists using just functions.  In what follows, we're going to use type
152 3 lists (the right fold implementation), though it's important to
153 wonder how things would change if we used some other strategy for
154 implementating lists.  These were the lists that made lists look like
155 Church numerals with extra bits embdded in them:
156
157     empty list:                fun f z -> z
158     list with one element:     fun f z -> f 1 z
159     list with two elements:    fun f z -> f 2 (f 1 z)
160     list with three elements:  fun f z -> f 3 (f 2 (f 1 z))
161
162 and so on.  To save time, we'll let the OCaml interpreter infer the
163 principle types of these functions (rather than inferring what the
164 types should be ourselves):
165
166         # fun f z -> z;;
167         - : 'a -> 'b -> 'b = <fun>
168         # fun f z -> f 1 z;;
169         - : (int -> 'a -> 'b) -> 'a -> 'b = <fun>
170         # fun f z -> f 2 (f 1 z);;
171         - : (int -> 'a -> 'a) -> 'a -> 'a = <fun>
172         # fun f z -> f 3 (f 2 (f 1 z))
173         - : (int -> 'a -> 'a) -> 'a -> 'a = <fun>
174
175 We can see what the consistent, general principle types are at the end, so we
176 can stop. These types should remind you of the simply-typed lambda calculus
177 types for Church numerals (`(o -> o) -> o -> o`) with one extra type
178 thrown in, the type of the element a the head of the list
179 (in this case, an int).
180
181 So here's our type constructor for our hand-rolled lists:
182
183     type 'b list' = (int -> 'b -> 'b) -> 'b -> 'b
184
185 Generalizing to lists that contain any kind of element (not just
186 ints), we have
187
188     type ('a, 'b) list' = ('a -> 'b -> 'b) -> 'b -> 'b
189
190 So an `('a, 'b) list'` is a list containing elements of type `'a`,
191 where `'b` is the type of some part of the plumbing.  This is more
192 general than an ordinary OCaml list, but we'll see how to map them
193 into OCaml lists soon.  We don't need to fully grasp the role of the `'b`'s
194 in order to proceed to build a monad:
195
196     l'_unit (a : 'a) : ('a, 'b) list = fun a -> fun f z -> f a z
197
198 No problem.  Arriving at bind is a little more complicated, but
199 exactly the same principles apply, you just have to be careful and
200 systematic about it.
201
202     l'_bind (u : ('a,'b) list') (f : 'a -> ('c, 'd) list') : ('c, 'd) list'  = ...
203
204 Unpacking the types gives:
205
206     l'_bind (u : ('a -> 'b -> 'b) -> 'b -> 'b)
207             (f : 'a -> ('c -> 'd -> 'd) -> 'd -> 'd)
208             : ('c -> 'd -> 'd) -> 'd -> 'd = ...
209
210 Perhaps a bit intimiating.
211 But it's a rookie mistake to quail before complicated types. You should
212 be no more intimiated by complex types than by a linguistic tree with
213 deeply embedded branches: complex structure created by repeated
214 application of simple rules.
215
216 [This would be a good time to try to build your own term for the types
217 just given.  Doing so (or attempting to do so) will make the next
218 paragraph much easier to follow.]
219
220 As usual, we need to unpack the `u` box.  Examine the type of `u`.
221 This time, `u` will only deliver up its contents if we give `u` an
222 argument that is a function expecting an `'a` and a `'b`. `u` will 
223 fold that function over its type `'a` members, and that's how we'll get the `'a`s we need. Thus:
224
225         ... u (fun (a : 'a) (b : 'b) -> ... f a ... ) ...
226
227 In order for `u` to have the kind of argument it needs, the `... (f a) ...` has to evaluate to a result of type `'b`. The easiest way to do this is to collapse (or "unify") the types `'b` and `'d`, with the result that `f a` will have type `('c -> 'b -> 'b) -> 'b -> 'b`. Let's postulate an argument `k` of type `('c -> 'b -> 'b)` and supply it to `(f a)`:
228
229         ... u (fun (a : 'a) (b : 'b) -> ... f a k ... ) ...
230
231 Now we have an argument `b` of type `'b`, so we can supply that to `(f a) k`, getting a result of type `'b`, as we need:
232
233         ... u (fun (a : 'a) (b : 'b) -> f a k b) ...
234
235 Now, we've used a `k` that we pulled out of nowhere, so we need to abstract over it:
236
237         fun (k : 'c -> 'b -> 'b) -> u (fun (a : 'a) (b : 'b) -> f a k b)
238
239 This whole expression has type `('c -> 'b -> 'b) -> 'b -> 'b`, which is exactly the type of a `('c, 'b) list'`. So we can hypothesize that we our bind is:
240
241     l'_bind (u : ('a -> 'b -> 'b) -> 'b -> 'b)
242             (f : 'a -> ('c -> 'b -> 'b) -> 'b -> 'b)
243             : ('c -> 'b -> 'b) -> 'b -> 'b = 
244       fun k -> u (fun a b -> f a k b)
245
246 That is a function of the right type for our bind, but to check whether it works, we have to verify it (with the unit we chose) against the monad laws, and reason whether it will have the right behavior.
247
248 Here's a way to persuade yourself that it will have the right behavior. First, it will be handy to eta-expand our `fun k -> u (fun a b -> f a k b)` to:
249
250       fun k z -> u (fun a b -> f a k b) z
251
252 Now let's think about what this does. It's a wrapper around `u`. In order to behave as the list which is the result of mapping `f` over each element of `u`, and then joining (`concat`ing) the results, this wrapper would have to accept arguments `k` and `z` and fold them in just the same way that the list which is the result of mapping `f` and then joining the results would fold them. Will it?
253
254 Suppose we have a list' whose contents are `[1; 2; 4; 8]`---that is, our list' will be `fun f z -> f 1 (f 2 (f 4 (f 8 z)))`. We call that list' `u`. Suppose we also have a function `f` that for each `int` we give it, gives back a list of the divisors of that `int` that are greater than 1. Intuitively, then, binding `u` to `f` should give us:
255
256         concat (map f u) =
257         concat [[]; [2]; [2; 4]; [2; 4; 8]] =
258         [2; 2; 4; 2; 4; 8]
259
260 Or rather, it should give us a list' version of that, which takes a function `k` and value `z` as arguments, and returns the right fold of `k` and `z` over those elements. What does our formula
261
262       fun k z -> u (fun a b -> f a k b) z
263         
264 do? Well, for each element `a` in `u`, it applies `f` to that `a`, getting one of the lists:
265
266         []
267         [2]
268         [2; 4]
269         [2; 4; 8]
270
271 (or rather, their list' versions). Then it takes the accumulated result `b` of previous steps in the fold, and it folds `k` and `b` over the list generated by `f a`. The result of doing so is passed on to the next step as the accumulated result so far.
272
273 So if, for example, we let `k` be `+` and `z` be `0`, then the computation would proceed:
274
275         0 ==>
276         right-fold + and 0 over [2; 4; 8] = 2+4+8+0 ==>
277         right-fold + and 2+4+8+0 over [2; 4] = 2+4+2+4+8+0 ==>
278         right-fold + and 2+4+2+4+8+0 over [2] = 2+2+4+2+4+8+0 ==>
279         right-fold + and 2+2+4+2+4+8+0 over [] = 2+2+4+2+4+8+0
280
281 which indeed is the result of right-folding + and 0 over `[2; 2; 4; 2; 4; 8]`. If you trace through how this works, you should be able to persuade yourself that our formula:
282
283       fun k z -> u (fun a b -> f a k b) z
284
285 will deliver just the same folds, for arbitrary choices of `k` and `z` (with the right types), and arbitrary list's `u` and appropriately-typed `f`s, as
286
287         fun k z -> List.fold_right k (concat (map f u)) z
288
289 would.
290
291 For future reference, we might make two eta-reductions to our formula, so that we have instead:
292
293       let l'_bind = fun k -> u (fun a -> f a k);;
294
295 Let's make some more tests:
296
297
298     l_bind [1;2] (fun i -> [i, i+1]) ~~> [1; 2; 2; 3]
299
300     l'_bind (fun f z -> f 1 (f 2 z)) 
301             (fun i -> fun f z -> f i (f (i+1) z)) ~~> <fun>
302
303 Sigh.  OCaml won't show us our own list.  So we have to choose an `f`
304 and a `z` that will turn our hand-crafted lists into standard OCaml
305 lists, so that they will print out.
306
307         # let cons h t = h :: t;;  (* OCaml is stupid about :: *)
308         # l'_bind (fun f z -> f 1 (f 2 z)) 
309                           (fun i -> fun f z -> f i (f (i+1) z)) cons [];;
310         - : int list = [1; 2; 2; 3]
311
312 Ta da!
313
314
315 Montague's PTQ treatment of DPs as generalized quantifiers
316 ----------------------------------------------------------
317
318 We've hinted that Montague's treatment of DPs as generalized
319 quantifiers embodies the spirit of continuations (see de Groote 2001,
320 Barker 2002 for lengthy discussion).  Let's see why.  
321
322 First, we'll need a type constructor.  As you probably know, 
323 Montague replaced individual-denoting determiner phrases (with type `e`)
324 with generalized quantifiers (with [extensional] type `(e -> t) -> t`.
325 In particular, the denotation of a proper name like *John*, which
326 might originally denote a object `j` of type `e`, came to denote a
327 generalized quantifier `fun pred -> pred j` of type `(e -> t) -> t`.
328 Let's write a general function that will map individuals into their
329 corresponding generalized quantifier:
330
331    gqize (a : e) = fun (p : e -> t) -> p a
332
333 This function is what Partee 1987 calls LIFT, and it would be
334 reasonable to use it here, but we will avoid that name, given that we
335 use that word to refer to other functions.
336
337 This function wraps up an individual in a box.  That is to say,
338 we are in the presence of a monad.  The type constructor, the unit and
339 the bind follow naturally.  We've done this enough times that we won't
340 belabor the construction of the bind function, the derivation is
341 highly similar to the List monad just given:
342
343         type 'a continuation = ('a -> 'b) -> 'b
344         c_unit (a : 'a) = fun (p : 'a -> 'b) -> p a
345         c_bind (u : ('a -> 'b) -> 'b) (f : 'a -> ('c -> 'd) -> 'd) : ('c -> 'd) -> 'd =
346           fun (k : 'a -> 'b) -> u (fun (a : 'a) -> f a k)
347
348 Note that `c_bind` is exactly the `gqize` function that Montague used
349 to lift individuals into the continuation monad.
350
351 That last bit in `c_bind` looks familiar---we just saw something like
352 it in the List monad.  How similar is it to the List monad?  Let's
353 examine the type constructor and the terms from the list monad derived
354 above:
355
356     type ('a, 'b) list' = ('a -> 'b -> 'b) -> 'b -> 'b
357     l'_unit a = fun f -> f a                 
358     l'_bind u f = fun k -> u (fun a -> f a k)
359
360 (We performed a sneaky but valid eta reduction in the unit term.)
361
362 The unit and the bind for the Montague continuation monad and the
363 homemade List monad are the same terms!  In other words, the behavior
364 of the List monad and the behavior of the continuations monad are
365 parallel in a deep sense.
366
367 Have we really discovered that lists are secretly continuations?  Or
368 have we merely found a way of simulating lists using list
369 continuations?  Well, strictly speaking, what we have done is shown
370 that one particular implementation of lists---the right fold
371 implementation---gives rise to a continuation monad fairly naturally,
372 and that this monad can reproduce the behavior of the standard list
373 monad.  But what about other list implementations?  Do they give rise
374 to monads that can be understood in terms of continuations?
375
376 Manipulating trees with monads
377 ------------------------------
378
379 This thread develops an idea based on a detailed suggestion of Ken
380 Shan's.  We'll build a series of functions that operate on trees,
381 doing various things, including replacing leaves, counting nodes, and
382 converting a tree to a list of leaves.  The end result will be an
383 application for continuations.
384
385 From an engineering standpoint, we'll build a tree transformer that
386 deals in monads.  We can modify the behavior of the system by swapping
387 one monad for another.  (We've already seen how adding a monad can add
388 a layer of funtionality without disturbing the underlying system, for
389 instance, in the way that the reader monad allowed us to add a layer
390 of intensionality to an extensional grammar, but we have not yet seen
391 the utility of replacing one monad with other.)
392
393 First, we'll be needing a lot of trees during the remainder of the
394 course.  Here's a type constructor for binary trees:
395
396     type 'a tree = Leaf of 'a | Node of ('a tree * 'a tree)
397
398 These are trees in which the internal nodes do not have labels.  [How
399 would you adjust the type constructor to allow for labels on the
400 internal nodes?]
401
402 We'll be using trees where the nodes are integers, e.g.,
403
404
405 <pre>
406 let t1 = Node ((Node ((Leaf 2), (Leaf 3))),
407                (Node ((Leaf 5),(Node ((Leaf 7),
408                                       (Leaf 11))))))
409
410     .
411  ___|___
412  |     |
413  .     .
414 _|__  _|__
415 |  |  |  |
416 2  3  5  .
417         _|__
418         |  |
419         7  11
420 </pre>
421
422 Our first task will be to replace each leaf with its double:
423
424 <pre>
425 let rec treemap (newleaf:'a -> 'b) (t:'a tree):('b tree) =
426   match t with Leaf x -> Leaf (newleaf x)
427              | Node (l, r) -> Node ((treemap newleaf l),
428                                     (treemap newleaf r));;
429 </pre>
430 `treemap` takes a function that transforms old leaves into new leaves, 
431 and maps that function over all the leaves in the tree, leaving the
432 structure of the tree unchanged.  For instance:
433
434 <pre>
435 let double i = i + i;;
436 treemap double t1;;
437 - : int tree =
438 Node (Node (Leaf 4, Leaf 6), Node (Leaf 10, Node (Leaf 14, Leaf 22)))
439
440     .
441  ___|____
442  |      |
443  .      .
444 _|__  __|__
445 |  |  |   |
446 4  6  10  .
447         __|___
448         |    |
449         14   22
450 </pre>
451
452 We could have built the doubling operation right into the `treemap`
453 code.  However, because what to do to each leaf is a parameter, we can
454 decide to do something else to the leaves without needing to rewrite
455 `treemap`.  For instance, we can easily square each leaf instead by
456 supplying the appropriate `int -> int` operation in place of `double`:
457
458 <pre>
459 let square x = x * x;;
460 treemap square t1;;
461 - : int tree =ppp
462 Node (Node (Leaf 4, Leaf 9), Node (Leaf 25, Node (Leaf 49, Leaf 121)))
463 </pre>
464
465 Note that what `treemap` does is take some global, contextual
466 information---what to do to each leaf---and supplies that information
467 to each subpart of the computation.  In other words, `treemap` has the
468 behavior of a reader monad.  Let's make that explicit.  
469
470 In general, we're on a journey of making our treemap function more and
471 more flexible.  So the next step---combining the tree transducer with
472 a reader monad---is to have the treemap function return a (monadized)
473 tree that is ready to accept any `int->int` function and produce the
474 updated tree.
475
476 \tree (. (. (f2) (f3))(. (f5) (.(f7)(f11))))
477 <pre>
478 \f    .
479   ____|____
480   |       |
481   .       .
482 __|__   __|__
483 |   |   |   |
484 f2  f3  f5  .
485           __|___
486           |    |
487           f7  f11
488 </pre>
489
490 That is, we want to transform the ordinary tree `t1` (of type `int
491 tree`) into a reader object of type `(int->int)-> int tree`: something 
492 that, when you apply it to an `int->int` function returns an `int
493 tree` in which each leaf `x` has been replaced with `(f x)`.
494
495 With previous readers, we always knew which kind of environment to
496 expect: either an assignment function (the original calculator
497 simulation), a world (the intensionality monad), an integer (the
498 Jacobson-inspired link monad), etc.  In this situation, it will be
499 enough for now to expect that our reader will expect a function of
500 type `int->int`.
501
502 <pre>
503 type 'a reader = (int->int) -> 'a;;  (* mnemonic: e for environment *)
504 let reader_unit (x:'a): 'a reader = fun _ -> x;;
505 let reader_bind (u: 'a reader) (f:'a -> 'c reader):'c reader = fun e -> f (u e) e;;
506 </pre>
507
508 It's easy to figure out how to turn an `int` into an `int reader`:
509
510 <pre>
511 let int2int_reader (x:'a): 'b reader = fun (op:'a -> 'b) -> op x;;
512 int2int_reader 2 (fun i -> i + i);;
513 - : int = 4
514 </pre>
515
516 But what do we do when the integers are scattered over the leaves of a
517 tree?  A binary tree is not the kind of thing that we can apply a
518 function of type `int->int` to.
519
520 <pre>
521 let rec treemonadizer (f:'a -> 'b reader) (t:'a tree):('b tree) reader =
522   match t with Leaf x -> reader_bind (f x) (fun x' -> reader_unit (Leaf x'))
523              | Node (l, r) -> reader_bind (treemonadizer f l) (fun x ->
524                                 reader_bind (treemonadizer f r) (fun y ->
525                                   reader_unit (Node (x, y))));;
526 </pre>
527
528 This function says: give me a function `f` that knows how to turn
529 something of type `'a` into an `'b reader`, and I'll show you how to 
530 turn an `'a tree` into an `'a tree reader`.  In more fanciful terms, 
531 the `treemonadizer` function builds plumbing that connects all of the
532 leaves of a tree into one connected monadic network; it threads the
533 monad through the leaves.
534
535 <pre>
536 # treemonadizer int2int_reader t1 (fun i -> i + i);;
537 - : int tree =
538 Node (Node (Leaf 4, Leaf 6), Node (Leaf 10, Node (Leaf 14, Leaf 22)))
539 </pre>
540
541 Here, our environment is the doubling function (`fun i -> i + i`).  If
542 we apply the very same `int tree reader` (namely, `treemonadizer
543 int2int_reader t1`) to a different `int->int` function---say, the
544 squaring function, `fun i -> i * i`---we get an entirely different
545 result:
546
547 <pre>
548 # treemonadizer int2int_reader t1 (fun i -> i * i);;
549 - : int tree =
550 Node (Node (Leaf 4, Leaf 9), Node (Leaf 25, Node (Leaf 49, Leaf 121)))
551 </pre>
552
553 Now that we have a tree transducer that accepts a monad as a
554 parameter, we can see what it would take to swap in a different monad.
555 For instance, we can use a state monad to count the number of nodes in
556 the tree.
557
558 <pre>
559 type 'a state = int -> 'a * int;;
560 let state_unit x i = (x, i+.5);;
561 let state_bind u f i = let (a, i') = u i in f a (i'+.5);;
562 </pre>
563
564 Gratifyingly, we can use the `treemonadizer` function without any
565 modification whatsoever, except for replacing the (parametric) type
566 `reader` with `state`:
567
568 <pre>
569 let rec treemonadizer (f:'a -> 'b state) (t:'a tree):('b tree) state =
570   match t with Leaf x -> state_bind (f x) (fun x' -> state_unit (Leaf x'))
571              | Node (l, r) -> state_bind (treemonadizer f l) (fun x ->
572                                 state_bind (treemonadizer f r) (fun y ->
573                                   state_unit (Node (x, y))));;
574 </pre>
575
576 Then we can count the number of nodes in the tree:
577
578 <pre>
579 # treemonadizer state_unit t1 0;;
580 - : int tree * int =
581 (Node (Node (Leaf 2, Leaf 3), Node (Leaf 5, Node (Leaf 7, Leaf 11))), 13)
582
583     .
584  ___|___
585  |     |
586  .     .
587 _|__  _|__
588 |  |  |  |
589 2  3  5  .
590         _|__
591         |  |
592         7  11
593 </pre>
594
595 Notice that we've counted each internal node twice---it's a good
596 exercise to adjust the code to count each node once.
597
598 One more revealing example before getting down to business: replacing
599 `state` everywhere in `treemonadizer` with `list` gives us
600
601 <pre>
602 # treemonadizer (fun x -> [ [x; square x] ]) t1;;
603 - : int list tree list =
604 [Node
605   (Node (Leaf [2; 4], Leaf [3; 9]),
606    Node (Leaf [5; 25], Node (Leaf [7; 49], Leaf [11; 121])))]
607 </pre>
608
609 Unlike the previous cases, instead of turning a tree into a function
610 from some input to a result, this transformer replaces each `int` with
611 a list of `int`'s.
612
613 Now for the main point.  What if we wanted to convert a tree to a list
614 of leaves?  
615
616 <pre>
617 type ('a, 'r) continuation = ('a -> 'r) -> 'r;;
618 let continuation_unit x c = c x;;
619 let continuation_bind u f c = u (fun a -> f a c);;
620
621 let rec treemonadizer (f:'a -> ('b, 'r) continuation) (t:'a tree):(('b tree), 'r) continuation =
622   match t with Leaf x -> continuation_bind (f x) (fun x' -> continuation_unit (Leaf x'))
623              | Node (l, r) -> continuation_bind (treemonadizer f l) (fun x ->
624                                 continuation_bind (treemonadizer f r) (fun y ->
625                                   continuation_unit (Node (x, y))));;
626 </pre>
627
628 We use the continuation monad described above, and insert the
629 `continuation` type in the appropriate place in the `treemonadizer` code.
630 We then compute:
631
632 <pre>
633 # treemonadizer (fun a c -> a :: (c a)) t1 (fun t -> []);;
634 - : int list = [2; 3; 5; 7; 11]
635 </pre>
636
637 We have found a way of collapsing a tree into a list of its leaves.
638
639 The continuation monad is amazingly flexible; we can use it to
640 simulate some of the computations performed above.  To see how, first
641 note that an interestingly uninteresting thing happens if we use the
642 continuation unit as our first argument to `treemonadizer`, and then
643 apply the result to the identity function:
644
645 <pre>
646 # treemonadizer continuation_unit t1 (fun x -> x);;
647 - : int tree =
648 Node (Node (Leaf 2, Leaf 3), Node (Leaf 5, Node (Leaf 7, Leaf 11)))
649 </pre>
650
651 That is, nothing happens.  But we can begin to substitute more
652 interesting functions for the first argument of `treemonadizer`:
653
654 <pre>
655 (* Simulating the tree reader: distributing a operation over the leaves *)
656 # treemonadizer (fun a c -> c (square a)) t1 (fun x -> x);;
657 - : int tree =
658 Node (Node (Leaf 4, Leaf 9), Node (Leaf 25, Node (Leaf 49, Leaf 121)))
659
660 (* Simulating the int list tree list *)
661 # treemonadizer (fun a c -> c [a; square a]) t1 (fun x -> x);;
662 - : int list tree =
663 Node
664  (Node (Leaf [2; 4], Leaf [3; 9]),
665   Node (Leaf [5; 25], Node (Leaf [7; 49], Leaf [11; 121])))
666
667 (* Counting leaves *)
668 # treemonadizer (fun a c -> 1 + c a) t1 (fun x -> 0);;
669 - : int = 5
670 </pre>
671
672 We could simulate the tree state example too, but it would require
673 generalizing the type of the continuation monad to 
674
675     type ('a -> 'b -> 'c) continuation = ('a -> 'b) -> 'c;;
676
677 The binary tree monad
678 ---------------------
679
680 Of course, by now you may have realized that we have discovered a new
681 monad, the binary tree monad:
682
683 <pre>
684 type 'a tree = Leaf of 'a | Node of ('a tree) * ('a tree);;
685 let tree_unit (x:'a) = Leaf x;;
686 let rec tree_bind (u:'a tree) (f:'a -> 'b tree):'b tree = 
687   match u with Leaf x -> f x 
688              | Node (l, r) -> Node ((tree_bind l f), (tree_bind r f));;
689 </pre>
690
691 For once, let's check the Monad laws.  The left identity law is easy:
692
693     Left identity: bind (unit a) f = bind (Leaf a) f = fa
694
695 To check the other two laws, we need to make the following
696 observation: it is easy to prove based on `tree_bind` by a simple
697 induction on the structure of the first argument that the tree
698 resulting from `bind u f` is a tree with the same strucure as `u`,
699 except that each leaf `a` has been replaced with `fa`:
700
701 \tree (. (fa1) (. (. (. (fa2)(fa3)) (fa4)) (fa5)))
702 <pre>
703                 .                         .       
704               __|__                     __|__   
705               |   |                     |   |   
706               a1  .                    fa1  .   
707                  _|__                     __|__ 
708                  |  |                     |   | 
709                  .  a5                    .  fa5
710    bind         _|__       f   =        __|__   
711                 |  |                    |   |   
712                 .  a4                   .  fa4  
713               __|__                   __|___   
714               |   |                   |    |   
715               a2  a3                 fa2  fa3         
716 </pre>   
717
718 Given this equivalence, the right identity law
719
720     Right identity: bind u unit = u
721
722 falls out once we realize that
723
724     bind (Leaf a) unit = unit a = Leaf a
725
726 As for the associative law,
727
728     Associativity: bind (bind u f) g = bind u (\a. bind (fa) g)
729
730 we'll give an example that will show how an inductive proof would
731 proceed.  Let `f a = Node (Leaf a, Leaf a)`.  Then
732
733 \tree (. (. (. (. (a1)(a2)))))
734 \tree (. (. (. (. (a1) (a1)) (. (a1) (a1)))  ))
735 <pre>
736                                            .
737                                        ____|____
738           .               .            |       |
739 bind    __|__   f  =    __|_    =      .       .
740         |   |           |   |        __|__   __|__
741         a1  a2         fa1 fa2       |   |   |   |
742                                      a1  a1  a1  a1  
743 </pre>
744
745 Now when we bind this tree to `g`, we get
746
747 <pre>
748            .
749        ____|____
750        |       |
751        .       .
752      __|__   __|__
753      |   |   |   |
754     ga1 ga1 ga1 ga1  
755 </pre>
756
757 At this point, it should be easy to convince yourself that
758 using the recipe on the right hand side of the associative law will
759 built the exact same final tree.
760
761 So binary trees are a monad.
762
763 Haskell combines this monad with the Option monad to provide a monad
764 called a
765 [SearchTree](http://hackage.haskell.org/packages/archive/tree-monad/0.2.1/doc/html/src/Control-Monad-SearchTree.html#SearchTree)
766 that is intended to 
767 represent non-deterministic computations as a tree.
768
769
770 Refunctionalizing zippers: from lists to continuations
771 ------------------------------------------------------
772
773 Let's work with lists of chars for a change.  To maximize readability, we'll
774 indulge in an abbreviatory convention that "abc" abbreviates the
775 list `['a'; 'b'; 'c']`.
776
777 Task 1: replace each occurrence of 'S' with a copy of the string up to
778 that point.  
779
780 Expected behavior:
781
782 <pre>
783 t1 "abSe" ~~> "ababe"
784 </pre>   
785
786
787 In linguistic terms, this is a kind of anaphora
788 resolution, where `'S'` is functioning like an anaphoric element, and
789 the preceding string portion is the antecedent.
790
791 This deceptively simple task gives rise to some mind-bending complexity.
792 Note that it matters which 'S' you target first (the position of the *
793 indicates the targeted 'S'):
794
795 <pre>
796     t1 "aSbS" 
797          *
798 ~~> t1 "aabS" 
799            *
800 ~~> "aabaab"
801 </pre>
802
803 versus
804
805 <pre>
806     t1 "aSbS"
807            *
808 ~~> t1 "aSbaSb" 
809          *
810 ~~> t1 "aabaSb"
811             *
812 ~~> "aabaaabab"
813 </pre>   
814
815 versus
816
817 <pre>
818     t1 "aSbS"
819            *
820 ~~> t1 "aSbaSb"
821             *
822 ~~> t1 "aSbaaSbab"
823              *
824 ~~> t1 "aSbaaaSbaabab"
825               *
826 ~~> ...
827 </pre>
828
829 Aparently, this task, as simple as it is, is a form of computation,
830 and the order in which the `'S'`s get evaluated can lead to divergent
831 behavior.
832
833 For now, as usual, we'll agree to always evaluate the leftmost `'S'`.
834
835 This is a task well-suited to using a zipper.  
836
837 <pre>
838 type 'a list_zipper = ('a list) * ('a list);;
839
840 let rec t1 (z:char list_zipper) = 
841     match z with (sofar, []) -> List.rev(sofar) (* Done! *)
842                | (sofar, 'S'::rest) -> t1 ((List.append sofar sofar), rest) 
843                | (sofar, fst::rest) -> t1 (fst::sofar, rest);; (* Move zipper *)
844
845 # t1 ([], ['a'; 'b'; 'S'; 'e']);;
846 - : char list = ['a'; 'b'; 'a'; 'b'; 'e']
847
848 # t1 ([], ['a'; 'S'; 'b'; 'S']);;
849 - : char list = ['a'; 'a'; 'b'; 'a'; 'a'; 'b']
850 </pre>
851
852 Note that this implementation enforces the evaluate-leftmost rule.
853 Task 1 completed.
854
855 One way to see exactly what is going on is to watch the zipper in
856 action by tracing the execution of `t1`.  By using the `#trace`
857 directive in the Ocaml interpreter, the system will print out the
858 arguments to `t1` each time it is (recurcively) called:
859
860 <pre>
861 # #trace t1;;
862 t1 is now traced.
863 # t1 ([], ['a'; 'b'; 'S'; 'e']);;
864 t1 <-- ([], ['a'; 'b'; 'S'; 'e'])
865 t1 <-- (['a'], ['b'; 'S'; 'e'])
866 t1 <-- (['b'; 'a'], ['S'; 'e'])
867 t1 <-- (['b'; 'a'; 'b'; 'a'], ['e'])
868 t1 <-- (['e'; 'b'; 'a'; 'b'; 'a'], [])
869 t1 --> ['a'; 'b'; 'a'; 'b'; 'e']
870 t1 --> ['a'; 'b'; 'a'; 'b'; 'e']
871 t1 --> ['a'; 'b'; 'a'; 'b'; 'e']
872 t1 --> ['a'; 'b'; 'a'; 'b'; 'e']
873 t1 --> ['a'; 'b'; 'a'; 'b'; 'e']
874 - : char list = ['a'; 'b'; 'a'; 'b'; 'e']
875 </pre>
876
877 The nice thing about computations involving lists is that it's so easy
878 to visualize them as a data structure.  Eventually, we want to get to
879 a place where we can talk about more abstract computations.  In order
880 to get there, we'll first do the exact same thing we just did with
881 concrete zipper using procedures.  
882
883 Think of a list as a procedural recipe: `['a'; 'b'; 'c']` means (1)
884 start with the empty list `[]`; (2) make a new list whose first
885 element is 'c' and whose tail is the list construted in the previous
886 step; (3) make a new list whose first element is 'b' and whose tail is
887 the list constructed in the previous step; and (4) make a new list
888 whose first element is 'a' and whose tail is the list constructed in
889 the previous step.
890
891 What is the type of each of these steps?  Well, it will be a function
892 from the result of the previous step (a list) to a new list: it will
893 be a function of type `char list -> char list`.  We'll call each step
894 a **continuation** of the recipe.  So in this context, a continuation
895 is a function of type `char list -> char list`.
896
897 This means that we can now represent the sofar part of our zipper--the 
898 part we've already unzipped--as a continuation, a function describing
899 how to finish building the list:
900
901 <pre>
902 let rec t1c (l: char list) (c: (char list) -> (char list)) =
903   match l with [] -> c []
904              | 'S'::rest -> t1c rest (fun x -> c (c x))
905              | a::rest -> t1c rest (fun x -> List.append (c x) [a]);;
906
907 # t1c ['a'; 'b'; 'S'] (fun x -> x);;
908 - : char list = ['a'; 'b'; 'a'; 'b']
909
910 # t1c ['a'; 'S'; 'b'; 'S'] (fun x -> x);;
911 - : char list = ['a'; 'a'; 'b'; 'a'; 'a'; 'b']
912 </pre>
913
914 Note that we don't need to do any reversing.
915