本文介绍了用于ProxyFast / ProxyCorrect的MonadTransControl实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用管道,我试图编写一个 或类型。这是我得到的:

 实例MonadTransControl(ProxyFast a'a b'b)其中
数据StT (ProxyFast a'a b'b)a = StProxy {unStProxy :: ProxyFast a'a b'b标识a}
liftWith =未定义
restoreT =未定义

我不知道如何编写liftWith或restoreT。其他monad变换器的实例都使用交换monad的函数,例如EitherT e m a - > m(EitherT e Identity a),但我在管道中找不到任何此类函数。 MonadTransControl for ProxyCorrect / ProxyFast的实例如何看起来像?还是不可能写一个? (如果是的话,是否可以在管道4.0中)

解决方案

感谢您的链接,现在我可以给出更好的答案。

不可以,使用任何版本的 pipes 都无法实现此功能。之所以这样做是因为 MonadTransControl 期望在底层基本monad的单层之上构建monad变换器。对于 MonadTransControl 当前实现的所有monad变换器,这是正确的,例如:

  ErrorT〜m(Either er)
StateT〜s - > m(r,s)
WriterT〜m(r,w)
ReaderT〜i - > mr
ListT〜m [r] - 此版本的ListT错误,真正的ListT
- 对于`MonadTransControl`

然而,一个代理不会包装一个单层monad。这对管道版本都是正确的,您可以根据需要嵌套基本单体层的许多层。



实际上,多次嵌套基本monad的monad变换器将会违反 MonadTransControl 实例,如:

 FreeT  - 来自`free`包
ListT - 完成right
ConduitM - 来自`conduit`包

但是,仅仅因为 pipes 没有实现 MonadTransControl 并不意味着所有的希望都会消失。 管道安全实现了人们通常期望从 MonadTransControl 的许多操作,例如支持资源获取,所以如果你能详细说明你的具体用例,我可以告诉你更多的是否有适合管道的解决方案你的问题。


Using pipes, I'm trying to write an instance of MonadTransControl for the ProxyFast or ProxyCorrect type. This is what I've got:

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

I have no idea how to write liftWith or restoreT. The instances for the other monad transformers all use a function that "swaps" the monads, for example EitherT e m a -> m (EitherT e Identity a), but I couldn't find any such function in pipes. How does the instance for MonadTransControl for ProxyCorrect / ProxyFast look like? Or is it impossible to write one? (If yes, is it possible in pipes 4.0?)

解决方案

Thanks for the link, and now I can give a better answer.

No, there is no way to implement this, using either version of pipes. The reason why is that MonadTransControl expects a monad transformer to be built on top of a single layer of the underlying base monad. This is true for all the monad transformers that MonadTransControl currently implements, such as:

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`

However, a Proxy does not wrap a single layer of the base monad. This is true for both pipes versions where you can nest as many layers of the base monad as you want.

In fact, any monad transformer that nests the base monad multiple times will defy a MonadTransControl instance, such as:

FreeT     -- from the `free` package
ListT     -- when done "right"
ConduitM  -- from the `conduit` package

However, just because pipes does not implement MonadTransControl doesn't mean that all hope is lost. pipes-safe implements many of the operations that one would typically expect from MonadTransControl, such as bracketing resource acquisitions, so if you can elaborate on your specific use case I can tell you more if there is an appropriate pipes-based solution for your problem.

这篇关于用于ProxyFast / ProxyCorrect的MonadTransControl实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 02:21