Why MonadPlus and not Monad + Monoid?

前端 未结 3 1078
余生分开走
余生分开走 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:38

    Your guard' does not match your Monoid m a type.

    If you mean Monoid (m a), then you need to define what mempty is for m (). Once you've done that, you've defined a MonadPlus.

    In other words, MonadPlus defines two opeartions: mzero and mplus satisfying two rules: mzero is neutral with respect to mplus, and mplus is associative. This satisfies the definition of a Monoid so that mzero is mempty and mplus is mappend.

    The difference is that MonadPlus m is a monoid m a for any a, but Monoid m defines a monoid only for m. Your guard' works because you only needed m to be a Monoid only for (). But MonadPlus is stronger, it claims m a to be a monoid for any a.

提交回复
热议问题