title: Relax ordering constraints in monadic computation
body: <p>here is some food for thought.</p>
  
  <p>When I write monadic code, the monad imposes ordering on the operations done. For example, If I write in the IO monad:</p>
  
  <pre><code>do a &lt;- doSomething
     b &lt;- doSomethingElse
     return (a + b)
  </code></pre>
  
  <p>I know <code>doSomething</code> will be executed before <code>doSomethingElse</code>.</p>
  
  <p>Now, consider the equivalent code in a language like C:</p>
  
  <pre><code>return (doSomething() + doSomethingElse());
  </code></pre>
  
  <p>The semantics of C don't actually specify what order these two function calls will be evaluated, so the compiler is free to move things around as it pleases.</p>
  
  <p>My question is this: <strong>How would I write monadic code in Haskell that also leaves this evaluation order undefined?</strong> Ideally, I would reap some benefits when my compiler's optimizer looks at the code and starts moving things around.</p>
  
  <p>Here are some possible techniques that don't get the job done, but are in the right "spirit":</p>
  
  <ul>
  <li>Write the code in <em>functorial style</em>, that is, write <code>plus doSomething doSomethingElse</code> and let <code>plus</code> schedule the monadic calls. Drawbacks: You lose sharing on the results of the monadic actions, and <code>plus</code> still makes a decision about when things end up being evaluated.</li>
  <li>Use <em>lazy IO</em>, that is, <code>unsafeInterleaveIO</code>, which defers the scheduling to the demands lazy of evaluation. But lazy is different from strict with undefined order: in particular I do want all of my monadic actions to get executed.</li>
  <li>Lazy IO, combined with immediately seq'ing all of the arguments. In particular, <code>seq</code> does not impose ordering constraints.</li>
  </ul>
  
  <p>In this sense, I want something more flexible than monadic ordering but less flexible than full-out laziness.</p>