2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
7 -- Copyright : (c) The University of Glasgow, 1992-2002
8 -- License : see libraries/base/LICENSE
10 -- Maintainer : cvs-ghc@haskell.org
11 -- Stability : internal
12 -- Portability : non-portable (GHC Extensions)
16 -----------------------------------------------------------------------------
28 %*********************************************************
30 \subsection{The @ST@ monad}
32 %*********************************************************
34 The state-transformer monad proper. By default the monad is strict;
35 too many people got bitten by space leaks when it was lazy.
38 -- | The strict state-transformer monad.
39 -- A computation of type @'ST' s a@ transforms an internal state indexed
40 -- by @s@, and returns a value of type @a@.
41 -- The @s@ parameter is either
43 -- * an uninstantiated type variable (inside invocations of 'runST'), or
45 -- * 'RealWorld' (inside invocations of 'Control.Monad.ST.stToIO').
47 -- It serves to keep the internal states of different invocations
48 -- of 'runST' separate from each other and from invocations of
49 -- 'Control.Monad.ST.stToIO'.
51 -- The '>>=' and '>>' operations are strict in the state (though not in
52 -- values stored in the state). For example,
54 -- @'runST' (writeSTRef _|_ v >>= f) = _|_@
55 newtype ST s a = ST (STRep s a)
56 type STRep s a = State# s -> (# State# s, a #)
58 instance Functor (ST s) where
59 fmap f (ST m) = ST $ \ s ->
60 case (m s) of { (# new_s, r #) ->
63 instance Monad (ST s) where
67 return x = ST (\ s -> (# s, x #))
68 m >> k = m >>= \ _ -> k
72 case (m s) of { (# new_s, r #) ->
73 case (k r) of { ST k2 ->
76 data STret s a = STret (State# s) a
78 -- liftST is useful when we want a lifted result from an ST computation. See
80 liftST :: ST s a -> State# s -> STret s a
81 liftST (ST m) = \s -> case m s of (# s', r #) -> STret s' r
83 {-# NOINLINE unsafeInterleaveST #-}
84 unsafeInterleaveST :: ST s a -> ST s a
85 unsafeInterleaveST (ST m) = ST ( \ s ->
87 r = case m s of (# _, res #) -> res
92 -- | Allow the result of a state transformer computation to be used (lazily)
93 -- inside the computation.
94 -- Note that if @f@ is strict, @'fixST' f = _|_@.
95 fixST :: (a -> ST s a) -> ST s a
97 let ans = liftST (k r) s
100 case ans of STret s' x -> (# s', x #)
102 instance Show (ST s a) where
103 showsPrec _ _ = showString "<<ST action>>"
104 showList = showList__ (showsPrec 0)
110 SLPJ 95/04: Why @runST@ must not have an unfolding; consider:
114 (a, s') = newArray# 100 [] s
115 (_, s'') = fill_in_array_or_something a x s'
119 If we inline @runST@, we'll get:
122 (a, s') = newArray# 100 [] realWorld#{-NB-}
123 (_, s'') = fill_in_array_or_something a x s'
127 And now the @newArray#@ binding can be floated to become a CAF, which
128 is totally and utterly wrong:
131 (a, s') = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
134 let (_, s'') = fill_in_array_or_something a x s' in
137 All calls to @f@ will share a {\em single} array! End SLPJ 95/04.
141 -- The INLINE prevents runSTRep getting inlined in *this* module
142 -- so that it is still visible when runST is inlined in an importing
143 -- module. Regrettably delicate. runST is behaving like a wrapper.
145 -- | Return the value computed by a state transformer computation.
146 -- The @forall@ ensures that the internal state used by the 'ST'
147 -- computation is inaccessible to the rest of the program.
148 runST :: (forall s. ST s a) -> a
149 runST st = runSTRep (case st of { ST st_rep -> st_rep })
151 -- I'm only letting runSTRep be inlined right at the end, in particular *after* full laziness
152 -- That's what the "INLINE [0]" says.
154 -- {-# INLINE [0] runSTRep #-}
156 -- SDM: further to the above, inline phase 0 is run *before*
157 -- full-laziness at the moment, which means that the above comment is
158 -- invalid. Inlining runSTRep doesn't make a huge amount of
159 -- difference, anyway. Hence:
161 {-# NOINLINE runSTRep #-}
162 runSTRep :: (forall s. STRep s a) -> a
163 runSTRep st_rep = case st_rep realWorld# of