title: What does fixIO do?
body: <p>The <a href="https://hackage.haskell.org/package/base-4.5.0.0/docs/System-IO.html">System.IO docs</a> contains a mysterious, undocumented function <code>fixIO</code>. <a href="https://hackage.haskell.org/package/base-4.5.0.0/docs/src/System-IO.html#fixIO">Its source</a> only adds to the mystery:</p>
  
  <pre><code>fixIO :: (a -&gt; IO a) -&gt; IO a
  fixIO k = do
      m &lt;- newEmptyMVar
      ans &lt;- unsafeInterleaveIO (takeMVar m)
      result &lt;- k ans
      putMVar m result
      return result
  </code></pre>
  
  <p>This appears to do the moral equivalent of dereferencing NULL (reading from an empty MVar). Indeed, trying it:</p>
  
  <pre><code>import System.IO
  main = fixIO $ \x -&gt; putStrLn x &gt;&gt; return x
  </code></pre>
  
  <p>results in an error "thread blocked indefinitely in an MVar operation"</p>
  
  <p>Searching turns up nothing save a <a href="http://www.haskell.org/pipermail/haskell/2001-February/006720.html">15 year old message</a> from Simon Peyton-Jones himself, in which he provides the above source, and hopes that it would make the meaning clear (and yet here I am).</p>
  
  <p>Can someone please shed some light on this? What does fixIO do and when should I use it?</p>