HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Moduleghc-internal-10.0.0Haskell2010

GHC.Internal.Control.Monad.ST.Lazy.Imp

This module presents an identical interface to Control.Monad.ST, except that the monad delays evaluation of ST operations until a value depending on them is required.

Contents
  1. The ST monad
  2. Converting between strict and lazy ST
  3. Converting ST To IO
  4. Unsafe operations
Synopsis

The ST monad

newtypenewtype ST s a

The lazy 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 executes 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 not strict in the state. For example,

runST (writeSTRef _|_ v >>= readSTRef _|_ >> return 2) = 2
Instances6Monad, Functor, MonadFix, Applicative, Semigroup, Monoid
  • Monad (ST s)Defined in ghc-internal-10.0.0 · GHC.Internal.Control.Monad.ST.Lazy.Imp
  • Functor (ST s)Defined in ghc-internal-10.0.0 · GHC.Internal.Control.Monad.ST.Lazy.Imp
  • MonadFix (ST s)Defined in ghc-internal-10.0.0 · GHC.Internal.Control.Monad.Fix
  • Applicative (ST s)Defined in ghc-internal-10.0.0 · GHC.Internal.Control.Monad.ST.Lazy.Imp
  • Semigroup a => Semigroup (ST s a)Defined in ghc-internal-10.0.0 · GHC.Internal.Control.Monad.ST.Lazy.Imp
  • Monoid a => Monoid (ST s a)Defined in ghc-internal-10.0.0 · GHC.Internal.Control.Monad.ST.Lazy.Imp
valuerunST :: (forall s. ST s a) -> a

Return the value computed by an ST computation. 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 between strict and lazy ST

valuestrictToLazyST :: ST s a -> ST s a

Convert a strict ST computation into a lazy one. The strict state thread passed to strictToLazyST is not performed until the result of the lazy state thread it returns is demanded.

Converting ST To IO

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

A monad transformer embedding lazy ST in the IO monad. 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