# Homework 4 -- Uniform convergence

Consider the sequence of functions $f_n : (0,\infty) \to \mathbb{R}$ given by
$$
f_n(x) = \frac{n}{nx + 1}.
$$
Prove that the sequence $(f_n)$ converges uniformly on every set of the form
$[a,\infty)$ with $a > 0$, but that the convergence is *not* uniform on
$(0,\infty)$.

**Course material.**

- Lecture 10: [pointwise and uniform convergence](../index.html#lecture10)
  (Abbott §6.2).
- Formalisation: [`theories/FunctionSequences.v`](../theories/FunctionSequences.v),
  in particular the examples `x_over_n_uniform`, `x_pow_n_not_uniform`; the
  definitions `converges_pointwise` / `converges_uniformly` are in
  [`theories/Lib/UniformConvergence.v`](../theories/Lib/UniformConvergence.v)
  and are re-exported by `FunctionSequences`.

Notes:

- Recall that `a / b` is `a * / b` for Waterproof, and that the automation needs
  help with inverses: the fact `b * / b = 1` (from `Rinv_r`, valid when
  `b ≠ 0`) is usually all it is missing.

<hint title="Import libraries (click to open/close)">

Run `dune build` once before opening this file, so that the `RUG.Analysis.*`
modules of the course library are available.

```coq
Require Import Stdlib.Reals.Reals.
Require Import Waterproof.Tactics.
Require Import Waterproof.Notations.Common.
Require Import Waterproof.Notations.Reals.
Require Import Waterproof.Notations.Sets.
Require Import Waterproof.Chains.
Require Import Waterproof.Automation.
Require Import Waterproof.Libs.Reals.ArchimedN.
Require Import RUG.Analysis.FunctionSequences.

Waterproof Enable Automation RealsAndIntegers.
Waterproof Enable Automation Intuition.

Open Scope R_scope.
Open Scope subset_scope.

Set Default Goal Selector "!".
Set Bullet Behavior "Waterproof Relaxed Subproofs".
```
</hint>

We define the sequence $(f_n)$ and the candidate limit function $g(x) = 1/x$.
```coq
Definition fn_seq : ℕ → ℝ → ℝ := fun n x => INR n / (INR n * x + 1).
Definition f_lim : ℝ → ℝ := fun x => 1 / x.
```

**Waterproof remark: natural numbers as real numbers.** Rocq keeps `ℕ` and `ℝ`
apart: `INR n` is the natural number `n` seen as a real number. The automation
knows very little about `INR`, so for some basic properties of real numbers
involving `INR n` (for instance that it is non-negative, or at least $1$) you
will need to supply facts yourself. These standard-library lemmas provide them:

- `le_INR : (n ≤ m)%nat → INR n ≤ INR m`: the inclusion `ℕ → ℝ` preserves `≤`;
- `S_INR : INR (S n) = INR n + 1`: the successor `S n` becomes `+ 1` in `ℝ`;
- `pos_INR : 0 ≤ INR n`: natural numbers are non-negative.

Use them as any other lemma, e.g. `By S_INR it holds that INR (S n) = INR n + 1.`

## Part 1: the exact value of the error

For $x > 0$ and $n \geq 1$,
$$
\frac{n}{nx+1} - \frac{1}{x} = -\frac{1}{(nx+1)x}.
$$
The statement is about a *real* number `nR` instead of `INR n`: this keeps the
algebra readable, and we will apply it with `nR := INR n`.

This identity is given to you, so that you can spend the lab on the estimates.
Note how the proof works: the automation needs `u * / u = 1` (`Rinv_r`) for
each denominator `u`; with those facts in the context, `We conclude that ...`
proves the identity. You will need the same trick later on.

```coq
Lemma diff_value (nR x : ℝ) (Hx : 0 < x) (Hn : 1 ≤ nR) :
  nR / (nR*x + 1) - 1/x = - (/ (nR*x + 1) * / x).
Proof.
It holds that nR*x + 1 > 0 as (Hpos).
By Rinv_r it holds that (nR*x + 1) * / (nR*x + 1) = 1 as (E1).
By Rinv_r it holds that x * / x = 1 as (E2).
We conclude that nR / (nR*x + 1) - 1/x = - (/ (nR*x + 1) * / x).
Qed.
```

## Part 2: a bound that does not depend on the point

Show that on $[a, \infty)$ the error is at most $\frac{1}{na^2}$:
$$
\bigg| \frac{n}{nx+1} - \frac1x \bigg| = \frac{1}{(nx+1)x} \leq \frac{1}{na^2},
$$
where the inequality holds because $(nx+1)x = nx^2 + x \geq na^2$ for $x \geq a$.

**Waterproof remark.** The automation does not find `a*a ≤ x*x` inside a bigger
goal: state it separately first. It also cannot compare inverses on its own;
supply the facts you need with
`Rinv_mult : / (r1 * r2) = / r1 * / r2` and
`Rinv_le_contravar : 0 < x → x ≤ y → / y ≤ / x`.

<hint title="💡 Hint (click to open/close)">
Use `diff_value`; the quantity inside the absolute value is negative. Then
compare the two inverses.
</hint>

```coq
Lemma diff_bound (nR x a : ℝ) (Ha : 0 < a) (Hx : a ≤ x) (Hn : 1 ≤ nR) :
  | nR / (nR*x + 1) - 1/x | ≤ 1 / (nR * (a*a)).
Proof.
```
<input-area>
```coq

```
</input-area>
```coq
Qed.
```

The following auxiliary fact, which turns "$m$ is large" into "$1/m$ is
small", is also given to you. It shows how to compare inverses with
`Rinv_lt_contravar : 0 < r1 * r2 → r1 < r2 → / r2 < / r1` and
`Rinv_inv : / / r = r`.

```coq
Lemma small_inv (m ε : ℝ) (Hε : ε > 0) (Hm : m > / ε) : 1 / m < ε.
Proof.
It holds that / ε > 0 as (Hinv).
It holds that m > 0 as (Hm0).
By Rinv_lt_contravar it holds that / m < / / ε as (Hlt).
By Rinv_inv it holds that / / ε = ε as (Einv).
We conclude that (& 1 / m = / m < / / ε = ε).
Qed.
```

## Part 3: uniform convergence on $[a,\infty)$

Now prove uniform convergence on $[a,\infty)$ for every $a > 0$. Note how the
threshold $N$ below depends on $\varepsilon$ and $a$, but *not* on $x$: that is
exactly what uniform convergence means.

**Waterproof remark.** After `Choose N1 := ...`, restate the goal with
`We need to show that ...` before using `Take`. For the facts about `INR n` you
need here, use `le_INR`, `S_INR` and `pos_INR` (see the remark after the
definition of `fn_seq`).

<hint title="💡 Hint (click to open/close)">
The threshold comes from the Archimedean property applied to
$\frac{1}{\varepsilon a^2}$; taking `N1 := S n1` also gives `1 ≤ INR n`. Then
combine Part 2 and `small_inv`.
</hint>

```coq
Lemma fn_uniform_on_ray (a : ℝ) (Ha : a > 0) :
  fn_seq _converges uniformly on_ [a, ∞) to f_lim.
Proof.
```
<input-area>
```coq

```
</input-area>
```coq
Qed.
```

## Part 4: the convergence is not uniform on $(0,\infty)$

The point $x = 1/n$ shows that no single threshold works for all of
$(0,\infty)$: there
$$
f_n(1/n) - g(1/n) = \frac{n}{2} - n = -\frac{n}{2},
\qquad\text{so}\qquad \bigg|f_n(1/n) - g(1/n)\bigg| = \frac n2 \geq \frac12 .
$$

First the computation at that point.

<hint title="💡 Hint (click to open/close)">
Simplify `m * / m` and `/ / m` first (`Rinv_r`, `Rinv_inv`). `Rabs_Ropp` and
`Rabs_pos_eq` remove the absolute value.
</hint>

```coq
Lemma gap_at_inv (m : ℝ) (Hm : 1 ≤ m) :
  | m / (m * / m + 1) - 1 / / m | ≥ 1/2.
Proof.
```
<input-area>
```coq

```
</input-area>
```coq
Qed.
```

And then the negative result. Take $\varepsilon = 1/2$ and evaluate the uniform
estimate at the point $x = 1/N$, where $N$ is the threshold that uniform
convergence would provide.

**Waterproof remark.** While deriving a contradiction `Use` is not available.
Specialise a hypothesis `H` by applying it to its arguments instead, as in
`By (H (1/2) Hhalf) it holds that ...`. Facts about `INR (S N1)` again need
`S_INR` and `pos_INR`.

<hint title="💡 Hint (click to open/close)">
Take $\varepsilon = \frac12$ and evaluate at $n = N_1 + 1$, $x = 1/(N_1+1)$,
then use `gap_at_inv`.
</hint>

```coq
Lemma fn_not_uniform : ¬ (fn_seq _converges uniformly on_ (0, ∞) to f_lim).
Proof.
```
<input-area>
```coq

```
</input-area>
```coq
Qed.
```

## Declaration of own work

Please declare that the work above is your own work by writing `Yes.` below.
In particular, this means the proofs are typed by yourself and you could
explain them when asked.
```coq
Inductive answer : Type := Yes : answer.

Definition own_work : answer. exact
```
<input-area>
```coq

```
</input-area>
```coq
Qed.
```
