When Do Staging Annotations Preserve Semantics? Mechanizing Typed Semantics-Preserving Multi-stage Programming with Let-Insertion (Extended Version)
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, and , 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 evaluates to a value , then is contextually equivalent to the stage-erasure of . 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.
Create a lesson
Metadata
- Comments
- 29 pages; preprint of paper accepted at OOPSLA 2026
Related papers
Special Delivery: Programming with Mailbox Types (Extended Version)
Shared title terminologycs.PLSimon Fowler, Duncan Paul Attard, Danielle Marshall et al.
On the Expressiveness of Asynchronous Processes with Nested Session Types (Extended Version)
Shared title terminologycs.PLSjors Wortelboer, Dan Frumin, Jorge A. Pérez
Moded Types for Grassroots Logic Programs, by AI, for AI (Full Version)
Shared title terminologycs.PLEhud Shapiro
Mover Logic: A Concurrent Program Logic for Reduction and Rely-Guarantee Reasoning (Extended Version)
Shared title terminologycs.PLCormac Flanagan, Stephen N. Freund
Scalable Probabilistic Program Verification via Typed Extended Decision Diagrams
Shared title terminologycs.PLDaniel Basgöze, Kevin Batz, Sebastian Junges et al.
An End-to-End Theory for Compositional Symbolic Execution: From Expressive Specifications to SMT (Extended Version)
Shared title terminologycs.PLShivanandan Tamil Kumaran, Andreas Lööw, Philippa Gardner