Why MonadPlus and not Monad + Monoid?

前端 未结 3 1068
余生分开走
余生分开走 2020-12-13 23:58

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

3条回答
  •  遥遥无期
    2020-12-14 00:45

    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.

提交回复
热议问题