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

栈操作计算的概念与相对单子(扩展版)

Notions of Stack-manipulating Computation and Relative Monads (Extended Version)

Yuchen Jiang, Runze Xue, Max S. New

AI总结:

本文将函数式编程的单子接口忠实适配至显式栈操作的底层环境,利用多态 CBPV 演算与范畴论中的相对单子建模计算效应,并提出单子块与自动扩展的相对单子变换器以增强代码抽象与组合能力。

摘要:

单子为函数式编程语言中的用户自定义计算效应提供了一个简单且简洁的接口。这使得对效应进行等式推理、对单子接口进行抽象,以及开发单子变换器栈以组合不同效应成为可能。编译器实现者和汇编代码程序员同样会对效应进行虚拟化,如果可能的话,他们也会从类似的抽象中受益。然而,效应的实现细节似乎与高层单子接口脱节:在较低的层次上,大部分设计在于运行时栈的布局,而这在高级编程语言中是无法访问的。我们证明了单子接口可以忠实地从高级函数式编程适配到具有显式栈操作的较低层设置中。我们使用多态的按值调用推入(CBPV)演算作为一种捕获栈操作本质的设置,其类型系统允许程序定义特定领域的栈结构。在此设置中,我们展示了现有的相对单子的范畴论概念可用于对计算效应的基于栈的实现进行建模。为了证明其通用性,我们将各种标准单子适配为相对单子。此外,我们展示了栈操作程序可以从一种我们称之为“单子块”的 do-表示法的泛化中受益,该泛化允许所有 CBPV 代码被重新解释以与任意相对单子一起工作。作为一项应用,我们展示了所有相对单子都会自动扩展为相对单子变换器,这一过程在纯语言中的单子并非自动完成的。

英文摘要:

Monads provide a simple and concise interface to user-defined computational effects in functional programming languages. This enables equational reasoning about effects, abstraction over monadic interfaces and the development of monad transformer stacks to combine different effects. Compiler implementors and assembly code programmers similarly virtualize effects, and would benefit from similar abstractions if possible. However, the implementation details of effects seem disconnected from the high-level monad interface: at this lower level much of the design is in the layout of the runtime stack, which is not accessible in a high-level programming language. We demonstrate that the monadic interface can be faithfully adapted from high-level functional programming to a lower level setting with explicit stack manipulation. We use a polymorphic call-by-push-value (CBPV) calculus as a setting that captures the essence of stack-manipulation, with a type system that allows programs to define domain-specific stack structures. Within this setting, we show that the existing category-theoretic notion of a relative monad can be used to model the stack-based implementation of computational effects. To demonstrate generality, we adapt a variety of standard monads to relative monads. Additionally, we show that stack-manipulating programs can benefit from a generalization of do-notation we call "monadic blocks" that allow all CBPV code to be reinterpreted to work with an arbitrary relative monad. As an application, we show that all relative monads extend automatically to relative monad transformers, a process which is not automatic for monads in pure languages.

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

↑