    {-# LANGUAGE CPP #-}
    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE Rank2Types #-}

    -----------------------------------------------------------------------------
    -- |
    -- Module      :  Control.Arrow.Transformer.Stream
    -- Copyright   :  (c) Ross Paterson 2003
    -- License     :  BSD-style (see the LICENSE file in the distribution)
    --
    -- Maintainer  :  R.Paterson@city.ac.uk
    -- Stability   :  experimental
    -- Portability :  non-portable (multi-parameter type classes)
    --
    -- Arrow transformer lifting an arrow to streams.

    module Control.Arrow.Transformer.Stream(
        StreamArrow(StreamArrow),
        runStream,
        StreamMap,
        StreamMapST, runStreamST,
        ArrowAddStream(..),
        ) where

    import Control.Arrow.Internals
    import Control.Arrow.Operations
    import Control.Arrow.Transformer

    import Control.Applicative
    import Control.Arrow
    import Control.Category
    import Control.Monad.ST
    import Data.Monoid
    #if (MIN_VERSION_base(4,9,0)) && !(MIN_VERSION_base(4,11,0))
    import Data.Semigroup
    #endif
    import Data.Stream (Stream(..))
    import qualified Data.Stream as Stream

    import Prelude hiding (id,(.))

    -- | Arrows between streams.
    --
    -- /Note/: 'lift' is only a functor if '***' in the underlying arrow is.

    newtype StreamArrow a b c = StreamArrow (a (Stream b) (Stream c))

    instance Category a => Category (StreamArrow a) where
        id :: forall a. StreamArrow a a a
    id = a (Stream a) (Stream a) -> StreamArrow a a a
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow a (Stream a) (Stream a)
    forall a. a a a
    forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
    id
        StreamArrow a (Stream b) (Stream c)
    f . :: forall b c a.
    StreamArrow a b c -> StreamArrow a a b -> StreamArrow a a c
    . StreamArrow a (Stream a) (Stream b)
    g = a (Stream a) (Stream c) -> StreamArrow a a c
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow (a (Stream b) (Stream c)
    f a (Stream b) (Stream c)
    -> a (Stream a) (Stream b) -> a (Stream a) (Stream c)
    forall b c a. a b c -> a a b -> a a c
    forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
    Category cat =>
    cat b c -> cat a b -> cat a c
    . a (Stream a) (Stream b)
    g)

    instance Arrow a => Arrow (StreamArrow a) where
        arr :: forall b c. (b -> c) -> StreamArrow a b c
    arr b -> c
    f = a (Stream b) (Stream c) -> StreamArrow a b c
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow ((Stream b -> Stream c) -> a (Stream b) (Stream c)
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr ((b -> c) -> Stream b -> Stream c
    forall a b. (a -> b) -> Stream a -> Stream b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap b -> c
    f))
        first :: forall b c d. StreamArrow a b c -> StreamArrow a (b, d) (c, d)
    first (StreamArrow a (Stream b) (Stream c)
    f) =
            a (Stream (b, d)) (Stream (c, d)) -> StreamArrow a (b, d) (c, d)
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow ((Stream (b, d) -> (Stream b, Stream d))
    -> a (Stream (b, d)) (Stream b, Stream d)
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr Stream (b, d) -> (Stream b, Stream d)
    forall a b. Stream (a, b) -> (Stream a, Stream b)
    Stream.unzip a (Stream (b, d)) (Stream b, Stream d)
    -> a (Stream b, Stream d) (Stream (c, d))
    -> a (Stream (b, d)) (Stream (c, d))
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> a (Stream b) (Stream c)
    -> a (Stream b, Stream d) (Stream c, Stream d)
    forall b c d. a b c -> a (b, d) (c, d)
    forall (a :: * -> * -> *) b c d.
    Arrow a =>
    a b c -> a (b, d) (c, d)
    first a (Stream b) (Stream c)
    f a (Stream b, Stream d) (Stream c, Stream d)
    -> a (Stream c, Stream d) (Stream (c, d))
    -> a (Stream b, Stream d) (Stream (c, d))
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> ((Stream c, Stream d) -> Stream (c, d))
    -> a (Stream c, Stream d) (Stream (c, d))
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr ((Stream c -> Stream d -> Stream (c, d))
    -> (Stream c, Stream d) -> Stream (c, d)
    forall a b c. (a -> b -> c) -> (a, b) -> c
    uncurry Stream c -> Stream d -> Stream (c, d)
    forall a b. Stream a -> Stream b -> Stream (a, b)
    Stream.zip))

    genmap :: Arrow a => a b c -> a (Stream b) (Stream c)
    genmap :: forall (a :: * -> * -> *) b c.
    Arrow a =>
    a b c -> a (Stream b) (Stream c)
    genmap a b c
    f =
        (Stream b -> (b, Stream b)) -> a (Stream b) (b, Stream b)
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr (\Stream b
    xs -> (Stream b -> b
    forall a. Stream a -> a
    Stream.head Stream b
    xs, Stream b -> Stream b
    forall a. Stream a -> Stream a
    Stream.tail Stream b
    xs)) a (Stream b) (b, Stream b)
    -> a (b, Stream b) (Stream c) -> a (Stream b) (Stream c)
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>>
            a b c
    f a b c -> a (Stream b) (Stream c) -> a (b, Stream b) (c, Stream c)
    forall b c b' c'. a b c -> a b' c' -> a (b, b') (c, c')
    forall (a :: * -> * -> *) b c b' c'.
    Arrow a =>
    a b c -> a b' c' -> a (b, b') (c, c')
    *** a b c -> a (Stream b) (Stream c)
    forall (a :: * -> * -> *) b c.
    Arrow a =>
    a b c -> a (Stream b) (Stream c)
    genmap a b c
    f a (b, Stream b) (c, Stream c)
    -> a (c, Stream c) (Stream c) -> a (b, Stream b) (Stream c)
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> ((c, Stream c) -> Stream c) -> a (c, Stream c) (Stream c)
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr ((c -> Stream c -> Stream c) -> (c, Stream c) -> Stream c
    forall a b c. (a -> b -> c) -> (a, b) -> c
    uncurry (c -> Stream c -> Stream c
    forall a. a -> Stream a -> Stream a
    Stream.Cons))

    -- Caution: genmap is only a functor if *** for the base arrow is.
    -- (For Kleisli arrows, that would mean a commutative monad.)
    -- The same goes for the equivalent lift: it can be used to lift arrows,
    -- but won't preserve composition unless *** does.

    instance Arrow a => ArrowTransformer (StreamArrow) a where
        lift :: forall b c. a b c -> StreamArrow a b c
    lift a b c
    f = a (Stream b) (Stream c) -> StreamArrow a b c
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow (a b c -> a (Stream b) (Stream c)
    forall (a :: * -> * -> *) b c.
    Arrow a =>
    a b c -> a (Stream b) (Stream c)
    genmap a b c
    f)

    -- The following promotions follow directly from the arrow transformer.

    instance ArrowZero a => ArrowZero (StreamArrow a) where
        zeroArrow :: forall b c. StreamArrow a b c
    zeroArrow = a b c -> StreamArrow a b c
    forall b c. a b c -> StreamArrow a b c
    forall (f :: (* -> * -> *) -> * -> * -> *) (a :: * -> * -> *) b c.
    ArrowTransformer f a =>
    a b c -> f a b c
    lift a b c
    forall b c. a b c
    forall (a :: * -> * -> *) b c. ArrowZero a => a b c
    zeroArrow

    instance ArrowState s a => ArrowState s (StreamArrow a) where
        fetch :: forall e. StreamArrow a e s
    fetch = a e s -> StreamArrow a e s
    forall b c. a b c -> StreamArrow a b c
    forall (f :: (* -> * -> *) -> * -> * -> *) (a :: * -> * -> *) b c.
    ArrowTransformer f a =>
    a b c -> f a b c
    lift a e s
    forall e. a e s
    forall s (a :: * -> * -> *) e. ArrowState s a => a e s
    fetch
        store :: StreamArrow a s ()
    store = a s () -> StreamArrow a s ()
    forall b c. a b c -> StreamArrow a b c
    forall (f :: (* -> * -> *) -> * -> * -> *) (a :: * -> * -> *) b c.
    ArrowTransformer f a =>
    a b c -> f a b c
    lift a s ()
    forall s (a :: * -> * -> *). ArrowState s a => a s ()
    store

    instance ArrowWriter w a => ArrowWriter w (StreamArrow a) where
        write :: StreamArrow a w ()
    write = a w () -> StreamArrow a w ()
    forall b c. a b c -> StreamArrow a b c
    forall (f :: (* -> * -> *) -> * -> * -> *) (a :: * -> * -> *) b c.
    ArrowTransformer f a =>
    a b c -> f a b c
    lift a w ()
    forall w (a :: * -> * -> *). ArrowWriter w a => a w ()
    write
        newWriter :: forall e b. StreamArrow a e b -> StreamArrow a e (b, w)
    newWriter (StreamArrow a (Stream e) (Stream b)
    f) = a (Stream e) (Stream (b, w)) -> StreamArrow a e (b, w)
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow (a (Stream e) (Stream b) -> a (Stream e) (Stream b, w)
    forall e b. a e b -> a e (b, w)
    forall w (a :: * -> * -> *) e b.
    ArrowWriter w a =>
    a e b -> a e (b, w)
    newWriter a (Stream e) (Stream b)
    f a (Stream e) (Stream b, w)
    -> a (Stream b, w) (Stream (b, w)) -> a (Stream e) (Stream (b, w))
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> ((Stream b, w) -> Stream (b, w)) -> a (Stream b, w) (Stream (b, w))
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr (Stream b, w) -> Stream (b, w)
    forall (w' :: * -> *) a' b. Functor w' => (w' a', b) -> w' (a', b)
    strength)
          where
            strength :: Functor w' => (w' a',b) -> w' (a',b)
            strength :: forall (w' :: * -> *) a' b. Functor w' => (w' a', b) -> w' (a', b)
    strength (w' a'
    v, b
    y) = (a' -> (a', b)) -> w' a' -> w' (a', b)
    forall a b. (a -> b) -> w' a -> w' b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (\a'
    x -> (a'
    x, b
    y)) w' a'
    v

    -- liftings of standard classes

    instance Arrow a => ArrowChoice (StreamArrow a) where
        left :: forall b c d.
    StreamArrow a b c -> StreamArrow a (Either b d) (Either c d)
    left (StreamArrow a (Stream b) (Stream c)
    f) =
            a (Stream (Either b d)) (Stream (Either c d))
    -> StreamArrow a (Either b d) (Either c d)
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow (((Stream (Either b d) -> Stream b)
    -> a (Stream (Either b d)) (Stream b)
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr Stream (Either b d) -> Stream b
    forall {a} {b}. Stream (Either a b) -> Stream a
    getLeft a (Stream (Either b d)) (Stream b)
    -> a (Stream b) (Stream c) -> a (Stream (Either b d)) (Stream c)
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> a (Stream b) (Stream c)
    f) a (Stream (Either b d)) (Stream c)
    -> a (Stream (Either b d)) (Stream (Either b d))
    -> a (Stream (Either b d)) (Stream c, Stream (Either b d))
    forall b c c'. a b c -> a b c' -> a b (c, c')
    forall (a :: * -> * -> *) b c c'.
    Arrow a =>
    a b c -> a b c' -> a b (c, c')
    &&& (Stream (Either b d) -> Stream (Either b d))
    -> a (Stream (Either b d)) (Stream (Either b d))
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr Stream (Either b d) -> Stream (Either b d)
    forall a. a -> a
    forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
    id a (Stream (Either b d)) (Stream c, Stream (Either b d))
    -> a (Stream c, Stream (Either b d)) (Stream (Either c d))
    -> a (Stream (Either b d)) (Stream (Either c d))
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> ((Stream c, Stream (Either b d)) -> Stream (Either c d))
    -> a (Stream c, Stream (Either b d)) (Stream (Either c d))
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr (Stream c, Stream (Either b d)) -> Stream (Either c d)
    forall {a} {a} {b}.
    (Stream a, Stream (Either a b)) -> Stream (Either a b)
    replace)
          where
            getLeft :: Stream (Either a b) -> Stream a
    getLeft (Cons (Left a
    x) Stream (Either a b)
    xs) = a -> Stream a -> Stream a
    forall a. a -> Stream a -> Stream a
    Cons a
    x (Stream (Either a b) -> Stream a
    getLeft Stream (Either a b)
    xs)
            getLeft (Cons (Right b
    _) Stream (Either a b)
    xs) = Stream (Either a b) -> Stream a
    getLeft Stream (Either a b)
    xs
            replace :: (Stream a, Stream (Either a b)) -> Stream (Either a b)
    replace (~(Cons a
    x Stream a
    xs), Cons (Left a
    _) Stream (Either a b)
    ys) =
                Either a b -> Stream (Either a b) -> Stream (Either a b)
    forall a. a -> Stream a -> Stream a
    Cons (a -> Either a b
    forall a b. a -> Either a b
    Left a
    x) ((Stream a, Stream (Either a b)) -> Stream (Either a b)
    replace (Stream a
    xs, Stream (Either a b)
    ys))
            replace (Stream a
    xs, Cons (Right b
    y) Stream (Either a b)
    ys) =
                Either a b -> Stream (Either a b) -> Stream (Either a b)
    forall a. a -> Stream a -> Stream a
    Cons (b -> Either a b
    forall a b. b -> Either a b
    Right b
    y) ((Stream a, Stream (Either a b)) -> Stream (Either a b)
    replace (Stream a
    xs, Stream (Either a b)
    ys))

    instance ArrowLoop a => ArrowLoop (StreamArrow a) where
        loop :: forall b d c. StreamArrow a (b, d) (c, d) -> StreamArrow a b c
    loop (StreamArrow a (Stream (b, d)) (Stream (c, d))
    f) =
            a (Stream b) (Stream c) -> StreamArrow a b c
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow (a (Stream b, Stream d) (Stream c, Stream d)
    -> a (Stream b) (Stream c)
    forall b d c. a (b, d) (c, d) -> a b c
    forall (a :: * -> * -> *) b d c.
    ArrowLoop a =>
    a (b, d) (c, d) -> a b c
    loop (((Stream b, Stream d) -> Stream (b, d))
    -> a (Stream b, Stream d) (Stream (b, d))
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr ((Stream b -> Stream d -> Stream (b, d))
    -> (Stream b, Stream d) -> Stream (b, d)
    forall a b c. (a -> b -> c) -> (a, b) -> c
    uncurry Stream b -> Stream d -> Stream (b, d)
    forall a b. Stream a -> Stream b -> Stream (a, b)
    Stream.zip) a (Stream b, Stream d) (Stream (b, d))
    -> a (Stream (b, d)) (Stream c, Stream d)
    -> a (Stream b, Stream d) (Stream c, Stream d)
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> a (Stream (b, d)) (Stream (c, d))
    f a (Stream (b, d)) (Stream (c, d))
    -> a (Stream (c, d)) (Stream c, Stream d)
    -> a (Stream (b, d)) (Stream c, Stream d)
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> (Stream (c, d) -> (Stream c, Stream d))
    -> a (Stream (c, d)) (Stream c, Stream d)
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr Stream (c, d) -> (Stream c, Stream d)
    forall a b. Stream (a, b) -> (Stream a, Stream b)
    Stream.unzip))

    instance ArrowPlus a => ArrowPlus (StreamArrow a) where
        StreamArrow a (Stream b) (Stream c)
    f <+> :: forall b c.
    StreamArrow a b c -> StreamArrow a b c -> StreamArrow a b c
    <+> StreamArrow a (Stream b) (Stream c)
    g = a (Stream b) (Stream c) -> StreamArrow a b c
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow (a (Stream b) (Stream c)
    f a (Stream b) (Stream c)
    -> a (Stream b) (Stream c) -> a (Stream b) (Stream c)
    forall b c. a b c -> a b c -> a b c
    forall (a :: * -> * -> *) b c.
    ArrowPlus a =>
    a b c -> a b c -> a b c
    <+> a (Stream b) (Stream c)
    g)

    -- I don't know of any other useful promotions.
    -- (elimWriter can be promoted, but doesn't seem useful.)

    -- Circuits

    instance ArrowLoop a => ArrowCircuit (StreamArrow a) where
        delay :: forall b. b -> StreamArrow a b b
    delay b
    x = a (Stream b) (Stream b) -> StreamArrow a b b
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow ((Stream b -> Stream b) -> a (Stream b) (Stream b)
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr (b -> Stream b -> Stream b
    forall a. a -> Stream a -> Stream a
    Cons b
    x))

    -- Other instances

    instance Arrow a => Functor (StreamArrow a b) where
        fmap :: forall a b. (a -> b) -> StreamArrow a b a -> StreamArrow a b b
    fmap a -> b
    f StreamArrow a b a
    g = StreamArrow a b a
    g StreamArrow a b a -> StreamArrow a a b -> StreamArrow a b b
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> (a -> b) -> StreamArrow a a b
    forall b c. (b -> c) -> StreamArrow a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr a -> b
    f

    instance Arrow a => Applicative (StreamArrow a b) where
        pure :: forall a. a -> StreamArrow a b a
    pure a
    x = (b -> a) -> StreamArrow a b a
    forall b c. (b -> c) -> StreamArrow a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr (a -> b -> a
    forall a b. a -> b -> a
    const a
    x)
        StreamArrow a b (a -> b)
    f <*> :: forall a b.
    StreamArrow a b (a -> b) -> StreamArrow a b a -> StreamArrow a b b
    <*> StreamArrow a b a
    g = StreamArrow a b (a -> b)
    f StreamArrow a b (a -> b)
    -> StreamArrow a b a -> StreamArrow a b (a -> b, a)
    forall b c c'.
    StreamArrow a b c -> StreamArrow a b c' -> StreamArrow a b (c, c')
    forall (a :: * -> * -> *) b c c'.
    Arrow a =>
    a b c -> a b c' -> a b (c, c')
    &&& StreamArrow a b a
    g StreamArrow a b (a -> b, a)
    -> StreamArrow a (a -> b, a) b -> StreamArrow a b b
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> ((a -> b, a) -> b) -> StreamArrow a (a -> b, a) b
    forall b c. (b -> c) -> StreamArrow a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr (((a -> b) -> a -> b) -> (a -> b, a) -> b
    forall a b c. (a -> b -> c) -> (a, b) -> c
    uncurry (a -> b) -> a -> b
    forall a. a -> a
    forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
    id)

    instance ArrowPlus a => Alternative (StreamArrow a b) where
        empty :: forall a. StreamArrow a b a
    empty = StreamArrow a b a
    forall b c. StreamArrow a b c
    forall (a :: * -> * -> *) b c. ArrowZero a => a b c
    zeroArrow
        StreamArrow a b a
    f <|> :: forall a.
    StreamArrow a b a -> StreamArrow a b a -> StreamArrow a b a
    <|> StreamArrow a b a
    g = StreamArrow a b a
    f StreamArrow a b a -> StreamArrow a b a -> StreamArrow a b a
    forall b c.
    StreamArrow a b c -> StreamArrow a b c -> StreamArrow a b c
    forall (a :: * -> * -> *) b c.
    ArrowPlus a =>
    a b c -> a b c -> a b c
    <+> StreamArrow a b a
    g

    #if MIN_VERSION_base(4,9,0)
    instance ArrowPlus a => Semigroup (StreamArrow a b c) where
        <> :: StreamArrow a b c -> StreamArrow a b c -> StreamArrow a b c
    (<>) = StreamArrow a b c -> StreamArrow a b c -> StreamArrow a b c
    forall b c.
    StreamArrow a b c -> StreamArrow a b c -> StreamArrow a b c
    forall (a :: * -> * -> *) b c.
    ArrowPlus a =>
    a b c -> a b c -> a b c
    (<+>)
    #endif

    instance ArrowPlus a => Monoid (StreamArrow a b c) where
        mempty :: StreamArrow a b c
    mempty = StreamArrow a b c
    forall b c. StreamArrow a b c
    forall (a :: * -> * -> *) b c. ArrowZero a => a b c
    zeroArrow
    #if !(MIN_VERSION_base(4,11,0))
        mappend = (<+>)
    #endif

    -- | Run a stream processor on a stream of inputs, obtaining a stream
    -- of outputs.
    --
    -- Typical usage in arrow notation:
    --
    -- >    proc p -> do
    -- >        ...
    -- >        ys <- (|runStream (\x -> ...)|) xs
    --
    -- Here @xs@ refers to the input stream and @x@ to individual
    -- elements of that stream.  @ys@ is bound to the output stream.

    runStream :: ArrowLoop a => StreamArrow a (e,b) c -> a (e,Stream b) (Stream c)
    runStream :: forall (a :: * -> * -> *) e b c.
    ArrowLoop a =>
    StreamArrow a (e, b) c -> a (e, Stream b) (Stream c)
    runStream (StreamArrow a (Stream (e, b)) (Stream c)
    f) = ((e, Stream b) -> Stream (e, b)) -> a (e, Stream b) (Stream (e, b))
    forall b c. (b -> c) -> a b c
    forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
    arr (\(e
    e, Stream b
    xs) -> (b -> (e, b)) -> Stream b -> Stream (e, b)
    forall a b. (a -> b) -> Stream a -> Stream b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (\b
    x -> (e
    e, b
    x)) Stream b
    xs) a (e, Stream b) (Stream (e, b))
    -> a (Stream (e, b)) (Stream c) -> a (e, Stream b) (Stream c)
    forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
    Category cat =>
    cat a b -> cat b c -> cat a c
    >>> a (Stream (e, b)) (Stream c)
    f

    instance ArrowLoop a => ArrowAddStream (StreamArrow a) a where
        liftStream :: forall e b. a e b -> StreamArrow a e b
    liftStream = a e b -> StreamArrow a e b
    forall e b. a e b -> StreamArrow a e b
    forall (f :: (* -> * -> *) -> * -> * -> *) (a :: * -> * -> *) b c.
    ArrowTransformer f a =>
    a b c -> f a b c
    lift
        elimStream :: forall e b c. StreamArrow a (e, b) c -> a (e, Stream b) (Stream c)
    elimStream = StreamArrow a (e, b) c -> a (e, Stream b) (Stream c)
    forall (a :: * -> * -> *) e b c.
    ArrowLoop a =>
    StreamArrow a (e, b) c -> a (e, Stream b) (Stream c)
    runStream

    -- | Mappings of streams
    type StreamMap = StreamArrow (->)

    -- | In-place state updates.
    --
    -- /Note/: this is an arrow type, and 'lift' can be used to promote arrows
    -- from @'Kleisli' ('ST' s)@: the resulting arrow updates the state for
    -- each stream element in turn, and as long as the final state in not
    -- required all is well.  However, 'lift' does not preserve composition,
    -- because this monad isn't commutative.  In particular, a composition
    -- of 'lift's of state transformers will not work, as the second will
    -- require the final state of the first.

    type StreamMapST s = StreamArrow (Kleisli (ST s))

    -- | Encapsulate a local state.

    runStreamST :: (forall s. StreamMapST s e c) -> StreamMap e c
    runStreamST :: forall e c. (forall s. StreamMapST s e c) -> StreamMap e c
    runStreamST forall s. StreamMapST s e c
    cf = (Stream e -> Stream c) -> StreamArrow (->) e c
    forall (a :: * -> * -> *) b c.
    a (Stream b) (Stream c) -> StreamArrow a b c
    StreamArrow ((Stream e -> Stream c) -> StreamArrow (->) e c)
    -> (Stream e -> Stream c) -> StreamArrow (->) e c
    forall a b. (a -> b) -> a -> b
    $ \ Stream e
    input ->
        (forall s. ST s (Stream c)) -> Stream c
    forall a. (forall s. ST s a) -> a
    runST (let StreamArrow (Kleisli Stream e -> ST s (Stream c)
    f) = StreamArrow (Kleisli (ST s)) e c
    forall s. StreamMapST s e c
    cf in Stream e -> ST s (Stream c)
    forall {s}. Stream e -> ST s (Stream c)
    f Stream e
    input)