Typeclasses and overloading, what is the connection? : <p>I am currently trying to wrap my head around typeclasses and instances and I don't quite understand the point of them yet. I have two questions on the matter so far:</p>

<p>1) Why is it necessary to have a type class in a function signature when the function uses some function from that type class. Example:</p>

<pre><code>f :: (Eq a) =&gt; a -&gt; a -&gt; Bool
f a b = a == b
</code></pre>

<p>Why put <code>(Eq a)</code> in the signature. If <code>==</code> is not defined for <code>a</code> then why not just throw the error when encountering <code>a == b</code>? What is the point in having to declare the type class ahead?</p>

<p>2) How are type classes and function overloading related?</p>

<p>It is not possible to do this:</p>

<pre><code>data A = A
data B = B

f :: A -&gt; A
f a = a

f :: B -&gt; B
f b = b
</code></pre>

<p>But it is possible to do this:</p>

<pre><code>data A = A
data B = B

class F a where
  f :: a -&gt; a

instance F A where
  f a = a

instance F B where
  f b = b
</code></pre>

<p>What is up with that? Why can't I have two functions with the same name but operating on different types... Coming from C++ I find that very strange. But I probably have wrong conceptions about what these things really are.  but once I wrap them in these type class instance thingies I can.</p>

<p>Feel free to hurl category or type theoretical words at me as well, as I am learning about these subjects in parallel to learning Haskell and I suspect there is a theoretical basis in these for how Haskell does things here.</p>
