[project @ 1997-08-25 22:43:11 by sof]
[ghc-hetmet.git] / ghc / lib / ghc / UnsafeST.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[UnsafeST]{Unsafe ST operations}
5
6 VERY IMPORTANT!  This module must be compiled without "-O".  If you
7 compile it with "-O" then the inlinings of the unsafe ST operators are exposed.
8 It turns out that exposing these inlininings can lead to unsound transformations,
9 such as generating a MutVar only once rather than once each call to unsafePerformIO.
10
11 \begin{code}
12 {-# OPTIONS -fno-implicit-prelude #-}
13 \end{code}
14
15
16 \begin{code}
17 module UnsafeST(
18         unsafeInterleaveST,
19         unsafePerformPrimIO,
20         unsafeInterleavePrimIO
21   )  where
22
23 import STBase
24 import PrelBase
25 import GHC
26
27
28 unsafeInterleaveST :: ST s a -> ST s a
29 unsafeInterleaveST (ST m) = ST $ \ s ->
30     let
31         (r, new_s) = m s
32     in
33     (r, s)
34
35 unsafePerformPrimIO     :: PrimIO a -> a
36         -- We give a fresh definition here.  There are no
37         -- magical universal types kicking around.
38 unsafePerformPrimIO (ST m)
39   = case m (S# realWorld#) of
40       (r,_) -> r
41
42 unsafeInterleavePrimIO  :: PrimIO a -> PrimIO a
43 unsafeInterleavePrimIO  = unsafeInterleaveST
44 \end{code}
45