[project @ 2003-09-08 16:23:57 by ross]
[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 -- but the underlying implementation of the state thread is /lazy/ (in
13 -- the sense that (@_|_ >> a@ is not necessarily equal to @_|_@).
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 newtype ST s a = ST (State s -> (a, State s))
65 data State s = S# (State# s)
66
67 instance Functor (ST s) where
68     fmap f m = ST $ \ s ->
69       let 
70        ST m_a = m
71        (r,new_s) = m_a s
72       in
73       (f r,new_s)
74
75 instance Monad (ST s) where
76
77         return a = ST $ \ s -> (a,s)
78         m >> k   =  m >>= \ _ -> k
79         fail s   = error s
80
81         (ST m) >>= k
82          = ST $ \ s ->
83            let
84              (r,new_s) = m s
85              ST k_a = k r
86            in
87            k_a new_s
88
89 {-# NOINLINE runST #-}
90 -- | Return the value computed by a state transformer computation.
91 -- The @forall@ ensures that the internal state used by the 'ST'
92 -- computation is inaccessible to the rest of the program.
93 runST :: (forall s. ST s a) -> a
94 runST st = case st of ST the_st -> let (r,_) = the_st (S# realWorld#) in r
95
96 -- | Allow the result of a state transformer computation to be used (lazily)
97 -- inside the computation.
98 -- Note that if @f@ is strict, @'fixST' f = _|_@.
99 fixST :: (a -> ST s a) -> ST s a
100 fixST m = ST (\ s -> 
101                 let 
102                    ST m_r = m r
103                    (r,s') = m_r s
104                 in
105                    (r,s'))
106 #endif
107
108 instance MonadFix (ST s) where
109         mfix = fixST
110
111 -- ---------------------------------------------------------------------------
112 -- Strict <--> Lazy
113
114 #ifdef __GLASGOW_HASKELL__
115 {-|
116 Convert a strict 'ST' computation into a lazy one.  The strict state
117 thread passed to 'strictToLazyST' is not performed until the result of
118 the lazy state thread it returns is demanded.
119 -}
120 strictToLazyST :: ST.ST s a -> ST s a
121 strictToLazyST m = ST $ \s ->
122         let 
123            pr = case s of { S# s# -> GHC.ST.liftST m s# }
124            r  = case pr of { GHC.ST.STret _ v -> v }
125            s' = case pr of { GHC.ST.STret s2# _ -> S# s2# }
126         in
127         (r, s')
128
129 {-| 
130 Convert a lazy 'ST' computation into a strict one.
131 -}
132 lazyToStrictST :: ST s a -> ST.ST s a
133 lazyToStrictST (ST m) = GHC.ST.ST $ \s ->
134         case (m (S# s)) of (a, S# s') -> (# s', a #)
135
136 unsafeInterleaveST :: ST s a -> ST s a
137 unsafeInterleaveST = strictToLazyST . ST.unsafeInterleaveST . lazyToStrictST
138 #endif
139
140 unsafeIOToST :: IO a -> ST s a
141 unsafeIOToST = strictToLazyST . ST.unsafeIOToST
142
143 -- | A monad transformer embedding lazy state transformers in the 'IO'
144 -- monad.  The 'RealWorld' parameter indicates that the internal state
145 -- used by the 'ST' computation is a special one supplied by the 'IO'
146 -- monad, and thus distinct from those used by invocations of 'runST'.
147 stToIO :: ST RealWorld a -> IO a
148 stToIO = ST.stToIO . lazyToStrictST