module Simulation.Aivika.Trans.Ref.Plain
(Ref,
newRef,
readRef,
writeRef,
modifyRef) where
import Data.IORef
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika.Trans.Session
import Simulation.Aivika.Trans.ProtoRef
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Internal.Simulation
import Simulation.Aivika.Trans.Internal.Event
newtype Ref m a =
Ref { refValue :: ProtoRef m a }
newRef :: MonadComp m => a -> Simulation m (Ref m a)
newRef a =
Simulation $ \r ->
do let s = runSession r
x <- newProtoRef s a
return Ref { refValue = x }
readRef :: MonadComp m => Ref m a -> Event m a
readRef r = Event $ \p -> readProtoRef (refValue r)
writeRef :: MonadComp m => Ref m a -> a -> Event m ()
writeRef r a = Event $ \p ->
a `seq` writeProtoRef (refValue r) a
modifyRef :: MonadComp m => Ref m a -> (a -> a) -> Event m ()
modifyRef r f = Event $ \p ->
do a <- readProtoRef (refValue r)
let b = f a
b `seq` writeProtoRef (refValue r) b