title: IO monad prevents short circuiting of embedded mapM?
body: <p>Somewhat mystified by the following code. In non-toy version of the problem I'm trying to do a monadic computation in a monad Result, the values of which can only be constructed from within IO. Seems like the magic behind IO makes such computations strict, but I can't figure out how exactly that happens.</p>
  
  <p>The code:</p>
  
  <pre><code>data Result a = Result a | Failure deriving (Show)
  
  instance Functor Result where
    fmap f (Result a) = Result (f a)
    fmap f Failure = Failure
  
  instance Applicative Result where
    pure = return
    (&lt;*&gt;) = ap
  
  instance Monad Result where
    return = Result
    Result a &gt;&gt;= f = f a
    Failure &gt;&gt;= _ = Failure
  
  compute :: Int -&gt; Result Int
  compute 3 = Failure
  compute x = traceShow x $ Result x
  
  compute2 :: Monad m =&gt; Int -&gt; m (Result Int)
  compute2 3 = return Failure
  compute2 x = traceShow x $ return $ Result x
  
  compute3 :: Monad m =&gt; Int -&gt; m (Result Int)
  compute3 = return . compute
  
  main :: IO ()
  main = do
    let results = mapM compute [1..5]
    print $ results
    results2 &lt;- mapM compute2 [1..5]
    print $ sequence results2
    results3 &lt;- mapM compute3 [1..5]
    print $ sequence results3
    let results2' = runIdentity $ mapM compute2 [1..5]
    print $ sequence results2'
  </code></pre>
  
  <p>The output:</p>
  
  <pre><code>1
  2
  Failure
  1
  2
  4
  5
  Failure
  1
  2
  Failure
  1
  2
  Failure
  </code></pre>