1 -----------------------------------------------------------------------------
3 -- Module : Control.Monad.ST.Lazy
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : non-portable (requires universal quantification for runST)
11 -- This module presents an identical interface to Control.Monad.ST,
12 -- but the underlying implementation of the state thread is lazy.
14 -----------------------------------------------------------------------------
16 module Control.Monad.ST.Lazy (
23 ST.unsafeIOToST, ST.stToIO,
25 strictToLazyST, lazyToStrictST
30 #ifdef __GLASGOW_HASKELL__
31 import qualified Control.Monad.ST as ST
32 import qualified GHC.ST
37 #ifdef __GLASGOW_HASKELL__
38 newtype ST s a = ST (State s -> (a, State s))
39 data State s = S# (State# s)
42 instance Functor (ST s) where
43 fmap f m = ST $ \ s ->
50 instance Monad (ST s) where
52 return a = ST $ \ s -> (a,s)
53 m >> k = m >>= \ _ -> k
65 #ifdef __GLASGOW_HASKELL__
66 {-# NOINLINE runST #-}
67 runST :: (forall s. ST s a) -> a
68 runST st = case st of ST the_st -> let (r,_) = the_st (S# realWorld#) in r
70 fixST :: (a -> ST s a) -> ST s a
79 -- ---------------------------------------------------------------------------
82 #ifdef __GLASGOW_HASKELL__
83 strictToLazyST :: ST.ST s a -> ST s a
84 strictToLazyST m = ST $ \s ->
86 pr = case s of { S# s# -> GHC.ST.liftST m s# }
87 r = case pr of { GHC.ST.STret _ v -> v }
88 s' = case pr of { GHC.ST.STret s2# _ -> S# s2# }
92 lazyToStrictST :: ST s a -> ST.ST s a
93 lazyToStrictST (ST m) = GHC.ST.ST $ \s ->
94 case (m (S# s)) of (a, S# s') -> (# s', a #)
97 unsafeInterleaveST :: ST s a -> ST s a
98 unsafeInterleaveST = strictToLazyST . ST.unsafeInterleaveST . lazyToStrictST