[Haskell-cafe] Is there a generic way to detect "mzero"?

Ting Lei tinlyx at hotmail.com
Mon Mar 26 23:25:40 CEST 2012


Hi Antoine and Tobias (and everyone else),

Thanks a lot for your answers. They are really helpful

Can you please show me how to use the (Eq m) constraint to do this?

Also, my general question (probably novice-level) is that in monadic programming, you can convert not necessarily monadic codes into monadic ones.
I know for many cases, it is impossible to do the reverse conversion, e.g. you can't make a function involving real IO operations into a pure code.
In other cases, for example, I may need to using things like Nothing as the "null" value as in other programming languages, just to represent a special "missing" value outside the regular type.
Is mzero a reasonable replacement for this or is there any reasonable (abstract) approximation in Haskell for doing this? (Like "null", I need the ability to detect it.)

Thanks,

Ting


> From: aslatter at gmail.com
> Date: Mon, 26 Mar 2012 14:24:09 -0500
> Subject: Re: [Haskell-cafe] Is there a generic way to detect "mzero"?
> To: tinlyx at hotmail.com
> CC: haskell-cafe at haskell.org
> 
> On Mon, Mar 26, 2012 at 1:33 PM, Ting Lei <tinlyx at hotmail.com> wrote:
> > 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
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120326/cc59c9ac/attachment.htm>


More information about the Haskell-Cafe mailing list