{-# LANGUAGE CPP #-}
{-# LANGUAGE Rank2Types #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.MonadicStreamFunction.Core
(
MSF
, constM
, arrM
, liftBaseM
, liftBaseS
, (^>>>)
, (>>>^)
, liftTransS
, morphS
, morphGS
, feedback
, reactimate
, embed
, module X
)
where
import Control.Arrow (Arrow (..), (>>>))
import qualified Control.Arrow as X
import Control.Category as C (id, (.))
import Control.Monad.Base (MonadBase, liftBase)
import Control.Monad.Trans.Class (MonadTrans, lift)
import Prelude hiding (id, sum, (.))
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative (Applicative(..))
#endif
import Data.MonadicStreamFunction.InternalCore (MSF, embed, feedback, morphGS,
reactimate)
instance Monad m => Arrow (MSF m) where
arr :: forall b c. (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 a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> m c) -> (b -> c) -> b -> m c
forall b c a. (b -> c) -> (a -> b) -> a -> 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 :: forall b c d. 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 (b
a, d
c) -> do
(c
b, c
msf') <- b -> m (c, c)
f b
a
((c, d), c) -> m ((c, d), c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((c
b, d
c), c
msf')
instance Monad m => Functor (MSF m a) where
fmap :: forall a b. (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 b c. (b -> c) -> MSF m b c
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 :: forall a. a -> MSF m a a
pure = (a -> a) -> MSF m a a
forall b c. (b -> c) -> MSF m b c
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 b c a. (b -> c) -> (a -> b) -> 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 -> a -> a
forall a b. a -> b -> a
const
MSF m a (a -> b)
fs <*> :: forall a b. 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 b c c'. MSF m b c -> MSF m b c' -> MSF m b (c, c')
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 b c. (b -> c) -> MSF m 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 b. (a -> b) -> a -> b
($))
constM :: Monad m => m b -> MSF m a b
constM :: forall (m :: * -> *) b a. Monad m => 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 b c a. (b -> c) -> (a -> b) -> a -> c
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 :: forall (m :: * -> *) a b. Monad m => (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 a b. m a -> (a -> m b) -> m b
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 a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b
b -> (b, c) -> m (b, c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, c
c)) MSF m a a
forall a. 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 :: forall (m2 :: * -> *) (m1 :: * -> *) a b.
(Monad m2, MonadBase m1 m2) =>
(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 b c a. (b -> c) -> (a -> b) -> a -> c
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 α. m1 α -> m2 α
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase (m1 b -> m2 b) -> (a -> m1 b) -> a -> m2 b
forall b c a. (b -> c) -> (a -> b) -> a -> c
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 :: forall (m2 :: * -> *) (m1 :: * -> *) a b.
(Monad m2, MonadBase m1 m2) =>
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 m1 c -> m2 c
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 ^>>> :: forall (m1 :: * -> *) (m2 :: * -> *) a b c.
MonadBase m1 m2 =>
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 >>>^ :: forall (m1 :: * -> *) (m2 :: * -> *) a b c.
MonadBase m1 m2 =>
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 :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(MonadTrans t, Monad m, Monad (t m)) =>
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 m c -> t m c
forall c. m c -> t m c
forall (m :: * -> *) a. Monad m => m a -> t m a
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 (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
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 (a -> m1 (b, c)) -> a -> m2 (b, c)
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 b c a. (b -> c) -> (a -> b) -> 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 -> m1 c
m1F