Multi-Staged Programming Explained
Table of Contents
A digest of paper A Gentle Introduction to Multi-stage Programming.
Why MSP
We might use the following macro to do compile-time code generation for kernels with different parameters:
#define DEFINE_KERNEL(x, y) template void my_kernel<x, y>() DEFINE_KERNEL(5, 6); DEFINE_KERNEL(2, 3);
The macro DEFINE_KERNEL
, works like string replacement, and there is no guarantee of type safety.
A desired multi-stage programming should enable us to do multi-stage code-generation at compile-time, while ensuring type-safety: the generated code should be well-typed.
How
The paper proposed a MSP solution: MetaOCaml by extending OCaml.
MSP Constructs
There are three basic constructs in MetaOCaml:
- Brackets
- Escape
- Run
Programmer could manually specify the order of evaluation of terms.