The fundamental theorem of calculus, the fundamental theorem of arithmetic and the fundamental theorem of algebra are three instances of the pattern of links 4, 5 and 7 in Section 73.18: placing and then reading returns the identity, and reading and then placing is an idempotent whose fixed points are everything there is. In each case there is a reading, a placing, and one equation between them.
The sum of \(f\) is built by the iteration of link 22:
The difference reads “one more”: \(DF(n) := F(S\,n) - F(n)\). Then
so \(D \circ \Sigma = \mathrm {id}\). The defining equation of the sum is this identity. Joining the two in the other order gives an idempotent, by the computation of link 7:
By induction the middle terms cancel,
and the fixed points of \(\Sigma D\) are the \(F\) with \(F(0) = 0\).
On \(\mathbb {R}\) (link 25) let \(If(x)\) be the least upper bound of the lower sums of \(f\) from \(a\) to \(x\). Let \(f\) be continuous, and let \(m \le f \le M\) on \([x, x+h]\). Then
As \(h \to 0\) both \(m\) and \(M\) tend to \(f(x)\), so
In the other order,
The second line uses that a function with derivative \(0\) is constant, which rests on the completeness of \(\mathbb {R}\).
| Links 4 and 7 | Calculus |
placing | \(\Delta \) | \(I\) |
reading | \(\pi _1\) | \(D\) |
place, then read | \(\pi _1 \Delta = \mathrm {id}\) | \(D I = \mathrm {id}\) |
read, then place | \(e_1 = \Delta \pi _1\) | \(ID : F \mapsto F - F(a)\) |
what reading discards | the second place | the constant |
|
On pairs of positive numbers define the Euclid step
The step fixes exactly the diagonal:
Repeating the step (link 20) reaches a point \(\Delta (g)\) of the diagonal, and \(g\) is the greatest common divisor of \(a\) and \(b\). Two things are kept by every step:
The first says that the common divisors do not change. The second gives \(g = xa + yb\) (link 23).
A prime dividing a product divides a factor. Let \(p\) be prime with \(p \mid ab\) and \(p \nmid a\). Then the greatest common divisor of \(p\) and \(a\) is \(1\), and
so \(p \mid b\).
Existence. By the induction of link 21: for \(n > 1\) the least divisor \(p > 1\) of \(n\) is prime, and \(n = p \cdot m\) with \(m < n\).
Uniqueness. Suppose \(p_1 \cdots p_r = q_1 \cdots q_s\) with all factors prime. Then \(p_1 \mid q_j\) for some \(j\), so \(p_1 = q_j\). Cancelling (link 22) leaves a shorter equation of the same kind, and induction finishes.
The pattern. Write \(v_p(n)\) for the exponent of \(p\) in \(n\), the value of \(n\) read at the place \(p\). Then
Link 5 says that a pair is what its two places hold. The fundamental theorem of arithmetic says that a number is what its prime places hold. Multiplication becomes addition place by place: \(v_p(mn) = v_p(m) + v_p(n)\).
The chain continues from the Euclid step (link 19) to the fundamental theorem of arithmetic. One move is repeated: take a step, say what the step keeps, and read at the fixed point. The integers and the carried combination (links 20 and 21) are not used.
Link 30 (from 10, 12, 15): a step that shrinks reaches a fixed point. Let \(t\) be a step and \(\mu \) a size, with \(\mu (t\,x) < \mu (x)\) whenever \(t(x) \neq x\). Let \(C(k)\) say: if \(\mu (x) \leq k\) then \(k(t)(x)\) is a fixed point. By induction on \(k\):
So \(x^{*} := \mu (x)(t)(x)\) is a fixed point of \(t\). The usual strong induction comes out the same way, with \(Q(n) :=\) “\(P(m)\) for every \(m < n\)”:
Link 31 (from 12, 30): what a step keeps. If \(I(x) \Rightarrow I(t\,x)\), then \(I(x) \Rightarrow I(k(t)(x))\) for every \(k\), and so \(I(x) \Rightarrow I(x^{*})\).
Link 32 (from 19, 30, 31): the Euclid step reaches the diagonal. Let \(a, b \geq 1\) and \(\mu (a,b) := a + b\).
So the fixed points are the diagonal, and \((a+b)(u)(a,b) = (g,g)\) for some \(g \geq 1\). Two lines on divisibility:
The step keeps two things (link 31):
Here \(S\) is closed under difference when \(x \in S\) and \(x + z \in S\) give \(z \in S\).
Link 33 (from 18): primes. \(p\) is prime when \(p \geq 2\) and \(d \mid p\) gives \(d = 1\) or \(d = p\).
Link 34 (from 32, 33): Euclid’s lemma. Let \(p\) be prime, \(a, b \geq 1\) and \(p \mid a\,b\). Put \(S := \{x : p \mid x\,b\}\).
So \(p \mid a\,b\) gives \(p \mid a\) or \(p \mid b\).
Link 35 (from 18, 30, 31, 33): the least factor above \(1\). Let \(n \geq 2\). Start at \(2\) and go up by one while the number does not divide \(n\).
So \(\ell \) is prime. Write \(\mathrm {lp}(n) := \ell \).
Link 36 (from 2, 3, 5): the swap is read off the diagonal. Write a pair \(p\) twice, \(\Delta p = (p,p)\), and read one place from each copy. There are four ways.
| Reading | Result on \((x,y)\) | Name |
| \((\pi _1,\pi _2)\) | \((x,y)\) | \(\mathrm {id}\) (link 5) |
| \((\pi _2,\pi _1)\) | \((y,x)\) | \(\sigma \) |
| \((\pi _1,\pi _1)\) | \((x,x)\) | \(e_1\) (link 7) |
| \((\pi _2,\pi _2)\) | \((y,y)\) | \(e_2\) |
The swap comes from the diagonal, and the diagonal is where the swap does nothing.
Link 37 (from 13, 15, 36): the order idempotent. Choose between \(\mathrm {id}\) and \(\sigma \).
So \(\mathrm {ord}(\mathrm {ord}(x,y)) = \mathrm {ord}(x,y)\). The fixed points of \(e_1\) are sameness; the fixed points of \(\mathrm {ord}\) are order.
Link 38 (from 10, 13, 37): inserting and sorting. A list is a pair taken repeatedly. To insert \(p\), make the same choice at the head: \(p\) stays in front, or moves past the head.
Link 39 (from 30, 34, 35, 38): the fundamental theorem of arithmetic. For lists of primes put
and read a number by taking its least prime factor out, again and again (link 30):
The theorem is two lines:
(a) \(\Pi (\mathrm {read}\,n) = n\), by link 30:
(b) Multiplying is inserting: \(\mathrm {read}(p \cdot k) = \mathrm {ins}_p(\mathrm {read}\,k)\) for a prime \(p\). For \(k = 1\), \(\mathrm {read}(p) = [p] = \mathrm {ins}_p[\,]\). For \(k \geq 2\) let \(q := \mathrm {lp}(k)\), \(k = q\,k_1\) and \(\ell := \mathrm {lp}(p\,k)\).
Euclid’s lemma is used here and nowhere else.
(c) \(\mathrm {read}(\Pi \,L) = \mathrm {sort}(L)\), by induction on the list:
(d) The rest follows from the general shape of links 4 and 5.
A number from \(1\) up is one ascending list of primes: that is existence. Two lists of primes have the same product exactly when they sort to the same list: that is uniqueness up to order.
| One way round | The other way round | Fixed points | |
| Pairs (links 4, 5) | \(\pi _1 \Delta = \mathrm {id}\) | \(\Delta \pi _1 = e_1\) | the diagonal |
| Chinese remainder (link 24) | \(\mathrm {read} \circ \mathrm {place} = \mathrm {id}\) | \(\mathrm {place} \circ \mathrm {read} = \mathrm {rem}_{ab}\) | numbers below \(a\,b\) |
| Arithmetic (link 39) | \(\Pi \circ \mathrm {read} = \mathrm {id}\) | \(\mathrm {read} \circ \Pi = \mathrm {sort}\) | ascending lists of primes |
Links 30–39 are checked in one Lean 4 file, FTAChain.lean (Listing 9), run in the Lean web editor under Lean v4.37.0-rc1. The file has no imports and uses the Lean core only. Every proof is a hand-written term: no omega, no simp, no ring, no decide. Its first part, links 1–19, is taken unchanged from BetaChain.lean. The file produces 25 messages and no warnings: one for each #print axioms command, each “does not depend on any axioms” (Figure 18).
| Links | Lean names | Checked |
| 30, 31 | (the descents inside euclid_walk, lp_search, factor_exists) | in general |
| 32 | euclid_walk, dv_sub, dv_add | in general |
| 33 | Pr, dv_dec | in general |
| 34 | euclid_core | in general |
| 35 | lp_search, lp_exists, lp_prime | in general |
| 36 | swp_pair, swp_swp, swp_fix, read_12, read_11 | in general |
| 37 | ord_le, ord_gt, ord_idem | in general |
| 38 | prod_ins, sorted_ins, prod_isort, allpr_isort, sorted_isort | in general |
| 38 | isort_example | one list |
| 39 (a) | factor_exists, fta_exists | in general |
| 39 (b) | head_le | in general |
| 39 (c) | fta_unique, read_prod | in general |
| 39 (d) | isort_idem, prod_eq_of_isort, isort_eq_of_prod | in general |
| 39 | fta | in general |
What the check rests on. The file does not define \(\mathrm {read}\) as a function. It proves that for every \(n \geq 1\) there is an ascending list of primes with product \(n\) (fta_exists), and that two ascending lists of primes with the same product are the same list (fta_unique); the two are put together in fta. Statement (a) is the first of these. Statement (c) is checked in the form: an ascending list of primes with the same product as \(L\) is \(\mathrm {sort}(L)\) (read_prod). Statement (b) is not a separate theorem; its use of Euclid’s lemma is in head_le, which says that a prime dividing the product of a list of primes, all at least \(q\), is itself at least \(q\). The descents of links 30–32 are run as inductions on a bound that goes down by one, not by iterating the step \(\mu \) times. A prime is written \(j + 1 + 1\), and order and divisibility are stated through \(+\) and \(\cdot \): \(a \leq b\) is \(a + k = b\) for some \(k\), and \(d \mid n\) is \(n = q\,d\) for some \(q\). That a successor is not zero is proved from the choosing of link 13, as in BetaChain.lean.
FTAChain.lean
/-
FTAChain.lean
One connected chain from `id` to the fundamental theorem of arithmetic
(links 1-19 as in BetaChain.lean, then links 30-39).
Lean 4 core only: no imports. Every proof is a hand-written term: no
`omega`, no `simp`, no `ring`, no `decide`. Every `#print axioms` line
should report no axioms.
Proved here:
euclid_walk the Euclid step reaches the diagonal, and what it
keeps (common divisors, families closed under
difference) (link 32)
euclid_core Euclid's lemma for a prime (link 34)
lp_exists,
lp_prime the least factor above 1, and it is prime (link 35)
swp, ord the swap read off the diagonal; the order
idempotent (links 36, 37)
ins, isort inserting and sorting (link 38)
fta_exists every n >= 1 is the product of an ascending list
of primes (link 39 a)
fta_unique two ascending lists of primes with the same
product are the same list (link 39)
read_prod the ascending list with the product of L is
isort L (link 39 c)
fta existence and uniqueness in one statement
A number p is written `j + 1 + 1` where it has to be at least 2.
-/
universe u
namespace FTAChain
/-! ## Links 1-8. Places -/
def diag {A : Type u} (x : A) : A × A := (x, x)
theorem tri1 {A : Type u} (x : A) : (diag x).1 = x := rfl
def e1 {A : Type u} (p : A × A) : A × A := diag p.1
theorem e1_idem {A : Type u} (p : A × A) : e1 (e1 p) = e1 p := rfl
theorem split_idem {A B : Type u} (r : B → A) (s : A → B)
(h : ∀ x, r (s x) = x) (y : B) : s (r (s (r y))) = s (r y) :=
congrArg s (h (r y))
theorem read_inj {A B : Type u} (read : A → B) (place : B → A)
(h : ∀ n, place (read n) = n) {m n : A} (e : read m = read n) : m = n :=
Eq.trans (Eq.symm (h m)) (Eq.trans (congrArg place e) (h n))
/-! ## Links 9-12. "Do it n times"; + and * are composition -/
def iter {A : Type u} : Nat → (A → A) → A → A
| 0, _, a => a
| n + 1, f, a => f (iter n f a)
theorem iter_zero {A : Type u} (f : A → A) (a : A) : iter 0 f a = a := rfl
theorem iter_succ {A : Type u} (n : Nat) (f : A → A) (a : A) :
iter (n + 1) f a = f (iter n f a) := rfl
theorem iter_one {A : Type u} (f : A → A) (a : A) : iter 1 f a = f a := rfl
theorem iter_add {A : Type u} (f : A → A) (a : A) (m : Nat) :
∀ n : Nat, iter (m + n) f a = iter n f (iter m f a)
| 0 => rfl
| n + 1 => congrArg f (iter_add f a m n)
theorem iter_mul {A : Type u} (f : A → A) (m : Nat) :
∀ (n : Nat) (a : A), iter (m * n) f a = iter n (iter m f) a
| 0, _ => rfl
| n + 1, a =>
Eq.trans (iter_add f a (m * n) m) (congrArg (iter m f) (iter_mul f m n a))
theorem iter_fix {A : Type u} (f : A → A) (r : A) (h : f r = r) :
∀ k : Nat, iter k f r = r
| 0 => rfl
| k + 1 => Eq.trans (congrArg f (iter_fix f r h k)) h
theorem iter_succ' {A : Type u} (f : A → A) (a : A) :
∀ q : Nat, iter (q + 1) f a = iter q f (f a)
| 0 => rfl
| q + 1 => congrArg f (iter_succ' f a q)
/-! ### The laws of + and *, from the defining equations -/
theorem nzero_add : ∀ (a : Nat), 0 + a = a
| 0 => rfl
| a + 1 => congrArg Nat.succ (nzero_add a)
theorem nsucc_add : ∀ (a b : Nat), Nat.succ a + b = Nat.succ (a + b)
| _, 0 => rfl
| a, b + 1 => congrArg Nat.succ (nsucc_add a b)
theorem nadd_assoc : ∀ (a b c : Nat), a + b + c = a + (b + c)
| _, _, 0 => rfl
| a, b, c + 1 => congrArg Nat.succ (nadd_assoc a b c)
theorem nadd_comm : ∀ (a b : Nat), a + b = b + a
| a, 0 => Eq.symm (nzero_add a)
| a, b + 1 =>
Eq.trans (congrArg Nat.succ (nadd_comm a b)) (Eq.symm (nsucc_add b a))
theorem nadd_left_comm (a b c : Nat) : a + (b + c) = b + (a + c) :=
Eq.trans (Eq.symm (nadd_assoc a b c))
(Eq.trans (congrArg (fun t => t + c) (nadd_comm a b)) (nadd_assoc b a c))
theorem nadd_right_comm (a b c : Nat) : a + b + c = a + c + b :=
Eq.trans (nadd_assoc a b c)
(Eq.trans (congrArg (fun t => a + t) (nadd_comm b c)) (Eq.symm (nadd_assoc a c b)))
theorem mid4 (a b c d : Nat) : (a + b) + (c + d) = (a + c) + (b + d) :=
Eq.trans (nadd_assoc a b (c + d))
(Eq.trans
(congrArg (fun t => a + t)
(Eq.trans (Eq.symm (nadd_assoc b c d))
(Eq.trans (congrArg (fun t => t + d) (nadd_comm b c)) (nadd_assoc c b d))))
(Eq.symm (nadd_assoc a c (b + d))))
theorem nzero_mul : ∀ a : Nat, 0 * a = 0
| 0 => rfl
| a + 1 => nzero_mul a
theorem nsucc_mul : ∀ (a b : Nat), (a + 1) * b = a * b + b
| _, 0 => rfl
| a, b + 1 =>
Eq.trans (congrArg (fun t => t + (a + 1)) (nsucc_mul a b)) (mid4 (a * b) b a 1)
theorem nmul_comm : ∀ (a b : Nat), a * b = b * a
| a, 0 => Eq.symm (nzero_mul a)
| a, b + 1 =>
Eq.trans (congrArg (fun t => t + a) (nmul_comm a b)) (Eq.symm (nsucc_mul b a))
theorem nmul_one (a : Nat) : a * 1 = a :=
Eq.trans (show a * 1 = 0 + a from rfl) (nzero_add a)
theorem nleft_distrib (a b : Nat) : ∀ c : Nat, a * (b + c) = a * b + a * c
| 0 => rfl
| c + 1 =>
Eq.trans (congrArg (fun t => t + a) (nleft_distrib a b c))
(nadd_assoc (a * b) (a * c) a)
theorem nright_distrib (a b c : Nat) : (a + b) * c = a * c + b * c :=
Eq.trans (nmul_comm (a + b) c)
(Eq.trans (nleft_distrib c a b)
(Eq.trans (congrArg (fun t => t + c * b) (nmul_comm c a))
(congrArg (fun t => a * c + t) (nmul_comm c b))))
theorem nmul_assoc (a b : Nat) : ∀ c : Nat, a * b * c = a * (b * c)
| 0 => rfl
| c + 1 =>
Eq.trans (congrArg (fun t => t + a * b) (nmul_assoc a b c))
(Eq.symm (nleft_distrib a (b * c) b))
theorem mmid4 (a b c d : Nat) : (a * b) * (c * d) = (a * c) * (b * d) :=
Eq.trans (nmul_assoc a b (c * d))
(Eq.trans
(congrArg (fun t => a * t)
(Eq.trans (Eq.symm (nmul_assoc b c d))
(Eq.trans (congrArg (fun t => t * d) (nmul_comm b c)) (nmul_assoc c b d))))
(Eq.symm (nmul_assoc a c (b * d))))
/-! ## Link 13. Choosing -/
def sel {A : Type u} (n : Nat) (x y : A) : A := iter n (fun _ => y) x
theorem sel_zero {A : Type u} (x y : A) : sel 0 x y = x := rfl
theorem sel_succ {A : Type u} (n : Nat) (x y : A) : sel (n + 1) x y = y := rfl
/-- A successor is not zero: choosing tells them apart.
`sel` reads `True` at `0` and `False` at a successor, so an equation
`n + 1 = 0` would carry `True` to `False`.
(Proved here from `sel`, so that the library lemma
`Nat.succ_ne_zero` is not used.) -/
theorem succ_ne_zero' (n : Nat) : Nat.succ n ≠ 0 :=
fun h =>
have e1 : True = sel 0 True False := Eq.symm (sel_zero True False)
have e2 : sel 0 True False = sel (Nat.succ n) True False :=
Eq.symm (congrArg (fun t => sel t True False) h)
have e3 : sel (Nat.succ n) True False = False := sel_succ n True False
Eq.mp (Eq.trans e1 (Eq.trans e2 e3)) True.intro
/-! ## Link 14. One-before, from a step on pairs -/
def pstep (p : Nat × Nat) : Nat × Nat := (p.1 + 1, p.1)
def pre (n : Nat) : Nat := (iter n pstep (0, 0)).2
theorem pstep_fst : ∀ n : Nat, (iter n pstep (0, 0)).1 = n
| 0 => rfl
| n + 1 => congrArg Nat.succ (pstep_fst n)
theorem pre_zero : pre 0 = 0 := rfl
theorem pre_succ (n : Nat) : pre (n + 1) = n := pstep_fst n
theorem succ_inj' {m n : Nat} (h : m + 1 = n + 1) : m = n :=
Eq.trans (Eq.symm (pre_succ m)) (Eq.trans (congrArg pre h) (pre_succ n))
theorem pre_eq : ∀ k : Nat, pre k = Nat.pred k
| 0 => rfl
| k + 1 => pre_succ k
/-! ## Link 15. Subtraction: one-before, n times -/
def sb (m n : Nat) : Nat := iter n pre m
theorem sb_zero (m : Nat) : sb m 0 = m := rfl
theorem sb_succ (m n : Nat) : sb m (n + 1) = pre (sb m n) := rfl
theorem sb_add_cancel : ∀ (n m : Nat), sb (m + n) n = m
| 0, _ => rfl
| n + 1, m =>
Eq.trans
(congrArg (fun t => pre (sb t n)) (Eq.symm (nsucc_add m n)))
(Eq.trans (congrArg pre (sb_add_cancel n (m + 1))) (pre_succ m))
theorem sb_self (a : Nat) : sb a a = 0 :=
Eq.trans (congrArg (fun t => sb t a) (Eq.symm (nzero_add a))) (sb_add_cancel a 0)
theorem sb_eq (m : Nat) : ∀ n : Nat, sb m n = m - n
| 0 => rfl
| n + 1 => Eq.trans (congrArg pre (sb_eq m n)) (pre_eq (m - n))
/-- `m - (m + x) = 0`. -/
theorem sb_le_zero (m x : Nat) : sb m (m + x) = 0 :=
Eq.trans (iter_add pre m m x)
(Eq.trans (congrArg (iter x pre) (sb_self m)) (iter_fix pre 0 pre_zero x))
/-! ## Links 16-17. The subtract-step and the remainder -/
def tstep (m n : Nat) : Nat := sel (sb m n) (sb n m) n
def rem (m n : Nat) : Nat := iter n (tstep m) n
theorem tstep_small (m r k : Nat) (h : sb m r = k + 1) : tstep m r = r :=
congrArg (fun t => sel t (sb r m) r) h
theorem tstep_big (m n : Nat) (h : sb m n = 0) : tstep m n = sb n m :=
congrArg (fun t => sel t (sb n m) n) h
theorem rem_of_fix (m r : Nat) (h : tstep m r = r) : rem m r = r :=
iter_fix (tstep m) r h r
theorem rem_small (m r k : Nat) (h : sb m r = k + 1) : rem m r = r :=
rem_of_fix m r (tstep_small m r k h)
theorem rem_idem_of (m n : Nat) (h : tstep m (rem m n) = rem m n) :
rem m (rem m n) = rem m n :=
rem_of_fix m (rem m n) h
theorem rem_values :
rem 3 10 = 1 ∧ rem 3 (rem 3 10) = rem 3 10 ∧ rem 6 11 = 5 ∧ rem 5 12 = 2 :=
⟨rfl, rfl, rfl, rfl⟩
/-! ### The remainder in general -/
/-- From `m` up, one step takes `m` away. -/
theorem tstep_add (m x : Nat) : tstep m (m + x) = x :=
Eq.trans (tstep_big m (m + x) (sb_le_zero m x))
(Eq.trans (congrArg (fun t => sb t m) (nadd_comm m x)) (sb_add_cancel m x))
/-- Below `m` (that is, `r + (k+1) = m`) the step changes nothing. -/
theorem fix_of_lt (m r k : Nat) (h : r + (k + 1) = m) : tstep m r = r :=
tstep_small m r k
(Eq.trans (congrArg (fun t => sb t r) (Eq.symm h))
(Eq.trans (congrArg (fun t => sb t r) (nadd_comm r (k + 1)))
(sb_add_cancel r (k + 1))))
/-- `r` with `m` added `q` times. -/
def up (m q r : Nat) : Nat := iter q (fun x => m + x) r
theorem up_eq (m r : Nat) : ∀ q : Nat, up m q r = q * m + r
| 0 => Eq.symm (Eq.trans (congrArg (fun t => t + r) (nzero_mul m)) (nzero_add r))
| q + 1 =>
Eq.trans (congrArg (fun t => m + t) (up_eq m r q))
(Eq.trans (Eq.symm (nadd_assoc m (q * m) r))
(Eq.trans (congrArg (fun t => t + r) (nadd_comm m (q * m)))
(congrArg (fun t => t + r) (Eq.symm (nsucc_mul q m)))))
/-- Taking `m` away `q` times undoes adding it `q` times. -/
theorem down_up (m r : Nat) : ∀ q : Nat, iter q (tstep m) (up m q r) = r
| 0 => rfl
| q + 1 =>
Eq.trans (iter_succ' (tstep m) (up m (q + 1) r) q)
(Eq.trans (congrArg (iter q (tstep m)) (tstep_add m (up m q r)))
(down_up m r q))
theorem up_ge (m' r : Nat) : ∀ q : Nat, ∃ j, up (m' + 1) q r = q + j
| 0 => ⟨r, Eq.symm (nzero_add r)⟩
| q + 1 =>
match up_ge m' r q with
| ⟨j, hj⟩ =>
⟨m' + j,
Eq.trans (congrArg (fun t => (m' + 1) + t) hj)
(Eq.trans (nsucc_add m' (q + j))
(Eq.trans (congrArg Nat.succ (nadd_left_comm m' q j))
(Eq.symm (nsucc_add q (m' + j)))))⟩
theorem rem_up (m' r q : Nat) (hfix : tstep (m' + 1) r = r) :
rem (m' + 1) (up (m' + 1) q r) = r :=
match up_ge m' r q with
| ⟨j, hj⟩ =>
Eq.trans
(congrArg (fun t => iter t (tstep (m' + 1)) (up (m' + 1) q r)) hj)
(Eq.trans (iter_add (tstep (m' + 1)) (up (m' + 1) q r) q j)
(Eq.trans (congrArg (iter j (tstep (m' + 1))) (down_up (m' + 1) r q))
(iter_fix (tstep (m' + 1)) r hfix j)))
/-- The remainder of `q m + r` is `r` when `r` is below `m`. -/
theorem rem_div (m' q r k : Nat) (h : r + (k + 1) = m' + 1) :
rem (m' + 1) (q * (m' + 1) + r) = r :=
Eq.trans (congrArg (rem (m' + 1)) (Eq.symm (up_eq (m' + 1) r q)))
(rem_up m' r q (fix_of_lt (m' + 1) r k h))
/-- Every number is `q m + r` with `r` below `m`. -/
theorem div_exists (m' : Nat) :
∀ n : Nat, ∃ q r k, n = q * (m' + 1) + r ∧ r + (k + 1) = m' + 1
| 0 => ⟨0, 0, m', Eq.symm (nzero_mul (m' + 1)), nzero_add (m' + 1)⟩
| n + 1 =>
match div_exists m' n with
| ⟨q, r, k, hn, hr⟩ =>
match k, hr with
| 0, hr =>
⟨q + 1, 0, m',
Eq.trans (congrArg Nat.succ hn)
(Eq.trans (congrArg (fun t => q * (m' + 1) + t) hr)
(Eq.symm (nsucc_mul q (m' + 1)))),
nzero_add (m' + 1)⟩
| k + 1, hr =>
⟨q, r + 1, k, congrArg Nat.succ hn, Eq.trans (nsucc_add r (k + 1)) hr⟩
theorem rem_spec (m' n : Nat) :
∃ q r k, n = q * (m' + 1) + r ∧ r + (k + 1) = m' + 1 ∧ rem (m' + 1) n = r :=
match div_exists m' n with
| ⟨q, r, k, hn, hr⟩ =>
⟨q, r, k, hn, hr, Eq.trans (congrArg (rem (m' + 1)) hn) (rem_div m' q r k hr)⟩
/-- The remainder is always below the modulus. -/
theorem rem_lt (m' n : Nat) : ∃ k, rem (m' + 1) n + (k + 1) = m' + 1 :=
match rem_spec m' n with
| ⟨_, _, k, _, hr, hrem⟩ => ⟨k, Eq.trans (congrArg (fun t => t + (k + 1)) hrem) hr⟩
/-- The remainder is idempotent. -/
theorem rem_idem (m' n : Nat) : rem (m' + 1) (rem (m' + 1) n) = rem (m' + 1) n :=
match rem_spec m' n with
| ⟨_, r, k, _, hr, hrem⟩ =>
Eq.trans (congrArg (rem (m' + 1)) hrem)
(Eq.trans (rem_of_fix (m' + 1) r (fix_of_lt (m' + 1) r k hr)) (Eq.symm hrem))
/-- Adding a multiple of the modulus does not change the remainder. -/
theorem rem_add_mul (m' x p : Nat) :
rem (m' + 1) (p * (m' + 1) + x) = rem (m' + 1) x :=
match rem_spec m' x with
| ⟨q, r, k, hx, hr, hrem⟩ =>
Eq.trans (congrArg (fun t => rem (m' + 1) (p * (m' + 1) + t)) hx)
(Eq.trans
(congrArg (rem (m' + 1)) (Eq.symm (nadd_assoc (p * (m' + 1)) (q * (m' + 1)) r)))
(Eq.trans
(congrArg (fun t => rem (m' + 1) (t + r)) (Eq.symm (nright_distrib p q (m' + 1))))
(Eq.trans (rem_div m' (p + q) r k hr) (Eq.symm hrem))))
/-! ## Links 30-35. Order, divisibility, the Euclid step, primes -/
abbrev Le (a b : Nat) : Prop := ∃ k, a + k = b
abbrev Lt (a b : Nat) : Prop := ∃ k, a + (k + 1) = b
abbrev Dv (d n : Nat) : Prop := ∃ q, n = q * d
theorem mul_succ' (a b : Nat) : a * (b + 1) = a * b + a := rfl
/-! ### Order -/
theorem le_or_lt : ∀ (a b : Nat), Le a b ∨ Lt b a
| 0, b => Or.inl ⟨b, nzero_add b⟩
| a + 1, 0 => Or.inr ⟨a, nzero_add (a + 1)⟩
| a + 1, b + 1 =>
match le_or_lt a b with
| Or.inl ⟨k, h⟩ => Or.inl ⟨k, Eq.trans (nsucc_add a k) (congrArg Nat.succ h)⟩
| Or.inr ⟨k, h⟩ => Or.inr ⟨k, Eq.trans (nsucc_add b (k + 1)) (congrArg Nat.succ h)⟩
theorem tri (a b : Nat) : a = b ∨ Lt a b ∨ Lt b a :=
match le_or_lt a b with
| Or.inr h => Or.inr (Or.inr h)
| Or.inl ⟨k, hk⟩ =>
match k, hk with
| 0, hk => Or.inl hk
| k + 1, hk => Or.inr (Or.inl ⟨k, hk⟩)
theorem add_succ_ne : ∀ (a k : Nat), a + (k + 1) = a → False
| 0, k, h =>
absurd (show Nat.succ k = 0 from Eq.trans (Eq.symm (nzero_add (k + 1))) h)
(succ_ne_zero' k)
| a + 1, k, h =>
add_succ_ne a k
(@succ_inj' (a + (k + 1)) a (Eq.trans (Eq.symm (nsucc_add a (k + 1))) h))
theorem lt_le_absurd (a b k j : Nat) (hk : a + (k + 1) = b) (hj : b + j = a) : False :=
add_succ_ne a (k + j)
(Eq.trans (congrArg (fun t => a + t) (Eq.symm (nsucc_add k j)))
(Eq.trans (Eq.symm (nadd_assoc a (k + 1) j))
(Eq.trans (congrArg (fun t => t + j) hk) hj)))
theorem le_antisym' (a b : Nat) (h1 : Le a b) (h2 : Le b a) : a = b :=
match h1, h2 with
| ⟨k, hk⟩, ⟨j, hj⟩ =>
match k, hk with
| 0, hk => hk
| k + 1, hk => False.elim (lt_le_absurd a b k j hk hj)
theorem le_trans' (a b c : Nat) (h1 : Le a b) (h2 : Le b c) : Le a c :=
match h1, h2 with
| ⟨k, hk⟩, ⟨j, hj⟩ =>
⟨k + j,
Eq.trans (Eq.symm (nadd_assoc a k j)) (Eq.trans (congrArg (fun t => t + j) hk) hj)⟩
theorem le_succ (a b : Nat) (h : Le a b) : Le (a + 1) (b + 1) :=
match h with
| ⟨k, hk⟩ => ⟨k, Eq.trans (nsucc_add a k) (congrArg Nat.succ hk)⟩
theorem le_pred (a b : Nat) (h : Le (a + 1) (b + 1)) : Le a b :=
match h with
| ⟨k, hk⟩ => ⟨k, @succ_inj' (a + k) b (Eq.trans (Eq.symm (nsucc_add a k)) hk)⟩
theorem le_zero_absurd (n : Nat) (h : Le (n + 1) 0) : False :=
match h with
| ⟨k, hk⟩ =>
absurd (show Nat.succ (n + k) = 0 from Eq.trans (Eq.symm (nsucc_add n k)) hk)
(succ_ne_zero' (n + k))
theorem lt_zero_absurd (d : Nat) (h : Lt d 0) : False :=
match h with
| ⟨k, hk⟩ => absurd (show Nat.succ (d + k) = 0 from hk) (succ_ne_zero' (d + k))
theorem lt_succ_cases (d b : Nat) (h : Lt d (b + 1)) : d = b ∨ Lt d b :=
match h with
| ⟨k, hk⟩ =>
match k, hk with
| 0, hk => Or.inl (@succ_inj' d b hk)
| k + 1, hk => Or.inr ⟨k, @succ_inj' (d + (k + 1)) b hk⟩
/-! ### Divisibility -/
theorem dv_refl (n : Nat) : Dv n n :=
⟨1, Eq.symm (Eq.trans (nmul_comm 1 n) (nmul_one n))⟩
theorem dv_trans (d m n : Nat) (h1 : Dv d m) (h2 : Dv m n) : Dv d n :=
match h1, h2 with
| ⟨q, hq⟩, ⟨r, hr⟩ =>
⟨r * q,
Eq.trans hr (Eq.trans (congrArg (fun t => r * t) hq) (Eq.symm (nmul_assoc r q d)))⟩
theorem dv_self_mul (h x : Nat) : Dv h (h * x) := ⟨x, nmul_comm h x⟩
theorem dv_mul_left (h d x : Nat) (hx : Dv d x) : Dv d (h * x) :=
dv_trans d x (h * x) hx ⟨h, rfl⟩
theorem dv_add (p x y : Nat) (hx : Dv p x) (hy : Dv p y) : Dv p (x + y) :=
match hx, hy with
| ⟨c, hc⟩, ⟨e, he⟩ =>
⟨c + e,
Eq.trans (congrArg (fun t => t + y) hc)
(Eq.trans (congrArg (fun t => c * p + t) he) (Eq.symm (nright_distrib c e p)))⟩
/-- If `p` divides `x` and `x + y`, it divides `y` (through the remainder). -/
theorem dv_sub (p' x y : Nat) (hx : Dv (p' + 1) x) (hxy : Dv (p' + 1) (x + y)) :
Dv (p' + 1) y :=
match hx, hxy, rem_spec p' y with
| ⟨c, hc⟩, ⟨e, he⟩, ⟨q, r, _, hy, _, hrem⟩ =>
have h0 : rem (p' + 1) y = 0 :=
Eq.trans (Eq.symm (rem_add_mul p' y c))
(Eq.trans (congrArg (fun t => rem (p' + 1) (t + y)) (Eq.symm hc))
(Eq.trans (congrArg (rem (p' + 1)) he)
(rem_div p' e 0 p' (nzero_add (p' + 1)))))
have hr0 : r = 0 := Eq.trans (Eq.symm hrem) h0
⟨q, Eq.trans hy (congrArg (fun t => q * (p' + 1) + t) hr0)⟩
/-- Divisibility is decided by the remainder. -/
theorem dv_dec (m' n : Nat) : Dv (m' + 1) n ∨ (Dv (m' + 1) n → False) :=
match rem_spec m' n with
| ⟨q, r, _, hn, _, hrem⟩ =>
match r, hn, hrem with
| 0, hn, _ => Or.inl ⟨q, hn⟩
| r + 1, _, hrem =>
Or.inr (fun hd =>
match hd with
| ⟨e, he⟩ =>
absurd
(show Nat.succ r = 0 from
Eq.trans (Eq.symm hrem)
(Eq.trans (congrArg (rem (m' + 1)) he)
(rem_div m' e 0 m' (nzero_add (m' + 1)))))
(succ_ne_zero' r))
/-- A divisor of a positive number is not above it. -/
theorem dv_le (d j : Nat) (h : Dv d (j + 1)) : Le d (j + 1) :=
match h with
| ⟨q, hq⟩ =>
match q, hq with
| 0, hq =>
absurd (show Nat.succ j = 0 from Eq.trans hq (nzero_mul d)) (succ_ne_zero' j)
| q + 1, hq =>
⟨q * d, Eq.trans (nadd_comm d (q * d)) (Eq.symm (Eq.trans hq (nsucc_mul q d)))⟩
theorem two_add_ne_one (j k : Nat) (h : (j + 1 + 1) + k = 1) : False :=
absurd
(show Nat.succ (j + k) = 0 from
Eq.trans (Eq.symm (nsucc_add j k))
(@succ_inj' ((j + 1) + k) 0 (Eq.trans (Eq.symm (nsucc_add (j + 1) k)) h)))
(succ_ne_zero' (j + k))
theorem dv_one_absurd (j : Nat) (h : Dv (j + 1 + 1) 1) : False :=
match dv_le (j + 1 + 1) 0 h with
| ⟨k, hk⟩ => two_add_ne_one j k hk
/-! ### Link 32. The Euclid step reaches the diagonal; what it keeps -/
/-- A family closed under difference. -/
abbrev Closed (S : Nat → Prop) : Prop := ∀ x z, S x → S (x + z) → S z
/-- `g` is where the Euclid step started at `(a, b)` ends: it divides
both, and it lies in every closed family that holds `a` and `b`. -/
abbrev Meet (a b g : Nat) : Prop :=
Dv g a ∧ Dv g b ∧ ∀ (S : Nat → Prop), Closed S → S a → S b → S g
theorem meet_diag (a : Nat) : Meet a a a :=
⟨dv_refl a, dv_refl a, fun _ _ h _ => h⟩
theorem meet_up (a k g : Nat) (h : Meet a k g) : Meet a (a + k) g :=
⟨h.1, dv_add g a k h.1 h.2.1,
fun S hcl hSa hSb => h.2.2 S hcl hSa (hcl a k hSa hSb)⟩
theorem meet_swap (a b g : Nat) (h : Meet a b g) : Meet b a g :=
⟨h.2.1, h.1, fun S hcl hSb hSa => h.2.2 S hcl hSa hSb⟩
theorem ex_map {P Q : Nat → Prop} (h : ∃ g, P g) (f : ∀ g, P g → Q g) : ∃ g, Q g :=
match h with
| ⟨g, hg⟩ => ⟨g, f g hg⟩
theorem fuelA (a b k j f : Nat) (hk : a + (k + 1) = b) (hf0 : (a + b) + j = f) :
Le ((a + k) + 1) f :=
⟨a + j,
Eq.trans (nsucc_add (a + k) (a + j))
(Eq.trans
(congrArg Nat.succ
(Eq.trans (Eq.symm (nadd_assoc (a + k) a j))
(congrArg (fun t => t + j) (nadd_comm (a + k) a))))
(Eq.trans (Eq.symm (nsucc_add (a + (a + k)) j))
(Eq.trans (congrArg (fun t => (a + t) + j) hk) hf0)))⟩
theorem fuelB (a b k j f : Nat) (hk : b + (k + 1) = a) (hf0 : (a + b) + j = f) :
Le ((b + k) + 1) f :=
⟨b + j,
Eq.trans (nsucc_add (b + k) (b + j))
(Eq.trans (congrArg Nat.succ (Eq.symm (nadd_assoc (b + k) b j)))
(Eq.trans (Eq.symm (nsucc_add ((b + k) + b) j))
(Eq.trans (congrArg (fun t => t + j) (Eq.symm (nsucc_add (b + k) b)))
(Eq.trans (congrArg (fun t => (t + b) + j) hk) hf0))))⟩
/-- The Euclid step on `(a + 1, b + 1)`, run as a descent on `a + b`. -/
theorem euclid_walk :
∀ (f a b : Nat), Le ((a + b) + 1) f → ∃ g, Meet (a + 1) (b + 1) g
| 0, a, b, hf => False.elim (le_zero_absurd (a + b) hf)
| f + 1, a, b, hf =>
match le_pred (a + b) f hf with
| ⟨j, hf0⟩ =>
match tri a b with
| Or.inl hk =>
⟨a + 1,
Eq.mp (congrArg (fun t => Meet (a + 1) (t + 1) (a + 1)) hk) (meet_diag (a + 1))⟩
| Or.inr (Or.inl ⟨k, hk⟩) =>
ex_map (euclid_walk f a k (fuelA a b k j f hk hf0))
(fun g hg =>
Eq.mp
(congrArg (fun t => Meet (a + 1) t g)
(Eq.trans (nsucc_add a (k + 1)) (congrArg Nat.succ hk)))
(meet_up (a + 1) (k + 1) g hg))
| Or.inr (Or.inr ⟨k, hk⟩) =>
ex_map (euclid_walk f b k (fuelB a b k j f hk hf0))
(fun g hg =>
meet_swap (b + 1) (a + 1) g
(Eq.mp
(congrArg (fun t => Meet (b + 1) t g)
(Eq.trans (nsucc_add b (k + 1)) (congrArg Nat.succ hk)))
(meet_up (b + 1) (k + 1) g hg)))
/-! ### Links 33-34. Primes and Euclid's lemma -/
/-- `p` is prime: at least 2, and its only divisors are 1 and itself. -/
abbrev Pr (p : Nat) : Prop :=
(∃ j, p = j + 1 + 1) ∧ ∀ d, Dv d p → d = 1 ∨ d = p
/-- Euclid's lemma, read off the Euclid step: the family
`S x := p ∣ x * b` is closed under difference and holds `a + 1`
and `p`, so it holds where the step ends. -/
theorem euclid_core (j a b : Nat)
(hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1)
(hab : Dv (j + 1 + 1) ((a + 1) * b)) :
Dv (j + 1 + 1) (a + 1) ∨ Dv (j + 1 + 1) b :=
match euclid_walk ((a + (j + 1)) + 1) a (j + 1) ⟨0, rfl⟩ with
| ⟨g, hga, hgp, hS⟩ =>
have hSg : Dv (j + 1 + 1) (g * b) :=
hS (fun x => Dv (j + 1 + 1) (x * b))
(fun x z hx hxz =>
dv_sub (j + 1) (x * b) (z * b) hx
(Eq.mp (congrArg (fun t => Dv (j + 1 + 1) t) (nright_distrib x z b)) hxz))
hab (dv_self_mul (j + 1 + 1) b)
match hpr g hgp with
| Or.inl h1 =>
Or.inr
(Eq.mp
(congrArg (fun t => Dv (j + 1 + 1) t)
(Eq.trans (congrArg (fun t => t * b) h1)
(Eq.trans (nmul_comm 1 b) (nmul_one b))))
hSg)
| Or.inr hP => Or.inl (Eq.mp (congrArg (fun t => Dv t (a + 1)) hP) hga)
/-! ### Link 35. The least factor above 1 -/
theorem min_of_no (n b : Nat) (hno : ∀ d, Lt d b → Dv (d + 1 + 1) n → False) :
∀ d, Dv (d + 1 + 1) n → Le b d :=
fun d hd =>
match le_or_lt b d with
| Or.inl h => h
| Or.inr hlt => False.elim (hno d hlt hd)
theorem no_step (n b : Nat) (hno : ∀ d, Lt d b → Dv (d + 1 + 1) n → False)
(hnd : Dv (b + 1 + 1) n → False) :
∀ d, Lt d (b + 1) → Dv (d + 1 + 1) n → False :=
fun d hlt hd =>
match lt_succ_cases d b hlt with
| Or.inl e => hnd (Eq.mp (congrArg (fun t => Dv (t + 1 + 1) n) e) hd)
| Or.inr hlt' => hno d hlt' hd
/-- Search upwards: either a least factor `l + 2` has been found, or
nothing below the bound divides. -/
theorem lp_search (n : Nat) :
∀ b : Nat,
(∃ l, Dv (l + 1 + 1) n ∧ ∀ d, Dv (d + 1 + 1) n → Le l d) ∨
(∀ d, Lt d b → Dv (d + 1 + 1) n → False)
| 0 => Or.inr (fun d hlt _ => lt_zero_absurd d hlt)
| b + 1 =>
match lp_search n b with
| Or.inl h => Or.inl h
| Or.inr hno =>
match dv_dec (b + 1) n with
| Or.inl hdv => Or.inl ⟨b, hdv, min_of_no n b hno⟩
| Or.inr hnd => Or.inr (no_step n b hno hnd)
theorem lp_exists (i : Nat) :
∃ l, Dv (l + 1 + 1) (i + 1 + 1) ∧ ∀ d, Dv (d + 1 + 1) (i + 1 + 1) → Le l d :=
match lp_search (i + 1 + 1) (i + 1) with
| Or.inl h => h
| Or.inr hno => False.elim (hno i ⟨0, rfl⟩ (dv_refl (i + 1 + 1)))
/-- The least factor above 1 is prime. -/
theorem lp_prime (n l : Nat) (hl : Dv (l + 1 + 1) n)
(hmin : ∀ d, Dv (d + 1 + 1) n → Le l d) :
∀ e, Dv e (l + 1 + 1) → e = 1 ∨ e = l + 1 + 1
| 0, h =>
match h with
| ⟨_, hq⟩ => absurd (show Nat.succ (l + 1) = 0 from hq) (succ_ne_zero' (l + 1))
| e + 1, h =>
match e, h with
| 0, _ => Or.inl rfl
| d + 1, h =>
Or.inr
(congrArg (fun t => t + 1 + 1)
(le_antisym' d l
(le_pred d l (le_pred (d + 1) (l + 1) (dv_le (d + 1 + 1) (l + 1) h)))
(hmin d (dv_trans (d + 1 + 1) (l + 1 + 1) n h hl))))
/-! ## Links 36-37. The swap read off the diagonal; the order idempotent -/
/-- Write the pair twice with the diagonal, then read the second place
of the first copy and the first place of the second. -/
def swp {A : Type u} (p : A × A) : A × A := ((diag p).1.2, (diag p).2.1)
theorem swp_pair {A : Type u} (x y : A) : swp (x, y) = (y, x) := rfl
theorem swp_swp {A : Type u} (p : A × A) : swp (swp p) = p := rfl
theorem swp_diag {A : Type u} (x : A) : swp (diag x) = diag x := rfl
/-- The swap does nothing exactly on the diagonal. -/
theorem swp_fix {A : Type u} {x y : A} (h : swp (x, y) = (x, y)) : x = y :=
congrArg Prod.snd h
/-- The other three readings of the doubled pair. -/
theorem read_12 {A : Type u} (p : A × A) : ((diag p).1.1, (diag p).2.2) = p := rfl
theorem read_11 {A : Type u} (p : A × A) : ((diag p).1.1, (diag p).2.1) = e1 p := rfl
/-- Choose between the identity and the swap. -/
def ord (p : Nat × Nat) : Nat × Nat := sel (sb p.1 p.2) p (swp p)
theorem ord_le (x k : Nat) : ord (x, x + k) = (x, x + k) :=
congrArg (fun t => sel t (x, x + k) (swp (x, x + k))) (sb_le_zero x k)
theorem ord_gt (y k : Nat) : ord (y + (k + 1), y) = (y, y + (k + 1)) :=
congrArg (fun t => sel t (y + (k + 1), y) (swp (y + (k + 1), y)))
(Eq.trans (congrArg (fun t => sb t y) (nadd_comm y (k + 1))) (sb_add_cancel y (k + 1)))
theorem ord_idem (x y : Nat) : ord (ord (x, y)) = ord (x, y) :=
match le_or_lt x y with
| Or.inl ⟨k, h⟩ =>
Eq.mp (congrArg (fun t => ord (ord (x, t)) = ord (x, t)) h) (congrArg ord (ord_le x k))
| Or.inr ⟨k, h⟩ =>
Eq.mp (congrArg (fun t => ord (ord (t, y)) = ord (t, y)) h)
(Eq.trans (congrArg ord (ord_gt y k))
(Eq.trans (ord_le y (k + 1)) (Eq.symm (ord_gt y k))))
/-! ## Link 38. Lists: product, primes, ascending; inserting and sorting -/
def prod : List Nat → Nat
| [] => 1
| p :: m => p * prod m
def AllPr : List Nat → Prop
| [] => True
| p :: m => Pr p ∧ AllPr m
/-- `a` is below everything in the list. -/
def LeAll (a : Nat) : List Nat → Prop
| [] => True
| q :: m => Le a q ∧ LeAll a m
/-- The list is ascending. -/
def Sorted : List Nat → Prop
| [] => True
| p :: m => LeAll p m ∧ Sorted m
/-- Insert `p`: it stays in front if it is not above the head, and
otherwise moves past the head. -/
def ins (p : Nat) : List Nat → List Nat
| [] => [p]
| q :: m => sel (sb p q) (p :: q :: m) (q :: ins p m)
def isort : List Nat → List Nat
| [] => []
| p :: m => ins p (isort m)
theorem ins_le (p q : Nat) (m : List Nat) (k : Nat) (h : p + k = q) :
ins p (q :: m) = p :: q :: m :=
congrArg (fun t => sel t (p :: q :: m) (q :: ins p m))
(Eq.trans (congrArg (fun t => sb p t) (Eq.symm h)) (sb_le_zero p k))
theorem ins_gt (p q : Nat) (m : List Nat) (k : Nat) (h : q + (k + 1) = p) :
ins p (q :: m) = q :: ins p m :=
congrArg (fun t => sel t (p :: q :: m) (q :: ins p m))
(Eq.trans (congrArg (fun t => sb t q) (Eq.symm h))
(Eq.trans (congrArg (fun t => sb t q) (nadd_comm q (k + 1))) (sb_add_cancel q (k + 1))))
theorem mul_left_comm' (a b c : Nat) : a * (b * c) = b * (a * c) :=
Eq.trans (Eq.symm (nmul_assoc a b c))
(Eq.trans (congrArg (fun t => t * c) (nmul_comm a b)) (nmul_assoc b a c))
theorem prod_ins (p : Nat) : ∀ (m : List Nat), prod (ins p m) = p * prod m
| [] => rfl
| q :: m =>
match le_or_lt p q with
| Or.inl ⟨k, h⟩ => congrArg prod (ins_le p q m k h)
| Or.inr ⟨k, h⟩ =>
Eq.trans (congrArg prod (ins_gt p q m k h))
(Eq.trans (congrArg (fun t => q * t) (prod_ins p m)) (mul_left_comm' q p (prod m)))
theorem allpr_ins (p : Nat) (hp : Pr p) : ∀ (m : List Nat), AllPr m → AllPr (ins p m)
| [], _ => show Pr p ∧ AllPr [] from And.intro hp True.intro
| q :: m, hA =>
have hA' : Pr q ∧ AllPr m := hA
match le_or_lt p q with
| Or.inl ⟨k, h⟩ =>
Eq.mpr (congrArg AllPr (ins_le p q m k h))
(show Pr p ∧ AllPr (q :: m) from And.intro hp hA)
| Or.inr ⟨k, h⟩ =>
Eq.mpr (congrArg AllPr (ins_gt p q m k h))
(show Pr q ∧ AllPr (ins p m) from And.intro hA'.1 (allpr_ins p hp m hA'.2))
theorem leall_mono (a b : Nat) (hab : Le a b) : ∀ (m : List Nat), LeAll b m → LeAll a m
| [], _ => True.intro
| q :: m, h =>
have h' : Le b q ∧ LeAll b m := h
show Le a q ∧ LeAll a m from
And.intro (le_trans' a b q hab h'.1) (leall_mono a b hab m h'.2)
theorem leall_ins (a p : Nat) (hap : Le a p) :
∀ (m : List Nat), LeAll a m → LeAll a (ins p m)
| [], _ => show Le a p ∧ LeAll a [] from And.intro hap True.intro
| q :: m, h =>
have h' : Le a q ∧ LeAll a m := h
match le_or_lt p q with
| Or.inl ⟨k, hk⟩ =>
Eq.mpr (congrArg (LeAll a) (ins_le p q m k hk))
(show Le a p ∧ LeAll a (q :: m) from And.intro hap h)
| Or.inr ⟨k, hk⟩ =>
Eq.mpr (congrArg (LeAll a) (ins_gt p q m k hk))
(show Le a q ∧ LeAll a (ins p m) from
And.intro h'.1 (leall_ins a p hap m h'.2))
theorem sorted_ins (p : Nat) : ∀ (m : List Nat), Sorted m → Sorted (ins p m)
| [], _ => show LeAll p [] ∧ Sorted [] from And.intro True.intro True.intro
| q :: m, h =>
have h' : LeAll q m ∧ Sorted m := h
match le_or_lt p q with
| Or.inl ⟨k, hk⟩ =>
Eq.mpr (congrArg Sorted (ins_le p q m k hk))
(show LeAll p (q :: m) ∧ Sorted (q :: m) from
And.intro
(show Le p q ∧ LeAll p m from
And.intro ⟨k, hk⟩ (leall_mono p q ⟨k, hk⟩ m h'.1))
h)
| Or.inr ⟨k, hk⟩ =>
Eq.mpr (congrArg Sorted (ins_gt p q m k hk))
(show LeAll q (ins p m) ∧ Sorted (ins p m) from
And.intro (leall_ins q p ⟨k + 1, hk⟩ m h'.1) (sorted_ins p m h'.2))
theorem prod_isort : ∀ (L : List Nat), prod (isort L) = prod L
| [] => rfl
| p :: m => Eq.trans (prod_ins p (isort m)) (congrArg (fun t => p * t) (prod_isort m))
theorem allpr_isort : ∀ (L : List Nat), AllPr L → AllPr (isort L)
| [], _ => True.intro
| p :: m, hA =>
have hA' : Pr p ∧ AllPr m := hA
allpr_ins p hA'.1 (isort m) (allpr_isort m hA'.2)
theorem sorted_isort : ∀ (L : List Nat), Sorted (isort L)
| [] => True.intro
| p :: m => sorted_ins p (isort m) (sorted_isort m)
theorem isort_example : isort [3, 2, 5, 2] = [2, 2, 3, 5] ∧ prod [2, 2, 3, 5] = 60 :=
⟨rfl, rfl⟩
/-! ## Link 39. The fundamental theorem of arithmetic -/
/-! ### (a) Every number from 1 up is the product of an ascending list of primes -/
theorem quot_le (i m l : Nat) (hq : i + 1 + 1 = (m + 1) * (l + 1 + 1)) : Le m i :=
have s1 : i + 1 + 1 = (l * (m + 1) + (m + 1)) + (m + 1) :=
Eq.trans hq
(Eq.trans (nmul_comm (m + 1) (l + 1 + 1))
(Eq.trans (nsucc_mul (l + 1) (m + 1))
(congrArg (fun t => t + (m + 1)) (nsucc_mul l (m + 1)))))
have s2 : i + 1 = (l * (m + 1) + (m + 1)) + m :=
@succ_inj' (i + 1) ((l * (m + 1) + (m + 1)) + m) s1
have s3 : i + 1 = ((l * (m + 1) + m) + m) + 1 :=
Eq.trans s2 (nsucc_add (l * (m + 1) + m) m)
have s4 : i = (l * (m + 1) + m) + m :=
@succ_inj' i ((l * (m + 1) + m) + m) s3
⟨l * (m + 1) + m, Eq.trans (nadd_comm m (l * (m + 1) + m)) (Eq.symm s4)⟩
theorem leall_of_min (l : Nat) :
∀ (L : List Nat), AllPr L → (∀ d, Dv (d + 1 + 1) (prod L) → Le l d) →
LeAll (l + 1 + 1) L
| [], _, _ => True.intro
| h :: t, hpr, hmin =>
have hpr' : Pr h ∧ AllPr t := hpr
match hpr'.1 with
| ⟨⟨j, hj⟩, _⟩ =>
have hdj : Dv (j + 1 + 1) (h * prod t) :=
Eq.mp (congrArg (fun x => Dv x (h * prod t)) hj) (dv_self_mul h (prod t))
show Le (l + 1 + 1) h ∧ LeAll (l + 1 + 1) t from
And.intro
(Eq.mpr (congrArg (fun x => Le (l + 1 + 1) x) hj)
(le_succ (l + 1) (j + 1) (le_succ l j (hmin j hdj))))
(leall_of_min l t hpr'.2
(fun d hd => hmin d (dv_mul_left h (d + 1 + 1) (prod t) hd)))
/-- Take the least factor out and put it in front. -/
theorem factor_step (i l m : Nat) (L : List Nat)
(hl : Dv (l + 1 + 1) (i + 1 + 1))
(hmin : ∀ d, Dv (d + 1 + 1) (i + 1 + 1) → Le l d)
(hq : i + 1 + 1 = (m + 1) * (l + 1 + 1))
(hL : AllPr L ∧ Sorted L ∧ prod L = m + 1) :
AllPr ((l + 1 + 1) :: L) ∧ Sorted ((l + 1 + 1) :: L) ∧
prod ((l + 1 + 1) :: L) = i + 1 + 1 :=
have hm : Dv (m + 1) (i + 1 + 1) :=
⟨l + 1 + 1, Eq.trans hq (nmul_comm (m + 1) (l + 1 + 1))⟩
have hminL : ∀ d, Dv (d + 1 + 1) (prod L) → Le l d :=
fun d hd =>
hmin d
(dv_trans (d + 1 + 1) (m + 1) (i + 1 + 1)
(Eq.mp (congrArg (fun t => Dv (d + 1 + 1) t) hL.2.2) hd) hm)
have hP : Pr (l + 1 + 1) := ⟨⟨l, rfl⟩, lp_prime (i + 1 + 1) l hl hmin⟩
And.intro
(show Pr (l + 1 + 1) ∧ AllPr L from And.intro hP hL.1)
(And.intro
(show LeAll (l + 1 + 1) L ∧ Sorted L from
And.intro (leall_of_min l L hL.1 hminL) hL.2.1)
(show (l + 1 + 1) * prod L = i + 1 + 1 from
Eq.trans (congrArg (fun t => (l + 1 + 1) * t) hL.2.2)
(Eq.trans (nmul_comm (l + 1 + 1) (m + 1)) (Eq.symm hq))))
/-- Descent on the number: `n + 1` with `n` at most `f`. -/
theorem factor_exists :
∀ (f n : Nat), Le n f → ∃ L, AllPr L ∧ Sorted L ∧ prod L = n + 1
| _, 0, _ => ⟨[], True.intro, True.intro, rfl⟩
| 0, n + 1, hf => False.elim (le_zero_absurd n hf)
| f + 1, i + 1, hf =>
match lp_exists i with
| ⟨l, hl, hmin⟩ =>
match hl with
| ⟨q, hq⟩ =>
match q, hq with
| 0, hq =>
absurd (show Nat.succ (i + 1) = 0 from Eq.trans hq (nzero_mul (l + 1 + 1)))
(succ_ne_zero' (i + 1))
| m + 1, hq =>
match factor_exists f m
(le_trans' m i f (quot_le i m l hq) (le_pred i f hf)) with
| ⟨L, hL⟩ => ⟨(l + 1 + 1) :: L, factor_step i l m L hl hmin hq hL⟩
theorem fta_exists (n : Nat) : ∃ L, AllPr L ∧ Sorted L ∧ prod L = n + 1 :=
factor_exists n n ⟨0, rfl⟩
/-! ### Uniqueness -/
theorem prod_pos : ∀ (L : List Nat), AllPr L → ∃ k, prod L = k + 1
| [], _ => ⟨0, rfl⟩
| p :: m, hA =>
have hA' : Pr p ∧ AllPr m := hA
match hA'.1, prod_pos m hA'.2 with
| ⟨⟨j, hj⟩, _⟩, ⟨k, hk⟩ =>
⟨(j + 1 + 1) * k + (j + 1),
Eq.trans (congrArg (fun t => t * prod m) hj)
(Eq.trans (congrArg (fun t => (j + 1 + 1) * t) hk) (mul_succ' (j + 1 + 1) k))⟩
/-- A nonempty list of primes does not have product 1. -/
theorem prod_ne_one (p : Nat) (m : List Nat) (hA : AllPr (p :: m))
(h : prod (p :: m) = 1) : False :=
have hA' : Pr p ∧ AllPr m := hA
match hA'.1, prod_pos m hA'.2 with
| ⟨⟨j, hj⟩, _⟩, ⟨k, hk⟩ =>
have eq0 : p * prod m = (j + 1 + 1) * k + (j + 1 + 1) :=
Eq.trans (congrArg (fun t => t * prod m) hj)
(Eq.trans (congrArg (fun t => (j + 1 + 1) * t) hk) (mul_succ' (j + 1 + 1) k))
have eq1 : ((j + 1 + 1) * k + (j + 1)) + 1 = 0 + 1 := Eq.trans (Eq.symm eq0) h
absurd
(show Nat.succ ((j + 1 + 1) * k + j) = 0 from
@succ_inj' ((j + 1 + 1) * k + (j + 1)) 0 eq1)
(succ_ne_zero' ((j + 1 + 1) * k + j))
theorem mul_lt_absurd (a x y k : Nat) (hk : x + (k + 1) = y)
(h : (a + 1) * x = (a + 1) * y) : False :=
add_succ_ne ((a + 1) * x) ((a + 1) * k + a)
(Eq.symm
(Eq.trans h
(Eq.trans (congrArg (fun t => (a + 1) * t) (Eq.symm hk))
(Eq.trans (nleft_distrib (a + 1) x (k + 1))
(congrArg (fun t => (a + 1) * x + t) (mul_succ' (a + 1) k))))))
/-- Cancelling a positive factor. -/
theorem mul_cancel (a x y : Nat) (h : (a + 1) * x = (a + 1) * y) : x = y :=
match tri x y with
| Or.inl e => e
| Or.inr (Or.inl ⟨k, hk⟩) => False.elim (mul_lt_absurd a x y k hk h)
| Or.inr (Or.inr ⟨k, hk⟩) => False.elim (mul_lt_absurd a y x k hk (Eq.symm h))
theorem pr_cancel (p x y : Nat) (hp : Pr p) (h : p * x = p * y) : x = y :=
match hp with
| ⟨⟨j, hj⟩, _⟩ =>
mul_cancel (j + 1) x y (Eq.mp (congrArg (fun t => t * x = t * y) hj) h)
/-- A prime that divides the product of a list of primes, all of them
at least `q`, is itself at least `q`. Euclid's lemma is used here. -/
theorem head_le (j q : Nat) (hpr : ∀ d, Dv d (j + 1 + 1) → d = 1 ∨ d = j + 1 + 1) :
∀ (m : List Nat), AllPr m → LeAll q m → Dv (j + 1 + 1) (prod m) → Le q (j + 1 + 1)
| [], _, _, hd => False.elim (dv_one_absurd j hd)
| h :: t, hall, hle, hd =>
have hall' : Pr h ∧ AllPr t := hall
have hle' : Le q h ∧ LeAll q t := hle
match hall'.1 with
| ⟨⟨i, hi⟩, hprh⟩ =>
match euclid_core j (i + 1) (prod t) hpr
(Eq.mp (congrArg (fun x => Dv (j + 1 + 1) (x * prod t)) hi) hd) with
| Or.inr hdt => head_le j q hpr t hall'.2 hle'.2 hdt
| Or.inl hdiv =>
match hprh (j + 1 + 1) (Eq.mpr (congrArg (fun x => Dv (j + 1 + 1) x) hi) hdiv) with
| Or.inl e => absurd (show Nat.succ j = 0 from @succ_inj' (j + 1) 0 e) (succ_ne_zero' j)
| Or.inr e => Eq.mpr (congrArg (fun x => Le q x) e) hle'.1
theorem head_le_of (p q : Nat) (m m' : List Nat) (hp : Pr p)
(hA' : Pr q ∧ AllPr m') (hLe : LeAll q m') (he : p * prod m = q * prod m') :
Le q p :=
match hp with
| ⟨⟨j, hj⟩, hprp⟩ =>
have hdv : Dv p (q * prod m') :=
⟨prod m, Eq.trans (Eq.symm he) (nmul_comm p (prod m))⟩
have hqq : Le q q := ⟨0, rfl⟩
Eq.mpr (congrArg (fun t => Le q t) hj)
(head_le j q
(Eq.mp (congrArg (fun t => ∀ d, Dv d t → d = 1 ∨ d = t) hj) hprp)
(q :: m') hA'
(show Le q q ∧ LeAll q m' from And.intro hqq hLe)
(Eq.mp (congrArg (fun t => Dv t (q * prod m')) hj) hdv))
/-- Two ascending lists of primes with equal products have equal heads. -/
theorem head_eq (p q : Nat) (m m' : List Nat)
(hA : Pr p ∧ AllPr m) (hS : LeAll p m ∧ Sorted m)
(hA' : Pr q ∧ AllPr m') (hS' : LeAll q m' ∧ Sorted m')
(he : p * prod m = q * prod m') : p = q :=
le_antisym' p q
(head_le_of q p m' m hA'.1 hA hS.1 (Eq.symm he))
(head_le_of p q m m' hA.1 hA' hS'.1 he)
/-- Two ascending lists of primes with the same product are the same. -/
theorem fta_unique :
∀ (L L' : List Nat), AllPr L → Sorted L → AllPr L' → Sorted L' →
prod L = prod L' → L = L'
| [], [], _, _, _, _, _ => rfl
| [], q :: m', _, _, hA', _, he => False.elim (prod_ne_one q m' hA' (Eq.symm he))
| p :: m, [], hA, _, _, _, he => False.elim (prod_ne_one p m hA he)
| p :: m, q :: m', hA, hS, hA', hS', he =>
have hA1 : Pr p ∧ AllPr m := hA
have hS1 : LeAll p m ∧ Sorted m := hS
have hA2 : Pr q ∧ AllPr m' := hA'
have hS2 : LeAll q m' ∧ Sorted m' := hS'
have he1 : p * prod m = q * prod m' := he
have hpq : p = q := head_eq p q m m' hA1 hS1 hA2 hS2 he1
have hpp : p * prod m = p * prod m' :=
Eq.trans he1 (congrArg (fun t => t * prod m') (Eq.symm hpq))
have hmm : prod m = prod m' := pr_cancel p (prod m) (prod m') hA1.1 hpp
Eq.trans (congrArg (fun t => t :: m) hpq)
(congrArg (fun t => q :: t) (fta_unique m m' hA1.2 hS1.2 hA2.2 hS2.2 hmm))
/-! ### The theorem -/
/-- Existence and uniqueness in one statement: for every `n + 1` there
is exactly one ascending list of primes with that product. -/
theorem fta (n : Nat) :
∃ L, (AllPr L ∧ Sorted L ∧ prod L = n + 1) ∧
∀ G, AllPr G → Sorted G → prod G = n + 1 → G = L :=
match fta_exists n with
| ⟨L, hL⟩ =>
⟨L, hL, fun G hG hGs hGp =>
fta_unique G L hG hGs hL.1 hL.2.1 (Eq.trans hGp (Eq.symm hL.2.2))⟩
/-- (c) Reading the product of a list of primes gives the sorted list:
the ascending list of primes with the product of `L` is `isort L`. -/
theorem read_prod (L G : List Nat) (hL : AllPr L) (hG : AllPr G) (hGs : Sorted G)
(h : prod G = prod L) : G = isort L :=
fta_unique G (isort L) hG hGs (allpr_isort L hL) (sorted_isort L)
(Eq.trans h (Eq.symm (prod_isort L)))
/-- (d) Sorting is idempotent on lists of primes. -/
theorem isort_idem (L : List Nat) (hL : AllPr L) : isort (isort L) = isort L :=
Eq.symm
(read_prod (isort L) (isort L) (allpr_isort L hL) (allpr_isort L hL) (sorted_isort L) rfl)
/-- (d) Equal after sorting gives equal products ... -/
theorem prod_eq_of_isort (L L' : List Nat) (h : isort L = isort L') : prod L = prod L' :=
Eq.trans (Eq.symm (prod_isort L)) (Eq.trans (congrArg prod h) (prod_isort L'))
/-- ... and equal products give equal lists after sorting. -/
theorem isort_eq_of_prod (L L' : List Nat) (hL : AllPr L) (hL' : AllPr L')
(h : prod L = prod L') : isort L = isort L' :=
read_prod L' (isort L) hL' (allpr_isort L hL) (sorted_isort L)
(Eq.trans (prod_isort L) h)
/-! ## Axiom check (all should report no axioms) -/
#print axioms le_or_lt
#print axioms dv_sub
#print axioms dv_dec
#print axioms euclid_walk
#print axioms euclid_core
#print axioms lp_exists
#print axioms lp_prime
#print axioms swp_fix
#print axioms ord_idem
#print axioms prod_ins
#print axioms sorted_ins
#print axioms prod_isort
#print axioms allpr_isort
#print axioms sorted_isort
#print axioms isort_example
#print axioms factor_exists
#print axioms fta_exists
#print axioms mul_cancel
#print axioms head_le
#print axioms fta_unique
#print axioms fta
#print axioms read_prod
#print axioms isort_idem
#print axioms prod_eq_of_isort
#print axioms isort_eq_of_prod
end FTAChain
A root exists. Let \(P\) be a non-constant polynomial over \(\mathbb {C}\) (link 26). Since \(|P(z)| \to \infty \) as \(|z| \to \infty \), the value \(|P|\) is least at some \(z_0\) (link 25). Write
If \(c_0 \neq 0\), choose a small \(w\) with \(c_k w^k = -t\,c_0\) for some \(t > 0\). Then \(|P(z_0 + w)| < |c_0|\), against the choice of \(z_0\). Hence \(P(z_0) = 0\). The choice of \(w\) uses \(k\)-th roots in \(\mathbb {C}\), which rest on the completeness of \(\mathbb {R}\).
The rest is arithmetic. Polynomials admit division with remainder, so the Euclid step of Section 75.3 runs unchanged:
and induction on the degree gives
A polynomial is what its root places hold. Together with the fact that the primes of \(\mathbb {C}[z]\) are the \(z - r\), this is the fundamental theorem of arithmetic for \(\mathbb {C}[z]\).
| Theorem | Reading | Placing | Equation |
| calculus | \(D\) | \(I\) | \(DI = \mathrm {id}\); \(ID\) idempotent |
| arithmetic | \(n \mapsto (v_p(n))_p\) | \(\prod _p p^{\,v_p}\) | read, then place, returns \(n\) |
| algebra | \(P \mapsto \) its roots | \(c \prod _i (z - r_i)\) | read, then place, returns \(P\) |
What each computation rests on. The discrete form of the fundamental theorem of calculus is a complete computation from the defining equation of the sum (link 22). The continuous form and the fundamental theorem of arithmetic are given as the standard steps, each from named earlier links. Three steps rest on the completeness of \(\mathbb {R}\) (link 25): that a function with derivative \(0\) is constant, that \(|P|\) attains its least value, and the existence of \(k\)-th roots.
The pattern, the discrete form of the fundamental theorem of calculus, the derived addition, and the derived integers (pairs of numbers with subtraction as the swap) are checked in the Lean 4 file FundAll.lean; every statement proved there reports no axioms. The discrete theorem is checked in three settings: the derived addition, the derived integers, and the two-valued world where \(1 + 1 = 0\). The continuous form, the fundamental theorem of arithmetic and the fundamental theorem of algebra are taken from Mathlib, where they are intervalIntegral.integral_hasDerivAt_right, intervalIntegral.integral_eq_sub_of_hasDerivAt, Nat.primeFactorsList_unique and Complex.exists_root.
FundAll.lean
import Mathlib
/-
FundAll.lean (second version: no tactics that bring axioms)
Checks for the section "Three Fundamental Theorems as One Pattern".
Parts A–E are proved by hand, with no `omega` and no `simp`.
Every `#print axioms` line below should report no axioms.
Mathlib is imported only for Part F, which points at the three full
theorems as they stand in the library.
Part A the pattern, and the discrete fundamental theorem of calculus,
for any addition and subtraction with four laws.
Part B addition derived: "do it n times", where the laws hold by rfl.
Part C subtraction and the integers derived: pairs of numbers,
minus = swap, zero = diagonal. The laws of + on numbers are
proved here from the two defining equations, not taken from
the library. Both halves of the discrete theorem are then
proved for these integers.
Part D the discrete theorem in the two-valued world, where 1 + 1 = 0.
Part E the Euclid step and the divisibility line.
Part F the three full theorems in the library.
-/
universe u
namespace FundAll
/-! ## Part A. The pattern and the discrete theorem -/
/-- If `r ∘ s = id` then `s ∘ r` is idempotent. -/
theorem split_idem {A B : Type u} (r : B → A) (s : A → B)
(h : ∀ x, r (s x) = x) (y : B) : s (r (s (r y))) = s (r y) :=
congrArg s (h (r y))
/-- If placing undoes reading, a thing is what its readings hold. -/
theorem read_inj {A B : Type u} (read : A → B) (place : B → A)
(h : ∀ n, place (read n) = n) {m n : A} (e : read m = read n) : m = n :=
Eq.trans (Eq.symm (h m)) (Eq.trans (congrArg place e) (h n))
/-- The running sum: `Σf(0) = 0`, `Σf(n+1) = Σf(n) + f(n)`. -/
def runsum {A : Type u} (add : A → A → A) (zero : A) (f : Nat → A) : Nat → A
| 0 => zero
| n + 1 => add (runsum add zero f n) (f n)
/-- The step difference: `DF(n) = F(n+1) - F(n)`. -/
def stepdiff {A : Type u} (sub : A → A → A) (F : Nat → A) (n : Nat) : A :=
sub (F (n + 1)) (F n)
/-- `D ∘ Σ = id`. -/
theorem diff_sum {A : Type u} (add sub : A → A → A) (zero : A)
(cancel : ∀ x y, sub (add x y) x = y)
(f : Nat → A) (n : Nat) :
stepdiff sub (runsum add zero f) n = f n :=
cancel (runsum add zero f n) (f n)
theorem runsum_congr {A : Type u} (add : A → A → A) (zero : A) (f g : Nat → A)
(h : ∀ k, f k = g k) : ∀ n : Nat, runsum add zero f n = runsum add zero g n
| 0 => rfl
| n + 1 =>
Eq.trans
(congrArg (fun t => add t (f n)) (runsum_congr add zero f g h n))
(congrArg (fun t => add (runsum add zero g n) t) (h n))
/-- Telescoping: if `F(n+1) = F(n) + d(n)` then `F(n) = F(0) + Σd(n)`. -/
theorem telescope {A : Type u} (add : A → A → A) (zero : A)
(assoc : ∀ x y z, add (add x y) z = add x (add y z))
(add_zero' : ∀ x, add x zero = x)
(F d : Nat → A) (h : ∀ n, F (n + 1) = add (F n) (d n)) :
∀ n : Nat, F n = add (F 0) (runsum add zero d n)
| 0 => Eq.symm (add_zero' (F 0))
| n + 1 =>
Eq.trans (h n)
(Eq.trans
(congrArg (fun t => add t (d n)) (telescope add zero assoc add_zero' F d h n))
(assoc (F 0) (runsum add zero d n) (d n)))
/-- `F(n) = F(0) + Σ(DF)(n)`. -/
theorem sum_diff {A : Type u} (add sub : A → A → A) (zero : A)
(assoc : ∀ x y z, add (add x y) z = add x (add y z))
(add_zero' : ∀ x, add x zero = x)
(uncancel : ∀ x y, add x (sub y x) = y)
(F : Nat → A) (n : Nat) :
F n = add (F 0) (runsum add zero (stepdiff sub F) n) :=
telescope add zero assoc add_zero' F (stepdiff sub F)
(fun k => Eq.symm (uncancel (F k) (F (k + 1)))) n
/-- `Σ ∘ D` is idempotent. -/
theorem sumdiff_idem {A : Type u} (add sub : A → A → A) (zero : A)
(cancel : ∀ x y, sub (add x y) x = y)
(F : Nat → A) (n : Nat) :
runsum add zero (stepdiff sub (runsum add zero (stepdiff sub F))) n
= runsum add zero (stepdiff sub F) n :=
runsum_congr add zero (stepdiff sub (runsum add zero (stepdiff sub F))) (stepdiff sub F)
(fun k => diff_sum add sub zero cancel (stepdiff sub F) k) n
/-- Telescoping up to a sameness `R` (used for the derived integers). -/
theorem telescopeR {A : Type u} (R : A → A → Prop) (add : A → A → A) (zero : A)
(rtrans : ∀ x y z, R x y → R y z → R x z)
(congr_left : ∀ x x' y, R x x' → R (add x y) (add x' y))
(assocR : ∀ x y z, R (add (add x y) z) (add x (add y z)))
(zeroR : ∀ x, R x (add x zero))
(F d : Nat → A) (h : ∀ n, R (F (n + 1)) (add (F n) (d n))) :
∀ n : Nat, R (F n) (add (F 0) (runsum add zero d n))
| 0 => zeroR (F 0)
| n + 1 =>
rtrans _ _ _ (h n)
(rtrans _ _ _
(congr_left _ _ (d n)
(telescopeR R add zero rtrans congr_left assocR zeroR F d h n))
(assocR (F 0) (runsum add zero d n) (d n)))
/-! ## Part B. Addition derived: composing "n times" -/
/-- A number is "do it n times". -/
abbrev CNum : Type 1 := ∀ A : Type, (A → A) → A → A
def czero : CNum := fun _ _ a => a
def cadd (m n : CNum) : CNum := fun A f a => n A f (m A f a)
theorem cadd_assoc (x y z : CNum) : cadd (cadd x y) z = cadd x (cadd y z) := rfl
theorem cadd_zero (x : CNum) : cadd x czero = x := rfl
/-- Telescoping with the derived addition. Both laws are `rfl`. -/
theorem telescope_church (F d : Nat → CNum)
(h : ∀ n, F (n + 1) = cadd (F n) (d n)) (n : Nat) :
F n = cadd (F 0) (runsum cadd czero d n) :=
telescope cadd czero cadd_assoc cadd_zero F d h n
/-! ## Part C. Subtraction derived: pairs, minus = swap, zero = diagonal -/
/-! The laws of `+` on numbers, from `a + 0 = a` and `a + (b+1) = (a+b) + 1`. -/
theorem nadd_assoc : ∀ (a b c : Nat), a + b + c = a + (b + c)
| _, _, 0 => rfl
| a, b, c + 1 => congrArg Nat.succ (nadd_assoc a b c)
theorem nzero_add : ∀ (a : Nat), 0 + a = a
| 0 => rfl
| a + 1 => congrArg Nat.succ (nzero_add a)
theorem nsucc_add : ∀ (a b : Nat), Nat.succ a + b = Nat.succ (a + b)
| _, 0 => rfl
| a, b + 1 => congrArg Nat.succ (nsucc_add a b)
theorem nadd_comm : ∀ (a b : Nat), a + b = b + a
| a, 0 => Eq.symm (nzero_add a)
| a, b + 1 =>
Eq.trans (congrArg Nat.succ (nadd_comm a b)) (Eq.symm (nsucc_add b a))
theorem four (a b c d : Nat) : a + b + c + d = c + d + a + b :=
Eq.trans (nadd_assoc (a + b) c d)
(Eq.symm (Eq.trans (nadd_assoc (c + d) a b) (nadd_comm (c + d) (a + b))))
theorem four' (a b c d : Nat) : a + (b + c) + d = c + (d + a) + b :=
Eq.trans
(Eq.trans
(congrArg (fun t => t + d) (Eq.symm (nadd_assoc a b c)))
(nadd_assoc (a + b) c d))
(Eq.symm
(Eq.trans
(congrArg (fun t => t + b) (Eq.symm (nadd_assoc c d a)))
(Eq.trans (nadd_assoc (c + d) a b) (nadd_comm (c + d) (a + b)))))
/-- Sameness of pairs read as differences: `(a,b) ~ (c,d)` when `a + d = b + c`. -/
def ksame (p q : Nat × Nat) : Prop := p.1 + q.2 = p.2 + q.1
def padd (p q : Nat × Nat) : Nat × Nat := (p.1 + q.1, p.2 + q.2)
def pneg (p : Nat × Nat) : Nat × Nat := (p.2, p.1)
def psub (p q : Nat × Nat) : Nat × Nat := padd p (pneg q)
/-- Minus is the swap, and swapping twice is the identity. -/
theorem pneg_pneg (p : Nat × Nat) : pneg (pneg p) = p := rfl
/-- `x + (-x)` lies on the diagonal, which is zero. -/
theorem padd_pneg (p : Nat × Nat) : ksame (padd p (pneg p)) (0, 0) :=
show p.1 + p.2 + 0 = p.2 + p.1 + 0 from
congrArg (fun t => t + 0) (nadd_comm p.1 p.2)
/-- The cancel law `(x + y) - x = y`. -/
theorem psub_cancel (x y : Nat × Nat) : ksame (psub (padd x y) x) y :=
show x.1 + y.1 + x.2 + y.2 = x.2 + y.2 + x.1 + y.1 from
four x.1 y.1 x.2 y.2
/-- The other cancel law `x + (y - x) = y`. -/
theorem psub_uncancel (x y : Nat × Nat) : ksame (padd x (psub y x)) y :=
show x.1 + (y.1 + x.2) + y.2 = x.2 + (y.2 + x.1) + y.1 from
four' x.1 y.1 x.2 y.2
/-! ### The integers derived: pairs of numbers with the sameness `ksame`
An integer is a pair `(a, b)` read as `a - b`. Two pairs are the same
integer when `ksame` holds. Below: `ksame` is reflexive, symmetric and
transitive, addition respects it, and both halves of the discrete
fundamental theorem of calculus hold for these integers.
-/
theorem nadd_right_cancel : ∀ (a b k : Nat), a + k = b + k → a = b
| _, _, 0, h => h
| a, b, k + 1, h =>
nadd_right_cancel a b k
(Nat.succ.inj (show Nat.succ (a + k) = Nat.succ (b + k) from h))
/-- `(a + b) + (c + d) = (a + d) + (b + c)`. -/
theorem swap4 (a b c d : Nat) : (a + b) + (c + d) = (a + d) + (b + c) :=
Eq.trans (nadd_assoc a b (c + d))
(Eq.trans
(congrArg (fun t => a + t)
(Eq.trans (Eq.symm (nadd_assoc b c d)) (nadd_comm (b + c) d)))
(Eq.symm (nadd_assoc a d (b + c))))
/-- `(a + b) + (c + d) = (a + c) + (b + d)`. -/
theorem mid4 (a b c d : Nat) : (a + b) + (c + d) = (a + c) + (b + d) :=
Eq.trans (nadd_assoc a b (c + d))
(Eq.trans
(congrArg (fun t => a + t)
(Eq.trans (Eq.symm (nadd_assoc b c d))
(Eq.trans (congrArg (fun t => t + d) (nadd_comm b c)) (nadd_assoc c b d))))
(Eq.symm (nadd_assoc a c (b + d))))
theorem ktrans_nat (p1 p2 q1 q2 r1 r2 : Nat)
(h1 : p1 + q2 = p2 + q1) (h2 : q1 + r2 = q2 + r1) : p1 + r2 = p2 + r1 :=
nadd_right_cancel (p1 + r2) (p2 + r1) (q2 + q1)
(Eq.trans (Eq.symm (swap4 p1 q2 q1 r2))
(Eq.trans
(Eq.trans
(congrArg (fun t => t + (q1 + r2)) h1)
(congrArg (fun t => (p2 + q1) + t) h2))
(Eq.trans (swap4 p2 q1 q2 r1)
(congrArg (fun t => (p2 + r1) + t) (nadd_comm q1 q2)))))
theorem kcongr_nat (x1 x2 x1' x2' y1 y2 : Nat) (h : x1 + x2' = x2 + x1') :
(x1 + y1) + (x2' + y2) = (x2 + y2) + (x1' + y1) :=
Eq.trans (mid4 x1 y1 x2' y2)
(Eq.trans
(congrArg (fun t => t + (y1 + y2)) h)
(Eq.symm
(Eq.trans (mid4 x2 y2 x1' y1)
(congrArg (fun t => (x2 + x1') + t) (nadd_comm y2 y1)))))
def pzero : Nat × Nat := (0, 0)
theorem ksame_refl (p : Nat × Nat) : ksame p p :=
show p.1 + p.2 = p.2 + p.1 from nadd_comm p.1 p.2
theorem ksame_symm (p q : Nat × Nat) (h : ksame p q) : ksame q p :=
show q.1 + p.2 = q.2 + p.1 from
Eq.trans (nadd_comm q.1 p.2) (Eq.trans (Eq.symm h) (nadd_comm p.1 q.2))
theorem ksame_trans (p q r : Nat × Nat) (h1 : ksame p q) (h2 : ksame q r) : ksame p r :=
show p.1 + r.2 = p.2 + r.1 from
ktrans_nat p.1 p.2 q.1 q.2 r.1 r.2 h1 h2
/-- Addition respects the sameness. -/
theorem padd_congr_left (x x' y : Nat × Nat) (h : ksame x x') :
ksame (padd x y) (padd x' y) :=
show (x.1 + y.1) + (x'.2 + y.2) = (x.2 + y.2) + (x'.1 + y.1) from
kcongr_nat x.1 x.2 x'.1 x'.2 y.1 y.2 h
theorem padd_assoc (x y z : Nat × Nat) : padd (padd x y) z = padd x (padd y z) :=
show ((x.1 + y.1 + z.1, x.2 + y.2 + z.2) : Nat × Nat)
= (x.1 + (y.1 + z.1), x.2 + (y.2 + z.2)) from
Eq.trans
(congrArg (fun t => ((t, x.2 + y.2 + z.2) : Nat × Nat)) (nadd_assoc x.1 y.1 z.1))
(congrArg (fun t => ((x.1 + (y.1 + z.1), t) : Nat × Nat)) (nadd_assoc x.2 y.2 z.2))
theorem padd_assocR (x y z : Nat × Nat) :
ksame (padd (padd x y) z) (padd x (padd y z)) :=
Eq.mpr (congrArg (fun t => ksame t (padd x (padd y z))) (padd_assoc x y z))
(ksame_refl (padd x (padd y z)))
theorem padd_zeroR (x : Nat × Nat) : ksame x (padd x pzero) :=
show x.1 + x.2 = x.2 + x.1 from nadd_comm x.1 x.2
/-- `D ∘ Σ = id` on the derived integers. -/
theorem diff_sum_pair (f : Nat → Nat × Nat) (n : Nat) :
ksame (stepdiff psub (runsum padd pzero f) n) (f n) :=
psub_cancel (runsum padd pzero f n) (f n)
/-- `F(n) = F(0) + Σ(DF)(n)` on the derived integers. -/
theorem sum_diff_pair (F : Nat → Nat × Nat) (n : Nat) :
ksame (F n) (padd (F 0) (runsum padd pzero (stepdiff psub F) n)) :=
telescopeR ksame padd pzero ksame_trans padd_congr_left padd_assocR padd_zeroR
F (stepdiff psub F)
(fun k => ksame_symm _ _ (psub_uncancel (F k) (F (k + 1)))) n
/-! ## Part D. The discrete theorem in the two-valued world (1 + 1 = 0) -/
/-- Addition of the two values. It is also their subtraction. -/
def bx : Bool → Bool → Bool
| true, true => false
| true, false => true
| false, true => true
| false, false => false
/-- `a + a = 0`. -/
theorem bx_self (x : Bool) : bx x x = false := by
cases x <;> rfl
theorem bx_zero (x : Bool) : bx x false = x := by
cases x <;> rfl
theorem bx_assoc (x y z : Bool) : bx (bx x y) z = bx x (bx y z) := by
cases x <;> cases y <;> cases z <;> rfl
theorem bx_cancel (x y : Bool) : bx (bx x y) x = y := by
cases x <;> cases y <;> rfl
theorem bx_uncancel (x y : Bool) : bx x (bx y x) = y := by
cases x <;> cases y <;> rfl
/-- `D ∘ Σ = id` on the two values. -/
theorem diff_sum_two (f : Nat → Bool) (n : Nat) :
stepdiff bx (runsum bx false f) n = f n :=
diff_sum bx bx false bx_cancel f n
/-- `F(n) = F(0) + Σ(DF)(n)` on the two values. -/
theorem sum_diff_two (F : Nat → Bool) (n : Nat) :
F n = bx (F 0) (runsum bx false (stepdiff bx F) n) :=
sum_diff bx bx false bx_assoc bx_zero bx_uncancel F n
/-- `Σ ∘ D` is idempotent on the two values. -/
theorem sumdiff_idem_two (F : Nat → Bool) (n : Nat) :
runsum bx false (stepdiff bx (runsum bx false (stepdiff bx F))) n
= runsum bx false (stepdiff bx F) n :=
sumdiff_idem bx bx false bx_cancel F n
/-! ## Part E. The Euclid step and the divisibility line -/
def ustep (a b : Nat) : Nat × Nat :=
if b < a then (a - b, b) else if a < b then (a, b - a) else (a, b)
/-- The Euclid step fixes the diagonal. -/
theorem ustep_diag (a : Nat) : ustep a a = (a, a) := by
rw [ustep, ite_eq_right (Nat.lt_irrefl a), ite_eq_right (Nat.lt_irrefl a)]
/-- From `b = (x b) p + y (a b)` and `a b = c p`: `b = (x b + y c) p`. -/
theorem euclid_line {R : Type u} (add mul : R → R → R)
(mul_assoc' : ∀ u v w, mul (mul u v) w = mul u (mul v w))
(right_distrib' : ∀ u v w, mul (add u v) w = add (mul u w) (mul v w))
(p a b x y c : R)
(hb : b = add (mul (mul x b) p) (mul y (mul a b)))
(hab : mul a b = mul c p) :
b = mul (add (mul x b) (mul y c)) p :=
Eq.trans hb
(Eq.trans
(congrArg (fun t => add (mul (mul x b) p) (mul y t)) hab)
(Eq.trans
(congrArg (fun t => add (mul (mul x b) p) t) (Eq.symm (mul_assoc' y c p)))
(Eq.symm (right_distrib' (mul x b) (mul y c) p))))
/-! ## Axiom check for Parts A–E (all should report no axioms) -/
#print axioms split_idem
#print axioms read_inj
#print axioms diff_sum
#print axioms telescope
#print axioms sum_diff
#print axioms sumdiff_idem
#print axioms telescope_church
#print axioms nadd_comm
#print axioms pneg_pneg
#print axioms padd_pneg
#print axioms psub_cancel
#print axioms psub_uncancel
#print axioms ksame_trans
#print axioms padd_congr_left
#print axioms diff_sum_pair
#print axioms sum_diff_pair
#print axioms bx_self
#print axioms diff_sum_two
#print axioms sum_diff_two
#print axioms sumdiff_idem_two
#print axioms ustep_diag
#print axioms euclid_line
end FundAll
/-! ## Part F. The three full theorems, as they stand in the library -/
-- discrete calculus
#check @Finset.sum_range_succ
#check @Finset.sum_range_sub
-- fundamental theorem of calculus, both halves
#check @intervalIntegral.integral_hasDerivAt_right
#check @intervalIntegral.integral_eq_sub_of_hasDerivAt
-- fundamental theorem of arithmetic
#check @Nat.Prime.dvd_mul
#check @Nat.prod_primeFactorsList
#check @Nat.primeFactorsList_unique
-- fundamental theorem of algebra
#check @Complex.exists_root