[Haskell-cafe] instance Monad (Either String) in Prelude or Data.Either?

Edward Kmett ekmett at gmail.com
Fri Sep 11 09:10:01 EDT 2009


Unfortunately, the instance of Monad for Either a is somewhat misguided in
Haskell.

There is a spurious restraint that the Left value in your Either be a member
of some Error class, which was brought about by the deletion of MonadZero
from Haskell 98 (as part of the elimination of failure free patterns, and an
attempted simplification of the language).

In order to support the arguable misfeature of including 'fail' in Monad,
the Either instance for Monad has this extra constraint.

That in and of itself is only mildly annoying, but then to use that
constraint requires a multi-parameter type class for MonadError, which isn't
a Haskell 98 feature. So it isn't in the standard prelude, which tries to
limit itself to that which could be plausibly implemented as Haskell 98, but
instead sits off in one of the dozen or so monad transformer library
variants floating around.

In theory, this is good, because you can then define your own monad instance
for Either without that constraint -- if you don't care to exploit the Error
instance's ability to get fail to work. Or you want a real Either monad for
tricks like Oleg's recent post on the constructive law of excluded middle,
or to talk about apomorphisms as a generalized anamorphism, parameterized on
the either monad so it can be symmetrical to paramorphisms. In practice,
you link in someone's code that links in the MTL, and then you're stuck
using the MTL instance anyways.

-Edward Kmett
On Thu, Sep 10, 2009 at 9:03 PM, Evan Laforge <qdunkan at gmail.com> wrote:

> Is there any particular reason to not move the instance to the
> prelude?  A module was failing when imported from one place and ok
> when imported from another, so I spent a frustrating 10 minutes
> searching for the instance.  I couldn't find a way to search haddock
> for instances (not even grep on the html), nor hoogle, nor anything
> else.  Finally I turned to google and found a couple of haskell-cafe
> threads with people wondering why other people's code failed for them.
>  This was especially frustrating because I knew I had written my own
> instance when I originally couldn't find it, then removed my own when
> I did find it, then forgot where I found it *two times* so I knew it
> existed, just not where.  So this is the third time I've done this
> search.  Now I have a few commented 'import Control.Monad.Error ()'
> scattered about in a few "low level" modules to try to make sure it's
> visible everywhere and next time I go hunting I can look in my own
> modules.
>
> If orphan instances are discouraged, shouldn't the instance for Monad
> Either be declared where Either is?  This should probably go for
> Functor Either too.  I notice that Monad Maybe is in Prelude, and it
> would be a pain if it weren't.
>
> If there's no particular reason to not move it, I'll do a libraries
> proposal... or since it involves the prelude should this be
> haskell-prime?
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090911/39fae09e/attachment.html


More information about the Haskell-Cafe mailing list