异常一元错误处理
Exceptionally Monadic Error Handling
摘要:
我们注意到catch::c a ->(e -> c a)-> c a操作符的类型是一元绑定操作符(>>=)::m a ->(a -> m b)-> m b的特殊情况,语义(令人惊讶地)匹配,并且这种观察有许多有趣的结果。例如,读者可能意识到错误单子$λA.E \lor A$的(>>=)运算符的单子本质是对“正常”值表现得像恒等单子,并在“错误”时停止。不被理解的事实是,用“翻转的”“联合的”错误单子$λE.E \lor A$的捕获运算符处理所述“错误”也是一个单子计算,其将仍然未处理的“错误”视为“正常”值,并且在“错误”最终被处理时停止。我们表明,对于一个适当的索引类型的计算,这样的“连体”结构自然遵循传统的操作语义的抛出和捕获操作符。因此,我们表明,这种结构统一概括了所有传统的一元错误处理机制,我们知道。我们还展示了几个更有趣的实例,这种结构,其中至少双索引的一元解析器组合子和传统的例外,通过延续实现有直接的实际应用。最后,我们注意到,这些意见提供了令人惊讶的角度错误处理一般和指向一个很大程度上未探索的线索在程序设计语言设计空间。
英文摘要:
We notice that the type of catch :: c a -> (e -> c a) -> c a operator is a special case of monadic bind operator (>>=) :: m a -> (a -> m b) -> m b, the semantics (surprisingly) matches, and this observation has many interesting consequences. For instance, the reader is probably aware that the monadic essence of the (>>=) operator of the error monad $λA.E \lor A$ is to behave like identity monad for "normal" values and to stop on "errors". The unappreciated fact is that handling of said "errors" with a catch operator of the "flipped" "conjoined" error monad $λE.E \lor A$ is, too, a monadic computation that treats still unhandled "errors" as "normal" values and stops when an "error" is finally handled. We show that for an appropriately indexed type of computations such a "conjoined" structure naturally follows from the conventional operational semantics of throw and catch operators. Consequently, we show that this structure uniformly generalizes all conventional monadic error handling mechanisms we are aware of. We also demonstrate several more interesting instances of this structure of which at least bi-indexed monadic parser combinators and conventional exceptions implemented via continuations have immediate practical applications. Finally, we notice that these observations provide surprising perspectives on error handling in general and point to a largely unexplored trail in programming language design space.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。