Sophie

Sophie

distrib > Mageia > 5 > i586 > media > core-release > by-pkgid > 6e204a966e8c42d976f99a1700ce5f20 > files > 2325

ghc-7.4.2-4.mga5.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>System.IO.Unsafe</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[
window.onload = function () {pageLoad();setSynopsis("mini_System-IO-Unsafe.html");};
//]]>
</script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">base-4.5.1.0: Basic libraries</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Portability</th><td>portable</td></tr><tr><th>Stability</th><td>provisional</td></tr><tr><th>Maintainer</th><td>libraries@haskell.org</td></tr><tr><th>Safe Haskell</th><td>Unsafe</td></tr></table><p class="caption">System.IO.Unsafe</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">Unsafe <code><a href="System-IO.html#t:IO">IO</a></code> operations
</a></li></ul></div><div id="description"><p class="caption">Description</p><div class="doc"><p>&quot;Unsafe&quot; IO operations.
</p></div></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><a href="#v:unsafePerformIO">unsafePerformIO</a> ::  <a href="System-IO.html#t:IO">IO</a> a -&gt; a</li><li class="src short"><a href="#v:unsafeDupablePerformIO">unsafeDupablePerformIO</a> ::  <a href="System-IO.html#t:IO">IO</a> a -&gt; a</li><li class="src short"><a href="#v:unsafeInterleaveIO">unsafeInterleaveIO</a> ::  <a href="System-IO.html#t:IO">IO</a> a -&gt; <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:unsafeFixIO">unsafeFixIO</a> ::  (a -&gt; <a href="System-IO.html#t:IO">IO</a> a) -&gt; <a href="System-IO.html#t:IO">IO</a> a</li></ul></div><div id="interface"><h1 id="g:1">Unsafe <code><a href="System-IO.html#t:IO">IO</a></code> operations
</h1><div class="top"><p class="src"><a name="v:unsafePerformIO" class="def">unsafePerformIO</a> ::  <a href="System-IO.html#t:IO">IO</a> a -&gt; a</p><div class="doc"><p>This is the &quot;back door&quot; into the <code><a href="System-IO.html#t:IO">IO</a></code> monad, allowing
<code><a href="System-IO.html#t:IO">IO</a></code> computation to be performed at any time.  For
this to be safe, the <code><a href="System-IO.html#t:IO">IO</a></code> computation should be
free of side effects and independent of its environment.
</p><p>If the I/O computation wrapped in <code><a href="System-IO-Unsafe.html#v:unsafePerformIO">unsafePerformIO</a></code> performs side
effects, then the relative order in which those side effects take
place (relative to the main I/O trunk, or other calls to
<code><a href="System-IO-Unsafe.html#v:unsafePerformIO">unsafePerformIO</a></code>) is indeterminate.  Furthermore, when using
<code><a href="System-IO-Unsafe.html#v:unsafePerformIO">unsafePerformIO</a></code> to cause side-effects, you should take the following
precautions to ensure the side effects are performed as many times as
you expect them to be.  Note that these precautions are necessary for
GHC, but may not be sufficient, and other compilers may require
different precautions:
</p><ul><li> Use <code>{-# NOINLINE foo #-}</code> as a pragma on any function <code>foo</code>
        that calls <code><a href="System-IO-Unsafe.html#v:unsafePerformIO">unsafePerformIO</a></code>.  If the call is inlined,
        the I/O may be performed more than once.
</li><li> Use the compiler flag <code>-fno-cse</code> to prevent common sub-expression
        elimination being performed on the module, which might combine
        two side effects that were meant to be separate.  A good example
        is using multiple global variables (like <code>test</code> in the example below).
</li><li> Make sure that the either you switch off let-floating (<code>-fno-full-laziness</code>), or that the 
        call to <code><a href="System-IO-Unsafe.html#v:unsafePerformIO">unsafePerformIO</a></code> cannot float outside a lambda.  For example, 
        if you say:
        <code>
           f x = unsafePerformIO (newIORef [])
        </code>
        you may get only one reference cell shared between all calls to <code>f</code>.
        Better would be
        <code>
           f x = unsafePerformIO (newIORef [x])
        </code>
        because now it can't float outside the lambda.
</li></ul><p>It is less well known that
<code><a href="System-IO-Unsafe.html#v:unsafePerformIO">unsafePerformIO</a></code> is not type safe.  For example:
</p><pre>     test :: IORef [a]
     test = unsafePerformIO $ newIORef []
     
     main = do
             writeIORef test [42]
             bang &lt;- readIORef test
             print (bang :: [Char])
</pre><p>This program will core dump.  This problem with polymorphic references
is well known in the ML community, and does not arise with normal
monadic use of references.  There is no easy way to make it impossible
once you use <code><a href="System-IO-Unsafe.html#v:unsafePerformIO">unsafePerformIO</a></code>.  Indeed, it is
possible to write <code>coerce :: a -&gt; b</code> with the
help of <code><a href="System-IO-Unsafe.html#v:unsafePerformIO">unsafePerformIO</a></code>.  So be careful!
</p></div></div><div class="top"><p class="src"><a name="v:unsafeDupablePerformIO" class="def">unsafeDupablePerformIO</a> ::  <a href="System-IO.html#t:IO">IO</a> a -&gt; a</p><div class="doc"><p>This version of <code><a href="System-IO-Unsafe.html#v:unsafePerformIO">unsafePerformIO</a></code> is more efficient
because it omits the check that the IO is only being performed by a
single thread.  Hence, when you use <code><a href="System-IO-Unsafe.html#v:unsafeDupablePerformIO">unsafeDupablePerformIO</a></code>,
there is a possibility that the IO action may be performed multiple
times (on a multiprocessor), and you should therefore ensure that
it gives the same results each time.
</p></div></div><div class="top"><p class="src"><a name="v:unsafeInterleaveIO" class="def">unsafeInterleaveIO</a> ::  <a href="System-IO.html#t:IO">IO</a> a -&gt; <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p><code><a href="System-IO-Unsafe.html#v:unsafeInterleaveIO">unsafeInterleaveIO</a></code> allows <code><a href="System-IO.html#t:IO">IO</a></code> computation to be deferred lazily.
When passed a value of type <code>IO a</code>, the <code><a href="System-IO.html#t:IO">IO</a></code> will only be performed
when the value of the <code>a</code> is demanded.  This is used to implement lazy
file reading, see <code><a href="System-IO.html#v:hGetContents">hGetContents</a></code>.
</p></div></div><div class="top"><p class="src"><a name="v:unsafeFixIO" class="def">unsafeFixIO</a> ::  (a -&gt; <a href="System-IO.html#t:IO">IO</a> a) -&gt; <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>A slightly faster version of <code><a href="System-IO.html#v:fixIO">fixIO</a></code> that may not be
 safe to use with multiple threads.  The unsafety arises when used
 like this:
</p><pre>  unsafeFixIO $ \r -&gt;
     forkIO (print r)
     return (...)
</pre><p>In this case, the child thread will receive a <code>NonTermination</code>
 exception instead of waiting for the value of <code>r</code> to be computed.
</p></div></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.11.0</p></div></body></html>