<p>Simple answer: When the above class definition is in scope, you simply cannot bind (&lt;) or (&lt;=) except as part of an instance declaration for Ord.</p>

CC BY-SA 3.0
2012-03-06T12:44:40.513Z
Thank you. I see that my lacking experience with Haskell is confusing me. Could you please clarify exactly what it means for a type class to be in scope?
Eyvind
CC BY-SA 3.0
2012-03-06T14:02:48.013Z
A class is in scope when it is defined in the current module (Haskell only allows classes to be defined on the module level). A class or its methods can also be imported from another module, and somewhat more complicated rules apply if the import is not unqualified, or omits some methods. Not sure I remember the exact details there, since that part of Haskell is rather ad-hoc.
Andreas Rossberg
CC BY-SA 3.0
2012-03-06T11:54:31.980Z
2012-03-06T11:54:31.980Z
Andreas Rossberg
<p>[This is the same answer as Andreas Rossberg's, a bit more elaborated.]</p>

<p>Note that the <em>only</em> way you can overload a function symbol in Haskell is via type classes.  So, if I want to define &lt; for some type, say (Int, Int), then the only way to do so is by saying that (Int, Int) is an instance of Ord.  But, in order to do that, (Int, Int) must first be an instance of Eq.  So, I have to make (Int, Int) an instance of Eq, define its == operation, then make it an instance of Ord and define its &lt; and &lt;= operations.</p>

<p>[You might also see the related question <a href="https://cstheory.stackexchange.com/questions/9731/type-classes-vs-object-interfaces">Type classes vs object interfaces</a> where similar issues are mentioned.]</p>

CC BY-SA 3.0
2012-03-06T19:17:16.123Z
2012-03-09T09:11:11.120Z
2017-04-13T12:32:28.053Z
Uday Reddy
<p>I'm reading up on type classes, and started looking at the paper <a href="http://dl.acm.org/citation.cfm?id=227700" rel="nofollow">Type Classes in Haskell</a>.</p>

<p>In Section 2.2 - Superclasses, the authors use the following example:</p>

<pre><code>class (Eq a) =&gt; Ord a where 
   (&lt;)  :: a -&gt; a -&gt; Bool
   (&lt;=) :: a -&gt; a -&gt; Bool
</code></pre>

<p>Then, they proceed to state that "<em>This declares that type a belongs to class Ord if there are operations (&lt;) and (&lt;=) of the appropriate type and if a belongs to class Eq. Thus, if (&lt;) is defined on some type, then (==) must be defined on that type as well.</em>"</p>

<p>The second sentence does not make any sense to me; why would a type that defines (&lt;) have to define (==) if it is not declared to be an instance of either Ord or Eq?</p>

<pl.programming-languages><type-theory>
Confusing (to me) statement from "Type Classes in Haskell"
Eyvind
# Confusing (to me) statement from "Type Classes in Haskell"

(Asked by: username_0 on 2012-03-06)

I'm reading up on type classes, and started looking at the paper Type Classes in Haskell.
In Section 2.2 - Superclasses, the authors use the following example:
class (Eq a) => Ord a where 
   (<)  :: a -> a -> Bool
   (<=) :: a -> a -> Bool

Then, they proceed to state that "This declares that type a belongs to class Ord if there are operations (<) and (<=) of the appropriate type and if a belongs to class Eq. Thus, if (<) is defined on some type, then (==) must be defined on that type as well."
The second sentence does not make any sense to me; why would a type that defines (<) have to define (==) if it is not declared to be an instance of either Ord or Eq?

**Tags:** pl.programming-languages, type-theory


---

## 2 answers

### Answer by username_1 on 2012-03-06. Score: 4 | Accepted answer

Simple answer: When the above class definition is in scope, you simply cannot bind (<) or (<=) except as part of an instance declaration for Ord.

> **Comments:**
> **username_0**: Thank you. I see that my lacking experience with Haskell is confusing me. Could you please clarify exactly what it means for a type class to be in scope?  
> **username_1**: A class is in scope when it is defined in the current module (Haskell only allows classes to be defined on the module level). A class or its methods can also be imported from another module, and somewhat more complicated rules apply if the import is not unqualified, or omits some methods. Not sure I remember the exact details there, since that part of Haskell is rather ad-hoc.  

---

### Answer by username_2 on 2012-03-06. Score: 4

[This is the same answer as username_1's, a bit more elaborated.]

Note that the *only* way you can overload a function symbol in Haskell is via type classes. So, if I want to define < for some type, say (Int, Int), then the only way to do so is by saying that (Int, Int) is an instance of Ord. But, in order to do that, (Int, Int) must first be an instance of Eq. So, I have to make (Int, Int) an instance of Eq, define its == operation, then make it an instance of Ord and define its < and <= operations.

[You might also see the related question [Type classes vs object interfaces](https://cstheory.stackexchange.com/questions/9731/type-classes-vs-object-interfaces) where similar issues are mentioned.]


---

