#2309: containers: specialize functions that fail in a Monad to Maybe

David Menendez dave at zednenem.com
Thu May 29 13:53:23 EDT 2008


On Sun, May 25, 2008 at 6:34 AM, Yitzchak Gale <gale at sefer.org> wrote:
> Dan Doel wrote:
>> I don't see how specializing to Maybe and requiring people to explicitly
>> inject into an arbitrary MonadPlus gains anything over restricting to
>> MonadPlus in the first place.
>
> Maybe is not arbitrary. It is the "unit" instance of MonadPlus. It can be
> lifted trivially into any other instance.

This is somewhat off-topic, but it's worth noting that there is some
controversy over whether Maybe is an appropriate instance of MonadPlus
at all. One of the laws commonly given for MonadPlus is left
distribution, which Maybe does not satisfy.

Left distribution: mplus a b >>= f  =  mplus (a >>= f) (b >>= f)

(The actual controversy is whether left distribution should be
required for MonadPlus instances. Unfortunately, the issue is fairly
quiet, so it is unlikely to be resolved.)


Back on topic: I vote in favor of explicit Maybe return types over
Monad or MonadPlus. Every time I've used functions like lookup, they
ended up returning a Maybe, even their callers immediately threw
exceptions on receiving Nothing. Much easier to create a wrapper
function that does exactly what you want than to create an instance of
Monad(Plus) just to handle partial functions correctly.

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Libraries mailing list