Continuations

Overview

  • Continuation = represents the rest of a computation at any given point in the computation
  • goto can be used to manipulate the continuation of a computation step
  • Only a few languages provide full unrestrained access to continuations
    • Scheme has call/cc (call-with-current-continuation)
    • Any language that supports closures can be used to manually implement call/cc

Example

def foo(x):
   return x + 1
  • Function returns a value but it is implicit where this value is actually returned to
  • Core idea of continuations: make this behaviour explicit by adding a continuation argument
    • Instead of returning the value, we continue with the value by providing it as an argument to the continuation
def foo(x, c):
   c(x + 1)
  • This means a function never returns but instead continues
    • See them as “gotos but with arguments”

Continuation-Passing-Style (CPS)

  • Basic idea:
    1. A function signature gets extra continuation parameter
    2. A function does not return a value; instead the value is passed to the continuation function

References