一句话:提出 thermometer continuations,用异常与状态从头重放计算来实现定界续体,并借此表达任意 monadic 效果。
定界续体常被视为“所有 monad 之母”,可统一实现任意 monadic 效果,但许多语言并不原生支持。本文展示如何仅用异常和状态实现定界续体,称为 thermometer continuations。传统实现需要捕获计算的中间状态;该方法则从程序开始重放整个计算,并借助一份记录引导重放,使其在到达被捕获点之前执行相同步骤,从而重建该中间状态。作者解释了定界续体与 monadic reflection,并利用 Filinski 构造证明 thermometer continuations 可表达任意 monadic 效果,还给出非确定性的优雅特例,并讨论为何“异常和状态不能宏表达续体”的理论结果不阻止该构造。
Delimited continuations are the mother of all monads! So goes the slogan inspired by Filinski’s 1994 paper, which showed that delimited continuations can implement any monadic effect, letting the programmer use an effect as easily as if it was built into the language. It’s a shame that not many languages have delimited continuations. Luckily, exceptions and state are also the mother of all monads! In this Pearl, we show how to implement delimited continuations in terms of exceptions and state, a construction we call thermometer continuations . While traditional implementations of delimited continuations require some way of ”capturing” an intermediate state of the computation, the insight of thermometer continuations is to reach this intermediate state by replaying the entire computation from the start, guiding it using a recording so that the same thing happens until the captured point. Along the way, we explain delimited continuations and monadic reflection, show how the Filinski construction lets thermometer continuations express any monadic effect, share an elegant special-case for nondeterminism, and discuss why our construction is not prevented by theoretical results that exceptions and state cannot macro-express continuations.
@article{paperbot278,
title = {Capturing the future by replaying the past (functional pearl)},
author = {James Koppel and Gabriel Scherer and Armando Solar-Lezama},
journal = {Proceedings of the ACM on Programming Languages},
volume = {2},
number = {ICFP},
year = {2018},
doi = {10.1145/3236771}
}