module Simulation.Aivika.Trans.Internal.Parameter
(
Parameter(..),
ParameterLift(..),
invokeParameter,
runParameter,
runParameters,
catchParameter,
finallyParameter,
throwParameter,
simulationIndex,
simulationCount,
simulationSpecs,
simulationEventQueue,
starttime,
stoptime,
dt,
generatorParameter,
memoParameter,
tableParameter) where
import Control.Exception
import Control.Concurrent.MVar
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Fix
import Control.Applicative
import Data.IORef
import qualified Data.IntMap as M
import Data.Array
import Simulation.Aivika.Trans.Exception
import Simulation.Aivika.Trans.Generator
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Trans.Internal.Types
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Concurrent.MVar
instance Monad m => Monad (Parameter m) where
return a = Parameter $ \r -> return a
(Parameter m) >>= k =
Parameter $ \r ->
do a <- m r
let Parameter m' = k a
m' r
runParameter :: MonadDES m => Parameter m a -> Specs m -> m a
runParameter (Parameter m) sc =
do q <- newEventQueue sc
g <- newGenerator $ spcGeneratorType sc
m Run { runSpecs = sc,
runIndex = 1,
runCount = 1,
runEventQueue = q,
runGenerator = g }
runParameters :: MonadDES m => Parameter m a -> Specs m -> Int -> [m a]
runParameters (Parameter m) sc runs = map f [1 .. runs]
where f i = do q <- newEventQueue sc
g <- newGenerator $ spcGeneratorType sc
m Run { runSpecs = sc,
runIndex = i,
runCount = runs,
runEventQueue = q,
runGenerator = g }
simulationIndex :: Monad m => Parameter m Int
simulationIndex = Parameter $ return . runIndex
simulationCount :: Monad m => Parameter m Int
simulationCount = Parameter $ return . runCount
simulationSpecs :: Monad m => Parameter m (Specs m)
simulationSpecs = Parameter $ return . runSpecs
generatorParameter :: Monad m => Parameter m (Generator m)
generatorParameter = Parameter $ return . runGenerator
instance Functor m => Functor (Parameter m) where
fmap f (Parameter x) = Parameter $ \r -> fmap f $ x r
instance Applicative m => Applicative (Parameter m) where
pure = Parameter . const . pure
(Parameter x) <*> (Parameter y) = Parameter $ \r -> x r <*> y r
liftMP :: Monad m => (a -> b) -> Parameter m a -> Parameter m b
liftMP f (Parameter x) =
Parameter $ \r -> do { a <- x r; return $ f a }
liftM2P :: Monad m => (a -> b -> c) -> Parameter m a -> Parameter m b -> Parameter m c
liftM2P f (Parameter x) (Parameter y) =
Parameter $ \r -> do { a <- x r; b <- y r; return $ f a b }
instance (Num a, Monad m) => Num (Parameter m a) where
x + y = liftM2P (+) x y
x y = liftM2P () x y
x * y = liftM2P (*) x y
negate = liftMP negate
abs = liftMP abs
signum = liftMP signum
fromInteger i = return $ fromInteger i
instance (Fractional a, Monad m) => Fractional (Parameter m a) where
x / y = liftM2P (/) x y
recip = liftMP recip
fromRational t = return $ fromRational t
instance (Floating a, Monad m) => Floating (Parameter m a) where
pi = return pi
exp = liftMP exp
log = liftMP log
sqrt = liftMP sqrt
x ** y = liftM2P (**) x y
sin = liftMP sin
cos = liftMP cos
tan = liftMP tan
asin = liftMP asin
acos = liftMP acos
atan = liftMP atan
sinh = liftMP sinh
cosh = liftMP cosh
tanh = liftMP tanh
asinh = liftMP asinh
acosh = liftMP acosh
atanh = liftMP atanh
instance MonadTrans Parameter where
lift = Parameter . const
instance MonadIO m => MonadIO (Parameter m) where
liftIO = Parameter . const . liftIO
instance Monad m => MonadCompTrans Parameter m where
liftComp = Parameter . const
class ParameterLift t m where
liftParameter :: Parameter m a -> t m a
instance Monad m => ParameterLift Parameter m where
liftParameter = id
catchParameter :: (MonadException m, Exception e) => Parameter m a -> (e -> Parameter m a) -> Parameter m a
catchParameter (Parameter m) h =
Parameter $ \r ->
catchComp (m r) $ \e ->
let Parameter m' = h e in m' r
finallyParameter :: MonadException m => Parameter m a -> Parameter m b -> Parameter m a
finallyParameter (Parameter m) (Parameter m') =
Parameter $ \r ->
finallyComp (m r) (m' r)
throwParameter :: (MonadException m, Exception e) => e -> Parameter m a
throwParameter e =
Parameter $ \r ->
throwComp e
instance MonadFix m => MonadFix (Parameter m) where
mfix f =
Parameter $ \r ->
do { rec { a <- invokeParameter r (f a) }; return a }
memoParameter :: (MonadComp m, MonadIO m) => Parameter m a -> m (Parameter m a)
memoParameter x =
do lock <- liftIO $ newMVar ()
dict <- liftIO $ newIORef M.empty
return $ Parameter $ \r ->
do let i = runIndex r
m <- liftIO $ readIORef dict
if M.member i m
then do let Just v = M.lookup i m
return v
else withMVarComp lock $
\() -> do { m <- liftIO $ readIORef dict;
if M.member i m
then do let Just v = M.lookup i m
return v
else do v <- invokeParameter r x
liftIO $ writeIORef dict $ M.insert i v m
return v }
tableParameter :: Monad m => Array Int a -> Parameter m a
tableParameter t =
do i <- simulationIndex
return $ t ! (((i i1) `mod` n) + i1)
where (i1, i2) = bounds t
n = i2 i1 + 1
starttime :: Monad m => Parameter m Double
starttime =
Parameter $ return . spcStartTime . runSpecs
stoptime :: Monad m => Parameter m Double
stoptime =
Parameter $ return . spcStopTime . runSpecs
dt :: Monad m => Parameter m Double
dt =
Parameter $ return . spcDT . runSpecs
simulationEventQueue :: Monad m => Parameter m (EventQueue m)
simulationEventQueue =
Parameter $ return . runEventQueue