title: Overriding class instance with new members
body: <p>Say I have a simple class <code>AClass</code> with a public member <code>f1</code> which can be overridden. What are the ways to define a new instance of <code>AClass</code> with another member <code>f2</code>, short of duplicating the source code of <code>AClass</code>? Toy code below:</p>
  
  <pre><code>class AClass a where
      f1 :: a -&gt; Int
  
  data Val = I Int
  
  instance AClass Val where
    f1 x = 0
  
    -- the method below can't be added as it is not public member of AClass
    -- f2:: a -&gt; Float
    -- f2 x = 0.0 
  </code></pre>
  
  <p>I looked around but I didn't find any clear examples on how to do it (i.e., examples I could understand well - clarity is relative). What are the possible ways? Closure, newtype declaration or something else? It will be helpful to demonstrate the technique with above toy code - you can change <code>data</code> declaration etc. (e.g., replace it with a <code>newtype</code> wrapper around <code>Int</code>) but the only thing immutable in above code is class declaration of <code>AClass</code>. This is because the assumption is the class has already been written by a library writer, and so, I can't touch it. The end result should be another toy code which inherits the goodies of <code>AClass</code>, and adds <code>f2</code> member.</p>
  
  <p>There will be caveats of course, in overriding classes like this. But, it helps to know what is possible, and how.</p>
  
  <p><strong>-- Update --</strong> </p>
  
  <p>Working code below - Credit to Ben and mergeconflict for coming up with the solution - there were few missing pieces - filled in below:</p>
  
  <pre><code>class AClass a where
      f1 :: a -&gt; Int
  
  class (AClass a) =&gt; BClass a where
      f2 :: a -&gt; Float
  
  data Val = I Int
  
  instance AClass Val where
     f1 _ = 0
  
  instance BClass Val where
     f2 _ = 0.0                       
  </code></pre>