File: libs-3.html

package info (click to toggle)
hugs 1.4.199801-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 7,220 kB
  • ctags: 5,609
  • sloc: ansic: 32,083; haskell: 12,143; yacc: 949; perl: 823; sh: 602; makefile: 236
file content (93 lines) | stat: -rw-r--r-- 3,060 bytes parent folder | download
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>
&gt; cache :: (a -&gt; b) -&gt; (a -&gt; b)
&gt; cache f = \x -&gt; unsafePerformIO (check x)
&gt;  where
&gt;   ref = unsafePerformIO (newIORef (error &quot;cache&quot;, error &quot;cache&quot;))
&gt;   check x = readIORef ref &gt;&gt;= \ (x',a) -&gt;
&gt;              if x `unsafePtrEq` x' then
&gt;                return a
&gt;              else
&gt;                let a = f x in
&gt;                writeIORef ref (x, a) &gt;&gt;
&gt;                return a
</PRE>
</CODE></BLOCKQUOTE>


</LI>
</UL>
</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
module IOExts where

fixIO               :: (a -&gt; IO a) -&gt; IO a
unsafePerformIO     :: IO a -&gt; a
unsafeInterleaveIO  :: IO a -&gt; IO a
                    
data IORef a        -- mutable variables containing values of type a
newIORef            :: a -&gt; IO (IORef a)
readIORef           :: IORef a -&gt; IO a
writeIORef          :: IORef a -&gt; a -&gt; 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 =&gt; (ix,ix) -&gt; elt -&gt; IO (IOArray ix elt)
boundsIOArray       :: Ix ix =&gt; IOArray ix elt -&gt; (ix, ix)
readIOArray         :: Ix ix =&gt; IOArray ix elt -&gt; ix -&gt; IO elt
writeIOArray        :: Ix ix =&gt; IOArray ix elt -&gt; ix -&gt; elt -&gt; IO ()
freezeIOArray       :: Ix ix =&gt; IOArray ix elt -&gt; IO (Array ix elt)
instance Eq (IOArray ix elt)

performGC           :: IO ()
trace               :: String -&gt; a -&gt; a
unsafePtrEq         :: a -&gt; a -&gt; 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>