我有两个函数,我很困惑序列如何知道将哪个参数放入列表中,以及两者中的 Left 实际发生了什么
getMonStat :: T.Text -> IO (Either CmdError MonStat)
..
getMonStats :: [T.Text] -> IO (Either CmdError [MonStat])
getMonStats ms = do
monStats <- mapM getMonStat ms
pure $ sequence monStats
我的问题是:monStats :: [Either CmdError MonStat]
.在 [m a]
上运行序列把它变成 m [a]
.为什么不是初始类型[m a b]
自 CmdError
也是一种类型吗?如果 CmdError
是要考虑的类型,ghc怎么知道挑MonStat
而不是 CmdError
列出化?或使用类型:为什么序列转换为 m a b -> m a [b]
而不是 m a b -> m [a] b
?[Either CmdError MonStat]
会最终CmdError
在 Either CmdError [MonStat]
是?首先?所有这些的连接字符串?我可以测试,但我也很好奇为什么。感谢您对此的任何见解
最佳答案
[m a b] = [(m a) b] = [m' b]
哪里m' = m a
.所以它总是采用“list-ify”的最后一个参数。Left
在列表中。 Monad
就是这样的Either
的实例定义为:Left x >>= _ = Left x
. sequence
函数使用该 Monad
实例。见 this question关于收集所有失败。关于haskell - 当在一个列表上运行 'sequence' 时,ghc 如何知道 list-ify 是哪个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68165537/