[Haskell-cafe] instance Monad Either?
Tillmann Rendel
rendel at rbg.informatik.tu-darmstadt.de
Thu Dec 20 17:26:37 EST 2007
Eric wrote:
> According to this
> <http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors>
> Either is an instance of class Monad, but when I try to use the do
> notation I get a compiler error. What's going on?
Try to import Control.Monad.Error to get a Monad instance for Either.
Actually, the instance is for (Error a => Either a), So it's more like
the 5. aproach on that page. But since there is an instance Error String
you can use (Either String) as a Monad (or a MonadError):
import Control.Monad.Error
test1 :: Either String Int
test1 = do
[x, y, z] <- return [3]
return 42
test2 :: MonadError e m => m Int
test2 = do
[x, y, z] <- return [3]
return 42
*Main> test
Left "Pattern match failure in do expression at test.hs:5:2-10"
*Main> test2 :: Either String Int
Left "Pattern match failure in do expression at test.hs:11:2-10"
*Main> test2 :: IO Int
*** Exception: user error (Pattern match failure in do expression at
test.hs:11:2-10)
Tillmann
More information about the Haskell-Cafe
mailing list