如何吊装STT导管



我一直在尝试编写该函数的实现:

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来编写我的管道。

看来您已经重新发明了ConduitMMFunctor实例。您可以检查源代码。

由导管包的作者撰写,当您尝试打开带有副作用的单体时,这种风格的 monad 提升机会给出令人惊讶的结果。在这种情况下,runST将被多次调用,因此每次管道生成项目时都会引发状态。

您最好将线路上的其他管道从Conduit i o m r提升到Conduit i o (STT s m) r,并致电runST结果。就像transPipe lift一样简单.

最新更新