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 -- The State Transformer Monad, ST
13 -----------------------------------------------------------------------------
15 module Control.Monad.ST
17 ST -- abstract, instance of Functor, Monad, Typeable.
18 , runST -- :: (forall s. ST s a) -> a
19 , fixST -- :: (a -> ST s a) -> ST s a
20 , unsafeInterleaveST -- :: ST s a -> ST s a
22 , unsafeIOToST -- :: IO a -> ST s a
24 , RealWorld -- abstract
25 , stToIO -- :: ST RealWorld a -> IO a
30 import Control.Monad.Fix
33 #ifdef __GLASGOW_HASKELL__
35 import GHC.Base ( unsafeCoerce#, RealWorld )
36 import GHC.IOBase ( IO(..), stToIO )
38 -- This relies on IO and ST having the same representation modulo the
39 -- constraint on the type of the state
41 unsafeIOToST :: IO a -> ST s a
42 unsafeIOToST (IO io) = ST $ \ s -> (unsafeCoerce# io) s
45 instance MonadFix (ST s) where
48 -- ---------------------------------------------------------------------------
54 instance (Typeable a, Typeable b) => Typeable (ST a b) where
55 typeOf st = mkAppTy sTTc [typeOf ((undefined :: ST a b -> a) st),
56 typeOf ((undefined :: ST a b -> b) st)]