module Simulation.Aivika.Trans.Dynamics.Memo
(MonadMemo(..),
unzipDynamics,
unzip0Dynamics) where
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika.Trans.Internal.Simulation
import Simulation.Aivika.Trans.Internal.Dynamics
class Monad m => MonadMemo m where
memoDynamics :: Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics :: Dynamics m e -> Simulation m (Dynamics m e)
iterateDynamics :: Dynamics m () -> Simulation m (Dynamics m ())
unzipDynamics :: MonadMemo m => Dynamics m (a, b) -> Simulation m (Dynamics m a, Dynamics m b)
{-# INLINABLE unzipDynamics #-}
unzipDynamics :: Dynamics m (a, b) -> Simulation m (Dynamics m a, Dynamics m b)
unzipDynamics Dynamics m (a, b)
m =
(Run m -> m (Dynamics m a, Dynamics m b))
-> Simulation m (Dynamics m a, Dynamics m b)
forall (m :: * -> *) a. (Run m -> m a) -> Simulation m a
Simulation ((Run m -> m (Dynamics m a, Dynamics m b))
-> Simulation m (Dynamics m a, Dynamics m b))
-> (Run m -> m (Dynamics m a, Dynamics m b))
-> Simulation m (Dynamics m a, Dynamics m b)
forall a b. (a -> b) -> a -> b
$ \Run m
r ->
do Dynamics m (a, b)
m' <- Run m -> Simulation m (Dynamics m (a, b)) -> m (Dynamics m (a, b))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Dynamics m (a, b) -> Simulation m (Dynamics m (a, b))
forall (m :: * -> *) e.
MonadMemo m =>
Dynamics m e -> Simulation m (Dynamics m e)
memoDynamics Dynamics m (a, b)
m)
let ma :: Dynamics m a
ma =
(Point m -> m a) -> Dynamics m a
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m a) -> Dynamics m a)
-> (Point m -> m a) -> Dynamics m a
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (a
a, b
_) <- Point m -> Dynamics m (a, b) -> m (a, b)
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m (a, b)
m'
a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
mb :: Dynamics m b
mb =
(Point m -> m b) -> Dynamics m b
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m b) -> Dynamics m b)
-> (Point m -> m b) -> Dynamics m b
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (a
_, b
b) <- Point m -> Dynamics m (a, b) -> m (a, b)
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m (a, b)
m'
b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
(Dynamics m a, Dynamics m b) -> m (Dynamics m a, Dynamics m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Dynamics m a
ma, Dynamics m b
mb)
unzip0Dynamics :: MonadMemo m => Dynamics m (a, b) -> Simulation m (Dynamics m a, Dynamics m b)
{-# INLINABLE unzip0Dynamics #-}
unzip0Dynamics :: Dynamics m (a, b) -> Simulation m (Dynamics m a, Dynamics m b)
unzip0Dynamics Dynamics m (a, b)
m =
(Run m -> m (Dynamics m a, Dynamics m b))
-> Simulation m (Dynamics m a, Dynamics m b)
forall (m :: * -> *) a. (Run m -> m a) -> Simulation m a
Simulation ((Run m -> m (Dynamics m a, Dynamics m b))
-> Simulation m (Dynamics m a, Dynamics m b))
-> (Run m -> m (Dynamics m a, Dynamics m b))
-> Simulation m (Dynamics m a, Dynamics m b)
forall a b. (a -> b) -> a -> b
$ \Run m
r ->
do Dynamics m (a, b)
m' <- Run m -> Simulation m (Dynamics m (a, b)) -> m (Dynamics m (a, b))
forall (m :: * -> *) a. Run m -> Simulation m a -> m a
invokeSimulation Run m
r (Dynamics m (a, b) -> Simulation m (Dynamics m (a, b))
forall (m :: * -> *) e.
MonadMemo m =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics Dynamics m (a, b)
m)
let ma :: Dynamics m a
ma =
(Point m -> m a) -> Dynamics m a
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m a) -> Dynamics m a)
-> (Point m -> m a) -> Dynamics m a
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (a
a, b
_) <- Point m -> Dynamics m (a, b) -> m (a, b)
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m (a, b)
m'
a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
mb :: Dynamics m b
mb =
(Point m -> m b) -> Dynamics m b
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m b) -> Dynamics m b)
-> (Point m -> m b) -> Dynamics m b
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
do (a
_, b
b) <- Point m -> Dynamics m (a, b) -> m (a, b)
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m (a, b)
m'
b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
(Dynamics m a, Dynamics m b) -> m (Dynamics m a, Dynamics m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Dynamics m a
ma, Dynamics m b
mb)