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
56 , freeHaskellFunctionPtr
69 import PrelHandle ( openFileEx, IOModeEx(..),
70 hSetEcho, hGetEcho, getHandleFd
77 import IO ( hPutStr, hPutChar )
78 import PrelAddr ( Addr )
82 unsafePtrEq :: a -> a -> Bool
85 unsafePtrEq = primReallyUnsafePtrEquality
88 case reallyUnsafePtrEquality# a b of
95 newIORef :: a -> IO (IORef a)
96 readIORef :: IORef a -> IO a
97 writeIORef :: IORef a -> a -> IO ()
100 type IORef a = STRef RealWorld a
102 readIORef = readSTRef
103 writeIORef = writeSTRef
105 newtype IORef a = IORef (MutableVar RealWorld a)
108 newIORef v = stToIO (newVar v) >>= \ var -> return (IORef var)
109 readIORef (IORef var) = stToIO (readVar var)
110 writeIORef (IORef var) v = stToIO (writeVar var v)
115 newIOArray :: Ix ix => (ix,ix) -> elt -> IO (IOArray ix elt)
116 boundsIOArray :: Ix ix => IOArray ix elt -> (ix, ix)
117 readIOArray :: Ix ix => IOArray ix elt -> ix -> IO elt
118 writeIOArray :: Ix ix => IOArray ix elt -> ix -> elt -> IO ()
119 freezeIOArray :: Ix ix => IOArray ix elt -> IO (Array ix elt)
120 thawIOArray :: Ix ix => Array ix elt -> IO (IOArray ix elt)
122 unsafeFreezeIOArray :: Ix ix => IOArray ix elt -> IO (Array ix elt)
123 unsafeThawIOArray :: Ix ix => Array ix elt -> IO (IOArray ix elt)
127 type IOArray ix elt = STArray RealWorld ix elt
128 newIOArray = newSTArray
129 boundsIOArray = boundsSTArray
130 readIOArray = readSTArray
131 writeIOArray = writeSTArray
132 freezeIOArray = freezeSTArray
133 thawIOArray = thawSTArray
135 newtype IOArray ix elt = IOArray (MutableArray RealWorld ix elt)
139 stToIO (newArray ixs elt) >>= \arr ->
142 boundsIOArray (IOArray arr) = boundsOfArray arr
144 readIOArray (IOArray arr) ix = stToIO (readArray arr ix)
146 writeIOArray (IOArray arr) ix elt = stToIO (writeArray arr ix elt)
148 freezeIOArray (IOArray arr) = stToIO (freezeArray arr)
151 marr <- stToIO (thawArray arr)
152 return (IOArray marr)
154 unsafeFreezeIOArray (IOArray arr) = stToIO (unsafeFreezeArray arr)
155 unsafeThawIOArray arr = do
156 marr <- stToIO (unsafeThawArray arr)
157 return (IOArray marr)
162 {-# NOINLINE trace #-}
163 trace :: String -> a -> a
165 trace string expr = unsafePerformIO $ do
169 trace string expr = unsafePerformIO $ do
170 fd <- getHandleFd stderr
171 hPutStr stderr string
173 _ccall_ PostTraceHook fd
178 Not something you want to call normally, but useful
179 in the cases where you do want to flush stuff out of
180 the heap or make sure you've got room enough
186 performGC = _ccall_GC_ performGC
190 When using 'foreign export dynamic' to dress up a Haskell
191 IO action to look like a C function pointer, a little bit
192 of memory is allocated (along with a stable pointer to
193 the Haskell IO action). When done with the C function
194 pointer, you'll need to call @freeHaskellFunctionPtr()@ to
195 let go of these resources - here's the Haskell wrapper for
196 that RTS entry point, should you want to free it from
200 foreign import ccall "freeHaskellFunctionPtr"
201 freeHaskellFunctionPtr :: Addr -> IO ()