Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It would be very difficult to use do notation without <code>return</code>. Consider the common pattern of:</p> <pre><code>foo = do x &lt;- bar y &lt;- quux return $ x + y </code></pre> <p>Without return it is impossible to define this. Recall that <code>&gt;&gt;=</code> has type <code>(&gt;&gt;=) :: Monad m =&gt; m a -&gt; (a -&gt; m b)</code> - it must return a value inside the monad. So even if we write this out without do notation, we have the same problem:</p> <pre><code>foo = bar &gt;&gt;= \x -&gt; quux &gt;&gt;= \y -&gt; (???????) (x + y) </code></pre> <p>So it's not a good idea to use do-notation for this. Particularly, there's nothing to stop the compiler implementation from introducing <code>return</code>s with transformations conforming to the monad laws (which assume the presence of <code>return</code>).</p> <p>Moreover, even defining >>= will be difficult for you. How will you translate it to your imperative language? Remember that >>= takes an arbitrary function on its right argument; you are unable to inspect this function to tell what it might do, and thus cannot translate the body of this function into an imperative language. I suppose you could constrain the type of the argument to some kind of traced datatype and try to figure out the <em>effects</em> of the function, but now this means the function can't inspect its argument. In short, it's just not going to work very well.</p> <p>One alternate approach you might consider is to create a Monad that represents the process of building an imperative function. It might, for example, look like this:</p> <pre><code>emitAddAndPrint varA varB = do varTmp &lt;- allocateTempVariable emitOp (Add varTmp varA varB) -- if you want to be fancy, emitOp (varTmp :=: varA :+: varB) or something emitCall "print" [varTmp] </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