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 -----------------------------------------------------------------------------
26 %*********************************************************
28 \subsection{The @ST@ monad}
30 %*********************************************************
32 The state-transformer monad proper. By default the monad is strict;
33 too many people got bitten by space leaks when it was lazy.
36 -- | The strict state-transformer monad.
37 -- A computation of type @'ST' s a@ transforms an internal state indexed
38 -- by @s@, and returns a value of type @a@.
39 -- The @s@ parameter is either
41 -- * an unstantiated type variable (inside invocations of 'runST'), or
43 -- * 'RealWorld' (inside invocations of 'Control.Monad.ST.stToIO').
45 -- It serves to keep the internal states of different invocations
46 -- of 'runST' separate from each other and from invocations of
47 -- 'Control.Monad.ST.stToIO'.
48 newtype ST s a = ST (STRep s a)
49 type STRep s a = State# s -> (# State# s, a #)
51 instance Functor (ST s) where
52 fmap f (ST m) = ST $ \ s ->
53 case (m s) of { (# new_s, r #) ->
56 instance Monad (ST s) where
60 return x = ST (\ s -> (# s, x #))
61 m >> k = m >>= \ _ -> k
65 case (m s) of { (# new_s, r #) ->
66 case (k r) of { ST k2 ->
69 data STret s a = STret (State# s) a
71 -- liftST is useful when we want a lifted result from an ST computation. See
73 liftST :: ST s a -> State# s -> STret s a
74 liftST (ST m) = \s -> case m s of (# s', r #) -> STret s' r
76 {-# NOINLINE unsafeInterleaveST #-}
77 unsafeInterleaveST :: ST s a -> ST s a
78 unsafeInterleaveST (ST m) = ST ( \ s ->
80 r = case m s of (# _, res #) -> res
85 -- | Allow the result of a state transformer computation to be used (lazily)
86 -- inside the computation.
87 -- Note that if @f@ is strict, @'fixST' f = _|_@.
88 fixST :: (a -> ST s a) -> ST s a
90 let ans = liftST (k r) s
93 case ans of STret s' x -> (# s', x #)
95 instance Show (ST s a) where
96 showsPrec _ _ = showString "<<ST action>>"
97 showList = showList__ (showsPrec 0)
103 SLPJ 95/04: Why @runST@ must not have an unfolding; consider:
107 (a, s') = newArray# 100 [] s
108 (_, s'') = fill_in_array_or_something a x s'
112 If we inline @runST@, we'll get:
115 (a, s') = newArray# 100 [] realWorld#{-NB-}
116 (_, s'') = fill_in_array_or_something a x s'
120 And now the @newArray#@ binding can be floated to become a CAF, which
121 is totally and utterly wrong:
124 (a, s') = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
127 let (_, s'') = fill_in_array_or_something a x s' in
130 All calls to @f@ will share a {\em single} array! End SLPJ 95/04.
134 -- The INLINE prevents runSTRep getting inlined in *this* module
135 -- so that it is still visible when runST is inlined in an importing
136 -- module. Regrettably delicate. runST is behaving like a wrapper.
138 -- | Return the value computed by a state transformer computation.
139 -- The @forall@ ensures that the internal state used by the 'ST'
140 -- computation is inaccessible to the rest of the program.
141 runST :: (forall s. ST s a) -> a
142 runST st = runSTRep (case st of { ST st_rep -> st_rep })
144 -- I'm only letting runSTRep be inlined right at the end, in particular *after* full laziness
145 -- That's what the "INLINE [0]" says.
147 {-# INLINE [0] runSTRep #-}
148 runSTRep :: (forall s. STRep s a) -> a
149 runSTRep st_rep = case st_rep realWorld# of