{-# LANGUAGE Rank2Types #-}
module Data.MonadicStreamFunction.InternalCore where
import Control.Category (Category (..))
import Prelude hiding (id, sum, (.))
data MSF m a b = MSF { forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF :: a -> m (b, MSF m a b) }
instance Monad m => Category (MSF m) where
id :: forall a. MSF m a a
id = MSF m a a
forall a. MSF m a a
go
where
go :: MSF m b b
go = (b -> m (b, MSF m b b)) -> MSF m b b
forall (m :: * -> *) a b. (a -> m (b, MSF m a b)) -> MSF m a b
MSF ((b -> m (b, MSF m b b)) -> MSF m b b)
-> (b -> m (b, MSF m b b)) -> MSF m b b
forall a b. (a -> b) -> a -> b
$ \b
a -> (b, MSF m b b) -> m (b, MSF m b b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
a, MSF m b b
go)
MSF m b c
sf2 . :: forall b c a. MSF m b c -> MSF m a b -> MSF m a c
. MSF m a b
sf1 = (a -> m (c, MSF m a c)) -> MSF m a c
forall (m :: * -> *) a b. (a -> m (b, MSF m a b)) -> MSF m a b
MSF ((a -> m (c, MSF m a c)) -> MSF m a c)
-> (a -> m (c, MSF m a c)) -> MSF m a c
forall a b. (a -> b) -> a -> b
$ \a
a -> do
(b
b, MSF m a b
sf1') <- MSF m a b -> a -> m (b, MSF m a b)
forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF MSF m a b
sf1 a
a
(c
c, MSF m b c
sf2') <- MSF m b c -> b -> m (c, MSF m b c)
forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF MSF m b c
sf2 b
b
let sf' :: MSF m a c
sf' = MSF m b c
sf2' MSF m b c -> MSF m a b -> MSF m a c
forall b c a. MSF m b c -> MSF m a b -> MSF m a c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. MSF m a b
sf1'
c
c c -> m (c, MSF m a c) -> m (c, MSF m a c)
forall a b. a -> b -> b
`seq` (c, MSF m a c) -> m (c, MSF m a c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (c
c, MSF m a c
sf')
morphGS :: Monad m2
=> (forall c . (a1 -> m1 (b1, c)) -> (a2 -> m2 (b2, c)))
-> MSF m1 a1 b1
-> MSF m2 a2 b2
morphGS :: 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. (a1 -> m1 (b1, c)) -> a2 -> m2 (b2, c)
morph MSF m1 a1 b1
msf = (a2 -> m2 (b2, MSF m2 a2 b2)) -> MSF m2 a2 b2
forall (m :: * -> *) a b. (a -> m (b, MSF m a b)) -> MSF m a b
MSF ((a2 -> m2 (b2, MSF m2 a2 b2)) -> MSF m2 a2 b2)
-> (a2 -> m2 (b2, MSF m2 a2 b2)) -> MSF m2 a2 b2
forall a b. (a -> b) -> a -> b
$ \a2
a2 -> do
(b2
b2, MSF m1 a1 b1
msf') <- (a1 -> m1 (b1, MSF m1 a1 b1)) -> a2 -> m2 (b2, MSF m1 a1 b1)
forall c. (a1 -> m1 (b1, c)) -> a2 -> m2 (b2, c)
morph (MSF m1 a1 b1 -> a1 -> m1 (b1, MSF m1 a1 b1)
forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF MSF m1 a1 b1
msf) a2
a2
(b2, MSF m2 a2 b2) -> m2 (b2, MSF m2 a2 b2)
forall a. a -> m2 a
forall (m :: * -> *) a. Monad m => a -> m a
return (b2
b2, (forall c. (a1 -> m1 (b1, c)) -> a2 -> m2 (b2, c))
-> MSF m1 a1 b1 -> MSF m2 a2 b2
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 (a1 -> m1 (b1, c)) -> a2 -> m2 (b2, c)
forall c. (a1 -> m1 (b1, c)) -> a2 -> m2 (b2, c)
morph MSF m1 a1 b1
msf')
feedback :: Monad m => c -> MSF m (a, c) (b, c) -> MSF m a b
feedback :: forall (m :: * -> *) c a b.
Monad m =>
c -> MSF m (a, c) (b, c) -> MSF m a b
feedback c
c MSF m (a, c) (b, c)
sf = (a -> m (b, MSF m a b)) -> MSF m a b
forall (m :: * -> *) a b. (a -> m (b, MSF m a b)) -> MSF m a b
MSF ((a -> m (b, MSF m a b)) -> MSF m a b)
-> (a -> m (b, MSF m a b)) -> MSF m a b
forall a b. (a -> b) -> a -> b
$ \a
a -> do
((b
b', c
c'), MSF m (a, c) (b, c)
sf') <- MSF m (a, c) (b, c) -> (a, c) -> m ((b, c), MSF m (a, c) (b, c))
forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF MSF m (a, c) (b, c)
sf (a
a, c
c)
(b, MSF m a b) -> m (b, MSF m a b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b', c -> MSF m (a, c) (b, c) -> MSF m a b
forall (m :: * -> *) c a b.
Monad m =>
c -> MSF m (a, c) (b, c) -> MSF m a b
feedback c
c' MSF m (a, c) (b, c)
sf')
embed :: Monad m => MSF m a b -> [a] -> m [b]
embed :: forall (m :: * -> *) a b. Monad m => MSF m a b -> [a] -> m [b]
embed MSF m a b
_ [] = [b] -> m [b]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return []
embed MSF m a b
sf (a
a:[a]
as) = do
(b
b, MSF m a b
sf') <- MSF m a b -> a -> m (b, MSF m a b)
forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF MSF m a b
sf a
a
[b]
bs <- MSF m a b -> [a] -> m [b]
forall (m :: * -> *) a b. Monad m => MSF m a b -> [a] -> m [b]
embed MSF m a b
sf' [a]
as
[b] -> m [b]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
bb -> [b] -> [b]
forall a. a -> [a] -> [a]
:[b]
bs)
reactimate :: Monad m => MSF m () () -> m ()
reactimate :: forall (m :: * -> *). Monad m => MSF m () () -> m ()
reactimate MSF m () ()
sf = do
(()
_, MSF m () ()
sf') <- MSF m () () -> () -> m ((), MSF m () ())
forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF MSF m () ()
sf ()
MSF m () () -> m ()
forall (m :: * -> *). Monad m => MSF m () () -> m ()
reactimate MSF m () ()
sf'