<!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>Foreign</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_Foreign.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.3.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>ffi@haskell.org</td></tr></table><p class="caption">Foreign</p></div><div id="description"><p class="caption">Description</p><div class="doc"><p>A collection of data types, classes, and functions for interfacing with another programming language. </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">module <a href="Data-Bits.html">Data.Bits</a></li><li class="src short">module <a href="Data-Int.html">Data.Int</a></li><li class="src short">module <a href="Data-Word.html">Data.Word</a></li><li class="src short">module <a href="Foreign-Ptr.html">Foreign.Ptr</a></li><li class="src short">module <a href="Foreign-ForeignPtr.html">Foreign.ForeignPtr</a></li><li class="src short">module <a href="Foreign-StablePtr.html">Foreign.StablePtr</a></li><li class="src short">module <a href="Foreign-Storable.html">Foreign.Storable</a></li><li class="src short">module <a href="Foreign-Marshal.html">Foreign.Marshal</a></li><li class="src short"><a href="#v:unsafePerformIO">unsafePerformIO</a> :: <a href="System-IO.html#t:IO">IO</a> a -> a</li></ul></div><div id="interface"><h1>Documentation</h1><div class="top"><p class="src">module <a href="Data-Bits.html">Data.Bits</a></p></div><div class="top"><p class="src">module <a href="Data-Int.html">Data.Int</a></p></div><div class="top"><p class="src">module <a href="Data-Word.html">Data.Word</a></p></div><div class="top"><p class="src">module <a href="Foreign-Ptr.html">Foreign.Ptr</a></p></div><div class="top"><p class="src">module <a href="Foreign-ForeignPtr.html">Foreign.ForeignPtr</a></p></div><div class="top"><p class="src">module <a href="Foreign-StablePtr.html">Foreign.StablePtr</a></p></div><div class="top"><p class="src">module <a href="Foreign-Storable.html">Foreign.Storable</a></p></div><div class="top"><p class="src">module <a href="Foreign-Marshal.html">Foreign.Marshal</a></p></div><div class="doc"><p><code><a href="Foreign.html#v:unsafePerformIO">unsafePerformIO</a></code> is exported here for backwards compatibility reasons only. For doing local marshalling in the FFI, use <code><a href="Foreign-Marshal.html#v:unsafeLocalState">unsafeLocalState</a></code>. For other uses, see <code><a href="Foreign.html#v:unsafePerformIO">unsafePerformIO</a></code>. </p></div><div class="top"><p class="src"><a name="v:unsafePerformIO" class="def">unsafePerformIO</a> :: <a href="System-IO.html#t:IO">IO</a> a -> a</p><div class="doc"><p>This is the "back door" 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="Foreign.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="Foreign.html#v:unsafePerformIO">unsafePerformIO</a></code>) is indeterminate. Furthermore, when using <code><a href="Foreign.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="Foreign.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="Foreign.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="Foreign.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 <- 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="Foreign.html#v:unsafePerformIO">unsafePerformIO</a></code>. Indeed, it is possible to write <code>coerce :: a -> b</code> with the help of <code><a href="Foreign.html#v:unsafePerformIO">unsafePerformIO</a></code>. So be careful! </p></div></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.9.2</p></div></body></html>