[project @ 2002-05-09 13:16:29 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/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  non-portable (requires universal quantification for runST)
10 --
11 -- The State Transformer Monad, ST
12 --
13 -----------------------------------------------------------------------------
14
15 module Control.Monad.ST
16       (
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
21
22       , unsafeIOToST        -- :: IO a -> ST s a
23
24       , RealWorld           -- abstract
25       , stToIO              -- :: ST RealWorld a -> IO a
26       ) where
27
28 import Prelude
29
30 import Control.Monad.Fix
31 import Data.Dynamic
32
33 #ifdef __GLASGOW_HASKELL__
34 import GHC.ST
35 import GHC.Base         ( unsafeCoerce#, RealWorld )
36 import GHC.IOBase       ( IO(..), stToIO )
37
38 -- This relies on IO and ST having the same representation modulo the
39 -- constraint on the type of the state
40 --
41 unsafeIOToST        :: IO a -> ST s a
42 unsafeIOToST (IO io) = ST $ \ s -> (unsafeCoerce# io) s
43 #endif
44
45 instance MonadFix (ST s) where
46         mfix = fixST
47
48 -- ---------------------------------------------------------------------------
49 -- Typeable instance
50
51 sTTc :: TyCon
52 sTTc = mkTyCon "ST"
53
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)]