arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2602.19951cs.PL
收藏

驯服渐进式命令式元编程中的作用域外泄

Taming Scope Extrusion in Gradual Imperative Metaprogramming

Indiana University(印第安纳大学);Meta

Tianyu Chen, Darshal Shetty, Jeremy G. Siek, Chao-Hong Chen, Weixi Ma, Arnaud Venet, Rocky Liu

AI总结:

本文提出首个支持可变引用的渐进元编程语言,通过动态执行环境分类器纪律确保作用域安全。

摘要:

元编程能够生成高效的代码,而渐进类型则有助于从无类型脚本平稳迁移到稳固的静态类型程序。然而,将这些特性与命令式状态——特别是可变引用——结合在一起,又重新引入了经典的问题,即作用域外泄,其中包含自由变量的代码片段逃逸出其定义的词法上下文。虽然静态类型系统利用环境分类器已经成功地驯服了这种交互,但在渐进语言中强制这些不变式仍是一个开放的挑战。本文提出了$λ^{α,\star}_{\text{Ref}}$,这是首个支持可变引用的渐进元编程语言,同时保证作用域安全。为了为$λ^{α,\star}_{\text{Ref}}$奠定坚实的基础,我们还开发了其静态类型姐妹语言$λ^α_{\text{Ref}}$,该语言引入了无限制的环境分类器子类型化。然而,我们的关键创新是动态执行环境分类器纪律在$λ^{α,\star}_{\text{Ref}}$中,使语言能够介于静态验证的作用域和动态验证的作用域之间。动态执行通过一种新的类型转换算术$\mathrm{CC}^{α,\star}_{\text{Ref}}$实现,该算术使用了Henglein的强制算术的扩展来处理代码类型、分类器多态性和子类型约束。我们证明了$λ^{α,\star}_{\text{Ref}}$满足类型安全和作用域安全。最后,我们提供了一种空间高效的实现策略用于动态作用域检查,确保运行时开销保持实用。

英文摘要:

Metaprogramming enables the generation of performant code, while gradual typing facilitates the smooth migration from untyped scripts to robust statically typed programs. However, combining these features with imperative state - specifically mutable references - introduces the classic peril of scope extrusion, where code fragments containing free variables escape their defining lexical context. While static type systems have employed environment classifiers to successfully tamed this interaction, enforcing these invariants in a gradual language remains an open challenge. This paper presents $λ^{α,\star}_{\text{Ref}}$, the first gradual metaprogramming language that supports mutable references while guaranteeing scope safety. To put $λ^{α,\star}_{\text{Ref}}$ on a firm foundation, we also develop its statically typed sister language, $λ^α_{\text{Ref}}$, which introduces unrestricted subtyping for environment classifiers. We mechanize the proof of scope safety for $λ^{α,\star}_{\text{Ref}}$, contributing the first mechanized proof of scope safety for a statically typed language. Our key innovation, however, is the dynamic enforcement of the environment classifier discipline in $λ^{α,\star}_{\text{Ref}}$, enabling the language to mediate between statically verified scopes and dynamically verified scopes. The dynamic enforcement is carried out in a novel cast calculus $\mathrm{CC}^{α,\star}_{\text{Ref}}$ that uses an extension of Henglein's Coercion Calculus to handle code types, classifier polymorphism, and subtype constraints. We prove that $λ^{α,\star}_{\text{Ref}}$ satisfies type safety and scope safety. Finally, we provide a space-efficient implementation strategy for the dynamic scope checks, ensuring that the runtime overhead remains practical. All of our results are fully mechanized in Agda.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑