[project @ 1997-11-24 15:42:47 by simonm]
[ghc-hetmet.git] / ghc / lib / glaExts / IOExts.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4
5 \section[IOExts]{Module @IOExts@}
6
7 \begin{code}
8 {-# OPTIONS -fno-implicit-prelude #-}
9
10 module IOExts
11         ( fixIO
12         , unsafePerformIO
13         , unsafeInterleaveIO
14
15         , IORef
16           -- instance Eq (IORef a)
17         , newIORef
18         , readIORef
19         , writeIORef
20
21         , IOArray
22           -- instance Eq (IOArray ix a)
23         , newIOArray
24         , boundsIOArray
25         , readIOArray
26         , writeIOArray
27         , freezeIOArray
28
29         , trace
30         , performGC
31         
32         , reallyUnsafePtrEq
33         ) where
34 \end{code}
35
36 \begin{code}
37 import PrelBase
38 import IOBase
39 import STBase
40 import Unsafe
41 import GHC
42
43 reallyUnsafePtrEq a b =
44     case reallyUnsafePtrEquality# a b of
45          0# -> False
46          _  -> True
47
48 \begin{code}
49 newtype IORef a = IORef (MutableVar RealWorld a) 
50     deriving Eq
51
52 newIORef :: a -> IO (IORef a)
53 newIORef v = stToIO (newVar v) >>= \ var -> return (IORef var)
54
55 readIORef :: IORef a -> IO a
56 readIORef (IORef var) = stToIO (readVar var)
57
58 writeIORef :: IORef a -> a -> IO ()
59 writeIORef (IORef var) v = stToIO (writeVar var v)
60 \end{code}
61
62 \begin{code}
63 newtype IOArray ix elt = IOArray (MutableArray RealWorld ix elt)
64     deriving Eq
65
66 newIOArray          :: Ix ix => (ix,ix) -> elt -> IO (IOArray ix elt)
67 boundsIOArray       :: Ix ix => IOArray ix elt -> (ix, ix)
68 readIOArray         :: Ix ix => IOArray ix elt -> ix -> IO elt
69 writeIOArray        :: Ix ix => IOArray ix elt -> ix -> elt -> IO ()
70 freezeIOArray       :: Ix ix => IOArray ix elt -> IO (Array ix elt)
71
72 newIOArray ixs elt = 
73     stToIO (newArray ixs elt) >>= \arr -> 
74     return (IOArray arr)
75
76 boundsIOArray (IOArray arr) = boundsOfArray
77
78 readIOArray (IOArray arr) ix = stToIO (readArray arr ix)
79
80 writeIOArray (IOArray arr) ix elt = stToIO (writeArray arr ix elt)
81
82 freezeIOArray (IOArray arr) = stToIO (freezeArray arr)
83 \end{code}