module Simulation.Aivika.Trans.Channel
(
Channel(..),
delayChannel,
delayChannelM,
sinkSignal,
traceChannel) where
import qualified Control.Category as C
import Control.Monad
import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Dynamics
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.Signal
import Simulation.Aivika.Trans.Composite
newtype Channel m a b =
Channel { runChannel :: Signal m a -> Composite m (Signal m b)
}
instance MonadDES m => C.Category (Channel m) where
{-# INLINE id #-}
id = Channel return
{-# INLINE (.) #-}
(Channel g) . (Channel f) =
Channel $ \a -> f a >>= g
delayChannel :: MonadDES m
=> Double
-> Channel m a a
{-# INLINABLE delayChannel #-}
delayChannel delay =
Channel $ \a -> return $ delaySignal delay a
delayChannelM :: MonadDES m
=> Event m Double
-> Channel m a a
{-# INLINABLE delayChannelM #-}
delayChannelM delay =
Channel $ \a -> return $ delaySignalM delay a
sinkSignal :: MonadDES m => Signal m a -> Composite m ()
{-# INLINABLE sinkSignal #-}
sinkSignal a =
do h <- liftEvent $
handleSignal a $
const $ return ()
disposableComposite h
traceChannel :: MonadDES m => String -> Channel m a b -> Channel m a b
{-# INLINABLE traceChannel #-}
traceChannel message (Channel f) =
Channel $ \a ->
do b <- f a
return $
traceSignal message b