HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulerebase-1.21.2Haskell2010

Rebase.Data.STRef

  • 1 type
  • 5 values
  • Packagerebase-1.21.2
  • Exports6
  • LanguageHaskell2010
  • LicenceMIT
  • SourceSTRef.hs
datadata STRef s a
#

a value of type STRef s a is a mutable variable in state thread s, containing a value of type a

Example1 expression
:{runST (do    ref <- newSTRef "hello"    x <- readSTRef ref    writeSTRef ref (x ++ "world")    readSTRef ref ):}"helloworld"
Instances4NFData2, NFData1, Eq, NFData
  • NFData2 STRefDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • NFData1 (STRef s)Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Eq (STRef s a)Defined in ghc-internal-9.1003.0 · GHC.Internal.STRef

    Pointer equality.

  • NFData (STRef s a)Defined in deepseq-1.5.0.0 · Control.DeepSeq

    NOTE: Only strict in the reference and not the referenced value.

valuemodifySTRef :: STRef s a -> (a -> a) -> ST s ()
#

Mutate the contents of an STRef.

Example1 expression
:{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 seldom 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:

Example2 expressions
import GHC.Internal.Control.Monad (replicateM_):{print (runST (do    ref <- newSTRef 0    replicateM_ 1000 $ modifySTRef ref (+1)    readSTRef ref )):}1000

To avoid this problem, use modifySTRef' instead.