week11 tweaks
[lambda.git] / week11.mdwn
index 9fc1d42..a76aadb 100644 (file)
@@ -645,8 +645,40 @@ Okay, so that's our second execution pattern.
 
 ##What do these have in common?##
 
+In both of these patterns, we need to have some way to take a snapshot of where we are in the evaluation of a complex piece of code, so that we might later resume execution at that point. In the coroutine example, the two threads need to have a snapshot of where they were in the enumeration of their tree's leaves. In the abort example, we need to have a snapshot of where to pick up again if some embedded piece of code aborts. Sometimes we might distill that snapshot into a datastructure like a zipper. But we might not always know how to do so; and learning how to think about these snapshots without the help of zippers will help us see patterns and similarities we might otherwise miss.
 
+A more general way to think about these snapshots is to think of the code we're taking a snapshot of as a *function.* For example, in this code:
 
+       let foo x =
+           try
+               (if x = 1 then 10
+               else abort 20) + 1
+           end
+       in (foo 2) + 1;;
+
+we can imagine a box:
+
+       let foo x =
+       +---------------------------+
+       |   try                     |
+       |       (if x = 1 then 10   |
+       |       else abort 20) + 1  |
+       |   end                     |
+       +---------------------------+
+       in (foo 2) + 1;;
+
+and as we're about to enter the box, we want to take a snapshot of the code *outside* the box. If we decide to abort, we'd be aborting to that snapshotted code.
+
+<!--
+# #require "delimcc";;
+# open Delimcc;;
+# let reset body = let p = new_prompt () in push_prompt p (body p);;
+val reset : ('a Delimcc.prompt -> unit -> 'a) -> 'a = <fun>
+# let foo x = reset(fun p () -> (shift p (fun k -> if x = 1 then k 10 else 20)) + 1) in (foo 1) + 100;;
+- : int = 111
+# let foo x = reset(fun p () -> (shift p (fun k -> if x = 1 then k 10 else 20)) + 1) in (foo 2) + 100;;
+- : int = 120
+-->