title: Defining Monad of nested monads
body: <p>I am trying to define an instance of a monad for a type, which is defined as follows:</p>
  
  <pre><code>type Error a = Either String a
  data Outer a = Outer { fromOuter :: Error (Maybe a) } 
  </code></pre>
  
  <p><em>Functors</em> and <em>Applicative</em> instances for the <strong>Outer</strong> type can be defined as:</p>
  
  <pre><code>instance Functor Outer where
      f `fmap` (Outer fa) = Outer $ fmap (fmap f) $ fa 
  
  
  instance Applicative Outer where
       pure = Outer . pure . pure
       (Outer l) &lt;*&gt; (Outer r) = Outer $ do
                               el &lt;- l
                               er &lt;- r
                               return $ el &lt;*&gt; er
  </code></pre>
  
  <p>However, I am struggling to define <strong>bind</strong> for defining <em>Monad</em> instance for this type:</p>
  
  <pre><code>instance Monad Outer where
      return = pure
      (Outer l) &gt;&gt;= f = ??????
  </code></pre>
  
  <p>Further question is:</p>
  
  <p>How the above can be generalised to, say,</p>
  
  <pre><code>data Outer m n a = Outer { fromOuter :: m (n a) }
  </code></pre>
  
  <p><em>This problem is an abstraction of a issue in <a href="https://hackage.haskell.org/package/hedis-simple-0.1.0.0" rel="nofollow">hedis-simple</a> where I am trying to define Monad instance for <a href="https://hackage.haskell.org/package/hedis-simple-0.1.0.0/docs/Database-Redis-Simple.html#t:RedisTx" rel="nofollow">RedisTx</a></em></p>