[project @ 2001-06-28 14:15:04 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
10 --
11 -- $Id: ST.hs,v 1.1 2001/06/28 14:15:02 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 Data.Dynamic
33
34 #ifdef __GLASGOW_HASKELL__
35 import GHC.ST
36 import GHC.Prim         ( unsafeCoerce#, RealWorld )
37 import GHC.IOBase       ( IO(..), stToIO )
38
39 unsafeIOToST        :: IO a -> ST s a
40 unsafeIOToST (IO io) = ST $ \ s ->
41     case ((unsafeCoerce# io) s) of
42       (#  new_s, a #) -> unsafeCoerce# (STret new_s a)
43 #endif
44
45 -- ---------------------------------------------------------------------------
46 -- Typeable instance
47
48 sTTc :: TyCon
49 sTTc = mkTyCon "ST"
50
51 instance (Typeable a, Typeable b) => Typeable (ST a b) where
52   typeOf st = mkAppTy sTTc [typeOf ((undefined :: ST a b -> a) st),
53                             typeOf ((undefined :: ST a b -> b) st)]