【问题标题】:Why can't ghci find the Monad instance for EIther b为什么 ghci 找不到 EIther b 的 Monad 实例
【发布时间】:2012-09-13 16:56:00
【问题描述】:

所以,几个月过去了,我有点生疏了,但我似乎记得 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。

【问题讨论】:

    标签: haskell typeclass


    【解决方案1】:

    base-4.6.0.0 开始,Either eMonadFunctor 实例在 Data.Either 中定义,并且可从 Prelude 自动获得。

    以前,在base-4.3.*base-4.5* 中,实例是在Control.Monad.Instances 中定义的,并且不会自动可用。在此之前,部分同时,有一个

    instance Error e => Monad (Either e) where
        ...
    

    mtl-1.*Control.Monad.Errortransformers 包中的Control.Monad.Trans.Error 中定义。如果这些模块与base &lt; 4.3 一起使用,它们仍然有条件地提供实例(现在没有Error 约束)。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 2016-01-18
        • 2012-10-18
        相关资源
        最近更新 更多