我一直在尝试编写该函数的实现:
foo :: Monad m => ConduitM i o (forall s. STT s m) r -> ConduitM i o m r
但是我每次都失败并出现错误:
Couldn't match type because variable `s` would escape its scope.
我现在怀疑实现这个功能是不可能的。
threadSTT :: Monad m
=> (forall a. (forall s. STT s m a) -> m a)
-> ConduitM i o (forall s. STT s m) r
-> ConduitM i o m r
threadSTT runM (ConduitM c0) =
ConduitM $ \rest ->
let go (Done r) = rest r
go (PipeM mp) = PipeM $ do
r <- runM mp -- ERROR
return $ go r
go (Leftover p i) = Leftover (go p) i
go (NeedInput x y) = NeedInput (go . x) (go . y)
go (HaveOutput p f o) = HaveOutput (go p) (runM f) o -- ERROR
in go (c0 Done)
foo :: Monad m => ConduitM i o (forall s. STT s m) r -> ConduitM i o m r
foo = threadSTT STT.runST
有人能谈谈这个吗?我真的很希望它能工作,但如果我不能,那么我需要放弃使用 Data.Array.ST
来编写我的管道。
最佳答案
您似乎重新发明了 ConduitM
的 MFunctor
实例。您可以查看source code .
由管道包的作者 monad hoist in this style gives surprising results when you try to unwrap a monad with side effect 。在这种情况下,runST
将被多次调用,因此每次管道生成项目时都会抛出状态。
您最好将线路上的所有其他导管从 Conduit i o m r
提升到 Conduit i o (STT s m) r
并调用 runST
结果。就像transPipe lift
一样简单。
关于haskell - STT导管如何吊装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35249381/