Lune

OOPSLA2026Top-tier venue

When Do Staging Annotations Preserve Semantics? Mechanizing Typed Semantics-Preserving Multi-stage Programming with Let-Insertion

Jun Tan, Guannan Wei

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 welltypedness 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 model 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 welltyped two-stage program 𝑡 1 evaluates to a value code 𝑡 2 , then 𝑡 2 is contextually equivalent to the stage-erasure of 𝑡 1 . Our calculi and their mechanized metatheory provide a simple and definitive answer to the question posed by Inoue and Taha [27,28] of when staging annotations preserve semantics, and lay a foundation for future work on semantics-preserving multi-stage programming.

Ask Lune

Know this paper in minutes, not an afternoon.

Lune can read every page of it and the top-tier papers that cite it, and gives you its claims, its evidence and its limits, each one cited to the paper it comes from. No skimming, no AI slop.

Questions to start from

Lune can draw on

the full paper · related work

Get the answer

Free to start. No credit card required.

Lineage

  • Graph IRs for Impure Higher-Order Languages: Making Aggressive Optimizations Affordable with Precise Effect Dependencies cites Reachability types: tracking aliasing and separation in higher-order functional programs
  • Graph IRs for Impure Higher-Order Languages: Making Aggressive Optimizations Affordable with Precise Effect Dependencies cites Polymorphic Reachability Types: Tracking Freshness, Aliasing, and Separation in Higher-Order Generic Programs
  • Polymorphic Reachability Types: Tracking Freshness, Aliasing, and Separation in Higher-Order Generic Programs cites Graph IRs for Impure Higher-Order Languages: Making Aggressive Optimizations Affordable with Precise Effect Dependencies
  • Polymorphic Reachability Types: Tracking Freshness, Aliasing, and Separation in Higher-Order Generic Programs cites Reachability types: tracking aliasing and separation in higher-order functional programs
Builds onCited byLeft to right by year; the more cited, the higher.

Builds on8

  1. Reachability types: tracking aliasing and separation in higher-order functional programsYuyan Bao, Guannan Wei, Oliver Bracevac, Yuxuan Jiang et al.OOPSLA 2021 · 19 citations
  2. Staging with class: a specification for typed template HaskellNingning Xie, Matthew Pickering, Andres Löh, Nicolas Wu et al.POPL 2022 · 17 citations
  3. Graph IRs for Impure Higher-Order Languages: Making Aggressive Optimizations Affordable with Precise Effect DependenciesOliver Bracevac, Guannan Wei, Songlin Jia, Supun Abeysinghe et al.OOPSLA 2023 · 14 citations
  4. Polymorphic Reachability Types: Tracking Freshness, Aliasing, and Separation in Higher-Order Generic ProgramsGuannan Wei, Oliver Bracevac, Songlin Jia, Yuyan Bao et al.POPL 2024 · 12 citations
  5. Compiling Parallel Symbolic Execution with ContinuationsGuannan Wei, Songlin Jia, Ruiqi Gao, Haotian Deng et al.ICSE 2023 · 10 citations

Related papers

  1. Refined² Environment ClassifiersYuito Murase, Atsushi IgarashiOOPSLA 2026
  2. Mechanised Semantics of Multi-stage ProgrammingKa Wing Li, Maite Kramarz, Ningning Xie, Jeremy YallopOOPSLA 2026 · 1 citation
  3. Contextual MetaML: Syntax and Full AbstractionHaoxuan Yin, Andrzej S. Murawski, C.-H. Luke OngLICS 2026 · 1 citation
  4. Mœbius: metaprogramming using contextual types: the stage where system f can pattern match on itselfJunyoung Jang, Samuel Gélineau, Stefan Monnier, Brigitte PientkaPOPL 2022 · 28 citations
  5. Modal Effect TypesWenhao Tang, Leo White, Stephen Dolan, Daniel Hillerström et al.OOPSLA 2025 · 4 citations
Dusk over the sea between two cliffs drawn in fine vertical lines