2 % (c) The AQUA Project, Glasgow University, 1994-1996
4 \section[IOExts]{Module @IOExts@}
6 @IOExts@ provides useful functionality that fall outside the
7 standard Haskell IO interface. Expect the contents of IOExts
8 to be the same for Hugs and GHC (same goes for any other
9 Hugs/GHC extension libraries, unless a function/type is
10 explicitly flagged as being implementation specific
14 {-# OPTIONS -fno-implicit-prelude #-}
21 , IORef -- instance of: Eq
26 , IOArray -- instance of: Eq
54 import PrelHandle ( openFileEx, IOModeEx(..),
55 hSetEcho, hGetEcho, getHandleFd
65 reallyUnsafePtrEq :: a -> a -> Bool
66 reallyUnsafePtrEq a b =
67 case reallyUnsafePtrEquality# a b of
73 newtype IORef a = IORef (MutableVar RealWorld a)
76 newIORef :: a -> IO (IORef a)
77 newIORef v = stToIO (newVar v) >>= \ var -> return (IORef var)
79 readIORef :: IORef a -> IO a
80 readIORef (IORef var) = stToIO (readVar var)
82 writeIORef :: IORef a -> a -> IO ()
83 writeIORef (IORef var) v = stToIO (writeVar var v)
87 newtype IOArray ix elt = IOArray (MutableArray RealWorld ix elt)
90 newIOArray :: Ix ix => (ix,ix) -> elt -> IO (IOArray ix elt)
91 boundsIOArray :: Ix ix => IOArray ix elt -> (ix, ix)
92 readIOArray :: Ix ix => IOArray ix elt -> ix -> IO elt
93 writeIOArray :: Ix ix => IOArray ix elt -> ix -> elt -> IO ()
94 freezeIOArray :: Ix ix => IOArray ix elt -> IO (Array ix elt)
97 stToIO (newArray ixs elt) >>= \arr ->
100 boundsIOArray (IOArray arr) = boundsOfArray arr
102 readIOArray (IOArray arr) ix = stToIO (readArray arr ix)
104 writeIOArray (IOArray arr) ix elt = stToIO (writeArray arr ix elt)
106 freezeIOArray (IOArray arr) = stToIO (freezeArray arr)
110 {-# NOINLINE trace #-}
111 trace :: String -> a -> a
112 trace string expr = unsafePerformIO $ do
113 fd <- getHandleFd stderr
114 hPutStrLn stderr string
115 _ccall_ PostTraceHook fd
121 unsafeIOToST :: IO a -> ST s a
122 unsafeIOToST (IO io) = ST $ \ s ->
123 case ((unsafeCoerce# io) s) of
124 IOok new_s a -> unsafeCoerce# (STret new_s a)
125 IOfail new_s e -> error ("I/O Error (unsafeIOToST): " ++ showsPrec 0 e "\n")
128 Not something you want to call normally, but useful
129 in the cases where you do want to flush stuff out of
130 the heap or make sure you've got room enough
134 performGC = _ccall_GC_ StgPerformGarbageCollection