These are the Lean 4 files behind the mysterious polynomial, checked in Lean, copied unchanged from my private research repository. The comments in them point to files in that repository (the thesis source and the September 2026 reports), which are not public; the write-up covers what they say.

Download: tatami-lean.zip (44 KB), the whole project without build output. Its SHA-256 is

2518b9c77ad3f3ebf4326eee07b2328553496aa8f62004f99b43079c14eb0355

Unzip it, then run lake exe cache get and lake build inside tatami-lean/. The write-up says what to expect.

There are 2,637 lines of Lean in 17 files, listed in the order the library imports them:

File Lines What it holds
lean-toolchain 1 the Lean version.
lakefile.toml 14 the project and its Mathlib pin.
Tatami.lean 15 the library root: imports every file below.
Tatami/Basic.lean 70 the four definitions S, VH, D, P. The file to read.
Tatami/Cyclotomic.lean 208 thesis thm:cyclotomic: D n ∣ VH n, so VH n = D n * P n.
Tatami/ConjA.lean 70 conj:Pcon(a) as printed is false: P 4, P 6, P 10 computed exactly.
Tatami/AtMinusOne.lean 375 order and leading constant at z = −1; (e) for even n.
Tatami/AtMinusOneOdd.lean 212 (e) for odd n, by a finite recurrence.
Tatami/LowCoefficients.lean 222 Lemma A, thesis conj:vhcon(a), and the corrected (a).
Tatami/ConjCCore.lean 59 (c) assembled from three sign facts about VH n.
Tatami/RealVH.lean 364 the sign facts for even n (written by a helper agent).
Tatami/ConjC.lean 21 (c): for even n, P n has no real root.
Tatami/OddRoot.lean 51 odd n: a real root in (−1, 0), by the intermediate value theorem.
Tatami/ModTwo.lean 177 S_m² = D n · B n and P n ≡ B n (mod 2).
Tatami/TopDegree.lean 139 degree and leading coefficient of VH n and P n.
Tatami/TopCoefficients.lean 127 thesis conj:vhcon(b), the top coefficients of VH n.
Tatami/RealVHOdd.lean 441 the sign facts for odd n (written by a helper agent).
Tatami/RealRoots.lean 59 every real root lies in (−1, −1/2); odd n ≥ 5 has one there.
Audit/Axioms.lean 27 prints the axioms each main theorem uses.
lake-manifest.json 96 the exact Mathlib commit.
README.md 93 the project’s own notes, shown as plain text.

lean-toolchain

leanprover/lean4:v4.36.0

lakefile.toml

name = "tatami"
defaultTargets = ["Tatami"]

[leanOptions]
pp.unicode.fun = true
autoImplicit = false

[[require]]
name = "mathlib"
scope = "leanprover-community"
rev = "v4.36.0"

[[lean_lib]]
name = "Tatami"

Tatami.lean

import Tatami.Basic
import Tatami.Cyclotomic
import Tatami.ConjA
import Tatami.AtMinusOne
import Tatami.AtMinusOneOdd
import Tatami.LowCoefficients
import Tatami.ConjCCore
import Tatami.RealVH
import Tatami.ConjC
import Tatami.OddRoot
import Tatami.ModTwo
import Tatami.TopDegree
import Tatami.TopCoefficients
import Tatami.RealVHOdd
import Tatami.RealRoots

Tatami/Basic.lean

import Mathlib

/-!
# The polynomials `S k`, `VH n`, `D n` and `P n`

The definitions are transcribed from the thesis, Ch. 4
(`papers/2013-phd-thesis-monomino-domino-tatami-coverings/chapters/4/chapter_vdimers.tex`),
and agree with `code/pn-polynomial/pn.py`.

**These four definitions are what a human reader has to check.** Every theorem in this
library is about the polynomials defined here; Lean checks the proofs, but only a reader can
confirm that the definitions mean what the thesis means.

Two Lean conventions to keep in mind when reading them:
* `n - 2` on natural numbers is *truncated*: it is `0` when `n < 2`.
* `a / b` on natural numbers is *floor division*: `7 / 2 = 3`.
-/

open Polynomial

namespace Tatami

/-- `S k = (1 + X) (1 + X²) ⋯ (1 + X^k)`, and `S 0 = 1` (the empty product). -/
noncomputable def S (k : ℕ) : ℤ[X] := ∏ i ∈ Finset.Icc 1 k, (1 + X ^ i)

/-- `VH n`, thesis `thm:vdominoes`:
`VH_n(z) = 2 ∑_{i=1}^{⌊(n−1)/2⌋} S_{n−i−2}(z) S_{i−1}(z) z^{n−i−1} + S_{⌊(n−2)/2⌋}(z)²`. -/
noncomputable def VH (n : ℕ) : ℤ[X] :=
  2 * ∑ i ∈ Finset.Icc 1 ((n - 1) / 2), S (n - i - 2) * S (i - 1) * X ^ (n - i - 1)
    + S ((n - 2) / 2) ^ 2

/-- `D n = ∏_{j ≥ 1} S_{⌊(n−2)/2^j⌋}`, thesis `thm:cyclotomic`. The factors with
`2^j > n − 2` are `S 0 = 1`, so stopping the product at `j = n` changes nothing. -/
noncomputable def D (n : ℕ) : ℤ[X] := ∏ j ∈ Finset.Icc 1 n, S ((n - 2) / 2 ^ j)

/-- The mysterious polynomial `P n = VH n / D n`. Here `/ₘ` is division by a monic
polynomial. That the division is exact is thesis `thm:cyclotomic`, proved in
`Tatami.Cyclotomic` as `Tatami.D_dvd_VH`, which gives `VH n = D n * P n`. -/
noncomputable def P (n : ℕ) : ℤ[X] := VH n /ₘ D n

/-! ## Basic facts -/

@[simp] lemma S_zero : S 0 = 1 := by simp [S]

lemma S_succ (k : ℕ) : S (k + 1) = S k * (1 + X ^ (k + 1)) := by
  rw [S, S, Finset.prod_Icc_succ_top (by omega)]

lemma one_add_X_pow_monic {i : ℕ} (hi : i ≠ 0) : (1 + X ^ i : ℤ[X]).Monic := by
  rw [add_comm]
  simpa using monic_X_pow_add_C (1 : ℤ) hi

lemma S_monic (k : ℕ) : (S k).Monic :=
  monic_prod_of_monic _ _ fun i hi => one_add_X_pow_monic (by simp at hi; omega)

lemma D_monic (n : ℕ) : (D n).Monic :=
  monic_prod_of_monic _ _ fun _ _ => S_monic _

/-- `VH` with `n = N + 2` written without truncated subtraction. -/
lemma VH_add_two (N : ℕ) :
    VH (N + 2) = 2 * ∑ i ∈ Finset.Icc 1 ((N + 1) / 2), S (N - i) * S (i - 1) * X ^ (N + 1 - i)
      + S (N / 2) ^ 2 := by
  rw [VH]
  congr 3
  funext i
  rw [show N + 2 - i - 2 = N - i by omega, show N + 2 - i - 1 = N + 1 - i by omega]

lemma D_add_two (N : ℕ) : D (N + 2) = ∏ j ∈ Finset.Icc 1 (N + 2), S (N / 2 ^ j) := by
  simp [D]

end Tatami

Tatami/Cyclotomic.lean

import Tatami.Basic

/-!
# Thesis `thm:cyclotomic`: `D n` divides `VH n`

The thesis proof: write every factor `1 + z^i` as a product of cyclotomic polynomials, and
check that each `Φ_d` occurs in `D n` no more often than in every term of `VH n`.

* `1 + X^i = ∏ Φ_d` over the divisors `d` of `2i` that do not divide `i`.
* So `Φ_d` occurs in `S k` exactly `cnt k d` times, where
  `cnt k d = #{1 ≤ i ≤ k : d ∣ 2i, d ∤ i}`; this is `0` for odd `d`, and
  `k / j − k / (2j)` for `d = 2j`.
* In `D (N+2)` the counts telescope to `N / (2j)`; in a term `S a * S b` with
  `a + b + 1 ≥ N` they are at least that.
-/

open Polynomial

namespace Tatami

lemma one_add_X_pow_eq_prod_cyclotomic {i : ℕ} (hi : 0 < i) :
    (1 + X ^ i : ℤ[X]) = ∏ d ∈ (2 * i).divisors.filter (fun d => ¬ d ∣ i), cyclotomic d ℤ := by
  have h2 := prod_cyclotomic_eq_X_pow_sub_one (show 0 < 2 * i by omega) ℤ
  have h1 := prod_cyclotomic_eq_X_pow_sub_one hi ℤ
  rw [← Finset.prod_filter_mul_prod_filter_not (2 * i).divisors (fun d => d ∣ i)] at h2
  have hfilt : (2 * i).divisors.filter (fun d => d ∣ i) = i.divisors := by
    ext d
    simp only [Finset.mem_filter, Nat.mem_divisors]
    constructor
    · rintro ⟨⟨_, _⟩, h⟩; exact ⟨h, hi.ne'⟩
    · rintro ⟨h, _⟩; exact ⟨⟨Dvd.dvd.mul_left h 2, by omega⟩, h⟩
  rw [hfilt, h1] at h2
  have hne : (X ^ i - 1 : ℤ[X]) ≠ 0 := by
    have := (monic_X_pow_sub_C (1 : ℤ) hi.ne').ne_zero
    simpa using this
  apply mul_left_cancel₀ hne
  rw [h2]
  ring

/-- How many times `Φ_d` divides `S k`. -/
def cnt (k d : ℕ) : ℕ := ((Finset.Icc 1 k).filter (fun i => d ∣ 2 * i ∧ ¬ d ∣ i)).card

lemma S_eq_prod_cyclotomic (k R : ℕ) (hR : 2 * k < R) :
    S k = ∏ d ∈ Finset.range R, cyclotomic d ℤ ^ cnt k d := by
  unfold S cnt
  have key : ∀ i ∈ Finset.Icc 1 k, (1 + X ^ i : ℤ[X]) =
      ∏ d ∈ Finset.range R, if d ∣ 2 * i ∧ ¬ d ∣ i then cyclotomic d ℤ else 1 := by
    intro i hi
    rw [Finset.mem_Icc] at hi
    rw [one_add_X_pow_eq_prod_cyclotomic (by omega), ← Finset.prod_filter]
    congr 1
    ext d
    simp only [Finset.mem_filter, Nat.mem_divisors, Finset.mem_range]
    constructor
    · rintro ⟨⟨h, _⟩, h'⟩
      exact ⟨(Nat.le_of_dvd (by omega) h).trans_lt (by omega), h, h'⟩
    · rintro ⟨_, h, h'⟩
      exact ⟨⟨h, by omega⟩, h'⟩
  rw [Finset.prod_congr rfl key, Finset.prod_comm]
  apply Finset.prod_congr rfl
  intro d _
  rw [← Finset.prod_filter, Finset.prod_const]

/-- If every `Φ_d` occurs at most as often in the `S (m l)` together as in `S a` and `S b`
together, then the product divides. -/
lemma prod_S_dvd {ι : Type*} (s : Finset ι) (m : ι → ℕ) (a b : ℕ)
    (h : ∀ d, ∑ l ∈ s, cnt (m l) d ≤ cnt a d + cnt b d) :
    ∏ l ∈ s, S (m l) ∣ S a * S b := by
  set R := 2 * (a + b + ∑ l ∈ s, m l) + 1 with hR
  have hml : ∀ l ∈ s, m l ≤ ∑ l ∈ s, m l := fun l hl =>
    Finset.single_le_sum (f := m) (fun _ _ => Nat.zero_le _) hl
  rw [Finset.prod_congr rfl (fun l hl => S_eq_prod_cyclotomic (m l) R
      (by have := hml l hl; omega)),
    S_eq_prod_cyclotomic a R (by omega), S_eq_prod_cyclotomic b R (by omega),
    Finset.prod_comm, ← Finset.prod_mul_distrib]
  apply Finset.prod_dvd_prod_of_dvd
  intro d _
  rw [Finset.prod_pow_eq_pow_sum, ← pow_add]
  exact pow_dvd_pow _ (h d)

/-! ## Counting -/

lemma cnt_zero_right (k : ℕ) : cnt k 0 = 0 := by
  unfold cnt
  rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff]
  intro i hi
  simp only [Finset.mem_Icc] at hi
  simp only [zero_dvd_iff, not_and, not_not]
  intro h; omega

lemma cnt_odd (k d : ℕ) (hd : ¬ 2 ∣ d) : cnt k d = 0 := by
  unfold cnt
  rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff]
  intro i _
  have hcop : Nat.Coprime d 2 :=
    (Nat.Prime.coprime_iff_not_dvd Nat.prime_two).mpr hd |>.symm
  rintro ⟨h, h'⟩
  exact h' (hcop.dvd_of_dvd_mul_left h)

lemma card_filter_dvd_Icc (k j : ℕ) :
    ((Finset.Icc 1 k).filter (fun i => j ∣ i)).card = k / j := by
  have : Finset.Icc 1 k = Finset.Ioc 0 k := by
    ext x; simp only [Finset.mem_Icc, Finset.mem_Ioc]; omega
  rw [this, Nat.Ioc_filter_dvd_card_eq_div]

lemma cnt_even (k j : ℕ) : cnt k (2 * j) = k / j - k / (2 * j) := by
  unfold cnt
  have hsplit : (Finset.Icc 1 k).filter (fun i => 2 * j ∣ 2 * i ∧ ¬ 2 * j ∣ i) =
      (Finset.Icc 1 k).filter (fun i => j ∣ i) \ (Finset.Icc 1 k).filter (fun i => 2 * j ∣ i) := by
    ext i
    simp only [Finset.mem_filter, Finset.mem_sdiff, Nat.mul_dvd_mul_iff_left (by omega : 0 < 2)]
    tauto
  have hsub : (Finset.Icc 1 k).filter (fun i => 2 * j ∣ i) ⊆
      (Finset.Icc 1 k).filter (fun i => j ∣ i) := by
    intro i
    simp only [Finset.mem_filter]
    rintro ⟨h1, h2⟩
    exact ⟨h1, Dvd.dvd.trans (Dvd.intro_left 2 rfl) h2⟩
  rw [hsplit, Finset.card_sdiff_of_subset hsub, card_filter_dvd_Icc, card_filter_dvd_Icc]

/-- A telescoping sum of an antitone sequence. -/
lemma sum_Icc_sub_le {x : ℕ → ℕ} (hx : Antitone x) (L : ℕ) :
    ∑ l ∈ Finset.Icc 1 L, (x l - x (l + 1)) ≤ x 1 := by
  suffices h : ∑ l ∈ Finset.Icc 1 L, (x l - x (l + 1)) + x (L + 1) = x 1 by omega
  induction L with
  | zero => simp
  | succ L ih =>
    rw [Finset.sum_Icc_succ_top (by omega)]
    have h1 : x (L + 1 + 1) ≤ x (L + 1) := hx (by omega)
    omega

lemma div_add_add_one_div_le (a b j : ℕ) (hj : 0 < j) : (a + b + 1) / j ≤ a / j + b / j + 1 := by
  have ha := Nat.lt_mul_div_succ a hj
  have hb := Nat.lt_mul_div_succ b hj
  have : (a + b + 1) / j < a / j + b / j + 2 := by
    rw [Nat.div_lt_iff_lt_mul hj]
    nlinarith
  omega

/-- The key inequality: `Φ_d` occurs in `D (N + 2)` at most as often as in `S a * S b`
whenever `a + b + 1 ≥ N`. -/
lemma cnt_D_le (N a b : ℕ) (hab : N ≤ a + b + 1) (L d : ℕ) :
    ∑ l ∈ Finset.Icc 1 L, cnt (N / 2 ^ l) d ≤ cnt a d + cnt b d := by
  rcases Nat.even_or_odd d with ⟨j, rfl⟩ | hodd
  · rcases Nat.eq_zero_or_pos j with rfl | hj
    · simp [cnt_zero_right]
    rw [show j + j = 2 * j by ring]
    simp only [cnt_even]
    set x : ℕ → ℕ := fun l => N / 2 ^ l / j with hx
    have hterm : ∀ l, N / 2 ^ l / j - N / 2 ^ l / (2 * j) = x l - x (l + 1) := by
      intro l
      simp only [hx, Nat.div_div_eq_div_mul, pow_succ]
      ring_nf
    simp only [hterm]
    have hanti : Antitone x := by
      intro l l' hll'
      simp only [hx]
      apply Nat.div_le_div_right
      apply Nat.div_le_div_left (Nat.pow_le_pow_right (by omega) hll') (by positivity)
    refine (sum_Icc_sub_le hanti L).trans ?_
    simp only [hx, pow_one]
    have h1 : N / 2 / j = N / j / 2 := by
      rw [Nat.div_div_eq_div_mul, Nat.div_div_eq_div_mul, mul_comm]
    have h2 : N / j ≤ (a + b + 1) / j := Nat.div_le_div_right hab
    have h3 := div_add_add_one_div_le a b j hj
    have h4 : a / (2 * j) = a / j / 2 := by
      rw [Nat.div_div_eq_div_mul, mul_comm]
    have h5 : b / (2 * j) = b / j / 2 := by
      rw [Nat.div_div_eq_div_mul, mul_comm]
    rw [h1, h4, h5]
    omega
  · have hd : ¬ 2 ∣ d := by
      rintro ⟨c, rfl⟩
      exact Nat.not_even_iff_odd.mpr hodd ⟨c, by ring⟩
    simp [cnt_odd _ _ hd]

/-- `D (N + 2)` divides `S a * S b` whenever `a + b + 1 ≥ N`. -/
lemma D_dvd_S_mul_S (N a b : ℕ) (hab : N ≤ a + b + 1) : D (N + 2) ∣ S a * S b := by
  rw [D_add_two]
  exact prod_S_dvd _ _ a b (cnt_D_le N a b hab (N + 2))

/-- **Thesis `thm:cyclotomic`**: `D n` divides `VH n`. -/
theorem D_dvd_VH (n : ℕ) : D n ∣ VH n := by
  rcases Nat.lt_or_ge n 2 with hn | hn
  · interval_cases n <;> simp [D, VH]
  obtain ⟨N, rfl⟩ : ∃ N, n = N + 2 := ⟨n - 2, by omega⟩
  rw [VH_add_two]
  apply dvd_add
  · apply Dvd.dvd.mul_left
    apply Finset.dvd_sum
    intro i hi
    rw [Finset.mem_Icc] at hi
    apply Dvd.dvd.mul_right
    exact D_dvd_S_mul_S N _ _ (by omega)
  · rw [sq]
    exact D_dvd_S_mul_S N _ _ (by omega)

/-- So `VH n = D n * P n`: the division defining `P n` is exact. -/
theorem VH_eq_D_mul_P (n : ℕ) : VH n = D n * P n := by
  have h := modByMonic_add_div (VH n) (D n)
  rw [(modByMonic_eq_zero_iff_dvd (D_monic n)).mpr (D_dvd_VH n), zero_add] at h
  exact h.symm

/-- `P n` is the only polynomial `Q` with `VH n = D n * Q`. -/
theorem P_eq_of_VH_eq {n : ℕ} {Q : ℤ[X]} (h : VH n = D n * Q) : P n = Q := by
  rw [P, h, mul_divByMonic_cancel_left _ (D_monic n)]

end Tatami

Tatami/ConjA.lean

import Tatami.Cyclotomic

/-!
# `conj:Pcon`(a) is false as printed

The thesis (Ch. 4) and the 2013 note *Three open problems* state:

> If k ≥ 1 and n ≡ 2 (mod 2^k), then [z^i]P_n(z) = [z^i]P_{n+j}(z)
> for i ≤ (n−2)/2^{k−1} and j ≤ 2^k.

`ConjA_as_printed` transcribes this literally. It fails already for n = 4, k = 1, j = 2,
i = 1: P_4 = 1 + z + 2z² but P_6 = 1 + 0·z + z² + 2z³ + 2z⁴ − 2z⁵ + 2z⁶.
(Source of the refutation: `code/pn-polynomial/reports/low-coefficients.md`, Corollary 3.)
-/

open Polynomial

namespace Tatami

/-- `conj:Pcon`(a), exactly as printed. `n % 2^k = 2 % 2^k` is `n ≡ 2 (mod 2^k)`;
`(n - 2) / 2^(k-1)` is floor division, which is harmless since `i` is a whole number. -/
def ConjA_as_printed : Prop :=
  ∀ k n i j : ℕ, 1 ≤ k → n % 2 ^ k = 2 % 2 ^ k → i ≤ (n - 2) / 2 ^ (k - 1) → j ≤ 2 ^ k →
    (P n).coeff i = (P (n + j)).coeff i

lemma S_one : S 1 = 1 + X := by simp [S]
lemma S_two : S 2 = (1 + X) * (1 + X ^ 2) := by rw [S_succ, S_one]
lemma S_three : S 3 = (1 + X) * (1 + X ^ 2) * (1 + X ^ 3) := by rw [S_succ, S_two]

theorem P_four : P 4 = 1 + X + 2 * X ^ 2 := by
  apply P_eq_of_VH_eq
  simp [VH, D, Finset.prod_Icc_succ_top, S_one]
  ring

theorem P_six : P 6 = 1 + X ^ 2 + 2 * X ^ 3 + 2 * X ^ 4 - 2 * X ^ 5 + 2 * X ^ 6 := by
  apply P_eq_of_VH_eq
  simp [VH, D, Finset.prod_Icc_succ_top, Finset.sum_Icc_succ_top, S_one, S_two, S_three]
  ring

lemma S_four : S 4 = (1 + X) * (1 + X ^ 2) * (1 + X ^ 3) * (1 + X ^ 4) := by rw [S_succ, S_three]
lemma S_five : S 5 = S 4 * (1 + X ^ 5) := S_succ 4
lemma S_six : S 6 = S 5 * (1 + X ^ 6) := S_succ 5
lemma S_seven : S 7 = S 6 * (1 + X ^ 7) := S_succ 6

theorem P_ten : P 10 = 1 - X + X ^ 2 + X ^ 4 + X ^ 5 + X ^ 6 + 2 * X ^ 7 + 2 * X ^ 8 - 6 * X ^ 9
    + 6 * X ^ 10 - 2 * X ^ 11 + 6 * X ^ 12 - 6 * X ^ 13 + 4 * X ^ 14 - 4 * X ^ 15 + 6 * X ^ 16
    - 6 * X ^ 17 + 6 * X ^ 18 - 4 * X ^ 19 + 4 * X ^ 20 - 4 * X ^ 21 + 2 * X ^ 22 := by
  apply P_eq_of_VH_eq
  simp [VH, D, Finset.prod_Icc_succ_top, Finset.sum_Icc_succ_top, S_one, S_two, S_three, S_four,
    S_five, S_six, S_seven]
  ring

/-- The conjecture is false: the counterexample n = 4, k = 1, j = 2, i = 1. -/
theorem conjA_as_printed_false : ¬ ConjA_as_printed := by
  intro h
  have := h 1 4 1 2 le_rfl (by norm_num) (by norm_num) (by norm_num)
  rw [P_four, show 4 + 2 = 6 from rfl, P_six] at this
  simp [coeff_one, coeff_X] at this

/-- Read literally, "n (mod 2^k) = 2" excludes k = 1. It still fails, at n = 6, k = 2, j = 4,
i = 1: [z]P_6 = 0 but [z]P_10 = −1. -/
theorem conjA_literal_false :
    ¬ ∀ k n i j : ℕ, 2 ≤ k → n % 2 ^ k = 2 → i ≤ (n - 2) / 2 ^ (k - 1) → j ≤ 2 ^ k →
      (P n).coeff i = (P (n + j)).coeff i := by
  intro h
  have := h 2 6 1 4 le_rfl (by norm_num) (by norm_num) (by norm_num)
  rw [P_six, show 6 + 4 = 10 from rfl, P_ten] at this
  simp [coeff_one, coeff_X] at this

end Tatami

Tatami/AtMinusOne.lean

import Tatami.Cyclotomic

/-!
# `conj:Pcon`(e): the value `P_n(−1)`

Source: `code/pn-polynomial/reports/at-minus-one.md`.

Both `VH n` and `D n` vanish at `z = −1`, so `P n (−1)` cannot be read off by substituting.
Instead, substitute `z = w − 1` and compare the lowest power of `w` in each. `HasLead F k L`
says that `F(w − 1) = w^k (L + w·(…))`: `F` vanishes to order at least `k` at `−1`, and `L`
is the coefficient of `(z + 1)^k`. (If `L = 0` the order is really higher.)

* `S k` has order `⌈k/2⌉ = (k + 1) / 2` and leading constant `lead k`, the product of the odd
  `i ≤ k` and a `2` for each even `i ≤ k`. (`lead k = k! / ⌊k/2⌋!`.)
* `D (2a+2)` has order `a` and leading constant `a!` (the products telescope).
* In `VH (2a+2)` every term has order `a`. Neighbouring terms cancel in pairs, and what is
  left is `lead (2a) = (2a)! / a!`.
* So `P_{2a+2}(−1) = (2a)! / (a! a!)`, the central binomial coefficient.
-/

open Polynomial

namespace Tatami

/-! ## Order and leading constant at `z = −1` -/

/-- `F(w − 1) = w^k (L + w·G(w))` for some polynomial `G`. -/
def HasLead (F : ℤ[X]) (k : ℕ) (L : ℤ) : Prop :=
  ∃ G : ℤ[X], F.comp (X - 1) = X ^ k * (C L + X * G)

namespace HasLead

variable {F G : ℤ[X]} {k k' : ℕ} {L L' : ℤ}

lemma mul (hF : HasLead F k L) (hG : HasLead G k' L') : HasLead (F * G) (k + k') (L * L') := by
  obtain ⟨A, hA⟩ := hF
  obtain ⟨B, hB⟩ := hG
  refine ⟨C L * B + A * C L' + X * A * B, ?_⟩
  rw [mul_comp, hA, hB, C_mul]
  ring

lemma add (hF : HasLead F k L) (hG : HasLead G k L') : HasLead (F + G) k (L + L') := by
  obtain ⟨A, hA⟩ := hF
  obtain ⟨B, hB⟩ := hG
  refine ⟨A + B, ?_⟩
  rw [add_comp, hA, hB, C_add]
  ring

/-- A higher order of vanishing means leading constant `0` at a lower order. -/
lemma lower (hF : HasLead F k' L) (h : k < k') : HasLead F k 0 := by
  obtain ⟨A, hA⟩ := hF
  refine ⟨X ^ (k' - k - 1) * (C L + X * A), ?_⟩
  rw [hA, C_0, zero_add]
  rw [show k' = k + 1 + (k' - k - 1) by omega]
  rw [show k + 1 + (k' - k - 1) - k - 1 = k' - k - 1 by omega]
  ring

/-- The leading constant at a given order is unique. -/
lemma unique (hF : HasLead F k L) (hF' : HasLead F k L') : L = L' := by
  obtain ⟨A, hA⟩ := hF
  obtain ⟨B, hB⟩ := hF'
  have h : C L + X * A = C L' + X * B :=
    mul_left_cancel₀ (pow_ne_zero k X_ne_zero) (hA.symm.trans hB)
  have := congrArg (fun p => p.coeff 0) h
  simpa using this

lemma one : HasLead 1 0 1 := ⟨0, by simp⟩

lemma prod {ι : Type*} (s : Finset ι) (F : ι → ℤ[X]) (k : ι → ℕ) (L : ι → ℤ)
    (h : ∀ i ∈ s, HasLead (F i) (k i) (L i)) :
    HasLead (∏ i ∈ s, F i) (∑ i ∈ s, k i) (∏ i ∈ s, L i) := by
  classical
  induction s using Finset.induction_on with
  | empty => simpa using one
  | insert a s ha ih =>
    rw [Finset.prod_insert ha, Finset.sum_insert ha, Finset.prod_insert ha]
    exact (h a (Finset.mem_insert_self a s)).mul
      (ih fun i hi => h i (Finset.mem_insert_of_mem hi))

lemma sum {ι : Type*} (s : Finset ι) (F : ι → ℤ[X]) (L : ι → ℤ)
    (h : ∀ i ∈ s, HasLead (F i) k (L i)) :
    HasLead (∑ i ∈ s, F i) k (∑ i ∈ s, L i) := by
  classical
  induction s using Finset.induction_on with
  | empty => exact ⟨0, by simp⟩
  | insert a s ha ih =>
    rw [Finset.sum_insert ha, Finset.sum_insert ha]
    exact (h a (Finset.mem_insert_self a s)).add
      (ih fun i hi => h i (Finset.mem_insert_of_mem hi))

lemma pow (hF : HasLead F k L) (r : ℕ) : HasLead (F ^ r) (k * r) (L ^ r) := by
  induction r with
  | zero => simpa using one
  | succ r ih =>
    rw [pow_succ, pow_succ, show k * (r + 1) = k * r + k by ring]
    exact ih.mul hF

end HasLead

/-- Any polynomial `Q` equals `Q(0) + w·(Q'(0) + w·(…))`. -/
lemma eq_C_add_X_mul (Q : ℤ[X]) :
    Q = C (Q.coeff 0) + X * (C (Q.coeff 1) + X * Q.divX.divX) := by
  have h1 := X_mul_divX_add Q
  have h2 := X_mul_divX_add Q.divX
  rw [coeff_divX] at h2
  calc Q = X * Q.divX + C (Q.coeff 0) := h1.symm
    _ = X * (X * Q.divX.divX + C (Q.coeff (0 + 1))) + C (Q.coeff 0) := by rw [h2]
    _ = _ := by simp; ring

/-- Every polynomial has order `≥ 0` at `−1`, with leading constant its value there. -/
lemma hasLead_zero (F : ℤ[X]) : HasLead F 0 (F.eval (-1)) := by
  refine ⟨(F.comp (X - 1)).divX, ?_⟩
  have h := X_mul_divX_add (F.comp (X - 1))
  have h0 : (F.comp (X - 1)).coeff 0 = F.eval (-1) := by
    rw [coeff_zero_eq_eval_zero, eval_comp]
    simp
  rw [h0] at h
  rw [pow_zero, one_mul]
  linear_combination -h

lemma hasLead_one_add_X_pow_odd {i : ℕ} (hi : i % 2 = 1) :
    HasLead (1 + X ^ i) 1 (i : ℤ) := by
  set Q := (1 + X ^ i : ℤ[X]).comp (X - 1) with hQ
  have hQ' : Q = 1 + (X + C (-1)) ^ i := by
    rw [hQ, add_comp, pow_comp, X_comp, one_comp]
    simp [sub_eq_add_neg]
  have hodd : Odd i := Nat.odd_iff.mpr hi
  have h0 : Q.coeff 0 = 0 := by
    rw [hQ', coeff_add, coeff_X_add_C_pow]
    simp [hodd.neg_one_pow]
  have h1 : Q.coeff 1 = i := by
    rw [hQ', coeff_add, coeff_X_add_C_pow]
    have : Even (i - 1) := by
      rcases hodd with ⟨t, ht⟩
      exact ⟨t, by omega⟩
    simp [this.neg_one_pow, coeff_one]
  refine ⟨Q.divX.divX, ?_⟩
  rw [← hQ]
  conv_lhs => rw [eq_C_add_X_mul Q]
  rw [h0, h1]
  simp

lemma hasLead_one_add_X_pow_even {i : ℕ} (hi : i % 2 = 0) :
    HasLead (1 + X ^ i) 0 2 := by
  have h := hasLead_zero (1 + X ^ i)
  have : (1 + X ^ i : ℤ[X]).eval (-1) = 2 := by
    have he : Even i := Nat.even_iff.mpr hi
    simp [he.neg_one_pow]
  rwa [this] at h

lemma hasLead_X_pow (r : ℕ) : HasLead (X ^ r) 0 ((-1) ^ r) := by
  have h := hasLead_zero (X ^ r : ℤ[X])
  simpa using h

lemma hasLead_two : HasLead (2 : ℤ[X]) 0 2 := by
  have h := hasLead_zero (2 : ℤ[X])
  simpa using h

/-! ## The leading constant of `S k` -/

/-- `lead k = ∏_{i ≤ k} (i if i is odd, 2 if i is even) = k! / ⌊k/2⌋!`. -/
def lead : ℕ → ℤ
  | 0 => 1
  | k + 1 => lead k * (if (k + 1) % 2 = 1 then ((k + 1 : ℕ) : ℤ) else 2)

lemma lead_succ (k : ℕ) :
    lead (k + 1) = lead k * (if (k + 1) % 2 = 1 then ((k + 1 : ℕ) : ℤ) else 2) := rfl

lemma lead_two_mul_add_two (b : ℕ) : lead (2 * b + 2) = 2 * lead (2 * b + 1) := by
  rw [lead_succ, if_neg (by omega)]
  ring

lemma lead_two_mul_add_one (b : ℕ) : lead (2 * b + 1) = lead (2 * b) * (2 * b + 1) := by
  rw [lead_succ, if_pos (by omega)]
  push_cast
  ring

lemma hasLead_S (k : ℕ) : HasLead (S k) ((k + 1) / 2) (lead k) := by
  induction k with
  | zero => simpa [S_zero, lead] using HasLead.one
  | succ k ih =>
    rw [S_succ, lead_succ]
    rcases Nat.mod_two_eq_zero_or_one (k + 1) with h | h
    · rw [if_neg (by omega)]
      have := ih.mul (hasLead_one_add_X_pow_even h)
      rwa [show (k + 1) / 2 + 0 = (k + 1 + 1) / 2 by omega] at this
    · rw [if_pos h]
      have := ih.mul (hasLead_one_add_X_pow_odd h)
      rwa [show (k + 1) / 2 + 1 = (k + 1 + 1) / 2 by omega] at this

/-- `lead k · ⌊k/2⌋! = k!`. -/
lemma lead_mul_factorial (k : ℕ) : lead k * ((k / 2).factorial : ℤ) = (k.factorial : ℤ) := by
  induction k with
  | zero => simp [lead]
  | succ k ih =>
    rw [lead_succ]
    rcases Nat.mod_two_eq_zero_or_one (k + 1) with h | h
    · rw [if_neg (by omega), show (k + 1) / 2 = k / 2 + 1 by omega, Nat.factorial_succ (k / 2),
        Nat.factorial_succ k]
      simp only [Nat.cast_mul, Nat.cast_add, Nat.cast_one]
      have h2 : (2 : ℤ) * (((k / 2 : ℕ) : ℤ) + 1) = (k : ℤ) + 1 := by
        have := (show 2 * (k / 2 + 1) = k + 1 by omega)
        exact_mod_cast this
      linear_combination (2 * (((k / 2 : ℕ) : ℤ) + 1)) * ih + ((k.factorial : ℕ) : ℤ) * h2
    · rw [if_pos h, show (k + 1) / 2 = k / 2 by omega, Nat.factorial_succ k]
      simp only [Nat.cast_mul, Nat.cast_add, Nat.cast_one]
      linear_combination ((k : ℤ) + 1) * ih

/-! ## The leading constant of `D (2a + 2)` -/

lemma sum_order_telescope (N L : ℕ) :
    ∑ j ∈ Finset.Icc 1 L, (N / 2 ^ j + 1) / 2 + N / 2 ^ (L + 1) = N / 2 := by
  induction L with
  | zero => simp
  | succ L ih =>
    rw [Finset.sum_Icc_succ_top (by omega)]
    have h : N / 2 ^ (L + 1 + 1) = N / 2 ^ (L + 1) / 2 := by
      rw [Nat.div_div_eq_div_mul, pow_succ]
    omega

lemma prod_lead_telescope (N L : ℕ) :
    (∏ j ∈ Finset.Icc 1 L, lead (N / 2 ^ j)) * ((N / 2 ^ (L + 1)).factorial : ℤ)
      = ((N / 2).factorial : ℤ) := by
  induction L with
  | zero => simp
  | succ L ih =>
    rw [Finset.prod_Icc_succ_top (by omega)]
    have h : N / 2 ^ (L + 1 + 1) = N / 2 ^ (L + 1) / 2 := by
      rw [Nat.div_div_eq_div_mul, pow_succ]
    rw [h, mul_assoc, lead_mul_factorial, ih]

lemma hasLead_D (N : ℕ) : HasLead (D (N + 2)) (N / 2) ((N / 2).factorial : ℤ) := by
  rw [D_add_two]
  have h := HasLead.prod (Finset.Icc 1 (N + 2)) (fun j => S (N / 2 ^ j))
    (fun j => (N / 2 ^ j + 1) / 2) (fun j => lead (N / 2 ^ j)) (fun j _ => hasLead_S _)
  have hz : N / 2 ^ (N + 2 + 1) = 0 :=
    Nat.div_eq_of_lt (lt_of_lt_of_le (Nat.lt_two_pow_self) (Nat.pow_le_pow_right (by omega)
      (by omega)))
  have h1 := sum_order_telescope N (N + 2)
  have h2 := prod_lead_telescope N (N + 2)
  rw [hz] at h1 h2
  simp only [add_zero, Nat.factorial_zero, Nat.cast_one, mul_one] at h1 h2
  rwa [h1, h2] at h

/-! ## Reading off `P n (−1)` -/

/-- If `VH n` and `D n` have the same order `k` at `−1`, with leading constants `c` and `d`,
then `d · P n (−1) = c`. -/
lemma eval_P_of_hasLead {n k : ℕ} {c d : ℤ} (hV : HasLead (VH n) k c) (hD : HasLead (D n) k d) :
    d * (P n).eval (-1) = c := by
  have h := hD.mul (hasLead_zero (P n))
  rw [← VH_eq_D_mul_P, add_zero] at h
  exact h.unique hV

/-! ## Even `n`: the alternating sum collapses -/

/-- The `i`-th sum term of `VH (2a + 2)` has order `a` at `−1`. -/
lemma hasLead_term_even (a i : ℕ) (hi1 : 1 ≤ i) (hia : i ≤ a) :
    HasLead (S (2 * a - i) * S (i - 1) * X ^ (2 * a + 1 - i)) a
      (lead (2 * a - i) * lead (i - 1) * (-1) ^ (2 * a + 1 - i)) := by
  have h := ((hasLead_S (2 * a - i)).mul (hasLead_S (i - 1))).mul (hasLead_X_pow (2 * a + 1 - i))
  rwa [show (2 * a - i + 1) / 2 + (i - 1 + 1) / 2 + 0 = a by omega] at h

/-- The alternating partial sums of `t i = lead (2a − i) · lead (i − 1)`. -/
lemma alt_sum (a : ℕ) (t : ℕ → ℤ) (ht : ∀ c, 1 ≤ c → 2 * c + 1 ≤ a → t (2 * c) = t (2 * c + 1)) :
    ∀ K, K ≤ a → ∑ i ∈ Finset.Icc 1 K, (-1) ^ (i + 1) * t i
      = if K = 0 then 0 else if K % 2 = 1 then t 1 else t 1 - t K := by
  intro K
  induction K with
  | zero => simp
  | succ K ih =>
    intro hK
    rw [Finset.sum_Icc_succ_top (by omega), ih (by omega)]
    rcases Nat.mod_two_eq_zero_or_one K with h | h
    · obtain ⟨c, rfl⟩ : ∃ c, K = 2 * c := ⟨K / 2, by omega⟩
      have hs : (-1 : ℤ) ^ (2 * c + 1 + 1) = 1 := by
        rw [show 2 * c + 1 + 1 = 2 * (c + 1) by ring, pow_mul]; simp
      rw [hs]
      rcases Nat.eq_zero_or_pos c with rfl | hc
      · simp
      · rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos (by omega),
          ht c hc (by omega)]
        ring
    · have hs : (-1 : ℤ) ^ (K + 1 + 1) = -1 := by
        obtain ⟨c, rfl⟩ : ∃ c, K = 2 * c + 1 := ⟨K / 2, by omega⟩
        rw [show 2 * c + 1 + 1 + 1 = 2 * (c + 1) + 1 by ring, pow_succ, pow_mul]; simp
      rw [hs, if_neg (by omega), if_pos h, if_neg (by omega), if_neg (by omega)]
      ring

/-- The leading constant of `VH (2a + 2)` at order `a` is `lead (2a) = (2a)!/a!`. -/
lemma hasLead_VH_even (a : ℕ) : HasLead (VH (2 * a + 2)) a (lead (2 * a)) := by
  rw [VH_add_two, show (2 * a + 1) / 2 = a by omega, show 2 * a / 2 = a by omega]
  -- the sum
  have hsum := HasLead.sum (k := a) (Finset.Icc 1 a)
    (fun i => S (2 * a - i) * S (i - 1) * X ^ (2 * a + 1 - i))
    (fun i => lead (2 * a - i) * lead (i - 1) * (-1) ^ (2 * a + 1 - i))
    (fun i hi => by rw [Finset.mem_Icc] at hi; exact hasLead_term_even a i hi.1 hi.2)
  have h2 := hasLead_two.mul hsum
  rw [zero_add] at h2
  -- the square
  have hsq := (hasLead_S a).pow 2
  set t : ℕ → ℤ := fun i => lead (2 * a - i) * lead (i - 1) with ht
  have hsign : ∀ i ∈ Finset.Icc 1 a, lead (2 * a - i) * lead (i - 1) * (-1) ^ (2 * a + 1 - i)
      = (-1) ^ (i + 1) * t i := by
    intro i hi
    rw [Finset.mem_Icc] at hi
    have : (-1 : ℤ) ^ (2 * a + 1 - i) = (-1) ^ (i + 1) := by
      rw [show 2 * a + 1 - i = (i + 1) + 2 * (a - i) by omega, pow_add, pow_mul]; simp
    rw [this, ht]; ring
  rw [Finset.sum_congr rfl hsign] at h2
  have hpair : ∀ c, 1 ≤ c → 2 * c + 1 ≤ a → t (2 * c) = t (2 * c + 1) := by
    intro c hc hca
    simp only [ht]
    rw [show 2 * a - 2 * c = 2 * (a - c - 1) + 2 by omega,
      show 2 * a - (2 * c + 1) = 2 * (a - c - 1) + 1 by omega,
      show 2 * c - 1 = 2 * (c - 1) + 1 by omega,
      show 2 * c + 1 - 1 = 2 * (c - 1) + 2 by omega,
      lead_two_mul_add_two, lead_two_mul_add_two]
    ring
  rw [alt_sum a t hpair a le_rfl] at h2
  rcases Nat.eq_zero_or_pos a with rfl | ha
  · simpa [lead] using h2.add hsq
  have ht1 : 2 * t 1 = lead (2 * a) := by
    simp only [ht]
    rw [show 2 * a - 1 = 2 * (a - 1) + 1 by omega, show 2 * a = 2 * (a - 1) + 2 by omega,
      lead_two_mul_add_two]
    simp [lead]
  rcases Nat.mod_two_eq_zero_or_one a with hpar | hpar
  · -- `a` even: the last term cancels against the square
    obtain ⟨b, rfl⟩ : ∃ b, a = 2 * (b + 1) := ⟨a / 2 - 1, by omega⟩
    have hsq' : HasLead (S (2 * (b + 1)) ^ 2) (2 * (b + 1)) (lead (2 * (b + 1)) ^ 2) := by
      rwa [show (2 * (b + 1) + 1) / 2 * 2 = 2 * (b + 1) by omega] at hsq
    have := h2.add hsq'
    rw [if_neg (by omega), if_neg (by omega)] at this
    convert this using 1
    rw [← ht1]
    simp only [ht]
    rw [show 2 * (2 * (b + 1)) - 2 * (b + 1) = 2 * b + 2 by omega,
      show 2 * (b + 1) - 1 = 2 * b + 1 by omega, show 2 * (b + 1) = 2 * b + 2 by ring,
      lead_two_mul_add_two]
    ring
  · -- `a` odd: the square vanishes to higher order
    have hsq' := hsq.lower (show a < (a + 1) / 2 * 2 by omega)
    have := h2.add hsq'
    rw [if_neg (by omega), if_pos hpar, add_zero, ht1] at this
    exact this

/-- **`conj:Pcon`(e), even `n`**: `P_n(−1) = C(n−2, (n−2)/2)`. -/
theorem P_eval_neg_one_even {n : ℕ} (hn : Even n) :
    (P n).eval (-1) = (Nat.centralBinom ((n - 2) / 2) : ℤ) := by
  rcases Nat.lt_or_ge n 2 with h | h
  · obtain rfl : n = 0 := by rcases hn with ⟨r, hr⟩; omega
    have : P 0 = 1 := P_eq_of_VH_eq (by simp [VH, D])
    simp [this]
  obtain ⟨a, rfl⟩ : ∃ a, n = 2 * a + 2 := by rcases hn with ⟨r, hr⟩; exact ⟨r - 1, by omega⟩
  have hD := hasLead_D (2 * a)
  rw [show 2 * a / 2 = a by omega] at hD
  have key := eval_P_of_hasLead (hasLead_VH_even a) hD
  rw [show (2 * a + 2 - 2) / 2 = a by omega]
  -- `a! · P(−1) = lead (2a)` and `lead (2a) · a! = (2a)!`
  have hl := lead_mul_factorial (2 * a)
  rw [show 2 * a / 2 = a by omega] at hl
  have hcb : (Nat.centralBinom a : ℤ) * (a.factorial : ℤ) * (a.factorial : ℤ)
      = ((2 * a).factorial : ℤ) := by
    rw [Nat.centralBinom_eq_two_mul_choose]
    have := Nat.choose_mul_factorial_mul_factorial (show a ≤ 2 * a by omega)
    rw [show 2 * a - a = a by omega] at this
    exact_mod_cast this
  have hf : (a.factorial : ℤ) ≠ 0 := by exact_mod_cast (Nat.factorial_ne_zero a)
  apply mul_right_cancel₀ hf
  apply mul_right_cancel₀ hf
  rw [hcb, ← hl, ← key]
  ring

end Tatami

Tatami/AtMinusOneOdd.lean

import Tatami.AtMinusOne

/-!
# `conj:Pcon`(e), odd `n`: `P_{2k+3}(−1) = −∑_{i ≤ k} 2^{k−i} C(2i, i)`

Source: `code/pn-polynomial/reports/at-minus-one.md`, Theorem B. Write `n = 2a + 3`.

* In `VH (2a+3)` only the sum terms with odd `i = 2c + 1` have order `a` at `−1`, each with
  leading constant `−2 T(a, c)` where `T(a, c) = lead (2a − 2c) · lead (2c)`; the square
  term contributes `T(a, a/2)` when `a` is even. Folding by the symmetry
  `T(a, c) = T(a, a − c)`, the total is `−U(a)` with `U(a) = ∑_{c ≤ a} T(a, c)`.
* The report evaluates `U(a)` with a Beta integral. Here instead: from
  `4(a+1) T(a, c) = T(a+1, c+1) + T(a+1, c)` we get `U(a+1) = 2(a+1) U(a) + T(a+1, 0)`,
  and by induction `U(a) = a! ∑_{i ≤ a} 2^{a−i} C(2i, i)`.
-/

open Polynomial

namespace Tatami

/-- `T a c = lead (2a − 2c) · lead (2c)`. -/
def T (a c : ℕ) : ℤ := lead (2 * a - 2 * c) * lead (2 * c)

lemma T_symm {a c : ℕ} (hc : c ≤ a) : T a (a - c) = T a c := by
  unfold T
  rw [show 2 * a - 2 * (a - c) = 2 * c by omega, show 2 * (a - c) = 2 * a - 2 * c by omega]
  ring

lemma lead_two_mul_succ (b : ℕ) : lead (2 * (b + 1)) = 2 * (2 * b + 1) * lead (2 * b) := by
  rw [show 2 * (b + 1) = 2 * b + 2 by ring, lead_two_mul_add_two, lead_two_mul_add_one]
  ring

/-- The recurrence behind the evaluation. -/
lemma T_rec {a c : ℕ} (hc : c ≤ a) :
    4 * ((a : ℤ) + 1) * T a c = T (a + 1) (c + 1) + T (a + 1) c := by
  unfold T
  obtain ⟨d, rfl⟩ : ∃ d, a = c + d := ⟨a - c, by omega⟩
  rw [show 2 * (c + d + 1) - 2 * (c + 1) = 2 * d by omega,
    show 2 * (c + d) - 2 * c = 2 * d by omega,
    show 2 * (c + d + 1) - 2 * c = 2 * (d + 1) by omega,
    lead_two_mul_succ c, lead_two_mul_succ d]
  push_cast
  ring

/-- `U a = ∑_{c ≤ a} T a c`. -/
def U (a : ℕ) : ℤ := ∑ c ∈ Finset.range (a + 1), T a c

lemma U_succ (a : ℕ) : U (a + 1) = 2 * ((a : ℤ) + 1) * U a + T (a + 1) 0 := by
  have hsum : 4 * ((a : ℤ) + 1) * U a
      = ∑ c ∈ Finset.range (a + 1), (T (a + 1) (c + 1) + T (a + 1) c) := by
    rw [U, Finset.mul_sum]
    exact Finset.sum_congr rfl fun c hc => T_rec (by simp at hc; omega)
  rw [Finset.sum_add_distrib] at hsum
  have h1 : U (a + 1) = ∑ c ∈ Finset.range (a + 1), T (a + 1) (c + 1) + T (a + 1) 0 := by
    rw [U, Finset.sum_range_succ']
  have h2 : U (a + 1) = ∑ c ∈ Finset.range (a + 1), T (a + 1) c + T (a + 1) (a + 1) := by
    rw [U, Finset.sum_range_succ]
  have h3 : T (a + 1) (a + 1) = T (a + 1) 0 := by
    have := T_symm (a := a + 1) (c := 0) (by omega)
    simpa using this
  have h2x : (2 : ℤ) * U (a + 1) = 2 * (2 * ((a : ℤ) + 1) * U a + T (a + 1) 0) := by
    linear_combination h1 + h2 + h3 - hsum
  exact mul_left_cancel₀ two_ne_zero h2x

/-- `lead (2b) = C(2b, b) · b!`. -/
lemma lead_two_mul (b : ℕ) : lead (2 * b) = (Nat.centralBinom b : ℤ) * (b.factorial : ℤ) := by
  have hl := lead_mul_factorial (2 * b)
  rw [show 2 * b / 2 = b by omega] at hl
  have hcb : (Nat.centralBinom b : ℤ) * (b.factorial : ℤ) * (b.factorial : ℤ)
      = ((2 * b).factorial : ℤ) := by
    rw [Nat.centralBinom_eq_two_mul_choose]
    have := Nat.choose_mul_factorial_mul_factorial (show b ≤ 2 * b by omega)
    rw [show 2 * b - b = b by omega] at this
    exact_mod_cast this
  have hf : (b.factorial : ℤ) ≠ 0 := by exact_mod_cast (Nat.factorial_ne_zero b)
  apply mul_right_cancel₀ hf
  rw [hl, hcb]

/-- `R a = ∑_{i ≤ a} 2^{a−i} C(2i, i)`. -/
def R (a : ℕ) : ℤ := ∑ i ∈ Finset.range (a + 1), 2 ^ (a - i) * (Nat.centralBinom i : ℤ)

lemma R_succ (a : ℕ) : R (a + 1) = 2 * R a + Nat.centralBinom (a + 1) := by
  rw [R, Finset.sum_range_succ, R, Finset.mul_sum]
  congr 1
  · apply Finset.sum_congr rfl
    intro i hi
    simp only [Finset.mem_range] at hi
    rw [show a + 1 - i = (a - i) + 1 by omega, pow_succ]
    ring
  · simp

lemma U_eq (a : ℕ) : U a = (a.factorial : ℤ) * R a := by
  induction a with
  | zero => simp [U, R, T, lead]
  | succ a ih =>
    rw [U_succ, ih, R_succ, Nat.factorial_succ]
    have hT : T (a + 1) 0 = (Nat.centralBinom (a + 1) : ℤ) * ((a + 1).factorial : ℤ) := by
      rw [T, show 2 * (a + 1) - 2 * 0 = 2 * (a + 1) by omega, lead_two_mul]
      simp [lead]
    rw [hT, Nat.factorial_succ]
    push_cast
    ring

/-! ## The leading constant of `VH (2a + 3)` -/

lemma hasLead_term_odd (a i : ℕ) (hi1 : 1 ≤ i) (hia : i ≤ a + 1) :
    HasLead (S (2 * a + 1 - i) * S (i - 1) * X ^ (2 * a + 1 + 1 - i)) a
      (if i % 2 = 1 then -T a ((i - 1) / 2) else 0) := by
  have h := ((hasLead_S (2 * a + 1 - i)).mul (hasLead_S (i - 1))).mul
    (hasLead_X_pow (2 * a + 1 + 1 - i))
  rcases Nat.mod_two_eq_zero_or_one i with hi | hi
  · rw [if_neg (by omega)]
    exact h.lower (by omega)
  · obtain ⟨c, rfl⟩ : ∃ c, i = 2 * c + 1 := ⟨i / 2, by omega⟩
    rw [if_pos hi, show (2 * c + 1 - 1) / 2 = c by omega]
    rw [show (2 * a + 1 - (2 * c + 1) + 1) / 2 + (2 * c + 1 - 1 + 1) / 2 + 0 = a by omega] at h
    have hs : (-1 : ℤ) ^ (2 * a + 1 + 1 - (2 * c + 1)) = -1 := by
      rw [show 2 * a + 1 + 1 - (2 * c + 1) = 2 * (a - c) + 1 by omega, pow_succ, pow_mul]
      simp
    have hL : lead (2 * a + 1 - (2 * c + 1)) * lead (2 * c + 1 - 1) * (-1) ^ (2 * a + 1 + 1 - (2 * c + 1))
        = -T a c := by
      rw [hs, T, show 2 * a + 1 - (2 * c + 1) = 2 * a - 2 * c by omega,
        show 2 * c + 1 - 1 = 2 * c by omega]
      ring
    rwa [hL] at h

/-- Summing over the odd `i ≤ K` only. -/
lemma sum_Icc_odd (g : ℕ → ℤ) (K : ℕ) :
    ∑ i ∈ Finset.Icc 1 K, (if i % 2 = 1 then g ((i - 1) / 2) else 0)
      = ∑ c ∈ Finset.range ((K + 1) / 2), g c := by
  induction K with
  | zero => simp
  | succ K ih =>
    rw [Finset.sum_Icc_succ_top (by omega), ih]
    rcases Nat.mod_two_eq_zero_or_one K with h | h
    · rw [if_pos (by omega), show (K + 1 + 1) / 2 = (K + 1) / 2 + 1 by omega,
        Finset.sum_range_succ, show (K + 1 - 1) / 2 = (K + 1) / 2 by omega]
    · rw [if_neg (by omega), show (K + 1 + 1) / 2 = (K + 1) / 2 by omega, add_zero]

/-- Folding the symmetric sum `U a` in half. -/
lemma U_fold (a : ℕ) :
    U a = 2 * ∑ c ∈ Finset.range ((a + 2) / 2), T a c - (if a % 2 = 0 then T a (a / 2) else 0) := by
  have hsplit : ∀ m n, m + n = a + 1 →
      U a = ∑ c ∈ Finset.range m, T a c + ∑ c ∈ Finset.range n, T a c := by
    intro m n hmn
    rw [U, ← hmn, Finset.sum_range_add]
    congr 1
    rw [← Finset.sum_range_reflect (fun c => T a c) n]
    apply Finset.sum_congr rfl
    intro x hx
    simp only [Finset.mem_range] at hx
    rw [← T_symm (show m + x ≤ a by omega)]
    congr 1
    omega
  rcases Nat.mod_two_eq_zero_or_one a with h | h
  · rw [hsplit (a / 2 + 1) (a / 2) (by omega), if_pos h, show (a + 2) / 2 = a / 2 + 1 by omega,
      Finset.sum_range_succ]
    ring
  · rw [hsplit ((a + 1) / 2) ((a + 1) / 2) (by omega), if_neg (by omega),
      show (a + 2) / 2 = (a + 1) / 2 by omega]
    ring

lemma hasLead_VH_odd (a : ℕ) : HasLead (VH (2 * a + 3)) a (-U a) := by
  rw [show 2 * a + 3 = (2 * a + 1) + 2 by ring, VH_add_two,
    show (2 * a + 1 + 1) / 2 = a + 1 by omega, show (2 * a + 1) / 2 = a by omega]
  have hsum := HasLead.sum (k := a) (Finset.Icc 1 (a + 1))
    (fun i => S (2 * a + 1 - i) * S (i - 1) * X ^ (2 * a + 1 + 1 - i))
    (fun i => if i % 2 = 1 then -T a ((i - 1) / 2) else 0)
    (fun i hi => by rw [Finset.mem_Icc] at hi; exact hasLead_term_odd a i hi.1 hi.2)
  rw [sum_Icc_odd (fun c => -T a c), show (a + 1 + 1) / 2 = (a + 2) / 2 by omega] at hsum
  have h2 := hasLead_two.mul hsum
  rw [zero_add] at h2
  have hsq := (hasLead_S a).pow 2
  rcases Nat.mod_two_eq_zero_or_one a with hpar | hpar
  · rw [show (a + 1) / 2 * 2 = a by omega] at hsq
    have := h2.add hsq
    convert this using 1
    rw [U_fold, if_pos hpar, T, show 2 * a - 2 * (a / 2) = a by omega,
      show 2 * (a / 2) = a by omega, Finset.sum_neg_distrib]
    ring
  · have := h2.add (hsq.lower (show a < (a + 1) / 2 * 2 by omega))
    convert this using 1
    rw [U_fold, if_neg (by omega), Finset.sum_neg_distrib]
    ring

/-- **`conj:Pcon`(e), odd `n`**: `P_{2k+3}(−1) = −∑_{i=0}^{k} 2^{k−i} C(2i, i)`. -/
theorem P_eval_neg_one_odd (k : ℕ) :
    (P (2 * k + 3)).eval (-1) = -∑ i ∈ Finset.range (k + 1), 2 ^ (k - i) * (Nat.centralBinom i : ℤ) := by
  have hD := hasLead_D (2 * k + 1)
  rw [show 2 * k + 1 + 2 = 2 * k + 3 by ring, show (2 * k + 1) / 2 = k by omega] at hD
  have key := eval_P_of_hasLead (hasLead_VH_odd k) hD
  rw [U_eq] at key
  have hf : (k.factorial : ℤ) ≠ 0 := by exact_mod_cast (Nat.factorial_ne_zero k)
  apply mul_left_cancel₀ hf
  rw [key, ← R]
  ring

/-- **`conj:Pcon`(e)**, in coefficient form: for `n ≥ 2`, `P_n(−1)` is `C(n−2, (n−2)/2)` for
even `n` and `−∑_{i ≤ k} 2^{k−i} C(2i, i)` for odd `n = 2k + 3`. These are the coefficients of
`(1+z)(1−2z) / ((1−2z²) √(1−4z²))`, as the report shows. -/
theorem conj_e (n : ℕ) (hn : 2 ≤ n) :
    (P n).eval (-1) = if n % 2 = 0 then (Nat.centralBinom ((n - 2) / 2) : ℤ)
      else -∑ i ∈ Finset.range ((n - 3) / 2 + 1), 2 ^ ((n - 3) / 2 - i) * (Nat.centralBinom i : ℤ) := by
  rcases Nat.mod_two_eq_zero_or_one n with h | h
  · rw [if_pos h]
    exact P_eval_neg_one_even (Nat.even_iff.mpr h)
  · rw [if_neg (by omega)]
    obtain ⟨k, rfl⟩ : ∃ k, n = 2 * k + 3 := ⟨(n - 3) / 2, by omega⟩
    rw [show (2 * k + 3 - 3) / 2 = k by omega]
    exact P_eval_neg_one_odd k

end Tatami

Tatami/LowCoefficients.lean

import Tatami.Cyclotomic

/-!
# The low coefficients: thesis `conj:vhcon`(a) and the corrected `conj:Pcon`(a)

Sources: `code/pn-polynomial/reports/low-coefficients.md` (Lemma A, Corollary 3) and
`structure.md` §2.1. "`F ≡ G` mod `z^m`" is written `X ^ m ∣ F - G`: the coefficients of
`z^0, …, z^{m−1}` agree.

* **Lemma A** (`VH_congr_S_sq`): `VH_{N+2} ≡ S_N²` mod `z^{N+1}`. Since `S_N ≡ S_K` mod
  `z^{N+1}` for every `K ≥ N`, this is thesis `conj:vhcon`(a): the first `n − 1` coefficients
  of `VH_n` are those of `∏_{m≥1}(1+z^m)²`.
* **Corrected (a)** (`conj_a_corrected`): if `k ≥ 1`, `n ≡ 2` (mod `2^k`) and `j < 2^k`, then
  `P_n` and `P_{n+j}` agree in every coefficient `z^i` with `i ≤ (n−2)/2^{k−1}`.

The proof of the corrected (a) here is more direct than the report's (which goes through the
exact first-difference index, its Theorem 2): `VH_n ≡ VH_{n+j}` mod `z^{n−1}` by Lemma A, and
`D_n ≡ D_{n+j}` mod `z^{M+1}`, `M = (n−2)/2^{k−1}`, factor by factor; then cancel `D_n`, whose
constant term is `1`.
-/

open Polynomial

namespace Tatami

/-- `S_a ≡ S_b` mod `z^{a+1}` for `a ≤ b`. -/
lemma S_sub_S_dvd {a b : ℕ} (hab : a ≤ b) : X ^ (a + 1) ∣ S b - S a := by
  induction b, hab using Nat.le_induction with
  | base => simp
  | succ b hab ih =>
    rw [S_succ, show S b * (1 + X ^ (b + 1)) - S a = (S b - S a) + S b * X ^ (b + 1) by ring]
    exact dvd_add ih (Dvd.dvd.mul_left (pow_dvd_pow X (by omega)) _)

lemma telescope (g : ℕ → ℤ[X]) (N : ℕ) :
    ∀ m, m ≤ N → ∑ i ∈ Finset.Icc 1 m, (g (N + 1 - i) - g (N - i)) = g N - g (N - m) := by
  intro m
  induction m with
  | zero => simp
  | succ m ih =>
    intro hm
    rw [Finset.sum_Icc_succ_top (by omega), ih (by omega),
      show N + 1 - (m + 1) = N - m by omega, show N - (m + 1) = N - m - 1 by omega]
    ring

/-- **Lemma A** (and thesis `conj:vhcon`(a)): `VH_{N+2} ≡ S_N²` mod `z^{N+1}`. -/
theorem VH_congr_S_sq (N : ℕ) : X ^ (N + 1) ∣ S N ^ 2 - VH (N + 2) := by
  rw [VH_add_two]
  have htel := telescope (fun t => S t ^ 2) N ((N + 1) / 2) (by omega)
  rw [show N - (N + 1) / 2 = N / 2 by omega] at htel
  have hsplit : S N ^ 2 - (2 * ∑ i ∈ Finset.Icc 1 ((N + 1) / 2), S (N - i) * S (i - 1) *
      X ^ (N + 1 - i) + S (N / 2) ^ 2) = ∑ i ∈ Finset.Icc 1 ((N + 1) / 2),
      (S (N + 1 - i) ^ 2 - S (N - i) ^ 2 - 2 * (S (N - i) * S (i - 1) * X ^ (N + 1 - i))) := by
    rw [Finset.sum_sub_distrib, Finset.sum_sub_distrib] at *
    rw [htel, ← Finset.mul_sum]
    ring
  rw [hsplit]
  apply Finset.dvd_sum
  intro i hi
  rw [Finset.mem_Icc] at hi
  rw [show N + 1 - i = (N - i) + 1 by omega, S_succ]
  obtain ⟨u, hu⟩ : X ^ i ∣ S (N - i) - S (i - 1) := by
    have := S_sub_S_dvd (show i - 1 ≤ N - i by omega)
    rwa [show i - 1 + 1 = i by omega] at this
  obtain ⟨w, hw⟩ : X ^ (N + 1) ∣ (X : ℤ[X]) ^ (2 * (N - i + 1)) := pow_dvd_pow X (by omega)
  refine ⟨2 * S (N - i) * u + w * S (N - i) ^ 2, ?_⟩
  have hX : (X : ℤ[X]) ^ (N - i + 1) * X ^ i = X ^ (N + 1) := by
    rw [← pow_add]; congr 1; omega
  have hS : S (i - 1) = S (N - i) - X ^ i * u := by rw [← hu]; ring
  have hw' : ((X : ℤ[X]) ^ (N - i + 1)) ^ 2 = X ^ (N + 1) * w := by rw [← pow_mul, mul_comm, hw]
  rw [hS]
  linear_combination (2 * S (N - i) * u) * hX + S (N - i) ^ 2 * hw'

/-- Thesis `conj:vhcon`(a), stated with coefficients: for `k ≤ n − 2`, the coefficient of
`z^k` in `VH_n` equals that in `(S_K)²` for any `K ≥ n − 2`, hence in `∏_{m≥1}(1+z^m)²`. -/
theorem conj_vhcon_a (N k K : ℕ) (hk : k ≤ N) (hK : N ≤ K) :
    (VH (N + 2)).coeff k = (S K ^ 2).coeff k := by
  have h1 := VH_congr_S_sq N
  have h2 : X ^ (N + 1) ∣ S K ^ 2 - S N ^ 2 := by
    rw [sq_sub_sq]
    exact Dvd.dvd.mul_left (S_sub_S_dvd hK) _
  have h3 : X ^ (N + 1) ∣ S K ^ 2 - VH (N + 2) := by
    have := dvd_add h2 h1
    rwa [sub_add_sub_cancel] at this
  have := (X_pow_dvd_iff.mp h3) k (by omega)
  rw [coeff_sub, sub_eq_zero] at this
  exact this.symm

/-! ## Constant terms, and cancelling `D` -/

lemma S_coeff_zero (k : ℕ) : (S k).coeff 0 = 1 := by
  rw [coeff_zero_eq_eval_zero, S, eval_prod]
  apply Finset.prod_eq_one
  intro i hi
  simp only [Finset.mem_Icc] at hi
  simp [show i ≠ 0 by omega]

lemma D_coeff_zero (n : ℕ) : (D n).coeff 0 = 1 := by
  rw [coeff_zero_eq_eval_zero, D, eval_prod]
  apply Finset.prod_eq_one
  intro j _
  rw [← coeff_zero_eq_eval_zero, S_coeff_zero]

lemma isCoprime_X_pow_of_coeff_zero {p : ℤ[X]} (hp : p.coeff 0 = 1) (m : ℕ) :
    IsCoprime (X ^ m) p := by
  apply IsCoprime.pow_left
  refine ⟨-p.divX, 1, ?_⟩
  have := X_mul_divX_add p
  rw [hp, C_1] at this
  linear_combination (-1 : ℤ[X]) * this

lemma prod_sub_prod_dvd {ι : Type*} (s : Finset ι) (f g : ι → ℤ[X]) (m : ℕ)
    (h : ∀ l ∈ s, X ^ m ∣ f l - g l) : X ^ m ∣ ∏ l ∈ s, f l - ∏ l ∈ s, g l := by
  classical
  induction s using Finset.induction_on with
  | empty => simp
  | insert a s ha ih =>
    rw [Finset.prod_insert ha, Finset.prod_insert ha]
    have h1 := h a (Finset.mem_insert_self a s)
    have h2 := ih fun l hl => h l (Finset.mem_insert_of_mem hl)
    rw [show f a * ∏ l ∈ s, f l - g a * ∏ l ∈ s, g l
        = (f a - g a) * ∏ l ∈ s, f l + g a * (∏ l ∈ s, f l - ∏ l ∈ s, g l) by ring]
    exact dvd_add (Dvd.dvd.mul_right h1 _) (Dvd.dvd.mul_left h2 _)

/-- `D (N + 2)` may be written over any longer range of `j`. -/
lemma D_eq_prod_Icc (N L : ℕ) (hL : N + 2 ≤ L) :
    D (N + 2) = ∏ l ∈ Finset.Icc 1 L, S (N / 2 ^ l) := by
  rw [D_add_two]
  apply Finset.prod_subset (Finset.Icc_subset_Icc_right hL)
  intro l hl hl'
  simp only [Finset.mem_Icc, not_and, not_le] at hl hl'
  have hlt : N < 2 ^ l :=
    lt_of_lt_of_le Nat.lt_two_pow_self (Nat.pow_le_pow_right (by omega) (by omega))
  rw [Nat.div_eq_of_lt hlt, S_zero]

/-! ## The corrected `conj:Pcon`(a) -/

/-- The `D`-factors of `n = 2^k q + 2` and `n + j` (`j < 2^k`) agree mod `z^{2q+1}`. -/
lemma D_congr (k q j : ℕ) (hj : j < 2 ^ k) :
    X ^ (2 * q + 1) ∣ D (2 ^ k * q + 2) - D (2 ^ k * q + j + 2) := by
  set N := 2 ^ k * q
  rw [D_eq_prod_Icc N (N + j + 2) (by omega), D_eq_prod_Icc (N + j) (N + j + 2) le_rfl]
  apply prod_sub_prod_dvd
  intro l _
  rcases Nat.lt_or_ge l k with hl | hl
  · -- `l < k`: both indices are at least `2q`, so the factors agree mod `z^{2q+1}`
    have hNl : N / 2 ^ l = 2 ^ (k - l) * q := by
      simp only [N]
      rw [show k = (k - l) + l by omega, pow_add, show k - l + l - l = k - l by omega,
        mul_comm (2 ^ (k - l)) (2 ^ l), mul_assoc, Nat.mul_div_cancel_left _ (by positivity)]
    have hge : 2 * q ≤ N / 2 ^ l := by
      rw [hNl]
      exact Nat.mul_le_mul_right q
        (by calc 2 = 2 ^ 1 := by norm_num
              _ ≤ 2 ^ (k - l) := Nat.pow_le_pow_right (by norm_num) (by omega))
    have hle : N / 2 ^ l ≤ (N + j) / 2 ^ l := Nat.div_le_div_right (by omega)
    have := S_sub_S_dvd hle
    rw [← neg_sub]
    exact (Dvd.dvd.neg_right (dvd_trans (pow_dvd_pow X (by omega)) this))
  · -- `l ≥ k`: the indices are equal
    have hsplit : ∀ x, x / 2 ^ l = x / 2 ^ k / 2 ^ (l - k) := by
      intro x
      rw [Nat.div_div_eq_div_mul, ← pow_add, show k + (l - k) = l by omega]
    have h1 : (N + j) / 2 ^ k = q := by
      simp only [N]
      rw [Nat.mul_add_div (by positivity), Nat.div_eq_of_lt hj, add_zero]
    have h2 : N / 2 ^ k = q := by
      simp only [N]
      rw [Nat.mul_div_cancel_left _ (by positivity)]
    rw [hsplit N, hsplit (N + j), h1, h2, sub_self]
    exact dvd_zero _

/-- **The corrected `conj:Pcon`(a)**: if `k ≥ 1`, `n ≡ 2` (mod `2^k`) and `0 ≤ j < 2^k`, then
`[z^i] P_n = [z^i] P_{n+j}` for all `i ≤ (n − 2)/2^{k−1}`. (As printed, with `j ≤ 2^k`, it is
false: `Tatami.conjA_as_printed_false`.) -/
theorem conj_a_corrected (k n j : ℕ) (hk : 1 ≤ k) (hn2 : 2 ≤ n) (hn : n % 2 ^ k = 2 % 2 ^ k)
    (hj : j < 2 ^ k) (i : ℕ) (hi : i ≤ (n - 2) / 2 ^ (k - 1)) :
    (P n).coeff i = (P (n + j)).coeff i := by
  obtain ⟨q, hq⟩ : 2 ^ k ∣ n - 2 := Nat.dvd_of_mod_eq_zero (Nat.sub_mod_eq_zero_of_mod_eq hn)
  set N := n - 2 with hN
  have hn' : n = 2 ^ k * q + 2 := by omega
  have hM : (n - 2) / 2 ^ (k - 1) = 2 * q := by
    rw [← hN, hq, show k = (k - 1) + 1 by omega, pow_succ, show k - 1 + 1 - 1 = k - 1 by omega,
      show 2 ^ (k - 1) * 2 * q = 2 ^ (k - 1) * (2 * q) by ring,
      Nat.mul_div_cancel_left _ (by positivity)]
  rw [hM] at hi
  subst hn'
  set n' := 2 ^ k * q + 2 + j
  have hqN : 2 * q ≤ 2 ^ k * q :=
    Nat.mul_le_mul_right q
      (by calc 2 = 2 ^ 1 := by norm_num
            _ ≤ 2 ^ k := Nat.pow_le_pow_right (by norm_num) hk)
  -- `VH_n ≡ VH_{n'}` mod `z^{N+1}`, hence mod `z^{2q+1}`
  have hVH : X ^ (2 * q + 1) ∣ VH (2 ^ k * q + 2) - VH n' := by
    have h1 := VH_congr_S_sq (2 ^ k * q)
    have h2 := VH_congr_S_sq (2 ^ k * q + j)
    rw [show 2 ^ k * q + j + 2 = n' by omega] at h2
    have h3 : X ^ (2 ^ k * q + 1) ∣ S (2 ^ k * q + j) ^ 2 - S (2 ^ k * q) ^ 2 := by
      rw [sq_sub_sq]
      exact Dvd.dvd.mul_left (S_sub_S_dvd (by omega)) _
    have h2' : X ^ (2 ^ k * q + 1) ∣ S (2 ^ k * q + j) ^ 2 - VH n' :=
      dvd_trans (pow_dvd_pow X (by omega)) h2
    have : X ^ (2 ^ k * q + 1) ∣ VH (2 ^ k * q + 2) - VH n' := by
      have := dvd_sub (dvd_sub h2' h3) h1
      convert this using 1
      ring
    exact dvd_trans (pow_dvd_pow X (by omega)) this
  have hD := D_congr k q j hj
  rw [show 2 ^ k * q + j + 2 = n' by omega] at hD
  -- `D_n (P_n − P_{n'}) = (VH_n − VH_{n'}) + (D_{n'} − D_n) P_{n'}`
  have hkey : X ^ (2 * q + 1) ∣ D (2 ^ k * q + 2) * (P (2 ^ k * q + 2) - P n') := by
    have : D (2 ^ k * q + 2) * (P (2 ^ k * q + 2) - P n')
        = (VH (2 ^ k * q + 2) - VH n') - (D (2 ^ k * q + 2) - D n') * P n' := by
      rw [VH_eq_D_mul_P, VH_eq_D_mul_P n']
      ring
    rw [this]
    exact dvd_sub hVH (Dvd.dvd.mul_right hD _)
  have hdiff := (isCoprime_X_pow_of_coeff_zero (D_coeff_zero _) _).dvd_of_dvd_mul_left hkey
  have := (X_pow_dvd_iff.mp hdiff) i (by omega)
  rw [coeff_sub, sub_eq_zero] at this
  exact this

end Tatami

Tatami/ConjCCore.lean

import Tatami.AtMinusOne

/-!
# `conj:Pcon`(c), assembled from the signs of `VH n`

`VH n = D n * P n`. Away from `x = −1`, `D n (x) ≠ 0` (its factors are `1 + x^i`), so a real
root of `P n` would be a real root of `VH n`. At `x = −1`, `P n (−1)` is a central binomial
coefficient by `conj:Pcon`(e). The signs of `VH n` on the three intervals are hypotheses
here; `Tatami.RealVH` proves them.
-/

open Polynomial

namespace Tatami

lemma aeval_S (x : ℝ) (k : ℕ) : aeval x (S k) = ∏ i ∈ Finset.Icc 1 k, (1 + x ^ i) := by
  simp [S, map_prod]

lemma one_add_pow_ne_zero {x : ℝ} (hx : x ≠ -1) (i : ℕ) : 1 + x ^ i ≠ 0 := by
  intro h
  have h1 : x ^ i = -1 := by linarith
  rcases Nat.eq_zero_or_pos i with rfl | hi
  · norm_num at h1
  have h2 : |x| ^ i = 1 := by rw [← abs_pow, h1]; simp
  have h3 : |x| = 1 := (pow_eq_one_iff_of_nonneg (abs_nonneg x) hi.ne').mp h2
  rcases abs_eq zero_le_one |>.mp h3 with h4 | h4
  · rw [h4, one_pow] at h1; norm_num at h1
  · exact hx h4

lemma aeval_D_ne_zero {x : ℝ} (hx : x ≠ -1) (n : ℕ) : aeval x (D n) ≠ 0 := by
  rw [D, map_prod]
  refine Finset.prod_ne_zero_iff.mpr fun j _ => ?_
  rw [aeval_S]
  exact Finset.prod_ne_zero_iff.mpr fun i _ => one_add_pow_ne_zero hx i

lemma aeval_neg_one (p : ℤ[X]) : aeval (-1 : ℝ) p = ((p.eval (-1) : ℤ) : ℝ) := by
  rw [aeval_def, show (-1 : ℝ) = ((-1 : ℤ) : ℝ) by norm_num, eval₂_at_intCast]
  simp

/-- `conj:Pcon`(c) from the three sign facts about `VH n`. -/
theorem conj_c_of_VH {n : ℕ} (hn : Even n)
    (hpos : ∀ x : ℝ, 0 ≤ x → 0 < aeval x (VH n))
    (hmid : ∀ x : ℝ, -1 < x → x < 0 → 0 < aeval x (VH n))
    (hlow : ∀ x : ℝ, x < -1 → aeval x (VH n) ≠ 0)
    (x : ℝ) : aeval x (P n) ≠ 0 := by
  by_cases hx : x = -1
  · subst hx
    rw [aeval_neg_one, P_eval_neg_one_even hn]
    exact_mod_cast (Nat.centralBinom_pos _).ne'
  · have hV : aeval x (VH n) ≠ 0 := by
      rcases lt_or_gt_of_ne hx with h | h
      · exact hlow x h
      · rcases lt_or_ge x 0 with h0 | h0
        · exact (hmid x h h0).ne'
        · exact (hpos x h0).ne'
    rw [VH_eq_D_mul_P, map_mul] at hV
    exact right_ne_zero_of_mul hV

end Tatami

Tatami/RealVH.lean

import Tatami.Basic

/-!
# Signs of `VH n` on the real line, for even `n`

These are the two analytic inputs to `conj:Pcon`(c) ("for even n, P_n has no real root"):

* `VH_pos_of_neg_one_lt_of_neg`: for `−1 < x < 0`, `VH_n(x) > 0`.
  Source: `code/pn-polynomial/reports/root-gap.md`, Theorem 1 (with `x = −q`).
* `VH_ne_zero_of_lt_neg_one`: for `x < −1`, `VH_n(x) ≠ 0`.
  Source: `code/pn-polynomial/reports/real-roots.md`, Theorem A, even case.

Plus the easy case `x ≥ 0`.

## How the proofs go

Write `n = 2h + 2` and `t_i = S_{2h−i}(x) S_{i−1}(x) x^{2h+1−i}` (`RealVH.vhTerm x (2h) i`), so
that `VH_n(x) = S_h(x)² + 2 ∑_{i=1}^{h} t_i` (`RealVH.aeval_VH_even`).

* `−1 < x < 0`: every `S_k(x) > 0`, `t_1 > 0`, and each pair `t_{2c} + t_{2c+1}` with `2c + 1 ≤ h`
  is positive (`RealVH.vhTerm_pair_pos`). When `h` is even, the one term left over is beaten by
  the square: `S_h² + 2 t_h > 0` (`RealVH.last_pos_of_neg_one_lt`). This is root-gap.md's proof.
* `x < −1`: instead of the reciprocal form `R_n(y)` of real-roots.md, we work with `x` directly.
  The identity `t_{i+1} · x (1 + x^{2h−i}) = t_i (1 + x^i)` (`RealVH.vhTerm_succ_mul`) shows that
  `t_{i+1} = ρ_i t_i` with `−1 < ρ_i < 0` (`RealVH.ratio_bounds`): the summands alternate in
  sign and shrink in size. `t_1` has the sign `(−1)^h` (`RealVH.S_odd_sign`). Pairing from the
  front, `(−1)^h ∑ t_i ≥ 0` for even `h` (so `VH_n(x) > 0`), and `−∑ t_i ≥ −t_h` for odd `h`, where
  `S_h² + 2 t_h < 0` (`RealVH.last_neg_of_lt`), so `VH_n(x) < 0`. Lemma 1 of real-roots.md is
  only used there for odd `n`, and is not needed here.

The helper lemmas live in the namespace `Tatami.RealVH`.
-/

open Polynomial

namespace Tatami

namespace RealVH

lemma aeval_S (x : ℝ) (k : ℕ) : aeval x (S k) = ∏ i ∈ Finset.Icc 1 k, (1 + x ^ i) := by
  simp [S, map_prod]

lemma aeval_S_succ (x : ℝ) (k : ℕ) :
    aeval x (S (k + 1)) = aeval x (S k) * (1 + x ^ (k + 1)) := by
  simp [S_succ]

/-- The `i`-th summand of `VH (m + 2)` at `x`, without its factor `2`. -/
noncomputable def vhTerm (x : ℝ) (m i : ℕ) : ℝ :=
  aeval x (S (m - i)) * aeval x (S (i - 1)) * x ^ (m + 1 - i)

lemma aeval_VH_even (x : ℝ) (h : ℕ) :
    aeval x (VH (2 * h + 2)) = 2 * ∑ i ∈ Finset.Icc 1 h, vhTerm x (2 * h) i + aeval x (S h) ^ 2 := by
  rw [VH_add_two, show (2 * h + 1) / 2 = h by omega, show 2 * h / 2 = h by omega]
  simp [map_sum, map_ofNat, vhTerm]

lemma one_add_pow_pos {x : ℝ} (hx : -1 < x) (k : ℕ) : 0 < 1 + x ^ k := by
  rcases le_or_gt 0 x with h0 | h0
  · positivity
  rcases Nat.eq_zero_or_pos k with rfl | hk
  · norm_num
  have h1 : |x| ^ k < 1 :=
    pow_lt_one₀ (abs_nonneg x) (by rw [abs_lt]; constructor <;> linarith) hk.ne'
  have h2 : |x ^ k| < 1 := by rwa [abs_pow]
  have := (abs_lt.mp h2).1
  linarith

lemma S_pos_of_neg_one_lt {x : ℝ} (hx : -1 < x) (k : ℕ) : 0 < aeval x (S k) := by
  rw [aeval_S]
  exact Finset.prod_pos fun i _ => one_add_pow_pos hx i

lemma sum_Ico_pairs_nonneg (f : ℕ → ℝ) (a k : ℕ)
    (h : ∀ j < k, 0 ≤ f (a + 2 * j) + f (a + 2 * j + 1)) :
    0 ≤ ∑ i ∈ Finset.Ico a (a + 2 * k), f i := by
  induction k with
  | zero => simp
  | succ k ih =>
    rw [show a + 2 * (k + 1) = a + 2 * k + 1 + 1 by ring,
      Finset.sum_Ico_succ_top (by omega), Finset.sum_Ico_succ_top (by omega)]
    have h1 := ih fun j hj => h j (by omega)
    have h2 := h k (by omega)
    linarith

/-! ### Index bookkeeping for `vhTerm` -/

lemma vhTerm_eq (x : ℝ) (a b : ℕ) :
    vhTerm x (b + 1 + 1 + a) (b + 1) = aeval x (S (a + 1)) * aeval x (S b) * x ^ (a + 2) := by
  simp only [vhTerm]
  rw [show b + 1 + 1 + a - (b + 1) = a + 1 by omega, show b + 1 - 1 = b by omega,
    show b + 1 + 1 + a + 1 - (b + 1) = a + 2 by omega]

lemma vhTerm_eq' (x : ℝ) (a b : ℕ) :
    vhTerm x (b + 1 + 1 + a) (b + 1 + 1) = aeval x (S a) * aeval x (S (b + 1)) * x ^ (a + 1) := by
  simp only [vhTerm]
  rw [show b + 1 + 1 + a - (b + 1 + 1) = a by omega, show b + 1 + 1 - 1 = b + 1 by omega,
    show b + 1 + 1 + a + 1 - (b + 1 + 1) = a + 1 by omega]

lemma vhTerm_last (x : ℝ) {h : ℕ} (hh : 1 ≤ h) :
    vhTerm x (2 * h) h = aeval x (S h) * aeval x (S (h - 1)) * x ^ (h + 1) := by
  simp only [vhTerm]
  rw [show 2 * h - h = h by omega, show 2 * h + 1 - h = h + 1 by omega]

/-- Consecutive summands: `t_{i+1} · x (1 + x^{m-i}) = t_i (1 + x^i)`, for every real `x`. -/
lemma vhTerm_succ_mul (x : ℝ) {m i : ℕ} (hi : 1 ≤ i) (him : i + 1 ≤ m) :
    vhTerm x m (i + 1) * (x * (1 + x ^ (m - i))) = vhTerm x m i * (1 + x ^ i) := by
  obtain ⟨a, rfl⟩ : ∃ a, m = i + 1 + a := ⟨m - i - 1, by omega⟩
  obtain ⟨b, rfl⟩ : ∃ b, i = b + 1 := ⟨i - 1, by omega⟩
  rw [vhTerm_eq', vhTerm_eq, show b + 1 + 1 + a - (b + 1) = a + 1 by omega,
    aeval_S_succ, aeval_S_succ]
  ring

/-! ### The interval `−1 < x < 0` -/

/-- For `−1 < x < 0`, even `m` and even `i ≥ 2` with `2i ≤ m`, the pair `t_i + t_{i+1}` is
positive (root-gap.md, Theorem 1: the bracket `b_{2c+1} − b_{2c}`). -/
lemma vhTerm_pair_pos {x : ℝ} (hx1 : -1 < x) (hx0 : x < 0) {m i : ℕ} (hm : Even m)
    (hi : Even i) (hi2 : 2 ≤ i) (him : 2 * i ≤ m) : 0 < vhTerm x m i + vhTerm x m (i + 1) := by
  obtain ⟨a, rfl⟩ : ∃ a, m = i + 1 + a := ⟨m - i - 1, by omega⟩
  obtain ⟨b, rfl⟩ : ∃ b, i = b + 1 := ⟨i - 1, by omega⟩
  rw [vhTerm_eq', vhTerm_eq, aeval_S_succ, aeval_S_succ]
  obtain ⟨r, hr⟩ := hm
  obtain ⟨s, hs⟩ := hi
  have ha : Even (a + 1) := ⟨r - s, by omega⟩
  have hSa := S_pos_of_neg_one_lt hx1 a
  have hSb := S_pos_of_neg_one_lt hx1 b
  have hy : 0 < x ^ (a + 1) := ha.pow_pos hx0.ne
  have hb : Even (b + 1) := ⟨s, hs⟩
  have hw : 0 < x ^ (b + 1) := hb.pow_pos hx0.ne
  have key : 0 ≤ x ^ (b + 1) + x ^ (a + 2) := by
    obtain ⟨d, hd⟩ : ∃ d, a + 2 = b + 1 + d := ⟨a + 1 - b, by omega⟩
    rw [hd, pow_add x (b + 1) d]
    have := one_add_pow_pos hx1 d
    nlinarith [mul_pos hw this]
  have hpos : 0 < aeval x (S a) * aeval x (S b) * x ^ (a + 1) *
      ((1 + x) + (x ^ (b + 1) + x ^ (a + 2))) :=
    mul_pos (mul_pos (mul_pos hSa hSb) hy) (by linarith)
  calc 0 < _ := hpos
    _ = _ := by ring

/-- For `−1 < x < 0` and even `h ≥ 1`: `S_h(x)² + 2 t_h > 0` (root-gap.md, Theorem 1, `h` even). -/
lemma last_pos_of_neg_one_lt {x : ℝ} (hx1 : -1 < x) (hx0 : x < 0) {h : ℕ} (hh : 1 ≤ h)
    (he : Even h) : 0 < aeval x (S h) ^ 2 + 2 * vhTerm x (2 * h) h := by
  rw [vhTerm_last x hh]
  obtain ⟨g, rfl⟩ : ∃ g, h = g + 1 := ⟨h - 1, by omega⟩
  rw [show g + 1 - 1 = g by omega, aeval_S_succ]
  have hS := S_pos_of_neg_one_lt hx1 g
  have hp : 0 < x ^ (g + 1) := he.pow_pos hx0.ne
  have hp1 : x ^ (g + 1) ≤ 1 := by
    have : |x| ^ (g + 1) ≤ 1 :=
      pow_le_one₀ (abs_nonneg x) (by rw [abs_le]; constructor <;> linarith)
    rwa [← abs_pow, abs_of_pos hp] at this
  have hq : 0 < 1 + x ^ (g + 1) := by linarith
  have key : 0 < 1 + x ^ (g + 1) * (1 + 2 * x) := by
    nlinarith [mul_pos hp (show 0 < 1 + x by linarith)]
  have hpos : 0 < aeval x (S g) ^ 2 * (1 + x ^ (g + 1)) * (1 + x ^ (g + 1) * (1 + 2 * x)) :=
    mul_pos (mul_pos (pow_pos hS 2) hq) key
  calc 0 < _ := hpos
    _ = _ := by ring

/-! ### The half-line `x < −1` -/

lemma one_add_pow_neg_of_odd {x : ℝ} (hx : x < -1) {k : ℕ} (hk : Odd k) : 1 + x ^ k < 0 := by
  have h1 : -x ≤ (-x) ^ k := le_self_pow₀ (by linarith) hk.pos.ne'
  have h2 : x ^ k = -(-x) ^ k := by rw [hk.neg_pow, neg_neg]
  linarith

lemma one_add_pow_pos_of_even (x : ℝ) {k : ℕ} (hk : Even k) : 0 < 1 + x ^ k := by
  have := hk.pow_nonneg x
  linarith

lemma one_add_pow_ne_zero_of_lt {x : ℝ} (hx : x < -1) (k : ℕ) : 1 + x ^ k ≠ 0 := by
  rcases Nat.even_or_odd k with hk | hk
  · exact (one_add_pow_pos_of_even x hk).ne'
  · exact (one_add_pow_neg_of_odd hx hk).ne

lemma S_ne_zero_of_lt {x : ℝ} (hx : x < -1) (k : ℕ) : aeval x (S k) ≠ 0 := by
  rw [aeval_S]
  exact Finset.prod_ne_zero_iff.mpr fun i _ => one_add_pow_ne_zero_of_lt hx i

/-- For `x < −1`, `S_{2j+1}(x)` has sign `(−1)^{j+1}`. -/
lemma S_odd_sign {x : ℝ} (hx : x < -1) (j : ℕ) :
    0 < (-1) ^ (j + 1) * aeval x (S (2 * j + 1)) := by
  induction j with
  | zero => norm_num [aeval_S]; linarith
  | succ j ih =>
    rw [show 2 * (j + 1) + 1 = 2 * j + 1 + 1 + 1 by ring, aeval_S_succ, aeval_S_succ]
    have h1 := one_add_pow_pos_of_even x (k := 2 * j + 1 + 1) ⟨j + 1, by ring⟩
    have h2 := one_add_pow_neg_of_odd hx (k := 2 * j + 1 + 1 + 1) ⟨j + 1, by ring⟩
    have hpos : 0 < ((-1) ^ (j + 1) * aeval x (S (2 * j + 1))) * (1 + x ^ (2 * j + 1 + 1)) *
        (-(1 + x ^ (2 * j + 1 + 1 + 1))) := mul_pos (mul_pos ih h1) (by linarith)
    calc 0 < _ := hpos
      _ = _ := by ring

/-- For `x < −1`, `1 ≤ i ≤ j` and `i ≡ j (mod 2)`, the ratio `(1 + x^i) / (x (1 + x^j))`
lies in `(−1, 0)` (real-roots.md, Theorem A, even case: `a_{i+1}/a_i < 1`). -/
lemma ratio_bounds {x : ℝ} (hx : x < -1) {i j : ℕ} (hi : 1 ≤ i) (hij : i ≤ j)
    (hpar : Even (i + j)) :
    -1 < (1 + x ^ i) / (x * (1 + x ^ j)) ∧ (1 + x ^ i) / (x * (1 + x ^ j)) < 0 := by
  obtain ⟨r, rfl⟩ : ∃ r, x = -r := ⟨-x, by ring⟩
  have hr : 1 < r := by linarith
  have hr0 : 0 < r := by linarith
  have hpow : r ^ i ≤ r ^ j := pow_le_pow_right₀ hr.le hij
  have hrj : 0 < r ^ j := by positivity
  rcases Nat.even_or_odd i with hie | hio
  · have hje : Even j := (Nat.even_add.mp hpar).mp hie
    rw [hie.neg_pow, hje.neg_pow]
    have hd : -r * (1 + r ^ j) < 0 := by nlinarith [mul_pos hr0 (show 0 < 1 + r ^ j by linarith)]
    refine ⟨(lt_div_iff_of_neg hd).mpr ?_, div_neg_of_pos_of_neg (by positivity) hd⟩
    nlinarith [mul_pos (sub_pos.mpr hr) hrj]
  · have hjo : Odd j := by
      rw [← Nat.not_even_iff_odd] at hio ⊢
      exact fun h => hio ((Nat.even_add.mp hpar).mpr h)
    rw [hio.neg_pow, hjo.neg_pow]
    have hri : 1 < r ^ i := one_lt_pow₀ hr (by omega)
    have hd : 0 < -r * (1 + -r ^ j) := by
      nlinarith [mul_pos hr0 (show 0 < r ^ j - 1 by linarith)]
    refine ⟨(lt_div_iff₀ hd).mpr ?_, div_neg_of_neg_of_pos (by linarith) hd⟩
    nlinarith [mul_pos (sub_pos.mpr hr) (sub_pos.mpr hri), mul_le_mul_of_nonneg_left hpow hr0.le]

/-- For `x < −1` and odd `h`: `S_h(x)² + 2 t_h < 0` (real-roots.md, Theorem A, last term). -/
lemma last_neg_of_lt {x : ℝ} (hx : x < -1) {h : ℕ} (hh : 1 ≤ h) (ho : Odd h) :
    aeval x (S h) ^ 2 + 2 * vhTerm x (2 * h) h < 0 := by
  rw [vhTerm_last x hh]
  obtain ⟨g, rfl⟩ : ∃ g, h = g + 1 := ⟨h - 1, by omega⟩
  rw [show g + 1 - 1 = g by omega, aeval_S_succ]
  have hS : 0 < aeval x (S g) ^ 2 :=
    lt_of_le_of_ne (sq_nonneg _) (pow_ne_zero 2 (S_ne_zero_of_lt hx g)).symm
  have hq : 1 + x ^ (g + 1) < 0 := one_add_pow_neg_of_odd hx ho
  have key : 0 < 1 + x ^ (g + 1) * (1 + 2 * x) := by
    nlinarith [mul_pos (show 0 < -x ^ (g + 1) - 1 by linarith) (show 0 < -2 * x - 2 by linarith)]
  have hneg : aeval x (S g) ^ 2 * (1 + x ^ (g + 1)) * (1 + x ^ (g + 1) * (1 + 2 * x)) < 0 :=
    mul_neg_of_neg_of_pos (mul_neg_of_pos_of_neg hS hq) key
  calc _ = aeval x (S g) ^ 2 * (1 + x ^ (g + 1)) * (1 + x ^ (g + 1) * (1 + 2 * x)) := by ring
    _ < 0 := hneg

end RealVH

open RealVH

/-- For `x ≥ 0`, `VH_n(x) > 0`. -/
theorem VH_pos_of_nonneg (n : ℕ) {x : ℝ} (hx : 0 ≤ x) : 0 < aeval x (VH n) := by
  have hS : ∀ k, 0 < aeval x (S k) := fun k => S_pos_of_neg_one_lt (by linarith) k
  have hsum : 0 ≤ ∑ i ∈ Finset.Icc 1 ((n - 1) / 2),
      aeval x (S (n - i - 2)) * aeval x (S (i - 1)) * x ^ (n - i - 1) :=
    Finset.sum_nonneg fun i _ => mul_nonneg (mul_nonneg (hS _).le (hS _).le) (pow_nonneg hx _)
  have e : aeval x (VH n) = 2 * ∑ i ∈ Finset.Icc 1 ((n - 1) / 2),
      aeval x (S (n - i - 2)) * aeval x (S (i - 1)) * x ^ (n - i - 1)
        + aeval x (S ((n - 2) / 2)) ^ 2 := by
    simp [VH, map_sum, map_ofNat]
  rw [e]
  have := pow_pos (hS ((n - 2) / 2)) 2
  linarith

/-- root-gap.md Theorem 1: for even `n` and `−1 < x < 0`, `VH_n(x) > 0`. -/
theorem VH_pos_of_neg_one_lt_of_neg {n : ℕ} (hn : Even n) {x : ℝ} (hx1 : -1 < x) (hx0 : x < 0) :
    0 < aeval x (VH n) := by
  obtain ⟨k, rfl⟩ := hn
  rcases k with _ | h
  · simp [VH]
  rw [show h + 1 + (h + 1) = 2 * h + 2 by ring, aeval_VH_even]
  rcases Nat.eq_zero_or_pos h with rfl | hh
  · simp
  have hS := pow_pos (S_pos_of_neg_one_lt hx1 h) 2
  have f1 : 0 < vhTerm x (2 * h) 1 := by
    simp only [vhTerm]
    rw [show (1 : ℕ) - 1 = 0 from rfl, S_zero, map_one, mul_one,
      show 2 * h + 1 - 1 = 2 * h by omega]
    exact mul_pos (S_pos_of_neg_one_lt hx1 _) (Even.pow_pos ⟨h, by ring⟩ hx0.ne)
  have hpair : ∀ j, 2 * j + 3 ≤ h →
      0 ≤ vhTerm x (2 * h) (1 + 1 + 2 * j) + vhTerm x (2 * h) (1 + 1 + 2 * j + 1) := fun j hj =>
    (vhTerm_pair_pos hx1 hx0 ⟨h, by ring⟩ ⟨j + 1, by ring⟩ (by omega) (by omega)).le
  rcases Nat.even_or_odd h with ⟨k, hk⟩ | ⟨k, hk⟩
  · -- `h = 2k + 2`: pairs, then the leftover term `t_h` against the square
    obtain ⟨k, rfl⟩ : ∃ k', k = k' + 1 := ⟨k - 1, by omega⟩
    have hsplit : ∑ i ∈ Finset.Icc 1 h, vhTerm x (2 * h) i = vhTerm x (2 * h) 1
        + ∑ i ∈ Finset.Ico (1 + 1) (1 + 1 + 2 * k), vhTerm x (2 * h) i + vhTerm x (2 * h) h := by
      rw [show Finset.Icc 1 h = Finset.Ico 1 (1 + 1 + 2 * k + 1) by ext i; simp; omega,
        Finset.sum_Ico_succ_top (by omega), Finset.sum_eq_sum_Ico_succ_bot (by omega),
        show 1 + 1 + 2 * k = h by omega]
    have hP := sum_Ico_pairs_nonneg (vhTerm x (2 * h)) (1 + 1) k fun j hj => hpair j (by omega)
    have hlast := last_pos_of_neg_one_lt hx1 hx0 (h := h) (by omega) ⟨k + 1, by omega⟩
    linarith
  · -- `h = 2k + 1`: the first term, then pairs
    have hsplit : ∑ i ∈ Finset.Icc 1 h, vhTerm x (2 * h) i = vhTerm x (2 * h) 1
        + ∑ i ∈ Finset.Ico (1 + 1) (1 + 1 + 2 * k), vhTerm x (2 * h) i := by
      rw [show Finset.Icc 1 h = Finset.Ico 1 (1 + 1 + 2 * k) by ext i; simp; omega,
        Finset.sum_eq_sum_Ico_succ_bot (by omega)]
    have hP := sum_Ico_pairs_nonneg (vhTerm x (2 * h)) (1 + 1) k fun j hj => hpair j (by omega)
    linarith

/-- real-roots.md Theorem A (even case): for even `n` and `x < −1`, `VH_n(x) ≠ 0`. -/
theorem VH_ne_zero_of_lt_neg_one {n : ℕ} (hn : Even n) {x : ℝ} (hx : x < -1) :
    aeval x (VH n) ≠ 0 := by
  obtain ⟨k, rfl⟩ := hn
  rcases k with _ | h
  · simp [VH]
  rw [show h + 1 + (h + 1) = 2 * h + 2 by ring, aeval_VH_even]
  rcases Nat.eq_zero_or_pos h with rfl | hh
  · simp
  have hx0 : x ≠ 0 := (show x < 0 by linarith).ne
  have hSh : 0 < aeval x (S h) ^ 2 :=
    lt_of_le_of_ne (sq_nonneg _) (pow_ne_zero 2 (S_ne_zero_of_lt hx h)).symm
  -- `f i = (−1)^h t_i` is an alternating sequence with decreasing absolute values and `f 1 > 0`.
  have f1 : 0 < (-1) ^ h * vhTerm x (2 * h) 1 := by
    have hs := S_odd_sign hx (h - 1)
    rw [show h - 1 + 1 = h by omega, show 2 * (h - 1) + 1 = 2 * h - 1 by omega] at hs
    simp only [vhTerm]
    rw [show (1 : ℕ) - 1 = 0 from rfl, S_zero, map_one, mul_one,
      show 2 * h + 1 - 1 = 2 * h by omega]
    have hp : 0 < x ^ (2 * h) := Even.pow_pos ⟨h, by ring⟩ hx0
    calc 0 < ((-1) ^ h * aeval x (S (2 * h - 1))) * x ^ (2 * h) := mul_pos hs hp
      _ = _ := by ring
  have hstep : ∀ i, 1 ≤ i → i < h → ∃ ρ : ℝ, -1 < ρ ∧ ρ < 0 ∧
      (-1) ^ h * vhTerm x (2 * h) (i + 1) = ρ * ((-1) ^ h * vhTerm x (2 * h) i) := by
    intro i hi1 hih
    have hd : x * (1 + x ^ (2 * h - i)) ≠ 0 := mul_ne_zero hx0 (one_add_pow_ne_zero_of_lt hx _)
    have hid := vhTerm_succ_mul x (m := 2 * h) hi1 (by omega)
    obtain ⟨hρ1, hρ2⟩ := ratio_bounds hx hi1 (show i ≤ 2 * h - i by omega) ⟨h, by omega⟩
    refine ⟨_, hρ1, hρ2, ?_⟩
    rw [eq_div_of_mul_eq hd hid]
    ring
  have hodd : ∀ j, 2 * j + 1 ≤ h → 0 < (-1) ^ h * vhTerm x (2 * h) (2 * j + 1) := by
    intro j
    induction j with
    | zero => intro _; exact f1
    | succ j ih =>
      intro hj
      obtain ⟨ρ1, h1a, h1b, h1⟩ := hstep (2 * j + 1) (by omega) (by omega)
      obtain ⟨ρ2, h2a, h2b, h2⟩ := hstep (2 * j + 1 + 1) (by omega) (by omega)
      rw [show 2 * (j + 1) + 1 = 2 * j + 1 + 1 + 1 by ring, h2, h1, ← mul_assoc]
      exact mul_pos (mul_pos_of_neg_of_neg h2b h1b) (ih (by omega))
  have hpair : ∀ j, 2 * j + 2 ≤ h →
      0 ≤ (-1) ^ h * vhTerm x (2 * h) (1 + 2 * j) + (-1) ^ h * vhTerm x (2 * h) (1 + 2 * j + 1) := by
    intro j hj
    obtain ⟨ρ, ha, hb, he⟩ := hstep (1 + 2 * j) (by omega) (by omega)
    rw [he]
    have := hodd j (by omega)
    rw [show 2 * j + 1 = 1 + 2 * j by ring] at this
    nlinarith [mul_pos (show 0 < 1 + ρ by linarith) this]
  rcases Nat.even_or_odd h with hev | hod
  · -- `h` even: `Σ t_i ≥ 0`, so `VH > 0`
    have hsig : (-1 : ℝ) ^ h = 1 := hev.neg_one_pow
    obtain ⟨k, hk⟩ := hev
    have hP := sum_Ico_pairs_nonneg (vhTerm x (2 * h)) 1 k fun j hj => by
      have := hpair j (by omega)
      rwa [hsig, one_mul, one_mul] at this
    rw [show Finset.Ico 1 (1 + 2 * k) = Finset.Icc 1 h by ext i; simp; omega] at hP
    have : 0 < 2 * ∑ i ∈ Finset.Icc 1 h, vhTerm x (2 * h) i + aeval x (S h) ^ 2 := by linarith
    exact this.ne'
  · -- `h` odd: `Σ t_i ≤ t_h`, and `S_h² + 2 t_h < 0`, so `VH < 0`
    have hsig : (-1 : ℝ) ^ h = -1 := hod.neg_one_pow
    obtain ⟨k, hk⟩ := hod
    have hP := sum_Ico_pairs_nonneg (fun i => -vhTerm x (2 * h) i) 1 k fun j hj => by
      have := hpair j (by omega)
      rwa [hsig, neg_one_mul, neg_one_mul] at this
    have hsplit : ∑ i ∈ Finset.Icc 1 h, vhTerm x (2 * h) i
        = ∑ i ∈ Finset.Ico 1 (1 + 2 * k), vhTerm x (2 * h) i + vhTerm x (2 * h) h := by
      rw [show Finset.Icc 1 h = Finset.Ico 1 (1 + 2 * k + 1) by ext i; simp; omega,
        Finset.sum_Ico_succ_top (by omega), show 1 + 2 * k = h by omega]
    simp only [Finset.sum_neg_distrib] at hP
    have hlast := last_neg_of_lt hx (h := h) (by omega) ⟨k, hk⟩
    have : 2 * ∑ i ∈ Finset.Icc 1 h, vhTerm x (2 * h) i + aeval x (S h) ^ 2 < 0 := by linarith
    exact this.ne

end Tatami

Tatami/ConjC.lean

import Tatami.ConjCCore
import Tatami.RealVH

/-!
# `conj:Pcon`(c): for even `n`, `P_n` has no real root

Combines `Tatami.conj_c_of_VH` (the assembly, which uses `conj:Pcon`(e) at `x = −1`) with the
three sign facts about `VH n` proved in `Tatami.RealVH`.
-/

open Polynomial

namespace Tatami

/-- **`conj:Pcon`(c)**: for even `n`, `P_n(x) ≠ 0` for every real `x`. -/
theorem conj_c {n : ℕ} (hn : Even n) (x : ℝ) : aeval x (P n) ≠ 0 :=
  conj_c_of_VH hn (fun _ hx => VH_pos_of_nonneg n hx)
    (fun _ h1 h0 => VH_pos_of_neg_one_lt_of_neg hn h1 h0)
    (fun _ hx => VH_ne_zero_of_lt_neg_one hn hx) x

end Tatami

Tatami/OddRoot.lean

import Tatami.AtMinusOneOdd
import Tatami.LowCoefficients
import Tatami.ConjCCore

/-!
# `conj:Pcon`(b), existence: for odd `n`, `P_n` has a real root in `(−1, 0)`

`P_n(0) = 1`, and for odd `n = 2k + 3`, `P_n(−1) = −∑ 2^{k−i} C(2i, i) < 0` by `conj:Pcon`(e).
The intermediate value theorem gives a root in between. (The report also places it in
`(−1, −1/2)`, and conjectures it is unique; neither is proved here.)
-/

open Polynomial

namespace Tatami

lemma P_eval_zero (n : ℕ) : (P n).eval 0 = 1 := by
  have h := congrArg (eval 0) (VH_eq_D_mul_P n)
  rw [eval_mul, ← coeff_zero_eq_eval_zero (D n), D_coeff_zero, one_mul] at h
  rw [← h, ← coeff_zero_eq_eval_zero]
  rcases Nat.lt_or_ge n 2 with hn | hn
  · interval_cases n <;> simp [VH]
  obtain ⟨N, rfl⟩ : ∃ N, n = N + 2 := ⟨n - 2, by omega⟩
  rw [conj_vhcon_a N 0 N (by omega) le_rfl, sq, coeff_zero_eq_eval_zero, eval_mul,
    ← coeff_zero_eq_eval_zero, S_coeff_zero, one_mul]

theorem odd_has_real_root (k : ℕ) :
    ∃ x : ℝ, -1 < x ∧ x < 0 ∧ aeval x (P (2 * k + 3)) = 0 := by
  set f : ℝ → ℝ := fun x => aeval x (P (2 * k + 3))
  have hcont : ContinuousOn f (Set.Icc (-1) 0) :=
    (Polynomial.continuous_aeval (P (2 * k + 3))).continuousOn
  have hneg : f (-1) < 0 := by
    simp only [f]
    rw [aeval_neg_one, P_eval_neg_one_odd]
    have hpos : (0 : ℤ) < ∑ i ∈ Finset.range (k + 1), 2 ^ (k - i) * (Nat.centralBinom i : ℤ) := by
      apply Finset.sum_pos
      · intro i _
        have := Nat.centralBinom_pos i
        positivity
      · exact ⟨0, by simp⟩
    have : ((-∑ i ∈ Finset.range (k + 1), 2 ^ (k - i) * (Nat.centralBinom i : ℤ) : ℤ) : ℝ) < 0 := by
      exact_mod_cast (neg_neg_of_pos hpos)
    exact this
  have hzero : 0 < f 0 := by
    simp only [f]
    rw [aeval_def, eval₂_at_zero, coeff_zero_eq_eval_zero, P_eval_zero]
    simp
  obtain ⟨x, hx, hfx⟩ := intermediate_value_Ioo (by norm_num) hcont ⟨hneg, hzero⟩
  exact ⟨x, hx.1, hx.2, hfx⟩

end Tatami

Tatami/ModTwo.lean

import Tatami.Cyclotomic

/-!
# `P_n` mod 2 (irreducibility report, Theorem 1 (a), (b))

Source: `code/pn-polynomial/reports/irreducibility.md`, §2, Theorem 1. With `m = ⌊(n−2)/2⌋`
and `J = {1 ≤ j ≤ m : (m mod 2j) ≥ j}`:

* (a) `S_m² = D_n · ∏_{j ∈ J} Φ_{2j}`: the quotient `B_n = S_m²/D_n` is a product of distinct
  cyclotomic polynomials.
* (b) `P_n ≡ ∏_{j ∈ J} Φ_{2j}` (mod 2).

(a) is proved by comparing how often each `Φ_d` occurs (`Tatami.cnt`). For (b),
`VH_n = 2A + S_m²`, so `D_n (P_n − B_n) = 2A`; reducing mod 2 and cancelling the monic `D_n`
leaves `P_n ≡ B_n`.
-/

open Polynomial

namespace Tatami

/-- The index set `J = {1 ≤ j ≤ m : (m mod 2j) ≥ j}`. -/
def Jset (m : ℕ) : Finset ℕ := (Finset.Icc 1 m).filter (fun j => j ≤ m % (2 * j))

/-- `B_n = ∏_{j ∈ J} Φ_{2j}`, with `m = ⌊(n−2)/2⌋`. -/
noncomputable def B (n : ℕ) : ℤ[X] := ∏ j ∈ Jset ((n - 2) / 2), cyclotomic (2 * j) ℤ

lemma sum_Icc_sub_add {x : ℕ → ℕ} (hx : Antitone x) (L : ℕ) :
    ∑ l ∈ Finset.Icc 1 L, (x l - x (l + 1)) + x (L + 1) = x 1 := by
  induction L with
  | zero => simp
  | succ L ih =>
    rw [Finset.sum_Icc_succ_top (by omega)]
    have h1 : x (L + 1 + 1) ≤ x (L + 1) := hx (by omega)
    have h2 : x (L + 1) ≤ x 1 := hx (by omega)
    omega

lemma div_eq_two_mul_add (m j : ℕ) (hj : 0 < j) :
    m / j = 2 * (m / (2 * j)) + (if j ≤ m % (2 * j) then 1 else 0) := by
  have hr : m % (2 * j) < 2 * j := Nat.mod_lt _ (by omega)
  have hm : m = j * (2 * (m / (2 * j))) + m % (2 * j) := by
    rw [show j * (2 * (m / (2 * j))) = 2 * j * (m / (2 * j)) by ring]
    exact (Nat.div_add_mod m (2 * j)).symm
  have h1 : m / j = 2 * (m / (2 * j)) + m % (2 * j) / j := by
    conv_lhs => rw [hm]
    rw [Nat.mul_add_div hj]
  rw [h1]
  congr 1
  split_ifs with h
  · exact Nat.div_eq_of_lt_le (by omega) (by omega)
  · exact Nat.div_eq_of_lt (by omega)

/-- The exponent of `Φ_d` in `B n`. -/
def bexp (m d : ℕ) : ℕ := if d ∈ (Jset m).image (fun j => 2 * j) then 1 else 0

lemma B_eq_prod (n R : ℕ) (hR : 2 * ((n - 2) / 2) < R) :
    B n = ∏ d ∈ Finset.range R, cyclotomic d ℤ ^ bexp ((n - 2) / 2) d := by
  set m := (n - 2) / 2
  have hinj : Set.InjOn (fun j => 2 * j) (Jset m : Set ℕ) := by
    intro a _ b _ h; simp only at h; omega
  rw [B, ← Finset.prod_image (f := fun d => cyclotomic d ℤ) hinj]
  have hsub : (Jset m).image (fun j => 2 * j) ⊆ Finset.range R := by
    intro d hd
    simp only [Finset.mem_image, Jset, Finset.mem_filter, Finset.mem_Icc] at hd
    obtain ⟨j, ⟨⟨_, hj⟩, _⟩, rfl⟩ := hd
    simp only [Finset.mem_range]; omega
  rw [← Finset.prod_subset hsub (fun d _ hd => by simp [bexp, hd])]
  apply Finset.prod_congr rfl
  intro d hd
  simp [bexp, hd]

/-- The exponents add up: `Φ_d` occurs `2 cnt(m, d)` times in `S_m²`, and that is its count
in `D_n` plus its count in `B_n`. -/
lemma exponent_identity (n d : ℕ) :
    ∑ j ∈ Finset.Icc 1 n, cnt ((n - 2) / 2 ^ j) d + bexp ((n - 2) / 2) d
      = 2 * cnt ((n - 2) / 2) d := by
  set N := n - 2
  set m := N / 2
  rcases Nat.even_or_odd d with ⟨j, rfl⟩ | hodd
  · rcases Nat.eq_zero_or_pos j with rfl | hj
    · simp [cnt_zero_right, bexp, Jset]
    rw [show j + j = 2 * j by ring]
    simp only [cnt_even]
    set x : ℕ → ℕ := fun l => N / 2 ^ l / j with hx
    have hterm : ∀ l, N / 2 ^ l / j - N / 2 ^ l / (2 * j) = x l - x (l + 1) := by
      intro l
      simp only [hx, Nat.div_div_eq_div_mul, pow_succ]
      ring_nf
    simp only [hterm]
    have hanti : Antitone x := by
      intro l l' hll'
      simp only [hx]
      apply Nat.div_le_div_right
      apply Nat.div_le_div_left (Nat.pow_le_pow_right (by omega) hll') (by positivity)
    have htel := sum_Icc_sub_add hanti n
    have hlast : x (n + 1) = 0 := by
      have hlt : N < 2 ^ (n + 1) :=
        lt_of_lt_of_le Nat.lt_two_pow_self (Nat.pow_le_pow_right (by omega) (by omega))
      simp only [hx]
      rw [Nat.div_eq_of_lt hlt, Nat.zero_div]
    rw [hlast, add_zero] at htel
    rw [htel]
    have hx1 : x 1 = m / j := by
      simp only [hx, m, pow_one]
    have hb : bexp m (2 * j) = if j ≤ m % (2 * j) then 1 else 0 := by
      have hJ : ∀ j', j' ∈ Jset m ↔ (1 ≤ j' ∧ j' ≤ m) ∧ j' ≤ m % (2 * j') := by
        intro j'; simp only [Jset, Finset.mem_filter, Finset.mem_Icc]
      have hmem : (2 * j ∈ (Jset m).image (fun j => 2 * j)) ↔ j ≤ m % (2 * j) := by
        rw [Finset.mem_image]
        constructor
        · rintro ⟨j', hj', hjj⟩
          rw [show j' = j by omega] at hj'
          exact ((hJ j).mp hj').2
        · intro h
          exact ⟨j, (hJ j).mpr ⟨⟨hj, le_trans h (Nat.mod_le _ _)⟩, h⟩, rfl⟩
      unfold bexp
      by_cases h : j ≤ m % (2 * j)
      · rw [if_pos (hmem.mpr h), if_pos h]
      · rw [if_neg (mt hmem.mp h), if_neg h]
    have hdiv : m / (2 * j) = m / j / 2 := by
      rw [Nat.div_div_eq_div_mul m j 2, mul_comm j 2]
    rw [hx1, hb, hdiv]
    have := div_eq_two_mul_add m j hj
    rw [← hdiv] at *
    omega
  · have hd : ¬ 2 ∣ d := by
      rintro ⟨c, rfl⟩
      exact Nat.not_even_iff_odd.mpr hodd ⟨c, by ring⟩
    have hb : bexp m d = 0 := by
      simp only [bexp, Finset.mem_image]
      rw [if_neg]
      rintro ⟨j, _, rfl⟩
      exact hd ⟨j, rfl⟩
    simp [cnt_odd _ _ hd, hb]

/-- **Theorem 1(a)**: `S_m² = D_n · B_n`, with `B_n = ∏_{j ∈ J} Φ_{2j}`. -/
theorem S_sq_eq_D_mul_B (n : ℕ) : S ((n - 2) / 2) ^ 2 = D n * B n := by
  set m := (n - 2) / 2
  set R := 2 * (m + n) + 1
  have hR : ∀ j ∈ Finset.Icc 1 n, 2 * ((n - 2) / 2 ^ j) < R := by
    intro j _
    have : (n - 2) / 2 ^ j ≤ n := le_trans (Nat.div_le_self _ _) (by omega)
    omega
  rw [D, Finset.prod_congr rfl (fun j hj => S_eq_prod_cyclotomic _ R (hR j hj)),
    Finset.prod_comm, B_eq_prod n R (by omega), ← Finset.prod_mul_distrib,
    S_eq_prod_cyclotomic m R (by omega), ← Finset.prod_pow]
  apply Finset.prod_congr rfl
  intro d _
  rw [Finset.prod_pow_eq_pow_sum, ← pow_add, ← pow_mul, exponent_identity, mul_comm]

/-- `B n` is a polynomial with integer coefficients and `D n * B n = S_m²`; it is monic. -/
lemma B_monic (n : ℕ) : (B n).Monic :=
  monic_prod_of_monic _ _ fun j _ => cyclotomic.monic _ ℤ

/-- **Theorem 1(b)**: `P_n ≡ ∏_{j ∈ J} Φ_{2j}` (mod 2). -/
theorem P_mod_two (n : ℕ) :
    (P n).map (Int.castRingHom (ZMod 2)) = (B n).map (Int.castRingHom (ZMod 2)) := by
  set φ := Int.castRingHom (ZMod 2)
  set A := ∑ i ∈ Finset.Icc 1 ((n - 1) / 2), S (n - i - 2) * S (i - 1) * X ^ (n - i - 1)
  have hVH : VH n = 2 * A + S ((n - 2) / 2) ^ 2 := rfl
  have hkey : D n * (P n - B n) = 2 * A := by
    have h1 := VH_eq_D_mul_P n
    have h2 := S_sq_eq_D_mul_B n
    rw [hVH] at h1
    linear_combination -h1 + h2
  have hmap := congrArg (Polynomial.map φ) hkey
  rw [Polynomial.map_mul, Polynomial.map_mul, Polynomial.map_sub] at hmap
  have h2 : (2 : ℤ[X]).map φ = 0 := by
    rw [show (2 : ℤ[X]) = C 2 by simp, map_C]
    simp [φ]
    rfl
  rw [h2, zero_mul] at hmap
  have hD : (D n).map φ ≠ 0 := ((D_monic n).map φ).ne_zero
  have := (mul_eq_zero.mp hmap).resolve_left hD
  exact sub_eq_zero.mp this

end Tatami

Tatami/TopDegree.lean

import Tatami.ModTwo

/-!
# Degrees and leading coefficients

* `VH_{N+2}` has degree `N(N+1)/2` and leading coefficient `2` (for `N ≥ 1`): the top term
  comes only from `i = 1`, `2 S_{N−1} z^N`.
* Hence `P_n` has leading coefficient `2` for `n ≥ 3` (irreducibility report, Theorem 1(c);
  low-coefficients report, Theorem 4).
* `deg P_n − deg B_n = ⌊(n−1)/2⌋²` (irreducibility report, Theorem 1(d)), where
  `B_n = ∏_{j∈J} Φ_{2j} ≡ P_n` (mod 2).
-/

open Polynomial

namespace Tatami

lemma two_mul_natDegree_S (k : ℕ) : 2 * (S k).natDegree = k * (k + 1) := by
  induction k with
  | zero => simp
  | succ k ih =>
    rw [S_succ, (S_monic k).natDegree_mul (one_add_X_pow_monic (by omega))]
    have h1 : (1 + X ^ (k + 1) : ℤ[X]).natDegree = k + 1 := by
      rw [add_comm, ← C_1, natDegree_X_pow_add_C]
    rw [h1]
    nlinarith [ih]

lemma natDegree_term (N i : ℕ) :
    (S (N - i) * S (i - 1) * X ^ (N + 1 - i)).natDegree
      = (S (N - i)).natDegree + (S (i - 1)).natDegree + (N + 1 - i) := by
  rw [((S_monic _).mul (S_monic _)).natDegree_mul (monic_X_pow _),
    (S_monic _).natDegree_mul (S_monic _), natDegree_X_pow]

/-- `VH_{N+2}` has degree `N(N+1)/2` and leading coefficient `2`, for `N ≥ 1`. -/
theorem VH_natDegree_leadingCoeff (N : ℕ) (hN : 1 ≤ N) :
    2 * (VH (N + 2)).natDegree = N * (N + 1) ∧ (VH (N + 2)).leadingCoeff = 2 := by
  set top := (S (N - 1)).natDegree + N with htop
  have htop2 : 2 * top = N * (N + 1) := by
    have := two_mul_natDegree_S (N - 1)
    obtain ⟨M, rfl⟩ : ∃ M, N = M + 1 := ⟨N - 1, by omega⟩
    simp only [htop, show M + 1 - 1 = M by omega] at *
    nlinarith
  -- split off the `i = 1` term
  set T1 := S (N - 1) * S (1 - 1) * X ^ (N + 1 - 1) with hT1
  set R := 2 * ∑ i ∈ Finset.Icc 2 ((N + 1) / 2), S (N - i) * S (i - 1) * X ^ (N + 1 - i)
    + S (N / 2) ^ 2 with hR
  have hsplit : VH (N + 2) = 2 * T1 + R := by
    rw [VH_add_two, hR, hT1]
    rw [show Finset.Icc 1 ((N + 1) / 2) = insert 1 (Finset.Icc 2 ((N + 1) / 2)) by
      ext x; simp only [Finset.mem_insert, Finset.mem_Icc]; omega]
    rw [Finset.sum_insert (by simp)]
    ring
  have hT1deg : T1.natDegree = top := by
    rw [hT1, natDegree_term]
    simp [htop]
  have hT1m : T1.Monic := ((S_monic _).mul (S_monic _)).mul (monic_X_pow _)
  -- every other term has lower degree
  have hRdeg : R.natDegree < top := by
    have hsum : (∑ i ∈ Finset.Icc 2 ((N + 1) / 2), S (N - i) * S (i - 1) * X ^ (N + 1 - i)).natDegree
        ≤ top - 1 := by
      apply natDegree_sum_le_of_forall_le
      intro i hi
      rw [Finset.mem_Icc] at hi
      rw [natDegree_term]
      have h1 := two_mul_natDegree_S (N - i)
      have h2 := two_mul_natDegree_S (i - 1)
      obtain ⟨s, rfl⟩ : ∃ s, i = s + 2 := ⟨i - 2, by omega⟩
      obtain ⟨t, rfl⟩ : ∃ t, N = s + 2 + t := ⟨N - (s + 2), by omega⟩
      simp only [show s + 2 + t - (s + 2) = t by omega, show s + 2 - 1 = s + 1 by omega,
        show s + 2 + t + 1 - (s + 2) = t + 1 by omega] at *
      have : 2 * ((S t).natDegree + (S (s + 1)).natDegree + (t + 1)) + 2 ≤ 2 * top := by
        rw [htop2]; nlinarith
      omega
    have hsq : (S (N / 2) ^ 2).natDegree < top := by
      rw [(S_monic _).natDegree_pow]
      have hS := two_mul_natDegree_S (N / 2)
      rcases Nat.even_or_odd N with ⟨h, hh⟩ | ⟨h, hh⟩
      · rw [show N / 2 = h by omega] at hS ⊢
        have hh1 : 1 ≤ h := by omega
        rw [hh] at htop2
        nlinarith
      · rw [show N / 2 = h by omega] at hS ⊢
        rw [hh] at htop2
        nlinarith
    have h2 : (2 * ∑ i ∈ Finset.Icc 2 ((N + 1) / 2), S (N - i) * S (i - 1) * X ^ (N + 1 - i)).natDegree
        ≤ top - 1 := by
      refine (natDegree_mul_le).trans ?_
      have : (2 : ℤ[X]).natDegree = 0 := by
        rw [show (2 : ℤ[X]) = C 2 by simp]; exact natDegree_C 2
      omega
    have htop1 : 1 ≤ top := by nlinarith
    refine lt_of_le_of_lt (natDegree_add_le _ _) ?_
    omega
  have h2T1 : (2 * T1).natDegree = top ∧ (2 * T1).leadingCoeff = 2 := by
    rw [show (2 : ℤ[X]) = C 2 by simp, natDegree_C_mul (by norm_num), leadingCoeff_mul,
      leadingCoeff_C, hT1m.leadingCoeff, mul_one]
    exact ⟨hT1deg, rfl⟩
  obtain ⟨hd, hl⟩ := h2T1
  have hlt : R.natDegree < (2 * T1).natDegree := hd ▸ hRdeg
  rw [hsplit]
  refine ⟨?_, ?_⟩
  · rw [natDegree_add_eq_left_of_natDegree_lt hlt, hd, htop2]
  · rw [leadingCoeff_add_of_degree_lt' (degree_lt_degree hlt), hl]

/-- **The leading coefficient of `P_n` is `2`** for `n ≥ 3`. -/
theorem P_leadingCoeff {n : ℕ} (hn : 3 ≤ n) : (P n).leadingCoeff = 2 := by
  obtain ⟨N, rfl⟩ : ∃ N, n = N + 2 := ⟨n - 2, by omega⟩
  have h := (VH_natDegree_leadingCoeff N (by omega)).2
  rw [VH_eq_D_mul_P, leadingCoeff_mul, (D_monic _).leadingCoeff, one_mul] at h
  exact h

lemma P_ne_zero {n : ℕ} (hn : 3 ≤ n) : P n ≠ 0 := by
  intro h
  have := P_leadingCoeff hn
  rw [h, leadingCoeff_zero] at this
  norm_num at this

/-- **Irreducibility report, Theorem 1(d)**: `deg P_n = deg B_n + ⌊(n−1)/2⌋²` for `n ≥ 3`. -/
theorem natDegree_P_eq {n : ℕ} (hn : 3 ≤ n) :
    (P n).natDegree = (B n).natDegree + ((n - 1) / 2) ^ 2 := by
  obtain ⟨N, rfl⟩ : ∃ N, n = N + 2 := ⟨n - 2, by omega⟩
  have hV := (VH_natDegree_leadingCoeff N (by omega)).1
  rw [VH_eq_D_mul_P, (D_monic _).natDegree_mul' (P_ne_zero hn)] at hV
  have hB := congrArg natDegree (S_sq_eq_D_mul_B (N + 2))
  rw [(D_monic _).natDegree_mul' (B_monic _).ne_zero, (S_monic _).natDegree_pow,
    show N + 2 - 2 = N by omega] at hB
  have hS := two_mul_natDegree_S (N / 2)
  rw [show N + 2 - 1 = N + 1 by omega]
  rcases Nat.even_or_odd N with ⟨h, hh⟩ | ⟨h, hh⟩
  · rw [show N / 2 = h by omega] at hS hB
    rw [show (N + 1) / 2 = h by omega]
    subst hh
    nlinarith
  · rw [show N / 2 = h by omega] at hS hB
    rw [show (N + 1) / 2 = h + 1 by omega]
    subst hh
    nlinarith

end Tatami

Tatami/TopCoefficients.lean

import Tatami.TopDegree
import Tatami.LowCoefficients

/-!
# The top coefficients of `VH_n`: thesis `conj:vhcon`(b)

Thesis `conj:vhcon`(b): for `k < n − 3`, the coefficient `k` below the top of `VH_n` is
`2 [z^k] ∏_{m≥1} (1 + z^m)`, twice the number of partitions of `k` into distinct parts.
Sources: `code/pn-polynomial/reports/structure.md` §2.1(b) and `low-coefficients.md`
Theorem 4.

Proof: the top of `VH_{N+2}` is the `i = 1` term `2 S_{N−1} z^N`; every other term reaches at
most `N − 1` below the top. And `S_{N−1}` is a palindrome.
-/

open Polynomial

namespace Tatami

lemma reverse_one' : (1 : ℤ[X]).reverse = 1 := by
  rw [← C_1, reverse_C]

lemma S_reverse (k : ℕ) : (S k).reverse = S k := by
  induction k with
  | zero => simp [reverse_one']
  | succ k ih =>
    rw [S_succ, reverse_mul_of_domain, ih]
    congr 1
    have : (1 + X ^ (k + 1) : ℤ[X]) = X ^ (k + 1) * 1 + C 1 := by simp; ring
    rw [this, reverse_add_C, reverse_X_pow_mul, reverse_one']
    simp
    ring

lemma S_coeff_palindrome (j k : ℕ) (hk : k ≤ (S j).natDegree) :
    (S j).coeff ((S j).natDegree - k) = (S j).coeff k := by
  conv_rhs => rw [← S_reverse j]
  rw [coeff_reverse, revAt_le hk]

/-- **Thesis `conj:vhcon`(b)**: for `k ≤ N − 2`, the coefficient `k` below the top of
`VH_{N+2}` is `2 [z^k] S_K` for any `K ≥ k` (equivalently, of `∏_{m≥1}(1 + z^m)`). -/
theorem conj_vhcon_b (N k K : ℕ) (hk : k + 2 ≤ N) (hK : k ≤ K) :
    (VH (N + 2)).coeff ((VH (N + 2)).natDegree - k) = 2 * (S K).coeff k := by
  set top := (S (N - 1)).natDegree + N with htop
  have hSdeg := two_mul_natDegree_S (N - 1)
  have htop2 : 2 * top = N * (N + 1) := by
    obtain ⟨M, rfl⟩ : ∃ M, N = M + 1 := ⟨N - 1, by omega⟩
    simp only [htop, show M + 1 - 1 = M by omega] at *
    nlinarith
  have hVdeg : (VH (N + 2)).natDegree = top := by
    have := (VH_natDegree_leadingCoeff N (by omega)).1
    omega
  rw [hVdeg]
  set R := 2 * ∑ i ∈ Finset.Icc 2 ((N + 1) / 2), S (N - i) * S (i - 1) * X ^ (N + 1 - i)
    + S (N / 2) ^ 2 with hR
  have hsplit : VH (N + 2) = 2 * (S (N - 1) * X ^ N) + R := by
    rw [VH_add_two, hR]
    rw [show Finset.Icc 1 ((N + 1) / 2) = insert 1 (Finset.Icc 2 ((N + 1) / 2)) by
      ext x; simp only [Finset.mem_insert, Finset.mem_Icc]; omega]
    rw [Finset.sum_insert (by simp)]
    simp only [show 1 - 1 = 0 from rfl, S_zero, mul_one, show N + 1 - 1 = N by omega]
    ring
  -- the rest stays at least `N − 1` below the top
  have hRdeg : R.natDegree ≤ top - (N - 1) := by
    have hsum : (∑ i ∈ Finset.Icc 2 ((N + 1) / 2), S (N - i) * S (i - 1) * X ^ (N + 1 - i)).natDegree
        ≤ top - (N - 1) := by
      apply natDegree_sum_le_of_forall_le
      intro i hi
      rw [Finset.mem_Icc] at hi
      rw [natDegree_term]
      have h1 := two_mul_natDegree_S (N - i)
      have h2 := two_mul_natDegree_S (i - 1)
      obtain ⟨s, rfl⟩ : ∃ s, i = s + 2 := ⟨i - 2, by omega⟩
      obtain ⟨t, rfl⟩ : ∃ t, N = s + 2 + t := ⟨N - (s + 2), by omega⟩
      simp only [show s + 2 + t - (s + 2) = t by omega, show s + 2 - 1 = s + 1 by omega,
        show s + 2 + t + 1 - (s + 2) = t + 1 by omega] at *
      have : 2 * ((S t).natDegree + (S (s + 1)).natDegree + (t + 1)) + 2 * (s + 2 + t - 1)
          ≤ 2 * top := by
        rw [htop2, show s + 2 + t - 1 = s + 1 + t by omega]; nlinarith
      omega
    have hsq : (S (N / 2) ^ 2).natDegree ≤ top - (N - 1) := by
      rw [(S_monic _).natDegree_pow]
      have hS := two_mul_natDegree_S (N / 2)
      rcases Nat.even_or_odd N with ⟨h, hh⟩ | ⟨h, hh⟩
      · rw [show N / 2 = h by omega] at hS ⊢
        rw [hh] at htop2
        have : 2 * (2 * (S h).natDegree) + 2 * (N - 1) ≤ 2 * top := by
          rw [hS, htop2, hh, show h + h - 1 = 2 * h - 1 by omega]
          rcases Nat.eq_zero_or_pos h with rfl | hpos
          · simp
          obtain ⟨g, rfl⟩ : ∃ g, h = g + 1 := ⟨h - 1, by omega⟩
          rw [show 2 * (g + 1) - 1 = 2 * g + 1 by omega]
          nlinarith
        omega
      · rw [show N / 2 = h by omega] at hS ⊢
        rw [hh] at htop2
        have : 2 * (2 * (S h).natDegree) + 2 * (N - 1) ≤ 2 * top := by
          rw [hS, htop2, hh, show 2 * h + 1 - 1 = 2 * h by omega]
          nlinarith
        omega
    have h2 : (2 * ∑ i ∈ Finset.Icc 2 ((N + 1) / 2), S (N - i) * S (i - 1) * X ^ (N + 1 - i)).natDegree
        ≤ top - (N - 1) := by
      refine (natDegree_mul_le).trans ?_
      have : (2 : ℤ[X]).natDegree = 0 := by
        rw [show (2 : ℤ[X]) = C 2 by simp]; exact natDegree_C 2
      omega
    exact (natDegree_add_le _ _).trans (max_le h2 hsq)
  have hRcoeff : R.coeff (top - k) = 0 := coeff_eq_zero_of_natDegree_lt (by omega)
  have hkdeg : k ≤ (S (N - 1)).natDegree := by
    obtain ⟨M, rfl⟩ : ∃ M, N = M + 2 := ⟨N - 2, by omega⟩
    rw [show M + 2 - 1 = M + 1 by omega] at hSdeg ⊢
    nlinarith
  rw [hsplit, coeff_add, hRcoeff, add_zero, show (2 : ℤ[X]) = C 2 by simp, coeff_C_mul,
    coeff_mul_X_pow', if_pos (by omega), show top - k - N = (S (N - 1)).natDegree - k by omega,
    S_coeff_palindrome _ _ hkdeg]
  -- `S_{N−1}` and `S_K` agree up to `z^k`
  congr 1
  have hdvd : X ^ (k + 1) ∣ S K - S (N - 1) := by
    rcases le_total K (N - 1) with h | h
    · have := S_sub_S_dvd h
      rw [← neg_sub]
      exact (dvd_trans (pow_dvd_pow X (by omega)) this).neg_right
    · exact dvd_trans (pow_dvd_pow X (by omega)) (S_sub_S_dvd h)
  have := (X_pow_dvd_iff.mp hdvd) k (by omega)
  rw [coeff_sub, sub_eq_zero] at this
  exact this.symm

end Tatami

Tatami/RealVHOdd.lean

import Tatami.Basic

/-!
# Signs of `VH n` on the real line, for odd `n`

The analytic inputs to "for `n ≥ 4`, every real root of `P_n` lies in `(−1, −1/2)`", odd case
(`code/pn-polynomial/reports/real-roots.md`, §2):

* `VH_ne_zero_of_lt_neg_one_odd`: for odd `n` and `x < −1`, `VH_n(x) ≠ 0`
  (Theorem A, "Odd n = 2m+3", using Lemma 1: `S_m(−s)² ≤ S_{2m}(−s)`).
* `VH_pos_of_neg_half_le_odd`: for odd `n ≥ 5` and `−1/2 ≤ x < 0`, `VH_n(x) > 0`
  (Theorem B, "Odd n = 2m+3, m ≥ 1").

## How the proofs go

Write `n = 2m + 3` and `t_i = S_{2m+1−i}(x) S_{i−1}(x) x^{2m+2−i}` (`RealVHOdd.vhTerm x (2m+1) i`),
so that `VH_n(x) = 2 ∑_{i=1}^{m+1} t_i + S_m(x)²` (`RealVHOdd.aeval_VH_odd`). Consecutive summands
satisfy `t_{i+1} · x (1 + x^{2m+1−i}) = t_i (1 + x^i)` (`RealVHOdd.vhTerm_succ_mul`).

* `x < −1`: we work with `x` directly instead of the reciprocal form `R_n(y)`. Since `i` and
  `2m+1−i` have opposite parities, the ratio `t_{i+1}/t_i` is positive, so every `t_i` has the
  sign of `t_1 = S_{2m}(x) x^{2m+1}`, which is `(−1)^{m+1}` (`RealVHOdd.S_even_sign`).
  - `m` odd: every term of `VH_n(x)` is positive.
  - `m` even: `VH_n(x) ≤ 2 t_1 + S_m² < 0`, because `S_m(x)² ≤ |S_{2m}(x)| ≤ |t_1|`. The first
    inequality is Lemma 1 for even `m`, in `x`-form: `|1 + x^i| ≤ |1 + x^{i+m}|` factor by factor
    (`RealVHOdd.S_sq_le_abs`). The odd-`m` case of Lemma 1 is not needed.
* `−1/2 ≤ x < 0`: `t_i` has the sign `(−1)^i`, and `|t_i| ≤ |t_{i+1}|` for `2 ≤ i ≤ m`
  (`RealVHOdd.abs_vhTerm_le_succ`, from `|x| |1 + x^j| ≤ 3/4 ≤ |1 + x^i|`). Pairing from `t_3`,
  `∑ t_i ≥ t_1 + t_2 − |t_{m+1}|`. The ratio identity at `i = 1` gives `t_1 + t_2 ≥ −t_2/2`, and
  `t_2 ≤ |t_{m+1}| = |x|^{m+1} S_m² ≤ S_m²/4`, so `VH_n(x) ≥ S_m²/4 > 0`. This replaces the
  numerical bounds (`0.419`, `1.356`, `6.48`) of real-roots.md by the chain `b_1 ≤ (3/2) b_2`,
  `b_2 ≤ b_{m+1}`.

The helper lemmas live in the namespace `Tatami.RealVHOdd`. Several are copied from
`Tatami.RealVH` (the even case), which this file does not import.
-/

open Polynomial

namespace Tatami

namespace RealVHOdd

/-! ### Copied from `Tatami.RealVH` -/

lemma aeval_S (x : ℝ) (k : ℕ) : aeval x (S k) = ∏ i ∈ Finset.Icc 1 k, (1 + x ^ i) := by
  simp [S, map_prod]

lemma aeval_S_succ (x : ℝ) (k : ℕ) :
    aeval x (S (k + 1)) = aeval x (S k) * (1 + x ^ (k + 1)) := by
  simp [S_succ]

/-- The `i`-th summand of `VH (m + 2)` at `x`, without its factor `2`. -/
noncomputable def vhTerm (x : ℝ) (m i : ℕ) : ℝ :=
  aeval x (S (m - i)) * aeval x (S (i - 1)) * x ^ (m + 1 - i)

lemma one_add_pow_pos {x : ℝ} (hx : -1 < x) (k : ℕ) : 0 < 1 + x ^ k := by
  rcases le_or_gt 0 x with h0 | h0
  · positivity
  rcases Nat.eq_zero_or_pos k with rfl | hk
  · norm_num
  have h1 : |x| ^ k < 1 :=
    pow_lt_one₀ (abs_nonneg x) (by rw [abs_lt]; constructor <;> linarith) hk.ne'
  have h2 : |x ^ k| < 1 := by rwa [abs_pow]
  have := (abs_lt.mp h2).1
  linarith

lemma S_pos_of_neg_one_lt {x : ℝ} (hx : -1 < x) (k : ℕ) : 0 < aeval x (S k) := by
  rw [aeval_S]
  exact Finset.prod_pos fun i _ => one_add_pow_pos hx i

lemma sum_Ico_pairs_nonneg (f : ℕ → ℝ) (a k : ℕ)
    (h : ∀ j < k, 0 ≤ f (a + 2 * j) + f (a + 2 * j + 1)) :
    0 ≤ ∑ i ∈ Finset.Ico a (a + 2 * k), f i := by
  induction k with
  | zero => simp
  | succ k ih =>
    rw [show a + 2 * (k + 1) = a + 2 * k + 1 + 1 by ring,
      Finset.sum_Ico_succ_top (by omega), Finset.sum_Ico_succ_top (by omega)]
    have h1 := ih fun j hj => h j (by omega)
    have h2 := h k (by omega)
    linarith

lemma vhTerm_eq (x : ℝ) (a b : ℕ) :
    vhTerm x (b + 1 + 1 + a) (b + 1) = aeval x (S (a + 1)) * aeval x (S b) * x ^ (a + 2) := by
  simp only [vhTerm]
  rw [show b + 1 + 1 + a - (b + 1) = a + 1 by omega, show b + 1 - 1 = b by omega,
    show b + 1 + 1 + a + 1 - (b + 1) = a + 2 by omega]

lemma vhTerm_eq' (x : ℝ) (a b : ℕ) :
    vhTerm x (b + 1 + 1 + a) (b + 1 + 1) = aeval x (S a) * aeval x (S (b + 1)) * x ^ (a + 1) := by
  simp only [vhTerm]
  rw [show b + 1 + 1 + a - (b + 1 + 1) = a by omega, show b + 1 + 1 - 1 = b + 1 by omega,
    show b + 1 + 1 + a + 1 - (b + 1 + 1) = a + 1 by omega]

/-- Consecutive summands: `t_{i+1} · x (1 + x^{m-i}) = t_i (1 + x^i)`, for every real `x`. -/
lemma vhTerm_succ_mul (x : ℝ) {m i : ℕ} (hi : 1 ≤ i) (him : i + 1 ≤ m) :
    vhTerm x m (i + 1) * (x * (1 + x ^ (m - i))) = vhTerm x m i * (1 + x ^ i) := by
  obtain ⟨a, rfl⟩ : ∃ a, m = i + 1 + a := ⟨m - i - 1, by omega⟩
  obtain ⟨b, rfl⟩ : ∃ b, i = b + 1 := ⟨i - 1, by omega⟩
  rw [vhTerm_eq', vhTerm_eq, show b + 1 + 1 + a - (b + 1) = a + 1 by omega,
    aeval_S_succ, aeval_S_succ]
  ring

lemma one_add_pow_neg_of_odd {x : ℝ} (hx : x < -1) {k : ℕ} (hk : Odd k) : 1 + x ^ k < 0 := by
  have h1 : -x ≤ (-x) ^ k := le_self_pow₀ (by linarith) hk.pos.ne'
  have h2 : x ^ k = -(-x) ^ k := by rw [hk.neg_pow, neg_neg]
  linarith

lemma one_add_pow_pos_of_even (x : ℝ) {k : ℕ} (hk : Even k) : 0 < 1 + x ^ k := by
  have := hk.pow_nonneg x
  linarith

lemma one_add_pow_ne_zero_of_lt {x : ℝ} (hx : x < -1) (k : ℕ) : 1 + x ^ k ≠ 0 := by
  rcases Nat.even_or_odd k with hk | hk
  · exact (one_add_pow_pos_of_even x hk).ne'
  · exact (one_add_pow_neg_of_odd hx hk).ne

lemma S_ne_zero_of_lt {x : ℝ} (hx : x < -1) (k : ℕ) : aeval x (S k) ≠ 0 := by
  rw [aeval_S]
  exact Finset.prod_ne_zero_iff.mpr fun i _ => one_add_pow_ne_zero_of_lt hx i

/-! ### Odd `n = 2m + 3` -/

lemma aeval_VH_odd (x : ℝ) (m : ℕ) :
    aeval x (VH (2 * m + 3)) =
      2 * ∑ i ∈ Finset.Icc 1 (m + 1), vhTerm x (2 * m + 1) i + aeval x (S m) ^ 2 := by
  rw [show 2 * m + 3 = 2 * m + 1 + 2 by ring, VH_add_two, show (2 * m + 1 + 1) / 2 = m + 1 by omega,
    show (2 * m + 1) / 2 = m by omega]
  simp [map_sum, map_ofNat, vhTerm]

lemma vhTerm_one (x : ℝ) (m : ℕ) :
    vhTerm x (2 * m + 1) 1 = aeval x (S (2 * m)) * x ^ (2 * m + 1) := by
  simp only [vhTerm]
  rw [show 2 * m + 1 - 1 = 2 * m by omega, show (1 : ℕ) - 1 = 0 from rfl, S_zero, map_one, mul_one,
    show 2 * m + 1 + 1 - 1 = 2 * m + 1 by omega]

lemma vhTerm_mid (x : ℝ) (m : ℕ) :
    vhTerm x (2 * m + 1) (m + 1) = aeval x (S m) ^ 2 * x ^ (m + 1) := by
  simp only [vhTerm]
  rw [show 2 * m + 1 - (m + 1) = m by omega, show m + 1 - 1 = m by omega,
    show 2 * m + 1 + 1 - (m + 1) = m + 1 by omega]
  ring

/-! ### The half-line `x < −1` -/

/-- For `x < −1`, `i ≤ j` and `i ≡ j (mod 2)`: `|1 + x^i| ≤ |1 + x^j|`. -/
lemma abs_one_add_pow_le {x : ℝ} (hx : x < -1) {i j : ℕ} (hij : i ≤ j) (hpar : Even (i + j)) :
    |1 + x ^ i| ≤ |1 + x ^ j| := by
  obtain ⟨r, rfl⟩ : ∃ r, x = -r := ⟨-x, by ring⟩
  have hr : 1 ≤ r := by linarith
  have hpow : r ^ i ≤ r ^ j := pow_le_pow_right₀ hr hij
  have hri : 0 ≤ r ^ i := pow_nonneg (by linarith) i
  rcases Nat.even_or_odd i with hie | hio
  · have hje : Even j := (Nat.even_add.mp hpar).mp hie
    rw [hie.neg_pow, hje.neg_pow, abs_of_nonneg (by linarith), abs_of_nonneg (by linarith)]
    linarith
  · have hjo : Odd j := by
      rw [← Nat.not_even_iff_odd] at hio ⊢
      exact fun h => hio ((Nat.even_add.mp hpar).mpr h)
    have hi1 : 1 ≤ r ^ i := one_le_pow₀ hr
    rw [hio.neg_pow, hjo.neg_pow, abs_of_nonpos (by linarith), abs_of_nonpos (by linarith)]
    linarith

/-- Lemma 1 of real-roots.md for even `m = 2p`, in `x`-form: for `x < −1`,
`S_{2p}(x)² ≤ |S_{4p}(x)|`. -/
lemma S_sq_le_abs {x : ℝ} (hx : x < -1) (p : ℕ) :
    aeval x (S (2 * p)) ^ 2 ≤ |aeval x (S (4 * p))| := by
  induction p with
  | zero => simp
  | succ p ih =>
    rw [show 2 * (p + 1) = 2 * p + 1 + 1 by ring, show 4 * (p + 1) = 4 * p + 1 + 1 + 1 + 1 by ring]
    simp only [aeval_S_succ, abs_mul]
    have h1 := abs_one_add_pow_le hx (i := 2 * p + 1) (j := 4 * p + 1) (by omega) ⟨3 * p + 1, by ring⟩
    have h2 := abs_one_add_pow_le hx (i := 2 * p + 1 + 1) (j := 4 * p + 1 + 1) (by omega)
      ⟨3 * p + 2, by ring⟩
    have h3 := abs_one_add_pow_le hx (i := 2 * p + 1) (j := 4 * p + 1 + 1 + 1) (by omega)
      ⟨3 * p + 2, by ring⟩
    have h4 := abs_one_add_pow_le hx (i := 2 * p + 1 + 1) (j := 4 * p + 1 + 1 + 1 + 1) (by omega)
      ⟨3 * p + 3, by ring⟩
    calc (aeval x (S (2 * p)) * (1 + x ^ (2 * p + 1)) * (1 + x ^ (2 * p + 1 + 1))) ^ 2
        = aeval x (S (2 * p)) ^ 2 * (|1 + x ^ (2 * p + 1)| * |1 + x ^ (2 * p + 1)|) *
            (|1 + x ^ (2 * p + 1 + 1)| * |1 + x ^ (2 * p + 1 + 1)|) := by
          rw [abs_mul_abs_self, abs_mul_abs_self]; ring
      _ = aeval x (S (2 * p)) ^ 2 * |1 + x ^ (2 * p + 1)| * |1 + x ^ (2 * p + 1 + 1)| *
            |1 + x ^ (2 * p + 1)| * |1 + x ^ (2 * p + 1 + 1)| := by ring
      _ ≤ |aeval x (S (4 * p))| * |1 + x ^ (4 * p + 1)| * |1 + x ^ (4 * p + 1 + 1)| *
            |1 + x ^ (4 * p + 1 + 1 + 1)| * |1 + x ^ (4 * p + 1 + 1 + 1 + 1)| := by
          gcongr

/-- For `x < −1`, `S_{2m}(x)` has the sign `(−1)^m`. -/
lemma S_even_sign {x : ℝ} (hx : x < -1) (m : ℕ) : 0 < (-1) ^ m * aeval x (S (2 * m)) := by
  induction m with
  | zero => simp
  | succ m ih =>
    rw [show 2 * (m + 1) = 2 * m + 1 + 1 by ring, aeval_S_succ, aeval_S_succ]
    have h1 := one_add_pow_neg_of_odd hx (k := 2 * m + 1) ⟨m, rfl⟩
    have h2 := one_add_pow_pos_of_even x (k := 2 * m + 1 + 1) ⟨m + 1, by ring⟩
    have hpos : 0 < ((-1) ^ m * aeval x (S (2 * m))) * (-(1 + x ^ (2 * m + 1))) *
        (1 + x ^ (2 * m + 1 + 1)) := mul_pos (mul_pos ih (by linarith)) h2
    calc 0 < _ := hpos
      _ = _ := by ring

/-- For `x < −1` and `1 ≤ i ≤ m + 1`, every summand `t_i` of `VH (2m + 3)` has the sign
`(−1)^{m+1}`. -/
lemma vhTerm_sign_of_lt {x : ℝ} (hx : x < -1) (m : ℕ) :
    ∀ i, 1 ≤ i → i ≤ m + 1 → 0 < (-1) ^ (m + 1) * vhTerm x (2 * m + 1) i := by
  have hx0 : x < 0 := by linarith
  intro i hi1 hi2
  induction i, hi1 using Nat.le_induction with
  | base =>
    rw [vhTerm_one]
    have hs := S_even_sign hx m
    have hp : x ^ (2 * m + 1) < 0 := Odd.pow_neg ⟨m, rfl⟩ hx0
    calc 0 < ((-1) ^ m * aeval x (S (2 * m))) * (-x ^ (2 * m + 1)) := mul_pos hs (by linarith)
      _ = _ := by ring
  | succ i hi ih =>
    have hpos := ih (by omega)
    have hid := vhTerm_succ_mul x (m := 2 * m + 1) hi (by omega)
    have hND : 0 < (1 + x ^ i) / (x * (1 + x ^ (2 * m + 1 - i))) := by
      rcases Nat.even_or_odd i with hie | hio
      · have hj : Odd (2 * m + 1 - i) := by
          obtain ⟨c, hc⟩ := hie; exact ⟨m - c, by omega⟩
        exact div_pos (one_add_pow_pos_of_even x hie)
          (mul_pos_of_neg_of_neg hx0 (one_add_pow_neg_of_odd hx hj))
      · have hj : Even (2 * m + 1 - i) := by
          obtain ⟨c, hc⟩ := hio; exact ⟨m - c, by omega⟩
        exact div_pos_of_neg_of_neg (one_add_pow_neg_of_odd hx hio)
          (mul_neg_of_neg_of_pos hx0 (one_add_pow_pos_of_even x hj))
    have hd : x * (1 + x ^ (2 * m + 1 - i)) ≠ 0 :=
      mul_ne_zero hx0.ne (one_add_pow_ne_zero_of_lt hx _)
    rw [eq_div_of_mul_eq hd hid]
    calc 0 < (-1) ^ (m + 1) * vhTerm x (2 * m + 1) i *
          ((1 + x ^ i) / (x * (1 + x ^ (2 * m + 1 - i)))) := mul_pos hpos hND
      _ = _ := by ring

/-! ### The interval `−1/2 ≤ x < 0` -/

lemma vhTerm_neg_of_odd {x : ℝ} (hx1 : -1 < x) (hx0 : x < 0) {m i : ℕ} (hi : Odd i)
    (him : i ≤ 2 * m + 1) : vhTerm x (2 * m + 1) i < 0 := by
  simp only [vhTerm]
  have hp : x ^ (2 * m + 1 + 1 - i) < 0 :=
    Odd.pow_neg (by obtain ⟨c, hc⟩ := hi; exact ⟨m - c, by omega⟩) hx0
  exact mul_neg_of_pos_of_neg (mul_pos (S_pos_of_neg_one_lt hx1 _) (S_pos_of_neg_one_lt hx1 _)) hp

lemma vhTerm_pos_of_even {x : ℝ} (hx1 : -1 < x) (hx0 : x < 0) {m i : ℕ} (hi : Even i)
    (him : i ≤ 2 * m + 1) : 0 < vhTerm x (2 * m + 1) i := by
  simp only [vhTerm]
  have hp : 0 < x ^ (2 * m + 1 + 1 - i) :=
    Even.pow_pos (by obtain ⟨c, hc⟩ := hi; exact ⟨m + 1 - c, by omega⟩) hx0.ne
  exact mul_pos (mul_pos (S_pos_of_neg_one_lt hx1 _) (S_pos_of_neg_one_lt hx1 _)) hp

/-- For `−1/2 ≤ x < 0` and `2 ≤ i ≤ 2m`: `|t_i| ≤ |t_{i+1}|` (real-roots.md, Theorem B:
`b_i / b_{i+1} < 1` for `2 ≤ i < I`). -/
lemma abs_vhTerm_le_succ {x : ℝ} (hx1 : -1 / 2 ≤ x) (hx0 : x < 0) {m i : ℕ} (hi : 2 ≤ i)
    (him : i ≤ 2 * m) : |vhTerm x (2 * m + 1) i| ≤ |vhTerm x (2 * m + 1) (i + 1)| := by
  have hid := vhTerm_succ_mul x (m := 2 * m + 1) (i := i) (by omega) (by omega)
  have hax : |x| ≤ 1 / 2 := by rw [abs_of_neg hx0]; linarith
  have hN : 3 / 4 ≤ |1 + x ^ i| := by
    have h : |x ^ i| ≤ 1 / 4 := by
      rw [abs_pow]
      calc |x| ^ i ≤ |x| ^ 2 := pow_le_pow_of_le_one (abs_nonneg x) (by linarith) hi
        _ ≤ (1 / 2) ^ 2 := pow_le_pow_left₀ (abs_nonneg x) hax 2
        _ = 1 / 4 := by norm_num
    have h' := abs_le.mp h
    rw [abs_of_pos (by linarith)]
    linarith
  have hD : |x| * |1 + x ^ (2 * m + 1 - i)| ≤ 3 / 4 := by
    have h : |x ^ (2 * m + 1 - i)| ≤ 1 / 2 := by
      rw [abs_pow]
      calc |x| ^ (2 * m + 1 - i) ≤ |x| := pow_le_of_le_one (abs_nonneg x) (by linarith) (by omega)
        _ ≤ 1 / 2 := hax
    have h' := abs_le.mp h
    have h2 : |1 + x ^ (2 * m + 1 - i)| ≤ 3 / 2 := by
      rw [abs_of_pos (by linarith)]
      linarith
    calc |x| * |1 + x ^ (2 * m + 1 - i)| ≤ (1 / 2) * (3 / 2) :=
          mul_le_mul hax h2 (abs_nonneg _) (by norm_num)
      _ = 3 / 4 := by norm_num
  have habs := congrArg abs hid
  simp only [abs_mul] at habs
  refine le_of_mul_le_mul_right ?_ (show 0 < |1 + x ^ i| by linarith)
  calc |vhTerm x (2 * m + 1) i| * |1 + x ^ i|
      = |vhTerm x (2 * m + 1) (i + 1)| * (|x| * |1 + x ^ (2 * m + 1 - i)|) := habs.symm
    _ ≤ |vhTerm x (2 * m + 1) (i + 1)| * (3 / 4) := mul_le_mul_of_nonneg_left hD (abs_nonneg _)
    _ ≤ |vhTerm x (2 * m + 1) (i + 1)| * |1 + x ^ i| := mul_le_mul_of_nonneg_left hN (abs_nonneg _)

/-- For `−1/2 ≤ x < 0` and `m ≥ 1`: `t_1 + t_2 ≥ −t_2/2` (real-roots.md, Theorem B: this
replaces the bound on `(b_1 − b_2)⁺`). -/
lemma vhTerm_one_add_two {x : ℝ} (hx1 : -1 / 2 ≤ x) (hx0 : x < 0) {m : ℕ} (hm : 1 ≤ m) :
    -(vhTerm x (2 * m + 1) 2) / 2 ≤ vhTerm x (2 * m + 1) 1 + vhTerm x (2 * m + 1) 2 := by
  have hid := vhTerm_succ_mul x (m := 2 * m + 1) (i := 1) le_rfl (by omega)
  rw [show 2 * m + 1 - 1 = 2 * m by omega, pow_one, show (1 : ℕ) + 1 = 2 from rfl] at hid
  have ht2 : 0 < vhTerm x (2 * m + 1) 2 :=
    vhTerm_pos_of_even (by linarith) hx0 ⟨1, rfl⟩ (by omega)
  have hy0 : 0 ≤ x ^ (2 * m) := Even.pow_nonneg ⟨m, by ring⟩ x
  have hy1 : x ^ (2 * m) ≤ 1 / 4 := by
    have hsq : x ^ 2 ≤ 1 / 4 := by nlinarith
    rw [pow_mul]
    calc (x ^ 2) ^ m ≤ x ^ 2 := pow_le_of_le_one (sq_nonneg x) (by linarith) (by omega)
      _ ≤ 1 / 4 := hsq
  have h1x : 0 < 1 + x := by linarith
  have key : 0 ≤ x * (1 + x ^ (2 * m)) + 3 / 2 * (1 + x) := by
    nlinarith [mul_nonneg (show 0 ≤ x + 1 / 2 by linarith) hy0]
  have hprod : 0 ≤ (vhTerm x (2 * m + 1) 1 + vhTerm x (2 * m + 1) 2 + vhTerm x (2 * m + 1) 2 / 2) *
      (1 + x) := by
    have e : (vhTerm x (2 * m + 1) 1 + vhTerm x (2 * m + 1) 2 + vhTerm x (2 * m + 1) 2 / 2) *
        (1 + x) = vhTerm x (2 * m + 1) 2 * (x * (1 + x ^ (2 * m)) + 3 / 2 * (1 + x)) := by
      linear_combination -hid
    rw [e]
    exact mul_nonneg ht2.le key
  have := nonneg_of_mul_nonneg_left hprod h1x
  linarith

end RealVHOdd

open RealVHOdd

/-- real-roots.md Theorem A (odd case): for odd `n` and `x < −1`, `VH_n(x) ≠ 0`. -/
theorem VH_ne_zero_of_lt_neg_one_odd {n : ℕ} (hn : Odd n) {x : ℝ} (hx : x < -1) :
    aeval x (VH n) ≠ 0 := by
  obtain ⟨k, rfl⟩ := hn
  rcases k with _ | m
  · simp [VH]
  rw [show 2 * (m + 1) + 1 = 2 * m + 3 by ring, aeval_VH_odd]
  have hall := vhTerm_sign_of_lt hx m
  rcases Nat.even_or_odd m with hm | hm
  · -- `m` even: every `t_i < 0`, and `S_m² + 2 t_1 < 0`
    have hσ : (-1 : ℝ) ^ (m + 1) = -1 := by rw [pow_succ, hm.neg_one_pow]; ring
    have hneg : ∀ i ∈ Finset.Icc 1 (m + 1), vhTerm x (2 * m + 1) i < 0 := by
      intro i hi
      rw [Finset.mem_Icc] at hi
      have := hall i hi.1 hi.2
      rw [hσ] at this
      linarith
    have hsum : ∑ i ∈ Finset.Icc 1 (m + 1), vhTerm x (2 * m + 1) i ≤ vhTerm x (2 * m + 1) 1 := by
      rw [show Finset.Icc 1 (m + 1) = Finset.Ico 1 (m + 2) by ext i; simp; omega,
        Finset.sum_eq_sum_Ico_succ_bot (by omega)]
      have : ∑ i ∈ Finset.Ico (1 + 1) (m + 2), vhTerm x (2 * m + 1) i ≤ 0 :=
        Finset.sum_nonpos fun i hi => (hneg i (by simp at hi ⊢; omega)).le
      linarith
    obtain ⟨p, hp⟩ := hm
    have hsq := S_sq_le_abs hx p
    rw [show 2 * p = m by omega, show 4 * p = 2 * m by omega] at hsq
    have habs : |aeval x (S (2 * m))| ≤ |vhTerm x (2 * m + 1) 1| := by
      rw [vhTerm_one, abs_mul]
      have h1 : 1 ≤ |x ^ (2 * m + 1)| := by
        rw [abs_pow]
        exact one_le_pow₀ (by rw [abs_of_neg (by linarith : x < 0)]; linarith)
      nlinarith [abs_nonneg (aeval x (S (2 * m)))]
    have hS2 : 0 < |aeval x (S (2 * m))| := abs_pos.mpr (S_ne_zero_of_lt hx _)
    rw [abs_of_neg (hneg 1 (by simp))] at habs
    have : 2 * ∑ i ∈ Finset.Icc 1 (m + 1), vhTerm x (2 * m + 1) i + aeval x (S m) ^ 2 < 0 := by
      linarith
    exact this.ne
  · -- `m` odd: every term is positive
    have hσ : (-1 : ℝ) ^ (m + 1) = 1 := by rw [pow_succ, hm.neg_one_pow]; ring
    have hpos : 0 < ∑ i ∈ Finset.Icc 1 (m + 1), vhTerm x (2 * m + 1) i := by
      apply Finset.sum_pos
      · intro i hi
        rw [Finset.mem_Icc] at hi
        have := hall i hi.1 hi.2
        rwa [hσ, one_mul] at this
      · exact ⟨1, by simp⟩
    have := sq_nonneg (aeval x (S m))
    have : 0 < 2 * ∑ i ∈ Finset.Icc 1 (m + 1), vhTerm x (2 * m + 1) i + aeval x (S m) ^ 2 := by
      linarith
    exact this.ne'

/-- real-roots.md Theorem B (odd case): for odd `n ≥ 5` and `−1/2 ≤ x < 0`, `VH_n(x) > 0`. -/
theorem VH_pos_of_neg_half_le_odd {n : ℕ} (hn : Odd n) (h5 : 5 ≤ n) {x : ℝ}
    (hx1 : -1 / 2 ≤ x) (hx0 : x < 0) : 0 < aeval x (VH n) := by
  obtain ⟨k, rfl⟩ := hn
  obtain ⟨m, rfl⟩ : ∃ m, k = m + 1 := ⟨k - 1, by omega⟩
  have hm : 1 ≤ m := by omega
  rw [show 2 * (m + 1) + 1 = 2 * m + 3 by ring, aeval_VH_odd]
  have hx1' : -1 < x := by linarith
  have hS : 0 < aeval x (S m) ^ 2 := pow_pos (S_pos_of_neg_one_lt hx1' m) 2
  -- pairs `t_i + t_{i+1}` with `i ≥ 3` odd are nonnegative
  have hpair : ∀ j, 1 + 1 + 1 + 2 * j + 1 ≤ m + 1 →
      0 ≤ vhTerm x (2 * m + 1) (1 + 1 + 1 + 2 * j) + vhTerm x (2 * m + 1) (1 + 1 + 1 + 2 * j + 1) := by
    intro j hj
    have hneg := vhTerm_neg_of_odd hx1' hx0 (m := m) (i := 1 + 1 + 1 + 2 * j) ⟨j + 1, by ring⟩
      (by omega)
    have hpos := vhTerm_pos_of_even hx1' hx0 (m := m) (i := 1 + 1 + 1 + 2 * j + 1)
      ⟨j + 2, by ring⟩ (by omega)
    have hle := abs_vhTerm_le_succ hx1 hx0 (m := m) (i := 1 + 1 + 1 + 2 * j) (by omega) (by omega)
    rw [abs_of_neg hneg, abs_of_pos hpos] at hle
    linarith
  -- `∑ t_i ≥ t_1 + t_2 − |t_{m+1}|`
  have hsum : vhTerm x (2 * m + 1) 1 + vhTerm x (2 * m + 1) 2 - |vhTerm x (2 * m + 1) (m + 1)| ≤
      ∑ i ∈ Finset.Icc 1 (m + 1), vhTerm x (2 * m + 1) i := by
    rcases Nat.even_or_odd m with ⟨k, hk⟩ | ⟨k, hk⟩
    · -- `m = 2k + 2`: `t_1 + t_2 + pairs + t_{m+1}`
      obtain ⟨k, rfl⟩ : ∃ k', k = k' + 1 := ⟨k - 1, by omega⟩
      have e : ∑ i ∈ Finset.Icc 1 (m + 1), vhTerm x (2 * m + 1) i = vhTerm x (2 * m + 1) 1
          + (vhTerm x (2 * m + 1) (1 + 1) + ∑ i ∈ Finset.Ico (1 + 1 + 1) (1 + 1 + 1 + 2 * k),
            vhTerm x (2 * m + 1) i) + vhTerm x (2 * m + 1) (1 + 1 + 1 + 2 * k) := by
        rw [show Finset.Icc 1 (m + 1) = Finset.Ico 1 (1 + 1 + 1 + 2 * k + 1) by ext i; simp; omega,
          Finset.sum_Ico_succ_top (by omega), Finset.sum_eq_sum_Ico_succ_bot (by omega),
          Finset.sum_eq_sum_Ico_succ_bot (by omega)]
      have htop : vhTerm x (2 * m + 1) (1 + 1 + 1 + 2 * k) = vhTerm x (2 * m + 1) (m + 1) := by
        congr 1; omega
      have hP := sum_Ico_pairs_nonneg (vhTerm x (2 * m + 1)) (1 + 1 + 1) k fun j hj =>
        hpair j (by omega)
      have := neg_abs_le (vhTerm x (2 * m + 1) (m + 1))
      rw [e, htop]
      linarith
    · -- `m = 2k + 1`: `t_1 + t_2 + pairs`
      have e : ∑ i ∈ Finset.Icc 1 (m + 1), vhTerm x (2 * m + 1) i = vhTerm x (2 * m + 1) 1
          + (vhTerm x (2 * m + 1) (1 + 1) + ∑ i ∈ Finset.Ico (1 + 1 + 1) (1 + 1 + 1 + 2 * k),
            vhTerm x (2 * m + 1) i) := by
        rw [show Finset.Icc 1 (m + 1) = Finset.Ico 1 (1 + 1 + 1 + 2 * k) by ext i; simp; omega,
          Finset.sum_eq_sum_Ico_succ_bot (by omega), Finset.sum_eq_sum_Ico_succ_bot (by omega)]
      have hP := sum_Ico_pairs_nonneg (vhTerm x (2 * m + 1)) (1 + 1 + 1) k fun j hj =>
        hpair j (by omega)
      have := abs_nonneg (vhTerm x (2 * m + 1) (m + 1))
      rw [e]
      linarith
  -- `t_1 + t_2 ≥ −t_2/2`, `t_2 ≤ |t_{m+1}| ≤ S_m²/4`
  have h12 := vhTerm_one_add_two hx1 hx0 hm
  have hchain : ∀ i, 2 ≤ i → i ≤ m + 1 →
      |vhTerm x (2 * m + 1) 2| ≤ |vhTerm x (2 * m + 1) i| := by
    intro i hi2 him
    induction i, hi2 using Nat.le_induction with
    | base => exact le_rfl
    | succ i hi ih => exact (ih (by omega)).trans (abs_vhTerm_le_succ hx1 hx0 hi (by omega))
  have h2m := hchain (m + 1) (by omega) le_rfl
  rw [abs_of_pos (vhTerm_pos_of_even hx1' hx0 ⟨1, rfl⟩ (by omega))] at h2m
  have hmid : |vhTerm x (2 * m + 1) (m + 1)| ≤ aeval x (S m) ^ 2 / 4 := by
    have hax : |x| ≤ 1 / 2 := by rw [abs_of_neg hx0]; linarith
    have hxm : |x ^ (m + 1)| ≤ 1 / 4 := by
      rw [abs_pow]
      calc |x| ^ (m + 1) ≤ |x| ^ 2 := pow_le_pow_of_le_one (abs_nonneg x) (by linarith) (by omega)
        _ ≤ (1 / 2) ^ 2 := pow_le_pow_left₀ (abs_nonneg x) hax 2
        _ = 1 / 4 := by norm_num
    rw [vhTerm_mid, abs_mul, abs_of_nonneg (sq_nonneg _)]
    nlinarith
  linarith

end Tatami

Tatami/RealRoots.lean

import Tatami.ConjC
import Tatami.RealVHOdd
import Tatami.OddRoot

/-!
# Where the real roots of `P_n` are

Source: `code/pn-polynomial/reports/real-roots.md`, Theorems A and B and their Corollary.

* `real_root_mem`: for `n ≥ 4`, every real root of `P_n` lies in `(−1, −1/2)`.
* `odd_has_real_root_in`: for odd `n ≥ 5`, `P_n` has a real root in `(−1, −1/2)`.

Together with `conj_c` this is everything in `conj:Pcon`(b) except **uniqueness** of the root
and the **monotonicity** of `α_n`, which remain open. (For `n = 3`, `P_3 = 1 + 2z` has its root
exactly at `−1/2`.)
-/

open Polynomial

namespace Tatami

lemma aeval_D_pos {x : ℝ} (hx : -1 < x) (n : ℕ) : 0 < aeval x (D n) := by
  rw [D, map_prod]
  exact Finset.prod_pos fun j _ => RealVH.S_pos_of_neg_one_lt hx _

/-- For `n ≥ 4`, every real root of `P_n` lies in the open interval `(−1, −1/2)`. -/
theorem real_root_mem {n : ℕ} (hn : 4 ≤ n) {x : ℝ} (hx : aeval x (P n) = 0) :
    -1 < x ∧ x < -1 / 2 := by
  rcases Nat.even_or_odd n with he | ho
  · exact absurd hx (conj_c he x)
  have h5 : 5 ≤ n := by rcases ho with ⟨r, hr⟩; omega
  have hVH : aeval x (VH n) = 0 := by rw [VH_eq_D_mul_P, map_mul, hx, mul_zero]
  refine ⟨?_, ?_⟩
  · by_contra h
    rcases (not_lt.mp h).lt_or_eq with hlt | heq
    · exact VH_ne_zero_of_lt_neg_one_odd ho hlt hVH
    · subst heq
      obtain ⟨k, rfl⟩ : ∃ k, n = 2 * k + 3 := by rcases ho with ⟨r, hr⟩; exact ⟨r - 1, by omega⟩
      rw [aeval_neg_one, P_eval_neg_one_odd] at hx
      have hpos : (0 : ℤ) < ∑ i ∈ Finset.range (k + 1), 2 ^ (k - i) * (Nat.centralBinom i : ℤ) :=
        Finset.sum_pos (fun i _ => by have := Nat.centralBinom_pos i; positivity) ⟨0, by simp⟩
      have : ((-∑ i ∈ Finset.range (k + 1), 2 ^ (k - i) * (Nat.centralBinom i : ℤ) : ℤ) : ℝ) ≠ 0 := by
        exact_mod_cast (neg_neg_of_pos hpos).ne
      exact this hx
  · by_contra h
    have hx' : -1 / 2 ≤ x := not_lt.mp h
    rcases lt_or_ge x 0 with h0 | h0
    · exact (VH_pos_of_neg_half_le_odd ho h5 hx' h0).ne' hVH
    · exact (VH_pos_of_nonneg n h0).ne' hVH

/-- For odd `n ≥ 5`, `P_n` has a real root in `(−1, −1/2)`. -/
theorem odd_has_real_root_in {n : ℕ} (ho : Odd n) (h5 : 5 ≤ n) :
    ∃ x : ℝ, -1 < x ∧ x < -1 / 2 ∧ aeval x (P n) = 0 := by
  obtain ⟨x, hx1, _, hx⟩ : ∃ x : ℝ, -1 < x ∧ x < 0 ∧ aeval x (P n) = 0 := by
    obtain ⟨k, rfl⟩ : ∃ k, n = 2 * k + 3 := by rcases ho with ⟨r, hr⟩; exact ⟨r - 1, by omega⟩
    exact odd_has_real_root k
  exact ⟨x, (real_root_mem (by omega) hx).1, (real_root_mem (by omega) hx).2, hx⟩

end Tatami

Audit/Axioms.lean

import Tatami

/-! Prints the axioms each main theorem depends on. Anything beyond `propext`,
`Classical.choice` and `Quot.sound` (Lean's standard axioms) would show a gap: a `sorry`
appears as `sorryAx`. -/

open Tatami
#print axioms D_dvd_VH
#print axioms VH_eq_D_mul_P
#print axioms conjA_as_printed_false
#print axioms conjA_literal_false
#print axioms conj_a_corrected
#print axioms conj_vhcon_a
#print axioms conj_vhcon_b
#print axioms P_eval_neg_one_even
#print axioms P_eval_neg_one_odd
#print axioms conj_e
#print axioms conj_c
#print axioms odd_has_real_root
#print axioms S_sq_eq_D_mul_B
#print axioms P_mod_two
#print axioms P_leadingCoeff
#print axioms natDegree_P_eq
#print axioms real_root_mem
#print axioms odd_has_real_root_in
#print axioms VH_ne_zero_of_lt_neg_one_odd
#print axioms VH_pos_of_neg_half_le_odd

lake-manifest.json

{"version": "1.3.0",
 "packagesDir": ".lake/packages",
 "packages":
 [{"url": "https://github.com/leanprover-community/mathlib4",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "fc4f59e577feb1a4956671bc04ac18aaaf8cd842",
   "name": "mathlib",
   "manifestFile": "lake-manifest.json",
   "inputRev": "v4.36.0",
   "inherited": false,
   "configFile": "lakefile.lean"},
  {"url": "https://github.com/leanprover-community/plausible",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "054b228ce0ac61d767c6e71ba73a24728f191b72",
   "name": "plausible",
   "manifestFile": "lake-manifest.json",
   "inputRev": "main",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover-community/LeanSearchClient",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "4fb58e749ef3fc0395fe274f6cab70c2ea85f1c9",
   "name": "LeanSearchClient",
   "manifestFile": "lake-manifest.json",
   "inputRev": "main",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover-community/import-graph",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "8370c640afcd1cc3fb1cb9bf1560cc66eafee5e0",
   "name": "importGraph",
   "manifestFile": "lake-manifest.json",
   "inputRev": "main",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover-community/ProofWidgets4",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "2f0f3520daeac2d9310ff25b7a877a0e5cdaa203",
   "name": "proofwidgets",
   "manifestFile": "lake-manifest.json",
   "inputRev": "main",
   "inherited": true,
   "configFile": "lakefile.lean"},
  {"url": "https://github.com/leanprover-community/aesop",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "66ff77cf0c4c898799e4b94d14db55f7a02d0c5a",
   "name": "aesop",
   "manifestFile": "lake-manifest.json",
   "inputRev": "master",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover-community/quote4",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "ff055a2fcc835935e0ae607339b455a5f5896b4b",
   "name": "Qq",
   "manifestFile": "lake-manifest.json",
   "inputRev": "master",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover-community/batteries",
   "type": "git",
   "subDir": null,
   "scope": "leanprover-community",
   "rev": "b4b25a3886a16daacaf6615c2eea7170a6e8f458",
   "name": "batteries",
   "manifestFile": "lake-manifest.json",
   "inputRev": "main",
   "inherited": true,
   "configFile": "lakefile.toml"},
  {"url": "https://github.com/leanprover/lean4-cli",
   "type": "git",
   "subDir": null,
   "scope": "leanprover",
   "rev": "50810f72af7618bee62fe9800dc26e806c188703",
   "name": "Cli",
   "manifestFile": "lake-manifest.json",
   "inputRev": "v4.36.0",
   "inherited": true,
   "configFile": "lakefile.toml"}],
 "name": "tatami",
 "lakeDir": ".lake",
 "fixedToolchain": false}

README.md

# Lean formalization of the P_n(z) results

Machine-checked proofs, in [Lean 4](https://lean-lang.org) with
[Mathlib](https://github.com/leanprover-community/mathlib4), of results about the
mysterious polynomial P_n(z) = VH_n(z) / D_n(z) (thesis Ch. 4, `conj:Pcon`). The
September 2026 agent work in [`../pn-polynomial/`](../pn-polynomial/) claimed these proofs.
Here Lean checks them. Started 2026-11-23.

## What Lean guarantees, and what it doesn't

Every theorem below compiles with no `sorry`. `Audit/Axioms.lean` prints the axioms each
main theorem depends on, and all depend only on Lean's three standard axioms (`propext`,
`Classical.choice`, `Quot.sound`). So each proof is correct **for its statement as
written in Lean**.

What a reader still has to check:

1. **The definitions**: `Tatami/Basic.lean`, four short definitions (`S`, `VH`, `D`, `P`)
   transcribed from the thesis. Two Lean conventions matter there: `n - 2` on naturals
   is truncated at 0, and `a / b` on naturals is floor division.
2. **The theorem statements**: each is a few lines, with a docstring saying what it is.
3. **Scope**: everything is about the polynomial VH_n *as given by the formula*
   (`thm:vdominoes`), not about tatami coverings. The thesis's link from coverings to
   that formula is not formalized. The thesis flags its own unproved premise there
   (`chapter_vdimers.tex`, about lines 295–298).

## What is proved

| Claim | Lean theorem | File | Status before |
|---|---|---|---|
| `thm:cyclotomic`: D_n divides VH_n, so VH_n = D_n · P_n | `D_dvd_VH`, `VH_eq_D_mul_P` | `Cyclotomic.lean` | thesis theorem |
| `conj:Pcon`(a) as printed is **false**: n=4, k=1, j=2 and the literal reading n=6, k=2, j=4 | `conjA_as_printed_false`, `conjA_literal_false` | `ConjA.lean` | agent-refuted |
| `conj:Pcon`(a) corrected (j < 2^k) | `conj_a_corrected` | `LowCoefficients.lean` | agent-proved |
| `conj:vhcon`(a) (Lemma A): VH_{N+2} ≡ S_N² mod z^{N+1} | `VH_congr_S_sq`, `conj_vhcon_a` | `LowCoefficients.lean` | agent-proved |
| `conj:vhcon`(b): top coefficients of VH_n are 2·(distinct partitions) | `conj_vhcon_b` | `TopCoefficients.lean` | agent-proved |
| `conj:Pcon`(e), even n: P_n(−1) = C(n−2, (n−2)/2) | `P_eval_neg_one_even` | `AtMinusOne.lean` | agent-proved |
| `conj:Pcon`(e), odd n: P_{2k+3}(−1) = −Σ 2^{k−i} C(2i,i) | `P_eval_neg_one_odd`, `conj_e` | `AtMinusOneOdd.lean` | agent-proved |
| `conj:Pcon`(c): for even n, P_n has no real root | `conj_c` | `ConjC.lean` (+ `RealVH.lean`, `ConjCCore.lean`) | agent-proved |
| `conj:Pcon`(b), existence: odd n ≥ 5 has a real root in (−1, −1/2) | `odd_has_real_root_in` (and `odd_has_real_root`) | `RealRoots.lean`, `OddRoot.lean` | agent-proved |
| For n ≥ 4, every real root of P_n lies in (−1, −1/2) | `real_root_mem` | `RealRoots.lean` (+ `RealVHOdd.lean`) | agent-proved (real-roots Thms A, B) |
| S_m² = D_n · ∏_{j∈J} Φ_{2j}, and P_n ≡ ∏_{j∈J} Φ_{2j} (mod 2) | `S_sq_eq_D_mul_B`, `P_mod_two` | `ModTwo.lean` | agent-proved (irreducibility Thm 1(a,b)) |
| P_n has leading coefficient 2 (n ≥ 3); deg P_n = deg B_n + ⌊(n−1)/2⌋² | `P_leadingCoeff`, `natDegree_P_eq` | `TopDegree.lean` | agent-proved (irreducibility Thm 1(c,d)) |

`conj:Pcon`(e) is proved in coefficient form: the values of P_n(−1). The report shows that
these are the coefficients of (1+z)(1−2z)/((1−2z²)√(1−4z²)). That power-series identity is
classical and is not formalized.

### Where the Lean proofs differ from the reports

- **(e), odd n.** The report evaluates a sum with a Beta integral. Here a recurrence,
  U(a+1) = 2(a+1)U(a) + T(a+1, 0), is proved by finite algebra instead, so no integrals
  are needed.
- **Corrected (a).** The report goes through the exact first-difference index (its
  Theorem 2). Here VH_n ≡ VH_{n+j} and D_n ≡ D_{n+j} in low degree, then D_n (constant
  term 1) is cancelled.
- **x < −1 (Theorem A).** Instead of the reciprocal form R_n(y), the proof works with x
  directly, using the ratio of consecutive terms.
- **Theorem B (−1/2 ≤ x < 0, odd n).** The report's numerical bounds (0.419, 1.356, 6.48)
  are correct but tight; ∏(1−2^{−(2k−1)}) = 0.41942… The Lean proof replaces all three
  with a short chain of inequalities, b₁ ≤ (3/2)b₂ ≤ (3/2)b_{m+1}, and gets
  VH_n(x) ≥ S_m(x)²/4.

No mathematical error was found in any of the formalized report proofs. The helper agents
that formalized Theorems A and B noted that only the even-m half of the report's Lemma 1
is ever used.

## Not formalized

- Uniqueness of the odd-n real root, monotonicity of α_n, (d) irreducibility, the middle of
  (f): these are open, with no proof to check.
- (f)'s end windows: the low window uses DeSalvo–Pak log-concavity of p(n) and an exact
  computation to n ≈ 10,000.
- The 2-adic Newton-polygon part of (d): Mathlib has no Newton polygons.
- Zero clustering (Theorem C): it needs the Erdős–Turán theorem.
- `thm:degpn`, `thm:nu`, the exact first-difference index (Theorem 2), the defect −n in
  Lemma A, and the cyclotomic-sum formula for P_n: these were not attempted.

## Building

Lean is installed with [elan](https://github.com/leanprover/elan), at `~/.elan/bin`. Then:

```sh
cd code/lean
lake exe cache get          # download precompiled Mathlib (a few GB, once)
lake build                  # check every proof (about a minute after the cache)
lake env lean Audit/Axioms.lean   # list the axioms each main theorem uses
```

The toolchain and Mathlib are pinned: `lean-toolchain` (Lean v4.36.0) and `lakefile.toml`
(Mathlib tag `v4.36.0`). Build output in `.lake/` is not committed.

To read a proof interactively, open `code/lean` in VS Code with the "Lean 4" extension and
put the cursor inside a proof. The goal view shows what is left to prove at each step.