[Haskell-cafe] Monadic function fails as Nothing in Maybe context but as Exception in Either context.

Michael Walker mike at barrucadu.co.uk
Mon Nov 30 22:36:22 UTC 2015


On Mon, 30 Nov 2015 14:25:49 -0800
Jeffrey Brown <jeffbrown.the at gmail.com> wrote:

> I've written a monadic function which in a Maybe context produces a
> Nothing when it fails (as intended), but in an Either context
> produces an Exception rather than a Left.

The Monad instance for (Either e) uses the default definition of 'fail',
which is to throw an exception:

instance Monad (Either e) where
    return = Right
    Left  l >>= _ = Left l
    Right r >>= k = k r

In general, 'Left' can't be used because that would only type check
for (Either String), but the Monad instance is more general than that.

-- 
Michael Walker (http://www.barrucadu.co.uk)


More information about the Haskell-Cafe mailing list