X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=coroutines_and_aborts.mdwn;h=8cca9e3f5aa209c683f00d8f6c20c3dc7a202691;hp=a0b35c8891c7747f8b51e6b3f3aeba4851a8c5f9;hb=4d77188b080ae01ca1a58559481c5a2cfff7cbe2;hpb=69b3f8c86471cc4473813e90d2db9b5540a111fe diff --git a/coroutines_and_aborts.mdwn b/coroutines_and_aborts.mdwn index a0b35c88..8cca9e3f 100644 --- a/coroutines_and_aborts.mdwn +++ b/coroutines_and_aborts.mdwn @@ -338,7 +338,27 @@ So what I should have said is that when you evaluate the expression: *and that exception is never caught*, then the effect is for the program to immediately stop. -Of course, it's possible to handle errors in other ways too. There's no reason why the implementation of `List.nth` *had* to do things this way. They might instead have returned `Some a` when the list had an nth member `a`, and `None` when it does not. But it's pedagogically useful for us to think about this pattern now. +Trivia: what's the type of the `raise (Failure "two")` in: + + if x = 1 then 10 + else raise (Failure "two") + +What's its type in: + + if x = 1 then "ten" + else raise (Failure "two") + +So now what do you expect the type of this to be: + + fun x -> raise (Failure "two") + +How about this: + + (fun x -> raise (Failure "two") : 'a -> 'a) + +Remind you of anything we discussed earlier? /Trivia. + +Of course, it's possible to handle errors in other ways too. There's no reason why the implementation of `List.nth` *had* to raise an exception. They might instead have returned `Some a` when the list had an nth member `a`, and `None` when it does not. But it's pedagogically useful for us to think about the exception-raising pattern now. When an exception is raised, it percolates up through the code that called it, until it finds a surrounding `try ... with ...` that matches it. That might not be the first `try ... with ...` that it encounters. For example: @@ -383,7 +403,7 @@ Many programming languages have this simplified exceution pattern, either instea if (x == 1) then value = 10 else - return 20 -- return early + return 20 -- abort early end return value + 100 -- in Lua, a function's normal value -- must always also be explicitly returned @@ -541,7 +561,7 @@ These snapshots are called **continuations** because they represent how the comp You can think of them as functions that represent "how the rest of the computation proposes to continue." Except that, once we're able to get our hands on those functions, we can do exotic and unwholesome things with them. Like use them to suspend and resume a thread. Or to abort from deep inside a sub-computation: one function might pass the command to abort *it* to a subfunction, so that the subfunction has the power to jump directly to the outside caller. Or a function might *return* its continuation function to the outside caller, giving *the outside caller* the ability to "abort" the function (the function that has already returned its value---so what should happen then?) Or we may call the same continuation function *multiple times* (what should happen then?). All of these weird and wonderful possibilities await us. -The key idea behind working with continuations is that we're *inverting control*. In the fragment above, the code `(if x = 1 then ... else snapshot 20) + 100` which is written so as to supply a value to the outside context that we snapshotted itself *makes non-trivial use of* that snapshot. So it has to be able to refer to that snapshot; the snapshot has to somehow be available to our inner code as an *argument* or bound variable. That is: the cde that is *written* like it's supplying an argument to the outside context is instead *getting that context as its own argument*. He who is written as value-supplying slave is instead become the outer context's master. +The key idea behind working with continuations is that we're *inverting control*. In the fragment above, the code `(if x = 1 then ... else snapshot 20) + 100`---which is written so as to supply a value to the outside context that we snapshotted---itself *makes non-trivial use of* that snapshot. So it has to be able to refer to that snapshot; the snapshot has to somehow be available to our inside-the-box code as an *argument* or bound variable. That is: the code that is *written* like it's supplying an argument to the outside context is instead *getting that context as its own argument*. He who is written as value-supplying slave is instead become the outer context's master. In fact you've already seen this several times this semester---recall how in our implementation of pairs in the untyped lambda-calculus, the handler who wanted to use the pair's components had *in the first place to be supplied to the pair as an argument*. So the exotica from the end of the seminar was already on the scene in some of our earliest steps. Recall also what we did with v2 and v5 lists. Version 5 lists were the ones that let us abort a fold early: go back and re-read the material on "Aborting a Search Through a List" in [[Week4]]. @@ -563,7 +583,8 @@ When working with continuations, it's easiest in the first place to write them o let foo x = try begin (if x = 1 then 10 - else abort 20) + 100 + else abort 20 + ) + 100 end in (foo 2) + 1;; @@ -573,11 +594,11 @@ into this: in let snapshot = fun box -> let foo_result = box in (foo_result) + 1000 - in let finish_value = fun start -> - let value = start + 100 + in let continue_normally = fun from_value -> + let value = from_value + 100 in snapshot value in - if x = 1 then finish_value 10 + if x = 1 then continue_normally 10 else snapshot 20;; Code written in the latter form is said to be written in **explicit continuation-passing style** or CPS. Later we'll talk about algorithms that mechanically convert an entire program into CPS.