Partial Evaluation and Multi-Stage Programming
Partial evaluation specializes a program to known data. The Futamura projections explain when specializing an interpreter produces a compiled program or even a compiler.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 3
- Reading time
- 2 min
In this article3 chapters
Separate static input from dynamic input
A partial evaluator \(\operatorname{mix}\) takes a program \(p\) and known input \(s\), then returns a residual program. For dynamic input \(d\), the intended preservation property is
\[ \operatorname{mix}(p,s)(d)=p(s,d). \]
This is an observational equation in the language under consideration. If a loop count is static but the accumulated values are dynamic, specialization may unfold control and leave arithmetic in the residual program. Unfolding a static recursive call without a termination discipline can make specialization itself run forever.
Partial evaluation maps fixed inputs to a residual program. Ordinary constant propagation gives a smaller local example of using known facts while preserving the operations that still depend on runtime input.
An interpreter becomes a source of compiled code
An interpreter takes a source program and runtime data. Specializing it to the source program removes interpretation work from the residual program when the relevant structure is static. This is the first Futamura projection. Specializing the partial evaluator to the interpreter gives a program that maps source programs to residual code, the second projection. Under suitable self-application, specializing the partial evaluator to itself gives a compiler generator, the third projection. Each step needs a specializer expressive enough to process the program it is given.
Staging enforces the boundary through types
Binding-time analysis classifies operations by which inputs must be known. Explicit staging can provide code values and operations that quote or splice them. A variable bound only in a future stage cannot be evaluated in an earlier one. Staging rules must also preserve scope and account for effects when computations move across stages. Compare operational semantics, effect handlers, and proof obligations of verified compilers.[1][2]