Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You will be disappointed, but the <code>&gt;&gt;=</code> in <code>IO</code> monad isn't that interesting. To quote the GHC source:</p> <pre><code>{- | A value of type @'IO' a@ is a computation which, when performed, does some I\/O before returning a value of type @a@. There is really only one way to \"perform\" an I\/O action: bind it to @Main.main@ in your program. When your program is run, the I\/O will be performed. It isn't possible to perform I\/O from an arbitrary function, unless that function is itself in the 'IO' monad and called at some point, directly or indirectly, from @Main.main@. 'IO' is a monad, so 'IO' actions can be combined using either the do-notation or the '&gt;&gt;' and '&gt;&gt;=' operations from the 'Monad' class. -} newtype IO a = IO (State# RealWorld -&gt; (# State# RealWorld, a #)) </code></pre> <p>That means <code>IO</code> monad is declared as the instance of <strike><code>State</code></strike> <code>State#</code> monad, <strike>and its <code>&gt;&gt;=</code> is defined there (and its implementation is fairly easy to guess)</strike>.</p> <p>See <a href="http://www.haskell.org/haskellwiki/IO_inside#Welcome_to_the_RealWorld.2C_baby_:.29" rel="nofollow noreferrer">IO inside</a> article on Haskell wiki for more details about the <code>IO</code> monad. It is also helpful to look at the <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO" rel="nofollow noreferrer">Haskell docs</a>, where every position has small "Source" link on the right.</p> <p><strong>Update:</strong> And there goes another disappointment, which is my answer, because I didn't notice the '#' in <code>State#</code>. However <code>IO</code> behaves like <code>State</code> monad carrying abstract <code>RealWorld</code> state</p> <p>As @ehird wrote <code>State#</code> is compiler's internal and <code>&gt;&gt;=</code> for the <code>IO</code> monad is defined in <a href="http://hackage.haskell.org/package/base-4.7.0.2/docs/src/GHC-Base.html#bindIO" rel="nofollow noreferrer">GHC.Base</a> module:</p> <pre><code>instance Monad IO where {-# INLINE return #-} {-# INLINE (&gt;&gt;) #-} {-# INLINE (&gt;&gt;=) #-} m &gt;&gt; k = m &gt;&gt;= \ _ -&gt; k return = returnIO (&gt;&gt;=) = bindIO fail s = failIO s 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>
 

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