stateref-0.2.1.1: Abstraction for things that work like IORef.

Data.StateRef.Classes

Synopsis

Documentation

class Monad m => NewRef sr m a | sr -> a whereSource

Methods

newRef :: a -> m srSource

Construct a new mutable reference containing the provided value.

Instances

Monad m => NewRef (IO a) m a 
(Storable a, MonadIO m) => NewRef (ForeignPtr a) m a 
MonadIO m => NewRef (TVar a) m a 
NewRef (TVar a) STM a 
MonadIO m => NewRef (IORef a) m a 
MonadIO m => NewRef (MVar a) m (Maybe a) 
MonadIO m => NewRef (TMVar a) m (Maybe a) 
NewRef (TMVar a) STM (Maybe a) 
Monad m => NewRef (ST s a) m a 
NewRef (STRef RealWorld a) IO a 
NewRef (STRef s a) (ST s) a 
NewRef (STRef s a) (ST s) a 

class Monad m => WriteRef sr m a | sr -> a whereSource

Methods

writeRef :: sr -> a -> m ()Source

Replace the existing value of the given reference with the provided value.

Instances

(Storable a, MonadIO m) => WriteRef (ForeignPtr a) m a 
MonadIO m => WriteRef (TVar a) m a 
WriteRef (TVar a) STM a 
MonadIO m => WriteRef (IORef a) m a 
WriteRef sr m a => WriteRef (UnsafeModifyRef sr) m a 
WriteRef (STRef RealWorld a) IO a 
Monad m => WriteRef (Accessor m a) m a 
Monad m => WriteRef (Setter m a) m a 
WriteRef (STRef s a) (ST s) a 
WriteRef (STRef s a) (ST s) a 

class Monad m => ReadRef sr m a | sr -> a whereSource

Methods

readRef :: sr -> m aSource

Get the current value referenced by the given state reference.

Instances

MonadIO m => ReadRef (IO a) m a 
(Storable a, MonadIO m) => ReadRef (ForeignPtr a) m a 
MonadIO m => ReadRef (STM a) m a 
ReadRef (STM a) STM a 
MonadIO m => ReadRef (TVar a) m a 
ReadRef (TVar a) STM a 
MonadIO m => ReadRef (IORef a) m a 
ReadRef sr m a => ReadRef (UnsafeModifyRef sr) m a 
MonadIO m => ReadRef (TMVar a) m (Maybe a) 
ReadRef (TMVar a) STM (Maybe a) 
MonadIO m => ReadRef (ST RealWorld a) m a 
ReadRef (STRef RealWorld a) IO a 
Monad m => ReadRef (Accessor m a) m a 
Monad m => ReadRef (Getter m a) m a 
ReadRef (ST s a) (ST s) a 
ReadRef (STRef s a) (ST s) a 
ReadRef (STRef s a) (ST s) a 

class (Monad m, ReadRef sr m a, WriteRef sr m a) => ModifyRef sr m a | sr -> a whereSource

Methods

atomicModifyRef :: sr -> (a -> (a, b)) -> m bSource

Atomically modify the contents of a reference. This is implemented in a separate class (rather than a function with context (ReadRef sr m a, WriteRef sr m a)) because in most cases the default implementation cannot act atomically.

modifyRef :: sr -> (a -> a) -> m ()Source

Same thing, but don't thread out the extra return. Could perhaps be implemented slightly more efficiently than atomicModifyRef in many cases. Note that implementations are expected to be atomic, if at all possible, but not strictly required to be.

Instances

(Storable a, MonadIO m) => ModifyRef (ForeignPtr a) m a 
MonadIO m => ModifyRef (TVar a) m a 
ModifyRef (TVar a) STM a 
MonadIO m => ModifyRef (IORef a) m a 
(ReadRef sr m a, WriteRef sr m a) => ModifyRef (UnsafeModifyRef sr) m a 
ModifyRef (STRef RealWorld a) IO a 
ModifyRef (STRef s a) (ST s) a 
ModifyRef (STRef s a) (ST s) a 

class Monad m => DefaultStateRef sr m a | sr -> a, m a -> srSource

The DefaultStateRef and Data.MRef.Classes.DefaultMRef are used to internally constrain types that do not escape an expression, so that the compiler may choose an instance for the reference type (which it otherwise would not, and maybe not even tell you until you tried to use your function). For an example, see the source for Data.StateRef.newCounter. See also Data.MRef.Classes.DefaultMRef.

The sole purpose for these classes' existence is as a carrier for an altered set of functional dependencies, which constrain the reference type to be uniquely determined by the monad and the contained type.

Instances