Continuation-Passing Style and Explicit Control
Represent the rest of a computation as a function and preserve source evaluation order through nested continuations.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
A computation passes its result onward
In direct style a subexpression returns its result to the surrounding expression. In continuation-passing style, give it a function \(k\) representing what to do with that result. To evaluate \(f(x)+g(y)\) from left to right, run \(f(x)\) with a continuation that receives \(a\), then runs \(g(y)\) with a continuation receiving \(b\), then passes \(a+b\) to \(k\). That nesting makes order explicit.
Return becomes an ordinary control transfer
A function rewritten this way can invoke its continuation in tail position. It may still allocate closures for pending work if those continuations escape. An exception can be represented with a separate failure continuation. Tail calls explain when these transfers avoid new frames. Algebraic effects use related ideas about an explicit remainder of computation when an operation is handled.[1][2]