使用管道,我正在尝试编写 MonadTransControl 的实例对于 ProxyFast或ProxyCorrect类型。这就是我所得到的:
instance MonadTransControl (ProxyFast a' a b' b) where
data StT (ProxyFast a' a b' b) a = StProxy { unStProxy :: ProxyFast a' a b' b Identity a}
liftWith = undefined
restoreT = undefined
我不知道如何编写 liftWith 或 RestoreT。其他 monad 转换器的实例都使用“交换”monad 的函数,例如 EitherT e m a -> m (EitherT e Identity a),但我在管道中找不到任何此类函数。 ProxyCorrect/ProxyFast 的 MonadTransControl 实例是什么样子的?或者说不可能写一篇? (如果是的话,在管道4.0中可以吗?)
最佳答案
感谢您的链接,现在我可以给出更好的答案。
不,使用任一版本的管道
都无法实现此目的。原因是 MonadTransControl 期望 monad 转换器构建在底层基础 monad 的单层之上。对于 MonadTransControl
当前实现的所有 monad 转换器都是如此,例如:
ErrorT ~ m (Either e r)
StateT ~ s -> m (r, s)
WriterT ~ m (r, w)
ReaderT ~ i -> m r
ListT ~ m [r] -- This version of ListT is wrong, and the true ListT
-- would not work for `MonadTransControl`
但是,Proxy
并不包装基础 monad 的单层。这对于两个 pipes
版本都是如此,您可以根据需要嵌套任意数量的基本 monad 层。
事实上,任何多次嵌套基本 monad 的 monad 转换器都会违反 MonadTransControl
实例,例如:
FreeT -- from the `free` package
ListT -- when done "right"
ConduitM -- from the `conduit` package
但是,仅仅因为 pipes
没有实现 MonadTransControl
并不意味着所有希望都破灭了。 pipes-safe
实现了人们通常期望从 MonadTransControl
获得的许多操作,例如 bracket
资源获取,所以如果您能详细说明您的具体用例如果有适合您的问题的基于管道的解决方案,我可以告诉您更多信息。
关于haskell - ProxyFast/ProxyCorrect 的 MonadTransControl 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17511841/