MonadTransControl instance for ProxyFast/ProxyCorrect

有些话、适合烂在心里 提交于 2019-12-11 02:57:20

问题


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?)


回答1:


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.



来源:https://stackoverflow.com/questions/17511841/monadtranscontrol-instance-for-proxyfast-proxycorrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!