haskell - STT导管如何吊装

标签 haskell monad-transformers conduit st-monad

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

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 实例。您可以查看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/

相关文章:

haskell - 如何在Haskell中旋转OpenGL图形而又无用地重新评估图形对象?

haskell - 是否可以使用您自己的数据类型模拟函数?

haskell - monad 变压器内 monad 的结果

haskell - 试图了解单子(monad)变压器产生的类型

haskell - 使用 Conduit 从 ByteString 过滤 ANSI 转义序列

haskell - 带有管道的源中的无限循环

scala - Haskell v. Scala 中的类型类

haskell - 如何编写调用 `run` 或 `runStateT` 的函数 `runReaderT`?

error-handling - EitherT 是如何工作的?

haskell - 使用 postgresql-simple 创建流式管道源