2 {-# OPTIONS_GHC -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow, 1992-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
15 -----------------------------------------------------------------------------
27 %*********************************************************
29 \subsection{The @ST@ monad}
31 %*********************************************************
33 The state-transformer monad proper. By default the monad is strict;
34 too many people got bitten by space leaks when it was lazy.
37 -- | The strict state-transformer monad.
38 -- A computation of type @'ST' s a@ transforms an internal state indexed
39 -- by @s@, and returns a value of type @a@.
40 -- The @s@ parameter is either
42 -- * an uninstantiated type variable (inside invocations of 'runST'), or
44 -- * 'RealWorld' (inside invocations of 'Control.Monad.ST.stToIO').
46 -- It serves to keep the internal states of different invocations
47 -- of 'runST' separate from each other and from invocations of
48 -- 'Control.Monad.ST.stToIO'.
49 newtype ST s a = ST (STRep s a)
50 type STRep s a = State# s -> (# State# s, a #)
52 instance Functor (ST s) where
53 fmap f (ST m) = ST $ \ s ->
54 case (m s) of { (# new_s, r #) ->
57 instance Monad (ST s) where
61 return x = ST (\ s -> (# s, x #))
62 m >> k = m >>= \ _ -> k
66 case (m s) of { (# new_s, r #) ->
67 case (k r) of { ST k2 ->
70 data STret s a = STret (State# s) a
72 -- liftST is useful when we want a lifted result from an ST computation. See
74 liftST :: ST s a -> State# s -> STret s a
75 liftST (ST m) = \s -> case m s of (# s', r #) -> STret s' r
77 {-# NOINLINE unsafeInterleaveST #-}
78 unsafeInterleaveST :: ST s a -> ST s a
79 unsafeInterleaveST (ST m) = ST ( \ s ->
81 r = case m s of (# _, res #) -> res
86 -- | Allow the result of a state transformer computation to be used (lazily)
87 -- inside the computation.
88 -- Note that if @f@ is strict, @'fixST' f = _|_@.
89 fixST :: (a -> ST s a) -> ST s a
91 let ans = liftST (k r) s
94 case ans of STret s' x -> (# s', x #)
96 instance Show (ST s a) where
97 showsPrec _ _ = showString "<<ST action>>"
98 showList = showList__ (showsPrec 0)
104 SLPJ 95/04: Why @runST@ must not have an unfolding; consider:
108 (a, s') = newArray# 100 [] s
109 (_, s'') = fill_in_array_or_something a x s'
113 If we inline @runST@, we'll get:
116 (a, s') = newArray# 100 [] realWorld#{-NB-}
117 (_, s'') = fill_in_array_or_something a x s'
121 And now the @newArray#@ binding can be floated to become a CAF, which
122 is totally and utterly wrong:
125 (a, s') = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
128 let (_, s'') = fill_in_array_or_something a x s' in
131 All calls to @f@ will share a {\em single} array! End SLPJ 95/04.
135 -- The INLINE prevents runSTRep getting inlined in *this* module
136 -- so that it is still visible when runST is inlined in an importing
137 -- module. Regrettably delicate. runST is behaving like a wrapper.
139 -- | Return the value computed by a state transformer computation.
140 -- The @forall@ ensures that the internal state used by the 'ST'
141 -- computation is inaccessible to the rest of the program.
142 runST :: (forall s. ST s a) -> a
143 runST st = runSTRep (case st of { ST st_rep -> st_rep })
145 -- I'm only letting runSTRep be inlined right at the end, in particular *after* full laziness
146 -- That's what the "INLINE [0]" says.
148 -- {-# INLINE [0] runSTRep #-}
150 -- SDM: further to the above, inline phase 0 is run *before*
151 -- full-laziness at the moment, which means that the above comment is
152 -- invalid. Inlining runSTRep doesn't make a huge amount of
153 -- difference, anyway. Hence:
155 {-# NOINLINE runSTRep #-}
156 runSTRep :: (forall s. STRep s a) -> a
157 runSTRep st_rep = case st_rep realWorld# of