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):||2 January 2008|
|PDF File Size:||17.45 Mb|
|ePub File Size:||20.20 Mb|
|Price:||Free* [*Free Regsitration Required]|
Consider an expanded input language: T ‘ g a ‘halt produces: Continuation-passing style If you’re new to continuation-passing style, I recommend my earlier post on continuation-passing style by example. The naive transformation The naive transformation likely dates to Plotkin’s earliest work. My post on implementing exceptions.
Compiling with Continuations
Atomic expressions always produce a value and never cause side effects. Danvy, Millikin and Nielsen have wppel 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. This is two steps forward, and one step back: There are some advantages [and disadvantages] to stackless compilation.
The transform T expr cont will transform expr into a CPS value, and then construct a call site that applies the term cont to that value:. In terms of unreasonable effectiveness, the transformation to continuation-passing style CPS ranks with the Y combinator.
How to compile with continuations
We can even use the stack pointer register. 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. This transformation is not hygienic ; if the continuation continuatios references any of the ; bound variables!
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. How to compile with continuations [ article index ]  [ mattmight ] [ rss ]. Because continuations are used in a last-allocated, first-invoked fashion, we can implement them as a stack. All calls become tail calls, so in effect, there is no stack.
Ultimately, however, that transformation must run on real code. If you’re new to continuation-passing style, I recommend my earlier post on continuation-passing style by example.
How to compile with continuations
Fortunately, the hybrid CPS transform readily adapts to features like basic values, conditionals, side effects, sequencing and explicit recursion. The wrinkle in the clntinuations transform was that it forced function application to bind its function and its arguments to variables, even if they were already atomic.
To start, split the grammar: 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 conhinuations transformer. During compilation, high-level control constructs ranging from coroutines and exceptions to while loops and break statements steadily desugar into a mixture of two constructs: For the first three transforms, the input language is the lambda calculus: The transform converts each with Tand then catches their results in newly-created continuations.
It is the transformation that newcomers often discover for themselves. The lambda calculus makes a nice platform for studying the architecture of a program transformation.
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:. For example, the following: My post on A-Normalization. co,piling
Appel’s Compiling with Continuations and Queinnec’s Lisp in Small Pieces are invaluable resources for the functional compiler writer.
In this transformation, we have two functions, M and T: This simple shift in perspective is economical: