2013-07-07 36 views
2

使用管道,我試圖爲ProxyFastProxyCorrect類型編寫MonadTransControl的實例。這是我得到的:用於ProxyFast/ProxyCorrect的MonadTransControl實例

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),但我在管道中找不到任何此類函數。 MonadTransControl for ProxyCorrect/ProxyFast的實例如何看起來像?還是不可能寫一個? (如果是的話,是否有可能在管道4.0?)

+2

我從來沒有理解'MonadTransControl'。是否有一些教程或博客文章解釋如何使用它? –

+1

我只能找到http://www.yesodweb.com/blog/2011/08/monad-control – bennofs

回答

4

感謝您的鏈接,現在我可以給出更好的答案。

不,沒有辦法執行此操作,使用版本pipes。之所以這樣做,是因爲MonadTransControl期望在底層基本monad的單層之上構建monad變換器。這是爲所有目前MonadTransControl實現單子變壓器,如真:

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不換行基座單子的單層。這對於pipes版本都是如此,您可以根據需要嵌套多個基本monad層。

事實上,該基地單子多次巢任何單子變壓器將違抗MonadTransControl實例,如:

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

然而,僅僅因爲pipes沒有實現MonadTransControl並不意味着所有的希望丟失。 pipes-safe實現許多操作的一個通常從MonadTransControl期望,如bracket ING資源的收購,因此,如果您可以在特定使用案例闡述我可以告訴你更多,如果有適合您的問題適當pipes爲基礎的解決方案。

+0

Rhanks的答案。你說得對,我正在尋找管道安全。 – bennofs