Questions about the MonadPlus type class in Haskell and implementations in other languages of the concept it embodies.
Questions tagged [monadplus]
41 questions
8
votes
2 answers
Use list monad inside monad transformer type classes?
My goal is to create a function that uses the list monad inside either a ReaderT WriterT stack, or a RWS stack. More generally, how do I use the list monad inside mtl typeclasses such as MonadReader, MonadWriter?
Why am I trying to do this? This…

Daniel K
- 887
- 6
- 13
6
votes
1 answer
An alternative Alternative for lists
A few times now, I've found myself defining:
(>) :: [a] -> [a] -> [a]
[] > ys = ys
xs > _ = xs
This is an associative operation, of course, and the empty list [] is both left- and right-identity. It functions like Python's or.
It seems to me…

Khuldraeseth na'Barya
- 720
- 1
- 7
- 30
6
votes
2 answers
What is the default type evaluation of MonadPlus in Haskell?
I have the following code:
import Control.Monad
coin :: MonadPlus m => m Int
coin = return 0 `mplus` return 1
If I evaluate coin :: Maybe Int on the interpreter, it prits Just 0. That's normal because of the implementation of Maybe as instance of…

ManuelVS
- 125
- 6
6
votes
1 answer
Why `guard` is not defined with a MonadPlus constraint?
I am currently reading about the Alternative/MonadPlus typeclasses in wikibooks. It describes the difference very well. However, one puzzling part is the guard function, which I am assuming, is used for "short-circuiting" a computation. (Am I…

zeronone
- 2,912
- 25
- 28
6
votes
1 answer
Control.MonadPlus.Free without unnecessary distribution
I'm trying to use a free monad to build an EDSL for constructing AND/OR decision trees like Prolog, with >>= mapped to AND, and mplus mapped to OR. I want to be able to describe something like A AND (B OR C) AND (D OR E), but I don't want…

pat
- 12,587
- 1
- 23
- 52
6
votes
1 answer
Is this Haskell function known/implemented as another name?
I find a lot of things I jigger together on my own that seem at all useful actually have a standard implementation that I just didn't know about, so was curious if anybody could say they've seen this type of thing used before:
It takes a monadic…

Jimmy Hoffa
- 5,909
- 30
- 53
5
votes
1 answer
Is there a name for "maybe mzero return"?
Is there an established name for maybe mzero return?
It has the type:
MonadPlus m => Maybe a -> m a
and converts Nothing to failure and Just a to return a.

ErikR
- 51,541
- 9
- 73
- 124
4
votes
2 answers
Requires MonadPlus (ST a) Instance
I'm reading the paper Typed Logical Variables in Haskell, but I'm failing to understand the details of the ultimate implementation. In particular, the backtracking state transformer introduced in section 4. For some reason, unknown to me, GHC…

emi
- 5,380
- 1
- 27
- 45
4
votes
3 answers
Obtaining a Bool out of [Maybe Bool] which is guaranteed to contain at least one Just
I have an input list of type [Maybe SomeType] and a predicate p of type SomeType -> Bool, and I want to answer the question "Does the predicate p hold for all SomeTypes that happen to be in the input?".
The first part is easy: (map . fmap) p list…

Enlico
- 23,259
- 6
- 48
- 102
3
votes
2 answers
Haskell: Is (MonadPlus m => Bool -> a -> m a) not useful?
I was inspired to play with FizzBuzz after taking a gander at codepad.org, and found myself wanting some function:
mwhen :: MonadPlus m => Bool -> a -> m a
mwhen b = if b then return else const mzero
just so I could do mwhen (n /? 3) "Foo"…

rampion
- 87,131
- 49
- 199
- 315
3
votes
1 answer
MonadPlus IO isn't a monoid
Instance MonadPlus IO is unique because mzero throws:
Prelude Control.Monad> mzero
*** Exception: user error (mzero)
So accordingly, MonadPlus IO implies that it is also intended for errors.
mzero apparently serves as the identity element if the…

Dannyu NDos
- 2,458
- 13
- 32
3
votes
1 answer
Non-Deterministic Merge Sort Doesn't Order Permutations Lexicographically
I've been trying to reproduce an aside mentioned in All Sorts of Permutations (Functional Pearl) by Christiansen, Danilenko and Dylus, a paper for the upcoming ICFP 2016. Section 8 (“Final Remarks”) claims that by choosing a particular…

R B
- 1,109
- 9
- 13
3
votes
1 answer
Haskell parser, Monad and MonadPlus
module Parser where
import Control.Monad (MonadPlus, mplus, mzero)
import Tagger (Tag, Token)
newtype Parser a = Parser ([(Token, Tag)] -> [(a, [(Token, Tag)])])
parse :: Parser a -> [(Token, Tag)] -> [(a, [(Token,…

katyp
- 29
- 4
3
votes
1 answer
MonadPlus instance for Control.Eff when Exc is member
In monad transformers, we have
instance (Monad m, Monoid e) => MonadPlus (ExceptT e m)
In extensible effects, there is no such thing as
instance (Monoid e) => MonadPlus (Eff (Exc e :> r))
I've tried implementing it, in vain. Here is what I have so…

koral
- 442
- 3
- 11
3
votes
1 answer
Defining MonadPlus instance for a transformer on top of ErrorT
I would like define a monad transformer, that, among other things, endows a base monad with error functionality. The transformed monad should be an instance of MonadPlus if the base monad is, but I can't figure out how to define the MonadPlus…

Eyal
- 1,094
- 10
- 16