{-# LANGUAGE Rank2Types #-}
module Data.MonadicStreamFunction.Core
(
MSF
, constM
, arrM
, liftBaseM
, liftBaseS
, (^>>>)
, (>>>^)
, liftTransS
, morphS
, morphGS
, feedback
, reactimate
, embed
, module Control.Arrow
)
where
import Control.Applicative
import Control.Arrow
import Control.Category as C
import Control.Monad.Base
import Control.Monad.Trans.Class
import Data.Tuple (swap)
import Prelude hiding ((.), id, sum)
import Data.MonadicStreamFunction.InternalCore (MSF, morphGS, feedback, reactimate, embed)
instance Monad m => Arrow (MSF m) where
arr :: (b -> c) -> MSF m b c
arr b -> c
f = (b -> m c) -> MSF m b c
forall (m :: * -> *) a b. Monad m => (a -> m b) -> MSF m a b
arrM (c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> m c) -> (b -> c) -> b -> m c
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. b -> c
f)
first :: MSF m b c -> MSF m (b, d) (c, d)
first = (forall c. (b -> m (c, c)) -> (b, d) -> m ((c, d), c))
-> MSF m b c -> MSF m (b, d) (c, d)
forall (m2 :: * -> *) a1 (m1 :: * -> *) b1 a2 b2.
Monad m2 =>
(forall c. (a1 -> m1 (b1, c)) -> a2 -> m2 (b2, c))
-> MSF m1 a1 b1 -> MSF m2 a2 b2
morphGS ((forall c. (b -> m (c, c)) -> (b, d) -> m ((c, d), c))
-> MSF m b c -> MSF m (b, d) (c, d))
-> (forall c. (b -> m (c, c)) -> (b, d) -> m ((c, d), c))
-> MSF m b c
-> MSF m (b, d) (c, d)
forall a b. (a -> b) -> a -> b
$ \b -> m (c, c)
f (a,c) -> do
(c
b, c
msf') <- b -> m (c, c)
f b
a
((c, d), c) -> m ((c, d), c)
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
b, d
c), c
msf')
instance Monad m => Functor (MSF m a) where
fmap :: (a -> b) -> MSF m a a -> MSF m a b
fmap a -> b
f MSF m a a
msf = MSF m a a
msf MSF m a a -> MSF m a b -> MSF m a 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) -> MSF m a b
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr a -> b
f
instance (Functor m, Monad m) => Applicative (MSF m a) where
pure :: a -> MSF m a a
pure = (a -> a) -> MSF m a a
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((a -> a) -> MSF m a a) -> (a -> a -> a) -> a -> MSF m a a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> a -> a
forall a b. a -> b -> a
const
MSF m a (a -> b)
fs <*> :: MSF m a (a -> b) -> MSF m a a -> MSF m a b
<*> MSF m a a
bs = (MSF m a (a -> b)
fs MSF m a (a -> b) -> MSF m a a -> MSF m a (a -> b, a)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& MSF m a a
bs) MSF m a (a -> b, a) -> MSF m (a -> b, a) b -> MSF m a 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) -> MSF m (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 a b. (a -> b) -> a -> b
($))
constM :: Monad m => m b -> MSF m a b
constM :: m b -> MSF m a b
constM = (a -> m b) -> MSF m a b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> MSF m a b
arrM ((a -> m b) -> MSF m a b) -> (m b -> a -> m b) -> m b -> MSF m a b
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. m b -> a -> m b
forall a b. a -> b -> a
const
arrM :: Monad m => (a -> m b) -> MSF m a b
arrM :: (a -> m b) -> MSF m a b
arrM a -> m b
f = (forall c. (a -> m (a, c)) -> a -> m (b, c))
-> MSF m a a -> MSF m a b
forall (m2 :: * -> *) a1 (m1 :: * -> *) b1 a2 b2.
Monad m2 =>
(forall c. (a1 -> m1 (b1, c)) -> a2 -> m2 (b2, c))
-> MSF m1 a1 b1 -> MSF m2 a2 b2
morphGS (\a -> m (a, c)
i a
a -> a -> m (a, c)
i a
a m (a, c) -> ((a, c) -> m (b, c)) -> m (b, c)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(a
_,c
c) -> a -> m b
f a
a m b -> (b -> m (b, c)) -> m (b, c)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b
b -> (b, c) -> m (b, c)
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, c
c)) MSF m a a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
C.id
liftBaseM :: (Monad m2, MonadBase m1 m2) => (a -> m1 b) -> MSF m2 a b
liftBaseM :: (a -> m1 b) -> MSF m2 a b
liftBaseM = (a -> m2 b) -> MSF m2 a b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> MSF m a b
arrM ((a -> m2 b) -> MSF m2 a b)
-> ((a -> m1 b) -> a -> m2 b) -> (a -> m1 b) -> MSF m2 a b
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (m1 b -> m2 b
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase (m1 b -> m2 b) -> (a -> m1 b) -> a -> m2 b
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
.)
liftBaseS :: (Monad m2, MonadBase m1 m2) => MSF m1 a b -> MSF m2 a b
liftBaseS :: MSF m1 a b -> MSF m2 a b
liftBaseS = (forall c. m1 c -> m2 c) -> MSF m1 a b -> MSF m2 a b
forall (m2 :: * -> *) (m1 :: * -> *) a b.
(Monad m2, Monad m1) =>
(forall c. m1 c -> m2 c) -> MSF m1 a b -> MSF m2 a b
morphS forall c. m1 c -> m2 c
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase
(^>>>) :: MonadBase m1 m2 => MSF m1 a b -> MSF m2 b c -> MSF m2 a c
MSF m1 a b
sf1 ^>>> :: MSF m1 a b -> MSF m2 b c -> MSF m2 a c
^>>> MSF m2 b c
sf2 = MSF m1 a b -> MSF m2 a b
forall (m2 :: * -> *) (m1 :: * -> *) a b.
(Monad m2, MonadBase m1 m2) =>
MSF m1 a b -> MSF m2 a b
liftBaseS MSF m1 a b
sf1 MSF m2 a b -> MSF m2 b c -> MSF m2 a c
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> MSF m2 b c
sf2
{-# INLINE (^>>>) #-}
(>>>^) :: MonadBase m1 m2 => MSF m2 a b -> MSF m1 b c -> MSF m2 a c
MSF m2 a b
sf1 >>>^ :: MSF m2 a b -> MSF m1 b c -> MSF m2 a c
>>>^ MSF m1 b c
sf2 = MSF m2 a b
sf1 MSF m2 a b -> MSF m2 b c -> MSF m2 a c
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> MSF m1 b c -> MSF m2 b c
forall (m2 :: * -> *) (m1 :: * -> *) a b.
(Monad m2, MonadBase m1 m2) =>
MSF m1 a b -> MSF m2 a b
liftBaseS MSF m1 b c
sf2
{-# INLINE (>>>^) #-}
liftTransS :: (MonadTrans t, Monad m, Monad (t m))
=> MSF m a b
-> MSF (t m) a b
liftTransS :: MSF m a b -> MSF (t m) a b
liftTransS = (forall c. m c -> t m c) -> MSF m a b -> MSF (t m) a b
forall (m2 :: * -> *) (m1 :: * -> *) a b.
(Monad m2, Monad m1) =>
(forall c. m1 c -> m2 c) -> MSF m1 a b -> MSF m2 a b
morphS forall c. m c -> t m c
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift
morphS :: (Monad m2, Monad m1)
=> (forall c . m1 c -> m2 c)
-> MSF m1 a b
-> MSF m2 a b
morphS :: (forall c. m1 c -> m2 c) -> MSF m1 a b -> MSF m2 a b
morphS forall c. m1 c -> m2 c
morph = (forall c. (a -> m1 (b, c)) -> a -> m2 (b, c))
-> MSF m1 a b -> MSF m2 a b
forall (m2 :: * -> *) a1 (m1 :: * -> *) b1 a2 b2.
Monad m2 =>
(forall c. (a1 -> m1 (b1, c)) -> a2 -> m2 (b2, c))
-> MSF m1 a1 b1 -> MSF m2 a2 b2
morphGS forall c. (a -> m1 (b, c)) -> a -> m2 (b, c)
forall a c. (a -> m1 c) -> a -> m2 c
morph'
where
morph' :: (a -> m1 c) -> a -> m2 c
morph' a -> m1 c
m1F = m1 c -> m2 c
forall c. m1 c -> m2 c
morph (m1 c -> m2 c) -> (a -> m1 c) -> a -> m2 c
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> m1 c
m1F