{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}

-- |
-- Module      :  Control.Monad.Ref
-- Copyright   :  (c) Harvard University 2006-2011
--                (c) Geoffrey Mainland 2011-2014
-- License     :  BSD-style
-- Maintainer  :  Geoffrey Mainland <mainland@cs.drexel.edu>
--
-- Stability   :  experimental
-- Portability :  non-portable

module Control.Monad.Ref (
    MonadRef(..),
    MonadAtomicRef(..)
  ) where

import Control.Concurrent.STM (STM)
import Control.Concurrent.STM.TVar (TVar,
                                    newTVar,
                                    readTVar,
                                    writeTVar)
import Control.Monad.ST (ST)
import Control.Monad.Trans.Cont (ContT)
import Control.Monad.Trans.Error (ErrorT, Error)
#if MIN_VERSION_transformers(0,4,0)
import Control.Monad.Trans.Except (ExceptT)
#endif /* MIN_VERSION_transformers(0,4,0) */
import Control.Monad.Trans.Identity (IdentityT)
import Control.Monad.Trans.List (ListT)
import Control.Monad.Trans.Maybe (MaybeT)
import Control.Monad.Trans.Reader (ReaderT)
import Control.Monad.Trans.State.Lazy as Lazy (StateT)
import Control.Monad.Trans.State.Strict as Strict (StateT)
import Control.Monad.Trans.Writer.Lazy as Lazy (WriterT)
import Control.Monad.Trans.Writer.Strict as Strict (WriterT)
import Control.Monad.Trans.Class (lift)
import Data.IORef (IORef,
#if MIN_VERSION_base(4,6,0)
                   atomicModifyIORef',
                   modifyIORef',
#endif /* MIN_VERSION_base(4,6,0) */
                   atomicModifyIORef,
                   modifyIORef,
                   newIORef,
                   readIORef,
                   writeIORef)
import Data.Monoid (Monoid)
import Data.STRef (STRef,
#if MIN_VERSION_base(4,6,0)
                   modifySTRef',
#endif /* MIN_VERSION_base(4,6,0) */
                   modifySTRef,
                   newSTRef,
                   readSTRef,
                   writeSTRef)

-- |The 'MonadRef' type class abstracts over the details of manipulating
-- references, allowing one to write code that uses references and can operate
-- in any monad that supports reference operations.

class (Monad m) => MonadRef r m | m -> r where
    -- |Create a new reference
    newRef    :: a -> m (r a)
    -- |Read the value of a reference
    readRef   :: r a -> m a
    -- |Write a new value to a reference
    writeRef  :: r a -> a -> m ()
    -- |Mutate the contents of a reference
    modifyRef :: r a -> (a -> a) -> m ()
    modifyRef r a
r a -> a
f = r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r m a -> (a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r (a -> m ()) -> (a -> a) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f
    -- |Strict version of 'modifyRef'
    modifyRef' :: r a -> (a -> a) -> m ()
    modifyRef' r a
r a -> a
f = r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r m a -> (a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
x -> let x' :: a
x' = a -> a
f a
x in a
x' a -> m () -> m ()
`seq` r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x'

class (MonadRef r m) => MonadAtomicRef r m | m -> r where
    -- |Atomically mutate the contents of a reference
    atomicModifyRef :: r a -> (a -> (a, b)) -> m b
    -- |Strict version of atomicModifyRef. This forces both the value stored in
    -- the reference as well as the value returned.
    atomicModifyRef' :: r a -> (a -> (a, b)) -> m b
    atomicModifyRef' r a
r a -> (a, b)
f = do
        b
b <- r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef r a
r
                (\a
x -> let (a
a, b
b) = a -> (a, b)
f a
x
                        in (a
a, a
a a -> b -> b
`seq` b
b))
        b
b b -> m b -> m b
`seq` b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b

instance MonadRef (STRef s) (ST s) where
    newRef :: a -> ST s (STRef s a)
newRef     = a -> ST s (STRef s a)
forall a s. a -> ST s (STRef s a)
newSTRef
    readRef :: STRef s a -> ST s a
readRef    = STRef s a -> ST s a
forall s a. STRef s a -> ST s a
readSTRef
    writeRef :: STRef s a -> a -> ST s ()
writeRef   = STRef s a -> a -> ST s ()
forall s a. STRef s a -> a -> ST s ()
writeSTRef
    modifyRef :: STRef s a -> (a -> a) -> ST s ()
modifyRef  = STRef s a -> (a -> a) -> ST s ()
forall s a. STRef s a -> (a -> a) -> ST s ()
modifySTRef
#if MIN_VERSION_base(4,6,0)
    modifyRef' :: STRef s a -> (a -> a) -> ST s ()
modifyRef' = STRef s a -> (a -> a) -> ST s ()
forall s a. STRef s a -> (a -> a) -> ST s ()
modifySTRef'
#endif /* MIN_VERSION_base(4,6,0) */

instance MonadRef IORef IO where
    newRef :: a -> IO (IORef a)
newRef     = a -> IO (IORef a)
forall a. a -> IO (IORef a)
newIORef
    readRef :: IORef a -> IO a
readRef    = IORef a -> IO a
forall a. IORef a -> IO a
readIORef
    writeRef :: IORef a -> a -> IO ()
writeRef   = IORef a -> a -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef
    modifyRef :: IORef a -> (a -> a) -> IO ()
modifyRef  = IORef a -> (a -> a) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef
#if MIN_VERSION_base(4,6,0)
    modifyRef' :: IORef a -> (a -> a) -> IO ()
modifyRef' = IORef a -> (a -> a) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef'
#endif /* MIN_VERSION_base(4,6,0) */

instance MonadRef TVar STM where
    newRef :: a -> STM (TVar a)
newRef    = a -> STM (TVar a)
forall a. a -> STM (TVar a)
newTVar
    readRef :: TVar a -> STM a
readRef   = TVar a -> STM a
forall a. TVar a -> STM a
readTVar
    writeRef :: TVar a -> a -> STM ()
writeRef  = TVar a -> a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar

instance MonadRef r m => MonadRef r (ContT r' m) where
    newRef :: a -> ContT r' m (r a)
newRef     a
r   = m (r a) -> ContT r' m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> ContT r' m (r a)) -> m (r a) -> ContT r' m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> ContT r' m a
readRef    r a
r   = m a -> ContT r' m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ContT r' m a) -> m a -> ContT r' m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> ContT r' m ()
writeRef   r a
r a
x = m () -> ContT r' m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ContT r' m ()) -> m () -> ContT r' m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> ContT r' m ()
modifyRef  r a
r a -> a
f = m () -> ContT r' m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ContT r' m ()) -> m () -> ContT r' m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> ContT r' m ()
modifyRef' r a
r a -> a
f = m () -> ContT r' m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ContT r' m ()) -> m () -> ContT r' m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f

instance (Error e, MonadRef r m) => MonadRef r (ErrorT e m) where
    newRef :: a -> ErrorT e m (r a)
newRef     a
r   = m (r a) -> ErrorT e m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> ErrorT e m (r a)) -> m (r a) -> ErrorT e m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> ErrorT e m a
readRef    r a
r   = m a -> ErrorT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ErrorT e m a) -> m a -> ErrorT e m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> ErrorT e m ()
writeRef   r a
r a
x = m () -> ErrorT e m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ErrorT e m ()) -> m () -> ErrorT e m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> ErrorT e m ()
modifyRef  r a
r a -> a
f = m () -> ErrorT e m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ErrorT e m ()) -> m () -> ErrorT e m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> ErrorT e m ()
modifyRef' r a
r a -> a
f = m () -> ErrorT e m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ErrorT e m ()) -> m () -> ErrorT e m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f

#if MIN_VERSION_transformers(0,4,0)
instance (MonadRef r m) => MonadRef r (ExceptT e m) where
    newRef :: a -> ExceptT e m (r a)
newRef     a
r   = m (r a) -> ExceptT e m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> ExceptT e m (r a)) -> m (r a) -> ExceptT e m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> ExceptT e m a
readRef    r a
r   = m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ExceptT e m a) -> m a -> ExceptT e m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> ExceptT e m ()
writeRef   r a
r a
x = m () -> ExceptT e m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ExceptT e m ()) -> m () -> ExceptT e m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> ExceptT e m ()
modifyRef  r a
r a -> a
f = m () -> ExceptT e m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ExceptT e m ()) -> m () -> ExceptT e m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> ExceptT e m ()
modifyRef' r a
r a -> a
f = m () -> ExceptT e m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ExceptT e m ()) -> m () -> ExceptT e m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f
#endif /* MIN_VERSION_transformers(0,4,0) */

instance MonadRef r m => MonadRef r (IdentityT m) where
    newRef :: a -> IdentityT m (r a)
newRef     a
r   = m (r a) -> IdentityT m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> IdentityT m (r a)) -> m (r a) -> IdentityT m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> IdentityT m a
readRef    r a
r   = m a -> IdentityT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> IdentityT m a) -> m a -> IdentityT m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> IdentityT m ()
writeRef   r a
r a
x = m () -> IdentityT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> IdentityT m ()) -> m () -> IdentityT m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> IdentityT m ()
modifyRef  r a
r a -> a
f = m () -> IdentityT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> IdentityT m ()) -> m () -> IdentityT m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> IdentityT m ()
modifyRef' r a
r a -> a
f = m () -> IdentityT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> IdentityT m ()) -> m () -> IdentityT m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f

instance MonadRef r m => MonadRef r (ListT m) where
    newRef :: a -> ListT m (r a)
newRef     a
r   = m (r a) -> ListT m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> ListT m (r a)) -> m (r a) -> ListT m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> ListT m a
readRef    r a
r   = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ListT m a) -> m a -> ListT m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> ListT m ()
writeRef   r a
r a
x = m () -> ListT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ListT m ()) -> m () -> ListT m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> ListT m ()
modifyRef  r a
r a -> a
f = m () -> ListT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ListT m ()) -> m () -> ListT m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> ListT m ()
modifyRef' r a
r a -> a
f = m () -> ListT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ListT m ()) -> m () -> ListT m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f

instance MonadRef r m => MonadRef r (MaybeT m) where
    newRef :: a -> MaybeT m (r a)
newRef     a
r   = m (r a) -> MaybeT m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> MaybeT m (r a)) -> m (r a) -> MaybeT m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> MaybeT m a
readRef    r a
r   = m a -> MaybeT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> MaybeT m a) -> m a -> MaybeT m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> MaybeT m ()
writeRef   r a
r a
x = m () -> MaybeT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> MaybeT m ()) -> m () -> MaybeT m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> MaybeT m ()
modifyRef  r a
r a -> a
f = m () -> MaybeT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> MaybeT m ()) -> m () -> MaybeT m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> MaybeT m ()
modifyRef' r a
r a -> a
f = m () -> MaybeT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> MaybeT m ()) -> m () -> MaybeT m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f

instance MonadRef r m => MonadRef r (ReaderT r' m) where
    newRef :: a -> ReaderT r' m (r a)
newRef     a
r   = m (r a) -> ReaderT r' m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> ReaderT r' m (r a)) -> m (r a) -> ReaderT r' m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> ReaderT r' m a
readRef    r a
r   = m a -> ReaderT r' m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ReaderT r' m a) -> m a -> ReaderT r' m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> ReaderT r' m ()
writeRef   r a
r a
x = m () -> ReaderT r' m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ReaderT r' m ()) -> m () -> ReaderT r' m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> ReaderT r' m ()
modifyRef  r a
r a -> a
f = m () -> ReaderT r' m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ReaderT r' m ()) -> m () -> ReaderT r' m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> ReaderT r' m ()
modifyRef' r a
r a -> a
f = m () -> ReaderT r' m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ReaderT r' m ()) -> m () -> ReaderT r' m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f

instance MonadRef r m => MonadRef r (Lazy.StateT s m) where
    newRef :: a -> StateT s m (r a)
newRef     a
r   = m (r a) -> StateT s m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> StateT s m (r a)) -> m (r a) -> StateT s m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> StateT s m a
readRef    r a
r   = m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> StateT s m a) -> m a -> StateT s m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> StateT s m ()
writeRef   r a
r a
x = m () -> StateT s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> StateT s m ()) -> m () -> StateT s m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> StateT s m ()
modifyRef  r a
r a -> a
f = m () -> StateT s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> StateT s m ()) -> m () -> StateT s m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> StateT s m ()
modifyRef' r a
r a -> a
f = m () -> StateT s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> StateT s m ()) -> m () -> StateT s m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f

instance MonadRef r m => MonadRef r (Strict.StateT s m) where
    newRef :: a -> StateT s m (r a)
newRef     a
r   = m (r a) -> StateT s m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> StateT s m (r a)) -> m (r a) -> StateT s m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> StateT s m a
readRef    r a
r   = m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> StateT s m a) -> m a -> StateT s m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> StateT s m ()
writeRef   r a
r a
x = m () -> StateT s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> StateT s m ()) -> m () -> StateT s m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> StateT s m ()
modifyRef  r a
r a -> a
f = m () -> StateT s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> StateT s m ()) -> m () -> StateT s m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> StateT s m ()
modifyRef' r a
r a -> a
f = m () -> StateT s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> StateT s m ()) -> m () -> StateT s m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f

instance (Monoid w, MonadRef r m) => MonadRef r (Lazy.WriterT w m) where
    newRef :: a -> WriterT w m (r a)
newRef     a
r   = m (r a) -> WriterT w m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> WriterT w m (r a)) -> m (r a) -> WriterT w m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> WriterT w m a
readRef    r a
r   = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a) -> m a -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> WriterT w m ()
writeRef   r a
r a
x = m () -> WriterT w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> WriterT w m ()) -> m () -> WriterT w m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> WriterT w m ()
modifyRef  r a
r a -> a
f = m () -> WriterT w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> WriterT w m ()) -> m () -> WriterT w m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> WriterT w m ()
modifyRef' r a
r a -> a
f = m () -> WriterT w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> WriterT w m ()) -> m () -> WriterT w m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f

instance (Monoid w, MonadRef r m) => MonadRef r (Strict.WriterT w m) where
    newRef :: a -> WriterT w m (r a)
newRef     a
r   = m (r a) -> WriterT w m (r a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (r a) -> WriterT w m (r a)) -> m (r a) -> WriterT w m (r a)
forall a b. (a -> b) -> a -> b
$ a -> m (r a)
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef     a
r
    readRef :: r a -> WriterT w m a
readRef    r a
r   = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a) -> m a -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ r a -> m a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef    r a
r
    writeRef :: r a -> a -> WriterT w m ()
writeRef   r a
r a
x = m () -> WriterT w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> WriterT w m ()) -> m () -> WriterT w m ()
forall a b. (a -> b) -> a -> b
$ r a -> a -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef   r a
r a
x
    modifyRef :: r a -> (a -> a) -> WriterT w m ()
modifyRef  r a
r a -> a
f = m () -> WriterT w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> WriterT w m ()) -> m () -> WriterT w m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef  r a
r a -> a
f
    modifyRef' :: r a -> (a -> a) -> WriterT w m ()
modifyRef' r a
r a -> a
f = m () -> WriterT w m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> WriterT w m ()) -> m () -> WriterT w m ()
forall a b. (a -> b) -> a -> b
$ r a -> (a -> a) -> m ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f

instance MonadAtomicRef IORef IO where
    atomicModifyRef :: IORef a -> (a -> (a, b)) -> IO b
atomicModifyRef = IORef a -> (a -> (a, b)) -> IO b
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef
#if MIN_VERSION_base(4,6,0)
    atomicModifyRef' :: IORef a -> (a -> (a, b)) -> IO b
atomicModifyRef' = IORef a -> (a -> (a, b)) -> IO b
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef'
#endif /* MIN_VERSION_base(4,6,0) */

-- Since there's no forking, it's automatically atomic.
instance MonadAtomicRef (STRef s) (ST s) where
    atomicModifyRef :: STRef s a -> (a -> (a, b)) -> ST s b
atomicModifyRef STRef s a
r a -> (a, b)
f = do
      a
x <- STRef s a -> ST s a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef STRef s a
r
      let (a
x', b
y) = a -> (a, b)
f a
x
      STRef s a -> a -> ST s ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef STRef s a
r a
x'
      b -> ST s b
forall (m :: * -> *) a. Monad m => a -> m a
return b
y

    atomicModifyRef' :: STRef s a -> (a -> (a, b)) -> ST s b
atomicModifyRef' STRef s a
r a -> (a, b)
f = do
      a
x <- STRef s a -> ST s a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef STRef s a
r
      let (a
x', b
y) = a -> (a, b)
f a
x
      STRef s a -> a -> ST s ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef STRef s a
r (a -> ST s ()) -> a -> ST s ()
forall a b. (a -> b) -> a -> b
$! a
x'
      b -> ST s b
forall (m :: * -> *) a. Monad m => a -> m a
return b
y

instance MonadAtomicRef TVar STM where
    atomicModifyRef :: TVar a -> (a -> (a, b)) -> STM b
atomicModifyRef TVar a
r a -> (a, b)
f = do a
x <- TVar a -> STM a
forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef TVar a
r
                             let (a
x', b
y) = a -> (a, b)
f a
x
                             TVar a -> a -> STM ()
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef TVar a
r a
x'
                             b -> STM b
forall (m :: * -> *) a. Monad m => a -> m a
return b
y

instance MonadAtomicRef r m => MonadAtomicRef r (ContT r' m) where
    atomicModifyRef :: r a -> (a -> (a, b)) -> ContT r' m b
atomicModifyRef  r a
r a -> (a, b)
f = m b -> ContT r' m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> ContT r' m b) -> m b -> ContT r' m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef  r a
r a -> (a, b)
f
    atomicModifyRef' :: r a -> (a -> (a, b)) -> ContT r' m b
atomicModifyRef' r a
r a -> (a, b)
f = m b -> ContT r' m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> ContT r' m b) -> m b -> ContT r' m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f

instance (Error e, MonadAtomicRef r m) => MonadAtomicRef r (ErrorT e m) where
    atomicModifyRef :: r a -> (a -> (a, b)) -> ErrorT e m b
atomicModifyRef  r a
r a -> (a, b)
f = m b -> ErrorT e m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> ErrorT e m b) -> m b -> ErrorT e m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef  r a
r a -> (a, b)
f
    atomicModifyRef' :: r a -> (a -> (a, b)) -> ErrorT e m b
atomicModifyRef' r a
r a -> (a, b)
f = m b -> ErrorT e m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> ErrorT e m b) -> m b -> ErrorT e m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f

instance MonadAtomicRef r m => MonadAtomicRef r (IdentityT m) where
    atomicModifyRef :: r a -> (a -> (a, b)) -> IdentityT m b
atomicModifyRef  r a
r a -> (a, b)
f = m b -> IdentityT m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> IdentityT m b) -> m b -> IdentityT m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef  r a
r a -> (a, b)
f
    atomicModifyRef' :: r a -> (a -> (a, b)) -> IdentityT m b
atomicModifyRef' r a
r a -> (a, b)
f = m b -> IdentityT m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> IdentityT m b) -> m b -> IdentityT m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f

instance MonadAtomicRef r m => MonadAtomicRef r (ListT m) where
    atomicModifyRef :: r a -> (a -> (a, b)) -> ListT m b
atomicModifyRef  r a
r a -> (a, b)
f = m b -> ListT m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> ListT m b) -> m b -> ListT m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef  r a
r a -> (a, b)
f
    atomicModifyRef' :: r a -> (a -> (a, b)) -> ListT m b
atomicModifyRef' r a
r a -> (a, b)
f = m b -> ListT m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> ListT m b) -> m b -> ListT m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f

instance MonadAtomicRef r m => MonadAtomicRef r (MaybeT m) where
    atomicModifyRef :: r a -> (a -> (a, b)) -> MaybeT m b
atomicModifyRef  r a
r a -> (a, b)
f = m b -> MaybeT m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> MaybeT m b) -> m b -> MaybeT m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef  r a
r a -> (a, b)
f
    atomicModifyRef' :: r a -> (a -> (a, b)) -> MaybeT m b
atomicModifyRef' r a
r a -> (a, b)
f = m b -> MaybeT m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> MaybeT m b) -> m b -> MaybeT m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f

instance MonadAtomicRef r m => MonadAtomicRef r (ReaderT r' m) where
    atomicModifyRef :: r a -> (a -> (a, b)) -> ReaderT r' m b
atomicModifyRef  r a
r a -> (a, b)
f = m b -> ReaderT r' m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> ReaderT r' m b) -> m b -> ReaderT r' m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef  r a
r a -> (a, b)
f
    atomicModifyRef' :: r a -> (a -> (a, b)) -> ReaderT r' m b
atomicModifyRef' r a
r a -> (a, b)
f = m b -> ReaderT r' m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> ReaderT r' m b) -> m b -> ReaderT r' m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f

instance MonadAtomicRef r m => MonadAtomicRef r (Lazy.StateT s m) where
    atomicModifyRef :: r a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef  r a
r a -> (a, b)
f = m b -> StateT s m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> StateT s m b) -> m b -> StateT s m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef  r a
r a -> (a, b)
f
    atomicModifyRef' :: r a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef' r a
r a -> (a, b)
f = m b -> StateT s m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> StateT s m b) -> m b -> StateT s m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f

instance MonadAtomicRef r m => MonadAtomicRef r (Strict.StateT s m) where
    atomicModifyRef :: r a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef  r a
r a -> (a, b)
f = m b -> StateT s m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> StateT s m b) -> m b -> StateT s m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef  r a
r a -> (a, b)
f
    atomicModifyRef' :: r a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef' r a
r a -> (a, b)
f = m b -> StateT s m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> StateT s m b) -> m b -> StateT s m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f

instance (Monoid w, MonadAtomicRef r m) => MonadAtomicRef r (Lazy.WriterT w m) where
    atomicModifyRef :: r a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef  r a
r a -> (a, b)
f = m b -> WriterT w m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> WriterT w m b) -> m b -> WriterT w m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef  r a
r a -> (a, b)
f
    atomicModifyRef' :: r a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef' r a
r a -> (a, b)
f = m b -> WriterT w m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> WriterT w m b) -> m b -> WriterT w m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f

instance (Monoid w, MonadAtomicRef r m) => MonadAtomicRef r (Strict.WriterT w m) where
    atomicModifyRef :: r a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef  r a
r a -> (a, b)
f = m b -> WriterT w m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> WriterT w m b) -> m b -> WriterT w m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef  r a
r a -> (a, b)
f
    atomicModifyRef' :: r a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef' r a
r a -> (a, b)
f = m b -> WriterT w m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> WriterT w m b) -> m b -> WriterT w m b
forall a b. (a -> b) -> a -> b
$ r a -> (a -> (a, b)) -> m b
forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f