Multi-stage programming allows programmers to write meta-programs to generate efficient code. Staging is typically realized either as a language primitive via quotations and splices (e.g., MetaML and its descendants), or as a library embedded in a host language (e.g., Lightweight Modular Staging). Practical library-based systems combine staged evaluation with automatic let-insertion to preserve evaluation order, together with a range of intrinsic optimizations to generate better residual code. However, despite their popularity and practical importance, such a combination has received little semantic treatment, making it difficult to reason about correctness or to systematically compare with other staging paradigms.

This pearl illuminates the operational aspects of staged evaluation with automatic let-insertion and optimizations using functional programming techniques. For a core two-stage language, we develop series of definitional interpreters that concisely describe its staged evaluation to generate optimized, let-inserted residual programs. The definitional interpreters are written in the extended continuation-passing style, where the use of two continuations naturally accounts for let-insertion. With minor refactoring, we scale it to a suite of optimizations, from simple constant propagation/folding, common sub-expression elimination, and dead-code elimination, to more involved optimizations such as partially-static data, β-inlining, and code motion. Each optimization is expressed as a small, modular extension integrated into staged evaluation, requiring neither additional effort from the meta-programmer, nor complex post-hoc compiler infrastructures.