{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
{-# 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)
#if !MIN_VERSION_transformers(0,6,0)
import Control.Monad.Trans.Error (ErrorT, Error)
#endif /* !MIN_VERSION_transformers(0,4,0) */
#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)
#if !MIN_VERSION_transformers(0,6,0)
import Control.Monad.Trans.List (ListT)
#endif /* !MIN_VERSION_transformers(0,4,0) */
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 m where
    type Ref m :: * -> *

    -- |Create a new reference
    newRef    :: a -> m (Ref m a)
    -- |Read the value of a reference
    readRef   :: Ref m a -> m a
    -- |Write a new value to a reference
    writeRef  :: Ref m a -> a -> m ()
    -- |Mutate the contents of a reference
    modifyRef :: Ref m a -> (a -> a) -> m ()
    modifyRef Ref m a
r a -> a
f = Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef Ref m a
r m a -> (a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef Ref m 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' :: Ref m a -> (a -> a) -> m ()
    modifyRef' Ref m a
r a -> a
f = Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef Ref m 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` Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef Ref m a
r a
x'

class (MonadRef m) => MonadAtomicRef m where
    -- |Atomically mutate the contents of a reference
    atomicModifyRef :: Ref m 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' :: Ref m a -> (a -> (a, b)) -> m b
    atomicModifyRef' Ref m a
r a -> (a, b)
f = do
        b
b <- Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef Ref m 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 (ST s) where
    type Ref (ST s) = STRef s

    newRef :: a -> ST s (Ref (ST s) a)
newRef     = a -> ST s (Ref (ST s) a)
forall a s. a -> ST s (STRef s a)
newSTRef
    readRef :: Ref (ST s) a -> ST s a
readRef    = Ref (ST s) a -> ST s a
forall s a. STRef s a -> ST s a
readSTRef
    writeRef :: Ref (ST s) a -> a -> ST s ()
writeRef   = Ref (ST s) a -> a -> ST s ()
forall s a. STRef s a -> a -> ST s ()
writeSTRef
    modifyRef :: Ref (ST s) a -> (a -> a) -> ST s ()
modifyRef  = Ref (ST 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' :: Ref (ST s) a -> (a -> a) -> ST s ()
modifyRef' = Ref (ST 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 IO where
    type Ref IO = IORef

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

instance MonadRef STM where
    type Ref STM = TVar

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

instance MonadRef m => MonadRef (ContT r m) where
    type Ref (ContT r m) = Ref m

    newRef :: a -> ContT r m (Ref (ContT r m) a)
newRef     a
r   = m (Ref m a) -> ContT r m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> ContT r m (Ref m a))
-> m (Ref m a) -> ContT r m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (ContT r m) a -> ContT r m a
readRef    Ref (ContT r m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (ContT r m) a
r
    writeRef :: Ref (ContT r m) a -> a -> ContT r m ()
writeRef   Ref (ContT r m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (ContT r m) a
r a
x
    modifyRef :: Ref (ContT r m) a -> (a -> a) -> ContT r m ()
modifyRef  Ref (ContT r m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (ContT r m) a
r a -> a
f
    modifyRef' :: Ref (ContT r m) a -> (a -> a) -> ContT r m ()
modifyRef' Ref (ContT r m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (ContT r m) a
r a -> a
f

#if !MIN_VERSION_transformers(0,6,0)
instance (Error e, MonadRef m) => MonadRef (ErrorT e m) where
    type Ref (ErrorT e m) = Ref m

    newRef :: a -> ErrorT e m (Ref (ErrorT e m) a)
newRef     a
r   = m (Ref m a) -> ErrorT e m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> ErrorT e m (Ref m a))
-> m (Ref m a) -> ErrorT e m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (ErrorT e m) a -> ErrorT e m a
readRef    Ref (ErrorT e m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (ErrorT e m) a
r
    writeRef :: Ref (ErrorT e m) a -> a -> ErrorT e m ()
writeRef   Ref (ErrorT e m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (ErrorT e m) a
r a
x
    modifyRef :: Ref (ErrorT e m) a -> (a -> a) -> ErrorT e m ()
modifyRef  Ref (ErrorT e m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (ErrorT e m) a
r a -> a
f
    modifyRef' :: Ref (ErrorT e m) a -> (a -> a) -> ErrorT e m ()
modifyRef' Ref (ErrorT e m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (ErrorT e m) a
r a -> a
f
#endif /* !MIN_VERSION_transformers(0,6,0) */

#if MIN_VERSION_transformers(0,4,0)
instance (MonadRef m) => MonadRef (ExceptT e m) where
    type Ref (ExceptT e m) = Ref m

    newRef :: a -> ExceptT e m (Ref (ExceptT e m) a)
newRef     a
r   = m (Ref m a) -> ExceptT e m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> ExceptT e m (Ref m a))
-> m (Ref m a) -> ExceptT e m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (ExceptT e m) a -> ExceptT e m a
readRef    Ref (ExceptT e m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (ExceptT e m) a
r
    writeRef :: Ref (ExceptT e m) a -> a -> ExceptT e m ()
writeRef   Ref (ExceptT e m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (ExceptT e m) a
r a
x
    modifyRef :: Ref (ExceptT e m) a -> (a -> a) -> ExceptT e m ()
modifyRef  Ref (ExceptT e m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (ExceptT e m) a
r a -> a
f
    modifyRef' :: Ref (ExceptT e m) a -> (a -> a) -> ExceptT e m ()
modifyRef' Ref (ExceptT e m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (ExceptT e m) a
r a -> a
f
#endif /* MIN_VERSION_transformers(0,4,0) */

instance MonadRef m => MonadRef (IdentityT m) where
    type Ref (IdentityT m) = Ref m

    newRef :: a -> IdentityT m (Ref (IdentityT m) a)
newRef     a
r   = m (Ref m a) -> IdentityT m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> IdentityT m (Ref m a))
-> m (Ref m a) -> IdentityT m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (IdentityT m) a -> IdentityT m a
readRef    Ref (IdentityT m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (IdentityT m) a
r
    writeRef :: Ref (IdentityT m) a -> a -> IdentityT m ()
writeRef   Ref (IdentityT m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (IdentityT m) a
r a
x
    modifyRef :: Ref (IdentityT m) a -> (a -> a) -> IdentityT m ()
modifyRef  Ref (IdentityT m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (IdentityT m) a
r a -> a
f
    modifyRef' :: Ref (IdentityT m) a -> (a -> a) -> IdentityT m ()
modifyRef' Ref (IdentityT m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (IdentityT m) a
r a -> a
f

#if !MIN_VERSION_transformers(0,6,0)
instance MonadRef m => MonadRef (ListT m) where
    type Ref (ListT m) = Ref m

    newRef :: a -> ListT m (Ref (ListT m) a)
newRef     a
r   = m (Ref m a) -> ListT m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> ListT m (Ref m a))
-> m (Ref m a) -> ListT m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (ListT m) a -> ListT m a
readRef    Ref (ListT m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (ListT m) a
r
    writeRef :: Ref (ListT m) a -> a -> ListT m ()
writeRef   Ref (ListT m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (ListT m) a
r a
x
    modifyRef :: Ref (ListT m) a -> (a -> a) -> ListT m ()
modifyRef  Ref (ListT m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (ListT m) a
r a -> a
f
    modifyRef' :: Ref (ListT m) a -> (a -> a) -> ListT m ()
modifyRef' Ref (ListT m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (ListT m) a
r a -> a
f
#endif /* !MIN_VERSION_transformers(0,6,0) */

instance MonadRef m => MonadRef (MaybeT m) where
    type Ref (MaybeT m) = Ref m

    newRef :: a -> MaybeT m (Ref (MaybeT m) a)
newRef     a
r   = m (Ref m a) -> MaybeT m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> MaybeT m (Ref m a))
-> m (Ref m a) -> MaybeT m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (MaybeT m) a -> MaybeT m a
readRef    Ref (MaybeT m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (MaybeT m) a
r
    writeRef :: Ref (MaybeT m) a -> a -> MaybeT m ()
writeRef   Ref (MaybeT m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (MaybeT m) a
r a
x
    modifyRef :: Ref (MaybeT m) a -> (a -> a) -> MaybeT m ()
modifyRef  Ref (MaybeT m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (MaybeT m) a
r a -> a
f
    modifyRef' :: Ref (MaybeT m) a -> (a -> a) -> MaybeT m ()
modifyRef' Ref (MaybeT m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (MaybeT m) a
r a -> a
f

instance MonadRef m => MonadRef (ReaderT r m) where
    type Ref (ReaderT r m) = Ref m

    newRef :: a -> ReaderT r m (Ref (ReaderT r m) a)
newRef     a
r   = m (Ref m a) -> ReaderT r m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> ReaderT r m (Ref m a))
-> m (Ref m a) -> ReaderT r m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (ReaderT r m) a -> ReaderT r m a
readRef    Ref (ReaderT r m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (ReaderT r m) a
r
    writeRef :: Ref (ReaderT r m) a -> a -> ReaderT r m ()
writeRef   Ref (ReaderT r m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (ReaderT r m) a
r a
x
    modifyRef :: Ref (ReaderT r m) a -> (a -> a) -> ReaderT r m ()
modifyRef  Ref (ReaderT r m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (ReaderT r m) a
r a -> a
f
    modifyRef' :: Ref (ReaderT r m) a -> (a -> a) -> ReaderT r m ()
modifyRef' Ref (ReaderT r m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (ReaderT r m) a
r a -> a
f

instance MonadRef m => MonadRef (Lazy.StateT s m) where
    type Ref (Lazy.StateT s m) = Ref m

    newRef :: a -> StateT s m (Ref (StateT s m) a)
newRef     a
r   = m (Ref m a) -> StateT s m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> StateT s m (Ref m a))
-> m (Ref m a) -> StateT s m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (StateT s m) a -> StateT s m a
readRef    Ref (StateT s m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (StateT s m) a
r
    writeRef :: Ref (StateT s m) a -> a -> StateT s m ()
writeRef   Ref (StateT s m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (StateT s m) a
r a
x
    modifyRef :: Ref (StateT s m) a -> (a -> a) -> StateT s m ()
modifyRef  Ref (StateT s m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (StateT s m) a
r a -> a
f
    modifyRef' :: Ref (StateT s m) a -> (a -> a) -> StateT s m ()
modifyRef' Ref (StateT s m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (StateT s m) a
r a -> a
f

instance MonadRef m => MonadRef (Strict.StateT s m) where
    type Ref (Strict.StateT s m) = Ref m

    newRef :: a -> StateT s m (Ref (StateT s m) a)
newRef     a
r   = m (Ref m a) -> StateT s m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> StateT s m (Ref m a))
-> m (Ref m a) -> StateT s m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (StateT s m) a -> StateT s m a
readRef    Ref (StateT s m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (StateT s m) a
r
    writeRef :: Ref (StateT s m) a -> a -> StateT s m ()
writeRef   Ref (StateT s m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (StateT s m) a
r a
x
    modifyRef :: Ref (StateT s m) a -> (a -> a) -> StateT s m ()
modifyRef  Ref (StateT s m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (StateT s m) a
r a -> a
f
    modifyRef' :: Ref (StateT s m) a -> (a -> a) -> StateT s m ()
modifyRef' Ref (StateT s m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (StateT s m) a
r a -> a
f

instance (Monoid w, MonadRef m) => MonadRef (Lazy.WriterT w m) where
    type Ref (Lazy.WriterT w m) = Ref m

    newRef :: a -> WriterT w m (Ref (WriterT w m) a)
newRef     a
r   = m (Ref m a) -> WriterT w m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> WriterT w m (Ref m a))
-> m (Ref m a) -> WriterT w m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (WriterT w m) a -> WriterT w m a
readRef    Ref (WriterT w m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (WriterT w m) a
r
    writeRef :: Ref (WriterT w m) a -> a -> WriterT w m ()
writeRef   Ref (WriterT w m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (WriterT w m) a
r a
x
    modifyRef :: Ref (WriterT w m) a -> (a -> a) -> WriterT w m ()
modifyRef  Ref (WriterT w m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (WriterT w m) a
r a -> a
f
    modifyRef' :: Ref (WriterT w m) a -> (a -> a) -> WriterT w m ()
modifyRef' Ref (WriterT w m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (WriterT w m) a
r a -> a
f

instance (Monoid w, MonadRef m) => MonadRef (Strict.WriterT w m) where
    type Ref (Strict.WriterT w m) = Ref m

    newRef :: a -> WriterT w m (Ref (WriterT w m) a)
newRef     a
r   = m (Ref m a) -> WriterT w m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Ref m a) -> WriterT w m (Ref m a))
-> m (Ref m a) -> WriterT w m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> m (Ref m a)
newRef     a
r
    readRef :: Ref (WriterT w m) a -> WriterT w m a
readRef    Ref (WriterT w m) 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
$ Ref m a -> m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef    Ref m a
Ref (WriterT w m) a
r
    writeRef :: Ref (WriterT w m) a -> a -> WriterT w m ()
writeRef   Ref (WriterT w m) 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
$ Ref m a -> a -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef   Ref m a
Ref (WriterT w m) a
r a
x
    modifyRef :: Ref (WriterT w m) a -> (a -> a) -> WriterT w m ()
modifyRef  Ref (WriterT w m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef  Ref m a
Ref (WriterT w m) a
r a -> a
f
    modifyRef' :: Ref (WriterT w m) a -> (a -> a) -> WriterT w m ()
modifyRef' Ref (WriterT w m) 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
$ Ref m a -> (a -> a) -> m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> (a -> a) -> m ()
modifyRef' Ref m a
Ref (WriterT w m) a
r a -> a
f

instance MonadAtomicRef IO where
    atomicModifyRef :: Ref IO a -> (a -> (a, b)) -> IO b
atomicModifyRef = Ref IO 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' :: Ref IO a -> (a -> (a, b)) -> IO b
atomicModifyRef' = Ref IO 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 (ST s) where
    atomicModifyRef :: Ref (ST s) a -> (a -> (a, b)) -> ST s b
atomicModifyRef Ref (ST s) a
r a -> (a, b)
f = do
      a
v <- Ref (ST s) a -> ST s a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef Ref (ST s) a
r
      let (a
a, b
b) = a -> (a, b)
f a
v
      Ref (ST s) a -> a -> ST s ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef Ref (ST s) a
r a
a
      b -> ST s b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
    atomicModifyRef' :: Ref (ST s) a -> (a -> (a, b)) -> ST s b
atomicModifyRef' Ref (ST s) a
r a -> (a, b)
f = do
      a
v <- Ref (ST s) a -> ST s a
forall (m :: * -> *) a. MonadRef m => Ref m a -> m a
readRef Ref (ST s) a
r
      let (a
a, b
b) = a -> (a, b)
f a
v
      Ref (ST s) a -> a -> ST s ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> m ()
writeRef Ref (ST s) a
r (a -> ST s ()) -> a -> ST s ()
forall a b. (a -> b) -> a -> b
$! a
a
      b -> ST s b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b

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

instance MonadAtomicRef m => MonadAtomicRef (ContT r m) where
    atomicModifyRef :: Ref (ContT r m) a -> (a -> (a, b)) -> ContT r m b
atomicModifyRef  Ref (ContT r m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef  Ref m a
Ref (ContT r m) a
r a -> (a, b)
f
    atomicModifyRef' :: Ref (ContT r m) a -> (a -> (a, b)) -> ContT r m b
atomicModifyRef' Ref (ContT r m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef' Ref m a
Ref (ContT r m) a
r a -> (a, b)
f

#if !MIN_VERSION_transformers(0,6,0)
instance (Error e, MonadAtomicRef m) => MonadAtomicRef (ErrorT e m) where
    atomicModifyRef :: Ref (ErrorT e m) a -> (a -> (a, b)) -> ErrorT e m b
atomicModifyRef  Ref (ErrorT e m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef  Ref m a
Ref (ErrorT e m) a
r a -> (a, b)
f
    atomicModifyRef' :: Ref (ErrorT e m) a -> (a -> (a, b)) -> ErrorT e m b
atomicModifyRef' Ref (ErrorT e m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef' Ref m a
Ref (ErrorT e m) a
r a -> (a, b)
f
#endif /* !MIN_VERSION_transformers(0,6,0) */

instance MonadAtomicRef m => MonadAtomicRef (IdentityT m) where
    atomicModifyRef :: Ref (IdentityT m) a -> (a -> (a, b)) -> IdentityT m b
atomicModifyRef  Ref (IdentityT m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef  Ref m a
Ref (IdentityT m) a
r a -> (a, b)
f
    atomicModifyRef' :: Ref (IdentityT m) a -> (a -> (a, b)) -> IdentityT m b
atomicModifyRef' Ref (IdentityT m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef' Ref m a
Ref (IdentityT m) a
r a -> (a, b)
f

#if !MIN_VERSION_transformers(0,6,0)
instance MonadAtomicRef m => MonadAtomicRef (ListT m) where
    atomicModifyRef :: Ref (ListT m) a -> (a -> (a, b)) -> ListT m b
atomicModifyRef  Ref (ListT m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef  Ref m a
Ref (ListT m) a
r a -> (a, b)
f
    atomicModifyRef' :: Ref (ListT m) a -> (a -> (a, b)) -> ListT m b
atomicModifyRef' Ref (ListT m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef' Ref m a
Ref (ListT m) a
r a -> (a, b)
f
#endif /* !MIN_VERSION_transformers(0,6,0) */

instance MonadAtomicRef m => MonadAtomicRef (MaybeT m) where
    atomicModifyRef :: Ref (MaybeT m) a -> (a -> (a, b)) -> MaybeT m b
atomicModifyRef  Ref (MaybeT m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef  Ref m a
Ref (MaybeT m) a
r a -> (a, b)
f
    atomicModifyRef' :: Ref (MaybeT m) a -> (a -> (a, b)) -> MaybeT m b
atomicModifyRef' Ref (MaybeT m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef' Ref m a
Ref (MaybeT m) a
r a -> (a, b)
f

instance MonadAtomicRef m => MonadAtomicRef (ReaderT r m) where
    atomicModifyRef :: Ref (ReaderT r m) a -> (a -> (a, b)) -> ReaderT r m b
atomicModifyRef  Ref (ReaderT r m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef  Ref m a
Ref (ReaderT r m) a
r a -> (a, b)
f
    atomicModifyRef' :: Ref (ReaderT r m) a -> (a -> (a, b)) -> ReaderT r m b
atomicModifyRef' Ref (ReaderT r m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef' Ref m a
Ref (ReaderT r m) a
r a -> (a, b)
f

instance MonadAtomicRef m => MonadAtomicRef (Lazy.StateT s m) where
    atomicModifyRef :: Ref (StateT s m) a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef  Ref (StateT s m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef  Ref m a
Ref (StateT s m) a
r a -> (a, b)
f
    atomicModifyRef' :: Ref (StateT s m) a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef' Ref (StateT s m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef' Ref m a
Ref (StateT s m) a
r a -> (a, b)
f

instance MonadAtomicRef m => MonadAtomicRef (Strict.StateT s m) where
    atomicModifyRef :: Ref (StateT s m) a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef  Ref (StateT s m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef  Ref m a
Ref (StateT s m) a
r a -> (a, b)
f
    atomicModifyRef' :: Ref (StateT s m) a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef' Ref (StateT s m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef' Ref m a
Ref (StateT s m) a
r a -> (a, b)
f

instance (Monoid w, MonadAtomicRef m) => MonadAtomicRef (Lazy.WriterT w m) where
    atomicModifyRef :: Ref (WriterT w m) a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef  Ref (WriterT w m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef  Ref m a
Ref (WriterT w m) a
r a -> (a, b)
f
    atomicModifyRef' :: Ref (WriterT w m) a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef' Ref (WriterT w m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef' Ref m a
Ref (WriterT w m) a
r a -> (a, b)
f

instance (Monoid w, MonadAtomicRef m) => MonadAtomicRef (Strict.WriterT w m) where
    atomicModifyRef :: Ref (WriterT w m) a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef  Ref (WriterT w m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef  Ref m a
Ref (WriterT w m) a
r a -> (a, b)
f
    atomicModifyRef' :: Ref (WriterT w m) a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef' Ref (WriterT w m) 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
$ Ref m a -> (a -> (a, b)) -> m b
forall (m :: * -> *) a b.
MonadAtomicRef m =>
Ref m a -> (a -> (a, b)) -> m b
atomicModifyRef' Ref m a
Ref (WriterT w m) a
r a -> (a, b)
f