b98316b8cd2873081a7f8f8591edf037bf72c44d
[packages/old-locale.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 -- * Unsafe operations
24 unsafeInterleaveST,
25 ST.unsafeIOToST,
26
27 -- * Converting 'ST' To 'IO'
28 RealWorld,
29 ST.stToIO,
30
31 -- * Converting between strict and lazy 'ST'
32 strictToLazyST, lazyToStrictST
33 ) where
34
35 import Prelude
36
37 #ifdef __GLASGOW_HASKELL__
38 import qualified Control.Monad.ST as ST
39 import qualified GHC.ST
40 import GHC.Base
41 import Control.Monad
42 #endif
43
44 #ifdef __GLASGOW_HASKELL__
45 newtype ST s a = ST (State s -> (a, State s))
46 data State s = S# (State# s)
47 #endif
48
49 instance Functor (ST s) where
50 fmap f m = ST $ \ s ->
51 let
52 ST m_a = m
53 (r,new_s) = m_a s
54 in
55 (f r,new_s)
56
57 instance Monad (ST s) where
58
59 return a = ST $ \ s -> (a,s)
60 m >> k = m >>= \ _ -> k
61 fail s = error s
62
63 (ST m) >>= k
64 = ST $ \ s ->
65 let
66 (r,new_s) = m s
67 ST k_a = k r
68 in
69 k_a new_s
70
71
72 #ifdef __GLASGOW_HASKELL__
73 {-# NOINLINE runST #-}
74 runST :: (forall s. ST s a) -> a
75 runST st = case st of ST the_st -> let (r,_) = the_st (S# realWorld#) in r
76
77 fixST :: (a -> ST s a) -> ST s a
78 fixST m = ST (\ s ->
79 let
80 ST m_r = m r
81 (r,s') = m_r s
82 in
83 (r,s'))
84 #endif
85
86 -- ---------------------------------------------------------------------------
87 -- Strict <--> Lazy
88
89 #ifdef __GLASGOW_HASKELL__
90 {-|
91 Convert a strict 'ST' computation into a lazy one. The strict state
92 thread passed to 'strictToLazyST' is not performed until the result of
93 the lazy state thread it returns is demanded.
94 -}
95 strictToLazyST :: ST.ST s a -> ST s a
96 strictToLazyST m = ST $ \s ->
97 let
98 pr = case s of { S# s# -> GHC.ST.liftST m s# }
99 r = case pr of { GHC.ST.STret _ v -> v }
100 s' = case pr of { GHC.ST.STret s2# _ -> S# s2# }
101 in
102 (r, s')
103
104 {-|
105 Convert a lazy 'ST' computation into a strict one.
106 -}
107 lazyToStrictST :: ST s a -> ST.ST s a
108 lazyToStrictST (ST m) = GHC.ST.ST $ \s ->
109 case (m (S# s)) of (a, S# s') -> (# s', a #)
110 #endif
111
112 unsafeInterleaveST :: ST s a -> ST s a
113 unsafeInterleaveST = strictToLazyST . ST.unsafeInterleaveST . lazyToStrictST