{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
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,6,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,6,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)
class (Monad m) => MonadRef r m | m -> r where
newRef :: a -> m (r a)
readRef :: r a -> m a
writeRef :: r a -> a -> m ()
modifyRef :: r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f = forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f
modifyRef' :: r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f = forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r 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' seq :: forall a b. a -> b -> b
`seq` 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
atomicModifyRef :: r a -> (a -> (a, b)) -> m b
atomicModifyRef' :: r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f = do
b
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 seq :: forall a b. a -> b -> b
`seq` b
b))
b
b seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return b
b
instance MonadRef (STRef s) (ST s) where
newRef :: forall a. a -> ST s (STRef s a)
newRef = forall a s. a -> ST s (STRef s a)
newSTRef
readRef :: forall a. STRef s a -> ST s a
readRef = forall s a. STRef s a -> ST s a
readSTRef
writeRef :: forall a. STRef s a -> a -> ST s ()
writeRef = forall s a. STRef s a -> a -> ST s ()
writeSTRef
modifyRef :: forall a. STRef s a -> (a -> a) -> ST s ()
modifyRef = forall s a. STRef s a -> (a -> a) -> ST s ()
modifySTRef
#if MIN_VERSION_base(4,6,0)
modifyRef' :: forall a. STRef s a -> (a -> a) -> ST s ()
modifyRef' = forall s a. STRef s a -> (a -> a) -> ST s ()
modifySTRef'
#endif /* MIN_VERSION_base(4,6,0) */
instance MonadRef IORef IO where
newRef :: forall a. a -> IO (IORef a)
newRef = forall a. a -> IO (IORef a)
newIORef
readRef :: forall a. IORef a -> IO a
readRef = forall a. IORef a -> IO a
readIORef
writeRef :: forall a. IORef a -> a -> IO ()
writeRef = forall a. IORef a -> a -> IO ()
writeIORef
modifyRef :: forall a. IORef a -> (a -> a) -> IO ()
modifyRef = forall a. IORef a -> (a -> a) -> IO ()
modifyIORef
#if MIN_VERSION_base(4,6,0)
modifyRef' :: forall a. IORef a -> (a -> a) -> IO ()
modifyRef' = forall a. IORef a -> (a -> a) -> IO ()
modifyIORef'
#endif /* MIN_VERSION_base(4,6,0) */
instance MonadRef TVar STM where
newRef :: forall a. a -> STM (TVar a)
newRef = forall a. a -> STM (TVar a)
newTVar
readRef :: forall a. TVar a -> STM a
readRef = forall a. TVar a -> STM a
readTVar
writeRef :: forall a. TVar a -> a -> STM ()
writeRef = forall a. TVar a -> a -> STM ()
writeTVar
instance MonadRef r m => MonadRef r (ContT r' m) where
newRef :: forall a. a -> ContT r' m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> ContT r' m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> ContT r' m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> ContT r' m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> ContT r' m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f
#if !MIN_VERSION_transformers(0,6,0)
instance (Error e, MonadRef r m) => MonadRef r (ErrorT e m) where
newRef :: forall a. a -> ErrorT e m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> ErrorT e m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> ErrorT e m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> ErrorT e m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> ErrorT e m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f
#endif /* !MIN_VERSION_transformers(0,6,0) */
#if MIN_VERSION_transformers(0,4,0)
instance (MonadRef r m) => MonadRef r (ExceptT e m) where
newRef :: forall a. a -> ExceptT e m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> ExceptT e m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> ExceptT e m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> ExceptT e m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> ExceptT e m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ 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 :: forall a. a -> IdentityT m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> IdentityT m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> IdentityT m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> IdentityT m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> IdentityT m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f
#if !MIN_VERSION_transformers(0,6,0)
instance MonadRef r m => MonadRef r (ListT m) where
newRef :: forall a. a -> ListT m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> ListT m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> ListT m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> ListT m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> ListT m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef' r a
r a -> a
f
#endif /* !MIN_VERSION_transformers(0,6,0) */
instance MonadRef r m => MonadRef r (MaybeT m) where
newRef :: forall a. a -> MaybeT m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> MaybeT m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> MaybeT m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> MaybeT m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> MaybeT m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ 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 :: forall a. a -> ReaderT r' m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> ReaderT r' m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> ReaderT r' m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> ReaderT r' m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> ReaderT r' m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ 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 :: forall a. a -> StateT s m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> StateT s m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> StateT s m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> StateT s m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> StateT s m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ 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 :: forall a. a -> StateT s m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> StateT s m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> StateT s m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> StateT s m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> StateT s m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ 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 :: forall a. a -> WriterT w m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> WriterT w m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> WriterT w m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> WriterT w m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> WriterT w m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ 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 :: forall a. a -> WriterT w m (r a)
newRef a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => a -> m (r a)
newRef a
r
readRef :: forall a. r a -> WriterT w m a
readRef r a
r = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a. MonadRef r m => r a -> m a
readRef r a
r
writeRef :: forall a. r a -> a -> WriterT w m ()
writeRef r a
r a
x = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef r a
r a
x
modifyRef :: forall a. r a -> (a -> a) -> WriterT w m ()
modifyRef r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> (a -> a) -> m ()
modifyRef r a
r a -> a
f
modifyRef' :: forall a. r a -> (a -> a) -> WriterT w m ()
modifyRef' r a
r a -> a
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ 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 :: forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyRef = forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef
#if MIN_VERSION_base(4,6,0)
atomicModifyRef' :: forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyRef' = forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef'
#endif /* MIN_VERSION_base(4,6,0) */
instance MonadAtomicRef (STRef s) (ST s) where
atomicModifyRef :: forall a b. STRef s a -> (a -> (a, b)) -> ST s b
atomicModifyRef STRef s a
r a -> (a, b)
f = do
a
x <- 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
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef STRef s a
r a
x'
forall (m :: * -> *) a. Monad m => a -> m a
return b
y
atomicModifyRef' :: forall a b. STRef s a -> (a -> (a, b)) -> ST s b
atomicModifyRef' STRef s a
r a -> (a, b)
f = do
a
x <- 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
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef STRef s a
r forall a b. (a -> b) -> a -> b
$! a
x'
forall (m :: * -> *) a. Monad m => a -> m a
return b
y
instance MonadAtomicRef TVar STM where
atomicModifyRef :: forall a b. TVar a -> (a -> (a, b)) -> STM b
atomicModifyRef TVar a
r a -> (a, b)
f = do a
x <- 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
forall (r :: * -> *) (m :: * -> *) a.
MonadRef r m =>
r a -> a -> m ()
writeRef TVar a
r a
x'
forall (m :: * -> *) a. Monad m => a -> m a
return b
y
instance MonadAtomicRef r m => MonadAtomicRef r (ContT r' m) where
atomicModifyRef :: forall a b. r a -> (a -> (a, b)) -> ContT r' m b
atomicModifyRef r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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' :: forall a b. r a -> (a -> (a, b)) -> ContT r' m b
atomicModifyRef' r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f
#if !MIN_VERSION_transformers(0,6,0)
instance (Error e, MonadAtomicRef r m) => MonadAtomicRef r (ErrorT e m) where
atomicModifyRef :: forall a b. r a -> (a -> (a, b)) -> ErrorT e m b
atomicModifyRef r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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' :: forall a b. r a -> (a -> (a, b)) -> ErrorT e m b
atomicModifyRef' r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f
#endif /* !MIN_VERSION_transformers(0,6,0) */
instance MonadAtomicRef r m => MonadAtomicRef r (IdentityT m) where
atomicModifyRef :: forall a b. r a -> (a -> (a, b)) -> IdentityT m b
atomicModifyRef r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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' :: forall a b. r a -> (a -> (a, b)) -> IdentityT m b
atomicModifyRef' r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f
#if !MIN_VERSION_transformers(0,6,0)
instance MonadAtomicRef r m => MonadAtomicRef r (ListT m) where
atomicModifyRef :: forall a b. r a -> (a -> (a, b)) -> ListT m b
atomicModifyRef r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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' :: forall a b. r a -> (a -> (a, b)) -> ListT m b
atomicModifyRef' r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f
#endif /* !MIN_VERSION_transformers(0,6,0) */
instance MonadAtomicRef r m => MonadAtomicRef r (MaybeT m) where
atomicModifyRef :: forall a b. r a -> (a -> (a, b)) -> MaybeT m b
atomicModifyRef r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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' :: forall a b. r a -> (a -> (a, b)) -> MaybeT m b
atomicModifyRef' r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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 :: forall a b. r a -> (a -> (a, b)) -> ReaderT r' m b
atomicModifyRef r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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' :: forall a b. r a -> (a -> (a, b)) -> ReaderT r' m b
atomicModifyRef' r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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 :: forall a b. r a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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' :: forall a b. r a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef' r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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 :: forall a b. r a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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' :: forall a b. r a -> (a -> (a, b)) -> StateT s m b
atomicModifyRef' r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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 :: forall a b. r a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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' :: forall a b. r a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef' r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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 :: forall a b. r a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> 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' :: forall a b. r a -> (a -> (a, b)) -> WriterT w m b
atomicModifyRef' r a
r a -> (a, b)
f = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (r :: * -> *) (m :: * -> *) a b.
MonadAtomicRef r m =>
r a -> (a -> (a, b)) -> m b
atomicModifyRef' r a
r a -> (a, b)
f