title: Is there a way to implement constraints in Haskell's type classes?
body: <p>Is there some way (<em>any</em> way) to implement constraints in type classes?</p>
  
  <p>As an example of what I'm talking about, suppose I want to implement a Group as a type class. So a type would be a group if there are three functions:</p>
  
  <pre><code>class Group a where
      product :: a -&gt; a -&gt; a  
      inverse :: a -&gt; a 
      identity :: a
  </code></pre>
  
  <p>But those are not any functions, but they must be related by some constraints. For example:</p>
  
  <pre><code>product a identity = a 
  product a (inverse a) = identity
  inverse identity = identity
  </code></pre>
  
  <p>etc...</p>
  
  <p>Is there a way to enforce this kind of constraint in the definition of the class so that any instance would automatically inherit it? As an example, suppose I'd like to implement the C2 group, defined by:</p>
  
  <pre><code> data C2 = E | C 
  
   instance Group C2 where
        identity = E 
        inverse C = C
  </code></pre>
  
  <p>This two definitions uniquely determines C2 (the constraints above define all possible operations - in fact, C2 is the only possible group with two elements because of the constraints). Is there a way to make this work?</p>