{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Rank2Types #-}
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,(.))
newtype StreamArrow a b c = StreamArrow (a (Stream b) (Stream c))
instance Category a => Category (StreamArrow a) where
id :: 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 k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
StreamArrow a (Stream b) (Stream c)
f . :: 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 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 :: (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 (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((b -> c) -> Stream b -> Stream c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> c
f))
first :: 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 (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 (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 (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 :: 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 (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 (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 (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))
instance Arrow a => ArrowTransformer (StreamArrow) a where
lift :: 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)
instance ArrowZero a => ArrowZero (StreamArrow a) where
zeroArrow :: StreamArrow a b c
zeroArrow = 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 (a :: * -> * -> *) b c. ArrowZero a => a b c
zeroArrow
instance ArrowState s a => ArrowState s (StreamArrow a) where
fetch :: StreamArrow a e s
fetch = a e s -> StreamArrow a e s
forall (f :: (* -> * -> *) -> * -> * -> *) (a :: * -> * -> *) b c.
ArrowTransformer f a =>
a b c -> f a b c
lift 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 (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 (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 :: 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 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 (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 :: (w' a', b) -> w' (a', b)
strength (w' a'
v, b
y) = (a' -> (a', b)) -> w' a' -> w' (a', b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a'
x -> (a'
x, b
y)) w' a'
v
instance Arrow a => ArrowChoice (StreamArrow a) where
left :: 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 (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 (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 (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr Stream (Either b d) -> Stream (Either b d)
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 (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 :: 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 (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 (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 (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 <+> :: 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 (a :: * -> * -> *) b c.
ArrowPlus a =>
a b c -> a b c -> a b c
<+> a (Stream b) (Stream c)
g)
instance ArrowLoop a => ArrowCircuit (StreamArrow a) where
delay :: 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 (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))
instance Arrow a => Functor (StreamArrow a b) where
fmap :: (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 (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr a -> b
f
instance Arrow a => Applicative (StreamArrow a b) where
pure :: a -> StreamArrow a b a
pure a
x = (b -> a) -> StreamArrow a b a
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 <*> :: 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 (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 (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 k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id)
instance ArrowPlus a => Alternative (StreamArrow a b) where
empty :: StreamArrow a b a
empty = StreamArrow a b a
forall (a :: * -> * -> *) b c. ArrowZero a => a b c
zeroArrow
StreamArrow a b a
f <|> :: 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 (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 (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 (a :: * -> * -> *) b c. ArrowZero a => a b c
zeroArrow
#if !(MIN_VERSION_base(4,11,0))
mappend = (<+>)
#endif
runStream :: ArrowLoop a => StreamArrow a (e,b) c -> a (e,Stream b) (Stream c)
runStream :: 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 (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 (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 :: a e b -> StreamArrow a e b
liftStream = a e b -> StreamArrow a e b
forall (f :: (* -> * -> *) -> * -> * -> *) (a :: * -> * -> *) b c.
ArrowTransformer f a =>
a b c -> f a b c
lift
elimStream :: 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
type StreamMap = StreamArrow (->)
type StreamMapST s = StreamArrow (Kleisli (ST s))
runStreamST :: (forall s. StreamMapST s e c) -> StreamMap e c
runStreamST :: (forall s. StreamMapST s e c) -> StreamMap e c
runStreamST forall s. StreamMapST s e c
cf = (Stream e -> Stream c) -> StreamMap e c
forall (a :: * -> * -> *) b c.
a (Stream b) (Stream c) -> StreamArrow a b c
StreamArrow ((Stream e -> Stream c) -> StreamMap e c)
-> (Stream e -> Stream c) -> StreamMap 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)