61bdc97927998489072d13ddd27286432b1e8220
[ghc-base.git] / Control / Monad / ST.hs
1 -----------------------------------------------------------------------------
2 -- 
3 -- Module      :  Control.Monad.ST
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/core/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  non-portable (requires universal quantification for runST)
10 --
11 -- $Id: ST.hs,v 1.3 2001/07/03 11:37:49 simonmar Exp $
12 --
13 -- The State Transformer Monad, ST
14 --
15 -----------------------------------------------------------------------------
16
17 module Control.Monad.ST
18       (
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
23
24       , unsafeIOToST        -- :: IO a -> ST s a
25
26       , RealWorld           -- abstract
27       , stToIO              -- :: ST RealWorld a -> IO a
28       ) where
29
30 import Prelude
31
32 import Control.Monad.Fix
33 import Data.Dynamic
34
35 #ifdef __GLASGOW_HASKELL__
36 import GHC.ST
37 import GHC.Prim         ( unsafeCoerce#, RealWorld )
38 import GHC.IOBase       ( IO(..), stToIO )
39
40 unsafeIOToST        :: IO a -> ST s a
41 unsafeIOToST (IO io) = ST $ \ s -> (unsafeCoerce# io) s
42 #endif
43
44 instance MonadFix (ST s) where
45         mfix = fixST
46
47 -- ---------------------------------------------------------------------------
48 -- Typeable instance
49
50 sTTc :: TyCon
51 sTTc = mkTyCon "ST"
52
53 instance (Typeable a, Typeable b) => Typeable (ST a b) where
54   typeOf st = mkAppTy sTTc [typeOf ((undefined :: ST a b -> a) st),
55                             typeOf ((undefined :: ST a b -> b) st)]