{-# LANGUAGE TypeFamilies #-}
module Simulation.Aivika.IO.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
instance MonadRef IO where
{-# SPECIALISE instance MonadRef IO #-}
newtype Ref IO a = Ref { refValue :: IORef a }
{-# INLINE newRef #-}
newRef a =
Simulation $ \r ->
do x <- liftIO $ newIORef a
return Ref { refValue = x }
{-# INLINE readRef #-}
readRef r = Event $ \p ->
liftIO $ readIORef (refValue r)
{-# INLINE writeRef #-}
writeRef r a = Event $ \p ->
liftIO $ writeIORef (refValue r) a
{-# INLINE modifyRef #-}
modifyRef r f = Event $ \p ->
do a <- liftIO $ readIORef (refValue r)
let b = f a
liftIO $ writeIORef (refValue r) b
{-# INLINE equalRef #-}
equalRef (Ref r1) (Ref r2) = (r1 == r2)
instance MonadRef0 IO where
{-# SPECIALISE instance MonadRef0 IO #-}
{-# INLINE newRef0 #-}
newRef0 a =
do x <- liftIO $ newIORef a
return Ref { refValue = x }