一句话:用扩展 CPS 定义解释器,为库式多阶段求值中的自动 let 插入与优化建立操作语义,并模块化展示多种优化。
多阶段编程让元程序生成高效代码。库式 staging 系统常将分阶段求值、自动 let 插入与残差代码优化结合,但缺乏语义刻画,难以推理正确性或与其他 staging 范式比较。本文针对核心两阶段语言,用函数式编程技术构建一组定义式解释器,以扩展 CPS 简洁描述分阶段求值,其中两个 continuation 自然处理 let 插入。通过小幅重构,作者把常量传播/折叠、公共子表达式消除、死代码消除、beta-inlining、部分静态数据、代码移动等优化模块化地接入求值过程。每种优化都是小而独立的扩展,无需元程序员额外工作,也无需复杂的事后编译器基础设施。
Multi-stage programming lets programmers write meta-programs that generate efficient code. Staging is typically realized either as a language primitive with quotations and splices (e.g., MetaML and its descendants), or as a library embedded in a host language (e.g., Lightweight Modular Staging). Unlike quotation-based approaches, practical library-based systems combine staged evaluation with automatic let-insertion to preserve evaluation order, along with optimizations that improve residual code. Despite their popularity and practical importance, this combination has received little semantic treatment, making it difficult to reason about correctness or to compare systematically with other staging paradigms. Using functional programming techniques, this pearl illuminates the operational aspects of staged evaluation with automatic let-insertion and optimizations as found in library-based staging systems. For a core two-stage language, we develop a series of definitional interpreters that concisely describe staged evaluation generating optimized, let-inserted residual programs. The interpreters are written in the extended continuation-passing style, where two continuations naturally account for let-insertion. With minor refactoring, we showcase a suite of optimizations, ranging from simple constant propagation/folding, common subexpression elimination, and dead-code elimination to more involved optimizations such as beta-inlining, partially-static data, and code motion. Each optimization is presented as a small, modular extension integrated into staged evaluation, requiring neither additional effort from the meta-programmer, nor complex post-hoc compiler infrastructure.
@article{WeiTZ26,
title = {Let It Be Optimized: Building Multi-stage Evaluators with Let-Insertion and Optimizations in Small Pieces (Functional Pearl)},
author = {Guannan Wei and Jun Tan and Dinghong Zhong},
journal = {Proceedings of the ACM on Programming Languages},
volume = {10},
number = {ICFP},
year = {2026},
doi = {10.1145/3828676}
}