title: How does deriving work in Haskell?
body: <p><em><strong>Algebraic</strong> Data Types</em> (ADTs) in Haskell can automatically become instances of some <em>typeclasse</em>s (like <code>Show</code>, <code>Eq</code>) by <em>deriving</em> from them.</p>
  
  <pre><code>data  Maybe a  =  Nothing | Just a
    deriving (Eq, Ord)
  </code></pre>
  
  <p>My question is, how does this <code>deriving</code> work, i.e. how does Haskell know how to implement the functions of the derived typeclass for the deriving ADT? </p>
  
  <p>Also, why is <code>deriving</code> restricted to certain typeclasses only? Why can't I write my own typeclass which can be derived?</p>