0e3485725e40d34c4a1f141a7ee0686d535d3c27
[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 -- This library provides support for /strict/ state threads, as
12 -- described in the PLDI \'94 paper by John Launchbury and Simon Peyton
13 -- Jones /Lazy State Threads/.
14 --
15 -----------------------------------------------------------------------------
16
17 module Control.Monad.ST
18   (
19         -- * The 'ST' Monad
20         ST,             -- abstract, instance of Functor, Monad, Typeable.
21         runST,          -- :: (forall s. ST s a) -> a
22         fixST,          -- :: (a -> ST s a) -> ST s a
23
24         -- * Converting 'ST' to 'IO'
25         RealWorld,              -- abstract
26         stToIO,                 -- :: ST RealWorld a -> IO a
27
28         -- * Unsafe operations
29         unsafeInterleaveST,     -- :: ST s a -> ST s a
30         unsafeIOToST            -- :: IO a -> ST s a
31       ) where
32
33 import Prelude
34
35 import Control.Monad.Fix
36
37 #include "Typeable.h"
38
39 #ifdef __HUGS__
40 import Data.Typeable
41 import Hugs.ST
42 import qualified Hugs.LazyST as LazyST
43
44 INSTANCE_TYPEABLE2(ST,sTTc,"ST")
45 INSTANCE_TYPEABLE0(RealWorld,realWorldTc,"RealWorld")
46
47 fixST :: (a -> ST s a) -> ST s a
48 fixST f = LazyST.lazyToStrictST (LazyST.fixST (LazyST.strictToLazyST . f))
49
50 unsafeInterleaveST :: ST s a -> ST s a
51 unsafeInterleaveST =
52     LazyST.lazyToStrictST . LazyST.unsafeInterleaveST . LazyST.strictToLazyST
53 #endif
54
55 #ifdef __GLASGOW_HASKELL__
56 import GHC.ST           ( ST, runST, fixST, unsafeInterleaveST )
57 import GHC.Base         ( RealWorld )
58 import GHC.IOBase       ( stToIO, unsafeIOToST )
59 #endif
60
61 instance MonadFix (ST s) where
62         mfix = fixST
63