Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Conduit and Pipe have an Arrow instance?

There is an archived thread on reddit which says essentially conduit/pipes cannot be arrows b/c arrows need to be synchronous. The thread is linked here https://www.reddit.com/r/haskell/comments/rq1q5/conduitssinks_and_refactoring_arrows/

I fail to see where the "synchronous" come in as that is not part of the definition of arrows. Plus, I stumbled upon this project on github https://github.com/cmahon/interactive-brokers which treats pipes explicitly as arrows. I am pasting the instance def here for your convenience. What am I missing here?

-- 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
like image 867
user2812201 Avatar asked Feb 23 '17 14:02

user2812201


1 Answers

Consider this pipe: yield '*' :: Pipe x Char IO (). We could wrap it in a newtype adapter like newtype PipeArrow a b = PipeArrow { getPipeArrow :: Pipe a b IO () } and try to define the Arrow instance there.

Buy how to write a first :: PipeArrow b c -> PipeArrow (b, d) (c, d) that works on yield '*' ? The pipe never awaits for a value from upstream. We would have to produce a d out of thin air, to accompany the '*'.

Pipes satisfy most of the laws for arrows (and for ArrowChoice) but first can't be implemented in a lawful way.

The code you posted doesn't define an Arrow intance for Pipe, but for a function that takes a value from upstream and returns a Pipe.

like image 129
danidiaz Avatar answered Oct 21 '22 13:10

danidiaz