cae255fa6faa49811ea0316308a8f5142909f43f
[ghc-base.git] / Control / Monad / ST.hs
1 {-# OPTIONS_GHC -fno-warn-orphans #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Control.Monad.ST
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  experimental
10 -- Portability :  non-portable (requires universal quantification for runST)
11 --
12 -- This library provides support for /strict/ state threads, as
13 -- described in the PLDI \'94 paper by John Launchbury and Simon Peyton
14 -- Jones /Lazy Functional State Threads/.
15 --
16 -----------------------------------------------------------------------------
17
18 module Control.Monad.ST
19   (
20         -- * The 'ST' Monad
21         ST,             -- abstract, instance of Functor, Monad, Typeable.
22         runST,          -- :: (forall s. ST s a) -> a
23         fixST,          -- :: (a -> ST s a) -> ST s a
24
25         -- * Converting 'ST' to 'IO'
26         RealWorld,              -- abstract
27         stToIO,                 -- :: ST RealWorld a -> IO a
28
29         -- * Unsafe operations
30         unsafeInterleaveST,     -- :: ST s a -> ST s a
31         unsafeIOToST,           -- :: IO a -> ST s a
32         unsafeSTToIO            -- :: ST s a -> IO a
33       ) where
34
35 import Prelude
36
37 import Control.Monad.Fix
38
39 #include "Typeable.h"
40
41 #ifdef __HUGS__
42 import Data.Typeable
43 import Hugs.ST
44 import qualified Hugs.LazyST as LazyST
45
46 INSTANCE_TYPEABLE2(ST,sTTc,"ST")
47 INSTANCE_TYPEABLE0(RealWorld,realWorldTc,"RealWorld")
48
49 fixST :: (a -> ST s a) -> ST s a
50 fixST f = LazyST.lazyToStrictST (LazyST.fixST (LazyST.strictToLazyST . f))
51
52 unsafeInterleaveST :: ST s a -> ST s a
53 unsafeInterleaveST =
54     LazyST.lazyToStrictST . LazyST.unsafeInterleaveST . LazyST.strictToLazyST
55 #endif
56
57 #ifdef __GLASGOW_HASKELL__
58 import GHC.ST           ( ST, runST, fixST, unsafeInterleaveST )
59 import GHC.Base         ( RealWorld )
60 import GHC.IO           ( stToIO, unsafeIOToST, unsafeSTToIO )
61 #endif
62
63 instance MonadFix (ST s) where
64         mfix = fixST
65