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.

TODO Brackets

TODO Escape

TODO Run

Author: expye(Zihao Ye)

Email: expye@outlook.com

Date: 2021-11-01 Mon 00:00

Last modified: 2022-09-23 Fri 00:04

Licensed under CC BY-NC 4.0