8ebb750e3a96f3d1a79695cb9e9166530a299c64
[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 Functional 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         unsafeSTToIO            -- :: ST s a -> IO a
32       ) where
33
34 #if defined(__GLASGOW_HASKELL__)
35 import Control.Monad.Fix ()
36 #else
37 import Control.Monad.Fix
38 #endif
39
40 #include "Typeable.h"
41
42 #if defined(__GLASGOW_HASKELL__)
43 import GHC.ST           ( ST, runST, fixST, unsafeInterleaveST )
44 import GHC.Base         ( RealWorld )
45 import GHC.IO           ( stToIO, unsafeIOToST, unsafeSTToIO )
46 #elif defined(__HUGS__)
47 import Data.Typeable
48 import Hugs.ST
49 import qualified Hugs.LazyST as LazyST
50 #endif
51
52 #if defined(__HUGS__)
53 INSTANCE_TYPEABLE2(ST,sTTc,"ST")
54 INSTANCE_TYPEABLE0(RealWorld,realWorldTc,"RealWorld")
55
56 fixST :: (a -> ST s a) -> ST s a
57 fixST f = LazyST.lazyToStrictST (LazyST.fixST (LazyST.strictToLazyST . f))
58
59 unsafeInterleaveST :: ST s a -> ST s a
60 unsafeInterleaveST =
61     LazyST.lazyToStrictST . LazyST.unsafeInterleaveST . LazyST.strictToLazyST
62 #endif
63
64 #if !defined(__GLASGOW_HASKELL__)
65 instance MonadFix (ST s) where
66         mfix = fixST
67 #endif
68