In CPS, each procedure takes an extra argument representing what should be done with the result the function is calculating. This, along with a restrictive style prohibiting a variety of constructs usually available, is used to expose the semantics of programs, making them easier to analyze. This style also makes it easy to express unusual control structures, like catch/throw or other non-local transfers of control. The key to CPS is to remember that (a)
every function takes an extra argument known as its continuation, and (b) every argument in a function call must be either a variable or a
lambda expression (not a more complex expression). This has the effect of turning expressions "inside-out" because the innermost parts of the expression must be evaluated first, thus CPS makes explicit the order of evaluation as well as the control flow. Some examples of code in direct style and the corresponding CPS appear below. These examples are written in the programming language
Scheme; by convention the continuation function is represented as a parameter named "k": In the CPS versions, the primitives used, like +& and *& are themselves CPS, not direct style, so to make the above examples work in a Scheme system requires writing these CPS versions of primitives, with for instance *& defined by: (define (*& x y k) (k (* x y))) To do this in general, we might write a conversion routine: (define (cps-prim f) (lambda args (let ((r (reverse args))) ((car r) (apply f (reverse (cdr r))))))) (define *& (cps-prim *)) (define +& (cps-prim +)) To call a procedure written in CPS from a procedure written in direct style, it is necessary to provide a continuation that will receive the result computed by the CPS procedure. In the example above (assuming that CPS primitives have been provided), we might call (factorial& 10 (lambda (x) (display x) (newline))). There is some variety between compilers in the way primitive functions are provided in CPS. Above is used the simplest convention, however sometimes Boolean primitives are provided that take two
thunks to be called in the two possible cases, so the (=& n 0 (lambda (b) (if b ...))) call inside f-aux& definition above would be written instead as (=& n 0 (lambda () (k a)) (lambda () (-& n 1 ...))). Similarly, sometimes the if primitive is not included in CPS, and instead a function if& is provided which takes three arguments: a Boolean condition and the two thunks corresponding to the two arms of the conditional. The translations shown above show that CPS is a global transformation. The direct-style
factorial takes, as might be expected, a single argument; the CPS
factorial& takes two: the argument and a continuation. Any function calling a CPS-ed function must either provide a new continuation or pass its own; any calls from a CPS-ed function to a non-CPS function will use implicit continuations. Thus, to ensure the total absence of a function stack, the entire program must be in CPS.
CPS in Haskell A function pyth to calculate a hypotenuse using the
Pythagorean theorem can be written in
Haskell. A traditional implementation of the pyth function looks like this: pow2 :: Float -> Float pow2 x = x ** 2 add :: Float -> Float -> Float add x y = x + y pyth :: Float -> Float -> Float pyth x y = sqrt (add (pow2 x) (pow2 y)) To transform the traditional function to CPS, its signature must be changed. The function will get another argument of function type, and its return type depends on that function: pow2' :: Float -> (Float -> a) -> a pow2' x cont = cont (x ** 2) add' :: Float -> Float -> (Float -> a) -> a add' x y cont = cont (x + y) -- Types a -> (b -> c) and a -> b -> c are equivalent, so CPS function -- may be viewed as a higher order function sqrt' :: Float -> ((Float -> a) -> a) sqrt' x = \cont -> cont (sqrt x) pyth' :: Float -> Float -> (Float -> a) -> a pyth' x y cont = pow2' x (\x2 -> pow2' y (\y2 -> add' x2 y2 (\anb -> sqrt' anb cont))) First we calculate the square of
a in pyth' function and pass a lambda function as a continuation which will accept a square of
a as a first argument. And so on until the result of the calculations are reached. To get the result of this function we can pass id function as a final argument which returns the value that was passed to it unchanged: pyth' 3 4 id == 5.0. The mtl
library, which is shipped with
Glasgow Haskell Compiler (GHC), has the module Control.Monad.Cont. This module provides the Cont type, which implements Monad and some other useful functions. The following snippet shows the pyth' function using Cont: pow2_m :: Float -> Cont a Float pow2_m a = return (a ** 2) pyth_m :: Float -> Float -> Cont a Float pyth_m a b = do a2 Not only has the syntax become cleaner, but this type allows us to use a function callCC with type MonadCont m => ((a -> m b) -> m a) -> m a. This function has one argument of a function type; that function argument accepts the function too, which discards all computations going after its call. For example, let's break the execution of the pyth function if at least one of its arguments is negative returning zero: pyth_m :: Float -> Float -> Cont a Float pyth_m a b = callCC $ \exitF -> do -- $ sign helps to avoid parentheses: a $ b + c == a (b + c) when (b Bool -> f () -> f () a2
Continuations as objects Programming with continuations can also be useful when a caller does not want to wait until the callee completes. For example, in
user interface (UI) programming, a routine can set up dialog box fields and pass these, along with a continuation function, to the UI framework. This call returns right away, allowing the application code to continue while the user interacts with the dialog box. Once the user presses the "OK" button, the framework calls the continuation function with the updated fields. Although this style of coding uses continuations, it is not full CPS. function confirmName() { fields.name = name; framework.Show_dialog_box(fields, confirmNameContinuation); } function confirmNameContinuation(fields) { name = fields.name; } A similar idea can be used when the function must run in a different thread or on a different processor. The framework can execute the called function in a worker thread, then call the continuation function in the original thread with the worker's results. This is in
Java 8 using the
Swing UI framework: void buttonHandler() { // This is executing in the Swing UI thread. // We can access UI widgets here to get query parameters. int parameter = getField(); new Thread(() -> { // This code runs in a separate thread. // We can do things like access a database or a // blocking resource like the network to get data. int result = lookup(parameter); javax.swing.SwingUtilities.invokeLater(() -> { // This code runs in the UI thread and can use // the fetched data to fill in UI widgets. setField(result); }); }).start(); } ==Tail calls==