[Haskell-beginners] Re: Maybe, Either

Heinrich Apfelmus apfelmus at quantentunnel.de
Fri Sep 18 05:05:02 EDT 2009


Yusaku Hashimoto wrote:
> If I understood your post correctly, you said
> 
> - generalizing lookup to MonadPlus or Alternative or such classes are
> not necessary

In particular, it doesn't become more general, it becomes less general
(in a sense).

> - use Maybe as usual, we should use adapters as we need
> 
> Conor, You have said this many times elsewhere, but unfortunately, I
> heard it for the first time =) so please correct me if I'm wrong.
> 
> I thought generalizing lookup is good example for usage of the
> MonadPlus as I read in RWH[1], but you said it's not necessary.
> 
> Now, I understood there are two positions for such classes. One is
> using generalizing for it, another is not.
> 
> So, I want to know that when such classes should be used from later position.
> 
> Heinrich suggested that is for overloading.

To elaborate on generality versus overloading: the function

     lookupM :: MonadPlus m => k -> Map k a -> m a

is not more general than

     lookup :: k -> Map k a -> Maybe a

because you can implement the former with the latter

     lookupM k = mop . lookup k

     mop = maybe mzero return

In other words,  lookupM  doesn't introduce new functionality.


Rather, it gives you the syntactic convenience of not having to mention
 mop  by overloading the result type. In other words, you can write

     lookup  = lookupM

or

     lookupE :: k -> Map k a -> Either e a
     lookupE = lookupM


> But do any other usages are exist?

I'm not quite sure I understand what you mean here?


Regards,
apfelmus

--
http://apfelmus.nfshost.com



More information about the Beginners mailing list