module Simulation.Aivika.Trans.Gate
(Gate,
newGate,
newGateOpened,
newGateClosed,
openGate,
closeGate,
invertGate,
gateOpened,
gateClosed,
awaitGateOpened,
awaitGateClosed,
gateChanged_) where
import Control.Monad
import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.Process
import Simulation.Aivika.Trans.Signal
import Simulation.Aivika.Trans.Ref
data Gate m = Gate { forall (m :: * -> *). Gate m -> Ref m Bool
gateRef :: Ref m Bool }
newGate :: MonadDES m => Bool -> Simulation m (Gate m)
{-# INLINE newGate #-}
newGate :: forall (m :: * -> *). MonadDES m => Bool -> Simulation m (Gate m)
newGate Bool
opened =
do Ref m Bool
r <- forall (m :: * -> *) a. MonadDES m => a -> Simulation m (Ref m a)
newRef Bool
opened
forall (m :: * -> *) a. Monad m => a -> m a
return Gate { gateRef :: Ref m Bool
gateRef = Ref m Bool
r }
newGateOpened :: MonadDES m => Simulation m (Gate m)
{-# INLINE newGateOpened #-}
newGateOpened :: forall (m :: * -> *). MonadDES m => Simulation m (Gate m)
newGateOpened = forall (m :: * -> *). MonadDES m => Bool -> Simulation m (Gate m)
newGate Bool
True
newGateClosed :: MonadDES m => Simulation m (Gate m)
{-# INLINE newGateClosed #-}
newGateClosed :: forall (m :: * -> *). MonadDES m => Simulation m (Gate m)
newGateClosed = forall (m :: * -> *). MonadDES m => Bool -> Simulation m (Gate m)
newGate Bool
False
openGate :: MonadDES m => Gate m -> Event m ()
{-# INLINE openGate #-}
openGate :: forall (m :: * -> *). MonadDES m => Gate m -> Event m ()
openGate Gate m
gate =
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Gate m -> Ref m Bool
gateRef Gate m
gate) Bool
True
closeGate :: MonadDES m => Gate m -> Event m ()
{-# INLINE closeGate #-}
closeGate :: forall (m :: * -> *). MonadDES m => Gate m -> Event m ()
closeGate Gate m
gate =
forall (m :: * -> *) a. MonadDES m => Ref m a -> a -> Event m ()
writeRef (forall (m :: * -> *). Gate m -> Ref m Bool
gateRef Gate m
gate) Bool
False
invertGate :: MonadDES m => Gate m -> Event m ()
{-# INLINABLE invertGate #-}
invertGate :: forall (m :: * -> *). MonadDES m => Gate m -> Event m ()
invertGate Gate m
gate =
forall (m :: * -> *) a.
MonadDES m =>
Ref m a -> (a -> a) -> Event m ()
modifyRef (forall (m :: * -> *). Gate m -> Ref m Bool
gateRef Gate m
gate) Bool -> Bool
not
gateOpened :: MonadDES m => Gate m -> Event m Bool
{-# INLINE gateOpened #-}
gateOpened :: forall (m :: * -> *). MonadDES m => Gate m -> Event m Bool
gateOpened Gate m
gate =
forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Gate m -> Ref m Bool
gateRef Gate m
gate)
gateClosed :: MonadDES m => Gate m -> Event m Bool
{-# INLINE gateClosed #-}
gateClosed :: forall (m :: * -> *). MonadDES m => Gate m -> Event m Bool
gateClosed Gate m
gate =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Gate m -> Ref m Bool
gateRef Gate m
gate)
awaitGateOpened :: MonadDES m => Gate m -> Process m ()
{-# INLINABLE awaitGateOpened #-}
awaitGateOpened :: forall (m :: * -> *). MonadDES m => Gate m -> Process m ()
awaitGateOpened Gate m
gate =
do Bool
f <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Gate m -> Ref m Bool
gateRef Gate m
gate)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
f forall a b. (a -> b) -> a -> b
$
do forall (m :: * -> *) a. MonadDES m => Signal m a -> Process m a
processAwait forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Signal m ()
refChanged_ (forall (m :: * -> *). Gate m -> Ref m Bool
gateRef Gate m
gate)
forall (m :: * -> *). MonadDES m => Gate m -> Process m ()
awaitGateOpened Gate m
gate
awaitGateClosed :: MonadDES m => Gate m -> Process m ()
{-# INLINABLE awaitGateClosed #-}
awaitGateClosed :: forall (m :: * -> *). MonadDES m => Gate m -> Process m ()
awaitGateClosed Gate m
gate =
do Bool
f <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Event m a
readRef (forall (m :: * -> *). Gate m -> Ref m Bool
gateRef Gate m
gate)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
f forall a b. (a -> b) -> a -> b
$
do forall (m :: * -> *) a. MonadDES m => Signal m a -> Process m a
processAwait forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadDES m => Ref m a -> Signal m ()
refChanged_ (forall (m :: * -> *). Gate m -> Ref m Bool
gateRef Gate m
gate)
forall (m :: * -> *). MonadDES m => Gate m -> Process m ()
awaitGateClosed Gate m
gate
gateChanged_ :: MonadDES m => Gate m -> Signal m ()
{-# INLINE gateChanged_ #-}
gateChanged_ :: forall (m :: * -> *). MonadDES m => Gate m -> Signal m ()
gateChanged_ Gate m
gate =
forall (m :: * -> *) a. MonadDES m => Ref m a -> Signal m ()
refChanged_ (forall (m :: * -> *). Gate m -> Ref m Bool
gateRef Gate m
gate)