Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell98 |
Data.MRef.Instances
Contents
Description
Documentation
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
Eq (MVar a) | Since: base-4.1.0.0 |
MonadIO m => NewMRef (MVar a) m a Source # | |
Defined in Data.MRef.Instances | |
MonadIO m => PutMRef (MVar a) m a Source # | |
Defined in Data.MRef.Instances Methods putMReference :: MVar a -> a -> m () Source # | |
MonadIO m => TakeMRef (MVar a) m a Source # | |
Defined in Data.MRef.Instances Methods takeMReference :: MVar a -> m a Source # | |
MonadIO m => NewRef (MVar a) m (Maybe a) Source # | |
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
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
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
, we would have ended up with this error:liftIO
• 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
and returns an IO
a(m a)
:
,
enabling us to run the program and see the expected results:liftIO
> evalStateT printState "hello" "hello" > evalStateT printState 3 3
A monad supporting atomic memory transactions.
Instances
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.
Shared memory locations that support atomic memory transactions.
Instances
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.