选择性应用函子
Selective Applicative Functors

原始链接: https://blog.veritates.love/selective_applicatives_theoretical_basis.html

本文探讨了 monads、applicatives 的理论基础,以及它们与控制流的关系,最终将它们与 `Alternative` functors 和解析联系起来。文章认为,理解这些概念需要超越简单的定义,深入到范畴论——特别是将 monads 和 applicatives 视为特定函子范畴中的 monoid。 一个关键点是这些范畴内组合发生的*方式*的重要性。Applicatives 使用“对称化”组合(Day convolution),而 monads 依赖于标准组合。这引出了“箭头变换器”和自由构造的讨论,用于表示控制流,最终形成一个 `ControlFlow` 数据类型。 作者随后将此与 `Alternative` functors(具有 `<|>` 运算符用于非确定性选择)联系起来,并展示了在特定条件下如何使用 `<|>` 和 `mapMaybe` 表达确定性、有限分支(如 `CaseTree`)。虽然 `Alternative` 代表非确定性分支,但 `Selective` applicatives 需要基于箭头的方案来有效地建模确定性控制流。文章最后得出结论,这两种结构既相关又不同,`Alternative` 倾向于通过近半环结构进行静态分析,而 `Selective` 则需要箭头来实现高效的确定性分支。

## 选择性函子:摘要 最近的 Hacker News 讨论集中在“选择性函子”概念上,旨在弥合函数式编程中 Applicative 函子和 Monad 之间的差距。核心思想是创建一个比 Applicative 更强大,但不如 Monad 的类型类,允许在固定结构内进行条件计算。 本质上:Applicative 具有没有条件逻辑的固定计算图;选择性 Applicative 为该固定图添加条件“分支”;而 Monad 允许完全动态的计算图,并具有运行时控制流。 其动机源于实际需求,例如优化数据库查询(受 Facebook 的 Haxl 库启发),可以在运行时结果的基础上做出预定义的选择,从而避免完全动态的 Monad 方法的开销。这允许进行静态分析和可能的优化,而 Monad 的任意回调则无法实现。 然而,原文被认为高度抽象且难以理解,即使对于经验丰富的函数式程序员来说也是如此。评论者建议更具体的解释,从实际示例开始,然后再深入到理论形式化,将提高可访问性。
相关文章

原文

To return to the theoretical considerations.

The free monad gets a lot of attention.

Well, one reason is that it requires the Day convolution, which requires existential types. And we have already established that programmers avoid thinking about existential types.

change of enriching category (via f being a lax monoidal functor), even though we are only going from Set (which is itself a Set-enriched category) back to a different Set-enriched category (whose arrows are f (i -> r)).

near-semiring.)

observeManyT, to only obtain the first few results.

Still, the common theme is that to obtain even one successful result, backtracking deeply through the computation may be necessary.

Because they are both monads, they support: f (Maybe x) -> f x, via (_ >>= maybe empty pure). This operation makes sense in some applicatives, too, but it needs a specialized implementation. So generally we will talk about mapMaybe :: (x -> Maybe y) -> f x -> f y.

Indeed, we want to talk about structures on applicative functors simply because ParserT and LogicT have no capability for static analysis, as they are monads. So can we combine mapMaybe and <|> to get an explanation of select, branch, and so on?

The answer is yes: If we have an applicative parser without other side effects (i.e. m = Identity) and with enough backtracking, we could encode determined choice via nondeterministic choice.

From monoids to near-semirings: the essence of MonadPlus and Alternative by Exequiel Rivas, Mauro Jaskelioff, and Tom Schrijvers explains the typeclass laws, free constructions, and Cayley constructions (à la difference lists) from this perspective.

So the conclusion is that the branching structures of Alternative and Selective are closely related, but not identical. The Alternative can be thought of as a functor with nondeterministic branching, while Selective needs to be explained via arrows to encode finite deterministic branching. They both allow retaining the structure of branches as independent from the sequential structure, and the selective structure can be encoded via <|> and mapMaybe in some well-behaved cases.

联系我们 contact @ memedata.com