haskell - 是否没有标准(任一)monad 实例?

标签 haskell import instance monads either

我的印象是某处有 Either a 的实例,但我似乎找不到它。我尝试导入 Control.Monad、Control.Monad.Instances 和 Data.Either,如图所示

module Main where

import Control.Monad
import Data.Either
import Control.Monad.Instances

test :: [Either a b] -> Either a [b]
test = sequence

main = return ()

但是 ghc 告诉我它无法推导出 (Monad (Either a))。添加

instance Monad (Either a) where
    return = Right
    Right b >>= f = f b
    Left a >>= _ = Left a

使代码编译,但是这个实例声明看起来很笼统,如果它还没有出现在某个标准模块中,那么它对我来说就没有意义。如果是,我应该在哪里找到它,如果不是,有什么原因吗?

-------------- 编辑 ---------------

请注意,我现在认为下面 user31708 的答案(“从基础 4.6 开始,实例位于 Data.Either 本身。”)当前是正确的答案。我不确定在这种情况下重新分配所选答案的正确协议(protocol),其中所选答案是提出问题时的正确答案,因此我将其保留原样。如果有其他指导方针,请纠正我。

最佳答案

这个instance已添加到 base 4.3.x.x 中,该版本随 ghc 7 一起提供。同时,您可以直接使用 Either 实例,或者,如果您使用 Either 来表示某些内容 这可能会失败,你应该使用 ErrorT单子(monad)变压器。

关于haskell - 是否没有标准(任一)monad 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5112212/

相关文章:

java - 对实例和声明的误解

haskell - 如何使用非 * 类型的幻影类型参数为 GADT 导出 Eq

java - 你在实际项目中使用过 Quickcheck

ios - 找不到“UIImageView+AFNetworking.h”文件

Java:调用实例化实例的方法

ruby - 如何像在 Prime 类中一样在 Ruby 中创建默认实例?

haskell - 从 TemplateHaskell 中的文件中读取模块

haskell - 如何在 Turtle 中 grep ls 的结果

python - Python中导入azure.servicebus错误

python - 将 python 模块导入例程或类定义有什么问题吗?