Questions about the MonadPlus type class in Haskell and implementations in other languages of the concept it embodies.
Questions tagged [monadplus]
41 questions
91
votes
1 answer
Distinction between typeclasses MonadPlus, Alternative, and Monoid?
The standard-library Haskell typeclasses MonadPlus, Alternative, and Monoid each provide two methods with essentially the same semantics:
An empty value: mzero, empty, or mempty.
An operator a -> a -> a that joins values in the typeclass together:…

00dani
- 1,498
- 15
- 17
76
votes
5 answers
Confused by the meaning of the 'Alternative' type class and its relationship to other type classes
I've been going through the Typeclassopedia to learn the type classes. I'm stuck understanding Alternative (and MonadPlus, for that matter).
The problems I'm having:
the 'pedia says that "the Alternative type class is for Applicative functors…

Matt Fenwick
- 48,199
- 22
- 128
- 192
42
votes
4 answers
Why MonadPlus and not Monad + Monoid?
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.…

mxxk
- 9,514
- 5
- 38
- 46
28
votes
3 answers
Must mplus always be associative? Haskell wiki vs. Oleg Kiselyov
The Haskell wikibook asserts that
Instances of MonadPlus are required to fulfill several rules, just as instances of Monad are required to fulfill the three monad laws. ... The most essential are that mzero and mplus form a monoid.
A consequence…

ben w
- 2,490
- 14
- 19
26
votes
1 answer
What’s an example of a Monad which is an Alternative but not a MonadPlus?
In his answer to the question “Distinction between typeclasses MonadPlus, Alternative, and Monoid?”, Edward Kmett says that
Moreover, even if Applicative was a superclass of Monad, you’d wind up needing the MonadPlus class anyways, because…

Antal Spector-Zabusky
- 36,191
- 7
- 77
- 140
24
votes
3 answers
Monoid vs MonadPlus
I am very new to both Monads and Monoids and recently also learned about MonadPlus. From what I see, Monoid and MonadPlus both provide a type with a associative binary operation and an identity. (I'd call this a semigroup in mathematical parlance.)…

Code-Apprentice
- 81,660
- 23
- 145
- 268
22
votes
2 answers
Appropriate uses of Monad `fail` vs. MonadPlus `mzero`
This is a question that has come up several times for me in the design code, especially libraries. There seems to be some interest in it so I thought it might make a good community wiki.
The fail method in Monad is considered by some to be a wart; a…

jberryman
- 16,334
- 5
- 42
- 83
18
votes
2 answers
What was wrong with Control.MonadPlus.Free?
The free MonadPlus defined as
data Free f a = Pure a | Free (f (Free f a)) | Plus [Free f a]
was removed in free 4.6 with the following remark (changelog):
Removed Control.MonadPlus.Free. Use FreeT f [] instead and the result will be…

Petr
- 62,528
- 13
- 153
- 317
16
votes
3 answers
How to combine and then branch in MonadPlus/Alternative
I recently wrote
do
e <- (Left <$> m) <|> (Right <$> n)
more actions
case e of
Left x -> ...
Right y -> ...
This seems awkward. I know that protolude (and some other packages) define
-- Called eitherP in parser combinator…

dfeuer
- 48,079
- 5
- 63
- 167
16
votes
1 answer
Is there a Codensity MonadPlus that asymptotically optimizes a sequence of MonadPlus operations?
Recently there was a question about the relation between DList <-> [] versus Codensity <-> Free.
This made me think whether there is such a thing for MonadPlus. The Codensity monad improves the asymptotic performance only for the monadic operations,…

Petr
- 62,528
- 13
- 153
- 317
14
votes
3 answers
MonadPlus definition for Haskell IO
I was just writing a quick bit of code, and I wanted to use the guard function in the IO Monad. However, there is no definition of MonadPlus for IO which means that we cannot use guard in IO land. I have seen an example of using the MabyeT…

Robert Massaioli
- 13,379
- 7
- 57
- 73
14
votes
1 answer
If MonadPlus is the "generator" class, then what is the "consumer" class?
A Pipe can be broken into two parts: the generator part (yield) and the consumer part (await).
If you have a Pipe that only uses it's generator half, and only returns () (or never returns), then it can be represented as a "ListT done right". It…

Dan Burton
- 53,238
- 27
- 117
- 198
11
votes
3 answers
How to convert a failed computation to a successful one and vice versa
This might very well be a solution seeking a problem ... if so, I beg your indulgence!
Possible implementation:
class Switch' f where
switch :: f a -> f ()
instance Switch' [] where
switch [] = [()]
switch (_:_) = []
instance Switch'…

Matt Fenwick
- 48,199
- 22
- 128
- 192
9
votes
1 answer
How to encode actions that take monadic arguments with free (or freer) monads?
Most monadic functions take pure arguments and return a monadic value. But there are a few that need also monadic arguments, for example:
mplus :: (MonadPlus m) => m a -> m a -> m a
finally :: IO a -> IO b -> IO a
forkIO :: m () -> m ThreadId
--…

Petr
- 62,528
- 13
- 153
- 317
9
votes
1 answer
What can we do with Alternative but cannot do with Monoid?
I read Why MonadPlus and not Monad + Monoid? and I understand a theoretical difference, but I cannot figure out a practical difference, because for List it looks the same.
mappend [1] [2] == [1] <|> [2]
Yes. Maybe has different…

ais
- 2,514
- 2
- 17
- 24