没有标准(或者是)monad实例吗? [英] Is there no standard (Either a) monad instance?
问题描述
我的印象是某个地方有一个实例,但我似乎无法找到它。我已经尝试导入Control.Monad,Control.Monad.Instances和Data.Ext,如图所示
模块Main其中
import Control.Monad
import Data.Ebsys
import Control.Monad.Instances
test :: [或者ab] - >或者[b]
test = sequence
main = return()
但是ghc告诉我它不能推断出(Monad(或者a))。添加
实例Monad(或者a)其中
return =右
右b>> = f = fb
左a>> = _ =左a
编译,但是这个实例声明看起来很普遍,以至于如果它在某个标准模块中还没有出现,那对我来说是没有意义的。如果是这样,我应该在哪里寻找它,如果不是,那么是否有这个原因?
-------- ------编辑---------------
请注意,我现在认为user31708下面的答案( 从4.6开始,实例在Data.Either本身中。)目前是正确的答案。在这种情况下,我不确定重新分配选定答案的正确方案,在问题被提问时所选答案是正确答案,所以我已将其保留原样。如果还有其他的指导方针,请纠正我。 实例已添加到 base 4.3中.xx
,它附带 ghc 7
。与此同时,您可以直接使用 Either
实例,或者,如果您使用任一
来表示
这可能会失败,你应该使用 ErrorT
单子变压器。
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.
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.
这篇关于没有标准(或者是)monad实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!