[Paper Review] C++ Templates as Partial Evaluation
This paper proposes that C++ templates, though originally designed for generic programming, unintentionally enable powerful compile-time computation and code generation through partial evaluation. By reinterpreting templates as a two-level language where types are first-class values, the authors introduce Catat—a language based on partial evaluation that unifies generic programming, compile-time computation, and code generation with a simpler, more intuitive syntax.
This paper explores the relationship between C++ templates and partial evaluation. Templates were designed to support generic programming, but unintentionally provided the ability to perform compile-time computations and code generation. These features are completely accidental, and as a result their syntax is awkward. By recasting these features in terms of partial evaluation, a much simpler syntax can be achieved. C++ may be regarded as a two-level language in which types are first-class values. Template instantiation resembles an offline partial evaluator. This paper describes preliminary work toward a single mechanism based on Partial Evaluation which unifies generic programming, compile-time computation and code generation. The language Catat is introduced to illustrate these ideas.
Motivation & Objective
- To explore the unintended but powerful capabilities of C++ templates for compile-time computation and code generation.
- To demonstrate that these capabilities arise from template instantiation resembling offline partial evaluation.
- To propose a unified framework based on partial evaluation that simplifies syntax for generic programming, compile-time computation, and code generation.
- To introduce Catat, a prototype language illustrating how partial evaluation can unify these features with cleaner semantics.
Proposed method
- Reinterprets C++ templates as a two-level language where types are first-class values with static binding.
- Models template instantiation as an offline partial evaluation process, where static parameters are specialized at compile time.
- Introduces Catat, a language designed around partial evaluation principles to unify generic programming, compile-time computation, and code generation.
- Applies flattening transformations to convert two-level functions into single-level generators, enabling efficient compilation.
- Uses binding-time analysis and static parameter propagation to enable residualization of code generators.
- Employs recursive template specialization and static constants to implement control flow and arithmetic at compile time.
Experimental results
Research questions
- RQ1How can C++ templates be formally understood as a form of partial evaluation?
- RQ2Can a unified language model based on partial evaluation simplify the syntax and semantics of generic programming and compile-time code generation?
- RQ3What are the practical benefits of recasting template metaprogramming as partial evaluation in terms of expressiveness and maintainability?
- RQ4How can partial evaluation principles be used to generate efficient, specialized code from high-level template abstractions?
Key findings
- C++ templates unintentionally support powerful compile-time computation and code generation, such as calculating powers, factorials, and primality at compile time.
- The use of recursive template specialization and static constants enables the implementation of control structures like loops and conditionals at compile time.
- Template metaprogramming can generate optimized, unrolled code for scientific computing, such as specialized dot product algorithms.
- The Catat language prototype demonstrates that a single mechanism based on partial evaluation can unify generic programming, compile-time computation, and code generation with simpler syntax.
- The approach enables scripting capabilities through interpretation of static constructs, and supports Futamura-style projections for domain-specific language compilation.
- Reflection and meta-level processing become natural extensions in such a system, enabling runtime inspection and manipulation of types and functions.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.