为什么选择MonadPlus而不是Monad + Monoid? [英] Why MonadPlus and not Monad + Monoid?

查看:89
本文介绍了为什么选择MonadPlus而不是Monad + Monoid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解 MonadPlus 背后的动机。为什么如果已经存在类型类 Monad Monoid



当然, Monoid 的实例是具体类型,而 Monad 的实例需要单个类型参数。 (请参阅 Monoid vs MonadPlus ,以获得有用的解释。)但是难道你不能重写任何类型的

 (MonadPlus m)=> ... 

作为 Monad Monoid

 (Monad m,Monoid(ma))= > ... 

guard 函数从例如, Control.Monad 。它的实现是:

  guard ::(MonadPlus m)=>布尔 - > m()
guard True = return()
guard False = mzero

我能够仅使用 Monad Monoid 来实现它:

  guard'::(Monad m,Monoid(m()))=>布尔 - > m()
guard'True = return()
guard'False = mempty



<有人可以澄清 MonadPlus Monad + Monoid

解决方案


但是难道你不能重写

 (MonadPlus m)=> ... 

作为Monad和Monoid的组合吗?


blockquote>

没有。在你链接的问题的顶部答案中,关于MonadPlus与Monoid的规律已经有了很好的解释。但即使我们忽略了类型类法则也有不同。

Monoid(m a)=> ... 意味着 ma 必须是一个特殊的 a 调用者,但 MonadPlus m 意味着 ma 必须是所有 a 。所以 MonadPlus a 更加灵活,这种灵活性在以下四种情况下有帮助:


  1. 如果我们不想告诉调用者我们打算使用什么 a >。
    MonadPlus m = > ... 而不是 Monoid(m SecretType)=> ...


  2. 如果我们想使用多个不同的 a

    MonadPlus m => ... 而不是(Monoid(m Type1),Monoid(m Type2),...)=> ...


  3. 如果我们想要使用无限多个不同的 a
    MonadPlus m => ... 而不是不可能。

  4. c $ c>我们需要。
    MonadPlus m => ... 而不是不可能。


I'm trying to understand the motivation behind the MonadPlus. Why is it necessary if there are already the typeclasses Monad and Monoid?

Granted, instances of Monoid are concrete types, whereas instances of Monad require a single type parameter. (See Monoid vs MonadPlus for a helpful explanation.) But couldn't you rewrite any type constraint of

(MonadPlus m) => ...

as a combination of Monad and Monoid?

(Monad m, Monoid (m a)) => ...

Take the guard function from Control.Monad, for example. Its implementation is:

guard :: (MonadPlus m) => Bool -> m ()
guard True = return ()
guard False = mzero

I was able to implement it using only Monad and Monoid:

guard' :: (Monad m, Monoid (m ())) => Bool -> m ()
guard' True = return ()
guard' False = mempty

Could someone please clarify the real difference between MonadPlus and Monad + Monoid?

解决方案

But couldn't you rewrite any type constraint of

(MonadPlus m) => ...

as a combination of Monad and Monoid?

No. In the top answer to the question you link, there is already a good explanation about the laws of MonadPlus vs. Monoid. But there are differences even if we ignore the typeclass laws.

Monoid (m a) => ... means that m a has to be a monoid for one particular a chosen by the caller, but MonadPlus m means that m a has to be a monoid for all a. So MonadPlus a is more flexible, and this flexibility is helpful in four situations:

  1. If we don't want to tell the caller what a we intend to use.
    MonadPlus m => ... instead of Monoid (m SecretType) => ...

  2. If we want to use multiple different a.
    MonadPlus m => ... instead of (Monoid (m Type1), Monoid (m Type2), ...) => ...

  3. If we want to use infinitely many different a.
    MonadPlus m => ... instead of not possible.

  4. If we don't know what a we need. MonadPlus m => ... instead of not possible.

这篇关于为什么选择MonadPlus而不是Monad + Monoid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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