1 -----------------------------------------------------------------------------
3 -- Module : Control.Monad.ST
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable (requires universal quantification for runST)
11 -- This library provides support for /strict/ state threads, as
12 -- described in the PLDI \'94 paper by John Launchbury and Simon Peyton
13 -- Jones /Lazy State Threads/.
15 -----------------------------------------------------------------------------
17 module Control.Monad.ST
20 ST, -- abstract, instance of Functor, Monad, Typeable.
21 runST, -- :: (forall s. ST s a) -> a
22 fixST, -- :: (a -> ST s a) -> ST s a
24 -- * Converting 'ST' to 'IO'
25 RealWorld, -- abstract
26 stToIO, -- :: ST RealWorld a -> IO a
28 -- * Unsafe operations
29 unsafeInterleaveST, -- :: ST s a -> ST s a
30 unsafeIOToST -- :: IO a -> ST s a
35 import Control.Monad.Fix
40 import qualified Hugs.LazyST as LazyST
42 fixST :: (a -> ST s a) -> ST s a
43 fixST f = LazyST.lazyToStrictST (LazyST.fixST (LazyST.strictToLazyST . f))
45 unsafeInterleaveST :: ST s a -> ST s a
47 LazyST.lazyToStrictST . LazyST.unsafeInterleaveST . LazyST.strictToLazyST
50 #ifdef __GLASGOW_HASKELL__
52 import GHC.Base ( unsafeCoerce#, RealWorld )
53 import GHC.IOBase ( IO(..), stToIO )
55 -- This relies on IO and ST having the same representation modulo the
56 -- constraint on the type of the state
58 unsafeIOToST :: IO a -> ST s a
59 unsafeIOToST (IO io) = ST $ \ s -> (unsafeCoerce# io) s
62 instance MonadFix (ST s) where
65 -- ---------------------------------------------------------------------------
71 instance (Typeable a, Typeable b) => Typeable (ST a b) where
72 typeOf st = mkAppTy sTTc [typeOf ((undefined :: ST a b -> a) st),
73 typeOf ((undefined :: ST a b -> b) st)]