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 -- $Id: Lazy.hs,v 1.5 2002/04/24 16:31:39 simonmar Exp $
13 -- This module presents an identical interface to Control.Monad.ST,
14 -- but the underlying implementation of the state thread is lazy.
16 -----------------------------------------------------------------------------
18 module Control.Monad.ST.Lazy (
25 ST.unsafeIOToST, ST.stToIO,
27 strictToLazyST, lazyToStrictST
32 #ifdef __GLASGOW_HASKELL__
33 import qualified Control.Monad.ST as ST
34 import qualified GHC.ST
39 #ifdef __GLASGOW_HASKELL__
40 newtype ST s a = ST (State s -> (a, State s))
41 data State s = S# (State# s)
44 instance Functor (ST s) where
45 fmap f m = ST $ \ s ->
52 instance Monad (ST s) where
54 return a = ST $ \ s -> (a,s)
55 m >> k = m >>= \ _ -> k
67 #ifdef __GLASGOW_HASKELL__
68 {-# NOINLINE runST #-}
69 runST :: (forall s. ST s a) -> a
70 runST st = case st of ST the_st -> let (r,_) = the_st (S# realWorld#) in r
72 fixST :: (a -> ST s a) -> ST s a
81 -- ---------------------------------------------------------------------------
84 #ifdef __GLASGOW_HASKELL__
85 strictToLazyST :: ST.ST s a -> ST s a
86 strictToLazyST m = ST $ \s ->
88 pr = case s of { S# s# -> GHC.ST.liftST m s# }
89 r = case pr of { GHC.ST.STret _ v -> v }
90 s' = case pr of { GHC.ST.STret s2# _ -> S# s2# }
94 lazyToStrictST :: ST s a -> ST.ST s a
95 lazyToStrictST (ST m) = GHC.ST.ST $ \s ->
96 case (m (S# s)) of (a, S# s') -> (# s', a #)
99 unsafeInterleaveST :: ST s a -> ST s a
100 unsafeInterleaveST = strictToLazyST . ST.unsafeInterleaveST . lazyToStrictST