## Concept:

# Total order

## Headline

A transitive, antisymmetric, and total (binary) relation on some set

## Illustration

In programming, total order serves for comparison of values. For instance, in Language:Haskell, we may leverage total order on numbers as follows:

```
> 41 < 42
True
> max 41 42
42
```

Some form of polymorphism may be used in many programming languages to define such a comparison-relate total order on given data types. For instance, in Haskell, there is a type class *Ord* for total order; its key member is *compare* which returns either LT, EQ, or GT. For instance:

```
> compare 41 42
LT
```

Let us illustrate the definition a total order for a simple data type for natural numbers:

```
-- Peano natural numbers
data Nat = Zero | Succ Nat
```

Before we define a total order for natural numbers, let us define equality, as it is effectively a precondition for total order. In Haskell, we instantiate the type class *Eq* hence:

```
-- Equality of natural numbers
instance Eq Nat
where
Zero == Zero = True
Zero == (Succ _) = False
(Succ _) == Zero = False
(Succ x) == (Succ y) = x == y
```

Thus, all pairs of constructor patterns are examined and accordingly mapped to truth values while subterms are processed recursively, when necessary. We can test for equality as follows:

```
> Succ Zero == Zero
False
> Succ Zero == Succ Zero
True
```

The type-class instance for total order follows the same scheme:

```
-- Total order on natural numbers
instance Ord Nat
where
compare Zero Zero = EQ
compare Zero (Succ _) = LT
compare (Succ _) Zero = GT
compare (Succ x) (Succ y) = compare x y
```

We can test for total order as follows:

```
> compare (Succ Zero) Zero
GT
> compare (Succ Zero) (Succ Zero)
EQ
```

## Metadata

### There are no revisions for this page.

## User contributions

## User edits

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**