module Simulation.Aivika.Trans.Internal.Parameter
(
ParameterLift(..),
runParameter,
runParameters,
catchParameter,
finallyParameter,
throwParameter,
simulationIndex,
simulationCount,
simulationSpecs,
simulationSession,
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.Session
import Simulation.Aivika.Trans.Generator
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.Comp.IO
import Simulation.Aivika.Trans.Internal.Specs
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 :: MonadComp m => Parameter m a -> Specs m -> m a
runParameter (Parameter m) sc =
do s <- newSession
q <- newEventQueue s sc
g <- newGenerator s $ spcGeneratorType sc
m Run { runSpecs = sc,
runSession = s,
runIndex = 1,
runCount = 1,
runEventQueue = q,
runGenerator = g }
runParameters :: MonadComp m => Parameter m a -> Specs m -> Int -> [m a]
runParameters (Parameter m) sc runs = map f [1 .. runs]
where f i = do s <- newSession
q <- newEventQueue s sc
g <- newGenerator s $ spcGeneratorType sc
m Run { runSpecs = sc,
runSession = s,
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 MonadCompTrans Parameter where
liftComp = Parameter . const
class ParameterLift t where
liftParameter :: MonadComp m => Parameter m a -> t m a
instance ParameterLift Parameter where
liftParameter = id
catchParameter :: (MonadComp 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 :: MonadComp m => Parameter m a -> Parameter m b -> Parameter m a
finallyParameter (Parameter m) (Parameter m') =
Parameter $ \r ->
finallyComp (m r) (m' r)
throwParameter :: (MonadComp m, Exception e) => e -> Parameter m a
throwParameter = throw
instance MonadFix m => MonadFix (Parameter m) where
mfix f =
Parameter $ \r ->
do { rec { a <- invokeParameter r (f a) }; return a }
memoParameter :: Parameter IO a -> IO (Parameter IO a)
memoParameter x =
do lock <- newMVar ()
dict <- newIORef M.empty
return $ Parameter $ \r ->
do let i = runIndex r
m <- readIORef dict
if M.member i m
then do let Just v = M.lookup i m
return v
else withMVar lock $
\() -> do { m <- readIORef dict;
if M.member i m
then do let Just v = M.lookup i m
return v
else do v <- invokeParameter r x
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
simulationSession :: Monad m => Parameter m (Session m)
simulationSession =
Parameter $ return . runSession