paperbot · PL 论文追踪

全部论文 待读 收藏

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

分阶段注解何时保持语义?机械化带 let 插入的类型化语义保持多阶段编程
OOPSLA 10(OOPSLA2)2026匹配度 2/10 · 论文属于程序语言理论中的多阶段编程、类型系统与机械化语义保持证明,与用户核心关注的静态分析、LLM/Agent 交叉、浮点程序分析和贝叶斯程序分析无直接交集;仅与形式化验证/证明工程有较远距离的弱关联。引用 0 inbox
Jun Tan, Guannan Wei
crossref: 2026-10-02

AI 速览

一句话:提出两种带效应追踪的两阶段演算,以自动 let 插入保持语义,并机械化证明分阶段程序与其去阶段化版本上下文等价。

多阶段编程允许程序员将代码当作数据,在生成期对代码片段做语法操作,但这种操作会微妙地改变求值顺序,使分阶段程序与常作为参考实现的去阶段化程序语义不一致,既妨碍正确性推理,也使其无法直接替换去阶段化版本。本文研究带语义保持保证的多阶段语言设计,提出两个静态类型的两阶段演算 λ|2| 与 λ|2|^ref,后者在第二阶段支持可变引用。其动态语义对自动 let 插入建模,并通过轻量级类型-效应系统把插入追踪为控制效应,从而对带效应的代码片段进行类型安全且保持语义的操作。作者用二元逻辑关系证明强语义保持定理:若良类型的两阶段程序 t1 求值为代码值 t2,则 t2 与 t1 的去阶段化结果上下文等价。该工作机械化验证了演算的元理论,回答了 Inoue 与 Taha 关于分阶段注解何时保持语义的问题,并为语义保持的多阶段编程奠定基础。

问题
在带引用/拼接的类型化多阶段语言中,对代码片段做语法操作会暗中改变求值顺序,导致分阶段程序与其去阶段化参考实现语义不一致,妨碍正确性推理与可替换性。
方法
设计两个静态类型两阶段演算 λ|2| 与 λ|2|^ref(后者支持第二阶段可变引用),用类型-效应系统将自动 let 插入追踪为控制效应,并借助二元逻辑关系证明强语义保持定理。
评估
机械化演算及元理论,并证明:若良类型两阶段程序 t1 求值为值代码 t2,则 t2 与 t1 的去阶段化在上下文等价意义下相同,以此回答 Inoue 与 Taha 的问题。
原文摘要(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 𝜆|2|^ref, 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 well-typed two-stage program t1 evaluates to a value code t2, then t2 is contextually equivalent to the stage-erasure of t1. 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.

链接与引用

DOI 原文 · PDF(开放获取) ·

BibTeX
@article{paperbot12108,
  title = {When Do Staging Annotations Preserve Semantics? Mechanizing Typed Semantics-Preserving Multi-stage Programming with Let-Insertion},
  author = {Jun Tan and Guannan Wei},
  journal = {Proceedings of the ACM on Programming Languages},
  volume = {10},
  number = {OOPSLA2},
  year = {2026},
  doi = {10.1145/3839477}
}