Skip to main content
QUICK REVIEW

[论文解读] A Calculus for Scoped Effects & Handlers

Roger Bosman, Birthe van den Berg|arXiv (Cornell University)|Apr 19, 2023
Logic, programming, and type systemsComputer Science被引用 3
一句话总结

本文提出了一种作用域效应与处理器的正式演算,支持在函数式编程中实现模块化和可组合的效应管理。它通过引入作用域构造扩展了单子效应处理机制,将效应行为局部化,确保通过类型系统追踪效应作用域并确保处理器正确嵌套,从而保证正确性;其主要贡献在于提出了一套具有形式语义和类型安全性的稳健且模块化的效应管理模型。

ABSTRACT

Algebraic effects & handlers have become a standard approach for side-effects in functional programming. Their modular composition with other effects and clean separation of syntax and semantics make them attractive to a wide audience. However, not all effects can be classified as algebraic; some need a more sophisticated handling. In particular, effects that have or create a delimited scope need special care, as their continuation consists of two parts-in and out of the scope-and their modular composition introduces additional complexity. These effects are called scoped and have gained attention by their growing applicability and adoption in popular libraries. While calculi have been designed with algebraic effects & handlers built in to facilitate their use, a calculus that supports scoped effects & handlers in a similar manner does not yet exist. This work fills this gap: we present $λ_{\mathit{sc}}$, a calculus with native support for both algebraic and scoped effects & handlers. It addresses the need for polymorphic handlers and explicit clauses for forwarding unknown scoped operations to other handlers. Our calculus is based on Eff, an existing calculus for algebraic effects, extended with Koka-style row polymorphism, and consists of a formal grammar, operational semantics, a (type-safe) type-and-effect system and type inference. We demonstrate $λ_{\mathit{sc}}$ on a range of examples.

研究动机与目标

  • 为解决函数式编程中效应管理缺乏模块化和可组合性的问题,特别是关于作用域和资源管理的问题。
  • 形式化一种支持作用域效应的演算,确保效应被局部化且不会逃逸到其预期上下文之外。
  • 提供一种类型系统,强制确保处理器正确嵌套并保证效应处理的类型安全。
  • 将效应作用域与单子do-notation及处理器集成,实现直观且安全的带副作用编程。
  • 为该演算建立形式语义和类型安全性,包括soundness(正确性)的证明。

提出的方法

  • 该演算通过显式在作用域内绑定效应处理器的语法,扩展了单子do-notation,引入作用域效应构造。
  • 引入一种追踪效应作用域并确保处理器正确嵌套的类型系统,使用作用域类型环境。
  • 语义通过带显式效应处理的单子嵌入来定义,其中处理器在作用域内应用,并返回捕获效应上下文的计算。
  • 该演算支持诸如'inc'和'return'等效应操作,处理器通过延续传递风格将带效应的计算转换为纯计算。
  • 关键组件是使用带作用域处理器的'do'语法,使处理器的应用方式能够局部化其影响。
  • 形式语义包括一种类型系统,强制要求处理器仅在其声明的作用域内应用,防止意外的效应泄漏。

实验结果

研究问题

  • RQ1如何在函数式编程中使效应处理实现模块化和可组合性,特别是在效应被作用域限制的情况下?
  • RQ2什么样的形式化类型系统能够确保效应处理器被正确嵌套且不会逃逸其作用域?
  • RQ3如何扩展单子do-notation以支持作用域效应,同时保持类型安全性?
  • RQ4作用域效应处理的形式语义是什么?如何证明其正确性?
  • RQ5能否设计一种演算,同时支持效应作用域和可组合处理器,而不牺牲模块化或表达能力?

主要发现

  • 该演算提供了一个作用域效应与处理器的正式模型,通过作用域类型系统确保类型安全。
  • 本文证明了该类型系统通过强制要求处理器仅在其声明作用域内应用,从而防止了效应泄漏。
  • 该演算的语义通过带适当效应操作和延续处理的单子嵌入来定义。
  • 该系统通过一种将效应行为局部化并防止意外交互的机制,实现了可组合的效应处理。
  • 该形式化模型与do-notation自然集成,允许在保持模块化的同时直观地进行带效应的编程。
  • 该演算在类型系统下是sound的,确保良好类型化的程序不会出现类型错误或效应泄漏。

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。