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):||23 November 2006|
|PDF File Size:||16.22 Mb|
|ePub File Size:||11.49 Mb|
|Price:||Free* [*Free Regsitration Required]|
Because continuations are used in a last-allocated, first-invoked fashion, we can implement them as a stack.
How to compile with continuations
Atomic expressions always produce a value and never cause side effects. The wrinkle in the previous transform was that it forced function application to bind its function and its arguments to variables, even if they were already atomic. The transform T expr cont will transform continuaitons into a CPS value, and then construct a call site that applies the term cont to that value:.
We can even use the stack pointer register. The transform now has three principal functions: Continuation-passing style If you’re new to continuation-passing style, I recommend my earlier post on continuation-passing style by example.
The transform converts each with Tand then catches their results in newly-created continuations. The transformation of function application is the main culprit: 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: My post on A-Normalization.
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.
Appel’s Compiling with Continuations and Queinnec’s Lisp in Small Pieces are invaluable resources for the functional compiler writer. Fortunately, the hybrid CPS transform readily adapts to features like basic values, conditionals, side effects, sequencing and explicit appell. Yet, if the transform tags variables, call forms and lambdas as being user or continuationthe stack is recoverable.
For example, the following: Code is available in Racket ; techniques applicable to any language. Examples Even while this transformation is simple, its results are poor. More resources Andrew Kennedy points out that CPS is more advantageous as an intermediate form with respect to optimization than had cntinuations previously thought.
If you’re using this in practice, alphatize the program first, or modify letrec to bind the continuation to a variable outside the scope of the letrec. Consider an expanded input language: My post on implementing exceptions.
In this transformation, we have two functions, Continuxtions and Wiith If the transform receives a 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.
When a continuation gets invoked, deallocate its space by resetting the stack pointer to that continuation. This transformation is not hygienic ; if the continuation c references any of the ; bound variables!
There are some advantages [and disadvantages] to stackless compilation. In the higher-order transform, the function T: The naive transformation The naive transformation likely dates to Plotkin’s earliest work. This simple shift in perspective is economical: When a continuation gets allocated, bump the stack pointer. The M function only has to watch for lambda terms. To start, split the grammar: This is two steps forward, and one step back: For the fourth transform, it will become partitioned CPS, and for the final transform, it will be a more realistic intermediate language with side effects, conditionals, basic values and explict recursion.
Ultimately, however, that transformation must run on real code. For the first three transforms, the input language is the lambda calculus: Both of those lambda terms are continuations.
Compiling with Continuations
Scaling to real language features The lambda calculus makes a nice platform for studying the architecture of a program transformation.
In terms of unreasonable effectiveness, the continuatilns to continuation-passing style CPS ranks with the Y combinator. T ‘ g a ‘halt produces: If you’re new to continuation-passing style, I recommend my earlier post on continuation-passing style by example. 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:. Danvy, Millikin and Nielsen have been able to connect some of these methods, including the well-known treatments in Appel’s Compiling with Continuations and Queinnec’s Lisp in Small Pieces.
Complex expressions may not terminate, and they may produce side effects.
How to compile with continuations [ article index ]  [ mattmight ] [ rss ]. The transformation for letrec is not hygienic, because the transformation can introduce the letrec ‘d bindings into the qppel of the continuation that gets passed to the transformer.
It is the transformation that newcomers often discover for themselves.