title: What is the previous value of a monadic computation in the function context?
body: <p>I am struggeling with the function instance of monads (who does not?). Monads may depend on the value of the previous monadic computation. What does this exactly mean for the function monad?</p>
  
  <p>When we look at the bare implementations of applicative/monad instances</p>
  
  <pre><code>ap   f g x = f x (g x)
  bind f g x = f (g x) x
  </code></pre>
  
  <p>it seems as if the result of <code>g x</code> constitutes this previous value. However, I recently stumbled upon the following exmaple:</p>
  
  <pre><code>mainA = (\x y z -&gt; (x,y,z)) &lt;$&gt; (2/) &lt;*&gt; (+1) &lt;*&gt; (\x -&gt; x * x)
  mainM = (\x -&gt; (\y -&gt; (\z w -&gt; if w == 0 then (0,y,z) else (x,y,z)) =&lt;&lt; (\x -&gt; x * x)) =&lt;&lt; (+1)) =&lt;&lt; (2/)
  mainA 0 -- (Infinity,1.0,0.0)
  mainM 0 -- (0.0,1.0,0.0)
  </code></pre>
  
  <p>Since a monadic action has to return a monad we have this additional lambda <code>w -&gt; ...</code> within <code>mainM</code>'s definition. This gives us access to the initial value that is passed to the overall computation. Given <code>bind f g x = f (g x) x</code>, does this mean the previous value for the function instance is <code>x</code> not the result of <code>g x</code>?</p>
  
  <p>Sorry for the confusion!</p>