在分类术语中,FP中的monad是什么? [英] What is a monad in FP, in categorical terms?

查看:123
本文介绍了在分类术语中,FP中的monad是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当有人承诺解释单子时,我的兴趣就会激化,只有当所谓的解释是一系列由一些副手所断言的例子,即数学理论背后的挫折时,才会被挫败所取代深奥的想法在这一点上太复杂了。

现在我要求的是相反的观点。我对类别理论有着坚实的把握,我不害怕追图,Yoneda的引理或派生的函子(实际上在 monads 和类别意义上的辅助函数中)。

有人能给我一个关于函数式编程中monad的清晰简洁的定义吗?例子越少越好:有时一个清晰的概念说明了超过一百个胆小的例子。哈斯克尔作为演示语言会很好,但我并不挑剔。

为了卡尔的回答,在Haskell中(理论上)是这样的:

  class Monad m其中
join :: m(ma) - > m a
return :: a - > m a
fmap ::(a - > b) - > m a - > mb

注意bind(>> =

  x>>> = f = join(fmap fx)
可以被定义为

根据 Haskell Wiki


类别 C 中的monad是一个三元组 :C→C,η: Id F ,μ: F em> F


...有一些公理。对于Haskell, fmap return join 排队分别与 F ,η和μ。 (Haskell中的 fmap 定义了一个Functor)。如果我没有弄错,Scala会调用这些 map 纯和 join 分别。 (Scala调用绑定flatMap)


Every time someone promises to "explain monads", my interest is piqued, only to be replaced by frustration when the alleged "explanation" is a long list of examples terminated by some off-hand remark that the "mathematical theory" behind the "esoteric ideas" is "too complicated to explain at this point".

Now I'm asking for the opposite. I have a solid grasp on category theory and I'm not afraid of diagram chasing, Yoneda's lemma or derived functors (and indeed on monads and adjunctions in the categorical sense).

Could someone give me a clear and concise definition of what a monad is in functional programming? The fewer examples the better: sometimes one clear concept says more than a hundred timid examples. Haskell would do nicely as a language for demonstration though I'm not picky.

解决方案

As a compliment to Carl's answer, a Monad in Haskell is (theoretically) this:

class Monad m where
  join :: m (m a) -> m a
  return :: a -> m a
  fmap :: (a -> b) -> m a -> m b

Note that "bind" (>>=) can be defined as

x >>= f = join (fmap f x)

According to the Haskell Wiki

A monad in a category C is a triple (F : C → C, η : IdF, μ : FFF)

...with some axioms. For Haskell, fmap, return, and join line up with F, η, and μ, respectively. (fmap in Haskell defines a Functor). If I'm not mistaken, Scala calls these map, pure, and join respectively. (Scala calls bind "flatMap")

这篇关于在分类术语中,FP中的monad是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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