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

发布于 2024-10-19 06:35:46 字数 757 浏览 8 评论 0原文

我的印象是某处有一个 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 本身中。”)目前是正确的答案。我不确定在这种情况下重新分配所选答案的正确协议,其中所选答案是提出问题时的正确答案,因此我将其保留原样。如果有其他指导方针,请纠正我。

I was under the impression that there was an instance for Either a somewhere, but I can't seem to find it. I have tried importing Control.Monad, Control.Monad.Instances and Data.Either as shown

module Main where

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

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

main = return ()

but ghc tells me that it could not deduce (Monad (Either a)). Adding

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

makes the code compile, but this instance declaration seems so general that it doesn't make sense to me if it isn't already out there in some standard module. If it is, where should I look to find it, and if it isn't, is there then a reason for this?

-------------- EDIT ---------------

Be aware that I now think that the answer by user31708 below ("As of base 4.6, the instance is in Data.Either itself.") is currently the correct answer. I am not sure of the proper protocol of reassigning the selected answer in this case, where the selected answer was the correct answer at the time that the question was asked, so I have left it as it is. Please correct me, if there is another guideline for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌若浮生 2024-10-26 06:35:46

实例 已添加到 base 4.3.xx 中,该版本随 ghc 7 一起提供。同时,您可以直接使用 Either 实例,或者,如果您使用 Either 来表示某些内容
这可能会失败,您应该使用 < code>ErrorT monad 转换器。

This instance has been added in base 4.3.x.x, which comes with ghc 7. Meanwhile, you can use the Either instance directly, or, if you are using Either to represent something
that may fail you should use ErrorT monad transformer.

神回复 2024-10-26 06:35:46

As of base 4.6, the instance is in Data.Either itself.

小红帽 2024-10-26 06:35:46

Control.Monad.Error 中没有 Either a 的实例,但有 Either String 的实例。 (实际上,它是针对Error e => Either e,IIRC)。

There is not an instance for Either a, but there is for Either String in Control.Monad.Error. (Actually, it's for Error e => Either e, IIRC).

世界和平 2024-10-26 06:35:46

相信 Control.Monad.Error 中有一些东西 - 但没有任何东西要检查。

I believe there's something in Control.Monad.Error - don't have anything to check, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文