
On Mon, Mar 26, 2012 at 1:33 PM, Ting Lei
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.
Well, you can sort of do it with only MonadPlus - but it really depends on your choice of Monad whether or not it does anything like what you want: can :: (MonadPlus m) => (a -> m ()) -> a -> m Bool can f x = (f x >> return True) <|> return false For 'Maybe' this works great, but for something like 'List' I couldn't even tell you what it would do without reasoning through it. So you might be better off with the suggestion from Tobias using Eq Antoine