Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no specific implementation for <code>IO</code>; it's an abstract type, with the exact implementation left undefined by the Haskell Report. Indeed, there's nothing stopping an implementation implementing <code>IO</code> and its <code>Monad</code> instance as compiler primitives, with no Haskell implementation at all.</p> <p>Basically, <code>Monad</code> is used as an <em>interface</em> to <code>IO</code>, which cannot itself be implemented in pure Haskell. That's probably all you need to know at this stage, and diving into implementation details is likely to just confuse, rather than give insight.</p> <p>That said, if you look at GHC's source code, you'll find that it represents <code>IO a</code> as a function looking like <code>State# RealWorld -&gt; (# State# RealWorld, a #)</code> (using an <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/primitives.html">unboxed tuple</a> as the return type), but this is misleading; it's an implementation detail, and these <code>State# RealWorld</code> values do not actually exist at runtime. <code>IO</code> is <em>not</em> a state monad,<sup>1</sup> in theory or in practice.</p> <p>Instead, GHC uses <em>impure</em> primitives to implement these IO operations; the <code>State# RealWorld</code> "values" are only to stop the compiler reordering statements by introducing data dependencies from one statement to the next.</p> <p>But if you really want to see GHC's implementation of <code>return</code> and <code>(&gt;&gt;=)</code>, here they are:</p> <pre><code>returnIO :: a -&gt; IO a returnIO x = IO $ \ s -&gt; (# s, x #) bindIO :: IO a -&gt; (a -&gt; IO b) -&gt; IO b bindIO (IO m) k = IO $ \ s -&gt; case m s of (# new_s, a #) -&gt; unIO (k a) new_s </code></pre> <p>where <code>unIO</code> simply unwraps the function from inside the <code>IO</code> constructor.</p> <p>It's important to note that <code>IO a</code> represents a <em>description</em> of an impure computation that could be run to produce a value of type <code>a</code>. The fact that there's a way to get values out of GHC's internal representation of <code>IO</code> doesn't mean that this holds in general, or that you can do such a thing for all monads. It's purely an implementation detail on the part of GHC.</p> <p><sup>1</sup> The <a href="http://learnyouahaskell.com/for-a-few-monads-more#state">state monad</a> is a monad used for accessing and mutating a state across a series of computations; it's represented as <code>s -&gt; (a, s)</code> (where <code>s</code> is the type of state), which looks very similar to the type GHC uses for <code>IO</code>, thus the confusion.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload