When Do Staging Annotations Preserve Semantics? Mechanizing Typed Semantics-Preserving Multi-Stage Programming with Let-Insertion (Extended Version)

📅 2026-06-29
📈 Citations: 0
Influential: 0
📄 PDF
🤖 AI Summary
This work addresses the challenge that syntactic manipulation of effectful code in multi-stage programming can disrupt evaluation order, leading to semantic discrepancies with non-staged programs. To resolve this, the paper introduces λ²_ref, a statically typed two-level calculus equipped with an automatic let-insertion mechanism. It integrates a lightweight type and effect system to track control effects and establishes a binary logical relation to prove a strong semantic preservation theorem. The study rigorously characterizes when staging annotations preserve semantics, providing—for the first time—a formal guarantee of semantic equivalence for multi-stage programs featuring mutable references. The meta-theory is fully mechanized, thereby laying a solid theoretical foundation for semantic-preserving multi-stage programming.
📝 Abstract
Multi-stage programming with quotations has long provided a powerful way to generate and manipulate code. By treating code as data, programmers can write multi-stage programs in which earlier stages produce specialized code from inputs available at generation time. Modern typed multi-stage languages (e.g., MetaML, MetaOCaml, Template Haskell, and Scala 3) adopt quotation/splicing constructs while enforcing the well-typedness of generated code. However, manipulating code fragments syntactically can subtly change evaluation order, leading to semantic discrepancies between a staged program and its unstaged counterpart, which is intended to serve as a reference implementation in many cases. The inconsistency complicates reasoning about correctness, and prevents staged code from being a drop-in replacement for its unstaged counterpart. In this paper, we study the design of multi-stage languages with semantics preservation guarantees. We develop two statically typed two-stage calculi, $λ_{|2|}$ and $λ^{ref}_{|2|}$, the latter supporting mutable references in the second stage. Their dynamic semantics models automatic let-insertion, tracked as a control effect in a lightweight type-and-effect system, enabling type-safe and semantics-preserving manipulation of effectful code fragments. We develop binary logical relations to prove strong semantics-preservation theorems: if a well-typed two-stage program $t_1$ evaluates to a value $\mathsf{code} t_2$, then $t_2$ is contextually equivalent to the stage-erasure of $t_1$. Our calculi and their mechanized metatheory provide a simple and definitive answer to the question posed by Inoue and Taha of when staging annotations preserve semantics, and lay a foundation for future work on semantics-preserving multi-stage programming.
Problem

Research questions and friction points this paper is trying to address.

multi-stage programming
semantics preservation
staging annotations
code generation
evaluation order
Innovation

Methods, ideas, or system contributions that make the work stand out.

multi-stage programming
semantics preservation
let-insertion
type-and-effect system
logical relations