[project @ 2002-02-12 11:44:54 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelST.lhs
1 % ------------------------------------------------------------------------------
2 % $Id: PrelST.lhs,v 1.21 2001/09/26 15:12:37 simonpj Exp $
3 %
4 % (c) The University of Glasgow, 1992-2000
5 %
6
7 \section[PrelST]{The @ST@ monad}
8
9 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11
12 module PrelST where
13
14 import PrelBase
15 import PrelShow
16 import PrelNum
17
18 default ()
19 \end{code}
20
21 %*********************************************************
22 %*                                                      *
23 \subsection{The @ST@ monad}
24 %*                                                      *
25 %*********************************************************
26
27 The state-transformer monad proper.  By default the monad is strict;
28 too many people got bitten by space leaks when it was lazy.
29
30 \begin{code}
31 newtype ST s a = ST (STRep s a)
32 type STRep s a = State# s -> (# State# s, a #)
33
34 instance Functor (ST s) where
35     fmap f (ST m) = ST $ \ s ->
36       case (m s) of { (# new_s, r #) ->
37       (# new_s, f r #) }
38
39 instance Monad (ST s) where
40     {-# INLINE return #-}
41     {-# INLINE (>>)   #-}
42     {-# INLINE (>>=)  #-}
43     return x = ST $ \ s -> (# s, x #)
44     m >> k   =  m >>= \ _ -> k
45
46     (ST m) >>= k
47       = ST $ \ s ->
48         case (m s) of { (# new_s, r #) ->
49         case (k r) of { ST k2 ->
50         (k2 new_s) }}
51
52 data STret s a = STret (State# s) a
53
54 -- liftST is useful when we want a lifted result from an ST computation.  See
55 -- fixST below.
56 liftST :: ST s a -> State# s -> STret s a
57 liftST (ST m) = \s -> case m s of (# s', r #) -> STret s' r
58
59 {-# NOINLINE unsafeInterleaveST #-}
60 unsafeInterleaveST :: ST s a -> ST s a
61 unsafeInterleaveST (ST m) = ST ( \ s ->
62     let
63         r = case m s of (# _, res #) -> res
64     in
65     (# s, r #)
66   )
67
68 instance  Show (ST s a)  where
69     showsPrec _ _  = showString "<<ST action>>"
70     showList       = showList__ (showsPrec 0)
71 \end{code}
72
73 Definition of runST
74 ~~~~~~~~~~~~~~~~~~~
75
76 SLPJ 95/04: Why @runST@ must not have an unfolding; consider:
77 \begin{verbatim}
78 f x =
79   runST ( \ s -> let
80                     (a, s')  = newArray# 100 [] s
81                     (_, s'') = fill_in_array_or_something a x s'
82                   in
83                   freezeArray# a s'' )
84 \end{verbatim}
85 If we inline @runST@, we'll get:
86 \begin{verbatim}
87 f x = let
88         (a, s')  = newArray# 100 [] realWorld#{-NB-}
89         (_, s'') = fill_in_array_or_something a x s'
90       in
91       freezeArray# a s''
92 \end{verbatim}
93 And now the @newArray#@ binding can be floated to become a CAF, which
94 is totally and utterly wrong:
95 \begin{verbatim}
96 f = let
97     (a, s')  = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
98     in
99     \ x ->
100         let (_, s'') = fill_in_array_or_something a x s' in
101         freezeArray# a s''
102 \end{verbatim}
103 All calls to @f@ will share a {\em single} array!  End SLPJ 95/04.
104
105 \begin{code}
106 {-# INLINE runST #-}
107 -- The INLINE prevents runSTRep getting inlined in *this* module
108 -- so that it is still visible when runST is inlined in an importing
109 -- module.  Regrettably delicate.  runST is behaving like a wrapper.
110 runST :: (forall s. ST s a) -> a
111 runST st = runSTRep (case st of { ST st_rep -> st_rep })
112
113 -- I'm only letting runSTRep be inlined right at the end, in particular *after* full laziness
114 -- That's what the "INLINE [0]" says.
115 --              SLPJ Apr 99
116 {-# INLINE [0] runSTRep #-}
117 runSTRep :: (forall s. STRep s a) -> a
118 runSTRep st_rep = case st_rep realWorld# of
119                         (# _, r #) -> r
120 \end{code}