Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell98 |
Data.StateRef.Instances
Contents
Description
This module exports no new symbols of its own. It defines several basic class instances for creating, reading, and writing standard reference types, and re-exports the types for which it defines instances.
TODO: add millions of SPECIALIZE INSTANCE pragmas, for IO monad at a minimum.
Synopsis
- data IORef a
- data MVar a
- class Monad m => MonadIO (m :: Type -> Type) where
- data STRef s a
- data ST s a
- data RealWorld
- data ForeignPtr a
- data STM a
- atomically :: STM a -> IO a
- data TVar a
- data TMVar a
- newtype UnsafeModifyRef sr = UnsafeModifyRef sr
Documentation
A mutable variable in the IO
monad.
>>>
import Data.IORef
>>>
r <- newIORef 0
>>>
readIORef r
0>>>
writeIORef r 1
>>>
readIORef r
1>>>
atomicWriteIORef r 2
>>>
readIORef r
2>>>
modifyIORef' r (+ 1)
>>>
readIORef r
3>>>
atomicModifyIORef' r (\a -> (a + 1, ()))
>>>
readIORef r
4
Instances
Eq (IORef a) | Pointer equality. Since: base-4.0.0.0 |
MonadIO m => ModifyRef (IORef a) m a Source # | |
Defined in Data.StateRef.Instances Methods atomicModifyReference :: IORef a -> (a -> (a, b)) -> m b Source # modifyReference :: IORef a -> (a -> a) -> m () Source # | |
MonadIO m => NewRef (IORef a) m a Source # | |
Defined in Data.StateRef.Instances Methods newReference :: a -> m (IORef a) Source # | |
MonadIO m => ReadRef (IORef a) m a Source # | |
Defined in Data.StateRef.Instances Methods readReference :: IORef a -> m a Source # | |
MonadIO m => WriteRef (IORef a) m a Source # | |
Defined in Data.StateRef.Instances Methods writeReference :: IORef a -> a -> m () 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
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 value of type STRef s a
is a mutable variable in state thread s
,
containing a value of type a
>>>
:{
runST (do ref <- newSTRef "hello" x <- readSTRef ref writeSTRef ref (x ++ "world") readSTRef ref ) :} "helloworld"
Instances
The strict ST
monad.
The ST
monad allows for destructive updates, but is escapable (unlike IO).
A computation of type
returns a value of type ST
s aa
, and
execute in "thread" s
. The s
parameter is either
- an uninstantiated type variable (inside invocations of
runST
), or RealWorld
(inside invocations ofstToIO
).
It serves to keep the internal states of different invocations
of runST
separate from each other and from invocations of
stToIO
.
The >>=
and >>
operations are strict in the state (though not in
values stored in the state). For example,
runST
(writeSTRef _|_ v >>= f) = _|_
Instances
RealWorld
is deeply magical. It is primitive, but it is not
unlifted (hence ptrArg
). We never manipulate values of type
RealWorld
; it's only used in the type system, to parameterise State#
.
Instances
ModifyRef (STRef RealWorld a) IO a Source # | |
Defined in Data.StateRef.Instances | |
NewRef (STRef RealWorld a) IO a Source # | |
Defined in Data.StateRef.Instances | |
MonadIO m => ReadRef (ST RealWorld a) m a Source # | |
Defined in Data.StateRef.Instances Methods readReference :: ST RealWorld a -> m a Source # | |
ReadRef (STRef RealWorld a) IO a Source # | |
Defined in Data.StateRef.Instances | |
WriteRef (STRef RealWorld a) IO a Source # | |
Defined in Data.StateRef.Instances |
data ForeignPtr a #
The type ForeignPtr
represents references to objects that are
maintained in a foreign language, i.e., that are not part of the
data structures usually managed by the Haskell storage manager.
The essential difference between ForeignPtr
s and vanilla memory
references of type Ptr a
is that the former may be associated
with finalizers. A finalizer is a routine that is invoked when
the Haskell storage manager detects that - within the Haskell heap
and stack - there are no more references left that are pointing to
the ForeignPtr
. Typically, the finalizer will, then, invoke
routines in the foreign language that free the resources bound by
the foreign object.
The ForeignPtr
is parameterised in the same way as Ptr
. The
type argument of ForeignPtr
should normally be an instance of
class Storable
.
Instances
Data a => Data (ForeignPtr a) | Since: base-4.8.0.0 |
Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ForeignPtr a -> c (ForeignPtr a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (ForeignPtr a) # toConstr :: ForeignPtr a -> Constr # dataTypeOf :: ForeignPtr a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (ForeignPtr a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (ForeignPtr a)) # gmapT :: (forall b. Data b => b -> b) -> ForeignPtr a -> ForeignPtr a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ForeignPtr a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ForeignPtr a -> r # gmapQ :: (forall d. Data d => d -> u) -> ForeignPtr a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> ForeignPtr a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> ForeignPtr a -> m (ForeignPtr a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ForeignPtr a -> m (ForeignPtr a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ForeignPtr a -> m (ForeignPtr a) # | |
Show (ForeignPtr a) | Since: base-2.1 |
Defined in GHC.ForeignPtr Methods showsPrec :: Int -> ForeignPtr a -> ShowS # show :: ForeignPtr a -> String # showList :: [ForeignPtr a] -> ShowS # | |
Eq (ForeignPtr a) | Since: base-2.1 |
Defined in GHC.ForeignPtr | |
Ord (ForeignPtr a) | Since: base-2.1 |
Defined in GHC.ForeignPtr Methods compare :: ForeignPtr a -> ForeignPtr a -> Ordering # (<) :: ForeignPtr a -> ForeignPtr a -> Bool # (<=) :: ForeignPtr a -> ForeignPtr a -> Bool # (>) :: ForeignPtr a -> ForeignPtr a -> Bool # (>=) :: ForeignPtr a -> ForeignPtr a -> Bool # max :: ForeignPtr a -> ForeignPtr a -> ForeignPtr a # min :: ForeignPtr a -> ForeignPtr a -> ForeignPtr a # | |
(Storable a, MonadIO m) => ModifyRef (ForeignPtr a) m a Source # | |
Defined in Data.StateRef.Instances Methods atomicModifyReference :: ForeignPtr a -> (a -> (a, b)) -> m b Source # modifyReference :: ForeignPtr a -> (a -> a) -> m () Source # | |
(Storable a, MonadIO m) => NewRef (ForeignPtr a) m a Source # | |
Defined in Data.StateRef.Instances Methods newReference :: a -> m (ForeignPtr a) Source # | |
(Storable a, MonadIO m) => ReadRef (ForeignPtr a) m a Source # | |
Defined in Data.StateRef.Instances Methods readReference :: ForeignPtr a -> m a Source # | |
(Storable a, MonadIO m) => WriteRef (ForeignPtr a) m a Source # | |
Defined in Data.StateRef.Instances Methods writeReference :: ForeignPtr a -> a -> m () Source # |
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.
Instances
newtype UnsafeModifyRef sr Source #
Wrap a state reference that supports reading and writing, and add a
potentially thread-unsafe ModifyRef
instance.
Constructors
UnsafeModifyRef sr |
Instances
(Monad m, ReadRef sr m a, WriteRef sr m a) => ModifyRef (UnsafeModifyRef sr) m a Source # | |
Defined in Data.StateRef.Instances.Undecidable Methods atomicModifyReference :: UnsafeModifyRef sr -> (a -> (a, b)) -> m b Source # modifyReference :: UnsafeModifyRef sr -> (a -> a) -> m () Source # | |
ReadRef sr m a => ReadRef (UnsafeModifyRef sr) m a Source # | |
Defined in Data.StateRef.Instances.Undecidable Methods readReference :: UnsafeModifyRef sr -> m a Source # | |
WriteRef sr m a => WriteRef (UnsafeModifyRef sr) m a Source # | |
Defined in Data.StateRef.Instances.Undecidable Methods writeReference :: UnsafeModifyRef sr -> a -> m () Source # |