[project @ 2002-04-24 16:31:37 by simonmar]
[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.6 2002/04/24 16:31:38 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.Base         ( unsafeCoerce#, RealWorld )
38 import GHC.IOBase       ( IO(..), stToIO )
39
40 -- This relies on IO and ST having the same representation modulo the
41 -- constraint on the type of the state
42 --
43 unsafeIOToST        :: IO a -> ST s a
44 unsafeIOToST (IO io) = ST $ \ s -> (unsafeCoerce# io) s
45 #endif
46
47 instance MonadFix (ST s) where
48         mfix = fixST
49
50 -- ---------------------------------------------------------------------------
51 -- Typeable instance
52
53 sTTc :: TyCon
54 sTTc = mkTyCon "ST"
55
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)]