1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
<HTML>
<HEAD>
<TITLE>The Hugs-GHC Extension Libraries: IOExts </TITLE>
</HEAD>
<BODY>
<A HREF="libs-2.html">Previous</A>
<A HREF="libs-4.html">Next</A>
<A HREF="libs.html#toc3">Table of Contents</A>
<HR>
<H2><A NAME="s3">3. IOExts </A></H2>
<P>This library provides the following extensions to the IO monad:
<UL>
<LI>The operations <CODE>fixIO</CODE>, <CODE>unsafePerformIO</CODE> and <CODE>unsafeInterleaveIO</CODE>
described in </I>]
</LI>
<LI>References (aka mutable variables) and mutable arrays (but no form of
mutable byte arrays)
</LI>
<LI><CODE>performGC</CODE> triggers an immediate garbage collection
</LI>
<LI>When called, <CODE>trace</CODE> prints the string in its first argument, and then
returns the second argument as its result. The <CODE>trace</CODE> function is not
referentially transparent, and should only be used for debugging, or for
monitoring execution.
</LI>
<LI><CODE>unsafePtrEq</CODE> compares two values for pointer equality without
evaluating them. The results are not referentially transparent and
may vary significantly from one compiler to another or in the face of
semantics-preserving program changes. However, pointer equality is useful
in creating a number of referentially transparent constructs such as this
simplified memoisation function:
<BLOCKQUOTE><CODE>
<PRE>
> cache :: (a -> b) -> (a -> b)
> cache f = \x -> unsafePerformIO (check x)
> where
> ref = unsafePerformIO (newIORef (error "cache", error "cache"))
> check x = readIORef ref >>= \ (x',a) ->
> if x `unsafePtrEq` x' then
> return a
> else
> let a = f x in
> writeIORef ref (x, a) >>
> return a
</PRE>
</CODE></BLOCKQUOTE>
</LI>
</UL>
</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
module IOExts where
fixIO :: (a -> IO a) -> IO a
unsafePerformIO :: IO a -> a
unsafeInterleaveIO :: IO a -> IO a
data IORef a -- mutable variables containing values of type a
newIORef :: a -> IO (IORef a)
readIORef :: IORef a -> IO a
writeIORef :: IORef a -> a -> IO ()
instance Eq (IORef a)
data IOArray ix elt -- mutable arrays indexed by values of type ix
-- containing values of type a.
newIOArray :: Ix ix => (ix,ix) -> elt -> IO (IOArray ix elt)
boundsIOArray :: Ix ix => IOArray ix elt -> (ix, ix)
readIOArray :: Ix ix => IOArray ix elt -> ix -> IO elt
writeIOArray :: Ix ix => IOArray ix elt -> ix -> elt -> IO ()
freezeIOArray :: Ix ix => IOArray ix elt -> IO (Array ix elt)
instance Eq (IOArray ix elt)
performGC :: IO ()
trace :: String -> a -> a
unsafePtrEq :: a -> a -> Bool
</PRE>
</CODE></BLOCKQUOTE>
</P>
<HR>
<A HREF="libs-2.html">Previous</A>
<A HREF="libs-4.html">Next</A>
<A HREF="libs.html#toc3">Table of Contents</A>
</BODY>
</HTML>
|