Control.Monad.ST.Lazy.Safe (original) (raw)
Contents
Description
Deprecated: Safe is now the default, please use Control.Monad.ST.Lazy instead
This module presents an identical interface to Control.Monad.ST, except that the monad delays evaluation of state operations until a value depending on them is required.
Safe API only.
Synopsis
- data ST s a
- runST :: (forall s. ST s a) -> a
- fixST :: (a -> ST s a) -> ST s a
- strictToLazyST :: ST s a -> ST s a
- lazyToStrictST :: ST s a -> ST s a
- data RealWorld :: Type
- stToIO :: ST RealWorld a -> IO a
The [ST](Control-Monad-ST-Lazy-Safe.html#t:ST "Control.Monad.ST.Lazy.Safe")
monad
The lazy state-transformer monad. A computation of type `[ST](Control-Monad-ST-Lazy-Safe.html#t:ST "Control.Monad.ST.Lazy.Safe")` s a
transforms an internal state indexed by s
, and returns a value of type a
. The s
parameter is either
- an uninstantiated type variable (inside invocations of
[runST](Control-Monad-ST-Lazy-Safe.html#v:runST "Control.Monad.ST.Lazy.Safe")
), or [RealWorld](Control-Monad-ST-Lazy-Safe.html#t:RealWorld "Control.Monad.ST.Lazy.Safe")
(inside invocations of[stToIO](Control-Monad-ST-Lazy-Safe.html#v:stToIO "Control.Monad.ST.Lazy.Safe")
).
It serves to keep the internal states of different invocations of[runST](Control-Monad-ST-Lazy-Safe.html#v:runST "Control.Monad.ST.Lazy.Safe")
separate from each other and from invocations of [stToIO](Control-Monad-ST-Lazy-Safe.html#v:stToIO "Control.Monad.ST.Lazy.Safe")
.
The [>>=](Control-Monad.html#v:-62--62--61- "Control.Monad")
and [>>](Control-Monad.html#v:-62--62- "Control.Monad")
operations are not strict in the state. For example,
[runST](Control-Monad-ST-Lazy-Safe.html#v:runST "Control.Monad.ST.Lazy.Safe")
(writeSTRef | v >>= readSTRef | >> return 2) = 2
runST :: (forall s. ST s a) -> a Source #
Return the value computed by a state transformer computation. The forall
ensures that the internal state used by the [ST](Control-Monad-ST-Lazy-Safe.html#t:ST "Control.Monad.ST.Lazy.Safe")
computation is inaccessible to the rest of the program.
fixST :: (a -> ST s a) -> ST s a Source #
Allow the result of a state transformer computation to be used (lazily) inside the computation. Note that if f
is strict, `[fixST](Control-Monad-ST-Lazy-Safe.html#v:fixST "Control.Monad.ST.Lazy.Safe")` f = _|_
.
Converting between strict and lazy [ST](Control-Monad-ST-Lazy-Safe.html#t:ST "Control.Monad.ST.Lazy.Safe")
strictToLazyST :: ST s a -> ST s a Source #
Convert a strict [ST](Control-Monad-ST-Lazy-Safe.html#t:ST "Control.Monad.ST.Lazy.Safe")
computation into a lazy one. The strict state thread passed to [strictToLazyST](Control-Monad-ST-Lazy-Safe.html#v:strictToLazyST "Control.Monad.ST.Lazy.Safe")
is not performed until the result of the lazy state thread it returns is demanded.
Converting [ST](Control-Monad-ST-Lazy-Safe.html#t:ST "Control.Monad.ST.Lazy.Safe")
To [IO](System-IO.html#t:IO "System.IO")
RealWorld
is deeply magical. It is primitive, but it is not_unlifted_ (hence ptrArg
). We never manipulate values of typeRealWorld
; it's only used in the type system, to parameterise State#
.
stToIO :: ST RealWorld a -> IO a Source #
A monad transformer embedding lazy state transformers in the [IO](System-IO.html#t:IO "System.IO")
monad. The [RealWorld](Control-Monad-ST-Lazy-Safe.html#t:RealWorld "Control.Monad.ST.Lazy.Safe")
parameter indicates that the internal state used by the [ST](Control-Monad-ST-Lazy-Safe.html#t:ST "Control.Monad.ST.Lazy.Safe")
computation is a special one supplied by the [IO](System-IO.html#t:IO "System.IO")
monad, and thus distinct from those used by invocations of [runST](Control-Monad-ST-Lazy-Safe.html#v:runST "Control.Monad.ST.Lazy.Safe")
.