title: Monad Transformer: troubles defining bind due to different monads
body: <p>I have a Monad of named <code>TaskMonad</code>, defined as follows:</p>
  
  <pre><code>data TaskMonad a = TaskMonad (Environment -&gt; (TaskResult a, Environment))
  </code></pre>
  
  <p>where <code>Environment</code> is a record type and <code>TaskResult</code> is an ADT; but they are not important for the problem.</p>
  
  <p>I have defed <code>Functor</code>, <code>Applicative</code> and <code>Monad</code> instances for <code>TaskMonad</code>, and I now want to be able to combine this monad with other Monads (e.g. <code>IO</code>), So I defined a new type as follows:</p>
  
  <pre><code>newtype Task m a = Task { runTask :: m (TaskMonad a) }
  </code></pre>
  
  <p>I have defined <code>Functor</code> and <code>Applicative</code> as follows:</p>
  
  <pre><code>instance Monad m =&gt; Functor (Task m) where
      fmap f ta = Task $ do tma &lt;- runTask ta
                            return (fmap f tma)
  
  instance Monad m =&gt; Applicative (Task m) where
      pure = Task . return . return
      (&lt;*&gt;) prod tx = Task $ do tmprod &lt;- runTask prod
                                tmtx   &lt;- runTask tx
                                return (tmprod &lt;*&gt; tmtx)
  </code></pre>
  
  <p>And I also made <code>Task</code> member of the <code>MonadTrans</code> class:</p>
  
  <pre><code>instance MonadTrans Task where
      lift = Task . (liftM return)
  </code></pre>
  
  <p>So far so good (or atleast it compiles..), but now I want to define the instance for <code>Monad</code>, but I am running into problems here:</p>
  
  <pre><code>instance Monad m =&gt; Monad (Task m) where
      return      = pure
      (&gt;&gt;=) ta tb = ...
  </code></pre>
  
  <p>I attempted multiple things, most attempts starting out like this:</p>
  
  <pre><code>(&gt;&gt;=) ta tb = Task $ do tma &lt;- runTask ta 
  </code></pre>
  
  <p>Now we have <code>tma :: TaskMonad a</code> inside the <code>do</code> block for the <code>m</code> monad. Now what I would like to do, is somehow calling the <code>&gt;&gt;=</code> instance for <code>TaskMonad</code> so I can get the result of <code>tma</code>, a value of type <code>a</code> so I can parameterize <code>tb</code> with it to obtain a value of <code>Task b</code>. But I am within the context of the <code>m</code> monad and I'm running into all kinds of problems.</p>
  
  <p>How could I obtain <code>tma</code>'s result to provide it to <code>tb</code>?</p>