这与加布里埃尔对another question的回应密切相关。我编写了一个函数,它提供|||
函数来自ArrowChoice
,但Proxy
来自pipes
库。它模式匹配并具有五个相互递归的函数。我想找到一个替代实现,它使用Pipes.Core
中的函数而不是模式匹配。
我首先尝试在另一个答案中修改left
的实现。它看起来像这样:
newtype Edge m r a b = Edge { unEdge :: a -> Pipe a b m r }
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)
然而,这种实现依赖于上游和下游的相同,我无法看到进一步概括它的方法。我能得到的最接近的是:
proxyLeft :: Monad m
=> (a -> Pipes.Proxy y a y b m r)
-> Either a x
-> Pipes.Proxy y (Either a x) y (Either b x) m r
proxyLeft k = bef >=> (up \>\ (k />/ dn))
where
bef x = case x of
Left b -> return b
Right d -> do
a <- respond (Right d)
x2 <- request a
bef x2
up a = do
x <- request a
bef x
dn c = respond (Left c)
我的|||
变体的代码片段很长,我只是提供它作为可以某种方式完成的证据。该函数名为downstreamOr
。所有其他函数只是相互递归的辅助函数:
downstreamOr ::
Monad m
=> (b' -> Pipes.Proxy a' a b' b m r)
-> (c' -> Pipes.Proxy a' a c' b m r)
-> Either b' c'
-> Pipes.Proxy a' a (Either b' c') b m r
downstreamOr f g e = case e of
Left b' -> case f b' of
Pipes.Request a' k -> Pipes.Request a' (downstreamOrLeft k g)
Pipes.Respond b k -> Pipes.Respond b (downstreamOr k g)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamLeftM p g
Pipes.Pure r -> Pipes.Pure r
Right c' -> case g c' of
Pipes.Request a' k -> Pipes.Request a' (downstreamOrRight f k)
Pipes.Respond b k -> Pipes.Respond b (downstreamOr f k)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamRightM f p
Pipes.Pure r -> Pipes.Pure r
downstreamOrLeft ::
Monad m
=> (a -> Pipes.Proxy a' a b' b m r)
-> (c' -> Pipes.Proxy a' a c' b m r)
-> a
-> Pipes.Proxy a' a (Either b' c') b m r
downstreamOrLeft f g a = case f a of
Pipes.Request a' k -> Pipes.Request a' (downstreamOrLeft k g)
Pipes.Respond b k -> Pipes.Respond b (downstreamOr k g)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamLeftM p g
Pipes.Pure r -> Pipes.Pure r
downstreamOrRight ::
Monad m
=> (b' -> Pipes.Proxy a' a b' b m r)
-> (a -> Pipes.Proxy a' a c' b m r)
-> a
-> Pipes.Proxy a' a (Either b' c') b m r
downstreamOrRight f g a = case g a of
Pipes.Request a' k -> Pipes.Request a' (downstreamOrRight f k)
Pipes.Respond b k -> Pipes.Respond b (downstreamOr f k)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamRightM f p
Pipes.Pure r -> Pipes.Pure r
downstreamLeftM ::
Monad m
=> Pipes.Proxy a' a b' b m r
-> (c' -> Pipes.Proxy a' a c' b m r)
-> Pipes.Proxy a' a (Either b' c') b m r
downstreamLeftM p g = case p of
Pipes.Pure r -> Pipes.Pure r
Pipes.Request a' k -> Pipes.Request a' (downstreamOrLeft k g)
Pipes.Respond b k -> Pipes.Respond b (downstreamOr k g)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamLeftM p g
downstreamRightM ::
Monad m
=> (b' -> Pipes.Proxy a' a b' b m r)
-> Pipes.Proxy a' a c' b m r
-> Pipes.Proxy a' a (Either b' c') b m r
downstreamRightM f p = case p of
Pipes.Pure r -> Pipes.Pure r
Pipes.Respond a' k -> Pipes.Respond a' (downstreamOr f k)
Pipes.Request a' k -> Pipes.Request a' (downstreamOrRight f k)
Pipes.M m -> Pipes.M $ m >>= \p -> return $ downstreamRightM f p
了解如何在没有模式匹配的情况下实现其中任何一个将会很有帮助。谢谢,让我知道我是否可以澄清任何事情。