00aa4f06d58f4b7350e8e3ff97c38c89968fbd07
[ghc-base.git] / Control / Monad / ST / Lazy.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Control.Monad.ST.Lazy
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  provisional
9 -- Portability :  non-portable (requires universal quantification for runST)
10 --
11 -- This module presents an identical interface to "Control.Monad.ST",
12 -- except that the monad delays evaluation of state operations until
13 -- a value depending on them is required.
14 --
15 -----------------------------------------------------------------------------
16
17 module Control.Monad.ST.Lazy (
18         -- * The 'ST' monad
19         ST,
20         runST,
21         fixST,
22
23         -- * Converting between strict and lazy 'ST'
24         strictToLazyST, lazyToStrictST,
25
26         -- * Converting 'ST' To 'IO'
27         RealWorld,
28         stToIO,
29
30         -- * Unsafe operations
31         unsafeInterleaveST,
32         unsafeIOToST
33     ) where
34
35 import Prelude
36
37 import Control.Monad.Fix
38
39 import Control.Monad.ST (RealWorld)
40 import qualified Control.Monad.ST as ST
41
42 #ifdef __GLASGOW_HASKELL__
43 import qualified GHC.ST
44 import GHC.Base
45 import Control.Monad
46 #endif
47
48 #ifdef __HUGS__
49 import Hugs.LazyST
50 #endif
51
52 #ifdef __GLASGOW_HASKELL__
53 -- | The lazy state-transformer monad.
54 -- A computation of type @'ST' s a@ transforms an internal state indexed
55 -- by @s@, and returns a value of type @a@.
56 -- The @s@ parameter is either
57 --
58 -- * an unstantiated type variable (inside invocations of 'runST'), or
59 --
60 -- * 'RealWorld' (inside invocations of 'stToIO').
61 --
62 -- It serves to keep the internal states of different invocations of
63 -- 'runST' separate from each other and from invocations of 'stToIO'.
64 --
65 -- The '>>=' and '>>' operations are not strict in the state.  For example,
66 --
67 -- @'runST' (writeSTRef _|_ v >>= readSTRef _|_ >> return 2) = 2@
68 newtype ST s a = ST (State s -> (a, State s))
69 data State s = S# (State# s)
70
71 instance Functor (ST s) where
72     fmap f m = ST $ \ s ->
73       let 
74        ST m_a = m
75        (r,new_s) = m_a s
76       in
77       (f r,new_s)
78
79 instance Monad (ST s) where
80
81         return a = ST $ \ s -> (a,s)
82         m >> k   =  m >>= \ _ -> k
83         fail s   = error s
84
85         (ST m) >>= k
86          = ST $ \ s ->
87            let
88              (r,new_s) = m s
89              ST k_a = k r
90            in
91            k_a new_s
92
93 {-# NOINLINE runST #-}
94 -- | Return the value computed by a state transformer computation.
95 -- The @forall@ ensures that the internal state used by the 'ST'
96 -- computation is inaccessible to the rest of the program.
97 runST :: (forall s. ST s a) -> a
98 runST st = case st of ST the_st -> let (r,_) = the_st (S# realWorld#) in r
99
100 -- | Allow the result of a state transformer computation to be used (lazily)
101 -- inside the computation.
102 -- Note that if @f@ is strict, @'fixST' f = _|_@.
103 fixST :: (a -> ST s a) -> ST s a
104 fixST m = ST (\ s -> 
105                 let 
106                    ST m_r = m r
107                    (r,s') = m_r s
108                 in
109                    (r,s'))
110 #endif
111
112 instance MonadFix (ST s) where
113         mfix = fixST
114
115 -- ---------------------------------------------------------------------------
116 -- Strict <--> Lazy
117
118 #ifdef __GLASGOW_HASKELL__
119 {-|
120 Convert a strict 'ST' computation into a lazy one.  The strict state
121 thread passed to 'strictToLazyST' is not performed until the result of
122 the lazy state thread it returns is demanded.
123 -}
124 strictToLazyST :: ST.ST s a -> ST s a
125 strictToLazyST m = ST $ \s ->
126         let 
127            pr = case s of { S# s# -> GHC.ST.liftST m s# }
128            r  = case pr of { GHC.ST.STret _ v -> v }
129            s' = case pr of { GHC.ST.STret s2# _ -> S# s2# }
130         in
131         (r, s')
132
133 {-| 
134 Convert a lazy 'ST' computation into a strict one.
135 -}
136 lazyToStrictST :: ST s a -> ST.ST s a
137 lazyToStrictST (ST m) = GHC.ST.ST $ \s ->
138         case (m (S# s)) of (a, S# s') -> (# s', a #)
139
140 unsafeInterleaveST :: ST s a -> ST s a
141 unsafeInterleaveST = strictToLazyST . ST.unsafeInterleaveST . lazyToStrictST
142 #endif
143
144 unsafeIOToST :: IO a -> ST s a
145 unsafeIOToST = strictToLazyST . ST.unsafeIOToST
146
147 -- | A monad transformer embedding lazy state transformers in the 'IO'
148 -- monad.  The 'RealWorld' parameter indicates that the internal state
149 -- used by the 'ST' computation is a special one supplied by the 'IO'
150 -- monad, and thus distinct from those used by invocations of 'runST'.
151 stToIO :: ST RealWorld a -> IO a
152 stToIO = ST.stToIO . lazyToStrictST