layout: post
title: “Understanding continuations”
description: “How ‘let’ works behind the scenes”
nav: thinking-functionally
seriesId: “Computation Expressions”

seriesOrder: 2

In the previous post we saw how some complex code could be condensed using computation expressions.

Here’s the code before using a computation expression:

  1. let log p = printfn "expression is %A" p
  2. let loggedWorkflow =
  3. let x = 42
  4. log x
  5. let y = 43
  6. log y
  7. let z = x + y
  8. log z
  9. //return
  10. z

And here’s the same code after using a computation expression:

  1. let loggedWorkflow =
  2. logger
  3. {
  4. let! x = 42
  5. let! y = 43
  6. let! z = x + y
  7. return z
  8. }

The use of let! rather than a normal let is important. Can we emulate this ourselves so we can understand what is going on? Yes, but we need to understand continuations first.

Continuations

In imperative programming, we have the concept of “returning” from a function. When you call a function, you “go in”, and then you “come out”, just like pushing and popping a stack.

Here is some typical C# code which works like this. Notice the use of the return keyword.

  1. public int Divide(int top, int bottom)
  2. {
  3. if (bottom==0)
  4. {
  5. throw new InvalidOperationException("div by 0");
  6. }
  7. else
  8. {
  9. return top/bottom;
  10. }
  11. }
  12. public bool IsEven(int aNumber)
  13. {
  14. var isEven = (aNumber % 2 == 0);
  15. return isEven;
  16. }

You’ve seen this a million times, but there is a subtle point about this approach that you might not have considered: the called function always decides what to do.

For example, the implemention of Divide has decided that it is going to throw an exception. But what if I don’t want an exception? Maybe I want a nullable<int>, or maybe I am going to display it on a screen as “#DIV/0”. Why throw an exception that I am immediately going to have to catch? In other words, why not let the caller decide what should happen, rather the callee.

Similarly in the IsEven example, what am I going to do with the boolean return value? Branch on it? Or maybe print it in a report? I don’t know, but again, rather than returning a boolean that the caller has to deal with, why not let the caller tell the callee what to do next?

So this is what continuations are. A continuation is simply a function that you pass into another function to tell it what to do next.

Here’s the same C# code rewritten to allow the caller to pass in functions which the callee uses to handle each case. If it helps, you can think of this as somewhat analogous to a visitor pattern. Or maybe not.

  1. public T Divide<T>(int top, int bottom, Func<T> ifZero, Func<int,T> ifSuccess)
  2. {
  3. if (bottom==0)
  4. {
  5. return ifZero();
  6. }
  7. else
  8. {
  9. return ifSuccess( top/bottom );
  10. }
  11. }
  12. public T IsEven<T>(int aNumber, Func<int,T> ifOdd, Func<int,T> ifEven)
  13. {
  14. if (aNumber % 2 == 0)
  15. {
  16. return ifEven(aNumber);
  17. }
  18. else
  19. { return ifOdd(aNumber);
  20. }
  21. }

Note that the C# functions have been changed to return a generic T now, and both continuations are a Func that returns a T.

Well, passing in lots of Func parameters always looks pretty ugly in C#, so it is not done very often. But passing functions is easy in F#, so let’s see how this code ports over.

Here’s the “before” code:

  1. let divide top bottom =
  2. if (bottom=0)
  3. then invalidOp "div by 0"
  4. else (top/bottom)
  5. let isEven aNumber =
  6. aNumber % 2 = 0

and here’s the “after” code:

  1. let divide ifZero ifSuccess top bottom =
  2. if (bottom=0)
  3. then ifZero()
  4. else ifSuccess (top/bottom)
  5. let isEven ifOdd ifEven aNumber =
  6. if (aNumber % 2 = 0)
  7. then aNumber |> ifEven
  8. else aNumber |> ifOdd

A few things to note. First, you can see that I have put the extra functions (ifZero, etc) first in the parameter list, rather than last, as in the C# example. Why? Because I am probably going to want to use partial application.

And also, in the isEven example, I wrote aNumber |> ifEven and aNumber |> ifOdd. This makes it clear that we are piping the current value into the continuation and the continuation is always the very last step to be evaluated. We will be using this exact same pattern later in this post, so make sure you understand what is going on here.

Continuation examples

With the power of continuations at our disposal, we can use the same divide function in three completely different ways, depending on what the caller wants.

Here are three scenarios we can create quickly:

  • pipe the result into a message and print it,
  • convert the result to an option using None for the bad case and Some for the good case,
  • or throw an exception in the bad case and just return the result in the good case.
  1. // Scenario 1: pipe the result into a message
  2. // ----------------------------------------
  3. // setup the functions to print a message
  4. let ifZero1 () = printfn "bad"
  5. let ifSuccess1 x = printfn "good %i" x
  6. // use partial application
  7. let divide1 = divide ifZero1 ifSuccess1
  8. //test
  9. let good1 = divide1 6 3
  10. let bad1 = divide1 6 0
  11. // Scenario 2: convert the result to an option
  12. // ----------------------------------------
  13. // setup the functions to return an Option
  14. let ifZero2() = None
  15. let ifSuccess2 x = Some x
  16. let divide2 = divide ifZero2 ifSuccess2
  17. //test
  18. let good2 = divide2 6 3
  19. let bad2 = divide2 6 0
  20. // Scenario 3: throw an exception in the bad case
  21. // ----------------------------------------
  22. // setup the functions to throw exception
  23. let ifZero3() = failwith "div by 0"
  24. let ifSuccess3 x = x
  25. let divide3 = divide ifZero3 ifSuccess3
  26. //test
  27. let good3 = divide3 6 3
  28. let bad3 = divide3 6 0

Notice that with this approach, the caller never has to catch an exception from divide anywhere. The caller decides whether an exception will be thrown, not the callee. So not only has the divide function become much more reusable in different contexts, but the cyclomatic complexity has just dropped a level as well.

The same three scenarios can be applied to the isEven implementation:

  1. // Scenario 1: pipe the result into a message
  2. // ----------------------------------------
  3. // setup the functions to print a message
  4. let ifOdd1 x = printfn "isOdd %i" x
  5. let ifEven1 x = printfn "isEven %i" x
  6. // use partial application
  7. let isEven1 = isEven ifOdd1 ifEven1
  8. //test
  9. let good1 = isEven1 6
  10. let bad1 = isEven1 5
  11. // Scenario 2: convert the result to an option
  12. // ----------------------------------------
  13. // setup the functions to return an Option
  14. let ifOdd2 _ = None
  15. let ifEven2 x = Some x
  16. let isEven2 = isEven ifOdd2 ifEven2
  17. //test
  18. let good2 = isEven2 6
  19. let bad2 = isEven2 5
  20. // Scenario 3: throw an exception in the bad case
  21. // ----------------------------------------
  22. // setup the functions to throw exception
  23. let ifOdd3 _ = failwith "assert failed"
  24. let ifEven3 x = x
  25. let isEven3 = isEven ifOdd3 ifEven3
  26. //test
  27. let good3 = isEven3 6
  28. let bad3 = isEven3 5

In this case, the benefits are subtler, but the same: the caller never had to handle booleans with an if/then/else anywhere. There is less complexity and less chance of error.

It might seem like a trivial difference, but by passing functions around like this, we can use all our favorite functional techniques such as composition, partial application, and so on.

We have also met continuations before, in the series on designing with types. We saw that their use enabled the caller to decide what would happen in case of possible validation errors in a constructor, rather than just throwing an exception.

  1. type EmailAddress = EmailAddress of string
  2. let CreateEmailAddressWithContinuations success failure (s:string) =
  3. if System.Text.RegularExpressions.Regex.IsMatch(s,@"^\S+@\S+\.\S+$")
  4. then success (EmailAddress s)
  5. else failure "Email address must contain an @ sign"

The success function takes the email as a parameter and the error function takes a string. Both functions must return the same type, but the type is up to you.

And here is a simple example of the continuations in use. Both functions do a printf, and return nothing (i.e. unit).

  1. // setup the functions
  2. let success (EmailAddress s) = printfn "success creating email %s" s
  3. let failure msg = printfn "error creating email: %s" msg
  4. let createEmail = CreateEmailAddressWithContinuations success failure
  5. // test
  6. let goodEmail = createEmail "x@example.com"
  7. let badEmail = createEmail "example.com"

Continuation passing style

Using continuations like this leads to a style of programming called “continuation passing style“ (or CPS), whereby every function is called with an extra “what to do next” function parameter.

To see the difference, let’s look at the standard, direct style of programming.

When you use the direct style, you go “in” and “out” of functions, like this

  1. call a function ->
  2. <- return from the function
  3. call another function ->
  4. <- return from the function
  5. call yet another function ->
  6. <- return from the function

In continuation passing style, on the other hand, you end up with a chain of functions, like this:

  1. evaluate something and pass it into ->
  2. a function that evaluates something and passes it into ->
  3. another function that evaluates something and passes it into ->
  4. yet another function that evaluates something and passes it into ->
  5. ...etc...

There is obviously a big difference between the two styles.

In the direct style, there is a hierarchy of functions. The top level function is a sort of “master controller” who calls one subroutine, and then another, deciding when to branch, when to loop, and generally coordinating the control flow explicitly.

In the contination passing style, though, there is no “master controller”. Instead there is a sort of “pipeline”, not of data but of control flow, where the “function in charge” changes as the execution logic flows through the pipe.

If you have ever attached a event handler to a button click in a GUI, or used a callback with BeginInvoke, then you have used this style without being aware of it. And in fact, this style will be key to understanding the async workflow, which I’ll discuss later in this series.

Continuations and ‘let’

So how does all this fit in with let?

Let’s go back and revisit what ‘let` actually does.

Remember that a (non-top-level) “let” can never be used in isolation — it must always be part of a larger code block.

That is:

  1. let x = someExpression

really means:

  1. let x = someExpression in [an expression involving x]

And then every time you see the x in the second expression (the body expression), substitute it with the first expression (someExpression).

So for example, the expression:

  1. let x = 42
  2. let y = 43
  3. let z = x + y

really means (using the verbose in keyword):

  1. let x = 42 in
  2. let y = 43 in
  3. let z = x + y in
  4. z // the result

Now funnily enough, a lambda looks very similar to a let:

  1. fun x -> [an expression involving x]

and if we pipe in the value of x as well, we get the following:

  1. someExpression |> (fun x -> [an expression involving x] )

Doesn’t this look awfully like a let to you? Here is a let and a lambda side by side:

  1. // let
  2. let x = someExpression in [an expression involving x]
  3. // pipe a value into a lambda
  4. someExpression |> (fun x -> [an expression involving x] )

They both have an x, and a someExpression, and everywhere you see x in the body of the lambda you replace it with someExpression.
Yes, the x and the someExpression are reversed in the lambda case, but otherwise it is basically the same thing as a let.

So, using this technique, we can rewrite the original example in this style:

  1. 42 |> (fun x ->
  2. 43 |> (fun y ->
  3. x + y |> (fun z ->
  4. z)))

When it is written this way, you can see that we have transformed the let style into a continuation passing style!

  • In the first line we have a value 42 — what do we want to do with it? Let’s pass it into a continuation, just as we did with the isEven function earlier. And in the context of the continuation, we will relabel 42 as x.
  • In the second line we have a value 43 — what do we want to do with it? Let’s pass it too into a continuation, calling it y in that context.
  • In the third line we add the x and y together to create a new value. And what do we want to do with it? Another continuation, another label (z).
  • Finally in the last line we are done and the whole expression evaluates to z.

Wrapping the continuation in a function

Let’s get rid of the explicit pipe and write a little function to wrap this logic. We can’t call it “let” because that is a reserved word, and more importantly, the parameters are backwards from ‘let’.
The “x” is on the right hand side, and the “someExpression” is on the left hand side. So we’ll call it pipeInto for now.

The definition of pipeInto is really obvious:

  1. let pipeInto (someExpression,lambda) =
  2. someExpression |> lambda

Note that we are passing both parameters in at once using a tuple rather than as two distinct parameters separated by whitespace. They will always come as a pair.

So, with this pipeInto function we can then rewrite the example once more as:

  1. pipeInto (42, fun x ->
  2. pipeInto (43, fun y ->
  3. pipeInto (x + y, fun z ->
  4. z)))

or we can eliminate the indents and write it like this:

  1. pipeInto (42, fun x ->
  2. pipeInto (43, fun y ->
  3. pipeInto (x + y, fun z ->
  4. z)))

You might be thinking: so what? Why bother to wrap the pipe into a function?

The answer is that we can add extra code in the pipeInto function to do stuff “behine the scenes”, just as in a computation expression.

The “logging” example revisited

Let’s redefine pipeInto to add a little bit of logging, like this:

  1. let pipeInto (someExpression,lambda) =
  2. printfn "expression is %A" someExpression
  3. someExpression |> lambda

Now… run that code again.

  1. pipeInto (42, fun x ->
  2. pipeInto (43, fun y ->
  3. pipeInto (x + y, fun z ->
  4. z
  5. )))

What is the output?

  1. expression is 42
  2. expression is 43
  3. expression is 85

This is exactly the same output as we had in the earlier implementations. We have created our own little computation expression workflow!

If we compare this side by side with the computation expression version, we can see that our homebrew version is very similar to the let!, except that we have the parameters reversed, and we have the explicit arrow for the continuation.

computation expression: logging

The “safe divide” example revisited

Let’s do the same thing with the “safe divide” example. Here was the original code:

  1. let divideBy bottom top =
  2. if bottom = 0
  3. then None
  4. else Some(top/bottom)
  5. let divideByWorkflow x y w z =
  6. let a = x |> divideBy y
  7. match a with
  8. | None -> None // give up
  9. | Some a' -> // keep going
  10. let b = a' |> divideBy w
  11. match b with
  12. | None -> None // give up
  13. | Some b' -> // keep going
  14. let c = b' |> divideBy z
  15. match c with
  16. | None -> None // give up
  17. | Some c' -> // keep going
  18. //return
  19. Some c'

You should see now that this “stepped” style is an obvious clue that we really should be using continuations.

Let’s see if we can add extra code to pipeInto to do the matching for us. The logic we want is:

  • If the someExpression parameter is None, then don’t call the continuation lambda.
  • If the someExpression parameter is Some, then do call the continuation lambda, passing in the contents of the Some.

Here it is:

  1. let pipeInto (someExpression,lambda) =
  2. match someExpression with
  3. | None ->
  4. None
  5. | Some x ->
  6. x |> lambda

With this new version of pipeInto we can rewrite the original code like this:

  1. let divideByWorkflow x y w z =
  2. let a = x |> divideBy y
  3. pipeInto (a, fun a' ->
  4. let b = a' |> divideBy w
  5. pipeInto (b, fun b' ->
  6. let c = b' |> divideBy z
  7. pipeInto (c, fun c' ->
  8. Some c' //return
  9. )))

We can clean this up quite a bit.

First we can eliminate the a, b and c, and replace them with the divideBy expression directly. So that this:

  1. let a = x |> divideBy y
  2. pipeInto (a, fun a' ->

becomes just this:

  1. pipeInto (x |> divideBy y, fun a' ->

Now we can relabel a' as just a, and so on, and we can also remove the stepped indentation, so that we get this:

  1. let divideByResult x y w z =
  2. pipeInto (x |> divideBy y, fun a ->
  3. pipeInto (a |> divideBy w, fun b ->
  4. pipeInto (b |> divideBy z, fun c ->
  5. Some c //return
  6. )))

Finally, we’ll create a little helper function called return' to wrap the result in an option. Putting it all together, the code looks like this:

  1. let divideBy bottom top =
  2. if bottom = 0
  3. then None
  4. else Some(top/bottom)
  5. let pipeInto (someExpression,lambda) =
  6. match someExpression with
  7. | None ->
  8. None
  9. | Some x ->
  10. x |> lambda
  11. let return' c = Some c
  12. let divideByWorkflow x y w z =
  13. pipeInto (x |> divideBy y, fun a ->
  14. pipeInto (a |> divideBy w, fun b ->
  15. pipeInto (b |> divideBy z, fun c ->
  16. return' c
  17. )))
  18. let good = divideByWorkflow 12 3 2 1
  19. let bad = divideByWorkflow 12 3 0 1

Again, if we compare this side by side with the computation expression version, we can see that our homebrew version is identical in meaning. Only the syntax is different.

computation expression: logging

Summary

In this post, we talked about continuations and continuation passing style, and how we can think of let as a nice syntax for doing continuations behind scenes.

So now we have everything we need to start creating our own version of let. In the next post, we’ll put this knowledge into practice.