Questions tagged [monadplus]

Questions about the MonadPlus type class in Haskell and implementations in other languages of the concept it embodies.

41 questions
3
votes
1 answer

How to restrict backtracking in a monad transformer parser combinator

tl;dr, How do I implement parsers whose backtracking can be restricted, where the parsers are monad transformer stacks? I haven't found any papers, blogs, or example implementations of this approach; it seems the typical approach to restricting…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
2
votes
2 answers

Haskell State monad and monadic guard

i have small task to emulate imperative loop in monadic code with state involved and there should be no IO, the task is to exit loop on condition and here is my attempt: > execState (forever $ modify (+1) >>= \x -> guard $ x < 5 ) 1 So i expect to…
Dfr
  • 4,075
  • 10
  • 40
  • 63
2
votes
1 answer

Why do safe partial functions use Maybe instead of generalizing to any Monad

I know some people consider fail to be a mistake, and I can see why. (It seems like MonadPlus was made to replace it). But as long as it is there, it seems like it would make sense for partial functions to use fail and pure instead of Just and…
semicolon
  • 2,530
  • 27
  • 37
2
votes
1 answer

How does mzero match guard type signature?

I have trouble understanding how guard works. Why does it type check? Isn't mzero capable of returning some m a where a /= ()? guard :: (MonadPlus m) => Bool -> m () guard True = return () guard False = mzero mzero :: m a
sevo
  • 4,559
  • 1
  • 15
  • 31
2
votes
1 answer

Does Maybe MonadPlus Parsers need to be in certain order?

Im working through the exercises on wikibooks/haskell and there is an exercise in the MonadPlus-chapter that wants you to write this hexChar function. My function works as shown below, but the thing is that when I try to switch the 2 helper parsers…
patriques
  • 5,057
  • 8
  • 38
  • 48
1
vote
2 answers

Haskell function which acts differently depending on the monad type

I'm trying to implement a function which usually puts the first element of a list in a monad, but if the monad is a list it returns the whole list: putInMonad :: MonadPlus m => [a] -> m a putInMonad (s:sx) = return s putInMonad _ = mzero putInMonad…
axyz
  • 13
  • 2
1
vote
2 answers

Flatten MonadPlus inside an Aeson Parser

I'm not sure if I'm barking up the wrong tree here, but I have an Aeson FromJSON definition that looks rather bulky and I was wondering if it could be turned into something more concise. I want to short-circuit the parsing of the entire object if…
passy
  • 7,170
  • 5
  • 36
  • 38
1
vote
2 answers

Trouble understanding error handling with monads

I'm trying to build a function which returns me the single element from a list. The list is part of a Maybe (Int,[Int]) tupel. If the list contains no elements, I want to return an error. If the list contains exactly 1 element, I want to return that…
Marco
  • 1,430
  • 10
  • 18
1
vote
2 answers

Building a nondeterministic monad transformer in haskell

I would like to build a nondeterministic monad transformer in haskell that, I believe, behaves differently from ListT and from the alternative ListT proposed at http://www.haskell.org/haskellwiki/ListT_done_right. The first of these associates a…
Eyal
  • 1,094
  • 10
  • 16
0
votes
1 answer

Using Logic monad to backtrack upon exception thrown with ExceptT

I'd like to use the Logic monad to ensure that error-throwing code (in a monad stack including ExcepT) backtracks upon throwing an error. Here's a simple example: newtype FooT m a = FooT { unFooT :: ExceptT String m a } deriving (Functor,…
trpnd
  • 455
  • 2
  • 10
0
votes
1 answer

Error handling with MonadPlus

Got stuck again while trying to learn some Haskell. What I'm trying to do is implementing a combined head/tail method for lists with error handling. The signature must look like this: head' :: MonadPlus m => [a] -> m (a,[a]) However I'm a bit lost…
Marco
  • 1,430
  • 10
  • 18
1 2
3