Skip to content

Commit

Permalink
[Chapter 6] Fix Eq/Ord relationship (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zelenya authored Nov 30, 2023
1 parent 44013c6 commit c584cf0
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions text/chapter6.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ true

The `Ord` type class defines the `compare` function, which can be used to compare two values, for types that support ordering. The comparison operators `<` and `>` along with their non-strict companions `<=` and `>=`, can be defined in terms of `compare`.

_Note_: In the example below, the class signature contains `<=`. This usage of `<=` in this context indicates that Eq is a subclass of Ord and is not intended to represent the use of `<=` as a comparison operator. See the section [Superclasses](#superclasses) below.
_Note_: In the example below, the class signature contains `<=`. This usage of `<=` in this context indicates that `Eq` is a superclass of `Ord` and is not intended to represent the use of `<=` as a comparison operator. See the section [Superclasses](#superclasses) below.

```haskell
data Ordering = LT | EQ | GT
Expand Down Expand Up @@ -639,7 +639,7 @@ We say that one type class is a superclass of another if every instance of the s

We've [already seen an example of superclass relationships](#ord): the `Eq` class is a superclass of `Ord`, and the `Semigroup` class is a superclass of `Monoid`. For every type class instance of the `Ord` class, there must be a corresponding `Eq` instance for the same type. This makes sense since, in many cases, when the `compare` function reports that two values are incomparable, we often want to use the `Eq` class to determine if they are equal.

In general, it makes sense to define a superclass relationship when the laws for the subclass mention the superclass members. For example, for any pair of Ord and Eq instances, it is reasonable to assume that if two values are equal under the `Eq` instance, then the `compare` function should return `EQ`. In other words, `a == b` should be true exactly when `compare a b` evaluates to `EQ`. This relationship on the level of laws justifies the superclass relationship between `Eq` and `Ord`.
In general, it makes sense to define a superclass relationship when the laws for the subclass mention the superclass members. For example, for any pair of `Ord` and `Eq` instances, it is reasonable to assume that if two values are equal under the `Eq` instance, then the `compare` function should return `EQ`. In other words, `a == b` should be true exactly when `compare a b` evaluates to `EQ`. This relationship on the level of laws justifies the superclass relationship between `Eq` and `Ord`.

Another reason to define a superclass relationship is when there is a clear "is-a" relationship between the two classes. That is, every member of the subclass _is a_ member of the superclass as well.

Expand Down

0 comments on commit c584cf0

Please sign in to comment.