Theoremadvanced
Futamura projections
Also called Futamura's three projections.
Specializing an interpreter to a program gives a compiled program, specializing the specializer to the interpreter gives a compiler.
The Futamura projections are three equations that follow from the definition of a partial evaluator. A specializer takes a program and the values of some of its inputs and makes a residual program for the other inputs. Apply it to an interpreter and a source program, and the result is the source program compiled. Apply it to itself and the interpreter, and the result is a compiler. Apply it to itself twice, and the result is a compiler generator. Futamura described the projections in 1971, and the proof is three lines of substitution. The common mistake is to think the theorem promises good compilers. It does not: the trivial specializer, which only stores its static input in the program, satisfies all three equations and gives a "compiler" that pairs the interpreter with the source program. Useful results need a specializer that removes the interpretive work, and self-application in practice needs an offline specializer with a binding-time analysis.
Statement
Setting. Fix a programming language whose programs are data of , so that an -program can take -programs as input. For a program write for the partial function it computes. An equation between outcomes means that both are undefined or both are defined and equal. A program with two inputs gets them as a pair.
Hypotheses.
- (mix) is an -program that satisfies the mix equation: for every -program of two inputs and all values and , is defined and is an -program, and .
- (int) is an -program that is an interpreter for a language : for every -program and every value , .
Theorem (the three projections). Define , and . Then:
- is an -program equivalent to : for all .
- is a compiler from to : , the program of item 1, for every -program .
- is a compiler generator: . More generally, for every -program of two inputs, is a generating extension of : . And .
The equalities in items 2 and 3 are equalities of program texts, not only of the functions that the texts compute: the compiler returns exactly the program that would return.
The partial case. If satisfies the mix equation only for the on which it stops, each item holds whenever every specialization named in its proof stops.
In plain words
A specializer takes a program and part of its input, and writes a new program that waits for the rest.
Give it an interpreter and a user program. The new program waits only for the user's data, and it does what the interpreter would do with that user program. That is a translation of the user program: a compiled program.
Now give the specializer a copy of itself and the interpreter. The new program waits for a user program and writes the translation. That is a compiler, made from an interpreter without writing a compiler.
Give the specializer two copies of itself. The new program waits for an interpreter and writes its compiler. That is a compiler generator.
None of this says the results are fast. That depends on how well the specializer works.
Proof
Each item applies the mix equation once or twice, with a different choice of , and .
Item 1. Take and in the mix equation, then use (int):
Item 2. Take , and . This is allowed, since is itself an -program of two inputs:
By item 1, the output is an -program equivalent to , so is a compiler.
Item 3. Take , and for any :
With the right side is by definition. With it is . For a general , let . The mix equation with gives , and the mix equation with gives . Together, .
The partial case. Each step above is one use of the mix equation at a named , or a use of (int). If each of these specializations stops, each step holds, and so does the chain.
Consequences
- Interpreters as specifications of compilers. A language implementer who writes an interpreter and has a good specializer gets compiled code (item 1) and a compiler (item 2). Item 2 also says the compiler is correct if the interpreter and the specializer are: there is nothing else to prove.
- A worked first projection. The partial evaluation page specializes an interpreter for a two-register machine to the program
0: jz 5, 1: dec, 2: inc, 3: inc, 4: jmp 0, 5: halt. The residual program has three functions, one per machine address that a jump can reach, and the loop bodyrun_1holds only the register work. A script counts calls, primitives and conditionals for the interpreter on input , and for the residual program. The residual program is the target of item 1. - Generating extensions. Item 3 says turns any two-input program into a program that, given , writes specialized to . Ershov called such a program a generating extension. A parser generator is a generating extension of a general parser, with the grammar as .
- Run-time compilation. The self-optimizing AST interpreter of Truffle is specialized to a hot user function at run time, which is item 1 applied inside a just-in-time compiler. Meta-tracing reaches item 1 by tracing the interpreter instead of specializing it, as a tracing JIT does for user programs.
- Existence. The s-m-n theorem gives a specializer for any programming language that can compute all computable functions, so the projections hold in every such language. The specializer it gives is the trivial one, so this says nothing about speed.
Tightness
- The equations do not force speed. Let return the text of the program
fun d -> p(s, d), with and written in as constants. It satisfies the mix equation, so all three items hold for it. Its target is the interpreter with the source program stored in it, and its compiler only stores the interpreter. The quality of the projections comes from the specializer, not from the theorem. A common measure is Jones optimality: for a self-interpreter of , the program should be at least as fast as on every input. The trivial specializer fails this by the whole cost of interpretation. - Self-application needs in and on . Items 2 and 3 apply to itself. A specializer written in another language, or one that accepts only a subset of that does not contain its own text, gives item 1 and no more.
- An online specializer self-applies badly. In item 2, is specialized with static and dynamic. An online specializer decides at each step whether a value is static by looking at it. When itself runs on , those values come from , which is dynamic. So the decisions cannot be made while is generated, and they stay in as run-time tests: the compiler is large and slow. Jones, Sestoft and Søndergaard found this in 1985. Their fix, now standard, is an offline specializer: a binding-time analysis marks the program from the split of its inputs before specialization, and the specializer only follows the marks. The marks depend on and not on , so in item 2 they are static, and the generated compiler contains no binding-time tests.
- Termination. The theorem is conditional when can fail to stop. A specializer that stops on for every may still loop when it is applied to itself, because has its own static loops over the text of .
- Encoding. The inputs and outputs of are program texts. In a typed language, programs must be values of one data type, so a typed interpreter has to encode the values of in one universal type, and the target of item 1 keeps the tags of that encoding unless the specializer removes them.
History
Futamura described the projections in 1971, and the paper was reprinted in 1999. Turchin and Ershov worked on the same ideas in the Soviet Union, and Ershov used the term generating extension. The first compiler and compiler generator made by self-application on a computer came from Jones, Sestoft and Søndergaard in 1985, with a specializer for first-order recursion equations whose annotations were computed before specialization. This was the start of binding-time analysis. The book of Jones, Gomard and Sestoft (1993) gives the projections for several languages. Truffle and Graal made the first projection the basis of production language runtimes.
References
- Yoshihiko Futamura, Partial evaluation of computation process, an approach to a compiler-compiler, Higher-Order and Symbolic Computation 12(4), 1999, 381-391, the reprint of the 1971 paper, with the specialization of an interpreter to a program, of the specializer to an interpreter, and of the specializer to itself
- Neil D. Jones, Carsten K. Gomard and Peter Sestoft, Partial Evaluation and Automatic Program Generation, Prentice Hall, 1993, the chapter on the Futamura projections and self-application, generating extensions, the compiler generator, and why self-application needs binding-time analysis
- Neil D. Jones, Peter Sestoft and Harald Søndergaard, An experiment in partial evaluation: the generation of a compiler generator, Rewriting Techniques and Applications (RTA 1985), LNCS 202, 124-140, the first specializer that generated compilers and a compiler generator by self-application, with an annotation of the specializer computed before specialization
- Neil D. Jones, An introduction to partial evaluation, ACM Computing Surveys 28(3), 1996, 480-503, the three projections derived from the mix equation, and the practical conditions for self-application
- Andrei P. Ershov, Mixed computation: potential applications and problems for study, Theoretical Computer Science 18(1), 1982, 41-67, the generating extension of a program and compilation by mixed computation of an interpreter
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag NE50NV Edit this page History Report an error Markdown