Monads 有什么特别之处? [英] What is so special about Monads?

查看:25
本文介绍了Monads 有什么特别之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

monad 是一种数学结构,在(纯)函数式编程中大量使用,基本上是 Haskell.然而,还有许多其他数学结构可用,例如应用函子、强单子或幺半群.有些更具体,有些更通用.然而,单子更受欢迎.这是为什么?

A monad is a mathematical structure which is heavily used in (pure) functional programming, basically Haskell. However, there are many other mathematical structures available, like for example applicative functors, strong monads, or monoids. Some have more specific, some are more generic. Yet, monads are much more popular. Why is that?

我想出的一个解释是,它们介于通用性和特异性之间.这意味着 monad 捕获了足够多的关于数据的假设,以应用我们通常使用的算法,并且我们通常拥有的数据满足 monadic 定律.

One explanation I came up with, is that they are a sweet spot between genericity and specificity. This means monads capture enough assumptions about the data to apply the algorithms we typically use and the data we usually have fulfills the monadic laws.

另一种解释可能是 Haskell 为 monad 提供了语法(do-notation),但没有为其他结构提供语法,这意味着 Haskell 程序员(以及因此函数式编程研究人员)直观地被 monad 所吸引,其中更通用或特定(高效) 函数也可以.

Another explanation could be that Haskell provides syntax for monads (do-notation), but not for other structures, which means Haskell programmers (and thus functional programming researchers) are intuitively drawn towards monads, where a more generic or specific (efficient) function would work as well.

推荐答案

我怀疑对这一特定类型类 (Monad) 的过多关注主要是历史上的侥幸.人们经常将 IOMonad 联系起来,尽管这两者是独立有用的想法(列表反转和香蕉).因为 IO 很神奇(有实现但没有外延)而且 Monad 经常与 IO 联系在一起,所以很容易陷入关于 IO 的神奇思维代码>Monad.

I suspect that the disproportionately large attention given to this one particular type class (Monad) over the many others is mainly a historical fluke. People often associate IO with Monad, although the two are independently useful ideas (as are list reversal and bananas). Because IO is magical (having an implementation but no denotation) and Monad is often associated with IO, it's easy to fall into magical thinking about Monad.

(旁白:IO 是否甚至是一个 monad 是有问题的.monad 法则是否成立?对于 IO 来说,这些法则甚至意味着什么, 即相等是什么意思?注意 与状态单子有问题的关联.)

(Aside: it's questionable whether IO even is a monad. Do the monad laws hold? What do the laws even mean for IO, i.e., what does equality mean? Note the problematic association with the state monad.)

这篇关于Monads 有什么特别之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆