module Simulation.Aivika.RealTime.Ref.Base.Lazy () where
import Data.IORef
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika.Trans.Internal.Types
import Simulation.Aivika.Trans.Ref.Base.Lazy
import Simulation.Aivika.RealTime.Internal.RT
instance (Monad m, MonadIO m) => MonadRef (RT m) where
newtype Ref (RT m) a = Ref { refValue :: IORef a }
newRef a =
Simulation $ \r ->
do x <- liftIO $ newIORef a
return Ref { refValue = x }
readRef r = Event $ \p ->
liftIO $ readIORef (refValue r)
writeRef r a = Event $ \p ->
liftIO $ writeIORef (refValue r) a
modifyRef r f = Event $ \p ->
do a <- liftIO $ readIORef (refValue r)
let b = f a
liftIO $ writeIORef (refValue r) b
equalRef (Ref r1) (Ref r2) = (r1 == r2)
instance MonadIO m => MonadRef0 (RT m) where
newRef0 a =
do x <- liftIO $ newIORef a
return Ref { refValue = x }