title: class system extension proposal
body: <p>I'm solving some ploblem on generating ancestor instances in Haskell. Recently I found this on Haskell wiki: <a href="https://wiki.haskell.org/Class_system_extension_proposal" rel="nofollow">Class system extension proposal</a>. So, I would like to know, are there any solutions for this proposal already?</p>
  
  <p><em>Here are the examples from the Proposal:</em></p>
  
  <blockquote>
    <p>The current class system in Haskell is based on the idea that you can
    often provide default implementations for class methods at the same
    time as defining the class, by using other methods of the class or its
    ancestors. However consider the following hierarchy, adapted from
    Functor hierarchy proposal and The Other Prelude:</p>
  
  <pre><code>class Functor m where
      fmap :: (a -&gt; b) -&gt; m a -&gt; m b
  
  class Functor m =&gt; Applicative m where
      return :: a -&gt; m a
  
      apply :: m (a -&gt; b) -&gt; m a -&gt; m b
  
      (&gt;&gt;) :: m a -&gt; m b -&gt; m b
      ma &gt;&gt; mb = (fmap (const id) ma) `apply` mb
  
  class Applicative m =&gt; Monad m where
      (&gt;&gt;=) :: m a -&gt; (a -&gt; m b) -&gt; m b
  </code></pre>
    
    <p>For all concrete instances of Monad we can define fmap, apply, and
    (>>)in terms of return and (>>=) as follows:</p>
  
  <pre><code>fmap f ma = ma &gt;&gt;= (\a -&gt; return (f a))
  
  apply mf ma = mf &gt;&gt;= \f -&gt; ma &gt;&gt;= \a -&gt; return (f a)
  
  ma &gt;&gt; mb = ma &gt;&gt;= \_ -&gt; mb
  </code></pre>
    
    <p>In other words, we'd like to be able to write:</p>
  
  <pre><code>class Applicative m =&gt; Monad m where
      (&gt;&gt;=) :: m a -&gt; (a -&gt; m b) -&gt; m b
  
      fmap f ma = ma &gt;&gt;= (\a -&gt; return (f a))
  
      apply mf ma = mf &gt;&gt;= \f -&gt; ma &gt;&gt;= \a -&gt; return (f a)
  
      ma &gt;&gt; mb = ma &gt;&gt;= \_ -&gt; mb
  </code></pre>
    
    <p>and be able to define new instances of Monad just by supplying
    definitions for return and (>>=) by writing:</p>
  
  <pre><code>instance Monad T where
      ma &gt;&gt;= a_mb = ... -- some definition
  
      return a = ... -- some definition
  </code></pre>
    
    <p>Explicit import/export of instances</p>
    
    <p>This is needed so that large programs can be built without fear of
    colliding instance declarations between different packages. A possible
    syntax could be:</p>
  
  <pre><code>module M
       -- exported instances 
     ( instance Monad T
     , instance Functor (F a) hiding (Functor (F Int), Functor (F Char))
     , F(..)
     ) where
  
  import Foo (instance Monad a hiding Monad Maybe)
  
  data T a
  data F a b
  </code></pre>
    
    <p>where the context is elided because this isn't used in instance
    selection (at the moment). The import directive tells the compiler to
    use all Monad instances exported by Foo except for the Monad Maybe
    instance (it doesn't matter whether or not Foo actually does export a
    Monad Maybe instance - all that matters here is that we don't want it
    if there is one).</p>
  </blockquote>