为什么ghci不能为EIther找到Monad实例b

时间:2012-09-13 16:56:01

标签: haskell typeclass

所以,已经有几个月了,我有点生疏,但我似乎记得Either bMonad

Prelude Control.Monad Data.Either> return "Hello" :: Either String String

<interactive>:0:1:                                                       
    No instance for (Monad (Either String))                              
      arising from a use of `return'                                     
    Possible fix:                                                        
      add an instance declaration for (Monad (Either String))            
    In the expression: return "Hello" :: Either String String            
    In an equation for `it':                                             
        it = return "Hello" :: Either String String                      
Prelude> Right "Hi" == return "Hi"                                    

<interactive>:0:15:                                                   
    No instance for (Monad (Either a0))                               
      arising from a use of `return'                                  
    Possible fix: add an instance declaration for (Monad (Either a0)) 
    In the second argument of `(==)', namely `return "Hi"'            
    In the expression: Right "Hi" == return "Hi"                      
    In an equation for `it': it = Right "Hi" == return "Hi"           

所以我可能做错了什么,但我不确定是什么。我正在使用GHC 7.2.2。

2 个答案:

答案 0 :(得分:11)

base-4.6.0.0开始,Monad的{​​{1}}和Functor个实例在Either e中定义,并自动从Data.Either获取。< / p>

以前,在base-4.3.*base-4.5*中,实例是在Prelude中定义的,并且不会自动提供。在此之前,部分与之同时,有一个

Control.Monad.Instances

Control.Monad.Errormtl-1.*Control.Monad.Trans.Error包中的transformers中定义。如果它们与instance Error e => Monad (Either e) where ... 一起使用,这些模块仍会有条件地提供实例(现在没有Error约束)。

答案 1 :(得分:4)

导入Control.Monad.Trans.ErrorControl.Monad.ErrorEither a monad实例通常用于错误处理。

> return "Hello" :: (Either String String)
Right "Hello"