{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleInstances #-}

-- |
-- Module     : Simulation.Aivika.Lattice.Ref.Base.Strict
-- 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 strict mutable references, where
-- 'LIO' is an instance of 'MonadRef' and 'MonadRef0'.
--
module Simulation.Aivika.Lattice.Ref.Base.Strict () where

import Simulation.Aivika.Trans.Internal.Types
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Ref.Base.Strict
import Simulation.Aivika.Trans.Observable

import Simulation.Aivika.Lattice.Internal.LIO
import qualified Simulation.Aivika.Lattice.Internal.Ref.Strict as R
import Simulation.Aivika.Lattice.Internal.Estimate
import Simulation.Aivika.Lattice.Internal.Event

-- | The implementation of mutable references.
instance MonadRef LIO where

  -- | The mutable reference.
  newtype Ref LIO a = Ref { Ref LIO a -> Ref a
refValue :: R.Ref a }

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

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

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

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

  {-# INLINE equalRef #-}
  equalRef :: Ref LIO a -> Ref LIO 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 LIO where

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

-- | An instance of the specified type class.
instance Observable (Ref LIO) (Estimate LIO) where

  {-# INLINE readObservable #-}
  readObservable :: Ref LIO a -> Estimate LIO a
readObservable (Ref r) = Ref a -> Estimate LIO a
forall a. Ref a -> Estimate LIO a
estimateStrictRef Ref a
r