{-# LANGUAGE TypeFamilies, FlexibleInstances #-}

-- |
-- Module     : Simulation.Aivika.Branch.Ref.Base.Lazy
-- Copyright  : Copyright (c) 2016-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 7.10.3
--
-- Here is an implementation of lazy mutable references, where
-- 'BR' can be an instance of 'MonadRef' and 'MonadRef0'.
--
module Simulation.Aivika.Branch.Ref.Base.Lazy () where

import Simulation.Aivika.Trans.Internal.Types
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Ref.Base.Lazy

import Simulation.Aivika.Branch.Internal.BR
import qualified Simulation.Aivika.Branch.Internal.Ref.Lazy as R

-- | The implementation of mutable references.
instance MonadRef (BR IO) where

  -- | The mutable reference.
  newtype Ref (BR IO) a = Ref { Ref (BR IO) a -> Ref a
refValue :: R.Ref a }

  {-# INLINE newRef #-}
  newRef :: a -> Simulation (BR IO) (Ref (BR IO) a)
newRef = (Ref a -> Ref (BR IO) a)
-> Simulation (BR IO) (Ref a) -> Simulation (BR IO) (Ref (BR IO) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ref a -> Ref (BR IO) a
forall a. Ref a -> Ref (BR IO) a
Ref (Simulation (BR IO) (Ref a) -> Simulation (BR IO) (Ref (BR IO) a))
-> (a -> Simulation (BR IO) (Ref a))
-> a
-> Simulation (BR IO) (Ref (BR IO) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Simulation (BR IO) (Ref a)
forall a. a -> Simulation (BR IO) (Ref a)
R.newRef 

  {-# INLINE readRef #-}
  readRef :: Ref (BR IO) a -> Event (BR IO) a
readRef (Ref r) = Ref a -> Event (BR IO) a
forall a. Ref a -> Event (BR IO) a
R.readRef Ref a
r

  {-# INLINE writeRef #-}
  writeRef :: Ref (BR IO) a -> a -> Event (BR IO) ()
writeRef (Ref r) = Ref a -> a -> Event (BR IO) ()
forall a. Ref a -> a -> Event (BR IO) ()
R.writeRef Ref a
r

  {-# INLINE modifyRef #-}
  modifyRef :: Ref (BR IO) a -> (a -> a) -> Event (BR IO) ()
modifyRef (Ref r) = Ref a -> (a -> a) -> Event (BR IO) ()
forall a. Ref a -> (a -> a) -> Event (BR IO) ()
R.modifyRef Ref a
r

  {-# INLINE equalRef #-}
  equalRef :: Ref (BR IO) a -> Ref (BR IO) a -> Bool
equalRef (Ref r1) (Ref r2) = (Ref a
r1 Ref a -> Ref a -> Bool
forall a. Eq a => a -> a -> Bool
== Ref a
r2)

-- | A subtype of mutable references that can be created under more weak conditions.
instance MonadRef0 (BR IO) where

  {-# INLINE newRef0 #-}
  newRef0 :: a -> BR IO (Ref (BR IO) a)
newRef0 = (Ref a -> Ref (BR IO) a) -> BR IO (Ref a) -> BR IO (Ref (BR IO) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ref a -> Ref (BR IO) a
forall a. Ref a -> Ref (BR IO) a
Ref (BR IO (Ref a) -> BR IO (Ref (BR IO) a))
-> (a -> BR IO (Ref a)) -> a -> BR IO (Ref (BR IO) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> BR IO (Ref a)
forall a. a -> BR IO (Ref a)
R.newRef0