Metric

[summary:
Let $S$ be a set. A **metric** on $S$ is a function $d : S \times S \to \mathbb R_{\ge 0}$ such that for all $a,b,c \in S$,

1. $d(a,b) = 0 \Leftrightarrow a = b$
2. $d(a,b) = d(b,a)$
3. $d(a,b) + d(b,c) \geq d(a,c)$

Such a function defines a notion of distance between pairs of elements of $S$.
]

A **metric**, sometimes referred to as a distance function, is a [-3jy] that defines a [-4bc real] nonnegative distance between every two elements of a [3jz set]. It is commonly denoted by the variable $d$. In [-3vl], a metric $d$ that defines distances between elements of the set $S$ is written:
$$d: S \times S \to [0, \infty)$$

In this case we say $d$ *is a metric on* $S$.

That is, a metric $d$ on a set $S$ takes as input any two elements $a$ and $b$ from $S$ and outputs a number that is taken to define their distance in $S$ under $d$. Apart from being nonnegative real numbers, the distances a metric outputs must follow three other rules in order for the function to meet the definition of a metric. A function that matches the above colon-to notation is called a metric if and only if it satisfies these requirements. The following must hold for any choice of $a$, $b$, and $c$ in $S$:

 1. $d(a, b) = 0 \iff a = b$

 2. $d(a, b) = d(b, a)$

 3. $d(a, b) + d(b, c) \geq d(a, c)$

(1) effectively states both that the distance from an element to itself is 0, and that the distance between non-identical elements must be greater than 0. (2) asserts that a metric must be [3jb commutative]; informally the distance from $a$ to $b$ must be the same as the distance from $b$ to $a$. Finally, (3) is known as the [-triangle_inequality] and asserts that the distance from $a$ to $c$ is at most as large as the sum of the distances from $a$ to $b$ and from $b$ to $c$. It is named as such because in [euclidean_space], the points $a$, $b$, and $c$ form a triangle, and the inequality requires that the length of one side of the triangle is not longer than the sum of the lengths of the other two sides; violating this would mean that the shortest path between two points is no longer the straight line between them.

It is possible (and relatively common!) to deal with multiple different metrics on the same set. This means we are using the same set elements as labels, but treating the distances between elements differently; in this case the different [metric_space metric spaces] we are defining may have very different properties. If multiple metrics are being considered, we must be careful when speaking of distances between elements of the set to specify which metric we are using. For example, if $d$ and $e$ are both metrics on $S$, we cannot just say "the distance between $a$ and $b$ in $S$" because it is ambiguous whether we are referring to $d(a, b)$ or to $e(a, b)$. We could instead say something like "the distance between $a$ and $b$ under $e$" to remove the ambiguity.

The most commonly-used metric on Cartesian space is the Euclidean metric, defined in two dimensions as $d(a, b) = \sqrt{(a_1-b_1)^2 + (a_2-b_2)^2}$, and more generally in $n$ dimensions as $d(a, b) = \sqrt{\sum_{i=1}^n (a_i-b_i)^2}$.

A less-common metric on Cartesian space is the Manhattan metric, defined generally as $d(a, b) = \sum_{i=1}^n |a_i-b_i|$; the distance is analogous to the distance taken between two points on a rectangular grid when motion is constrained to be purely vertical or horizontal, but not diagonal.

[todo: A metric induces a topology]
[todo: add intuitive/nonalgebraic explanation lens]
[todo: proof that Euclidean and Manhattan distances are metrics]


Solovay's theorems of arithmetical adequacy for GL

[summary:
$GL\vdash A$ iff $PA\vdash A^*$ for every realization $*$.

Realizations are translations from modal sentences to sentences of arithmetic.
]

One of the things that makes [-5l3] such an interesting formal system is the direct relation between its theorems and a restricted albeit rich class of theorems regarding [5j7 provability predicates] in [3ft].

As usual, the adequacy result comes in the form of a pair of theorems, proving respectively [-soundness] and [-completeness] for this class. Before stating the results, we describe the way to [translation translate]  modal sentences to sentences of arithmetic, thus describing the class of sentences of arithmetic the result alludes to.

##Realizations
A realization $*$ is a function from the set of well-formed sentences of modal logic to the set of sentences of arithmetic. Intuitively, we are trying to preserve the structure of the sentence while mapping the expressions proper of modal logic to related predicates in the language of $PA$.

Concretely,

* $p^* = S_p$: sentence letters are mapped to arbitrary closed sentences of arithmetic.
* $(\square A)^*=P(A^*)$: the box operator is mapped to a [-5j7] $P$, usually the [-5gt].
* $(A\to B)^* = A^* \to B^*$: truth functional compounds are mapped as expected.
* $\bot ^* = \neg X$, where $X$ is any theorem of $PA$, for example, $0\ne 1$.

The class of sentences of $PA$ such that there exists a modal sentence of which they are a realization is the set for which we will prove the soundness and completeness.

##Arithmetical soundness
> If $GL\vdash A$, then $PA\vdash A^*$ for every realization $*$.

The applications to this result are endless. For example, this theorem allows us to take advantage of the procedures to calculate [5lx fixed points] in $GL$ to get results about $PA$.

To better get an intuition of how this correspondence works, try figuring out how the properties of the [-5j7] relate to the axioms and rules of inference of $GL$.

[ Proof]

##Arithmetical completeness
> If $GL\not\vdash A$, then there exists a realization $*$ such that $PA\not\vdash A^*$.

The proof of arithmetical completeness is a beautiful and intricate construction that exploits the semantical relationship between $GL$ and the finite, transitive and irreflexive [5ll Kripke models]. Check [ its page] for the details.

##Uniform arithmetical completeness
> There exists a realization $*$ such that for every modal sentence $A$ we have that $GL\not\vdash A$ only if $PA\not\vdash A$.

This result generalizes the arithmetical completeness theorem to a new level.

[ Proof]


Rice's theorem and the Halting problem

We will show that [5mv Rice's theorem] and the [46h the halting problem] are equivalent.

#The Halting theorem implies Rice's theorem
Let $S$ be a non trivial set of computable partial functions, and suppose that there exists a Turing machine encoded by $[n]$ such that:
$$
[n](m) =
\left\{
        \begin{array}{ll}
             1 & [m] \text{ computes a function in $S$} \\
             0 & \text{otherwise} \\
        \end{array}
    \right.
$$

We can assume [without_loss_of_generality w.l.o.g.] that the empty function undefined on every input is not in $S$
%%note:Suppose that the empty function is in $S$. Then it is satisfied that the empty function is **not** in $S^c$, and if $S$ is decidable then it follows immediately that [the_complement_of_a_decidable_set_is_decidable $S^c$ is decidable as well]. So we can use $S^c$ as our $S$ and the argument follows exactly the same.%%.
Thus there exists a computable function in $S$ computed by some machine $[s]$ such that $[s](x)$ is defined for some input $x$.

Suppose we want to decide whether the machine $[m]$ halts on input $[x]$.

For that purpose we can build a machine $Proxy_s$ which does the following:

    Proxy_s(z):
        call [m](x)
        return s[z]

Clearly, if $[m](x)$ halts then Proxy_z computes the same function as $[s]$, and thus $[n](Proxy_s)=1$.

If on the other hand $[m](x)$ does not halt, then Proxy_s(z) computes the empty function, which we assumed to not be in $S$, and therefore $[n](Proxy_s)=0$.

Thus we can use a Turing machine computing pertinence to $S$ to decide the halting problem, which we know is undecidable. We conclude that such a machine cannot possibly exists, and thus Rice's theorem holds.

#Rice's Theorem implies the Halting theorem
Suppose that there was a Turing machine $HALT$ deciding the Halting Problem.

Let $S$ be the set of computable functions defined on a fixed input $x$, which is clearly non-trivial, as it does not contain the empty function but is not empty either. Let $[n]$ be a Turing machine, and we want to decide whether $[n]\in S$ or not. If this was possible for an arbitrary $[n]$, then we would have reached a contradiction, as Rice's theorem forbids this outcome.

But $[n]$ belongs to $S$ iff $[n]$ halts on input $x$, so we can use $HALT$ to decide whether $[n]$ belongs to $S$, in contradiction with Rice's theorem. So our supposition of the existence of $HALT$ was erroneous, and thus the Halting theorem is true.


Modular arithmetic

[summary:
Modular arithmetic is the type of [-addition] we use when calculating dates and times.  In ordinary [-arithmetic], $9 + 6 = 15$, but when working with the hours of the day, 6 hours after 9 o'clock is 3 o'clock, not 15 o'clock.  This type of "wrap-around" addition generalizes to many other domains.
]

[todo: needs images!]

In ordinary [-arithmetic], you can think of [-addition] and [-subtraction] as traveling in different directions along an [infinity infinitely] long road.  A calculation like $9 + 6$ can be thought of as starting at kilometer marker 9, then driving for another 6 kilometers, which would bring you to kilometer marker 15 ([-negative_numbers] are analogous to driving along the road backwards). If the road is perfectly straight, you can never go back to a marker you've already visited by driving forward.  But what if the road were a circle?

Modular arithmetic is a type of addition that's more like driving around in a circle than along an infinite straight line.  In modular arithmetic, you can start with a number, add a positive number to it, and come out with the same number you started with--just as you can drive forward on a circular road to get right back where you started.  If the length of the road were 12, for example, then if you drove 12 kilometers you would wind up right back where you started.  In this case, we would call it *modulus 12* arithmetic, or *mod 12* for short.

Modular arithmetic may seem strange, but in fact, you probably use it every day!  The hours on the face of a clock "wrap around" from 12 to 1 in exactly the same way that a circular road wraps around on itself.  Thus, while in ordinary arithmetic $9 + 6 = 15$, when figuring out what time it will be 6 hours after 9 o'clock, we use modular arithmetic to arrive at the correct answer of 3 o'clock, rather than 15 o'clock.


Partial function

A **partial function** is like a [-3jy] $f: A \to B$, but where we relax the requirement that $f(a)$ must be defined for all $a \in A$.
That is, it must still be the case that "$a = b$ and $f(a)$ is defined" implies "$f(b)$ is defined and $f(a) = f(b)$", but now we no longer need $f(a)$ to be defined everywhere.
We can write $f: A \rightharpoonup B$ %%note:In LaTeX, this symbol is given by `\rightharpoonup`.%%to denote that $f$ is a partial function with **domain** $A$ and **codomain** $B$: that is, whenever $f(x)$ is defined then we have $x \in A$ and $f(x) \in B$.

This idea is essentially the "flip side" to the distinction between the [-3lv] dichotomy.

# Implementation in set theory

Just as a function can be implemented as a set $f$ of ordered pairs $(a, b)$ such that:

- every $x \in A$ appears as the first element of some ordered pair in $f$
- if $(a, b)$ is an ordered pair in $f$ then $a \in A$ and $b \in B$
- if $(a, b)$ and $(a, c)$ are ordered pairs in $f$, then $b=c$

so we can define a *partial* function as a set $f$ of ordered pairs $(a,b)$ such that:

- if $(a, b)$ is an ordered pair in $f$ then $a \in A$ and $b \in B$
- if $(a, b)$ and $(a, c)$ are ordered pairs in $f$, then $b=c$

(That is, we omit the first listed requirement from the definition of a *bona fide* function.)

# Relationship to Turing machines
[todo: maybe this should be under Turing machine rather than partial function?]

Morally speaking, every [5pd Turing machine] $\mathcal{T}$ may be viewed as computing some function $f: \mathbb{N} \to \mathbb{N}$, by defining $f(n)$ to be the state of the tape after $\mathcal{T}$ has been allowed to execute on the tape which has been initialised with the value $n$.

However, if $\mathcal{T}$ does not terminate on input $n$ (for example, it may be the machine "if $n = 3$ then return $1$; otherwise loop indefinitely"), then this "morally correct" state of affairs is not accurate: how should we define $f(4)$?
The answer is that we should instead view $f$ as a *partial* function which is just undefined if $\mathcal{T}$ fails to halt on the input in question.
So with the example $\mathcal{T}$ above, $f$ is the partial function which is only defined at $3$, and $f(3) = 1$.


Turing machine

A Turing Machine is a simple mathematical model of [-computation] that is powerful enough to describe any computation a computer can do.

Imagine a robot, in front of a little whiteboard, with infinitely many whiteboards to both sides, finitely many of which have a symbol written on them. The robot can erase the contents of a whiteboard and replace it with some other symbol, and it can move over to the next whiteboard on the left or right, or shut down. This is all the robot can do. The robot's actions are determined by only two things: the symbol on the whiteboard it just saw, and its internal state. The output of this process is defined to be "whatever is written on the string of whiteboards when the robot has shut down".

This is equivalent to a Turing Machine (with the robot replaced by a machine head and the infinite line of whiteboards replaced by an infinite tape subdivided into cells). The *halting problem* (which [halting_problem_is_uncomputable is unsolvable] in general) asks whether the robot will eventually shut down at some point.

So, a Turing Machine can be specified with the following information:

- A finite set of symbols the robot can write (one of which is the null symbol, an empty board).
- A finite set of states the robot can be in (at least one of which causes the robot to shut down).
- A starting state for the robot.
- Starting symbols on finitely many of the boards (whiteboard location and symbol type data).
- A transition function for the robot, which takes a symbol/state pair as input, and has a $(\text{symbol},\text{state},\text{move left or right})$ triple as output. For example, ine such transition might be represented as `if symbol is 7 and state is FQUF, then (erase and write 4, set state to ZEXA, move left)`.

Surprisingly enough, other proposed models of computation have all been shown to be weaker than, or equivalent to, Turing Machines! With infinite memory space, and sufficiently intricate sets of symbols and states, the robot and whiteboard (or machine head and memory tape) system can compute anything at all that is computable in principle!
This fact is known as the Church-Turing thesis; it's very widely believed to be true, and certainly no-one has ever found any hint of a counterexample, but it's not "proved" in any meaningful sense.

# Variants of Turing machines

*Multi-tape Turing Machines* would be equivalent to having several robots in infinite whiteboard hallways, except that the robots are networked together to all share the same state. An example state transition is as follows:

`If symbol A is * and symbol B is 6 and symbol C is absent and state is VREJ, set state to IXXI, robot A writes ! and moves left, robot B writes 9 and moves left, robot C writes = and doesn't move.`

These Multi-tape Machines can speed up some computations polynomially (so, for example, a problem which would normally take 1 million steps to solve may be solvable in a thousand steps, because of the square root speedup). Because these machines can only muster a polynomial speedup, and moving to a one-tape Turing Machine only incurs a polynomial slowdown, the computational [5pf complexity class P] is unchanged across Turing Machines with different numbers of tapes.

*Write-only Turing Machines* are Multi-tape Turing Machines where one of the tapes/hallways of whiteboards has its input ignored when determining the next state, written symbols and movements.
We can think of this situation as one where one particular robot is blind.

*Read-only Turing Machines* are Multi-tape Turing Machines, and one of the tapes/hallways of whiteboards cannot be rewritten. The robot in there can only move around and observe, but it has not been given a pen or rubber so it can't write on or erase the boards.

*Oracle Machines* (which are more powerful than Turing Machines, and don't exist in reality, though they are a very useful tool in computational complexity theory), are like a mult-tape machine with exactly two tapes: one tape is designated as the "oracle tape", and one tape as the "machine tape".
This time, one of the robot states is "INVOKING MAGIC ORACLE".
When that happens, the contents of the whiteboards in the machine hall (that is, the contents of the machine tape) are interpreted as the description of a problem, and then a correct solution to the problem magically appears on the string of whiteboards in the oracle hall (that is, on the oracle tape), completely erasing whatever was on the oracle hall whiteboards originally; and finally the oracle robot is moved to the first whiteboard of the answer.

Therefore the functionality of the oracle machine depends very strongly on what the oracle does! A given oracle machine might do one thing when the oracle does "compute the [-5bv] of the number I was called with" than when it does "compute whether or not the number I was given is the [-description_number] of a halting Turing machine".

Oracle machines are like ordinary Turing machines, except we also give them the ability (in principle) to obtain instant correct answers to any particular problem. The problem we may instantly solve is fixed in advance, before we ever start running the machine.
With the right oracle, oracle machines can solve *any* problem, where Turing machines cannot (recalling that the halting problem can't be solved by Turing machines).
However, the price is that oracle machines don't exist: they require a magic oracle, and we don't have any of those in nature.


P (Polynomial Time Complexity Class)

P is the [problem_class class of problems] which can be solved by [algorithm algorithms] whose run time is bounded by a [-polynomial].


Ordering of rational numbers (Math 0)

[summary: "Ordering" is the idea that some quantities of apple are "bigger" than others.]

So far while learning about [514 how to combine rational numbers], we have seen [55m addition], [56x subtraction], [59s multiplication] and [5jd division].
There is one final major thing we can do to a pair of rational numbers: to *compare* them.
Once you know what you're looking for, it is very easy to compare certain pairs of rational numbers; in this page, we'll look at how to extend that.

Intuitively, if I gave you an apple in one hand, and four apples in the other hand %%note:My hands are rather large.%%, you would be able to tell me that the four-apples hand was holding more apples.
This is the kind of comparison we are trying to generalise, and we will do it from one simple observation.

The observation we will make is that it is very easy to determine whether a rational number is negative or not. %%note:Recall that "negative" meant "it is expressed in anti-apples rather than apples".%% Indeed, we just need to see if we're holding an anti-thing or not.
This might be hard if we have to do some calculations first—for example, it's not immediately obvious whether $\frac{16}{107} - \frac{3}{20}$ is anti- or not—but we'll assume that we've already done all the calculations to reduce an expression down to just a single rational number.
(In this example, we can use the [56x subtraction techniques] to work out that $\frac{16}{107} - \frac{3}{20} = -\frac{1}{2140}$. That's obviously negative, because it's got a negative sign out the front.)

To summarise, then, what I have just asserted is that it is easy to see whether a rational number is negative or not; we say that a number which is *positive* %%note:That is, expressed in apples rather than anti-apples.%% is "*greater than $0$*".
Recall that $0$ was the name we gave to the rational number which is "no apples at all"; then what this is saying is that if I have a positive number of apples in one hand, and no apples at all in the other, then according to the intuition earlier, I have more apples in the first hand than in the no-apples hand.
(Hopefully you see that this is true; if not, let us know, because this is one of those strange areas where it's very hard for a mathematician to remember *not* understanding it immediately and intuitively, since we've each been doing this for decades. We're doing our best to remember what parts of the maths are genuinely difficult and weird, but we might get it wrong.)

Similarly, we say that $0$ is *less than* any positive number, and write $0 < \frac{5}{16}$, for instance.
The littler quantity always goes on the littler end of the arrow.
(The number $0$ itself is neither negative nor positive. It's just $0$.
Therefore we can't write $0 < 0$ or $0 > 0$; it's actually the case that $0=0$, and this excludes the other two options of $<$ or $>$.)

# One weird trick to compare any two rationals %%note:Mathematicians hate it!%%

Now that we can compare any rational with $0$, we will work out how to compare any rational with any other rational.

The key insight is that adding the same number of apples %%note:Or anti-apples.%% to each hand should not change the relative fact of whether there are more apples in one hand or the other.
For a real-world example, on a [balance scale](https://commons.wikimedia.org/wiki/File:Balanced_scale_of_Justice.svg) it doesn't matter whether you add five grams or even fifty kilograms onto each of the two pans; the result of the weight comparison will be the same.
(Now you should probably forget the scales metaphor again, because the weight of antimatter behaves in a way that doesn't lend itself nicely to what we're trying to do.)

## Example

So let's say we want to compare $\frac{5}{6}$ and $\frac{3}{4}$.
Which of the two is bigger?

Well, what we can do is add $\frac{3}{4}$ of an anti-apple to both hands.
By the principle that "adding the same amount to each hand doesn't change their quantity relative to each other", the result of the comparison between $\frac{5}{6}$ and $\frac{3}{4}$ is just the same as the result of the comparison between $\frac{5}{6} - \frac{3}{4}$ and $\frac{3}{4} - \frac{3}{4}$: that is, between $\frac{1}{12}$ and $0$.
That's easy, though, since we already know how to compare $0$ with anything!

So $\frac{5}{6}$ is bigger than $\frac{3}{4}$, since $\frac{1}{12}$ is bigger than $0$: we write $\frac{5}{6} > \frac{3}{4}$.

## Comparisons with anti-apples

In this section, we will just close our eyes, swallow grimly, and hope for the best.

If we want to compare $-\frac{59}{12}$ and $\frac{4}{7}$, what should happen?
If you already have the right intuition built in, then this will be obvious, but before you know how to do it, it's really not clear at all.
After all, $-\frac{59}{12}$ is "a large amount of anti-apple" (it's nearly five whole anti-apples!) but $\frac{4}{7}$ is "a small amount of apple" (not even one whole apple).

Here's where the "close our eyes" happens.
Let's just go by the principle that adding the same amount of apple to both hands shouldn't change anything, and we'll add $\frac{59}{12}$ apples to both sides.

Then the $-\frac{59}{12}$ becomes $0$, and the $\frac{4}{7}$ becomes the rather gruesome $\frac{461}{84}$. %%note:This is all good practice for you to get fluent with adding and subtracting.%%
But we already know how to compare $0$ with things, so we can see that $\frac{461}{84}$ is bigger than $0$.

Therefore we must have $\frac{4}{7}$ being bigger than $-\frac{59}{12}$.

By the same token, *any* amount of anti-apple is always less than *any* amount of apple, and indeed any amount of anti-apple is always less than $0$.

## Another example

How about comparing $\frac{-3}{5}$ and $\frac{9}{-11}$?
The first thing to do is to remember that we can take the minus signs outside the fractions, because an anti-chunk of apple is the same as a chunk of anti-apple.

That is, we are trying to compare $-\frac{3}{5}$ and $-\frac{9}{11}$.

Add $\frac{3}{5}$ to both, to compare $0$ and $-\frac{9}{11} + \frac{3}{5} = -\frac{12}{55}$.

Add $\frac{12}{55}$ to both again, to compare $\frac{12}{55}$ and $0$.

Clearly the $\frac{12}{55}$ is bigger, so it must be that $\frac{-3}{5}$ is bigger than $\frac{9}{-11}$.

## Instant rule

Just as we had an [55m instant rule for addition], so we can make an instant rule for comparison.

If we want to see which of $\frac{a}{b}$ and $\frac{c}{d}$ is bigger, it is enough to see which of $0$ and $\frac{c}{d} - \frac{a}{b}$ is bigger.

But $$\frac{c}{d} - \frac{a}{b} = \frac{c \times b - a \times d}{b \times d}$$

Sadly from this point there are actually two cases to consider, because we might have produced something that looks like any of the following:

- $\frac{5}{6}$
- $\frac{-4}{7}$
- $\frac{3}{-8}$
- $\frac{-2}{-9}$

(That is, there could be minus-signs scattered all over the place.)

However, there is a way to get around this, and it hinges on the fact from the [5jd Division page] that $\frac{-1}{-1} = 1$.

If $b$ is negative, then we can just write $\frac{a}{b} = \frac{-a}{-b}$, and now $-b$ is positive!
For example, $\frac{5}{-6}$ has $b=-6$; then that is the same as $\frac{-5}{6}$.
Similarly, $\frac{-7}{-8}$ is the same as $\frac{7}{8}$.

Likewise we can always write $\frac{c}{d}$ so that the numerator is positive: as $\frac{-c}{-d}$ if necessary.

So, we have four cases:

- If $b, d$ are both positive, then $\frac{c \times b - a \times d}{b \times d}$ is positive precisely when $c \times b - a \times d$ is positive as an integer; i.e. when $cb > ad$.
- If $b$ is positive and $d$ is negative, then $$\frac{c}{d} - \frac{a}{b} = \frac{-c}{-d} - \frac{a}{b} = \frac{(-c) \times b - a \times (-d)}{b \times (-d)}$$ where the denominator %%note:Remember, that's the thing on the bottom of the fraction: in this case, $b \times (-d)$.%% is positive. [todo: the rest of this section and bullet point]

## Why did I say "Mathematicians hate it"?%%note:Aside from parodying Internet banner ads, that is.%% A diversion on pedagogy

The way I've done this is all completely correct, but it's slightly backwards from the way a mathematician would usually present it.
(Not sufficiently backwards that mathematicians should hate it, but I couldn't resist.)

Usually, when *finding* a way of comparing objects, mathematicians would probably do what we've done above: find an easy way of comparing some of the objects, and then try to extend it to cover all the objects.

But if a mathematician *already knows* a way of comparing objects and is just writing it down for the benefit of other mathematicians, they would usually write down the complete "cover all the objects" method right at the start, and would then go on to show that it does indeed cover all the objects and has all the right properties.

This has the benefit of producing very terse descriptions with the minimum necessary amount of writing; but it's very *bad* at helping other people understand where it came from.
If you know where something came from, you stand a better chance of being able to recreate it yourself if you forget the bottom line, and you might well remember the bottom line better, too.
This is why we've done it slightly backwards here.


Rational arithmetic all works together

[summary: We have seen various operations of arithmetic on the rational numbers; in fact the operations are not as stand-alone as they may have appeared at first, but they are part of a closely-knit structure.]

We have seen [4zx what the rational numbers are], and five things we can do with them:

- [55m addition]
- [56x subtraction]
- [59s multiplication]
- [5jd division]
- [5pk comparison]

These might seem like five standalone operations, but in fact they all play nicely together in a particular way.
You don't need to know the fancy name, but here it is anyway: mathematicians say that the rationals **form an [ordered_field ordered field]**, meaning that the five operations above:

- work in the rationals
- slot together, behaving in certain specific ways that make it easy to calculate

*You shouldn't bother learning this page particularly deeply, because none of the properties alone is very interesting; instead, try and absorb it as a whole.*

We've already seen the "instant rules" for manipulating the rationals, and where they come from.
Here, we'll first quickly define the rational numbers themselves, and all the operations above, in "instant rule" format, just to have them all here in one place.
Then we'll go through all the properties that are required for mathematicians to be able to say that the operations on rationals "play nicely together" in the above sense; and we'll be relying on the instant rules as our definitions, because they're totally unambiguous.
That way we can be much more sure that we're not making some small error.
(Relying on an intuition about how apples work could in theory lead us astray; but the rules leave no wiggle-room or scope for interpretation.)

The letters $a, b, c, d$ should be read as standing for integers (possibly positive, negative or $0$), and $b$ and $d$ should be assumed not to be $0$ (recalling that [5jd it makes no sense] to divide by $0$).

- A **rational number** is a pair of [53r integers], written as $\frac{a}{b}$, where $b$ is not $0$, such that $\frac{a}{b}$ is viewed as being the same as $\frac{c}{d}$ precisely when $a \times d = b \times c$. %%note:This is just the instant rule for subtraction, below, together with the assumption that $\frac{0}{x} = \frac{0}{y}$ for any $x, y$ nonzero integers; in particular, the assumption that $\frac{0}{b \times d} = \frac{0}{1}$ when $b, d$ are not $0$, which we will need later.%%
- Addition: $$\frac{a}{b} + \frac{c}{d} = \frac{a \times d + b \times c} {b \times d}$$
- Subtraction: $$\frac{a}{b} - \frac{c}{d} = \frac{a}{b} + \frac{-c}{d} = \frac{a \times d - b \times c}{b \times d}$$
- Multiplication: $$\frac{a}{b} \times \frac{c}{d} = \frac{a \times c}{b \times d}$$
- Division (where also $c$ is not $0$): $$\frac{a}{b} \big/ \frac{c}{d} = \frac{a}{b} \times \frac{d}{c} = \frac{a \times d}{b \times c}$$
- Comparison (where we write $\frac{a}{b}$ and $\frac{c}{d}$ such that both $b$ and $d$ are positive %%note:Remember, we can do that: if $b$ is negative, for instance, we may instead write $\frac{a}{b}$ as $\frac{-a}{-b}$, and the extra minus-sign we introduce has now flipped $b$ from being negative to being positive.%%): $\frac{a}{b} < \frac{c}{d}$ precisely when $\frac{c}{d}-\frac{a}{b}$ is positive: that is, when $$\frac{b \times c - a \times d}{b \times d} > 0$$
which is in turn when $$b \times c - a \times d > 0$$

To be super-pedantic, it is necessary to show that the rules above are "well-defined": for instance, you don't get different answers if you apply the rules using $\frac{2}{4}$ instead of $\frac{1}{2}$.
Concretely, for example, $$\frac{2}{4} + \frac{1}{3} = \frac{1}{2} + \frac{1}{3}$$ according to the instant rules.
It's actually true that they *are* well-defined in this sense, but we won't show it here; consider them to be exercises.

Additionally, each integer can also be viewed as a rational number: namely, the integer $n$ can be viewed as $\frac{n}{1}$, being "$n$ copies of the $\frac{1}{1}$-chunk".

The properties required for an "ordered field"—that is, a nicely-behaved system of arithmetic in the rationals—fall naturally into groups, which will correspond to main-level headings below.

[toc:]

# How addition behaves

## Addition always spits out a rational number %%note:Confucius say that man who run in front of bus get tired; man who run behind bus get exhausted. On the other hand, mathematicians say that the rationals are *closed* under addition.%%

You might remember a certain existential dread mentioned in the [4zx intro to rational numbers]: whether it made sense to add rational numbers at all.
Then we saw in [55m] that in fact it does make sense.

However, in this page we are working from the instant rules above, so if we want to prove that "addition spits out a rational number", we actually have to prove the fact that $\frac{a \times d + b \times c} {b \times d}$ is a rational number; no mention of apples at all.

Remember, a rational number here (as defined by instant rule!) is a pair of integers, the bottom one being non-zero.
So we just need to check that $a \times d + b \times c$ is an integer and that $b \times d$ is a non-zero integer, where $a, b, c, d$ are integers and $b, d$ are also not zero.

But that's easy: the product of integers is integer, and the product of integers which aren't zero is not zero.

## There must be a rational $0$ such that adding $0$ to anything doesn't change the thing  %%note:Mathematicians say that there is an [-54p] for addition.%%

This tells us that if we want to stay where we are, but we absolutely must add something, then we can always add $0$; this won't change our answer.
More concretely, $\frac{5}{6} + 0 = \frac{5}{6}$.

Strictly speaking, I'm pulling a bit of a fast one here; if you noticed how, then *very* well done.
A rational number is a *pair* of integers, such that the bottom one is not $0$; and I've just tried to say that $0$ is our rational such that adding $0$ doesn't change rationals.
But $0$ is not a pair of integers!

The trick is to use $\frac{0}{1}$ instead.
Then we can verify that $$\frac{0}{1} + \frac{a}{b} = \frac{0 \times b + a \times 1}{1 \times b} = \frac{0 + a}{b} = \frac{a}{b}$$ as we require.

## Every rational must have an anti-rational under addition  %%note:Mathematicians say that addition is *invertible*.%%

Concretely, every rational $\frac{a}{b}$ must have some rational $\frac{c}{d}$ such that $\frac{a}{b} + \frac{c}{d} = \frac{0}{1}$.
We've [56x already seen] that we can define $-\frac{a}{b}$ to be such an "anti-rational", but remember that in order to fit in with our instant rules above, $-\frac{a}{b}$ is also not a pair of integers; we should instead use $\frac{-a}{b}$.

Then $$\frac{a}{b} + \frac{-a}{b} = \frac{a \times b + (-a) \times b}{b \times b} = \frac{0}{b \times b}$$

Finally, we want $\frac{0}{b \times b} = \frac{0}{1}$; but this is immediate by the instant-rule definition of "rational number", since $0 \times 1 = 0 \times (b \times b)$ (both being equal to $0$).

## Addition doesn't care which way round we do it %%note:Mathematicians say that addition is [3jb commutative].%%

Concretely, this is the fact that $$\frac{a}{b} + \frac{c}{d} = \frac{c}{d} + \frac{a}{b}$$
This is intuitively plausible because "addition" is just "placing apples next to each other", and if I put five apples down and then seven apples, I get the same number of apples as if I put down seven and then five.

But we have to use the instant rules now, so that we can be sure our definition is completely watertight.

So here we go: $$\frac{a}{b} + \frac{c}{d} = \frac{a \times d + b \times c}{b \times d} = \frac{c \times b + d \times a}{d \times b} = \frac{c}{d} + \frac{a}{b}$$
where we have used the fact that multiplication of *integers* doesn't care about the order in which we do it, and similarly addition of *integers*.

## Addition doesn't care about the grouping of terms %%note:Mathematicians say that addition is [3h4 associative].%%

Here, we mean that $$\left(\frac{a}{b} + \frac{c}{d}\right) + \frac{e}{f} = \frac{a}{b} + \left( \frac{c}{d} + \frac{e}{f} \right)$$
This is an intuitively plausible fact, because "addition" is just "placing apples next to each other", and if I put down three apples, then two apples to the right, then one apple to the left, I get the same number ($6$) of apples as if I had put down one apple on the left, then three apples in the middle, then two on the right.

But we have to use the instant rules now, so that we can be sure our definition is completely watertight.
So here goes, starting from the left-hand side:
$$\left(\frac{a}{b} + \frac{c}{d}\right) + \frac{e}{f} = \frac{a \times d + b \times c}{b \times d} + \frac{e}{f} = \frac{(a \times d + b \times c) \times f + (b \times d) \times e}{(b \times d) \times f}$$

Since addition and multiplication of *integers* don't care about grouping of terms, this is just $$\frac{a \times d \times f + b \times c \times f + b \times d \times e}{b \times d \times f}$$

Now going from the right-hand side:
$$\frac{a}{b} + \left( \frac{c}{d} + \frac{e}{f} \right) = \frac{a}{b} + \frac{c \times f + d \times e}{d \times f} = \frac{a \times (d \times f) + b \times (c \times f + d \times e))}{b \times (d \times f)}$$
and similarly we can write this $$\frac{a \times d \times f + b \times c \times f + b \times d \times e}{b \times d \times f}$$
which is the same as we got by starting on the left-hand side.

Since we showed that both the left-hand and the right-hand side are equal to the same thing, they are in fact equal to each other.

# How multiplication behaves

## Multiplication always spits out a rational number %%note:Mathematicians say that the rationals are *closed* under multiplication.%%
This one is very easy to show: $\frac{a}{b} \times \frac{c}{d} = \frac{a \times c}{b \times d}$, but $b \times d$ is not zero when $b$ and $d$ are not zero, so this is a valid rational number.

## There must be a rational $1$ such that multiplying a thing by $1$ doesn't change the thing %%note:Mathematicians say that multiplication has an [-54p].%%

From our motivation of what multiplication was ("do unto $\frac{a}{b}$ what you would otherwise have done unto $1$"), it should be clear that the number $1$ ought to work here.
However, $1$ is not strictly speaking a rational number according to the letter of the "instant rules" above; so instead we use $\frac{1}{1}$.

Then $$\frac{1}{1} \times \frac{a}{b} = \frac{1 \times a}{1 \times b} = \frac{a}{b}$$ since $1 \times n = n$ for any integer $n$.

## Every nonzero rational must have a corresponding rational such that when we multiply them, we get $1$ %%note:Mathematicians say that every nonzero rational has an *inverse*. (Multiplication is not quite *invertible*, because $0$ has no inverse.)%%
[todo: recall the intuition from multiplication]

Using the instant rule, though, we can take our nonzero rational $\frac{a}{b}$ (where neither $a$ nor $b$ is zero; this is forced by the requirement that $\frac{a}{b}$ be nonzero), and use $\frac{b}{a}$ as our corresponding rational.
Then $$\frac{a}{b} \times \frac{b}{a} = \frac{a\times b}{b \times a} = \frac{a \times b}{a \times b} = \frac{1}{1}$$

## Multiplication doesn't care which way round we do it %%note:Mathematicians say that multiplication is [3jb commutative].%%
This had its own section on the [59s multiplication page], and its justification there was by rotating a certain picture.
Here, we'll use the instant rule:
$$\frac{a}{b} \times \frac{c}{d} = \frac{a \times c}{b \times d} = \frac{c \times a}{d \times b} = \frac{c}{d} \times \frac{a}{b}$$
where we have used that multiplication of *integers* doesn't care about order.

## Multiplication doesn't care about the grouping of terms %%note:Mathematicians say that multiplication is [3h4 associative].%%

This is much harder to see from our intuition, because while addition is a very natural operation ("put something next to something else"), multiplication is much less natural (it boils down to "make something bigger").
In a sense, we're trying to show that "make something bigger and then bigger again" is the same as "make something bigger by a bigger amount".
However, it turns out to be the case that multiplication *also* doesn't care about the grouping of terms.

Using the instant rule for multiplication, it's quite easy to show that $$\frac{a}{b} \times \left(\frac{c}{d} \times \frac{e}{f} \right) = \left(\frac{a}{b} \times \frac{c}{d} \right) \times \frac{e}{f}$$

Indeed, working from the left-hand side:
$$\frac{a}{b} \times \left(\frac{c}{d} \times \frac{e}{f} \right) = \frac{a}{b} \times \frac{c \times e}{d \times f} = \frac{a \times (c \times e)}{b \times (d \times f)}$$
which is $\frac{a \times c \times e}{b \times d \times f}$ because multiplication of *integers* doesn't care about the grouping of terms.

On the other hand, $$\left(\frac{a}{b} \times \frac{c}{d} \right) \times \frac{e}{f} = \frac{a \times c}{b \times d} \times \frac{e}{f} = \frac{(a \times c) \times e}{(b \times d) \times f} = \frac{a \times c \times e}{b \times d \times f}$$

These two are equal, so we have shown that the left-hand and right-hand sides are both equal to the same thing, and hence they are the equal to each other.

# How addition and multiplication interact

## Multiplication "filters through" addition %%note:Mathematicians say that multiplication *distributes over* addition.%%

What we will show is that $$\left(\frac{c}{d} + \frac{e}{f}\right) \times \frac{a}{b} = \left(\frac{a}{b} \times \frac{c}{d}\right) + \left(\frac{a}{b} \times \frac{e}{f}\right)$$

This is intuitively true: the left-hand side is "make $\frac{a}{b}$, but instead of starting out with $1$, start with $\left(\frac{c}{d} + \frac{e}{f}\right)$"; while the right-hand side is "make $\frac{a}{b}$, but instead of starting out with $1$, start with $\frac{c}{d}$; then do the same but start with $\frac{e}{f}$; and then put the two together".
If we draw out diagrams for the right-hand side, and put them next to each other, we get the diagram for the left-hand side.
(As an exercise, you should do this for some specific values of $a,b,c,d,e,f$.)

We'll prove it now using the instant rules.

The left-hand side is $$\left(\frac{c}{d} + \frac{e}{f}\right) \times \frac{a}{b} = \frac{c \times f + d \times e}{d \times f} \times \frac{a}{b} = \frac{(c \times f + d \times e) \times a}{(d \times f) \times b}$$
which is $\frac{c \times f \times a + d \times e \times a}{d \times f \times b}$.

The right-hand side is $$\left(\frac{a}{b} \times \frac{c}{d}\right) + \left(\frac{a}{b} \times \frac{e}{f}\right) = \frac{a \times c}{b \times d} + \frac{a \times e}{b \times f} = \frac{(a \times c) \times (b \times f) + (b \times d) \times (a \times e)}{(b \times d) \times (b \times f)} = \frac{(a \times c \times b \times f) + (b \times d \times a \times e)}{(b \times d \times b \times f)}$$
Notice that everything on the top has a $b$ in it somewhere, so we can use this same "filtering-through" property that we already know the *integers* have:
$$\frac{b \times [(a \times c \times f) + (d \times a \times e)]}{b \times (d \times b \times f)}$$
And finally we know that multiplying a fraction's numerator and denominator both by $b$ doesn't change the fraction:
$$\frac{(a \times c \times f) + (d \times a \times e)}{d \times b \times f}$$

This is just the same as the left-hand side, after we rearrange the product terms.

# How comparison interacts with the operations

## How comparison interacts with addition
The founding principle for how we came up with the definition of "comparison" was that adding the same thing to two sides of a balance scale didn't change the balance.

In terms of the instant rules, that becomes: if $\frac{a}{b} < \frac{c}{d}$, then $\frac{a}{b} + \frac{e}{f} < \frac{c}{d} + \frac{e}{f}$.

But by our instant rule, this is true precisely when $$0 < \frac{c}{d} + \frac{e}{f} - (\frac{a}{b} + \frac{e}{f})$$ and since multiplication by $-1$ filters through addition, that is precisely when $$0 < \frac{c}{d} + \frac{e}{f} - \frac{a}{b} - \frac{e}{f}$$
which (since addition doesn't care about which way round we do the additions, and subtraction is just a kind of addition) is precisely when $$0 < \frac{c}{d} - \frac{a}{b} + \frac{e}{f} - \frac{e}{f}$$
i.e. when $0 < \frac{c}{d} - \frac{a}{b}$; i.e. when $\frac{a}{b} < \frac{c}{d}$.

## How comparison interacts with multiplication

The aim here is basically to show that we can't multiply two things and get an anti-thing.
Written out in the notation, we wish to show that if $0 < \frac{a}{b}$ and if $0 < \frac{c}{d}$ then $0 < \frac{a}{b} \times \frac{c}{d}$, since the test for anti-ness is "am I less than $0$?".

Since $0 < \frac{a}{b}$, there are two options: either both $a$ and $b$ are positive, or they are both negative.
(If one is negative and one is positive, then the fraction will be negative.)

Likewise either both $c$ and $d$ are positive, or both are negative.

So we have four options in total:

- $a, b, c, d$ are positive
- $a, b, c, d$ are negative
- $a, b$ are positive; $c, d$ are negative
- $a, b$ are negative; $c, d$ are positive.

In the first case, we have $\frac{a \times c}{b \times d}$ positive, because all of $a, b, c, d$ are.

In the second case, we have $a \times c$ positive and $b \times d$ also positive (because both are two negative integers multiplied together); so again the fraction $\frac{a \times c}{b \times d}$ is positive.

In the third case, we have $a \times c$ negative and $b \times d$ also negative (because both are a positive number times a negative number); so the fraction $\frac{a \times c}{b \times d}$ is a negative divided by a negative.
Therefore it is positive, because we can multiply the numerator and the denominator by $-1$ to turn it into a positive divided by a positive.

%%hidden(Example):
We'll consider $\frac{1}{3}$ and $\frac{-2}{-5}$.
Then the product is $\frac{1}{3} \times \frac{-2}{-5} = \frac{-2}{-15}$; but that is the same as $\frac{2}{15}$.
%%

In the fourth case, we can do the same as the above, or we can be a bit sneaky: using the fact from earlier that multiplication doesn't care about order, we can note that $\frac{a}{b} \times \frac{c}{d}$ is the same as $\frac{c}{d} \times \frac{a}{b}$.
But now $c$ and $d$ are positive, and $a$ and $b$ are negative; we've already shown (in the previous case) that if the first fraction is "positive divided by positive", and the second fraction is "negative divided by negative".
By swapping $a$ for $c$, and $b$ for $d$, we can use a result we've already proved to obtain this final case.

Hence we've shown all four possible cases, and so the final result follows.


Order of rational operations (Math 0)

[summary: When an expression from rational arithmetic is written out, evaluate anything in brackets first; then perform multiplication and division left-to-right; then perform addition and subtraction left-to-right.]

[todo: write the page]


Principal ideal domain

[summary: A principal ideal domain is an [-5md] in which every [ideal_ring_theory ideal] has a single generator.]

In [3gq ring theory], an [-5md] is a **principal ideal domain** (or **PID**) if every [ideal_ring_theory ideal] can be generated by a single element.
That is, for every ideal $I$ there is an element $i \in I$ such that $\langle i \rangle = I$; equivalently, every element of $I$ is a multiple of $i$.

Since ideals are [5r6 kernels] of [ring_homomorphism ring homomorphisms] ([5r9 proof]), this is saying that a PID $R$ has the special property that *every* ring homomorphism from $R$ acts "nearly non-trivially", in that the collection of things it sends to the identity is just "one particular element, and everything that is forced by that, but nothing else".

# Examples

- Every [euclidean_domain Euclidean domain] is a PID. ([euclidean_domain_is_pid Proof.])
- Therefore $\mathbb{Z}$ is a PID, because it is a [euclidean_domain Euclidean domain]. (Its Euclidean function is "take the modulus".)
- Every [481 field] is a PID because every ideal is either the singleton $\{ 0 \}$ (i.e. generated by $0$) or else is the entire ring (i.e. generated by $1$).
- The [polynomial_ring ring $F[X]$ of polynomials] over a field $F$ is a PID, because it is a Euclidean domain. (Its Euclidean function is "take the [polynomial_degree degree] of the polynomial".)
- The ring of [gaussian_integer Gaussian integers], $\mathbb{Z}[i]$, is a PID because it is a Euclidean domain. ([gaussian_integers_is_pid Proof]; its Euclidean function is "take the [norm_complex_number norm]".)
- The ring $\mathbb{Z}[X]$ (of integer-coefficient polynomials) is *not* a PID, because the ideal $\langle 2, X \rangle$ is not principal. This is an example of a [-unique_factorisation_domain] which is not a PID. [todo: proof of this]
- The ring $\mathbb{Z}_6$ is *not* a PID, because it is not an integral domain. (Indeed, $3 \times 2 = 0$ in this ring.)

There are examples of PIDs which are not Euclidean domains, but they are mostly uninteresting.
One such ring is $\mathbb{Z}[\frac{1}{2} (1+\sqrt{-19})]$. ([Proof.](http://www.maths.qmul.ac.uk/~raw/MTH5100/PIDnotED.pdf))

# Properties

- Every PID is a [-unique_factorisation_domain]. ([principal_ideal_domain_has_unique_factorisation Proof]; this fact is not trivial.) The converse is false; see the case $\mathbb{Z}[X]$ above.
- In a PID, "[5m2 prime]" and "[5m1 irreducible]" coincide. ([5mf Proof.]) This fact also characterises the [maximal_ideal maximal ideals] of PIDs.
- Every PID is trivially [noetherian_ring Noetherian]: every ideal is not just *finitely* generated, but generated by a single element.


Kernel of ring homomorphism

[summary: The kernel of a [-ring_homomorphism] is the collection of elements which the homomorphism sends to $0$.]

Given a [-ring_homomorphism] $f: R \to S$ between [3gq rings] $R$ and $S$, we say the **kernel** of $f$ is the collection of elements of $R$ which $f$ sends to the zero element of $S$.

Formally, it is $$\{ r \in R \mid f(r) = 0_S \}$$
where $0_S$ is the zero element of $S$.

# Examples

- Given the "identity" (or "do nothing") ring homomorphism $\mathrm{id}: \mathbb{Z} \to \mathbb{Z}$, which sends $n$ to $n$, the kernel is just $\{ 0 \}$.
- Given the ring homomorphism $\mathbb{Z} \to \mathbb{Z}$ taking $n \mapsto n \pmod{2}$ (using the usual shorthand for [-5ns]), the kernel is the set of even numbers.

# Properties

Kernels of ring homomorphisms are very important because they are precisely [ideal_ring_theory ideals]. ([5r9 Proof.])
In a way, "ideal" is to "ring" as "[-576]" is to "[3gd group]", and certainly [subring_ring_theory subrings] are much less interesting than ideals; a lot of ring theory is about the study of ideals.

The kernel of a ring homomorphism always contains $0$, because a ring homomorphism always sends $0$ to $0$.
This is because it may be viewed as a [-47t] acting on the underlying additive group of the ring in question, and [49z the image of the identity is the identity] in a group.

If the kernel of a ring homomorphism contains $1$, then the ring homomorphism sends everything to $0$.
Indeed, if $f(1) = 0$, then $f(r) = f(r \times 1) = f(r) \times f(1) = f(r) \times 0 = 0$.


0.999...=1

Although some people find it counterintuitive, the [4sl decimal expansions] $0.999\dotsc$ and $1$ represent the same [-4bc].

# Informal proofs

These "proofs" can help give insight, but be careful; a similar technique can "prove" that $1+2+4+8+\dotsc=-1$. They work in this case because the [-series] corresponding to $0.999\dotsc$ is [-absolutely_convergent].

* \begin{align}
x &= 0.999\dotsc \newline
10x &= 9.999\dotsc \newline
10x-x &= 9.999\dotsc-0.999\dotsc \newline
9x &= 9 \newline
x &= 1 \newline
\end{align}

* \begin{align}
\frac 1 9 &= 0.111\dotsc \newline
1 &= \frac 9 9 \newline
&= 9 \times \frac 1 9 \newline
&= 9 \times 0.111\dotsc \newline
&= 0.999\dotsc
\end{align}

* The real numbers are [-dense], which means that if $0.999\dots\neq1$, there must be some number in between. But there's no decimal expansion that could represent a number in between $0.999\dots$ and $1$.

# Formal proof

This is a more formal version of the first informal proof, using the definition of [-4sl].

%%hidden(Show proof):
$0.999\dots$ is the decimal expansion where every digit after the decimal point is a $9$. By definition, it is the value of the series $\sum_{k=1}^\infty 9 \cdot 10^{-k}$. This value is in turn defined as the [-limit] of the sequence $(\sum_{k=1}^n 9 \cdot 10^{-k})_{n\in\mathbb N}$. Let $a_n$ denote the $n$th term of this sequence. I claim the limit is $1$. To prove this, we have to show that for any $\varepsilon>0$, there is some $N\in\mathbb N$ such that for every $n>N$, $|1-a_n|<\varepsilon$.

Let's prove by [5fz induction] that $1-a_n=10^{-n}$. Since $a_0$ is the sum of {$0$ terms, $a_0=0$, so $1-a_0=1=10^0$. If $1-a_i=10^{-i}$, then

\begin{align}
1 - a_{i+1} &= 1 - (a_i + 9 \cdot 10^{-(i+1)}) \newline
&= 1-a_i - 9 \cdot 10^{-(i+1)} \newline
&= 10^{-i} - 9 \cdot 10^{-(i+1)} \newline
&= 10 \cdot 10^{-(i+1)} - 9 \cdot 10^{-(i+1)} \newline
&= 10^{-(i+1)}
\end{align}

So $1-a_n=10^{-n}$ for all $n$. What remains to be shown is that $10^{-n}$ eventually gets (and stays) arbitrarily small; this is true by the [archimedean_property] and because $10^{-n}$ is monotonically decreasing.
%%

# Arguments against $0.999\dotsc=1$

These arguments are used to try to refute the claim that $0.999\dotsc=1$. They're flawed, since they claim to prove a false conclusion.

* $0.999\dotsc$ and $1$ have different digits, so they can't be the same. In particular, $0.999\dotsc$ starts "$0.$," so it must be less than 1.

%%hidden(Why is this wrong?):
Decimal expansions and real numbers are different objects. Decimal expansions are a nice way to represent real numbers, but there's no reason different decimal expansions have to represent different real numbers.
%%

* If two numbers are the same, their difference must be $0$. But $1-0.999\dotsc=0.000\dotsc001\neq0$.

%%hidden(Why is this wrong?):
Decimal expansions go on infinitely, but no farther. $0.000\dotsc001$ doesn't represent a real number because the $1$ is supposed to be after infinitely many $0$s, but each digit has to be a finite distance from the decimal point. If you have to pick a real number to for $0.000\dotsc001$ to represent, it would be $0$.
%%

* $0.999\dotsc$ is the limit of the sequence $0.9, 0.99, 0.999, \dotsc$. Since each term in this sequence is less than $1$, the limit must also be less than $1$. (Or "the sequence can never reach $1$.")

%%hidden(Why is this wrong?):
The sequence gets arbitrarily close to $1$, so its limit is $1$. It doesn't matter that all of the terms are less than $1$.
%%

* In the first proof, when you subtract $0.999\dotsc$ from $9.999\dotsc$, you don't get $9$. There's an extra digit left over; just as $9.99-0.999=8.991$, $9.999\dotsc-0.999\dotsc=8.999\dotsc991$.

%%hidden(Why is this wrong?):
There are infinitely many $9$s in $0.999\dotsc$, so when you shift it over a digit there are still the same amount. And the "decimal expansion" $8.999\dotsc991$ doesn't make sense, because it has infinitely many digits and then a $1$.
%%


Ideals are the same thing as kernels of ring homomorphisms

[summary: In [3gq ring theory], the notion of "[ideal_ring_theory ideal]" corresponds precisely with the notion of "[5r6 kernel] of [-ring_homomorphism]".]

In [3gq ring theory], the notion of "[ideal_ring_theory ideal]" corresponds precisely with the notion of "[5r6 kernel] of [-ring_homomorphism]".

This result is analogous to the fact from [3gd group theory] that [4h6 normal subgroups] are the same thing as [49y kernels of group homomorphisms] ([4h7 proof]).

# Proof

## Kernels are ideals

Let $f: R \to S$ be a ring homomorphism between rings $R$ and $S$.
We claim that the kernel $K$ of $f$ is an ideal.

Indeed, it is clearly a [-576] of the ring $R$ when viewed as just an additive group %%note:That is, after removing the multiplicative structure from the ring.%% because $f$ is a *group* homomorphism between the underlying additive groups, and kernels of *group* homomorphisms are subgroups (indeed, *normal* subgroups). ([4h7 Proof.])

We just need to show, then, that $K$ is closed under multiplication by elements of the ring $R$.
But this is easy: if $k \in K$ and $r \in R$, then $f(kr) = f(k)f(r) = 0 \times r = 0$, so $kr$ is in $K$ if $k$ is.

## Ideals are kernels

[todo: refer to the quotient group, and therefore introduce the quotient ring]


Fundamental Theorem of Arithmetic

[todo: make Number Theory a parent of this]

[summary: The Fundamental Theorem of Arithmetic is a statement about the [45h natural numbers]; it says that every natural number may be decomposed as a product of [4mf primes], and this expression is unique up to reordering the factors. It is an extremely important theorem, and it is the basis of the field of [-number_theory].]

The Fundamental Theorem of Arithmetic states that every [-45h] (greater than or equal to $2$) may be expressed as a product of [4mf prime numbers], and the product is unique up to reordering.

This theorem is one of the main reasons $1$ is not considered to be prime: indeed, if it were prime then $3 \times 5$ could be factorised into primes as $3 \times 5 \times 1$, but these would be two *different* factorisations of the number $15$.
The FTA's statement is much cleaner if $1$ is not thought of as prime.

In a more general context, the FTA says precisely that the [3gq ring] $\mathbb{Z}$ is a [-unique_factorisation_domain]; there is therefore a much more abstract proof than the elementary one we will present further on in this article:

- $\mathbb{Z}$ is a [euclidean_domain Euclidean domain] (with Euclidean function given by "take the modulus");
- Therefore $\mathbb{Z}$ is a [-5r5] ([euclidean_domain_is_pid proof]);
- And principal ideal domains are unique factorisation domains ([principal_ideal_domain_has_unique_factorisation proof]).

# Examples

- The FTA does not talk about $0$ or $1$; this is because these numbers are conventionally considered neither prime nor composite.
- Even if we haven't bothered to calculate $17 \times 23 \times 23$, we can immediately say that it is odd. Indeed, by the FTA, $2$ cannot divide $17 \times 23^2$, because the complete list of prime factors of this number is $\{ 17, 23, 23\}$, and $2$ is prime.

# Proof

Timothy Gowers has an [excellent article](https://gowers.wordpress.com/2011/11/18/proving-the-fundamental-theorem-of-arithmetic/) about the proof of the FTA.

The FTA consists of two parts: we must show that every number can be decomposed as primes, and also that every number can be decomposed *uniquely*.

## Every number can be written as a product of primes

This part is the easier, and it uses [-strong_induction] (a version of [5fz proof by induction]).

Clearly $2$ can be written as a product of primes, because it *is* prime; so it can be written as just itself.

Now, for $n$ bigger than $2$, if $n$ is prime then we are immediately done (just write it as itself).
Otherwise, $n$ is not prime, so it can be written as $a \times b$, say, with $a$ and $b$ both less than $n$.

But by the inductive hypothesis, we can express $a$ and $b$ each as products of primes, so we can express $n$ as the combined product of the two sets of factors of $a$ and $b$.

%%hidden(Example):
Consider $n = 1274$.
We have two options: $n$ is prime or $n$ is composite.

It turns out that $n$ is actually equal to $49 \times 26$, so it's not prime.

By the inductive hypothesis, we can factor $49$ as a product of primes (indeed, it's $7^2$); and we can factor $26$ as a product of primes (indeed, it's $2 \times 13$); so we can factor $1274$ as $2 \times 7^2 \times 13$.

(If you like, you can view this as just "start again at $49$ instead of at $1274$, and spit out what you get; then start again at $26$ instead of $1274$, and spit out what you get; and finally combine the spittings-out"; no mention of a spooky "inductive hypothesis" at all.)

Note that at this point, we haven't any guarantee at all that this is the *only* prime factorisation; all we assert so far is that it is *a* prime factorisation.
%%

## Every number can be decomposed *uniquely* as a product of primes

For this, we will need a basic (but non-obvious and important) fact about the behaviour of prime numbers: [5mh Euclid's lemma], which states that if a prime $p$ divides a product $ab$, then $p$ divides at least one of $a$ and $b$.

We will work by induction on $n$ again.
If $n = 2$ then the result is immediate: a number can only be divided by numbers which are not larger than it, but $1$ and $2$ are the only such numbers.

Suppose $n$ can be written as both $p_1 p_2 \dots p_r$ and $q_1 q_2 \dots q_s$, where each $p_i$ and $q_j$ is prime (but there might be repeats: maybe $p_1 = p_2 = q_3 = q_7$, for instance).
We need to show that $r=s$ and that (possibly after reordering the lists) $p_i = q_i$ for each $i$.

Certainly $p_1$ divides $n$, because it divides $p_1 p_2 \dots p_r$.
Therefore it divides $q_1 q_2 \dots q_s$, and hence it divides one of $q_1$ or $q_2 \dots q_s$, by Euclid's lemma.
Therefore either it divides $q_1$, or it divides one of $q_2$ or $q_3 \dots q_s$; by induction, $p_1$ divides some $q_i$.
Because we don't care about the ordering of the list, let us reorder the list if necessary so that in fact $i=1$: put the factor $q_i$ at the start of the list.

Now, $q_1$ is prime, and $p_1$ is not equal to $1$ but it divides $q_1$; hence $p_1 = q_1$.

Dividing through by $p_1$, then, we obtain $p_2 \dots p_r = q_2 \dots q_s$, a strictly smaller number; so by the inductive hypothesis, $r-1 = s-1$ (so $r=s$) and the unordered list of $p_i$ is the same as the unordered list of $q_i$ for $i \geq 2$.

This proves the theorem.

# Why is this not obvious?

Timothy Gowers has a [good piece](https://gowers.wordpress.com/2011/11/13/why-isnt-the-fundamental-theorem-of-arithmetic-obvious/) on why this result is not just obvious.
Of course, what is "obvious" and what is not "obvious" varies heavily depending on who you're talking to.
For this author personally, the true reason it's not obvious is Gowers's reason number 4: because there are very similar structures which do *not* have the property of unique factorisation.
(Gowers uses $\mathbb{Z}[\sqrt{-5}]$; on the [5m1 page on irreducibles], we show that $\mathbb{Z}[\sqrt{-3}]$ could be used just as well.)


Formal definition of the free group

[summary: The [-5kg] may be constructed formally using van der Waerden's trick, which is not intuitive at all but leads to a definition that is very easy to work with. This page will detail van der Waerden's construction, and will prove that the trick yields a group which has all the right properties to be the free group.]

The [-5kg] may be constructed formally using van der Waerden's trick, which is not intuitive at all but leads to a definition that is very easy to work with. This page will detail van der Waerden's construction, and will prove that the trick yields a group which has all the right properties to be the free group.

# The construction

Write $X^r$ for the set that contains all the [5jc freely reduced words] over $X \cup X^{-1}$ (so, for instance, excluding the word $aa^{-1}$). %%note:We use the superscript $r$ to denote "reduced".%%

We define the *free group* $F(X)$, or $FX$, on the set $X$ to be a certain [-576] of the [-497] $\mathrm{Sym}(X^r)$: namely that which is generated by the following elements, one for each $x \in X \cup X^{-1}$:

- $\rho_x : \mathrm{Sym}(X^r) \to \mathrm{Sym}(X^r)$, sending $a_1 a_2 \dots a_n \mapsto a_1 a_2 \dots a_n x$ if $a_n \not = x^{-1}$, and $a_1 a_2 \dots a_{n-1} x^{-1} \mapsto a_1 a_2 \dots a_{n-1}$.
- $\rho_{x^{-1}} : \mathrm{Sym}(X^r) \to \mathrm{Sym}(X^r)$, sending $a_1 a_2 \dots a_n \mapsto a_1 a_2 \dots a_n x^{-1}$ if $a_n \not = x$, and $a_1 a_2 \dots a_{n-1} x \mapsto a_1 a_2 \dots a_{n-1}$.

Recall that each $\rho_x$ lies in $\mathrm{Sym}(X^r)$, so each is a [-499] from $X^r$ to $X^r$.
We specify it by stating what it does to every element of $X^r$ (that is, to every freely reduced word over $X$).

We first specify what it does to those words which don't end in $x^{-1}$: $\rho_x$ simply appends an $x$ to such words.
We then specify it to the remaining words, those which do end in $x^{-1}$: then $\rho_x$ just removes the $x^{-1}$.

It's easy to check that if $\rho_x$ is given a freely-reduced word as input, then it produces a freely-reduced word as output, because the only change to the word is at the end and we make sure to provide a separate definition if $x$ is to be cancelled.
Therefore each $\rho_x$ is a function $X^r \to X^r$.

Then we do it all again for all the inverses $x^{-1}$, creating the functions $\rho_{x^{-1}}$; and finally, we add in the [-54p], denoted $\rho_{\varepsilon}$, which simply returns its input unchanged.

Notice that the $\rho_x$ and $\rho_{x^{-1}}$ are all indeed bijective (and therefore members of $\mathrm{Sym}(X^r)$), because in fact $\rho_x$ and $\rho_{x^{-1}}$ are [4sn inverse] to each other (each cancelling off what the other did), and a function with an inverse is bijective.

So, we've defined the free group as a certain subgroup of the symmetric group.
Remember that the subgroup has as its group operation "function composition"; so $\rho_x \cdot \rho_y = \rho_x \circ \rho_y$, for instance.
We will write $\rho_x \rho_y$ for this, omitting the group operation.

Something key to notice is that if we apply $\rho_{a_n} \rho_{a_{n-1}} \dots \rho_{a_1}$ to the empty word $\varepsilon$, we get $$\rho_{a_n} \rho_{a_{n-1}} \dots \rho_{a_1}(\varepsilon) = \rho_{a_n} \rho_{a_{n-1}} \dots \rho_{a_3}(\rho_{a_2}(a_1)) = \rho_{a_n a_{n-1} \dots a_3}(a_1 a_2) = \dots = a_1 a_2 \dots a_n$$
if $a_1 a_2 \dots a_n$ is a freely reduced word.
(Indeed, if the word is freely reduced then none of the successive $\rho_{a_i}, \rho_{a_{i+1}}$ can have cancelled each other's effect out, so every application of a $\rho_{a_i}$ must be appending a letter.)
Hence we might hope to have captured the freely reduced words in our subgroup.

# The formal definition is the same as the intuitive definition

We'll show that there is a bijection between the free group and the set of reduced words, by "converting" each reduced word into a corresponding member of the free group.

Take a reduced word, $w = a_1 a_2 \dots a_n$, and produce the member of the free group (that is, the function) $\rho_{a_1} \rho_{a_2} \dots \rho_{a_n}$. %%note:Recall that the group operation here is composition of functions, so this is actually the function $\rho_{a_1} \circ \rho_{a_2} \circ \dots \circ \rho_{a_n}$.%%
This really does produce a member of the free group (i.e. of the subgroup of the symmetry group), because each $a_i$ is an element of $X \cup X^{-1}$ and we have already specified how to make $\rho_{a_i}$ from such an element.

Now, we claim that in fact this map is injective: that is, we can't take two words $a_1 a_2 \dots a_n$ and $b_1 b_2 \dots b_m$ and produce the same member of the free group.
(That is, we show that $\rho_{a_1} \rho_{a_2} \dots \rho_{a_n} = \rho_{b_1} \rho_{b_2} \dots \rho_{b_m}$ implies $a_1 \dots a_n = b_1 \dots b_m$.)
Indeed, if the two functions ("elements of the free group") are equal, then they must in particular do the same thing when they are applied to the empty word $\varepsilon$.
But by the "key notice" above, when we evaluate $\rho_{a_1} \rho_{a_2} \dots \rho_{a_n}$ at the empty word, we get $a_n a_{n-1} \dots a_2 a_1$; and when we evaluate $\rho_{b_1} \rho_{b_2} \dots \rho_{b_m}$ at the empty word, we get $b_m b_{m-1} \dots b_2 b_1$; so the two words must be equal after all. [todo: we've got this backwards]

Finally, the map is surjective: we can make any member of the free group by "converting the appropriate reduced word into a function".
Indeed, the free group is generated by the $\rho_x$ and $\rho_{x^{-1}}$ for $x \in X$, so every element is some $\rho_{x_1} \dots \rho_{x_n}$ for some selection of $x_1, \dots, x_n \in X \cup X^{-1}$.
Note that $x_1 \dots x_n$ need not necessarily be freely reduced as a word at the moment; but if it is indeed not freely reduced, so some $x_i, x_{i+1}$ cancel each other out, then removing that pair completely doesn't change the function $\rho_{x_1} \dots \rho_{x_n}$.
For example, $\rho_{x_1} \rho_{x_1^{-1}} \rho_{x_2} = \rho_{x_2}$.
Hence the process of "performing one step of a free reduction" (i.e. removing a cancelling pair) doesn't change the member of the free group as a function; and since each such removal makes the word shorter, it must eventually terminate.
It remains to show that it doesn't matter in what order we remove the cancelling pairs; but that is immediate because we've already shown that our "conversion" process is injective: we started with a member of the free group, so if it corresponds to a freely reduced word then it corresponds to a *unique* freely reduced word.
Since we've just shown that it does indeed correspond to a freely reduced word (by repeatedly removing cancelling pairs), we are done.

The above shows that the free group can be considered just to be the set of reduced words.


Operations in Set theory

An operation in [3jz set] theory is a [-3jy] of two sets, that returns a set.

Common set operations include [5s8 Union ($\cup$)], [5sb Intersection ($\cap$)], [5sc Relative complement ($\setminus$)], and [3xb Cartesian product ($\times$)].

![illustrations of the output of four basic set operations](https://imgh.us/basic_set_operations_1.svg)


Absolute Complement

The complement $A^\complement$ of a set $A$ is the set of all things that are not in $A$. Put simply, the complement is its opposite.

Where $U$ denotes the universe, $A^\complement = U \setminus A$. That is, $A^\complement$ is the [set_relative_complement Relative complement] of $U$ and $A$.


Union

The union of two sets $A$ and $B$, denoted $A \cup B$, is the set of things which are either in $A$ or in $B$ or both.

![illustration of the output of the union operation](https://imgh.us/set_union.svg)

Formally stated, where $C = A \cup B$

$$x \in C \leftrightarrow (x \in A \lor x \in B)$$

That is, [46m] $x$ is in the union $C$, then either $x$ is in $A$ or $B$ or possibly both.

%%todo: more lengthy explanation for [1r6] level%%

# Examples

 - $\{1,2\} \cup \{2,3\} = \{1,2,3\}$
 - $\{1,2\} \cup \{8,9\} = \{1,2,8,9\}$
 - $\{0,2,4,6\} \cup \{3,4,5,6\} = \{0,2,3,4,5,6\}$
 - $\mathbb{R^-} \cup \mathbb{R^+} \cup \{0\} = \mathbb{R}$ (In other words, the union of the negative reals, the positive reals and zero make up all of the [4bc real numbers].)


Intersection

The intersection of two sets $A$ and $B$, denoted $A \cap B$, is the set of elements which are in both $A$ and $B$.

![illustration of the output of an intersection](https://imgh.us/set_intersection_1.svg)

Formally stated, where $C = A \cap B$

$$x \in C \leftrightarrow (x \in A \land x \in B)$$

That is, [46m] $x$ is in the intersection $C$, then $x$ is in $A$ and $x$ is in $B$.

For example,

 - $\{1,2\} \cap \{2,3\} = \{2\}$
 - $\{1,2\} \cap \{8,9\} = \{\}$
 - $\{0,2,4,6\} \cap \{3,4,5,6\} = \{4,6\}$


Relative complement

The relative complement of two sets $A$ and $B$, denoted $A \setminus B$, is the set of elements that are in $A$ while not in $B$.

![illustration of the output of a relative complement](https://imgh.us/set_relative_complement.svg)

Formally stated, where $C = A \setminus B$

$$x \in C \leftrightarrow (x \in A \land x \notin B)$$

That is, [46m] $x$ is in the relative complement $C$, then $x$ is in $A$ and x is not in $B$.

For example,

 - $\{1,2,3\} \setminus \{2\} = \{1,3\}$
 - $\{1,2,3\} \setminus \{9\} = \{1,2,3\}$
 - $\{1,2\} \setminus \{1,2,3,4\} = \{\}$

If we name the set $U$ as the set of all things, then we can define the [5s7 Absolute complement] of the set $A$, $A^\complement$, as $U \setminus A$


Cantor-Schröder-Bernstein theorem

[summary: The Cantor-Schröder-Bernstein theorem tells us that if one [3jz set] is [4w5 smaller-than-or-equal-to] another, and the other is smaller-than-or-equal-to the one, then they are the same size. That is, "comparison of set sizes" behaves itself, analogously to the fact in the [45h natural numbers] that it can't be the case that both $1 < 2$ and $2<1$.]

[summary(Technical): The Cantor-Schröder-Bernstein theorem states that the [class_set_theory class] of [4w5 cardinals] forms a [540 total order]. (Though not a totally ordered *set*, because [cardinals_form_a_proper_class there is no set of all cardinals]).]

Recall that we say a [3jz set] $A$ is *smaller than or equal to* a set $B$ if there is an [4b7 injection] from $A$ to $B$.
The set $A$ *has the same [4w5 size]* as $B$ if there is a [499 bijection] between $A$ and $B$.

The Cantor-Schröder-Bernstein theorem states that if $A$ is smaller than or equal to $B$, and $B$ is smaller than or equal to $A$, then $A$ and $B$ have the same size.
This tells us that the arithmetic of [4w5 cardinals] is well-behaved in that it behaves like a [540 total order].
It is similar to the arithmetic of the [45h natural numbers] in that it can never be the case that simultaneously $a < b$ and $b < a$.

# Proofs

There are several proofs, some concrete and some less so.

## Concrete proof

A clear explanation of the intuition of this proof [has been written](https://www.math.brown.edu/~res/infinity.pdf) by Richard Evan Schwartz; see page 61 of the linked PDF, or search on the word "dog" (which appears in the first page of the explanation).
[comment: The intuition is Math0 suitable.]

Let $f: A \to B$ and $g: B \to A$ be injections; we will define a bijective function $h: A \to B$.

Because $f$ is injective, if $f$ ever hits $b$ (that is, if there is $a \in A$ such that $f(a) = b$) then it is possible to define $f^{-1}(b)$ to be the *unique* $a \in A$ such that $f(a) = b$; similarly with $g$.
The slogan is "if $f^{-1}(a)$ exists, then it is well-defined: there is no leeway about which element we might choose to be $f^{-1}(a)$".

Fix some $a \in A$, and consider the sequence $$\dots, f^{-1}(g^{-1}(a)), g^{-1}(a), a, f(a), g(f(a)), \dots$$
Now, this sequence might not extend infinitely to the left; it may not even get past $a$ to the left (if $g^{-1}(a)$ doesn't exist, for instance). %%note:On the other hand, perhaps the sequence does extend infinitely to the left.%%
Also, the sequence might duplicate elements: it might be the case that $gfgf(a) = a$, for instance. %%note:And maybe there is a repeat somewhere to the left, too.%%

Similarly, we can fix some $b \in B$, and consider $$\dots g^{-1} f^{-1}(b), f^{-1}(b), b, g(b), f(g(b)), \dots$$

Every element of $A$, and every element of $B$, appears in one of these chains.
Moreover, if $a \in A$ appears in two different chains, then in fact the two chains are the same %%note:Though maybe we're looking at a different place on the same chain. If we compare the $g^{-1} f^{-1}(b)$-chain with the $b$-chain, we see they're the same chain viewed two different ways: one viewing is two places offset from the other viewing.%%, because each element of the chain specifies and is specified by the previous element in the chain (if it exists) and each element of the chain specifies and is specified by the next element of the chain.

So every element of $A$ and every element of $B$ is in exactly one chain.
Now, it turns out that there are exactly four distinct "types" of chain.

- It could extend infinitely in both directions without repeats. In this case, we define $h(a) = f(a)$ for each element of $A$ in the chain. (Basically assigning to each element of $A$ the element of $B$ which is next in the chain.)
- It could extend infinitely off to the right, but it has a hard barrier at the left, and has no repeats: the chain stops at an element of $A$. In this case, again we define $h(a) = f(a)$ for each element of $A$ in the chain. (Again, basically assigning to each element of $A$ the element of $B$ which is next in the chain.)
- It could extend infinitely off to the right, but it has a hard barrier at the left, and has no repeats: the chain stops at an element of $B$. In this case, we define $h(a) = g^{-1}(a)$ for each element of $A$ in the chain. (Basically assigning to each element of $A$ the element of $B$ which is *previous* in the chain.)
- It could have repeats. Then it must actually be a cycle of even length (unrolled into an infinite line), because each element of the chain only depends on the one before it, and because we can't have two successive elements of $A$ (since the elements are alternating between $A$ and $B$). In this case, we define $h(a) = f(a)$ for each element of $A$ in the chain. (Basically assigning to each element of $A$ the element of $B$ which is next in the chain.)

Have we actually made a bijection?
Certainly our function is well-defined: every element of $A$ appears in exactly one chain, and we've specified where every element of $A$ in any chain goes, so we've specified where every element of $A$ goes.

Our function is [4bg surjective], because every element of $B$ is in a chain; if $b \in B$ has an element $a$ of $A$ before it in its chain, then we specified that $h$ takes $a$ to $b$, while if $b \in B$ is at the leftmost end of its chain, we specified that $h$ takes $g(b)$ (that is, the following element in the chain) to $b$.

Our function is injective.
Since the chains don't overlap, and the first three cases of "what a chain might look like" have no repeats at all, the only possible way an element of $B$ can be hit twice by $h$ is if that element lies in one of the cyclical chains.
But then to elements of $A$ in that chain, $h$ assigns the following element of $B$; so $b \in B$ is hit only by the preceding element of $A$, which is the same in all cases because the chain is a cycle.
%%note:The picture in the linked intuitive document makes this much clearer.%%

## Proof from the [knaster_tarski_theorem Knaster-Tarski theorem]
This proof is very quick, but almost completely opaque.
It relies on the [knaster_tarski_theorem Knaster-Tarski fixed point theorem], which states that if $X$ is a [complete_poset complete poset] and $f: X \to X$ is [order_preserving_map order-preserving], then $f$ has a [-fixed_point] (i.e. $x$ such that $f(x) = x$).

Let $f: A \to B$ and $g: B \to A$ be injective.

We are looking for a [set_partition partition] $P \cup Q$ of $A$, and a partition $R \cup S$ of $B$, such that $f$ is injective from $P$ to $R$, and $g$ is injective from $S$ to $Q$.
(Then we can just define our bijection $A \to B$ by "do $f$ on $P$, and do $g^{-1}$ on $Q$".)

Now, the function $P \mapsto A \setminus g(B \setminus f(P))$ is order-preserving from the [-power_set] $\mathcal{P}(A)$ to $\mathcal{P}(A)$ (ordered by inclusion), because there is an even number of complements.

But $\mathcal{P}(A)$ is complete as a poset ([power_set_poset_is_complete proof]), so by Knaster-Tarski there is a set $P$ such that $P = A \setminus g(B \setminus f(P))$.

This yields our partition as required. [todo: spell this out]


Well-defined

[summary: "Well-defined" is a slightly fuzzy word in mathematics. An object is said to be "well-defined" if it has been given a definition that is completely unambiguous, can be executed without regard to any arbitrary choices the mathematician might make, or generally is crisply defined.]

"Well-defined" is a slightly fuzzy word in mathematics.
Broadly, an object is said to be "well-defined" if it has been given a definition that is completely unambiguous, can be executed without regard to any arbitrary choices the mathematician might make, or generally is crisply defined.

(The [Wikipedia page](https://en.wikipedia.org/wiki/Well-defined) on well-definedness contains many examples for those who are more comfortable with mathematical notation.)

# Specific instances

## Functions
One of the most common uses of the phrase "well-defined" is when talking about [3jy functions].
A function is **well-defined** if it really is a bona fide function.
This usually manifests itself as the following:

> Whenever $x=y$, we have $f(x) = f(y)$: that is, the output of the function doesn't depend on how we specify the input to the function, only on the input itself.

This property is often pretty easy to check.
For instance, the function from [45h $\mathbb{N}$] to itself given by $n \mapsto n+1$ is "obviously" well-defined: it's trivially obvious that if $n=m$ then $f(n) = f(m)$.

However, sometimes it is not so easy.
The function $\mathbb{N} \to \mathbb{N}$ given by "take the number of prime factors" is not obviously well-defined, because it could in principle be the case that some number $n$ is equal to both $p_1 p_2 p_3$ and $q_1 q_2$ for some primes $p_1, p_2, p_3, q_1, q_2$; then our putative function might plausibly attempt to output either $3$ or $2$ on the same natural number input $n$, so the function would not be well-defined.
(It turns out that there is a non-trivial theorem, the [5rh Fundamental Theorem of Arithmetic], guaranteeing that this function *is* in fact well-defined.)

Well-definedness in this context comes up very often when we are attempting to take a [quotient_by_equivalence_relation quotient].
The fact that we can take the quotient of a [-3jz] $X$ by an [-53y] $\sim$ is tantamount to saying:

> The function $X \to \frac{X}{\sim}$, given by $x \mapsto [x]$ the equivalence class of $X$, is well-defined.

---

Another, different, way a function could fail to be well-defined is if we tried to take the function $\mathbb{N} \to \mathbb{N}$ given by $n \mapsto n-5$.
This function is unambiguous, but it's not well-defined, because on the input $2$ it tries to output $-3$, which is not in the specified [3lg codomain].


Needs splitting by mastery

Readers who come to Arbital pages with different levels of mathematical background require different pages to explain the same concept. Sometimes a page is written in a way that seems to cater for multiple audiences (e.g. having a careful wordy explanation designed for people with little background, then throwing out formulas and proofs that could only be followed by someone with much more background). In these cases, it's often best to split the page into multiple [17b lenses], designed for different [52x math level] audiences.


Proof of Rice's theorem

[summary: This lens proves [5mv Rice's theorem] via a fixed-point theorem on computable functions.]

Recall the formal statement of [5mv Rice's theorem]:

> We will use the notation $[n]$ for the $n$th [5pd Turing machine] under some fixed [description_number numbering system].
Each such machine induces a [-3jy], which we will also write as $[n]$ where this is unambiguous due to context; then it makes sense to write $[n](m)$ for the value that machine $[n]$ outputs when it is run on input $m$.

> Let $A$ be a non-empty, proper %%note:That is, it is not the entire set.%% subset of $\{ \mathrm{Graph}(n) : n \in \mathbb{N} \}$, where $\mathrm{Graph}(n)$ is the [graph_of_a_function graph] of the [-5p2] computed by $[n]$, the $n$th Turing machine.
Then there is no Turing machine $[r]$ such that:

> - $[r](i)$ is $1$ if $\mathrm{Graph}(i) \in A$
> - $[r](i)$ is $0$ if $\mathrm{Graph}(i) \not \in A$.

We give a proof that is (very nearly) constructive: one which (if we could be bothered to work it all through) gives us an explicit example %%note:Well, very nearly; see the next note.%% of a [5pd Turing machine] whose "am-I-in-$A$" nature cannot be determined by a Turing machine.
%%note:It's only "very nearly" constructive. It would be *actually* constructive if we knew in advance a specific example of a program whose function is in $A$, and a program whose function is in $B$. The proof here assumes the existence of a program of each type, but ironically the theorem itself guarantees that there is no fully-general way to *find* such programs.%%

We will present an intermediate lemma which does all the heavy lifting; this makes the actual reasoning rather unclear but very succinct, so we will also include an extensive worked example of what this lemma does for us.

# Fixed point theorem

The intermediate lemma is a certain fixed-point theorem.

> Let $h: \mathbb{N} \to \mathbb{N}$ be [total_function total] computable: that is, it halts on every input.
Then there is $n \in \mathbb{N}$ such that $\mathrm{Graph}(n) = \mathrm{Graph}(h(n))$. %%note:And, moreover, we can actually *find* such an $n$.%%

That is, the "underlying function" of $n$ - the partial function computed by $[n]$ - has the same output, at every point, as the function computed by $[h(n)]$.
If we view $h$ as a way of manipulating a program (as specified by its [-description_number]), then this fixed-point theorem states that we can find a program whose underlying function is not changed at all by $h$.

The proof of this lemma is quite simple once the magic steps have been discovered, but it is devilishly difficult to intuit, because it involves two rather strange and confusing recursions and some self-reference.

Recall the [translation_lemma $s_{mn}$ theorem], which states that there is a total computable function $S$ of two variables $m, n$ such that for every $e \in \mathbb{N}$, we have $[e](m, n) = [S(e,m)](n)$: that is, there is a total computable way $S$ of [-50p] computable functions.
(Strictly speaking, our Turing machines only take one argument. Therefore we should use a computable pairing scheme such as [cantor_pairing_function Cantor's pairing function], so that actually $[e](m,n)$ should be interpreted as $[e](\mathrm{pair}(m, n))$.)

Then the function which takes the pair $(e, x)$ and outputs the value of $[ h(S(e,e)) ](x)$ is computable, so it has a description number $a$, say.
%%note:This is the first strange part: we are treating $e$ both as a description number, and as an input to $[e]$, when we consider $S(e,e)$.%%

Now we claim that $S(a, a)$ is the $n$ we seek. %%note:This is the second strange part, for the same reason as $S(e,e)$ was the first; but this one is even worse, because the definition of $a$ already involves a weird recursion and we've just added another one on top.%%
Indeed, for any $x$, $[n](x) = [S(a,a)](x)$ by definition of $n$; this is $[a](a, x)$ by the $s_{mn}$ theorem; this is $[h(S(a,a))](x)$ by definition of $[a]$; and that is $[h(n)](x)$ by definition of $n$.

Therefore $[n](x) = [h(n)](x)$, so we have found our fixed point.

%%hidden(Worked example):
[todo: I'm not clever enough to check this properly, but someone should]

Suppose our description numbering scheme is just "expand $n$ as a number in base $128$, and interpret the result as an [ASCII](https://en.wikipedia.org/wiki/ASCII) %%note:This is a standard, agreed-upon method of turning a number between $0$ and $128$ into a character.%% string; then interpret that string as [Python](https://en.wikipedia.org/wiki/Python_(programming_language)) code".

Then our function $h$, whatever it may be, can be viewed just as transforming Python code.

Suppose $h$ does nothing more than insert the following line of code as the second line of its input:

	x = 0

So, for instance, it takes the string

	x = 1
	print(x)

and returns

	x = 1
	x = 0
	print(x)

thereby changing the function computed from "return the constant $1$" to "return the constant $0$", in this case.
Note that many other functions will not change at all: for example, those which don't contain a variable $x$ in the first place will be unchanged, because all the modification does is add in an initialisation of a variable which will never subsequently be used.

The fixed-point theorem guarantees that there is indeed a Python program which will not change at all under this modification (though in this case it's very obvious).
In fact the theorem *constructs* such a program; can we work out what it is?

First of all, $S(m, n)$ can be implemented as follows.
We will take our Python code to be written so that their input is given in the variable `r1`, so $[e](5)$ is simply the Python code represented by $e$ but where the code-variable `r1` is initialised to $5$ first; that is, it can be found by prepending the line `r1 = 5` to the code represented by $e$.
Then we will assume that Python comes with a function `eval` (corresponding to $S$) which takes as its input a string %%note:The string is standing in place of $m$, but we have just skipped the intermediate step of "unpack the integer into a string" and gone straight to assuming it is a string.%% and another argument with which `eval` initialises the variable `x` before running the string as a Python program in a separate instance of Python:

	eval("print(r1)", 5)  # does nothing other than print the number 5
	eval("print(y)", 5)  # throws an error because `y` is not defined when it comes to printing it
	eval("print(6)", 5)  # prints 6, ignoring the fact that the variable `r1` is equal to `5` in the sub-instance

Remember, our proof of the fixed point theorem says that the program we want has code $S(a, a)$, where $a$ takes a pair $(e, x)$ as input, and outputs $[h(S(e,e))](x)$.
What is $a$ specifically here?
Well, on the one hand we're viewing it as a string of code (because it comes as the first argument to $S$), and on the other we're viewing it as an integer (because it also comes as the second argument to $S$).

As code, `a` is the following string, where `h` is to be replaced by whatever we've already decided $h$ is:

	eval("r1 = e; h(eval(r1, str_as_int(r1)))", x)

We are assuming the existence of a function `str_as_int` which takes an ASCII string and returns the integer whose places in base 128 are the ASCII for each character of the string in turn.

For example, we have $h$ inserting the line `x = 0` as the second line, so our `a` is:

	eval("r1 = e; x = 0; eval(r1, str_as_int(r1))", x)

As a number, `a` is just the ASCII for this, interpreted in base 128 (i.e. a certain number which in this case happens to have 106 digits, which is why we don't give it here).

The claim of the fixed-point theorem, then, is that the following program is unchanged by $h$:

	eval("eval(\"r1 = e; x = 0; eval(r1, str_as_int(r1))\", x)", str_to_int("eval(\"r1 = e; x = 0; eval(r1, str_as_int(r1))\", x)"))

You may recognise this as a [322 quining] construction.
%%

# Deducing Rice's theorem from the fixed point theorem

Finally, Rice's theorem follows quickly: suppose we could decide in general whether $\mathrm{Graph}(n) \in A$ or not, and label by $\iota$ the computable function which decides this (that is, whose value is $1$ if $\mathrm{Graph}(n) \in A$, and $0$ otherwise).

Since $A$ is nonempty and proper, there are natural numbers $a$ and $b$ such that $\mathrm{Graph}(a) \in A$ but $\mathrm{Graph}(b) \not \in A$.
Define the computable function $g$ which takes $n$ and outputs $a$ if $\iota(n) = 0$, and $b$ otherwise.
(That is, it flips its input: if its input had the property of $A$, the function $g$ outputs $b$ whose graph is not in $A$, and vice versa.
Informally, it is the program-transformer that reads in a program, determines whether the program computes a function in $A$ or not, and transforms the program into a specific canonical example of something which has the *opposite* $A$-ness status.)

By the fixed-point theorem, we can find $n$ such that $\mathrm{Graph}(n) = \mathrm{Graph}(g(n))$.

But now we can ask whether $\mathrm{Graph}(n)$ is in $A$ (and therefore whether $\mathrm{Graph}(g(n))$ is in $A$).

- If it is in $A$, then $g(n) = b$ and so $\mathrm{Graph}(g(n)) = \mathrm{Graph}(b)$ which is not in $A$.
- If it is not in $A$, then $g(n) = a$ and so $\mathrm{Graph}(g(n)) = \mathrm{Graph}(a)$ is in $A$.

We have obtained [46z contradictions] in both cases (namely that $\mathrm{Graph}(g(n))$ is both in $A$ and not in $A$), so it must be the case that $\iota$ does not exist after all.


Needs links

Some pages don't have many [17f greenlinks], making them less useful than they could be.


Algorithmic complexity

Algorithmic complexity is a formal measure of the minimum amount of information required to specify some message, binary string, computer program, classification rule, etcetera.  The algorithmic complexity or Kolmogorov complexity of a message is the number of 1s and 0s required to specify a Turing machine that reproduces the message.

(The relevant Wikipedia article is filed under [Kolmogorov complexity](http://en.wikipedia.org/wiki/Kolmogorov_complexity).  Not to be confused with "[49w computational complexity]", which isn't the same thing at all.)

A string of a million 1s has around as much K-complexity as the number 1,000,000, since getting a Turing machine to print out exactly a million 1s and then stop, is mostly a matter of encoding the number 1,000,000.  The number 1,000,000 can itself potentially be compressed further, since it's an unusually simple number of that magnitude: it's 10 to the 6th power, so if we already have a concept of 'exponentiation' or can encode it simply, we just need to encode the numbers 10 and 6, which are quite small.

When we say that a message has high Kolmogorov complexity, we mean that it *can't be compressed beyond a certain point* (unless the 'compression algorithm' is itself large and contains much of the key information baked in).  Things have high Kolmogorov complexity when they're made up of many independent facts that can't be predicted from knowing the previous facts.

Shakespeare's _Romeo and Juliet_ will compress by a lot using simple algorithms, because there are many words used more than once, and some words are much more frequent than others.  But we are exceedingly unlikely to find, anywhere in the first trillion Turing machines, any Turing machine that prints out the exact text of this play.  2^40 is greater than a trillion, so if we consider the set of all 40-bit binary strings, it's clear we can't print out *all* of them exactly using the first trillion Turing machines.  Finding a 40-bit Turing machine that printed out the exact text of _Romeo and Juliet_ would be vastly improbable.

On the other hand, it wouldn't be particularly surprising to find a small Turing machine that printed out $3\uparrow\uparrow\uparrow3$ 1s and then stopped, because the algorithm for this enormous number seems simple, and easy to encode as a computer program or Turing machine.

The algorithmic complexity of a system shouldn't be confused with the total number of visible, complicated-looking details.  The [Mandelbrot set](https://en.wikipedia.org/wiki/Mandelbrot_set) looks very complicated visually - you can keep zooming in using more and more detail - but there's a very simple rule that generates it, so we say the algorithmic complexity is very low.

As a corollary, a piece of a big-looking object can easily have more Kolmogorov complexity than the whole.  If you zoom far down into the Mandelbrot set and isolate a particular piece of the fractal, the information of that image now includes both the Mandelbrot-generating rule and also the exact location of that particular piece.

Similarly, the Earth is much more algorithmically complex than the laws of physics, and if there's a multiverse that developed deterministically out of the laws of physics, the Earth would be much more complex than that multiverse.  To print out the whole multiverse, you'd only need to start from the laws of physics and work forward; to print out *Earth in particular* you'd need a huge number of additional bits to locate Earth *inside* the multiverse.

Or more simply, we can observe that a program that prints out all possible books in order, is much simpler than a program that prints out only _Romeo and Juliet_.  To put it another way, Borge's "Library of Babel" containing every possible book has far lower algorithmic complexity than an Earth library containing only some books.  The moral is that the amount of visible stuff and its seeming surface complexity should not be confused with the notion of algorithmic complexity or theoretical compressibility.


Image requested

An editor has requested an image for this page, described in a \[todo: ].