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
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…