Eric | 20 Dec 22:34

instance Monad Either?

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?

E.
Philip Weaver | 20 Dec 22:39

Re: instance Monad Either?


On Dec 20, 2007 1:35 PM, Eric <eeoam <at> ukfsn.org> 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?

It would help if you'd provide an example of how you're trying to use it and what the error message is.  Thanks!

It's actually Either String, not Either, that can be an instance of Monad.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Tom Phoenix | 20 Dec 22:41

Re: instance Monad Either?

On 12/20/07, Eric <eeoam <at> ukfsn.org> 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?

Near the bottom of that page is a comment by Luis Cabellos. Does that
comment bring you any closer to enlightenment?

Cheers!

--Tom Phoenix
Eric | 20 Dec 22:54

Re: instance Monad Either?

Tom Phoenix wrote:
> On 12/20/07, Eric <eeoam <at> ukfsn.org> 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?
>>     
>
> Near the bottom of that page is a comment by Luis Cabellos. Does that
> comment bring you any closer to enlightenment?
>
>   
It works now. Thanks!

E.
Tillmann Rendel | 20 Dec 23:26

Re: instance Monad Either?

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

Gmane