问题描述
monad是一种数学结构,基本上在(纯)函数式编程中使用,基本上是Haskell.但是,还有许多其他数学结构可用,例如,应用函子,强单子或单半体.有些具有更具体的含义,有些是更通用的.然而,单子更受欢迎.为什么会这样?
我想出的一个解释是,它们是通用性和特异性之间的最佳结合点.这意味着monad可以捕获有关数据的足够假设,以应用我们通常使用的算法,并且我们通常拥有的数据满足monadic定律.
另一种解释可能是Haskell提供了monad的语法(do-notation),而不提供其他结构的语法,这意味着Haskell程序员(以及函数式编程研究人员)被直观地吸引到monad上,而monad则更通用或更具体(有效) )功能也可以正常工作.
我怀疑这个特殊类型的类(Monad
)与其他许多类相比所受到的不成比例的大量关注主要是历史上的uke幸.人们经常将IO
与Monad
相关联,尽管两者是独立有用的想法(列表反转和香蕉).因为IO
是神奇的(具有实现但没有符号),并且Monad
通常与IO
关联,所以很容易陷入关于Monad
的神奇思考.(此外:IO
甚至不是单子还是个问题.单子法则是否成立?法律对IO
的法则甚至是平均值是什么,即相等是什么意思?请注意.)
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?
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.
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.
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
.
(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有什么特别之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!