【发布时间】:2017-02-23 14:41:46
【问题描述】:
reddit 上有一个存档线程,它说基本上导管/管道不能是箭头 b/c 箭头需要同步。帖子链接在这里https://www.reddit.com/r/haskell/comments/rq1q5/conduitssinks_and_refactoring_arrows/
我看不到“同步”的来源,因为这不是箭头定义的一部分。另外,我在 github https://github.com/cmahon/interactive-brokers 上偶然发现了这个项目,它明确地将管道视为箭头。为了您的方便,我将实例 def 粘贴在这里。我在这里错过了什么?
-- The code in this module was provided by Gabriel Gonzalez
{-# LANGUAGE RankNTypes #-}
module Pipes.Edge where
import Control.Arrow
import Control.Category (Category((.), id))
import Control.Monad ((>=>))
import Control.Monad.Trans.State.Strict (get, put)
import Pipes
import Pipes.Core (request, respond, (\>\), (/>/), push, (>~>))
import Pipes.Internal (unsafeHoist)
import Pipes.Lift (evalStateP)
import Prelude hiding ((.), id)
newtype Edge m r a b = Edge { unEdge :: a -> Pipe a b m r }
instance (Monad m) => Category (Edge m r) where
id = Edge push
(Edge p2) . (Edge p1) = Edge (p1 >~> p2)
instance (Monad m) => Arrow (Edge m r) where
arr f = Edge (push />/ respond . f)
first (Edge p) = Edge $ \(b, d) ->
evalStateP d $ (up \>\ unsafeHoist lift . p />/ dn) b
where
up () = do
(b, d) <- request ()
lift $ put d
return b
dn c = do
d <- lift get
respond (c, d)
instance (Monad m) => ArrowChoice (Edge m r) where
left (Edge k) = Edge (bef >=> (up \>\ (k />/ dn)))
where
bef x = case x of
Left b -> return b
Right d -> do
_ <- respond (Right d)
x2 <- request ()
bef x2
up () = do
x <- request ()
bef x
dn c = respond (Left c)
runEdge :: (Monad m) => Edge m r a b -> Pipe a b m r
runEdge e = await >>= unEdge e
【问题讨论】:
-
这是否满足
arr (f >>> g) = arr f >>> arr g?我怀疑它没有,但不确定 -
这是由类别公理引起的,不是吗?
-
This message by Gabriel Gonzalez 对您引用的基于推送的管道实例提供了一些额外的评论。
标签: haskell arrows conduit haskell-pipes