Cambridge University Press, Appel, Andrew W., Compiling with continuations / Andrew W. Appel. p. 25 cm. Includes bibliographical references. Then, during CPS transformation, continuations desugar into functions. methods, including the well-known treatments in Appel’s Compiling with Continuations. This book shows how continuation-passing style is used as an intermediate representation to perform optimizations and program transformations. Continuations.
|Published (Last):||7 October 2005|
|PDF File Size:||8.13 Mb|
|ePub File Size:||2.20 Mb|
|Price:||Free* [*Free Regsitration Required]|
Danvy, Millikin and Nielsen have been able to connect some of these methods, including compilign well-known treatments in Appel’s Compiling with Continuations and Queinnec’s Lisp in Small Pieces.
Because continuations are used in a last-allocated, first-invoked fashion, we can implement them as a stack. The transform T expr cont will transform expr into a CPS contibuations, and then construct a call site that applies the term cont to that value:. Code is available in Racket ; techniques applicable to any language.
How to compile with continuations
This is two steps forward, and one step back: The transformation for letrec is not hygienic, because the transformation can introduce the letrec ‘d bindings into the scope of the continuation that gets passed to the transformer.
Scaling to real language features The lambda calculus makes a nice platform for studying the architecture of a program transformation. The goal of this article is to introduce CPS transforms in small, individually digestible pieces before stitching them back together as a unified whole.
Both of those lambda terms are continuations. There are some advantages [and disadvantages] to stackless compilation. When a continuation gets allocated, bump the stack pointer. If the wifh receives continations real function expecting the atomic version of a the supplied expression, then the transform can check whether it is necessary to bind it to a variable. Continuation-passing style If you’re new to continuation-passing style, I recommend my earlier post on continuation-passing style by example.
A hybrid transform Combining the naive and higher-order transforms provides the best of both worlds. The naive transformation The naive transformation likely dates to Plotkin’s earliest work. More resources Andrew Kennedy points out that CPS is more advantageous as an intermediate witn with respect to optimization than had been previously thought.
This transformation is not hygienic ; if the continuation c references any of the ; bound variables! In terms of unreasonable effectiveness, the transformation to continuation-passing style CPS ranks with the Y combinator. The transform converts each with Tand then catches their results in newly-created continuations.
Fortunately, the hybrid CPS transform readily adapts to features like basic values, conditionals, side effects, sequencing and explicit recursion. It is the transformation that newcomers often discover for themselves.
My post on A-Normalization. The transformation of function application is the main culprit: Consider an expanded input language: The expression T expr cont might be read “the transformation of expr into continuation-passing style, such that cont will be invoked on its result.
Compiling with Continuations
To generate a fresh variable bound to a user value, the transform will use genusymand for a fresh variables bound to a continuation, the transform will use genksym:. Atomic expressions always produce a value and never cause side effects.
T ‘ g a ‘halt produces: The M function only has to watch for lambda terms. To generate a fresh variable bound to a user value, the transform will use genusymand for a fresh variables bound to a continuation, the transform will use genksym: Appel’s Compiling with Continuations and Queinnec’s Lisp in Small Pieces are invaluable resources for the functional compiler writer. In the function-application transform, the values of both the function and the argument have to cobtinuations converted into CPS.
The lambda calculus makes a nice platform for studying the architecture of a program transformation. The wrinkle in the previous transform was that it forced function application to bind its function and its arguments to variables, even contnuations they were already atomic. This simple shift in perspective is economical: My contonuations on implementing exceptions.
How to compile with continuations
Ultimately, however, that transformation must run on real code. How to compile with continuations [ article index ]  [ mattmight ] [ rss ]. All calls become tail calls, so in effect, there is no stack. During compilation, high-level control constructs compi,ing from coroutines and exceptions to while loops and break statements steadily desugar into a mixture of two constructs: In the higher-order transform, the function T: