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

Safe Haskell | Safe-Inferred
Language | Haskell98

Data.MRef.Instances

Contents

- Orphan instances

Description

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

Synopsis

- data MVar a
- class Monad m => MonadIO (m :: Type -> Type) where
  - liftIO :: IO a -> m a
- data TVar a
- data STM a
- atomically :: STM a -> IO a
- data TMVar a

# Documentation

data MVar a Source #

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) | Compares the underlying pointers. Since: base-4.1.0.0
Instance details Defined in GHC.Internal.MVar Methods (==) :: MVar a -> MVar a -> Bool Source # (/=) :: MVar a -> MVar a -> Bool Source #
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 Source #

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:

-     liftIO . return = return

-     liftIO (m >>= f) = liftIO m >>= (liftIO . f)

Methods

liftIO :: IO a -> m a Source #

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 Source #

data TVar a Source #

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.Internal.Conc.Sync Methods (==) :: TVar a -> TVar a -> Bool Source # (/=) :: TVar a -> TVar a -> Bool Source #
NewMRef (TVar (Maybe a)) STM a Source # |
Instance details Defined in Data.MRef.Instances.STM Methods newMReference :: a -> STM (TVar (Maybe a)) Source # newEmptyMReference :: STM (TVar (Maybe a)) Source #
NewMRef (TVar (Maybe a)) IO a Source # |
Instance details Defined in Data.MRef.Instances.STM Methods newMReference :: a -> IO (TVar (Maybe a)) Source # newEmptyMReference :: IO (TVar (Maybe a)) Source #
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 STM a Source #

A monad supporting atomic memory transactions.

#### Instances

Instances details

Alternative STM | Takes the first non-`retry`ing `STM` action. Since: base-4.8.0.0
Instance details Defined in GHC.Internal.Conc.Sync Methods empty :: STM a Source # (<|>) :: STM a -> STM a -> STM a Source # some :: STM a -> STM [a] Source # many :: STM a -> STM [a] Source #
Applicative STM | Since: base-4.8.0.0
Instance details Defined in GHC.Internal.Conc.Sync Methods pure :: a -> STM a Source # (<*>) :: STM (a -> b) -> STM a -> STM b Source # liftA2 :: (a -> b -> c) -> STM a -> STM b -> STM c Source # (*>) :: STM a -> STM b -> STM b Source # (<*) :: STM a -> STM b -> STM a Source #
Functor STM | Since: base-4.3.0.0
Instance details Defined in GHC.Internal.Conc.Sync Methods fmap :: (a -> b) -> STM a -> STM b Source # (<$) :: a -> STM b -> STM a Source #
Monad STM | Since: base-4.3.0.0
Instance details Defined in GHC.Internal.Conc.Sync Methods (>>=) :: STM a -> (a -> STM b) -> STM b Source # (>>) :: STM a -> STM b -> STM b Source # return :: a -> STM a Source #
MonadPlus STM | Takes the first non-`retry`ing `STM` action. Since: base-4.3.0.0
Instance details Defined in GHC.Internal.Conc.Sync Methods mzero :: STM a Source # mplus :: STM a -> STM a -> STM a Source #
MonadFix STM | Since: stm-2.3
Instance details Defined in Control.Monad.STM Methods mfix :: (a -> STM a) -> STM a Source #
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) Source #

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

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

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

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

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

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

Monoid a => Monoid (STM a) | Since: base-4.17.0.0
Instance details Defined in GHC.Internal.Conc.Sync Methods mempty :: STM a Source # mappend :: STM a -> STM a -> STM a Source # mconcat :: [STM a] -> STM a Source #
Semigroup a => Semigroup (STM a) | Since: base-4.17.0.0
Instance details Defined in GHC.Internal.Conc.Sync Methods (<>) :: STM a -> STM a -> STM a Source # sconcat :: NonEmpty (STM a) -> STM a Source # stimes :: Integral b => b -> STM a -> STM a Source #
NewMRef (TVar (Maybe a)) STM a Source # |
Instance details Defined in Data.MRef.Instances.STM Methods newMReference :: a -> STM (TVar (Maybe a)) Source # newEmptyMReference :: STM (TVar (Maybe a)) Source #
NewMRef (TMVar a) STM a Source # |
Instance details Defined in Data.MRef.Instances.STM Methods newMReference :: a -> STM (TMVar a) Source # newEmptyMReference :: STM (TMVar a) Source #
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 Methods newMReference :: a -> IO (MRef STM a) Source # newEmptyMReference :: IO (MRef STM a) Source #
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 Source #

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 TMVar a Source #

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 Source # (/=) :: TMVar a -> TMVar a -> Bool Source #
NewMRef (TMVar a) STM a Source # |
Instance details Defined in Data.MRef.Instances.STM Methods newMReference :: a -> STM (TMVar a) Source # newEmptyMReference :: STM (TMVar a) Source #
NewMRef (TMVar a) IO a Source # |
Instance details Defined in Data.MRef.Instances.STM Methods newMReference :: a -> IO (TMVar a) Source # newEmptyMReference :: IO (TMVar a) Source #
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 #