1 -----------------------------------------------------------------------------
3 -- Module : Control.Monad.ST
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 : experimental
9 -- Portability : non-portable (requires universal quantification for runST)
11 -- $Id: ST.hs,v 1.5 2001/12/21 15:07:21 simonmar Exp $
13 -- The State Transformer Monad, ST
15 -----------------------------------------------------------------------------
17 module Control.Monad.ST
19 ST -- abstract, instance of Functor, Monad, Typeable.
20 , runST -- :: (forall s. ST s a) -> a
21 , fixST -- :: (a -> ST s a) -> ST s a
22 , unsafeInterleaveST -- :: ST s a -> ST s a
24 , unsafeIOToST -- :: IO a -> ST s a
26 , RealWorld -- abstract
27 , stToIO -- :: ST RealWorld a -> IO a
32 import Control.Monad.Fix
35 #ifdef __GLASGOW_HASKELL__
37 import GHC.Base ( unsafeCoerce#, RealWorld )
38 import GHC.IOBase ( IO(..), stToIO )
40 -- This relies on IO and ST having the same representation modulo the
41 -- constraint on the type of the state
43 unsafeIOToST :: IO a -> ST s a
44 unsafeIOToST (IO io) = ST $ \ s -> (unsafeCoerce# io) s
47 instance MonadFix (ST s) where
50 -- ---------------------------------------------------------------------------
56 instance (Typeable a, Typeable b) => Typeable (ST a b) where
57 typeOf st = mkAppTy sTTc [typeOf ((undefined :: ST a b -> a) st),
58 typeOf ((undefined :: ST a b -> b) st)]