Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- data ST s a
- runST :: (forall s. ST s a) -> a
- fixST :: (a -> ST s a) -> ST s a
- data RealWorld
- stToIO :: ST RealWorld a -> IO a
- data STE e s a
- runSTE :: (forall s. STE e s a) -> (Either e a -> b) -> b
- fixSTE :: (a -> STE e s a) -> STE e s a
- throwSTE :: e -> STE e s a
- handleSTE :: (Either e a -> b) -> (forall s. STE e s a) -> b
- unsafeInterleaveSTE :: STE e s a -> STE e s a
- unsafeIOToSTE :: IO a -> STE e s a
- unsafeSTEToIO :: STE e s a -> IO a
- data STRef s a
- newSTRef :: a -> ST s (STRef s a)
- readSTRef :: STRef s a -> ST s a
- writeSTRef :: STRef s a -> a -> ST s ()
- modifySTRef :: STRef s a -> (a -> a) -> ST s ()
- modifySTRef' :: STRef s a -> (a -> a) -> ST s ()
ST
The strict state-transformer monad.
A computation of type
transforms an internal state indexed
by ST
s as
, and returns a value of type a
.
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
Monad (ST s) | Since: base-2.1 |
Functor (ST s) | Since: base-2.1 |
MonadFix (ST s) | Since: base-2.1 |
Defined in Control.Monad.Fix | |
MonadFail (ST s) | Since: base-4.11.0.0 |
Applicative (ST s) | Since: base-4.4.0.0 |
PrimMonad (ST s) | |
PrimBase (ST s) | |
MonadBase (ST s) (ST s) | |
Defined in Control.Monad.Base | |
Show (ST s a) | Since: base-2.1 |
Semigroup a => Semigroup (ST s a) | Since: base-4.11.0.0 |
Monoid a => Monoid (ST s a) | Since: base-4.11.0.0 |
Strict (ST s a) (ST s a) | |
type PrimState (ST s) | |
Defined in Control.Monad.Primitive |
runST :: (forall s. ST s a) -> a #
Return the value computed by a state transformer computation.
The forall
ensures that the internal state used by the ST
computation is inaccessible to the rest of the program.
fixST :: (a -> ST s a) -> ST s a #
Allow the result of a state transformer computation to be used (lazily)
inside the computation.
Note that if f
is strict,
.fixST
f = _|_
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#
.
STE
Instances
Monad (STE e s) | |
Functor (STE e s) | |
MonadFix (STE e s) | |
Defined in Control.Monad.STE.Internal | |
Applicative (STE e s) | |
SomeException ~ err => MonadThrow (STE err s) | |
Defined in Control.Monad.STE.Internal | |
PrimMonad (STE e s) | |
PrimBase (STE e s) | |
type PrimState (STE e s) | |
Defined in Control.Monad.STE.Internal |
fixSTE :: (a -> STE e s a) -> STE e s a #
Allow the result of a state transformer computation to be used (lazily)
inside the computation.
Note that if f
is strict,
.fixSTE
f = _|_
unsafeInterleaveSTE :: STE e s a -> STE e s a #
unsafeIOToSTE :: IO a -> STE e s a #
unsafeSTEToIO :: STE e s a -> IO a #
STRef
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
NFData2 STRef | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
NFData1 (STRef s) | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
Eq (STRef s a) | Pointer equality. Since: base-2.1 |
NFData (STRef s a) | NOTE: Only strict in the reference and not the referenced value. Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq |
writeSTRef :: STRef s a -> a -> ST s () #
Write a new value into an STRef
modifySTRef :: STRef s a -> (a -> a) -> ST s () #
Mutate the contents of an STRef
.
>>>
:{
runST (do ref <- newSTRef "" modifySTRef ref (const "world") modifySTRef ref (++ "!") modifySTRef ref ("Hello, " ++) readSTRef ref ) :} "Hello, world!"
Be warned that modifySTRef
does not apply the function strictly. This
means if the program calls modifySTRef
many times, but seldomly uses the
value, thunks will pile up in memory resulting in a space leak. This is a
common mistake made when using an STRef as a counter. For example, the
following will leak memory and may produce a stack overflow:
>>>
import Control.Monad (replicateM_)
>>>
:{
print (runST (do ref <- newSTRef 0 replicateM_ 1000 $ modifySTRef ref (+1) readSTRef ref )) :} 1000
To avoid this problem, use modifySTRef'
instead.
modifySTRef' :: STRef s a -> (a -> a) -> ST s () #
Strict version of modifySTRef
Since: base-4.6.0.0