Avoid using deprecated flags
[ghc-base.git] / GHC / ST.lhs
1 \begin{code}
2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module      :  GHC.ST
7 -- Copyright   :  (c) The University of Glasgow, 1992-2002
8 -- License     :  see libraries/base/LICENSE
9 -- 
10 -- Maintainer  :  cvs-ghc@haskell.org
11 -- Stability   :  internal
12 -- Portability :  non-portable (GHC Extensions)
13 --
14 -- The 'ST' Monad.
15 --
16 -----------------------------------------------------------------------------
17
18 -- #hide
19 module GHC.ST where
20
21 import GHC.Base
22 import GHC.Show
23 import GHC.Num
24
25 default ()
26 \end{code}
27
28 %*********************************************************
29 %*                                                      *
30 \subsection{The @ST@ monad}
31 %*                                                      *
32 %*********************************************************
33
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.
36
37 \begin{code}
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
42 --
43 -- * an uninstantiated type variable (inside invocations of 'runST'), or
44 --
45 -- * 'RealWorld' (inside invocations of 'Control.Monad.ST.stToIO').
46 --
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'.
50 --
51 -- The '>>=' and '>>' operations are strict in the state (though not in
52 -- values stored in the state).  For example,
53 --
54 -- @'runST' (writeSTRef _|_ v >>= f) = _|_@
55 newtype ST s a = ST (STRep s a)
56 type STRep s a = State# s -> (# State# s, a #)
57
58 instance Functor (ST s) where
59     fmap f (ST m) = ST $ \ s ->
60       case (m s) of { (# new_s, r #) ->
61       (# new_s, f r #) }
62
63 instance Monad (ST s) where
64     {-# INLINE return #-}
65     {-# INLINE (>>)   #-}
66     {-# INLINE (>>=)  #-}
67     return x = ST (\ s -> (# s, x #))
68     m >> k   = m >>= \ _ -> k
69
70     (ST m) >>= k
71       = ST (\ s ->
72         case (m s) of { (# new_s, r #) ->
73         case (k r) of { ST k2 ->
74         (k2 new_s) }})
75
76 data STret s a = STret (State# s) a
77
78 -- liftST is useful when we want a lifted result from an ST computation.  See
79 -- fixST below.
80 liftST :: ST s a -> State# s -> STret s a
81 liftST (ST m) = \s -> case m s of (# s', r #) -> STret s' r
82
83 {-# NOINLINE unsafeInterleaveST #-}
84 unsafeInterleaveST :: ST s a -> ST s a
85 unsafeInterleaveST (ST m) = ST ( \ s ->
86     let
87         r = case m s of (# _, res #) -> res
88     in
89     (# s, r #)
90   )
91
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
96 fixST k = ST $ \ s ->
97     let ans       = liftST (k r) s
98         STret _ r = ans
99     in
100     case ans of STret s' x -> (# s', x #)
101
102 instance  Show (ST s a)  where
103     showsPrec _ _  = showString "<<ST action>>"
104     showList       = showList__ (showsPrec 0)
105 \end{code}
106
107 Definition of runST
108 ~~~~~~~~~~~~~~~~~~~
109
110 SLPJ 95/04: Why @runST@ must not have an unfolding; consider:
111 \begin{verbatim}
112 f x =
113   runST ( \ s -> let
114                     (a, s')  = newArray# 100 [] s
115                     (_, s'') = fill_in_array_or_something a x s'
116                   in
117                   freezeArray# a s'' )
118 \end{verbatim}
119 If we inline @runST@, we'll get:
120 \begin{verbatim}
121 f x = let
122         (a, s')  = newArray# 100 [] realWorld#{-NB-}
123         (_, s'') = fill_in_array_or_something a x s'
124       in
125       freezeArray# a s''
126 \end{verbatim}
127 And now the @newArray#@ binding can be floated to become a CAF, which
128 is totally and utterly wrong:
129 \begin{verbatim}
130 f = let
131     (a, s')  = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
132     in
133     \ x ->
134         let (_, s'') = fill_in_array_or_something a x s' in
135         freezeArray# a s''
136 \end{verbatim}
137 All calls to @f@ will share a {\em single} array!  End SLPJ 95/04.
138
139 \begin{code}
140 {-# INLINE runST #-}
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.
144
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 })
150
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.
153 --              SLPJ Apr 99
154 -- {-# INLINE [0] runSTRep #-}
155
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:
160
161 {-# NOINLINE runSTRep #-}
162 runSTRep :: (forall s. STRep s a) -> a
163 runSTRep st_rep = case st_rep realWorld# of
164                         (# _, r #) -> r
165 \end{code}