[Haskell-cafe] Why Either = Left | Right instead of something
like Result = Success | Failure
Yitzchak Gale
gale at sefer.org
Sat May 29 18:47:54 EDT 2010
Vo Minh Thu wrote:
>>>> Control.Monad.Error provides an instance for Either.
Donn Cave wrote:
>>> ... in the mtl transformer library, in case anyone else besides
>>> myself didn't know that. And I see it has to be there because
>>> it depends on the Error typeclass.
Daniel Fischer <daniel.is.fischer at web.de> writes:
>> Which is considered a wart by some. (Either left) has a perfectly valid
>> Monad instance for any type left
Indeed, and it is one of the most useful Monads there is.
The Either monad provides short-circuiting logic, so that
you can exit an arbitrary number of levels of nested calculation.
The type Either a b returns a if the calculation exits,
or b if it completes without exiting.
Exiting on an error condition is one tiny corner case of
multi-level exit, so it is a shame that the mtl library defines
an orphan instance. By doing that, it hijacks the Either monad
and monopolizes it for its own specialized purpose. This is the
classic example why defining orphan instances is so dangerous.
Now, instead of the natural use of Either for short-circuiting logic,
we are forced to find other solutions, such as:
o use the MonadPlus instance of Maybe and write everything
"additively", using mplus and return instead of (>>=) and Left.
That is equivalent to the Either monad, but in practice it ends
up looking a lot messier and harder to read.
o use a CPS-based monad, like Cont or LogicT. I find those
highly obfuscated, and a huge amount of complexity overkill
for most tasks.
o use a clone of Either, like the Exit monad. That is currently the
best workaround, in my opinion.
http://www.haskell.org/haskellwiki/New_monads/MonadExit
Regards,
Yitz
More information about the Haskell-Cafe
mailing list