stateref-0.3: Abstraction for things that work like IORef.
Safe HaskellSafe-Inferred
LanguageHaskell98

Data.MRef.Instances

Description

This module exports no new symbols of its own. It defines basic class instances for creating, reading, and writing MVars, and re-exports MVar.

Synopsis

Documentation

data MVar a #

An MVar (pronounced "em-var") is a synchronising variable, used for communication between concurrent threads. It can be thought of as a box, which may be empty or full.

Instances

Instances details
Eq (MVar a)

Since: base-4.1.0.0

Instance details

Defined in GHC.MVar

Methods

(==) :: MVar a -> MVar a -> Bool #

(/=) :: MVar a -> MVar a -> Bool #

MonadIO m => NewMRef (MVar a) m a Source # 
Instance details

Defined in Data.MRef.Instances

Methods

newMReference :: a -> m (MVar a) Source #

newEmptyMReference :: m (MVar a) Source #

MonadIO m => PutMRef (MVar a) m a Source # 
Instance details

Defined in Data.MRef.Instances

Methods

putMReference :: MVar a -> a -> m () Source #

MonadIO m => TakeMRef (MVar a) m a Source # 
Instance details

Defined in Data.MRef.Instances

Methods

takeMReference :: MVar a -> m a Source #

MonadIO m => NewRef (MVar a) m (Maybe a) Source # 
Instance details

Defined in Data.StateRef.Instances

Methods

newReference :: Maybe a -> m (MVar a) Source #

class Monad m => MonadIO (m :: Type -> Type) where #

Monads in which IO computations may be embedded. Any monad built by applying a sequence of monad transformers to the IO monad will be an instance of this class.

Instances should satisfy the following laws, which state that liftIO is a transformer of monads:

Methods

liftIO :: IO a -> m a #

Lift a computation from the IO monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. IO is the base monad for the stack).

Example

Expand
import Control.Monad.Trans.State -- from the "transformers" library

printState :: Show s => StateT s IO ()
printState = do
  state <- get
  liftIO $ print state

Had we omitted liftIO, we would have ended up with this error:

• Couldn't match type ‘IO’ with ‘StateT s IO’
 Expected type: StateT s IO ()
   Actual type: IO ()

The important part here is the mismatch between StateT s IO () and IO ().

Luckily, we know of a function that takes an IO a and returns an (m a): liftIO, enabling us to run the program and see the expected results:

> evalStateT printState "hello"
"hello"

> evalStateT printState 3
3

Instances

Instances details
MonadIO IO

Since: base-4.9.0.0

Instance details

Defined in Control.Monad.IO.Class

Methods

liftIO :: IO a -> IO a #

data STM a #

A monad supporting atomic memory transactions.

Instances

Instances details
Alternative STM

Takes the first non-retrying STM action.

Since: base-4.8.0.0

Instance details

Defined in GHC.Conc.Sync

Methods

empty :: STM a #

(<|>) :: STM a -> STM a -> STM a #

some :: STM a -> STM [a] #

many :: STM a -> STM [a] #

Applicative STM

Since: base-4.8.0.0

Instance details

Defined in GHC.Conc.Sync

Methods

pure :: a -> STM a #

(<*>) :: STM (a -> b) -> STM a -> STM b #

liftA2 :: (a -> b -> c) -> STM a -> STM b -> STM c #

(*>) :: STM a -> STM b -> STM b #

(<*) :: STM a -> STM b -> STM a #

Functor STM

Since: base-4.3.0.0

Instance details

Defined in GHC.Conc.Sync

Methods

fmap :: (a -> b) -> STM a -> STM b #

(<$) :: a -> STM b -> STM a #

Monad STM

Since: base-4.3.0.0

Instance details

Defined in GHC.Conc.Sync

Methods

(>>=) :: STM a -> (a -> STM b) -> STM b #

(>>) :: STM a -> STM b -> STM b #

return :: a -> STM a #

MonadPlus STM

Takes the first non-retrying STM action.

Since: base-4.3.0.0

Instance details

Defined in GHC.Conc.Sync

Methods

mzero :: STM a #

mplus :: STM a -> STM a -> STM a #

HasMRef STM Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

newMRef :: a -> STM (MRef STM a) Source #

newEmptyMRef :: STM (MRef STM a) Source #

HasRef STM Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

newRef :: a -> STM (Ref STM a) Source #

MArray TArray e STM 
Instance details

Defined in Control.Concurrent.STM.TArray

Methods

getBounds :: Ix i => TArray i e -> STM (i, i) #

getNumElements :: Ix i => TArray i e -> STM Int #

newArray :: Ix i => (i, i) -> e -> STM (TArray i e) #

newArray_ :: Ix i => (i, i) -> STM (TArray i e) #

unsafeNewArray_ :: Ix i => (i, i) -> STM (TArray i e) #

unsafeRead :: Ix i => TArray i e -> Int -> STM e #

unsafeWrite :: Ix i => TArray i e -> Int -> e -> STM () #

Monoid a => Monoid (STM a)

Since: base-4.17.0.0

Instance details

Defined in GHC.Conc.Sync

Methods

mempty :: STM a #

mappend :: STM a -> STM a -> STM a #

mconcat :: [STM a] -> STM a #

Semigroup a => Semigroup (STM a)

Since: base-4.17.0.0

Instance details

Defined in GHC.Conc.Sync

Methods

(<>) :: STM a -> STM a -> STM a #

sconcat :: NonEmpty (STM a) -> STM a #

stimes :: Integral b => b -> STM a -> STM a #

NewMRef (TVar (Maybe a)) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

NewMRef (TMVar a) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

PutMRef (TVar (Maybe a)) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

putMReference :: TVar (Maybe a) -> a -> STM () Source #

PutMRef (TMVar a) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

putMReference :: TMVar a -> a -> STM () Source #

TakeMRef (TVar (Maybe a)) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

takeMReference :: TVar (Maybe a) -> STM a Source #

TakeMRef (TMVar a) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

takeMReference :: TMVar a -> STM a Source #

ModifyRef (TVar a) STM a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

atomicModifyReference :: TVar a -> (a -> (a, b)) -> STM b Source #

modifyReference :: TVar a -> (a -> a) -> STM () Source #

NewRef (TVar a) STM a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

newReference :: a -> STM (TVar a) Source #

ReadRef (STM a) STM a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

readReference :: STM a -> STM a Source #

MonadIO m => ReadRef (STM a) m a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

readReference :: STM a -> m a Source #

ReadRef (TVar a) STM a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

readReference :: TVar a -> STM a Source #

WriteRef (TVar a) STM a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

writeReference :: TVar a -> a -> STM () Source #

NewRef (TMVar a) STM (Maybe a) Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

newReference :: Maybe a -> STM (TMVar a) Source #

ReadRef (TMVar a) STM (Maybe a) Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

readReference :: TMVar a -> STM (Maybe a) Source #

NewMRef (MRef STM a) IO a Source # 
Instance details

Defined in Data.MRef.Instances.STM

PutMRef (MRef STM a) IO a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

putMReference :: MRef STM a -> a -> IO () Source #

TakeMRef (MRef STM a) IO a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

takeMReference :: MRef STM a -> IO a Source #

MonadIO m => ModifyRef (Ref STM a) m a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

atomicModifyReference :: Ref STM a -> (a -> (a, b)) -> m b Source #

modifyReference :: Ref STM a -> (a -> a) -> m () Source #

MonadIO m => NewRef (Ref STM a) m a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

newReference :: a -> m (Ref STM a) Source #

MonadIO m => ReadRef (Ref STM a) m a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

readReference :: Ref STM a -> m a Source #

MonadIO m => WriteRef (Ref STM a) m a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

writeReference :: Ref STM a -> a -> m () Source #

atomically :: STM a -> IO a #

Perform a series of STM actions atomically.

Using atomically inside an unsafePerformIO or unsafeInterleaveIO subverts some of guarantees that STM provides. It makes it possible to run a transaction inside of another transaction, depending on when the thunk is evaluated. If a nested transaction is attempted, an exception is thrown by the runtime. It is possible to safely use atomically inside unsafePerformIO or unsafeInterleaveIO, but the typechecker does not rule out programs that may attempt nested transactions, meaning that the programmer must take special care to prevent these.

However, there are functions for creating transactional variables that can always be safely called in unsafePerformIO. See: newTVarIO, newTChanIO, newBroadcastTChanIO, newTQueueIO, newTBQueueIO, and newTMVarIO.

Using unsafePerformIO inside of atomically is also dangerous but for different reasons. See unsafeIOToSTM for more on this.

data TVar a #

Shared memory locations that support atomic memory transactions.

Instances

Instances details
Eq (TVar a)

Since: base-4.8.0.0

Instance details

Defined in GHC.Conc.Sync

Methods

(==) :: TVar a -> TVar a -> Bool #

(/=) :: TVar a -> TVar a -> Bool #

NewMRef (TVar (Maybe a)) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

NewMRef (TVar (Maybe a)) IO a Source # 
Instance details

Defined in Data.MRef.Instances.STM

PutMRef (TVar (Maybe a)) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

putMReference :: TVar (Maybe a) -> a -> STM () Source #

PutMRef (TVar (Maybe a)) IO a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

putMReference :: TVar (Maybe a) -> a -> IO () Source #

TakeMRef (TVar (Maybe a)) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

takeMReference :: TVar (Maybe a) -> STM a Source #

TakeMRef (TVar (Maybe a)) IO a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

takeMReference :: TVar (Maybe a) -> IO a Source #

ModifyRef (TVar a) STM a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

atomicModifyReference :: TVar a -> (a -> (a, b)) -> STM b Source #

modifyReference :: TVar a -> (a -> a) -> STM () Source #

MonadIO m => ModifyRef (TVar a) m a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

atomicModifyReference :: TVar a -> (a -> (a, b)) -> m b Source #

modifyReference :: TVar a -> (a -> a) -> m () Source #

NewRef (TVar a) STM a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

newReference :: a -> STM (TVar a) Source #

MonadIO m => NewRef (TVar a) m a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

newReference :: a -> m (TVar a) Source #

ReadRef (TVar a) STM a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

readReference :: TVar a -> STM a Source #

MonadIO m => ReadRef (TVar a) m a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

readReference :: TVar a -> m a Source #

WriteRef (TVar a) STM a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

writeReference :: TVar a -> a -> STM () Source #

MonadIO m => WriteRef (TVar a) m a Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

writeReference :: TVar a -> a -> m () Source #

data TMVar a #

A TMVar is a synchronising variable, used for communication between concurrent threads. It can be thought of as a box, which may be empty or full.

Instances

Instances details
Eq (TMVar a) 
Instance details

Defined in Control.Concurrent.STM.TMVar

Methods

(==) :: TMVar a -> TMVar a -> Bool #

(/=) :: TMVar a -> TMVar a -> Bool #

NewMRef (TMVar a) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

NewMRef (TMVar a) IO a Source # 
Instance details

Defined in Data.MRef.Instances.STM

PutMRef (TMVar a) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

putMReference :: TMVar a -> a -> STM () Source #

PutMRef (TMVar a) IO a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

putMReference :: TMVar a -> a -> IO () Source #

TakeMRef (TMVar a) STM a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

takeMReference :: TMVar a -> STM a Source #

TakeMRef (TMVar a) IO a Source # 
Instance details

Defined in Data.MRef.Instances.STM

Methods

takeMReference :: TMVar a -> IO a Source #

NewRef (TMVar a) STM (Maybe a) Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

newReference :: Maybe a -> STM (TMVar a) Source #

MonadIO m => NewRef (TMVar a) m (Maybe a) Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

newReference :: Maybe a -> m (TMVar a) Source #

ReadRef (TMVar a) STM (Maybe a) Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

readReference :: TMVar a -> STM (Maybe a) Source #

MonadIO m => ReadRef (TMVar a) m (Maybe a) Source # 
Instance details

Defined in Data.StateRef.Instances.STM

Methods

readReference :: TMVar a -> m (Maybe a) Source #

Orphan instances

HasMRef IO Source # 
Instance details

Methods

newMRef :: a -> IO (MRef IO a) Source #

newEmptyMRef :: IO (MRef IO a) Source #

MonadIO m => NewMRef (MVar a) m a Source # 
Instance details

Methods

newMReference :: a -> m (MVar a) Source #

newEmptyMReference :: m (MVar a) Source #

MonadIO m => PutMRef (MVar a) m a Source # 
Instance details

Methods

putMReference :: MVar a -> a -> m () Source #

MonadIO m => TakeMRef (MVar a) m a Source # 
Instance details

Methods

takeMReference :: MVar a -> m a Source #