首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Conduit和Pipe不能有Arrow实例?

为什么Conduit和Pipe不能有Arrow实例?
EN

Stack Overflow用户
提问于 2017-02-23 22:41:47
回答 1查看 451关注 0票数 5

在reddit上有一个归档线程,上面写着本质上管道/管道不能是箭头,b/c箭头必须是同步的。该线程在此处链接https://www.reddit.com/r/haskell/comments/rq1q5/conduitssinks_and_refactoring_arrows/

我不明白“同步”是从哪里来的,因为它不是箭头定义的一部分。另外,我在github https://github.com/cmahon/interactive-brokers上偶然发现了这个项目,它明确地将管道视为箭头。为了方便起见,我在这里粘贴了实例定义。这里我漏掉了什么?

代码语言:javascript
复制
-- 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
EN

回答 1

Stack Overflow用户

发布于 2017-02-24 04:46:12

考虑一下这个管道:yield '*' :: Pipe x Char IO ()。我们可以将它包装在newtype PipeArrow a b = PipeArrow { getPipeArrow :: Pipe a b IO () }这样的新型适配器中,并尝试在那里定义Arrow实例。

购买如何编写一个在yield '*'上工作的first :: PipeArrow b c -> PipeArrow (b, d) (c, d)?管道从不等待来自上游的值。我们将不得不凭空产生一个d,来伴随'*'

管道满足箭头(和ArrowChoice)的大多数法律,但是first不能以合法的方式实现。

您发布的代码没有为Pipe定义Arrow实例,而是为一个从上游获取值并返回Pipe的函数定义。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42418842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档