Hi,

I was writing a code trying to use MonadPlus to detect some error cases 
(representing missing values etc. in pure code). With the Maybe monad, I can do 
this:

can0 :: (a -> Maybe b) -> a -> Bool
can0 f x = case f x of
                  Nothing -> False
                  Just  x -> True

And I got the expected result:

*Main> can0 (\x -> Just x) 1
True

But, when I try to generalize this using MonadPlus, as follows:

can :: (MonadPlus m) => (a -> m b) -> a -> Bool
can f x = case f x of 
            mzero -> False
            _ -> True


I got a warning:

__testError.hs:31:11:
    Warning: Pattern match(es) are overlapped
             In a case alternative: _ -> ...
Ok, modules loaded: Main.

And the result is also not as intended (see also can0):

*Main> can (\x -> Just x) 1
False


Can anyone help to explain why this wouldn't work or if there is a workaround 
to use Monadplus and mzero (or Monad and fail) to achieve this?


Thanks in advance for your help

Ting

                                          
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to