\chapter{Rational and Real Numbers}
\label{chap:rational-and-real-numbers}

In previous chapters, we have seen how the natural numbers \li{ℕ} can be
defined as an inductive type and how the integers \li{ℤ} can be defined as a
quotient over \li{ℕ} \li{×} \li{ℕ}. In this chapter, we review the construction
of the rational numbers \li{ℚ} and the real numbers \li{ℝ}. The tools used for
these constructions are inductive types, subtypes, and quotients.

The following procedure can be used to construct types that enjoy specific
properties:
%
\begin{itemize}
\item[1.] Create a new type that is large enough to represent all elements, but
  not necessarily in a unique manner.

\item[2.] Take the quotient of this representation, equating elements as needed.

\item[3.] Define operators on the quotient type by lifting functions from the base
  type, and prove that they are compatible with the quotient relation.
\end{itemize}
%
We used this approach to construct the type \li{ℤ} in
Section~\ref{ssec:first-example-integers}. It can also be used for
\li{ℚ} and \li{ℝ}.

\section{Rational Numbers}
\label{sec:rational-numbers}

A rational number is a number that can be expressed as a fraction
\textit{n}$/$\textit{d} of integers \textit{n} and \textit{d}, where \textit{d}
$\neq$ 0:
%
\lstinputlisting[linerange=fraction-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
The number \textit{n} is called the numerator, and the number \textit{d} is
called the denominator. The representation of a rational number as a fraction
is not unique. For example, the rationals 1$/$2, 2$/$4, and $-$1$/-$2 are all
equal. This representation as a fraction will serve as the base type of which
we will take the quotient.

Two fractions \textit{n}$_\text{1}/$\textit{d}$_\text{1}$ and
\textit{n}$_\text{2}/$\textit{d}$_\text{2}$ represent the same rational
number if the ratio between numerator and denominator are the same:
\textit{n}$_\text{1}$ $*$ \textit{d}$_\text{2}$ $=$
\textit{n}$_\text{2}$ $*$ \textit{d}$_\text{1}$.
To construct the quotient over the type \li{Fraction} with respect to this
relation, we show that the relation is an equivalence relation. This is
achieved by declaring \li{Fraction} an instance of the \li{Setoid} type class:
%
\lstinputlisting[linerange=namespace_fraction-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=fraction_setoid-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=end_fraction-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
Then we can define the type of rationals as the quotient over this setoid:
%
\lstinputlisting[linerange=rat-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
To define zero, one, addition, multiplication, and other operations, we first
define them on the \li{Fraction} type. To add two fractions, we convert them
to a common denominator and add the numerators. The easiest common
denominator to use is simply the product of the two denominators:
%
\lstinputlisting[linerange=namespace_fraction-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=has_add_fraction-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
We register these operations as instances of the syntactic type
classes such as \li{Add} to be able to use convenient notation such as
\li{+} on \li{Fraction}. Similarly, we define zero as \li{0 := 0/1}, one as
\li{1 := 1/1}, and multiplication as the pairwise multiplication of
numerators and denominators.

To lift these operations to the type of rational numbers, \li{Rat}, we must
prove them compatible with~\li{≈}:
%
\lstinputlisting[linerange=setoid_fraction-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=end_fraction-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
Then we can use
\li{Quotient.lift}(\li{₂}) to define the \hbox{\li{Rat}} operations, and we can
instantiate the relevant syntactic type classes, such as
%
\lstinputlisting[linerange=namespace_rat-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=has_add_rat-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=end_rat-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
From here, we can proceed and prove all the properties needed to make \li{Rat}
an instance of \li{Field}.
% TODO: Reintroduce? or, if we are more ambitious, \li{DiscreteLinearOrderedField}.

\paragraph{Alternative Definitions of the Rational Numbers}

In \li{mathlib}, another approach is used to define the rationals. The type
\li{Rat} is defined as a subtype of \hbox{\li{Fraction},} with the requirement
that the denominator is positive and that the numerator and the denominator
are coprime (i.e., they have no common divisors except 1 and $-$1):
%
\lstinputlisting[linerange=alternative.rat_def-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
This is an instance of the general strategy described in
Section~\ref{ssec:alternative-definitions-via-normalization-and-subtyping}. With
this approach, no quotient is required, computation is more efficient, and more
properties are syntactic equalities up to computation. A drawback is that
function definitions become more complicated due to the need to normalize
fractions.

\section{Real Numbers}
\label{sec:real-numbers}

Some sequences of rational numbers seem to converge because the numbers in the
sequence get closer and closer to each other and yet do not converge to a
rational number. The sequence
%
\begin{align*}
    \textit{a}_\text{0} &= \text{1} \\
    \textit{a}_\text{1} &= \text{1.4} \\
    \textit{a}_\text{2} &= \text{1.41} \\
    \textit{a}_\text{3} &= \text{1.414} \\
    \textit{a}_\text{4} &= \text{1.4142} \\[-\jot]
	& \;\,\,\vdots
\end{align*}
%
where $\textit{a}_\textit{n}$ is the largest number with \textit{n}
digits after the decimal point
such that $\textit{a}_\textit{n}^\text{2} < \text{2}$, is such a sequence. It
seems to converge because each $\textit{a}_\textit{n}$ is at most
$\text{10}^{-\textit{n}}$ away from any of the following numbers, but the limit
is $\sqrt{\text{2}} \notin \mathbb{Q}$. In that sense, the rational numbers are
incomplete, and the reals are their \emph{completion}. To construct the reals,
we need to fill in the gaps that are revealed by these sequences that seem to
converge but do not.

Cauchy sequences capture the notion of a sequence that seems to converge. A
sequence $\textit{a}_\text{0}, \textit{a}_\text{1}, \dots$ is \emph{Cauchy} if
for any $\varepsilon > \text{0}$, there exists an $\textit{N} \in \mathbb{N}$
such that for all $\textit{m} \geq \textit{N}$, we have
$\left|\textit{a}_\textit{N} - \textit{a}_\textit{m} \right| < \varepsilon$. In
other words, no matter how small we choose $\varepsilon$, we can always find a
point in the sequence from which all following numbers deviate by less
than~$\varepsilon$.

We formalize sequences of rational numbers as functions \li{f : ℕ → ℚ} and
denote the absolute value $\left|\phantom{x} \right|$ by \li{abs}. This
yields the following Lean definition of Cauchy sequences:
%
\lstinputlisting[linerange=IsCauchySeq-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
Not every sequence is a Cauchy sequence:
%
\lstinputlisting[linerange=id_not_CauchySeq-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}

We define a type of Cauchy sequences as a subtype:
%
\lstinputlisting[linerange=CauchySeq-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
It will be convenient to have an auxiliary function that extracts the actual sequence from a \li{CauchySeq}:
%
\lstinputlisting[linerange=seqOf-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}

The basic idea of the construction is to represent the real numbers by
Cauchy sequences. Each Cauchy sequence represents the real number that is its
limit; for example, the sequence $\textit{a}_\textit{n} = \text{1}/\textit{n}$
represents the real number 0, and the sequence 1, 1.4, 1.41, {\dots}\
represents the real number $\sqrt{\text{2}}$.

Two different Cauchy sequences can represent the same real number; for example,
the sequence
$\textit{a}_\textit{n} = \text{1}/\textit{n}$ and the constant sequence
$\textit{b}_\textit{n} = \text{0}$ both represent $\text{0}$. Therefore, we
need to take the quotient over sequences representing the same real number. Two
sequences represent the same real number when their difference converges to
zero:
%
\lstinputlisting[linerange=namespace_CauchySeq-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=CauchySeq_equiv-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=end_CauchySeq-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
Using this \li{Setoid} instance, we can now define the real numbers:
%
\lstinputlisting[linerange=real-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}

As with the rational numbers, we need to define zero, one, addition,
multiplication, and other operators. We define them on \li{CauchySeq} first and
lift them to \li{Real} afterwards. For the constants \li{0} and \li{1}, we can
define them simply as constant sequences. Any constant sequence is a Cauchy
sequence:
%
\lstinputlisting[linerange=namespace_CauchySeq-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=const-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=end_CauchySeq-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
We can declare \li{Real} instances of the syntactic type classes
\li{Zero} and \li{One}:
%
\lstinputlisting[linerange=namespace_real-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=has_zero_real-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=end_real-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
Defining addition %(and, similarly, multiplication)
of real numbers requires a
little more effort. We define addition on Cauchy sequences by adding the
elements of the sequence pairwise:
%
\lstinputlisting[linerange=namespace_CauchySeq-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=CauchySeq_has_add-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
This definition requires a proof that the result is a Cauchy sequence, given
that \li{f}~and~\li{g} are Cauchy sequences. It is omitted, hence the \li{sorry}.

Next, we need to show that this addition is compatible with \li{≈}:
%
\lstinputlisting[linerange=CauchySeq_Setoid_add-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=end_CauchySeq-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
%
To prove that \li{f + g ≈ f' + g'}, we are given an \li{ε₀ > 0} and must show
that there exists a number \li{N} such that
%
\[\hbox{\tabletsmall\lic{∀m, m ≥ N → abs (seqOf (f + g) m - seqOf (f' + g') m) < ε₀}}\]
%
To obtain \li{N}, we use \li{f ≈ f'} and \li{g ≈ g'}. The
equivalence \li{f ≈ f'} gives us for any \li{ε >} \li{0} a number \li{Nf} such that
\hbox{\li{abs (seqOf f m - seqOf}} \li{f' m) < ε} for all \li{m ≥ Nf}. The fact \li{g ≈ g'}
gives us a number \li{Ng} with a similar property. For the calculations to work
out in the end, we take the numbers \li{Nf} and \li{Ng} for \li{ε} \li{:=}
\li{ε₀ / 2}. Then we choose \li{N} to be the maximum of \li{Nf} and \li{Ng}, so
that we get the inequalities for any \li{m ≥ N}. The \li{calc} block
at the end of the proof establishes that, for all \li{m ≥ N},
%
\[\hbox{\lic{abs (seqOf (f + g) m - seqOf (f' + g') m) < ε₀}}\]

Having shown that addition on Cauchy sequences is compatible with \li{≈},
we can define addition on \li{Real}:
%
\lstinputlisting[linerange=namespace_real-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=has_add_real-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}
\betweenlistings
\lstinputlisting[linerange=end_real-q]{../lean/LoVe/LoVe14_RationalAndRealNumbers_DemoMaster.lean}

We could continue like this with multiplication and other operators.
In summary, real numbers are defined as a quotient over Cauchy sequences, which
are in turn defined as a subtype of \li{ℕ → ℚ}.

\paragraph{Alternative Definitions of the Real Numbers}

In \li{mathlib}, the construction of the real numbers is essentially as
described above. Some definitions are stated in a more general fashion to
allow construction of other algebraic structures, such as the \textit{p}-adic
numbers \cite{lewis-2019}.

Alternatively, the real numbers can be defined using Dedekind cuts. A number
\li{r : ℝ} is then represented as the set of numbers \hbox{\li{x :}} \li{ℚ} such that
\li{x < r}. Another alternative, which does not depend on \li{ℚ}, is to define
\li{ℝ} using binary sequences \hbox{\li{ℕ → Bool}.} The elements of the sequence
represent the number's digits. This works particularly well if we only need the
real interval $[0, 1]$.

\section{Final Exhortation}

We have now reached the end of this guide. You now know fundamental theory and
techniques in interactive theorem proving as well as some application areas.
Although we used Lean, what you have learned should translate to other systems,
especially those based on simple or dependent type theory. And you ought to be
able to read most scientific papers in the area.

Even if you do not choose to pursue a career in theorem proving, the authors
hope you will bring proof assistants with you and use them when it makes sense,
either because of their high trustworthiness or because of their convenience for
keeping track of complex proof goals.

If you continue using Lean, the natural next step would be to familiarize
yourself with \li{mathlib} and its documentation. If you use Lean outside a
class context, you will often find yourself looking up definitions and
theorems. You will surely find the \li{#find} command useful.%
\footnote{\url{https://leanprover-community.github.io/mathlib4_docs/Mathlib/Tactic/Find.html}}
And most Lean users use the Lean Zulip chat.%
\footnote{\url{https://leanprover.zulipchat.com/}}

\section{Summary of Newly Introduced Lean Constructs}

\begingroup
\summarysize

\subsection*{Diagnostic Command}

\noindent
\begin{xltabular}{\textwidth}{@{}p{9em}X@{}}
\lic|#find| & looks up a definition or theorem by pattern matching \\
\end{xltabular}

\endgroup
