HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleghc-internal-9.1003.0Haskell2010

GHC.Internal.Control.Monad.ST.Imp

This library provides support for strict state threads, as described in the PLDI '94 paper by John Launchbury and Simon Peyton Jones Lazy Functional State Threads.

  • 2 types
  • 7 values

The ST Monad

3 declarations
newtypenewtype ST s a
#

The strict ST monad. The ST monad allows for destructive updates, but is escapable (unlike IO). A computation of type ST s a returns a value of type a, and execute in "thread" s. The s parameter is either

  • an uninstantiated type variable (inside invocations of runST), or

  • RealWorld (inside invocations of stToIO).

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) = _|_
Instances7Monad, Functor, MonadFix, Applicative, Show, Semigroup, …
  • Monad (ST s)Defined in ghc-internal-9.1003.0 · GHC.Internal.ST
  • Functor (ST s)Defined in ghc-internal-9.1003.0 · GHC.Internal.ST
  • MonadFix (ST s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.Fix
  • Applicative (ST s)Defined in ghc-internal-9.1003.0 · GHC.Internal.ST
  • Show (ST s a)Defined in ghc-internal-9.1003.0 · GHC.Internal.ST
  • Semigroup a => Semigroup (ST s a)Defined in ghc-internal-9.1003.0 · GHC.Internal.ST
  • Monoid a => Monoid (ST s a)Defined in ghc-internal-9.1003.0 · GHC.Internal.ST
valuerunST :: (forall s. ST s a) -> a
#

Return the value computed by a state thread. The forall ensures that the internal state used by the ST computation is inaccessible to the rest of the program.

valuefixST :: (a -> ST s a) -> ST s a
#

Allow the result of an ST computation to be used (lazily) inside the computation.

Note that if f is strict, fixST f = _|_.

Converting ST to Prelude.IO

2 declarations
datadata RealWorld
#

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

valuestToIO :: ST RealWorld a -> IO a
#

Embed a strict state thread in an IO action. The RealWorld parameter indicates that the internal state used by the ST computation is a special one supplied by the IO monad, and thus distinct from those used by invocations of runST.

Unsafe operations

4 declarations
valueunsafeInterleaveST :: ST s a -> ST s a
#

unsafeInterleaveST allows an ST computation to be deferred lazily. When passed a value of type ST a, the ST computation will only be performed when the value of the a is demanded.

valueunsafeDupableInterleaveST :: ST s a -> ST s a
#

unsafeDupableInterleaveST allows an ST computation to be deferred lazily. When passed a value of type ST a, the ST computation will only be performed when the value of the a is demanded.

The computation may be performed multiple times by different threads, possibly at the same time. To prevent this, use unsafeInterleaveST instead.

valueunsafeIOToST :: IO a -> ST s a
#

Convert an IO action to an ST action. This relies on IO and ST having the same representation modulo the constraint on the state thread type parameter.