I was wondering, in the MonadPlus documentation it says that: * mzero is the identity of mplus (and some extra conditions) * mplus is an associative operation While for Monoid we have: * mempty is identity of mappend * mappend is an associative operation MonadPlus is of course a 'stronger' assertion. But why is not every instance of MonadPlus also an instance of Monoid?
instance MonadPlus m => Monoid (m a) where mempty = mzero mappend = mplus
The only type that is an instance of both is [a]. But I see no reason why it there should not be a Monoid instance for other MonadPlus types. In particular, an instance for Maybe could be useful with a writer monad when you are only interested in the first result. Twan
Am Montag, 9. Januar 2006 19:42 schrieb Twan van Laarhoven:
[...]
But why is not every instance of MonadPlus also an instance of Monoid?
instance MonadPlus m => Monoid (m a) where mempty = mzero mappend = mplus
I think that this is not legal Haskell 98.
[...]
Best wishes, Wolfgang
On 09/01/06, Twan van Laarhoven <twanvl@gmail.com> wrote:
I was wondering, in the MonadPlus documentation it says that: * mzero is the identity of mplus (and some extra conditions) * mplus is an associative operation While for Monoid we have: * mempty is identity of mappend * mappend is an associative operation
MonadPlus is of course a 'stronger' assertion. But why is not every instance of MonadPlus also an instance of Monoid?
instance MonadPlus m => Monoid (m a) where mempty = mzero mappend = mplus
The only type that is an instance of both is [a]. But I see no reason why it there should not be a Monoid instance for other MonadPlus types. In particular, an instance for Maybe could be useful with a writer monad when you are only interested in the first result.
Good idea, I agree. The fact that it's not Haskell 98 doesn't seem to be too much of a concern, seeing as the major use of Monoid right now is in conjunction with the MonadWriter class, and that's not expressible in Haskell 98 either. The only trouble with this is that there may be a case where you have a MonadPlus type which is a monoid in a way which is separate from the way given above, and you want that instance instead. I can't really think of a good example of that happening though. - Cale
participants (3)
-
Cale Gibbard -
Twan van Laarhoven -
Wolfgang Jeltsch