90 Yang–Mills: The Ladder to the Mass Gap

90.1 The ladder and its status




rung statement status



L1 \(\mathbb Z_2\) lattice gauge theory, every dimension: gap and area law (both sides) proved, incl. \(\mathbb Z^d\)
L2 every finite gauge group (incl. \(2I\)): link vanishing, area law, gap proved (limits: completeness of \(\mathbb R\))
L3 2D \(SU(2)\): exact solution, continuum limit, area law proved modulo H0, completeness of \(\mathbb R\)
L4 strong-coupling gap on \(\mathbb Z^d\), uniform in volume and dimension proved modulo H0, completeness of \(\mathbb R\)
L5 4D strong coupling: infinite volume, OS axioms, gap proved modulo H0, completeness of \(\mathbb R\)
L6 one block-spin step: gauge covariance, positivity, reflection positivity proved
L7 weak–strong bridge (contraction norm, uniform coupling decrement) 2D proved; \(d=3,4\) open
L8 continuum limit and \(\Delta >0\) (= T45) open



Throughout, a plaquette complex is a finite list of plaquettes given by their link-incidence vectors, \(D_g\) bounds the number of plaquettes sharing a link with a given plaquette, and \(s\) is the number of plaquettes touching the loop. All bounds are uniform in the volume: the constants depend only on \(D_g\), \(s\) and the coupling.

90.2 L1: \(\mathbb Z_2\) gauge theory in every dimension

Statement. For weights \(1+tU_p\) on any plaquette complex, \(t=ux\), \(x(1+y)^{D_g}\le y\), \(0\le u\le 1\):

\[ t^{|S|}\;\le \;\langle W\rangle \;\le \;(1+y)^{s}\,u^{m},\qquad 0\le \langle U_pU_q\rangle -\langle U_p\rangle \langle U_q\rangle \le (1+y)^{s}u^{R}, \]
for every \(S\) with \(\partial S=W\), \(m\) the least size of such \(S\), and \(p,q\) whose \(R\)-step closure balls are detached. On \(\mathbb Z^d\), \(D_g\le 8d\) and for an \(R\times T\) rectangle \(m=RT\):
\[ t^{RT}\le \langle W_{R\times T}\rangle \le (1+y)^{s}u^{RT}. \]

Key steps.

Lean files.

ym/StrongZ2.lean

/-
  ym/StrongZ2 — T45, cycle 19 (S2 for ℤ₂ lattice gauge theory, any dimension): Wilson loops bounded by surface sums.

  Plaquette weight 1 + t U_q (e^{βU} = cosh β (1 + tanh β U), t = tanh β ≥ 0). For a monomial n (the loop) and a
  plaquette list P on L links:
    parity / evenAll      every link multiplicity even
    link_exact            Σ_c mono n c = 2^L if every n_l is even, 0 otherwise
    G n P                 the surface sum: G n [] = [n even], G n (q :: P) = G n P + t · G (n + q) P
                          (= Σ_{S ⊆ P} t^|S| [n + Σ_S q even], the plaquette sets S with ∂S = the loop)
    N_eq                  N n P := Σ_c mono n c · Π_{q∈P}(1 + t U_q(c)) = 2^L · G n P
    Z_low                 Z = N 0 P ≥ 2^L
    wilson_le_surfaces    N_W ≤ G_W · Z, i.e. ⟨W⟩ = N_W / Z ≤ G_W
  The bound holds on every finite lattice in every d, but G_W itself depends on the lattice: exactly
  ⟨W⟩ = G_W / G_0, and G_W contains closed surfaces far from the loop. A bound uniform in the volume needs the
  factorisation G_W ≤ G_0 · Σ_{S connected to W, ∂S = W} t^|S| (connected components of plaquette sets); with a
  count of connected surfaces of area A it gives the strong-coupling area law. That factorisation is open.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GKS

namespace StrongZ2

open YMChain LinkIntegral GKS

variable {R : Type} (K : DetChain.CRing R)

def parity : Nat → Bool
  | 0 => true
  | k + 1 => !(parity k)

def evenAll : List Nat → Bool
  | [] => true
  | k :: n => parity k && evenAll n

theorem negone_pow_parity : ∀ k, pwR K (K.neg K.one) k = (if parity k = true then K.one else K.neg K.one)
  | 0 => rfl
  | k + 1 => by
    show K.mul (pwR K (K.neg K.one) k) (K.neg K.one) = (if (!(parity k)) = true then K.one else K.neg K.one)
    rw [negone_pow_parity k]
    cases parity k with
    | true => show K.mul K.one (K.neg K.one) = K.neg K.one; exact K.one_mul _
    | false => show K.mul (K.neg K.one) (K.neg K.one) = K.one; exact negone_sq K

def two : R := K.add K.one K.one

/-- **Exact link integral.** -/
theorem link_exact : ∀ n : List Nat,
    prodSum K (fsOf K n) = (if evenAll n = true then pwR K (two K) n.length else K.zero)
  | [] => rfl
  | k :: n => by
    show K.mul (K.add (pwR K K.one k) (pwR K (K.neg K.one) k)) (prodSum K (fsOf K n))
      = (if (parity k && evenAll n) = true then K.mul (pwR K (two K) n.length) (two K) else K.zero)
    rw [pw_one', negone_pow_parity, link_exact n]
    cases parity k with
    | true =>
      cases evenAll n with
      | true => show K.mul (K.add K.one K.one) (pwR K (two K) n.length) = K.mul (pwR K (two K) n.length) (two K)
                exact K.mul_comm _ _
      | false => show K.mul (K.add K.one K.one) K.zero = K.zero; exact DetChain.mul_zero K _
    | false => show K.mul (K.add K.one (K.neg K.one)) _ = K.zero; rw [K.add_neg, DetChain.zero_mul']

/-- The surface sum. -/
def G (t : R) : List Nat → List (List Nat) → R
  | n, [] => if evenAll n = true then K.one else K.zero
  | n, q :: P => K.add (G t n P) (K.mul t (G t (zadd n q) P))

def N (t : R) (L : Nat) (n : List Nat) (P : List (List Nat)) : R :=
  sumK K (fun c => K.mul (mono K n c) (prodQ K K.one t P c)) (bools L)

theorem N_nil (t : R) (n : List Nat) : N K t n.length n [] = K.mul (pwR K (two K) n.length) (G K t n []) := by
  show sumK K (fun c => K.mul (mono K n c) K.one) (bools n.length) = _
  have e : sumK K (fun c => K.mul (mono K n c) K.one) (bools n.length) = prodSum K (fsOf K n) := by
    rw [← sum_prod_factor K (fsOf K n), len_fsOf]
    exact sumK_ext K _ _ (fun c => by rw [K.mul_comm, K.one_mul]; rfl) _
  rw [e, link_exact]
  show _ = K.mul (pwR K (two K) n.length) (if evenAll n = true then K.one else K.zero)
  cases evenAll n with
  | true => exact (by show pwR K (two K) n.length = K.mul (pwR K (two K) n.length) K.one; rw [K.mul_comm, K.one_mul])
  | false => exact (DetChain.mul_zero K _).symm

theorem N_split (t : R) (L : Nat) (n q : List Nat) (P : List (List Nat)) (hnq : n.length = q.length) :
    N K t L n (q :: P) = K.add (N K t L n P) (K.mul t (N K t L (zadd n q) P)) := by
  show sumK K (fun c => K.mul (mono K n c) (K.mul (K.add K.one (K.mul t (mono K q c))) (prodQ K K.one t P c))) (bools L) = _
  have split : ∀ c, K.mul (mono K n c) (K.mul (K.add K.one (K.mul t (mono K q c))) (prodQ K K.one t P c))
      = K.add (K.mul (mono K n c) (prodQ K K.one t P c)) (K.mul t (K.mul (mono K (zadd n q) c) (prodQ K K.one t P c))) := fun c => by
    rw [← mono_mul K n q c hnq, DetChain.right_distrib, K.one_mul, K.distrib]
    congr 1
    rw [← K.mul_assoc (mono K n c) (K.mul t (mono K q c)), ← K.mul_assoc (mono K n c) t (mono K q c),
      K.mul_comm (mono K n c) t, K.mul_assoc t (mono K n c) (mono K q c), K.mul_assoc t]
  rw [sumK_ext K _ _ split, sumK_add, ← mul_sumK]
  rfl

/-- **N = 2^L G.** -/
theorem N_eq (t : R) : ∀ (P : List (List Nat)) (n : List Nat), (∀ q, memL q P → q.length = n.length) →
    N K t n.length n P = K.mul (pwR K (two K) n.length) (G K t n P)
  | [], n, _ => N_nil K t n
  | q :: P, n, hP => by
    have hq : n.length = q.length := (hP q (Or.inl rfl)).symm
    have hl : (zadd n q).length = n.length := len_zadd n.length n q rfl hq.symm
    rw [N_split K t n.length n q P hq, N_eq t P n (fun r hr => hP r (Or.inr hr))]
    have ih2 := N_eq t P (zadd n q) (fun r hr => (hP r (Or.inr hr)).trans hl.symm)
    rw [hl] at ih2
    rw [ih2]
    show _ = K.mul (pwR K (two K) n.length) (K.add (G K t n P) (K.mul t (G K t (zadd n q) P)))
    rw [K.distrib, swap3 K t]

variable (O : ORing K)

theorem G_nn (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) : ∀ (P : List (List Nat)) (n : List Nat),
    O.le K.zero (G K t n P)
  | [], n => by
    show O.le K.zero (if evenAll n = true then K.one else K.zero)
    cases evenAll n with
    | true => exact h1
    | false => exact O.le_refl _
  | q :: P, n => by
    have := O.add_le_add _ _ _ _ (G_nn h1 t ht P n) (O.mul_nonneg _ _ ht (G_nn h1 t ht P (zadd n q)))
    rw [K.zero_add] at this
    exact this

theorem G_ge_nil (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) : ∀ (P : List (List Nat)) (n : List Nat),
    O.le (G K t n []) (G K t n P)
  | [], _ => O.le_refl _
  | q :: P, n => by
    have := O.add_le_add _ _ _ _ (G_ge_nil h1 t ht P n) (O.mul_nonneg _ _ ht (G_nn K O h1 t ht P (zadd n q)))
    rw [DetChain.add_zero'] at this
    exact this

def zl : Nat → List Nat
  | 0 => []
  | L + 1 => 0 :: zl L

theorem len_zl : ∀ L, (zl L).length = L
  | 0 => rfl
  | L + 1 => congrArg Nat.succ (len_zl L)

theorem even_zl : ∀ L, evenAll (zl L) = true
  | 0 => rfl
  | L + 1 => even_zl L

theorem two_pow_nn (h1 : O.le K.zero K.one) : ∀ L, O.le K.zero (pwR K (two K) L)
  | 0 => h1
  | L + 1 => O.mul_nonneg _ _ (two_pow_nn h1 L) (by
      have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this)

/-- **Wilson loops are bounded by surface sums** (ℤ₂, any dimension, any lattice):
    `N_W ≤ G_W · Z`, i.e. `⟨W⟩ ≤ G_W = Σ_{S : ∂S = W} t^|S|`. -/
theorem wilson_le_surfaces (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) (L : Nat) (nW : List Nat)
    (hW : nW.length = L) (P : List (List Nat)) (hP : ∀ q, memL q P → q.length = L) :
    O.le (N K t L nW P) (K.mul (G K t nW P) (N K t L (zl L) P)) := by
  have eW := N_eq K t P nW (fun q hq => (hP q hq).trans hW.symm)
  have e0 := N_eq K t P (zl L) (fun q hq => (hP q hq).trans (len_zl L).symm)
  rw [hW] at eW
  rw [len_zl] at e0
  rw [eW, e0]
  -- 2^L G_W ≤ G_W · (2^L G_0) since G_0 ≥ G_0[] = 1
  have hG0 : O.le K.one (G K t (zl L) P) := by
    have := G_ge_nil K O h1 t ht P (zl L)
    have e1 : G K t (zl L) [] = K.one := by
      show (if evenAll (zl L) = true then K.one else K.zero) = K.one
      rw [even_zl]; rfl
    rw [e1] at this
    exact this
  have hc : O.le K.zero (K.mul (G K t nW P) (pwR K (two K) L)) :=
    O.mul_nonneg _ _ (G_nn K O h1 t ht P nW) (two_pow_nn K O h1 L)
  have := O.mul_le_mul_right _ _ _ hG0 hc
  rw [K.one_mul] at this
  rw [K.mul_comm (pwR K (two K) L) (G K t nW P), ← K.mul_assoc, K.mul_comm (K.mul (G K t nW P) (pwR K (two K) L))]
  exact this

end StrongZ2

#print axioms StrongZ2.negone_pow_parity
#print axioms StrongZ2.link_exact
#print axioms StrongZ2.N_nil
#print axioms StrongZ2.N_split
#print axioms StrongZ2.N_eq
#print axioms StrongZ2.G_nn
#print axioms StrongZ2.G_ge_nil
#print axioms StrongZ2.len_zl
#print axioms StrongZ2.even_zl
#print axioms StrongZ2.two_pow_nn
#print axioms StrongZ2.wilson_le_surfaces

ym/GKS.lean

/-
  ym/GKS — T45, cycle 14: the first Griffiths inequality for ℤ₂ lattice gauge theory, any dimension.

  Link fields are Bool lists c of length L (u_l = ±1). A monomial is given by an exponent list n (one
  exponent per link): mono c n = Π_l u_l^(n_l). A plaquette p is the monomial with exponent 1 on its four
  links (or any 0/1 list); the plaquette weight is a + b U_p (e^{βU} = cosh β + sinh β U, a, b ≥ 0).
    mono_mul       mono c n · mono c m = mono c (n + m)
    link_int_nn    Σ_c mono c n = Π_l (1 + (−1)^(n_l)) ≥ 0
    gks1           Σ_c mono c n · Π_{q ∈ P} (a + b · mono c q) ≥ 0  for a, b ≥ 0
  With n the loop's link set: ⟨W(C)⟩ ≥ 0 for every loop, every lattice, every β ≥ 0 (GKS I).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.LinkIntegral

namespace GKS

open YMChain LinkIntegral

variable {R : Type} (K : DetChain.CRing R)

def fsOf (n : List Nat) : List (Bool → R) := n.map (fun k b => pwR K (sgn K b) k)

def mono (n : List Nat) (c : List Bool) : R := prodF K (fsOf K n) c

def zadd : List Nat → List Nat → List Nat
  | [], m => m
  | n, [] => n
  | a :: n, b :: m => (a + b) :: zadd n m

theorem pwR_add (x : R) (a : Nat) : ∀ b, pwR K x (a + b) = K.mul (pwR K x a) (pwR K x b)
  | 0 => by show pwR K x a = K.mul (pwR K x a) K.one; rw [K.mul_comm, K.one_mul]
  | b + 1 => by
    show K.mul (pwR K x (a + b)) x = K.mul (pwR K x a) (K.mul (pwR K x b) x)
    rw [pwR_add x a b, K.mul_assoc]

theorem mono_mul : ∀ (n m : List Nat) (c : List Bool), n.length = m.length →
    K.mul (mono K n c) (mono K m c) = mono K (zadd n m) c
  | [], [], _, _ => K.one_mul K.one
  | a :: n, b :: m, [], _ => K.one_mul K.one
  | a :: n, b :: m, x :: c, h => by
    show K.mul (K.mul (pwR K (sgn K x) a) (mono K n c)) (K.mul (pwR K (sgn K x) b) (mono K m c))
      = K.mul (pwR K (sgn K x) (a + b)) (mono K (zadd n m) c)
    rw [← mono_mul n m c (Nat.succ.inj h), pwR_add, K.mul_assoc, ← K.mul_assoc (mono K n c),
      K.mul_comm (mono K n c) (pwR K (sgn K x) b), K.mul_assoc, ← K.mul_assoc]
  | [], _ :: _, _, h => absurd h (Nat.noConfusion)
  | _ :: _, [], _, h => absurd h (Nat.noConfusion)

variable (O : ORing K)

theorem sgn_pow_nn (h1 : O.le K.zero K.one) : ∀ k, O.le K.zero (K.add (pwR K (sgn K true) k) (pwR K (sgn K false) k))
  | k => by
    -- (−1)^k is 1 or −1; the sum is 2 or 0
    have par : ∀ k, pwR K (K.neg K.one) k = K.one ∨ pwR K (K.neg K.one) k = K.neg K.one := by
      intro k
      induction k with
      | zero => exact Or.inl rfl
      | succ k ih =>
        cases ih with
        | inl h => exact Or.inr (by show K.mul _ _ = _; rw [h, K.one_mul])
        | inr h => exact Or.inl (by show K.mul _ _ = _; rw [h, negone_sq])
    show O.le K.zero (K.add (pwR K K.one k) (pwR K (K.neg K.one) k))
    rw [pw_one']
    cases par k with
    | inl h => rw [h]; have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
    | inr h => rw [h, K.add_neg]; exact O.le_refl _

theorem prodSum_nn (h1 : O.le K.zero K.one) : ∀ n : List Nat, O.le K.zero (prodSum K (fsOf K n))
  | [] => h1
  | k :: n => O.mul_nonneg _ _ (sgn_pow_nn K O h1 k) (prodSum_nn h1 n)

theorem len_fsOf (n : List Nat) : (fsOf K n).length = n.length := by
  induction n with
  | nil => rfl
  | cons _ n ih => exact congrArg Nat.succ ih

/-- **Link integrals of monomials are nonnegative.** -/
theorem link_int_nn (h1 : O.le K.zero K.one) (n : List Nat) :
    O.le K.zero (sumK K (mono K n) (bools n.length)) := by
  have e : sumK K (mono K n) (bools n.length) = prodSum K (fsOf K n) := by
    rw [← sum_prod_factor K (fsOf K n), len_fsOf]; rfl
  rw [e]; exact prodSum_nn K O h1 n

/-- `Π_{q ∈ P} (a + b · U_q)`. -/
def prodQ (a b : R) : List (List Nat) → List Bool → R
  | [], _ => K.one
  | q :: P, c => K.mul (K.add a (K.mul b (mono K q c))) (prodQ a b P c)

theorem len_zadd (L : Nat) : ∀ (n q : List Nat), n.length = L → q.length = L → (zadd n q).length = L
  | [], _, h1, h2 => h2
  | _ :: _, [], h1, _ => h1
  | _ :: n, _ :: q, h1, h2 => by
    cases L with
    | zero => exact absurd h1 Nat.noConfusion
    | succ L => exact congrArg Nat.succ (len_zadd L n q (Nat.succ.inj h1) (Nat.succ.inj h2))

omit O in
theorem zero_add_le' (O : ORing K) {x y : R} (hx : O.le K.zero x) (hy : O.le K.zero y) : O.le K.zero (K.add x y) := by
  have := O.add_le_add _ _ _ _ hx hy; rw [K.zero_add] at this; exact this

/-- **GKS I.** -/
theorem gks1 (h1 : O.le K.zero K.one) (a b : R) (ha : O.le K.zero a) (hb : O.le K.zero b) (L : Nat) :
    ∀ (P : List (List Nat)) (n : List Nat), n.length = L → (∀ q, memL q P → q.length = L) →
    O.le K.zero (sumK K (fun c => K.mul (mono K n c) (prodQ K a b P c)) (bools L))
  | [], n, hn, _ => by
    have := link_int_nn K O h1 n
    rw [hn] at this
    exact (sumK_ext K _ _ (fun c => (by show mono K n c = K.mul (mono K n c) K.one; rw [K.mul_comm, K.one_mul])) _) ▸ this
  | q :: P, n, hn, hP => by
    have hq : q.length = L := hP q (Or.inl rfl)
    have split : ∀ c, K.mul (mono K n c) (prodQ K a b (q :: P) c)
        = K.add (K.mul a (K.mul (mono K n c) (prodQ K a b P c))) (K.mul b (K.mul (mono K (zadd n q) c) (prodQ K a b P c))) := fun c => by
      show K.mul (mono K n c) (K.mul (K.add a (K.mul b (mono K q c))) (prodQ K a b P c)) = _
      rw [← mono_mul K n q c (hn.trans hq.symm), DetChain.right_distrib, K.distrib]
      rw [← K.mul_assoc (mono K n c) a, K.mul_comm (mono K n c) a, K.mul_assoc a]
      rw [← K.mul_assoc (mono K n c) (K.mul b (mono K q c)), ← K.mul_assoc (mono K n c) b (mono K q c),
        K.mul_comm (mono K n c) b, K.mul_assoc b (mono K n c) (mono K q c), K.mul_assoc b]
    rw [sumK_ext K _ _ split, sumK_add, ← mul_sumK, ← mul_sumK]
    have hz : (zadd n q).length = L := len_zadd L n q hn hq
    exact zero_add_le' K O (O.mul_nonneg _ _ ha (gks1 h1 a b ha hb L P n hn (fun r hr => hP r (Or.inr hr))))
      (O.mul_nonneg _ _ hb (gks1 h1 a b ha hb L P (zadd n q) hz (fun r hr => hP r (Or.inr hr))))

/-- Per-plaquette coefficients: `Π_{(q, a, b) ∈ P} (a + b · U_q)`. -/
def prodQ' : List (List Nat × R × R) → List Bool → R
  | [], _ => K.one
  | (q, a, b) :: P, c => K.mul (K.add a (K.mul b (mono K q c))) (prodQ' P c)

/-- **GKS I with plaquette-dependent nonnegative coefficients.** -/
theorem gks1' (h1 : O.le K.zero K.one) (L : Nat) :
    ∀ (P : List (List Nat × R × R)) (n : List Nat), n.length = L →
    (∀ t, memL t P → t.1.length = L ∧ O.le K.zero t.2.1 ∧ O.le K.zero t.2.2) →
    O.le K.zero (sumK K (fun c => K.mul (mono K n c) (prodQ' K P c)) (bools L))
  | [], n, hn, _ => by
    have := link_int_nn K O h1 n
    rw [hn] at this
    exact (sumK_ext K _ _ (fun c => (by show mono K n c = K.mul (mono K n c) K.one; rw [K.mul_comm, K.one_mul])) _) ▸ this
  | (q, a, b) :: P, n, hn, hP => by
    obtain ⟨hq, ha, hb⟩ := hP (q, a, b) (Or.inl rfl)
    have split : ∀ c, K.mul (mono K n c) (prodQ' K ((q, a, b) :: P) c)
        = K.add (K.mul a (K.mul (mono K n c) (prodQ' K P c))) (K.mul b (K.mul (mono K (zadd n q) c) (prodQ' K P c))) := fun c => by
      show K.mul (mono K n c) (K.mul (K.add a (K.mul b (mono K q c))) (prodQ' K P c)) = _
      rw [← mono_mul K n q c (hn.trans hq.symm), DetChain.right_distrib, K.distrib]
      rw [← K.mul_assoc (mono K n c) a, K.mul_comm (mono K n c) a, K.mul_assoc a]
      rw [← K.mul_assoc (mono K n c) (K.mul b (mono K q c)), ← K.mul_assoc (mono K n c) b (mono K q c),
        K.mul_comm (mono K n c) b, K.mul_assoc b (mono K n c) (mono K q c), K.mul_assoc b]
    rw [sumK_ext K _ _ split, sumK_add, ← mul_sumK, ← mul_sumK]
    exact zero_add_le' K O (O.mul_nonneg _ _ ha (gks1' h1 L P n hn (fun t ht => hP t (Or.inr ht))))
      (O.mul_nonneg _ _ hb (gks1' h1 L P (zadd n q) (len_zadd L n q hn hq) (fun t ht => hP t (Or.inr ht))))

omit O in
theorem perm4' (A X Y Z : R) : K.add (K.add A X) (K.add Y Z) = K.add (K.add A Z) (K.add Y X) := by
  rw [K.add_assoc A X, K.add_assoc A Z, K.add_comm Y Z, ← K.add_assoc X Z Y, K.add_comm X Z,
    K.add_assoc Z X Y, K.add_comm X Y]

/-- The doubled plaquette factor: `(a + b U)(a + b U S) = (a² + b² S) + ab (1 + S) U` for `U, S = ±1`,
    and both coefficients are `≥ 0` when `a ≥ b ≥ 0` and `S = ±1`. -/
theorem doubled_factor (a b U S : R) (hU : K.mul U U = K.one) :
    K.mul (K.add a (K.mul b U)) (K.add a (K.mul b (K.mul U S)))
      = K.add (K.add (K.mul a a) (K.mul (K.mul b b) S)) (K.mul (K.mul (K.mul a b) (K.add K.one S)) U) := by
  rw [K.distrib, DetChain.right_distrib, DetChain.right_distrib]
  -- (bU)(b U S) = b² S
  have e1 : K.mul (K.mul b U) (K.mul b (K.mul U S)) = K.mul (K.mul b b) S := by
    rw [K.mul_assoc b U, ← K.mul_assoc U b, K.mul_comm U b, K.mul_assoc b U, ← K.mul_assoc U U S, hU, K.one_mul,
      ← K.mul_assoc]
  rw [e1]
  -- a(b U S) + (bU) a = ab(1 + S) U   (after reordering)
  have e2 : K.add (K.mul a (K.mul b (K.mul U S))) (K.mul (K.mul b U) a) = K.mul (K.mul (K.mul a b) (K.add K.one S)) U := by
    rw [K.distrib (K.mul a b), K.mul_comm (K.mul a b) K.one, K.one_mul, DetChain.right_distrib, K.add_comm]
    rw [K.mul_comm (K.mul b U) a, ← K.mul_assoc a b U]
    congr 1
    rw [← K.mul_assoc a b, K.mul_assoc (K.mul a b) S U, K.mul_comm S U]
  rw [← e2]
  exact perm4' K _ _ _ _

end GKS

#print axioms GKS.pwR_add
#print axioms GKS.mono_mul
#print axioms GKS.sgn_pow_nn
#print axioms GKS.prodSum_nn
#print axioms GKS.len_fsOf
#print axioms GKS.link_int_nn
#print axioms GKS.len_zadd
#print axioms GKS.zero_add_le'
#print axioms GKS.gks1
#print axioms GKS.gks1'
#print axioms GKS.perm4'
#print axioms GKS.doubled_factor

ym/GKS2.lean

/-
  ym/GKS2 — T45, cycle 16 (towards GKS II for ℤ₂ gauge theory): monomials under the doubling change of variables.

  Link fields c, s (Bool lists); c' = zx s c is the field with u'_l = u_l(s) · u_l(c).
    sgn_bx        sgn (b xor b') = sgn b · sgn b'
    mono_zx       mono n (s ⊙ c) = mono n s · mono n c,  s ⊙ c = zx s (not c)  (lists of equal length)
    mono_sq       mono n s · mono n s = 1
    mono_pm       monomials are ±1;  prodQ'_double: w(c) w(s⊙c) = Π_q [(a² + b² U_q(s)) + ab (1 + U_q(s)) U_q(c)]
    dbl_nn        those coefficients are ≥ 0 when 0 ≤ b ≤ a
    gks2          GKS II: Σ_{c,c'} A(c)B(c') w w' ≤ Σ_{c,c'} A(c)B(c) w w', i.e. ⟨A⟩⟨B⟩ ≤ ⟨AB⟩, any lattice, any dimension
  With ChangeVar2D.sum_xor (Σ_s G (zx s c) = Σ_s G s) the doubled difference is
  Σ_s (1 − W_B(s)) Σ_c W_A W_B(c) Π_q [(a² + b² U_q(s)) + ab (1 + U_q(s)) U_q(c)], a sum of GKS I terms.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GKS
import ym.ChangeVar2D
import BridgeChain

namespace GKS2

open YMChain LinkIntegral GKS ChangeVar2D

variable {R : Type} (K : DetChain.CRing R)

/-- The group law of signs in the Bool encoding (true ↦ +1): `b ⊙ b' = b xor (not b')`. -/
theorem sgn_bx (a b : Bool) : sgn K (bx a (!b)) = K.mul (sgn K a) (sgn K b) := by
  cases a <;> cases b
  · show K.one = K.mul (K.neg K.one) (K.neg K.one)
    rw [negone_sq]
  · show K.neg K.one = K.mul (K.neg K.one) K.one
    rw [K.mul_comm, K.one_mul]
  · show K.neg K.one = K.mul K.one (K.neg K.one)
    rw [K.one_mul]
  · show K.one = K.mul K.one K.one
    rw [K.one_mul]

def notL (c : List Bool) : List Bool := c.map (fun b => !b)

theorem pwR_mul (x y : R) : ∀ k, pwR K (K.mul x y) k = K.mul (pwR K x k) (pwR K y k)
  | 0 => (K.one_mul K.one).symm
  | k + 1 => by
    show K.mul (pwR K (K.mul x y) k) (K.mul x y) = K.mul (K.mul (pwR K x k) x) (K.mul (pwR K y k) y)
    rw [pwR_mul x y k, mul_mid4 K]

theorem mono_zx : ∀ (n : List Nat) (s c : List Bool), s.length = c.length →
    mono K n (zx s (notL c)) = K.mul (mono K n s) (mono K n c)
  | [], _, _, _ => (K.one_mul K.one).symm
  | _ :: _, [], [], _ => (K.one_mul K.one).symm
  | k :: n, a :: s, b :: c, h => by
    show K.mul (pwR K (sgn K (bx a (!b))) k) (mono K n (zx s (notL c)))
      = K.mul (K.mul (pwR K (sgn K a) k) (mono K n s)) (K.mul (pwR K (sgn K b) k) (mono K n c))
    rw [mono_zx n s c (Nat.succ.inj h), sgn_bx, pwR_mul, mul_mid4 K]
  | _ :: _, [], _ :: _, h => absurd h Nat.noConfusion
  | _ :: _, _ :: _, [], h => absurd h Nat.noConfusion

theorem sgn_sq (b : Bool) : K.mul (sgn K b) (sgn K b) = K.one := by
  cases b
  · exact negone_sq K
  · exact K.one_mul K.one

theorem mono_sq : ∀ (n : List Nat) (s : List Bool), K.mul (mono K n s) (mono K n s) = K.one
  | [], _ => K.one_mul K.one
  | _ :: _, [] => K.one_mul K.one
  | k :: n, a :: s => by
    show K.mul (K.mul (pwR K (sgn K a) k) (mono K n s)) (K.mul (pwR K (sgn K a) k) (mono K n s)) = K.one
    rw [mul_mid4 K, ← pwR_mul, sgn_sq, pw_one', mono_sq n s, K.one_mul]

/-- Monomials take the values ±1. -/
theorem mono_pm : ∀ (n : List Nat) (s : List Bool), mono K n s = K.one ∨ mono K n s = K.neg K.one
  | [], _ => Or.inl rfl
  | _ :: _, [] => Or.inl rfl
  | k :: n, a :: s => by
    show K.mul (pwR K (sgn K a) k) (mono K n s) = K.one ∨ K.mul (pwR K (sgn K a) k) (mono K n s) = K.neg K.one
    have hp : pwR K (sgn K a) k = K.one ∨ pwR K (sgn K a) k = K.neg K.one := by
      induction k with
      | zero => exact Or.inl rfl
      | succ k ih =>
        show K.mul (pwR K (sgn K a) k) (sgn K a) = K.one ∨ K.mul (pwR K (sgn K a) k) (sgn K a) = K.neg K.one
        cases a with
        | true => cases ih with
          | inl h => exact Or.inl (by rw [h]; exact K.one_mul K.one)
          | inr h => exact Or.inr (by rw [h]; show K.mul (K.neg K.one) K.one = _; rw [K.mul_comm, K.one_mul])
        | false => cases ih with
          | inl h => exact Or.inr (by rw [h]; exact K.one_mul _)
          | inr h => exact Or.inl (by rw [h]; exact negone_sq K)
    cases hp with
    | inl h => rw [h, K.one_mul]; exact mono_pm n s
    | inr h =>
      rw [h, DetChain.neg_mul_left, K.one_mul]
      cases mono_pm n s with
      | inl h' => exact Or.inr (by rw [h'])
      | inr h' => exact Or.inl (by rw [h', DetChain.neg_neg'])

/-- The doubled plaquette list for a fixed `s`. -/
def dbl (s : List Bool) : List (List Nat × R × R) → List (List Nat × R × R)
  | [] => []
  | (q, a, b) :: P => (q, K.add (K.mul a a) (K.mul (K.mul b b) (mono K q s)),
      K.mul (K.mul a b) (K.add K.one (mono K q s))) :: dbl s P

theorem prodQ'_double (s c : List Bool) (hsc : s.length = c.length) : ∀ P : List (List Nat × R × R),
    K.mul (prodQ' K P c) (prodQ' K P (zx s (notL c))) = prodQ' K (dbl K s P) c
  | [] => K.one_mul K.one
  | (q, a, b) :: P => by
    show K.mul (K.mul (K.add a (K.mul b (mono K q c))) (prodQ' K P c))
        (K.mul (K.add a (K.mul b (mono K q (zx s (notL c))))) (prodQ' K P (zx s (notL c))))
      = K.mul (K.add (K.add (K.mul a a) (K.mul (K.mul b b) (mono K q s)))
          (K.mul (K.mul (K.mul a b) (K.add K.one (mono K q s))) (mono K q c))) (prodQ' K (dbl K s P) c)
    rw [mul_mid4 K, prodQ'_double s c hsc P, mono_zx K q s c hsc, K.mul_comm (mono K q s) (mono K q c),
      doubled_factor K a b (mono K q c) (mono K q s) (mono_sq K q c)]

variable (O : ORing K)

theorem nn_add (x y : R) (hx : O.le K.zero x) (hy : O.le K.zero y) : O.le K.zero (K.add x y) := by
  have := O.add_le_add _ _ _ _ hx hy; rw [K.zero_add] at this; exact this

/-- Coefficients of the doubled list are nonnegative when `0 ≤ b ≤ a`. -/
theorem dbl_nn (h1 : O.le K.zero K.one) (s : List Bool) (L : Nat) : ∀ P : List (List Nat × R × R),
    (∀ t, memL t P → t.1.length = L ∧ O.le K.zero t.2.2 ∧ O.le t.2.2 t.2.1) →
    ∀ t, memL t (dbl K s P) → t.1.length = L ∧ O.le K.zero t.2.1 ∧ O.le K.zero t.2.2
  | [], _, _, h => absurd h (fun z => z)
  | (q, a, b) :: P, hP, t, ht => by
    obtain ⟨hq, hb0, hba0⟩ := hP (q, a, b) (Or.inl rfl)
    have hb : O.le K.zero b := hb0
    have hba : O.le b a := hba0
    have ha : O.le K.zero a := O.le_trans _ _ _ hb hba
    cases ht with
    | inr h' => exact dbl_nn h1 s L P (fun u hu => hP u (Or.inr hu)) t h'
    | inl e =>
      rw [e]
      refine ⟨hq, ?_, ?_⟩
      · show O.le K.zero (K.add (K.mul a a) (K.mul (K.mul b b) (mono K q s)))
        cases mono_pm K q s with
        | inl hS => rw [hS, K.mul_comm (K.mul b b) K.one, K.one_mul]
                    exact nn_add K O _ _ (O.mul_nonneg _ _ ha ha) (O.mul_nonneg _ _ hb hb)
        | inr hS =>
          rw [hS, mul_neg' K, K.mul_comm (K.mul b b) K.one, K.one_mul]
          -- b b ≤ a b ≤ a a
          have h2a : O.le (K.mul b b) (K.mul a b) := O.mul_le_mul_right _ _ b hba hb
          have h2b : O.le (K.mul b a) (K.mul a a) := O.mul_le_mul_right _ _ a hba ha
          rw [K.mul_comm b a] at h2b
          have h2 : O.le (K.mul b b) (K.mul a a) := O.le_trans _ _ _ h2a h2b
          have := O.add_le_add _ _ _ _ h2 (O.le_refl (K.neg (K.mul b b)))
          rw [K.add_neg] at this
          exact this
      · show O.le K.zero (K.mul (K.mul a b) (K.add K.one (mono K q s)))
        cases mono_pm K q s with
        | inl hS => rw [hS]; exact O.mul_nonneg _ _ (O.mul_nonneg _ _ ha hb) (nn_add K O _ _ h1 h1)
        | inr hS => rw [hS, K.add_neg, DetChain.mul_zero]; exact O.le_refl _
where
  mul_neg' (K : DetChain.CRing R) (x y : R) : K.mul x (K.neg y) = K.neg (K.mul x y) := by
    rw [K.mul_comm, DetChain.neg_mul_left, K.mul_comm]

/-- **GKS II.** For plaquette weights `a + b U` with `0 ≤ b ≤ a` and monomials `A`, `B` (exponent lists of length L):
    `Σ_{c,c'} A(c) B(c') w(c) w(c') ≤ Σ_{c,c'} A(c) B(c) w(c) w(c')`, i.e. `⟨A⟩⟨B⟩ ≤ ⟨A B⟩`. -/
theorem gks2 (h1 : O.le K.zero K.one) (L : Nat) (P : List (List Nat × R × R))
    (hP : ∀ t, memL t P → t.1.length = L ∧ O.le K.zero t.2.2 ∧ O.le t.2.2 t.2.1)
    (nA nB : List Nat) (hA : nA.length = L) (hB : nB.length = L) :
    O.le (sumK K (fun c => sumK K (fun c' => K.mul (K.mul (mono K nA c) (mono K nB c'))
            (K.mul (prodQ' K P c) (prodQ' K P c'))) (bools L)) (bools L))
         (sumK K (fun c => sumK K (fun c' => K.mul (K.mul (mono K nA c) (mono K nB c))
            (K.mul (prodQ' K P c) (prodQ' K P c'))) (bools L)) (bools L)) := by
  -- Z c s = A(c)B(c) (1 − B(s)) w(c) w(s⊙c)
  let Z : List Bool → List Bool → R := fun c s => K.mul (K.mul (K.mul (mono K nA c) (mono K nB c))
      (K.add K.one (K.neg (mono K nB s)))) (K.mul (prodQ' K P c) (prodQ' K P (zx s (notL c))))
  have hlen : ∀ c, memL c (bools L) → (notL c).length = L := fun c hc => by
    show (c.map (fun b => !b)).length = L
    rw [len_map3]; exact mem_bools_len L c hc
  -- rewrite both inner sums with c' = s ⊙ c and compare pointwise
  have hX : ∀ c, memL c (bools L) →
      sumK K (fun c' => K.mul (K.mul (mono K nA c) (mono K nB c)) (K.mul (prodQ' K P c) (prodQ' K P c'))) (bools L)
        = K.add (sumK K (fun s => K.mul (K.mul (mono K nA c) (mono K nB (zx s (notL c))))
              (K.mul (prodQ' K P c) (prodQ' K P (zx s (notL c))))) (bools L))
            (sumK K (Z c) (bools L)) := fun c hc => by
    rw [← sum_xor K L (fun c' => K.mul (K.mul (mono K nA c) (mono K nB c)) (K.mul (prodQ' K P c) (prodQ' K P c')))
      (notL c) (hlen c hc), ← sumK_add]
    refine sumK_ext_mem K _ _ (bools L) (fun s hs => ?_)
    have hsl : s.length = c.length := (mem_bools_len L s hs).trans (mem_bools_len L c hc).symm
    show K.mul (K.mul (mono K nA c) (mono K nB c)) (K.mul (prodQ' K P c) (prodQ' K P (zx s (notL c))))
      = K.add (K.mul (K.mul (mono K nA c) (mono K nB (zx s (notL c)))) (K.mul (prodQ' K P c) (prodQ' K P (zx s (notL c)))))
          (K.mul (K.mul (K.mul (mono K nA c) (mono K nB c)) (K.add K.one (K.neg (mono K nB s))))
            (K.mul (prodQ' K P c) (prodQ' K P (zx s (notL c)))))
    rw [mono_zx K nB s c hsl, ← DetChain.right_distrib, K.distrib (K.mul (mono K nA c) (mono K nB c)), K.mul_comm _ K.one,
      K.one_mul]
    congr 1
    -- A·(B(s)B(c)) + (AB(c) + AB(c)(−B(s))) = AB(c)
    rw [Sym2Local'.mul_neg K, ← K.add_assoc, K.add_comm (K.mul (mono K nA c) (K.mul (mono K nB s) (mono K nB c))),
      K.add_assoc]
    have e : K.mul (mono K nA c) (K.mul (mono K nB s) (mono K nB c)) = K.mul (K.mul (mono K nA c) (mono K nB c)) (mono K nB s) := by
      rw [K.mul_comm (mono K nB s) (mono K nB c), K.mul_assoc]
    rw [e, K.add_neg, DetChain.add_zero']
  have hY : ∀ c, memL c (bools L) →
      sumK K (fun c' => K.mul (K.mul (mono K nA c) (mono K nB c')) (K.mul (prodQ' K P c) (prodQ' K P c'))) (bools L)
        = sumK K (fun s => K.mul (K.mul (mono K nA c) (mono K nB (zx s (notL c))))
              (K.mul (prodQ' K P c) (prodQ' K P (zx s (notL c))))) (bools L) := fun c hc =>
    (sum_xor K L (fun c' => K.mul (K.mul (mono K nA c) (mono K nB c')) (K.mul (prodQ' K P c) (prodQ' K P c')))
      (notL c) (hlen c hc)).symm
  rw [sumK_ext_mem K _ _ (bools L) hX, sumK_ext_mem K _ _ (bools L) hY, sumK_add]
  -- Σ_c Σ_s Z ≥ 0
  have hZ : O.le K.zero (sumK K (fun c => sumK K (Z c) (bools L)) (bools L)) := by
    rw [sumK_swap]
    refine zero_le_sum' K O _ (bools L) (fun s hs => ?_)
    have e : ∀ c, memL c (bools L) → Z c s
        = K.mul (K.add K.one (K.neg (mono K nB s))) (K.mul (mono K (zadd nA nB) c) (prodQ' K (dbl K s P) c)) := fun c hc => by
      have hsl : s.length = c.length := (mem_bools_len L s hs).trans (mem_bools_len L c hc).symm
      show K.mul (K.mul (K.mul (mono K nA c) (mono K nB c)) (K.add K.one (K.neg (mono K nB s))))
          (K.mul (prodQ' K P c) (prodQ' K P (zx s (notL c)))) = _
      rw [prodQ'_double K s c hsl, mono_mul K nA nB c (hA.trans hB.symm), K.mul_comm (mono K (zadd nA nB) c),
        K.mul_assoc, K.mul_comm (K.add K.one (K.neg (mono K nB s)))]
    rw [sumK_ext_mem K _ _ (bools L) e, ← mul_sumK]
    have hB1 : O.le K.zero (K.add K.one (K.neg (mono K nB s))) := by
      cases mono_pm K nB s with
      | inl h => rw [h, K.add_neg]; exact O.le_refl _
      | inr h => rw [h, DetChain.neg_neg']; exact nn_add K O _ _ h1 h1
    exact O.mul_nonneg _ _ hB1 (gks1' K O h1 L (dbl K s P) (zadd nA nB) (len_zadd L nA nB hA hB)
      (dbl_nn K O h1 s L P hP))
  have := O.add_le_add _ _ _ _ (O.le_refl (sumK K (fun c => sumK K (fun s => K.mul (K.mul (mono K nA c)
      (mono K nB (zx s (notL c)))) (K.mul (prodQ' K P c) (prodQ' K P (zx s (notL c))))) (bools L)) (bools L))) hZ
  rw [DetChain.add_zero'] at this
  exact this
where
  Sym2Local'.mul_neg (K : DetChain.CRing R) (x y : R) : K.mul x (K.neg y) = K.neg (K.mul x y) := by
    rw [K.mul_comm, DetChain.neg_mul_left, K.mul_comm]
  zero_le_sum' (K : DetChain.CRing R) (O : ORing K) {A : Type} (f : A → R) : ∀ l : List A,
      (∀ a, memL a l → O.le K.zero (f a)) → O.le K.zero (sumK K f l)
    | [], _ => O.le_refl _
    | a :: l, h => by
      have := O.add_le_add _ _ _ _ (h a (Or.inl rfl)) (zero_le_sum' K O f l (fun b hb => h b (Or.inr hb)))
      rw [K.zero_add] at this; exact this

/-- Double sums of products split: `Σ_c Σ_c' f(c) g(c') = (Σ f)(Σ g)`. -/
theorem dsum (f g : List Bool → R) (l : List (List Bool)) :
    sumK K (fun c => sumK K (fun c' => K.mul (f c) (g c')) l) l = K.mul (sumK K f l) (sumK K g l) :=
  BridgeChain.double_sum K f g l l

/-- **Monotonicity in the lattice.** Switching on one more plaquette `q` with weight `a + b U_q` (`b ≥ 0`) does not
    decrease any Wilson loop: `⟨W⟩_P ≤ ⟨W⟩_(P+q)`, cross-multiplied:
    `(Σ W w)(Σ w (a + b U_q)) ≤ (Σ W w (a + b U_q))(Σ w)`. -/
theorem monotone_plaquette (h1 : O.le K.zero K.one) (L : Nat) (P : List (List Nat × R × R))
    (hP : ∀ t, memL t P → t.1.length = L ∧ O.le K.zero t.2.2 ∧ O.le t.2.2 t.2.1)
    (nW q : List Nat) (hW : nW.length = L) (hq : q.length = L) (a b : R) (hb : O.le K.zero b) :
    O.le (K.mul (sumK K (fun c => K.mul (mono K nW c) (prodQ' K P c)) (bools L))
            (sumK K (fun c => K.mul (prodQ' K P c) (K.add a (K.mul b (mono K q c)))) (bools L)))
         (K.mul (sumK K (fun c => K.mul (K.mul (mono K nW c) (prodQ' K P c)) (K.add a (K.mul b (mono K q c)))) (bools L))
            (sumK K (prodQ' K P) (bools L))) := by
  -- GKS II with A = W, B = U_q
  have g := gks2 K O h1 L P hP nW q hW hq
  -- rewrite both double sums as products of single sums
  have eY : sumK K (fun c => sumK K (fun c' => K.mul (K.mul (mono K nW c) (mono K q c'))
      (K.mul (prodQ' K P c) (prodQ' K P c'))) (bools L)) (bools L)
      = K.mul (sumK K (fun c => K.mul (mono K nW c) (prodQ' K P c)) (bools L))
          (sumK K (fun c => K.mul (mono K q c) (prodQ' K P c)) (bools L)) := by
    rw [← dsum]
    exact sumK_ext K _ _ (fun c => sumK_ext K _ _ (fun c' => mul_mid4 K _ _ _ _) _) _
  have eX : sumK K (fun c => sumK K (fun c' => K.mul (K.mul (mono K nW c) (mono K q c))
      (K.mul (prodQ' K P c) (prodQ' K P c'))) (bools L)) (bools L)
      = K.mul (sumK K (fun c => K.mul (K.mul (mono K nW c) (mono K q c)) (prodQ' K P c)) (bools L))
          (sumK K (prodQ' K P) (bools L)) := by
    rw [← dsum]
    exact sumK_ext K _ _ (fun c => sumK_ext K _ _ (fun c' => by
      rw [K.mul_assoc (K.mul (mono K nW c) (mono K q c)) (prodQ' K P c) (prodQ' K P c'), K.mul_assoc (mono K nW c) (mono K q c)]) _) _
  rw [eY, eX] at g
  -- expand both sides: the a-parts agree, the b-parts are b·Y ≤ b·X
  have hb' := O.mul_le_mul_right _ _ b g hb
  -- the two single sums with the new plaquette
  have hS1 : sumK K (fun c => K.mul (prodQ' K P c) (K.add a (K.mul b (mono K q c)))) (bools L)
      = K.add (K.mul a (sumK K (prodQ' K P) (bools L))) (K.mul b (sumK K (fun c => K.mul (mono K q c) (prodQ' K P c)) (bools L))) := by
    rw [mul_sumK, mul_sumK, ← sumK_add]
    exact sumK_ext K _ _ (fun c => by
      show K.mul (prodQ' K P c) (K.add a (K.mul b (mono K q c))) = K.add (K.mul a (prodQ' K P c)) (K.mul b (K.mul (mono K q c) (prodQ' K P c)))
      rw [K.distrib, K.mul_comm (prodQ' K P c) a, K.mul_comm (prodQ' K P c) (K.mul b (mono K q c)), K.mul_assoc]) _
  have hS2 : sumK K (fun c => K.mul (K.mul (mono K nW c) (prodQ' K P c)) (K.add a (K.mul b (mono K q c)))) (bools L)
      = K.add (K.mul a (sumK K (fun c => K.mul (mono K nW c) (prodQ' K P c)) (bools L)))
          (K.mul b (sumK K (fun c => K.mul (K.mul (mono K nW c) (mono K q c)) (prodQ' K P c)) (bools L))) := by
    rw [mul_sumK, mul_sumK, ← sumK_add]
    exact sumK_ext K _ _ (fun c => by
      show K.mul (K.mul (mono K nW c) (prodQ' K P c)) (K.add a (K.mul b (mono K q c)))
        = K.add (K.mul a (K.mul (mono K nW c) (prodQ' K P c))) (K.mul b (K.mul (K.mul (mono K nW c) (mono K q c)) (prodQ' K P c)))
      rw [K.distrib, K.mul_comm (K.mul (mono K nW c) (prodQ' K P c)) a]
      congr 1
      rw [K.mul_comm (K.mul (mono K nW c) (prodQ' K P c)) (K.mul b (mono K q c)), K.mul_assoc b, ← K.mul_assoc (mono K q c),
        K.mul_comm (mono K q c) (mono K nW c), K.mul_assoc (mono K nW c) (mono K q c), ← K.mul_assoc (mono K nW c)]) _
  rw [hS1, hS2, K.distrib, DetChain.right_distrib]
  -- (ΣWw)(aΣw) = (aΣWw)(Σw);  (ΣWw)(bΣUw) = (ΣWw ΣUw) b;  (bΣWUw)(Σw) = (ΣWUw Σw) b
  have e1 : K.mul (sumK K (fun c => K.mul (mono K nW c) (prodQ' K P c)) (bools L)) (K.mul a (sumK K (prodQ' K P) (bools L)))
      = K.mul (K.mul a (sumK K (fun c => K.mul (mono K nW c) (prodQ' K P c)) (bools L))) (sumK K (prodQ' K P) (bools L)) := by
    rw [swap3 K, ← K.mul_assoc]
  have e2 : K.mul (sumK K (fun c => K.mul (mono K nW c) (prodQ' K P c)) (bools L))
      (K.mul b (sumK K (fun c => K.mul (mono K q c) (prodQ' K P c)) (bools L)))
      = K.mul (K.mul (sumK K (fun c => K.mul (mono K nW c) (prodQ' K P c)) (bools L))
          (sumK K (fun c => K.mul (mono K q c) (prodQ' K P c)) (bools L))) b := by
    rw [swap3 K, K.mul_comm b]
  have e3 : K.mul (K.mul b (sumK K (fun c => K.mul (K.mul (mono K nW c) (mono K q c)) (prodQ' K P c)) (bools L)))
      (sumK K (prodQ' K P) (bools L))
      = K.mul (K.mul (sumK K (fun c => K.mul (K.mul (mono K nW c) (mono K q c)) (prodQ' K P c)) (bools L))
          (sumK K (prodQ' K P) (bools L))) b := by
    rw [K.mul_assoc, K.mul_comm b]
  rw [e1, e2, e3]
  exact O.add_le_add _ _ _ _ (O.le_refl _) hb'

end GKS2

#print axioms GKS2.sgn_bx
#print axioms GKS2.pwR_mul
#print axioms GKS2.mono_zx
#print axioms GKS2.sgn_sq
#print axioms GKS2.mono_sq
#print axioms GKS2.mono_pm
#print axioms GKS2.prodQ'_double
#print axioms GKS2.nn_add
#print axioms GKS2.dbl_nn
#print axioms GKS2.gks2
#print axioms GKS2.dsum
#print axioms GKS2.monotone_plaquette

ym/PlaqLower.lean

/-
  ym/PlaqLower — T45, cycle 23 (ℤ₂, any dimension, any finite lattice): ⟨U_p⟩ ≥ tanh β.

  For a plaquette p (a monomial with some odd entry) and any further plaquettes Q, with weights 1 + t U_q, 0 ≤ t ≤ 1,
  in an ordered ring with 1 ≠ 0:
    one_le_two_pow, Z_ge_one   Z ≥ 2^L ≥ 1, so Z ≠ 0
    base                       t · Z([p]) = N_p([p])            (exact: ⟨U_p⟩ = t when p is the only plaquette)
    step                       adding a plaquette keeps t · Z ≤ N_p  (GKS II, GKS2.monotone_plaquette)
    plaquette_lower            t · Z(Q ++ [p]) ≤ N_p(Q ++ [p]),  i.e. ⟨U_p⟩ ≥ t
  The bound is uniform in the volume and the dimension: the strong-coupling series of ⟨U_p⟩ starts at t and every
  further plaquette can only raise it. With WilsonBound: t ≤ ⟨U_p⟩ ≤ 1.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.WilsonBound
import ym.GKS2

namespace PlaqLower

open YMChain LinkIntegral GKS StrongZ2 WilsonBound

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

def mapE (t : R) (P : List (List Nat)) : List (List Nat × R × R) := P.map (fun q => (q, K.one, t))

theorem prodQ'_mapE (t : R) (c : List Bool) : ∀ P : List (List Nat), prodQ' K (mapE K t P) c = prodQ K K.one t P c
  | [] => rfl
  | q :: P => by
    show K.mul (K.add K.one (K.mul t (mono K q c))) (prodQ' K (mapE K t P) c) = _
    rw [prodQ'_mapE t c P]
    rfl

theorem mem_mapE (t : R) (x : List Nat × R × R) : ∀ P : List (List Nat), memL x (mapE K t P) →
    ∃ q, memL q P ∧ x = (q, K.one, t)
  | [], h => False.elim h
  | q :: P, h => by
    cases h with
    | inl e => exact ⟨q, Or.inl rfl, e⟩
    | inr e =>
      match mem_mapE t x P e with
      | ⟨r, hr, er⟩ => exact ⟨r, Or.inr hr, er⟩

theorem two_ge_one (h1 : O.le K.zero K.one) : O.le K.one (two K) := by
  have := O.add_le_add _ _ _ _ (O.le_refl K.one) h1
  rw [DetChain.add_zero'] at this
  exact this

theorem one_le_mul (h1 : O.le K.zero K.one) (x y : R) (hx : O.le K.one x) (hy : O.le K.one y) :
    O.le K.one (K.mul x y) := by
  have hy0 : O.le K.zero y := O.le_trans _ _ _ h1 hy
  have := O.mul_le_mul_right _ _ _ hx hy0
  rw [K.one_mul] at this
  exact O.le_trans _ _ _ hy this

theorem one_le_two_pow (h1 : O.le K.zero K.one) : ∀ L, O.le K.one (pwR K (two K) L)
  | 0 => O.le_refl _
  | L + 1 => one_le_mul K O h1 _ _ (one_le_two_pow h1 L) (two_ge_one K O h1)

theorem Z_ge_one (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) (L : Nat) (P : List (List Nat))
    (hP : ∀ q, memL q P → q.length = L) : O.le K.one (N K t L (zl L) P) := by
  have e0 := N_eq K t P (zl L) (fun q hq => (hP q hq).trans (len_zl L).symm)
  rw [len_zl] at e0
  rw [e0]
  have hG0 : O.le K.one (G K t (zl L) P) := by
    have := G_ge_nil K O h1 t ht P (zl L)
    have e1 : G K t (zl L) [] = K.one := by
      show (if evenAll (zl L) = true then K.one else K.zero) = K.one
      rw [even_zl]; rfl
    rw [e1] at this
    exact this
  exact one_le_mul K O h1 _ _ (one_le_two_pow K O h1 L) hG0

theorem parity_double : ∀ a, parity (a + a) = true
  | 0 => rfl
  | a + 1 => by
    show parity (Nat.succ (a + 1 + a)) = true
    rw [Nat.succ_add]
    show (!(!(parity (a + a)))) = true
    rw [parity_double a]
    rfl

theorem even_double : ∀ p : List Nat, evenAll (zadd p p) = true
  | [] => rfl
  | a :: p => by
    show (parity (a + a) && evenAll (zadd p p)) = true
    rw [parity_double, even_double p]
    rfl

theorem zadd_zl_left : ∀ p : List Nat, zadd (zl p.length) p = p
  | [] => rfl
  | a :: p => by
    show (0 + a) :: zadd (zl p.length) p = a :: p
    rw [Nat.zero_add, zadd_zl_left p]

/-- **Base**: with p the only plaquette, `t · Z = N_p`. -/
theorem base (t : R) (p : List Nat) (hodd : evenAll p = false) :
    K.mul t (N K t p.length (zl p.length) [p]) = N K t p.length p [p] := by
  have eN := N_eq K t [p] p (fun q hq => by
    cases hq with
    | inl e => rw [e]
    | inr e => exact False.elim e)
  have eZ := N_eq K t [p] (zl p.length) (fun q hq => by
    cases hq with
    | inl e => rw [e, len_zl]
    | inr e => exact False.elim e)
  rw [len_zl] at eZ
  rw [eN, eZ]
  show K.mul t (K.mul (pwR K (two K) p.length) (K.add (G K t (zl p.length) []) (K.mul t (G K t (zadd (zl p.length) p) []))))
    = K.mul (pwR K (two K) p.length) (K.add (G K t p []) (K.mul t (G K t (zadd p p) [])))
  rw [zadd_zl_left]
  show K.mul t (K.mul (pwR K (two K) p.length) (K.add (if evenAll (zl p.length) = true then K.one else K.zero)
      (K.mul t (if evenAll p = true then K.one else K.zero))))
    = K.mul (pwR K (two K) p.length) (K.add (if evenAll p = true then K.one else K.zero)
      (K.mul t (if evenAll (zadd p p) = true then K.one else K.zero)))
  rw [even_zl, hodd, even_double]
  show K.mul t (K.mul (pwR K (two K) p.length) (K.add K.one (K.mul t K.zero)))
    = K.mul (pwR K (two K) p.length) (K.add K.zero (K.mul t K.one))
  rw [DetChain.mul_zero, DetChain.add_zero', K.zero_add, K.mul_comm t K.one, K.one_mul, K.mul_comm (pwR K (two K) p.length) K.one,
    K.one_mul, K.mul_comm]

/-- **Step**: GKS II keeps `t Z ≤ N_p` when a plaquette is added. -/
theorem step (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one)
    (L : Nat) (p q : List Nat) (hp : p.length = L) (hq : q.length = L) (P : List (List Nat))
    (hP : ∀ r, memL r P → r.length = L)
    (ih : O.le (K.mul t (N K t L (zl L) P)) (N K t L p P)) :
    O.le (K.mul t (N K t L (zl L) (q :: P))) (N K t L p (q :: P)) := by
  have hPE : ∀ x, memL x (mapE K t P) → x.1.length = L ∧ O.le K.zero x.2.2 ∧ O.le x.2.2 x.2.1 := fun x hx =>
    match mem_mapE K t x P hx with
    | ⟨r, hr, er⟩ => by rw [er]; exact ⟨hP r hr, ht0, ht1⟩
  have m := GKS2.monotone_plaquette K O h1 L (mapE K t P) hPE p q hp hq K.one t ht0
  -- identify the four sums
  have eN : sumK K (fun c => K.mul (mono K p c) (prodQ' K (mapE K t P) c)) (bools L) = N K t L p P :=
    sumK_ext K _ _ (fun c => by rw [prodQ'_mapE]) _
  have eZ : sumK K (prodQ' K (mapE K t P)) (bools L) = N K t L (zl L) P :=
    sumK_ext K _ _ (fun c => by rw [prodQ'_mapE, mono_zl K L c, K.one_mul]) _
  have eN' : sumK K (fun c => K.mul (K.mul (mono K p c) (prodQ' K (mapE K t P) c)) (K.add K.one (K.mul t (mono K q c)))) (bools L)
      = N K t L p (q :: P) :=
    sumK_ext K _ _ (fun c => by
      rw [prodQ'_mapE, K.mul_assoc, K.mul_comm (prodQ K K.one t P c)]; rfl) _
  have eZ' : sumK K (fun c => K.mul (prodQ' K (mapE K t P) c) (K.add K.one (K.mul t (mono K q c)))) (bools L)
      = N K t L (zl L) (q :: P) :=
    sumK_ext K _ _ (fun c => by
      rw [prodQ'_mapE, mono_zl K L c, K.one_mul, K.mul_comm]; rfl) _
  rw [eN, eZ, eN', eZ'] at m
  -- t Z Z' ≤ N Z' ≤ N' Z
  have Z1 := Z_ge_one K O h1 t ht0 L P hP
  have Z1' := Z_ge_one K O h1 t ht0 L (q :: P) (fun r hr => by
    cases hr with
    | inl e => rw [e]; exact hq
    | inr e => exact hP r e)
  have Z0 : O.le K.zero (N K t L (zl L) P) := O.le_trans _ _ _ h1 Z1
  have Z0' : O.le K.zero (N K t L (zl L) (q :: P)) := O.le_trans _ _ _ h1 Z1'
  have a1 := O.mul_le_mul_right _ _ _ ih Z0'
  have a2 := O.le_trans _ _ _ a1 m
  have e : K.mul (K.mul t (N K t L (zl L) P)) (N K t L (zl L) (q :: P))
      = K.mul (K.mul t (N K t L (zl L) (q :: P))) (N K t L (zl L) P) := by
    rw [K.mul_assoc, K.mul_comm (N K t L (zl L) P), ← K.mul_assoc]
  rw [e] at a2
  have Zne : N K t L (zl L) P ≠ K.zero := fun hz => by
    rw [hz] at Z1
    exact h10 (O.le_antisymm _ _ Z1 h1)
  exact O.pos_cancel _ _ _ Z0 Zne a2

/-- **⟨U_p⟩ ≥ tanh β** on every finite ℤ₂ lattice in every dimension. -/
theorem plaquette_lower (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht1 : O.le t K.one) (p : List Nat) (hodd : evenAll p = false) :
    ∀ Q : List (List Nat), (∀ r, memL r Q → r.length = p.length) →
      O.le (K.mul t (N K t p.length (zl p.length) (Q ++ [p]))) (N K t p.length p (Q ++ [p]))
  | [], _ => by
    show O.le (K.mul t (N K t p.length (zl p.length) [p])) (N K t p.length p [p])
    rw [base K t p hodd]
    exact O.le_refl _
  | q :: Q, hQ =>
    step K O h1 h10 t ht0 ht1 p.length p q rfl (hQ q (Or.inl rfl)) (Q ++ [p])
      (fun r hr => mem_app_len r Q hr (fun r' hr' => hQ r' (Or.inr hr')))
      (plaquette_lower h1 h10 t ht0 ht1 p hodd Q (fun r hr => hQ r (Or.inr hr)))
where
  mem_app_len (r : List Nat) : ∀ Q : List (List Nat), memL r (Q ++ [p]) → (∀ r', memL r' Q → r'.length = p.length) →
      r.length = p.length
    | [], h, _ => by
      cases h with
      | inl e => rw [e]
      | inr e => exact False.elim e
    | r0 :: Q, h, hQ => by
      cases h with
      | inl e => rw [e]; exact hQ r0 (Or.inl rfl)
      | inr e => exact mem_app_len r Q e (fun r' hr' => hQ r' (Or.inr hr'))

end PlaqLower

#print axioms PlaqLower.prodQ'_mapE
#print axioms PlaqLower.mem_mapE
#print axioms PlaqLower.two_ge_one
#print axioms PlaqLower.one_le_mul
#print axioms PlaqLower.one_le_two_pow
#print axioms PlaqLower.Z_ge_one
#print axioms PlaqLower.parity_double
#print axioms PlaqLower.even_double
#print axioms PlaqLower.zadd_zl_left
#print axioms PlaqLower.base
#print axioms PlaqLower.step
#print axioms PlaqLower.plaquette_lower

ym/AreaLower.lean

/-
  ym/AreaLower — T45, cycle 24 (ℤ₂, any dimension, any finite lattice): the area lower bound ⟨W_S⟩ ≥ (tanh β)^|S|.

  For a list S of plaquettes (each of length L) let W = Σ_S q (the loop bounding S, entries mod 2) and t ∈ [0, 1].
    PE, PE_zadd, evenAll_PE, G_congr   G n P depends on n only through the parities of its entries
    PE_double                          n + q + q has the parities of n
    area_G                             t^|S| · G n S ≤ G (n + Σ_S q) S
    area_base                          t^|S| · Z(S) ≤ N_W(S)
    step_c                             GKS II: adding a plaquette keeps c · Z ≤ N_W
    area_lower                         t^|S| · Z(Q ++ S) ≤ N_W(Q ++ S),  i.e. ⟨W⟩ ≥ t^|S| for every lattice Q ++ S
  So every Wilson loop that bounds a surface of |S| plaquettes has ⟨W⟩ ≥ (tanh β)^|S|, uniformly in the volume and the
  dimension — the lower half of the strong-coupling area law (the upper half is S2c).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.PlaqLower
import ym.Decouple

namespace AreaLower

open YMChain LinkIntegral GKS StrongZ2 WilsonBound PlaqLower Decouple

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

def PE : List Nat → List Nat → Prop
  | [], [] => True
  | a :: n, b :: m => parity a = parity b ∧ PE n m
  | [], _ :: _ => False
  | _ :: _, [] => False

theorem PE_refl : ∀ n : List Nat, PE n n
  | [] => trivial
  | _ :: n => ⟨rfl, PE_refl n⟩

theorem parity_congr (a b : Nat) (h : parity a = parity b) : ∀ c, parity (a + c) = parity (b + c)
  | 0 => h
  | c + 1 => by
    show (!(parity (a + c))) = (!(parity (b + c)))
    rw [parity_congr a b h c]

theorem PE_zadd : ∀ (n n' q : List Nat), PE n n' → PE (zadd n q) (zadd n' q)
  | [], [], q, _ => PE_refl q
  | _ :: _, _ :: _, [], h => h
  | a :: n, b :: n', c :: q, h => ⟨parity_congr a b h.1 c, PE_zadd n n' q h.2⟩
  | [], _ :: _, _, h => False.elim h
  | _ :: _, [], _, h => False.elim h

theorem evenAll_PE : ∀ (n n' : List Nat), PE n n' → evenAll n = evenAll n'
  | [], [], _ => rfl
  | a :: n, b :: n', h => by
    show (parity a && evenAll n) = (parity b && evenAll n')
    rw [h.1, evenAll_PE n n' h.2]
  | [], _ :: _, h => False.elim h
  | _ :: _, [], h => False.elim h

theorem G_congr (t : R) : ∀ (P : List (List Nat)) (n n' : List Nat), PE n n' → G K t n P = G K t n' P
  | [], n, n', h => by
    show (if evenAll n = true then K.one else K.zero) = (if evenAll n' = true then K.one else K.zero)
    rw [evenAll_PE n n' h]
  | q :: P, n, n', h => by
    show K.add (G K t n P) (K.mul t (G K t (zadd n q) P)) = K.add (G K t n' P) (K.mul t (G K t (zadd n' q) P))
    rw [G_congr t P n n' h, G_congr t P (zadd n q) (zadd n' q) (PE_zadd n n' q h)]

theorem parity_double (a : Nat) : ∀ c, parity (a + c + c) = parity a
  | 0 => rfl
  | c + 1 => by
    show parity (Nat.succ (Nat.succ (a + c) + c)) = parity a
    rw [Nat.succ_add]
    show (!(!(parity (a + c + c)))) = parity a
    rw [parity_double a c]
    cases parity a <;> rfl

theorem PE_double : ∀ n q : List Nat, q.length ≤ n.length → PE (zadd (zadd n q) q) n
  | [], [], _ => trivial
  | [], _ :: _, h => absurd h (Nat.not_succ_le_zero _)
  | _ :: _, [], _ => PE_refl _
  | a :: n, c :: q, h => ⟨parity_double a c, PE_double n q (Nat.le_of_succ_le_succ h)⟩

def sumL : List (List Nat) → List Nat
  | [] => []
  | q :: S => zadd q (sumL S)

theorem zadd_comm : ∀ n m : List Nat, zadd n m = zadd m n
  | [], [] => rfl
  | [], _ :: _ => rfl
  | _ :: _, [] => rfl
  | a :: n, b :: m => by
    show (a + b) :: zadd n m = (b + a) :: zadd m n
    rw [Nat.add_comm a b, zadd_comm n m]

theorem zadd_nil : ∀ n : List Nat, zadd n [] = n
  | [] => rfl
  | _ :: _ => rfl

theorem len_zadd_le : ∀ n m : List Nat, m.length ≤ n.length → (zadd n m).length = n.length
  | [], [], _ => rfl
  | [], _ :: _, h => absurd h (Nat.not_succ_le_zero _)
  | _ :: _, [], _ => rfl
  | _ :: n, _ :: m, h => congrArg Nat.succ (len_zadd_le n m (Nat.le_of_succ_le_succ h))

theorem len_sumL (L : Nat) : ∀ S : List (List Nat), (∀ q, memL q S → q.length = L) → (sumL S).length ≤ L
  | [], _ => Nat.zero_le _
  | q :: S, hS => by
    show (zadd q (sumL S)).length ≤ L
    rw [len_zadd_le q (sumL S) (by rw [hS q (Or.inl rfl)]; exact len_sumL L S (fun r hr => hS r (Or.inr hr))),
      hS q (Or.inl rfl)]
    exact Nat.le_refl L

theorem pw_nn (t : R) (ht : O.le K.zero t) (h1 : O.le K.zero K.one) : ∀ s, O.le K.zero (pwR K t s)
  | 0 => h1
  | s + 1 => O.mul_nonneg _ _ (pw_nn t ht h1 s) ht

theorem tt_le_one (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one) : O.le (K.mul t t) K.one := by
  have := O.mul_le_mul_right _ _ _ ht1 ht0
  rw [K.one_mul] at this
  exact O.le_trans _ _ _ this ht1

/-- **Area bound for surface sums.** `t^|S| · G n S ≤ G (n + Σ_S q) S`. -/
theorem area_G (h1 : O.le K.zero K.one) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one) :
    ∀ (S : List (List Nat)) (n : List Nat), (∀ q, memL q S → q.length = n.length) →
      O.le (K.mul (pwR K t S.length) (G K t n S)) (G K t (zadd n (sumL S)) S)
  | [], n, _ => by
    show O.le (K.mul K.one (G K t n [])) (G K t (zadd n []) [])
    rw [K.one_mul, zadd_nil]
    exact O.le_refl _
  | q :: S, n, hS => by
    have hq : q.length = n.length := hS q (Or.inl rfl)
    have hS' : ∀ r, memL r S → r.length = n.length := fun r hr => hS r (Or.inr hr)
    have hnq : (zadd n q).length = n.length := len_zadd_le n q (by rw [hq]; exact Nat.le_refl _)
    have hsum : (sumL S).length ≤ n.length := len_sumL n.length S hS'
    have ih1 := area_G h1 t ht0 ht1 S (zadd n q) (fun r hr => (hS' r hr).trans hnq.symm)
    have ih2 := area_G h1 t ht0 ht1 S n hS'
    rw [zadd_assoc] at ih1
    -- G (X + q) S = G (n + Σ) S
    have hX : G K t (zadd (zadd n (zadd q (sumL S))) q) S = G K t (zadd n (sumL S)) S := by
      rw [zadd_comm q (sumL S), ← zadd_assoc]
      apply G_congr
      exact PE_double (zadd n (sumL S)) q (by rw [len_zadd_le n (sumL S) hsum, hq]; exact Nat.le_refl _)
    show O.le (K.mul (K.mul (pwR K t S.length) t) (K.add (G K t n S) (K.mul t (G K t (zadd n q) S))))
      (K.add (G K t (zadd n (zadd q (sumL S))) S) (K.mul t (G K t (zadd (zadd n (zadd q (sumL S))) q) S)))
    rw [hX, K.distrib, K.add_comm (G K t (zadd n (zadd q (sumL S))) S)]
    apply O.add_le_add
    · have := O.mul_le_mul_right _ _ _ ih2 ht0
      have e1 : K.mul (K.mul (pwR K t S.length) t) (G K t n S) = K.mul (K.mul (pwR K t S.length) (G K t n S)) t := by
        rw [K.mul_assoc, K.mul_comm t, ← K.mul_assoc]
      rw [e1, K.mul_comm t]
      exact this
    · have e2 : K.mul (K.mul (pwR K t S.length) t) (K.mul t (G K t (zadd n q) S))
          = K.mul (K.mul t t) (K.mul (pwR K t S.length) (G K t (zadd n q) S)) := by
        rw [K.mul_assoc, ← K.mul_assoc t t, swap3 K]
      rw [e2]
      have hnn : O.le K.zero (K.mul (pwR K t S.length) (G K t (zadd n q) S)) :=
        O.mul_nonneg _ _ (pw_nn K O t ht0 h1 _) (G_nn K O h1 t ht0 S _)
      have a := O.mul_le_mul_right _ _ _ (tt_le_one K O t ht0 ht1) hnn
      rw [K.one_mul] at a
      exact O.le_trans _ _ _ a ih1

theorem area_base (h1 : O.le K.zero K.one) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one) (L : Nat)
    (S : List (List Nat)) (hS : ∀ q, memL q S → q.length = L) :
    O.le (K.mul (pwR K t S.length) (N K t L (zl L) S)) (N K t L (zadd (zl L) (sumL S)) S) := by
  have hW : (zadd (zl L) (sumL S)).length = L := by
    rw [len_zadd_le (zl L) (sumL S) (by rw [len_zl]; exact len_sumL L S hS), len_zl]
  have eZ := N_eq K t S (zl L) (fun q hq => (hS q hq).trans (len_zl L).symm)
  have eW := N_eq K t S (zadd (zl L) (sumL S)) (fun q hq => (hS q hq).trans hW.symm)
  rw [len_zl] at eZ
  rw [hW] at eW
  rw [eZ, eW]
  have a := area_G K O h1 t ht0 ht1 S (zl L) (fun q hq => (hS q hq).trans (len_zl L).symm)
  have b := O.mul_le_mul_right _ _ _ a (two_pow_nn K O h1 L)
  rw [swap3 K, K.mul_comm (pwR K (two K) L) (K.mul (pwR K t S.length) (G K t (zl L) S)),
    K.mul_comm (pwR K (two K) L)]
  exact b

theorem step_c (c : R) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one)
    (L : Nat) (p q : List Nat) (hp : p.length = L) (hq : q.length = L) (P : List (List Nat))
    (hP : ∀ r, memL r P → r.length = L)
    (ih : O.le (K.mul c (N K t L (zl L) P)) (N K t L p P)) :
    O.le (K.mul c (N K t L (zl L) (q :: P))) (N K t L p (q :: P)) := by
  have hPE : ∀ x, memL x (mapE K t P) → x.1.length = L ∧ O.le K.zero x.2.2 ∧ O.le x.2.2 x.2.1 := fun x hx =>
    match mem_mapE K t x P hx with
    | ⟨r, hr, er⟩ => by rw [er]; exact ⟨hP r hr, ht0, ht1⟩
  have m := GKS2.monotone_plaquette K O h1 L (mapE K t P) hPE p q hp hq K.one t ht0
  -- identify the four sums
  have eN : sumK K (fun c => K.mul (mono K p c) (prodQ' K (mapE K t P) c)) (bools L) = N K t L p P :=
    sumK_ext K _ _ (fun c => by rw [prodQ'_mapE]) _
  have eZ : sumK K (prodQ' K (mapE K t P)) (bools L) = N K t L (zl L) P :=
    sumK_ext K _ _ (fun c => by rw [prodQ'_mapE, mono_zl K L c, K.one_mul]) _
  have eN' : sumK K (fun c => K.mul (K.mul (mono K p c) (prodQ' K (mapE K t P) c)) (K.add K.one (K.mul t (mono K q c)))) (bools L)
      = N K t L p (q :: P) :=
    sumK_ext K _ _ (fun c => by
      rw [prodQ'_mapE, K.mul_assoc, K.mul_comm (prodQ K K.one t P c)]; rfl) _
  have eZ' : sumK K (fun c => K.mul (prodQ' K (mapE K t P) c) (K.add K.one (K.mul t (mono K q c)))) (bools L)
      = N K t L (zl L) (q :: P) :=
    sumK_ext K _ _ (fun c => by
      rw [prodQ'_mapE, mono_zl K L c, K.one_mul, K.mul_comm]; rfl) _
  rw [eN, eZ, eN', eZ'] at m
  -- t Z Z' ≤ N Z' ≤ N' Z
  have Z1 := Z_ge_one K O h1 t ht0 L P hP
  have Z1' := Z_ge_one K O h1 t ht0 L (q :: P) (fun r hr => by
    cases hr with
    | inl e => rw [e]; exact hq
    | inr e => exact hP r e)
  have Z0 : O.le K.zero (N K t L (zl L) P) := O.le_trans _ _ _ h1 Z1
  have Z0' : O.le K.zero (N K t L (zl L) (q :: P)) := O.le_trans _ _ _ h1 Z1'
  have a1 := O.mul_le_mul_right _ _ _ ih Z0'
  have a2 := O.le_trans _ _ _ a1 m
  have e : K.mul (K.mul c (N K t L (zl L) P)) (N K t L (zl L) (q :: P))
      = K.mul (K.mul c (N K t L (zl L) (q :: P))) (N K t L (zl L) P) := by
    rw [K.mul_assoc, K.mul_comm (N K t L (zl L) P), ← K.mul_assoc]
  rw [e] at a2
  have Zne : N K t L (zl L) P ≠ K.zero := fun hz => by
    rw [hz] at Z1
    exact h10 (O.le_antisymm _ _ Z1 h1)
  exact O.pos_cancel _ _ _ Z0 Zne a2

theorem mem_app' {A : Type} (r : A) : ∀ Q S : List A, memL r (Q ++ S) → memL r Q ∨ memL r S
  | [], _, h => Or.inr h
  | _ :: Q, S, h => by
    cases h with
    | inl e => exact Or.inl (Or.inl e)
    | inr e =>
      cases mem_app' r Q S e with
      | inl h' => exact Or.inl (Or.inr h')
      | inr h' => exact Or.inr h'

/-- **Area lower bound**: `⟨W⟩ ≥ t^|S|` for the loop W bounding S, on every lattice Q ++ S. -/
theorem area_lower (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht1 : O.le t K.one) (L : Nat) (S : List (List Nat)) (hS : ∀ q, memL q S → q.length = L) :
    ∀ Q : List (List Nat), (∀ q, memL q Q → q.length = L) →
      O.le (K.mul (pwR K t S.length) (N K t L (zl L) (Q ++ S))) (N K t L (zadd (zl L) (sumL S)) (Q ++ S))
  | [], _ => area_base K O h1 t ht0 ht1 L S hS
  | q :: Q, hQ =>
    step_c K O (pwR K t S.length) h1 h10 t ht0 ht1 L (zadd (zl L) (sumL S)) q
      (by rw [len_zadd_le (zl L) (sumL S) (by rw [len_zl]; exact len_sumL L S hS), len_zl])
      (hQ q (Or.inl rfl)) (Q ++ S)
      (fun r hr => match mem_app' r Q S hr with
        | Or.inl h => hQ r (Or.inr h)
        | Or.inr h => hS r h)
      (area_lower h1 h10 t ht0 ht1 L S hS Q (fun r hr => hQ r (Or.inr hr)))

end AreaLower

#print axioms AreaLower.PE_refl
#print axioms AreaLower.parity_congr
#print axioms AreaLower.PE_zadd
#print axioms AreaLower.evenAll_PE
#print axioms AreaLower.G_congr
#print axioms AreaLower.parity_double
#print axioms AreaLower.PE_double
#print axioms AreaLower.zadd_comm
#print axioms AreaLower.zadd_nil
#print axioms AreaLower.len_zadd_le
#print axioms AreaLower.len_sumL
#print axioms AreaLower.pw_nn
#print axioms AreaLower.tt_le_one
#print axioms AreaLower.area_G
#print axioms AreaLower.area_base
#print axioms AreaLower.step_c
#print axioms AreaLower.mem_app'
#print axioms AreaLower.area_lower

ym/Monotone.lean

/-
  ym/Monotone — T45, cycle 25 (ℤ₂, any dimension, any finite lattice): ⟨W⟩ is nondecreasing in each plaquette coupling.

  Weights 1 + t_q U_q with 0 ≤ t_q ≤ 1. Raising one coupling t → t' (t ≤ t' ≤ 1, t² ≠ 1) multiplies the weight of that
  plaquette by a + b U_q with a = 1 − t t', b = t' − t ≥ 0:
    expand        (1 + tU)(a + bU) = (a + tb) + (b + ta)U         (U² = 1)
    coeff0/1      a + tb = 1 − t²,  b + ta = (1 − t²) t'
    reweight      (1 + tU)(a + bU) = (1 − t²)(1 + t'U)
    coupling_monotone   N_W(t) · Z(t') ≤ N_W(t') · Z(t),  i.e. ⟨W⟩_t ≤ ⟨W⟩_t'   (GKS II, GKS2.monotone_plaquette)
  With PlaqLower/AreaLower: every Wilson loop expectation is a nondecreasing function of β = artanh t on every finite
  lattice; the infinite-volume limits along increasing β exist by monotonicity once the volume limit is taken.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AreaLower

namespace Monotone

open YMChain LinkIntegral GKS StrongZ2 WilsonBound PlaqLower

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem expand (t a b U : R) (hU : K.mul U U = K.one) :
    K.mul (K.add K.one (K.mul t U)) (K.add a (K.mul b U))
      = K.add (K.add a (K.mul t b)) (K.mul (K.add b (K.mul t a)) U) := by
  rw [DetChain.right_distrib, K.one_mul, K.distrib (K.mul t U) a (K.mul b U)]
  have h1 : K.mul (K.mul t U) a = K.mul (K.mul t a) U := by
    rw [K.mul_assoc, K.mul_comm U a, ← K.mul_assoc]
  have h2 : K.mul (K.mul t U) (K.mul b U) = K.mul t b := by
    rw [K.mul_assoc, swap3 K U b U, hU, K.mul_comm b K.one, K.one_mul]
  rw [h1, h2, DetChain.right_distrib K b (K.mul t a) U, K.add_comm (K.mul (K.mul t a) U) (K.mul t b), DetChain.add_mid4]

theorem coeff0 (t t' : R) :
    K.add (K.add K.one (K.neg (K.mul t t'))) (K.mul t (K.add t' (K.neg t))) = K.add K.one (K.neg (K.mul t t)) := by
  rw [K.distrib, mul_neg_r K, K.add_assoc, ← K.add_assoc (K.neg (K.mul t t')) (K.mul t t'), DetChain.add_neg', K.zero_add]

theorem coeff1 (t t' : R) :
    K.add (K.add t' (K.neg t)) (K.mul t (K.add K.one (K.neg (K.mul t t'))))
      = K.mul (K.add K.one (K.neg (K.mul t t))) t' := by
  rw [K.distrib, mul_neg_r K, K.mul_comm t K.one, K.one_mul, K.add_assoc, ← K.add_assoc (K.neg t) t,
    DetChain.add_neg', K.zero_add, DetChain.right_distrib, K.one_mul, DetChain.neg_mul_left, K.mul_assoc]

theorem reweight (t t' U : R) (hU : K.mul U U = K.one) :
    K.mul (K.add K.one (K.mul t U)) (K.add (K.add K.one (K.neg (K.mul t t'))) (K.mul (K.add t' (K.neg t)) U))
      = K.mul (K.add K.one (K.neg (K.mul t t))) (K.add K.one (K.mul t' U)) := by
  rw [expand K t _ _ U hU, coeff0, coeff1, K.distrib (K.add K.one (K.neg (K.mul t t))) K.one (K.mul t' U),
    K.mul_comm (K.add K.one (K.neg (K.mul t t))) K.one, K.one_mul, K.mul_assoc]

theorem mono_sq (n : List Nat) (c : List Bool) : K.mul (mono K n c) (mono K n c) = K.one := by
  cases mono_pm K n c with
  | inl h => rw [h, K.one_mul]
  | inr h => rw [h]; exact negone_sq K

/-- **⟨W⟩ is nondecreasing in a plaquette coupling.** -/
theorem coupling_monotone (h1 : O.le K.zero K.one) (L : Nat) (P : List (List Nat × R × R))
    (hP : ∀ x, memL x P → x.1.length = L ∧ O.le K.zero x.2.2 ∧ O.le x.2.2 x.2.1)
    (nW q : List Nat) (hW : nW.length = L) (hq : q.length = L) (t t' : R) (ht0 : O.le K.zero t)
    (ht1 : O.le t K.one) (htt : O.le t t') (hc : K.add K.one (K.neg (K.mul t t)) ≠ K.zero) :
    O.le (K.mul (sumK K (fun c => K.mul (mono K nW c) (prodQ' K ((q, K.one, t) :: P) c)) (bools L))
                (sumK K (prodQ' K ((q, K.one, t') :: P)) (bools L)))
         (K.mul (sumK K (fun c => K.mul (mono K nW c) (prodQ' K ((q, K.one, t') :: P) c)) (bools L))
                (sumK K (prodQ' K ((q, K.one, t) :: P)) (bools L))) := by
  let c0 := K.add K.one (K.neg (K.mul t t))
  have hP0 : ∀ x, memL x ((q, K.one, t) :: P) → x.1.length = L ∧ O.le K.zero x.2.2 ∧ O.le x.2.2 x.2.1 :=
    fun x hx => by
      cases hx with
      | inl e => rw [e]; exact ⟨hq, ht0, ht1⟩
      | inr e => exact hP x e
  have hb : O.le K.zero (K.add t' (K.neg t)) := by
    have := O.add_le_add _ _ _ _ htt (O.le_refl (K.neg t))
    rw [K.add_neg] at this
    exact this
  have m := GKS2.monotone_plaquette K O h1 L ((q, K.one, t) :: P) hP0 nW q hW hq
    (K.add K.one (K.neg (K.mul t t'))) (K.add t' (K.neg t)) hb
  have pw : ∀ c, K.mul (prodQ' K ((q, K.one, t) :: P) c)
      (K.add (K.add K.one (K.neg (K.mul t t'))) (K.mul (K.add t' (K.neg t)) (mono K q c)))
      = K.mul c0 (prodQ' K ((q, K.one, t') :: P) c) := fun c => by
    show K.mul (K.mul (K.add K.one (K.mul t (mono K q c))) (prodQ' K P c)) _
      = K.mul c0 (K.mul (K.add K.one (K.mul t' (mono K q c))) (prodQ' K P c))
    rw [K.mul_assoc, K.mul_comm (prodQ' K P c), ← K.mul_assoc, reweight K t t' _ (mono_sq K q c), K.mul_assoc]
  have eZ : sumK K (fun c => K.mul (prodQ' K ((q, K.one, t) :: P) c)
      (K.add (K.add K.one (K.neg (K.mul t t'))) (K.mul (K.add t' (K.neg t)) (mono K q c)))) (bools L)
      = K.mul c0 (sumK K (prodQ' K ((q, K.one, t') :: P)) (bools L)) := by
    rw [mul_sumK]
    exact sumK_ext K _ _ pw _
  have eN : sumK K (fun c => K.mul (K.mul (mono K nW c) (prodQ' K ((q, K.one, t) :: P) c))
      (K.add (K.add K.one (K.neg (K.mul t t'))) (K.mul (K.add t' (K.neg t)) (mono K q c)))) (bools L)
      = K.mul c0 (sumK K (fun c => K.mul (mono K nW c) (prodQ' K ((q, K.one, t') :: P) c)) (bools L)) := by
    rw [mul_sumK]
    exact sumK_ext K _ _ (fun c => by rw [K.mul_assoc, pw c, swap3 K]) _
  rw [eZ, eN] at m
  have hc0 : O.le K.zero c0 := by
    have := O.add_le_add _ _ _ _ (AreaLower.tt_le_one K O t ht0 ht1) (O.le_refl (K.neg (K.mul t t)))
    rw [K.add_neg] at this
    exact this
  apply O.pos_cancel _ _ c0 hc0 hc
  have e1 : ∀ X Y : R, K.mul (K.mul X Y) c0 = K.mul X (K.mul c0 Y) := fun X Y => by
    rw [K.mul_assoc, K.mul_comm Y c0]
  rw [e1, e1]
  rw [K.mul_comm c0 (sumK K (fun c => K.mul (mono K nW c) (prodQ' K ((q, K.one, t') :: P) c)) (bools L))] at m
  rw [K.mul_assoc] at m
  exact m

end Monotone

#print axioms Monotone.expand
#print axioms Monotone.coeff0
#print axioms Monotone.coeff1
#print axioms Monotone.reweight
#print axioms Monotone.mono_sq
#print axioms Monotone.coupling_monotone

ym/VolumeMono.lean

/-
  ym/VolumeMono — T45, cycle 26 (ℤ₂, any dimension): ⟨W⟩ is nondecreasing in the volume (free boundary conditions).

  Weights 1 + t U_q, 0 ≤ t ≤ 1, 1 ≠ 0. A larger volume is a longer plaquette list Q ++ P on the same links (links
  not touched by any plaquette or by the loop factor out of N and Z alike).
    gks_step        N_W(P) Z(q :: P) ≤ N_W(q :: P) Z(P)          (GKS II in the N, Z notation of StrongZ2)
    volume_mono     N_W(P) Z(Q ++ P) ≤ N_W(Q ++ P) Z(P),  i.e. ⟨W⟩_P ≤ ⟨W⟩_{Q ++ P}
  With WilsonBound (⟨W⟩ ≤ 1) and AreaLower (⟨W⟩ ≥ t^|S|): along any increasing sequence of volumes the expectations
  form a nondecreasing sequence in [t^|S|, 1]; over the reals it converges (the infinite-volume limit with free
  boundary conditions), and the limit keeps both bounds.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AreaLower

namespace VolumeMono

open YMChain LinkIntegral GKS StrongZ2 WilsonBound PlaqLower

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem gks_step (h1 : O.le K.zero K.one) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one)
    (L : Nat) (p q : List Nat) (hp : p.length = L) (hq : q.length = L) (P : List (List Nat))
    (hP : ∀ r, memL r P → r.length = L) :
    O.le (K.mul (N K t L p P) (N K t L (zl L) (q :: P))) (K.mul (N K t L p (q :: P)) (N K t L (zl L) P)) := by
  have hPE : ∀ x, memL x (mapE K t P) → x.1.length = L ∧ O.le K.zero x.2.2 ∧ O.le x.2.2 x.2.1 := fun x hx =>
    match mem_mapE K t x P hx with
    | ⟨r, hr, er⟩ => by rw [er]; exact ⟨hP r hr, ht0, ht1⟩
  have m := GKS2.monotone_plaquette K O h1 L (mapE K t P) hPE p q hp hq K.one t ht0
  have eN : sumK K (fun c => K.mul (mono K p c) (prodQ' K (mapE K t P) c)) (bools L) = N K t L p P :=
    sumK_ext K _ _ (fun c => by rw [prodQ'_mapE]) _
  have eZ : sumK K (prodQ' K (mapE K t P)) (bools L) = N K t L (zl L) P :=
    sumK_ext K _ _ (fun c => by rw [prodQ'_mapE, mono_zl K L c, K.one_mul]) _
  have eN' : sumK K (fun c => K.mul (K.mul (mono K p c) (prodQ' K (mapE K t P) c)) (K.add K.one (K.mul t (mono K q c)))) (bools L)
      = N K t L p (q :: P) :=
    sumK_ext K _ _ (fun c => by
      rw [prodQ'_mapE, K.mul_assoc, K.mul_comm (prodQ K K.one t P c)]; rfl) _
  have eZ' : sumK K (fun c => K.mul (prodQ' K (mapE K t P) c) (K.add K.one (K.mul t (mono K q c)))) (bools L)
      = N K t L (zl L) (q :: P) :=
    sumK_ext K _ _ (fun c => by
      rw [prodQ'_mapE, mono_zl K L c, K.one_mul, K.mul_comm]; rfl) _
  rw [eN, eZ, eN', eZ'] at m
  exact m

theorem mem_app_len (L : Nat) (r : List Nat) : ∀ Q P : List (List Nat), memL r (Q ++ P) →
    (∀ q, memL q Q → q.length = L) → (∀ q, memL q P → q.length = L) → r.length = L
  | [], _, h, _, hP => hP r h
  | q :: Q, P, h, hQ, hP => by
    cases h with
    | inl e => rw [e]; exact hQ q (Or.inl rfl)
    | inr e => exact mem_app_len L r Q P e (fun q' hq' => hQ q' (Or.inr hq')) hP

/-- **Volume monotonicity.** `⟨W⟩_P ≤ ⟨W⟩_{Q ++ P}`. -/
theorem volume_mono (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht1 : O.le t K.one) (L : Nat) (nW : List Nat) (hW : nW.length = L) (P : List (List Nat))
    (hP : ∀ r, memL r P → r.length = L) :
    ∀ Q : List (List Nat), (∀ q, memL q Q → q.length = L) →
      O.le (K.mul (N K t L nW P) (N K t L (zl L) (Q ++ P))) (K.mul (N K t L nW (Q ++ P)) (N K t L (zl L) P))
  | [], _ => O.le_refl _
  | q :: Q, hQ => by
    have hQP : ∀ r, memL r (Q ++ P) → r.length = L := fun r hr =>
      mem_app_len L r Q P hr (fun q' hq' => hQ q' (Or.inr hq')) hP
    have ih := volume_mono h1 h10 t ht0 ht1 L nW hW P hP Q (fun q' hq' => hQ q' (Or.inr hq'))
    have st := gks_step K O h1 t ht0 ht1 L nW q hW (hQ q (Or.inl rfl)) (Q ++ P) hQP
    -- names
    have Z0 := O.le_trans _ _ _ h1 (Z_ge_one K O h1 t ht0 L P hP)
    have Z1' := Z_ge_one K O h1 t ht0 L (Q ++ P) hQP
    have Z1 := O.le_trans _ _ _ h1 Z1'
    have Z2 := O.le_trans _ _ _ h1 (Z_ge_one K O h1 t ht0 L (q :: (Q ++ P)) (fun r hr => by
      cases hr with
      | inl e => rw [e]; exact hQ q (Or.inl rfl)
      | inr e => exact hQP r e))
    -- N(P) Z(qQP) Z(QP) ≤ N(QP) Z(P) Z(qQP) ≤ N(qQP) Z(QP) Z(P)
    have a := O.mul_le_mul_right _ _ _ ih Z2
    have b := O.mul_le_mul_right _ _ _ st Z0
    have e1 : K.mul (K.mul (N K t L nW P) (N K t L (zl L) (Q ++ P))) (N K t L (zl L) (q :: (Q ++ P)))
        = K.mul (K.mul (N K t L nW P) (N K t L (zl L) (q :: (Q ++ P)))) (N K t L (zl L) (Q ++ P)) := by
      rw [K.mul_assoc, K.mul_comm (N K t L (zl L) (Q ++ P)), ← K.mul_assoc]
    have e2 : K.mul (K.mul (N K t L nW (Q ++ P)) (N K t L (zl L) P)) (N K t L (zl L) (q :: (Q ++ P)))
        = K.mul (K.mul (N K t L nW (Q ++ P)) (N K t L (zl L) (q :: (Q ++ P)))) (N K t L (zl L) P) := by
      rw [K.mul_assoc, K.mul_comm (N K t L (zl L) P), ← K.mul_assoc]
    have e3 : K.mul (K.mul (N K t L nW (q :: (Q ++ P))) (N K t L (zl L) (Q ++ P))) (N K t L (zl L) P)
        = K.mul (K.mul (N K t L nW (q :: (Q ++ P))) (N K t L (zl L) P)) (N K t L (zl L) (Q ++ P)) := by
      rw [K.mul_assoc, K.mul_comm (N K t L (zl L) (Q ++ P)), ← K.mul_assoc]
    rw [e1, e2] at a
    rw [e3] at b
    have c := O.le_trans _ _ _ a b
    have Zne : N K t L (zl L) (Q ++ P) ≠ K.zero := fun hz => by
      rw [hz] at Z1'
      exact h10 (O.le_antisymm _ _ Z1' h1)
    exact O.pos_cancel _ _ _ Z1 Zne c

end VolumeMono

#print axioms VolumeMono.gks_step
#print axioms VolumeMono.mem_app_len
#print axioms VolumeMono.volume_mono

ym/MaskSum.lean

/-
  ym/MaskSum — T45, cycle 28 (Lad1, ℤ₂, any dimension): sums over subsets as sums over masks, and the
  injection bound behind the connected-component factorisation S2c.

  A subset of a list of n plaquettes is a mask S ∈ bools n.
    sum_zero        Σ 0 = 0
    sel             Σ_{A ∈ bools n} [A = a] x = x                       (a of length n: each mask is listed once)
    orB, len_orB    pointwise union of masks
    inj_bound       if every mask S is the union of c S and d S (masks of length n), then for g, h ≥ 0
                      Σ_S g(c S) h(d S) ≤ (Σ_A g A) (Σ_B h B)
                    (S ↦ (c S, d S) is injective because S = c S ∪ d S)
    term, G_mask    the surface sum G n P of StrongZ2 is Σ_{S ∈ bools |P|} term n P S
  Next (cycle 29): c S = the plaquettes of S connected to the loop, d S = the rest; then
  G_W ≤ (Σ_{A connected to W, ∂A = W} t^|A|) · G_0, i.e. ⟨W⟩ ≤ Σ_{A connected to W} t^|A|, uniformly in the volume.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Decouple
import ym.ChangeVar2D

namespace MaskSum

open YMChain LinkIntegral GKS StrongZ2

variable {R : Type} (K : DetChain.CRing R)

theorem sum_zero {A : Type} : ∀ l : List A, sumK K (fun _ => K.zero) l = K.zero
  | [] => rfl
  | _ :: l => by show K.add K.zero (sumK K (fun _ => K.zero) l) = K.zero; rw [sum_zero l, K.zero_add]

theorem sumK_app {A : Type} (f : A → R) : ∀ l m : List A, sumK K f (l ++ m) = K.add (sumK K f l) (sumK K f m)
  | [], m => (K.zero_add _).symm
  | a :: l, m => by
    show K.add (f a) (sumK K f (l ++ m)) = K.add (K.add (f a) (sumK K f l)) (sumK K f m)
    rw [sumK_app f l m, K.add_assoc]

theorem sumK_map {A B : Type} (f : B → R) (g : A → B) : ∀ l : List A, sumK K f (l.map g) = sumK K (fun a => f (g a)) l
  | [] => rfl
  | a :: l => by
    show K.add (f (g a)) (sumK K f (l.map g)) = K.add (f (g a)) (sumK K (fun a => f (g a)) l)
    rw [sumK_map f g l]

theorem ip {α : Type} {c : Prop} [inst : Decidable c] (h : c) {a b : α} : (if c then a else b) = a := by
  cases inst with
  | isTrue _ => rfl
  | isFalse h' => exact absurd h h'
theorem ineg {α : Type} {c : Prop} [inst : Decidable c] (h : ¬c) {a b : α} : (if c then a else b) = b := by
  cases inst with
  | isTrue h' => exact absurd h' h
  | isFalse _ => rfl

theorem cons_eq_iff (b c : Bool) (x y : List Bool) : (b :: x = c :: y) ↔ (b = c ∧ x = y) :=
  ⟨fun h => ⟨List.head_eq_of_cons_eq h, List.tail_eq_of_cons_eq h⟩, fun h => by rw [h.1, h.2]⟩

/-- **Each mask is listed once.** -/
theorem sel (x : R) : ∀ (n : Nat) (a : List Bool), a.length = n →
    sumK K (fun A => if A = a then x else K.zero) (bools n) = x
  | 0, [], _ => by
    show K.add (if ([] : List Bool) = [] then x else K.zero) K.zero = x
    rw [ip rfl, DetChain.add_zero']
  | 0, _ :: _, h => absurd h Nat.noConfusion
  | _ + 1, [], h => absurd h Nat.noConfusion
  | n + 1, b :: a, h => by
    have ha : a.length = n := Nat.succ.inj h
    show sumK K (fun A => if A = b :: a then x else K.zero)
      ((bools n).map (List.cons true) ++ (bools n).map (List.cons false)) = x
    rw [sumK_app, sumK_map, sumK_map]
    cases b with
    | true =>
      have e1 : sumK K (fun A => if true :: A = true :: a then x else K.zero) (bools n) = x :=
        (sumK_ext K _ _ (fun A => by
          by_cases hA : A = a
          · rw [ip (by rw [hA]), ip hA]
          · rw [ineg (fun e => hA (List.tail_eq_of_cons_eq e)), ineg hA]) _).trans (sel x n a ha)
      have e2 : sumK K (fun A => if false :: A = true :: a then x else K.zero) (bools n) = K.zero :=
        (sumK_ext K _ _ (fun A => ineg (fun e => by cases List.head_eq_of_cons_eq e)) _).trans (sum_zero K _)
      rw [e1, e2, DetChain.add_zero']
    | false =>
      have e1 : sumK K (fun A => if false :: A = false :: a then x else K.zero) (bools n) = x :=
        (sumK_ext K _ _ (fun A => by
          by_cases hA : A = a
          · rw [ip (by rw [hA]), ip hA]
          · rw [ineg (fun e => hA (List.tail_eq_of_cons_eq e)), ineg hA]) _).trans (sel x n a ha)
      have e2 : sumK K (fun A => if true :: A = false :: a then x else K.zero) (bools n) = K.zero :=
        (sumK_ext K _ _ (fun A => ineg (fun e => by cases List.head_eq_of_cons_eq e)) _).trans (sum_zero K _)
      rw [e1, e2, K.zero_add]

theorem sumK_ext_mem {A : Type} (f g : A → R) : ∀ l : List A, (∀ a, memL a l → f a = g a) → sumK K f l = sumK K g l
  | [], _ => rfl
  | a :: l, h => by
    show K.add (f a) (sumK K f l) = K.add (g a) (sumK K g l)
    rw [h a (Or.inl rfl), sumK_ext_mem f g l (fun b hb => h b (Or.inr hb))]

def orB : List Bool → List Bool → List Bool
  | [], _ => []
  | _ :: _, [] => []
  | a :: x, b :: y => (a || b) :: orB x y

theorem len_orB : ∀ (n : Nat) (x y : List Bool), x.length = n → y.length = n → (orB x y).length = n
  | 0, [], _, _, _ => rfl
  | 0, _ :: _, _, h, _ => absurd h Nat.noConfusion
  | _ + 1, [], _, h, _ => absurd h Nat.noConfusion
  | _ + 1, _ :: _, [], _, h => absurd h Nat.noConfusion
  | n + 1, _ :: x, _ :: y, hx, hy => congrArg Nat.succ (len_orB n x y (Nat.succ.inj hx) (Nat.succ.inj hy))

variable (O : ORing K)

theorem sumK_le {A : Type} (f g : A → R) : ∀ l : List A, (∀ a, memL a l → O.le (f a) (g a)) →
    O.le (sumK K f l) (sumK K g l)
  | [], _ => O.le_refl _
  | a :: l, h => O.add_le_add _ _ _ _ (h a (Or.inl rfl)) (sumK_le f g l (fun b hb => h b (Or.inr hb)))

/-- **Injection bound.** -/
theorem inj_bound (n : Nat) (c d : List Bool → List Bool) (g h : List Bool → R)
    (hc : ∀ S, S.length = n → (c S).length = n) (hd : ∀ S, S.length = n → (d S).length = n)
    (hu : ∀ S, S.length = n → S = orB (c S) (d S))
    (hg : ∀ A, O.le K.zero (g A)) (hh : ∀ B, O.le K.zero (h B)) :
    O.le (sumK K (fun S => K.mul (g (c S)) (h (d S))) (bools n))
      (K.mul (sumK K g (bools n)) (sumK K h (bools n))) := by
  have hmem := ChangeVar2D.mem_bools_len n
  -- write each term as a double selection
  have e1 : sumK K (fun S => K.mul (g (c S)) (h (d S))) (bools n)
      = sumK K (fun S => sumK K (fun A => sumK K (fun B =>
          if A = c S then (if B = d S then K.mul (g A) (h B) else K.zero) else K.zero) (bools n)) (bools n)) (bools n) := by
    refine sumK_ext_mem K _ _ (bools n) (fun S hS => ?_)
    have hS' := hmem S hS
    have i1 : ∀ A, sumK K (fun B => if A = c S then (if B = d S then K.mul (g A) (h B) else K.zero) else K.zero) (bools n)
        = (if A = c S then K.mul (g A) (h (d S)) else K.zero) := by
      intro A
      by_cases hA : A = c S
      · rw [ip hA, ← sel K (K.mul (g A) (h (d S))) n (d S) (hd S hS')]
        exact sumK_ext K _ _ (fun B => by
          rw [ip hA]
          by_cases hB : B = d S
          · rw [ip hB, ip hB, hB]
          · rw [ineg hB, ineg hB]) _
      · rw [ineg hA]
        exact (sumK_ext K _ _ (fun B => ineg hA) _).trans (sum_zero K _)
    rw [sumK_ext K _ _ i1, ← sel K (K.mul (g (c S)) (h (d S))) n (c S) (hc S hS')]
    exact sumK_ext K _ _ (fun A => by
      by_cases hA : A = c S
      · rw [ip hA, ip hA, hA]
      · rw [ineg hA, ineg hA]) _
  rw [e1, sumK_swap]
  -- inner Σ_S ≤ g A h B
  have e2 : K.mul (sumK K g (bools n)) (sumK K h (bools n))
      = sumK K (fun A => sumK K (fun B => K.mul (g A) (h B)) (bools n)) (bools n) := by
    rw [sumK_mul]
    exact sumK_ext K _ _ (fun A => mul_sumK K _ _ _) _
  rw [e2]
  refine sumK_le K O _ _ (bools n) (fun A hA => ?_)
  rw [sumK_swap]
  refine sumK_le K O _ _ (bools n) (fun B hB => ?_)
  have hAB := len_orB n A B (hmem A hA) (hmem B hB)
  refine O.le_trans _ _ _ (sumK_le K O _ (fun S => if S = orB A B then K.mul (g A) (h B) else K.zero) (bools n)
    (fun S hS => ?_)) (by rw [sel K (K.mul (g A) (h B)) n (orB A B) hAB]; exact O.le_refl _)
  by_cases h1 : A = c S
  · by_cases h2 : B = d S
    · rw [ip h1, ip h2, ip (by rw [h1, h2]; exact hu S (hmem S hS))]
      exact O.le_refl _
    · rw [ip h1, ineg h2]
      by_cases h3 : S = orB A B
      · rw [ip h3]; exact O.mul_nonneg _ _ (hg A) (hh B)
      · rw [ineg h3]; exact O.le_refl _
  · rw [ineg h1]
    by_cases h3 : S = orB A B
    · rw [ip h3]; exact O.mul_nonneg _ _ (hg A) (hh B)
    · rw [ineg h3]; exact O.le_refl _

/-- The weight of one plaquette subset. -/
def term (t : R) : List Nat → List (List Nat) → List Bool → R
  | n, [], _ => if evenAll n = true then K.one else K.zero
  | n, q :: P, true :: S => K.mul t (term t (zadd n q) P S)
  | n, _ :: P, false :: S => term t n P S
  | _, _ :: _, [] => K.zero

/-- **The surface sum is a sum over masks.** -/
theorem G_mask (t : R) : ∀ (P : List (List Nat)) (n : List Nat), G K t n P = sumK K (term K t n P) (bools P.length)
  | [], n => by
    show (if evenAll n = true then K.one else K.zero) = K.add (if evenAll n = true then K.one else K.zero) K.zero
    rw [DetChain.add_zero']
  | q :: P, n => by
    show K.add (G K t n P) (K.mul t (G K t (zadd n q) P))
      = sumK K (term K t n (q :: P)) ((bools P.length).map (List.cons true) ++ (bools P.length).map (List.cons false))
    rw [sumK_app, sumK_map, sumK_map, G_mask t P n, G_mask t P (zadd n q), mul_sumK, K.add_comm]
    rfl

end MaskSum

#print axioms MaskSum.sum_zero
#print axioms MaskSum.sel
#print axioms MaskSum.len_orB
#print axioms MaskSum.sumK_le
#print axioms MaskSum.inj_bound
#print axioms MaskSum.G_mask

ym/MaskMono.lean

/-
  ym/MaskMono — T45, cycle 36 (Lad1, ℤ₂, any dimension): ⟨W⟩ is nondecreasing along nested plaquette masks.

    diff_exists     M ⊆ M', M ≠ M'  ⇒  some v is in M' and not in M
    G0_ge_one       G_0(M) ≥ 1
    mask_mono       M ⊆ M'  ⇒  G_W(M) G_0(M') ≤ G_W(M') G_0(M),   i.e.  ⟨W⟩_M ≤ ⟨W⟩_M'
  (G(M) = G n (selL P M); free boundary conditions; chain of GPerm.gksG_mask, cancelling G_0 ≥ 1.)

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GPerm

namespace MaskMono

open YMChain LinkIntegral GKS StrongZ2 MaskSum Closure Factor Seeds Count GPerm

variable {R : Type} (K : DetChain.CRing R)

theorem diff_exists : ∀ x y : List Bool, leB x y → x ≠ y →
    ∃ v, v < x.length ∧ nthB x v = false ∧ nthB y v = true
  | [], [], _, h => absurd rfl h
  | [], _ :: _, h, _ => h.elim
  | _ :: _, [], h, _ => h.elim
  | a :: x, b :: y, h, hne => by
    cases a <;> cases b
    · obtain ⟨v, hv, h1, h2⟩ := diff_exists x y h.2 (fun e' => hne (by rw [e']))
      exact ⟨v + 1, Nat.succ_lt_succ hv, h1, h2⟩
    · exact ⟨0, Nat.succ_pos _, rfl, rfl⟩
    · exact absurd (h.1 rfl) Bool.noConfusion
    · obtain ⟨v, hv, h1, h2⟩ := diff_exists x y h.2 (fun e' => hne (by rw [e']))
      exact ⟨v + 1, Nat.succ_lt_succ hv, h1, h2⟩

theorem leB_upd (M M' : List Bool) (v : Nat) (hv : v < M.length) (hl : M.length = M'.length)
    (h : leB M M') (hM' : nthB M' v = true) : leB (upd M v true) M' :=
  leB_of _ _ ((len_upd M v true).trans hl) (fun i hi => by
    rw [nth_upd M v true i hv] at hi
    by_cases e : i = v
    · rw [e]; exact hM'
    · rw [ineg e] at hi; exact of_leB M M' h i hi)

variable (O : ORing K)

theorem G0_ge_one (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) (L : Nat) (Q : List (List Nat)) :
    O.le K.one (G K t (zl L) Q) := by
  have := G_ge_nil K O h1 t ht Q (zl L)
  have e1 : G K t (zl L) [] = K.one := by
    show (if evenAll (zl L) = true then K.one else K.zero) = K.one
    rw [even_zl]; rfl
  rw [e1] at this
  exact this

/-- **Volume monotonicity along masks.** -/
theorem mask_mono (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one)
    (L : Nat) (n : List Nat) (hn : n.length = L) (P : List (List Nat)) (hP : ∀ r, memL r P → r.length = L)
    (M' : List Bool) (hM' : M'.length = P.length) :
    ∀ (f : Nat) (M : List Bool), M.length = P.length → leB M M' → cnt M' ≤ cnt M + f →
      O.le (K.mul (G K t n (selL P M)) (G K t (zl L) (selL P M'))) (K.mul (G K t n (selL P M')) (G K t (zl L) (selL P M))) := by
  intro f
  induction f with
  | zero =>
    intro M hM hle hc
    have e : M = M' := by
      by_cases e : M = M'
      · exact e
      · have := leB_cnt_lt M M' hle e
        exact absurd (Nat.lt_of_lt_of_le this hc) (Nat.lt_irrefl _)
    rw [e]; exact O.le_refl _
  | succ f ih =>
    intro M hM hle hc
    by_cases e : M = M'
    · rw [e]; exact O.le_refl _
    · obtain ⟨v, hv, hMv, hM'v⟩ := diff_exists M M' hle e
      let M1 := upd M v true
      have hM1 : M1.length = P.length := (len_upd M v true).trans hM
      have hle1 : leB M1 M' := leB_upd M M' v hv (hM.trans hM'.symm) hle hM'v
      have hc1 : cnt M' ≤ cnt M1 + f := by
        show cnt M' ≤ cnt (upd M v true) + f
        rw [cnt_upd_true M v hv hMv, Nat.add_right_comm]
        exact Nat.le_trans hc (Nat.le_of_eq (Nat.add_succ _ _))
      have a := gksG_mask K O h1 h10 t ht0 ht1 L n hn P hP M hM v (by rw [← hM]; exact hv) hMv
      have b := ih M1 hM1 hle1 hc1
      -- names
      let gW := G K t n (selL P M)
      let gW1 := G K t n (selL P M1)
      let gW' := G K t n (selL P M')
      let z := G K t (zl L) (selL P M)
      let z1 := G K t (zl L) (selL P M1)
      let z' := G K t (zl L) (selL P M')
      have z1p : O.le K.one z1 := G0_ge_one K O h1 t ht0 L _
      have z1n : O.le K.zero z1 := O.le_trans _ _ _ h1 z1p
      have z1ne : z1 ≠ K.zero := fun hz => by rw [hz] at z1p; exact h10 (O.le_antisymm _ _ z1p h1)
      have zp : O.le K.zero z := O.le_trans _ _ _ h1 (G0_ge_one K O h1 t ht0 L _)
      have z'p : O.le K.zero z' := O.le_trans _ _ _ h1 (G0_ge_one K O h1 t ht0 L _)
      -- gW z1 ≤ gW1 z ;  gW1 z' ≤ gW' z1
      have a' := O.mul_le_mul_right _ _ _ a z'p
      have b' := O.mul_le_mul_right _ _ _ b zp
      -- (gW z1) z' ≤ (gW1 z) z' = (gW1 z') z ≤ (gW' z1) z
      have e1 : K.mul (K.mul gW1 z) z' = K.mul (K.mul gW1 z') z := by
        rw [K.mul_assoc, K.mul_comm z z', ← K.mul_assoc]
      have c := O.le_trans _ _ _ (e1 ▸ a') b'
      have e2 : K.mul (K.mul gW z1) z' = K.mul (K.mul gW z') z1 := by
        rw [K.mul_assoc, K.mul_comm z1 z', ← K.mul_assoc]
      have e3 : K.mul (K.mul gW' z1) z = K.mul (K.mul gW' z) z1 := by
        rw [K.mul_assoc, K.mul_comm z1 z, ← K.mul_assoc]
      rw [e2, e3] at c
      exact O.pos_cancel _ _ _ z1n z1ne c

end MaskMono

#print axioms MaskMono.diff_exists
#print axioms MaskMono.leB_upd
#print axioms MaskMono.G0_ge_one
#print axioms MaskMono.mask_mono

ym/Closure.lean

/-
  ym/Closure — T45, cycle 29 (Lad1, ℤ₂, any dimension): the connected part of a plaquette set, abstractly.

  Masks are List Bool of a fixed length N. V : mask → mask is inflationary (A ≤ V A). For a mask S,
    iter S 0 = ∅,   iter S (k+1) = S ∧ V (iter S k)          ("add every plaquette of S reached from what we have")
    cl S = iter S N.
  Results:
    iter_le_S, iter_mono      iter S k ⊆ S and the iterates increase
    iter_step                 either iter S (k+1) = iter S k, or iter S (k+1) has ≥ k+1 elements
    cl_fix                    S ∧ V (cl S) = cl S      (after N steps nothing more is reached)
    cl_le                     cl S ⊆ S
    cl_idem                   cl (cl S) = cl S         (the connected part of the connected part is itself)
  With V A = A ∨ {plaquettes touching the loop or a plaquette of A} (cycle 30) cl S is the part of S connected to the
  loop, and cl_fix says that S \ cl S touches neither the loop nor cl S — the hypothesis of Decouple.

  Lean 4 core only, proofs by hand, no axioms.
-/

namespace Closure

def leB : List Bool → List Bool → Prop
  | [], [] => True
  | [], _ :: _ => False
  | _ :: _, [] => False
  | a :: x, b :: y => (a = true → b = true) ∧ leB x y

def cnt : List Bool → Nat
  | [] => 0
  | true :: x => cnt x + 1
  | false :: x => cnt x

def andB : List Bool → List Bool → List Bool
  | [], _ => []
  | _ :: _, [] => []
  | a :: x, b :: y => (a && b) :: andB x y

def fz : Nat → List Bool
  | 0 => []
  | N + 1 => false :: fz N

theorem leB_refl : ∀ x : List Bool, leB x x
  | [] => trivial
  | _ :: x => ⟨id, leB_refl x⟩

theorem leB_trans : ∀ x y z : List Bool, leB x y → leB y z → leB x z
  | [], [], [], _, _ => trivial
  | [], [], _ :: _, _, h => h.elim
  | [], _ :: _, _, h, _ => h.elim
  | _ :: _, [], _, h, _ => h.elim
  | _ :: _, _ :: _, [], _, h => h.elim
  | _ :: x, _ :: y, _ :: z, h1, h2 => ⟨fun h => h2.1 (h1.1 h), leB_trans x y z h1.2 h2.2⟩

theorem leB_len : ∀ x y : List Bool, leB x y → x.length = y.length
  | [], [], _ => rfl
  | [], _ :: _, h => h.elim
  | _ :: _, [], h => h.elim
  | _ :: x, _ :: y, h => congrArg Nat.succ (leB_len x y h.2)

theorem cnt_le_len : ∀ x : List Bool, cnt x ≤ x.length
  | [] => Nat.le_refl 0
  | true :: x => Nat.succ_le_succ (cnt_le_len x)
  | false :: x => Nat.le_succ_of_le (cnt_le_len x)

theorem leB_cnt : ∀ x y : List Bool, leB x y → cnt x ≤ cnt y
  | [], [], _ => Nat.le_refl 0
  | [], _ :: _, h => h.elim
  | _ :: _, [], h => h.elim
  | false :: x, false :: y, h => leB_cnt x y h.2
  | false :: x, true :: y, h => Nat.le_succ_of_le (leB_cnt x y h.2)
  | true :: _, false :: _, h => absurd (h.1 rfl) Bool.noConfusion
  | true :: x, true :: y, h => Nat.succ_le_succ (leB_cnt x y h.2)

theorem leB_cnt_lt : ∀ x y : List Bool, leB x y → x ≠ y → cnt x < cnt y
  | [], [], _, hne => absurd rfl hne
  | [], _ :: _, h, _ => h.elim
  | _ :: _, [], h, _ => h.elim
  | false :: x, false :: y, h, hne => leB_cnt_lt x y h.2 (fun e => hne (by rw [e]))
  | false :: x, true :: y, h, _ => Nat.lt_succ_of_le (leB_cnt x y h.2)
  | true :: _, false :: _, h, _ => absurd (h.1 rfl) Bool.noConfusion
  | true :: x, true :: y, h, hne => Nat.succ_lt_succ (leB_cnt_lt x y h.2 (fun e => hne (by rw [e])))

theorem andB_le : ∀ S X : List Bool, S.length = X.length → leB (andB S X) S
  | [], _, _ => trivial
  | _ :: _, [], h => absurd h Nat.noConfusion
  | a :: S, b :: X, h => ⟨fun e => by cases a <;> cases b <;> first | rfl | exact e, andB_le S X (Nat.succ.inj h)⟩

theorem le_andB : ∀ A S X : List Bool, leB A S → leB A X → leB A (andB S X)
  | [], [], [], _, _ => trivial
  | [], [], _ :: _, _, h => h.elim
  | [], _ :: _, _, h, _ => h.elim
  | _ :: _, [], _, h, _ => h.elim
  | _ :: _, _ :: _, [], _, h => h.elim
  | a :: A, s :: S, x :: X, h1, h2 => ⟨fun e => by rw [h1.1 e, h2.1 e]; rfl, le_andB A S X h1.2 h2.2⟩

theorem andB_congr : ∀ S S' X : List Bool, leB (andB S X) S' → leB S' S → andB S' X = andB S X
  | [], [], _, _, _ => rfl
  | [], _ :: _, _, _, h => h.elim
  | _ :: _, [], _, _, h => h.elim
  | _ :: _, _ :: _, [], _, _ => rfl
  | s :: S, s' :: S', x :: X, h1, h2 => by
    show (s' && x) :: andB S' X = (s && x) :: andB S X
    rw [andB_congr S S' X h1.2 h2.2]
    have k1 : (s && x) = true → s' = true := h1.1
    have k2 : s' = true → s = true := h2.1
    cases s <;> cases s' <;> cases x <;> first | rfl | exact absurd (k2 rfl) Bool.noConfusion | exact absurd (k1 rfl) Bool.noConfusion

theorem fz_le : ∀ (N : Nat) (S : List Bool), S.length = N → leB (fz N) S
  | 0, [], _ => trivial
  | 0, _ :: _, h => absurd h Nat.noConfusion
  | _ + 1, [], h => absurd h Nat.noConfusion
  | N + 1, _ :: S, h => ⟨fun e => absurd e Bool.noConfusion, fz_le N S (Nat.succ.inj h)⟩

section
variable (V : List Bool → List Bool) (N : Nat)

def iter (S : List Bool) : Nat → List Bool
  | 0 => fz N
  | k + 1 => andB S (V (iter S k))

def cl (S : List Bool) : List Bool := iter V N S N


theorem iter_le_S (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) : ∀ k, leB (iter V N S k) S
  | 0 => fz_le N S hS
  | k + 1 => by
    have hl : (iter V N S k).length = N := (leB_len _ _ (iter_le_S hV S hS k)).trans hS
    exact andB_le S _ (hS.trans (hl.symm.trans (leB_len _ _ (hV _ hl))))

theorem iter_len (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) (k : Nat) : (iter V N S k).length = N :=
  (leB_len _ _ (iter_le_S V N hV S hS k)).trans hS

theorem iter_succ_ge (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) (k : Nat) : leB (iter V N S k) (iter V N S (k + 1)) :=
  le_andB _ S _ (iter_le_S V N hV S hS k) (hV _ (iter_len V N hV S hS k))

theorem iter_step (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) :
    ∀ k, iter V N S (k + 1) = iter V N S k ∨ k + 1 ≤ cnt (iter V N S (k + 1))
  | 0 => by
    by_cases e : iter V N S 1 = iter V N S 0
    · exact Or.inl e
    · exact Or.inr (Nat.lt_of_le_of_lt (Nat.zero_le _)
        (leB_cnt_lt _ _ (iter_succ_ge V N hV S hS 0) (fun h => e h.symm)))
  | k + 1 => by
    cases iter_step hV S hS k with
    | inl e =>
      refine Or.inl ?_
      show andB S (V (iter V N S (k + 1))) = andB S (V (iter V N S k))
      rw [e]
    | inr h =>
      by_cases e : iter V N S (k + 2) = iter V N S (k + 1)
      · exact Or.inl e
      · exact Or.inr (Nat.lt_of_le_of_lt h
          (leB_cnt_lt _ _ (iter_succ_ge V N hV S hS (k + 1)) (fun h' => e h'.symm)))

/-- **Fixpoint**: after N steps nothing more is reached. -/
theorem cl_fix (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) : andB S (V (cl V N S)) = cl V N S := by
  show iter V N S (N + 1) = iter V N S N
  cases iter_step V N hV S hS N with
  | inl e => exact e
  | inr h =>
    have h2 := cnt_le_len (iter V N S (N + 1))
    rw [iter_len V N hV S hS] at h2
    exact absurd (Nat.le_trans h h2) (Nat.not_succ_le_self N)

theorem cl_le (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) : leB (cl V N S) S := iter_le_S V N hV S hS N

theorem cl_len (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) : (cl V N S).length = N := iter_len V N hV S hS N

theorem iter_late (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) : ∀ j, iter V N S (N + j) = cl V N S
  | 0 => rfl
  | j + 1 => by
    show andB S (V (iter V N S (N + j))) = cl V N S
    rw [iter_late hV S hS j]
    exact cl_fix V N hV S hS

theorem iter_chain (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) (k : Nat) : ∀ j, leB (iter V N S k) (iter V N S (k + j))
  | 0 => leB_refl _
  | j + 1 => leB_trans _ _ _ (iter_chain hV S hS k j) (iter_succ_ge V N hV S hS (k + j))

theorem split_nat : ∀ k N : Nat, (∃ d, k + d = N) ∨ (∃ j, k = N + j)
  | k, 0 => Or.inr ⟨k, (Nat.zero_add k).symm⟩
  | 0, N + 1 => Or.inl ⟨N + 1, Nat.zero_add _⟩
  | k + 1, N + 1 => by
    cases split_nat k N with
    | inl h => obtain ⟨d, h⟩ := h; exact Or.inl ⟨d, by rw [Nat.succ_add, h]⟩
    | inr h => obtain ⟨j, h⟩ := h; exact Or.inr ⟨j, by rw [h, Nat.succ_add]⟩

theorem iter_le_cl (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) (k : Nat) :
    leB (iter V N S k) (cl V N S) := by
  cases split_nat k N with
  | inl h =>
    obtain ⟨d, h⟩ := h
    have := iter_chain V N hV S hS k d
    rw [h] at this
    exact this
  | inr h =>
    obtain ⟨j, h⟩ := h
    rw [h, iter_late V N hV S hS j]
    exact leB_refl _

/-- **Idempotence**: the connected part of the connected part is itself. -/
theorem cl_idem (hV : ∀ A : List Bool, A.length = N → leB A (V A)) (S : List Bool) (hS : S.length = N) : cl V N (cl V N S) = cl V N S := by
  have key : ∀ k, iter V N (cl V N S) k = iter V N S k := by
    intro k
    induction k with
    | zero => rfl
    | succ k ih =>
      show andB (cl V N S) (V (iter V N (cl V N S) k)) = andB S (V (iter V N S k))
      rw [ih]
      exact andB_congr S (cl V N S) _ (iter_le_cl V N hV S hS (k + 1)) (cl_le V N hV S hS)
  exact key N

end

end Closure

#print axioms Closure.leB_trans
#print axioms Closure.cnt_le_len
#print axioms Closure.leB_cnt_lt
#print axioms Closure.andB_congr
#print axioms Closure.iter_step
#print axioms Closure.cl_fix
#print axioms Closure.cl_le
#print axioms Closure.iter_le_cl
#print axioms Closure.cl_idem

ym/Factor.lean

/-
  ym/Factor — T45, cycle 30 (Lad1, ℤ₂, any dimension): the connected-component factorisation S2c.

  Loop n (link multiplicities), plaquettes P (link-incidence vectors), t = tanh β ≥ 0, masks of length N = |P|.
    touch p q        p and q share a link (both nonzero somewhere)
    V n P A          A ∪ {plaquettes touching the loop or a plaquette of A}; cl = Closure.cl (V n P) N
                     cl S = the part of S connected to the loop
    term_split       if the C-part and the B-part of a mask share no link with each other, and the B-part shares none
                     with the loop: term (n + m) (C ∪ B) = term n C · term m B
    fix_read         a plaquette of S \ cl S touches neither the loop nor cl S       (from Closure.cl_fix)
    factor           **G_W ≤ C_W · G_0**,  C_W = Σ_{A = cl A} t^|A| [∂A = W]
  Since ⟨W⟩ = G_W / G_0 (StrongZ2.N_eq), this is ⟨W⟩ ≤ C_W: the Wilson loop is bounded by a sum over plaquette sets
  every component of which is connected to the loop — closed surfaces far away have cancelled. C_W only grows with
  the lattice and is bounded by counting connected sets (next: Lad1 counting, C_W ≤ Σ_{k ≥ Area} c^k t^k).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.MaskSum
import ym.Closure
import ym.AreaLower

namespace Factor

open YMChain LinkIntegral GKS StrongZ2 MaskSum Closure

variable {R : Type} (K : DetChain.CRing R)

def nzb : Nat → Bool
  | 0 => false
  | _ + 1 => true

def touch : List Nat → List Nat → Bool
  | [], _ => false
  | _ :: _, [] => false
  | a :: x, b :: y => (nzb a && nzb b) || touch x y

def anyT : List (List Nat) → List Bool → List Nat → Bool
  | [], _, _ => false
  | _ :: _, [], _ => false
  | p :: P, a :: A, q => (a && touch q p) || anyT P A q

def vAux (r : List Nat → Bool) : List (List Nat) → List Bool → List Bool
  | [], _ => []
  | _ :: _, [] => []
  | q :: P, a :: A => (a || r q) :: vAux r P A

def V (n : List Nat) (P : List (List Nat)) (A : List Bool) : List Bool :=
  vAux (fun q => touch q n || anyT P A q) P A

def notB : List Bool → List Bool
  | [] => []
  | a :: x => (!a) :: notB x

def selL : List (List Nat) → List Bool → List (List Nat)
  | [], _ => []
  | _ :: _, [] => []
  | q :: P, true :: A => q :: selL P A
  | _ :: P, false :: A => selL P A

def disjN : List Nat → List Nat → Prop
  | [], _ => True
  | _ :: _, [] => True
  | a :: x, b :: y => (a = 0 ∨ b = 0) ∧ disjN x y

def disjM : List Bool → List Bool → Prop
  | [], _ => True
  | _ :: _, [] => True
  | a :: x, b :: y => ¬(a = true ∧ b = true) ∧ disjM x y

theorem vAux_ge (r : List Nat → Bool) : ∀ (P : List (List Nat)) (A : List Bool), P.length = A.length →
    leB A (vAux r P A)
  | [], [], _ => trivial
  | [], _ :: _, h => absurd h Nat.noConfusion
  | _ :: _, [], h => absurd h Nat.noConfusion
  | _ :: P, _ :: A, h => ⟨fun e => by rw [e]; rfl, vAux_ge r P A (Nat.succ.inj h)⟩

theorem touch_disj : ∀ p q : List Nat, touch p q = false → disjN p q
  | [], _, _ => trivial
  | _ :: _, [], _ => trivial
  | 0 :: x, _ :: y, h => ⟨Or.inl rfl, touch_disj x y h⟩
  | (_ + 1) :: x, 0 :: y, h => ⟨Or.inr rfl, touch_disj x y h⟩
  | (_ + 1) :: _, (_ + 1) :: _, h => absurd h Bool.noConfusion

theorem disj_comm : ∀ p q : List Nat, disjN p q → disjN q p
  | [], [], _ => trivial
  | [], _ :: _, _ => trivial
  | _ :: _, [], _ => trivial
  | _ :: x, _ :: y, h => ⟨h.1.symm, disj_comm x y h.2⟩

theorem disj_zadd_left : ∀ n q m : List Nat, disjN n m → disjN q m → disjN (zadd n q) m
  | [], _, _, _, h => h
  | _ :: _, [], _, h, _ => h
  | _ :: _, _ :: _, [], _, _ => trivial
  | a :: n, b :: q, c :: m, h1, h2 => by
    refine ⟨?_, disj_zadd_left n q m h1.2 h2.2⟩
    cases h1.1 with
    | inr hc => exact Or.inr hc
    | inl ha =>
      cases h2.1 with
      | inr hc => exact Or.inr hc
      | inl hb => exact Or.inl (by rw [ha, hb])

theorem disj_zadd_right (n m q : List Nat) (h1 : disjN n m) (h2 : disjN n q) : disjN n (zadd m q) :=
  disj_comm _ _ (disj_zadd_left m q n (disj_comm _ _ h1) (disj_comm _ _ h2))

theorem disj_zl : ∀ (x : List Nat) (k : Nat), disjN x (zl k)
  | [], _ => trivial
  | _ :: _, 0 => trivial
  | _ :: x, k + 1 => ⟨Or.inr rfl, disj_zl x k⟩

theorem band3 (a b c : Bool) : (a && (b && c)) = ((true && b) && (a && c)) := by
  cases a <;> cases b <;> cases c <;> rfl

theorem even_split : ∀ n m : List Nat, disjN n m → evenAll (zadd n m) = (evenAll n && evenAll m)
  | [], _, _ => rfl
  | _ :: _, [], _ => (Bool.and_true _).symm
  | a :: n, b :: m, h => by
    show (parity (a + b) && evenAll (zadd n m)) = ((parity a && evenAll n) && (parity b && evenAll m))
    rw [even_split n m h.2]
    cases h.1 with
    | inl ha => rw [ha, Nat.zero_add]; exact band3 _ _ _
    | inr hb => rw [hb]; exact Decouple.band4' _ _ _

/-- **Splitting a mask into two link-disjoint parts.** -/
theorem term_split (t : R) : ∀ (P : List (List Nat)) (C B : List Bool) (n m : List Nat),
    C.length = P.length → B.length = P.length → disjM C B → disjN n m →
    (∀ q, memL q (selL P C) → disjN q m) → (∀ q, memL q (selL P B) → disjN n q) →
    (∀ q q', memL q (selL P C) → memL q' (selL P B) → disjN q q') →
    MaskSum.term K t (zadd n m) P (orB C B) = K.mul (MaskSum.term K t n P C) (MaskSum.term K t m P B)
  | [], _, _, n, m, _, _, _, hnm, _, _, _ => by
    show (if evenAll (zadd n m) = true then K.one else K.zero)
      = K.mul (if evenAll n = true then K.one else K.zero) (if evenAll m = true then K.one else K.zero)
    rw [even_split n m hnm]
    cases evenAll n <;> cases evenAll m
    · exact (DetChain.zero_mul' K _).symm
    · exact (DetChain.zero_mul' K _).symm
    · exact (DetChain.mul_zero K _).symm
    · exact (K.one_mul _).symm
  | _ :: _, [], _, _, _, h, _, _, _, _, _, _ => absurd h Nat.noConfusion
  | _ :: _, _ :: _, [], _, _, _, h, _, _, _, _, _ => absurd h Nat.noConfusion
  | q :: P, c :: C, b :: B, n, m, hC, hB, hd, hnm, hCm, hnB, hCB => by
    have hC' := Nat.succ.inj hC
    have hB' := Nat.succ.inj hB
    cases c with
    | false =>
      cases b with
      | false =>
        exact term_split t P C B n m hC' hB' hd.2 hnm hCm hnB hCB
      | true =>
        -- the B part takes q
        show K.mul t (MaskSum.term K t (zadd (zadd n m) q) P (orB C B))
          = K.mul (MaskSum.term K t n P C) (K.mul t (MaskSum.term K t (zadd m q) P B))
        have hq : disjN n q := hnB q (Or.inl rfl)
        rw [Decouple.zadd_assoc,
          term_split t P C B n (zadd m q) hC' hB' hd.2 (disj_zadd_right n m q hnm hq)
            (fun r hr => disj_zadd_right r m q (hCm r hr) (hCB r q hr (Or.inl rfl)))
            (fun r hr => hnB r (Or.inr hr)) (fun r r' hr hr' => hCB r r' hr (Or.inr hr')),
          ← K.mul_assoc, K.mul_comm t, K.mul_assoc]
    | true =>
      cases b with
      | true => exact absurd ⟨rfl, rfl⟩ hd.1
      | false =>
        show K.mul t (MaskSum.term K t (zadd (zadd n m) q) P (orB C B))
          = K.mul (K.mul t (MaskSum.term K t (zadd n q) P C)) (MaskSum.term K t m P B)
        have hq : disjN q m := hCm q (Or.inl rfl)
        rw [Decouple.zadd_assoc, AreaLower.zadd_comm m q, ← Decouple.zadd_assoc,
          term_split t P C B (zadd n q) m hC' hB' hd.2 (disj_zadd_left n q m hnm hq)
            (fun r hr => hCm r (Or.inr hr))
            (fun r hr => disj_zadd_left n q r (hnB r hr) (hCB q r (Or.inl rfl) hr))
            (fun r r' hr hr' => hCB r r' (Or.inr hr) hr'),
          K.mul_assoc]

/-- **Reading the fixpoint.** -/
theorem fix_read (r : List Nat → Bool) : ∀ (P : List (List Nat)) (S C : List Bool),
    S.length = P.length → C.length = P.length → andB S (vAux r P C) = C →
    ∀ q, memL q (selL P (andB S (notB C))) → r q = false
  | [], _, _, _, _, _, _, h => h.elim
  | _ :: _, [], _, h, _, _, _, _ => absurd h Nat.noConfusion
  | _ :: _, _ :: _, [], _, h, _, _, _ => absurd h Nat.noConfusion
  | p :: P, s :: S, c :: C, hS, hC, hfix, q, hq => by
    have hhead : (s && (c || r p)) = c := List.head_eq_of_cons_eq hfix
    have htail : andB S (vAux r P C) = C := List.tail_eq_of_cons_eq hfix
    have ih := fix_read r P S C (Nat.succ.inj hS) (Nat.succ.inj hC) htail q
    cases s with
    | false => exact ih hq
    | true =>
      cases c with
      | true => exact ih hq
      | false =>
        cases hq with
        | inl e => rw [e]; exact hhead
        | inr hq => exact ih hq

theorem anyT_sel : ∀ (P : List (List Nat)) (C : List Bool) (q' : List Nat), anyT P C q' = false →
    ∀ q, memL q (selL P C) → touch q' q = false
  | [], _, _, _, _, h => h.elim
  | _ :: _, [], _, _, _, h => h.elim
  | p :: P, false :: C, q', h, q, hq => anyT_sel P C q' h q hq
  | p :: P, true :: C, q', h, q, hq => by
    have h' : (touch q' p || anyT P C q') = false := h
    cases e : touch q' p with
    | true => rw [e] at h'; exact absurd h' Bool.noConfusion
    | false =>
      rw [e] at h'
      cases hq with
      | inl hq => rw [hq]; exact e
      | inr hq => exact anyT_sel P C q' h' q hq

theorem split_mask : ∀ C S : List Bool, leB C S →
    S = orB C (andB S (notB C)) ∧ disjM C (andB S (notB C)) ∧ (andB S (notB C)).length = S.length
  | [], [], _ => ⟨rfl, trivial, rfl⟩
  | [], _ :: _, h => h.elim
  | _ :: _, [], h => h.elim
  | c :: C, s :: S, h => by
    have ih := split_mask C S h.2
    refine ⟨?_, ⟨?_, ih.2.1⟩, congrArg Nat.succ ih.2.2⟩
    · show s :: S = (c || (s && !c)) :: orB C (andB S (notB C))
      rw [← ih.1]
      have k : c = true → s = true := h.1
      cases c <;> cases s <;> first | rfl | exact absurd (k rfl) Bool.noConfusion
    · intro e
      have e1 := e.1
      have e2 : (s && !c) = true := e.2
      rw [e1] at e2
      cases s <;> exact absurd e2 Bool.noConfusion

variable (O : ORing K)

theorem term_nn (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) :
    ∀ (P : List (List Nat)) (n : List Nat) (S : List Bool), O.le K.zero (MaskSum.term K t n P S)
  | [], n, _ => by
    show O.le K.zero (if evenAll n = true then K.one else K.zero)
    cases evenAll n
    · exact O.le_refl _
    · exact h1
  | _ :: _, _, [] => O.le_refl _
  | q :: P, n, true :: S => O.mul_nonneg _ _ ht (term_nn h1 t ht P (zadd n q) S)
  | _ :: P, n, false :: S => term_nn h1 t ht P n S

/-- The connected surface sum `C_W = Σ_{A = cl A} t^|A| [∂A = W]`. -/
def CW (t : R) (n : List Nat) (P : List (List Nat)) : R :=
  sumK K (fun A => if Closure.cl (V n P) P.length A = A then MaskSum.term K t n P A else K.zero) (bools P.length)

/-- **Connected-component factorisation**: `G_W ≤ C_W · G_0`. -/
theorem factor (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) (n : List Nat) (P : List (List Nat)) :
    O.le (G K t n P) (K.mul (CW K t n P) (G K t (zl n.length) P)) := by
  let N := P.length
  have hV : ∀ A : List Bool, A.length = N → leB A (V n P A) := fun A hA => vAux_ge _ P A hA.symm
  let c : List Bool → List Bool := Closure.cl (V n P) N
  let d : List Bool → List Bool := fun S => andB S (notB (c S))
  let g : List Bool → R := fun A => if c A = A then MaskSum.term K t n P A else K.zero
  let h : List Bool → R := MaskSum.term K t (zl n.length) P
  have hc : ∀ S, S.length = N → (c S).length = N := fun S hS => cl_len (V n P) N hV S hS
  have hsplit : ∀ S, S.length = N →
      S = orB (c S) (d S) ∧ disjM (c S) (d S) ∧ (d S).length = S.length :=
    fun S hS => split_mask (c S) S (cl_le (V n P) N hV S hS)
  have hd : ∀ S, S.length = N → (d S).length = N := fun S hS => (hsplit S hS).2.2.trans hS
  have hterm : ∀ S, S.length = N → MaskSum.term K t n P S = K.mul (g (c S)) (h (d S)) := by
    intro S hS
    have hfix : andB S (vAux (fun q => touch q n || anyT P (c S) q) P (c S)) = c S := cl_fix (V n P) N hV S hS
    have hread := fix_read _ P S (c S) hS (hc S hS) hfix
    have hB : ∀ q, memL q (selL P (d S)) → touch q n = false ∧ anyT P (c S) q = false := by
      intro q hq
      have e : (touch q n || anyT P (c S) q) = false := hread q hq
      cases e1 : touch q n with
      | true => rw [e1] at e; exact absurd e Bool.noConfusion
      | false => rw [e1] at e; exact ⟨rfl, e⟩
    have hgc : g (c S) = MaskSum.term K t n P (c S) := ip (cl_idem (V n P) N hV S hS)
    rw [hgc]
    have hS' := (hsplit S hS).1
    have e := term_split K t P (c S) (d S) n (zl n.length) (hc S hS) (hd S hS) (hsplit S hS).2.1
      (disj_zl n _) (fun q _ => disj_zl q _)
      (fun q hq => disj_comm _ _ (touch_disj _ _ (hB q hq).1))
      (fun q q' hq hq' => disj_comm _ _ (touch_disj _ _ (anyT_sel P (c S) q' (hB q' hq').2 q hq)))
    rw [Decouple.zadd_zl, ← hS'] at e
    exact e
  rw [MaskSum.G_mask K t P n, MaskSum.G_mask K t P (zl n.length),
    sumK_ext_mem K _ _ (bools N) (fun S hS => hterm S (ChangeVar2D.mem_bools_len N S hS))]
  exact inj_bound K O N c d g h hc hd (fun S hS => (hsplit S hS).1)
    (fun A => by
      show O.le K.zero (if c A = A then MaskSum.term K t n P A else K.zero)
      by_cases e : c A = A
      · rw [ip e]; exact term_nn K O h1 t ht P n A
      · rw [ineg e]; exact O.le_refl _)
    (fun B => term_nn K O h1 t ht P (zl n.length) B)

end Factor

#print axioms Factor.touch_disj
#print axioms Factor.disj_zadd_left
#print axioms Factor.even_split
#print axioms Factor.term_split
#print axioms Factor.fix_read
#print axioms Factor.anyT_sel
#print axioms Factor.split_mask
#print axioms Factor.term_nn
#print axioms Factor.factor

ym/Tail.lean

/-
  ym/Tail — T45, cycle 31 (Lad1, ℤ₂, any dimension): from C_W to a pure counting sum.

    bdS P A          Σ_{q ∈ A} q (the boundary of the plaquette set A, as link multiplicities)
    term_eq          term n P A = [n + ∂A even] · t^|A|
    pw_tail          0 ≤ u ≤ 1, m ≤ k:  (u x)^k ≤ u^m x^k
    CW_tail          if every A with ∂A = W has |A| ≥ m (m = the area of W), then with t = u x
                       C_W(t) ≤ u^m · D_W(x),   D_W(x) = Σ_{A = cl A} x^|A|
  With Factor.factor: ⟨W⟩ ≤ u^Area · D_W(x). D_W(x) counts loop-connected plaquette sets with weight x; it grows with
  the lattice but (next) is bounded by (1 + y)^(number of plaquettes touching W) once x(1 + y)^D ≤ y, D the number of
  plaquettes touching a given one — that bound is uniform in the volume and gives the area law with rate −ln u.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Factor
import ym.GKS2

namespace Tail

open YMChain LinkIntegral GKS StrongZ2 MaskSum Closure Factor

variable {R : Type} (K : DetChain.CRing R)

def bdS : List (List Nat) → List Bool → List Nat
  | [], _ => []
  | _ :: _, [] => []
  | q :: P, true :: A => zadd q (bdS P A)
  | _ :: P, false :: A => bdS P A

theorem term_eq (t : R) : ∀ (P : List (List Nat)) (n : List Nat) (A : List Bool), A.length = P.length →
    MaskSum.term K t n P A = (if evenAll (zadd n (bdS P A)) = true then pwR K t (cnt A) else K.zero)
  | [], n, A, _ => by
    show (if evenAll n = true then K.one else K.zero) = (if evenAll (zadd n []) = true then pwR K t (cnt A) else K.zero)
    have e : cnt A = 0 := by
      cases A with
      | nil => rfl
      | cons _ _ => exact absurd ‹_› Nat.noConfusion
    rw [e, AreaLower.zadd_nil]
    rfl
  | _ :: _, _, [], h => absurd h Nat.noConfusion
  | q :: P, n, true :: A, h => by
    show K.mul t (MaskSum.term K t (zadd n q) P A)
      = (if evenAll (zadd n (zadd q (bdS P A))) = true then K.mul (pwR K t (cnt A)) t else K.zero)
    rw [term_eq t P (zadd n q) A (Nat.succ.inj h), ← Decouple.zadd_assoc]
    cases evenAll (zadd (zadd n q) (bdS P A))
    · exact DetChain.mul_zero K t
    · exact K.mul_comm _ _
  | _ :: P, n, false :: A, h => term_eq t P n A (Nat.succ.inj h)

variable (O : ORing K)

theorem pw_le_one (h1 : O.le K.zero K.one) (u : R) (hu0 : O.le K.zero u) (hu1 : O.le u K.one) :
    ∀ j, O.le (pwR K u j) K.one
  | 0 => O.le_refl _
  | j + 1 => by
    have a := O.mul_le_mul_right _ _ _ (pw_le_one h1 u hu0 hu1 j) hu0
    rw [K.one_mul] at a
    exact O.le_trans _ _ _ a hu1

theorem pw_tail (h1 : O.le K.zero K.one) (u x : R) (hu0 : O.le K.zero u) (hu1 : O.le u K.one) (hx : O.le K.zero x)
    (m k : Nat) (hmk : m ≤ k) : O.le (pwR K (K.mul u x) k) (K.mul (pwR K u m) (pwR K x k)) := by
  rw [GKS2.pwR_mul]
  refine O.mul_le_mul_right _ _ _ ?_ (AreaLower.pw_nn K O x hx h1 k)
  obtain ⟨j, e⟩ := Nat.le.dest hmk
  rw [← e, pwR_add]
  have a := O.mul_le_mul_right _ _ _ (pw_le_one K O h1 u hu0 hu1 j) (AreaLower.pw_nn K O u hu0 h1 m)
  rw [K.one_mul, K.mul_comm] at a
  exact a

/-- The loop-connected sum with weight `x` per plaquette. -/
def DW (x : R) (n : List Nat) (P : List (List Nat)) : R :=
  sumK K (fun A => if Closure.cl (V n P) P.length A = A then pwR K x (cnt A) else K.zero) (bools P.length)

/-- **Area tail**: `C_W(u x) ≤ u^m · D_W(x)` when every surface spanning `W` has at least `m` plaquettes. -/
theorem CW_tail (h1 : O.le K.zero K.one) (u x : R) (hu0 : O.le K.zero u) (hu1 : O.le u K.one) (hx : O.le K.zero x)
    (n : List Nat) (P : List (List Nat)) (m : Nat)
    (hm : ∀ A : List Bool, A.length = P.length → evenAll (zadd n (bdS P A)) = true → m ≤ cnt A) :
    O.le (CW K (K.mul u x) n P) (K.mul (pwR K u m) (DW K x n P)) := by
  show O.le (sumK K _ (bools P.length)) (K.mul (pwR K u m) (sumK K _ (bools P.length)))
  rw [mul_sumK]
  refine MaskSum.sumK_le K O _ _ (bools P.length) (fun A hA => ?_)
  have hl := ChangeVar2D.mem_bools_len P.length A hA
  by_cases e : Closure.cl (V n P) P.length A = A
  · rw [ip e, ip e, term_eq K _ P n A hl]
    cases hev : evenAll (zadd n (bdS P A))
    · show O.le K.zero (K.mul (pwR K u m) (pwR K x (cnt A)))
      exact O.mul_nonneg _ _ (AreaLower.pw_nn K O u hu0 h1 m) (AreaLower.pw_nn K O x hx h1 _)
    · exact pw_tail K O h1 u x hu0 hu1 hx m (cnt A) (hm A hl hev)
  · rw [ineg e, ineg e, DetChain.mul_zero]
    exact O.le_refl _

end Tail

#print axioms Tail.term_eq
#print axioms Tail.pw_le_one
#print axioms Tail.pw_tail
#print axioms Tail.CW_tail

ym/Seeds.lean

/-
  ym/Seeds — T45, cycle 32 (Lad1, any dimension): connected sets with a general seed predicate, pointwise.

  Plaquettes P (N of them), a seed predicate σ on plaquettes (σ q = "q touches the loop" in Factor). Masks of length N.
    Vs σ P A          A ∪ {i : σ q_i or q_i touches some q_j, j ∈ A};  clS σ = Closure.cl (Vs σ P) N
    nthB, upd         pointwise access, set one coordinate
    nth_andB, nth_vAux, anyT_of, anyT_ex, leB_of, of_leB, ext_nth     pointwise toolkit
    closed            i ∈ A, (σ q_i or q_i touches clS A) ⇒ i ∈ clS A          (cl_fix read pointwise)
    remove_seed       if A ∪ {v} is σ-connected (v ∉ A), then A is σ'-connected, σ' q = σ q ∨ q touches q_v
    no_seed           if no plaquette of A is a seed, clS A = ∅
  These are the two facts behind the counting recursion D(U, σ) ≤ D(U \ v, σ) + x · D(U \ v, σ ∪ N(v)).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Factor

namespace Seeds

open Closure Factor

def Vs (σ : List Nat → Bool) (P : List (List Nat)) (A : List Bool) : List Bool :=
  vAux (fun q => σ q || anyT P A q) P A

def nthB : List Bool → Nat → Bool
  | [], _ => false
  | a :: _, 0 => a
  | _ :: x, k + 1 => nthB x k

def nthL : List (List Nat) → Nat → List Nat
  | [], _ => []
  | q :: _, 0 => q
  | _ :: P, k + 1 => nthL P k

def upd : List Bool → Nat → Bool → List Bool
  | [], _, _ => []
  | _ :: x, 0, b => b :: x
  | a :: x, k + 1, b => a :: upd x k b

theorem nth_andB : ∀ (x y : List Bool) (i : Nat), nthB (andB x y) i = (nthB x i && nthB y i)
  | [], _, _ => rfl
  | a :: _, [], 0 => (Bool.and_false a).symm
  | _ :: x, [], k + 1 => by
    show false = (nthB x k && false)
    exact (Bool.and_false _).symm
  | _ :: _, _ :: _, 0 => rfl
  | _ :: x, _ :: y, k + 1 => nth_andB x y k

theorem nth_vAux (r : List Nat → Bool) : ∀ (P : List (List Nat)) (A : List Bool) (i : Nat),
    A.length = P.length → i < P.length → nthB (vAux r P A) i = (nthB A i || r (nthL P i))
  | [], _, _, _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, [], _, h, _ => absurd h Nat.noConfusion
  | _ :: _, _ :: _, 0, _, _ => rfl
  | _ :: P, _ :: A, k + 1, h, hi => nth_vAux r P A k (Nat.succ.inj h) (Nat.lt_of_succ_lt_succ hi)

theorem anyT_of : ∀ (P : List (List Nat)) (A : List Bool) (q : List Nat) (j : Nat),
    nthB A j = true → touch q (nthL P j) = true → j < P.length → anyT P A q = true
  | [], _, _, _, _, _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, [], _, _, h, _, _ => absurd h Bool.noConfusion
  | p :: _, a :: _, q, 0, h1, h2, _ => by
    show ((a && touch q p) || _) = true
    have h1' : a = true := h1
    have h2' : touch q p = true := h2
    rw [h1', Bool.true_and, h2']; rfl
  | _ :: P, a :: A, q, k + 1, h1, h2, hk => by
    show ((a && touch q _) || anyT P A q) = true
    rw [anyT_of P A q k h1 h2 (Nat.lt_of_succ_lt_succ hk), Bool.or_true]

theorem anyT_ex : ∀ (P : List (List Nat)) (A : List Bool) (q : List Nat), anyT P A q = true →
    ∃ j, j < P.length ∧ nthB A j = true ∧ touch q (nthL P j) = true
  | [], _, _, h => absurd h Bool.noConfusion
  | _ :: _, [], _, h => absurd h Bool.noConfusion
  | p :: P, a :: A, q, h => by
    have h' : ((a && touch q p) || anyT P A q) = true := h
    cases e : (a && touch q p) with
    | true =>
      cases a with
      | false => exact absurd e Bool.noConfusion
      | true => exact ⟨0, Nat.succ_pos _, rfl, e⟩
    | false =>
      rw [e, Bool.false_or] at h'
      obtain ⟨j, hj, h1, h2⟩ := anyT_ex P A q h'
      exact ⟨j + 1, Nat.succ_lt_succ hj, h1, h2⟩

theorem leB_of : ∀ x y : List Bool, x.length = y.length → (∀ i, nthB x i = true → nthB y i = true) → leB x y
  | [], [], _, _ => trivial
  | [], _ :: _, h, _ => absurd h Nat.noConfusion
  | _ :: _, [], h, _ => absurd h Nat.noConfusion
  | _ :: x, _ :: y, h, hp => ⟨hp 0, leB_of x y (Nat.succ.inj h) (fun i hi => hp (i + 1) hi)⟩

theorem of_leB : ∀ x y : List Bool, leB x y → ∀ i, nthB x i = true → nthB y i = true
  | [], _, _, _, h => absurd h Bool.noConfusion
  | _ :: _, [], h, _, _ => h.elim
  | _ :: _, _ :: _, h, 0, hi => h.1 hi
  | _ :: x, _ :: y, h, k + 1, hi => of_leB x y h.2 k hi

theorem leB_antisymm : ∀ x y : List Bool, leB x y → leB y x → x = y
  | [], [], _, _ => rfl
  | [], _ :: _, h, _ => h.elim
  | _ :: _, [], h, _ => h.elim
  | a :: x, b :: y, h1, h2 => by
    rw [leB_antisymm x y h1.2 h2.2]
    have k1 : a = true → b = true := h1.1
    have k2 : b = true → a = true := h2.1
    cases a <;> cases b <;> first | rfl | exact absurd (k1 rfl) Bool.noConfusion | exact absurd (k2 rfl) Bool.noConfusion

theorem nthB_ge : ∀ (x : List Bool) (i : Nat), x.length ≤ i → nthB x i = false
  | [], _, _ => rfl
  | _ :: _, 0, h => absurd h (Nat.not_succ_le_zero _)
  | _ :: x, k + 1, h => nthB_ge x k (Nat.le_of_succ_le_succ h)

theorem nth_upd : ∀ (x : List Bool) (v : Nat) (b : Bool) (i : Nat), v < x.length →
    nthB (upd x v b) i = (if i = v then b else nthB x i)
  | [], _, _, _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, 0, _, 0, _ => rfl
  | _ :: _, 0, _, _ + 1, _ => rfl
  | _ :: _, _ + 1, _, 0, _ => rfl
  | _ :: x, v + 1, b, k + 1, h => by
    show nthB (upd x v b) k = _
    rw [nth_upd x v b k (Nat.lt_of_succ_lt_succ h)]
    by_cases e : k = v
    · rw [MaskSum.ip e, MaskSum.ip (by rw [e])]
    · rw [MaskSum.ineg e, MaskSum.ineg (fun h' => e (Nat.succ.inj h'))]; rfl

theorem len_upd : ∀ (x : List Bool) (v : Nat) (b : Bool), (upd x v b).length = x.length
  | [], _, _ => rfl
  | _ :: _, 0, _ => rfl
  | _ :: x, v + 1, b => congrArg Nat.succ (len_upd x v b)

theorem nth_fz : ∀ (L i : Nat), nthB (fz L) i = false
  | 0, _ => rfl
  | _ + 1, 0 => rfl
  | L + 1, i + 1 => nth_fz L i

section
variable (σ : List Nat → Bool) (P : List (List Nat))

theorem Vs_ge (A : List Bool) (hA : A.length = P.length) : leB A (Vs σ P A) := vAux_ge _ P A hA.symm

def clS (A : List Bool) : List Bool := Closure.cl (Vs σ P) P.length A

theorem hV : ∀ A : List Bool, A.length = P.length → leB A (Vs σ P A) := fun A hA => Vs_ge σ P A hA

/-- **Closedness**, pointwise. -/
theorem closed (A : List Bool) (hA : A.length = P.length) (i : Nat) (hi : i < P.length) (hAi : nthB A i = true)
    (hr : (σ (nthL P i) || anyT P (clS σ P A) (nthL P i)) = true) : nthB (clS σ P A) i = true := by
  have hfix := cl_fix (Vs σ P) P.length (hV σ P) A hA
  have e : nthB (andB A (Vs σ P (clS σ P A))) i = nthB (clS σ P A) i := congrArg (fun z => nthB z i) hfix
  have hcl : (clS σ P A).length = P.length := cl_len (Vs σ P) P.length (hV σ P) A hA
  rw [nth_andB, Vs, nth_vAux _ P (clS σ P A) i hcl hi, hAi, hr, Bool.or_true] at e
  exact e.symm

theorem iter_nth (A : List Bool) (hA : A.length = P.length) (k i : Nat) (hi : i < P.length) :
    nthB (iter (Vs σ P) P.length A (k + 1)) i
      = (nthB A i && (nthB (iter (Vs σ P) P.length A k) i || (σ (nthL P i) || anyT P (iter (Vs σ P) P.length A k) (nthL P i)))) := by
  show nthB (andB A (Vs σ P (iter (Vs σ P) P.length A k))) i = _
  rw [nth_andB, Vs, nth_vAux _ P _ i (iter_len (Vs σ P) P.length (hV σ P) A hA k) hi]

end

/-- **Removing a seed.** -/
theorem remove_seed (σ : List Nat → Bool) (P : List (List Nat)) (A : List Bool) (hA : A.length = P.length)
    (v : Nat) (hv : v < P.length) (hAv : nthB A v = false)
    (hconn : clS σ P (upd A v true) = upd A v true) :
    clS (fun q => σ q || touch q (nthL P v)) P A = A := by
  let σ' : List Nat → Bool := fun q => σ q || touch q (nthL P v)
  let A1 := upd A v true
  have hA1 : A1.length = P.length := (len_upd A v true).trans hA
  let C' := clS σ' P A
  have hC'len : C'.length = P.length := cl_len (Vs σ' P) P.length (hV σ' P) A hA
  -- every iterate of the σ-closure of A1 lies in C' ∪ {v}
  have key : ∀ k i, i < P.length → nthB (iter (Vs σ P) P.length A1 k) i = true → i = v ∨ nthB C' i = true := by
    intro k
    induction k with
    | zero =>
      intro i _ h
      have h' : nthB (fz P.length) i = true := h
      rw [nth_fz] at h'
      exact absurd h' Bool.noConfusion
    | succ k ih =>
      intro i hi h
      rw [iter_nth σ P A1 hA1 k i hi] at h
      by_cases hiv : i = v
      · exact Or.inl hiv
      · refine Or.inr ?_
        have hA1i : nthB A1 i = nthB A i := by
          show nthB (upd A v true) i = _
          rw [nth_upd A v true i (by rw [hA]; exact hv), MaskSum.ineg hiv]
        cases eA : nthB A1 i with
        | false => rw [eA] at h; exact absurd h Bool.noConfusion
        | true =>
          rw [eA, Bool.true_and] at h
          have hAi : nthB A i = true := hA1i ▸ eA
          cases e1 : nthB (iter (Vs σ P) P.length A1 k) i with
          | true =>
            cases ih i hi e1 with
            | inl h' => exact absurd h' hiv
            | inr h' => exact h'
          | false =>
            rw [e1, Bool.false_or] at h
            cases e2 : σ (nthL P i) with
            | true =>
              exact closed σ' P A hA i hi hAi (by show ((σ (nthL P i) || _) || _) = true; rw [e2]; rfl)
            | false =>
              rw [e2, Bool.false_or] at h
              obtain ⟨j, hj, hj1, hj2⟩ := anyT_ex P _ _ h
              cases ih j hj hj1 with
              | inl hjv =>
                rw [hjv] at hj2
                exact closed σ' P A hA i hi hAi (by show ((σ (nthL P i) || _) || _) = true; rw [hj2, Bool.or_true]; rfl)
              | inr hjC =>
                exact closed σ' P A hA i hi hAi (by
                  show ((σ (nthL P i) || touch (nthL P i) (nthL P v)) || anyT P C' (nthL P i)) = true
                  rw [anyT_of P C' _ j hjC hj2 hj, Bool.or_true])
  -- A ⊆ C'
  have hsub : leB A C' := leB_of A C' (hA.trans hC'len.symm) (fun i hi => by
    have hiP : i < P.length := by
      cases Nat.lt_or_ge i P.length with
      | inl h => exact h
      | inr h => rw [nthB_ge A i (by rw [hA]; exact h)] at hi; exact absurd hi Bool.noConfusion
    have hiv : i ≠ v := fun e => by rw [e, hAv] at hi; exact Bool.noConfusion hi
    have hA1i : nthB A1 i = true := by
      show nthB (upd A v true) i = true
      rw [nth_upd A v true i (by rw [hA]; exact hv), MaskSum.ineg hiv]; exact hi
    have : nthB (iter (Vs σ P) P.length A1 P.length) i = true := by
      have := hconn
      show nthB (clS σ P A1) i = true
      rw [hconn]; exact hA1i
    cases key P.length i hiP this with
    | inl h => exact absurd h hiv
    | inr h => exact h)
  exact leB_antisymm _ _ (cl_le (Vs σ' P) P.length (hV σ' P) A hA) hsub

/-- **No seed, nothing connected.** -/
theorem no_seed (σ : List Nat → Bool) (P : List (List Nat)) (A : List Bool) (hA : A.length = P.length)
    (hns : ∀ i, i < P.length → nthB A i = true → σ (nthL P i) = false) : clS σ P A = fz P.length := by
  have hz : ∀ i, nthB (fz P.length) i = false := nth_fz P.length
  have hzl : (fz P.length).length = P.length := by
    induction P.length with
    | zero => rfl
    | succ L ih => exact congrArg Nat.succ ih
  have step : ∀ k, iter (Vs σ P) P.length A k = fz P.length := by
    intro k
    induction k with
    | zero => rfl
    | succ k ih =>
      have hl : (iter (Vs σ P) P.length A (k + 1)).length = P.length := iter_len (Vs σ P) P.length (hV σ P) A hA (k + 1)
      refine leB_antisymm _ _ (leB_of _ _ (hl.trans hzl.symm) (fun i h => ?_)) (fz_le _ _ hl)
      have hiP : i < P.length := by
        cases Nat.lt_or_ge i P.length with
        | inl h' => exact h'
        | inr h' => rw [nthB_ge _ i (by rw [hl]; exact h')] at h; exact absurd h Bool.noConfusion
      rw [iter_nth σ P A hA k i hiP, ih, hz] at h
      cases eA : nthB A i with
      | false => rw [eA] at h; exact absurd h Bool.noConfusion
      | true =>
        rw [eA, hns i hiP eA, Bool.true_and, Bool.false_or, Bool.false_or] at h
        obtain ⟨j, _, hj1, _⟩ := anyT_ex P _ _ h
        rw [hz] at hj1
        exact absurd hj1 Bool.noConfusion
  exact step P.length

end Seeds

#print axioms Seeds.nth_andB
#print axioms Seeds.nth_vAux
#print axioms Seeds.anyT_of
#print axioms Seeds.anyT_ex
#print axioms Seeds.leB_antisymm
#print axioms Seeds.nth_upd
#print axioms Seeds.closed
#print axioms Seeds.iter_nth
#print axioms Seeds.remove_seed
#print axioms Seeds.no_seed

ym/Count.lean

/-
  ym/Count — T45, cycle 33 (Lad1, any dimension): the connected-set sum is bounded by (1 + y)^(number of seeds).

  D σ U = Σ_{A ⊆ U, A σ-connected} x^|A|  (masks over the plaquette list P).
    split_at        Σ_A f A = Σ_{A : A_v = false} (f A + f (A ∪ {v}))
    D_rec           D σ U ≤ D σ (U \ v) + x · D σ' (U \ v),   σ' q = σ q ∨ (q touches q_v)        (any v ∈ U)
    D_bound         if every plaquette touches at most Dg plaquettes of P and x (1 + y)^Dg ≤ y, then
                      D σ U ≤ (1 + y)^(number of seeds in U)
  This bound depends only on the number of seeds (plaquettes touching the loop), not on the lattice.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Seeds
import ym.Tail

namespace Count

open YMChain LinkIntegral MaskSum Closure Factor Seeds

variable {R : Type} (K : DetChain.CRing R)

/-! ### Pointwise list facts -/

theorem ext_nth (x y : List Bool) (h : x.length = y.length) (hp : ∀ i, nthB x i = nthB y i) : x = y :=
  leB_antisymm x y (leB_of x y h (fun i hi => (hp i) ▸ hi)) (leB_of y x h.symm (fun i hi => (hp i).symm ▸ hi))

theorem len_andB : ∀ x y : List Bool, x.length = y.length → (andB x y).length = x.length
  | [], _, _ => rfl
  | _ :: _, [], h => absurd h Nat.noConfusion
  | _ :: x, _ :: y, h => congrArg Nat.succ (len_andB x y (Nat.succ.inj h))

theorem cnt_upd_true : ∀ (A : List Bool) (v : Nat), v < A.length → nthB A v = false → cnt (upd A v true) = cnt A + 1
  | [], _, h, _ => absurd h (Nat.not_lt_zero _)
  | false :: _, 0, _, _ => rfl
  | true :: _, 0, _, h => absurd h Bool.noConfusion
  | true :: A, v + 1, hv, h => by
    show cnt (upd A v true) + 1 = cnt A + 1 + 1
    rw [cnt_upd_true A v (Nat.lt_of_succ_lt_succ hv) h]
  | false :: A, v + 1, hv, h => cnt_upd_true A v (Nat.lt_of_succ_lt_succ hv) h

theorem cnt_upd_false : ∀ (A : List Bool) (v : Nat), v < A.length → nthB A v = true → cnt (upd A v false) + 1 = cnt A
  | [], _, h, _ => absurd h (Nat.not_lt_zero _)
  | true :: _, 0, _, _ => rfl
  | false :: _, 0, _, h => absurd h Bool.noConfusion
  | true :: A, v + 1, hv, h => by
    show cnt (upd A v false) + 1 + 1 = cnt A + 1
    rw [cnt_upd_false A v (Nat.lt_of_succ_lt_succ hv) h]
  | false :: A, v + 1, hv, h => cnt_upd_false A v (Nat.lt_of_succ_lt_succ hv) h

theorem andB_upd : ∀ (U M : List Bool) (v : Nat), U.length = M.length →
    andB (upd U v false) M = upd (andB U M) v false
  | [], _, _, _ => rfl
  | _ :: _, [], _, h => absurd h Nat.noConfusion
  | _ :: _, _ :: _, 0, _ => rfl
  | _ :: U, _ :: M, v + 1, h => by
    show _ :: andB (upd U v false) M = _ :: upd (andB U M) v false
    rw [andB_upd U M v (Nat.succ.inj h)]

theorem cnt_andB_or : ∀ U M1 M2 : List Bool, cnt (andB U (orB M1 M2)) ≤ cnt (andB U M1) + cnt M2
  | [], _, _ => Nat.zero_le _
  | _ :: _, [], _ => Nat.zero_le _
  | _ :: _, _ :: _, [] => Nat.zero_le _
  | u :: U, a :: M1, b :: M2 => by
    have ih := cnt_andB_or U M1 M2
    cases u <;> cases a <;> cases b
    · exact ih
    · exact Nat.le_succ_of_le ih
    · exact ih
    · exact Nat.le_succ_of_le ih
    · exact ih
    · show cnt _ + 1 ≤ cnt (andB U M1) + (cnt M2 + 1); exact Nat.succ_le_succ ih
    · show cnt _ + 1 ≤ cnt (andB U M1) + 1 + cnt M2; rw [Nat.succ_add]; exact Nat.succ_le_succ ih
    · show cnt _ + 1 ≤ cnt (andB U M1) + 1 + (cnt M2 + 1)
      rw [Nat.succ_add]; exact Nat.succ_le_succ (Nat.le_succ_of_le ih)

theorem map_or (σ τ : List Nat → Bool) : ∀ P : List (List Nat),
    P.map (fun q => σ q || τ q) = orB (P.map σ) (P.map τ)
  | [] => rfl
  | q :: P => by
    show (σ q || τ q) :: P.map (fun q => σ q || τ q) = (σ q || τ q) :: orB (P.map σ) (P.map τ)
    rw [map_or σ τ P]

theorem nth_map (σ : List Nat → Bool) : ∀ (P : List (List Nat)) (i : Nat), i < P.length → nthB (P.map σ) i = σ (nthL P i)
  | [], _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, 0, _ => rfl
  | _ :: P, i + 1, h => nth_map σ P i (Nat.lt_of_succ_lt_succ h)

theorem cnt_pos : ∀ x : List Bool, cnt x ≠ 0 → ∃ v, v < x.length ∧ nthB x v = true
  | [], h => absurd rfl h
  | true :: _, _ => ⟨0, Nat.succ_pos _, rfl⟩
  | false :: x, h => by
    obtain ⟨v, hv, h'⟩ := cnt_pos x h
    exact ⟨v + 1, Nat.succ_lt_succ hv, h'⟩

theorem cnt_of_nth : ∀ (M : List Bool) (i : Nat), nthB M i = true → 1 ≤ cnt M
  | [], _, h => absurd h Bool.noConfusion
  | true :: _, _, _ => Nat.succ_le_succ (Nat.zero_le _)
  | false :: _, 0, h => absurd h Bool.noConfusion
  | false :: M, i + 1, h => cnt_of_nth M i h

theorem len_map (σ : List Nat → Bool) : ∀ P : List (List Nat), (P.map σ).length = P.length
  | [] => rfl
  | _ :: P => congrArg Nat.succ (len_map σ P)

theorem cnt_fz : ∀ L, cnt (fz L) = 0
  | 0 => rfl
  | L + 1 => cnt_fz L

theorem len_fz : ∀ L, (fz L).length = L
  | 0 => rfl
  | L + 1 => congrArg Nat.succ (len_fz L)

/-! ### Splitting a mask sum at one coordinate -/

theorem split_at (f : List Bool → R) : ∀ (N v : Nat), v < N →
    sumK K f (bools N) = sumK K (fun A => if nthB A v = false then K.add (f A) (f (upd A v true)) else K.zero) (bools N)
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | N + 1, 0, _ => by
    show sumK K f ((bools N).map (List.cons true) ++ (bools N).map (List.cons false))
      = sumK K _ ((bools N).map (List.cons true) ++ (bools N).map (List.cons false))
    rw [sumK_app, sumK_app, MaskSum.sumK_map, MaskSum.sumK_map, MaskSum.sumK_map, MaskSum.sumK_map]
    have e1 : sumK K (fun A => if nthB (true :: A) 0 = false then K.add (f (true :: A)) (f (upd (true :: A) 0 true))
        else K.zero) (bools N) = K.zero :=
      (sumK_ext K _ _ (fun A => ineg (fun e => Bool.noConfusion e)) _).trans (sum_zero K _)
    have e2 : sumK K (fun A => if nthB (false :: A) 0 = false then K.add (f (false :: A)) (f (upd (false :: A) 0 true))
        else K.zero) (bools N) = sumK K (fun A => K.add (f (false :: A)) (f (true :: A))) (bools N) :=
      sumK_ext K _ _ (fun A => ip rfl) _
    rw [e1, e2, K.zero_add, sumK_add, K.add_comm]
  | N + 1, v + 1, hv => by
    show sumK K f ((bools N).map (List.cons true) ++ (bools N).map (List.cons false))
      = sumK K _ ((bools N).map (List.cons true) ++ (bools N).map (List.cons false))
    rw [sumK_app, sumK_app, MaskSum.sumK_map, MaskSum.sumK_map, MaskSum.sumK_map, MaskSum.sumK_map,
      split_at (fun A => f (true :: A)) N v (Nat.lt_of_succ_lt_succ hv),
      split_at (fun A => f (false :: A)) N v (Nat.lt_of_succ_lt_succ hv)]
    rfl

/-! ### The recursion -/

def D (x : R) (σ : List Nat → Bool) (P : List (List Nat)) (U : List Bool) : R :=
  sumK K (fun A => if andB A U = A ∧ clS σ P A = A then pwR K x (cnt A) else K.zero) (bools P.length)

variable (O : ORing K)

theorem D_nn (h1 : O.le K.zero K.one) (x : R) (hx : O.le K.zero x) (σ : List Nat → Bool) (P : List (List Nat))
    (U : List Bool) : O.le K.zero (D K x σ P U) := by
  have := MaskSum.sumK_le K O (fun _ => K.zero)
    (fun A => if andB A U = A ∧ clS σ P A = A then pwR K x (cnt A) else K.zero) (bools P.length) (fun A _ => by
      by_cases e : andB A U = A ∧ clS σ P A = A
      · rw [ip e]; exact AreaLower.pw_nn K O x hx h1 _
      · rw [ineg e]; exact O.le_refl _)
  rw [sum_zero] at this
  exact this

/-- **One step of the recursion**, at any `v ∈ U`. -/
theorem D_rec (h1 : O.le K.zero K.one) (x : R) (hx : O.le K.zero x) (σ : List Nat → Bool) (P : List (List Nat))
    (U : List Bool) (hU : U.length = P.length) (v : Nat) (hv : v < P.length) :
    O.le (D K x σ P U)
      (K.add (D K x σ P (upd U v false)) (K.mul x (D K x (fun q => σ q || touch q (nthL P v)) P (upd U v false)))) := by
  let σ' : List Nat → Bool := fun q => σ q || touch q (nthL P v)
  let U1 := upd U v false
  have hU1 : U1.length = P.length := (len_upd U v false).trans hU
  let f : List Bool → R := fun A => if andB A U = A ∧ clS σ P A = A then pwR K x (cnt A) else K.zero
  let g1 : List Bool → R := fun A => if andB A U1 = A ∧ clS σ P A = A then pwR K x (cnt A) else K.zero
  let g2 : List Bool → R := fun A => if andB A U1 = A ∧ clS σ' P A = A then pwR K x (cnt A) else K.zero
  have g1nn : ∀ A, O.le K.zero (g1 A) := fun A => by
    show O.le K.zero (if _ then _ else _)
    by_cases e : andB A U1 = A ∧ clS σ P A = A
    · rw [ip e]; exact AreaLower.pw_nn K O x hx h1 _
    · rw [ineg e]; exact O.le_refl _
  have g2nn : ∀ A, O.le K.zero (g2 A) := fun A => by
    show O.le K.zero (if _ then _ else _)
    by_cases e : andB A U1 = A ∧ clS σ' P A = A
    · rw [ip e]; exact AreaLower.pw_nn K O x hx h1 _
    · rw [ineg e]; exact O.le_refl _
  show O.le (sumK K f (bools P.length)) (K.add (sumK K g1 (bools P.length)) (K.mul x (sumK K g2 (bools P.length))))
  rw [split_at K f P.length v hv, mul_sumK, ← sumK_add]
  refine MaskSum.sumK_le K O _ _ (bools P.length) (fun A hA => ?_)
  have hAl := ChangeVar2D.mem_bools_len P.length A hA
  by_cases hAv : nthB A v = false
  · rw [ip hAv]
    -- subset conditions agree off v
    have sub_iff : andB A U = A → andB A U1 = A := fun h => by
      refine ext_nth _ _ ((len_andB A U1 (hAl.trans hU1.symm))) (fun i => ?_)
      rw [nth_andB]
      show (nthB A i && nthB (upd U v false) i) = nthB A i
      rw [nth_upd U v false i (by rw [hU]; exact hv)]
      by_cases e : i = v
      · rw [ip e, e, hAv]; rfl
      · rw [ineg e]
        have := congrArg (fun z => nthB z i) h
        simp only [nth_andB] at this
        exact this
    have t1 : O.le (f A) (g1 A) := by
      show O.le (if _ then _ else _) (if _ then _ else _)
      by_cases e : andB A U = A ∧ clS σ P A = A
      · rw [ip e, ip ⟨sub_iff e.1, e.2⟩]; exact O.le_refl _
      · rw [ineg e]; exact g1nn A
    have t2 : O.le (f (upd A v true)) (K.mul x (g2 A)) := by
      show O.le (if _ then _ else _) (K.mul x (if _ then _ else _))
      by_cases e : andB (upd A v true) U = upd A v true ∧ clS σ P (upd A v true) = upd A v true
      · have hsubU : andB A U = A := by
          refine ext_nth _ _ (len_andB A U (hAl.trans hU.symm)) (fun i => ?_)
          rw [nth_andB]
          have := congrArg (fun z => nthB z i) e.1
          simp only [nth_andB] at this
          rw [nth_upd A v true i (by rw [hAl]; exact hv)] at this
          by_cases ei : i = v
          · rw [ei, hAv]; rfl
          · rw [ineg ei] at this; exact this
        have hcon := remove_seed σ P A hAl v hv hAv e.2
        rw [ip e, ip ⟨sub_iff hsubU, hcon⟩, cnt_upd_true A v (by rw [hAl]; exact hv) hAv]
        show O.le (K.mul (pwR K x (cnt A)) x) (K.mul x (pwR K x (cnt A)))
        rw [K.mul_comm]; exact O.le_refl _
      · rw [ineg e]; exact O.mul_nonneg _ _ hx (g2nn A)
    exact O.add_le_add _ _ _ _ t1 t2
  · rw [ineg hAv]
    have := O.add_le_add _ _ _ _ (g1nn A) (O.mul_nonneg _ _ hx (g2nn A))
    rw [K.zero_add] at this
    exact this

/-! ### The bound -/

theorem pw_mono (h1 : O.le K.zero K.one) (z : R) (hz : O.le K.one z) : ∀ a j, O.le (pwR K z a) (pwR K z (a + j))
  | a, 0 => O.le_refl _
  | a, j + 1 => by
    have ih := pw_mono h1 z hz a j
    have hz0 : O.le K.zero z := O.le_trans _ _ _ h1 hz
    have := O.mul_le_mul_right _ _ _ hz (AreaLower.pw_nn K O z hz0 h1 (a + j))
    rw [K.one_mul, K.mul_comm] at this
    exact O.le_trans _ _ _ ih this

theorem D_base (h1 : O.le K.zero K.one) (x : R) (hx : O.le K.zero x) (σ : List Nat → Bool) (P : List (List Nat))
    (U : List Bool) (hU : U.length = P.length) (hs : cnt (andB U (P.map σ)) = 0) : O.le (D K x σ P U) K.one := by
  show O.le (sumK K _ (bools P.length)) K.one
  have hnos : ∀ A, A.length = P.length → andB A U = A → ∀ i, i < P.length → nthB A i = true → σ (nthL P i) = false := by
    intro A _ hsub i hi hAi
    cases e : σ (nthL P i) with
    | false => rfl
    | true =>
      have hUi : nthB U i = true := by
        have := congrArg (fun z => nthB z i) hsub
        simp only [nth_andB] at this
        rw [hAi] at this
        cases eU : nthB U i
        · rw [eU] at this; exact absurd this Bool.noConfusion
        · rfl
      have : nthB (andB U (P.map σ)) i = true := by rw [nth_andB, hUi, nth_map σ P i hi, e]; rfl
      have hle := cnt_of_nth _ i this
      rw [hs] at hle
      exact absurd hle (Nat.not_succ_le_zero 0)
  have hterm : ∀ A, memL A (bools P.length) →
      O.le (if andB A U = A ∧ clS σ P A = A then pwR K x (cnt A) else K.zero)
        (if A = fz P.length then K.one else K.zero) := by
    intro A hA
    have hAl := ChangeVar2D.mem_bools_len P.length A hA
    by_cases e : andB A U = A ∧ clS σ P A = A
    · have hz : A = fz P.length := by
        have := no_seed σ P A hAl (hnos A hAl e.1)
        rw [e.2] at this
        exact this
      rw [ip e, ip hz, hz, cnt_fz]
      exact O.le_refl _
    · rw [ineg e]
      by_cases hz : A = fz P.length
      · rw [ip hz]; exact h1
      · rw [ineg hz]; exact O.le_refl _
  have := MaskSum.sumK_le K O _ _ (bools P.length) hterm
  rw [sel K K.one P.length (fz P.length) (len_fz _)] at this
  exact this

/-- **The connected-set sum is bounded by the number of seeds.** -/
theorem D_bound (h1 : O.le K.zero K.one) (x y : R) (hx : O.le K.zero x) (hy : O.le K.zero y)
    (P : List (List Nat)) (Dg : Nat)
    (hdeg : ∀ v, v < P.length → cnt (P.map (fun q => touch q (nthL P v))) ≤ Dg)
    (hxy : O.le (K.mul x (pwR K (K.add K.one y) Dg)) y) :
    ∀ (fuel : Nat) (σ : List Nat → Bool) (U : List Bool), U.length = P.length → cnt U ≤ fuel →
      O.le (D K x σ P U) (pwR K (K.add K.one y) (cnt (andB U (P.map σ)))) := by
  have hz1 : O.le K.one (K.add K.one y) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) hy
    rw [DetChain.add_zero'] at this
    exact this
  have hz0 : O.le K.zero (K.add K.one y) := O.le_trans _ _ _ h1 hz1
  intro fuel
  induction fuel with
  | zero =>
    intro σ U hU hc
    have hc0 : cnt U = 0 := Nat.le_zero.mp hc
    have hs : cnt (andB U (P.map σ)) = 0 := by
      have := leB_cnt _ _ (andB_le U (P.map σ) (hU.trans (len_map _ P).symm))
      rw [hc0] at this
      exact Nat.le_zero.mp this
    rw [hs]
    exact D_base K O h1 x hx σ P U hU hs
  | succ fuel ih =>
    intro σ U hU hc
    by_cases hs : cnt (andB U (P.map σ)) = 0
    · rw [hs]; exact D_base K O h1 x hx σ P U hU hs
    · obtain ⟨v, hv, hvt⟩ := cnt_pos _ hs
      have hvl : v < P.length := by
        rw [len_andB U _ (hU.trans (len_map _ P).symm), hU] at hv; exact hv
      have hUv : nthB U v = true := by
        rw [nth_andB] at hvt
        cases e : nthB U v
        · rw [e] at hvt; exact absurd hvt Bool.noConfusion
        · rfl
      let U1 := upd U v false
      have hU1 : U1.length = P.length := (len_upd U v false).trans hU
      have hcU1 : cnt U1 ≤ fuel := by
        have := cnt_upd_false U v (by rw [hU]; exact hvl) hUv
        rw [← this] at hc
        exact Nat.le_of_succ_le_succ hc
      let s := cnt (andB U (P.map σ))
      have hs1 : cnt (andB U1 (P.map σ)) + 1 = s := by
        show cnt (andB (upd U v false) (P.map σ)) + 1 = s
        rw [andB_upd U _ v (hU.trans (len_map _ P).symm)]
        exact cnt_upd_false _ v (by rw [len_andB U _ (hU.trans (len_map _ P).symm), hU]; exact hvl) hvt
      let σ' : List Nat → Bool := fun q => σ q || touch q (nthL P v)
      have hs2 : cnt (andB U1 (P.map σ')) ≤ cnt (andB U1 (P.map σ)) + Dg := by
        have e : P.map σ' = orB (P.map σ) (P.map (fun q => touch q (nthL P v))) := map_or σ _ P
        rw [e]
        exact Nat.le_trans (cnt_andB_or _ _ _) (Nat.add_le_add_left (hdeg v hvl) _)
      have r := D_rec K O h1 x hx σ P U hU v hvl
      have b1 := ih σ U1 hU1 hcU1
      have b2 := ih σ' U1 hU1 hcU1
      let s1 := cnt (andB U1 (P.map σ))
      have b2' : O.le (D K x σ' P U1) (pwR K (K.add K.one y) (s1 + Dg)) := by
        refine O.le_trans _ _ _ b2 ?_
        obtain ⟨j, hj⟩ := Nat.le.dest hs2
        rw [← hj]
        exact pw_mono K O h1 _ hz1 _ j
      -- combine
      have c1 := O.add_le_add _ _ _ _ b1 (O.mul_le_mul_right _ _ _ b2' hx)
      have c1' : O.le (D K x σ P U)
          (K.add (pwR K (K.add K.one y) s1) (K.mul (pwR K (K.add K.one y) (s1 + Dg)) x)) := by
        refine O.le_trans _ _ _ r ?_
        rw [K.mul_comm x (D K x σ' P U1)]
        exact c1
      refine O.le_trans _ _ _ c1' ?_
      have hs1' : cnt (andB U (P.map σ)) = s1 + 1 := hs1.symm
      rw [hs1', GKS.pwR_add, K.mul_assoc]
      let z := K.add K.one y
      let p := pwR K z s1
      let a := K.mul (pwR K z Dg) x
      have hb : O.le (K.add K.one a) z := by
        show O.le (K.add K.one (K.mul (pwR K z Dg) x)) (K.add K.one y)
        rw [K.mul_comm]; exact O.add_le_add _ _ _ _ (O.le_refl _) hxy
      have hm := O.mul_le_mul_right _ _ _ hb (AreaLower.pw_nn K O z hz0 h1 s1)
      have e1 : K.mul (K.add K.one a) p = K.add p (K.mul p a) := by
        rw [K.mul_comm, K.distrib, K.mul_comm p K.one, K.one_mul]
      have e2 : K.mul z p = pwR K z (s1 + 1) := K.mul_comm _ _
      rw [e1, e2] at hm
      exact hm

end Count

#print axioms Count.ext_nth
#print axioms Count.cnt_andB_or
#print axioms Count.split_at
#print axioms Count.D_rec
#print axioms Count.D_base
#print axioms Count.D_bound

ym/AreaUpper.lean

/-
  ym/AreaUpper — T45, cycle 34 (Lad1, ℤ₂, every dimension): the upper area law, uniform in the volume.

  ℤ₂ lattice gauge theory on any finite set of plaquettes P (any dimension, any boundary), weight Π_q (1 + t U_q),
  t = tanh β = u · x. Hypotheses, all local:
    hdeg   every plaquette shares a link with at most Dg plaquettes of P      (Dg = 8d − 11 on Z^d suffices)
    hxy    x (1 + y)^Dg ≤ y                                                    (e.g. y = 1, x = 2^(−Dg))
    hm     every plaquette set A with ∂A = W has at least m plaquettes        (m = area of W)
    s      the number of plaquettes sharing a link with W                     (≤ 2(d−1)|W|)
  Then
    area_upper_G     G_W ≤ u^m (1 + y)^s · G_0
    area_upper       N_W ≤ u^m (1 + y)^s · Z,   i.e.   ⟨W⟩ ≤ (1 + y)^s · u^Area(W)
  The constants do not depend on P: the bound holds uniformly in the volume. With GKS (ym/AreaLower:
  ⟨W⟩ ≥ t^Area) this brackets ⟨W⟩ between two area laws at small t — rung Lad1's area-law part.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Count

namespace AreaUpper

open YMChain LinkIntegral GKS StrongZ2 MaskSum Closure Factor Seeds Tail Count

variable {R : Type} (K : DetChain.CRing R)

def tr : Nat → List Bool
  | 0 => []
  | N + 1 => true :: tr N

theorem len_tr : ∀ N, (tr N).length = N
  | 0 => rfl
  | N + 1 => congrArg Nat.succ (len_tr N)

theorem andB_tr : ∀ A : List Bool, andB A (tr A.length) = A
  | [] => rfl
  | a :: A => by
    show (a && true) :: andB A (tr A.length) = a :: A
    rw [Bool.and_true, andB_tr A]

theorem andB_tr' : ∀ M : List Bool, andB (tr M.length) M = M
  | [] => rfl
  | a :: M => by
    show (true && a) :: andB (tr M.length) M = a :: M
    rw [Bool.true_and, andB_tr' M]

theorem cnt_tr : ∀ N, cnt (tr N) = N
  | 0 => rfl
  | N + 1 => congrArg Nat.succ (cnt_tr N)

theorem DW_eq_D (x : R) (n : List Nat) (P : List (List Nat)) :
    DW K x n P = D K x (fun q => touch q n) P (tr P.length) := by
  refine sumK_ext_mem K _ _ (bools P.length) (fun A hA => ?_)
  have hl := ChangeVar2D.mem_bools_len P.length A hA
  have e : andB A (tr P.length) = A := by rw [← hl]; exact andB_tr A
  by_cases h : Closure.cl (V n P) P.length A = A
  · rw [ip h, ip ⟨e, h⟩]
  · rw [ineg h, ineg (fun h' => h h'.2)]

variable (O : ORing K)

/-- **Upper area law for the surface sums.** -/
theorem area_upper_G (h1 : O.le K.zero K.one) (u x y : R) (hu0 : O.le K.zero u) (hu1 : O.le u K.one)
    (hx : O.le K.zero x) (hy : O.le K.zero y) (n : List Nat) (P : List (List Nat)) (Dg m : Nat)
    (hdeg : ∀ v, v < P.length → cnt (P.map (fun q => touch q (nthL P v))) ≤ Dg)
    (hxy : O.le (K.mul x (pwR K (K.add K.one y) Dg)) y)
    (hm : ∀ A : List Bool, A.length = P.length → evenAll (zadd n (bdS P A)) = true → m ≤ cnt A) :
    O.le (G K (K.mul u x) n P)
      (K.mul (K.mul (pwR K u m) (pwR K (K.add K.one y) (cnt (P.map (fun q => touch q n)))))
        (G K (K.mul u x) (zl n.length) P)) := by
  have ht : O.le K.zero (K.mul u x) := O.mul_nonneg _ _ hu0 hx
  have f := factor K O h1 (K.mul u x) ht n P
  have c := CW_tail K O h1 u x hu0 hu1 hx n P m hm
  have d := D_bound K O h1 x y hx hy P Dg hdeg hxy P.length (fun q => touch q n) (tr P.length) (len_tr _)
    (by rw [cnt_tr]; exact Nat.le_refl _)
  rw [← len_map (fun q => touch q n) P, andB_tr', len_map] at d
  rw [← DW_eq_D] at d
  have G0nn : O.le K.zero (G K (K.mul u x) (zl n.length) P) := G_nn K O h1 _ ht P _
  have d' := O.mul_le_mul_right _ _ _ d (AreaLower.pw_nn K O u hu0 h1 m)
  rw [K.mul_comm (DW K x n P), K.mul_comm (pwR K (K.add K.one y) _)] at d'
  have cd := O.le_trans _ _ _ c d'
  exact O.le_trans _ _ _ f (O.mul_le_mul_right _ _ _ cd G0nn)

/-- **Upper area law**: `N_W ≤ u^m (1 + y)^s · Z`, i.e. `⟨W⟩ ≤ (1 + y)^s u^Area`, uniformly in the volume. -/
theorem area_upper (h1 : O.le K.zero K.one) (u x y : R) (hu0 : O.le K.zero u) (hu1 : O.le u K.one)
    (hx : O.le K.zero x) (hy : O.le K.zero y) (L : Nat) (n : List Nat) (hn : n.length = L)
    (P : List (List Nat)) (hP : ∀ q, memL q P → q.length = L) (Dg m : Nat)
    (hdeg : ∀ v, v < P.length → cnt (P.map (fun q => touch q (nthL P v))) ≤ Dg)
    (hxy : O.le (K.mul x (pwR K (K.add K.one y) Dg)) y)
    (hm : ∀ A : List Bool, A.length = P.length → evenAll (zadd n (bdS P A)) = true → m ≤ cnt A) :
    O.le (N K (K.mul u x) L n P)
      (K.mul (K.mul (pwR K u m) (pwR K (K.add K.one y) (cnt (P.map (fun q => touch q n)))))
        (N K (K.mul u x) L (zl L) P)) := by
  have eW := N_eq K (K.mul u x) P n (fun q hq => (hP q hq).trans hn.symm)
  have e0 := N_eq K (K.mul u x) P (zl L) (fun q hq => (hP q hq).trans (len_zl L).symm)
  rw [hn] at eW
  rw [len_zl] at e0
  rw [eW, e0]
  have g := area_upper_G K O h1 u x y hu0 hu1 hx hy n P Dg m hdeg hxy hm
  rw [hn] at g
  have := O.mul_le_mul_right _ _ _ g (two_pow_nn K O h1 L)
  rw [K.mul_comm (pwR K (two K) L) (G K _ n P), K.mul_comm (pwR K (two K) L) (G K _ (zl L) P), ← K.mul_assoc]
  exact this

end AreaUpper

#print axioms AreaUpper.andB_tr
#print axioms AreaUpper.DW_eq_D
#print axioms AreaUpper.area_upper_G
#print axioms AreaUpper.area_upper

ym/GPerm.lean

/-
  ym/GPerm — T27, cycle 73: the surface sum does not depend on the order of the plaquettes.
    G_swap     G t n (x :: y :: l) = G t n (y :: x :: l)
    G_perm     P ~ P' (List.Perm) ⇒ G t n P = G t n P'
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AreaBox

namespace GPerm

open YMChain GKS StrongZ2

section Main
variable {R : Type} (K : DetChain.CRing R)

theorem G_swap (t : R) (n x y : List Nat) (l : List (List Nat)) : G K t n (x :: y :: l) = G K t n (y :: x :: l) := by
  show K.add (K.add (G K t n l) (K.mul t (G K t (zadd n y) l))) (K.mul t (K.add (G K t (zadd n x) l) (K.mul t (G K t (zadd (zadd n x) y) l)))) =
    K.add (K.add (G K t n l) (K.mul t (G K t (zadd n x) l))) (K.mul t (K.add (G K t (zadd n y) l) (K.mul t (G K t (zadd (zadd n y) x) l))))
  have e : zadd (zadd n x) y = zadd (zadd n y) x := by
    rw [S2cDec.zadd_assoc, S2cDec.zadd_assoc, S2cDec.zadd_comm x y]
  rw [e]
  generalize G K t n l = a
  generalize G K t (zadd n y) l = b
  generalize G K t (zadd n x) l = c
  generalize G K t (zadd (zadd n y) x) l = d
  rw [K.distrib, K.distrib, ← K.add_assoc, ← K.add_assoc, K.add_assoc a, K.add_comm (K.mul t b), ← K.add_assoc a]

/-- **G is invariant under permutations of the plaquette list.** -/
theorem G_perm (t : R) : ∀ (P P' : List (List Nat)), P.Perm P' → ∀ n, G K t n P = G K t n P' := by
  intro P P' h
  induction h with
  | nil => intro n; rfl
  | cons x _ ih =>
    intro n
    show K.add (G K t n _) (K.mul t (G K t (zadd n x) _)) = K.add (G K t n _) (K.mul t (G K t (zadd n x) _))
    rw [ih n, ih (zadd n x)]
  | swap x y l => intro n; exact G_swap K t n y x l
  | trans _ _ ih1 ih2 => intro n; rw [ih1 n, ih2 n]

end Main

end GPerm

#print axioms GPerm.G_swap
#print axioms GPerm.G_perm

ym/Ratio.lean

/-
  ym/Ratio — T45, cycle 37 (Lad1, ℤ₂, any dimension): the partition-function ratio inequality.

  Masks over one plaquette list P; G(n, M) = G n (selL P M); M \ X = andB M (notB X).
    split_pw        Σ_S f S = Σ_{Y ⊆ X} Σ_{Z ⊆ ¬X} f (Y ∪ Z)                 (pointwise split of a mask sum)
    term_union      term n (Y ∪ Z) = t^|Y| · term (n + ∂Y) Z                    (Y, Z disjoint)
    expand_X        G(n, M) = Σ_{Y ⊆ M ∩ X} t^|Y| G(n + ∂Y, M \ X)
    ratio           B ⊆ P:  G(0, P \ X) · G(0, B) ≤ G(0, B \ X) · G(0, P)
  In words: deleting a set X of plaquettes costs relatively more in the larger volume,
  Z(P \ X)/Z(P) ≤ Z(B \ X)/Z(B) — the GKS input for comparing finite-volume expectations.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.MaskMono

namespace Ratio

open YMChain LinkIntegral GKS StrongZ2 MaskSum Closure Factor Seeds Count GPerm MaskMono Tail

variable {R : Type} (K : DetChain.CRing R)

theorem ite_iff {α : Type} {c d : Prop} [Decidable c] [Decidable d] (h : c ↔ d) (a b : α) :
    (if c then a else b) = (if d then a else b) := by
  by_cases hc : c
  · rw [ip hc, ip (h.1 hc)]
  · rw [ineg hc, ineg (fun hd => hc (h.2 hd))]

theorem cons_iff (a b : Bool) (x y : List Bool) : (a :: x = b :: y) ↔ (a = b ∧ x = y) :=
  ⟨fun h => ⟨List.head_eq_of_cons_eq h, List.tail_eq_of_cons_eq h⟩, fun h => by rw [h.1, h.2]⟩

theorem head_iff (x s y z : Bool) :
    ((y && x) = y ∧ (z && !x) = z ∧ (y || z) = s) ↔ (y = (s && x) ∧ z = (s && !x)) := by
  cases x <;> cases s <;> cases y <;> cases z <;> decide

theorem decomp_iff : ∀ (X S Y Z : List Bool), S.length = X.length → Y.length = X.length → Z.length = X.length →
    ((andB Y X = Y ∧ andB Z (notB X) = Z ∧ orB Y Z = S) ↔ (Y = andB S X ∧ Z = andB S (notB X)))
  | [], [], [], [], _, _, _ => ⟨fun _ => ⟨rfl, rfl⟩, fun _ => ⟨rfl, rfl, rfl⟩⟩
  | [], _ :: _, _, _, h, _, _ => absurd h Nat.noConfusion
  | [], [], _ :: _, _, _, h, _ => absurd h Nat.noConfusion
  | [], [], [], _ :: _, _, _, h => absurd h Nat.noConfusion
  | _ :: _, [], _, _, h, _, _ => absurd h Nat.noConfusion
  | _ :: _, _ :: _, [], _, _, h, _ => absurd h Nat.noConfusion
  | _ :: _, _ :: _, _ :: _, [], _, _, h => absurd h Nat.noConfusion
  | x :: X, s :: S, y :: Y, z :: Z, hS, hY, hZ => by
    have ih := decomp_iff X S Y Z (Nat.succ.inj hS) (Nat.succ.inj hY) (Nat.succ.inj hZ)
    have hh := head_iff x s y z
    show (((y && x) :: andB Y X = y :: Y) ∧ ((z && !x) :: andB Z (notB X) = z :: Z) ∧ ((y || z) :: orB Y Z = s :: S))
      ↔ ((y :: Y = (s && x) :: andB S X) ∧ (z :: Z = (s && !x) :: andB S (notB X)))
    have C := cons_iff
    exact ⟨fun h =>
        have a1 := (C _ _ _ _).1 h.1
        have a2 := (C _ _ _ _).1 h.2.1
        have a3 := (C _ _ _ _).1 h.2.2
        ⟨(C _ _ _ _).2 ⟨(hh.1 ⟨a1.1, a2.1, a3.1⟩).1, (ih.1 ⟨a1.2, a2.2, a3.2⟩).1⟩,
         (C _ _ _ _).2 ⟨(hh.1 ⟨a1.1, a2.1, a3.1⟩).2, (ih.1 ⟨a1.2, a2.2, a3.2⟩).2⟩⟩,
      fun h =>
        have b1 := (C _ _ _ _).1 h.1
        have b2 := (C _ _ _ _).1 h.2
        ⟨(C _ _ _ _).2 ⟨(hh.2 ⟨b1.1, b2.1⟩).1, (ih.2 ⟨b1.2, b2.2⟩).1⟩,
         (C _ _ _ _).2 ⟨(hh.2 ⟨b1.1, b2.1⟩).2.1, (ih.2 ⟨b1.2, b2.2⟩).2.1⟩,
         (C _ _ _ _).2 ⟨(hh.2 ⟨b1.1, b2.1⟩).2.2, (ih.2 ⟨b1.2, b2.2⟩).2.2⟩⟩⟩

theorem len_notB : ∀ x : List Bool, (notB x).length = x.length
  | [] => rfl
  | _ :: x => congrArg Nat.succ (len_notB x)

/-- **Pointwise split of a mask sum.** -/
theorem split_pw (f : List Bool → R) (X : List Bool) :
    sumK K f (bools X.length) = sumK K (fun Y => sumK K (fun Z =>
      if andB Y X = Y ∧ andB Z (notB X) = Z then f (orB Y Z) else K.zero) (bools X.length)) (bools X.length) := by
  let N := X.length
  have hmem := ChangeVar2D.mem_bools_len N
  -- step 1: each term as a selection over S
  have s1 : ∀ Y Z, Y.length = N → Z.length = N →
      (if andB Y X = Y ∧ andB Z (notB X) = Z then f (orB Y Z) else K.zero)
        = sumK K (fun S => if andB Y X = Y ∧ andB Z (notB X) = Z ∧ orB Y Z = S then f S else K.zero) (bools N) := by
    intro Y Z hY hZ
    by_cases c : andB Y X = Y ∧ andB Z (notB X) = Z
    · rw [ip c, ← sel K (f (orB Y Z)) N (orB Y Z) (len_orB N Y Z hY hZ)]
      exact sumK_ext K _ _ (fun S => by
        by_cases e : S = orB Y Z
        · rw [ip e, ip ⟨c.1, c.2, e.symm⟩, e]
        · rw [ineg e, ineg (fun h => e h.2.2.symm)]) _
    · rw [ineg c]
      exact ((sumK_ext K _ _ (fun S => ineg (fun h => c ⟨h.1, h.2.1⟩)) _).trans (sum_zero K _)).symm
  rw [sumK_ext_mem K _ _ (bools N) (fun Y hY => sumK_ext_mem K _ _ (bools N) (fun Z hZ => s1 Y Z (hmem Y hY) (hmem Z hZ)))]
  -- step 2: swap
  rw [sumK_ext K _ _ (fun Y => sumK_swap K _ (bools N) (bools N)), sumK_swap]
  -- step 3: inner = f S
  refine (sumK_ext_mem K _ _ (bools N) (fun S hS => ?_)).symm
  have hSl := hmem S hS
  have e3 : ∀ Y Z, Y.length = N → Z.length = N →
      (if andB Y X = Y ∧ andB Z (notB X) = Z ∧ orB Y Z = S then f S else K.zero)
        = (if Y = andB S X then (if Z = andB S (notB X) then f S else K.zero) else K.zero) := by
    intro Y Z hY hZ
    have iff := decomp_iff X S Y Z hSl hY hZ
    by_cases c1 : Y = andB S X
    · by_cases c2 : Z = andB S (notB X)
      · rw [ip (iff.2 ⟨c1, c2⟩), ip c1, ip c2]
      · rw [ineg (fun h => c2 (iff.1 h).2), ip c1, ineg c2]
    · rw [ineg (fun h => c1 (iff.1 h).1), ineg c1]
  rw [sumK_ext_mem K _ _ (bools N) (fun Y hY => sumK_ext_mem K _ _ (bools N) (fun Z hZ => e3 Y Z (hmem Y hY) (hmem Z hZ)))]
  have hl1 : (andB S X).length = N := (len_andB S X hSl).trans hSl
  have hl2 : (andB S (notB X)).length = N := (len_andB S (notB X) (hSl.trans (len_notB X).symm)).trans hSl
  have e4 : ∀ Y, sumK K (fun Z => if Y = andB S X then (if Z = andB S (notB X) then f S else K.zero) else K.zero) (bools N)
      = (if Y = andB S X then f S else K.zero) := by
    intro Y
    by_cases c1 : Y = andB S X
    · rw [ip c1]
      exact (sumK_ext K _ _ (fun Z => ip c1) _).trans (sel K (f S) N _ hl2)
    · rw [ineg c1]
      exact (sumK_ext K _ _ (fun Z => ineg c1) _).trans (sum_zero K _)
  rw [sumK_ext K _ _ e4]
  exact sel K (f S) N _ hl1

theorem term_union (t : R) : ∀ (P : List (List Nat)) (Y Z : List Bool) (n : List Nat),
    Y.length = P.length → Z.length = P.length → disjM Y Z →
    MaskSum.term K t n P (orB Y Z) = K.mul (pwR K t (cnt Y)) (MaskSum.term K t (zadd n (bdS P Y)) P Z)
  | [], [], [], n, _, _, _ => by
    show (if evenAll n = true then K.one else K.zero) = K.mul K.one (if evenAll (zadd n []) = true then K.one else K.zero)
    rw [AreaLower.zadd_nil, K.one_mul]
  | [], _ :: _, _, _, h, _, _ => absurd h Nat.noConfusion
  | [], [], _ :: _, _, _, h, _ => absurd h Nat.noConfusion
  | _ :: _, [], _, _, h, _, _ => absurd h Nat.noConfusion
  | _ :: _, _ :: _, [], _, _, h, _ => absurd h Nat.noConfusion
  | q :: P, y :: Y, z :: Z, n, hY, hZ, hd => by
    have hY' := Nat.succ.inj hY
    have hZ' := Nat.succ.inj hZ
    cases y <;> cases z
    · exact term_union t P Y Z n hY' hZ' hd.2
    · show K.mul t (MaskSum.term K t (zadd n q) P (orB Y Z))
        = K.mul (pwR K t (cnt Y)) (K.mul t (MaskSum.term K t (zadd (zadd n (bdS P Y)) q) P Z))
      rw [term_union t P Y Z (zadd n q) hY' hZ' hd.2, Decouple.zadd_assoc, AreaLower.zadd_comm q,
        ← Decouple.zadd_assoc, ← K.mul_assoc, K.mul_comm t, K.mul_assoc]
    · show K.mul t (MaskSum.term K t (zadd n q) P (orB Y Z))
        = K.mul (K.mul (pwR K t (cnt Y)) t) (MaskSum.term K t (zadd n (zadd q (bdS P Y))) P Z)
      rw [term_union t P Y Z (zadd n q) hY' hZ' hd.2, Decouple.zadd_assoc, ← K.mul_assoc, K.mul_comm t]
    · exact absurd ⟨rfl, rfl⟩ hd.1

theorem cond_iff : ∀ (X M Y Z : List Bool), M.length = X.length → Y.length = X.length → Z.length = X.length →
    (((andB Y X = Y ∧ andB Z (notB X) = Z) ∧ andB (orB Y Z) M = orB Y Z)
      ↔ (andB Y (andB M X) = Y ∧ andB Z (andB M (notB X)) = Z))
  | [], [], [], [], _, _, _ => ⟨fun _ => ⟨rfl, rfl⟩, fun _ => ⟨⟨rfl, rfl⟩, rfl⟩⟩
  | [], _ :: _, _, _, h, _, _ => absurd h Nat.noConfusion
  | [], [], _ :: _, _, _, h, _ => absurd h Nat.noConfusion
  | [], [], [], _ :: _, _, _, h => absurd h Nat.noConfusion
  | _ :: _, [], _, _, h, _, _ => absurd h Nat.noConfusion
  | _ :: _, _ :: _, [], _, _, h, _ => absurd h Nat.noConfusion
  | _ :: _, _ :: _, _ :: _, [], _, _, h => absurd h Nat.noConfusion
  | x :: X, m :: M, y :: Y, z :: Z, hM, hY, hZ => by
    have ih := cond_iff X M Y Z (Nat.succ.inj hM) (Nat.succ.inj hY) (Nat.succ.inj hZ)
    have hh : (((y && x) = y ∧ (z && !x) = z) ∧ ((y || z) && m) = (y || z)) ↔ ((y && (m && x)) = y ∧ (z && (m && !x)) = z) := by
      cases x <;> cases m <;> cases y <;> cases z <;> decide
    have C := cons_iff
    show (((((y && x) :: andB Y X = y :: Y)) ∧ ((z && !x) :: andB Z (notB X) = z :: Z)) ∧
        (((y || z) && m) :: andB (orB Y Z) M = (y || z) :: orB Y Z))
      ↔ (((y && (m && x)) :: andB Y (andB M X) = y :: Y) ∧ ((z && (m && !x)) :: andB Z (andB M (notB X)) = z :: Z))
    exact ⟨fun h =>
        have a1 := (C _ _ _ _).1 h.1.1
        have a2 := (C _ _ _ _).1 h.1.2
        have a3 := (C _ _ _ _).1 h.2
        ⟨(C _ _ _ _).2 ⟨(hh.1 ⟨⟨a1.1, a2.1⟩, a3.1⟩).1, (ih.1 ⟨⟨a1.2, a2.2⟩, a3.2⟩).1⟩,
         (C _ _ _ _).2 ⟨(hh.1 ⟨⟨a1.1, a2.1⟩, a3.1⟩).2, (ih.1 ⟨⟨a1.2, a2.2⟩, a3.2⟩).2⟩⟩,
      fun h =>
        have b1 := (C _ _ _ _).1 h.1
        have b2 := (C _ _ _ _).1 h.2
        ⟨⟨(C _ _ _ _).2 ⟨(hh.2 ⟨b1.1, b2.1⟩).1.1, (ih.2 ⟨b1.2, b2.2⟩).1.1⟩,
          (C _ _ _ _).2 ⟨(hh.2 ⟨b1.1, b2.1⟩).1.2, (ih.2 ⟨b1.2, b2.2⟩).1.2⟩⟩,
         (C _ _ _ _).2 ⟨(hh.2 ⟨b1.1, b2.1⟩).2, (ih.2 ⟨b1.2, b2.2⟩).2⟩⟩⟩

theorem disj_of : ∀ (X Y Z : List Bool), andB Y X = Y → andB Z (notB X) = Z → disjM Y Z
  | [], [], _, _, _ => trivial
  | [], _ :: _, _, h1, _ => by cases h1
  | _ :: _, [], _, _, _ => trivial
  | _ :: _, _ :: _, [], _, _ => trivial
  | x :: X, y :: Y, z :: Z, h1, h2 => by
    have a1 := (cons_iff _ _ _ _).1 h1
    have a2 := (cons_iff _ _ _ _).1 h2
    refine ⟨fun h => ?_, disj_of X Y Z a1.2 a2.2⟩
    have e1 : (y && x) = y := a1.1
    have e2 : (z && !x) = z := a2.1
    rw [h.1] at e1
    rw [h.2] at e2
    cases x
    · exact absurd e1 (by decide)
    · exact absurd e2 (by decide)

/-- **Expansion over a set `X` of plaquettes.** -/
theorem expand_X (t : R) (P : List (List Nat)) (M X : List Bool) (hM : M.length = P.length) (hX : X.length = P.length)
    (n : List Nat) :
    G K t n (selL P M) = sumK K (fun Y => if andB Y (andB M X) = Y then
      K.mul (pwR K t (cnt Y)) (G K t (zadd n (bdS P Y)) (selL P (andB M (notB X)))) else K.zero) (bools P.length) := by
  have hmem := ChangeVar2D.mem_bools_len P.length
  have hMX : (andB M (notB X)).length = P.length := (len_andB M (notB X) (hM.trans (hX.symm.trans (len_notB X).symm))).trans hM
  rw [G_sel K t P M n hM, ← hX, split_pw K _ X, hX]
  refine sumK_ext_mem K _ _ (bools P.length) (fun Y hY => ?_)
  have hYl := hmem Y hY
  by_cases cY : andB Y (andB M X) = Y
  · rw [ip cY, G_sel K t P _ _ hMX, mul_sumK]
    refine sumK_ext_mem K _ _ (bools P.length) (fun Z hZ => ?_)
    have hZl := hmem Z hZ
    have iff := cond_iff X M Y Z (hM.trans hX.symm) (hYl.trans hX.symm) (hZl.trans hX.symm)
    by_cases cZ : andB Z (andB M (notB X)) = Z
    · have c := iff.2 ⟨cY, cZ⟩
      rw [ip c.1, ip c.2, ip cZ]
      exact term_union K t P Y Z n hYl hZl (disj_of X Y Z c.1.1 c.1.2)
    · rw [ineg cZ, DetChain.mul_zero]
      by_cases c1 : andB Y X = Y ∧ andB Z (notB X) = Z
      · rw [ip c1, ineg (fun h => cZ (iff.1 ⟨c1, h⟩).2)]
      · rw [ineg c1]
  · rw [ineg cY]
    refine (sumK_ext_mem K _ _ (bools P.length) (fun Z hZ => ?_)).trans (sum_zero K _)
    have hZl := hmem Z hZ
    have iff := cond_iff X M Y Z (hM.trans hX.symm) (hYl.trans hX.symm) (hZl.trans hX.symm)
    by_cases c1 : andB Y X = Y ∧ andB Z (notB X) = Z
    · rw [ip c1, ineg (fun h => cY (iff.1 ⟨c1, h⟩).1)]
    · rw [ineg c1]

theorem len_bdS (L : Nat) : ∀ (P : List (List Nat)) (Y : List Bool), (∀ q, memL q P → q.length = L) →
    (bdS P Y).length ≤ L
  | [], _, _ => Nat.zero_le _
  | _ :: _, [], _ => Nat.zero_le _
  | q :: P, true :: Y, hP => by
    show (zadd q (bdS P Y)).length ≤ L
    rw [AreaLower.len_zadd_le q _ (by rw [hP q (Or.inl rfl)]; exact len_bdS L P Y (fun r hr => hP r (Or.inr hr))),
      hP q (Or.inl rfl)]
    exact Nat.le_refl L
  | _ :: P, false :: Y, hP => len_bdS L P Y (fun r hr => hP r (Or.inr hr))

theorem leB_andB_mono : ∀ (B Q X : List Bool), leB B Q → leB (andB B X) (andB Q X)
  | [], [], _, _ => trivial
  | [], _ :: _, _, h => h.elim
  | _ :: _, [], _, h => h.elim
  | _ :: _, _ :: _, [], _ => trivial
  | b :: B, c :: Q, x :: X, h => ⟨fun e => by
      have k : b = true → c = true := h.1
      cases b <;> cases c <;> cases x <;> first | rfl | exact e | exact absurd (k rfl) Bool.noConfusion,
    leB_andB_mono B Q X h.2⟩

theorem sub_mono : ∀ (Y B Q : List Bool), leB B Q → andB Y B = Y → andB Y Q = Y
  | [], _, _, _, _ => rfl
  | _ :: _, [], _, _, h => by cases h
  | _ :: _, _ :: _, [], h, _ => h.elim
  | y :: Y, b :: B, c :: Q, h, e => by
    have a := (cons_iff _ _ _ _).1 e
    show (y && c) :: andB Y Q = y :: Y
    rw [sub_mono Y B Q h.2 a.2]
    have k : b = true → c = true := h.1
    have a1 : (y && b) = y := a.1
    cases y <;> cases b <;> cases c <;> first | rfl | exact absurd (k rfl) Bool.noConfusion | exact absurd a1 (by decide)

variable (O : ORing K)

/-- **Ratio inequality**: for masks `B ⊆ Pm`, `Z(Pm \ X) Z(B) ≤ Z(B \ X) Z(Pm)`. -/
theorem ratio (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one)
    (L : Nat) (P : List (List Nat)) (hP : ∀ r, memL r P → r.length = L)
    (B Pm X : List Bool) (hB : B.length = P.length) (hPm : Pm.length = P.length) (hX : X.length = P.length)
    (hBP : leB B Pm) :
    O.le (K.mul (G K t (zl L) (selL P (andB Pm (notB X)))) (G K t (zl L) (selL P B)))
      (K.mul (G K t (zl L) (selL P (andB B (notB X)))) (G K t (zl L) (selL P Pm))) := by
  have hnX : (notB X).length = P.length := (len_notB X).trans hX
  have hBX : (andB B (notB X)).length = P.length := (len_andB B _ (hB.trans hnX.symm)).trans hB
  have hPX : (andB Pm (notB X)).length = P.length := (len_andB Pm _ (hPm.trans hnX.symm)).trans hPm
  have hle : leB (andB B (notB X)) (andB Pm (notB X)) := leB_andB_mono B Pm (notB X) hBP
  rw [expand_X K t P B X hB hX (zl L), expand_X K t P Pm X hPm hX (zl L), mul_sumK, mul_sumK]
  refine MaskSum.sumK_le K O _ _ (bools P.length) (fun Y hY => ?_)
  have Gn : ∀ n M, O.le K.zero (G K t n (selL P M)) := fun n M => G_nn K O h1 t ht0 _ n
  have pwn : O.le K.zero (pwR K t (cnt Y)) := AreaLower.pw_nn K O t ht0 h1 _
  by_cases cB : andB Y (andB B X) = Y
  · have cP : andB Y (andB Pm X) = Y := sub_mono Y _ _ (leB_andB_mono B Pm X hBP) cB
    rw [ip cB, ip cP]
    let nY := zadd (zl L) (bdS P Y)
    have hnY : nY.length = L :=
      (AreaLower.len_zadd_le (zl L) _ (by rw [len_zl]; exact len_bdS L P Y hP)).trans (len_zl L)
    have mm := mask_mono K O h1 h10 t ht0 ht1 L nY hnY P hP (andB Pm (notB X)) hPX
      (cnt (andB Pm (notB X))) (andB B (notB X)) hBX hle (Nat.le_add_left _ _)
    -- mm : G(nY, B\X) G(0, Pm\X) ≤ G(nY, Pm\X) G(0, B\X)
    have := O.mul_le_mul_right _ _ _ mm pwn
    -- rearrange both sides
    have e1 : K.mul (G K t (zl L) (selL P (andB Pm (notB X)))) (K.mul (pwR K t (cnt Y)) (G K t nY (selL P (andB B (notB X)))))
        = K.mul (K.mul (G K t nY (selL P (andB B (notB X)))) (G K t (zl L) (selL P (andB Pm (notB X))))) (pwR K t (cnt Y)) := by
      rw [K.mul_comm (pwR K t (cnt Y)), ← K.mul_assoc, K.mul_comm (G K t (zl L) _)]
    have e2 : K.mul (G K t (zl L) (selL P (andB B (notB X)))) (K.mul (pwR K t (cnt Y)) (G K t nY (selL P (andB Pm (notB X)))))
        = K.mul (K.mul (G K t nY (selL P (andB Pm (notB X)))) (G K t (zl L) (selL P (andB B (notB X))))) (pwR K t (cnt Y)) := by
      rw [K.mul_comm (pwR K t (cnt Y)), ← K.mul_assoc, K.mul_comm (G K t (zl L) _)]
    show O.le (K.mul (G K t (zl L) (selL P (andB Pm (notB X)))) (K.mul (pwR K t (cnt Y)) (G K t nY (selL P (andB B (notB X))))))
      (K.mul (G K t (zl L) (selL P (andB B (notB X)))) (K.mul (pwR K t (cnt Y)) (G K t nY (selL P (andB Pm (notB X))))))
    rw [e1, e2]
    exact this
  · rw [ineg cB, DetChain.mul_zero]
    by_cases cP : andB Y (andB Pm X) = Y
    · rw [ip cP]; exact O.mul_nonneg _ _ (Gn _ _) (O.mul_nonneg _ _ pwn (Gn _ _))
    · rw [ineg cP, DetChain.mul_zero]; exact O.le_refl _

end Ratio

#print axioms Ratio.head_iff
#print axioms Ratio.decomp_iff
#print axioms Ratio.split_pw
#print axioms Ratio.term_union
#print axioms Ratio.cond_iff
#print axioms Ratio.disj_of
#print axioms Ratio.expand_X
#print axioms Ratio.len_bdS
#print axioms Ratio.sub_mono
#print axioms Ratio.ratio

ym/Cluster.lean

/-
  ym/Cluster — T27, cycle 89: **exponential clustering for ℤ₂ at strong coupling** (mass gap in the correlation sense).
    conn_tail     Σ_{C connected from p} [|C| ≥ m] t^|C| ≤ 2^(D|supp p|+1)·2 s^m      (s = 2^(Dw) t, 2s ≤ 1)
    cluster       if every C connected from p reaching q has |C| ≥ m:
                    G_{p+q} Z ≤ G_p G_q + Z² · 2^(D|supp p|+1)·2 s^m,
                  i.e. 0 ≤ ⟨U_pU_q⟩ − ⟨U_p⟩⟨U_q⟩ ≤ 2^(D|supp p|+2) (2^(Dw) t)^m
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.CorrBound

namespace Cluster

open YMChain LinkIntegral GKS StrongZ2 S2cMask S2cComp S2cFinal AnimalSum AnimalArea AnimalDeg AnimalSupp AreaGeo AreaExp CorrBound

section Main
variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- **Σ over connected C from p of [|C| ≥ m] t^|C| ≤ 2^(D|supp p|+1) (s^m + s^m).** -/
theorem conn_tail (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) (P : List (List Nat)) (p : List Nat) (D w m : Nat)
    (hD : allLe (degV P) D) (hw : allQ P w)
    (hs : O.le (K.add (K.mul (nsm K (2 ^ (D * w)) K.one) t) (K.mul (nsm K (2 ^ (D * w)) K.one) t)) K.one) :
    O.le (sumK K (fun C => gA K m t (cnt C)) (filt (conn P p) (masks P.length)))
      (K.mul (nsm K (2 ^ (D * nz p + 1)) K.one)
        (K.add (pwR K (K.mul (nsm K (2 ^ (D * w)) K.one) t) m) (pwR K (K.mul (nsm K (2 ^ (D * w)) K.one) t) m))) := by
  have a1 := animal_sum_g K O h1 m t ht P p D w hD hw
  have e2 : sumK K (fun y => gA K m t y.1) (codeSet P p D w) =
      sumK K (fun A => nsm K (upTo (D * (nz p + w * A))).length (gA K m t A)) (rangeL P.length) :=
    sum_bindL K (gA K m t) (fun A => D * (nz p + w * A)) _
  have a3 := sum_mono K O (fun A => nsm K (upTo (D * (nz p + w * A))).length (gA K m t A))
    (fun A => nsm K (2 ^ (D * (nz p + w * A) + 1)) (gA K m t A)) (rangeL P.length)
    (fun A _ => nsm_mono K O _ (gA_nn K O h1 m t ht A) _ _ (upTo_le _))
  have e4 : sumK K (fun A => nsm K (2 ^ (D * (nz p + w * A) + 1)) (gA K m t A)) (rangeL P.length) =
      K.mul (nsm K (2 ^ (D * nz p + 1)) K.one) (sumK K (gA K m (K.mul (nsm K (2 ^ (D * w)) K.one) t)) (rangeL P.length)) := by
    rw [mul_sumK]
    exact sumK_ext_mem K _ _ _ (fun A _ => term_eq K t D (nz p) w m A)
  have s0 : O.le K.zero (K.mul (nsm K (2 ^ (D * w)) K.one) t) := O.mul_nonneg _ _ (nsm_nn K O h1 _) ht
  have g := geo_tail K O h1 _ s0 hs P.length m
  have g2 := O.mul_le_mul_right _ _ _ g (nsm_nn K O h1 (2 ^ (D * nz p + 1)))
  rw [K.mul_comm (sumK K _ _), K.mul_comm (K.add _ _)] at g2
  rw [e2] at a1
  rw [e4] at a3
  exact O.le_trans _ _ _ a1 (O.le_trans _ _ _ a3 g2)

/-- **Exponential clustering (ℤ₂, strong coupling).** -/
theorem cluster (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one) (L : Nat)
    (p q : List Nat) (hq : q.length = L) (P : List (List Nat)) (hP : ∀ r, memL r P → r.length = L) (D w m : Nat)
    (hD : allLe (degV P) D) (hw : allQ P w)
    (hs : O.le (K.add (K.mul (nsm K (2 ^ (D * w)) K.one) t) (K.mul (nsm K (2 ^ (D * w)) K.one) t)) K.one)
    (hdist : ∀ C, memL C (filt (conn P p) (masks P.length)) → missC P p q C = false → m ≤ cnt C) :
    O.le (K.mul (G K t (zadd p q) P) (G K t (zl L) P))
      (K.add (K.mul (G K t p P) (G K t q P)) (K.mul (K.mul (G K t (zl L) P) (G K t (zl L) P))
        (K.mul (nsm K (2 ^ (D * nz p + 1)) K.one)
          (K.add (pwR K (K.mul (nsm K (2 ^ (D * w)) K.one) t) m) (pwR K (K.mul (nsm K (2 ^ (D * w)) K.one) t) m))))) := by
  refine O.le_trans _ _ _ (corr_bound K O h1 h10 t ht0 ht1 L p q hq P hP) (O.add_le_add _ _ _ _ (O.le_refl _) ?_)
  have zz : O.le K.zero (K.mul (G K t (zl L) P) (G K t (zl L) P)) :=
    O.mul_nonneg _ _ (G_nn K O h1 t ht0 P (zl L)) (G_nn K O h1 t ht0 P (zl L))
  rw [K.mul_comm (K.mul (G K t (zl L) P) (G K t (zl L) P)), K.mul_comm (K.mul (G K t (zl L) P) (G K t (zl L) P))]
  refine O.mul_le_mul_right _ _ _ ?_ zz
  refine O.le_trans _ _ _ (sum_mono K O _ (fun C => gA K m t (cnt C)) _ ?_) (conn_tail K O h1 t ht0 P p D w m hD hw hs)
  intro C hC
  show O.le _ (if m ≤ cnt C then pwR K t (cnt C) else K.zero)
  cases hm : missC P p q C
  · show O.le (if false = true then K.zero else pwR K t (cnt C)) _
    rw [FiniteRecChain.ite_pos' _ _ (hdist C hC hm)]
    exact O.le_refl _
  · show O.le K.zero _
    exact gA_nn K O h1 m t ht0 (cnt C)

end Main

end Cluster

#print axioms Cluster.conn_tail
#print axioms Cluster.cluster

ym/Approx.lean

/-
  ym/Approx — T45, cycle 39 (Lad1, ℤ₂, any dimension): finite-volume comparison.

  One plaquette list P (N plaquettes, each of length L), loop n (length L), masks B ⊆ Pm.
  σ q = touch q n; cl = Seeds.clS σ P (= Factor's cl); X(A) = A ∪ {plaquettes touching the loop or A}.
    term_dec        D detached from the loop and from A, disjoint from A:  term_n(A ∪ D) = term_n(A) · term_0(D)
    upper_P         G_n(Pm) ≤ Σ_{A conn ⊆ Pm} term_n(A) · (A ⊆ B ? G_0(Pm \ X(A)) : G_0(Pm))
    lower_B         Σ_{A conn ⊆ B} term_n(A) · G_0(B \ X(A)) ≤ G_n(B)
    approx          G_n(Pm) G_0(B) ≤ G_n(B) G_0(Pm) + T · G_0(Pm) G_0(B),
                    T = Σ_{A conn ⊆ Pm, A ⊄ B} term_n(A)               i.e.  ⟨W⟩_Pm ≤ ⟨W⟩_B + T
  (with Ratio.ratio for the A ⊆ B terms). T only contains loop-connected sets that leave B.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Cluster

namespace Approx

open YMChain LinkIntegral GKS StrongZ2 MaskSum Closure Factor Seeds Count GPerm MaskMono Tail Ratio Cluster

variable {R : Type} (K : DetChain.CRing R)

def Xm (n : List Nat) (P : List (List Nat)) (A : List Bool) : List Bool :=
  orB A (P.map (fun q => touch q n || anyT P A q))

theorem sel_nth : ∀ (P : List (List Nat)) (D : List Bool) (q : List Nat), memL q (selL P D) →
    ∃ i, i < P.length ∧ nthB D i = true ∧ q = nthL P i
  | [], _, _, h => h.elim
  | _ :: _, [], _, h => h.elim
  | p :: P, true :: D, q, h => by
    cases h with
    | inl e => exact ⟨0, Nat.succ_pos _, rfl, e⟩
    | inr h =>
      obtain ⟨i, hi, h1, h2⟩ := sel_nth P D q h
      exact ⟨i + 1, Nat.succ_lt_succ hi, h1, h2⟩
  | _ :: P, false :: D, q, h => by
    obtain ⟨i, hi, h1, h2⟩ := sel_nth P D q h
    exact ⟨i + 1, Nat.succ_lt_succ hi, h1, h2⟩

/-- **Weight of a detached union.** -/
theorem term_dec (t : R) (n : List Nat) (P : List (List Nat)) (A D : List Bool)
    (hA : A.length = P.length) (hD : D.length = P.length) (hdisj : disjM A D)
    (hDs : ∀ i, i < P.length → nthB D i = true → touch (nthL P i) n = false ∧ anyT P A (nthL P i) = false) :
    MaskSum.term K t n P (orB A D) = K.mul (MaskSum.term K t n P A) (MaskSum.term K t (zl n.length) P D) := by
  have hB : ∀ q, memL q (selL P D) → touch q n = false ∧ anyT P A q = false := by
    intro q hq
    obtain ⟨i, hi, h1, h2⟩ := sel_nth P D q hq
    rw [h2]; exact hDs i hi h1
  have e := term_split K t P A D n (zl n.length) hA hD hdisj (disj_zl n _) (fun q _ => disj_zl q _)
    (fun q hq => disj_comm _ _ (touch_disj _ _ (hB q hq).1))
    (fun q q' hq hq' => disj_comm _ _ (touch_disj _ _ (anyT_sel P A q' (hB q' hq').2 q hq)))
  rw [Decouple.zadd_zl] at e
  exact e

theorem len_Xm (n : List Nat) (P : List (List Nat)) (A : List Bool) (hA : A.length = P.length) :
    (Xm n P A).length = P.length := len_orB _ _ _ hA (len_map _ P)

/-- `D ⊆ ¬X(A)` read pointwise. -/
theorem off_Xm (n : List Nat) (P : List (List Nat)) (A D : List Bool) (hA : A.length = P.length)
    (hD : D.length = P.length) (h : andB D (notB (Xm n P A)) = D) (i : Nat) (hi : i < P.length)
    (hDi : nthB D i = true) :
    nthB A i = false ∧ touch (nthL P i) n = false ∧ anyT P A (nthL P i) = false := by
  have e := congrArg (fun z => nthB z i) h
  simp only [nth_andB] at e
  rw [hDi, nth_notB _ i (by rw [len_Xm n P A hA]; exact hi), Xm, nth_orB A _ i (hA.trans (len_map _ P).symm),
    nth_map _ P i hi] at e
  cases e1 : nthB A i <;> cases e2 : touch (nthL P i) n <;> cases e3 : anyT P A (nthL P i) <;>
    rw [e1, e2, e3] at e <;> first | exact ⟨rfl, rfl, rfl⟩ | exact absurd e (by decide)

theorem disj_of_off (A D : List Bool) (hl : A.length = D.length)
    (h : ∀ i, nthB D i = true → nthB A i = false) : disjM A D := by
  induction A generalizing D with
  | nil => trivial
  | cons a A ih =>
    cases D with
    | nil => trivial
    | cons d D =>
      refine ⟨fun hh => ?_, ih D (Nat.succ.inj hl) (fun i hi => h (i + 1) hi)⟩
      have := h 0 hh.2
      rw [hh.1] at this
      exact Bool.noConfusion this

theorem sub_of_le : ∀ (C S M : List Bool), leB C S → andB S M = S → andB C M = C
  | [], _, _, _, _ => rfl
  | _ :: _, [], _, h, _ => h.elim
  | _ :: _, _ :: _, [], _, h => by cases h
  | c :: C, s :: S, m :: M, h, e => by
    have a := (cons_iff _ _ _ _).1 e
    show (c && m) :: andB C M = c :: C
    rw [sub_of_le C S M h.2 a.2]
    have k : c = true → s = true := h.1
    have a1 : (s && m) = s := a.1
    cases c <;> cases s <;> cases m <;> first | rfl | exact absurd (k rfl) Bool.noConfusion | exact absurd a1 (by decide)

theorem or_sub : ∀ (A D M : List Bool), andB A M = A → andB D M = D → andB (orB A D) M = orB A D
  | [], _, _, _, _ => rfl
  | _ :: _, [], _, _, _ => rfl
  | _ :: _, _ :: _, [], h, _ => by cases h
  | a :: A, d :: D, m :: M, h1, h2 => by
    have a1 := (cons_iff _ _ _ _).1 h1
    have a2 := (cons_iff _ _ _ _).1 h2
    show ((a || d) && m) :: andB (orB A D) M = (a || d) :: orB A D
    rw [or_sub A D M a1.2 a2.2]
    have e1 : (a && m) = a := a1.1
    have e2 : (d && m) = d := a2.1
    cases a <;> cases d <;> cases m <;> first | rfl | exact absurd e1 (by decide) | exact absurd e2 (by decide)

theorem andB_assoc_sub (D B Y : List Bool) (h : andB D (andB B Y) = D) (hl : B.length = Y.length) : andB D B = D :=
  sub_mono D (andB B Y) B (andB_le B Y hl) h

variable (O : ORing K)

theorem G_sel_nn (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) (P : List (List Nat)) (n : List Nat) (M : List Bool) :
    O.le K.zero (G K t n (selL P M)) := G_nn K O h1 t ht _ n

/-- **Lower bound in the small volume.** -/
theorem lower_B (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) (n : List Nat) (P : List (List Nat))
    (B : List Bool) (hB : B.length = P.length) :
    O.le (sumK K (fun A => if clS (fun q => touch q n) P A = A ∧ andB A B = A then
        K.mul (MaskSum.term K t n P A) (G K t (zl n.length) (selL P (andB B (notB (Xm n P A))))) else K.zero) (bools P.length))
      (G K t n (selL P B)) := by
  let σ : List Nat → Bool := fun q => touch q n
  let N := P.length
  have hmem := ChangeVar2D.mem_bools_len N
  let c : List Bool → List Bool := clS σ P
  let d : List Bool → List Bool := fun S => andB S (notB (c S))
  let Φ : List Bool → R := fun S => if andB S B = S then MaskSum.term K t n P S else K.zero
  have hc : ∀ S, S.length = N → (c S).length = N := fun S hS => cl_len (Vs σ P) N (hV σ P) S hS
  have hd : ∀ S, S.length = N → (d S).length = N := fun S hS =>
    (len_andB S _ (hS.trans ((len_notB _).trans (hc S hS)).symm)).trans hS
  have hΦ : ∀ S, O.le K.zero (Φ S) := fun S => by
    show O.le K.zero (if _ then _ else _)
    by_cases e : andB S B = S
    · rw [ip e]; exact term_nn K O h1 t ht P n S
    · rw [ineg e]; exact O.le_refl _
  let F : List Bool → List Bool → R := fun A D =>
    if clS σ P A = A ∧ andB A B = A then
      (if andB D (andB B (notB (Xm n P A))) = D then
        K.mul (MaskSum.term K t n P A) (MaskSum.term K t (zl n.length) P D) else K.zero) else K.zero
  have eF : ∀ A, A.length = N → (if clS σ P A = A ∧ andB A B = A then
        K.mul (MaskSum.term K t n P A) (G K t (zl n.length) (selL P (andB B (notB (Xm n P A))))) else K.zero)
      = sumK K (fun D => F A D) (bools N) := by
    intro A hAl
    show _ = sumK K (fun D => if clS σ P A = A ∧ andB A B = A then _ else K.zero) (bools N)
    by_cases cA : clS σ P A = A ∧ andB A B = A
    · rw [ip cA]
      have hXl : (andB B (notB (Xm n P A))).length = P.length :=
        (len_andB B _ (by rw [len_notB, len_Xm n P A hAl, hB])).trans hB
      rw [G_sel K t P _ _ hXl, mul_sumK]
      exact sumK_ext K _ _ (fun D => by
        rw [ip cA]
        by_cases cD : andB D (andB B (notB (Xm n P A))) = D
        · rw [ip cD, ip cD]
        · rw [ineg cD, ineg cD, DetChain.mul_zero]) _
    · rw [ineg cA]
      exact ((sumK_ext K _ _ (fun D => ineg cA) _).trans (sum_zero K _)).symm
  rw [sumK_ext_mem K _ _ (bools N) (fun A hA => eF A (hmem A hA)), G_sel K t P B n hB]
  refine rev_inj K O N c d F Φ hc hd hΦ (fun A D hA hD => ?_)
  show O.le (if clS σ P A = A ∧ andB A B = A then _ else K.zero) _
  by_cases cA : clS σ P A = A ∧ andB A B = A
  · rw [ip cA]
    by_cases cD : andB D (andB B (notB (Xm n P A))) = D
    · rw [ip cD]
      have hXl : (notB (Xm n P A)).length = B.length := by rw [len_notB, len_Xm n P A hA, hB]
      have hDX : andB D (notB (Xm n P A)) = D := by
        have := sub_mono D (andB B (notB (Xm n P A))) (notB (Xm n P A))
          (by
            refine leB_of _ _ ((len_andB B _ hXl.symm).trans hXl.symm) (fun i hi => ?_)
            rw [nth_andB] at hi
            cases e : nthB (notB (Xm n P A)) i
            · rw [e, Bool.and_false] at hi; exact hi
            · rfl) cD
        exact this
      have off := fun i hi hDi => off_Xm n P A D hA hD hDX i hi hDi
      have hdisj : disjM A D := disj_of_off A D (hA.trans hD.symm) (fun i hDi => by
        by_cases hi : i < P.length
        · exact (off i hi hDi).1
        · rw [nthB_ge D i (by rw [hD]; exact Nat.le_of_not_lt hi)] at hDi; exact absurd hDi Bool.noConfusion)
      have hcu : c (orB A D) = A := cl_union σ P A D hA hD cA.1 hdisj (fun i hi hDi => ⟨(off i hi hDi).2.1, (off i hi hDi).2.2⟩)
      have hdu : d (orB A D) = D := by
        show andB (orB A D) (notB (c (orB A D))) = D
        rw [hcu]; exact rest_eq A D (hA.trans hD.symm) hdisj
      rw [ip ⟨hcu.symm, hdu.symm⟩]
      have hsub : andB (orB A D) B = orB A D :=
        or_sub A D B cA.2 (andB_assoc_sub D B _ cD hXl.symm)
      show O.le _ (if andB (orB A D) B = orB A D then _ else _)
      rw [ip hsub, term_dec K t n P A D hA hD hdisj (fun i hi hDi => ⟨(off i hi hDi).2.1, (off i hi hDi).2.2⟩)]
      exact O.le_refl _
    · rw [ineg cD]
      by_cases cc : A = c (orB A D) ∧ D = d (orB A D)
      · rw [ip cc]; exact hΦ _
      · rw [ineg cc]; exact O.le_refl _
  · rw [ineg cA]
    by_cases cc : A = c (orB A D) ∧ D = d (orB A D)
    · rw [ip cc]; exact hΦ _
    · rw [ineg cc]; exact O.le_refl _

theorem and_sub2 (D M Y : List Bool) (h1 : andB D M = D) (h2 : andB D Y = D) : andB D (andB M Y) = D := by
  induction D generalizing M Y with
  | nil => rfl
  | cons a D ih =>
    cases M with
    | nil => exact h1
    | cons m M =>
      cases Y with
      | nil => exact h2
      | cons y Y =>
        have a1 := (cons_iff _ _ _ _).1 h1
        have a2 := (cons_iff _ _ _ _).1 h2
        show (a && (m && y)) :: andB D (andB M Y) = a :: D
        rw [ih M Y a1.2 a2.2]
        have e1 : (a && m) = a := a1.1
        have e2 : (a && y) = a := a2.1
        cases a <;> cases m <;> cases y <;> first | rfl | exact absurd e1 (by decide) | exact absurd e2 (by decide)

/-- **Upper bound in the large volume.** -/
theorem upper_P (h1 : O.le K.zero K.one) (t : R) (ht : O.le K.zero t) (n : List Nat) (P : List (List Nat))
    (B Pm : List Bool) (hPm : Pm.length = P.length) :
    O.le (G K t n (selL P Pm))
      (sumK K (fun A => if clS (fun q => touch q n) P A = A ∧ andB A Pm = A then
        K.mul (MaskSum.term K t n P A) (if andB A B = A then G K t (zl n.length) (selL P (andB Pm (notB (Xm n P A))))
          else G K t (zl n.length) (selL P Pm)) else K.zero) (bools P.length)) := by
  let σ : List Nat → Bool := fun q => touch q n
  let N := P.length
  have hmem := ChangeVar2D.mem_bools_len N
  let c : List Bool → List Bool := clS σ P
  let d : List Bool → List Bool := fun S => andB S (notB (c S))
  have hc : ∀ S, S.length = N → (c S).length = N := fun S hS => cl_len (Vs σ P) N (hV σ P) S hS
  have hsplit : ∀ S, S.length = N → S = orB (c S) (d S) ∧ disjM (c S) (d S) ∧ (d S).length = S.length :=
    fun S hS => split_mask (c S) S (cl_le (Vs σ P) N (hV σ P) S hS)
  have hd : ∀ S, S.length = N → (d S).length = N := fun S hS => (hsplit S hS).2.2.trans hS
  let tm := MaskSum.term K t n P
  let t0 := MaskSum.term K t (zl n.length) P
  let F : List Bool → List Bool → R := fun A D =>
    if clS σ P A = A ∧ andB A Pm = A then
      (if andB A B = A then (if andB D (andB Pm (notB (Xm n P A))) = D then K.mul (tm A) (t0 D) else K.zero)
       else (if andB D Pm = D then K.mul (tm A) (t0 D) else K.zero)) else K.zero
  have hF : ∀ A D, O.le K.zero (F A D) := fun A D => by
    have p := O.mul_nonneg _ _ (term_nn K O h1 t ht P n A) (term_nn K O h1 t ht P (zl n.length) D)
    show O.le K.zero (if _ then _ else _)
    by_cases c1 : clS σ P A = A ∧ andB A Pm = A
    · rw [ip c1]
      by_cases c2 : andB A B = A
      · rw [ip c2]
        by_cases c3 : andB D (andB Pm (notB (Xm n P A))) = D
        · rw [ip c3]; exact p
        · rw [ineg c3]; exact O.le_refl _
      · rw [ineg c2]
        by_cases c3 : andB D Pm = D
        · rw [ip c3]; exact p
        · rw [ineg c3]; exact O.le_refl _
    · rw [ineg c1]; exact O.le_refl _
  -- each term is bounded by F (c S) (d S)
  have hterm : ∀ S, S.length = N → O.le (if andB S Pm = S then tm S else K.zero) (F (c S) (d S)) := by
    intro S hS
    by_cases cS : andB S Pm = S
    · rw [ip cS]
      have hcS := hc S hS
      have hdS := hd S hS
      have hcle : leB (c S) S := cl_le (Vs σ P) N (hV σ P) S hS
      have hdle : leB (d S) S := andB_le S _ (hS.trans ((len_notB _).trans hcS).symm)
      have hoff : ∀ i, i < N → nthB (d S) i = true →
          nthB (c S) i = false ∧ touch (nthL P i) n = false ∧ anyT P (c S) (nthL P i) = false := by
        intro i hi hdi
        have e : nthB (andB S (notB (c S))) i = true := hdi
        rw [nth_andB, nth_notB _ i (by rw [hcS]; exact hi)] at e
        cases eS : nthB S i <;> cases eC : nthB (c S) i <;> rw [eS, eC] at e
        · exact absurd e Bool.noConfusion
        · exact absurd e Bool.noConfusion
        · refine ⟨rfl, ?_⟩
          cases er : (σ (nthL P i) || anyT P (clS σ P S) (nthL P i)) with
          | true =>
            have := closed σ P S hS i hi eS er
            rw [eC] at this; exact absurd this Bool.noConfusion
          | false =>
            have hf : (touch (nthL P i) n || anyT P (c S) (nthL P i)) = false := er
            cases e1 : touch (nthL P i) n with
            | true => rw [e1] at hf; exact absurd hf Bool.noConfusion
            | false => rw [e1, Bool.false_or] at hf; exact ⟨rfl, hf⟩
        · exact absurd e Bool.noConfusion
      have hdX : andB (d S) (notB (Xm n P (c S))) = d S := by
        refine ext_nth _ _ (len_andB _ _ (by rw [len_notB, len_Xm n P _ hcS, hdS])) (fun i => ?_)
        rw [nth_andB]
        by_cases hi : i < N
        · cases edi : nthB (d S) i
          · rfl
          · have o := hoff i hi edi
            rw [nth_notB _ i (by rw [len_Xm n P _ hcS]; exact hi), Xm, nth_orB _ _ i (hcS.trans (len_map _ P).symm),
              nth_map _ P i hi, o.1, o.2.1, o.2.2]
            rfl
        · rw [nthB_ge (d S) i (by rw [hdS]; exact Nat.le_of_not_lt hi)]; rfl
      have hdPm : andB (d S) Pm = d S := sub_of_le _ S Pm hdle cS
      have hcPm : andB (c S) Pm = c S := sub_of_le _ S Pm hcle cS
      have htm : tm S = K.mul (tm (c S)) (t0 (d S)) := by
        show MaskSum.term K t n P S = _
        conv => lhs; rw [(hsplit S hS).1]
        exact term_dec K t n P (c S) (d S) hcS hdS (hsplit S hS).2.1
          (fun i hi hdi => ⟨(hoff i hi hdi).2.1, (hoff i hi hdi).2.2⟩)
      show O.le (tm S) (if clS σ P (c S) = c S ∧ andB (c S) Pm = c S then _ else _)
      rw [ip ⟨cl_idem (Vs σ P) N (hV σ P) S hS, hcPm⟩, htm]
      by_cases cB : andB (c S) B = c S
      · rw [ip cB, ip (and_sub2 _ _ _ hdPm hdX)]; exact O.le_refl _
      · rw [ineg cB, ip hdPm]; exact O.le_refl _
    · rw [ineg cS]; exact hF _ _
  rw [G_sel K t P Pm n hPm]
  refine O.le_trans _ _ _ (MaskSum.sumK_le K O _ _ (bools N) (fun S hS => hterm S (hmem S hS))) ?_
  refine O.le_trans _ _ _ (inj2 K O N c d F hc hd (fun S hS => (hsplit S hS).1) hF) ?_
  refine MaskSum.sumK_le K O _ _ (bools N) (fun A hA => ?_)
  have hAl := hmem A hA
  show O.le (sumK K (fun D => if clS σ P A = A ∧ andB A Pm = A then _ else K.zero) (bools N)) _
  by_cases c1 : clS σ P A = A ∧ andB A Pm = A
  · rw [ip c1]
    by_cases c2 : andB A B = A
    · rw [ip c2]
      have hXl : (andB Pm (notB (Xm n P A))).length = P.length :=
        (len_andB Pm _ (by rw [len_notB, len_Xm n P A hAl, hPm])).trans hPm
      rw [G_sel K t P _ _ hXl, mul_sumK]
      refine O.le_trans _ _ _ (O.le_refl _) (O.le_trans _ _ _ ?_ (O.le_refl _))
      exact MaskSum.sumK_le K O _ _ (bools N) (fun D _ => by
        rw [ip c1, ip c2]
        by_cases c3 : andB D (andB Pm (notB (Xm n P A))) = D
        · rw [ip c3, ip c3]; exact O.le_refl _
        · rw [ineg c3, ineg c3, DetChain.mul_zero]; exact O.le_refl _)
    · rw [ineg c2, G_sel K t P Pm _ hPm, mul_sumK]
      exact MaskSum.sumK_le K O _ _ (bools N) (fun D _ => by
        rw [ip c1, ineg c2]
        by_cases c3 : andB D Pm = D
        · rw [ip c3, ip c3]; exact O.le_refl _
        · rw [ineg c3, ineg c3, DetChain.mul_zero]; exact O.le_refl _)
  · have hz : ∀ D, F A D = K.zero := fun D => ineg c1
    show O.le (sumK K (fun D => F A D) (bools N)) _
    rw [sumK_ext K _ _ hz, sum_zero, ineg c1]
    exact O.le_refl _

/-- The tail: loop-connected sets inside `Pm` that leave `B`. -/
def T (t : R) (n : List Nat) (P : List (List Nat)) (B Pm : List Bool) : R :=
  sumK K (fun A => if clS (fun q => touch q n) P A = A ∧ andB A Pm = A ∧ ¬ andB A B = A then
    MaskSum.term K t n P A else K.zero) (bools P.length)

/-- **Finite-volume comparison**: `⟨W⟩_Pm ≤ ⟨W⟩_B + T` for masks `B ⊆ Pm`. -/
theorem approx (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one)
    (L : Nat) (n : List Nat) (hn : n.length = L) (P : List (List Nat)) (hP : ∀ r, memL r P → r.length = L)
    (B Pm : List Bool) (hB : B.length = P.length) (hPm : Pm.length = P.length) (hBP : leB B Pm) :
    O.le (K.mul (G K t n (selL P Pm)) (G K t (zl L) (selL P B)))
      (K.add (K.mul (G K t n (selL P B)) (G K t (zl L) (selL P Pm)))
        (K.mul (T K t n P B Pm) (K.mul (G K t (zl L) (selL P Pm)) (G K t (zl L) (selL P B))))) := by
  let σ : List Nat → Bool := fun q => touch q n
  let N := P.length
  have hmem := ChangeVar2D.mem_bools_len N
  let G0 : List Bool → R := fun M => G K t (zl L) (selL P M)
  have G0n : ∀ M, O.le K.zero (G0 M) := fun M => G_nn K O h1 t ht0 _ _
  have tmn : ∀ A, O.le K.zero (MaskSum.term K t n P A) := fun A => term_nn K O h1 t ht0 P n A
  have u := upper_P K O h1 t ht0 n P B Pm hPm
  have l := lower_B K O h1 t ht0 n P B hB
  rw [hn] at u l
  have u' := O.mul_le_mul_right _ _ _ u (G0n B)
  refine O.le_trans _ _ _ u' ?_
  rw [sumK_mul]
  let g : List Bool → R := fun A => if clS σ P A = A ∧ andB A B = A then
    K.mul (MaskSum.term K t n P A) (G0 (andB B (notB (Xm n P A)))) else K.zero
  let h : List Bool → R := fun A => if clS σ P A = A ∧ andB A Pm = A ∧ ¬ andB A B = A then
    MaskSum.term K t n P A else K.zero
  have step : ∀ A, A.length = N → O.le (K.mul (if clS σ P A = A ∧ andB A Pm = A then
        K.mul (MaskSum.term K t n P A) (if andB A B = A then G0 (andB Pm (notB (Xm n P A))) else G0 Pm) else K.zero) (G0 B))
      (K.add (K.mul (g A) (G0 Pm)) (K.mul (h A) (K.mul (G0 Pm) (G0 B)))) := by
    intro A hA
    have gn : O.le K.zero (g A) := by
      show O.le K.zero (if _ then _ else _)
      by_cases e : clS σ P A = A ∧ andB A B = A
      · rw [ip e]; exact O.mul_nonneg _ _ (tmn A) (G0n _)
      · rw [ineg e]; exact O.le_refl _
    have hn' : O.le K.zero (h A) := by
      show O.le K.zero (if _ then _ else _)
      by_cases e : clS σ P A = A ∧ andB A Pm = A ∧ ¬ andB A B = A
      · rw [ip e]; exact tmn A
      · rw [ineg e]; exact O.le_refl _
    have rhs_nn : O.le K.zero (K.add (K.mul (g A) (G0 Pm)) (K.mul (h A) (K.mul (G0 Pm) (G0 B)))) := by
      have := O.add_le_add _ _ _ _ (O.mul_nonneg _ _ gn (G0n Pm)) (O.mul_nonneg _ _ hn' (O.mul_nonneg _ _ (G0n Pm) (G0n B)))
      rw [K.zero_add] at this; exact this
    by_cases c1 : clS σ P A = A ∧ andB A Pm = A
    · rw [ip c1]
      by_cases c2 : andB A B = A
      · rw [ip c2]
        have hgA : g A = K.mul (MaskSum.term K t n P A) (G0 (andB B (notB (Xm n P A)))) := ip ⟨c1.1, c2⟩
        have hhA : h A = K.zero := ineg (fun e => e.2.2 c2)
        rw [hgA, hhA, DetChain.zero_mul', DetChain.add_zero']
        have r := ratio K O h1 h10 t ht0 ht1 L P hP B Pm (Xm n P A) hB hPm (len_Xm n P A hA) hBP
        have r' := O.mul_le_mul_right _ _ _ r (tmn A)
        have e1 : K.mul (K.mul (MaskSum.term K t n P A) (G0 (andB Pm (notB (Xm n P A))))) (G0 B)
            = K.mul (K.mul (G0 (andB Pm (notB (Xm n P A)))) (G0 B)) (MaskSum.term K t n P A) := by
          rw [K.mul_comm (MaskSum.term K t n P A), K.mul_assoc, K.mul_comm (MaskSum.term K t n P A), ← K.mul_assoc]
        have e2 : K.mul (K.mul (MaskSum.term K t n P A) (G0 (andB B (notB (Xm n P A))))) (G0 Pm)
            = K.mul (K.mul (G0 (andB B (notB (Xm n P A)))) (G0 Pm)) (MaskSum.term K t n P A) := by
          rw [K.mul_comm (MaskSum.term K t n P A), K.mul_assoc, K.mul_comm (MaskSum.term K t n P A), ← K.mul_assoc]
        rw [e1, e2]
        exact r'
      · rw [ineg c2]
        have hgA : g A = K.zero := ineg (fun e => c2 e.2)
        have hhA : h A = MaskSum.term K t n P A := ip ⟨c1.1, c1.2, c2⟩
        rw [hgA, hhA, DetChain.zero_mul', K.zero_add, K.mul_assoc]
        exact O.le_refl _
    · rw [ineg c1, DetChain.zero_mul']
      exact rhs_nn
  refine O.le_trans _ _ _ (MaskSum.sumK_le K O _ _ (bools N) (fun A hA => step A (hmem A hA))) ?_
  rw [sumK_add, ← sumK_mul, ← sumK_mul]
  refine O.add_le_add _ _ _ _ ?_ (O.le_refl _)
  exact O.mul_le_mul_right _ _ _ l (G0n Pm)

end Approx

#print axioms Approx.sel_nth
#print axioms Approx.term_dec
#print axioms Approx.off_Xm
#print axioms Approx.disj_of_off
#print axioms Approx.lower_B
#print axioms Approx.upper_P
#print axioms Approx.approx

ym/Reach.lean

/-
  ym/Reach — T45, cycle 40 (Lad1, ℤ₂, any dimension): connected sets that leave a ball are large; the tail bound.

  σ q = touch q n (seeds = plaquettes touching the loop). ball R = R closure steps from the seeds in the full list:
    ball R = iter (Vs σ P) N (all plaquettes) R.
    iter_mono       S ⊆ S'  ⇒  iter_S k ⊆ iter_S' k
    stable          iter (k+1) = iter k  ⇒  iter (k + j) = iter k
    reach           A connected, A ⊄ ball R  ⇒  |A| ≥ R
    T_tail          if ball R ⊆ B then, for t = u x and x (1+y)^Dg ≤ y,
                      T(B) ≤ u^R (1 + y)^s       (s = number of seeds)
  With Approx.approx: ⟨W⟩_P ≤ ⟨W⟩_B + u^R (1+y)^s for B ⊇ ball R, uniformly in P.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Approx
import ym.AreaUpper

namespace Reach

open YMChain LinkIntegral GKS StrongZ2 MaskSum Closure Factor Seeds Count GPerm MaskMono Tail Ratio Cluster Approx AreaUpper

variable {R : Type} (K : DetChain.CRing R)

theorem anyT_mono (P : List (List Nat)) (X X' : List Bool) (h : leB X X') (q : List Nat) :
    anyT P X q = true → anyT P X' q = true := fun e => by
  obtain ⟨j, hj, h1, h2⟩ := anyT_ex P X q e
  exact anyT_of P X' q j (of_leB X X' h j h1) h2 hj

theorem V_mono (σ : List Nat → Bool) (P : List (List Nat)) (X X' : List Bool) (hX : X.length = P.length)
    (h : leB X X') : leB (Vs σ P X) (Vs σ P X') := by
  have hX' : X'.length = P.length := (leB_len X X' h).symm.trans hX
  refine leB_of _ _ ((leB_len _ _ (Vs_ge σ P X hX)).symm.trans ((leB_len X X' h).trans (leB_len _ _ (Vs_ge σ P X' hX'))))
    (fun i hi => ?_)
  by_cases hiP : i < P.length
  · rw [Vs, nth_vAux _ P X i hX hiP] at hi
    rw [Vs, nth_vAux _ P X' i hX' hiP]
    cases e1 : nthB X i
    · rw [e1, Bool.false_or] at hi
      cases e2 : σ (nthL P i)
      · rw [e2, Bool.false_or] at hi
        rw [anyT_mono P X X' h _ hi, Bool.or_true, Bool.or_true]
      · rw [Bool.true_or, Bool.or_true]
    · rw [of_leB X X' h i e1]; rfl
  · rw [nthB_ge _ i (by rw [← leB_len _ _ (Vs_ge σ P X hX), hX]; exact Nat.le_of_not_lt hiP)] at hi
    exact absurd hi Bool.noConfusion

theorem andB_mono2 (S S' X X' : List Bool) (h1 : leB S S') (h2 : leB X X') (hl : S.length = X.length) :
    leB (andB S X) (andB S' X') :=
  leB_of _ _ ((len_andB S X hl).trans ((leB_len S S' h1).trans
      (len_andB S' X' ((leB_len S S' h1).symm.trans (hl.trans (leB_len X X' h2)))).symm))
    (fun i hi => by
      rw [nth_andB] at hi ⊢
      cases e1 : nthB S i <;> cases e2 : nthB X i <;> rw [e1, e2] at hi
      · exact absurd hi Bool.noConfusion
      · exact absurd hi Bool.noConfusion
      · exact absurd hi Bool.noConfusion
      · rw [of_leB S S' h1 i e1, of_leB X X' h2 i e2]; rfl)

theorem iter_mono (σ : List Nat → Bool) (P : List (List Nat)) (S S' : List Bool) (hS : S.length = P.length)
    (hS' : S'.length = P.length) (h : leB S S') :
    ∀ k, leB (iter (Vs σ P) P.length S k) (iter (Vs σ P) P.length S' k)
  | 0 => leB_refl _
  | k + 1 => by
    have ih := iter_mono σ P S S' hS hS' h k
    have hl := iter_len (Vs σ P) P.length (hV σ P) S hS k
    exact andB_mono2 S S' _ _ h (V_mono σ P _ _ hl ih) (hS.trans (hl.symm.trans (leB_len _ _ (Vs_ge σ P _ hl))))

theorem stable (σ : List Nat → Bool) (P : List (List Nat)) (A : List Bool) (k : Nat)
    (h : iter (Vs σ P) P.length A (k + 1) = iter (Vs σ P) P.length A k) :
    ∀ j, iter (Vs σ P) P.length A (k + j) = iter (Vs σ P) P.length A k
  | 0 => rfl
  | j + 1 => by
    show andB A (Vs σ P (iter (Vs σ P) P.length A (k + j))) = _
    rw [stable σ P A k h j]
    exact h

theorem exists_out : ∀ A B : List Bool, A.length = B.length → ¬ andB A B = A →
    ∃ i, nthB A i = true ∧ nthB B i = false
  | [], _, _, h => absurd rfl h
  | _ :: _, [], hl, _ => absurd hl Nat.noConfusion
  | a :: A, b :: B, hl, h => by
    cases a <;> cases b
    · obtain ⟨i, h1, h2⟩ := exists_out A B (Nat.succ.inj hl) (fun e => h (by
        show (false && false) :: andB A B = false :: A; rw [e]; rfl))
      exact ⟨i + 1, h1, h2⟩
    · obtain ⟨i, h1, h2⟩ := exists_out A B (Nat.succ.inj hl) (fun e => h (by
        show (false && true) :: andB A B = false :: A; rw [e]; rfl))
      exact ⟨i + 1, h1, h2⟩
    · exact ⟨0, rfl, rfl⟩
    · obtain ⟨i, h1, h2⟩ := exists_out A B (Nat.succ.inj hl) (fun e => h (by
        show (true && true) :: andB A B = true :: A; rw [e]; rfl))
      exact ⟨i + 1, h1, h2⟩

theorem nth_tr : ∀ L j, j < L → nthB (tr L) j = true
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | _ + 1, 0, _ => rfl
  | L + 1, j + 1, h => nth_tr L j (Nat.lt_of_succ_lt_succ h)

def ball (σ : List Nat → Bool) (P : List (List Nat)) (Rr : Nat) : List Bool :=
  iter (Vs σ P) P.length (tr P.length) Rr

/-- **Connected sets that leave the ball are large.** -/
theorem reach (σ : List Nat → Bool) (P : List (List Nat)) (A : List Bool) (hA : A.length = P.length)
    (hconn : clS σ P A = A) (Rr : Nat) (i : Nat) (hAi : nthB A i = true) (hbi : nthB (ball σ P Rr) i = false) :
    Rr ≤ cnt A := by
  have htr : (tr P.length).length = P.length := len_tr _
  have hAtr : leB A (tr P.length) := leB_of _ _ (hA.trans htr.symm) (fun j hj => by
    by_cases hjP : j < P.length
    · exact nth_tr P.length j hjP
    · rw [nthB_ge A j (by rw [hA]; exact Nat.le_of_not_lt hjP)] at hj; exact absurd hj Bool.noConfusion)
  -- the R-th iterate of A misses i
  have hmiss : nthB (iter (Vs σ P) P.length A Rr) i = false := by
    cases e : nthB (iter (Vs σ P) P.length A Rr) i
    · rfl
    · have := of_leB _ _ (iter_mono σ P A (tr P.length) hA htr hAtr Rr) i e
      rw [show nthB (iter (Vs σ P) P.length (tr P.length) Rr) i = nthB (ball σ P Rr) i from rfl, hbi] at this
      exact absurd this Bool.noConfusion
  cases Rr with
  | zero => exact Nat.zero_le _
  | succ k =>
    cases iter_step (Vs σ P) P.length (hV σ P) A hA k with
    | inr h => exact Nat.le_trans h (leB_cnt _ _ (iter_le_S (Vs σ P) P.length (hV σ P) A hA (k + 1)))
    | inl h =>
      -- stable from k on, so the (k+1)-th iterate is cl A = A, which contains i
      have st := stable σ P A k h
      have e1 : iter (Vs σ P) P.length A (k + 1) = A := by
        cases split_nat k P.length with
        | inl hd =>
          obtain ⟨d, hd⟩ := hd
          have a := st 1
          have b := st d
          rw [hd] at b
          rw [a, ← b]
          exact hconn
        | inr hj =>
          obtain ⟨j, hj⟩ := hj
          rw [st 1, hj]
          rw [iter_late (Vs σ P) P.length (hV σ P) A hA j]
          exact hconn
      rw [e1, hAi] at hmiss
      exact absurd hmiss Bool.noConfusion

variable (O : ORing K)

/-- **Tail bound.** -/
theorem T_tail (h1 : O.le K.zero K.one) (u x y : R) (hu0 : O.le K.zero u) (hu1 : O.le u K.one)
    (hx : O.le K.zero x) (hy : O.le K.zero y) (n : List Nat) (P : List (List Nat)) (Dg Rr : Nat)
    (hdeg : ∀ v, v < P.length → cnt (P.map (fun q => touch q (nthL P v))) ≤ Dg)
    (hxy : O.le (K.mul x (pwR K (K.add K.one y) Dg)) y)
    (B Pm : List Bool) (hball : leB (ball (fun q => touch q n) P Rr) B) :
    O.le (T K (K.mul u x) n P B Pm)
      (K.mul (pwR K u Rr) (pwR K (K.add K.one y) (cnt (P.map (fun q => touch q n))))) := by
  let σ : List Nat → Bool := fun q => touch q n
  let N := P.length
  have hmem := ChangeVar2D.mem_bools_len N
  have d := D_bound K O h1 x y hx hy P Dg hdeg hxy N σ (tr N) (len_tr _) (by rw [cnt_tr]; exact Nat.le_refl _)
  have etr' : andB (tr N) (P.map σ) = P.map σ := by
    have := andB_tr' (P.map σ); rw [len_map] at this; exact this
  rw [etr'] at d
  have d' := O.mul_le_mul_right _ _ _ d (AreaLower.pw_nn K O u hu0 h1 Rr)
  rw [K.mul_comm (D K x σ P (tr N)), K.mul_comm (pwR K (K.add K.one y) _)] at d'
  refine O.le_trans _ _ _ ?_ d'
  show O.le (sumK K _ (bools N)) (K.mul (pwR K u Rr) (sumK K _ (bools N)))
  rw [mul_sumK]
  refine MaskSum.sumK_le K O _ _ (bools N) (fun A hA => ?_)
  have hAl := hmem A hA
  have etr : andB A (tr N) = A := by rw [← hAl]; exact andB_tr A
  by_cases c : clS σ P A = A ∧ andB A Pm = A ∧ ¬ andB A B = A
  · rw [ip c, ip ⟨etr, c.1⟩, term_eq K _ P n A hAl]
    have hBl : B.length = N := (leB_len _ _ hball).symm.trans (iter_len (Vs σ P) N (hV σ P) (tr N) (len_tr _) Rr)
    obtain ⟨i, hAi, hBi⟩ := exists_out A B (hAl.trans hBl.symm) c.2.2
    have hbi : nthB (ball σ P Rr) i = false := by
      cases e : nthB (ball σ P Rr) i
      · rfl
      · rw [of_leB _ _ hball i e] at hBi; exact absurd hBi Bool.noConfusion
    have hR := reach σ P A hAl c.1 Rr i hAi hbi
    cases hev : evenAll (zadd n (bdS P A))
    · exact O.mul_nonneg _ _ (AreaLower.pw_nn K O u hu0 h1 Rr) (AreaLower.pw_nn K O x hx h1 _)
    · exact pw_tail K O h1 u x hu0 hu1 hx Rr (cnt A) hR
  · rw [ineg c]
    exact O.mul_nonneg _ _ (AreaLower.pw_nn K O u hu0 h1 Rr) (by
      by_cases e : andB A (tr N) = A ∧ clS σ P A = A
      · rw [ip e]; exact AreaLower.pw_nn K O x hx h1 _
      · rw [ineg e]; exact O.le_refl _)

end Reach

#print axioms Reach.V_mono
#print axioms Reach.iter_mono
#print axioms Reach.reach
#print axioms Reach.T_tail

ym/Gap.lean

/-
  ym/Gap — T45, cycle 41 (Lad1, ℤ₂, every dimension): exponential clustering uniform in the volume.

  Two observables U_p, U_q (link vectors p, q), balls B_p, B_q (masks) that are disjoint and share no link with each
  other or with the other observable. Pm ⊇ B_p ∪ B_q is the volume.
    G_shift'        G (a + b) Q = [a even] · G b Q        when a shares no link with b or with any plaquette of Q
    bd_disj         ∂Y shares no link with r if no plaquette of Y does
    decouple_mask   G_{p+q}(B_p ∪ B_q) = G_p(B_p) · G_q(B_q)
    gap_upper       G_{p+q}(Pm) G_0(Pm) ≤ G_p(Pm) G_q(Pm) + T · G_0(Pm)²,
                    i.e.  ⟨U_p U_q⟩ − ⟨U_p⟩⟨U_q⟩ ≤ T ≤ u^R (1 + y)^s      (Reach.T_tail, ball R ⊆ B_p ∪ B_q)
  Together with GKS II (⟨U_pU_q⟩ − ⟨U_p⟩⟨U_q⟩ ≥ 0, ym/GKS2) this is the strong-coupling clustering with a
  rate −ln u per closure step, uniform in the volume: the "gap" half of rung Lad1.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Reach

namespace Gap

open YMChain LinkIntegral GKS StrongZ2 MaskSum Closure Factor Seeds Count GPerm MaskMono Tail Ratio Cluster Approx
  AreaUpper Reach

variable {R : Type} (K : DetChain.CRing R)

theorem G_shift' (t : R) : ∀ (Q : List (List Nat)) (a b : List Nat), disjN a b → (∀ r, memL r Q → disjN a r) →
    G K t (zadd a b) Q = (if evenAll a = true then G K t b Q else K.zero)
  | [], a, b, hab, _ => by
    show (if evenAll (zadd a b) = true then K.one else K.zero)
      = (if evenAll a = true then (if evenAll b = true then K.one else K.zero) else K.zero)
    rw [even_split a b hab]
    cases evenAll a <;> cases evenAll b <;> rfl
  | r :: Q, a, b, hab, hQ => by
    show K.add (G K t (zadd a b) Q) (K.mul t (G K t (zadd (zadd a b) r) Q))
      = (if evenAll a = true then K.add (G K t b Q) (K.mul t (G K t (zadd b r) Q)) else K.zero)
    rw [Decouple.zadd_assoc, G_shift' t Q a b hab (fun r' hr => hQ r' (Or.inr hr)),
      G_shift' t Q a (zadd b r) (disj_zadd_right a b r hab (hQ r (Or.inl rfl))) (fun r' hr => hQ r' (Or.inr hr))]
    cases evenAll a
    · show K.add K.zero (K.mul t K.zero) = K.zero; rw [DetChain.mul_zero, K.zero_add]
    · rfl

theorem bd_disj (r : List Nat) : ∀ (P : List (List Nat)) (Y : List Bool),
    (∀ i, i < P.length → nthB Y i = true → disjN (nthL P i) r) → disjN (bdS P Y) r
  | [], _, _ => trivial
  | _ :: _, [], _ => trivial
  | q :: P, true :: Y, h => disj_zadd_left q (bdS P Y) r (h 0 (Nat.succ_pos _) rfl)
      (bd_disj r P Y (fun i hi hY => h (i + 1) (Nat.succ_lt_succ hi) hY))
  | _ :: P, false :: Y, h => bd_disj r P Y (fun i hi hY => h (i + 1) (Nat.succ_lt_succ hi) hY)

theorem or_and_self : ∀ A D : List Bool, A.length = D.length → andB (orB A D) A = A
  | [], [], _ => rfl
  | [], _ :: _, h => absurd h Nat.noConfusion
  | _ :: _, [], h => absurd h Nat.noConfusion
  | a :: A, d :: D, h => by
    show ((a || d) && a) :: andB (orB A D) A = a :: A
    rw [or_and_self A D (Nat.succ.inj h)]
    cases a <;> cases d <;> rfl

/-- **Two detached balls decouple.** -/
theorem decouple_mask (t : R) (P : List (List Nat)) (Bp Bq : List Bool) (hp : Bp.length = P.length)
    (hq : Bq.length = P.length) (hd : disjM Bp Bq) (p q : List Nat) (hpq : disjN p q)
    (hBp : ∀ i, i < P.length → nthB Bp i = true → disjN (nthL P i) q)
    (hBq : ∀ j, j < P.length → nthB Bq j = true → disjN p (nthL P j))
    (hBB : ∀ i j, i < P.length → j < P.length → nthB Bp i = true → nthB Bq j = true → disjN (nthL P i) (nthL P j)) :
    G K t (zadd p q) (selL P (orB Bp Bq)) = K.mul (G K t p (selL P Bp)) (G K t q (selL P Bq)) := by
  have hM : (orB Bp Bq).length = P.length := len_orB _ _ _ hp hq
  rw [expand_X K t P (orB Bp Bq) Bp hM hp, or_and_self Bp Bq (hp.trans hq.symm),
    rest_eq Bp Bq (hp.trans hq.symm) hd, G_sel K t P Bp p hp, sumK_mul]
  refine sumK_ext_mem K _ _ (bools P.length) (fun Y hY => ?_)
  have hYl := ChangeVar2D.mem_bools_len P.length Y hY
  by_cases cY : andB Y Bp = Y
  · rw [ip cY, ip cY, term_eq K t P p Y hYl]
    have hYi : ∀ i, i < P.length → nthB Y i = true → nthB Bp i = true := fun i _ hi => by
      have := congrArg (fun z => nthB z i) cY
      simp only [nth_andB] at this
      rw [hi] at this
      cases e : nthB Bp i
      · rw [e] at this; exact absurd this Bool.noConfusion
      · rfl
    have e1 : zadd (zadd p q) (bdS P Y) = zadd (zadd p (bdS P Y)) q := by
      rw [Decouple.zadd_assoc, AreaLower.zadd_comm q, ← Decouple.zadd_assoc]
    have hab : disjN (zadd p (bdS P Y)) q :=
      disj_zadd_left p _ q hpq (bd_disj q P Y (fun i hi hy => hBp i hi (hYi i hi hy)))
    have hQ : ∀ r, memL r (selL P Bq) → disjN (zadd p (bdS P Y)) r := fun r hr => by
      obtain ⟨j, hj, hj1, hj2⟩ := sel_nth P Bq r hr
      rw [hj2]
      exact disj_zadd_left p _ _ (hBq j hj hj1) (bd_disj _ P Y (fun i hi hy => hBB i j hi hj (hYi i hi hy) hj1))
    rw [e1, G_shift' K t _ _ q hab hQ]
    cases evenAll (zadd p (bdS P Y))
    · show K.mul (pwR K t (cnt Y)) K.zero = K.mul K.zero (G K t q (selL P Bq))
      rw [DetChain.mul_zero, DetChain.zero_mul']
    · rfl
  · rw [ineg cY, ineg cY, DetChain.zero_mul']

variable (O : ORing K)

theorem mul_le_mul4 (a a' b b' : R) (ha : O.le K.zero a) (hb : O.le K.zero b) (hb' : O.le K.zero b')
    (h1 : O.le a a') (h2 : O.le b b') : O.le (K.mul a b) (K.mul a' b') := by
  have x := O.mul_le_mul_right _ _ _ h1 hb
  have y := O.mul_le_mul_right _ _ _ h2 (O.le_trans _ _ _ ha h1)
  rw [K.mul_comm b a', K.mul_comm b' a'] at y
  exact O.le_trans _ _ _ x y

/-- **Clustering, upper half**: `⟨U_pU_q⟩ − ⟨U_p⟩⟨U_q⟩ ≤ T`, in cross-multiplied form. -/
theorem gap_upper (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one)
    (L : Nat) (P : List (List Nat)) (hP : ∀ r, memL r P → r.length = L)
    (p q : List Nat) (hpL : p.length = L) (hqL : q.length = L)
    (Bp Bq Pm : List Bool) (hp : Bp.length = P.length) (hq : Bq.length = P.length) (hPm : Pm.length = P.length)
    (hd : disjM Bp Bq) (hpq : disjN p q)
    (hBp : ∀ i, i < P.length → nthB Bp i = true → disjN (nthL P i) q)
    (hBq : ∀ j, j < P.length → nthB Bq j = true → disjN p (nthL P j))
    (hBB : ∀ i j, i < P.length → j < P.length → nthB Bp i = true → nthB Bq j = true → disjN (nthL P i) (nthL P j))
    (hsub : leB (orB Bp Bq) Pm) :
    O.le (K.mul (G K t (zadd p q) (selL P Pm)) (G K t (zl L) (selL P Pm)))
      (K.add (K.mul (G K t p (selL P Pm)) (G K t q (selL P Pm)))
        (K.mul (T K t (zadd p q) P (orB Bp Bq) Pm) (K.mul (G K t (zl L) (selL P Pm)) (G K t (zl L) (selL P Pm))))) := by
  let G0 : List Bool → R := fun M => G K t (zl L) (selL P M)
  have G0p : ∀ M, O.le K.one (G0 M) := fun M => G0_ge_one K O h1 t ht0 L _
  have G0n : ∀ M, O.le K.zero (G0 M) := fun M => O.le_trans _ _ _ h1 (G0p M)
  have Gn : ∀ n M, O.le K.zero (G K t n (selL P M)) := fun n M => G_nn K O h1 t ht0 _ n
  have hM : (orB Bp Bq).length = P.length := len_orB _ _ _ hp hq
  have hpqL : (zadd p q).length = L := (AreaLower.len_zadd_le p q (by rw [hpL, hqL]; exact Nat.le_refl _)).trans hpL
  -- (1) finite-volume comparison
  have a := approx K O h1 h10 t ht0 ht1 L (zadd p q) hpqL P hP (orB Bp Bq) Pm hM hPm hsub
  -- (2) decoupling
  have dW := decouple_mask K t P Bp Bq hp hq hd p q hpq hBp hBq hBB
  have d0 := decouple_mask K t P Bp Bq hp hq hd (zl L) (zl L) (disj_zl _ _)
    (fun i _ _ => disj_zl _ _) (fun j hj _ => disj_comm _ _ (disj_zl _ _)) hBB
  have ezz : zadd (zl L) (zl L) = zl L := by
    have := Decouple.zadd_zl (zl L); rw [len_zl] at this; exact this
  rw [ezz] at d0
  rw [dW, d0] at a
  -- (3) monotonicity of each factor
  have leBp : leB Bp Pm := leB_trans _ _ _ (leB_of _ _ (hp.trans hM.symm) (fun i hi => by
    rw [nth_orB Bp Bq i (hp.trans hq.symm), hi]; rfl)) hsub
  have leBq : leB Bq Pm := leB_trans _ _ _ (leB_of _ _ (hq.trans hM.symm) (fun i hi => by
    rw [nth_orB Bp Bq i (hp.trans hq.symm), hi, Bool.or_true])) hsub
  have mp := mask_mono K O h1 h10 t ht0 ht1 L p hpL P hP Pm hPm (cnt Pm) Bp hp leBp (Nat.le_add_left _ _)
  have mq := mask_mono K O h1 h10 t ht0 ht1 L q hqL P hP Pm hPm (cnt Pm) Bq hq leBq (Nat.le_add_left _ _)
  -- names
  let W := G K t (zadd p q) (selL P Pm)
  let gp := G K t p (selL P Bp)
  let gq := G K t q (selL P Bq)
  let Gp := G K t p (selL P Pm)
  let Gq := G K t q (selL P Pm)
  let z := G0 Pm
  let zp := G0 Bp
  let zq := G0 Bq
  let Tt := T K t (zadd p q) P (orB Bp Bq) Pm
  -- a : W (zp zq) ≤ (gp gq) z + Tt (z (zp zq))
  -- mp : gp z ≤ Gp zp ; mq : gq z ≤ Gq zq
  have a' := O.mul_le_mul_right _ _ _ a (G0n Pm)
  have prod := mul_le_mul4 K O (K.mul gp z) (K.mul Gp zp) (K.mul gq z) (K.mul Gq zq)
    (O.mul_nonneg _ _ (Gn _ _) (G0n _)) (O.mul_nonneg _ _ (Gn _ _) (G0n _)) (O.mul_nonneg _ _ (Gn _ _) (G0n _)) mp mq
  -- rewrite both sides as (…) · (zp zq)
  have eL : K.mul (K.mul W (K.mul zp zq)) z = K.mul (K.mul W z) (K.mul zp zq) := by
    rw [K.mul_assoc, K.mul_comm (K.mul zp zq) z, ← K.mul_assoc]
  have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
    rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
  have eR1 : K.mul (K.mul (K.mul gp gq) z) z = K.mul (K.mul gp z) (K.mul gq z) := by
    rw [K.mul_assoc, mul4]
  have eR2 : K.mul (K.mul Gp zp) (K.mul Gq zq) = K.mul (K.mul Gp Gq) (K.mul zp zq) := mul4 _ _ _ _
  have eR3 : K.mul (K.mul Tt (K.mul z (K.mul zp zq))) z = K.mul (K.mul Tt (K.mul z z)) (K.mul zp zq) := by
    rw [K.mul_assoc Tt, K.mul_assoc Tt]
    congr 1
    rw [K.mul_assoc, K.mul_comm (K.mul zp zq) z, ← K.mul_assoc]
  rw [eL, DetChain.right_distrib, eR1, eR3] at a'
  have c := O.le_trans _ _ _ a' (O.add_le_add _ _ _ _ prod (O.le_refl _))
  rw [eR2, ← DetChain.right_distrib] at c
  have zz1 : O.le K.one (K.mul zp zq) := one_le_mul K O zp zq (G0p Bp) (G0p Bq) h1
  have zzn : O.le K.zero (K.mul zp zq) := O.le_trans _ _ _ h1 zz1
  have zzne : K.mul zp zq ≠ K.zero := fun hz => by rw [hz] at zz1; exact h10 (O.le_antisymm _ _ zz1 h1)
  exact O.pos_cancel _ _ _ zzn zzne c

end Gap

#print axioms Gap.G_shift'
#print axioms Gap.bd_disj
#print axioms Gap.decouple_mask
#print axioms Gap.gap_upper

ym/InjCount.lean

/-
  ym/InjCount — T45, cycle 77 (Lad4 step 1): counting by injection.

    nodupL            no repetitions
    eraseL            remove the first occurrence
    count_inj         P duplicate-free, φ a selector, κ injective on the selected elements with values in the key
                      list ks:   |{p ∈ P : φ p}| ≤ |ks|
  Used for: on Z^d each link lies in at most 2(d−1) plaquettes (key = (other direction, side)).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Lad3

namespace InjCount

open YMChain Closure

variable {A B : Type} [DecidableEq A] [DecidableEq B]

def nodupL : List A → Prop
  | [] => True
  | a :: l => ¬ memL a l ∧ nodupL l

def eraseL (b : B) : List B → List B
  | [] => []
  | c :: l => if c = b then l else c :: eraseL b l

theorem len_erase (b : B) : ∀ l : List B, memL b l → (eraseL b l).length + 1 = l.length
  | [], h => h.elim
  | c :: l, h => by
    show (if c = b then l else c :: eraseL b l).length + 1 = l.length + 1
    by_cases e : c = b
    · rw [MaskSum.ip e]
    · rw [MaskSum.ineg e]
      cases h with
      | inl h' => exact absurd h'.symm e
      | inr h' => show (eraseL b l).length + 1 + 1 = l.length + 1; rw [len_erase b l h']

theorem mem_erase (b x : B) (hx : x ≠ b) : ∀ l : List B, memL x l → memL x (eraseL b l)
  | [], h => h.elim
  | c :: l, h => by
    show memL x (if c = b then l else c :: eraseL b l)
    by_cases e : c = b
    · rw [MaskSum.ip e]
      cases h with
      | inl h' => rw [h', e] at hx; exact absurd rfl hx
      | inr h' => exact h'
    · rw [MaskSum.ineg e]
      cases h with
      | inl h' => exact Or.inl h'
      | inr h' => exact Or.inr (mem_erase b x hx l h')

/-- **Counting by injection.** -/
theorem count_inj (φ : A → Bool) (κ : A → B) :
    ∀ (P : List A) (ks : List B), nodupL P →
      (∀ p, memL p P → φ p = true → memL (κ p) ks) →
      (∀ p p', memL p P → memL p' P → φ p = true → φ p' = true → κ p = κ p' → p = p') →
      cnt (P.map φ) ≤ ks.length
  | [], _, _, _, _ => Nat.zero_le _
  | p :: P, ks, hnd, hin, hinj => by
    show cnt (φ p :: P.map φ) ≤ ks.length
    cases e : φ p with
    | false =>
      exact count_inj φ κ P ks hnd.2 (fun q hq => hin q (Or.inr hq))
        (fun q q' hq hq' => hinj q q' (Or.inr hq) (Or.inr hq'))
    | true =>
      have hk := hin p (Or.inl rfl) e
      have ih := count_inj φ κ P (eraseL (κ p) ks) hnd.2
        (fun q hq hφ => mem_erase (κ p) (κ q) (fun h => hnd.1 (by
          rw [hinj q p (Or.inr hq) (Or.inl rfl) hφ e h] at hq; exact hq)) ks (hin q (Or.inr hq) hφ))
        (fun q q' hq hq' => hinj q q' (Or.inr hq) (Or.inr hq'))
      show cnt (P.map φ) + 1 ≤ ks.length
      rw [← len_erase (κ p) ks hk]
      exact Nat.succ_le_succ ih

end InjCount

#print axioms InjCount.len_erase
#print axioms InjCount.count_inj

ym/Degree.lean

/-
  ym/Degree — T45, cycle 66 (Lad1/2-inst, step 1): the touch-degree of a plaquette is bounded by link multiplicities.

    nthN q l        the multiplicity of link l in plaquette q (0 beyond the length)
    linkdeg P l     the number of plaquettes of P containing link l
    deg_bound       |{q ∈ P : q touches p}| ≤ Σ_{l : p_l ≠ 0} linkdeg P l
    deg_uniform     if p has at most s links and every link lies in at most m plaquettes, the degree is ≤ s · m
  On Z^d: s = 4, m = 2(d − 1), so Dg = 8(d − 1) suffices in AreaUpper / AreaG / GapG / GapH.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GapH

namespace Degree

open Closure Factor Count PolyBound

def nthN : List Nat → Nat → Nat
  | [], _ => 0
  | a :: _, 0 => a
  | _ :: q, l + 1 => nthN q l

def linkdeg (P : List (List Nat)) (l : Nat) : Nat := cnt (P.map (fun q => nzb (nthN q l)))

def tl' : List Nat → List Nat
  | [] => []
  | _ :: q => q

def hd' : List Nat → Nat
  | [] => 0
  | a :: _ => a

theorem touch_cons (q : List Nat) (a : Nat) (p : List Nat) :
    touch q (a :: p) = ((nzb (hd' q) && nzb a) || touch (tl' q) p) := by
  cases q with
  | nil => show false = ((nzb 0 && nzb a) || touch [] p); rfl
  | cons b q => rfl

theorem cnt_mono_map (σ τ : List Nat → Bool) (h : ∀ q, σ q = true → τ q = true) :
    ∀ P : List (List Nat), cnt (P.map σ) ≤ cnt (P.map τ)
  | [] => Nat.le_refl 0
  | q :: P => by
    have ih := cnt_mono_map σ τ h P
    show cnt (σ q :: P.map σ) ≤ cnt (τ q :: P.map τ)
    cases e1 : σ q <;> cases e2 : τ q
    · exact ih
    · exact Nat.le_succ_of_le ih
    · rw [h q e1] at e2; exact absurd e2 Bool.noConfusion
    · exact Nat.succ_le_succ ih

/-- Sum of link degrees over the support of `p`, with link offset `k`. -/
def suppDeg (P : List (List Nat)) : Nat → List Nat → Nat
  | _, [] => 0
  | k, a :: p => (if nzb a = true then linkdeg P k else 0) + suppDeg P (k + 1) p

theorem linkdeg_shift (P : List (List Nat)) (k : Nat) :
    linkdeg (P.map tl') k = linkdeg P (k + 1) := by
  unfold linkdeg
  induction P with
  | nil => rfl
  | cons q P ih =>
    show cnt (nzb (nthN (tl' q) k) :: _) = cnt (nzb (nthN q (k + 1)) :: _)
    have e : nthN (tl' q) k = nthN q (k + 1) := by cases q <;> rfl
    rw [e]
    cases nzb (nthN q (k + 1))
    · exact ih
    · exact congrArg Nat.succ ih

theorem map_map' (f : List Nat → List Nat) (g : List Nat → Bool) : ∀ P : List (List Nat),
    (P.map f).map g = P.map (fun q => g (f q))
  | [] => rfl
  | q :: P => by show g (f q) :: (P.map f).map g = g (f q) :: _; rw [map_map' f g P]

theorem L_a (a : Nat) (p : List Nat) : ∀ P : List (List Nat),
    P.map (fun q => touch q (a :: p)) = P.map (fun q => (nzb (hd' q) && nzb a) || touch (tl' q) p)
  | [] => rfl
  | q :: P => by show touch q (a :: p) :: _ = _ :: _; rw [touch_cons, L_a a p P]

theorem L_b : ∀ P : List (List Nat), cnt (P.map (fun q => nzb (hd' q) && false)) = 0
  | [] => rfl
  | q :: P => by show cnt ((nzb (hd' q) && false) :: _) = 0; rw [Bool.and_false]; exact L_b P

theorem L_c : ∀ P : List (List Nat), P.map (fun q => nzb (hd' q) && true) = P.map (fun q => nzb (nthN q 0))
  | [] => rfl
  | q :: P => by
    show (nzb (hd' q) && true) :: _ = nzb (nthN q 0) :: _
    rw [Bool.and_true, L_c P]
    cases q <;> rfl

theorem L_d (P : List (List Nat)) : ∀ (k : Nat) (p : List Nat), suppDeg (P.map tl') k p = suppDeg P (k + 1) p
  | _, [] => rfl
  | k, b :: p => by
    show (if nzb b = true then linkdeg (P.map tl') k else 0) + suppDeg (P.map tl') (k + 1) p
      = (if nzb b = true then linkdeg P (k + 1) else 0) + suppDeg P (k + 1 + 1) p
    rw [linkdeg_shift, L_d P (k + 1) p]

theorem L_e : ∀ P : List (List Nat), cnt (P.map (fun q => touch q [])) = 0
  | [] => rfl
  | q :: P => by
    have e : touch q [] = false := by cases q <;> rfl
    show cnt (touch q [] :: _) = 0
    rw [e]; exact L_e P

/-- **Degree bound.** -/
theorem deg_bound : ∀ (p : List Nat) (P : List (List Nat)), cnt (P.map (fun q => touch q p)) ≤ suppDeg P 0 p
  | [], P => by rw [L_e]; exact Nat.le_refl 0
  | a :: p, P => by
    rw [L_a]
    refine Nat.le_trans (cnt_map_or (fun q => nzb (hd' q) && nzb a) (fun q => touch (tl' q) p) P) ?_
    have ih := deg_bound p (P.map tl')
    rw [map_map', L_d P 0 p] at ih
    show _ ≤ (if nzb a = true then linkdeg P 0 else 0) + suppDeg P 1 p
    refine Nat.add_le_add ?_ ih
    cases e : nzb a
    · rw [L_b]; exact Nat.le_refl 0
    · show cnt (P.map (fun q => nzb (hd' q) && true)) ≤ linkdeg P 0
      rw [L_c]; exact Nat.le_refl _

/-- **Uniform degree bound.** -/
theorem deg_uniform (P : List (List Nat)) (m : Nat) (hm : ∀ l, linkdeg P l ≤ m) :
    ∀ (k : Nat) (p : List Nat), suppDeg P k p ≤ cnt (p.map nzb) * m
  | _, [] => Nat.zero_le _
  | k, a :: p => by
    have ih := deg_uniform P m hm (k + 1) p
    show (if nzb a = true then linkdeg P k else 0) + suppDeg P (k + 1) p ≤ cnt (nzb a :: p.map nzb) * m
    cases e : nzb a
    · show 0 + _ ≤ cnt (p.map nzb) * m; rw [Nat.zero_add]; exact ih
    · show linkdeg P k + _ ≤ (cnt (p.map nzb) + 1) * m
      rw [Nat.succ_mul, Nat.add_comm (cnt (p.map nzb) * m)]
      exact Nat.add_le_add (hm k) ih

end Degree

#print axioms Degree.deg_bound
#print axioms Degree.deg_uniform

ym/ZdLinks.lean

/-
  ym/ZdLinks — T45, cycle 78 (Lad4 step 2): on Z^d each link lies in at most 2d plaquettes (2(d−1) in fact).

  Points are coordinate lists of length d; shf y ν adds 1 to coordinate ν. A plaquette is (y, a, b) with a < b < d;
  its links are (y, a), (y + e_a, b), (y + e_b, a), (y, b). For a link l = (x, μ):
    key l p = (other direction, side)    side = false iff x is the base point y
    keysAll d       the 2d keys (ν, s), ν < d
    key_inj         two plaquettes containing l with the same key are equal
    linkdeg_Zd      for any duplicate-free list of plaquettes in dimension d, |{p : l ∈ p}| ≤ 2d

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.InjCount

namespace ZdLinks

open YMChain Closure InjCount

def shf : List Nat → Nat → List Nat
  | [], _ => []
  | a :: x, 0 => (a + 1) :: x
  | a :: x, ν + 1 => a :: shf x ν

abbrev Lk := List Nat × Nat
abbrev Pq := List Nat × Nat × Nat

def linkIn (l : Lk) (p : Pq) : Bool :=
  decide (l = (p.1, p.2.1) ∨ l = (shf p.1 p.2.1, p.2.2) ∨ l = (shf p.1 p.2.2, p.2.1) ∨ l = (p.1, p.2.2))

def key (l : Lk) (p : Pq) : Nat × Bool :=
  (if l.2 = p.2.1 then p.2.2 else p.2.1, if l.1 = p.1 then false else true)

def keysAll : Nat → List (Nat × Bool)
  | 0 => []
  | d + 1 => (d, false) :: (d, true) :: keysAll d

theorem len_keysAll : ∀ d, (keysAll d).length = 2 * d
  | 0 => rfl
  | d + 1 => by show (keysAll d).length + 1 + 1 = 2 * (d + 1); rw [len_keysAll d, Nat.mul_succ]

theorem mem_keysAll (ν : Nat) (s : Bool) : ∀ d, ν < d → memL (ν, s) (keysAll d)
  | 0, h => absurd h (Nat.not_lt_zero _)
  | d + 1, h => by
    cases Nat.lt_or_ge ν d with
    | inl hl => exact Or.inr (Or.inr (mem_keysAll ν s d hl))
    | inr hg =>
      have e : ν = d := Nat.le_antisymm (Nat.le_of_lt_succ h) hg
      rw [e]
      cases s
      · exact Or.inl rfl
      · exact Or.inr (Or.inl rfl)

theorem shf_ne : ∀ (y : List Nat) (ν : Nat), ν < y.length → shf y ν ≠ y
  | [], _, h => absurd h (Nat.not_lt_zero _)
  | a :: y, 0, _ => fun e => by
    have := List.head_eq_of_cons_eq e
    exact Nat.succ_ne_self a this
  | a :: y, ν + 1, h => fun e => shf_ne y ν (Nat.lt_of_succ_lt_succ h) (List.tail_eq_of_cons_eq e)

theorem shf_inj : ∀ (y y' : List Nat) (ν : Nat), shf y ν = shf y' ν → y = y'
  | [], [], _, _ => rfl
  | [], _ :: _, 0, h => by cases h
  | [], _ :: _, _ + 1, h => by cases h
  | _ :: _, [], 0, h => by cases h
  | _ :: _, [], _ + 1, h => by cases h
  | a :: y, a' :: y', 0, h => by
    have e1 := List.head_eq_of_cons_eq h
    have e2 := List.tail_eq_of_cons_eq h
    rw [Nat.succ.inj e1, e2]
  | a :: y, a' :: y', ν + 1, h => by
    have e1 := List.head_eq_of_cons_eq h
    rw [e1, shf_inj y y' ν (List.tail_eq_of_cons_eq h)]

theorem len_shf : ∀ (y : List Nat) (ν : Nat), (shf y ν).length = y.length
  | [], _ => rfl
  | _ :: _, 0 => rfl
  | _ :: y, ν + 1 => congrArg Nat.succ (len_shf y ν)

def valid (d : Nat) (p : Pq) : Prop := p.1.length = d ∧ p.2.1 < p.2.2 ∧ p.2.2 < d

theorem linkIn_cases (x : List Nat) (μ : Nat) (y : List Nat) (a b : Nat) (h : linkIn (x, μ) (y, a, b) = true) :
    (μ = a ∧ (x = y ∨ x = shf y b)) ∨ (μ = b ∧ (x = y ∨ x = shf y a)) := by
  have h' := of_decide_eq_true h
  rcases h' with e | e | e | e
  · cases e; exact Or.inl ⟨rfl, Or.inl rfl⟩
  · cases e; exact Or.inr ⟨rfl, Or.inr rfl⟩
  · cases e; exact Or.inl ⟨rfl, Or.inr rfl⟩
  · cases e; exact Or.inr ⟨rfl, Or.inl rfl⟩

theorem side_eq (x y : List Nat) (ν : Nat) (hν : ν < y.length) (h : x = y ∨ x = shf y ν) (y' : List Nat)
    (hν' : ν < y'.length) (h' : x = y' ∨ x = shf y' ν)
    (hs : (if x = y then false else true) = (if x = y' then false else true)) : y = y' := by
  by_cases e : x = y
  · by_cases e' : x = y'
    · exact e.symm.trans e'
    · rw [MaskSum.ip e, MaskSum.ineg e'] at hs; exact absurd hs Bool.noConfusion
  · by_cases e' : x = y'
    · rw [MaskSum.ineg e, MaskSum.ip e'] at hs; exact absurd hs Bool.noConfusion
    · have h1 : x = shf y ν := h.resolve_left e
      have h2 : x = shf y' ν := h'.resolve_left e'
      exact shf_inj y y' ν (h1.symm.trans h2)

/-- **Injectivity of the key.** -/
theorem key_inj (d : Nat) (l : Lk) (p p' : Pq) (hp : valid d p) (hp' : valid d p')
    (h1 : linkIn l p = true) (h2 : linkIn l p' = true) (hk : key l p = key l p') : p = p' := by
  obtain ⟨y, a, b⟩ := p
  obtain ⟨y', a', b'⟩ := p'
  obtain ⟨x, μ⟩ := l
  obtain ⟨hy, hab, hb⟩ := hp
  obtain ⟨hy', hab', hb'⟩ := hp'
  simp only at hy hab hb hy' hab' hb'
  have k1 : (if μ = a then b else a) = (if μ = a' then b' else a') := congrArg Prod.fst hk
  have k2 : (if x = y then false else true) = (if x = y' then false else true) := congrArg Prod.snd hk
  have hbl : b < y.length := by rw [hy]; exact hb
  have hal : a < y.length := Nat.lt_trans hab hbl
  have hbl' : b' < y'.length := by rw [hy']; exact hb'
  have hal' : a' < y'.length := Nat.lt_trans hab' hbl'
  rcases linkIn_cases x μ y a b h1 with ⟨ea, ex⟩ | ⟨eb, ex⟩ <;>
    rcases linkIn_cases x μ y' a' b' h2 with ⟨ea', ex'⟩ | ⟨eb', ex'⟩
  · -- μ = a = a'
    rw [MaskSum.ip ea, MaskSum.ip ea'] at k1
    have eaa : a = a' := ea.symm.trans ea'
    rw [← k1] at ex'
    have hyy := side_eq x y b hbl ex y' (by rw [k1]; exact hbl') ex' k2
    rw [hyy, eaa, k1]
  · -- μ = a, μ = b'
    have hμa' : ¬ μ = a' := fun h => by rw [eb'] at h; rw [h] at hab'; exact Nat.lt_irrefl _ hab'
    rw [MaskSum.ip ea, MaskSum.ineg hμa'] at k1
    have c1 : a < b' := Nat.lt_trans hab (by rw [k1]; exact hab')
    rw [← ea, ← eb'] at c1
    exact absurd c1 (Nat.lt_irrefl μ)
  · -- μ = b, μ = a'
    have hμa : ¬ μ = a := fun h => by rw [eb] at h; rw [h] at hab; exact Nat.lt_irrefl _ hab
    rw [MaskSum.ineg hμa, MaskSum.ip ea'] at k1
    have c1 : a' < b := Nat.lt_trans hab' (by rw [← k1]; exact hab)
    rw [← ea', ← eb] at c1
    exact absurd c1 (Nat.lt_irrefl μ)
  · -- μ = b = b'
    have hμa : ¬ μ = a := fun h => by rw [eb] at h; rw [h] at hab; exact Nat.lt_irrefl _ hab
    have hμa' : ¬ μ = a' := fun h => by rw [eb'] at h; rw [h] at hab'; exact Nat.lt_irrefl _ hab'
    rw [MaskSum.ineg hμa, MaskSum.ineg hμa'] at k1
    have ebb : b = b' := eb.symm.trans eb'
    rw [← k1] at ex'
    have hyy := side_eq x y a hal ex y' (by rw [k1]; exact hal') ex' k2
    rw [hyy, ebb, k1]

theorem key_mem (d : Nat) (l : Lk) (p : Pq) (hp : valid d p) : memL (key l p) (keysAll d) := by
  obtain ⟨y, a, b⟩ := p
  obtain ⟨_, hab, hb⟩ := hp
  show memL (if l.2 = a then b else a, if l.1 = y then false else true) (keysAll d)
  apply mem_keysAll
  by_cases e : l.2 = a
  · rw [MaskSum.ip e]; exact hb
  · rw [MaskSum.ineg e]; exact Nat.lt_trans hab hb

/-- **Each link lies in at most 2d plaquettes.** -/
theorem linkdeg_Zd (d : Nat) (l : Lk) (P : List Pq) (hnd : nodupL P) (hv : ∀ p, memL p P → valid d p) :
    cnt (P.map (linkIn l)) ≤ 2 * d := by
  rw [← len_keysAll d]
  exact count_inj (linkIn l) (key l) P (keysAll d) hnd (fun p hp _ => key_mem d l p (hv p hp))
    (fun p p' hp hp' h1 h2 hk => key_inj d l p p' (hv p hp) (hv p' hp') h1 h2 hk)

end ZdLinks

#print axioms ZdLinks.key_inj
#print axioms ZdLinks.linkdeg_Zd

ym/ZdDegree.lean

/-
  ym/ZdDegree — T45, cycle 79 (Lad4 step 3): the degree hypothesis holds on Z^d with Dg = 4·(2d) = 8d.

  LL: a duplicate-free list of links (the links of the region); a plaquette p becomes the link-incidence vector
  vec p = (1 if l ∈ p else 0)_{l ∈ LL}; a duplicate-free list Pn of valid plaquettes gives P = Pn.map vec.
    nth_vec          (vec p)_i = [LL_i ∈ p]
    supp_le4         a plaquette has at most 4 links
    linkdeg_vec      every link lies in at most 2d of the vectors
    deg_Zd           |{q ∈ P : q touches vec p}| ≤ 8d        — the hypothesis hdeg of AreaUpper, Gap, AreaG, GapG,
                                                               AreaH, GapH, for every finite subset of Z^d

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.ZdLinks

namespace ZdDegree

open YMChain Closure Factor InjCount ZdLinks Degree

def vec (LL : List Lk) (p : Pq) : List Nat := LL.map (fun l => if linkIn l p = true then 1 else 0)

theorem nthN_vec (p : Pq) : ∀ (LL : List Lk) (i : Nat), nzb (nthN (vec LL p) i) = (match Indep.getO LL i with
    | some l => linkIn l p | none => false)
  | [], _ => rfl
  | l :: LL, 0 => by
    show nzb (if linkIn l p = true then 1 else 0) = linkIn l p
    cases linkIn l p <;> rfl
  | _ :: LL, i + 1 => nthN_vec p LL i

def slot (p : Pq) (l : Lk) : Nat :=
  if l = (p.1, p.2.1) then 0 else if l = (shf p.1 p.2.1, p.2.2) then 1 else if l = (shf p.1 p.2.2, p.2.1) then 2 else 3

theorem slot_inj (p : Pq) (l l' : Lk) (h : linkIn l p = true) (h' : linkIn l' p = true) (hs : slot p l = slot p l') :
    l = l' := by
  have c := of_decide_eq_true h
  have c' := of_decide_eq_true h'
  unfold slot at hs
  by_cases e1 : l = (p.1, p.2.1)
  · by_cases f1 : l' = (p.1, p.2.1)
    · exact e1.trans f1.symm
    · rw [MaskSum.ip e1, MaskSum.ineg f1] at hs
      by_cases f2 : l' = (shf p.1 p.2.1, p.2.2)
      · rw [MaskSum.ip f2] at hs; exact absurd hs (by decide)
      · rw [MaskSum.ineg f2] at hs
        by_cases f3 : l' = (shf p.1 p.2.2, p.2.1)
        · rw [MaskSum.ip f3] at hs; exact absurd hs (by decide)
        · rw [MaskSum.ineg f3] at hs; exact absurd hs (by decide)
  · rw [MaskSum.ineg e1] at hs
    by_cases e2 : l = (shf p.1 p.2.1, p.2.2)
    · rw [MaskSum.ip e2] at hs
      by_cases f1 : l' = (p.1, p.2.1)
      · rw [MaskSum.ip f1] at hs; exact absurd hs (by decide)
      · rw [MaskSum.ineg f1] at hs
        by_cases f2 : l' = (shf p.1 p.2.1, p.2.2)
        · exact e2.trans f2.symm
        · rw [MaskSum.ineg f2] at hs
          by_cases f3 : l' = (shf p.1 p.2.2, p.2.1)
          · rw [MaskSum.ip f3] at hs; exact absurd hs (by decide)
          · rw [MaskSum.ineg f3] at hs; exact absurd hs (by decide)
    · rw [MaskSum.ineg e2] at hs
      by_cases e3 : l = (shf p.1 p.2.2, p.2.1)
      · rw [MaskSum.ip e3] at hs
        by_cases f1 : l' = (p.1, p.2.1)
        · rw [MaskSum.ip f1] at hs; exact absurd hs (by decide)
        · rw [MaskSum.ineg f1] at hs
          by_cases f2 : l' = (shf p.1 p.2.1, p.2.2)
          · rw [MaskSum.ip f2] at hs; exact absurd hs (by decide)
          · rw [MaskSum.ineg f2] at hs
            by_cases f3 : l' = (shf p.1 p.2.2, p.2.1)
            · exact e3.trans f3.symm
            · rw [MaskSum.ineg f3] at hs; exact absurd hs (by decide)
      · rw [MaskSum.ineg e3] at hs
        have e4 : l = (p.1, p.2.2) := by
          rcases c with x | x | x | x
          · exact absurd x e1
          · exact absurd x e2
          · exact absurd x e3
          · exact x
        by_cases f1 : l' = (p.1, p.2.1)
        · rw [MaskSum.ip f1] at hs; exact absurd hs (by decide)
        · rw [MaskSum.ineg f1] at hs
          by_cases f2 : l' = (shf p.1 p.2.1, p.2.2)
          · rw [MaskSum.ip f2] at hs; exact absurd hs (by decide)
          · rw [MaskSum.ineg f2] at hs
            by_cases f3 : l' = (shf p.1 p.2.2, p.2.1)
            · rw [MaskSum.ip f3] at hs; exact absurd hs (by decide)
            · have f4 : l' = (p.1, p.2.2) := by
                rcases c' with x | x | x | x
                · exact absurd x f1
                · exact absurd x f2
                · exact absurd x f3
                · exact x
              exact e4.trans f4.symm

theorem slot_mem (p : Pq) (l : Lk) : memL (slot p l) [0, 1, 2, 3] := by
  unfold slot
  by_cases e1 : l = (p.1, p.2.1)
  · rw [MaskSum.ip e1]; exact Or.inl rfl
  · rw [MaskSum.ineg e1]
    by_cases e2 : l = (shf p.1 p.2.1, p.2.2)
    · rw [MaskSum.ip e2]; exact Or.inr (Or.inl rfl)
    · rw [MaskSum.ineg e2]
      by_cases e3 : l = (shf p.1 p.2.2, p.2.1)
      · rw [MaskSum.ip e3]; exact Or.inr (Or.inr (Or.inl rfl))
      · rw [MaskSum.ineg e3]; exact Or.inr (Or.inr (Or.inr (Or.inl rfl)))

theorem cnt_map_eq {α : Type} (f g : α → Bool) (h : ∀ a, f a = g a) : ∀ l : List α, cnt (l.map f) = cnt (l.map g)
  | [] => rfl
  | a :: l => by
    show cnt (f a :: l.map f) = cnt (g a :: l.map g)
    have ih := cnt_map_eq f g h l
    rw [h a]
    cases g a
    · exact ih
    · exact congrArg Nat.succ ih

theorem map_map_g {α β γ : Type} (f : α → β) (g : β → γ) : ∀ l : List α, (l.map f).map g = l.map (fun a => g (f a))
  | [] => rfl
  | a :: l => by show g (f a) :: (l.map f).map g = g (f a) :: _; rw [map_map_g f g l]

theorem cnt_false {α : Type} : ∀ l : List α, cnt (l.map (fun _ => false)) = 0
  | [] => rfl
  | _ :: l => cnt_false l

/-- **A plaquette has at most 4 links.** -/
theorem supp_le4 (LL : List Lk) (hLL : nodupL LL) (p : Pq) : cnt ((vec LL p).map nzb) ≤ 4 := by
  have e : cnt ((vec LL p).map nzb) = cnt (LL.map (fun l => linkIn l p)) := by
    unfold vec
    induction LL with
    | nil => rfl
    | cons l LL ih =>
      show cnt (nzb (if linkIn l p = true then 1 else 0) :: _) = cnt (linkIn l p :: _)
      have : nzb (if linkIn l p = true then 1 else 0) = linkIn l p := by cases linkIn l p <;> rfl
      rw [this]
      cases linkIn l p
      · exact ih hLL.2
      · exact congrArg Nat.succ (ih hLL.2)
  rw [e]
  exact count_inj (fun l => linkIn l p) (slot p) LL [0, 1, 2, 3] hLL (fun l _ _ => slot_mem p l)
    (fun l l' _ _ h h' hs => slot_inj p l l' h h' hs)

/-- **Every link lies in at most 2d plaquette vectors.** -/
theorem linkdeg_vec (d : Nat) (LL : List Lk) (Pn : List Pq) (hnd : nodupL Pn) (hv : ∀ p, memL p Pn → valid d p) (i : Nat) :
    linkdeg (Pn.map (vec LL)) i ≤ 2 * d := by
  unfold linkdeg
  rw [map_map_g]
  rw [cnt_map_eq _ (fun p => match Indep.getO LL i with | some l => linkIn l p | none => false) (fun p => nthN_vec p LL i)]
  cases Indep.getO LL i with
  | none =>
    show cnt (Pn.map (fun _ => false)) ≤ 2 * d
    rw [cnt_false Pn]; exact Nat.zero_le _
  | some l => exact linkdeg_Zd d l Pn hnd hv

/-- **The degree hypothesis on Z^d: Dg = 4·(2d) = 8d.** -/
theorem deg_Zd (d : Nat) (LL : List Lk) (hLL : nodupL LL) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p : Pq) :
    cnt ((Pn.map (vec LL)).map (fun q => touch q (vec LL p))) ≤ 4 * (2 * d) := by
  refine Nat.le_trans (deg_bound _ _) (Nat.le_trans (deg_uniform _ (2 * d) (linkdeg_vec d LL Pn hnd hv) 0 _) ?_)
  exact Nat.mul_le_mul_right (2 * d) (supp_le4 LL hLL p)

end ZdDegree

#print axioms ZdDegree.supp_le4
#print axioms ZdDegree.linkdeg_vec
#print axioms ZdDegree.deg_Zd

ym/ZdDist.lean

/-
  ym/ZdDist — T45, cycle 80 (Lad4 step 4): closure steps versus lattice distance on Z^d.

  dcl k x y : every coordinate of x and y differs by at most k (ℓ∞ distance ≤ k).
    dcl_trans, dcl_mono        triangle inequality, monotonicity
    shared_dcl                 two plaquettes with a common link have base points at distance ≤ 1
    touch_shared               touching link vectors share a link of LL
    ball_dist                  every plaquette reached in k closure steps from the plaquettes touching p0 has its base
                               within distance k of p0's base
    far_ok                     if dist(base p0, base q0) > R + 1, no plaquette of the R-ball around p0 touches q0 —
                               the hypothesis hfar of GapG.gap_G / GapH.gap_H
  So on Z^d the strong-coupling clustering decays like u^R with R = dist(p, q) − 2.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.ZdDegree

namespace ZdDist

open YMChain Closure Factor Seeds InjCount ZdLinks ZdDegree Degree Reach

def dcl (k : Nat) (x y : List Nat) : Prop := ∀ i, nthN x i ≤ nthN y i + k ∧ nthN y i ≤ nthN x i + k

theorem dcl_symm (k : Nat) (x y : List Nat) (h : dcl k x y) : dcl k y x := fun i => ⟨(h i).2, (h i).1⟩

theorem dcl_trans (k m : Nat) (x y z : List Nat) (h1 : dcl k x y) (h2 : dcl m y z) : dcl (k + m) x z := fun i =>
  ⟨Nat.le_trans (h1 i).1 (by
      have := Nat.add_le_add_right (h2 i).1 k
      rw [Nat.add_assoc, Nat.add_comm m k] at this; exact this),
   Nat.le_trans (h2 i).2 (by
      have := Nat.add_le_add_right (h1 i).2 m
      rw [Nat.add_assoc] at this; exact this)⟩

theorem dcl_mono (k m : Nat) (x y : List Nat) (hkm : k ≤ m) (h : dcl k x y) : dcl m x y := fun i =>
  ⟨Nat.le_trans (h i).1 (Nat.add_le_add_left hkm _), Nat.le_trans (h i).2 (Nat.add_le_add_left hkm _)⟩

theorem shf_coord : ∀ (y : List Nat) (ν i : Nat), nthN y i ≤ nthN (shf y ν) i ∧ nthN (shf y ν) i ≤ nthN y i + 1
  | [], _, _ => ⟨Nat.le_refl _, Nat.le_succ _⟩
  | a :: y, 0, 0 => ⟨Nat.le_succ a, Nat.le_refl _⟩
  | a :: y, 0, _ + 1 => ⟨Nat.le_refl _, Nat.le_succ _⟩
  | a :: y, _ + 1, 0 => ⟨Nat.le_refl a, Nat.le_succ a⟩
  | a :: y, ν + 1, i + 1 => shf_coord y ν i

/-- the point of a link of p lies in [y, y + 1] coordinatewise -/
theorem link_point (x : List Nat) (μ : Nat) (p : Pq) (h : linkIn (x, μ) p = true) :
    ∀ i, nthN p.1 i ≤ nthN x i ∧ nthN x i ≤ nthN p.1 i + 1 := by
  obtain ⟨y, a, b⟩ := p
  rcases linkIn_cases x μ y a b h with ⟨_, ex⟩ | ⟨_, ex⟩ <;> intro i <;> rcases ex with e | e <;> rw [e]
  · exact ⟨Nat.le_refl _, Nat.le_succ _⟩
  · exact shf_coord y b i
  · exact ⟨Nat.le_refl _, Nat.le_succ _⟩
  · exact shf_coord y a i

theorem shared_dcl (l : Lk) (p q : Pq) (hp : linkIn l p = true) (hq : linkIn l q = true) : dcl 1 p.1 q.1 := by
  obtain ⟨x, μ⟩ := l
  intro i
  have a := link_point x μ p hp i
  have b := link_point x μ q hq i
  exact ⟨Nat.le_trans a.1 b.2, Nat.le_trans b.1 a.2⟩

theorem touch_ex : ∀ (u v : List Nat), touch u v = true → ∃ i, nzb (nthN u i) = true ∧ nzb (nthN v i) = true
  | [], _, h => absurd h Bool.noConfusion
  | _ :: _, [], h => absurd h Bool.noConfusion
  | a :: u, b :: v, h => by
    have h' : ((nzb a && nzb b) || touch u v) = true := h
    cases e : (nzb a && nzb b) with
    | true =>
      cases ea : nzb a <;> cases eb : nzb b <;> rw [ea, eb] at e
      · exact absurd e Bool.noConfusion
      · exact absurd e Bool.noConfusion
      · exact absurd e Bool.noConfusion
      · exact ⟨0, ea, eb⟩
    | false =>
      rw [e, Bool.false_or] at h'
      obtain ⟨i, h1, h2⟩ := touch_ex u v h'
      exact ⟨i + 1, h1, h2⟩

theorem touch_shared (LL : List Lk) (p q : Pq) (h : touch (vec LL p) (vec LL q) = true) :
    ∃ l, linkIn l p = true ∧ linkIn l q = true := by
  obtain ⟨i, h1, h2⟩ := touch_ex _ _ h
  rw [nthN_vec] at h1 h2
  cases e : Indep.getO LL i with
  | none => rw [e] at h1; exact absurd h1 Bool.noConfusion
  | some l => rw [e] at h1 h2; exact ⟨l, h1, h2⟩

theorem touch_dcl (LL : List Lk) (p q : Pq) (h : touch (vec LL p) (vec LL q) = true) : dcl 1 p.1 q.1 := by
  obtain ⟨l, h1, h2⟩ := touch_shared LL p q h
  exact shared_dcl l p q h1 h2

def getP (Pn : List Pq) (i : Nat) : Pq := match Indep.getO Pn i with | some p => p | none => ([], 0, 0)

theorem nthL_map (LL : List Lk) : ∀ (Pn : List Pq) (i : Nat), i < Pn.length → nthL (Pn.map (vec LL)) i = vec LL (getP Pn i)
  | [], _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, 0, _ => rfl
  | _ :: Pn, i + 1, h => nthL_map LL Pn i (Nat.lt_of_succ_lt_succ h)

/-- **Balls of closure steps stay within lattice distance.** -/
theorem ball_dist (LL : List Lk) (Pn : List Pq) (p0 : Pq) :
    ∀ (k i : Nat), i < Pn.length →
      nthB (iter (Vs (fun r => touch r (vec LL p0)) (Pn.map (vec LL))) (Pn.map (vec LL)).length
        (AreaUpper.tr (Pn.map (vec LL)).length) k) i = true → dcl k (getP Pn i).1 p0.1
  | 0, i, _, h => by
    have : nthB (fz (Pn.map (vec LL)).length) i = false := nth_fz _ i
    exact absurd (this ▸ h) Bool.noConfusion
  | k + 1, i, hi, h => by
    let σ : List Nat → Bool := fun r => touch r (vec LL p0)
    let P := Pn.map (vec LL)
    have hiP : i < P.length := by rw [StripAxial.len_map_g]; exact hi
    rw [iter_nth σ P (AreaUpper.tr P.length) (AreaUpper.len_tr _) k i hiP] at h
    have hq : nthL P i = vec LL (getP Pn i) := nthL_map LL Pn i hi
    cases e1 : nthB (iter (Vs σ P) P.length (AreaUpper.tr P.length) k) i with
    | true => exact dcl_mono k (k + 1) _ _ (Nat.le_succ k) (ball_dist LL Pn p0 k i hi e1)
    | false =>
      rw [e1] at h
      cases e2 : σ (nthL P i) with
      | true =>
        have t : touch (vec LL (getP Pn i)) (vec LL p0) = true := by rw [← hq]; exact e2
        exact dcl_mono 1 (k + 1) _ _ (Nat.succ_le_succ (Nat.zero_le k)) (touch_dcl LL _ _ t)
      | false =>
        rw [e2] at h
        have h' : anyT P (iter (Vs σ P) P.length (AreaUpper.tr P.length) k) (nthL P i) = true := by
          cases ea : anyT P (iter (Vs σ P) P.length (AreaUpper.tr P.length) k) (nthL P i)
          · rw [ea] at h; cases e3 : nthB (AreaUpper.tr P.length) i <;> rw [e3] at h <;> exact absurd h (by decide)
          · rfl
        obtain ⟨j, hj, hj1, hj2⟩ := anyT_ex P _ _ h'
        have hjn : j < Pn.length := by rw [← StripAxial.len_map_g (vec LL) Pn]; exact hj
        rw [hq, nthL_map LL Pn j hjn] at hj2
        have d1 := touch_dcl LL _ _ hj2
        have d2 := ball_dist LL Pn p0 k j hjn hj1
        have := dcl_trans 1 k _ _ _ d1 d2
        rw [Nat.add_comm] at this
        exact this

/-- **Far plaquettes are not reached.** -/
theorem far_ok (LL : List Lk) (Pn : List Pq) (p0 q0 : Pq) (R : Nat) (hfar : ¬ dcl (R + 1) p0.1 q0.1) :
    ∀ i, i < (Pn.map (vec LL)).length →
      nthB (ball (fun r => touch r (vec LL p0)) (Pn.map (vec LL)) R) i = true →
      touch (nthL (Pn.map (vec LL)) i) (vec LL q0) = false := by
  intro i hi hb
  have hin : i < Pn.length := by rw [← StripAxial.len_map_g (vec LL) Pn]; exact hi
  cases e : touch (nthL (Pn.map (vec LL)) i) (vec LL q0) with
  | false => rfl
  | true =>
    rw [nthL_map LL Pn i hin] at e
    have d1 := touch_dcl LL _ _ e
    have d2 := ball_dist LL Pn p0 R i hin hb
    exact absurd (dcl_trans R 1 _ _ _ (dcl_symm R _ _ d2) d1) hfar

end ZdDist

#print axioms ZdDist.dcl_trans
#print axioms ZdDist.shared_dcl
#print axioms ZdDist.touch_dcl
#print axioms ZdDist.ball_dist
#print axioms ZdDist.far_ok

ym/Flux.lean

/-
  ym/Flux — T45, cycle 90 (Lad1-inst step 1): a flux functional detecting plaquettes over a square.

  Points of Z^d are coordinate lists; coordinates 0 and 1 span the plane of the Wilson loop. For a square (i, j) of
  that plane, φ_{ij}(x, μ) = [μ = 1 ∧ x₁ = j ∧ x₀ ≤ i] (vertical links of row j left of column i+1).
    phi_plaq        for a valid plaquette p = (y, a, b) (a < b):  φ(p's 4 links) is odd  ⟺  a = 0, b = 1, y₀ = i, y₁ = j
  Every plaquette set A with ∂A = W (mod 2) therefore contains, for every square (i, j) with φ(W) odd, a (0,1)-plaquette
  over (i, j) — the projection argument behind "area(W) ≤ |A|" (next cycles).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Block2D

namespace Flux

open Degree ZdLinks

def phi (i j : Nat) (l : Lk) : Bool := decide (l.2 = 1 ∧ nthN l.1 1 = j ∧ nthN l.1 0 ≤ i)

theorem shf_c0 : ∀ (y : List Nat) (ν : Nat), ν ≠ 0 → nthN (shf y ν) 0 = nthN y 0
  | [], _, _ => rfl
  | _ :: _, 0, h => absurd rfl h
  | _ :: _, _ + 1, _ => rfl

theorem shf_c1 : ∀ (y : List Nat) (ν : Nat), ν ≠ 1 → nthN (shf y ν) 1 = nthN y 1
  | [], _, _ => rfl
  | _ :: [], 0, _ => rfl
  | _ :: _ :: _, 0, _ => rfl
  | _ :: [], 1, h => absurd rfl h
  | _ :: _ :: _, 1, h => absurd rfl h
  | _ :: [], _ + 2, _ => rfl
  | _ :: _ :: _, _ + 2, _ => rfl

theorem shf0_c0 (y : List Nat) (hy : 0 < y.length) : nthN (shf y 0) 0 = nthN y 0 + 1 := by
  cases y with
  | nil => exact absurd hy (Nat.lt_irrefl 0)
  | cons a y => rfl

def b2n (b : Bool) : Nat := if b = true then 1 else 0

/-- parity of the φ-count of a plaquette -/
def cnt4 (i j : Nat) (p : Pq) : Nat :=
  b2n (phi i j (p.1, p.2.1)) + b2n (phi i j (shf p.1 p.2.1, p.2.2)) + b2n (phi i j (shf p.1 p.2.2, p.2.1))
    + b2n (phi i j (p.1, p.2.2))

theorem phi_dir (i j : Nat) (x : List Nat) (μ : Nat) (h : μ ≠ 1) : phi i j (x, μ) = false :=
  decide_eq_false (fun c => h c.1)

theorem phi_t (i j : Nat) (x : List Nat) (h2 : nthN x 1 = j) (h3 : nthN x 0 ≤ i) : phi i j (x, 1) = true :=
  decide_eq_true ⟨rfl, h2, h3⟩

theorem phi_f2 (i j : Nat) (x : List Nat) (h2 : nthN x 1 ≠ j) : phi i j (x, 1) = false :=
  decide_eq_false (fun c => h2 c.2.1)

theorem phi_f3 (i j : Nat) (x : List Nat) (h3 : ¬ nthN x 0 ≤ i) : phi i j (x, 1) = false :=
  decide_eq_false (fun c => h3 c.2.2)

theorem ne21 (b : Nat) : b + 2 ≠ 1 := fun h => Nat.noConfusion (Nat.succ.inj h)

/-- **φ detects the (0,1)-plaquette over the square (i, j).** -/
theorem phi_plaq (i j : Nat) (y : List Nat) (a b : Nat) (hab : a < b) (hb : b < y.length) :
    StrongZ2.parity (cnt4 i j (y, a, b)) = !(decide (a = 0 ∧ b = 1 ∧ nthN y 0 = i ∧ nthN y 1 = j)) := by
  show StrongZ2.parity (b2n (phi i j (y, a)) + b2n (phi i j (shf y a, b)) + b2n (phi i j (shf y b, a)) + b2n (phi i j (y, b))) = _
  cases a with
  | zero =>
    rw [phi_dir i j y 0 (by decide), phi_dir i j (shf y b) 0 (by decide)]
    cases b with
    | zero => exact absurd hab (Nat.lt_irrefl 0)
    | succ b =>
      cases b with
      | zero =>
        have h0 : 0 < y.length := Nat.lt_trans (Nat.zero_lt_succ 0) hb
        have c1 : nthN (shf y 0) 1 = nthN y 1 := shf_c1 y 0 (by decide)
        have c0 : nthN (shf y 0) 0 = nthN y 0 + 1 := shf0_c0 y h0
        by_cases hj : nthN y 1 = j
        · by_cases hi : nthN y 0 = i
          · rw [phi_f3 i j (shf y 0) (by rw [c0, hi]; exact Nat.not_succ_le_self i), phi_t i j y hj (by rw [hi]; exact Nat.le_refl _),
              decide_eq_true ⟨rfl, rfl, hi, hj⟩]
            rfl
          · cases Nat.lt_or_ge (nthN y 0) i with
            | inl hlt =>
              rw [phi_t i j (shf y 0) (by rw [c1]; exact hj) (by rw [c0]; exact hlt), phi_t i j y hj (Nat.le_of_lt hlt),
                decide_eq_false (fun c => hi c.2.2.1)]
              rfl
            | inr hge =>
              have hgt : i < nthN y 0 := Nat.lt_of_le_of_ne hge (fun e => hi e.symm)
              rw [phi_f3 i j (shf y 0) (by rw [c0]; exact fun h => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le hgt (Nat.le_of_succ_le h))),
                phi_f3 i j y (fun h => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le hgt h)), decide_eq_false (fun c => hi c.2.2.1)]
              rfl
        · rw [phi_f2 i j (shf y 0) (by rw [c1]; exact hj), phi_f2 i j y hj, decide_eq_false (fun c => hj c.2.2.2)]
          rfl
      | succ b =>
        rw [phi_dir i j (shf y 0) (b + 2) (ne21 _), phi_dir i j y (b + 2) (ne21 _),
          decide_eq_false (fun c => ne21 _ c.2.1)]
        rfl
  | succ a =>
    cases a with
    | zero =>
      -- a = 1, b ≥ 2
      have hb2 : b ≠ 0 ∧ b ≠ 1 := ⟨fun h => by rw [h] at hab; exact Nat.not_succ_le_zero 1 hab,
        fun h => by rw [h] at hab; exact Nat.lt_irrefl 1 hab⟩
      have e : phi i j (shf y b, 1) = phi i j (y, 1) := by
        cases ev : phi i j (y, 1) with
        | true =>
          have c := of_decide_eq_true ev
          exact decide_eq_true ⟨rfl, by rw [shf_c1 y b hb2.2]; exact c.2.1, by rw [shf_c0 y b hb2.1]; exact c.2.2⟩
        | false =>
          exact decide_eq_false (fun c => by
            have : phi i j (y, 1) = true := decide_eq_true ⟨rfl, by rw [← shf_c1 y b hb2.2]; exact c.2.1,
              by rw [← shf_c0 y b hb2.1]; exact c.2.2⟩
            rw [ev] at this; exact Bool.noConfusion this)
      rw [e, phi_dir i j (shf y 1) b hb2.2, phi_dir i j y b hb2.2, decide_eq_false (fun c => Nat.noConfusion c.1)]
      cases phi i j (y, 1) <;> rfl
    | succ a =>
      have hbne : b ≠ 1 := fun h => by rw [h] at hab; exact Nat.not_succ_le_zero _ (Nat.le_of_succ_le_succ hab)
      rw [phi_dir i j y (a + 2) (ne21 _), phi_dir i j (shf y (a + 2)) b hbne,
        phi_dir i j (shf y b) (a + 2) (ne21 _), phi_dir i j y b hbne, decide_eq_false (fun c => Nat.noConfusion c.1)]
      rfl

end Flux

#print axioms Flux.shf_c1
#print axioms Flux.phi_plaq

ym/AreaBound.lean

/-
  ym/AreaBound — T45, cycle 91 (Lad1-inst step 2): spanning surfaces have at least as many plaquettes as squares.

  Vectors over a link list (entries = multiplicities), a 0/1 mask φ on links, wsum φ v = Σ_{φ_l} v_l.
    wsum_zadd        wsum φ (u + v) = wsum φ u + wsum φ v
    wsum_even        evenAll v ⇒ wsum φ v even
    wsum_bd          wsum φ (∂A) = Σ_{p ∈ A} wsum φ p
    odd_exists       if wsum φ n is odd and n + ∂A is even, some selected plaquette has odd wsum φ
    surj_count       if every square s (a duplicate-free list) is the key of some element of A, |squares| ≤ |A|
    area_bound       if for every square s there is φ_s with wsum φ_s n odd and (wsum φ_s p odd ⇒ key p = s), then
                     every A with n + ∂A even has at least |squares| plaquettes
  With Flux.phi_plaq (cycle 92) the squares are those of an R × T rectangle: m = RT in AreaUpper.area_upper.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Flux

namespace AreaBound

open YMChain GKS StrongZ2 Closure Factor Tail InjCount Count

def wsum : List Bool → List Nat → Nat
  | [], _ => 0
  | _ :: _, [] => 0
  | b :: φ, a :: v => (if b = true then a else 0) + wsum φ v

theorem wsum_nil_r : ∀ φ : List Bool, wsum φ [] = 0
  | [] => rfl
  | _ :: _ => rfl

theorem wsum_zadd : ∀ (φ : List Bool) (u v : List Nat), wsum φ (zadd u v) = wsum φ u + wsum φ v
  | [], _, _ => by show 0 = 0 + 0; rfl
  | _ :: _, [], v => by show wsum _ v = 0 + wsum _ v; rw [Nat.zero_add]
  | b :: φ, a :: u, [] => by show wsum (b :: φ) (a :: u) = wsum (b :: φ) (a :: u) + wsum (b :: φ) []; rw [wsum_nil_r]; rfl
  | b :: φ, a :: u, c :: v => by
    show (if b = true then a + c else 0) + wsum φ (zadd u v)
      = (if b = true then a else 0) + wsum φ u + ((if b = true then c else 0) + wsum φ v)
    rw [wsum_zadd φ u v]
    cases b
    · show 0 + (wsum φ u + wsum φ v) = 0 + wsum φ u + (0 + wsum φ v); rw [Nat.zero_add, Nat.zero_add, Nat.zero_add]
    · show a + c + (wsum φ u + wsum φ v) = a + wsum φ u + (c + wsum φ v)
      rw [Nat.add_assoc, ← Nat.add_assoc c, Nat.add_comm c (wsum φ u), Nat.add_assoc, ← Nat.add_assoc a]

theorem parity_add (a b : Nat) : parity (a + b) = (parity a == parity b) := by
  induction b with
  | zero => show parity a = (parity a == true); cases parity a <;> rfl
  | succ b ih => show (!parity (a + b)) = (parity a == !parity b); rw [ih]; cases parity a <;> cases parity b <;> rfl

theorem wsum_even : ∀ (φ : List Bool) (v : List Nat), evenAll v = true → parity (wsum φ v) = true
  | [], _, _ => rfl
  | _ :: _, [], _ => rfl
  | b :: φ, a :: v, h => by
    have h' : (parity a && evenAll v) = true := h
    have ha : parity a = true := by
      cases e : parity a with
      | true => rfl
      | false => rw [e, Bool.false_and] at h'; exact Bool.noConfusion h'
    have hv : evenAll v = true := by rw [ha] at h'; exact h'
    show parity ((if b = true then a else 0) + wsum φ v) = true
    rw [parity_add, wsum_even φ v hv]
    cases b
    · rfl
    · show (parity a == true) = true; rw [ha]; rfl

/-- the φ-sums of the selected plaquettes -/
def selSum (φ : List Bool) : List (List Nat) → List Bool → Nat
  | [], _ => 0
  | _ :: _, [] => 0
  | q :: P, true :: A => wsum φ q + selSum φ P A
  | _ :: P, false :: A => selSum φ P A

theorem wsum_bd (φ : List Bool) : ∀ (P : List (List Nat)) (A : List Bool), wsum φ (bdS P A) = selSum φ P A
  | [], _ => wsum_nil_r φ
  | _ :: _, [] => wsum_nil_r φ
  | q :: P, true :: A => by show wsum φ (zadd q (bdS P A)) = _; rw [wsum_zadd, wsum_bd φ P A]; rfl
  | _ :: P, false :: A => wsum_bd φ P A

theorem selSum_even (φ : List Bool) : ∀ (P : List (List Nat)) (A : List Bool),
    (∀ i, i < P.length → Seeds.nthB A i = true → parity (wsum φ (Seeds.nthL P i)) = true) → parity (selSum φ P A) = true
  | [], _, _ => rfl
  | _ :: _, [], _ => rfl
  | q :: P, true :: A, h => by
    show parity (wsum φ q + selSum φ P A) = true
    have h0 : parity (wsum φ q) = true := h 0 (Nat.succ_pos _) rfl
    rw [parity_add, h0,
      selSum_even φ P A (fun i hi ha => h (i + 1) (Nat.succ_lt_succ hi) ha)]
    rfl
  | _ :: P, false :: A, h => selSum_even φ P A (fun i hi ha => h (i + 1) (Nat.succ_lt_succ hi) ha)

/-- **An odd flux through the loop forces an odd plaquette.** -/
theorem odd_exists (φ : List Bool) (n : List Nat) (P : List (List Nat)) (A : List Bool)
    (hn : parity (wsum φ n) = false) (hev : evenAll (zadd n (bdS P A)) = true) :
    ∃ i, i < P.length ∧ Seeds.nthB A i = true ∧ parity (wsum φ (Seeds.nthL P i)) = false := by
  have h := wsum_even φ _ hev
  rw [wsum_zadd, parity_add, hn, wsum_bd] at h
  -- (false == parity selSum) = true ⇒ selSum odd
  have hs : parity (selSum φ P A) = false := by
    cases e : parity (selSum φ P A)
    · rfl
    · rw [e] at h; exact absurd h (by decide)
  -- contrapositive of selSum_even, done constructively by induction
  revert hs
  clear h hev hn
  induction P generalizing A with
  | nil => intro hs; exact Bool.noConfusion (show true = false from hs)
  | cons q P ih =>
    cases A with
    | nil => intro hs; exact Bool.noConfusion (show true = false from hs)
    | cons a A =>
      cases a with
      | false =>
        intro hs
        obtain ⟨i, hi, h1, h2⟩ := ih A hs
        exact ⟨i + 1, Nat.succ_lt_succ hi, h1, h2⟩
      | true =>
        intro hs
        have hs' : parity (wsum φ q + selSum φ P A) = false := hs
        rw [parity_add] at hs'
        cases e1 : parity (wsum φ q) with
        | false => exact ⟨0, Nat.succ_pos _, rfl, e1⟩
        | true =>
          rw [e1] at hs'
          have h3 : parity (selSum φ P A) = false := by
            cases e2 : parity (selSum φ P A)
            · rfl
            · rw [e2] at hs'; exact absurd hs' (by decide)
          obtain ⟨i, hi, h1, h2⟩ := ih A h3
          exact ⟨i + 1, Nat.succ_lt_succ hi, h1, h2⟩

theorem mem_of_erase {β : Type} [DecidableEq β] (b x : β) : ∀ l : List β, memL x (eraseL b l) → memL x l
  | [], h => h
  | c :: l, h => by
    have h' : memL x (if c = b then l else c :: eraseL b l) := h
    by_cases e : c = b
    · rw [MaskSum.ip e] at h'; exact Or.inr h'
    · rw [MaskSum.ineg e] at h'
      cases h' with
      | inl h'' => exact Or.inl h''
      | inr h'' => exact Or.inr (mem_of_erase b x l h'')

theorem nodup_erase {β : Type} [DecidableEq β] (b : β) : ∀ l : List β, nodupL l → nodupL (eraseL b l)
  | [], _ => trivial
  | c :: l, h => by
    show nodupL (if c = b then l else c :: eraseL b l)
    by_cases e : c = b
    · rw [MaskSum.ip e]; exact h.2
    · rw [MaskSum.ineg e]; exact ⟨fun hm => h.1 (mem_of_erase b c l hm), nodup_erase b l h.2⟩

theorem len_erase_le {β : Type} [DecidableEq β] (b : β) : ∀ l : List β, l.length ≤ (eraseL b l).length + 1
  | [] => Nat.zero_le _
  | c :: l => by
    show l.length + 1 ≤ (if c = b then l else c :: eraseL b l).length + 1
    by_cases e : c = b
    · rw [MaskSum.ip e]; exact Nat.le_refl _
    · rw [MaskSum.ineg e]; exact Nat.succ_le_succ (len_erase_le b l)

/-- **Surjection counting.** -/
theorem surj_count {α β : Type} [DecidableEq β] (f : α → β) :
    ∀ (D : List α) (S : List β), nodupL S → (∀ s, memL s S → ∃ d, memL d D ∧ f d = s) → S.length ≤ D.length
  | [], [], _, _ => Nat.le_refl 0
  | [], s :: _, _, h => by obtain ⟨d, hd, _⟩ := h s (Or.inl rfl); exact hd.elim
  | d :: D, S, hS, h => by
    have ih := surj_count f D (eraseL (f d) S) (nodup_erase (f d) S hS) (fun s hs => by
      have hsS := mem_of_erase (f d) s S hs
      obtain ⟨d', hd', e⟩ := h s hsS
      cases hd' with
      | inl e' =>
        -- s = f d, but erased: contradiction with nodup
        exfalso
        rw [e'] at e
        rw [← e] at hs
        -- f d ∈ eraseL (f d) S impossible for duplicate-free S
        have nd := nodup_erase (f d) S hS
        exact (erase_not_mem (f d) S hS) hs
      | inr hd'' => exact ⟨d', hd'', e⟩)
    exact Nat.le_trans (len_erase_le (f d) S) (Nat.succ_le_succ ih)
where
  erase_not_mem (b : β) : ∀ l : List β, nodupL l → ¬ memL b (eraseL b l)
    | [], _, h => h
    | c :: l, hl, h => by
      have h' : memL b (if c = b then l else c :: eraseL b l) := h
      by_cases e : c = b
      · rw [MaskSum.ip e] at h'; rw [← e] at h'; exact hl.1 h'
      · rw [MaskSum.ineg e] at h'
        cases h' with
        | inl h'' => exact e h''.symm
        | inr h'' => exact erase_not_mem b l hl.2 h''

/-- indices of the selected plaquettes, offset by k -/
def selI : List Bool → Nat → List Nat
  | [], _ => []
  | true :: A, k => k :: selI A (k + 1)
  | false :: A, k => selI A (k + 1)

theorem len_selI : ∀ (A : List Bool) (k : Nat), (selI A k).length = cnt A
  | [], _ => rfl
  | true :: A, k => by show (selI A (k + 1)).length + 1 = cnt A + 1; rw [len_selI A (k + 1)]
  | false :: A, k => len_selI A (k + 1)

theorem mem_selI : ∀ (A : List Bool) (k i : Nat), Seeds.nthB A i = true → memL (k + i) (selI A k)
  | [], _, _, h => Bool.noConfusion h
  | true :: _, _, 0, _ => Or.inl rfl
  | true :: A, k, i + 1, h => by
    have := mem_selI A (k + 1) i h
    rw [Nat.succ_add] at this
    exact Or.inr this
  | false :: _, _, 0, h => Bool.noConfusion h
  | false :: A, k, i + 1, h => by
    have := mem_selI A (k + 1) i h
    rw [Nat.succ_add] at this
    exact this

/-- **Area bound.** If for each square s an odd flux φ_s through the loop n is carried only by plaquettes keyed s,
  every surface A with n + ∂A even has at least as many plaquettes as there are squares. -/
theorem area_bound {β : Type} [DecidableEq β] (key : Nat → β) (S : List β) (hS : nodupL S)
    (n : List Nat) (P : List (List Nat)) (A : List Bool) (hev : evenAll (zadd n (bdS P A)) = true)
    (φ : β → List Bool) (hodd : ∀ s, memL s S → parity (wsum (φ s) n) = false)
    (hdet : ∀ s i, memL s S → i < P.length → parity (wsum (φ s) (Seeds.nthL P i)) = false → key i = s) :
    S.length ≤ cnt A := by
  rw [← len_selI A 0]
  refine surj_count key (selI A 0) S hS (fun s hs => ?_)
  obtain ⟨i, hi, ha, hp⟩ := odd_exists (φ s) n P A (hodd s hs) hev
  have hm := mem_selI A 0 i ha
  rw [Nat.zero_add] at hm
  exact ⟨i, hm, hdet s i hs hi hp⟩

end AreaBound

#print axioms AreaBound.wsum_zadd
#print axioms AreaBound.wsum_even
#print axioms AreaBound.odd_exists
#print axioms AreaBound.surj_count
#print axioms AreaBound.area_bound

ym/AreaZd.lean

/-
  ym/AreaZd — T45, cycle 92 (Lad1-inst step 3): the area lower bound for surfaces on Z^d.

  Links LL (duplicate-free, containing every link of every plaquette of Pn), valid plaquettes Pn, plaquette vectors
  vec LL p. For the flux mask LL.map (phi i j):
    cnt_sub          counting a predicate supported on a sublist Q (duplicate-free) of LL is counting it on Q
    wsum_vec_par     parity of the flux through vec LL p = parity of Flux.cnt4 i j p (the four links of p)
    area_Zd          if S is a duplicate-free list of squares (i, j) and the loop vector n has odd flux through every
                     square of S, then every plaquette set A with n + ∂A even has at least |S| plaquettes.
  This is the hypothesis hm of AreaUpper.area_upper on Z^d with m = |S| (for an R × T rectangle, |S| = RT once the
  rectangle's flux is checked, next cycle).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AreaBound

namespace AreaZd

open YMChain GKS Count StrongZ2 Closure Factor Tail InjCount ZdLinks ZdDegree Degree AreaBound Flux

theorem mem_of_erase' {β : Type} [DecidableEq β] (b x : β) : ∀ l : List β, memL x (eraseL b l) → memL x l :=
  AreaBound.mem_of_erase b x

theorem not_mem_erase {β : Type} [DecidableEq β] (b : β) : ∀ l : List β, nodupL l → ¬ memL b (eraseL b l) :=
  AreaBound.surj_count.erase_not_mem b

theorem cnt_erase {β : Type} [DecidableEq β] (F : β → Bool) (b : β) : ∀ l : List β, memL b l →
    cnt (l.map F) = cnt ((eraseL b l).map F) + b2n (F b)
  | [], h => h.elim
  | c :: l, h => by
    show cnt (F c :: l.map F) = cnt ((if c = b then l else c :: eraseL b l).map F) + b2n (F b)
    by_cases e : c = b
    · rw [MaskSum.ip e, e]
      cases F b
      · rfl
      · rfl
    · rw [MaskSum.ineg e]
      have hl : memL b l := by
        cases h with
        | inl h' => exact absurd h'.symm e
        | inr h' => exact h'
      have ih := cnt_erase F b l hl
      show cnt (F c :: l.map F) = cnt (F c :: (eraseL b l).map F) + b2n (F b)
      cases F c
      · exact ih
      · show cnt (l.map F) + 1 = cnt ((eraseL b l).map F) + 1 + b2n (F b)
        rw [ih, Nat.add_assoc, Nat.add_assoc, Nat.add_comm 1 (b2n (F b))]

def memB {β : Type} [DecidableEq β] (b : β) : List β → Bool
  | [] => false
  | c :: l => decide (b = c) || memB b l

theorem memB_t {β : Type} [DecidableEq β] (b : β) : ∀ l : List β, memB b l = true → memL b l
  | [], h => Bool.noConfusion h
  | c :: l, h => by
    cases e : decide (b = c) with
    | true => exact Or.inl (of_decide_eq_true e)
    | false =>
      have h2 : (decide (b = c) || memB b l) = true := h
      rw [e, Bool.false_or] at h2
      exact Or.inr (memB_t b l h2)

theorem memB_f {β : Type} [DecidableEq β] (b : β) : ∀ l : List β, memB b l = false → ¬ memL b l
  | [], _, hm => hm
  | c :: l, h, hm => by
    cases e : decide (b = c) with
    | true =>
      have h2 : (decide (b = c) || memB b l) = false := h
      rw [e, Bool.true_or] at h2
      exact Bool.noConfusion h2
    | false =>
      have h' : (decide (b = c) || memB b l) = false := h
      rw [e, Bool.false_or] at h'
      cases hm with
      | inl x => rw [decide_eq_true x] at e; exact Bool.noConfusion e
      | inr x => exact memB_f b l h' x

/-- **Counting on a support.** -/
theorem cnt_sub {β : Type} [DecidableEq β] (F : β → Bool) : ∀ (LL : List β), nodupL LL → ∀ Q : List β, nodupL Q →
    (∀ l, memL l Q → memL l LL) → (∀ l, memL l LL → F l = true → memL l Q) → cnt (LL.map F) = cnt (Q.map F)
  | [], _, [], _, _, _ => rfl
  | [], _, q :: _, _, hQ, _ => (hQ q (Or.inl rfl)).elim
  | l :: LL, hLL, Q, hQn, hQ, hF => by
    cases hmb : memB l Q with
    | true =>
      have hm := memB_t l Q hmb
      rw [cnt_erase F l Q hm]
      have ih := cnt_sub F LL hLL.2 (eraseL l Q) (AreaBound.nodup_erase l Q hQn)
        (fun x hx => by
          have hxQ := mem_of_erase' l x Q hx
          cases hQ x hxQ with
          | inl e => rw [e] at hx; exact absurd hx (not_mem_erase l Q hQn)
          | inr h => exact h)
        (fun x hx hFx => mem_erase l x (fun e => by rw [e] at hx; exact hLL.1 hx) Q (hF x (Or.inr hx) hFx))
      show cnt (F l :: LL.map F) = _
      rw [← ih]
      cases F l
      · rfl
      · rfl
    | false =>
      have hm := memB_f l Q hmb
      have hFl : F l = false := by
        cases e : F l with
        | false => rfl
        | true => exact absurd (hF l (Or.inl rfl) e) hm
      have ih := cnt_sub F LL hLL.2 Q hQn
        (fun x hx => by
          cases hQ x hx with
          | inl e => rw [e] at hx; exact absurd hx hm
          | inr h => exact h)
        (fun x hx hFx => hF x (Or.inr hx) hFx)
      show cnt (F l :: LL.map F) = _
      rw [hFl]; exact ih

theorem wsum_map_vec (φ : Lk → Bool) (p : Pq) : ∀ LL : List Lk,
    wsum (LL.map φ) (vec LL p) = cnt (LL.map (fun l => φ l && linkIn l p))
  | [] => rfl
  | l :: LL => by
    show (if φ l = true then (if linkIn l p = true then 1 else 0) else 0) + wsum (LL.map φ) (vec LL p)
      = cnt ((φ l && linkIn l p) :: LL.map (fun l => φ l && linkIn l p))
    rw [wsum_map_vec φ p LL]
    cases φ l <;> cases linkIn l p
    · exact Nat.zero_add _
    · exact Nat.zero_add _
    · exact Nat.zero_add _
    · exact Nat.add_comm _ _

theorem cnt4_list (a b c d : Bool) : cnt [a, b, c, d] = b2n a + b2n b + b2n c + b2n d := by
  cases a <;> cases b <;> cases c <;> cases d <;> rfl

def four (p : Pq) : List Lk := [(p.1, p.2.1), (shf p.1 p.2.1, p.2.2), (shf p.1 p.2.2, p.2.1), (p.1, p.2.2)]

theorem ne_snd {x y : List Nat} {a b : Nat} (h : a ≠ b) : (x, a) ≠ (y, b) := fun e => h (congrArg Prod.snd e)

theorem ne_fst {x y : List Nat} {a : Nat} (h : x ≠ y) : (x, a) ≠ (y, a) := fun e => h (congrArg Prod.fst e)

theorem nodup_four (d : Nat) (p : Pq) (hp : valid d p) : nodupL (four p) := by
  obtain ⟨y, a, b⟩ := p
  have hab : a ≠ b := Nat.ne_of_lt hp.2.1
  have hb : b < y.length := by rw [hp.1]; exact hp.2.2
  have ha : a < y.length := Nat.lt_trans hp.2.1 hb
  have n13 : (y, a) ≠ (shf y b, a) := ne_fst (fun e => shf_ne y b hb e.symm)
  have n24 : (shf y a, b) ≠ (y, b) := ne_fst (shf_ne y a ha)
  refine ⟨?_, ?_, ?_, ?_, trivial⟩
  · intro h
    rcases h with h | h | h | h
    · exact ne_snd hab h
    · exact n13 h
    · exact ne_snd hab h
    · exact h.elim
  · intro h
    rcases h with h | h | h
    · exact ne_snd (fun e => hab e.symm) h
    · exact n24 h
    · exact h.elim
  · intro h
    rcases h with h | h
    · exact ne_snd hab h
    · exact h.elim
  · exact fun h => h.elim

theorem linkIn_four (p : Pq) (l : Lk) (h : linkIn l p = true) : memL l (four p) := by
  have c := of_decide_eq_true h
  rcases c with e | e | e | e
  · exact Or.inl e
  · exact Or.inr (Or.inl e)
  · exact Or.inr (Or.inr (Or.inl e))
  · exact Or.inr (Or.inr (Or.inr (Or.inl e)))

/-- **Flux parity through a plaquette vector.** -/
theorem wsum_vec_par (d : Nat) (LL : List Lk) (hLL : nodupL LL) (p : Pq) (hp : valid d p)
    (hcov : ∀ l, linkIn l p = true → memL l LL) (i j : Nat) :
    parity (wsum (LL.map (phi i j)) (vec LL p)) = parity (cnt4 i j p) := by
  rw [wsum_map_vec]
  have hin : ∀ l, memL l (four p) → linkIn l p = true := fun l hl => by
    rcases hl with e | e | e | e | e
    · rw [e]; exact decide_eq_true (Or.inl rfl)
    · rw [e]; exact decide_eq_true (Or.inr (Or.inl rfl))
    · rw [e]; exact decide_eq_true (Or.inr (Or.inr (Or.inl rfl)))
    · rw [e]; exact decide_eq_true (Or.inr (Or.inr (Or.inr rfl)))
    · exact e.elim
  rw [cnt_sub (fun l => phi i j l && linkIn l p) LL hLL (four p) (nodup_four d p hp)
    (fun l hl => hcov l (hin l hl))
    (fun l _ hF => linkIn_four p l (by
      cases e : linkIn l p with
      | true => rfl
      | false => rw [e, Bool.and_false] at hF; exact hF))]
  obtain ⟨y, a, b⟩ := p
  show parity (cnt [phi i j (y, a) && linkIn (y, a) (y, a, b), phi i j (shf y a, b) && linkIn (shf y a, b) (y, a, b),
    phi i j (shf y b, a) && linkIn (shf y b, a) (y, a, b), phi i j (y, b) && linkIn (y, b) (y, a, b)]) = _
  rw [hin _ (Or.inl rfl), hin _ (Or.inr (Or.inl rfl)), hin _ (Or.inr (Or.inr (Or.inl rfl))),
    hin _ (Or.inr (Or.inr (Or.inr (Or.inl rfl)))), Bool.and_true, Bool.and_true, Bool.and_true, Bool.and_true,
    cnt4_list]
  rfl

def nthP : List Pq → Nat → Pq
  | [], _ => ([], 0, 0)
  | p :: _, 0 => p
  | _ :: P, k + 1 => nthP P k

theorem nthP_map (LL : List Lk) : ∀ (Pn : List Pq) (i : Nat), i < (Pn.map (vec LL)).length →
    Seeds.nthL (Pn.map (vec LL)) i = vec LL (nthP Pn i) ∧ memL (nthP Pn i) Pn
  | [], _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, 0, _ => ⟨rfl, Or.inl rfl⟩
  | _ :: Pn, i + 1, h => by
    have ih := nthP_map LL Pn i (Nat.lt_of_succ_lt_succ h)
    exact ⟨ih.1, Or.inr ih.2⟩

def sqKey (Pn : List Pq) (i : Nat) : Nat × Nat := (nthN (nthP Pn i).1 0, nthN (nthP Pn i).1 1)

/-- **Area bound on Z^d.** -/
theorem area_Zd (d : Nat) (LL : List Lk) (hLL : nodupL LL) (Pn : List Pq)
    (hv : ∀ p, memL p Pn → valid d p) (hcov : ∀ p, memL p Pn → ∀ l, linkIn l p = true → memL l LL)
    (n : List Nat) (S : List (Nat × Nat)) (hS : nodupL S)
    (hodd : ∀ s, memL s S → parity (wsum (LL.map (phi s.1 s.2)) n) = false)
    (A : List Bool) (hev : evenAll (zadd n (bdS (Pn.map (vec LL)) A)) = true) :
    S.length ≤ cnt A := by
  refine area_bound (sqKey Pn) S hS n (Pn.map (vec LL)) A hev (fun s => LL.map (phi s.1 s.2)) hodd ?_
  intro s i _ hi hp
  obtain ⟨e, hm⟩ := nthP_map LL Pn i hi
  rw [e] at hp
  have hvp := hv _ hm
  rw [wsum_vec_par d LL hLL _ hvp (hcov _ hm) s.1 s.2] at hp
  show (nthN (nthP Pn i).1 0, nthN (nthP Pn i).1 1) = s
  revert hp hvp
  generalize nthP Pn i = p
  intro hp hvp
  obtain ⟨y, a, b⟩ := p
  have hb : b < y.length := by rw [hvp.1]; exact hvp.2.2
  rw [phi_plaq s.1 s.2 y a b hvp.2.1 hb] at hp
  have c : a = 0 ∧ b = 1 ∧ nthN y 0 = s.1 ∧ nthN y 1 = s.2 := by
    cases e2 : decide (a = 0 ∧ b = 1 ∧ nthN y 0 = s.1 ∧ nthN y 1 = s.2) with
    | true => exact of_decide_eq_true e2
    | false => rw [e2] at hp; exact Bool.noConfusion hp
  show (nthN y 0, nthN y 1) = s
  rw [c.2.2.1, c.2.2.2]

end AreaZd

#print axioms AreaZd.cnt_sub
#print axioms AreaZd.wsum_vec_par
#print axioms AreaZd.area_Zd

ym/RectLoop.lean

/-
  ym/RectLoop — T45, cycle 93 (Lad1-inst step 4): the R × T rectangle loop on Z^d and |A| ≥ RT.

  The rectangle with lower-left corner x0 :: y0 :: z in the (0,1)-plane has boundary links
    horizontal (dir 0) at rows y0, y0 + T, columns x0 ≤ a < x0 + R;  vertical (dir 1) at columns x0, x0 + R, rows y0 ≤ b < y0 + T.
    flux_rect        for x0 ≤ i < x0 + R, y0 ≤ j < y0 + T the loop vector has odd φ_{ij}-flux (only the left link of row j counts)
    sqs              the RT squares, duplicate-free, of length R * T
    rect_area        every plaquette set A with n_rect + ∂A even has |A| ≥ R T  (hypothesis hm of AreaUpper.area_upper with m = RT)

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AreaZd

namespace RectLoop

open YMChain GKS Count StrongZ2 Closure Factor Tail InjCount ZdLinks ZdDegree Degree AreaBound Flux AreaZd

def tl2 : List Nat → List Nat
  | [] => []
  | [_] => []
  | _ :: _ :: w => w

def bdR (x0 y0 R T : Nat) (z : List Nat) (l : Lk) : Bool :=
  decide ((l.2 = 0 ∧ tl2 l.1 = z ∧ (nthN l.1 1 = y0 ∨ nthN l.1 1 = y0 + T) ∧ x0 ≤ nthN l.1 0 ∧ nthN l.1 0 < x0 + R) ∨
    (l.2 = 1 ∧ tl2 l.1 = z ∧ (nthN l.1 0 = x0 ∨ nthN l.1 0 = x0 + R) ∧ y0 ≤ nthN l.1 1 ∧ nthN l.1 1 < y0 + T))

def nRect (LL : List Lk) (x0 y0 R T : Nat) (z : List Nat) : List Nat :=
  LL.map (fun l => if bdR x0 y0 R T z l = true then 1 else 0)

theorem wsum_ind (φ g : Lk → Bool) : ∀ LL : List Lk,
    wsum (LL.map φ) (LL.map (fun l => if g l = true then 1 else 0)) = cnt (LL.map (fun l => φ l && g l))
  | [] => rfl
  | l :: LL => by
    show (if φ l = true then (if g l = true then 1 else 0) else 0) + wsum (LL.map φ) (LL.map (fun l => if g l = true then 1 else 0))
      = cnt ((φ l && g l) :: LL.map (fun l => φ l && g l))
    rw [wsum_ind φ g LL]
    cases φ l <;> cases g l
    · exact Nat.zero_add _
    · exact Nat.zero_add _
    · exact Nat.zero_add _
    · exact Nat.add_comm _ _

/-- **The rectangle has odd flux through each of its squares.** -/
theorem flux_rect (LL : List Lk) (hLL : nodupL LL) (hLd : ∀ l, memL l LL → 2 ≤ l.1.length)
    (x0 y0 R T : Nat) (z : List Nat) (i j : Nat) (hi : x0 ≤ i ∧ i < x0 + R) (hj : y0 ≤ j ∧ j < y0 + T)
    (hq : memL (x0 :: j :: z, 1) LL) :
    parity (wsum (LL.map (phi i j)) (nRect LL x0 y0 R T z)) = false := by
  unfold nRect
  rw [wsum_ind]
  have hFq : (phi i j (x0 :: j :: z, 1) && bdR x0 y0 R T z (x0 :: j :: z, 1)) = true := by
    have h1 : phi i j (x0 :: j :: z, 1) = true := phi_t i j (x0 :: j :: z) rfl hi.1
    rw [h1]
    exact decide_eq_true (Or.inr ⟨rfl, rfl, Or.inl rfl, hj.1, hj.2⟩)
  rw [cnt_sub (fun l => phi i j l && bdR x0 y0 R T z l) LL hLL [(x0 :: j :: z, 1)] ⟨fun h => h, trivial⟩
    (fun l hl => by
      cases hl with
      | inl e => rw [e]; exact hq
      | inr h => exact h.elim)
    (fun l hl hF => by
      have hp : phi i j l = true := by
        cases e : phi i j l with
        | true => rfl
        | false => rw [e, Bool.false_and] at hF; exact hF
      have hb : bdR x0 y0 R T z l = true := by
        cases e : bdR x0 y0 R T z l with
        | true => rfl
        | false => rw [e, Bool.and_false] at hF; exact hF
      have c := of_decide_eq_true hp
      have cb := of_decide_eq_true hb
      have hlen := hLd l hl
      obtain ⟨pt, μ⟩ := l
      cases pt with
      | nil => exact absurd hlen (Nat.not_succ_le_zero 1)
      | cons a pt =>
        cases pt with
        | nil => exact absurd hlen (Nat.not_succ_le_self 1)
        | cons b w =>
          have c1 : μ = 1 := c.1
          have c2 : b = j := c.2.1
          have c3 : a ≤ i := c.2.2
          cases cb with
          | inl h => rw [c1] at h; exact Nat.noConfusion h.1
          | inr h =>
            have hw : w = z := h.2.1
            have ha : a = x0 := by
              cases h.2.2.1 with
              | inl e => exact e
              | inr e =>
                have : x0 + R ≤ i := by rw [← e]; exact c3
                exact absurd (Nat.lt_of_lt_of_le hi.2 this) (Nat.lt_irrefl _)
            rw [c1, c2, hw, ha]
            exact Or.inl rfl)]
  show parity (cnt [phi i j (x0 :: j :: z, 1) && bdR x0 y0 R T z (x0 :: j :: z, 1)]) = false
  rw [hFq]
  rfl

def rowL (a y0 : Nat) : Nat → List (Nat × Nat)
  | 0 => []
  | T + 1 => (a, y0 + T) :: rowL a y0 T

def sqs (x0 y0 T : Nat) : Nat → List (Nat × Nat)
  | 0 => []
  | R + 1 => rowL (x0 + R) y0 T ++ sqs x0 y0 T R

theorem len_app {α : Type} : ∀ l₁ l₂ : List α, (l₁ ++ l₂).length = l₁.length + l₂.length
  | [], l₂ => (Nat.zero_add _).symm
  | _ :: l₁, l₂ => by
    show (l₁ ++ l₂).length + 1 = l₁.length + 1 + l₂.length
    rw [len_app l₁ l₂, Nat.succ_add]

theorem mem_app {α : Type} (x : α) : ∀ l₁ l₂ : List α, memL x (l₁ ++ l₂) → memL x l₁ ∨ memL x l₂
  | [], _, h => Or.inr h
  | _ :: l₁, l₂, h => by
    cases h with
    | inl e => exact Or.inl (Or.inl e)
    | inr h' =>
      cases mem_app x l₁ l₂ h' with
      | inl h1 => exact Or.inl (Or.inr h1)
      | inr h2 => exact Or.inr h2

theorem nodup_app {α : Type} [DecidableEq α] : ∀ l₁ l₂ : List α, nodupL l₁ → nodupL l₂ →
    (∀ x, memL x l₁ → ¬ memL x l₂) → nodupL (l₁ ++ l₂)
  | [], _, _, h2, _ => h2
  | a :: l₁, l₂, h1, h2, hd => ⟨fun hm => by
      cases mem_app a l₁ l₂ hm with
      | inl h => exact h1.1 h
      | inr h => exact hd a (Or.inl rfl) h,
    nodup_app l₁ l₂ h1.2 h2 (fun x hx => hd x (Or.inr hx))⟩

theorem len_rowL (a y0 : Nat) : ∀ T, (rowL a y0 T).length = T
  | 0 => rfl
  | T + 1 => congrArg Nat.succ (len_rowL a y0 T)

theorem len_sqs (x0 y0 T : Nat) : ∀ R, (sqs x0 y0 T R).length = R * T
  | 0 => (Nat.zero_mul T).symm
  | R + 1 => by
    show (rowL (x0 + R) y0 T ++ sqs x0 y0 T R).length = (R + 1) * T
    rw [len_app, len_rowL, len_sqs x0 y0 T R, Nat.succ_mul, Nat.add_comm]

theorem mem_rowL (a y0 : Nat) : ∀ T (s : Nat × Nat), memL s (rowL a y0 T) → s.1 = a ∧ y0 ≤ s.2 ∧ s.2 < y0 + T
  | 0, _, h => h.elim
  | T + 1, s, h => by
    cases h with
    | inl e => rw [e]; exact ⟨rfl, Nat.le_add_right _ _, Nat.lt_succ_self _⟩
    | inr h' =>
      have ih := mem_rowL a y0 T s h'
      exact ⟨ih.1, ih.2.1, Nat.lt_trans ih.2.2 (Nat.lt_succ_self _)⟩

theorem nodup_rowL (a y0 : Nat) : ∀ T, nodupL (rowL a y0 T)
  | 0 => trivial
  | T + 1 => ⟨fun h => Nat.lt_irrefl _ (mem_rowL a y0 T _ h).2.2, nodup_rowL a y0 T⟩

theorem mem_sqs (x0 y0 T : Nat) : ∀ R (s : Nat × Nat), memL s (sqs x0 y0 T R) →
    (x0 ≤ s.1 ∧ s.1 < x0 + R) ∧ (y0 ≤ s.2 ∧ s.2 < y0 + T)
  | 0, _, h => h.elim
  | R + 1, s, h => by
    cases mem_app s _ _ h with
    | inl h1 =>
      have r := mem_rowL (x0 + R) y0 T s h1
      rw [r.1]
      exact ⟨⟨Nat.le_add_right _ _, Nat.lt_succ_self _⟩, r.2⟩
    | inr h2 =>
      have ih := mem_sqs x0 y0 T R s h2
      exact ⟨⟨ih.1.1, Nat.lt_trans ih.1.2 (Nat.lt_succ_self _)⟩, ih.2⟩

theorem nodup_sqs (x0 y0 T : Nat) : ∀ R, nodupL (sqs x0 y0 T R)
  | 0 => trivial
  | R + 1 => nodup_app _ _ (nodup_rowL _ _ _) (nodup_sqs x0 y0 T R) (fun s h1 h2 => by
      have r := mem_rowL (x0 + R) y0 T s h1
      have m := mem_sqs x0 y0 T R s h2
      rw [r.1] at m
      exact Nat.lt_irrefl _ m.1.2)

/-- **Area lower bound for the rectangle: |A| ≥ R T.** -/
theorem rect_area (d : Nat) (LL : List Lk) (hLL : nodupL LL) (hLd : ∀ l, memL l LL → 2 ≤ l.1.length)
    (Pn : List Pq) (hv : ∀ p, memL p Pn → valid d p) (hcov : ∀ p, memL p Pn → ∀ l, linkIn l p = true → memL l LL)
    (x0 y0 R T : Nat) (z : List Nat) (hq : ∀ j, y0 ≤ j → j < y0 + T → memL (x0 :: j :: z, 1) LL)
    (A : List Bool) (hev : evenAll (zadd (nRect LL x0 y0 R T z) (bdS (Pn.map (vec LL)) A)) = true) :
    R * T ≤ cnt A := by
  rw [← len_sqs x0 y0 T R]
  exact area_Zd d LL hLL Pn hv hcov _ (sqs x0 y0 T R) (nodup_sqs x0 y0 T R)
    (fun s hs => by
      have m := mem_sqs x0 y0 T R s hs
      exact flux_rect LL hLL hLd x0 y0 R T z s.1 s.2 m.1 m.2 (hq s.2 m.2.1 m.2.2)) A hev

end RectLoop

#print axioms RectLoop.flux_rect
#print axioms RectLoop.len_sqs
#print axioms RectLoop.rect_area

90.3 L2: every finite gauge group

Statement. For weights \(c_0+f_p\) with \(m\le c_0+f_p\le 1\), \(|f_p|\le \varepsilon \), \(\kappa m=1\), \(\varepsilon \kappa ^{D_g+1}\le ux\), and any compact group with a Haar integral (finite groups included):

\[ |\langle W\rangle |\le K_\chi \,(\kappa (1+y))^{s}\,u^{m_A},\qquad |\langle U_pU_q\rangle -\langle U_p\rangle \langle U_q\rangle |\le \tfrac 12(2K_1)^2(\kappa ^2(1+y))^{\hat s}u^{R}, \]
where \(m_A\) is the least size of a polymer of nonzero weight. If the group has a central \(z\) acting by \(-1\) on the loop representation (e.g. \(2I\subset SU(2)\)), then on \(\mathbb Z^d\), \(m_A=RT\) for an \(R\times T\) rectangle.

Key steps.

Wilson-action constants for \(2I\) (and \(SU(2)\)): unit quaternions have \(t=\mathrm {Re\,tr}\,U/2\in [-1,1]\) (QuatUnit.unit_t), so the normalised weight is \(1+f\) with \(-4\beta \le f\le 0\), \(m=1-4\beta \), \(\varepsilon =4\beta \) (WeightBound); only completeness of \(\mathbb R\) is cited.

Lean files.

ym/GroupAverage.lean

/-
  ym/GroupAverage — T45, cycle 27: the link integral for a finite (non-abelian) gauge group.

  G finite, listed once each (FiniteRecChain.Complete), ρ : G → matrices over a commutative ring R (entries ρ g i j,
  indices from a list idx), multiplicative: ρ(gh)_ij = Σ_k ρ(g)_ik ρ(h)_kj. The link integral of ρ is the average
  M_ij = Σ_g ρ(g)_ij (unnormalised Haar measure).
    average_invariant   ρ(h) M = M for every h           (reindex g ↦ h g)
    average_idem        M M = |G| M                      (M/|G| is a projection)
    average_zero        if ρ has no nonzero invariant vector, M = 0
  For a gauge group such as the binary icosahedral group 2I ⊂ SU(2) (the Kamiak runs): ∫ dU ρ(U) = 0 for every
  nontrivial irreducible ρ — the rule that kills every link carrying a single nontrivial representation in the
  strong-coupling (character) expansion. CharOrth is the one-dimensional case.

  Lean 4 core only, proofs by hand, no axioms.
-/
import FiniteRecChain

namespace GroupAverage

open YMChain FiniteRecChain

variable {R : Type} (Rg : DetChain.CRing R) {G : Type} [DecidableEq G] (K : Grp G) {ι : Type}

def M (els : List G) (ρ : G → ι → ι → R) (i j : ι) : R := sumK Rg (fun g => ρ g i j) els

/-- **Invariance.** -/
theorem average_invariant (els : List G) (hc : Complete Rg els) (idx : List ι) (ρ : G → ι → ι → R)
    (hom : ∀ g h i j, ρ (K.mul g h) i j = sumK Rg (fun k => Rg.mul (ρ g i k) (ρ h k j)) idx)
    (h : G) (i j : ι) :
    sumK Rg (fun k => Rg.mul (ρ h i k) (M Rg els ρ k j)) idx = M Rg els ρ i j := by
  have e1 : sumK Rg (fun k => Rg.mul (ρ h i k) (M Rg els ρ k j)) idx
      = sumK Rg (fun k => sumK Rg (fun g => Rg.mul (ρ h i k) (ρ g k j)) els) idx :=
    sumK_ext Rg _ _ (fun k => mul_sumK Rg _ _ els) idx
  rw [e1, ← sumK_swap]
  have e2 : sumK Rg (fun g => sumK Rg (fun k => Rg.mul (ρ h i k) (ρ g k j)) idx) els
      = sumK Rg (fun g => ρ (K.mul h g) i j) els :=
    sumK_ext Rg _ _ (fun g => (hom h g i j).symm) els
  rw [e2]
  exact reindex Rg els hc (K.mul h) (K.mul (K.inv h))
    (fun x => by rw [← K.assoc, K.inv_mul, K.one_mul]) (fun y => by rw [← K.assoc, K.mul_inv, K.one_mul])
    (fun g => ρ g i j)

/-- **Idempotence**: `M M = |G| M`. -/
theorem average_idem (els : List G) (hc : Complete Rg els) (idx : List ι) (ρ : G → ι → ι → R)
    (hom : ∀ g h i j, ρ (K.mul g h) i j = sumK Rg (fun k => Rg.mul (ρ g i k) (ρ h k j)) idx) (i j : ι) :
    sumK Rg (fun k => Rg.mul (M Rg els ρ i k) (M Rg els ρ k j)) idx = sumK Rg (fun _ => M Rg els ρ i j) els := by
  have e1 : sumK Rg (fun k => Rg.mul (M Rg els ρ i k) (M Rg els ρ k j)) idx
      = sumK Rg (fun k => sumK Rg (fun g => Rg.mul (ρ g i k) (M Rg els ρ k j)) els) idx :=
    sumK_ext Rg _ _ (fun k => by
      show Rg.mul (sumK Rg (fun g => ρ g i k) els) (M Rg els ρ k j) = _
      rw [Rg.mul_comm, mul_sumK]
      exact sumK_ext Rg _ _ (fun g => Rg.mul_comm _ _) els) idx
  rw [e1, ← sumK_swap]
  exact sumK_ext Rg _ _ (fun g => average_invariant Rg K els hc idx ρ hom g i j) els

/-- **No invariant vectors ⇒ the link integral vanishes.** -/
theorem average_zero (els : List G) (hc : Complete Rg els) (idx : List ι) (ρ : G → ι → ι → R)
    (hom : ∀ g h i j, ρ (K.mul g h) i j = sumK Rg (fun k => Rg.mul (ρ g i k) (ρ h k j)) idx)
    (noinv : ∀ c : ι → R, (∀ h i, sumK Rg (fun k => Rg.mul (ρ h i k) (c k)) idx = c i) → ∀ i, c i = Rg.zero)
    (i j : ι) : M Rg els ρ i j = Rg.zero :=
  noinv (fun k => M Rg els ρ k j) (fun h i' => average_invariant Rg K els hc idx ρ hom h i' j) i

end GroupAverage

#print axioms GroupAverage.average_invariant
#print axioms GroupAverage.average_idem
#print axioms GroupAverage.average_zero

ym/LinkFactor.lean

/-
  ym/LinkFactor — T45, cycle 42 (Lad2, finite gauge group G, any dimension): link integrals factorize, and a link
  carrying a single nontrivial representation kills the integral.

  G finite, listed by els (complete list, each element once: FiniteRecChain.Complete). A gauge field on L links is
  a list c ∈ cfgs els L (all L-tuples of elements). Haar measure = counting measure.
    sumK_join          Σ over a concatenation of lists
    cfg_factor         Σ_c Π_l f_l(c_l) = Π_l Σ_g f_l(g)                       (Fubini for the product of link measures)
    cfg_vanish         if one f_l has Σ_g f_l(g) = 0, the whole integral is 0
    rep_vanish         if ρ has no invariant vector (e.g. a nontrivial irreducible representation), then every
                       monomial with one matrix entry ρ(c_l)_{ij} on link l and arbitrary functions on the other
                       links integrates to 0                                   (GroupAverage.average_zero)
  This is S2b for every finite gauge group (ℤ_N was ym/LinkIntegralZN), including 2I ⊂ SU(2): in the character
  expansion, a polymer with a link covered once by a nontrivial irrep has zero weight.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GroupAverage

namespace LinkFactor

open YMChain FiniteRecChain

variable {R : Type} (K : DetChain.CRing R) {G : Type}

def joinL {A : Type} : List (List A) → List A
  | [] => []
  | l :: ls => l ++ joinL ls

def cfgs (els : List G) : Nat → List (List G)
  | 0 => [[]]
  | L + 1 => joinL (els.map (fun g => (cfgs els L).map (List.cons g)))

def prodG : List (G → R) → List G → R
  | [], _ => K.one
  | _ :: _, [] => K.one
  | f :: fs, g :: c => K.mul (f g) (prodG fs c)

def prodSums (els : List G) : List (G → R) → R
  | [] => K.one
  | f :: fs => K.mul (sumK K f els) (prodSums els fs)

theorem sumK_app {A : Type} (f : A → R) : ∀ l m : List A, sumK K f (l ++ m) = K.add (sumK K f l) (sumK K f m)
  | [], m => (K.zero_add _).symm
  | a :: l, m => by
    show K.add (f a) (sumK K f (l ++ m)) = K.add (K.add (f a) (sumK K f l)) (sumK K f m)
    rw [sumK_app f l m, K.add_assoc]

theorem sumK_join {A : Type} (f : A → R) : ∀ ls : List (List A), sumK K f (joinL ls) = sumK K (fun l => sumK K f l) ls
  | [] => rfl
  | l :: ls => by
    show sumK K f (l ++ joinL ls) = K.add (sumK K f l) (sumK K (fun l => sumK K f l) ls)
    rw [sumK_app, sumK_join f ls]

theorem sumK_map {A B : Type} (f : B → R) (g : A → B) : ∀ l : List A, sumK K f (l.map g) = sumK K (fun a => f (g a)) l
  | [] => rfl
  | a :: l => by
    show K.add (f (g a)) (sumK K f (l.map g)) = K.add (f (g a)) (sumK K (fun a => f (g a)) l)
    rw [sumK_map f g l]

/-- **Fubini for link integrals.** -/
theorem cfg_factor (els : List G) : ∀ fs : List (G → R),
    sumK K (prodG K fs) (cfgs els fs.length) = prodSums K els fs
  | [] => by
    show K.add K.one K.zero = K.one
    exact DetChain.add_zero' K _
  | f :: fs => by
    show sumK K (prodG K (f :: fs)) (joinL (els.map (fun g => (cfgs els fs.length).map (List.cons g))))
      = K.mul (sumK K f els) (prodSums K els fs)
    rw [sumK_join, sumK_map, sumK_mul]
    refine sumK_ext K _ _ (fun g => ?_) els
    show sumK K (prodG K (f :: fs)) ((cfgs els fs.length).map (List.cons g)) = _
    rw [sumK_map]
    show sumK K (fun c => K.mul (f g) (prodG K fs c)) (cfgs els fs.length) = _
    rw [← mul_sumK, cfg_factor els fs]

theorem prodSums_zero (els : List G) : ∀ (pre post : List (G → R)) (f : G → R), sumK K f els = K.zero →
    prodSums K els (pre ++ f :: post) = K.zero
  | [], post, f, h => by
    show K.mul (sumK K f els) (prodSums K els post) = K.zero
    rw [h, DetChain.zero_mul']
  | p :: pre, post, f, h => by
    show K.mul (sumK K p els) (prodSums K els (pre ++ f :: post)) = K.zero
    rw [prodSums_zero els pre post f h, DetChain.mul_zero]

/-- **One vanishing link kills the integral.** -/
theorem cfg_vanish (els : List G) (pre post : List (G → R)) (f : G → R) (h : sumK K f els = K.zero) :
    sumK K (prodG K (pre ++ f :: post)) (cfgs els (pre ++ f :: post).length) = K.zero := by
  rw [cfg_factor, prodSums_zero K els pre post f h]

/-- **A link carrying a representation without invariant vectors integrates to zero.** -/
theorem rep_vanish [DecidableEq G] (Kg : Grp G) (els : List G) (hc : Complete K els) {ι : Type} (idx : List ι)
    (ρ : G → ι → ι → R)
    (hom : ∀ g h i j, ρ (Kg.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (noinv : ∀ c : ι → R, (∀ h i, sumK K (fun k => K.mul (ρ h i k) (c k)) idx = c i) → ∀ i, c i = K.zero)
    (i j : ι) (pre post : List (G → R)) :
    sumK K (prodG K (pre ++ (fun g => ρ g i j) :: post)) (cfgs els (pre ++ (fun g => ρ g i j) :: post).length) = K.zero :=
  cfg_vanish K els pre post _ (GroupAverage.average_zero K Kg els hc idx ρ hom noinv i j)

end LinkFactor

#print axioms LinkFactor.sumK_join
#print axioms LinkFactor.cfg_factor
#print axioms LinkFactor.prodSums_zero
#print axioms LinkFactor.cfg_vanish
#print axioms LinkFactor.rep_vanish

ym/Indep.lean

/-
  ym/Indep — T45, cycle 43 (Lad2, any finite gauge group, any dimension): integrals of functions of disjoint link
  sets factorize.

  Gauge fields c ∈ cfgs els L (LinkFactor). A function F on fields depends only on the links in a mask S when
  F c = F c' whenever c and c' agree on S.
    getO, agree, depOn          access, agreement on a mask, dependence on a mask
    dep_cons_true/false         a function of g :: c, seen as a function of c
    indep                       F depends on S, H on T, S and T disjoint:
                                  |G|^L · Σ_c F(c) H(c) = (Σ_c F(c)) · (Σ_c H(c))
  (|G| = number of elements, as a ring element; normalised Haar measure: ⟨F H⟩ = ⟨F⟩⟨H⟩.)
  This replaces the ℤ₂ parity split (Factor.even_split) for a general group: link-disjoint parts of a polymer
  integrate independently.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.LinkFactor
import ym.Factor
import ym.Seeds

namespace Indep

open YMChain LinkFactor Closure Factor

variable {R : Type} (K : DetChain.CRing R) {G : Type}

def getO : List G → Nat → Option G
  | [], _ => none
  | g :: _, 0 => some g
  | _ :: c, k + 1 => getO c k

def agree (S : List Bool) (c c' : List G) : Prop := ∀ l, Seeds.nthB S l = true → getO c l = getO c' l

def depOn (S : List Bool) (F : List G → R) : Prop := ∀ c c', agree S c c' → F c = F c'

def natR : Nat → R
  | 0 => K.zero
  | k + 1 => K.add (natR k) K.one

theorem sum_const {A : Type} (x : R) : ∀ l : List A, sumK K (fun _ => x) l = K.mul (natR K l.length) x
  | [] => (DetChain.zero_mul' K x).symm
  | _ :: l => by
    show K.add x (sumK K (fun _ => x) l) = K.mul (K.add (natR K l.length) K.one) x
    rw [sum_const x l, DetChain.right_distrib, K.one_mul, K.add_comm]

def pwN (x : R) : Nat → R
  | 0 => K.one
  | k + 1 => K.mul x (pwN x k)

theorem dep_tail (s : Bool) (S : List Bool) (F : List G → R) (h : depOn (s :: S) F) (g : G) :
    depOn S (fun c => F (g :: c)) := fun c c' hcc => h (g :: c) (g :: c') (fun l hl => by
  cases l with
  | zero => rfl
  | succ l => exact hcc l hl)

theorem dep_free (S : List Bool) (F : List G → R) (h : depOn (false :: S) F) (g g' : G) (c : List G) :
    F (g :: c) = F (g' :: c) := h _ _ (fun l hl => by
  cases l with
  | zero => exact absurd hl Bool.noConfusion
  | succ l => rfl)

/-- **Independence of functions of disjoint link sets.** -/
theorem indep (els : List G) : ∀ (S T : List Bool) (F H : List G → R), S.length = T.length →
    disjM S T → depOn S F → depOn T H →
    K.mul (pwN K (natR K els.length) S.length) (sumK K (fun c => K.mul (F c) (H c)) (cfgs els S.length))
      = K.mul (sumK K F (cfgs els S.length)) (sumK K H (cfgs els S.length))
  | [], [], F, H, _, _, _, _ => by
    show K.mul K.one (K.add (K.mul (F []) (H [])) K.zero) = K.mul (K.add (F []) K.zero) (K.add (H []) K.zero)
    rw [K.one_mul, DetChain.add_zero', DetChain.add_zero', DetChain.add_zero']
  | [], _ :: _, _, _, h, _, _, _ => absurd h Nat.noConfusion
  | _ :: _, [], _, _, h, _, _, _ => absurd h Nat.noConfusion
  | s :: S, u :: T, F, H, hl, hd, hF, hH => by
    have hl' := Nat.succ.inj hl
    let n := natR K els.length
    -- sums over g :: c
    have split : ∀ Φ : List G → R, sumK K Φ (cfgs els (S.length + 1))
        = sumK K (fun g => sumK K (fun c => Φ (g :: c)) (cfgs els S.length)) els := fun Φ => by
      show sumK K Φ (joinL (els.map (fun g => (cfgs els S.length).map (List.cons g)))) = _
      rw [sumK_join, LinkFactor.sumK_map]
      exact sumK_ext K _ _ (fun g => LinkFactor.sumK_map K Φ _ _) els
    show K.mul (K.mul n (pwN K n S.length)) (sumK K (fun c => K.mul (F c) (H c)) (cfgs els (S.length + 1)))
      = K.mul (sumK K F (cfgs els (S.length + 1))) (sumK K H (cfgs els (S.length + 1)))
    rw [split, split, split]
    cases s with
    | true =>
      -- link 0 is not in T: H does not see it
      have hu : u = false := by
        cases u
        · rfl
        · exact absurd ⟨rfl, rfl⟩ hd.1
      rw [hu] at hH
      cases els with
      | nil =>
        show K.mul (K.mul n _) K.zero = K.mul K.zero K.zero
        rw [DetChain.mul_zero, DetChain.mul_zero]
      | cons g0 rest =>
        let H0 : List G → R := fun c => H (g0 :: c)
        have hHg : ∀ g c, H (g :: c) = H0 c := fun g c => dep_free T H hH g g0 c
        have hH0 : depOn T H0 := dep_tail false T H hH g0
        have e1 : ∀ g, sumK K (fun c => K.mul (F (g :: c)) (H (g :: c))) (cfgs (g0 :: rest) S.length)
            = sumK K (fun c => K.mul (F (g :: c)) (H0 c)) (cfgs (g0 :: rest) S.length) :=
          fun g => sumK_ext K _ _ (fun c => by rw [hHg]) _
        have e2 : sumK K (fun g => sumK K (fun c => H (g :: c)) (cfgs (g0 :: rest) S.length)) (g0 :: rest)
            = K.mul n (sumK K H0 (cfgs (g0 :: rest) S.length)) := by
          rw [sumK_ext K _ _ (fun g => sumK_ext K _ _ (fun c => hHg g c) _), sum_const]
        rw [sumK_ext K _ _ e1, e2, mul_sumK]
        -- Σ_g p n^{|S|} Σ_c F_g H0 = Σ_g (Σ F_g)(Σ H0)
        have e3 : ∀ g, K.mul (K.mul n (pwN K n S.length)) (sumK K (fun c => K.mul (F (g :: c)) (H0 c)) (cfgs (g0 :: rest) S.length))
            = K.mul n (K.mul (sumK K (fun c => F (g :: c)) (cfgs (g0 :: rest) S.length)) (sumK K H0 (cfgs (g0 :: rest) S.length))) := by
          intro g
          rw [K.mul_assoc, indep (g0 :: rest) S T (fun c => F (g :: c)) H0 hl' hd.2 (dep_tail true S F hF g) hH0]
        rw [sumK_ext K _ _ e3, ← mul_sumK, ← sumK_mul]
        rw [K.mul_comm n, K.mul_assoc, K.mul_comm (sumK K H0 _) n]
    | false =>
      cases els with
      | nil =>
        show K.mul (K.mul n _) K.zero = K.mul K.zero K.zero
        rw [DetChain.mul_zero, DetChain.mul_zero]
      | cons g0 rest =>
        let F0 : List G → R := fun c => F (g0 :: c)
        have hFg : ∀ g c, F (g :: c) = F0 c := fun g c => dep_free S F hF g g0 c
        have hF0 : depOn S F0 := dep_tail false S F hF g0
        have e1 : ∀ g, sumK K (fun c => K.mul (F (g :: c)) (H (g :: c))) (cfgs (g0 :: rest) S.length)
            = sumK K (fun c => K.mul (F0 c) (H (g :: c))) (cfgs (g0 :: rest) S.length) :=
          fun g => sumK_ext K _ _ (fun c => by rw [hFg]) _
        have e2 : sumK K (fun g => sumK K (fun c => F (g :: c)) (cfgs (g0 :: rest) S.length)) (g0 :: rest)
            = K.mul n (sumK K F0 (cfgs (g0 :: rest) S.length)) := by
          rw [sumK_ext K _ _ (fun g => sumK_ext K _ _ (fun c => hFg g c) _), sum_const]
        rw [sumK_ext K _ _ e1, e2, mul_sumK]
        have e3 : ∀ g, K.mul (K.mul n (pwN K n S.length)) (sumK K (fun c => K.mul (F0 c) (H (g :: c))) (cfgs (g0 :: rest) S.length))
            = K.mul n (K.mul (sumK K F0 (cfgs (g0 :: rest) S.length)) (sumK K (fun c => H (g :: c)) (cfgs (g0 :: rest) S.length))) := by
          intro g
          rw [K.mul_assoc, indep (g0 :: rest) S T F0 (fun c => H (g :: c)) hl' hd.2 hF0 (dep_tail u T H hH g)]
        rw [sumK_ext K _ _ e3, ← mul_sumK, ← mul_sumK]
        rw [K.mul_assoc]

end Indep

#print axioms Indep.sum_const
#print axioms Indep.indep

ym/MeanZero.lean

/-
  ym/MeanZero — T45, cycle 44 (Lad2, any finite gauge group): the single-cover rule.

    setG c l g          the field c with link l set to g
    single_cover        f has mean zero along link l (Σ_g f(c[l := g]) = 0 for every c) and H does not depend on
                        link l  ⇒  Σ_c f(c) H(c) = 0
    mean_zero_transl    if, for each c, g ↦ f(c[l := g]) is φ ∘ σ_c for a bijection σ_c of G (a plaquette variable
                        U_p = a U_l b or a U_l⁻¹ b) and Σ_g φ(g) = 0, then f has mean zero along l
  In the plaquette expansion with w_p = c(1 + f_p), ∫ f_p = 0, and the Wilson loop χ(U_W) (∫χ = 0): a polymer A in
  which some link of A ∪ W is covered by only one factor has zero weight. So nonzero polymers cover every link twice.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Indep

namespace MeanZero

open YMChain LinkFactor FiniteRecChain Indep

variable {R : Type} (K : DetChain.CRing R) {G : Type}

def setG : List G → Nat → G → List G
  | [], _, _ => []
  | _ :: c, 0, g => g :: c
  | a :: c, l + 1, g => a :: setG c l g

theorem split_cfg (els : List G) (L : Nat) (Φ : List G → R) :
    sumK K Φ (cfgs els (L + 1)) = sumK K (fun g => sumK K (fun c => Φ (g :: c)) (cfgs els L)) els := by
  show sumK K Φ (joinL (els.map (fun g => (cfgs els L).map (List.cons g)))) = _
  rw [sumK_join, LinkFactor.sumK_map]
  exact sumK_ext K _ _ (fun g => LinkFactor.sumK_map K Φ _ _) els

/-- **Single-cover rule.** -/
theorem single_cover (els : List G) : ∀ (L l : Nat) (T : List Bool) (f H : List G → R),
    l < L → Seeds.nthB T l = false → depOn T H →
    (∀ c, sumK K (fun g => f (setG c l g)) els = K.zero) →
    sumK K (fun c => K.mul (f c) (H c)) (cfgs els L) = K.zero
  | 0, _, _, _, _, h, _, _, _ => absurd h (Nat.not_lt_zero _)
  | L + 1, 0, T, f, H, _, hT, hH, hf => by
    rw [split_cfg]
    cases els with
    | nil => rfl
    | cons g0 rest =>
      have hHg : ∀ g c, H (g :: c) = H (g0 :: c) := fun g c => hH _ _ (fun l hl => by
        cases l with
        | zero => rw [hT] at hl; exact absurd hl Bool.noConfusion
        | succ l => rfl)
      rw [sumK_swap]
      refine (sumK_ext K _ _ (fun c => ?_) _).trans (MaskSum.sum_zero K _)
      rw [sumK_ext K _ _ (fun g => by rw [hHg g c]), ← sumK_mul]
      have := hf (g0 :: c)
      show K.mul (sumK K (fun g => f (g :: c)) (g0 :: rest)) _ = K.zero
      rw [show sumK K (fun g => f (g :: c)) (g0 :: rest) = sumK K (fun g => f (setG (g0 :: c) 0 g)) (g0 :: rest) from rfl,
        this, DetChain.zero_mul']
  | L + 1, l + 1, T, f, H, hl, hT, hH, hf => by
    rw [split_cfg]
    refine (sumK_ext K _ _ (fun g => ?_) _).trans (MaskSum.sum_zero K _)
    cases T with
    | nil =>
      exact single_cover els L l [] (fun c => f (g :: c)) (fun c => H (g :: c)) (Nat.lt_of_succ_lt_succ hl) rfl
        (fun c c' _ => hH _ _ (fun l' hl' => absurd hl' Bool.noConfusion))
        (fun c => hf (g :: c))
    | cons t T =>
      exact single_cover els L l T (fun c => f (g :: c)) (fun c => H (g :: c)) (Nat.lt_of_succ_lt_succ hl) hT
        (dep_tail t T H hH g) (fun c => hf (g :: c))

/-- **Mean zero along a link from translation invariance.** -/
theorem mean_zero_transl [DecidableEq G] (els : List G) (hc : Complete K els) (f : List G → R) (l : Nat) (φ : G → R)
    (hφ : sumK K φ els = K.zero)
    (σ τ : List G → G → G) (hστ : ∀ c x, τ c (σ c x) = x) (hτσ : ∀ c y, σ c (τ c y) = y)
    (hf : ∀ c g, f (setG c l g) = φ (σ c g)) (c : List G) :
    sumK K (fun g => f (setG c l g)) els = K.zero := by
  rw [sumK_ext K _ _ (fun g => hf c g), reindex K els hc (σ c) (τ c) (hστ c) (hτσ c) φ]
  exact hφ

end MeanZero

#print axioms MeanZero.split_cfg
#print axioms MeanZero.single_cover
#print axioms MeanZero.mean_zero_transl

ym/PosRatio.lean

/-
  ym/PosRatio — T45, cycle 45 (Lad2, any finite gauge group): partition-function ratios from positivity.

  Plaquette weights w i : fields → R (plaquette i of the list), bounded m ≤ w i c ≤ Mx with 0 ≤ m (for the Wilson
  action w = e^{β(Re tr U_p − d)}: m = e^{−2βd}, Mx = 1). Z(M) = Σ_c Π_{i ∈ M} w i c for a plaquette mask M.
    prodW_split     Π_M w = Π_{M \ X} w · Π_{M ∩ X} w
    prodW_ge        Π_Y w ≥ m^|Y|,   prodW_le   Π_Y w ≤ Mx^|Y|
    ratio_pos       Z(M \ X) · m^|M ∩ X| ≤ Z(M) ≤ Z(M \ X) · Mx^|M ∩ X|
  No GKS: this replaces Ratio.ratio for a general group, at the price of a factor (Mx/m)^|X| → 1 as β → 0.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.MeanZero
import ym.Ratio
import ym.Gap

namespace PosRatio

open YMChain LinkIntegral LinkFactor Indep Closure Factor Seeds Count Ratio

variable {R : Type} (K : DetChain.CRing R) {G : Type}

def prodW (w : Nat → List G → R) : Nat → List Bool → List G → R
  | _, [], _ => K.one
  | k, b :: M, c => K.mul (if b = true then w k c else K.one) (prodW w (k + 1) M c)

theorem prodW_split (w : Nat → List G → R) (c : List G) : ∀ (k : Nat) (M X : List Bool), M.length = X.length →
    prodW K w k M c = K.mul (prodW K w k (andB M (notB X)) c) (prodW K w k (andB M X) c)
  | _, [], _, _ => (K.one_mul _).symm
  | _, _ :: _, [], h => absurd h Nat.noConfusion
  | k, b :: M, x :: X, h => by
    show K.mul (if b = true then w k c else K.one) (prodW K w (k + 1) M c)
      = K.mul (K.mul (if (b && !x) = true then w k c else K.one) (prodW K w (k + 1) (andB M (notB X)) c))
          (K.mul (if (b && x) = true then w k c else K.one) (prodW K w (k + 1) (andB M X) c))
    rw [prodW_split w c (k + 1) M X (Nat.succ.inj h)]
    have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
      rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
    rw [mul4]
    congr 1
    cases b <;> cases x
    · show K.one = K.mul K.one K.one; rw [K.one_mul]
    · show K.one = K.mul K.one K.one; rw [K.one_mul]
    · show w k c = K.mul (w k c) K.one; rw [K.mul_comm, K.one_mul]
    · show w k c = K.mul K.one (w k c); rw [K.one_mul]

variable (O : ORing K)

theorem prodW_ge (w : Nat → List G → R) (m : R) (hm : O.le K.zero m) (h1 : O.le K.zero K.one) (hm1 : O.le m K.one)
    (hw : ∀ k c, O.le m (w k c)) (c : List G) : ∀ (k : Nat) (Y : List Bool),
    O.le (pwR K m (cnt Y)) (prodW K w k Y c)
  | _, [] => O.le_refl _
  | k, true :: Y => by
    show O.le (K.mul (pwR K m (cnt Y)) m) (K.mul (w k c) (prodW K w (k + 1) Y c))
    rw [K.mul_comm]
    exact Gap.mul_le_mul4 K O _ _ _ _ hm (AreaLower.pw_nn K O m hm h1 _)
      (O.le_trans _ _ _ (AreaLower.pw_nn K O m hm h1 _) (prodW_ge w m hm h1 hm1 hw c (k + 1) Y)) (hw k c)
      (prodW_ge w m hm h1 hm1 hw c (k + 1) Y)
  | k, false :: Y => by
    show O.le (pwR K m (cnt Y)) (K.mul K.one (prodW K w (k + 1) Y c))
    rw [K.one_mul]; exact prodW_ge w m hm h1 hm1 hw c (k + 1) Y

theorem prodW_nn (w : Nat → List G → R) (h1 : O.le K.zero K.one) (hw : ∀ k c, O.le K.zero (w k c)) (c : List G) :
    ∀ (k : Nat) (Y : List Bool), O.le K.zero (prodW K w k Y c)
  | _, [] => h1
  | k, b :: Y => O.mul_nonneg _ _ (by cases b; exact h1; exact hw k c) (prodW_nn w h1 hw c (k + 1) Y)

theorem prodW_le (w : Nat → List G → R) (Mx : R) (h1 : O.le K.zero K.one) (hM : O.le K.zero Mx)
    (hw0 : ∀ k c, O.le K.zero (w k c)) (hw : ∀ k c, O.le (w k c) Mx) (c : List G) : ∀ (k : Nat) (Y : List Bool),
    O.le (prodW K w k Y c) (pwR K Mx (cnt Y))
  | _, [] => O.le_refl _
  | k, true :: Y => by
    show O.le (K.mul (w k c) (prodW K w (k + 1) Y c)) (K.mul (pwR K Mx (cnt Y)) Mx)
    rw [K.mul_comm (pwR K Mx (cnt Y))]
    exact Gap.mul_le_mul4 K O _ _ _ _ (hw0 k c) (prodW_nn K O w h1 hw0 c (k + 1) Y)
      (AreaLower.pw_nn K O Mx hM h1 _) (hw k c) (prodW_le w Mx h1 hM hw0 hw c (k + 1) Y)
  | k, false :: Y => by
    show O.le (K.mul K.one (prodW K w (k + 1) Y c)) (pwR K Mx (cnt Y))
    rw [K.one_mul]; exact prodW_le w Mx h1 hM hw0 hw c (k + 1) Y

/-- **Ratio bounds from positivity.** -/
theorem ratio_pos (els : List G) (L : Nat) (w : Nat → List G → R) (m Mx : R) (h1 : O.le K.zero K.one)
    (hm : O.le K.zero m) (hm1 : O.le m K.one) (hMx : O.le K.zero Mx)
    (hwm : ∀ k c, O.le m (w k c)) (hwM : ∀ k c, O.le (w k c) Mx) (M X : List Bool) (hl : M.length = X.length) :
    O.le (K.mul (sumK K (prodW K w 0 (andB M (notB X))) (cfgs els L)) (pwR K m (cnt (andB M X))))
        (sumK K (prodW K w 0 M) (cfgs els L)) ∧
      O.le (sumK K (prodW K w 0 M) (cfgs els L))
        (K.mul (sumK K (prodW K w 0 (andB M (notB X))) (cfgs els L)) (pwR K Mx (cnt (andB M X)))) := by
  have hw0 : ∀ k c, O.le K.zero (w k c) := fun k c => O.le_trans _ _ _ hm (hwm k c)
  rw [sumK_mul, sumK_mul]
  constructor
  · refine MaskSum.sumK_le K O _ _ _ (fun c _ => ?_)
    rw [prodW_split K w c 0 M X hl]
    rw [K.mul_comm (prodW K w 0 (andB M (notB X)) c), K.mul_comm (prodW K w 0 (andB M (notB X)) c)]
    exact O.mul_le_mul_right _ _ _ (prodW_ge K O w m hm h1 hm1 hwm c 0 _) (prodW_nn K O w h1 hw0 c 0 _)
  · refine MaskSum.sumK_le K O _ _ _ (fun c _ => ?_)
    rw [prodW_split K w c 0 M X hl]
    rw [K.mul_comm (prodW K w 0 (andB M (notB X)) c), K.mul_comm (prodW K w 0 (andB M (notB X)) c)]
    exact O.mul_le_mul_right _ _ _ (prodW_le K O w Mx h1 hMx hw0 hwM c 0 _) (prodW_nn K O w h1 hw0 c 0 _)

end PosRatio

#print axioms PosRatio.prodW_split
#print axioms PosRatio.prodW_ge
#print axioms PosRatio.prodW_le
#print axioms PosRatio.ratio_pos

ym/Signed.lean

/-
  ym/Signed — T45, cycle 46 (Lad2, any finite gauge group): exact regrouping of signed sums, two-sided bounds.

    bij_sum        if every mask S is c S ∪ d S, then for any Φ (signed):
                     Σ_S Φ(S) = Σ_A Σ_D [A = c(A ∪ D) ∧ D = d(A ∪ D)] Φ(A ∪ D)
    two-sided bounds (no absolute value needed):
    bnd_sum        −b_i ≤ x_i ≤ b_i  ⇒  −Σ b ≤ Σ x ≤ Σ b
    bnd_mul        −α ≤ a ≤ α, −β ≤ b ≤ β, α, β ≥ 0  ⇒  −αβ ≤ ab ≤ αβ
  For a non-abelian group the polymer weights have no sign; these replace the positivity used for ℤ₂.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.PosRatio

namespace Signed

open YMChain LinkIntegral MaskSum Closure

variable {R : Type} (K : DetChain.CRing R)

/-- **Exact regrouping of a mask sum along a decomposition S = c S ∪ d S.** -/
theorem bij_sum (n : Nat) (c d : List Bool → List Bool) (Φ : List Bool → R)
    (hc : ∀ S, S.length = n → (c S).length = n) (hd : ∀ S, S.length = n → (d S).length = n)
    (hu : ∀ S, S.length = n → S = orB (c S) (d S)) :
    sumK K Φ (bools n) = sumK K (fun A => sumK K (fun D =>
      if A = c (orB A D) ∧ D = d (orB A D) then Φ (orB A D) else K.zero) (bools n)) (bools n) := by
  have hmem := ChangeVar2D.mem_bools_len n
  -- each term as a triple selection
  have e1 : ∀ A D, A.length = n → D.length = n →
      (if A = c (orB A D) ∧ D = d (orB A D) then Φ (orB A D) else K.zero)
        = sumK K (fun S => if S = orB A D ∧ A = c S ∧ D = d S then Φ S else K.zero) (bools n) := by
    intro A D hA hD
    have hAD := len_orB n A D hA hD
    by_cases cnd : A = c (orB A D) ∧ D = d (orB A D)
    · rw [ip cnd, ← sel K (Φ (orB A D)) n (orB A D) hAD]
      exact sumK_ext K _ _ (fun S => by
        by_cases e : S = orB A D
        · rw [ip e, ip ⟨e, by rw [e]; exact cnd.1, by rw [e]; exact cnd.2⟩, e]
        · rw [ineg e, ineg (fun h => e h.1)]) _
    · rw [ineg cnd]
      exact ((sumK_ext K _ _ (fun S => ineg (fun h => cnd (by rw [← h.1]; exact ⟨h.2.1, h.2.2⟩))) _).trans
        (sum_zero K _)).symm
  rw [sumK_ext_mem K _ _ (bools n) (fun A hA => sumK_ext_mem K _ _ (bools n) (fun D hD => e1 A D (hmem A hA) (hmem D hD)))]
  rw [sumK_ext K _ _ (fun A => sumK_swap K _ (bools n) (bools n)), sumK_swap]
  refine sumK_ext_mem K _ _ (bools n) (fun S hS => ?_)
  have hS' := hmem S hS
  have e2 : ∀ A D, (if S = orB A D ∧ A = c S ∧ D = d S then Φ S else K.zero)
      = (if A = c S then (if D = d S then Φ S else K.zero) else K.zero) := by
    intro A D
    by_cases h1 : A = c S
    · by_cases h2 : D = d S
      · rw [ip ⟨by rw [h1, h2]; exact hu S hS', h1, h2⟩, ip h1, ip h2]
      · rw [ineg (fun h => h2 h.2.2), ip h1, ineg h2]
    · rw [ineg (fun h => h1 h.2.1), ineg h1]
  rw [sumK_ext K _ _ (fun A => sumK_ext K _ _ (fun D => e2 A D) _)]
  have e3 : ∀ A, sumK K (fun D => if A = c S then (if D = d S then Φ S else K.zero) else K.zero) (bools n)
      = (if A = c S then Φ S else K.zero) := by
    intro A
    by_cases h1 : A = c S
    · rw [ip h1]
      exact (sumK_ext K _ _ (fun D => ip h1) _).trans (sel K (Φ S) n (d S) (hd S hS'))
    · rw [ineg h1]
      exact (sumK_ext K _ _ (fun D => ineg h1) _).trans (sum_zero K _)
  rw [sumK_ext K _ _ e3, sel K (Φ S) n (c S) (hc S hS')]

theorem uniq_neg (x y : R) (h : K.add x y = K.zero) : y = K.neg x := by
  have : K.add (K.neg x) (K.add x y) = K.neg x := by rw [h, DetChain.add_zero']
  rw [← K.add_assoc, K.add_comm (K.neg x) x, K.add_neg, K.zero_add] at this
  exact this

variable (O : ORing K)

theorem neg_le_neg (a b : R) (h : O.le a b) : O.le (K.neg b) (K.neg a) := by
  have := O.add_le_add _ _ _ _ h (O.le_refl (K.add (K.neg a) (K.neg b)))
  have e1 : K.add a (K.add (K.neg a) (K.neg b)) = K.neg b := by
    rw [← K.add_assoc, K.add_neg, K.zero_add]
  have e2 : K.add b (K.add (K.neg a) (K.neg b)) = K.neg a := by
    rw [K.add_comm (K.neg a), ← K.add_assoc, K.add_neg, K.zero_add]
  rw [e1, e2] at this
  exact this

theorem neg_add' (a b : R) : K.neg (K.add a b) = K.add (K.neg a) (K.neg b) := by
  have h : K.add (K.add a b) (K.add (K.neg a) (K.neg b)) = K.zero := by
    rw [DetChain.add_mid4, K.add_neg, K.add_neg, K.zero_add]
  have u : ∀ x y : R, K.add x y = K.zero → y = K.neg x := uniq_neg K
  exact (u _ _ h).symm

theorem bnd_sum {A : Type} (x b : A → R) : ∀ l : List A, (∀ a, memL a l → O.le (K.neg (b a)) (x a) ∧ O.le (x a) (b a)) →
    O.le (K.neg (sumK K b l)) (sumK K x l) ∧ O.le (sumK K x l) (sumK K b l)
  | [], _ => by
    have e : K.neg K.zero = K.zero := by
      have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
    show O.le (K.neg K.zero) K.zero ∧ O.le K.zero K.zero
    rw [e]; exact ⟨O.le_refl _, O.le_refl _⟩
  | a :: l, h => by
    have ih := bnd_sum x b l (fun a' ha => h a' (Or.inr ha))
    have ha := h a (Or.inl rfl)
    show O.le (K.neg (K.add (b a) (sumK K b l))) (K.add (x a) (sumK K x l)) ∧
      O.le (K.add (x a) (sumK K x l)) (K.add (b a) (sumK K b l))
    rw [neg_add' K]
    exact ⟨O.add_le_add _ _ _ _ ha.1 ih.1, O.add_le_add _ _ _ _ ha.2 ih.2⟩

theorem neg_mul' (a b : R) : K.mul (K.neg a) b = K.neg (K.mul a b) := by
  have h : K.add (K.mul a b) (K.mul (K.neg a) b) = K.zero := by
    rw [← DetChain.right_distrib, K.add_neg, DetChain.zero_mul']
  have u : ∀ x y : R, K.add x y = K.zero → y = K.neg x := uniq_neg K
  exact u _ _ h

theorem neg_neg' (a : R) : K.neg (K.neg a) = a := by
  have h : K.add (K.neg a) a = K.zero := by rw [K.add_comm, K.add_neg]
  have u : ∀ x y : R, K.add x y = K.zero → y = K.neg x := uniq_neg K
  exact (u _ _ h).symm

theorem le_neg_of (a : R) (h : O.le a K.zero) : O.le K.zero (K.neg a) := by
  have := neg_le_neg K O _ _ h
  have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
  rw [e] at this; exact this

/-- **Product of two-sided bounds.** -/
theorem bnd_mul (a b α β : R) (hα : O.le K.zero α) (hβ : O.le K.zero β)
    (ha : O.le (K.neg α) a ∧ O.le a α) (hb : O.le (K.neg β) b ∧ O.le b β) :
    O.le (K.neg (K.mul α β)) (K.mul a b) ∧ O.le (K.mul a b) (K.mul α β) := by
  -- write a = sign · |a| by cases on the sign of a and b
  have key : ∀ x y : R, O.le K.zero x → O.le x α → O.le K.zero y → O.le y β → O.le (K.mul x y) (K.mul α β) :=
    fun x y hx hxa hy hyb => Gap.mul_le_mul4 K O x α y β hx hy hβ hxa hyb
  cases O.le_total K.zero a with
  | inl ha0 =>
    cases O.le_total K.zero b with
    | inl hb0 =>
      have u := key a b ha0 ha.2 hb0 hb.2
      have e0 : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
      have hneg0 : O.le (K.neg (K.mul α β)) K.zero := by
        have := neg_le_neg K O _ _ (O.mul_nonneg _ _ hα hβ); rw [e0] at this; exact this
      exact ⟨O.le_trans _ _ _ hneg0 (O.mul_nonneg _ _ ha0 hb0), u⟩
    | inr hb0 =>
      -- b ≤ 0: −b ∈ [0, β]
      have nb0 := le_neg_of K O b hb0
      have nbb : O.le (K.neg b) β := by have := neg_le_neg K O _ _ hb.1; rw [neg_neg'] at this; exact this
      have u := key a (K.neg b) ha0 ha.2 nb0 nbb
      have e : K.mul a (K.neg b) = K.neg (K.mul a b) := by rw [K.mul_comm, neg_mul', K.mul_comm]
      rw [e] at u
      refine ⟨by have := neg_le_neg K O _ _ u; rw [neg_neg'] at this; exact this, ?_⟩
      have : O.le (K.mul a b) K.zero := by
        have := O.mul_nonneg _ _ ha0 nb0
        rw [e] at this
        have := neg_le_neg K O _ _ this
        rw [neg_neg'] at this
        have e0 : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
        rw [e0] at this; exact this
      exact O.le_trans _ _ _ this (O.mul_nonneg _ _ hα hβ)
  | inr ha0 =>
    have na0 := le_neg_of K O a ha0
    have naa : O.le (K.neg a) α := by have := neg_le_neg K O _ _ ha.1; rw [neg_neg'] at this; exact this
    cases O.le_total K.zero b with
    | inl hb0 =>
      have u := key (K.neg a) b na0 naa hb0 hb.2
      rw [neg_mul'] at u
      refine ⟨by have := neg_le_neg K O _ _ u; rw [neg_neg'] at this; exact this, ?_⟩
      have : O.le (K.mul a b) K.zero := by
        have := O.mul_nonneg _ _ na0 hb0
        rw [neg_mul'] at this
        have := neg_le_neg K O _ _ this
        rw [neg_neg'] at this
        have e0 : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
        rw [e0] at this; exact this
      exact O.le_trans _ _ _ this (O.mul_nonneg _ _ hα hβ)
    | inr hb0 =>
      have nb0 := le_neg_of K O b hb0
      have nbb : O.le (K.neg b) β := by have := neg_le_neg K O _ _ hb.1; rw [neg_neg'] at this; exact this
      have u := key (K.neg a) (K.neg b) na0 naa nb0 nbb
      have e : K.mul (K.neg a) (K.neg b) = K.mul a b := by
        rw [neg_mul', K.mul_comm a, neg_mul', neg_neg', K.mul_comm]
      rw [e] at u
      have hp : O.le K.zero (K.mul a b) := by
        have := O.mul_nonneg _ _ na0 nb0; rw [e] at this; exact this
      refine ⟨O.le_trans _ _ _ ?_ hp, u⟩
      have := neg_le_neg K O _ _ (O.mul_nonneg _ _ hα hβ)
      have e0 : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
      rw [e0] at this; exact this

end Signed

#print axioms Signed.bij_sum
#print axioms Signed.bnd_sum
#print axioms Signed.bnd_mul

ym/Expand.lean

/-
  ym/Expand — T45, cycle 47 (Lad2, any finite gauge group): polymer expansion of the plaquette product and the
  dependence of products on link sets.

  Plaquette i has weight c + f i (c a constant, f i the fluctuation). prodW (PosRatio) is the product over a mask.
    expand          Π_{i ∈ M} (c + f_i) = Σ_{S ⊆ M} c^{|M \ S|} Π_{i ∈ S} f_i
    prodW_or        Π_{A ∪ D} f = Π_A f · Π_D f                      (A, D disjoint)
    depOn_mono      F depends on S, S ⊆ S'  ⇒  F depends on S'
    depOn_mul       F on S, H on T  ⇒  F·H on S ∪ T
    depOn_prodW     each f_i depends on sp_i  ⇒  Π_{i ∈ A} f_i depends on ⋃_{i ∈ A} sp_i

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Signed

namespace Expand

open YMChain LinkIntegral MaskSum Closure Factor Seeds Count Indep PosRatio Ratio

variable {R : Type} (K : DetChain.CRing R) {G : Type}

/-- **Polymer expansion over a mask.** -/
theorem expand (c : R) (f : Nat → List G → R) (x : List G) : ∀ (k : Nat) (M : List Bool),
    prodW K (fun i x => K.add c (f i x)) k M x
      = sumK K (fun S => if andB S M = S then K.mul (pwR K c (cnt (andB M (notB S)))) (prodW K f k S x) else K.zero)
          (bools M.length)
  | _, [] => by
    show K.one = K.add (if ([] : List Bool) = [] then K.mul (pwR K c (cnt ([] : List Bool))) (prodW K f _ [] x) else K.zero) K.zero
    rw [ip rfl, DetChain.add_zero']
    show K.one = K.mul K.one K.one
    rw [K.one_mul]
  | k, b :: M => by
    show K.mul (if b = true then K.add c (f k x) else K.one) (prodW K (fun i x => K.add c (f i x)) (k + 1) M x)
      = sumK K _ ((bools M.length).map (List.cons true) ++ (bools M.length).map (List.cons false))
    rw [expand c f x (k + 1) M, sumK_app, MaskSum.sumK_map, MaskSum.sumK_map]
    cases b with
    | true =>
      show K.mul (K.add c (f k x)) _ = _
      rw [K.mul_comm, K.distrib, K.mul_comm, K.mul_comm _ (f k x), mul_sumK, mul_sumK, K.add_comm]
      congr 1
      · refine sumK_ext K _ _ (fun S => ?_) _
        show _ = (if true :: andB S M = true :: S then
          K.mul (pwR K c (cnt (andB M (notB S)))) (K.mul (f k x) (prodW K f (k + 1) S x)) else K.zero)
        rw [GPerm.if_cons]
        by_cases e : andB S M = S
        · rw [ip e, ip e, ← K.mul_assoc, K.mul_comm (f k x), K.mul_assoc]
        · rw [ineg e, ineg e, DetChain.mul_zero]
      · refine sumK_ext K _ _ (fun S => ?_) _
        show _ = (if false :: andB S M = false :: S then
          K.mul (pwR K c (cnt (andB M (notB S)) + 1)) (K.mul K.one (prodW K f (k + 1) S x)) else K.zero)
        rw [GPerm.if_cons]
        by_cases e : andB S M = S
        · rw [ip e, ip e, K.one_mul]
          show K.mul c (K.mul (pwR K c (cnt (andB M (notB S)))) (prodW K f (k + 1) S x))
            = K.mul (K.mul (pwR K c (cnt (andB M (notB S)))) c) (prodW K f (k + 1) S x)
          rw [K.mul_comm (pwR K c _) c, K.mul_assoc]
        · rw [ineg e, ineg e, DetChain.mul_zero]
    | false =>
      show K.mul K.one _ = _
      rw [K.one_mul]
      have e1 : sumK K (fun S => if andB (true :: S) (false :: M) = true :: S then
          K.mul (pwR K c (cnt (andB (false :: M) (notB (true :: S))))) (prodW K f k (true :: S) x) else K.zero)
          (bools M.length) = K.zero :=
        (sumK_ext K _ _ (fun S => ineg (fun e => Bool.noConfusion (List.head_eq_of_cons_eq e))) _).trans (sum_zero K _)
      rw [e1, K.zero_add]
      refine sumK_ext K _ _ (fun S => ?_) _
      show _ = (if false :: andB S M = false :: S then
        K.mul (pwR K c (cnt (andB M (notB S)))) (K.mul K.one (prodW K f (k + 1) S x)) else K.zero)
      rw [GPerm.if_cons]
      by_cases e : andB S M = S
      · rw [ip e, ip e, K.one_mul]
      · rw [ineg e, ineg e]

theorem prodW_or (f : Nat → List G → R) (x : List G) : ∀ (k : Nat) (A D : List Bool), A.length = D.length → disjM A D →
    prodW K f k (orB A D) x = K.mul (prodW K f k A x) (prodW K f k D x)
  | _, [], [], _, _ => (K.one_mul _).symm
  | _, [], _ :: _, h, _ => absurd h Nat.noConfusion
  | _, _ :: _, [], h, _ => absurd h Nat.noConfusion
  | k, a :: A, d :: D, h, hd => by
    show K.mul (if (a || d) = true then f k x else K.one) (prodW K f (k + 1) (orB A D) x)
      = K.mul (K.mul (if a = true then f k x else K.one) (prodW K f (k + 1) A x))
          (K.mul (if d = true then f k x else K.one) (prodW K f (k + 1) D x))
    rw [prodW_or f x (k + 1) A D (Nat.succ.inj h) hd.2]
    have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
      rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
    rw [mul4]
    congr 1
    cases a <;> cases d
    · show K.one = K.mul K.one K.one; rw [K.one_mul]
    · show f k x = K.mul K.one (f k x); rw [K.one_mul]
    · show f k x = K.mul (f k x) K.one; rw [K.mul_comm, K.one_mul]
    · exact absurd ⟨rfl, rfl⟩ hd.1

theorem depOn_mono (S S' : List Bool) (F : List G → R) (hS : leB S S') (h : depOn S F) : depOn S' F :=
  fun c c' hcc => h c c' (fun l hl => hcc l (of_leB S S' hS l hl))

theorem depOn_mul (S T : List Bool) (F H : List G → R) (hl : S.length = T.length) (hF : depOn S F) (hH : depOn T H) :
    depOn (orB S T) (fun c => K.mul (F c) (H c)) := fun c c' hcc => by
  show K.mul (F c) (H c) = K.mul (F c') (H c')
  rw [hF c c' (fun l hl' => hcc l (by rw [Cluster.nth_orB S T l hl, hl']; rfl)),
    hH c c' (fun l hl' => hcc l (by rw [Cluster.nth_orB S T l hl, hl', Bool.or_true]))]

/-- Union of the supports `sp i` of the plaquettes selected by `A` (index offset `k`), as a mask of length `L`. -/
def uSupp (L : Nat) (sp : Nat → List Bool) : Nat → List Bool → List Bool
  | _, [] => fz L
  | k, true :: A => orB (sp k) (uSupp L sp (k + 1) A)
  | k, false :: A => uSupp L sp (k + 1) A

theorem len_uSupp (L : Nat) (sp : Nat → List Bool) (hsp : ∀ i, (sp i).length = L) :
    ∀ (k : Nat) (A : List Bool), (uSupp L sp k A).length = L
  | _, [] => len_fz L
  | k, true :: A => len_orB L _ _ (hsp k) (len_uSupp L sp hsp (k + 1) A)
  | k, false :: A => len_uSupp L sp hsp (k + 1) A

theorem depOn_const (S : List Bool) (x : R) : depOn S (fun _ : List G => x) := fun _ _ _ => rfl

theorem depOn_prodW (L : Nat) (sp : Nat → List Bool) (hsp : ∀ i, (sp i).length = L) (f : Nat → List G → R)
    (hf : ∀ i, depOn (sp i) (f i)) : ∀ (k : Nat) (A : List Bool), depOn (uSupp L sp k A) (prodW K f k A)
  | _, [] => fun _ _ _ => rfl
  | k, true :: A => by
    have := depOn_mul K (sp k) (uSupp L sp (k + 1) A) (f k) (prodW K f (k + 1) A)
      ((hsp k).trans (len_uSupp L sp hsp (k + 1) A).symm) (hf k) (depOn_prodW L sp hsp f hf (k + 1) A)
    intro c c' hcc
    show K.mul (if true = true then f k c else K.one) _ = K.mul (if true = true then f k c' else K.one) _
    exact this c c' hcc
  | k, false :: A => by
    intro c c' hcc
    show K.mul K.one (prodW K f (k + 1) A c) = K.mul K.one (prodW K f (k + 1) A c')
    rw [depOn_prodW L sp hsp f hf (k + 1) A c c' hcc]

end Expand

#print axioms Expand.expand
#print axioms Expand.prodW_or
#print axioms Expand.depOn_mul
#print axioms Expand.depOn_prodW

ym/Poly.lean

/-
  ym/Poly — T45, cycle 48 (Lad2, any finite gauge group, any dimension): the exact cluster identity.

  Data: plaquette link vectors P (N of them, length L), loop vector n, fields c ∈ cfgs els L, plaquette fluctuations
  f i depending only on the links of plaquette i, Wilson loop χ depending only on the links of n, weight c₀ + f i.
    aW A = Σ_x χ(x) Π_{i∈A} f_i(x),  a0 D = Σ_x Π_{i∈D} f_i(x)
    ZW M = Σ_x χ(x) Π_{i∈M} (c₀ + f_i(x)),  Z0 M = Σ_x Π_{i∈M} (c₀ + f_i(x))
    ZW_exp, Z0_exp     ZW M = Σ_{S⊆M} c₀^{|M∖S|} aW S,   Z0 M = Σ_{D⊆M} c₀^{|M∖D|} a0 D
    touch_of_nth, uSupp_nth, supp_disj      the links of a detached part D are disjoint from those of A and of n
    pair_fact          |G|^L aW(A ∪ D) = aW A · a0 D                 (D detached from A and the loop; Indep)
  (The regrouping into Σ_{A conn} c₀^{…} aW(A) Z0(M ∖ X(A)) is the next cycle.)

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Expand
import ym.Approx

namespace Poly

open YMChain LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Ratio Expand Approx

variable {R : Type} (K : DetChain.CRing R) {G : Type}

theorem touch_of_nth : ∀ (p q : List Nat) (l : Nat), nthB (p.map nzb) l = true → nthB (q.map nzb) l = true →
    touch p q = true
  | [], _, _, h, _ => absurd h Bool.noConfusion
  | _ :: _, [], _, _, h => absurd h Bool.noConfusion
  | a :: p, b :: q, 0, h1, h2 => by
    show ((nzb a && nzb b) || touch p q) = true
    have e1 : nzb a = true := h1
    have e2 : nzb b = true := h2
    rw [e1, e2]; rfl
  | a :: p, b :: q, l + 1, h1, h2 => by
    show ((nzb a && nzb b) || touch p q) = true
    rw [touch_of_nth p q l h1 h2, Bool.or_true]

theorem uSupp_nth (L : Nat) (sp : Nat → List Bool) (hsp : ∀ i, (sp i).length = L) (l : Nat) :
    ∀ (k : Nat) (A : List Bool), nthB (uSupp L sp k A) l = true →
      ∃ j, j < A.length ∧ nthB A j = true ∧ nthB (sp (k + j)) l = true
  | _, [], h => by
    show ∃ j, j < 0 ∧ _
    have : nthB (fz L) l = false := nth_fz L l
    rw [show uSupp L sp _ [] = fz L from rfl, this] at h
    exact absurd h Bool.noConfusion
  | k, true :: A, h => by
    have h' : nthB (orB (sp k) (uSupp L sp (k + 1) A)) l = true := h
    rw [Cluster.nth_orB _ _ l ((hsp k).trans (len_uSupp L sp hsp (k + 1) A).symm)] at h'
    cases e : nthB (sp k) l with
    | true => exact ⟨0, Nat.succ_pos _, rfl, e⟩
    | false =>
      rw [e, Bool.false_or] at h'
      obtain ⟨j, hj, h1, h2⟩ := uSupp_nth L sp hsp l (k + 1) A h'
      exact ⟨j + 1, Nat.succ_lt_succ hj, h1, by rw [Nat.add_succ, ← Nat.succ_add]; exact h2⟩
  | k, false :: A, h => by
    obtain ⟨j, hj, h1, h2⟩ := uSupp_nth L sp hsp l (k + 1) A h
    exact ⟨j + 1, Nat.succ_lt_succ hj, h1, by rw [Nat.add_succ, ← Nat.succ_add]; exact h2⟩

def padB : Nat → List Bool → List Bool
  | 0, _ => []
  | L + 1, [] => false :: padB L []
  | L + 1, b :: x => b :: padB L x

theorem len_padB : ∀ (L : Nat) (x : List Bool), (padB L x).length = L
  | 0, _ => rfl
  | L + 1, [] => congrArg Nat.succ (len_padB L [])
  | L + 1, _ :: x => congrArg Nat.succ (len_padB L x)

theorem padB_eq : ∀ (L : Nat) (x : List Bool), x.length = L → padB L x = x
  | 0, [], _ => rfl
  | 0, _ :: _, h => absurd h Nat.noConfusion
  | _ + 1, [], h => absurd h Nat.noConfusion
  | L + 1, b :: x, h => by
    show b :: padB L x = b :: x
    rw [padB_eq L x (Nat.succ.inj h)]

theorem len_map' (p : List Nat) : (p.map nzb).length = p.length := by
  induction p with
  | nil => rfl
  | cons _ p ih => exact congrArg Nat.succ ih

section
variable (els : List G) (L : Nat) (P : List (List Nat)) (n : List Nat) (f : Nat → List G → R) (χ : List G → R) (c0 : R)

def aW (A : List Bool) : R := sumK K (fun x => K.mul (χ x) (prodW K f 0 A x)) (cfgs els L)
def a0 (D : List Bool) : R := sumK K (fun x => prodW K f 0 D x) (cfgs els L)
def ZW (M : List Bool) : R :=
  sumK K (fun x => K.mul (χ x) (prodW K (fun i x => K.add c0 (f i x)) 0 M x)) (cfgs els L)
def Z0 (M : List Bool) : R := sumK K (fun x => prodW K (fun i x => K.add c0 (f i x)) 0 M x) (cfgs els L)

theorem ZW_exp (M : List Bool) :
    ZW K els L f χ c0 M = sumK K (fun S => if andB S M = S then
      K.mul (pwR K c0 (cnt (andB M (notB S)))) (aW K els L f χ S) else K.zero) (bools M.length) := by
  show sumK K _ (cfgs els L) = _
  rw [sumK_ext K _ _ (fun x => by rw [expand K c0 f x 0 M, mul_sumK]), sumK_swap]
  refine sumK_ext K _ _ (fun S => ?_) _
  by_cases e : andB S M = S
  · rw [ip e]
    show sumK K (fun x => K.mul (χ x) (if andB S M = S then _ else _)) _ = _
    rw [sumK_ext K _ _ (fun x => by rw [ip e]), aW, mul_sumK]
    exact sumK_ext K _ _ (fun x => by rw [← K.mul_assoc, K.mul_comm (χ x), K.mul_assoc]) _
  · rw [ineg e]
    exact (sumK_ext K _ _ (fun x => by rw [ineg e, DetChain.mul_zero]) _).trans (sum_zero K _)

theorem Z0_exp (M : List Bool) :
    Z0 K els L f c0 M = sumK K (fun S => if andB S M = S then
      K.mul (pwR K c0 (cnt (andB M (notB S)))) (a0 K els L f S) else K.zero) (bools M.length) := by
  show sumK K _ (cfgs els L) = _
  rw [sumK_ext K _ _ (fun x => expand K c0 f x 0 M), sumK_swap]
  refine sumK_ext K _ _ (fun S => ?_) _
  by_cases e : andB S M = S
  · rw [ip e, sumK_ext K _ _ (fun x => ip e), a0, mul_sumK]
  · rw [ineg e]
    exact (sumK_ext K _ _ (fun x => ineg e) _).trans (sum_zero K _)

/-- Support of plaquette `i`, padded to length `L`. -/
def spP (L : Nat) (P : List (List Nat)) (i : Nat) : List Bool := padB L ((nthL P i).map nzb)

/-- The links of a part detached from `A` and the loop are disjoint from those of `A` and the loop. -/
theorem supp_disj (hP : ∀ i, i < P.length → (nthL P i).length = L) (hn : n.length = L)
    (A D : List Bool) (hA : A.length = P.length) (hD : D.length = P.length)
    (hDs : ∀ i, i < P.length → nthB D i = true → touch (nthL P i) n = false ∧ anyT P A (nthL P i) = false) :
    disjM (orB (n.map nzb) (uSupp L (spP L P) 0 A)) (uSupp L (spP L P) 0 D) := by
  have hsp : ∀ i, (spP L P i).length = L := fun i => len_padB L _
  have hsn : (n.map nzb).length = L := (len_map' n).trans hn
  refine disj_of_off _ _ (by rw [len_orB L _ _ hsn (len_uSupp L _ hsp 0 A), len_uSupp L _ hsp 0 D]) (fun l hl => ?_)
  obtain ⟨j, hj, hDj, hsj⟩ := uSupp_nth L (spP L P) hsp l 0 D hl
  rw [Nat.zero_add] at hsj
  have hjP : j < P.length := by rw [← hD]; exact hj
  have hsj' : nthB ((nthL P j).map nzb) l = true := by
    have := hsj; unfold spP at this; rw [padB_eq L _ ((len_map' _).trans (hP j hjP))] at this; exact this
  have hd := hDs j hjP hDj
  rw [Cluster.nth_orB _ _ l (hsn.trans (len_uSupp L _ hsp 0 A).symm)]
  cases e1 : nthB (n.map nzb) l with
  | true => rw [touch_of_nth _ _ l hsj' e1] at hd; exact absurd hd.1 Bool.noConfusion
  | false =>
    cases e2 : nthB (uSupp L (spP L P) 0 A) l with
    | false => rfl
    | true =>
      obtain ⟨i, hi, hAi, hsi⟩ := uSupp_nth L (spP L P) hsp l 0 A e2
      rw [Nat.zero_add] at hsi
      have hiP : i < P.length := by rw [← hA]; exact hi
      have hsi' : nthB ((nthL P i).map nzb) l = true := by
        have := hsi; unfold spP at this; rw [padB_eq L _ ((len_map' _).trans (hP i hiP))] at this; exact this
      have := anyT_of P A (nthL P j) i hAi (touch_of_nth _ _ l hsj' hsi') hiP
      rw [hd.2] at this
      exact absurd this Bool.noConfusion

/-- **Pair factorization**: a part detached from `A` and the loop integrates independently. -/
theorem pair_fact (hP : ∀ i, i < P.length → (nthL P i).length = L) (hn : n.length = L)
    (hχ : depOn (n.map nzb) χ) (hf : ∀ i, depOn (spP L P i) (f i))
    (A D : List Bool) (hA : A.length = P.length) (hD : D.length = P.length) (hdisj : disjM A D)
    (hDs : ∀ i, i < P.length → nthB D i = true → touch (nthL P i) n = false ∧ anyT P A (nthL P i) = false) :
    K.mul (pwN K (natR K els.length) L) (aW K els L f χ (orB A D)) = K.mul (aW K els L f χ A) (a0 K els L f D) := by
  have hsp : ∀ i, (spP L P i).length = L := fun i => len_padB L _
  have hsn : (n.map nzb).length = L := (len_map' n).trans hn
  let S := orB (n.map nzb) (uSupp L (spP L P) 0 A)
  let T := uSupp L (spP L P) 0 D
  have hSl : S.length = L := len_orB L _ _ hsn (len_uSupp L _ hsp 0 A)
  have hTl : T.length = L := len_uSupp L _ hsp 0 D
  have hF : depOn S (fun x => K.mul (χ x) (prodW K f 0 A x)) :=
    depOn_mul K _ _ χ (prodW K f 0 A) (hsn.trans (len_uSupp L _ hsp 0 A).symm) hχ (depOn_prodW K L _ hsp f hf 0 A)
  have hH : depOn T (prodW K f 0 D) := depOn_prodW K L _ hsp f hf 0 D
  have ind := indep K els S T _ _ (hSl.trans hTl.symm) (supp_disj L P n hP hn A D hA hD hDs) hF hH
  rw [hSl] at ind
  show K.mul _ (sumK K _ (cfgs els L)) = K.mul (sumK K _ (cfgs els L)) (sumK K _ (cfgs els L))
  rw [← ind]
  congr 1
  exact sumK_ext K _ _ (fun x => by rw [prodW_or K f x 0 A D (hA.trans hD.symm) hdisj, K.mul_assoc]) _

theorem cnt_split3 : ∀ (M X A D : List Bool), M.length = X.length → A.length = X.length → D.length = X.length →
    (∀ i, nthB A i = true → nthB X i = true) → (∀ i, nthB D i = true → nthB X i = false) →
    cnt (andB M (notB (orB A D))) = cnt (andB (andB M X) (notB A)) + cnt (andB (andB M (notB X)) (notB D))
  | [], [], [], [], _, _, _, _, _ => rfl
  | [], _ :: _, _, _, h, _, _, _, _ => absurd h Nat.noConfusion
  | _ :: _, [], _, _, h, _, _, _, _ => absurd h Nat.noConfusion
  | _ :: _, _ :: _, [], _, _, h, _, _, _ => absurd h Nat.noConfusion
  | _ :: _, _ :: _, _ :: _, [], _, _, h, _, _ => absurd h Nat.noConfusion
  | [], [], _ :: _, _, _, h, _, _, _ => absurd h Nat.noConfusion
  | [], [], [], _ :: _, _, _, h, _, _ => absurd h Nat.noConfusion
  | m :: M, x :: X, a :: A, d :: D, hM, hA, hD, hAX, hDX => by
    have ih := cnt_split3 M X A D (Nat.succ.inj hM) (Nat.succ.inj hA) (Nat.succ.inj hD)
      (fun i h => hAX (i + 1) h) (fun i h => hDX (i + 1) h)
    have k1 : a = true → x = true := hAX 0
    have k2 : d = true → x = false := hDX 0
    show cnt ((m && !(a || d)) :: andB M (notB (orB A D)))
      = cnt (((m && x) && !a) :: andB (andB M X) (notB A)) + cnt (((m && !x) && !d) :: andB (andB M (notB X)) (notB D))
    cases m <;> cases x <;> cases a <;> cases d <;>
      first
      | exact ih
      | exact congrArg (· + 1) ih
      | exact (congrArg (· + 1) ih).trans (Nat.add_right_comm _ _ _)
      | exact absurd (k1 rfl) (by decide)
      | exact absurd (k2 rfl) (by decide)

theorem le_orB_l (A D : List Bool) (h : A.length = D.length) : leB A (orB A D) :=
  leB_of _ _ (len_orB _ A D rfl h.symm).symm (fun i hi => by rw [Cluster.nth_orB A D i h, hi]; rfl)

theorem le_orB_r (A D : List Bool) (h : A.length = D.length) : leB D (orB A D) :=
  leB_of _ _ (h.symm.trans (len_orB _ A D rfl h.symm).symm) (fun i hi => by rw [Cluster.nth_orB A D i h, hi, Bool.or_true])

/-- **Characterisation of the regrouping condition.** -/
theorem cond_eq (A D : List Bool) (hA : A.length = P.length) (hD : D.length = P.length) :
    (A = clS (fun q => touch q n) P (orB A D) ∧
        D = andB (orB A D) (notB (clS (fun q => touch q n) P (orB A D))))
      ↔ (clS (fun q => touch q n) P A = A ∧ andB D (notB (Xm n P A)) = D) := by
  let σ : List Nat → Bool := fun q => touch q n
  have hS : (orB A D).length = P.length := len_orB _ A D hA hD
  constructor
  · intro h
    have hcl : clS σ P A = A := by
      have := cl_idem (Vs σ P) P.length (hV σ P) (orB A D) hS
      have e : Closure.cl (Vs σ P) P.length (orB A D) = A := h.1.symm
      rw [e] at this
      exact this
    refine ⟨hcl, ?_⟩
    have hCl : (clS σ P (orB A D)).length = P.length := cl_len (Vs σ P) P.length (hV σ P) _ hS
    refine ext_nth _ _ (len_andB _ _ (by rw [len_notB, len_Xm n P A hA, hD])) (fun i => ?_)
    rw [nth_andB]
    by_cases hi : i < P.length
    · cases eD : nthB D i with
      | false => rfl
      | true =>
        have e2 : nthB D i = nthB (andB (orB A D) (notB (clS σ P (orB A D)))) i := congrArg (fun z => nthB z i) h.2
        rw [eD, nth_andB, Cluster.nth_notB _ i (by rw [hCl]; exact hi)] at e2
        have hSi : nthB (orB A D) i = true := by
          cases e : nthB (orB A D) i
          · rw [e, Bool.false_and] at e2; exact absurd e2 (by decide)
          · rfl
        have hCi : nthB (clS σ P (orB A D)) i = false := by
          cases e : nthB (clS σ P (orB A D)) i
          · rfl
          · rw [hSi, e] at e2; exact absurd e2 (by decide)
        have hr : (σ (nthL P i) || anyT P (clS σ P (orB A D)) (nthL P i)) = false := by
          cases e : (σ (nthL P i) || anyT P (clS σ P (orB A D)) (nthL P i))
          · rfl
          · have := closed σ P (orB A D) hS i hi hSi e
            rw [hCi] at this; exact absurd this Bool.noConfusion
        have hAC : clS σ P (orB A D) = A := h.1.symm
        rw [hAC] at hr hCi
        have hr' : (touch (nthL P i) n || anyT P A (nthL P i)) = false := hr
        rw [Cluster.nth_notB _ i (by rw [len_Xm n P A hA]; exact hi), Xm, Cluster.nth_orB A _ i (hA.trans (len_map _ P).symm),
          nth_map _ P i hi, hCi]
        show (true && !(false || (touch (nthL P i) n || anyT P A (nthL P i)))) = true
        rw [hr']; rfl
    · rw [nthB_ge D i (by rw [hD]; exact Nat.le_of_not_lt hi)]; rfl
  · intro h
    have off := fun i hi hDi => off_Xm n P A D hA hD h.2 i hi hDi
    have hdisj : disjM A D := disj_of_off A D (hA.trans hD.symm) (fun i hDi => by
      by_cases hi : i < P.length
      · exact (off i hi hDi).1
      · rw [nthB_ge D i (by rw [hD]; exact Nat.le_of_not_lt hi)] at hDi; exact absurd hDi Bool.noConfusion)
    have hcu : clS σ P (orB A D) = A :=
      Cluster.cl_union σ P A D hA hD h.1 hdisj (fun i hi hDi => ⟨(off i hi hDi).2.1, (off i hi hDi).2.2⟩)
    refine ⟨hcu.symm, ?_⟩
    show D = andB (orB A D) (notB (clS σ P (orB A D)))
    rw [hcu]
    exact (Cluster.rest_eq A D (hA.trans hD.symm) hdisj).symm

theorem sub_right : ∀ (D M Y : List Bool), andB D (andB M Y) = D → andB D Y = D
  | [], _, _, _ => rfl
  | _ :: _, [], _, h => by cases h
  | _ :: _, _ :: _, [], h => by cases h
  | d :: D, m :: M, y :: Y, h => by
    have a := (Ratio.cons_iff _ _ _ _).1 h
    show (d && y) :: andB D Y = d :: D
    rw [sub_right D M Y a.2]
    have e : (d && (m && y)) = d := a.1
    cases d <;> cases m <;> cases y <;> first | rfl | exact absurd e (by decide)

/-- **Exact cluster identity** for any finite gauge group. -/
theorem cluster_id (hP : ∀ i, i < P.length → (nthL P i).length = L) (hn : n.length = L)
    (hχ : depOn (n.map nzb) χ) (hf : ∀ i, depOn (spP L P i) (f i)) (M : List Bool) (hM : M.length = P.length) :
    K.mul (pwN K (natR K els.length) L) (ZW K els L f χ c0 M)
      = sumK K (fun A => if clS (fun q => touch q n) P A = A ∧ andB A M = A then
          K.mul (K.mul (pwR K c0 (cnt (andB (andB M (Xm n P A)) (notB A)))) (aW K els L f χ A))
            (Z0 K els L f c0 (andB M (notB (Xm n P A)))) else K.zero) (bools P.length) := by
  let σ : List Nat → Bool := fun q => touch q n
  let N := P.length
  have hmem := ChangeVar2D.mem_bools_len N
  let gL := pwN K (natR K els.length) L
  let cS : List Bool → List Bool := clS σ P
  let dS : List Bool → List Bool := fun S => andB S (notB (cS S))
  have hc : ∀ S, S.length = N → (cS S).length = N := fun S hS => cl_len (Vs σ P) N (hV σ P) S hS
  have hsplit : ∀ S, S.length = N → S = orB (cS S) (dS S) ∧ disjM (cS S) (dS S) ∧ (dS S).length = S.length :=
    fun S hS => split_mask (cS S) S (cl_le (Vs σ P) N (hV σ P) S hS)
  have hd : ∀ S, S.length = N → (dS S).length = N := fun S hS => (hsplit S hS).2.2.trans hS
  let Φ : List Bool → R := fun S => K.mul gL (if andB S M = S then
    K.mul (pwR K c0 (cnt (andB M (notB S)))) (aW K els L f χ S) else K.zero)
  rw [ZW_exp, mul_sumK, hM]
  show sumK K Φ (bools N) = _
  rw [Signed.bij_sum K N cS dS Φ hc hd (fun S hS => (hsplit S hS).1)]
  refine sumK_ext_mem K _ _ (bools N) (fun A hA => ?_)
  have hAl := hmem A hA
  have hXl : (Xm n P A).length = N := len_Xm n P A hAl
  have hMX : (andB M (notB (Xm n P A))).length = N := (len_andB M _ (by rw [len_notB, hXl, hM])).trans hM
  -- per D
  have term : ∀ D, D.length = N →
      (if A = cS (orB A D) ∧ D = dS (orB A D) then Φ (orB A D) else K.zero)
        = (if clS σ P A = A ∧ andB A M = A then
            (if andB D (andB M (notB (Xm n P A))) = D then
              K.mul (K.mul (pwR K c0 (cnt (andB (andB M (Xm n P A)) (notB A)))) (aW K els L f χ A))
                (K.mul (pwR K c0 (cnt (andB (andB M (notB (Xm n P A))) (notB D)))) (a0 K els L f D))
            else K.zero) else K.zero) := by
    intro D hDl
    have ce := cond_eq P n A D hAl hDl
    have hADl : (orB A D).length = N := len_orB _ A D hAl hDl
    by_cases cnd : A = cS (orB A D) ∧ D = dS (orB A D)
    · rw [ip cnd]
      have c2 := ce.1 cnd
      have hDX : andB D (notB (Xm n P A)) = D := c2.2
      have off := fun i hi hDi => off_Xm n P A D hAl hDl hDX i hi hDi
      have hdisj : disjM A D := disj_of_off A D (hAl.trans hDl.symm) (fun i hDi => by
        by_cases hi : i < N
        · exact (off i hi hDi).1
        · rw [nthB_ge D i (by rw [hDl]; exact Nat.le_of_not_lt hi)] at hDi; exact absurd hDi Bool.noConfusion)
      show K.mul gL (if andB (orB A D) M = orB A D then _ else K.zero) = _
      by_cases cM : andB (orB A D) M = orB A D
      · have hAM : andB A M = A := sub_of_le A _ M (le_orB_l A D (hAl.trans hDl.symm)) cM
        have hDM : andB D M = D := sub_of_le D _ M (le_orB_r A D (hAl.trans hDl.symm)) cM
        rw [ip cM, ip ⟨c2.1, hAM⟩, ip (and_sub2 D M _ hDM hDX)]
        -- c0-power split
        have hAX : ∀ i, nthB A i = true → nthB (Xm n P A) i = true := fun i hi => by
          have hiN : i < N := by
            cases Nat.lt_or_ge i N with
            | inl h => exact h
            | inr h => rw [nthB_ge A i (by rw [hAl]; exact h)] at hi; exact absurd hi Bool.noConfusion
          rw [Xm, Cluster.nth_orB A _ i (hAl.trans (len_map _ P).symm), hi]; rfl
        have hDX' : ∀ i, nthB D i = true → nthB (Xm n P A) i = false := fun i hi => by
          have hiN : i < N := by
            cases Nat.lt_or_ge i N with
            | inl h => exact h
            | inr h => rw [nthB_ge D i (by rw [hDl]; exact h)] at hi; exact absurd hi Bool.noConfusion
          have o := off i hiN hi
          rw [Xm, Cluster.nth_orB A _ i (hAl.trans (len_map _ P).symm), nth_map _ P i hiN, o.1, o.2.1, o.2.2]; rfl
        have hcnt := cnt_split3 M (Xm n P A) A D (hM.trans hXl.symm) (hAl.trans hXl.symm) (hDl.trans hXl.symm) hAX hDX'
        rw [hcnt, GKS.pwR_add]
        have pf := pair_fact K els L P n f χ hP hn hχ hf A D hAl hDl hdisj (fun i hi hDi => ⟨(off i hi hDi).2.1, (off i hi hDi).2.2⟩)
        -- gL · (cA cD · aW(A∪D)) = (cA aW A)(cD a0 D)
        show K.mul gL (K.mul (K.mul (pwR K c0 _) (pwR K c0 _)) (aW K els L f χ (orB A D))) = _
        rw [← K.mul_assoc, K.mul_comm gL, K.mul_assoc, pf]
        have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
          rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
        exact mul4 _ _ _ _
      · rw [ineg cM, DetChain.mul_zero]
        by_cases cA : clS σ P A = A ∧ andB A M = A
        · rw [ip cA]
          by_cases cD : andB D (andB M (notB (Xm n P A))) = D
          · exact absurd (or_sub A D M cA.2 (Approx.andB_assoc_sub D M _ cD (hM.trans (by rw [len_notB, hXl])))) cM
          · rw [ineg cD]
        · rw [ineg cA]
    · rw [ineg cnd]
      by_cases cA : clS σ P A = A ∧ andB A M = A
      · rw [ip cA]
        by_cases cD : andB D (andB M (notB (Xm n P A))) = D
        · exact absurd (ce.2 ⟨cA.1, sub_right D M _ cD⟩) cnd
        · rw [ineg cD]
      · rw [ineg cA]
  rw [sumK_ext_mem K _ _ (bools N) (fun D hD => term D (hmem D hD))]
  by_cases cA : clS σ P A = A ∧ andB A M = A
  · rw [ip cA, Z0_exp K els L f c0 _, hMX, mul_sumK]
    refine sumK_ext K _ _ (fun D => ?_) _
    rw [ip cA]
    by_cases cD : andB D (andB M (notB (Xm n P A))) = D
    · rw [ip cD, ip cD]
    · rw [ineg cD, ineg cD, DetChain.mul_zero]
  · rw [ineg cA]
    exact (sumK_ext K _ _ (fun D => ineg cA) _).trans (sum_zero K _)

end

end Poly

#print axioms Poly.touch_of_nth
#print axioms Poly.uSupp_nth
#print axioms Poly.ZW_exp
#print axioms Poly.Z0_exp
#print axioms Poly.supp_disj
#print axioms Poly.pair_fact
#print axioms Poly.cnt_split3
#print axioms Poly.cond_eq
#print axioms Poly.cluster_id

ym/PolyBound.lean

/-
  ym/PolyBound — T45, cycle 50 (Lad2, any finite gauge group): the three bounds behind the non-abelian area law.

    cfg_count      Σ_x b over all fields = |G|^L · b
    prodW_bnd      −ε ≤ f_i ≤ ε  ⇒  −ε^|A| ≤ Π_{i∈A} f_i ≤ ε^|A|
    aW_bnd         −Kχ ≤ χ ≤ Kχ also  ⇒  −|G|^L Kχ ε^|A| ≤ aW(A) ≤ |G|^L Kχ ε^|A|
    Z0_ratio       m ≤ c₀ + f_i, κ m = 1  ⇒  Z0(M ∖ X) ≤ κ^{|M ∩ X|} Z0(M)          (PosRatio)
    cnt_Xm         |X(A)| ≤ |A| + s + Dg |A|     (s = plaquettes touching the loop, Dg = plaquettes touching one)

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Poly

namespace PolyBound

open YMChain LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Ratio Expand Approx Poly Signed

variable {R : Type} (K : DetChain.CRing R) {G : Type}

theorem cfg_count (els : List G) (b : R) : ∀ L, sumK K (fun _ => b) (cfgs els L) = K.mul (pwN K (natR K els.length) L) b
  | 0 => by show K.add b K.zero = K.mul K.one b; rw [DetChain.add_zero', K.one_mul]
  | L + 1 => by
    rw [MeanZero.split_cfg, sumK_ext K _ _ (fun g => cfg_count els b L), sum_const]
    show K.mul (natR K els.length) (K.mul (pwN K (natR K els.length) L) b)
      = K.mul (K.mul (natR K els.length) (pwN K (natR K els.length) L)) b
    rw [K.mul_assoc]

theorem cnt_orB_le : ∀ x y : List Bool, cnt (orB x y) ≤ cnt x + cnt y
  | [], _ => Nat.zero_le _
  | _ :: _, [] => Nat.zero_le _
  | a :: x, b :: y => by
    have ih := cnt_orB_le x y
    cases a <;> cases b
    · exact ih
    · exact Nat.succ_le_succ ih
    · show cnt _ + 1 ≤ cnt x + 1 + cnt y; rw [Nat.succ_add]; exact Nat.succ_le_succ ih
    · show cnt _ + 1 ≤ cnt x + 1 + (cnt y + 1); rw [Nat.succ_add]; exact Nat.succ_le_succ (Nat.le_succ_of_le ih)

theorem cnt_map_or (σ τ : List Nat → Bool) : ∀ Q : List (List Nat),
    cnt (Q.map (fun q => σ q || τ q)) ≤ cnt (Q.map σ) + cnt (Q.map τ)
  | [] => Nat.zero_le _
  | q :: Q => by
    have ih := cnt_map_or σ τ Q
    show cnt ((σ q || τ q) :: Q.map _) ≤ cnt (σ q :: Q.map σ) + cnt (τ q :: Q.map τ)
    cases σ q <;> cases τ q
    · exact ih
    · exact Nat.succ_le_succ ih
    · show cnt _ + 1 ≤ cnt _ + 1 + cnt _; rw [Nat.succ_add]; exact Nat.succ_le_succ ih
    · show cnt _ + 1 ≤ cnt _ + 1 + (cnt _ + 1); rw [Nat.succ_add]; exact Nat.succ_le_succ (Nat.le_succ_of_le ih)

theorem cnt_map_false (Q : List (List Nat)) : cnt (Q.map (fun _ => false)) = 0 := by
  induction Q with
  | nil => rfl
  | cons _ Q ih => exact ih

/-- `|{q ∈ Q : q touches some plaquette of A}| ≤ Dg |A|`. -/
theorem cnt_anyT (Q : List (List Nat)) (Dg : Nat) : ∀ (P : List (List Nat)) (A : List Bool),
    (∀ p, memL p P → cnt (Q.map (fun q => touch q p)) ≤ Dg) →
    cnt (Q.map (fun q => anyT P A q)) ≤ Dg * cnt A
  | [], A, _ => by rw [show (fun q => anyT [] A q) = (fun _ => false) from rfl, cnt_map_false]; exact Nat.zero_le _
  | _ :: _, [], _ => by
    rw [show (fun q => anyT (_ :: _) [] q) = (fun _ => false) from rfl, cnt_map_false]
    exact Nat.zero_le _
  | p :: P, a :: A, hdeg => by
    have ih := cnt_anyT Q Dg P A (fun p' hp' => hdeg p' (Or.inr hp'))
    have e : (fun q => anyT (p :: P) (a :: A) q) = (fun q => (a && touch q p) || anyT P A q) := rfl
    rw [e]
    refine Nat.le_trans (cnt_map_or (fun q => a && touch q p) (fun q => anyT P A q) Q) ?_
    cases a
    · rw [show (fun q => false && touch q p) = (fun _ => false) from rfl, cnt_map_false, Nat.zero_add]
      exact ih
    · show cnt (Q.map (fun q => true && touch q p)) + _ ≤ Dg * (cnt A + 1)
      rw [show (fun q => true && touch q p) = (fun q => touch q p) from rfl, Nat.mul_succ, Nat.add_comm (Dg * cnt A)]
      exact Nat.add_le_add (hdeg p (Or.inl rfl)) ih

theorem cnt_Xm (n : List Nat) (P : List (List Nat)) (A : List Bool) (Dg : Nat)
    (hdeg : ∀ p, memL p P → cnt (P.map (fun q => touch q p)) ≤ Dg) :
    cnt (Xm n P A) ≤ cnt A + cnt (P.map (fun q => touch q n)) + Dg * cnt A := by
  refine Nat.le_trans (cnt_orB_le _ _) ?_
  rw [Nat.add_assoc]
  exact Nat.add_le_add_left (Nat.le_trans (cnt_map_or _ _ P) (Nat.add_le_add_left (cnt_anyT P Dg P A hdeg) _)) _

variable (O : ORing K)

theorem prodW_bnd (f : Nat → List G → R) (ε : R) (he : O.le K.zero ε) (h1 : O.le K.zero K.one)
    (hf : ∀ i x, O.le (K.neg ε) (f i x) ∧ O.le (f i x) ε) (x : List G) : ∀ (k : Nat) (A : List Bool),
    O.le (K.neg (pwR K ε (cnt A))) (prodW K f k A x) ∧ O.le (prodW K f k A x) (pwR K ε (cnt A))
  | _, [] => by
    have e0 : O.le (K.neg K.one) K.one := O.le_trans _ _ _ (by
      have := neg_le_neg K O _ _ h1
      have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
      rw [e] at this; exact this) h1
    exact ⟨e0, O.le_refl _⟩
  | k, true :: A => by
    have ih := prodW_bnd f ε he h1 hf x (k + 1) A
    have b := bnd_mul K O (f k x) (prodW K f (k + 1) A x) ε (pwR K ε (cnt A)) he (AreaLower.pw_nn K O ε he h1 _) (hf k x) ih
    show O.le (K.neg (K.mul (pwR K ε (cnt A)) ε)) (K.mul (f k x) (prodW K f (k + 1) A x)) ∧
      O.le (K.mul (f k x) (prodW K f (k + 1) A x)) (K.mul (pwR K ε (cnt A)) ε)
    rw [K.mul_comm (pwR K ε (cnt A)) ε]
    exact b
  | k, false :: A => by
    have ih := prodW_bnd f ε he h1 hf x (k + 1) A
    show O.le _ (K.mul K.one _) ∧ O.le (K.mul K.one _) _
    rw [K.one_mul]; exact ih

theorem aW_bnd (els : List G) (L : Nat) (f : Nat → List G → R) (χ : List G → R) (ε Kχ : R)
    (he : O.le K.zero ε) (hK : O.le K.zero Kχ) (h1 : O.le K.zero K.one)
    (hf : ∀ i x, O.le (K.neg ε) (f i x) ∧ O.le (f i x) ε) (hχ : ∀ x, O.le (K.neg Kχ) (χ x) ∧ O.le (χ x) Kχ)
    (A : List Bool) :
    O.le (K.neg (K.mul (pwN K (natR K els.length) L) (K.mul Kχ (pwR K ε (cnt A))))) (aW K els L f χ A) ∧
      O.le (aW K els L f χ A) (K.mul (pwN K (natR K els.length) L) (K.mul Kχ (pwR K ε (cnt A)))) := by
  have bs := bnd_sum K O (fun x => K.mul (χ x) (prodW K f 0 A x)) (fun _ => K.mul Kχ (pwR K ε (cnt A))) (cfgs els L)
    (fun x _ => bnd_mul K O _ _ _ _ hK (AreaLower.pw_nn K O ε he h1 _) (hχ x) (prodW_bnd K O f ε he h1 hf x 0 A))
  rw [cfg_count] at bs
  exact bs

theorem Z0_ratio (els : List G) (L : Nat) (f : Nat → List G → R) (c0 m κ : R) (h1 : O.le K.zero K.one)
    (hm : O.le K.zero m) (hm1 : O.le m K.one) (hκ : O.le K.zero κ) (hκm : K.mul κ m = K.one)
    (hw : ∀ i x, O.le m (K.add c0 (f i x))) (hw1 : ∀ i x, O.le (K.add c0 (f i x)) K.one)
    (M X : List Bool) (hl : M.length = X.length) :
    O.le (Z0 K els L f c0 (andB M (notB X))) (K.mul (pwR K κ (cnt (andB M X))) (Z0 K els L f c0 M)) := by
  have r := (ratio_pos K O els L (fun i x => K.add c0 (f i x)) m K.one h1 hm hm1 h1 hw hw1 M X hl).1
  have r' := O.mul_le_mul_right _ _ _ r (AreaLower.pw_nn K O κ hκ h1 (cnt (andB M X)))
  have e1 : ∀ k, K.mul (pwR K m k) (pwR K κ k) = K.one := by
    intro k
    rw [← GKS2.pwR_mul, K.mul_comm m κ, hκm]
    exact pw_one' K k
  rw [K.mul_assoc, e1, K.mul_comm _ K.one, K.one_mul] at r'
  show O.le (sumK K _ _) (K.mul _ (sumK K _ _))
  rw [K.mul_comm]
  exact r'

end PolyBound

#print axioms PolyBound.cfg_count
#print axioms PolyBound.cnt_anyT
#print axioms PolyBound.cnt_Xm
#print axioms PolyBound.prodW_bnd
#print axioms PolyBound.aW_bnd
#print axioms PolyBound.Z0_ratio

ym/AreaG.lean

/-
  ym/AreaG — T45, cycle 51 (Lad2, any finite gauge group, any dimension): the area law, uniform in the volume,
  given that small polymers have zero weight.

  Hypotheses (all local):
    weights     m ≤ c₀ + f_i ≤ 1, 0 ≤ c₀ ≤ 1, κ m = 1, 1 ≤ κ;  −ε ≤ f_i ≤ ε;  −Kχ ≤ χ ≤ Kχ
    degree      every plaquette touches at most Dg plaquettes (itself included)
    smallness   ε κ^{Dg+1} ≤ u x,  x (1 + y)^Dg ≤ y,  0 ≤ u ≤ 1
    vanishing   aW(A) = 0 for every loop-connected A with |A| < m    (next cycle: from the single-cover rule)
  Then, with s = number of plaquettes touching the loop,
    area_G      −B · Z0(M) ≤ Z_W(M) ≤ B · Z0(M),   B = Kχ κ^s (1 + y)^s u^m,
  i.e. |⟨W⟩| ≤ Kχ (κ(1+y))^s u^Area — the area law (confinement) for any finite gauge group at small β.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.PolyBound
import ym.Reach

namespace AreaG

open YMChain LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Ratio Expand Approx Poly Signed
  PolyBound

variable {R : Type} (K : DetChain.CRing R) {G : Type}

theorem andB_le_r : ∀ (M X : List Bool), M.length = X.length → leB (andB M X) X
  | [], [], _ => trivial
  | [], _ :: _, h => absurd h Nat.noConfusion
  | _ :: _, [], h => absurd h Nat.noConfusion
  | m :: M, x :: X, h => ⟨fun e => by cases m <;> cases x <;> first | rfl | exact absurd e (by decide),
      andB_le_r M X (Nat.succ.inj h)⟩

theorem pw_exp_mul (z : R) (a : Nat) : ∀ k, pwR K z (a * k) = pwR K (pwR K z a) k
  | 0 => rfl
  | k + 1 => by
    show pwR K z (a * k + a) = K.mul (pwR K (pwR K z a) k) (pwR K z a)
    rw [GKS.pwR_add, pw_exp_mul z a k]

theorem natR_ge_one (O : ORing K) (h1 : O.le K.zero K.one) : ∀ n, 1 ≤ n → O.le K.one (natR K n)
  | 0, h => absurd h (by decide)
  | 1, _ => by show O.le K.one (K.add K.zero K.one); rw [K.zero_add]; exact O.le_refl _
  | n + 2, _ => by
    have ih := natR_ge_one O h1 (n + 1) (Nat.succ_le_succ (Nat.zero_le _))
    have := O.add_le_add _ _ _ _ ih h1
    rw [DetChain.add_zero'] at this
    exact this

theorem pwN_ge_one (O : ORing K) (h1 : O.le K.zero K.one) (z : R) (hz : O.le K.one z) : ∀ L, O.le K.one (pwN K z L)
  | 0 => O.le_refl _
  | L + 1 => GPerm.one_le_mul K O z _ hz (pwN_ge_one O h1 z hz L) h1

variable (O : ORing K)

theorem pw_base_mono (h1 : O.le K.zero K.one) (a b : R) (ha : O.le K.zero a) (hab : O.le a b) :
    ∀ k, O.le (pwR K a k) (pwR K b k)
  | 0 => O.le_refl _
  | k + 1 => Gap.mul_le_mul4 K O _ _ _ _ (AreaLower.pw_nn K O a ha h1 k) ha (O.le_trans _ _ _ ha hab)
      (pw_base_mono h1 a b ha hab k) hab

/-- **Area law for a finite gauge group** (two-sided, uniform in the volume). -/
theorem area_G (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (els : List G) (hels : 1 ≤ els.length) (L : Nat) (P : List (List Nat)) (n : List Nat)
    (f : Nat → List G → R) (χ : List G → R) (c0 m κ ε Kχ u x y : R) (Dg mA : Nat)
    (hP : ∀ i, i < P.length → (nthL P i).length = L) (hn : n.length = L)
    (hχd : depOn (n.map nzb) χ) (hfd : ∀ i, depOn (spP L P i) (f i))
    (hc0 : O.le K.zero c0) (hc1 : O.le c0 K.one) (hm : O.le K.zero m) (hm1 : O.le m K.one)
    (hκ1 : O.le K.one κ) (hκm : K.mul κ m = K.one)
    (hw : ∀ i x, O.le m (K.add c0 (f i x))) (hw1 : ∀ i x, O.le (K.add c0 (f i x)) K.one)
    (he : O.le K.zero ε) (hf : ∀ i x, O.le (K.neg ε) (f i x) ∧ O.le (f i x) ε)
    (hK : O.le K.zero Kχ) (hχ : ∀ x, O.le (K.neg Kχ) (χ x) ∧ O.le (χ x) Kχ)
    (hu0 : O.le K.zero u) (hu1 : O.le u K.one) (hx : O.le K.zero x) (hy : O.le K.zero y)
    (hdeg : ∀ p, memL p P → cnt (P.map (fun q => touch q p)) ≤ Dg)
    (hsmall : O.le (K.mul ε (pwR K κ (Dg + 1))) (K.mul u x))
    (hxy : O.le (K.mul x (pwR K (K.add K.one y) Dg)) y)
    (hvan : ∀ A : List Bool, A.length = P.length → clS (fun q => touch q n) P A = A → cnt A < mA →
      aW K els L f χ A = K.zero)
    (M : List Bool) (hM : M.length = P.length) :
    let B := K.mul (K.mul Kχ (pwR K κ (cnt (P.map (fun q => touch q n)))))
      (K.mul (pwR K (K.add K.one y) (cnt (P.map (fun q => touch q n)))) (pwR K u mA))
    O.le (K.neg (K.mul B (Z0 K els L f c0 M))) (ZW K els L f χ c0 M) ∧
      O.le (ZW K els L f χ c0 M) (K.mul B (Z0 K els L f c0 M)) := by
  intro B
  let σ : List Nat → Bool := fun q => touch q n
  let N := P.length
  let s := cnt (P.map σ)
  have hmem := ChangeVar2D.mem_bools_len N
  let gL := pwN K (natR K els.length) L
  have hg1 : O.le K.one gL := pwN_ge_one K O h1 _ (natR_ge_one K O h1 _ hels) L
  have hg0 : O.le K.zero gL := O.le_trans _ _ _ h1 hg1
  have hgne : gL ≠ K.zero := fun hz => by rw [hz] at hg1; exact h10 (O.le_antisymm _ _ hg1 h1)
  have hκ : O.le K.zero κ := O.le_trans _ _ _ h1 hκ1
  have Z0nn : ∀ Q : List Bool, O.le K.zero (Z0 K els L f c0 Q) := fun Q => by
    have h := MaskSum.sumK_le K O (fun _ => K.zero) (fun x' => prodW K (fun i x => K.add c0 (f i x)) 0 Q x') (cfgs els L)
      (fun x' _ => prodW_nn K O (fun i x => K.add c0 (f i x)) h1 (fun i x => O.le_trans _ _ _ hm (hw i x)) x' 0 Q)
    rw [sum_zero] at h
    exact h
  let Z := Z0 K els L f c0 M
  -- per-polymer bound
  let bA : List Bool → R := fun A => if clS σ P A = A then
    K.mul (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K u mA) (K.mul (pwR K x (cnt A)) Z)) else K.zero
  have term_b : ∀ A, A.length = N →
      O.le (K.neg (bA A)) (if clS σ P A = A ∧ andB A M = A then
          K.mul (K.mul (pwR K c0 (cnt (andB (andB M (Xm n P A)) (notB A)))) (aW K els L f χ A))
            (Z0 K els L f c0 (andB M (notB (Xm n P A)))) else K.zero) ∧
        O.le (if clS σ P A = A ∧ andB A M = A then
          K.mul (K.mul (pwR K c0 (cnt (andB (andB M (Xm n P A)) (notB A)))) (aW K els L f χ A))
            (Z0 K els L f c0 (andB M (notB (Xm n P A)))) else K.zero) (bA A) := by
    intro A hA
    have bnn : O.le K.zero (bA A) := by
      show O.le K.zero (if _ then _ else _)
      by_cases c : clS σ P A = A
      · rw [ip c]
        exact O.mul_nonneg _ _ (O.mul_nonneg _ _ hg0 (O.mul_nonneg _ _ hK (AreaLower.pw_nn K O κ hκ h1 _)))
          (O.mul_nonneg _ _ (AreaLower.pw_nn K O u hu0 h1 _) (O.mul_nonneg _ _ (AreaLower.pw_nn K O x hx h1 _) (Z0nn M)))
      · rw [ineg c]; exact O.le_refl _
    have zero_ok : O.le (K.neg (bA A)) K.zero ∧ O.le K.zero (bA A) := ⟨by
      have := neg_le_neg K O _ _ bnn
      have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
      rw [e] at this; exact this, bnn⟩
    by_cases cA : clS σ P A = A ∧ andB A M = A
    · rw [ip cA]
      by_cases small : cnt A < mA
      · rw [hvan A hA cA.1 small, DetChain.mul_zero, DetChain.zero_mul']; exact zero_ok
      · have hmA : mA ≤ cnt A := Nat.le_of_not_lt small
        -- factors
        let k := cnt A
        let p := cnt (andB (andB M (Xm n P A)) (notB A))
        have cp : O.le (K.neg K.one) (pwR K c0 p) ∧ O.le (pwR K c0 p) K.one := ⟨O.le_trans _ _ _ (by
            have := neg_le_neg K O _ _ h1
            have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
            rw [e] at this; exact this) (AreaLower.pw_nn K O c0 hc0 h1 p), Tail.pw_le_one K O h1 c0 hc0 hc1 p⟩
        have aB := aW_bnd K O els L f χ ε Kχ he hK h1 hf hχ A
        have hXl : M.length = (Xm n P A).length := hM.trans (len_Xm n P A hA).symm
        have zr := Z0_ratio K O els L f c0 m κ h1 hm hm1 hκ hκm hw hw1 M (Xm n P A) hXl
        -- κ^{|M∩X|} ≤ κ^{s + (Dg+1) k}
        have hcX : cnt (andB M (Xm n P A)) ≤ s + (Dg + 1) * k := by
          refine Nat.le_trans (leB_cnt _ _ (andB_le_r M _ hXl)) (Nat.le_trans (cnt_Xm n P A Dg hdeg) (Nat.le_of_eq ?_))
          show cnt A + s + Dg * cnt A = s + (Dg + 1) * cnt A
          rw [Nat.succ_mul, Nat.add_comm (cnt A) s, Nat.add_assoc, Nat.add_comm (cnt A)]
        obtain ⟨j, hj⟩ := Nat.le.dest hcX
        have kp : O.le (pwR K κ (cnt (andB M (Xm n P A)))) (pwR K κ (s + (Dg + 1) * k)) := by
          rw [← hj]; exact pw_mono K O h1 κ hκ1 _ j
        have z' : O.le (Z0 K els L f c0 (andB M (notB (Xm n P A)))) (K.mul (pwR K κ (s + (Dg + 1) * k)) Z) :=
          O.le_trans _ _ _ zr (O.mul_le_mul_right _ _ _ kp (Z0nn M))
        have zb : O.le (K.neg (K.mul (pwR K κ (s + (Dg + 1) * k)) Z)) (Z0 K els L f c0 (andB M (notB (Xm n P A)))) ∧
            O.le (Z0 K els L f c0 (andB M (notB (Xm n P A)))) (K.mul (pwR K κ (s + (Dg + 1) * k)) Z) :=
          ⟨O.le_trans _ _ _ (by
            have := neg_le_neg K O _ _ (O.mul_nonneg _ _ (AreaLower.pw_nn K O κ hκ h1 (s + (Dg + 1) * k)) (Z0nn M))
            have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
            rw [e] at this; exact this) (Z0nn _), z'⟩
        have b1 := bnd_mul K O _ _ _ _ h1 (O.mul_nonneg _ _ hg0 (O.mul_nonneg _ _ hK (AreaLower.pw_nn K O ε he h1 _))) cp aB
        rw [K.one_mul] at b1
        have b2 := bnd_mul K O _ _ _ _ (O.mul_nonneg _ _ hg0 (O.mul_nonneg _ _ hK (AreaLower.pw_nn K O ε he h1 _)))
          (O.mul_nonneg _ _ (AreaLower.pw_nn K O κ hκ h1 _) (Z0nn M)) b1 zb
        -- compare the bound with bA A
        have hb : O.le (K.mul (K.mul gL (K.mul Kχ (pwR K ε k))) (K.mul (pwR K κ (s + (Dg + 1) * k)) Z)) (bA A) := by
          show O.le _ (if clS σ P A = A then _ else _)
          rw [ip cA.1]
          -- ε^k κ^{s+(Dg+1)k} = κ^s (ε κ^{Dg+1})^k ≤ κ^s (u x)^k ≤ κ^s u^m x^k
          have e1 : K.mul (pwR K ε k) (pwR K κ (s + (Dg + 1) * k))
              = K.mul (pwR K κ s) (pwR K (K.mul ε (pwR K κ (Dg + 1))) k) := by
            rw [GKS.pwR_add, pw_exp_mul K κ (Dg + 1) k, GKS2.pwR_mul, ← K.mul_assoc, K.mul_comm (pwR K ε k) (pwR K κ s),
              K.mul_assoc]
          have t1 : O.le (pwR K (K.mul ε (pwR K κ (Dg + 1))) k) (K.mul (pwR K u mA) (pwR K x k)) :=
            O.le_trans _ _ _ (pw_base_mono K O h1 _ _ (O.mul_nonneg _ _ he (AreaLower.pw_nn K O κ hκ h1 _)) hsmall k)
              (Tail.pw_tail K O h1 u x hu0 hu1 hx mA k hmA)
          have lc : ∀ q e W : R, K.mul q (K.mul e W) = K.mul e (K.mul q W) := fun q e W => by
            rw [← K.mul_assoc, K.mul_comm q e, K.mul_assoc]
          have lhs : K.mul (K.mul gL (K.mul Kχ (pwR K ε k))) (K.mul (pwR K κ (s + (Dg + 1) * k)) Z)
              = K.mul (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K (K.mul ε (pwR K κ (Dg + 1))) k) Z) := by
            rw [GKS.pwR_add, pw_exp_mul K κ (Dg + 1) k, GKS2.pwR_mul]
            rw [K.mul_assoc gL, K.mul_assoc Kχ, K.mul_assoc (pwR K κ s), K.mul_assoc gL, K.mul_assoc Kχ,
              K.mul_assoc (pwR K ε k), lc (pwR K κ s) (pwR K ε k)]
          rw [lhs, K.mul_comm (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K (K.mul ε (pwR K κ (Dg + 1))) k) Z),
            K.mul_comm (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K u mA) (K.mul (pwR K x k) Z))]
          refine O.mul_le_mul_right _ _ _ ?_ (O.mul_nonneg _ _ hg0 (O.mul_nonneg _ _ hK (AreaLower.pw_nn K O κ hκ h1 _)))
          rw [← K.mul_assoc]
          exact O.mul_le_mul_right _ _ _ t1 (Z0nn M)
        exact ⟨O.le_trans _ _ _ (neg_le_neg K O _ _ hb) b2.1, O.le_trans _ _ _ b2.2 hb⟩
    · rw [ineg cA]; exact zero_ok
  -- sum
  have cid := cluster_id K els L P n f χ c0 hP hn hχd hfd M hM
  have bs := bnd_sum K O _ bA (bools N) (fun A hA => term_b A (hmem A hA))
  rw [← cid] at bs
  -- Σ bA = gL Kχ κ^s u^m Z · D(x) ≤ gL · B · Z
  have sb : O.le (sumK K bA (bools N)) (K.mul gL (K.mul B Z)) := by
    have d := D_bound K O h1 x y hx hy P Dg (fun v hv => hdeg _ (GPerm.memL_nthL P v hv)) hxy N σ (AreaUpper.tr N)
      (AreaUpper.len_tr _) (by rw [AreaUpper.cnt_tr]; exact Nat.le_refl _)
    have etr' : andB (AreaUpper.tr N) (P.map σ) = P.map σ := by
      have := AreaUpper.andB_tr' (P.map σ); rw [len_map] at this; exact this
    rw [etr'] at d
    have eD : sumK K bA (bools N) = K.mul (K.mul (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K u mA) Z)) (D K x σ P (AreaUpper.tr N)) := by
      show _ = K.mul _ (sumK K _ (bools N))
      rw [mul_sumK]
      refine sumK_ext_mem K _ _ (bools N) (fun A hA => ?_)
      have hAl := hmem A hA
      have etr : andB A (AreaUpper.tr N) = A := by rw [← hAl]; exact AreaUpper.andB_tr A
      show (if clS σ P A = A then _ else _) = K.mul _ (if andB A (AreaUpper.tr N) = A ∧ clS σ P A = A then _ else _)
      by_cases c : clS σ P A = A
      · rw [ip c, ip ⟨etr, c⟩]
        rw [K.mul_assoc (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K u mA) Z), K.mul_assoc (pwR K u mA) Z,
          K.mul_comm Z (pwR K x (cnt A))]
      · rw [ineg c, ineg (fun h => c h.2), DetChain.mul_zero]
    rw [eD]
    have hC : O.le K.zero (K.mul (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K u mA) Z)) :=
      O.mul_nonneg _ _ (O.mul_nonneg _ _ hg0 (O.mul_nonneg _ _ hK (AreaLower.pw_nn K O κ hκ h1 s)))
        (O.mul_nonneg _ _ (AreaLower.pw_nn K O u hu0 h1 mA) (Z0nn M))
    have := O.mul_le_mul_right _ _ _ d hC
    rw [K.mul_comm (D K x σ P (AreaUpper.tr N)), K.mul_comm (pwR K (K.add K.one y) s)] at this
    have r5 : ∀ a b c z e : R, K.mul (K.mul (K.mul a b) (K.mul c z)) e = K.mul a (K.mul (K.mul b (K.mul e c)) z) :=
      fun a b c z e => by
        rw [K.mul_assoc (K.mul a b), K.mul_assoc a, K.mul_assoc b (K.mul e c) z, K.mul_assoc e c z, K.mul_comm e (K.mul c z)]
    refine O.le_trans _ _ _ this ?_
    rw [r5]
    exact O.le_refl _
  -- cancel |G|^L
  have up : O.le (K.mul (ZW K els L f χ c0 M) gL) (K.mul (K.mul B Z) gL) := by
    rw [K.mul_comm _ gL, K.mul_comm _ gL]; exact O.le_trans _ _ _ bs.2 sb
  have lo : O.le (K.mul (K.neg (K.mul B Z)) gL) (K.mul (ZW K els L f χ c0 M) gL) := by
    rw [K.mul_comm _ gL, K.mul_comm _ gL, K.mul_comm gL (K.neg _), neg_mul', K.mul_comm (K.mul B Z)]
    exact O.le_trans _ _ _ (neg_le_neg K O _ _ sb) bs.1
  exact ⟨O.pos_cancel _ _ _ hg0 hgne lo, O.pos_cancel _ _ _ hg0 hgne up⟩

end AreaG

#print axioms AreaG.area_G

ym/Vanish.lean

/-
  ym/Vanish — T45, cycle 52 (Lad2, any finite gauge group): polymers with a singly covered link have zero weight.

    prodW_pick       i ∈ A  ⇒  Π_{j∈A} f_j = f_i · Π_{j∈A∖i} f_j
    vanish_plaq      link l lies in plaquette i ∈ A, in no other plaquette of A and not in the loop, and f_i has
                     mean zero along l  ⇒  aW(A) = 0
    vanish_loop      link l lies in the loop and in no plaquette of A, and χ has mean zero along l  ⇒  aW(A) = 0
  So aW(A) ≠ 0 forces every link of A ∪ W to be covered at least twice; the least size of such an A is the area of W.
  With AreaG.area_G this gives the area law for every finite gauge group with only the double-cover count assumed.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AreaG

namespace Vanish

open YMChain LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Ratio Expand Approx Poly Signed
  PolyBound MeanZero

variable {R : Type} (K : DetChain.CRing R) {G : Type}

theorem prodW_pick (f : Nat → List G → R) (x : List G) : ∀ (k : Nat) (A : List Bool) (i : Nat), i < A.length →
    nthB A i = true → prodW K f k A x = K.mul (f (k + i) x) (prodW K f k (upd A i false) x)
  | _, [], _, h, _ => absurd h (Nat.not_lt_zero _)
  | k, true :: A, 0, _, _ => by
    show K.mul (f k x) (prodW K f (k + 1) A x) = K.mul (f (k + 0) x) (K.mul K.one (prodW K f (k + 1) A x))
    rw [K.one_mul]; rfl
  | _, false :: _, 0, _, h => absurd h Bool.noConfusion
  | k, a :: A, i + 1, hi, h => by
    show K.mul (if a = true then f k x else K.one) (prodW K f (k + 1) A x)
      = K.mul (f (k + (i + 1)) x) (K.mul (if a = true then f k x else K.one) (prodW K f (k + 1) (upd A i false) x))
    rw [prodW_pick f x (k + 1) A i (Nat.lt_of_succ_lt_succ hi) h, Nat.succ_add, ← Nat.add_succ]
    rw [← K.mul_assoc, K.mul_comm (if a = true then f k x else K.one), K.mul_assoc]

theorem nth_upd' (x : List Bool) (v : Nat) (b : Bool) (i : Nat) (hv : v < x.length) (hiv : i ≠ v) :
    nthB (upd x v b) i = nthB x i := by
  rw [nth_upd x v b i hv, ineg hiv]

/-- **A plaquette alone on a link kills the polymer.** -/
theorem vanish_plaq (els : List G) (L : Nat) (P : List (List Nat)) (n : List Nat) (f : Nat → List G → R)
    (χ : List G → R) (hn : n.length = L)
    (hχd : depOn (n.map nzb) χ) (hfd : ∀ i, depOn (spP L P i) (f i))
    (A : List Bool) (hA : A.length = P.length) (i l : Nat) (hi : i < P.length) (hAi : nthB A i = true) (hl : l < L)
    (hnl : nthB (n.map nzb) l = false)
    (hother : ∀ j, j < P.length → j ≠ i → nthB A j = true → nthB (spP L P j) l = false)
    (hmz : ∀ c, sumK K (fun g => f i (setG c l g)) els = K.zero) :
    aW K els L f χ A = K.zero := by
  have hsp : ∀ j, (spP L P j).length = L := fun j => len_padB L _
  have hsn : (n.map nzb).length = L := (len_map' n).trans hn
  let A' := upd A i false
  let T := orB (n.map nzb) (uSupp L (spP L P) 0 A')
  have hT : nthB T l = false := by
    show nthB (orB (n.map nzb) (uSupp L (spP L P) 0 A')) l = false
    rw [Cluster.nth_orB _ _ l (hsn.trans (len_uSupp L _ hsp 0 A').symm), hnl, Bool.false_or]
    cases e : nthB (uSupp L (spP L P) 0 A') l with
    | false => rfl
    | true =>
      obtain ⟨j, hj, hA'j, hsj⟩ := uSupp_nth L (spP L P) hsp l 0 A' e
      rw [Nat.zero_add] at hsj
      have hjP : j < P.length := by rw [← hA, ← len_upd A i false]; exact hj
      have hji : j ≠ i := fun e' => by
        rw [e'] at hA'j
        show False
        have : nthB (upd A i false) i = false := by rw [nth_upd A i false i (by rw [hA]; exact hi), ip rfl]
        rw [this] at hA'j; exact Bool.noConfusion hA'j
      have hAj : nthB A j = true := by rw [← nth_upd' A i false j (by rw [hA]; exact hi) hji]; exact hA'j
      rw [hother j hjP hji hAj] at hsj
      exact absurd hsj Bool.noConfusion
  have hH : depOn T (fun x => K.mul (χ x) (prodW K f 0 A' x)) :=
    depOn_mul K _ _ χ _ (hsn.trans (len_uSupp L _ hsp 0 A').symm) hχd (depOn_prodW K L _ hsp f hfd 0 A')
  have sc := single_cover K els L l T (f i) (fun x => K.mul (χ x) (prodW K f 0 A' x)) hl hT hH hmz
  show sumK K (fun x => K.mul (χ x) (prodW K f 0 A x)) (cfgs els L) = K.zero
  rw [← sc]
  refine sumK_ext K _ _ (fun x => ?_) _
  rw [prodW_pick K f x 0 A i (by rw [hA]; exact hi) hAi, Nat.zero_add, ← K.mul_assoc, K.mul_comm (χ x), K.mul_assoc]

/-- **The loop alone on a link kills the polymer.** -/
theorem vanish_loop (els : List G) (L : Nat) (P : List (List Nat)) (f : Nat → List G → R)
    (χ : List G → R) (hfd : ∀ i, depOn (spP L P i) (f i))
    (A : List Bool) (hA : A.length = P.length) (l : Nat) (hl : l < L)
    (hnone : ∀ j, j < P.length → nthB A j = true → nthB (spP L P j) l = false)
    (hmz : ∀ c, sumK K (fun g => χ (setG c l g)) els = K.zero) :
    aW K els L f χ A = K.zero := by
  have hsp : ∀ j, (spP L P j).length = L := fun j => len_padB L _
  let T := uSupp L (spP L P) 0 A
  have hT : nthB T l = false := by
    cases e : nthB (uSupp L (spP L P) 0 A) l with
    | false => rfl
    | true =>
      obtain ⟨j, hj, hAj, hsj⟩ := uSupp_nth L (spP L P) hsp l 0 A e
      rw [Nat.zero_add, hnone j (by rw [← hA]; exact hj) hAj] at hsj
      exact absurd hsj Bool.noConfusion
  exact single_cover K els L l T χ (prodW K f 0 A) hl hT (depOn_prodW K L _ hsp f hfd 0 A) hmz

end Vanish

#print axioms Vanish.prodW_pick
#print axioms Vanish.vanish_plaq
#print axioms Vanish.vanish_loop

ym/Dup.lean

/-
  ym/Dup — T45, cycle 53 (Lad2, any finite gauge group): duplicated fields.

  Two independent copies of the gauge field = one field with values in G × G. els2 = all pairs.
    pairs, fstL, sndL      the pair list and the two projections of a pair field
    dup_factor             Σ_{y ∈ fields(G×G)} φ(fst y) ψ(snd y) = (Σ_x φ x)(Σ_x ψ x)
    swapL, dup_swap        Σ_y Φ(swap y) = Σ_y Φ(y)                    (the two copies are exchangeable)
    dup_anti               Φ(swap y) = −Φ(y) for all y  ⇒  Σ_y Φ(y) = 0
  With these, ⟨U_pU_q⟩ − ⟨U_p⟩⟨U_q⟩ = ½ ⟨(U_p − U_p')(U_q − U_q')⟩ in the duplicated system, whose polymers that
  do not connect p and q vanish by antisymmetry (next cycles).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Vanish

namespace Dup

open YMChain LinkFactor MaskSum MeanZero Signed

variable {R : Type} (K : DetChain.CRing R) {G : Type}

def pairs (els : List G) : List (G × G) := joinL (els.map (fun a => els.map (fun b => (a, b))))

def fstL : List (G × G) → List G := List.map Prod.fst
def sndL : List (G × G) → List G := List.map Prod.snd
def swapL : List (G × G) → List (G × G) := List.map (fun p => (p.2, p.1))

theorem sum_pairs (els : List G) (h : G × G → R) :
    sumK K h (pairs els) = sumK K (fun a => sumK K (fun b => h (a, b)) els) els := by
  unfold pairs
  rw [sumK_join, LinkFactor.sumK_map]
  exact sumK_ext K _ _ (fun a => LinkFactor.sumK_map K h _ _) els

/-- **The two copies are independent.** -/
theorem dup_factor (els : List G) : ∀ (L : Nat) (φ ψ : List G → R),
    sumK K (fun y => K.mul (φ (fstL y)) (ψ (sndL y))) (cfgs (pairs els) L)
      = K.mul (sumK K φ (cfgs els L)) (sumK K ψ (cfgs els L))
  | 0, φ, ψ => by
    show K.add (K.mul (φ []) (ψ [])) K.zero = K.mul (K.add (φ []) K.zero) (K.add (ψ []) K.zero)
    rw [DetChain.add_zero', DetChain.add_zero', DetChain.add_zero']
  | L + 1, φ, ψ => by
    rw [split_cfg, split_cfg, split_cfg, sum_pairs]
    show sumK K (fun a => sumK K (fun b => sumK K (fun c =>
        K.mul (φ (a :: fstL c)) (ψ (b :: sndL c))) (cfgs (pairs els) L)) els) els = _
    rw [sumK_ext K _ _ (fun a => sumK_ext K _ _ (fun b => dup_factor els L (fun c => φ (a :: c)) (fun c => ψ (b :: c))) els),
      sumK_mul]
    exact sumK_ext K _ _ (fun a => (mul_sumK K _ _ _).symm) els

/-- **Exchangeability.** -/
theorem dup_swap (els : List G) : ∀ (L : Nat) (Φ : List (G × G) → R),
    sumK K (fun y => Φ (swapL y)) (cfgs (pairs els) L) = sumK K Φ (cfgs (pairs els) L)
  | 0, Φ => rfl
  | L + 1, Φ => by
    rw [split_cfg, split_cfg, sum_pairs, sum_pairs]
    show sumK K (fun a => sumK K (fun b => sumK K (fun c => Φ ((b, a) :: swapL c)) (cfgs (pairs els) L)) els) els = _
    rw [sumK_ext K _ _ (fun a => sumK_ext K _ _ (fun b => dup_swap els L (fun c => Φ ((b, a) :: c))) els), sumK_swap]

theorem two_zero (O : ORing K) (S : R) (h : K.add S S = K.zero) : S = K.zero := by
  cases O.le_total K.zero S with
  | inl hs =>
    have := O.add_le_add _ _ _ _ (O.le_refl S) hs
    rw [DetChain.add_zero', h] at this
    exact O.le_antisymm _ _ this hs
  | inr hs =>
    have := O.add_le_add _ _ _ _ (O.le_refl S) hs
    rw [DetChain.add_zero', h] at this
    exact O.le_antisymm _ _ hs this

/-- **Antisymmetric observables have zero integral in the duplicated system.** -/
theorem dup_anti (O : ORing K) (els : List G) (L : Nat) (Φ : List (G × G) → R) (h : ∀ y, Φ (swapL y) = K.neg (Φ y)) :
    sumK K Φ (cfgs (pairs els) L) = K.zero := by
  have e := dup_swap K els L Φ
  rw [sumK_ext K _ _ h, WilsonBound.sumK_neg] at e
  refine two_zero K O _ ?_
  calc K.add (sumK K Φ (cfgs (pairs els) L)) (sumK K Φ (cfgs (pairs els) L))
      = K.add (sumK K Φ (cfgs (pairs els) L)) (K.neg (sumK K Φ (cfgs (pairs els) L))) := by rw [e]
    _ = K.zero := K.add_neg _

end Dup

#print axioms Dup.sum_pairs
#print axioms Dup.dup_factor
#print axioms Dup.dup_swap
#print axioms Dup.dup_anti

ym/Join.lean

/-
  ym/Join — T45, cycle 54 (Lad2, any finite gauge group): in the duplicated system, polymers that do not join p and q
  have zero weight.

  Duplicated fields y (values in G × G), plaquette fluctuations f_i symmetric under exchange of the copies, observables
  F_p, F_q antisymmetric (F_p = U_p − U_p′), depending on the links of p resp. q, with p and q sharing no link.
  A_p = closure of A from the plaquettes touching p (Seeds.clS), A_q = A ∖ A_p.
    disjN_nth          disjoint link vectors have disjoint supports, pointwise
    prodW_swap         symmetric f_i ⇒ Π_A f is symmetric
    len_pairs          G nonempty ⇒ G × G nonempty
    vanish_join        if no plaquette of A_p touches q, then aW(A) = 0 for χ = F_p F_q
  (A_q is detached from p and A_p by the closure fixpoint; the p-part integrates to 0 by antisymmetry, Dup.dup_anti.)

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Dup

namespace Join

open YMChain LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Dup AreaG

variable {R : Type} (K : DetChain.CRing R) {G : Type}

theorem disjN_nth : ∀ (p q : List Nat) (l : Nat), disjN p q → nthB (p.map nzb) l = true → nthB (q.map nzb) l = false
  | [], _, _, _, h => absurd h Bool.noConfusion
  | _ :: _, [], _, _, _ => rfl
  | a :: p, b :: q, 0, h, hp => by
    show nzb b = false
    cases h.1 with
    | inl ha => rw [ha] at hp; exact absurd hp Bool.noConfusion
    | inr hb => rw [hb]; rfl
  | _ :: p, _ :: q, l + 1, h, hp => disjN_nth p q l h.2 hp

theorem prodW_swap (f : Nat → List (G × G) → R) (hfs : ∀ i y, f i (swapL y) = f i y) (y : List (G × G)) :
    ∀ (k : Nat) (A : List Bool), prodW K f k A (swapL y) = prodW K f k A y
  | _, [] => rfl
  | k, a :: A => by
    show K.mul (if a = true then f k (swapL y) else K.one) (prodW K f (k + 1) A (swapL y))
      = K.mul (if a = true then f k y else K.one) (prodW K f (k + 1) A y)
    rw [hfs, prodW_swap f hfs y (k + 1) A]

theorem len_pairs (els : List G) (h : 1 ≤ els.length) : 1 ≤ (pairs els).length := by
  cases els with
  | nil => exact absurd h (Nat.not_succ_le_zero 0)
  | cons a rest =>
    show 1 ≤ ((a, a) :: (rest.map (fun b => (a, b)) ++ joinL (rest.map (fun a' => (a :: rest).map (fun b => (a', b)))))).length
    exact Nat.succ_le_succ (Nat.zero_le _)

variable (O : ORing K)

/-- **Polymers that do not join p and q vanish** (duplicated system). -/
theorem vanish_join (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (els : List G) (hels : 1 ≤ els.length)
    (L : Nat) (P : List (List Nat)) (hP : ∀ i, i < P.length → (nthL P i).length = L)
    (p q : List Nat) (hpL : p.length = L) (hqL : q.length = L) (hpq : disjN p q)
    (f : Nat → List (G × G) → R) (Fp Fq : List (G × G) → R)
    (hfd : ∀ i, depOn (spP L P i) (f i)) (hFp : depOn (p.map nzb) Fp) (hFq : depOn (q.map nzb) Fq)
    (hfs : ∀ i y, f i (swapL y) = f i y) (hFpa : ∀ y, Fp (swapL y) = K.neg (Fp y))
    (A : List Bool) (hA : A.length = P.length)
    (hnj : ∀ i, i < P.length → nthB (clS (fun r => touch r p) P A) i = true → touch (nthL P i) q = false) :
    aW K (pairs els) L f (fun y => K.mul (Fp y) (Fq y)) A = K.zero := by
  let σp : List Nat → Bool := fun r => touch r p
  let N := P.length
  let Ap := clS σp P A
  let Aq := andB A (notB Ap)
  have hsp : ∀ i, (spP L P i).length = L := fun i => len_padB L _
  have hApl : Ap.length = N := cl_len (Vs σp P) N (hV σp P) A hA
  have hspl := split_mask Ap A (cl_le (Vs σp P) N (hV σp P) A hA)
  have hAql : Aq.length = N := hspl.2.2.trans hA
  have hpn : (p.map nzb).length = L := (len_map' p).trans hpL
  have hqn : (q.map nzb).length = L := (len_map' q).trans hqL
  -- plaquettes of A_q touch neither p nor A_p
  have hoff : ∀ j, j < N → nthB Aq j = true → touch (nthL P j) p = false ∧ anyT P Ap (nthL P j) = false := by
    intro j hj hq'
    have e : nthB (andB A (notB Ap)) j = true := hq'
    rw [nth_andB, Cluster.nth_notB _ j (by rw [hApl]; exact hj)] at e
    cases eA : nthB A j <;> cases eP : nthB Ap j <;> rw [eA, eP] at e
    · exact absurd e Bool.noConfusion
    · exact absurd e Bool.noConfusion
    · cases er : (σp (nthL P j) || anyT P (clS σp P A) (nthL P j)) with
      | true =>
        have := closed σp P A hA j hj eA er
        rw [show clS σp P A = Ap from rfl, eP] at this; exact absurd this Bool.noConfusion
      | false =>
        have hf' : (touch (nthL P j) p || anyT P Ap (nthL P j)) = false := er
        cases e1 : touch (nthL P j) p with
        | true => rw [e1] at hf'; exact absurd hf' Bool.noConfusion
        | false => rw [e1, Bool.false_or] at hf'; exact ⟨rfl, hf'⟩
    · exact absurd e Bool.noConfusion
  -- the two supports
  let S := orB (p.map nzb) (uSupp L (spP L P) 0 Ap)
  let T := orB (q.map nzb) (uSupp L (spP L P) 0 Aq)
  have hSl : S.length = L := len_orB L _ _ hpn (len_uSupp L _ hsp 0 Ap)
  have hTl : T.length = L := len_orB L _ _ hqn (len_uSupp L _ hsp 0 Aq)
  have spnth : ∀ i l, i < N → nthB (spP L P i) l = true → nthB ((nthL P i).map nzb) l = true := fun i l hi h => by
    unfold spP at h; rw [padB_eq L _ ((len_map' _).trans (hP i hi))] at h; exact h
  have hST : disjM S T := by
    refine disj_of_off S T (hSl.trans hTl.symm) (fun l hl => ?_)
    show nthB (orB (p.map nzb) (uSupp L (spP L P) 0 Ap)) l = false
    rw [Cluster.nth_orB _ _ l (hpn.trans (len_uSupp L _ hsp 0 Ap).symm)]
    have hT' : nthB (orB (q.map nzb) (uSupp L (spP L P) 0 Aq)) l = true := hl
    rw [Cluster.nth_orB _ _ l (hqn.trans (len_uSupp L _ hsp 0 Aq).symm)] at hT'
    cases eq : nthB (q.map nzb) l with
    | true =>
      -- l is a link of q
      have hpl : nthB (p.map nzb) l = false := by
        cases e : nthB (p.map nzb) l
        · rfl
        · rw [disjN_nth p q l hpq e] at eq; exact absurd eq Bool.noConfusion
      rw [hpl, Bool.false_or]
      cases eu : nthB (uSupp L (spP L P) 0 Ap) l with
      | false => rfl
      | true =>
        obtain ⟨i, hi, hAi, hsi⟩ := uSupp_nth L (spP L P) hsp l 0 Ap eu
        rw [Nat.zero_add] at hsi
        have hiN : i < N := by rw [← hApl]; exact hi
        have := touch_of_nth _ _ l (spnth i l hiN hsi) eq
        rw [hnj i hiN hAi] at this; exact absurd this Bool.noConfusion
    | false =>
      rw [eq, Bool.false_or] at hT'
      obtain ⟨j, hj, hAqj, hsj⟩ := uSupp_nth L (spP L P) hsp l 0 Aq hT'
      rw [Nat.zero_add] at hsj
      have hjN : j < N := by rw [← hAql]; exact hj
      have o := hoff j hjN hAqj
      cases ep : nthB (p.map nzb) l with
      | true => have := touch_of_nth _ _ l (spnth j l hjN hsj) ep; rw [o.1] at this; exact absurd this Bool.noConfusion
      | false =>
        rw [Bool.false_or]
        cases eu : nthB (uSupp L (spP L P) 0 Ap) l with
        | false => rfl
        | true =>
          obtain ⟨i, hi, hAi, hsi⟩ := uSupp_nth L (spP L P) hsp l 0 Ap eu
          rw [Nat.zero_add] at hsi
          have hiN : i < N := by rw [← hApl]; exact hi
          have := anyT_of P Ap (nthL P j) i hAi (touch_of_nth _ _ l (spnth j l hjN hsj) (spnth i l hiN hsi)) hiN
          rw [o.2] at this; exact absurd this Bool.noConfusion
  -- factorization and antisymmetry
  have hF : depOn S (fun y => K.mul (Fp y) (prodW K f 0 Ap y)) :=
    depOn_mul K _ _ Fp _ (hpn.trans (len_uSupp L _ hsp 0 Ap).symm) hFp (depOn_prodW K L _ hsp f hfd 0 Ap)
  have hH : depOn T (fun y => K.mul (Fq y) (prodW K f 0 Aq y)) :=
    depOn_mul K _ _ Fq _ (hqn.trans (len_uSupp L _ hsp 0 Aq).symm) hFq (depOn_prodW K L _ hsp f hfd 0 Aq)
  have ind := indep K (pairs els) S T _ _ (hSl.trans hTl.symm) hST hF hH
  rw [hSl] at ind
  have z : sumK K (fun y => K.mul (Fp y) (prodW K f 0 Ap y)) (cfgs (pairs els) L) = K.zero :=
    dup_anti K O els L _ (fun y => by rw [hFpa, prodW_swap K f hfs y 0 Ap, Signed.neg_mul'])
  rw [z, DetChain.zero_mul'] at ind
  have hg1 := pwN_ge_one K O h1 _ (natR_ge_one K O h1 _ (len_pairs els hels)) L
  have hgne : pwN K (natR K (pairs els).length) L ≠ K.zero := fun hz => by
    rw [hz] at hg1; exact h10 (O.le_antisymm _ _ hg1 h1)
  refine O.pos_mul_eq_zero _ _ (O.le_trans _ _ _ h1 hg1) hgne ?_
  show K.mul _ (sumK K _ _) = K.zero
  rw [← ind]
  congr 1
  refine sumK_ext K _ _ (fun y => ?_) _
  show K.mul (K.mul (Fp y) (Fq y)) (prodW K f 0 A y) = _
  have eA : A = orB Ap Aq := hspl.1
  conv => lhs; rw [eA]
  rw [prodW_or K f y 0 Ap Aq (hApl.trans hAql.symm) hspl.2.1]
  have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
    rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
  exact mul4 _ _ _ _

end Join

#print axioms Join.disjN_nth
#print axioms Join.prodW_swap
#print axioms Join.vanish_join

ym/DupAlg.lean

/-
  ym/DupAlg — T45, cycle 55 (Lad2, any finite gauge group): algebra of the duplicated system.

    fst_swap, snd_swap       the projections of a swapped pair field
    getO_map, dep_fst/snd    a function of one copy depends on the same links
    prodW_prod               Π_i w_i(x) w_i(x') = Π_i w_i(x) · Π_i w_i(x')
    Zdup0                    Ẑ_0 = Z_0²
    ZdupW                    Ẑ_W = 2 (Z_pq Z_0 − Z_p Z_q)  for χ = (U_p(x) − U_p(x'))(U_q(x) − U_q(x'))
  Here Z_X = Σ_x X(x) Π_{i∈M} w_i(x) and hats are the same sums over duplicated fields with weights w_i(x) w_i(x').

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Join

namespace DupAlg

open YMChain LinkFactor MaskSum Closure Factor Seeds Indep PosRatio Dup

variable {R : Type} (K : DetChain.CRing R) {G : Type}

theorem fst_swap (y : List (G × G)) : fstL (swapL y) = sndL y := by
  induction y with
  | nil => rfl
  | cons a y ih => show a.2 :: fstL (swapL y) = a.2 :: sndL y; rw [ih]

theorem snd_swap (y : List (G × G)) : sndL (swapL y) = fstL y := by
  induction y with
  | nil => rfl
  | cons a y ih => show a.1 :: sndL (swapL y) = a.1 :: fstL y; rw [ih]

theorem getO_map {A B : Type} (g : A → B) : ∀ (y : List A) (l : Nat), getO (y.map g) l = (getO y l).map g
  | [], _ => rfl
  | _ :: _, 0 => rfl
  | _ :: y, l + 1 => getO_map g y l

theorem dep_fst (S : List Bool) (φ : List G → R) (h : depOn S φ) : depOn S (fun y : List (G × G) => φ (fstL y)) :=
  fun y y' hyy => h _ _ (fun l hl => by
    show getO (y.map Prod.fst) l = getO (y'.map Prod.fst) l
    rw [getO_map, getO_map, hyy l hl])

theorem dep_snd (S : List Bool) (φ : List G → R) (h : depOn S φ) : depOn S (fun y : List (G × G) => φ (sndL y)) :=
  fun y y' hyy => h _ _ (fun l hl => by
    show getO (y.map Prod.snd) l = getO (y'.map Prod.snd) l
    rw [getO_map, getO_map, hyy l hl])

theorem prodW_prod (w : Nat → List G → R) (y : List (G × G)) : ∀ (k : Nat) (M : List Bool),
    prodW K (fun i y => K.mul (w i (fstL y)) (w i (sndL y))) k M y
      = K.mul (prodW K w k M (fstL y)) (prodW K w k M (sndL y))
  | _, [] => (K.one_mul _).symm
  | k, b :: M => by
    show K.mul (if b = true then K.mul (w k (fstL y)) (w k (sndL y)) else K.one) (prodW K _ (k + 1) M y)
      = K.mul (K.mul (if b = true then w k (fstL y) else K.one) (prodW K w (k + 1) M (fstL y)))
          (K.mul (if b = true then w k (sndL y) else K.one) (prodW K w (k + 1) M (sndL y)))
    rw [prodW_prod w y (k + 1) M]
    have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
      rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
    rw [mul4]
    cases b
    · show K.mul K.one _ = K.mul (K.mul K.one K.one) _; rw [K.one_mul, K.one_mul, K.one_mul]
    · rfl

section
variable (els : List G) (L : Nat) (w : Nat → List G → R) (M : List Bool)

def Zx (X : List G → R) : R := sumK K (fun x => K.mul (X x) (prodW K w 0 M x)) (cfgs els L)

def What : Nat → List (G × G) → R := fun i y => K.mul (w i (fstL y)) (w i (sndL y))

theorem Zdup0 :
    sumK K (fun y => prodW K (What K w) 0 M y) (cfgs (pairs els) L)
      = K.mul (Zx K els L w M (fun _ => K.one)) (Zx K els L w M (fun _ => K.one)) := by
  show sumK K (fun y => prodW K (fun i y => K.mul (w i (fstL y)) (w i (sndL y))) 0 M y) _ = _
  rw [sumK_ext K _ _ (fun y => prodW_prod K w y 0 M), dup_factor K els L (prodW K w 0 M) (prodW K w 0 M)]
  unfold Zx
  rw [sumK_ext K _ _ (fun x => K.one_mul (prodW K w 0 M x))]

theorem ZdupW (Up Uq : List G → R) :
    let D := K.add (K.mul (Zx K els L w M (fun x => K.mul (Up x) (Uq x))) (Zx K els L w M (fun _ => K.one)))
      (K.neg (K.mul (Zx K els L w M Up) (Zx K els L w M Uq)))
    sumK K (fun y => K.mul (K.mul (K.add (Up (fstL y)) (K.neg (Up (sndL y)))) (K.add (Uq (fstL y)) (K.neg (Uq (sndL y)))))
        (prodW K (What K w) 0 M y)) (cfgs (pairs els) L) = K.add D D := by
  intro D
  have nm := Signed.neg_mul' K
  have nn := Signed.neg_neg' K
  have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
    rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
  -- pointwise expansion into four single-copy products
  have pt : ∀ y : List (G × G),
      K.mul (K.mul (K.add (Up (fstL y)) (K.neg (Up (sndL y)))) (K.add (Uq (fstL y)) (K.neg (Uq (sndL y)))))
        (prodW K (What K w) 0 M y)
      = K.add (K.add (K.mul (K.mul (K.mul (Up (fstL y)) (Uq (fstL y))) (prodW K w 0 M (fstL y))) (K.mul K.one (prodW K w 0 M (sndL y))))
                (K.neg (K.mul (K.mul (Up (fstL y)) (prodW K w 0 M (fstL y))) (K.mul (Uq (sndL y)) (prodW K w 0 M (sndL y))))))
          (K.add (K.neg (K.mul (K.mul (Uq (fstL y)) (prodW K w 0 M (fstL y))) (K.mul (Up (sndL y)) (prodW K w 0 M (sndL y)))))
                (K.mul (K.mul K.one (prodW K w 0 M (fstL y))) (K.mul (K.mul (Up (sndL y)) (Uq (sndL y))) (prodW K w 0 M (sndL y))))) := by
    intro y
    show K.mul _ (prodW K (fun i y => K.mul (w i (fstL y)) (w i (sndL y))) 0 M y) = _
    rw [prodW_prod K w y 0 M]
    generalize Up (fstL y) = a
    generalize Up (sndL y) = a'
    generalize Uq (fstL y) = b
    generalize Uq (sndL y) = b'
    generalize prodW K w 0 M (fstL y) = W
    generalize prodW K w 0 M (sndL y) = W'
    rw [DetChain.right_distrib, K.distrib, K.distrib, DetChain.right_distrib, DetChain.right_distrib, DetChain.right_distrib]
    congr 1
    · congr 1
      · rw [K.one_mul, ← K.mul_assoc]
      · rw [K.mul_comm a (K.neg b'), nm b' a, nm (K.mul b' a) (K.mul W W'), K.mul_comm b' a, mul4]
    · congr 1
      · rw [nm a' b, nm (K.mul a' b) (K.mul W W'), K.mul_comm a' b, mul4]
      · rw [nm a' (K.neg b'), K.mul_comm a' (K.neg b'), nm b' a', nn, K.one_mul, K.mul_comm b' a',
          K.mul_comm (K.mul a' b') (K.mul W W'), K.mul_assoc, K.mul_comm W' (K.mul a' b')]
  rw [sumK_ext K _ _ pt, sumK_add, sumK_add, sumK_add, WilsonBound.sumK_neg, WilsonBound.sumK_neg]
  rw [dup_factor K els L (fun x => K.mul (K.mul (Up x) (Uq x)) (prodW K w 0 M x)) (fun x => K.mul K.one (prodW K w 0 M x)),
    dup_factor K els L (fun x => K.mul (Up x) (prodW K w 0 M x)) (fun x => K.mul (Uq x) (prodW K w 0 M x)),
    dup_factor K els L (fun x => K.mul (Uq x) (prodW K w 0 M x)) (fun x => K.mul (Up x) (prodW K w 0 M x)),
    dup_factor K els L (fun x => K.mul K.one (prodW K w 0 M x)) (fun x => K.mul (K.mul (Up x) (Uq x)) (prodW K w 0 M x))]
  show K.add (K.add (K.mul (Zx K els L w M _) (Zx K els L w M _)) (K.neg (K.mul (Zx K els L w M Up) (Zx K els L w M Uq))))
      (K.add (K.neg (K.mul (Zx K els L w M Uq) (Zx K els L w M Up))) (K.mul (Zx K els L w M (fun _ => K.one)) (Zx K els L w M _))) = _
  rw [K.mul_comm (Zx K els L w M Uq), K.add_comm (K.neg _) (K.mul (Zx K els L w M (fun _ => K.one)) _),
    K.mul_comm (Zx K els L w M (fun _ => K.one))]

end

end DupAlg

#print axioms DupAlg.prodW_prod
#print axioms DupAlg.Zdup0
#print axioms DupAlg.ZdupW

ym/GapG.lean

/-
  ym/GapG — T45, cycle 56 (Lad2, any finite gauge group, any dimension): exponential clustering uniform in the volume.

  Original system: fields x ∈ cfgs els L, plaquette weights w_i = c₀ + f_i with m ≤ w_i ≤ 1, |f_i| ≤ ε, 0 ≤ c₀ ≤ 1,
  κ m = 1, 1 ≤ κ; observables U_p, U_q with |U| ≤ K₁ on the links of p resp. q (p, q share no link).
  Z_X = Σ_x X(x) Π_{i∈M} w_i(x)  (DupAlg.Zx).
  Hypotheses: every plaquette touches ≤ Dg plaquettes; ε̂ κ̂^{Dg+1} ≤ u x with ε̂ = ε + ε + ε², κ̂ = κ²;
  x(1+y)^Dg ≤ y; the ball of R closure steps around p contains no plaquette touching q.
    gap_G     −B Z_1² ≤ 2 (Z_{U_pU_q} Z_1 − Z_{U_p} Z_{U_q}) ≤ B Z_1²,   B = (2K₁)² κ̂^ŝ (1+y)^ŝ u^R
  i.e. |⟨U_pU_q⟩ − ⟨U_p⟩⟨U_q⟩| ≤ ½ B, decaying like u^R in the separation, uniformly in the volume — the
  strong-coupling mass gap for every finite gauge group (2I included), without GKS and without log Z.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.DupAlg

namespace GapG

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed Dup DupAlg AreaG
  Join Reach

variable {R : Type} (K : DetChain.CRing R) {G : Type}

theorem depOn_add (S : List Bool) (F H : List G → R) (hF : depOn S F) (hH : depOn S H) :
    depOn S (fun c => K.add (F c) (H c)) := fun c c' h => by
  show K.add (F c) (H c) = K.add (F c') (H c'); rw [hF c c' h, hH c c' h]

theorem depOn_neg (S : List Bool) (F : List G → R) (hF : depOn S F) : depOn S (fun c => K.neg (F c)) :=
  fun c c' h => by show K.neg (F c) = K.neg (F c'); rw [hF c c' h]

theorem depOn_mul' (S : List Bool) (F H : List G → R) (hF : depOn S F) (hH : depOn S H) :
    depOn S (fun c => K.mul (F c) (H c)) := fun c c' h => by
  show K.mul (F c) (H c) = K.mul (F c') (H c'); rw [hF c c' h, hH c c' h]

theorem nzb_add (a b : Nat) : (nzb a || nzb b) = true → nzb (a + b) = true := by
  intro h
  cases a with
  | zero => cases b with
    | zero => exact absurd h (by decide)
    | succ b => rw [Nat.zero_add]; rfl
  | succ a => rw [Nat.succ_add]; rfl

theorem supp_zadd : ∀ p q : List Nat, p.length = q.length →
    leB (orB (p.map nzb) (q.map nzb)) ((zadd p q).map nzb)
  | [], [], _ => trivial
  | [], _ :: _, h => absurd h Nat.noConfusion
  | _ :: _, [], h => absurd h Nat.noConfusion
  | a :: p, b :: q, h => ⟨nzb_add a b, supp_zadd p q (Nat.succ.inj h)⟩

theorem prodW_congr (f g : Nat → List G → R) (h : ∀ i x, f i x = g i x) (x : List G) :
    ∀ (k : Nat) (M : List Bool), prodW K f k M x = prodW K g k M x
  | _, [] => rfl
  | k, b :: M => by
    show K.mul (if b = true then f k x else K.one) (prodW K f (k + 1) M x)
      = K.mul (if b = true then g k x else K.one) (prodW K g (k + 1) M x)
    rw [h, prodW_congr f g h x (k + 1) M]

variable (O : ORing K)

theorem bnd_add (a b α β : R) (ha : O.le (K.neg α) a ∧ O.le a α) (hb : O.le (K.neg β) b ∧ O.le b β) :
    O.le (K.neg (K.add α β)) (K.add a b) ∧ O.le (K.add a b) (K.add α β) := by
  rw [neg_add' K]
  exact ⟨O.add_le_add _ _ _ _ ha.1 hb.1, O.add_le_add _ _ _ _ ha.2 hb.2⟩

theorem bnd_neg (a α : R) (ha : O.le (K.neg α) a ∧ O.le a α) : O.le (K.neg α) (K.neg a) ∧ O.le (K.neg a) α := by
  have h1 := neg_le_neg K O _ _ ha.2
  have h2 := neg_le_neg K O _ _ ha.1
  rw [neg_neg' K] at h2
  exact ⟨h1, h2⟩

theorem bnd_unit (h1 : O.le K.zero K.one) (a : R) (ha0 : O.le K.zero a) (ha1 : O.le a K.one) :
    O.le (K.neg K.one) a ∧ O.le a K.one :=
  ⟨O.le_trans _ _ _ (le_of_neg O h1) ha0, ha1⟩
where
  le_of_neg (O : ORing K) (h1 : O.le K.zero K.one) : O.le (K.neg K.one) K.zero := by
    have := neg_le_neg K O _ _ h1
    have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
    rw [e] at this; exact this

theorem wid (c0 a b : R) :
    K.add (K.mul c0 c0) (K.add (K.add (K.mul c0 a) (K.mul c0 b)) (K.mul a b)) = K.mul (K.add c0 a) (K.add c0 b) := by
  rw [K.distrib, DetChain.right_distrib, DetChain.right_distrib,
    K.add_assoc (K.mul c0 c0) (K.mul a c0) (K.add (K.mul c0 b) (K.mul a b)), K.mul_comm a c0,
    ← K.add_assoc (K.mul c0 a) (K.mul c0 b) (K.mul a b)]

/-- **Clustering for any finite gauge group** (uniform in the volume). -/
theorem gap_G (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (els : List G) (hels : 1 ≤ els.length)
    (L : Nat) (P : List (List Nat)) (hP : ∀ i, i < P.length → (nthL P i).length = L)
    (p q : List Nat) (hpL : p.length = L) (hqL : q.length = L) (hpq : disjN p q)
    (f : Nat → List G → R) (Up Uq : List G → R) (c0 m κ ε K1 u x y : R) (Dg Rr : Nat)
    (hfd : ∀ i, depOn (spP L P i) (f i)) (hUp : depOn (p.map nzb) Up) (hUq : depOn (q.map nzb) Uq)
    (hc0 : O.le K.zero c0) (hc1 : O.le c0 K.one) (hm : O.le K.zero m) (hm1 : O.le m K.one)
    (hκ1 : O.le K.one κ) (hκm : K.mul κ m = K.one)
    (hw : ∀ i x, O.le m (K.add c0 (f i x))) (hw1 : ∀ i x, O.le (K.add c0 (f i x)) K.one)
    (he : O.le K.zero ε) (hf : ∀ i x, O.le (K.neg ε) (f i x) ∧ O.le (f i x) ε)
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hu0 : O.le K.zero u) (hu1 : O.le u K.one) (hx : O.le K.zero x) (hy : O.le K.zero y)
    (hdeg : ∀ r, memL r P → cnt (P.map (fun q' => touch q' r)) ≤ Dg)
    (hsmall : O.le (K.mul (K.add (K.add ε ε) (K.mul ε ε)) (pwR K (K.mul κ κ) (Dg + 1))) (K.mul u x))
    (hxy : O.le (K.mul x (pwR K (K.add K.one y) Dg)) y)
    (hfar : ∀ i, i < P.length → nthB (ball (fun r => touch r p) P Rr) i = true → touch (nthL P i) q = false)
    (M : List Bool) (hM : M.length = P.length) :
    let w : Nat → List G → R := fun i x => K.add c0 (f i x)
    let D := K.add (K.mul (Zx K els L w M (fun x => K.mul (Up x) (Uq x))) (Zx K els L w M (fun _ => K.one)))
      (K.neg (K.mul (Zx K els L w M Up) (Zx K els L w M Uq)))
    let s := cnt (P.map (fun r => touch r (zadd p q)))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) s))
      (K.mul (pwR K (K.add K.one y) s) (pwR K u Rr))
    O.le (K.neg (K.mul B (K.mul (Zx K els L w M (fun _ => K.one)) (Zx K els L w M (fun _ => K.one))))) (K.add D D) ∧
      O.le (K.add D D) (K.mul B (K.mul (Zx K els L w M (fun _ => K.one)) (Zx K els L w M (fun _ => K.one)))) := by
  intro w D s B
  let fh : Nat → List (G × G) → R := fun i yy =>
    K.add (K.add (K.mul c0 (f i (fstL yy))) (K.mul c0 (f i (sndL yy)))) (K.mul (f i (fstL yy)) (f i (sndL yy)))
  let Fp : List (G × G) → R := fun yy => K.add (Up (fstL yy)) (K.neg (Up (sndL yy)))
  let Fq : List (G × G) → R := fun yy => K.add (Uq (fstL yy)) (K.neg (Uq (sndL yy)))
  let χh : List (G × G) → R := fun yy => K.mul (Fp yy) (Fq yy)
  have whd : ∀ i yy, K.add (K.mul c0 c0) (fh i yy) = What K w i yy := fun i yy => wid K c0 _ _
  -- dependence
  have hfd' : ∀ i, depOn (spP L P i) (fh i) := fun i =>
    depOn_add K _ _ _ (depOn_add K _ _ _
      (depOn_mul' K _ (fun _ => c0) _ (depOn_const _ _) (dep_fst _ _ (hfd i)))
      (depOn_mul' K _ (fun _ => c0) _ (depOn_const _ _) (dep_snd _ _ (hfd i))))
      (depOn_mul' K _ _ _ (dep_fst _ _ (hfd i)) (dep_snd _ _ (hfd i)))
  have hFp : depOn (p.map nzb) Fp := depOn_add K _ _ _ (dep_fst _ _ hUp) (depOn_neg K _ _ (dep_snd _ _ hUp))
  have hFq : depOn (q.map nzb) Fq := depOn_add K _ _ _ (dep_fst _ _ hUq) (depOn_neg K _ _ (dep_snd _ _ hUq))
  have hpn : (p.map nzb).length = (q.map nzb).length := (len_map' p).trans (hpL.trans (hqL.symm.trans (len_map' q).symm))
  have hχd : depOn ((zadd p q).map nzb) χh :=
    depOn_mono _ _ χh (supp_zadd p q (hpL.trans hqL.symm)) (depOn_mul K _ _ Fp Fq hpn hFp hFq)
  have hnL : (zadd p q).length = L := (AreaLower.len_zadd_le p q (by rw [hpL, hqL]; exact Nat.le_refl _)).trans hpL
  -- symmetry
  have hfs : ∀ i yy, fh i (swapL yy) = fh i yy := fun i yy => by
    show K.add (K.add (K.mul c0 (f i (fstL (swapL yy)))) (K.mul c0 (f i (sndL (swapL yy))))) (K.mul (f i (fstL (swapL yy))) (f i (sndL (swapL yy)))) = _
    rw [fst_swap, snd_swap, K.add_comm (K.mul c0 (f i (sndL yy))), K.mul_comm (f i (sndL yy))]
  have hFpa : ∀ yy, Fp (swapL yy) = K.neg (Fp yy) := fun yy => by
    show K.add (Up (fstL (swapL yy))) (K.neg (Up (sndL (swapL yy)))) = K.neg (K.add (Up (fstL yy)) (K.neg (Up (sndL yy))))
    rw [fst_swap, snd_swap, neg_add' K, neg_neg' K, K.add_comm]
  -- constants
  have hb1 : ∀ i x', O.le (K.neg K.one) (K.add c0 (f i x')) ∧ O.le (K.add c0 (f i x')) K.one := fun i x' =>
    bnd_unit K O h1 _ (O.le_trans _ _ _ hm (hw i x')) (hw1 i x')
  have hc0b := bnd_unit K O h1 c0 hc0 hc1
  have hfh : ∀ i yy, O.le (K.neg (K.add (K.add ε ε) (K.mul ε ε))) (fh i yy) ∧ O.le (fh i yy) (K.add (K.add ε ε) (K.mul ε ε)) := by
    intro i yy
    have a1 := bnd_mul K O _ _ _ _ h1 he hc0b (hf i (fstL yy))
    have a2 := bnd_mul K O _ _ _ _ h1 he hc0b (hf i (sndL yy))
    have a3 := bnd_mul K O _ _ _ _ he he (hf i (fstL yy)) (hf i (sndL yy))
    rw [K.one_mul] at a1 a2
    exact bnd_add K O _ _ _ _ (bnd_add K O _ _ _ _ a1 a2) a3
  have hwh : ∀ i yy, O.le (K.mul m m) (K.add (K.mul c0 c0) (fh i yy)) := fun i yy => by
    rw [whd]; exact Gap.mul_le_mul4 K O _ _ _ _ hm hm (O.le_trans _ _ _ hm (hw i _)) (hw i _) (hw i _)
  have hwh1 : ∀ i yy, O.le (K.add (K.mul c0 c0) (fh i yy)) K.one := fun i yy => by
    rw [whd]
    have := Gap.mul_le_mul4 K O _ _ _ _ (O.le_trans _ _ _ hm (hw i (fstL yy))) (O.le_trans _ _ _ hm (hw i (sndL yy))) h1
      (hw1 i _) (hw1 i _)
    rw [K.one_mul] at this; exact this
  have hκm' : K.mul (K.mul κ κ) (K.mul m m) = K.one := by
    rw [K.mul_assoc, ← K.mul_assoc κ m m, K.mul_comm κ m, K.mul_assoc m κ m, hκm, K.mul_comm m K.one, K.one_mul, hκm]
  have hFpb : ∀ yy, O.le (K.neg (K.add K1 K1)) (Fp yy) ∧ O.le (Fp yy) (K.add K1 K1) := fun yy =>
    bnd_add K O _ _ _ _ (hUpb _) (bnd_neg K O _ _ (hUpb _))
  have hFqb : ∀ yy, O.le (K.neg (K.add K1 K1)) (Fq yy) ∧ O.le (Fq yy) (K.add K1 K1) := fun yy =>
    bnd_add K O _ _ _ _ (hUqb _) (bnd_neg K O _ _ (hUqb _))
  have hK2 : O.le K.zero (K.add K1 K1) := by have := O.add_le_add _ _ _ _ hK1 hK1; rw [K.zero_add] at this; exact this
  have hχb : ∀ yy, O.le (K.neg (K.mul (K.add K1 K1) (K.add K1 K1))) (χh yy) ∧ O.le (χh yy) (K.mul (K.add K1 K1) (K.add K1 K1)) :=
    fun yy => bnd_mul K O _ _ _ _ hK2 hK2 (hFpb yy) (hFqb yy)
  have he2 : O.le K.zero (K.add (K.add ε ε) (K.mul ε ε)) := by
    have a := O.add_le_add _ _ _ _ he he; rw [K.zero_add] at a
    have b := O.add_le_add _ _ _ _ a (O.mul_nonneg _ _ he he); rw [K.zero_add] at b; exact b
  -- vanishing of small polymers
  have hvan : ∀ A : List Bool, A.length = P.length → clS (fun r => touch r (zadd p q)) P A = A → cnt A < Rr →
      aW K (pairs els) L fh χh A = K.zero := by
    intro A hA _ hsm
    refine vanish_join K O h1 h10 els hels L P hP p q hpL hqL hpq fh Fp Fq hfd' hFp hFq hfs hFpa A hA (fun i hi hcl => ?_)
    cases e : touch (nthL P i) q with
    | false => rfl
    | true =>
      exfalso
      let σp : List Nat → Bool := fun r => touch r p
      have hb : nthB (ball σp P Rr) i = false := by
        cases eb : nthB (ball σp P Rr) i
        · rfl
        · rw [hfar i hi eb] at e; exact absurd e Bool.noConfusion
      have hcl_len : (clS σp P A).length = P.length := cl_len (Vs σp P) P.length (hV σp P) A hA
      have hR := reach σp P (clS σp P A) hcl_len (cl_idem (Vs σp P) P.length (hV σp P) A hA) Rr i hcl hb
      have hle := leB_cnt _ _ (cl_le (Vs σp P) P.length (hV σp P) A hA)
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le hsm (Nat.le_trans hR hle))
  -- area_G in the duplicated system
  have ag := area_G K O h1 h10 (pairs els) (len_pairs els hels) L P (zadd p q) fh χh (K.mul c0 c0) (K.mul m m) (K.mul κ κ)
    (K.add (K.add ε ε) (K.mul ε ε)) (K.mul (K.add K1 K1) (K.add K1 K1)) u x y Dg Rr hP hnL hχd hfd'
    (O.mul_nonneg _ _ hc0 hc0) (by have := Gap.mul_le_mul4 K O _ _ _ _ hc0 hc0 h1 hc1 hc1; rw [K.one_mul] at this; exact this)
    (O.mul_nonneg _ _ hm hm) (by have := Gap.mul_le_mul4 K O _ _ _ _ hm hm h1 hm1 hm1; rw [K.one_mul] at this; exact this)
    (GPerm.one_le_mul K O _ _ hκ1 hκ1 h1) hκm' hwh hwh1 he2 hfh
    (O.mul_nonneg _ _ hK2 hK2) hχb hu0 hu1 hx hy hdeg hsmall hxy hvan M hM
  -- translate
  have eZ0 : Z0 K (pairs els) L fh (K.mul c0 c0) M = K.mul (Zx K els L w M (fun _ => K.one)) (Zx K els L w M (fun _ => K.one)) := by
    show sumK K (fun yy => prodW K (fun i yy => K.add (K.mul c0 c0) (fh i yy)) 0 M yy) _ = _
    rw [sumK_ext K _ _ (fun yy => prodW_congr K _ _ whd yy 0 M)]
    exact Zdup0 K els L w M
  have eZW : ZW K (pairs els) L fh χh (K.mul c0 c0) M = K.add D D := by
    show sumK K (fun yy => K.mul (χh yy) (prodW K (fun i yy => K.add (K.mul c0 c0) (fh i yy)) 0 M yy)) _ = _
    rw [sumK_ext K _ _ (fun yy => by rw [prodW_congr K _ _ whd yy 0 M])]
    exact ZdupW K els L w M Up Uq
  have ag' := ag
  simp only at ag'
  rw [eZ0, eZW] at ag'
  exact ag'

end GapG

#print axioms GapG.gap_G

ym/HaarStr.lean

/-
  ym/HaarStr — T45, cycle 57 (Lad3 step 1, any compact gauge group): an abstract Haar integral over gauge fields.

  SU(2) is a continuous group; Lean core has no Haar measure. The structure below lists exactly the properties of
  the iterated Haar integral over L link variables that the strong-coupling proofs use. It is a hypothesis-carrying
  structure (like ORing), not an axiom: every theorem stated with it holds for every instance. Instances: every finite
  group (normalised counting measure, ym/Indep and ym/MeanZero), and — by Haar's theorem and Fubini for continuous
  functions — every compact group, SU(2) included, on continuous functions (the cited input).

    adm F        F is admissible (closed under +, ·, constants, negation)
    I F          the integral; linear, positive, I 1 = 1
    indep        F depends only on links S, H only on links T, S ∩ T = ∅  ⇒  I(F H) = I F · I H
    J l F        partial integral over link l: admissible, independent of l, I(J l F) = I F,
                 J l (F H) = (J l F) H when H does not depend on l
  Consequences proved here:
    I_mono       F ≤ H pointwise ⇒ I F ≤ I H
    I_bnd        −c ≤ F ≤ c ⇒ −c ≤ I F ≤ c
    single_cover_H   J l f = 0 and H independent of l ⇒ I(f H) = 0

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GapG

namespace HaarStr

open YMChain Indep Signed

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

structure Haar (G : Type) (L : Nat) where
  adm : (List G → R) → Prop
  adm_add : ∀ F H, adm F → adm H → adm (fun c => K.add (F c) (H c))
  adm_mul : ∀ F H, adm F → adm H → adm (fun c => K.mul (F c) (H c))
  adm_const : ∀ a : R, adm (fun _ => a)
  adm_neg : ∀ F, adm F → adm (fun c => K.neg (F c))
  I : (List G → R) → R
  I_add : ∀ F H, adm F → adm H → I (fun c => K.add (F c) (H c)) = K.add (I F) (I H)
  I_smul : ∀ (a : R) F, adm F → I (fun c => K.mul a (F c)) = K.mul a (I F)
  I_one : I (fun _ => K.one) = K.one
  I_pos : ∀ F, adm F → (∀ c, O.le K.zero (F c)) → O.le K.zero (I F)
  I_ext : ∀ F H, (∀ c, F c = H c) → I F = I H
  indep : ∀ (S T : List Bool) (F H : List G → R), adm F → adm H → S.length = L → T.length = L →
    Factor.disjM S T → depOn S F → depOn T H → I (fun c => K.mul (F c) (H c)) = K.mul (I F) (I H)
  J : Nat → (List G → R) → (List G → R)
  J_adm : ∀ l F, adm F → adm (J l F)
  J_I : ∀ l F, adm F → I (J l F) = I F
  J_mul : ∀ (l : Nat) (T : List Bool) (F H : List G → R), adm F → adm H → Seeds.nthB T l = false → depOn T H →
    ∀ c, J l (fun c' => K.mul (F c') (H c')) c = K.mul (J l F c) (H c)

variable {G : Type} {L : Nat} (H : Haar K O G L)

theorem I_zero : H.I (fun _ => K.zero) = K.zero := by
  have := H.I_smul K.zero (fun _ => K.one) (H.adm_const K.one)
  rw [DetChain.zero_mul', DetChain.zero_mul'] at this
  exact this

theorem I_neg (F : List G → R) (hF : H.adm F) : H.I (fun c => K.neg (F c)) = K.neg (H.I F) := by
  have e := H.I_add F (fun c => K.neg (F c)) hF (H.adm_neg F hF)
  rw [H.I_ext _ (fun _ => K.zero) (fun c => K.add_neg (F c)), I_zero K O H] at e
  exact uniq_neg K _ _ e.symm

/-- **Monotonicity.** -/
theorem I_mono (F F' : List G → R) (hF : H.adm F) (hF' : H.adm F') (h : ∀ c, O.le (F c) (F' c)) :
    O.le (H.I F) (H.I F') := by
  have hd := H.I_pos (fun c => K.add (F' c) (K.neg (F c))) (H.adm_add _ _ hF' (H.adm_neg _ hF)) (fun c => by
    have := O.add_le_add _ _ _ _ (h c) (O.le_refl (K.neg (F c)))
    rw [K.add_neg] at this; exact this)
  rw [H.I_add _ _ hF' (H.adm_neg _ hF), I_neg K O H F hF] at hd
  have := O.add_le_add _ _ _ _ hd (O.le_refl (H.I F))
  rw [K.zero_add, K.add_assoc, K.add_comm (K.neg (H.I F)), K.add_neg, DetChain.add_zero'] at this
  exact this

theorem I_const (a : R) : H.I (fun _ => a) = a := by
  have := H.I_smul a (fun _ => K.one) (H.adm_const K.one)
  rw [H.I_one, K.mul_comm a K.one, K.one_mul] at this
  exact this

/-- **Two-sided bound.** -/
theorem I_bnd (F : List G → R) (c0 : R) (hF : H.adm F) (h : ∀ c, O.le (K.neg c0) (F c) ∧ O.le (F c) c0) :
    O.le (K.neg c0) (H.I F) ∧ O.le (H.I F) c0 := by
  constructor
  · have := I_mono K O H (fun _ => K.neg c0) F (H.adm_const _) hF (fun c => (h c).1)
    rw [I_const K O H] at this; exact this
  · have := I_mono K O H F (fun _ => c0) hF (H.adm_const _) (fun c => (h c).2)
    rw [I_const K O H] at this; exact this

/-- **Single-cover rule** for the abstract integral. -/
theorem single_cover_H (l : Nat) (T : List Bool) (f Hf : List G → R) (hf : H.adm f) (hH : H.adm Hf)
    (hT : Seeds.nthB T l = false) (hdep : depOn T Hf) (hz : ∀ c, H.J l f c = K.zero) :
    H.I (fun c => K.mul (f c) (Hf c)) = K.zero := by
  rw [← H.J_I l _ (H.adm_mul _ _ hf hH)]
  rw [H.I_ext _ (fun _ => K.zero) (fun c => by rw [H.J_mul l T f Hf hf hH hT hdep c, hz c, DetChain.zero_mul'])]
  exact I_zero K O H

end HaarStr

#print axioms HaarStr.I_zero
#print axioms HaarStr.I_neg
#print axioms HaarStr.I_mono
#print axioms HaarStr.I_const
#print axioms HaarStr.I_bnd
#print axioms HaarStr.single_cover_H

ym/HaarFin.lean

/-
  ym/HaarFin — T45, cycle 58 (Lad3 step 2): every finite group gives a Haar structure (the interface is not vacuous).

  Given ν ≥ 0 with ν · |G| = 1 (e.g. R a field of characteristic 0):
    I F = ν^L Σ_{c ∈ cfgs els L} F c,      J l F c = ν Σ_g F(c[l := g]),      adm = everything.
    pwN_mul, setG_agree, sum_set     auxiliary facts (Σ_c Σ_g F(c[l := g]) = |G| Σ_c F c)
    finHaar                          the instance: all fields of HaarStr.Haar are proved
  So every statement proved for an abstract Haar structure holds for finite gauge groups; for SU(2) the instance is
  Haar's integral on continuous functions (cited, not formalised).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.HaarStr

namespace HaarFin

open YMChain LinkFactor MaskSum Indep MeanZero PolyBound HaarStr

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) {G : Type}

theorem pwN_mul (a b : R) : ∀ L, K.mul (pwN K a L) (pwN K b L) = pwN K (K.mul a b) L
  | 0 => K.one_mul _
  | L + 1 => by
    show K.mul (K.mul a (pwN K a L)) (K.mul b (pwN K b L)) = K.mul (K.mul a b) (pwN K (K.mul a b) L)
    rw [← pwN_mul a b L]
    rw [K.mul_assoc, ← K.mul_assoc (pwN K a L) b, K.mul_comm (pwN K a L) b, K.mul_assoc b, ← K.mul_assoc a b]

theorem pwN_one : ∀ L, pwN K K.one L = K.one
  | 0 => rfl
  | L + 1 => by show K.mul K.one (pwN K K.one L) = K.one; rw [K.one_mul, pwN_one L]

theorem pwN_nn (a : R) (ha : O.le K.zero a) (h1 : O.le K.zero K.one) : ∀ L, O.le K.zero (pwN K a L)
  | 0 => h1
  | L + 1 => O.mul_nonneg _ _ ha (pwN_nn a ha h1 L)

theorem getO_set : ∀ (c : List G) (l l' : Nat) (g : G), l' ≠ l → getO (setG c l g) l' = getO c l'
  | [], _, _, _, _ => rfl
  | _ :: _, 0, 0, _, h => absurd rfl h
  | _ :: _, 0, _ + 1, _, _ => rfl
  | _ :: _, _ + 1, 0, _, _ => rfl
  | _ :: c, l + 1, l' + 1, g, h => getO_set c l l' g (fun e => h (by rw [e]))

theorem sum_set (els : List G) (F : List G → R) : ∀ (L l : Nat), l < L →
    sumK K (fun c => sumK K (fun g => F (setG c l g)) els) (cfgs els L)
      = K.mul (natR K els.length) (sumK K F (cfgs els L))
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | L + 1, 0, _ => by
    rw [split_cfg, split_cfg, sumK_ext K _ _ (fun h => sumK_swap K _ _ _), ← sumK_swap]
    show sumK K (fun g => sumK K (fun h => sumK K (fun c => F (g :: c)) (cfgs els L)) els) els = _
    rw [sumK_ext K _ _ (fun g => sum_const K _ els), ← mul_sumK]
  | L + 1, l + 1, hl => by
    rw [split_cfg, split_cfg]
    show sumK K (fun h => sumK K (fun c => sumK K (fun g => F (h :: setG c l g)) els) (cfgs els L)) els = _
    rw [sumK_ext K _ _ (fun h => sum_set els (fun c => F (h :: c)) L l (Nat.lt_of_succ_lt_succ hl)), ← mul_sumK]

theorem memL_join {A : Type} (x : A) : ∀ ls : List (List A), memL x (joinL ls) → ∃ l, memL l ls ∧ memL x l
  | [], h => h.elim
  | l :: ls, h => by
    show ∃ l', memL l' (l :: ls) ∧ memL x l'
    have h' : memL x (l ++ joinL ls) := h
    cases GPerm.memL_app x l (joinL ls) h' with
    | inl h1 => exact ⟨l, Or.inl rfl, h1⟩
    | inr h2 =>
      obtain ⟨l', h3, h4⟩ := memL_join x ls h2
      exact ⟨l', Or.inr h3, h4⟩

theorem memL_map' {A B : Type} (f : A → B) (y : B) : ∀ l : List A, memL y (l.map f) → ∃ x, memL x l ∧ y = f x
  | [], h => h.elim
  | a :: l, h => by
    cases h with
    | inl e => exact ⟨a, Or.inl rfl, e⟩
    | inr h' =>
      obtain ⟨x, hx, e⟩ := memL_map' f y l h'
      exact ⟨x, Or.inr hx, e⟩

theorem mem_cfgs_len (els : List G) : ∀ (L : Nat) (c : List G), memL c (cfgs els L) → c.length = L
  | 0, c, h => by
    cases h with
    | inl e => rw [e]; rfl
    | inr h' => exact h'.elim
  | L + 1, c, h => by
    obtain ⟨l, hl, hc⟩ := memL_join c _ h
    obtain ⟨g, _, e⟩ := memL_map' _ l els hl
    rw [e] at hc
    obtain ⟨c', hc', e'⟩ := memL_map' _ c _ hc
    rw [e']
    exact congrArg Nat.succ (mem_cfgs_len els L c' hc')

theorem setG_ge : ∀ (c : List G) (l : Nat) (g : G), c.length ≤ l → setG c l g = c
  | [], _, _, _ => rfl
  | _ :: _, 0, _, h => absurd h (Nat.not_succ_le_zero _)
  | a :: c, l + 1, g, h => by
    show a :: setG c l g = a :: c
    rw [setG_ge c l g (Nat.le_of_succ_le_succ h)]

/-- **The finite-group Haar structure.** -/
def finHaar (els : List G) (L : Nat) (ν : R) (hν : O.le K.zero ν) (h1 : O.le K.zero K.one)
    (hνG : K.mul ν (natR K els.length) = K.one) : HaarStr.Haar K O G L where
  adm := fun _ => True
  adm_add := fun _ _ _ _ => trivial
  adm_mul := fun _ _ _ _ => trivial
  adm_const := fun _ => trivial
  adm_neg := fun _ _ => trivial
  I := fun F => K.mul (pwN K ν L) (sumK K F (cfgs els L))
  I_add := fun F H _ _ => by
    show K.mul _ (sumK K (fun c => K.add (F c) (H c)) _) = _
    rw [sumK_add, K.distrib]
  I_smul := fun a F _ => by
    show K.mul _ (sumK K (fun c => K.mul a (F c)) _) = K.mul a (K.mul _ _)
    rw [← mul_sumK, ← K.mul_assoc, K.mul_comm (pwN K ν L) a, K.mul_assoc]
  I_one := by
    show K.mul (pwN K ν L) (sumK K (fun _ => K.one) (cfgs els L)) = K.one
    rw [cfg_count, K.mul_comm (pwN K (natR K els.length) L) K.one, K.one_mul, pwN_mul, hνG, pwN_one]
  I_pos := fun F _ hF => O.mul_nonneg _ _ (pwN_nn K O ν hν h1 L) (by
    have := MaskSum.sumK_le K O (fun _ => K.zero) F (cfgs els L) (fun c _ => hF c)
    rw [sum_zero] at this; exact this)
  I_ext := fun F H h => by show K.mul _ (sumK K F _) = K.mul _ (sumK K H _); rw [sumK_ext K F H h]
  indep := fun S T F H _ _ hS hT hd hF hH => by
    show K.mul (pwN K ν L) (sumK K (fun c => K.mul (F c) (H c)) (cfgs els L))
      = K.mul (K.mul (pwN K ν L) (sumK K F (cfgs els L))) (K.mul (pwN K ν L) (sumK K H (cfgs els L)))
    have ind := indep K els S T F H (hS.trans hT.symm) hd hF hH
    rw [hS] at ind
    -- ν^L X = ν^L (ν^L |G|^L) X = ν^L ν^L (|G|^L X)
    have e1 : K.mul (pwN K ν L) (pwN K (natR K els.length) L) = K.one := by rw [pwN_mul, hνG, pwN_one]
    calc K.mul (pwN K ν L) (sumK K (fun c => K.mul (F c) (H c)) (cfgs els L))
        = K.mul (pwN K ν L) (K.mul (K.mul (pwN K ν L) (pwN K (natR K els.length) L))
            (sumK K (fun c => K.mul (F c) (H c)) (cfgs els L))) := by rw [e1, K.one_mul]
      _ = K.mul (K.mul (pwN K ν L) (pwN K ν L)) (K.mul (pwN K (natR K els.length) L)
            (sumK K (fun c => K.mul (F c) (H c)) (cfgs els L))) := by
          rw [K.mul_assoc (pwN K ν L) (pwN K (natR K els.length) L), ← K.mul_assoc (pwN K ν L) (pwN K ν L)]
      _ = _ := by
          rw [ind]
          rw [K.mul_assoc, ← K.mul_assoc (pwN K ν L) (sumK K F _) (sumK K H _), K.mul_comm (pwN K ν L) (sumK K F _),
            K.mul_assoc (sumK K F _), ← K.mul_assoc (pwN K ν L) (sumK K F _)]
          rw [K.mul_comm (sumK K F _) (pwN K ν L)]
  J := fun l F c => K.mul ν (sumK K (fun g => F (setG c l g)) els)
  J_adm := fun _ _ _ => trivial
  J_I := fun l F _ => by
    show K.mul (pwN K ν L) (sumK K (fun c => K.mul ν (sumK K (fun g => F (setG c l g)) els)) (cfgs els L))
      = K.mul (pwN K ν L) (sumK K F (cfgs els L))
    by_cases hl : l < L
    · rw [← mul_sumK, sum_set K els F L l hl, ← K.mul_assoc ν (natR K els.length), hνG, K.one_mul]
    · congr 1
      refine sumK_ext_mem K _ _ (cfgs els L) (fun c hc => ?_)
      have hlen := mem_cfgs_len els L c hc
      rw [sumK_ext K _ _ (fun g => by rw [setG_ge c l g (by rw [hlen]; exact Nat.le_of_not_lt hl)]), sum_const,
        ← K.mul_assoc, hνG, K.one_mul]
  J_mul := fun l T F H _ _ hT hdep c => by
    show K.mul ν (sumK K (fun g => K.mul (F (setG c l g)) (H (setG c l g))) els)
      = K.mul (K.mul ν (sumK K (fun g => F (setG c l g)) els)) (H c)
    have hH : ∀ g, H (setG c l g) = H c := fun g => hdep _ _ (fun l' hl' => by
      by_cases e : l' = l
      · rw [e] at hl'; rw [hT] at hl'; exact absurd hl' Bool.noConfusion
      · exact getO_set c l l' g e)
    rw [sumK_ext K _ _ (fun g => by rw [hH g]), ← sumK_mul, K.mul_assoc]

end HaarFin

#print axioms HaarFin.sum_set
#print axioms HaarFin.finHaar

ym/HaarPoly.lean

/-
  ym/HaarPoly — T45, cycle 59 (Lad3 step 3, any compact gauge group): the exact cluster identity over an abstract
  Haar integral (HaarStr.Haar). Same statement as Poly.cluster_id with the normalised integral I in place of the
  finite sums (no |G|^L factor). Admissibility of the plaquette fluctuations and of χ is assumed.
    adm_prodW, adm_ite, I_sumK      products / masked terms are admissible; I commutes with finite mask sums
    ZW_exp_H, Z0_exp_H              polymer expansions
    pair_fact_H                     I(χ Π_{A∪D} f) = I(χ Π_A f) · I(Π_D f) for D detached (H.indep)
    cluster_id_H                    Z_W(M) = Σ_{A conn ⊆ M} c₀^{|(M∩X(A))∖A|} aW(A) Z₀(M ∖ X(A))

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.HaarFin

namespace HaarPoly

open YMChain LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Ratio Expand Approx Poly Signed
  HaarStr

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) {G : Type} {L : Nat} (H : Haar K O G L)

theorem adm_prodW (f : Nat → List G → R) (hfa : ∀ i, H.adm (f i)) : ∀ (k : Nat) (A : List Bool), H.adm (prodW K f k A)
  | _, [] => H.adm_const K.one
  | k, true :: A => H.adm_mul (f k) (prodW K f (k + 1) A) (hfa k) (adm_prodW f hfa (k + 1) A)
  | k, false :: A => by
    have := H.adm_mul (fun _ => K.one) (prodW K f (k + 1) A) (H.adm_const K.one) (adm_prodW f hfa (k + 1) A)
    exact this

theorem I_sumK {β : Type} (g : β → List G → R) (hg : ∀ b, H.adm (g b)) : ∀ l : List β,
    H.adm (fun x => sumK K (fun b => g b x) l) ∧ H.I (fun x => sumK K (fun b => g b x) l) = sumK K (fun b => H.I (g b)) l
  | [] => ⟨H.adm_const K.zero, I_zero K O H⟩
  | b :: l => by
    have ih := I_sumK g hg l
    refine ⟨H.adm_add _ _ (hg b) ih.1, ?_⟩
    show H.I (fun x => K.add (g b x) (sumK K (fun b => g b x) l)) = K.add (H.I (g b)) (sumK K (fun b => H.I (g b)) l)
    rw [H.I_add _ _ (hg b) ih.1, ih.2]

section
variable (f : Nat → List G → R) (χ : List G → R) (c0 : R)

def aWH (A : List Bool) : R := H.I (fun x => K.mul (χ x) (prodW K f 0 A x))
def a0H (D : List Bool) : R := H.I (prodW K f 0 D)
def ZWH (M : List Bool) : R := H.I (fun x => K.mul (χ x) (prodW K (fun i x => K.add c0 (f i x)) 0 M x))
def Z0H (M : List Bool) : R := H.I (prodW K (fun i x => K.add c0 (f i x)) 0 M)

theorem ite_app {α β : Type} {c : Prop} [Decidable c] (F Z : α → β) (x : α) :
    (if c then F else Z) x = (if c then F x else Z x) := by
  by_cases h : c
  · rw [ip h, ip h]
  · rw [ineg h, ineg h]

theorem adm_ite {c : Prop} [Decidable c] (F : List G → R) (hF : H.adm F) :
    H.adm (if c then F else (fun _ => K.zero)) := by
  by_cases h : c
  · rw [ip h]; exact hF
  · rw [ineg h]; exact H.adm_const K.zero

theorem I_ite {c : Prop} [Decidable c] (F : List G → R) :
    H.I (if c then F else (fun _ => K.zero)) = (if c then H.I F else K.zero) := by
  by_cases h : c
  · rw [ip h, ip h]
  · rw [ineg h, ineg h]; exact I_zero K O H

theorem gen_exp (Ψ : List G → R) (hΨ : H.adm Ψ) (w : Nat → List G → R) (hfa : ∀ i, H.adm (f i))
    (hw : ∀ x, w 0 x = w 0 x) (M : List Bool) :
    H.I (fun x => K.mul (Ψ x) (prodW K (fun i x => K.add c0 (f i x)) 0 M x))
      = sumK K (fun S => if andB S M = S then
          K.mul (pwR K c0 (cnt (andB M (notB S)))) (H.I (fun x => K.mul (Ψ x) (prodW K f 0 S x))) else K.zero)
          (bools M.length) := by
  let g : List Bool → List G → R := fun S => if andB S M = S then
    (fun x => K.mul (pwR K c0 (cnt (andB M (notB S)))) (K.mul (Ψ x) (prodW K f 0 S x))) else (fun _ => K.zero)
  have hg : ∀ S, H.adm (g S) := fun S => adm_ite K O H _
    (H.adm_mul (fun _ => pwR K c0 (cnt (andB M (notB S)))) _ (H.adm_const _) (H.adm_mul Ψ _ hΨ (adm_prodW K O H f hfa 0 S)))
  have e1 : H.I (fun x => K.mul (Ψ x) (prodW K (fun i x => K.add c0 (f i x)) 0 M x))
      = H.I (fun x => sumK K (fun S => g S x) (bools M.length)) := by
    refine H.I_ext _ _ (fun x => ?_)
    rw [expand K c0 f x 0 M, mul_sumK]
    refine sumK_ext K _ _ (fun S => ?_) _
    have ia : g S x = (if andB S M = S then K.mul (pwR K c0 (cnt (andB M (notB S)))) (K.mul (Ψ x) (prodW K f 0 S x))
        else K.zero) := ite_app _ _ x
    rw [ia]
    by_cases e : andB S M = S
    · rw [ip e, ip e, ← K.mul_assoc, K.mul_comm (Ψ x), K.mul_assoc]
    · rw [ineg e, ineg e, DetChain.mul_zero]
  rw [e1, (I_sumK K O H g hg (bools M.length)).2]
  refine sumK_ext K _ _ (fun S => ?_) _
  show H.I (if andB S M = S then _ else _) = _
  rw [I_ite K O H]
  by_cases e : andB S M = S
  · rw [ip e, ip e, H.I_smul _ _ (H.adm_mul Ψ _ hΨ (adm_prodW K O H f hfa 0 S))]
  · rw [ineg e, ineg e]

theorem ZW_exp_H (hfa : ∀ i, H.adm (f i)) (hχa : H.adm χ) (M : List Bool) :
    ZWH K O H f χ c0 M = sumK K (fun S => if andB S M = S then
      K.mul (pwR K c0 (cnt (andB M (notB S)))) (aWH K O H f χ S) else K.zero) (bools M.length) :=
  gen_exp K O H f c0 χ hχa f hfa (fun _ => rfl) M

theorem Z0_exp_H (hfa : ∀ i, H.adm (f i)) (M : List Bool) :
    Z0H K O H f c0 M = sumK K (fun S => if andB S M = S then
      K.mul (pwR K c0 (cnt (andB M (notB S)))) (a0H K O H f S) else K.zero) (bools M.length) := by
  have e := gen_exp K O H f c0 (fun _ => K.one) (H.adm_const _) f hfa (fun _ => rfl) M
  have e1 : Z0H K O H f c0 M = H.I (fun x => K.mul K.one (prodW K (fun i x => K.add c0 (f i x)) 0 M x)) :=
    H.I_ext _ _ (fun x => (K.one_mul _).symm)
  rw [e1, e]
  refine sumK_ext K _ _ (fun S => ?_) _
  by_cases c : andB S M = S
  · rw [ip c, ip c]
    show _ = K.mul _ (H.I (prodW K f 0 S))
    rw [H.I_ext _ (prodW K f 0 S) (fun x => K.one_mul _)]
  · rw [ineg c, ineg c]

variable (P : List (List Nat)) (n : List Nat)

theorem pair_fact_H (hP : ∀ i, i < P.length → (nthL P i).length = L) (hn : n.length = L)
    (hχ : depOn (n.map nzb) χ) (hf : ∀ i, depOn (spP L P i) (f i)) (hfa : ∀ i, H.adm (f i)) (hχa : H.adm χ)
    (A D : List Bool) (hA : A.length = P.length) (hD : D.length = P.length) (hdisj : disjM A D)
    (hDs : ∀ i, i < P.length → nthB D i = true → touch (nthL P i) n = false ∧ anyT P A (nthL P i) = false) :
    aWH K O H f χ (orB A D) = K.mul (aWH K O H f χ A) (a0H K O H f D) := by
  have hsp : ∀ i, (spP L P i).length = L := fun i => len_padB L _
  have hsn : (n.map nzb).length = L := (len_map' n).trans hn
  have hF : depOn (orB (n.map nzb) (uSupp L (spP L P) 0 A)) (fun x => K.mul (χ x) (prodW K f 0 A x)) :=
    depOn_mul K _ _ χ (prodW K f 0 A) (hsn.trans (len_uSupp L _ hsp 0 A).symm) hχ (depOn_prodW K L _ hsp f hf 0 A)
  have hHd : depOn (uSupp L (spP L P) 0 D) (prodW K f 0 D) := depOn_prodW K L _ hsp f hf 0 D
  have ind := H.indep _ _ (fun x => K.mul (χ x) (prodW K f 0 A x)) (prodW K f 0 D)
    (H.adm_mul χ _ hχa (adm_prodW K O H f hfa 0 A)) (adm_prodW K O H f hfa 0 D)
    (len_orB L _ _ hsn (len_uSupp L _ hsp 0 A)) (len_uSupp L _ hsp 0 D) (supp_disj L P n hP hn A D hA hD hDs) hF hHd
  show H.I _ = K.mul (H.I _) (H.I _)
  rw [← ind]
  exact H.I_ext _ _ (fun x => by rw [prodW_or K f x 0 A D (hA.trans hD.symm) hdisj, K.mul_assoc])

theorem cluster_id_H (hP : ∀ i, i < P.length → (nthL P i).length = L) (hn : n.length = L)
    (hχ : depOn (n.map nzb) χ) (hf : ∀ i, depOn (spP L P i) (f i)) (hfa : ∀ i, H.adm (f i)) (hχa : H.adm χ) (M : List Bool) (hM : M.length = P.length) :
    ZWH K O H f χ c0 M
      = sumK K (fun A => if clS (fun q => touch q n) P A = A ∧ andB A M = A then
          K.mul (K.mul (pwR K c0 (cnt (andB (andB M (Xm n P A)) (notB A)))) (aWH K O H f χ A))
            (Z0H K O H f c0 (andB M (notB (Xm n P A)))) else K.zero) (bools P.length) := by
  let σ : List Nat → Bool := fun q => touch q n
  let N := P.length
  have hmem := ChangeVar2D.mem_bools_len N
  let cS : List Bool → List Bool := clS σ P
  let dS : List Bool → List Bool := fun S => andB S (notB (cS S))
  have hc : ∀ S, S.length = N → (cS S).length = N := fun S hS => cl_len (Vs σ P) N (hV σ P) S hS
  have hsplit : ∀ S, S.length = N → S = orB (cS S) (dS S) ∧ disjM (cS S) (dS S) ∧ (dS S).length = S.length :=
    fun S hS => split_mask (cS S) S (cl_le (Vs σ P) N (hV σ P) S hS)
  have hd : ∀ S, S.length = N → (dS S).length = N := fun S hS => (hsplit S hS).2.2.trans hS
  let Φ : List Bool → R := fun S => (if andB S M = S then
    K.mul (pwR K c0 (cnt (andB M (notB S)))) (aWH K O H f χ S) else K.zero)
  rw [ZW_exp_H K O H f χ c0 hfa hχa M, hM]
  show sumK K Φ (bools N) = _
  rw [Signed.bij_sum K N cS dS Φ hc hd (fun S hS => (hsplit S hS).1)]
  refine sumK_ext_mem K _ _ (bools N) (fun A hA => ?_)
  have hAl := hmem A hA
  have hXl : (Xm n P A).length = N := len_Xm n P A hAl
  have hMX : (andB M (notB (Xm n P A))).length = N := (len_andB M _ (by rw [len_notB, hXl, hM])).trans hM
  -- per D
  have term : ∀ D, D.length = N →
      (if A = cS (orB A D) ∧ D = dS (orB A D) then Φ (orB A D) else K.zero)
        = (if clS σ P A = A ∧ andB A M = A then
            (if andB D (andB M (notB (Xm n P A))) = D then
              K.mul (K.mul (pwR K c0 (cnt (andB (andB M (Xm n P A)) (notB A)))) (aWH K O H f χ A))
                (K.mul (pwR K c0 (cnt (andB (andB M (notB (Xm n P A))) (notB D)))) (a0H K O H f D))
            else K.zero) else K.zero) := by
    intro D hDl
    have ce := cond_eq P n A D hAl hDl
    have hADl : (orB A D).length = N := len_orB _ A D hAl hDl
    by_cases cnd : A = cS (orB A D) ∧ D = dS (orB A D)
    · rw [ip cnd]
      have c2 := ce.1 cnd
      have hDX : andB D (notB (Xm n P A)) = D := c2.2
      have off := fun i hi hDi => off_Xm n P A D hAl hDl hDX i hi hDi
      have hdisj : disjM A D := disj_of_off A D (hAl.trans hDl.symm) (fun i hDi => by
        by_cases hi : i < N
        · exact (off i hi hDi).1
        · rw [nthB_ge D i (by rw [hDl]; exact Nat.le_of_not_lt hi)] at hDi; exact absurd hDi Bool.noConfusion)
      show (if andB (orB A D) M = orB A D then _ else K.zero) = _
      by_cases cM : andB (orB A D) M = orB A D
      · have hAM : andB A M = A := sub_of_le A _ M (le_orB_l A D (hAl.trans hDl.symm)) cM
        have hDM : andB D M = D := sub_of_le D _ M (le_orB_r A D (hAl.trans hDl.symm)) cM
        rw [ip cM, ip ⟨c2.1, hAM⟩, ip (and_sub2 D M _ hDM hDX)]
        -- c0-power split
        have hAX : ∀ i, nthB A i = true → nthB (Xm n P A) i = true := fun i hi => by
          have hiN : i < N := by
            cases Nat.lt_or_ge i N with
            | inl h => exact h
            | inr h => rw [nthB_ge A i (by rw [hAl]; exact h)] at hi; exact absurd hi Bool.noConfusion
          rw [Xm, Cluster.nth_orB A _ i (hAl.trans (len_map _ P).symm), hi]; rfl
        have hDX' : ∀ i, nthB D i = true → nthB (Xm n P A) i = false := fun i hi => by
          have hiN : i < N := by
            cases Nat.lt_or_ge i N with
            | inl h => exact h
            | inr h => rw [nthB_ge D i (by rw [hDl]; exact h)] at hi; exact absurd hi Bool.noConfusion
          have o := off i hiN hi
          rw [Xm, Cluster.nth_orB A _ i (hAl.trans (len_map _ P).symm), nth_map _ P i hiN, o.1, o.2.1, o.2.2]; rfl
        have hcnt := cnt_split3 M (Xm n P A) A D (hM.trans hXl.symm) (hAl.trans hXl.symm) (hDl.trans hXl.symm) hAX hDX'
        rw [hcnt, GKS.pwR_add]
        have pf := pair_fact_H K O H f χ P n hP hn hχ hf hfa hχa A D hAl hDl hdisj (fun i hi hDi => ⟨(off i hi hDi).2.1, (off i hi hDi).2.2⟩)
        show K.mul (K.mul (pwR K c0 _) (pwR K c0 _)) (aWH K O H f χ (orB A D)) = _
        rw [pf]
        have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
          rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
        exact mul4 _ _ _ _
      · rw [ineg cM]
        by_cases cA : clS σ P A = A ∧ andB A M = A
        · rw [ip cA]
          by_cases cD : andB D (andB M (notB (Xm n P A))) = D
          · exact absurd (or_sub A D M cA.2 (Approx.andB_assoc_sub D M _ cD (hM.trans (by rw [len_notB, hXl])))) cM
          · rw [ineg cD]
        · rw [ineg cA]
    · rw [ineg cnd]
      by_cases cA : clS σ P A = A ∧ andB A M = A
      · rw [ip cA]
        by_cases cD : andB D (andB M (notB (Xm n P A))) = D
        · exact absurd (ce.2 ⟨cA.1, sub_right D M _ cD⟩) cnd
        · rw [ineg cD]
      · rw [ineg cA]
  rw [sumK_ext_mem K _ _ (bools N) (fun D hD => term D (hmem D hD))]
  by_cases cA : clS σ P A = A ∧ andB A M = A
  · rw [ip cA, Z0_exp_H K O H f c0 hfa _, hMX, mul_sumK]
    refine sumK_ext K _ _ (fun D => ?_) _
    rw [ip cA]
    by_cases cD : andB D (andB M (notB (Xm n P A))) = D
    · rw [ip cD, ip cD]
    · rw [ineg cD, ineg cD, DetChain.mul_zero]
  · rw [ineg cA]
    exact (sumK_ext K _ _ (fun D => ineg cA) _).trans (sum_zero K _)


end

end HaarPoly

#print axioms HaarPoly.I_sumK
#print axioms HaarPoly.ZW_exp_H
#print axioms HaarPoly.Z0_exp_H
#print axioms HaarPoly.pair_fact_H
#print axioms HaarPoly.cluster_id_H

ym/AreaH.lean

/-
  ym/AreaH — T45, cycle 60 (Lad3 step 4, any compact gauge group with a Haar structure, any dimension): the area law.

  Same hypotheses as AreaG.area_G with the normalised Haar integral I in place of finite sums (plaquette fluctuations
  and χ admissible). Conclusion: −B Z₀(M) ≤ Z_W(M) ≤ B Z₀(M), B = Kχ κ^s (1+y)^s u^m, uniform in the volume.
  For SU(2) this is the strong-coupling area law, given the cited Haar integral and a weight with the stated bounds.
    aWH_bnd, Z0H_ratio    the two bounds over a Haar structure (I_bnd, I_mono)
    area_H                the area law

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.HaarPoly
import ym.AreaG

namespace AreaH

open YMChain LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Ratio Expand Approx Poly Signed
  PolyBound AreaG HaarStr HaarPoly

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) {G : Type} {L : Nat} (H : Haar K O G L)

theorem aWH_bnd (f : Nat → List G → R) (χ : List G → R) (ε Kχ : R)
    (he : O.le K.zero ε) (hK : O.le K.zero Kχ) (h1 : O.le K.zero K.one)
    (hf : ∀ i x, O.le (K.neg ε) (f i x) ∧ O.le (f i x) ε) (hχ : ∀ x, O.le (K.neg Kχ) (χ x) ∧ O.le (χ x) Kχ)
    (hfa : ∀ i, H.adm (f i)) (hχa : H.adm χ) (A : List Bool) :
    O.le (K.neg (K.mul K.one (K.mul Kχ (pwR K ε (cnt A))))) (aWH K O H f χ A) ∧
      O.le (aWH K O H f χ A) (K.mul K.one (K.mul Kχ (pwR K ε (cnt A)))) := by
  rw [K.one_mul]
  exact I_bnd K O H _ _ (H.adm_mul χ _ hχa (adm_prodW K O H f hfa 0 A))
    (fun x => bnd_mul K O _ _ _ _ hK (AreaLower.pw_nn K O ε he h1 _) (hχ x) (prodW_bnd K O f ε he h1 hf x 0 A))

theorem Z0H_ratio (f : Nat → List G → R) (c0 m κ : R) (h1 : O.le K.zero K.one)
    (hm : O.le K.zero m) (hm1 : O.le m K.one) (hκ : O.le K.zero κ) (hκm : K.mul κ m = K.one)
    (hw : ∀ i x, O.le m (K.add c0 (f i x))) (hw1 : ∀ i x, O.le (K.add c0 (f i x)) K.one)
    (hfa : ∀ i, H.adm (f i)) (M X : List Bool) (hl : M.length = X.length) :
    O.le (Z0H K O H f c0 (andB M (notB X))) (K.mul (pwR K κ (cnt (andB M X))) (Z0H K O H f c0 M)) := by
  let w : Nat → List G → R := fun i x => K.add c0 (f i x)
  have hwa : ∀ i, H.adm (w i) := fun i => H.adm_add _ _ (H.adm_const c0) (hfa i)
  have hw0 : ∀ k x, O.le K.zero (w k x) := fun k x => O.le_trans _ _ _ hm (hw k x)
  have hr := I_mono K O H (prodW K w 0 (andB M (notB X))) (fun x => K.mul (pwR K κ (cnt (andB M X))) (prodW K w 0 M x))
    (adm_prodW K O H w hwa 0 _) (H.adm_mul (fun _ => _) _ (H.adm_const _) (adm_prodW K O H w hwa 0 M)) (fun x => by
      rw [prodW_split K w x 0 M X hl]
      let P1 := prodW K w 0 (andB M (notB X)) x
      let P2 := prodW K w 0 (andB M X) x
      let k := cnt (andB M X)
      have g := prodW_ge K O w m hm h1 hm1 hw x 0 (andB M X)
      have e1 : K.mul (pwR K m k) (pwR K κ k) = K.one := by
        rw [← GKS2.pwR_mul, K.mul_comm m κ, hκm]; exact pw_one' K _
      have t := O.mul_le_mul_right _ _ _ g (O.mul_nonneg _ _ (AreaLower.pw_nn K O κ hκ h1 k)
        (prodW_nn K O w h1 hw0 x 0 (andB M (notB X))))
      -- t : m^k (κ^k P1) ≤ P2 (κ^k P1)
      rw [← K.mul_assoc, e1, K.one_mul] at t
      show O.le P1 (K.mul (pwR K κ k) (K.mul P1 P2))
      have e2 : K.mul P2 (K.mul (pwR K κ k) P1) = K.mul (pwR K κ k) (K.mul P1 P2) := by
        rw [K.mul_comm P2, K.mul_assoc]
      rw [e2] at t
      exact t)
  rw [H.I_smul _ _ (adm_prodW K O H w hwa 0 M)] at hr
  exact hr

theorem area_H (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (P : List (List Nat)) (n : List Nat)
    (f : Nat → List G → R) (χ : List G → R) (c0 m κ ε Kχ u x y : R) (Dg mA : Nat)
    (hP : ∀ i, i < P.length → (nthL P i).length = L) (hn : n.length = L)
    (hχd : depOn (n.map nzb) χ) (hfd : ∀ i, depOn (spP L P i) (f i)) (hfa : ∀ i, H.adm (f i)) (hχa : H.adm χ)
    (hc0 : O.le K.zero c0) (hc1 : O.le c0 K.one) (hm : O.le K.zero m) (hm1 : O.le m K.one)
    (hκ1 : O.le K.one κ) (hκm : K.mul κ m = K.one)
    (hw : ∀ i x, O.le m (K.add c0 (f i x))) (hw1 : ∀ i x, O.le (K.add c0 (f i x)) K.one)
    (he : O.le K.zero ε) (hf : ∀ i x, O.le (K.neg ε) (f i x) ∧ O.le (f i x) ε)
    (hK : O.le K.zero Kχ) (hχ : ∀ x, O.le (K.neg Kχ) (χ x) ∧ O.le (χ x) Kχ)
    (hu0 : O.le K.zero u) (hu1 : O.le u K.one) (hx : O.le K.zero x) (hy : O.le K.zero y)
    (hdeg : ∀ p, memL p P → cnt (P.map (fun q => touch q p)) ≤ Dg)
    (hsmall : O.le (K.mul ε (pwR K κ (Dg + 1))) (K.mul u x))
    (hxy : O.le (K.mul x (pwR K (K.add K.one y) Dg)) y)
    (hvan : ∀ A : List Bool, A.length = P.length → clS (fun q => touch q n) P A = A → cnt A < mA →
      aWH K O H f χ A = K.zero)
    (M : List Bool) (hM : M.length = P.length) :
    let B := K.mul (K.mul Kχ (pwR K κ (cnt (P.map (fun q => touch q n)))))
      (K.mul (pwR K (K.add K.one y) (cnt (P.map (fun q => touch q n)))) (pwR K u mA))
    O.le (K.neg (K.mul B (Z0H K O H f c0 M))) (ZWH K O H f χ c0 M) ∧
      O.le (ZWH K O H f χ c0 M) (K.mul B (Z0H K O H f c0 M)) := by
  intro B
  let σ : List Nat → Bool := fun q => touch q n
  let N := P.length
  let s := cnt (P.map σ)
  have hmem := ChangeVar2D.mem_bools_len N
  let gL := K.one
  have hg1 : O.le K.one gL := O.le_refl _
  have hg0 : O.le K.zero gL := h1
  have hgne : gL ≠ K.zero := h10
  have hκ : O.le K.zero κ := O.le_trans _ _ _ h1 hκ1
  have Z0nn : ∀ Q : List Bool, O.le K.zero (Z0H K O H f c0 Q) := fun Q =>
    H.I_pos _ (adm_prodW K O H _ (fun i => H.adm_add _ _ (H.adm_const c0) (hfa i)) 0 Q)
      (fun x' => prodW_nn K O (fun i x => K.add c0 (f i x)) h1 (fun i x => O.le_trans _ _ _ hm (hw i x)) x' 0 Q)
  let Z := Z0H K O H f c0 M
  -- per-polymer bound
  let bA : List Bool → R := fun A => if clS σ P A = A then
    K.mul (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K u mA) (K.mul (pwR K x (cnt A)) Z)) else K.zero
  have term_b : ∀ A, A.length = N →
      O.le (K.neg (bA A)) (if clS σ P A = A ∧ andB A M = A then
          K.mul (K.mul (pwR K c0 (cnt (andB (andB M (Xm n P A)) (notB A)))) (aWH K O H f χ A))
            (Z0H K O H f c0 (andB M (notB (Xm n P A)))) else K.zero) ∧
        O.le (if clS σ P A = A ∧ andB A M = A then
          K.mul (K.mul (pwR K c0 (cnt (andB (andB M (Xm n P A)) (notB A)))) (aWH K O H f χ A))
            (Z0H K O H f c0 (andB M (notB (Xm n P A)))) else K.zero) (bA A) := by
    intro A hA
    have bnn : O.le K.zero (bA A) := by
      show O.le K.zero (if _ then _ else _)
      by_cases c : clS σ P A = A
      · rw [ip c]
        exact O.mul_nonneg _ _ (O.mul_nonneg _ _ hg0 (O.mul_nonneg _ _ hK (AreaLower.pw_nn K O κ hκ h1 _)))
          (O.mul_nonneg _ _ (AreaLower.pw_nn K O u hu0 h1 _) (O.mul_nonneg _ _ (AreaLower.pw_nn K O x hx h1 _) (Z0nn M)))
      · rw [ineg c]; exact O.le_refl _
    have zero_ok : O.le (K.neg (bA A)) K.zero ∧ O.le K.zero (bA A) := ⟨by
      have := neg_le_neg K O _ _ bnn
      have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
      rw [e] at this; exact this, bnn⟩
    by_cases cA : clS σ P A = A ∧ andB A M = A
    · rw [ip cA]
      by_cases small : cnt A < mA
      · rw [hvan A hA cA.1 small, DetChain.mul_zero, DetChain.zero_mul']; exact zero_ok
      · have hmA : mA ≤ cnt A := Nat.le_of_not_lt small
        -- factors
        let k := cnt A
        let p := cnt (andB (andB M (Xm n P A)) (notB A))
        have cp : O.le (K.neg K.one) (pwR K c0 p) ∧ O.le (pwR K c0 p) K.one := ⟨O.le_trans _ _ _ (by
            have := neg_le_neg K O _ _ h1
            have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
            rw [e] at this; exact this) (AreaLower.pw_nn K O c0 hc0 h1 p), Tail.pw_le_one K O h1 c0 hc0 hc1 p⟩
        have aB := aWH_bnd K O H f χ ε Kχ he hK h1 hf hχ hfa hχa A
        have hXl : M.length = (Xm n P A).length := hM.trans (len_Xm n P A hA).symm
        have zr := Z0H_ratio K O H f c0 m κ h1 hm hm1 hκ hκm hw hw1 hfa M (Xm n P A) hXl
        -- κ^{|M∩X|} ≤ κ^{s + (Dg+1) k}
        have hcX : cnt (andB M (Xm n P A)) ≤ s + (Dg + 1) * k := by
          refine Nat.le_trans (leB_cnt _ _ (andB_le_r M _ hXl)) (Nat.le_trans (cnt_Xm n P A Dg hdeg) (Nat.le_of_eq ?_))
          show cnt A + s + Dg * cnt A = s + (Dg + 1) * cnt A
          rw [Nat.succ_mul, Nat.add_comm (cnt A) s, Nat.add_assoc, Nat.add_comm (cnt A)]
        obtain ⟨j, hj⟩ := Nat.le.dest hcX
        have kp : O.le (pwR K κ (cnt (andB M (Xm n P A)))) (pwR K κ (s + (Dg + 1) * k)) := by
          rw [← hj]; exact pw_mono K O h1 κ hκ1 _ j
        have z' : O.le (Z0H K O H f c0 (andB M (notB (Xm n P A)))) (K.mul (pwR K κ (s + (Dg + 1) * k)) Z) :=
          O.le_trans _ _ _ zr (O.mul_le_mul_right _ _ _ kp (Z0nn M))
        have zb : O.le (K.neg (K.mul (pwR K κ (s + (Dg + 1) * k)) Z)) (Z0H K O H f c0 (andB M (notB (Xm n P A)))) ∧
            O.le (Z0H K O H f c0 (andB M (notB (Xm n P A)))) (K.mul (pwR K κ (s + (Dg + 1) * k)) Z) :=
          ⟨O.le_trans _ _ _ (by
            have := neg_le_neg K O _ _ (O.mul_nonneg _ _ (AreaLower.pw_nn K O κ hκ h1 (s + (Dg + 1) * k)) (Z0nn M))
            have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
            rw [e] at this; exact this) (Z0nn _), z'⟩
        have b1 := bnd_mul K O _ _ _ _ h1 (O.mul_nonneg _ _ hg0 (O.mul_nonneg _ _ hK (AreaLower.pw_nn K O ε he h1 _))) cp aB
        rw [K.one_mul] at b1
        have b2 := bnd_mul K O _ _ _ _ (O.mul_nonneg _ _ hg0 (O.mul_nonneg _ _ hK (AreaLower.pw_nn K O ε he h1 _)))
          (O.mul_nonneg _ _ (AreaLower.pw_nn K O κ hκ h1 _) (Z0nn M)) b1 zb
        -- compare the bound with bA A
        have hb : O.le (K.mul (K.mul gL (K.mul Kχ (pwR K ε k))) (K.mul (pwR K κ (s + (Dg + 1) * k)) Z)) (bA A) := by
          show O.le _ (if clS σ P A = A then _ else _)
          rw [ip cA.1]
          -- ε^k κ^{s+(Dg+1)k} = κ^s (ε κ^{Dg+1})^k ≤ κ^s (u x)^k ≤ κ^s u^m x^k
          have e1 : K.mul (pwR K ε k) (pwR K κ (s + (Dg + 1) * k))
              = K.mul (pwR K κ s) (pwR K (K.mul ε (pwR K κ (Dg + 1))) k) := by
            rw [GKS.pwR_add, pw_exp_mul K κ (Dg + 1) k, GKS2.pwR_mul, ← K.mul_assoc, K.mul_comm (pwR K ε k) (pwR K κ s),
              K.mul_assoc]
          have t1 : O.le (pwR K (K.mul ε (pwR K κ (Dg + 1))) k) (K.mul (pwR K u mA) (pwR K x k)) :=
            O.le_trans _ _ _ (pw_base_mono K O h1 _ _ (O.mul_nonneg _ _ he (AreaLower.pw_nn K O κ hκ h1 _)) hsmall k)
              (Tail.pw_tail K O h1 u x hu0 hu1 hx mA k hmA)
          have lc : ∀ q e W : R, K.mul q (K.mul e W) = K.mul e (K.mul q W) := fun q e W => by
            rw [← K.mul_assoc, K.mul_comm q e, K.mul_assoc]
          have lhs : K.mul (K.mul gL (K.mul Kχ (pwR K ε k))) (K.mul (pwR K κ (s + (Dg + 1) * k)) Z)
              = K.mul (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K (K.mul ε (pwR K κ (Dg + 1))) k) Z) := by
            rw [GKS.pwR_add, pw_exp_mul K κ (Dg + 1) k, GKS2.pwR_mul]
            rw [K.mul_assoc gL, K.mul_assoc Kχ, K.mul_assoc (pwR K κ s), K.mul_assoc gL, K.mul_assoc Kχ,
              K.mul_assoc (pwR K ε k), lc (pwR K κ s) (pwR K ε k)]
          rw [lhs, K.mul_comm (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K (K.mul ε (pwR K κ (Dg + 1))) k) Z),
            K.mul_comm (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K u mA) (K.mul (pwR K x k) Z))]
          refine O.mul_le_mul_right _ _ _ ?_ (O.mul_nonneg _ _ hg0 (O.mul_nonneg _ _ hK (AreaLower.pw_nn K O κ hκ h1 _)))
          rw [← K.mul_assoc]
          exact O.mul_le_mul_right _ _ _ t1 (Z0nn M)
        exact ⟨O.le_trans _ _ _ (neg_le_neg K O _ _ hb) b2.1, O.le_trans _ _ _ b2.2 hb⟩
    · rw [ineg cA]; exact zero_ok
  -- sum
  have cid : K.mul gL (ZWH K O H f χ c0 M) = _ := (K.one_mul _).trans (cluster_id_H K O H f χ c0 P n hP hn hχd hfd hfa hχa M hM)
  have bs := bnd_sum K O _ bA (bools N) (fun A hA => term_b A (hmem A hA))
  rw [← cid] at bs
  -- Σ bA = gL Kχ κ^s u^m Z · D(x) ≤ gL · B · Z
  have sb : O.le (sumK K bA (bools N)) (K.mul gL (K.mul B Z)) := by
    have d := D_bound K O h1 x y hx hy P Dg (fun v hv => hdeg _ (GPerm.memL_nthL P v hv)) hxy N σ (AreaUpper.tr N)
      (AreaUpper.len_tr _) (by rw [AreaUpper.cnt_tr]; exact Nat.le_refl _)
    have etr' : andB (AreaUpper.tr N) (P.map σ) = P.map σ := by
      have := AreaUpper.andB_tr' (P.map σ); rw [len_map] at this; exact this
    rw [etr'] at d
    have eD : sumK K bA (bools N) = K.mul (K.mul (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K u mA) Z)) (D K x σ P (AreaUpper.tr N)) := by
      show _ = K.mul _ (sumK K _ (bools N))
      rw [mul_sumK]
      refine sumK_ext_mem K _ _ (bools N) (fun A hA => ?_)
      have hAl := hmem A hA
      have etr : andB A (AreaUpper.tr N) = A := by rw [← hAl]; exact AreaUpper.andB_tr A
      show (if clS σ P A = A then _ else _) = K.mul _ (if andB A (AreaUpper.tr N) = A ∧ clS σ P A = A then _ else _)
      by_cases c : clS σ P A = A
      · rw [ip c, ip ⟨etr, c⟩]
        rw [K.mul_assoc (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K u mA) Z), K.mul_assoc (pwR K u mA) Z,
          K.mul_comm Z (pwR K x (cnt A))]
      · rw [ineg c, ineg (fun h => c h.2), DetChain.mul_zero]
    rw [eD]
    have hC : O.le K.zero (K.mul (K.mul gL (K.mul Kχ (pwR K κ s))) (K.mul (pwR K u mA) Z)) :=
      O.mul_nonneg _ _ (O.mul_nonneg _ _ hg0 (O.mul_nonneg _ _ hK (AreaLower.pw_nn K O κ hκ h1 s)))
        (O.mul_nonneg _ _ (AreaLower.pw_nn K O u hu0 h1 mA) (Z0nn M))
    have := O.mul_le_mul_right _ _ _ d hC
    rw [K.mul_comm (D K x σ P (AreaUpper.tr N)), K.mul_comm (pwR K (K.add K.one y) s)] at this
    have r5 : ∀ a b c z e : R, K.mul (K.mul (K.mul a b) (K.mul c z)) e = K.mul a (K.mul (K.mul b (K.mul e c)) z) :=
      fun a b c z e => by
        rw [K.mul_assoc (K.mul a b), K.mul_assoc a, K.mul_assoc b (K.mul e c) z, K.mul_assoc e c z, K.mul_comm e (K.mul c z)]
    refine O.le_trans _ _ _ this ?_
    rw [r5]
    exact O.le_refl _
  -- cancel |G|^L
  have up : O.le (K.mul (ZWH K O H f χ c0 M) gL) (K.mul (K.mul B Z) gL) := by
    rw [K.mul_comm _ gL, K.mul_comm _ gL]; exact O.le_trans _ _ _ bs.2 sb
  have lo : O.le (K.mul (K.neg (K.mul B Z)) gL) (K.mul (ZWH K O H f χ c0 M) gL) := by
    rw [K.mul_comm _ gL, K.mul_comm _ gL, K.mul_comm gL (K.neg _), neg_mul', K.mul_comm (K.mul B Z)]
    exact O.le_trans _ _ _ (neg_le_neg K O _ _ sb) bs.1
  exact ⟨O.pos_cancel _ _ _ hg0 hgne lo, O.pos_cancel _ _ _ hg0 hgne up⟩



end AreaH

#print axioms AreaH.aWH_bnd
#print axioms AreaH.Z0H_ratio
#print axioms AreaH.area_H

ym/VanishH.lean

/-
  ym/VanishH — T45, cycle 61 (Lad3 step 5, any compact group with a Haar structure): singly covered polymers vanish.

  As ym/Vanish, with the abstract partial integral: if link l lies in plaquette i ∈ A only (not in other plaquettes of
  A, not in the loop) and J l f_i = 0 (f_i has mean zero along l — for a class-function fluctuation of U_p with zero
  Haar mean this is translation invariance of Haar measure on the group), then aW(A) = 0; likewise for the loop alone.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AreaH

namespace VanishH

open YMChain LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  HaarStr HaarPoly Vanish

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) {G : Type} {L : Nat} (H : Haar K O G L)

theorem vanish_plaq_H (P : List (List Nat)) (n : List Nat) (f : Nat → List G → R) (χ : List G → R) (hn : n.length = L)
    (hχd : depOn (n.map nzb) χ) (hfd : ∀ i, depOn (spP L P i) (f i)) (hfa : ∀ i, H.adm (f i)) (hχa : H.adm χ)
    (A : List Bool) (hA : A.length = P.length) (i l : Nat) (hi : i < P.length) (hAi : nthB A i = true)
    (hnl : nthB (n.map nzb) l = false)
    (hother : ∀ j, j < P.length → j ≠ i → nthB A j = true → nthB (spP L P j) l = false)
    (hmz : ∀ c, H.J l (f i) c = K.zero) :
    aWH K O H f χ A = K.zero := by
  have hsp : ∀ j, (spP L P j).length = L := fun j => len_padB L _
  have hsn : (n.map nzb).length = L := (len_map' n).trans hn
  let A' := upd A i false
  let T := orB (n.map nzb) (uSupp L (spP L P) 0 A')
  have hT : nthB T l = false := by
    show nthB (orB (n.map nzb) (uSupp L (spP L P) 0 A')) l = false
    rw [Cluster.nth_orB _ _ l (hsn.trans (len_uSupp L _ hsp 0 A').symm), hnl, Bool.false_or]
    cases e : nthB (uSupp L (spP L P) 0 A') l with
    | false => rfl
    | true =>
      obtain ⟨j, hj, hA'j, hsj⟩ := uSupp_nth L (spP L P) hsp l 0 A' e
      rw [Nat.zero_add] at hsj
      have hjP : j < P.length := by rw [← hA, ← len_upd A i false]; exact hj
      have hji : j ≠ i := fun e' => by
        rw [e'] at hA'j
        have : nthB (upd A i false) i = false := by rw [nth_upd A i false i (by rw [hA]; exact hi), ip rfl]
        rw [this] at hA'j; exact Bool.noConfusion hA'j
      have hAj : nthB A j = true := by rw [← nth_upd' A i false j (by rw [hA]; exact hi) hji]; exact hA'j
      rw [hother j hjP hji hAj] at hsj
      exact absurd hsj Bool.noConfusion
  have hHd : depOn T (fun x => K.mul (χ x) (prodW K f 0 A' x)) :=
    depOn_mul K _ _ χ _ (hsn.trans (len_uSupp L _ hsp 0 A').symm) hχd (depOn_prodW K L _ hsp f hfd 0 A')
  have sc := single_cover_H K O H l T (f i) (fun x => K.mul (χ x) (prodW K f 0 A' x)) (hfa i)
    (H.adm_mul χ _ hχa (adm_prodW K O H f hfa 0 A')) hT hHd hmz
  show H.I (fun x => K.mul (χ x) (prodW K f 0 A x)) = K.zero
  rw [← sc]
  refine H.I_ext _ _ (fun x => ?_)
  rw [prodW_pick K f x 0 A i (by rw [hA]; exact hi) hAi, Nat.zero_add, ← K.mul_assoc, K.mul_comm (χ x), K.mul_assoc]

theorem vanish_loop_H (P : List (List Nat)) (f : Nat → List G → R) (χ : List G → R)
    (hfd : ∀ i, depOn (spP L P i) (f i)) (hfa : ∀ i, H.adm (f i)) (hχa : H.adm χ)
    (A : List Bool) (l : Nat)
    (hnone : ∀ j, j < P.length → nthB A j = true → nthB (spP L P j) l = false)
    (hA : A.length = P.length) (hmz : ∀ c, H.J l χ c = K.zero) :
    aWH K O H f χ A = K.zero := by
  have hsp : ∀ j, (spP L P j).length = L := fun j => len_padB L _
  have hT : nthB (uSupp L (spP L P) 0 A) l = false := by
    cases e : nthB (uSupp L (spP L P) 0 A) l with
    | false => rfl
    | true =>
      obtain ⟨j, hj, hAj, hsj⟩ := uSupp_nth L (spP L P) hsp l 0 A e
      rw [Nat.zero_add, hnone j (by rw [← hA]; exact hj) hAj] at hsj
      exact absurd hsj Bool.noConfusion
  exact single_cover_H K O H l _ χ (prodW K f 0 A) hχa (adm_prodW K O H f hfa 0 A) hT
    (depOn_prodW K L _ hsp f hfd 0 A) hmz

end VanishH

#print axioms VanishH.vanish_plaq_H
#print axioms VanishH.vanish_loop_H

ym/DupH.lean

/-
  ym/DupH — T45, cycle 64 (any compact group with a Haar structure): the duplicated system and the vanishing of
  polymers that do not join p and q.

  DupH: a Haar structure H2 on fields with values in G × G (two independent copies), with
    lift_fst/snd    functions of one copy are admissible,
    factor          H2.I(φ(x) ψ(x′)) = H.I φ · H.I ψ            (independence of the copies; product measure)
    swap            H2.I(Φ ∘ swap) = H2.I Φ                    (exchangeability)
  For SU(2): the product Haar measure on SU(2)^L × SU(2)^L (cited with H0). For finite groups: ym/Dup.
    dup_anti_H       antisymmetric Φ ⇒ H2.I Φ = 0
    vanish_join_H    as Join.vanish_join, over H2

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Heat2D

namespace DupH

open YMChain LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Dup HaarStr HaarPoly Join

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) {G : Type} {L : Nat} (H : Haar K O G L)

structure DupHaar where
  H2 : Haar K O (G × G) L
  lift_fst : ∀ φ : List G → R, H.adm φ → H2.adm (fun y => φ (fstL y))
  lift_snd : ∀ φ : List G → R, H.adm φ → H2.adm (fun y => φ (sndL y))
  factor : ∀ φ ψ : List G → R, H.adm φ → H.adm ψ →
    H2.I (fun y => K.mul (φ (fstL y)) (ψ (sndL y))) = K.mul (H.I φ) (H.I ψ)
  swap : ∀ Φ : List (G × G) → R, H2.adm Φ → H2.I (fun y => Φ (swapL y)) = H2.I Φ

variable (D : DupHaar K O H)

theorem dup_anti_H (Φ : List (G × G) → R) (hΦ : D.H2.adm Φ) (h : ∀ y, Φ (swapL y) = K.neg (Φ y)) :
    D.H2.I Φ = K.zero := by
  have e := D.swap Φ hΦ
  rw [D.H2.I_ext _ _ h, I_neg K O D.H2 Φ hΦ] at e
  refine two_zero K O _ ?_
  calc K.add (D.H2.I Φ) (D.H2.I Φ) = K.add (D.H2.I Φ) (K.neg (D.H2.I Φ)) := by rw [e]
    _ = K.zero := K.add_neg _

theorem vanish_join_H (h1 : O.le K.zero K.one) (P : List (List Nat)) (hP : ∀ i, i < P.length → (nthL P i).length = L)
    (p q : List Nat) (hpL : p.length = L) (hqL : q.length = L) (hpq : disjN p q)
    (f : Nat → List (G × G) → R) (Fp Fq : List (G × G) → R)
    (hfd : ∀ i, depOn (spP L P i) (f i)) (hFp : depOn (p.map nzb) Fp) (hFq : depOn (q.map nzb) Fq)
    (hfs : ∀ i y, f i (swapL y) = f i y) (hFpa : ∀ y, Fp (swapL y) = K.neg (Fp y))
    (hfa : ∀ i, D.H2.adm (f i)) (hFpad : D.H2.adm Fp) (hFqad : D.H2.adm Fq)
    (A : List Bool) (hA : A.length = P.length)
    (hnj : ∀ i, i < P.length → nthB (clS (fun r => touch r p) P A) i = true → touch (nthL P i) q = false) :
    aWH K O D.H2 f (fun y => K.mul (Fp y) (Fq y)) A = K.zero := by
  let σp : List Nat → Bool := fun r => touch r p
  let N := P.length
  let Ap := clS σp P A
  let Aq := andB A (notB Ap)
  have hsp : ∀ i, (spP L P i).length = L := fun i => len_padB L _
  have hApl : Ap.length = N := cl_len (Vs σp P) N (hV σp P) A hA
  have hspl := split_mask Ap A (cl_le (Vs σp P) N (hV σp P) A hA)
  have hAql : Aq.length = N := hspl.2.2.trans hA
  have hpn : (p.map nzb).length = L := (len_map' p).trans hpL
  have hqn : (q.map nzb).length = L := (len_map' q).trans hqL
  -- plaquettes of A_q touch neither p nor A_p
  have hoff : ∀ j, j < N → nthB Aq j = true → touch (nthL P j) p = false ∧ anyT P Ap (nthL P j) = false := by
    intro j hj hq'
    have e : nthB (andB A (notB Ap)) j = true := hq'
    rw [nth_andB, Cluster.nth_notB _ j (by rw [hApl]; exact hj)] at e
    cases eA : nthB A j <;> cases eP : nthB Ap j <;> rw [eA, eP] at e
    · exact absurd e Bool.noConfusion
    · exact absurd e Bool.noConfusion
    · cases er : (σp (nthL P j) || anyT P (clS σp P A) (nthL P j)) with
      | true =>
        have := closed σp P A hA j hj eA er
        rw [show clS σp P A = Ap from rfl, eP] at this; exact absurd this Bool.noConfusion
      | false =>
        have hf' : (touch (nthL P j) p || anyT P Ap (nthL P j)) = false := er
        cases e1 : touch (nthL P j) p with
        | true => rw [e1] at hf'; exact absurd hf' Bool.noConfusion
        | false => rw [e1, Bool.false_or] at hf'; exact ⟨rfl, hf'⟩
    · exact absurd e Bool.noConfusion
  -- the two supports
  let S := orB (p.map nzb) (uSupp L (spP L P) 0 Ap)
  let T := orB (q.map nzb) (uSupp L (spP L P) 0 Aq)
  have hSl : S.length = L := len_orB L _ _ hpn (len_uSupp L _ hsp 0 Ap)
  have hTl : T.length = L := len_orB L _ _ hqn (len_uSupp L _ hsp 0 Aq)
  have spnth : ∀ i l, i < N → nthB (spP L P i) l = true → nthB ((nthL P i).map nzb) l = true := fun i l hi h => by
    unfold spP at h; rw [padB_eq L _ ((len_map' _).trans (hP i hi))] at h; exact h
  have hST : disjM S T := by
    refine disj_of_off S T (hSl.trans hTl.symm) (fun l hl => ?_)
    show nthB (orB (p.map nzb) (uSupp L (spP L P) 0 Ap)) l = false
    rw [Cluster.nth_orB _ _ l (hpn.trans (len_uSupp L _ hsp 0 Ap).symm)]
    have hT' : nthB (orB (q.map nzb) (uSupp L (spP L P) 0 Aq)) l = true := hl
    rw [Cluster.nth_orB _ _ l (hqn.trans (len_uSupp L _ hsp 0 Aq).symm)] at hT'
    cases eq : nthB (q.map nzb) l with
    | true =>
      -- l is a link of q
      have hpl : nthB (p.map nzb) l = false := by
        cases e : nthB (p.map nzb) l
        · rfl
        · rw [disjN_nth p q l hpq e] at eq; exact absurd eq Bool.noConfusion
      rw [hpl, Bool.false_or]
      cases eu : nthB (uSupp L (spP L P) 0 Ap) l with
      | false => rfl
      | true =>
        obtain ⟨i, hi, hAi, hsi⟩ := uSupp_nth L (spP L P) hsp l 0 Ap eu
        rw [Nat.zero_add] at hsi
        have hiN : i < N := by rw [← hApl]; exact hi
        have := touch_of_nth _ _ l (spnth i l hiN hsi) eq
        rw [hnj i hiN hAi] at this; exact absurd this Bool.noConfusion
    | false =>
      rw [eq, Bool.false_or] at hT'
      obtain ⟨j, hj, hAqj, hsj⟩ := uSupp_nth L (spP L P) hsp l 0 Aq hT'
      rw [Nat.zero_add] at hsj
      have hjN : j < N := by rw [← hAql]; exact hj
      have o := hoff j hjN hAqj
      cases ep : nthB (p.map nzb) l with
      | true => have := touch_of_nth _ _ l (spnth j l hjN hsj) ep; rw [o.1] at this; exact absurd this Bool.noConfusion
      | false =>
        rw [Bool.false_or]
        cases eu : nthB (uSupp L (spP L P) 0 Ap) l with
        | false => rfl
        | true =>
          obtain ⟨i, hi, hAi, hsi⟩ := uSupp_nth L (spP L P) hsp l 0 Ap eu
          rw [Nat.zero_add] at hsi
          have hiN : i < N := by rw [← hApl]; exact hi
          have := anyT_of P Ap (nthL P j) i hAi (touch_of_nth _ _ l (spnth j l hjN hsj) (spnth i l hiN hsi)) hiN
          rw [o.2] at this; exact absurd this Bool.noConfusion
  -- factorization and antisymmetry
  have hF : depOn S (fun y => K.mul (Fp y) (prodW K f 0 Ap y)) :=
    depOn_mul K _ _ Fp _ (hpn.trans (len_uSupp L _ hsp 0 Ap).symm) hFp (depOn_prodW K L _ hsp f hfd 0 Ap)
  have hH : depOn T (fun y => K.mul (Fq y) (prodW K f 0 Aq y)) :=
    depOn_mul K _ _ Fq _ (hqn.trans (len_uSupp L _ hsp 0 Aq).symm) hFq (depOn_prodW K L _ hsp f hfd 0 Aq)
  have ind := D.H2.indep S T _ _ (D.H2.adm_mul _ _ hFpad (adm_prodW K O D.H2 f hfa 0 Ap))
    (D.H2.adm_mul _ _ hFqad (adm_prodW K O D.H2 f hfa 0 Aq)) hSl hTl hST hF hH
  have z : D.H2.I (fun y => K.mul (Fp y) (prodW K f 0 Ap y)) = K.zero :=
    dup_anti_H K O H D _ (D.H2.adm_mul _ _ hFpad (adm_prodW K O D.H2 f hfa 0 Ap))
      (fun y => by rw [hFpa, prodW_swap K f hfs y 0 Ap, Signed.neg_mul'])
  rw [z, DetChain.zero_mul'] at ind
  show D.H2.I _ = K.zero
  rw [← ind]
  refine D.H2.I_ext _ _ (fun y => ?_)
  show K.mul (K.mul (Fp y) (Fq y)) (prodW K f 0 A y) = _
  have eA : A = orB Ap Aq := hspl.1
  conv => lhs; rw [eA]
  rw [prodW_or K f y 0 Ap Aq (hApl.trans hAql.symm) hspl.2.1]
  have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
    rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
  exact mul4 _ _ _ _

end DupH

#print axioms DupH.dup_anti_H
#print axioms DupH.vanish_join_H

ym/DupAlgH.lean

/-
  ym/DupAlgH — T45, cycle 65 (compact groups): algebra of the duplicated system over a Haar structure.
    Zdup0_H     H2.I(Π_i w_i(x) w_i(x′)) = Z₀²
    ZdupW_H     H2.I((U_p − U_p′)(U_q − U_q′) Π w w′) = D + D,  D = Z_{pq} Z₁ − Z_p Z_q   (Z_X = H.I(X Π w))

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.DupH
import ym.DupAlg

namespace DupAlgH

open YMChain LinkFactor MaskSum Closure Factor Seeds Indep PosRatio Dup DupAlg HaarStr HaarPoly DupH

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) {G : Type} {L : Nat} (H : Haar K O G L) (D : DupHaar K O H)

section
variable (w : Nat → List G → R) (M : List Bool)

def ZxH (X : List G → R) : R := H.I (fun x => K.mul (X x) (prodW K w 0 M x))

theorem Zdup0_H (hwa : ∀ i, H.adm (w i)) :
    D.H2.I (fun y => prodW K (What K w) 0 M y) = K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)) := by
  have pw := adm_prodW K O H w hwa 0 M
  show D.H2.I (fun y => prodW K (fun i y => K.mul (w i (fstL y)) (w i (sndL y))) 0 M y) = _
  rw [D.H2.I_ext _ _ (fun y => prodW_prod K w y 0 M), D.factor _ _ pw pw]
  unfold ZxH
  rw [H.I_ext (fun x => K.mul K.one (prodW K w 0 M x)) _ (fun x => K.one_mul _)]

theorem ZdupW_H (hwa : ∀ i, H.adm (w i)) (Up Uq : List G → R) (hUp : H.adm Up) (hUq : H.adm Uq) :
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    D.H2.I (fun y => K.mul (K.mul (K.add (Up (fstL y)) (K.neg (Up (sndL y)))) (K.add (Uq (fstL y)) (K.neg (Uq (sndL y)))))
        (prodW K (What K w) 0 M y)) = K.add Dd Dd := by
  intro Dd
  have nm := Signed.neg_mul' K
  have nn := Signed.neg_neg' K
  have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
    rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
  have pt : ∀ y : List (G × G),
      K.mul (K.mul (K.add (Up (fstL y)) (K.neg (Up (sndL y)))) (K.add (Uq (fstL y)) (K.neg (Uq (sndL y)))))
        (prodW K (What K w) 0 M y)
      = K.add (K.add (K.mul (K.mul (K.mul (Up (fstL y)) (Uq (fstL y))) (prodW K w 0 M (fstL y))) (K.mul K.one (prodW K w 0 M (sndL y))))
                (K.neg (K.mul (K.mul (Up (fstL y)) (prodW K w 0 M (fstL y))) (K.mul (Uq (sndL y)) (prodW K w 0 M (sndL y))))))
          (K.add (K.neg (K.mul (K.mul (Uq (fstL y)) (prodW K w 0 M (fstL y))) (K.mul (Up (sndL y)) (prodW K w 0 M (sndL y)))))
                (K.mul (K.mul K.one (prodW K w 0 M (fstL y))) (K.mul (K.mul (Up (sndL y)) (Uq (sndL y))) (prodW K w 0 M (sndL y))))) := by
    intro y
    show K.mul _ (prodW K (fun i y => K.mul (w i (fstL y)) (w i (sndL y))) 0 M y) = _
    rw [prodW_prod K w y 0 M]
    generalize Up (fstL y) = a
    generalize Up (sndL y) = a'
    generalize Uq (fstL y) = b
    generalize Uq (sndL y) = b'
    generalize prodW K w 0 M (fstL y) = W
    generalize prodW K w 0 M (sndL y) = W'
    rw [DetChain.right_distrib, K.distrib, K.distrib, DetChain.right_distrib, DetChain.right_distrib, DetChain.right_distrib]
    congr 1
    · congr 1
      · rw [K.one_mul, ← K.mul_assoc]
      · rw [K.mul_comm a (K.neg b'), nm b' a, nm (K.mul b' a) (K.mul W W'), K.mul_comm b' a, mul4]
    · congr 1
      · rw [nm a' b, nm (K.mul a' b) (K.mul W W'), K.mul_comm a' b, mul4]
      · rw [nm a' (K.neg b'), K.mul_comm a' (K.neg b'), nm b' a', nn, K.one_mul, K.mul_comm b' a',
          K.mul_comm (K.mul a' b') (K.mul W W'), K.mul_assoc, K.mul_comm W' (K.mul a' b')]
  have pw := adm_prodW K O H w hwa 0 M
  have a1 : H.adm (fun x => K.mul (K.mul (Up x) (Uq x)) (prodW K w 0 M x)) := H.adm_mul _ _ (H.adm_mul _ _ hUp hUq) pw
  have a0 : H.adm (fun x => K.mul K.one (prodW K w 0 M x)) := H.adm_mul _ _ (H.adm_const _) pw
  have ap : H.adm (fun x => K.mul (Up x) (prodW K w 0 M x)) := H.adm_mul _ _ hUp pw
  have aq : H.adm (fun x => K.mul (Uq x) (prodW K w 0 M x)) := H.adm_mul _ _ hUq pw
  have L2 : ∀ φ ψ : List G → R, H.adm φ → H.adm ψ → D.H2.adm (fun y => K.mul (φ (fstL y)) (ψ (sndL y))) :=
    fun φ ψ hφ hψ => D.H2.adm_mul _ _ (D.lift_fst φ hφ) (D.lift_snd ψ hψ)
  rw [D.H2.I_ext _ _ pt]
  rw [D.H2.I_add _ _ (D.H2.adm_add _ _ (L2 _ _ a1 a0) (D.H2.adm_neg _ (L2 _ _ ap aq)))
      (D.H2.adm_add _ _ (D.H2.adm_neg _ (L2 _ _ aq ap)) (L2 _ _ a0 a1)),
    D.H2.I_add _ _ (L2 _ _ a1 a0) (D.H2.adm_neg _ (L2 _ _ ap aq)),
    D.H2.I_add _ _ (D.H2.adm_neg _ (L2 _ _ aq ap)) (L2 _ _ a0 a1),
    I_neg K O D.H2 _ (L2 _ _ ap aq), I_neg K O D.H2 _ (L2 _ _ aq ap),
    D.factor _ _ a1 a0, D.factor _ _ ap aq, D.factor _ _ aq ap, D.factor _ _ a0 a1]
  show K.add (K.add (K.mul (ZxH K O H w M _) (ZxH K O H w M _)) (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq))))
      (K.add (K.neg (K.mul (ZxH K O H w M Uq) (ZxH K O H w M Up))) (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M _))) = _
  rw [K.mul_comm (ZxH K O H w M Uq), K.add_comm (K.neg _) (K.mul (ZxH K O H w M (fun _ => K.one)) _),
    K.mul_comm (ZxH K O H w M (fun _ => K.one))]

end

end DupAlgH

#print axioms DupAlgH.Zdup0_H
#print axioms DupAlgH.ZdupW_H

ym/GapH.lean

/-
  ym/GapH — T45, cycle 65 (compact groups with a Haar structure and its duplicate, any dimension): exponential
  clustering at strong coupling, uniform in the volume — GapG.gap_G over Haar.
    gap_H     −B Z₁² ≤ 2 (Z_{U_pU_q} Z₁ − Z_{U_p} Z_{U_q}) ≤ B Z₁²,   B = (2K₁)² κ̂^ŝ (1+y)^ŝ u^R
  For SU(2): the strong-coupling mass gap (clustering of plaquette correlations with rate −ln u per closure step) in
  every dimension, uniformly in the volume, given the cited Haar measure (H0) and its product (DupHaar).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.DupAlgH
import ym.GapG

namespace GapH

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) {G : Type} {L : Nat} (H : Haar K O G L)

theorem gap_H (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (P : List (List Nat)) (hP : ∀ i, i < P.length → (nthL P i).length = L)
    (p q : List Nat) (hpL : p.length = L) (hqL : q.length = L) (hpq : disjN p q)
    (f : Nat → List G → R) (Up Uq : List G → R) (c0 m κ ε K1 u x y : R) (Dg Rr : Nat)
    (hfd : ∀ i, depOn (spP L P i) (f i)) (hUp : depOn (p.map nzb) Up) (hUq : depOn (q.map nzb) Uq)
    (hfaG : ∀ i, H.adm (f i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (hc0 : O.le K.zero c0) (hc1 : O.le c0 K.one) (hm : O.le K.zero m) (hm1 : O.le m K.one)
    (hκ1 : O.le K.one κ) (hκm : K.mul κ m = K.one)
    (hw : ∀ i x, O.le m (K.add c0 (f i x))) (hw1 : ∀ i x, O.le (K.add c0 (f i x)) K.one)
    (he : O.le K.zero ε) (hf : ∀ i x, O.le (K.neg ε) (f i x) ∧ O.le (f i x) ε)
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hu0 : O.le K.zero u) (hu1 : O.le u K.one) (hx : O.le K.zero x) (hy : O.le K.zero y)
    (hdeg : ∀ r, memL r P → cnt (P.map (fun q' => touch q' r)) ≤ Dg)
    (hsmall : O.le (K.mul (K.add (K.add ε ε) (K.mul ε ε)) (pwR K (K.mul κ κ) (Dg + 1))) (K.mul u x))
    (hxy : O.le (K.mul x (pwR K (K.add K.one y) Dg)) y)
    (hfar : ∀ i, i < P.length → nthB (ball (fun r => touch r p) P Rr) i = true → touch (nthL P i) q = false)
    (M : List Bool) (hM : M.length = P.length) :
    let w : Nat → List G → R := fun i x => K.add c0 (f i x)
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let s := cnt (P.map (fun r => touch r (zadd p q)))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) s))
      (K.mul (pwR K (K.add K.one y) s) (pwR K u Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro w Dd s B
  let fh : Nat → List (G × G) → R := fun i yy =>
    K.add (K.add (K.mul c0 (f i (fstL yy))) (K.mul c0 (f i (sndL yy)))) (K.mul (f i (fstL yy)) (f i (sndL yy)))
  let Fp : List (G × G) → R := fun yy => K.add (Up (fstL yy)) (K.neg (Up (sndL yy)))
  let Fq : List (G × G) → R := fun yy => K.add (Uq (fstL yy)) (K.neg (Uq (sndL yy)))
  let χh : List (G × G) → R := fun yy => K.mul (Fp yy) (Fq yy)
  have whd : ∀ i yy, K.add (K.mul c0 c0) (fh i yy) = What K w i yy := fun i yy => wid K c0 _ _
  -- dependence
  have hfd' : ∀ i, depOn (spP L P i) (fh i) := fun i =>
    depOn_add K _ _ _ (depOn_add K _ _ _
      (depOn_mul' K _ (fun _ => c0) _ (depOn_const _ _) (dep_fst _ _ (hfd i)))
      (depOn_mul' K _ (fun _ => c0) _ (depOn_const _ _) (dep_snd _ _ (hfd i))))
      (depOn_mul' K _ _ _ (dep_fst _ _ (hfd i)) (dep_snd _ _ (hfd i)))
  have hFp : depOn (p.map nzb) Fp := depOn_add K _ _ _ (dep_fst _ _ hUp) (depOn_neg K _ _ (dep_snd _ _ hUp))
  have hFq : depOn (q.map nzb) Fq := depOn_add K _ _ _ (dep_fst _ _ hUq) (depOn_neg K _ _ (dep_snd _ _ hUq))
  have hpn : (p.map nzb).length = (q.map nzb).length := (len_map' p).trans (hpL.trans (hqL.symm.trans (len_map' q).symm))
  have hχd : depOn ((zadd p q).map nzb) χh :=
    depOn_mono _ _ χh (supp_zadd p q (hpL.trans hqL.symm)) (depOn_mul K _ _ Fp Fq hpn hFp hFq)
  have hnL : (zadd p q).length = L := (AreaLower.len_zadd_le p q (by rw [hpL, hqL]; exact Nat.le_refl _)).trans hpL
  -- symmetry
  have hfs : ∀ i yy, fh i (swapL yy) = fh i yy := fun i yy => by
    show K.add (K.add (K.mul c0 (f i (fstL (swapL yy)))) (K.mul c0 (f i (sndL (swapL yy))))) (K.mul (f i (fstL (swapL yy))) (f i (sndL (swapL yy)))) = _
    rw [fst_swap, snd_swap, K.add_comm (K.mul c0 (f i (sndL yy))), K.mul_comm (f i (sndL yy))]
  have hFpa : ∀ yy, Fp (swapL yy) = K.neg (Fp yy) := fun yy => by
    show K.add (Up (fstL (swapL yy))) (K.neg (Up (sndL (swapL yy)))) = K.neg (K.add (Up (fstL yy)) (K.neg (Up (sndL yy))))
    rw [fst_swap, snd_swap, neg_add' K, neg_neg' K, K.add_comm]
  have hfaH : ∀ i, DH.H2.adm (fh i) := fun i =>
    DH.H2.adm_add _ _ (DH.H2.adm_add _ _ (DH.H2.adm_mul _ _ (DH.H2.adm_const c0) (DH.lift_fst _ (hfaG i)))
      (DH.H2.adm_mul _ _ (DH.H2.adm_const c0) (DH.lift_snd _ (hfaG i))))
      (DH.H2.adm_mul _ _ (DH.lift_fst _ (hfaG i)) (DH.lift_snd _ (hfaG i)))
  have hFpad : DH.H2.adm Fp := DH.H2.adm_add _ _ (DH.lift_fst _ hUpa) (DH.H2.adm_neg _ (DH.lift_snd _ hUpa))
  have hFqad : DH.H2.adm Fq := DH.H2.adm_add _ _ (DH.lift_fst _ hUqa) (DH.H2.adm_neg _ (DH.lift_snd _ hUqa))
  have hχad : DH.H2.adm χh := DH.H2.adm_mul _ _ hFpad hFqad
  -- constants
  have hb1 : ∀ i x', O.le (K.neg K.one) (K.add c0 (f i x')) ∧ O.le (K.add c0 (f i x')) K.one := fun i x' =>
    bnd_unit K O h1 _ (O.le_trans _ _ _ hm (hw i x')) (hw1 i x')
  have hc0b := bnd_unit K O h1 c0 hc0 hc1
  have hfh : ∀ i yy, O.le (K.neg (K.add (K.add ε ε) (K.mul ε ε))) (fh i yy) ∧ O.le (fh i yy) (K.add (K.add ε ε) (K.mul ε ε)) := by
    intro i yy
    have a1 := bnd_mul K O _ _ _ _ h1 he hc0b (hf i (fstL yy))
    have a2 := bnd_mul K O _ _ _ _ h1 he hc0b (hf i (sndL yy))
    have a3 := bnd_mul K O _ _ _ _ he he (hf i (fstL yy)) (hf i (sndL yy))
    rw [K.one_mul] at a1 a2
    exact bnd_add K O _ _ _ _ (bnd_add K O _ _ _ _ a1 a2) a3
  have hwh : ∀ i yy, O.le (K.mul m m) (K.add (K.mul c0 c0) (fh i yy)) := fun i yy => by
    rw [whd]; exact Gap.mul_le_mul4 K O _ _ _ _ hm hm (O.le_trans _ _ _ hm (hw i _)) (hw i _) (hw i _)
  have hwh1 : ∀ i yy, O.le (K.add (K.mul c0 c0) (fh i yy)) K.one := fun i yy => by
    rw [whd]
    have := Gap.mul_le_mul4 K O _ _ _ _ (O.le_trans _ _ _ hm (hw i (fstL yy))) (O.le_trans _ _ _ hm (hw i (sndL yy))) h1
      (hw1 i _) (hw1 i _)
    rw [K.one_mul] at this; exact this
  have hκm' : K.mul (K.mul κ κ) (K.mul m m) = K.one := by
    rw [K.mul_assoc, ← K.mul_assoc κ m m, K.mul_comm κ m, K.mul_assoc m κ m, hκm, K.mul_comm m K.one, K.one_mul, hκm]
  have hFpb : ∀ yy, O.le (K.neg (K.add K1 K1)) (Fp yy) ∧ O.le (Fp yy) (K.add K1 K1) := fun yy =>
    bnd_add K O _ _ _ _ (hUpb _) (bnd_neg K O _ _ (hUpb _))
  have hFqb : ∀ yy, O.le (K.neg (K.add K1 K1)) (Fq yy) ∧ O.le (Fq yy) (K.add K1 K1) := fun yy =>
    bnd_add K O _ _ _ _ (hUqb _) (bnd_neg K O _ _ (hUqb _))
  have hK2 : O.le K.zero (K.add K1 K1) := by have := O.add_le_add _ _ _ _ hK1 hK1; rw [K.zero_add] at this; exact this
  have hχb : ∀ yy, O.le (K.neg (K.mul (K.add K1 K1) (K.add K1 K1))) (χh yy) ∧ O.le (χh yy) (K.mul (K.add K1 K1) (K.add K1 K1)) :=
    fun yy => bnd_mul K O _ _ _ _ hK2 hK2 (hFpb yy) (hFqb yy)
  have he2 : O.le K.zero (K.add (K.add ε ε) (K.mul ε ε)) := by
    have a := O.add_le_add _ _ _ _ he he; rw [K.zero_add] at a
    have b := O.add_le_add _ _ _ _ a (O.mul_nonneg _ _ he he); rw [K.zero_add] at b; exact b
  -- vanishing of small polymers
  have hvan : ∀ A : List Bool, A.length = P.length → clS (fun r => touch r (zadd p q)) P A = A → cnt A < Rr →
      aWH K O DH.H2 fh χh A = K.zero := by
    intro A hA _ hsm
    refine vanish_join_H K O H DH h1 P hP p q hpL hqL hpq fh Fp Fq hfd' hFp hFq hfs hFpa hfaH hFpad hFqad A hA (fun i hi hcl => ?_)
    cases e : touch (nthL P i) q with
    | false => rfl
    | true =>
      exfalso
      let σp : List Nat → Bool := fun r => touch r p
      have hb : nthB (ball σp P Rr) i = false := by
        cases eb : nthB (ball σp P Rr) i
        · rfl
        · rw [hfar i hi eb] at e; exact absurd e Bool.noConfusion
      have hcl_len : (clS σp P A).length = P.length := cl_len (Vs σp P) P.length (hV σp P) A hA
      have hR := reach σp P (clS σp P A) hcl_len (cl_idem (Vs σp P) P.length (hV σp P) A hA) Rr i hcl hb
      have hle := leB_cnt _ _ (cl_le (Vs σp P) P.length (hV σp P) A hA)
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le hsm (Nat.le_trans hR hle))
  -- area_G in the duplicated system
  have ag := area_H K O DH.H2 h1 h10 P (zadd p q) fh χh (K.mul c0 c0) (K.mul m m) (K.mul κ κ)
    (K.add (K.add ε ε) (K.mul ε ε)) (K.mul (K.add K1 K1) (K.add K1 K1)) u x y Dg Rr hP hnL hχd hfd' hfaH hχad
    (O.mul_nonneg _ _ hc0 hc0) (by have := Gap.mul_le_mul4 K O _ _ _ _ hc0 hc0 h1 hc1 hc1; rw [K.one_mul] at this; exact this)
    (O.mul_nonneg _ _ hm hm) (by have := Gap.mul_le_mul4 K O _ _ _ _ hm hm h1 hm1 hm1; rw [K.one_mul] at this; exact this)
    (GPerm.one_le_mul K O _ _ hκ1 hκ1 h1) hκm' hwh hwh1 he2 hfh
    (O.mul_nonneg _ _ hK2 hK2) hχb hu0 hu1 hx hy hdeg hsmall hxy hvan M hM
  -- translate
  have hwa : ∀ i, H.adm (w i) := fun i => H.adm_add _ _ (H.adm_const c0) (hfaG i)
  have eZ0 : Z0H K O DH.H2 fh (K.mul c0 c0) M = K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)) := by
    show DH.H2.I (prodW K (fun i yy => K.add (K.mul c0 c0) (fh i yy)) 0 M) = _
    rw [DH.H2.I_ext _ _ (fun yy => prodW_congr K _ _ whd yy 0 M)]
    exact Zdup0_H K O H DH w M hwa
  have eZW : ZWH K O DH.H2 fh χh (K.mul c0 c0) M = K.add Dd Dd := by
    show DH.H2.I (fun yy => K.mul (χh yy) (prodW K (fun i yy => K.add (K.mul c0 c0) (fh i yy)) 0 M yy)) = _
    rw [DH.H2.I_ext _ _ (fun yy => by rw [prodW_congr K _ _ whd yy 0 M])]
    exact ZdupW_H K O H DH w M hwa Up Uq hUpa hUqa
  have ag' := ag
  simp only at ag'
  rw [eZ0, eZW] at ag'
  exact ag'

end GapH

#print axioms GapH.gap_H

ym/Center.lean

/-
  ym/Center — T45, cycle 94 (Lad2-inst step 1): center symmetry kills odd integrands.

  For a finite gauge group with a central element z (SU(2) and 2I: z = −1), shifting one link variable by z is a
  bijection of the group. An integrand F that changes sign under this shift (F(c[l := z g]) = −F(c[l := g])) has
    odd_mean     mean zero along the link l
    odd_vanish   total sum zero over all configurations
  In the strong-coupling expansion every plaquette factor splits into a center-even and a center-odd part; a term in
  which some link is touched by an odd number of center-odd factors (the fundamental Wilson loop counts as odd)
  vanishes. So the odd plaquettes O of a nonzero term satisfy ∂O = W (mod 2), and RectLoop.rect_area gives |O| ≥ RT:
  the area of the double cover is the area of the loop (Lad2-inst).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.RectLoop

namespace Center

open YMChain LinkFactor FiniteRecChain Indep MeanZero

variable {R : Type} (K : DetChain.CRing R) {G : Type} [DecidableEq G]

theorem sumK_neg' {A : Type} (f : A → R) : ∀ l : List A, sumK K (fun a => K.neg (f a)) l = K.neg (sumK K f l)
  | [] => DetChain.neg_unique K _ _ (K.zero_add K.zero)
  | a :: l => by
    show K.add (K.neg (f a)) (sumK K (fun a => K.neg (f a)) l) = K.neg (K.add (f a) (sumK K f l))
    rw [sumK_neg' f l]
    apply DetChain.neg_unique
    rw [DetChain.add_mid4, DetChain.add_neg', DetChain.add_neg', K.zero_add]

/-- In an ordered ring, x = −x forces x = 0. -/
theorem neg_self_zero (O : ORing K) (x : R) (h : x = K.neg x) : x = K.zero := by
  cases O.le_total K.zero x with
  | inl hx =>
    have a := O.add_le_add _ _ _ _ hx (O.le_refl (K.neg x))
    rw [K.zero_add, K.add_neg, ← h] at a
    exact O.le_antisymm _ _ a hx
  | inr hx =>
    have a := O.add_le_add _ _ _ _ hx (O.le_refl (K.neg x))
    rw [K.zero_add, K.add_neg, ← h] at a
    exact O.le_antisymm _ _ hx a

/-- **Odd integrands have mean zero along the link.** -/
theorem odd_mean (O : ORing K) (els : List G) (hc : Complete K els) (zm zi : G → G) (h1 : ∀ g, zi (zm g) = g) (h2 : ∀ g, zm (zi g) = g)
    (F : List G → R) (l : Nat) (hodd : ∀ c g, F (setG c l (zm g)) = K.neg (F (setG c l g))) (c : List G) :
    sumK K (fun g => F (setG c l g)) els = K.zero := by
  apply neg_self_zero K O
  have r := reindex K els hc zm zi h1 h2 (fun g => F (setG c l g))
  exact r.symm.trans ((sumK_ext K _ _ (fun g => hodd c g) els).trans (sumK_neg' K _ els))

/-- **Odd integrands sum to zero.** -/
theorem odd_vanish (O : ORing K) (els : List G) (hc : Complete K els) (zm zi : G → G) (h1 : ∀ g, zi (zm g) = g)
    (h2 : ∀ g, zm (zi g) = g) (L l : Nat) (hl : l < L)
    (F : List G → R) (hodd : ∀ c g, F (setG c l (zm g)) = K.neg (F (setG c l g))) :
    sumK K F (cfgs els L) = K.zero := by
  have sc := single_cover K els L l [] F (fun _ => K.one) hl rfl (fun _ _ _ => rfl)
    (odd_mean K O els hc zm zi h1 h2 F l hodd)
  rw [← sc]
  exact sumK_ext K _ _ (fun c => by rw [K.mul_comm, K.one_mul]) _

end Center

#print axioms Center.neg_self_zero
#print axioms Center.odd_mean
#print axioms Center.odd_vanish

ym/CenterExp.lean

/-
  ym/CenterExp — T45, cycle 95 (Lad2-inst step 2): the center-graded polymer expansion and the area of polymers.

  Split each fluctuation f_i = e_i + o_i into a center-even part (unchanged when a link is shifted by z) and a
  center-odd part (sign (−1)^{P_i(l)} when link l is shifted); the loop observable χ has sign (−1)^{n(l)}.
    expand2        Π_{i∈A}(e_i + o_i) = Σ_{S ⊆ A} Π_{A∖S} e · Π_S o
    sign_prod      shifting link l multiplies Π_S o by (−1)^{(∂S)(l)}  (∂S = bdS P S)
    term_vanish    if n + ∂S is not even, Σ_x χ(x) Π_D e(x) Π_S o(x) = 0               (Center.odd_vanish)
    center_area    if every S with n + ∂S even has |S| ≥ m (RectLoop.rect_area: m = RT), then aW(A) = 0 whenever |A| < m
  This is the hypothesis hvan of AreaG.area_G (area of the double cover) for every finite group with a central z of
  order two acting by −1 on the loop representation — SU(2) subgroups such as 2I in the fundamental representation.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Center

namespace CenterExp

open YMChain LinkIntegral MaskSum Closure Factor Seeds Count Indep PosRatio Ratio Expand Poly Signed MeanZero
  LinkFactor FiniteRecChain StrongZ2 GKS Tail Degree AreaBound

variable {R : Type} (K : DetChain.CRing R) {G : Type}

def sg (b : Bool) (x : R) : R := if b = true then x else K.neg x

theorem neg_mul_neg (a b : R) : K.mul (K.neg a) (K.neg b) = K.mul a b := by
  rw [neg_mul' K, K.mul_comm a (K.neg b), neg_mul' K, neg_neg' K, K.mul_comm]

theorem sg_mul (a b : Bool) (x y : R) : sg K (a == b) (K.mul x y) = K.mul (sg K a x) (sg K b y) := by
  cases a <;> cases b
  · exact (neg_mul_neg K x y).symm
  · show K.neg (K.mul x y) = K.mul (K.neg x) y; rw [neg_mul' K]
  · show K.neg (K.mul x y) = K.mul x (K.neg y); rw [K.mul_comm x (K.neg y), neg_mul' K, K.mul_comm]
  · rfl

theorem sg_mul_l (b : Bool) (x y : R) : K.mul x (sg K b y) = sg K b (K.mul x y) := by
  cases b
  · show K.mul x (K.neg y) = K.neg (K.mul x y); rw [K.mul_comm x (K.neg y), neg_mul' K, K.mul_comm]
  · rfl

theorem nthN_zadd : ∀ (u v : List Nat) (l : Nat), nthN (zadd u v) l = nthN u l + nthN v l
  | [], v, l => (Nat.zero_add _).symm
  | _ :: _, [], 0 => rfl
  | _ :: _, [], _ + 1 => rfl
  | _ :: _, _ :: _, 0 => rfl
  | _ :: u, _ :: v, l + 1 => nthN_zadd u v l

/-- **Even/odd polymer expansion.** -/
theorem expand2 (e o : Nat → List G → R) (x : List G) : ∀ (k : Nat) (M : List Bool),
    prodW K (fun i x => K.add (e i x) (o i x)) k M x
      = sumK K (fun S => if andB S M = S then K.mul (prodW K e k (andB M (notB S)) x) (prodW K o k S x) else K.zero)
          (bools M.length)
  | _, [] => by
    show K.one = K.add (if ([] : List Bool) = [] then K.mul (prodW K e _ [] x) (prodW K o _ [] x) else K.zero) K.zero
    rw [ip rfl, DetChain.add_zero']
    show K.one = K.mul K.one K.one
    rw [K.one_mul]
  | k, b :: M => by
    show K.mul (if b = true then K.add (e k x) (o k x) else K.one) (prodW K (fun i x => K.add (e i x) (o i x)) (k + 1) M x)
      = sumK K _ ((bools M.length).map (List.cons true) ++ (bools M.length).map (List.cons false))
    rw [expand2 e o x (k + 1) M, MaskSum.sumK_app, MaskSum.sumK_map, MaskSum.sumK_map]
    cases b with
    | true =>
      show K.mul (K.add (e k x) (o k x)) _ = _
      rw [K.mul_comm, K.distrib, K.mul_comm, K.mul_comm _ (o k x), mul_sumK, mul_sumK, K.add_comm]
      congr 1
      · refine sumK_ext K _ _ (fun S => ?_) _
        show _ = (if true :: andB S M = true :: S then
          K.mul (K.mul K.one (prodW K e (k + 1) (andB M (notB S)) x)) (K.mul (o k x) (prodW K o (k + 1) S x)) else K.zero)
        rw [GPerm.if_cons]
        by_cases h : andB S M = S
        · rw [ip h, ip h, K.one_mul, ← K.mul_assoc, K.mul_comm (o k x), K.mul_assoc]
        · rw [ineg h, ineg h, DetChain.mul_zero]
      · refine sumK_ext K _ _ (fun S => ?_) _
        show _ = (if false :: andB S M = false :: S then
          K.mul (K.mul (e k x) (prodW K e (k + 1) (andB M (notB S)) x)) (K.mul K.one (prodW K o (k + 1) S x)) else K.zero)
        rw [GPerm.if_cons]
        by_cases h : andB S M = S
        · rw [ip h, ip h, K.one_mul, K.mul_assoc]
        · rw [ineg h, ineg h, DetChain.mul_zero]
    | false =>
      show K.mul K.one _ = _
      rw [K.one_mul]
      have e1 : sumK K (fun S => if andB (true :: S) (false :: M) = true :: S then
          K.mul (prodW K e k (andB (false :: M) (notB (true :: S))) x) (prodW K o k (true :: S) x) else K.zero)
          (bools M.length) = K.zero :=
        (sumK_ext K _ _ (fun S => ineg (fun h => Bool.noConfusion (List.head_eq_of_cons_eq h))) _).trans (sum_zero K _)
      rw [e1, K.zero_add]
      refine sumK_ext K _ _ (fun S => ?_) _
      show _ = (if false :: andB S M = false :: S then
        K.mul (K.mul K.one (prodW K e (k + 1) (andB M (notB S)) x)) (K.mul K.one (prodW K o (k + 1) S x)) else K.zero)
      rw [GPerm.if_cons]
      by_cases h : andB S M = S
      · rw [ip h, ip h, K.one_mul, K.one_mul]
      · rw [ineg h, ineg h]

/-- **Sign of an odd product under a central link shift.** -/
theorem sign_prod (o : Nat → List G → R) (c c' : List G) (l : Nat) :
    ∀ (S : List Bool) (P : List (List Nat)) (k : Nat),
      (∀ i, o (k + i) c' = sg K (parity (nthN (nthL P i) l)) (o (k + i) c)) →
      prodW K o k S c' = sg K (parity (nthN (bdS P S) l)) (prodW K o k S c)
  | [], [], _, _ => rfl
  | [], _ :: _, _, _ => rfl
  | b :: S, [], k, ho => by
    have ih := sign_prod o c c' l S [] (k + 1) (fun i => by rw [Nat.succ_add]; exact ho (i + 1))
    have h0 : o k c' = o k c := ho 0
    show K.mul (if b = true then o k c' else K.one) (prodW K o (k + 1) S c') = K.mul (if b = true then o k c else K.one) (prodW K o (k + 1) S c)
    rw [ih, h0]
    rfl
  | b :: S, q :: P, k, ho => by
    have ih := sign_prod o c c' l S P (k + 1) (fun i => by rw [Nat.succ_add]; exact ho (i + 1))
    have h0 : o k c' = sg K (parity (nthN q l)) (o k c) := ho 0
    cases b with
    | true =>
      show K.mul (o k c') (prodW K o (k + 1) S c') = sg K (parity (nthN (zadd q (bdS P S)) l)) (K.mul (o k c) (prodW K o (k + 1) S c))
      rw [ih, h0, nthN_zadd, parity_add, sg_mul]
    | false =>
      show K.mul K.one (prodW K o (k + 1) S c') = sg K (parity (nthN (bdS P S) l)) (K.mul K.one (prodW K o (k + 1) S c))
      rw [K.one_mul, K.one_mul, ih]

theorem odd_link : ∀ v : List Nat, evenAll v = false → ∃ l, parity (nthN v l) = false
  | [], h => Bool.noConfusion h
  | a :: v, h => by
    cases e : parity a with
    | false => exact ⟨0, e⟩
    | true =>
      have h' : (parity a && evenAll v) = false := h
      rw [e, Bool.true_and] at h'
      obtain ⟨l, hl⟩ := odd_link v h'
      exact ⟨l + 1, hl⟩

theorem setG_long : ∀ (c : List G) (l : Nat) (g : G), c.length ≤ l → setG c l g = c
  | [], _, _, _ => rfl
  | _ :: _, 0, _, h => absurd h (Nat.not_succ_le_zero _)
  | a :: c, l + 1, g, h => by
    show a :: setG c l g = a :: c
    rw [setG_long c l g (Nat.le_of_succ_le_succ h)]

theorem sum_zero_mem {A : Type} (F : A → R) : ∀ l : List A, (∀ a, memL a l → F a = K.zero) → sumK K F l = K.zero
  | [], _ => rfl
  | a :: l, h => by
    show K.add (F a) (sumK K F l) = K.zero
    rw [h a (Or.inl rfl), sum_zero_mem F l (fun b hb => h b (Or.inr hb)), K.zero_add]

/-- **Terms whose odd plaquettes do not span the loop vanish.** -/
theorem term_vanish [DecidableEq G] (O : ORing K) (els : List G) (hc : Complete K els) (zm zi : G → G)
    (h1 : ∀ g, zi (zm g) = g) (h2 : ∀ g, zm (zi g) = g) (g0 : G) (L : Nat) (n : List Nat) (P : List (List Nat))
    (e o : Nat → List G → R) (χ : List G → R)
    (he : ∀ i c l g, e i (setG c l (zm g)) = e i (setG c l g))
    (ho : ∀ i c l g, o i (setG c l (zm g)) = sg K (parity (nthN (nthL P i) l)) (o i (setG c l g)))
    (hχ : ∀ c l g, χ (setG c l (zm g)) = sg K (parity (nthN n l)) (χ (setG c l g)))
    (D S : List Bool) (hS : evenAll (zadd n (bdS P S)) = false) :
    sumK K (fun x => K.mul (χ x) (K.mul (prodW K e 0 D x) (prodW K o 0 S x))) (cfgs els L) = K.zero := by
  obtain ⟨l, hl⟩ := odd_link _ hS
  rw [nthN_zadd, parity_add] at hl
  have hodd : ∀ c g, K.mul (χ (setG c l (zm g))) (K.mul (prodW K e 0 D (setG c l (zm g))) (prodW K o 0 S (setG c l (zm g))))
      = K.neg (K.mul (χ (setG c l g)) (K.mul (prodW K e 0 D (setG c l g)) (prodW K o 0 S (setG c l g)))) := by
    intro c g
    have pe := sign_prod K e (setG c l g) (setG c l (zm g)) l D [] 0 (fun i => by rw [Nat.zero_add]; exact he i c l g)
    have po := sign_prod K o (setG c l g) (setG c l (zm g)) l S P 0 (fun i => by rw [Nat.zero_add]; exact ho i c l g)
    rw [hχ, pe, po]
    show K.mul (sg K (parity (nthN n l)) _) (K.mul (prodW K e 0 D (setG c l g)) (sg K (parity (nthN (bdS P S) l)) _)) = _
    rw [sg_mul_l, ← sg_mul, hl]
    rfl
  cases Nat.lt_or_ge l L with
  | inl hlt => exact Center.odd_vanish K O els hc zm zi h1 h2 L l hlt _ hodd
  | inr hge =>
    refine sum_zero_mem K _ _ (fun c hc' => ?_)
    have hlen := HaarFin.mem_cfgs_len els L c hc'
    have hs : ∀ g, setG c l g = c := fun g => setG_long c l g (by rw [hlen]; exact hge)
    have := hodd c g0
    rw [hs, hs] at this
    exact Center.neg_self_zero K O _ this

theorem prodW_ext (w w' : Nat → List G → R) (x : List G) (h : ∀ i, w i x = w' i x) :
    ∀ (k : Nat) (M : List Bool), prodW K w k M x = prodW K w' k M x
  | _, [] => rfl
  | k, b :: M => by
    show K.mul (if b = true then w k x else K.one) (prodW K w (k + 1) M x)
      = K.mul (if b = true then w' k x else K.one) (prodW K w' (k + 1) M x)
    rw [h k, prodW_ext w w' x h (k + 1) M]

/-- **Polymers smaller than the area vanish (center charge).** -/
theorem center_area [DecidableEq G] (O : ORing K) (els : List G) (hc : Complete K els) (zm zi : G → G)
    (h1 : ∀ g, zi (zm g) = g) (h2 : ∀ g, zm (zi g) = g) (g0 : G) (L : Nat) (n : List Nat) (P : List (List Nat))
    (f e o : Nat → List G → R) (χ : List G → R) (hf : ∀ i x, f i x = K.add (e i x) (o i x))
    (he : ∀ i c l g, e i (setG c l (zm g)) = e i (setG c l g))
    (ho : ∀ i c l g, o i (setG c l (zm g)) = sg K (parity (nthN (nthL P i) l)) (o i (setG c l g)))
    (hχ : ∀ c l g, χ (setG c l (zm g)) = sg K (parity (nthN n l)) (χ (setG c l g)))
    (m : Nat) (hm : ∀ S : List Bool, evenAll (zadd n (bdS P S)) = true → m ≤ cnt S)
    (A : List Bool) (hA : cnt A < m) :
    aW K els L f χ A = K.zero := by
  show sumK K (fun x => K.mul (χ x) (prodW K f 0 A x)) (cfgs els L) = K.zero
  have step : ∀ x, K.mul (χ x) (prodW K f 0 A x) = sumK K (fun S => if andB S A = S then
      K.mul (χ x) (K.mul (prodW K e 0 (andB A (notB S)) x) (prodW K o 0 S x)) else K.zero) (bools A.length) := by
    intro x
    rw [prodW_ext K f (fun i x => K.add (e i x) (o i x)) x (fun i => hf i x) 0 A, expand2, mul_sumK]
    refine sumK_ext K _ _ (fun S => ?_) _
    by_cases h : andB S A = S
    · rw [ip h, ip h]
    · rw [ineg h, ineg h, DetChain.mul_zero]
  rw [sumK_ext K _ _ step, sumK_swap]
  refine (sumK_ext K _ _ (fun S => ?_) _).trans (sum_zero K _)
  by_cases h : andB S A = S
  · rw [sumK_ext K _ _ (fun x => ip h)]
    have hev : evenAll (zadd n (bdS P S)) = false := by
      cases ev : evenAll (zadd n (bdS P S)) with
      | false => rfl
      | true =>
        have c1 := hm S ev
        have c2 := BallSize.cnt_andB_le S A
        rw [h] at c2
        exact absurd (Nat.lt_of_le_of_lt c1 (Nat.lt_of_le_of_lt c2 hA)) (Nat.lt_irrefl _)
    exact term_vanish K O els hc zm zi h1 h2 g0 L n P e o χ he ho hχ _ S hev
  · rw [sumK_ext K _ _ (fun x => ineg h)]
    exact sum_zero K _

end CenterExp

#print axioms CenterExp.expand2
#print axioms CenterExp.sign_prod
#print axioms CenterExp.term_vanish
#print axioms CenterExp.center_area

ym/QuatUnit.lean

/-
  ym/QuatUnit — T45, cycle 103 (Lad2/Lad4 constants): unit quaternions have |Re tr U|/2 ≤ 1.

  An element of SU(2) (and of its finite subgroup 2I) is a unit quaternion (a, b, c, d), a² + b² + c² + d² = 1, and
  Re tr U = 2a. In any ordered ring:
    sq_nn          0 ≤ x x
    sq_le_one      a a ≤ 1  ⇒  −1 ≤ a ≤ 1
    unit_t         a² + b² + c² + d² = 1  ⇒  −1 ≤ a ≤ 1
  So t = Re tr U/2 ∈ [−1, 1] for every SU(2) and 2I link variable, and WeightBound applies: the normalised Wilson weight is
  c₀ + f with c₀ = 1, −4β ≤ f ≤ 0, m = 1 − 4β — the Wilson-action constants for 2I (and SU(2)) in AreaG/GapG/GapZd.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.WeightBound

namespace QuatUnit

open YMChain Signed

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem sq_nn (x : R) : O.le K.zero (K.mul x x) := by
  cases O.le_total K.zero x with
  | inl h => exact O.mul_nonneg _ _ h h
  | inr h =>
    have n := le_neg_of K O x h
    have := O.mul_nonneg _ _ n n
    rw [CenterExp.neg_mul_neg] at this; exact this

theorem sq_le_one (h1 : O.le K.zero K.one) (a : R) (h : O.le (K.mul a a) K.one) :
    O.le (K.neg K.one) a ∧ O.le a K.one := by
  constructor
  · cases O.le_total (K.neg K.one) a with
    | inl h' => exact h'
    | inr h' =>
      -- a ≤ −1: then −a ≥ 1 and (−a)·1 ≤ (−a)(−a) = a a ≤ 1, so −a ≤ 1, a ≥ −1
      have na : O.le K.one (K.neg a) := by
        have := neg_le_neg K O _ _ h'; rw [neg_neg' K] at this; exact this
      have na0 : O.le K.zero (K.neg a) := O.le_trans _ _ _ h1 na
      have m := O.mul_le_mul_right _ _ _ na na0
      rw [K.one_mul, CenterExp.neg_mul_neg] at m
      have := O.le_trans _ _ _ m h
      have := neg_le_neg K O _ _ this
      rw [neg_neg' K] at this; exact this
  · cases O.le_total a K.one with
    | inl h' => exact h'
    | inr h' =>
      have a0 : O.le K.zero a := O.le_trans _ _ _ h1 h'
      have m := O.mul_le_mul_right _ _ _ h' a0
      rw [K.one_mul] at m
      exact O.le_trans _ _ _ m h

/-- **Unit quaternions: −1 ≤ a ≤ 1.** -/
theorem unit_t (h1 : O.le K.zero K.one) (a b c d : R)
    (hu : K.add (K.add (K.mul a a) (K.mul b b)) (K.add (K.mul c c) (K.mul d d)) = K.one) :
    O.le (K.neg K.one) a ∧ O.le a K.one := by
  apply sq_le_one K O h1
  rw [← hu]
  have r := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ (O.le_refl (K.mul a a)) (sq_nn K O b))
    (O.add_le_add _ _ _ _ (sq_nn K O c) (sq_nn K O d))
  rw [DetChain.add_zero', K.zero_add, DetChain.add_zero'] at r
  exact r

end QuatUnit

#print axioms QuatUnit.sq_le_one
#print axioms QuatUnit.unit_t

90.4 L3: two-dimensional gauge theory

Statement. For any compact group with a right-invariant normalised one-link integral, a class-function weight \(w\) and an irreducible \(\rho \) of dimension \(d\), on every \(R\times T\) rectangle of the 2D lattice

\[ \langle W_{R\times T}\rangle =d\,(\lambda /z)^{RT},\qquad d\lambda =\mu (w\chi _\rho ),\quad z=\mu (w); \]
for the heat-kernel action \(\lambda /z=e^{-Ct}\), \(t=g^2a^2/2\), so \(\langle W\rangle =d\,e^{-Cg^2\mathcal A/2}\) at every lattice spacing (continuum limit and area law).

\(SU(2)\) explicitly (S1–S4). The Haar integral on class functions of \(y=\mathrm {tr}\,U\) is \(I(p)=\sum _ip_i\,m_i\) with \(m_i\) the number of walks on \(\mathbb N\) from \(0\) to \(0\): \(m_0=1\), \(m_1=0\), \(m_{n+2}=\sum _{a+b=n}m_am_b\) (Catalan; in \(\mathrm {tr}\,U/2\) the moments \(C_k/4^k\) of the semicircle law) (SU2Haar.catalan); characters \(\chi _{n+2}=y\chi _{n+1}-\chi _n\) satisfy \(I\chi _n=\delta _{n0}\), \(I(\chi _k\chi _n)=\delta _{kn}\) and \(I(p^2)=\sum a_k^2\ge 0\) (SU2Char); the character coefficients of \(e^{\beta \chi _1/2}\) obey \(0\le c_n/c_0\le \sum _k t^{n+2k}/(n!\,k!\,k!)\), \(t=\beta /2\) (SU2CharExp.ratio_bound); and on every rectangle \(\langle W\rangle =(c_1/(2c_0))^{RT}\) (SU2Wilson2D.wilson_2d).

Toward H0 (Gaussian route). On polynomials in \(n\) variables let \(G(x^\alpha )=\prod _i m(\alpha _i)\), \(m(2k)=(2k-1)!!\), \(m(\text {odd})=0\). Then \(G(x_ix^\alpha )=\alpha _iG(x^{\alpha -e_i})\) (Gauss.G_ibp), and any linear \(\Lambda \) with \(\Lambda (1)=1\) obeying this rule equals \(G\) (GaussPoly.unique_mono). Products, \(\partial _j\), the Leibniz rule and the chain rule \(\partial _j(x^\alpha \circ O)\equiv \sum _iO_{ij}\alpha _i\, x^{\alpha -e_i}\circ O\) hold up to test-equivalence \(p\equiv q\iff G(pr)=G(qr)\ \forall r\) (GaussPoly, GaussSubst.chain). For \(O\) with orthonormal rows, \(\sum _mO_{im}O_{lm}=\delta _{il}\) collapses the chain-rule sums, so \(\Lambda (p)=G(p\circ O)\) satisfies the hypotheses of uniqueness:

\[ G(x^\alpha \circ O)=G(x^\alpha )\qquad \text {(\texttt {GaussRot.rot\_inv})}. \]
For a unit quaternion \(q\) (\(a^2+b^2+c^2+d^2=1\)) the matrices of \(x\mapsto qx\) and \(x\mapsto xq\) have orthonormal rows, so \(G\) is invariant under both actions of \(SU(2)\cong S^3\) on \(\mathbb R^4=\mathbb H\) (GaussQuat.bi_inv). The walk moments of SU2Haar are Catalan numbers by reflection, \(P_m(0\to t)+\binom {m}{a-1}=\binom ma\) (\(m=t+2a\)), whence \(\mathrm {mom}(2k)\,k!\,(k+1)!=(2k)!\) and
\[ 2^k\,(2k-1)!!=\mathrm {mom}(2k)\,(k+1)!\qquad \text {(\texttt {GaussCatalan.catalan\_gauss})}, \]
i.e. \(\mathrm {mom}(2k)/4^k=G(x_0^{2k})/(2^k(k+1)!)\). For \(Q\) homogeneous of degree \(d\) in \(n\) variables, \(G(Q\,|x|^2)=(d+n)\,G(Q)\) (each \(x_i^2\) contributes \(\alpha _i+1\)), so on \(\mathbb R^4\) \(G(|x|^{2k})=2^k(k+1)!\) (GaussSphere.norm_moment) and
\[ \int _{SU(2)}(\mathrm {tr}\,U)^{2k}\,dU=\mathrm {mom}(2k)=\frac {G((2x_0)^{2k})}{G(|x|^{2k})} \qquad \text {(\texttt {GaussSphere.haar\_sphere})}: \]
the walk-count Haar integral of SU2Haar is the normalised average of the \(SU(2)\)-bi-invariant functional \(G\) on \(\mathbb R^4=\mathbb H\) (H0 for class functions).

Key steps. Axial gauge by right-invariant substitution and telescoping (AxialAlg, StripAxial); Schur sandwich (Sandwich); peeling rows of the rectangle (Peel, RectGeom, RectInt.rect_W); Schur averages and character orthogonality (SchurAvg, CharOrthH); Lad3.rect_char; heat kernel (Heat2D). Cited for \(SU(2)\): the Haar integral (H0) and convergence of the heat-kernel series \(\sum _j d_je^{-tC_j}\chi _j\) (completeness of \(\mathbb R\)); its Schur ratio \(e^{-tC_\rho }\) then follows from character orthogonality (CharOrthH), and positivity of the weight is not used.

Lean files.

ym/LinkIntegral.lean

/-
  ym/LinkIntegral — T45, cycle 7 (S2b for G = ℤ₂): link integration kills open polymers.

  Configurations of L links with values ±1 are Bool lists of length L (bools L, 2^L of them).
    sum_prod_factor   Σ_c Π_l f_l(c_l) = Π_l (f_l(true) + f_l(false))      (sum over a product space)
    sgnpow_sum        Σ_{u = ±1} u^n = 1 + (−1)^n, which is 0 for odd n
    open_vanishes     if some link occurs an odd number of times, Σ_c Π_l u_l^(n_l) = 0
  A polymer S contributes Π_{p ∈ S} U_p; the multiplicity of link l is the number of plaquettes of S
  containing it. So after integration only polymers in which every link has even multiplicity — closed
  surfaces mod 2 — survive (checked on the 2 × 2 torus in cycle 6). For non-abelian G the same step
  needs character orthogonality.

  Lean 4 core only, proofs by hand, no axioms.
-/
import YMChain

namespace LinkIntegral

open YMChain

variable {R : Type} (K : DetChain.CRing R)

def bools : Nat → List (List Bool)
  | 0 => [[]]
  | n + 1 => (bools n).map (List.cons true) ++ (bools n).map (List.cons false)

/-- `Π_i f_i(c_i)`. -/
def prodF : List (Bool → R) → List Bool → R
  | [], _ => K.one
  | _ :: _, [] => K.one
  | f :: fs, b :: c => K.mul (f b) (prodF fs c)

def prodSum : List (Bool → R) → R
  | [] => K.one
  | f :: fs => K.mul (K.add (f true) (f false)) (prodSum fs)

theorem sumK_app' {A : Type} (f : A → R) : ∀ l m : List A, sumK K f (l ++ m) = K.add (sumK K f l) (sumK K f m)
  | [], m => (K.zero_add _).symm
  | a :: l, m => by
    show K.add (f a) (sumK K f (l ++ m)) = K.add (K.add (f a) (sumK K f l)) (sumK K f m)
    rw [sumK_app' f l m, K.add_assoc]

theorem sumK_map'' {A B : Type} (f : B → R) (g : A → B) : ∀ l : List A, sumK K f (l.map g) = sumK K (fun a => f (g a)) l
  | [] => rfl
  | a :: l => by
    show K.add (f (g a)) (sumK K f (l.map g)) = K.add (f (g a)) (sumK K (fun a => f (g a)) l)
    rw [sumK_map'' f g l]

/-- **Sum over a product space factorises.** -/
theorem sum_prod_factor : ∀ fs : List (Bool → R), sumK K (prodF K fs) (bools fs.length) = prodSum K fs
  | [] => by
    show K.add K.one K.zero = K.one
    exact DetChain.add_zero' K _
  | f :: fs => by
    show sumK K (prodF K (f :: fs)) ((bools fs.length).map (List.cons true) ++ (bools fs.length).map (List.cons false))
      = K.mul (K.add (f true) (f false)) (prodSum K fs)
    rw [sumK_app', sumK_map'', sumK_map'']
    show K.add (sumK K (fun c => K.mul (f true) (prodF K fs c)) (bools fs.length))
        (sumK K (fun c => K.mul (f false) (prodF K fs c)) (bools fs.length)) = _
    rw [← mul_sumK, ← mul_sumK, sum_prod_factor fs, ← DetChain.right_distrib]

def sgn (b : Bool) : R := match b with
  | true => K.one
  | false => K.neg K.one

def pwR (x : R) : Nat → R
  | 0 => K.one
  | k + 1 => K.mul (pwR x k) x

theorem pw_one' : ∀ n, pwR K K.one n = K.one
  | 0 => rfl
  | n + 1 => by show K.mul (pwR K K.one n) K.one = K.one; rw [pw_one' n, K.one_mul]

theorem negone_sq : K.mul (K.neg K.one) (K.neg K.one) = K.one := by
  rw [DetChain.neg_mul_left, K.one_mul, DetChain.neg_neg']

/-- `(−1)^(2m+1) = −1`. -/
theorem negone_odd : ∀ m, pwR K (K.neg K.one) (2 * m + 1) = K.neg K.one
  | 0 => by show K.mul K.one (K.neg K.one) = K.neg K.one; exact K.one_mul _
  | m + 1 => by
    show K.mul (K.mul (pwR K (K.neg K.one) (2 * m + 1)) (K.neg K.one)) (K.neg K.one) = K.neg K.one
    rw [negone_odd m, K.mul_assoc, negone_sq, K.mul_comm, K.one_mul]

/-- **An odd power of a ±1 variable integrates to zero.** -/
theorem sgnpow_odd (m : Nat) :
    K.add (pwR K (sgn K true) (2 * m + 1)) (pwR K (sgn K false) (2 * m + 1)) = K.zero := by
  show K.add (pwR K K.one (2 * m + 1)) (pwR K (K.neg K.one) (2 * m + 1)) = K.zero
  rw [pw_one', negone_odd, K.add_neg]

theorem prodSum_zero (fs : List (Bool → R)) (f : Bool → R) (hf : K.add (f true) (f false) = K.zero) :
    ∀ pre : List (Bool → R), prodSum K (pre ++ f :: fs) = K.zero
  | [] => by show K.mul (K.add (f true) (f false)) (prodSum K fs) = K.zero; rw [hf, DetChain.zero_mul']
  | g :: pre => by
    show K.mul (K.add (g true) (g false)) (prodSum K (pre ++ f :: fs)) = K.zero
    rw [prodSum_zero fs f hf pre, DetChain.mul_zero]

/-- **Open polymers vanish.** Links before and after the odd one carry arbitrary weights. -/
theorem open_vanishes (pre post : List (Bool → R)) (m : Nat) :
    sumK K (prodF K (pre ++ (fun b => pwR K (sgn K b) (2 * m + 1)) :: post))
      (bools (pre ++ (fun b => pwR K (sgn K b) (2 * m + 1)) :: post).length) = K.zero := by
  rw [sum_prod_factor]
  exact prodSum_zero K post _ (sgnpow_odd K m) pre

end LinkIntegral

#print axioms LinkIntegral.sumK_app'
#print axioms LinkIntegral.sumK_map''
#print axioms LinkIntegral.sum_prod_factor
#print axioms LinkIntegral.pw_one'
#print axioms LinkIntegral.negone_sq
#print axioms LinkIntegral.negone_odd
#print axioms LinkIntegral.sgnpow_odd
#print axioms LinkIntegral.prodSum_zero
#print axioms LinkIntegral.open_vanishes

ym/Strip2D.lean

/-
  ym/Strip2D — T45, cycle 62 (Lad3 step 6, any compact group): the 2D exact solution of a strip of A plaquettes with
  independent plaquette variables (axial gauge), from Schur averages.

  μ : (G → R) → R a linear one-link integral (Haar on G: finite sums for finite G, the cited Haar integral for SU(2)).
  The A-link integral is iterated: I 0 F = F [], I (A+1) F = μ (g ↦ I A (c ↦ F (g :: c)))  (Fubini by construction).
  ρ : G → ι → ι → R a matrix representation on idx (dimension d = |idx|), w a plaquette weight. Schur hypothesis:
  μ(w ρ_{ik}) = λ δ_{ik}, μ(w) = z.
    I_sum, I_smul_fn         linearity of the iterated integral
    strip                    I_A(Π_k w(c_k) · (ρ(c_0)⋯ρ(c_{A−1}))_{ij}) = λ^A δ_ij
    wilson_strip             I_A(Π w · tr ρ(c_0⋯c_{A−1})) = d λ^A,      I_A(Π w) = z^A
  i.e. ⟨tr ρ(U_1⋯U_A)⟩ = d (λ/z)^A — the exact 2D area law.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.VanishH

namespace Strip2D

open YMChain LinkIntegral

variable {R : Type} (K : DetChain.CRing R) {G : Type} {ι : Type} [DecidableEq ι]

def delta (i j : ι) : R := if i = j then K.one else K.zero

def mprod (ρ : G → ι → ι → R) (idx : List ι) : List G → ι → ι → R
  | [], i, j => delta K i j
  | g :: c, i, j => sumK K (fun k => K.mul (ρ g i k) (mprod ρ idx c k j)) idx

def wprod (w : G → R) : List G → R
  | [] => K.one
  | g :: c => K.mul (w g) (wprod w c)

/-- A linear one-link integral. -/
structure Lin (μ : (G → R) → R) : Prop where
  add : ∀ f h : G → R, μ (fun g => K.add (f g) (h g)) = K.add (μ f) (μ h)
  smul : ∀ (a : R) (f : G → R), μ (fun g => K.mul a (f g)) = K.mul a (μ f)
  ext : ∀ f h : G → R, (∀ g, f g = h g) → μ f = μ h
  zero : μ (fun _ => K.zero) = K.zero

def It (μ : (G → R) → R) : Nat → (List G → R) → R
  | 0, F => F []
  | A + 1, F => μ (fun g => It μ A (fun c => F (g :: c)))

theorem It_ext (μ : (G → R) → R) (hμ : Lin K μ) : ∀ (A : Nat) (F F' : List G → R), (∀ c, F c = F' c) → It μ A F = It μ A F'
  | 0, F, F', h => h []
  | A + 1, F, F', h => hμ.ext _ _ (fun g => It_ext μ hμ A _ _ (fun c => h (g :: c)))

theorem It_smul (μ : (G → R) → R) (hμ : Lin K μ) : ∀ (A : Nat) (a : R) (F : List G → R), It μ A (fun c => K.mul a (F c)) = K.mul a (It μ A F)
  | 0, _, _ => rfl
  | A + 1, a, F => by
    show μ (fun g => It μ A (fun c => K.mul a (F (g :: c)))) = K.mul a (μ (fun g => It μ A (fun c => F (g :: c))))
    rw [← hμ.smul]
    exact hμ.ext _ _ (fun g => It_smul μ hμ A a _)

theorem It_add (μ : (G → R) → R) (hμ : Lin K μ) : ∀ (A : Nat) (F H : List G → R),
    It μ A (fun c => K.add (F c) (H c)) = K.add (It μ A F) (It μ A H)
  | 0, _, _ => rfl
  | A + 1, F, H => by
    show μ (fun g => It μ A (fun c => K.add (F (g :: c)) (H (g :: c)))) = K.add (μ _) (μ _)
    rw [← hμ.add]
    exact hμ.ext _ _ (fun g => It_add μ hμ A _ _)

theorem It_zero (μ : (G → R) → R) (hμ : Lin K μ) : ∀ A : Nat, It μ A (fun _ => K.zero) = K.zero
  | 0 => rfl
  | A + 1 => by
    show μ (fun g => It μ A (fun _ => K.zero)) = K.zero
    rw [hμ.ext _ (fun _ => K.zero) (fun g => It_zero μ hμ A), hμ.zero]

theorem It_sum {β : Type} (μ : (G → R) → R) (hμ : Lin K μ) (A : Nat) (F : β → List G → R) : ∀ l : List β,
    It μ A (fun c => sumK K (fun b => F b c) l) = sumK K (fun b => It μ A (F b)) l
  | [] => It_zero K μ hμ A
  | b :: l => by
    show It μ A (fun c => K.add (F b c) (sumK K (fun b => F b c) l)) = K.add _ _
    rw [It_add K μ hμ, It_sum μ hμ A F l]

theorem mu_sum {β : Type} (μ : (G → R) → R) (hμ : Lin K μ) (f : β → G → R) : ∀ l : List β,
    μ (fun g => sumK K (fun b => f b g) l) = sumK K (fun b => μ (f b)) l
  | [] => hμ.zero
  | b :: l => by
    show μ (fun g => K.add (f b g) (sumK K (fun b => f b g) l)) = K.add _ _
    rw [hμ.add, mu_sum μ hμ f l]

/-- **2D strip.** -/
theorem strip (μ : (G → R) → R) (hμ : Lin K μ) (ρ : G → ι → ι → R) (idx : List ι) (w : G → R) (lam : R)
    (hschur : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k))
    (hsel : ∀ (i j : ι), sumK K (fun k => K.mul (delta K i k) (delta K k j)) idx = delta K i j) :
    ∀ (A : Nat) (i j : ι), It μ A (fun c => K.mul (wprod K w c) (mprod K ρ idx c i j)) = K.mul (pwR K lam A) (delta K i j)
  | 0, i, j => by show K.mul K.one (delta K i j) = K.mul K.one (delta K i j); rfl
  | A + 1, i, j => by
    show μ (fun g => It μ A (fun c => K.mul (K.mul (w g) (wprod K w c))
      (sumK K (fun k => K.mul (ρ g i k) (mprod K ρ idx c k j)) idx))) = _
    have e1 : ∀ g, It μ A (fun c => K.mul (K.mul (w g) (wprod K w c)) (sumK K (fun k => K.mul (ρ g i k) (mprod K ρ idx c k j)) idx))
        = sumK K (fun k => K.mul (K.mul (w g) (ρ g i k)) (K.mul (pwR K lam A) (delta K k j))) idx := by
      intro g
      rw [It_ext K μ hμ A _ (fun c => sumK K (fun k => K.mul (K.mul (w g) (ρ g i k)) (K.mul (wprod K w c) (mprod K ρ idx c k j))) idx)
        (fun c => by
          rw [mul_sumK]
          exact sumK_ext K _ _ (fun k => by
            rw [K.mul_assoc, ← K.mul_assoc (wprod K w c), K.mul_comm (wprod K w c) (ρ g i k), K.mul_assoc (ρ g i k),
              ← K.mul_assoc (w g)]) idx),
        It_sum K μ hμ A _ idx]
      exact sumK_ext K _ _ (fun k => by rw [It_smul K μ hμ, strip μ hμ ρ idx w lam hschur hsel A k j]) idx
    rw [hμ.ext _ _ e1, mu_sum K μ hμ _ idx]
    rw [sumK_ext K _ _ (fun k => by
      rw [hμ.ext _ (fun g => K.mul (K.mul (pwR K lam A) (delta K k j)) (K.mul (w g) (ρ g i k))) (fun g => K.mul_comm _ _),
        hμ.smul, hschur])]
    -- Σ_k (λ^A δ_kj)(λ δ_ik) = λ^{A+1} δ_ij
    have e2 : ∀ k, K.mul (K.mul (pwR K lam A) (delta K k j)) (K.mul lam (delta K i k))
        = K.mul (K.mul (pwR K lam A) lam) (K.mul (delta K i k) (delta K k j)) := fun k => by
      rw [K.mul_assoc, K.mul_comm (delta K k j), K.mul_assoc lam, ← K.mul_assoc (pwR K lam A), K.mul_comm (delta K i k)]
    rw [sumK_ext K _ _ e2, ← mul_sumK, hsel]
    rfl

/-- **The Wilson loop of a strip and the partition function.** -/
theorem wilson_strip (μ : (G → R) → R) (hμ : Lin K μ) (ρ : G → ι → ι → R) (idx : List ι) (w : G → R) (lam z : R)
    (hschur : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k))
    (hsel : ∀ (i j : ι), sumK K (fun k => K.mul (delta K i k) (delta K k j)) idx = delta K i j)
    (hz : μ w = z) (A : Nat) :
    It μ A (fun c => K.mul (wprod K w c) (sumK K (fun i => mprod K ρ idx c i i) idx))
        = K.mul (sumK K (fun _ => K.one) idx) (pwR K lam A) ∧
      It μ A (wprod K w) = pwR K z A := by
  constructor
  · rw [It_ext K μ hμ A _ (fun c => sumK K (fun i => K.mul (wprod K w c) (mprod K ρ idx c i i)) idx) (fun c => mul_sumK K _ _ _),
      It_sum K μ hμ A _ idx, sumK_ext K _ _ (fun i => strip K μ hμ ρ idx w lam hschur hsel A i i), sumK_mul]
    refine sumK_ext K _ _ (fun i => ?_) idx
    show K.mul (pwR K lam A) (if i = i then K.one else K.zero) = K.mul K.one (pwR K lam A)
    rw [MaskSum.ip rfl, K.mul_comm]
  · induction A with
    | zero => rfl
    | succ A ih =>
      show μ (fun g => It μ A (fun c => K.mul (w g) (wprod K w c))) = K.mul (pwR K z A) z
      rw [hμ.ext _ (fun g => K.mul (w g) (It μ A (wprod K w))) (fun g => It_smul K μ hμ A (w g) _), ih]
      rw [hμ.ext _ (fun g => K.mul (pwR K z A) (w g)) (fun g => K.mul_comm _ _), hμ.smul, hz]

end Strip2D

#print axioms Strip2D.It_sum
#print axioms Strip2D.strip
#print axioms Strip2D.wilson_strip

ym/Heat2D.lean

/-
  ym/Heat2D — T45, cycle 63 (Lad3 step 7): the continuum limit of 2D lattice gauge theory with the heat-kernel action.

  E : R → R an exponential (E 0 = 1, E(s + t) = E s · E t; for real numbers E = exp). Heat-kernel plaquette weight at
  lattice spacing a with coupling g: Schur ratio λ = z · E(−C g² a² / 2) (C the Casimir of ρ; for SU(2) and ρ the
  fundamental representation C = 3/4). A rectangle of physical area 𝒜 = n a² contains n plaquettes.
    E_nat           E(n · s) = (E s)^n
    heat_strip      I(Π w · tr ρ) = d · E(−C g² 𝒜 / 2) · I(Π w)      for every a and n with n a² = 𝒜
  So ⟨W⟩ = d e^{−σ 𝒜} with σ = C g²/2 at every lattice spacing: the a → 0 limit exists (the sequence is constant), is
  non-trivial, and obeys the area law. Inputs beyond Lean: the Haar integral (H0) and the heat-kernel weight with this
  Schur ratio (its character expansion Σ_j d_j e^{−t C_j} χ_j).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Strip2D

namespace Heat2D

open YMChain LinkIntegral Strip2D

variable {R : Type} (K : DetChain.CRing R) {G : Type} {ι : Type} [DecidableEq ι]

def natMul (n : Nat) (s : R) : R := sumK K (fun _ => s) (List.replicate n ())

theorem natMul_succ (n : Nat) (s : R) : natMul K (n + 1) s = K.add s (natMul K n s) := rfl

/-- **Exponential law on multiples.** -/
theorem E_nat (E : R → R) (hE0 : E K.zero = K.one) (hEadd : ∀ s t, E (K.add s t) = K.mul (E s) (E t)) (s : R) :
    ∀ n, E (natMul K n s) = pwR K (E s) n
  | 0 => hE0
  | n + 1 => by
    rw [natMul_succ, hEadd, E_nat E hE0 hEadd s n, K.mul_comm]
    rfl

/-- **Heat-kernel strip at every lattice spacing.** -/
theorem heat_strip (μ : (G → R) → R) (hμ : Lin K μ) (ρ : G → ι → ι → R) (idx : List ι) (w : G → R) (z s : R)
    (E : R → R) (hE0 : E K.zero = K.one) (hEadd : ∀ s t, E (K.add s t) = K.mul (E s) (E t))
    (hschur : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul (K.mul z (E s)) (delta K i k))
    (hsel : ∀ (i j : ι), sumK K (fun k => K.mul (delta K i k) (delta K k j)) idx = delta K i j)
    (hz : μ w = z) (n : Nat) :
    It μ n (fun c => K.mul (wprod K w c) (sumK K (fun i => mprod K ρ idx c i i) idx))
      = K.mul (K.mul (sumK K (fun _ => K.one) idx) (E (natMul K n s))) (It μ n (wprod K w)) := by
  have ws := wilson_strip K μ hμ ρ idx w (K.mul z (E s)) z hschur hsel hz n
  rw [ws.1, ws.2, E_nat K E hE0 hEadd s n, GKS2.pwR_mul, K.mul_comm (pwR K z n), K.mul_assoc]

end Heat2D

#print axioms Heat2D.E_nat
#print axioms Heat2D.heat_strip

ym/Sandwich.lean

/-
  ym/Sandwich — T45, cycle 67 (Lad3 step 8, any compact group): Schur averages with fixed matrices in between.

  With the iterated integral It of Strip2D (one-link linear functional μ, Schur averages μ(w ρ_ik) = λ δ_ik):
    msand c Ms        (ρ(c_0) M_0 ρ(c_1) M_1 ⋯ ρ(c_{A−1}) M_{A−1})_{ij}
    mlist Ms          (M_0 M_1 ⋯ M_{A−1})_{ij}
    sandwich          It_A(Π w(c_k) · msand c Ms i j) = λ^A · mlist Ms i j
  In the axial gauge of a 2D lattice the Wilson loop, after the right-invariant substitution of the plaquette
  variables, has exactly this form with M_k built from the remaining links; their product is the identity
  (telescoping), so ⟨W⟩ = d (λ/z)^Area without assuming independent plaquettes (next cycles).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Degree

namespace Sandwich

open YMChain LinkIntegral Strip2D

variable {R : Type} (K : DetChain.CRing R) {G : Type} {ι : Type} [DecidableEq ι]

def mlist (idx : List ι) : List (ι → ι → R) → ι → ι → R
  | [], i, j => delta K i j
  | M :: Ms, i, j => sumK K (fun l => K.mul (M i l) (mlist idx Ms l j)) idx

def msand (ρ : G → ι → ι → R) (idx : List ι) : List G → List (ι → ι → R) → ι → ι → R
  | [], _, i, j => delta K i j
  | _ :: _, [], i, j => delta K i j
  | g :: c, M :: Ms, i, j =>
      sumK K (fun k => K.mul (ρ g i k) (sumK K (fun l => K.mul (M k l) (msand ρ idx c Ms l j)) idx)) idx

/-- **Schur sandwich.** -/
theorem sandwich (μ : (G → R) → R) (hμ : Lin K μ) (ρ : G → ι → ι → R) (idx : List ι) (w : G → R) (lam : R)
    (hschur : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k))
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i) :
    ∀ (Ms : List (ι → ι → R)) (i j : ι),
      It μ Ms.length (fun c => K.mul (wprod K w c) (msand K ρ idx c Ms i j)) = K.mul (pwR K lam Ms.length) (mlist K idx Ms i j)
  | [], i, j => by show K.mul K.one (delta K i j) = K.mul K.one (delta K i j); rfl
  | M :: Ms, i, j => by
    show μ (fun g => It μ Ms.length (fun c => K.mul (K.mul (w g) (wprod K w c))
      (sumK K (fun k => K.mul (ρ g i k) (sumK K (fun l => K.mul (M k l) (msand K ρ idx c Ms l j)) idx)) idx))) = _
    -- inner integral over the tail
    have e1 : ∀ g, It μ Ms.length (fun c => K.mul (K.mul (w g) (wprod K w c))
          (sumK K (fun k => K.mul (ρ g i k) (sumK K (fun l => K.mul (M k l) (msand K ρ idx c Ms l j)) idx)) idx))
        = sumK K (fun k => K.mul (K.mul (w g) (ρ g i k))
            (sumK K (fun l => K.mul (M k l) (K.mul (pwR K lam Ms.length) (mlist K idx Ms l j))) idx)) idx := by
      intro g
      rw [It_ext K μ hμ _ _ (fun c => sumK K (fun k => K.mul (K.mul (w g) (ρ g i k))
          (sumK K (fun l => K.mul (M k l) (K.mul (wprod K w c) (msand K ρ idx c Ms l j))) idx)) idx) (fun c => by
          rw [mul_sumK]
          refine sumK_ext K _ _ (fun k => ?_) idx
          rw [mul_sumK, mul_sumK, mul_sumK]
          refine sumK_ext K _ _ (fun l => ?_) idx
          -- (w·W)(ρ (M m)) = (w ρ)(M (W m))
          rw [K.mul_assoc (w g), ← K.mul_assoc (wprod K w c) (ρ g i k), K.mul_comm (wprod K w c) (ρ g i k),
            K.mul_assoc (ρ g i k), ← K.mul_assoc (wprod K w c) (M k l), K.mul_comm (wprod K w c) (M k l),
            K.mul_assoc (M k l), ← K.mul_assoc (w g) (ρ g i k)]),
        It_sum K μ hμ _ _ idx]
      refine sumK_ext K _ _ (fun k => ?_) idx
      rw [It_smul K μ hμ, It_sum K μ hμ _ _ idx]
      congr 1
      refine sumK_ext K _ _ (fun l => ?_) idx
      rw [It_smul K μ hμ, sandwich μ hμ ρ idx w lam hschur hsel1 Ms l j]
    rw [hμ.ext _ _ e1, mu_sum K μ hμ _ idx]
    rw [sumK_ext K _ _ (fun k => by
      rw [hμ.ext _ (fun g => K.mul (sumK K (fun l => K.mul (M k l) (K.mul (pwR K lam Ms.length) (mlist K idx Ms l j))) idx)
        (K.mul (w g) (ρ g i k))) (fun g => K.mul_comm _ _), hμ.smul, hschur])]
    -- Σ_k X_k (λ δ_ik) = λ X_i
    rw [sumK_ext K _ _ (fun k => by rw [K.mul_comm, K.mul_assoc])]
    rw [show (fun k => K.mul lam (K.mul (delta K i k) (sumK K (fun l => K.mul (M k l)
        (K.mul (pwR K lam Ms.length) (mlist K idx Ms l j))) idx)))
        = (fun k => K.mul lam (K.mul (delta K i k) (sumK K (fun l => K.mul (M k l)
        (K.mul (pwR K lam Ms.length) (mlist K idx Ms l j))) idx))) from rfl, ← mul_sumK, hsel1]
    -- λ · Σ_l M_il λ^A mlist = λ^{A+1} Σ_l M_il mlist
    show K.mul lam (sumK K _ idx) = K.mul (K.mul (pwR K lam Ms.length) lam) (sumK K (fun l => K.mul (M i l) (mlist K idx Ms l j)) idx)
    rw [mul_sumK, mul_sumK]
    refine sumK_ext K _ _ (fun l => ?_) idx
    rw [← K.mul_assoc lam, K.mul_comm lam (M i l), K.mul_assoc (M i l), ← K.mul_assoc lam, K.mul_comm lam (pwR K lam _),
      ← K.mul_assoc (K.mul (pwR K lam _) lam) (M i l), K.mul_comm (K.mul (pwR K lam _) lam) (M i l), K.mul_assoc (M i l)]

end Sandwich

#print axioms Sandwich.sandwich

ym/AxialAlg.lean

/-
  ym/AxialAlg — T45, cycle 68 (Lad3 step 9, any compact group): group/representation algebra and right invariance
  for the non-abelian axial gauge.

    gprod l              ordered product l_0 l_1 ⋯ of group elements
    zipMul c Xs          (c_0 X_0, c_1 X_1, …)
    rho_gprod            ρ(l_0 ⋯ l_{n−1}) = ρ(l_0)⋯ρ(l_{n−1})            (ρ a homomorphism on index sums)
    rho_interleave       ρ((c_0 X_0)(c_1 X_1)⋯) = msand c (X.map ρ)
    It_rinv              right invariance of μ ⇒ It_A(F ∘ zipMul · Xs) = It_A F

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Sandwich

namespace AxialAlg

open YMChain LinkIntegral Strip2D Sandwich

variable {R : Type} (K : DetChain.CRing R) {G : Type} (Gp : Grp G) {ι : Type} [DecidableEq ι]

def gprod : List G → G
  | [] => Gp.one
  | g :: l => Gp.mul g (gprod l)

def zipMul : List G → List G → List G
  | g :: c, x :: Xs => Gp.mul g x :: zipMul c Xs
  | [], _ => []
  | _ :: _, [] => []

variable (ρ : G → ι → ι → R) (idx : List ι)
  (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
  (hone : ∀ i j, ρ Gp.one i j = delta K i j)

theorem rho_gprod (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j) :
    ∀ (l : List G) (i j : ι), ρ (gprod Gp l) i j = mlist K idx (l.map ρ) i j
  | [], i, j => hone i j
  | g :: l, i, j => by
    show ρ (Gp.mul g (gprod Gp l)) i j = sumK K (fun k => K.mul (ρ g i k) (mlist K idx (l.map ρ) k j)) idx
    rw [hom]
    exact sumK_ext K _ _ (fun k => by rw [rho_gprod hom hone l k j]) idx

theorem rho_interleave (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j) :
    ∀ (c Xs : List G), c.length = Xs.length → ∀ i j,
      ρ (gprod Gp (zipMul Gp c Xs)) i j = msand K ρ idx c (Xs.map ρ) i j
  | [], [], _, i, j => hone i j
  | [], _ :: _, h, _, _ => absurd h Nat.noConfusion
  | _ :: _, [], h, _, _ => absurd h Nat.noConfusion
  | g :: c, x :: Xs, h, i, j => by
    show ρ (Gp.mul (Gp.mul g x) (gprod Gp (zipMul Gp c Xs))) i j
      = sumK K (fun k => K.mul (ρ g i k) (sumK K (fun l => K.mul (ρ x k l) (msand K ρ idx c (Xs.map ρ) l j)) idx)) idx
    rw [Gp.assoc, hom]
    refine sumK_ext K _ _ (fun k => ?_) idx
    rw [hom]
    congr 1
    exact sumK_ext K _ _ (fun l => by rw [rho_interleave hom hone c Xs (Nat.succ.inj h) l j]) idx

/-- **Right invariance of the iterated integral.** -/
theorem It_rinv (μ : (G → R) → R) (hμ : Lin K μ) (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f) :
    ∀ (Xs : List G) (F : List G → R), It μ Xs.length (fun c => F (zipMul Gp c Xs)) = It μ Xs.length F
  | [], F => by show F (zipMul Gp [] []) = F []; rfl
  | x :: Xs, F => by
    show μ (fun g => It μ Xs.length (fun c => F (zipMul Gp (g :: c) (x :: Xs)))) = μ (fun g => It μ Xs.length (fun c => F (g :: c)))
    have e : ∀ g, It μ Xs.length (fun c => F (zipMul Gp (g :: c) (x :: Xs))) = It μ Xs.length (fun c => F (Gp.mul g x :: c)) :=
      fun g => It_rinv μ hμ hr Xs (fun c => F (Gp.mul g x :: c))
    rw [hμ.ext _ _ e]
    exact hr (fun g => It μ Xs.length (fun c => F (g :: c))) x

end AxialAlg

#print axioms AxialAlg.rho_gprod
#print axioms AxialAlg.rho_interleave
#print axioms AxialAlg.It_rinv

ym/StripAxial.lean

/-
  ym/StripAxial — T45, cycle 69 (Lad3 step 10, any compact group): the genuine 1 × A lattice strip, non-abelian.

  Links: bottom h_0…h_{A−1} (integrated), top t_k and vertical v_k (any fixed values; the remaining integrals are of
  constants). Plaquette k: U_k = h_k v_{k+1} t_k⁻¹ v_k⁻¹. Wilson loop around the strip: W = h_0⋯h_{A−1} · Y,
  Y = v_A t_{A−1}⁻¹ ⋯ t_0⁻¹ v_0⁻¹.
    tele            (v_k t_k v_{k+1}⁻¹)(v_{k+1} t_{k+1} v_{k+2}⁻¹)⋯ = v_k t_k⋯t_{k+n−1} v_{k+n}⁻¹
    strip_axial     It_A(h ↦ Π_k w(U_k) · tr ρ(W)) = d λ^A,   It_A(h ↦ Π_k w(U_k)) = z^A
  for a right-invariant linear μ with Schur averages μ(wρ_ik) = λδ_ik, μ(w) = z. Hence ⟨W⟩ = d (λ/z)^A for the lattice
  strip itself (no independence assumed): substitute h_k ↦ h_k X_k (X_k = v_k t_k v_{k+1}⁻¹, right invariance), the
  plaquettes become h_k, the loop becomes ρ((h_0X_0)⋯(h_{A−1}X_{A−1})) ρ(Y), and X_0⋯X_{A−1}Y = 1.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.AxialAlg

namespace StripAxial

open YMChain LinkIntegral Strip2D Sandwich AxialAlg

variable {R : Type} (K : DetChain.CRing R) {G : Type} (Gp : Grp G) {ι : Type} [DecidableEq ι]
variable (t v : Nat → G)

def X (k : Nat) : G := Gp.mul (Gp.mul (v k) (t k)) (Gp.inv (v (k + 1)))

def xl (f : Nat → G) : Nat → Nat → List G
  | _, 0 => []
  | k, n + 1 => f k :: xl f (k + 1) n

def tp : Nat → Nat → G
  | _, 0 => Gp.one
  | k, n + 1 => Gp.mul (t k) (tp (k + 1) n)

def tinv : Nat → G
  | 0 => Gp.one
  | n + 1 => Gp.mul (Gp.inv (t n)) (tinv n)

def Y (A : Nat) : G := Gp.mul (Gp.mul (v A) (tinv Gp t A)) (Gp.inv (v 0))

def U (h : List G) : List G := zipMul Gp h (xl (fun k => Gp.inv (X Gp t v k)) 0 h.length)

theorem len_xl (f : Nat → G) : ∀ k n, (xl f k n).length = n
  | _, 0 => rfl
  | k, n + 1 => congrArg Nat.succ (len_xl f (k + 1) n)

/-- **Telescoping.** -/
theorem tele : ∀ (n k : Nat), gprod Gp (xl (X Gp t v) k n) = Gp.mul (Gp.mul (v k) (tp Gp t k n)) (Gp.inv (v (k + n)))
  | 0, k => by
    show Gp.one = Gp.mul (Gp.mul (v k) Gp.one) (Gp.inv (v (k + 0)))
    rw [Gp.mul_one, Nat.add_zero, Gp.mul_inv]
  | n + 1, k => by
    show Gp.mul (X Gp t v k) (gprod Gp (xl (X Gp t v) (k + 1) n)) = _
    rw [tele n (k + 1)]
    show Gp.mul (Gp.mul (Gp.mul (v k) (t k)) (Gp.inv (v (k + 1)))) (Gp.mul (Gp.mul (v (k + 1)) (tp Gp t (k + 1) n)) (Gp.inv (v (k + 1 + n))))
      = Gp.mul (Gp.mul (v k) (Gp.mul (t k) (tp Gp t (k + 1) n))) (Gp.inv (v (k + (n + 1))))
    rw [show k + 1 + n = k + (n + 1) from by rw [Nat.add_assoc, Nat.add_comm 1 n]]
    rw [Gp.assoc (Gp.mul (v k) (t k)), ← Gp.assoc (Gp.inv (v (k + 1))), ← Gp.assoc (Gp.inv (v (k + 1))), Gp.inv_mul,
      Gp.one_mul, ← Gp.assoc, ← Gp.assoc, Gp.assoc (v k)]

theorem tp_tinv : ∀ n, Gp.mul (tp Gp t 0 n) (tinv Gp t n) = Gp.one := by
  -- t_0⋯t_{n−1} · t_{n−1}⁻¹⋯t_0⁻¹ = 1; prove the shifted form
  have key : ∀ n k, Gp.mul (tp Gp t k (n + 1)) (Gp.inv (t (k + n))) = tp Gp t k n := by
    intro n
    induction n with
    | zero => intro k; show Gp.mul (Gp.mul (t k) Gp.one) (Gp.inv (t (k + 0))) = Gp.one; rw [Gp.mul_one, Nat.add_zero, Gp.mul_inv]
    | succ n ih =>
      intro k
      show Gp.mul (Gp.mul (t k) (tp Gp t (k + 1) (n + 1))) (Gp.inv (t (k + (n + 1)))) = Gp.mul (t k) (tp Gp t (k + 1) n)
      rw [Gp.assoc, show k + (n + 1) = k + 1 + n from by rw [Nat.add_assoc, Nat.add_comm n 1], ih (k + 1)]
  intro n
  induction n with
  | zero => show Gp.mul Gp.one Gp.one = Gp.one; rw [Gp.one_mul]
  | succ n ih =>
    show Gp.mul (tp Gp t 0 (n + 1)) (Gp.mul (Gp.inv (t n)) (tinv Gp t n)) = Gp.one
    have k0 := key n 0
    rw [Nat.zero_add] at k0
    rw [← Gp.assoc, k0]
    exact ih

theorem XY_one (A : Nat) : Gp.mul (gprod Gp (xl (X Gp t v) 0 A)) (Y Gp t v A) = Gp.one := by
  rw [tele Gp t v A 0, Nat.zero_add]
  show Gp.mul (Gp.mul (Gp.mul (v 0) (tp Gp t 0 A)) (Gp.inv (v A))) (Gp.mul (Gp.mul (v A) (tinv Gp t A)) (Gp.inv (v 0))) = Gp.one
  rw [Gp.assoc (Gp.mul (v 0) (tp Gp t 0 A)), ← Gp.assoc (Gp.inv (v A)), ← Gp.assoc (Gp.inv (v A)), Gp.inv_mul, Gp.one_mul,
    ← Gp.assoc, Gp.assoc (v 0), tp_tinv Gp t A, Gp.mul_one, Gp.mul_inv]

theorem zip_cancel : ∀ (h Xs : List G), h.length = Xs.length →
    zipMul Gp (zipMul Gp h Xs) (Xs.map Gp.inv) = h
  | [], [], _ => rfl
  | [], _ :: _, hl => absurd hl Nat.noConfusion
  | _ :: _, [], hl => absurd hl Nat.noConfusion
  | g :: h, x :: Xs, hl => by
    show Gp.mul (Gp.mul g x) (Gp.inv x) :: zipMul Gp (zipMul Gp h Xs) (Xs.map Gp.inv) = g :: h
    rw [Gp.assoc, Gp.mul_inv, Gp.mul_one, zip_cancel h Xs (Nat.succ.inj hl)]

theorem xl_map_inv (f : Nat → G) : ∀ k n, (xl f k n).map Gp.inv = xl (fun j => Gp.inv (f j)) k n
  | _, 0 => rfl
  | k, n + 1 => by show Gp.inv (f k) :: (xl f (k + 1) n).map Gp.inv = _ :: _; rw [xl_map_inv f (k + 1) n]

theorem len_zipMul : ∀ (h Xs : List G), h.length = Xs.length → (zipMul Gp h Xs).length = h.length
  | [], [], _ => rfl
  | [], _ :: _, hl => absurd hl Nat.noConfusion
  | _ :: _, [], hl => absurd hl Nat.noConfusion
  | _ :: h, _ :: Xs, hl => congrArg Nat.succ (len_zipMul h Xs (Nat.succ.inj hl))

theorem len_map_g {α β : Type} (f : α → β) : ∀ l : List α, (l.map f).length = l.length
  | [] => rfl
  | _ :: l => congrArg Nat.succ (len_map_g f l)

theorem It_ext_len (μ : (G → R) → R) (hμ : Lin K μ) : ∀ (A : Nat) (F F' : List G → R),
    (∀ c, c.length = A → F c = F' c) → It μ A F = It μ A F'
  | 0, F, F', h => h [] rfl
  | A + 1, F, F', h => hμ.ext _ _ (fun g => It_ext_len μ hμ A _ _ (fun c hc => h (g :: c) (congrArg Nat.succ hc)))

/-- **The 1 × A lattice strip, non-abelian, exactly.** -/
theorem strip_axial (μ : (G → R) → R) (hμ : Lin K μ) (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
    (ρ : G → ι → ι → R) (idx : List ι)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j)
    (w : G → R) (lam z : R)
    (hschur : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k))
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i)
    (hz : μ w = z) (A : Nat) :
    It μ A (fun h => K.mul (wprod K w (U Gp t v h)) (sumK K (fun i => ρ (Gp.mul (gprod Gp h) (Y Gp t v A)) i i) idx))
        = K.mul (sumK K (fun _ => K.one) idx) (pwR K lam A) ∧
      It μ A (fun h => wprod K w (U Gp t v h)) = pwR K z A := by
  let Xs := xl (X Gp t v) 0 A
  have hXs : Xs.length = A := len_xl _ 0 A
  have hsub : ∀ F : List G → R, It μ A F = It μ A (fun c => F (zipMul Gp c Xs)) := fun F => by
    have := It_rinv K Gp μ hμ hr Xs F
    rw [hXs] at this
    exact this.symm
  have hU : ∀ c : List G, c.length = A → U Gp t v (zipMul Gp c Xs) = c := fun c hc => by
    show zipMul Gp (zipMul Gp c Xs) (xl (fun k => Gp.inv (X Gp t v k)) 0 (zipMul Gp c Xs).length) = c
    rw [len_zipMul Gp c Xs (hc.trans hXs.symm), hc, ← xl_map_inv Gp (X Gp t v) 0 A]
    exact zip_cancel Gp c Xs (hc.trans hXs.symm)
  constructor
  · rw [hsub]
    rw [It_ext_len K μ hμ A _ (fun c => sumK K (fun i => sumK K (fun j =>
        K.mul (K.mul (wprod K w c) (msand K ρ idx c (Xs.map ρ) i j)) (ρ (Y Gp t v A) j i)) idx) idx) (fun c hc => by
        rw [hU c hc, mul_sumK]
        refine sumK_ext K _ _ (fun i => ?_) idx
        rw [hom, mul_sumK]
        refine sumK_ext K _ _ (fun j => ?_) idx
        rw [rho_interleave K Gp ρ idx hom hone c Xs (hc.trans hXs.symm) i j, K.mul_assoc])]
    have step : ∀ i, It μ A (fun c => sumK K (fun j =>
        K.mul (K.mul (wprod K w c) (msand K ρ idx c (Xs.map ρ) i j)) (ρ (Y Gp t v A) j i)) idx)
        = sumK K (fun j => K.mul (ρ (Y Gp t v A) j i) (K.mul (pwR K lam A) (mlist K idx (Xs.map ρ) i j))) idx := by
      intro i
      rw [It_sum K μ hμ A _ idx]
      refine sumK_ext K _ _ (fun j => ?_) idx
      rw [It_ext K μ hμ A _ (fun c => K.mul (ρ (Y Gp t v A) j i) (K.mul (wprod K w c) (msand K ρ idx c (Xs.map ρ) i j)))
        (fun c => K.mul_comm _ _), It_smul K μ hμ]
      have sw := sandwich K μ hμ ρ idx w lam hschur hsel1 (Xs.map ρ) i j
      rw [len_map_g, hXs] at sw
      rw [sw]
    rw [It_sum K μ hμ A _ idx, sumK_ext K _ _ step]
    -- Σ_i Σ_j ρY_ji λ^A mlist_ij = λ^A Σ_i ρ(ΠX · Y)_ii = λ^A d
    have e : ∀ i, sumK K (fun j => K.mul (ρ (Y Gp t v A) j i) (K.mul (pwR K lam A) (mlist K idx (Xs.map ρ) i j))) idx
        = K.mul (pwR K lam A) (delta K i i) := fun i => by
      rw [← hone i i, ← XY_one Gp t v A, hom, mul_sumK]
      refine sumK_ext K _ _ (fun j => ?_) idx
      rw [rho_gprod K Gp ρ idx hom hone Xs i j, K.mul_comm (ρ (Y Gp t v A) j i), K.mul_assoc]
    rw [sumK_ext K _ _ e, ← mul_sumK, K.mul_comm]
    congr 1
    exact sumK_ext K _ _ (fun i => MaskSum.ip rfl) idx
  · rw [hsub]
    rw [It_ext_len K μ hμ A _ (wprod K w) (fun c hc => by rw [hU c hc])]
    clear hsub hU hXs
    induction A with
    | zero => rfl
    | succ A ih =>
      show μ (fun g => It μ A (fun c => K.mul (w g) (wprod K w c))) = K.mul (pwR K z A) z
      rw [hμ.ext _ (fun g => K.mul (w g) (It μ A (wprod K w))) (fun g => It_smul K μ hμ A (w g) _), ih]
      rw [hμ.ext _ (fun g => K.mul (pwR K z A) (w g)) (fun g => K.mul_comm _ _), hμ.smul, hz]

end StripAxial

#print axioms StripAxial.tele
#print axioms StripAxial.tp_tinv
#print axioms StripAxial.XY_one
#print axioms StripAxial.zip_cancel
#print axioms StripAxial.strip_axial

ym/Peel.lean

/-
  ym/Peel — T45, cycle 70 (Lad3 step 11, any compact group): peeling one row of plaquettes.

    peel        It_R(h ↦ Π_k w(h_k X_k⁻¹) · tr ρ((h_0⋯h_{R−1}) Q)) = λ^R tr ρ((X_0⋯X_{R−1}) Q)
    peelZ       It_R(h ↦ Π_k w(h_k X_k⁻¹)) = z^R
    tr_conj     tr ρ(g W g⁻¹) = tr ρ(W)
  For an R × T rectangle, the bottom-row plaquettes are h_k X_k⁻¹ with X_k = v_k t_k v_{k+1}⁻¹ and the loop is
  (h_0⋯h_{R−1})·Q; after peeling, (X_0⋯X_{R−1})Q is a conjugate of the loop of the rectangle one row lower.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.StripAxial

namespace Peel

open YMChain LinkIntegral Strip2D Sandwich AxialAlg StripAxial

variable {R : Type} (K : DetChain.CRing R) {G : Type} (Gp : Grp G) {ι : Type} [DecidableEq ι]

theorem peel (μ : (G → R) → R) (hμ : Lin K μ) (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
    (ρ : G → ι → ι → R) (idx : List ι)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j)
    (w : G → R) (lam : R)
    (hschur : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k))
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i)
    (Xs : List G) (Q : G) :
    It μ Xs.length (fun h => K.mul (wprod K w (zipMul Gp h (Xs.map Gp.inv))) (sumK K (fun i => ρ (Gp.mul (gprod Gp h) Q) i i) idx))
      = K.mul (pwR K lam Xs.length) (sumK K (fun i => ρ (Gp.mul (gprod Gp Xs) Q) i i) idx) := by
  have hsub : ∀ F : List G → R, It μ Xs.length F = It μ Xs.length (fun c => F (zipMul Gp c Xs)) :=
    fun F => (It_rinv K Gp μ hμ hr Xs F).symm
  rw [hsub]
  rw [It_ext_len K μ hμ _ _ (fun c => sumK K (fun i => sumK K (fun j =>
      K.mul (K.mul (wprod K w c) (msand K ρ idx c (Xs.map ρ) i j)) (ρ Q j i)) idx) idx) (fun c hc => by
      rw [zip_cancel Gp c Xs hc, mul_sumK]
      refine sumK_ext K _ _ (fun i => ?_) idx
      rw [hom, mul_sumK]
      refine sumK_ext K _ _ (fun j => ?_) idx
      rw [rho_interleave K Gp ρ idx hom hone c Xs hc i j, K.mul_assoc])]
  have step : ∀ i, It μ Xs.length (fun c => sumK K (fun j =>
      K.mul (K.mul (wprod K w c) (msand K ρ idx c (Xs.map ρ) i j)) (ρ Q j i)) idx)
      = sumK K (fun j => K.mul (ρ Q j i) (K.mul (pwR K lam Xs.length) (mlist K idx (Xs.map ρ) i j))) idx := by
    intro i
    rw [It_sum K μ hμ _ _ idx]
    refine sumK_ext K _ _ (fun j => ?_) idx
    rw [It_ext K μ hμ _ _ (fun c => K.mul (ρ Q j i) (K.mul (wprod K w c) (msand K ρ idx c (Xs.map ρ) i j)))
      (fun c => K.mul_comm _ _), It_smul K μ hμ]
    have sw := sandwich K μ hμ ρ idx w lam hschur hsel1 (Xs.map ρ) i j
    rw [len_map_g] at sw
    rw [sw]
  rw [It_sum K μ hμ _ _ idx, sumK_ext K _ _ step, mul_sumK]
  refine sumK_ext K _ _ (fun i => ?_) idx
  rw [hom, mul_sumK]
  refine sumK_ext K _ _ (fun j => ?_) idx
  rw [rho_gprod K Gp ρ idx hom hone Xs i j, K.mul_comm (ρ Q j i), K.mul_assoc]

theorem peelZ (μ : (G → R) → R) (hμ : Lin K μ) (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
    (w : G → R) (z : R) (hz : μ w = z) (Xs : List G) :
    It μ Xs.length (fun h => wprod K w (zipMul Gp h (Xs.map Gp.inv))) = pwR K z Xs.length := by
  rw [(It_rinv K Gp μ hμ hr Xs _).symm, It_ext_len K μ hμ _ _ (wprod K w) (fun c hc => by rw [zip_cancel Gp c Xs hc])]
  generalize Xs.length = A
  induction A with
  | zero => rfl
  | succ A ih =>
    show μ (fun g => It μ A (fun c => K.mul (w g) (wprod K w c))) = K.mul (pwR K z A) z
    rw [hμ.ext _ (fun g => K.mul (w g) (It μ A (wprod K w))) (fun g => It_smul K μ hμ A (w g) _), ih]
    rw [hμ.ext _ (fun g => K.mul (pwR K z A) (w g)) (fun g => K.mul_comm _ _), hμ.smul, hz]

/-- **Trace is conjugation invariant.** -/
theorem tr_conj (ρ : G → ι → ι → R) (idx : List ι)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j)
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i) (g W : G) :
    sumK K (fun i => ρ (Gp.mul (Gp.mul g W) (Gp.inv g)) i i) idx = sumK K (fun i => ρ W i i) idx := by
  calc sumK K (fun i => ρ (Gp.mul (Gp.mul g W) (Gp.inv g)) i i) idx
      = sumK K (fun i => sumK K (fun k => K.mul (ρ (Gp.mul g W) i k) (ρ (Gp.inv g) k i)) idx) idx :=
        sumK_ext K _ _ (fun i => hom _ _ i i) idx
    _ = sumK K (fun k => sumK K (fun i => K.mul (ρ (Gp.mul g W) i k) (ρ (Gp.inv g) k i)) idx) idx :=
        sumK_swap K (fun i k => K.mul (ρ (Gp.mul g W) i k) (ρ (Gp.inv g) k i)) idx idx
    _ = sumK K (fun k => ρ (Gp.mul (Gp.inv g) (Gp.mul g W)) k k) idx :=
        sumK_ext K _ _ (fun k => by rw [hom]; exact sumK_ext K _ _ (fun i => K.mul_comm _ _) idx) idx
    _ = sumK K (fun i => ρ W i i) idx :=
        sumK_ext K _ _ (fun k => by rw [← Gp.assoc, Gp.inv_mul, Gp.one_mul]) idx

end Peel

#print axioms Peel.peel
#print axioms Peel.peelZ
#print axioms Peel.tr_conj

ym/Sites.lean

/-
  ym/Sites — T45, cycle 71 (Lad3 step 12): iterated integrals over named links.

  A gauge field is a function f : S → G on link names S (DecidableEq). For a list of sites ss,
    IntS ss Φ f = μ(g₁ ↦ μ(g₂ ↦ ⋯ Φ(f[s₁ := g₁, s₂ := g₂, …]) ⋯)),
  i.e. the integral over the links in ss with the others fixed at f.
    IntS_app        IntS (l₁ ++ l₂) Φ f = IntS l₁ (IntS l₂ Φ) f
    IntS_const      if Φ ignores the links of ss, IntS ss Φ f = Φ f          (μ normalised)
    IntS_ext        pointwise equal integrands (on all fields) give equal integrals
    IntS_smul       constants (functions of links outside ss) factor out
    IntS_row        integrating a row of distinct sites s_0…s_{n−1} is the list integral It_n of Strip2D

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Peel

namespace Sites

open YMChain LinkIntegral Strip2D MaskSum

variable {R : Type} (K : DetChain.CRing R) {G : Type} {S : Type} [DecidableEq S]

def upd (f : S → G) (s : S) (g : G) : S → G := fun s' => if s' = s then g else f s'

def IntS (μ : (G → R) → R) : List S → ((S → G) → R) → (S → G) → R
  | [], Φ, f => Φ f
  | s :: ss, Φ, f => μ (fun g => IntS μ ss Φ (upd f s g))

variable (μ : (G → R) → R)

theorem IntS_app (hμ : Lin K μ) : ∀ (l₁ l₂ : List S) (Φ : (S → G) → R) (f : S → G),
    IntS μ (l₁ ++ l₂) Φ f = IntS μ l₁ (IntS μ l₂ Φ) f
  | [], _, _, _ => rfl
  | s :: l₁, l₂, Φ, f => by
    show μ (fun g => IntS μ (l₁ ++ l₂) Φ (upd f s g)) = μ (fun g => IntS μ l₁ (IntS μ l₂ Φ) (upd f s g))
    rw [hμ.ext _ _ (fun g => IntS_app hμ l₁ l₂ Φ (upd f s g))]

theorem IntS_ext (hμ : Lin K μ) : ∀ (ss : List S) (Φ Ψ : (S → G) → R), (∀ f, Φ f = Ψ f) → ∀ f, IntS μ ss Φ f = IntS μ ss Ψ f
  | [], _, _, h, f => h f
  | s :: ss, Φ, Ψ, h, f => hμ.ext _ _ (fun g => IntS_ext hμ ss Φ Ψ h (upd f s g))

theorem mu_const (hμ : Lin K μ) (hμ1 : μ (fun _ => K.one) = K.one) (c : R) : μ (fun _ => c) = c := by
  have := hμ.smul c (fun _ => K.one)
  rw [hμ1, K.mul_comm c K.one, K.one_mul] at this
  exact this

/-- Φ does not depend on the links in ss. -/
def Indep (ss : List S) (Φ : (S → G) → R) : Prop := ∀ s, memL s ss → ∀ f g, Φ (upd f s g) = Φ f

theorem IntS_const (hμ : Lin K μ) (hμ1 : μ (fun _ => K.one) = K.one) : ∀ (ss : List S) (Φ : (S → G) → R), Indep ss Φ →
    ∀ f, IntS μ ss Φ f = Φ f
  | [], _, _, _ => rfl
  | s :: ss, Φ, h, f => by
    show μ (fun g => IntS μ ss Φ (upd f s g)) = Φ f
    rw [hμ.ext _ (fun _ => Φ f) (fun g => by
      rw [IntS_const hμ hμ1 ss Φ (fun s' hs' => h s' (Or.inr hs')) (upd f s g), h s (Or.inl rfl) f g])]
    exact mu_const K μ hμ hμ1 _

theorem IntS_smul (hμ : Lin K μ) : ∀ (ss : List S) (a : (S → G) → R) (Φ : (S → G) → R), Indep ss a →
    ∀ f, IntS μ ss (fun f' => K.mul (a f') (Φ f')) f = K.mul (a f) (IntS μ ss Φ f)
  | [], _, _, _, _ => rfl
  | s :: ss, a, Φ, h, f => by
    show μ (fun g => IntS μ ss (fun f' => K.mul (a f') (Φ f')) (upd f s g)) = K.mul (a f) (μ (fun g => IntS μ ss Φ (upd f s g)))
    rw [hμ.ext _ (fun g => K.mul (a f) (IntS μ ss Φ (upd f s g))) (fun g => by
      rw [IntS_smul hμ ss a Φ (fun s' hs' => h s' (Or.inr hs')) (upd f s g), h s (Or.inl rfl) f g]), hμ.smul]

/-- The values of a field along a list of sites. -/
def vals (f : S → G) : List S → List G
  | [] => []
  | s :: ss => f s :: vals f ss

/-- Distinct sites. -/
def nodupS : List S → Prop
  | [] => True
  | s :: ss => ¬ memL s ss ∧ nodupS ss

theorem vals_upd_notin (f : S → G) (s : S) (g : G) : ∀ ss : List S, ¬ memL s ss → vals (upd f s g) ss = vals f ss
  | [], _ => rfl
  | s' :: ss, h => by
    show (if s' = s then g else f s') :: vals (upd f s g) ss = f s' :: vals f ss
    rw [ineg (fun e => h (Or.inl e.symm)), vals_upd_notin f s g ss (fun h' => h (Or.inr h'))]

/-- **A row of distinct sites is a list integral** (the integrand may also depend on links outside the row). -/
theorem IntS_row (hμ : Lin K μ) : ∀ (ss : List S) (F : (S → G) → List G → R), nodupS ss →
    (∀ s, memL s ss → ∀ f' g c, F (upd f' s g) c = F f' c) →
    ∀ f, IntS μ ss (fun f' => F f' (vals f' ss)) f = It μ ss.length (F f)
  | [], _, _, _, _ => rfl
  | s :: ss, F, hnd, hF, f => by
    show μ (fun g => IntS μ ss (fun f' => F f' (f' s :: vals f' ss)) (upd f s g))
      = μ (fun g => It μ ss.length (fun c => F f (g :: c)))
    refine hμ.ext _ _ (fun g => ?_)
    have ih := IntS_row hμ ss (fun f' c => F f' (f' s :: c)) hnd.2 (fun s' hs' f' g' c => by
      show F (upd f' s' g') ((upd f' s' g') s :: c) = F f' (f' s :: c)
      have hne : s ≠ s' := fun e => hnd.1 (e ▸ hs')
      rw [show (upd f' s' g') s = f' s from ineg hne, hF s' (Or.inr hs') f' g' _]) (upd f s g)
    rw [ih]
    refine It_ext K μ hμ _ _ _ (fun c => ?_)
    show F (upd f s g) ((upd f s g) s :: c) = F f (g :: c)
    rw [show (upd f s g) s = g from ip rfl, hF s (Or.inl rfl) f g]

end Sites

#print axioms Sites.IntS_app
#print axioms Sites.IntS_const
#print axioms Sites.IntS_smul
#print axioms Sites.IntS_row

ym/RectGeom.lean

/-
  ym/RectGeom — T45, cycle 72 (Lad3 step 13, any group): geometry of an R × T rectangle in 2D.

  Links: horizontal H r k (height r, column k), vertical V r k (from height r to r+1 at column k).
    pl r k       = H r k · V r (k+1) · H (r+1) k⁻¹ · V r k⁻¹             (plaquette, counterclockwise)
    hp r k n     = H r k ⋯ H r (k+n−1)                                  (row product)
    rgt r0 T     = V r0 R ⋯ V (r0+T−1) R                                 (right side, upwards)
    hinv r n     = H r (n−1)⁻¹ ⋯ H r 0⁻¹                                 (top side, leftwards)
    linv r0 T    = V (r0+T−1) 0⁻¹ ⋯ V r0 0⁻¹                             (left side, downwards)
    Wl r0 T      = hp r0 0 R · (rgt r0 T · (hinv (r0+T) R · linv r0 T))  (the Wilson loop from corner (0, r0))
    Xb r0 k      = V r0 k · H (r0+1) k · V r0 (k+1)⁻¹,   pl r0 k = H r0 k · (Xb r0 k)⁻¹
    peel_id      (Xb r0 0 ⋯ Xb r0 (R−1)) · (rgt r0 (T+1) · (hinv (r0+T+1) R · linv r0 (T+1)))
                   = V r0 0 · Wl (r0+1) T · V r0 0⁻¹
    Wl_zero      Wl r0 0 = 1
  Peeling the bottom row (Peel.peel) therefore turns the loop of the R × (T+1) rectangle into a conjugate of the loop
  of the R × T rectangle above it.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Sites

namespace RectGeom

open YMChain AxialAlg StripAxial

variable {G : Type} (Gp : Grp G) (R : Nat) (H V : Nat → Nat → G)

def pl (r k : Nat) : G := Gp.mul (Gp.mul (Gp.mul (H r k) (V r (k + 1))) (Gp.inv (H (r + 1) k))) (Gp.inv (V r k))

def hp (r : Nat) : Nat → Nat → G
  | _, 0 => Gp.one
  | k, n + 1 => Gp.mul (H r k) (hp r (k + 1) n)

def rgt (r0 : Nat) : Nat → G
  | 0 => Gp.one
  | T + 1 => Gp.mul (V r0 R) (rgt (r0 + 1) T)

def hinv (r : Nat) : Nat → G
  | 0 => Gp.one
  | n + 1 => Gp.mul (Gp.inv (H r n)) (hinv r n)

def linv (r0 : Nat) : Nat → G
  | 0 => Gp.one
  | T + 1 => Gp.mul (linv (r0 + 1) T) (Gp.inv (V r0 0))

def Wl (r0 T : Nat) : G :=
  Gp.mul (hp Gp H r0 0 R) (Gp.mul (rgt Gp R V r0 T) (Gp.mul (hinv Gp H (r0 + T) R) (linv Gp V r0 T)))

def Xb (r0 k : Nat) : G := Gp.mul (Gp.mul (V r0 k) (H (r0 + 1) k)) (Gp.inv (V r0 (k + 1)))

theorem inv_mul_rev (a b : G) : Gp.inv (Gp.mul a b) = Gp.mul (Gp.inv b) (Gp.inv a) :=
  inv_unique Gp _ _ (by rw [Gp.assoc, ← Gp.assoc b, Gp.mul_inv, Gp.one_mul, Gp.mul_inv])

theorem inv_inv' (a : G) : Gp.inv (Gp.inv a) = a := inv_unique Gp _ _ (Gp.inv_mul a)

theorem pl_X (r0 k : Nat) : pl Gp H V r0 k = Gp.mul (H r0 k) (Gp.inv (Xb Gp H V r0 k)) := by
  unfold pl Xb
  rw [inv_mul_rev, inv_mul_rev, inv_inv', Gp.assoc, Gp.assoc]

theorem hp_tp (r : Nat) : ∀ k n, hp Gp H r k n = tp Gp (H r) k n
  | _, 0 => rfl
  | k, n + 1 => by show Gp.mul (H r k) (hp Gp H r (k + 1) n) = Gp.mul (H r k) (tp Gp (H r) (k + 1) n); rw [hp_tp r (k + 1) n]

theorem hinv_tinv (r : Nat) : ∀ n, hinv Gp H r n = tinv Gp (H r) n
  | 0 => rfl
  | n + 1 => by show Gp.mul (Gp.inv (H r n)) (hinv Gp H r n) = Gp.mul (Gp.inv (H r n)) (tinv Gp (H r) n); rw [hinv_tinv r n]

theorem Wl_zero (r0 : Nat) : Wl Gp R H V r0 0 = Gp.one := by
  unfold Wl
  show Gp.mul (hp Gp H r0 0 R) (Gp.mul Gp.one (Gp.mul (hinv Gp H (r0 + 0) R) Gp.one)) = Gp.one
  rw [Gp.one_mul, Gp.mul_one, Nat.add_zero, hp_tp, hinv_tinv]
  exact tp_tinv Gp (H r0) R

theorem gprod_Xb (r0 : Nat) : ∀ n k,
    gprod Gp (xl (Xb Gp H V r0) k n) = Gp.mul (Gp.mul (V r0 k) (hp Gp H (r0 + 1) k n)) (Gp.inv (V r0 (k + n))) := by
  intro n k
  have := tele Gp (H (r0 + 1)) (V r0) n k
  rw [hp_tp]
  exact this

/-- **The peeling identity.** -/
theorem peel_id (r0 T : Nat) :
    Gp.mul (gprod Gp (xl (Xb Gp H V r0) 0 R))
        (Gp.mul (rgt Gp R V r0 (T + 1)) (Gp.mul (hinv Gp H (r0 + (T + 1)) R) (linv Gp V r0 (T + 1))))
      = Gp.mul (Gp.mul (V r0 0) (Wl Gp R H V (r0 + 1) T)) (Gp.inv (V r0 0)) := by
  rw [gprod_Xb Gp H V r0 R 0, Nat.zero_add]
  show Gp.mul (Gp.mul (Gp.mul (V r0 0) (hp Gp H (r0 + 1) 0 R)) (Gp.inv (V r0 R)))
      (Gp.mul (Gp.mul (V r0 R) (rgt Gp R V (r0 + 1) T))
        (Gp.mul (hinv Gp H (r0 + (T + 1)) R) (Gp.mul (linv Gp V (r0 + 1) T) (Gp.inv (V r0 0)))))
    = Gp.mul (Gp.mul (V r0 0) (Gp.mul (hp Gp H (r0 + 1) 0 R) (Gp.mul (rgt Gp R V (r0 + 1) T)
        (Gp.mul (hinv Gp H (r0 + 1 + T) R) (linv Gp V (r0 + 1) T))))) (Gp.inv (V r0 0))
  rw [show r0 + (T + 1) = r0 + 1 + T from by rw [Nat.add_assoc, Nat.add_comm T 1]]
  -- cancel V r0 R ⁻¹ · V r0 R and reassociate
  rw [Gp.assoc (Gp.mul (V r0 0) (hp Gp H (r0 + 1) 0 R)), Gp.assoc (V r0 R), ← Gp.assoc (Gp.inv (V r0 R)) (V r0 R),
    Gp.inv_mul, Gp.one_mul]
  repeat rw [Gp.assoc]

end RectGeom

#print axioms RectGeom.pl_X
#print axioms RectGeom.Wl_zero
#print axioms RectGeom.peel_id

ym/RectInt.lean

/-
  ym/RectInt — T45, cycle 73 (Lad3 step 14, any compact group): the R × T rectangle in 2D, exactly.

  Link names S = Bool × Nat × Nat: (true, r, k) is H r k, (false, r, k) is V r k. Field f : S → G.
    hs r k n, vs r            the sites of a horizontal row / of a vertical row (R + 1 links)
    sitesR r0 T               all links of the R × T rectangle on rows r0 … r0+T−1, ordered
                              (vertical row r0) ++ (rectangle above) ++ (bottom row r0)
    plaqProd f r0 T           Π over its plaquettes of w(pl)
    rect_W, rect_Z            IntS (sitesR r0 T) (plaqProd · tr ρ(Wl)) f = d λ^{RT},   IntS (sitesR r0 T) plaqProd f = z^{RT}
  So ⟨W_{R×T}⟩ = d (λ/z)^{RT}: the exact 2D area law for every rectangle (any compact group with a right-invariant
  normalised one-link integral and Schur averages; SU(2) given H0).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.RectGeom

namespace RectInt

open YMChain LinkIntegral MaskSum Strip2D Sandwich AxialAlg StripAxial Peel Sites RectGeom

variable {R : Type} (K : DetChain.CRing R) {G : Type} (Gp : Grp G)

abbrev S := Bool × Nat × Nat

def Hf (f : S → G) (r k : Nat) : G := f (true, r, k)
def Vf (f : S → G) (r k : Nat) : G := f (false, r, k)

def hs (r : Nat) : Nat → Nat → List S
  | _, 0 => []
  | k, n + 1 => (true, r, k) :: hs r (k + 1) n

def vsl (r : Nat) : Nat → Nat → List S
  | _, 0 => []
  | k, n + 1 => (false, r, k) :: vsl r (k + 1) n

theorem vals_hs (f : S → G) (r : Nat) : ∀ k n, vals f (hs r k n) = xl (Hf f r) k n
  | _, 0 => rfl
  | k, n + 1 => by show f (true, r, k) :: vals f (hs r (k + 1) n) = Hf f r k :: xl (Hf f r) (k + 1) n; rw [vals_hs f r (k + 1) n]; rfl

theorem mem_hs (r : Nat) : ∀ k n (s : S), memL s (hs r k n) → s.1 = true ∧ s.2.1 = r ∧ k ≤ s.2.2
  | _, 0, _, h => h.elim
  | k, n + 1, s, h => by
    cases h with
    | inl e => rw [e]; exact ⟨rfl, rfl, Nat.le_refl k⟩
    | inr h' => have := mem_hs r (k + 1) n s h'; exact ⟨this.1, this.2.1, Nat.le_of_succ_le this.2.2⟩

theorem nodup_hs (r : Nat) : ∀ k n, nodupS (hs r k n)
  | _, 0 => trivial
  | k, n + 1 => ⟨fun h => by have := (mem_hs r (k + 1) n _ h).2.2; exact Nat.not_succ_le_self k this,
      nodup_hs r (k + 1) n⟩

theorem zip_xl (a b : Nat → G) : ∀ k n, zipMul Gp (xl a k n) (xl b k n) = xl (fun j => Gp.mul (a j) (b j)) k n
  | _, 0 => rfl
  | k, n + 1 => by show Gp.mul (a k) (b k) :: zipMul Gp (xl a (k + 1) n) (xl b (k + 1) n) = _; rw [zip_xl a b (k + 1) n]; rfl

theorem gprod_xl (r : Nat) (H : Nat → Nat → G) : ∀ k n, gprod Gp (xl (H r) k n) = hp Gp H r k n
  | _, 0 => rfl
  | k, n + 1 => by show Gp.mul (H r k) (gprod Gp (xl (H r) (k + 1) n)) = _; rw [gprod_xl r H (k + 1) n]; rfl

theorem xl_congr (a b : Nat → G) (h : ∀ j, a j = b j) : ∀ k n, xl a k n = xl b k n
  | _, 0 => rfl
  | k, n + 1 => by show a k :: xl a (k + 1) n = b k :: xl b (k + 1) n; rw [h k, xl_congr a b h (k + 1) n]

variable (Rr : Nat) (w : G → R)

def rowW (H V : Nat → Nat → G) (r : Nat) : R := wprod K w (xl (pl Gp H V r) 0 Rr)

def plaqProd (H V : Nat → Nat → G) : Nat → Nat → R
  | _, 0 => K.one
  | r0, T + 1 => K.mul (rowW K Gp Rr w H V r0) (plaqProd H V (r0 + 1) T)

/-- Frame: rows strictly above r0 determine the plaquettes from row r0+1 on. -/
theorem plaqProd_congr (H H' V : Nat → Nat → G) : ∀ (T r1 : Nat), (∀ r k, r1 ≤ r → H r k = H' r k) →
    plaqProd K Gp Rr w H V r1 T = plaqProd K Gp Rr w H' V r1 T
  | 0, _, _ => rfl
  | T + 1, r1, h => by
    show K.mul (rowW K Gp Rr w H V r1) (plaqProd K Gp Rr w H V (r1 + 1) T)
      = K.mul (rowW K Gp Rr w H' V r1) (plaqProd K Gp Rr w H' V (r1 + 1) T)
    rw [plaqProd_congr H H' V T (r1 + 1) (fun r k hr => h r k (Nat.le_of_succ_le hr))]
    congr 1
    unfold rowW
    rw [xl_congr (pl Gp H V r1) (pl Gp H' V r1) (fun j => by
      unfold pl; rw [h r1 j (Nat.le_refl _), h (r1 + 1) j (Nat.le_succ _)]) 0 Rr]

theorem plaqProd_congr2 (H H' V V' : Nat → Nat → G) (hV : ∀ r k, V r k = V' r k) : ∀ (T r1 : Nat),
    (∀ r k, r1 ≤ r → H r k = H' r k) → plaqProd K Gp Rr w H V r1 T = plaqProd K Gp Rr w H' V' r1 T
  | 0, _, _ => rfl
  | T + 1, r1, h => by
    show K.mul (rowW K Gp Rr w H V r1) (plaqProd K Gp Rr w H V (r1 + 1) T)
      = K.mul (rowW K Gp Rr w H' V' r1) (plaqProd K Gp Rr w H' V' (r1 + 1) T)
    rw [plaqProd_congr2 H H' V V' hV T (r1 + 1) (fun r k hr => h r k (Nat.le_of_succ_le hr))]
    congr 1
    unfold rowW
    rw [xl_congr (pl Gp H V r1) (pl Gp H' V' r1) (fun j => by
      unfold pl; rw [h r1 j (Nat.le_refl _), h (r1 + 1) j (Nat.le_succ _), hV, hV]) 0 Rr]

theorem len_hs (r : Nat) : ∀ k n, (hs r k n).length = n
  | _, 0 => rfl
  | k, n + 1 => congrArg Nat.succ (len_hs r (k + 1) n)

theorem rgt_congr (V V' : Nat → Nat → G) (h : ∀ r k, V r k = V' r k) : ∀ r0 T, rgt Gp Rr V r0 T = rgt Gp Rr V' r0 T
  | _, 0 => rfl
  | r0, T + 1 => by show Gp.mul (V r0 Rr) (rgt Gp Rr V (r0 + 1) T) = Gp.mul (V' r0 Rr) (rgt Gp Rr V' (r0 + 1) T); rw [h, rgt_congr V V' h (r0 + 1) T]

theorem linv_congr (V V' : Nat → Nat → G) (h : ∀ r k, V r k = V' r k) : ∀ r0 T, linv Gp V r0 T = linv Gp V' r0 T
  | _, 0 => rfl
  | r0, T + 1 => by show Gp.mul (linv Gp V (r0 + 1) T) (Gp.inv (V r0 0)) = Gp.mul (linv Gp V' (r0 + 1) T) (Gp.inv (V' r0 0)); rw [h, linv_congr V V' h (r0 + 1) T]

theorem hinv_congr (H H' : Nat → Nat → G) (r : Nat) (h : ∀ k, H r k = H' r k) : ∀ n, hinv Gp H r n = hinv Gp H' r n
  | 0 => rfl
  | n + 1 => by show Gp.mul (Gp.inv (H r n)) (hinv Gp H r n) = Gp.mul (Gp.inv (H' r n)) (hinv Gp H' r n); rw [h, hinv_congr H H' r h n]

theorem Hf_upd (f : S → G) (r0 k' : Nat) (g : G) (r k : Nat) (hr : r ≠ r0) : Hf (upd f (true, r0, k') g) r k = Hf f r k :=
  ineg (fun e => hr (congrArg (fun p : S => p.2.1) e))

theorem Vf_upd (f : S → G) (r0 k' : Nat) (g : G) (r k : Nat) : Vf (upd f (true, r0, k') g) r k = Vf f r k :=
  show (if ((false, r, k) : S) = (true, r0, k') then g else f (false, r, k)) = f (false, r, k) from
    ineg (fun e => Bool.noConfusion (congrArg (fun p : S => p.1) e))

section
variable {ι : Type} [DecidableEq ι] (ρ : G → ι → ι → R) (idx : List ι)

def trW (f : S → G) (r0 T : Nat) : R := sumK K (fun i => ρ (Wl Gp Rr (Hf f) (Vf f) r0 T) i i) idx

def sitesR (r0 : Nat) : Nat → List S
  | 0 => hs r0 0 Rr
  | T + 1 => vsl r0 0 (Rr + 1) ++ (sitesR (r0 + 1) T ++ hs r0 0 Rr)

variable (μ : (G → R) → R) (hμ : Lin K μ) (hμ1 : μ (fun _ => K.one) = K.one)
  (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
  (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
  (hone : ∀ i j, ρ Gp.one i j = delta K i j)
  (lam z : R) (hschur : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k))
  (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i) (hz : μ w = z)

/-- **One row.** -/
theorem row_step (hμ : Lin K μ) (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j)
    (lam : R) (hschur : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k))
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i)
    (r0 T : Nat) (f : S → G) :
    IntS μ (hs r0 0 Rr) (fun f' => K.mul (plaqProd K Gp Rr w (Hf f') (Vf f') r0 (T + 1)) (trW K Gp Rr ρ idx f' r0 (T + 1))) f
      = K.mul (pwR K lam Rr) (K.mul (plaqProd K Gp Rr w (Hf f) (Vf f) (r0 + 1) T) (trW K Gp Rr ρ idx f (r0 + 1) T)) := by
  let Xs : (S → G) → List G := fun f' => xl (Xb Gp (Hf f') (Vf f') r0) 0 Rr
  let Q : (S → G) → G := fun f' => Gp.mul (rgt Gp Rr (Vf f') r0 (T + 1))
    (Gp.mul (hinv Gp (Hf f') (r0 + (T + 1)) Rr) (linv Gp (Vf f') r0 (T + 1)))
  let F : (S → G) → List G → R := fun f' c => K.mul (plaqProd K Gp Rr w (Hf f') (Vf f') (r0 + 1) T)
    (K.mul (wprod K w (zipMul Gp c ((Xs f').map Gp.inv))) (sumK K (fun i => ρ (Gp.mul (gprod Gp c) (Q f')) i i) idx))
  -- the integrand in the form F f' (vals f' row)
  have hform : ∀ f', K.mul (plaqProd K Gp Rr w (Hf f') (Vf f') r0 (T + 1)) (trW K Gp Rr ρ idx f' r0 (T + 1))
      = F f' (vals f' (hs r0 0 Rr)) := by
    intro f'
    rw [vals_hs]
    show K.mul (K.mul (rowW K Gp Rr w (Hf f') (Vf f') r0) (plaqProd K Gp Rr w (Hf f') (Vf f') (r0 + 1) T)) _ = _
    have e1 : rowW K Gp Rr w (Hf f') (Vf f') r0 = wprod K w (zipMul Gp (xl (Hf f' r0) 0 Rr) ((Xs f').map Gp.inv)) := by
      unfold rowW
      rw [xl_map_inv, zip_xl]
      rw [xl_congr _ _ (fun j => pl_X Gp (Hf f') (Vf f') r0 j) 0 Rr]
    have e2 : trW K Gp Rr ρ idx f' r0 (T + 1) = sumK K (fun i => ρ (Gp.mul (gprod Gp (xl (Hf f' r0) 0 Rr)) (Q f')) i i) idx := by
      unfold trW Wl
      rw [gprod_xl]
    rw [e1, e2, K.mul_comm (wprod K w _) (plaqProd K Gp Rr w _ _ _ _), K.mul_assoc]
  rw [IntS_ext K μ hμ _ _ _ hform]
  -- F does not depend on the bottom row
  have hind : ∀ s, memL s (hs r0 0 Rr) → ∀ f' g c, F (upd f' s g) c = F f' c := by
    intro s hs' f' g c
    have hm := mem_hs r0 0 Rr s hs'
    obtain ⟨b, r, k⟩ := s
    simp only at hm
    obtain ⟨hb, hrr, _⟩ := hm
    rw [hb, hrr]
    have hH : ∀ r' k', r0 + 1 ≤ r' → Hf (upd f' (true, r0, k) g) r' k' = Hf f' r' k' :=
      fun r' k' h' => Hf_upd f' r0 k g r' k' (fun e => by rw [e] at h'; exact Nat.not_succ_le_self r0 h')
    have hV : ∀ r' k', Vf (upd f' (true, r0, k) g) r' k' = Vf f' r' k' := fun r' k' => Vf_upd f' r0 k g r' k'
    show K.mul (plaqProd K Gp Rr w (Hf (upd f' (true, r0, k) g)) (Vf (upd f' (true, r0, k) g)) (r0 + 1) T) _ = _
    show K.mul (plaqProd K Gp Rr w (Hf (upd f' (true, r0, k) g)) (Vf (upd f' (true, r0, k) g)) (r0 + 1) T)
        (K.mul (wprod K w (zipMul Gp c ((xl (Xb Gp (Hf (upd f' (true, r0, k) g)) (Vf (upd f' (true, r0, k) g)) r0) 0 Rr).map Gp.inv)))
          (sumK K (fun i => ρ (Gp.mul (gprod Gp c) (Gp.mul (rgt Gp Rr (Vf (upd f' (true, r0, k) g)) r0 (T + 1))
            (Gp.mul (hinv Gp (Hf (upd f' (true, r0, k) g)) (r0 + (T + 1)) Rr) (linv Gp (Vf (upd f' (true, r0, k) g)) r0 (T + 1))))) i i) idx))
      = K.mul (plaqProd K Gp Rr w (Hf f') (Vf f') (r0 + 1) T)
        (K.mul (wprod K w (zipMul Gp c ((xl (Xb Gp (Hf f') (Vf f') r0) 0 Rr).map Gp.inv)))
          (sumK K (fun i => ρ (Gp.mul (gprod Gp c) (Gp.mul (rgt Gp Rr (Vf f') r0 (T + 1))
            (Gp.mul (hinv Gp (Hf f') (r0 + (T + 1)) Rr) (linv Gp (Vf f') r0 (T + 1))))) i i) idx))
    rw [plaqProd_congr2 K Gp Rr w _ (Hf f') _ (Vf f') hV T (r0 + 1) hH,
      xl_congr (Xb Gp (Hf (upd f' (true, r0, k) g)) (Vf (upd f' (true, r0, k) g)) r0) (Xb Gp (Hf f') (Vf f') r0)
        (fun j => by unfold Xb; rw [hV, hV, hH (r0 + 1) j (Nat.le_refl _)]) 0 Rr,
      rgt_congr Gp Rr _ (Vf f') hV, linv_congr Gp _ (Vf f') hV,
      hinv_congr Gp _ (Hf f') (r0 + (T + 1)) (fun k' => hH _ k' (by rw [← Nat.add_assoc]; exact Nat.succ_le_succ (Nat.le_add_right r0 T))) Rr]
  rw [IntS_row K μ hμ (hs r0 0 Rr) F (nodup_hs r0 0 Rr) hind f, len_hs]
  show It μ Rr (fun c => K.mul (plaqProd K Gp Rr w (Hf f) (Vf f) (r0 + 1) T)
    (K.mul (wprod K w (zipMul Gp c ((Xs f).map Gp.inv))) (sumK K (fun i => ρ (Gp.mul (gprod Gp c) (Q f)) i i) idx))) = _
  rw [It_smul K μ hμ]
  have pl' := peel K Gp μ hμ hr ρ idx hom hone w lam hschur hsel1 (Xs f) (Q f)
  have hlen : (Xs f).length = Rr := len_xl _ 0 Rr
  rw [hlen] at pl'
  rw [pl']
  have pid := peel_id Gp Rr (Hf f) (Vf f) r0 T
  show K.mul _ (K.mul (pwR K lam Rr) (sumK K (fun i => ρ (Gp.mul (gprod Gp (xl (Xb Gp (Hf f) (Vf f) r0) 0 Rr))
    (Gp.mul (rgt Gp Rr (Vf f) r0 (T + 1)) (Gp.mul (hinv Gp (Hf f) (r0 + (T + 1)) Rr) (linv Gp (Vf f) r0 (T + 1))))) i i) idx)) = _
  rw [pid, tr_conj K Gp ρ idx hom hone hsel1]
  unfold trW
  rw [← K.mul_assoc, K.mul_comm (plaqProd K Gp Rr w _ _ _ _) (pwR K lam Rr), K.mul_assoc]

theorem row_stepZ (hμ : Lin K μ) (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
    (z : R) (hz : μ w = z) (r0 T : Nat) (f : S → G) :
    IntS μ (hs r0 0 Rr) (fun f' => plaqProd K Gp Rr w (Hf f') (Vf f') r0 (T + 1)) f
      = K.mul (pwR K z Rr) (plaqProd K Gp Rr w (Hf f) (Vf f) (r0 + 1) T) := by
  let Xs : (S → G) → List G := fun f' => xl (Xb Gp (Hf f') (Vf f') r0) 0 Rr
  let F : (S → G) → List G → R := fun f' c => K.mul (plaqProd K Gp Rr w (Hf f') (Vf f') (r0 + 1) T)
    (wprod K w (zipMul Gp c ((Xs f').map Gp.inv)))
  have hform : ∀ f', plaqProd K Gp Rr w (Hf f') (Vf f') r0 (T + 1) = F f' (vals f' (hs r0 0 Rr)) := by
    intro f'
    rw [vals_hs]
    show K.mul (rowW K Gp Rr w (Hf f') (Vf f') r0) (plaqProd K Gp Rr w (Hf f') (Vf f') (r0 + 1) T) = _
    have e1 : rowW K Gp Rr w (Hf f') (Vf f') r0 = wprod K w (zipMul Gp (xl (Hf f' r0) 0 Rr) ((Xs f').map Gp.inv)) := by
      unfold rowW
      rw [xl_map_inv, zip_xl]
      rw [xl_congr _ _ (fun j => pl_X Gp (Hf f') (Vf f') r0 j) 0 Rr]
    rw [e1, K.mul_comm]
  rw [IntS_ext K μ hμ _ _ _ hform]
  have hind : ∀ s, memL s (hs r0 0 Rr) → ∀ f' g c, F (upd f' s g) c = F f' c := by
    intro s hs' f' g c
    have hm := mem_hs r0 0 Rr s hs'
    obtain ⟨b, r, k⟩ := s
    simp only at hm
    obtain ⟨hb, hrr, _⟩ := hm
    rw [hb, hrr]
    have hH : ∀ r' k', r0 + 1 ≤ r' → Hf (upd f' (true, r0, k) g) r' k' = Hf f' r' k' :=
      fun r' k' h' => Hf_upd f' r0 k g r' k' (fun e => by rw [e] at h'; exact Nat.not_succ_le_self r0 h')
    have hV : ∀ r' k', Vf (upd f' (true, r0, k) g) r' k' = Vf f' r' k' := fun r' k' => Vf_upd f' r0 k g r' k'
    show K.mul (plaqProd K Gp Rr w (Hf (upd f' (true, r0, k) g)) (Vf (upd f' (true, r0, k) g)) (r0 + 1) T)
        (wprod K w (zipMul Gp c ((xl (Xb Gp (Hf (upd f' (true, r0, k) g)) (Vf (upd f' (true, r0, k) g)) r0) 0 Rr).map Gp.inv)))
      = K.mul (plaqProd K Gp Rr w (Hf f') (Vf f') (r0 + 1) T) (wprod K w (zipMul Gp c ((xl (Xb Gp (Hf f') (Vf f') r0) 0 Rr).map Gp.inv)))
    rw [plaqProd_congr2 K Gp Rr w _ (Hf f') _ (Vf f') hV T (r0 + 1) hH,
      xl_congr (Xb Gp (Hf (upd f' (true, r0, k) g)) (Vf (upd f' (true, r0, k) g)) r0) (Xb Gp (Hf f') (Vf f') r0)
        (fun j => by unfold Xb; rw [hV, hV, hH (r0 + 1) j (Nat.le_refl _)]) 0 Rr]
  rw [IntS_row K μ hμ (hs r0 0 Rr) F (nodup_hs r0 0 Rr) hind f, len_hs]
  show It μ Rr (fun c => K.mul (plaqProd K Gp Rr w (Hf f) (Vf f) (r0 + 1) T) (wprod K w (zipMul Gp c ((Xs f).map Gp.inv)))) = _
  rw [It_smul K μ hμ]
  have pz := peelZ K Gp μ hμ hr w z hz (Xs f)
  have hlen : (Xs f).length = Rr := len_xl _ 0 Rr
  rw [hlen] at pz
  rw [pz, K.mul_comm]

theorem indep_const (ss : List S) (c : R) : Sites.Indep ss (fun _ : S → G => c) := fun _ _ _ _ => rfl

/-- **Wilson loop of the R × T rectangle.** -/
theorem rect_W (hμ : Lin K μ) (hμ1 : μ (fun _ => K.one) = K.one)
    (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j)
    (lam : R) (hschur : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k))
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i) :
    ∀ (T r0 : Nat) (f : S → G),
      IntS μ (sitesR Rr r0 T) (fun f' => K.mul (plaqProd K Gp Rr w (Hf f') (Vf f') r0 T) (trW K Gp Rr ρ idx f' r0 T)) f
        = K.mul (sumK K (fun _ => K.one) idx) (pwR K lam (Rr * T))
  | 0, r0, f => by
    have e : ∀ f' : S → G, K.mul (plaqProd K Gp Rr w (Hf f') (Vf f') r0 0) (trW K Gp Rr ρ idx f' r0 0)
        = K.mul (sumK K (fun _ => K.one) idx) (pwR K lam (Rr * 0)) := by
      intro f'
      show K.mul K.one (sumK K (fun i => ρ (Wl Gp Rr (Hf f') (Vf f') r0 0) i i) idx) = K.mul _ K.one
      rw [K.one_mul, Wl_zero, K.mul_comm, K.one_mul]
      exact sumK_ext K _ _ (fun i => by rw [hone]; exact ip rfl) idx
    rw [IntS_ext K μ hμ _ _ _ e]
    exact IntS_const K μ hμ hμ1 _ _ (indep_const _ _) f
  | T + 1, r0, f => by
    show IntS μ (vsl r0 0 (Rr + 1) ++ (sitesR Rr (r0 + 1) T ++ hs r0 0 Rr)) _ f = _
    rw [IntS_app K μ hμ, IntS_ext K μ hμ _ _ _ (fun f2 => IntS_app K μ hμ _ _ _ f2)]
    rw [IntS_ext K μ hμ _ _ _ (fun f2 => IntS_ext K μ hμ _ _ _ (fun f3 =>
      row_step K Gp Rr w ρ idx μ hμ hr hom hone lam hschur hsel1 r0 T f3) f2)]
    rw [IntS_ext K μ hμ _ _ _ (fun f2 => IntS_smul K μ hμ _ (fun _ => pwR K lam Rr) _ (indep_const _ _) f2)]
    rw [IntS_ext K μ hμ _ _ _ (fun f2 => by rw [rect_W hμ hμ1 hr hom hone lam hschur hsel1 T (r0 + 1) f2])]
    rw [IntS_const K μ hμ hμ1 _ _ (indep_const _ _) f]
    rw [Nat.mul_succ, GKS.pwR_add, ← K.mul_assoc, K.mul_comm (pwR K lam Rr), K.mul_assoc, K.mul_comm (pwR K lam Rr)]

/-- **Partition function of the R × T rectangle.** -/
theorem rect_Z (hμ : Lin K μ) (hμ1 : μ (fun _ => K.one) = K.one)
    (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f) (z : R) (hz : μ w = z) :
    ∀ (T r0 : Nat) (f : S → G),
      IntS μ (sitesR Rr r0 T) (fun f' => plaqProd K Gp Rr w (Hf f') (Vf f') r0 T) f = pwR K z (Rr * T)
  | 0, r0, f => IntS_const K μ hμ hμ1 _ _ (indep_const _ _) f
  | T + 1, r0, f => by
    show IntS μ (vsl r0 0 (Rr + 1) ++ (sitesR Rr (r0 + 1) T ++ hs r0 0 Rr)) _ f = _
    rw [IntS_app K μ hμ, IntS_ext K μ hμ _ _ _ (fun f2 => IntS_app K μ hμ _ _ _ f2)]
    rw [IntS_ext K μ hμ _ _ _ (fun f2 => IntS_ext K μ hμ _ _ _ (fun f3 =>
      row_stepZ K Gp Rr w μ hμ hr z hz r0 T f3) f2)]
    rw [IntS_ext K μ hμ _ _ _ (fun f2 => IntS_smul K μ hμ _ (fun _ => pwR K z Rr) _ (indep_const _ _) f2)]
    rw [IntS_ext K μ hμ _ _ _ (fun f2 => by rw [rect_Z hμ hμ1 hr z hz T (r0 + 1) f2])]
    rw [IntS_const K μ hμ hμ1 _ _ (indep_const _ _) f]
    rw [Nat.mul_succ, GKS.pwR_add, K.mul_comm]

end

end RectInt

#print axioms RectInt.nodup_hs
#print axioms RectInt.plaqProd_congr
#print axioms RectInt.row_step
#print axioms RectInt.rect_W
#print axioms RectInt.rect_Z

ym/SchurAvg.lean

/-
  ym/SchurAvg — T45, cycle 74 (Lad3 step 15, any compact group): Schur averages from invariance and irreducibility.

  μ linear, left and right invariant (Haar); w a class function (w(x y x⁻¹) = w(y)); ρ a representation on idx.
    commute         M = μ(w ρ) satisfies ρ(h) M = M ρ(h) for all h
    schur_avg       if every matrix commuting with all ρ(h) is scalar (absolute irreducibility), then
                    μ(w ρ_ik) = λ δ_ik with d λ = μ(w χ_ρ),   χ_ρ = tr ρ
  So the hypothesis hschur of Strip2D/RectInt holds for every irreducible ρ, with λ = (1/d) μ(w χ_ρ): the
  character-expansion coefficient of the weight. For the heat-kernel weight λ_j/z = e^{−t C_j}.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.RectInt

namespace SchurAvg

open YMChain LinkIntegral Strip2D

variable {R : Type} (K : DetChain.CRing R) {G : Type} (Gp : Grp G) {ι : Type} [DecidableEq ι]

theorem commute (μ : (G → R) → R) (hμ : Lin K μ)
    (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
    (ρ : G → ι → ι → R) (idx : List ι)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (w : G → R) (hw : ∀ x y, w (Gp.mul (Gp.mul x y) (Gp.inv x)) = w y) (h : G) (i j : ι) :
    sumK K (fun k => K.mul (ρ h i k) (μ (fun g => K.mul (w g) (ρ g k j)))) idx
      = sumK K (fun k => K.mul (μ (fun g => K.mul (w g) (ρ g i k))) (ρ h k j)) idx := by
  -- left side = μ(w(g) ρ(hg)_ij), right side = μ(w(g) ρ(gh)_ij)
  have eL : sumK K (fun k => K.mul (ρ h i k) (μ (fun g => K.mul (w g) (ρ g k j)))) idx
      = μ (fun g => K.mul (w g) (ρ (Gp.mul h g) i j)) := by
    rw [sumK_ext K _ _ (fun k => (hμ.smul _ _).symm), ← mu_sum K μ hμ _ idx]
    refine hμ.ext _ _ (fun g => ?_)
    rw [hom, mul_sumK]
    exact sumK_ext K _ _ (fun k => by rw [← K.mul_assoc, K.mul_comm (ρ h i k), K.mul_assoc]) idx
  have eR : sumK K (fun k => K.mul (μ (fun g => K.mul (w g) (ρ g i k))) (ρ h k j)) idx
      = μ (fun g => K.mul (w g) (ρ (Gp.mul g h) i j)) := by
    rw [sumK_ext K _ _ (fun k => K.mul_comm _ _), sumK_ext K _ _ (fun k => (hμ.smul _ _).symm), ← mu_sum K μ hμ _ idx]
    refine hμ.ext _ _ (fun g => ?_)
    rw [hom, mul_sumK]
    exact sumK_ext K _ _ (fun k => by rw [K.mul_comm (ρ h k j), K.mul_assoc]) idx
  rw [eL, eR]
  -- substitute g ↦ h⁻¹ g on the left and g ↦ g h⁻¹ on the right; both become μ(w(·) ρ(·)) after class invariance
  have l1 := hl (fun g => K.mul (w g) (ρ (Gp.mul h g) i j)) (Gp.inv h)
  have r1 := hr (fun g => K.mul (w g) (ρ (Gp.mul g h) i j)) (Gp.inv h)
  rw [← l1, ← r1]
  refine hμ.ext _ _ (fun g => ?_)
  show K.mul (w (Gp.mul (Gp.inv h) g)) (ρ (Gp.mul h (Gp.mul (Gp.inv h) g)) i j)
    = K.mul (w (Gp.mul g (Gp.inv h))) (ρ (Gp.mul (Gp.mul g (Gp.inv h)) h) i j)
  rw [← Gp.assoc h, Gp.mul_inv, Gp.one_mul, Gp.assoc g, Gp.inv_mul, Gp.mul_one]
  -- w(h⁻¹ g) = w(g h⁻¹): conjugate by h⁻¹
  have := hw (Gp.inv h) (Gp.mul g (Gp.inv h))
  rw [RectGeom.inv_inv' Gp, Gp.assoc (Gp.inv h), Gp.assoc g, Gp.inv_mul, Gp.mul_one] at this
  rw [this]

/-- **Schur averages.** -/
theorem schur_avg (μ : (G → R) → R) (hμ : Lin K μ)
    (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
    (ρ : G → ι → ι → R) (idx : List ι)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (w : G → R) (hw : ∀ x y, w (Gp.mul (Gp.mul x y) (Gp.inv x)) = w y)
    (hirr : ∀ M : ι → ι → R, (∀ h i j, sumK K (fun k => K.mul (ρ h i k) (M k j)) idx
      = sumK K (fun k => K.mul (M i k) (ρ h k j)) idx) → ∃ c, ∀ i j, M i j = K.mul c (delta K i j))
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i) :
    ∃ lam, (∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k)) ∧
      K.mul (sumK K (fun _ => K.one) idx) lam = μ (fun g => K.mul (w g) (sumK K (fun i => ρ g i i) idx)) := by
  obtain ⟨c, hc⟩ := hirr (fun i j => μ (fun g => K.mul (w g) (ρ g i j)))
    (fun h i j => commute K Gp μ hμ hl hr ρ idx hom w hw h i j)
  refine ⟨c, hc, ?_⟩
  rw [hμ.ext _ (fun g => sumK K (fun i => K.mul (w g) (ρ g i i)) idx) (fun g => mul_sumK K _ _ _), mu_sum K μ hμ _ idx,
    sumK_ext K _ _ (fun i => hc i i), sumK_mul]
  exact sumK_ext K _ _ (fun i => by
    show K.mul K.one c = K.mul c (if i = i then K.one else K.zero)
    rw [MaskSum.ip rfl, K.one_mul, K.mul_comm, K.one_mul]) idx

end SchurAvg

#print axioms SchurAvg.commute
#print axioms SchurAvg.schur_avg

ym/CharOrthH.lean

/-
  ym/CharOrthH — T45, cycle 75 (Lad3 step 16, any compact group): orthogonality of matrix coefficients and characters.

  μ linear, normalised, left invariant; ρ on idx, σ on idx' representations (index-sum homomorphisms, ρ(1) = I).
    M b c i j := μ(g ↦ ρ(g)_ib σ(g⁻¹)_cj)
    intertw        Σ_k ρ(h)_ik M b c k j = Σ_k M b c i k σ(h)_kj
    orth_ineq      if the only intertwiner from σ to ρ is 0 (inequivalent irreducibles): μ(χ_ρ · χ_σ ∘ inv) = 0
    trace_M        Σ_i M b c i i = δ_cb          (σ = ρ)
    orth_eq        if σ = ρ has scalar commutant and d = dim ρ is cancellable: μ(χ_ρ · χ_ρ ∘ inv) = 1
  With real characters (χ(g⁻¹) = χ(g), true for SU(2)) a weight w = Σ_j a_j χ_j has μ(w χ_k) = a_k; with SchurAvg,
  λ_k d_k = a_k and z = μ(w) = a_0 — the exact 2D solution in terms of the character coefficients.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.SchurAvg

namespace CharOrthH

open YMChain LinkIntegral Strip2D

variable {R : Type} (K : DetChain.CRing R) {G : Type} (Gp : Grp G)
variable {ι κ : Type} [DecidableEq ι] [DecidableEq κ]

def Mco (μ : (G → R) → R) (ρ : G → ι → ι → R) (σ : G → κ → κ → R) (b : ι) (c : κ) (i : ι) (j : κ) : R :=
  μ (fun g => K.mul (ρ g i b) (σ (Gp.inv g) c j))

theorem inv_mul_inv (h g : G) : Gp.inv (Gp.mul (Gp.inv h) g) = Gp.mul (Gp.inv g) h := by
  rw [RectGeom.inv_mul_rev, RectGeom.inv_inv']

theorem intertw (μ : (G → R) → R) (hμ : Lin K μ) (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (ρ : G → ι → ι → R) (idx : List ι) (σ : G → κ → κ → R) (idx' : List κ)
    (homρ : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (homσ : ∀ g h i j, σ (Gp.mul g h) i j = sumK K (fun k => K.mul (σ g i k) (σ h k j)) idx')
    (b : ι) (c : κ) (h : G) (i : ι) (j : κ) :
    sumK K (fun k => K.mul (ρ h i k) (Mco K Gp μ ρ σ b c k j)) idx
      = sumK K (fun k => K.mul (Mco K Gp μ ρ σ b c i k) (σ h k j)) idx' := by
  unfold Mco
  rw [sumK_ext K _ _ (fun k => (hμ.smul _ _).symm), ← mu_sum K μ hμ _ idx,
    sumK_ext K _ _ (fun k => K.mul_comm (μ _) (σ h k j)), sumK_ext K _ _ (fun k => (hμ.smul _ _).symm), ← mu_sum K μ hμ _ idx']
  -- left: μ(ρ(hg)_ib σ(g⁻¹)_cj);  substitute g ↦ h⁻¹ g
  rw [← hl _ (Gp.inv h)]
  refine hμ.ext _ _ (fun g => ?_)
  show sumK K (fun k => K.mul (ρ h i k) (K.mul (ρ (Gp.mul (Gp.inv h) g) k b) (σ (Gp.inv (Gp.mul (Gp.inv h) g)) c j))) idx
    = sumK K (fun k => K.mul (σ h k j) (K.mul (ρ g i b) (σ (Gp.inv g) c k))) idx'
  rw [inv_mul_inv Gp, homσ]
  have e : sumK K (fun k => K.mul (ρ h i k) (K.mul (ρ (Gp.mul (Gp.inv h) g) k b)
      (sumK K (fun k' => K.mul (σ (Gp.inv g) c k') (σ h k' j)) idx'))) idx
      = K.mul (ρ (Gp.mul h (Gp.mul (Gp.inv h) g)) i b) (sumK K (fun k' => K.mul (σ (Gp.inv g) c k') (σ h k' j)) idx') := by
    rw [homρ h, sumK_mul]
    exact sumK_ext K _ _ (fun k => by rw [K.mul_assoc]) idx
  rw [e, ← Gp.assoc, Gp.mul_inv, Gp.one_mul, mul_sumK]
  exact sumK_ext K _ _ (fun k => by rw [← K.mul_assoc, K.mul_comm (σ h k j)]) idx'

/-- **Inequivalent irreducibles: characters are orthogonal.** -/
theorem orth_ineq (μ : (G → R) → R) (hμ : Lin K μ) (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (ρ : G → ι → ι → R) (idx : List ι) (σ : G → κ → κ → R) (idx' : List κ)
    (homρ : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (homσ : ∀ g h i j, σ (Gp.mul g h) i j = sumK K (fun k => K.mul (σ g i k) (σ h k j)) idx')
    (hinq : ∀ M : ι → κ → R, (∀ h i j, sumK K (fun k => K.mul (ρ h i k) (M k j)) idx
      = sumK K (fun k => K.mul (M i k) (σ h k j)) idx') → ∀ i j, M i j = K.zero) :
    μ (fun g => K.mul (sumK K (fun i => ρ g i i) idx) (sumK K (fun c => σ (Gp.inv g) c c) idx')) = K.zero := by
  have hz : ∀ b c, ∀ i j, Mco K Gp μ ρ σ b c i j = K.zero := fun b c =>
    hinq _ (fun h i j => intertw K Gp μ hμ hl ρ idx σ idx' homρ homσ b c h i j)
  rw [hμ.ext _ (fun g => sumK K (fun i => sumK K (fun c => K.mul (ρ g i i) (σ (Gp.inv g) c c)) idx') idx)
    (fun g => by rw [sumK_mul]; exact sumK_ext K _ _ (fun i => mul_sumK K _ _ _) idx),
    mu_sum K μ hμ _ idx]
  refine (sumK_ext K _ _ (fun i => ?_) idx).trans (MaskSum.sum_zero K _)
  rw [mu_sum K μ hμ _ idx']
  exact (sumK_ext K _ _ (fun c => hz i c i c) idx').trans (MaskSum.sum_zero K _)

/-- **Trace of the averaged matrix unit** (σ = ρ). -/
theorem trace_M (μ : (G → R) → R) (hμ : Lin K μ) (hμ1 : μ (fun _ => K.one) = K.one)
    (ρ : G → ι → ι → R) (idx : List ι)
    (homρ : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j) (b c : ι) :
    sumK K (fun i => Mco K Gp μ ρ ρ b c i i) idx = delta K c b := by
  unfold Mco
  rw [← mu_sum K μ hμ _ idx]
  rw [hμ.ext _ (fun _ => delta K c b) (fun g => by
    rw [← hone c b, ← Gp.inv_mul g, homρ]
    exact sumK_ext K _ _ (fun i => K.mul_comm _ _) idx)]
  exact Sites.mu_const K μ hμ hμ1 _

/-- **Normalisation of an irreducible character.** -/
theorem orth_eq (O : ORing K) (μ : (G → R) → R) (hμ : Lin K μ) (hμ1 : μ (fun _ => K.one) = K.one)
    (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (ρ : G → ι → ι → R) (idx : List ι)
    (homρ : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j)
    (hirr : ∀ M : ι → ι → R, (∀ h i j, sumK K (fun k => K.mul (ρ h i k) (M k j)) idx
      = sumK K (fun k => K.mul (M i k) (ρ h k j)) idx) → ∃ c, ∀ i j, M i j = K.mul c (delta K i j))
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i)
    (hd0 : O.le K.zero (sumK K (fun _ => K.one) idx)) (hdne : sumK K (fun _ => K.one) idx ≠ K.zero)
    (hdiag : ∀ i : ι, delta K i i = K.one) :
    μ (fun g => K.mul (sumK K (fun i => ρ g i i) idx) (sumK K (fun b => ρ (Gp.inv g) b b) idx)) = K.one := by
  let d := sumK K (fun _ => K.one) idx
  have key : ∀ b c i j, K.mul d (Mco K Gp μ ρ ρ b c i j) = K.mul (delta K c b) (delta K i j) := by
    intro b c i j
    obtain ⟨s, hs⟩ := hirr _ (fun h i j => intertw K Gp μ hμ hl ρ idx ρ idx homρ homρ b c h i j)
    have tr := trace_M K Gp μ hμ hμ1 ρ idx homρ hone b c
    rw [sumK_ext K _ _ (fun i => by rw [hs i i, hdiag]) idx, ← mul_sumK] at tr
    rw [hs, ← K.mul_assoc, K.mul_comm d s, tr]
  have e1 : μ (fun g => K.mul (sumK K (fun i => ρ g i i) idx) (sumK K (fun b => ρ (Gp.inv g) b b) idx))
      = sumK K (fun i => sumK K (fun b => Mco K Gp μ ρ ρ i b i b) idx) idx := by
    rw [hμ.ext _ (fun g => sumK K (fun i => sumK K (fun b => K.mul (ρ g i i) (ρ (Gp.inv g) b b)) idx) idx)
      (fun g => by rw [sumK_mul]; exact sumK_ext K _ _ (fun i => mul_sumK K _ _ _) idx), mu_sum K μ hμ _ idx]
    exact sumK_ext K _ _ (fun i => mu_sum K μ hμ _ idx) idx
  rw [e1]
  let X := sumK K (fun i => sumK K (fun b => Mco K Gp μ ρ ρ i b i b) idx) idx
  have hX : K.mul d X = d := by
    show K.mul d (sumK K _ idx) = d
    rw [mul_sumK]
    refine (sumK_ext K _ _ (fun i => ?_) idx)
    rw [mul_sumK, sumK_ext K _ _ (fun b => key i b i b)]
    -- Σ_b δ_bi δ_ib = δ_ii = 1
    rw [sumK_ext K _ _ (fun b => K.mul_comm (delta K b i) (delta K i b))]
    have := hsel1 i (fun b => delta K b i)
    rw [this, hdiag]
  -- cancel d
  have a1 : O.le (K.mul X d) (K.mul K.one d) := by rw [K.mul_comm X, hX, K.one_mul]; exact O.le_refl _
  have a2 : O.le (K.mul K.one d) (K.mul X d) := by rw [K.mul_comm X, hX, K.one_mul]; exact O.le_refl _
  exact O.le_antisymm _ _ (O.pos_cancel _ _ _ hd0 hdne a1) (O.pos_cancel _ _ _ hd0 hdne a2)

end CharOrthH

#print axioms CharOrthH.intertw
#print axioms CharOrthH.orth_ineq
#print axioms CharOrthH.trace_M
#print axioms CharOrthH.orth_eq

ym/Lad3.lean

/-
  ym/Lad3 — T45, cycle 76 (Lad3 closing statement): exact 2D gauge theory for any compact group.

  For a left- and right-invariant normalised linear one-link integral μ (Haar), a class-function plaquette weight w,
  and a representation ρ with scalar commutant (irreducible), on every R × T rectangle of the 2D lattice:
    rect_char     ∃ λ, d λ = μ(w χ_ρ) and
                  IntS(Π w(U_p) · tr ρ(W_{R×T})) = d λ^{RT},   IntS(Π w(U_p)) = μ(w)^{RT}
  i.e. ⟨W_{R×T}⟩ = d (λ/z)^{RT} with λ/z = μ(w χ_ρ)/(d μ(w)) — the character-expansion solution, area law with
  string tension −ln(λ/z). With the heat kernel (λ/z = e^{−C t}, Heat2D) the continuum limit is exact.
  For SU(2) the only inputs not in Lean are the Haar integral (H0) and the heat-kernel character coefficients.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.CharOrthH

namespace Lad3

open YMChain LinkIntegral Strip2D Sites RectInt SchurAvg

variable {R : Type} (K : DetChain.CRing R) {G : Type} (Gp : Grp G) {ι : Type} [DecidableEq ι]

theorem rect_char (Rr : Nat) (w : G → R) (ρ : G → ι → ι → R) (idx : List ι)
    (μ : (G → R) → R) (hμ : Lin K μ) (hμ1 : μ (fun _ => K.one) = K.one)
    (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j)
    (hw : ∀ x y, w (Gp.mul (Gp.mul x y) (Gp.inv x)) = w y)
    (hirr : ∀ M : ι → ι → R, (∀ h i j, sumK K (fun k => K.mul (ρ h i k) (M k j)) idx
      = sumK K (fun k => K.mul (M i k) (ρ h k j)) idx) → ∃ c, ∀ i j, M i j = K.mul c (delta K i j))
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i) :
    ∃ lam, K.mul (sumK K (fun _ => K.one) idx) lam = μ (fun g => K.mul (w g) (sumK K (fun i => ρ g i i) idx)) ∧
      ∀ (T r0 : Nat) (f : S → G),
        IntS μ (sitesR Rr r0 T) (fun f' => K.mul (plaqProd K Gp Rr w (Hf f') (Vf f') r0 T) (trW K Gp Rr ρ idx f' r0 T)) f
          = K.mul (sumK K (fun _ => K.one) idx) (pwR K lam (Rr * T)) ∧
        IntS μ (sitesR Rr r0 T) (fun f' => plaqProd K Gp Rr w (Hf f') (Vf f') r0 T) f = pwR K (μ w) (Rr * T) := by
  obtain ⟨lam, hs, htr⟩ := schur_avg K Gp μ hμ hl hr ρ idx hom w hw hirr hsel1
  exact ⟨lam, htr, fun T r0 f => ⟨rect_W K Gp Rr w ρ idx μ hμ hμ1 hr hom hone lam hs hsel1 T r0 f,
    rect_Z K Gp Rr w μ hμ hμ1 hr (μ w) rfl T r0 f⟩⟩

end Lad3

#print axioms Lad3.rect_char

ym/SU2Haar.lean

/-
  ym/SU2Haar — T45, S1: the SU(2) Haar integral on class functions, algebraically.

  A class function of U ∈ SU(2) is a function of y = tr U ∈ [−2, 2]. Under Haar measure y follows the semicircle law;
  its moments are counted by walks on ℕ:
    P m s t        number of walks of m steps ±1 from s to t that stay ≥ 0 (first-step recursion)
    last_step      the last-step recursion P (m+1) s (t+1) = P m s t + P m s (t+2), P (m+1) s 0 = P m s 1
    P_symm         P m s t = P m t s
    mom m := P m 0 0  (the Haar moment ∫ (tr U)^m dU)
    first_return   P (m+1) 0 (n+1) = Σ_{a+b=m} mom a · P b 0 n
    catalan        mom 0 = 1, mom 1 = 0, mom (m+2) = Σ_{a+b=m} mom a · mom b   (so mom (2k) = C_k, mom (2k+1) = 0;
                   in terms of tr U/2 the moments are C_k/4^k: the semicircle law)
    odd_zero       mom (2k+1) = 0
  The integral of a polynomial class function p(y) = Σ_i p_i y^i (coefficient list over any commutative ring) is
    I p = Σ_i p_i · mom i      (Im m p = Σ_i p_i · mom (m+i) = I(y^m p))
    I_add, I_smul  linearity;  I_one  normalisation I 1 = 1;  Im_shift  I(y · p) = Im 1 p.
  Positivity and invariance (I χ_n = δ_{n0}, orthogonality) are in ym/SU2Char (S2).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Decrement
import lib.RingTac
import lib.Semi

namespace SU2Haar

open DetChain

/-- Kronecker delta on ℕ. -/
def dl : Nat → Nat → Nat
  | 0, 0 => 1
  | 0, _ + 1 => 0
  | _ + 1, 0 => 0
  | s + 1, t + 1 => dl s t

/-- Walks of `m` steps ±1 from `s` to `t` staying in ℕ. -/
def P : Nat → Nat → Nat → Nat
  | 0, s, t => dl s t
  | m + 1, 0, t => P m 1 t
  | m + 1, s + 1, t => P m s t + P m (s + 2) t

theorem dl_symm : ∀ s t, dl s t = dl t s
  | 0, 0 => rfl
  | 0, _ + 1 => rfl
  | _ + 1, 0 => rfl
  | s + 1, t + 1 => dl_symm s t

/-- **Last-step recursion.** -/
theorem last_step : ∀ m s, (∀ t, P (m + 1) s (t + 1) = P m s t + P m s (t + 2)) ∧ P (m + 1) s 0 = P m s 1
  | 0, 0 => ⟨fun t => by
      show dl 1 (t + 1) = dl 0 t + dl 0 (t + 2)
      show dl 0 t = dl 0 t + 0; rfl, rfl⟩
  | 0, k + 1 => ⟨fun t => by
      show dl k (t + 1) + dl (k + 2) (t + 1) = dl (k + 1) t + dl (k + 1) (t + 2)
      show dl k (t + 1) + dl (k + 1) t = dl (k + 1) t + dl k (t + 1)
      exact Nat.add_comm _ _, by
      show dl k 0 + dl (k + 2) 0 = dl (k + 1) 1
      show dl k 0 + 0 = dl k 0; rfl⟩
  | m + 1, 0 => by
    have ih := last_step m 1
    exact ⟨fun t => ih.1 t, ih.2⟩
  | m + 1, k + 1 => by
    have ia := last_step m k
    have ib := last_step m (k + 2)
    refine ⟨fun t => ?_, ?_⟩
    · show P (m + 1) k (t + 1) + P (m + 1) (k + 2) (t + 1)
        = (P m k t + P m (k + 2) t) + (P m k (t + 2) + P m (k + 2) (t + 2))
      rw [ia.1 t, ib.1 t]
      lib_nat
    · show P (m + 1) k 0 + P (m + 1) (k + 2) 0 = P m k 1 + P m (k + 2) 1
      rw [ia.2, ib.2]

/-- **Walk reversal.** -/
theorem P_symm : ∀ m s t, P m s t = P m t s
  | 0, s, t => dl_symm s t
  | m + 1, 0, t => by
    show P m 1 t = P (m + 1) t 0
    rw [(last_step m t).2, P_symm m 1 t]
  | m + 1, k + 1, t => by
    show P m k t + P m (k + 2) t = P (m + 1) t (k + 1)
    rw [(last_step m t).1 k, P_symm m k t, P_symm m (k + 2) t]

/-- Haar moments of tr U. -/
def mom (m : Nat) : Nat := P m 0 0

/-- splittings m = a + b, as pairs (a, b). -/
def spl : Nat → List (Nat × Nat)
  | 0 => [(0, 0)]
  | m + 1 => (m + 1, 0) :: (spl m).map (fun p => (p.1, p.2 + 1))

def sN (f : Nat × Nat → Nat) : List (Nat × Nat) → Nat
  | [] => 0
  | p :: l => f p + sN f l

theorem sN_map (f : Nat × Nat → Nat) (g : Nat × Nat → Nat × Nat) : ∀ l, sN f (l.map g) = sN (fun p => f (g p)) l
  | [] => rfl
  | p :: l => by show f (g p) + sN f (l.map g) = _; rw [sN_map f g l]; rfl

theorem sN_add (f g : Nat × Nat → Nat) : ∀ l, sN (fun p => f p + g p) l = sN f l + sN g l
  | [] => rfl
  | p :: l => by
    show f p + g p + sN (fun p => f p + g p) l = (f p + sN f l) + (g p + sN g l)
    rw [sN_add f g l]; lib_nat

theorem sN_ext (f g : Nat × Nat → Nat) (h : ∀ p, f p = g p) : ∀ l, sN f l = sN g l
  | [] => rfl
  | p :: l => by show f p + sN f l = g p + sN g l; rw [h p, sN_ext f g h l]

/-- **First-return decomposition.** -/
theorem first_return : ∀ m n, P (m + 1) 0 (n + 1) = sN (fun p => mom p.1 * P p.2 0 n) (spl m)
  | 0, n => by
    show dl 1 (n + 1) = mom 0 * P 0 0 n + 0
    show dl 0 n = 1 * dl 0 n + 0
    rw [Nat.one_mul]; rfl
  | m + 1, n => by
    rw [(last_step (m + 1) 0).1 n]
    show _ = mom (m + 1) * P 0 0 n + sN (fun p => mom p.1 * P p.2 0 n) ((spl m).map (fun p => (p.1, p.2 + 1)))
    rw [sN_map]
    cases n with
    | zero =>
      show P (m + 1) 0 0 + P (m + 1) 0 2 = mom (m + 1) * 1 + sN (fun p => mom p.1 * P (p.2 + 1) 0 0) (spl m)
      rw [sN_ext _ (fun p => mom p.1 * P p.2 0 1) (fun p => by rw [(last_step p.2 0).2]) (spl m),
        ← first_return m 1, Nat.mul_one]
      rfl
    | succ k =>
      show P (m + 1) 0 (k + 1) + P (m + 1) 0 (k + 3) = mom (m + 1) * 0 + sN (fun p => mom p.1 * P (p.2 + 1) 0 (k + 1)) (spl m)
      rw [sN_ext _ (fun p => mom p.1 * P p.2 0 k + mom p.1 * P p.2 0 (k + 2))
          (fun p => by rw [(last_step p.2 0).1 k, Nat.mul_add]) (spl m),
        sN_add, ← first_return m k, ← first_return m (k + 2), Nat.mul_zero, Nat.zero_add]

/-- **The Haar moments of tr U satisfy the Catalan recursion.** -/
theorem catalan : mom 0 = 1 ∧ mom 1 = 0 ∧ ∀ m, mom (m + 2) = sN (fun p => mom p.1 * mom p.2) (spl m) :=
  ⟨rfl, rfl, fun m => by
    show P (m + 2) 0 0 = _
    rw [(last_step (m + 1) 0).2, first_return m 0]
    rfl⟩

theorem mom_values : mom 2 = 1 ∧ mom 4 = 2 ∧ mom 6 = 5 ∧ mom 8 = 14 ∧ mom 3 = 0 := by decide

/-- parity of walks -/
theorem P_parity : ∀ m s t, StrongZ2.parity (m + s + t) = false → P m s t = 0
  | 0, s, t, h => by
    show dl s t = 0
    revert h
    induction s generalizing t with
    | zero => cases t with
      | zero => intro h; exact Bool.noConfusion h
      | succ t => intro _; rfl
    | succ s ih => cases t with
      | zero => intro _; rfl
      | succ t =>
        intro h
        apply ih t
        have e : 0 + (s + 1) + (t + 1) = 0 + s + t + 1 + 1 := by lib_nat
        rw [e] at h
        show StrongZ2.parity (0 + s + t) = false
        have h' : (!(!(StrongZ2.parity (0 + s + t)))) = false := h
        cases e2 : StrongZ2.parity (0 + s + t) <;> rw [e2] at h' <;> first | rfl | exact h'
  | m + 1, 0, t, h => by
    show P m 1 t = 0
    apply P_parity m 1 t
    have e : m + 1 + 0 + t = m + 1 + t := by lib_nat
    rw [e] at h; exact h
  | m + 1, k + 1, t, h => by
    show P m k t + P m (k + 2) t = 0
    have e1 : m + 1 + (k + 1) + t = m + k + t + 1 + 1 := by lib_nat
    have e2 : m + (k + 2) + t = m + k + t + 1 + 1 := by lib_nat
    have hp : StrongZ2.parity (m + k + t) = false := by
      rw [e1] at h
      have h' : (!(!(StrongZ2.parity (m + k + t)))) = false := h
      cases e3 : StrongZ2.parity (m + k + t) <;> rw [e3] at h' <;> first | rfl | exact h'
    rw [P_parity m k t hp, P_parity m (k + 2) t (by rw [e2]; exact (by
      show (!(!(StrongZ2.parity (m + k + t)))) = false
      rw [hp]; rfl))]

theorem parity_double_succ (k : Nat) : StrongZ2.parity (2 * k + 1) = false := by
  induction k with
  | zero => rfl
  | succ k ih =>
    have e : 2 * (k + 1) + 1 = 2 * k + 1 + 1 + 1 := by lib_nat
    rw [e]
    show (!(!(StrongZ2.parity (2 * k + 1)))) = false
    rw [ih]; rfl

/-- **Odd moments vanish.** -/
theorem odd_zero (k : Nat) : mom (2 * k + 1) = 0 :=
  P_parity (2 * k + 1) 0 0 (by
    have e : 2 * k + 1 + 0 + 0 = 2 * k + 1 := by lib_nat
    rw [e]; exact parity_double_succ k)

/-! ## The integral on polynomial class functions -/

section Integral
variable {R : Type} (K : CRing R)

/-- `Im m p = Σ_i p_i · mom (m + i) = I(y^m · p)` -/
def Im : Nat → List R → R
  | _, [] => K.zero
  | m, c :: p => K.add (K.mul c (LibRing.castN K (mom m))) (Im (m + 1) p)

/-- the Haar integral of the class function y ↦ Σ_i p_i y^i -/
def I (p : List R) : R := Im K 0 p

def addL : List R → List R → List R
  | [], q => q
  | a :: p, [] => a :: p
  | a :: p, b :: q => K.add a b :: addL p q

def smulL (c : R) (p : List R) : List R := p.map (K.mul c)

theorem Im_add : ∀ (m : Nat) (p q : List R), Im K m (addL K p q) = K.add (Im K m p) (Im K m q)
  | _, [], q => by show Im K _ q = K.add K.zero _; rw [K.zero_add]
  | _, a :: p, [] => by show Im K _ (a :: p) = K.add _ K.zero; rw [add_zero']
  | m, a :: p, b :: q => by
    show K.add (K.mul (K.add a b) _) (Im K (m + 1) (addL K p q)) = K.add (K.add (K.mul a _) (Im K (m + 1) p))
      (K.add (K.mul b _) (Im K (m + 1) q))
    rw [Im_add (m + 1) p q]
    lib_ring

theorem Im_smul (c : R) : ∀ (m : Nat) (p : List R), Im K m (smulL K c p) = K.mul c (Im K m p)
  | _, [] => by show K.zero = K.mul c K.zero; rw [mul_zero]
  | m, a :: p => by
    show K.add (K.mul (K.mul c a) _) (Im K (m + 1) (smulL K c p)) = K.mul c (K.add (K.mul a _) (Im K (m + 1) p))
    rw [Im_smul c (m + 1) p]
    lib_ring

/-- multiplication by y shifts the moment index -/
theorem Im_shift (m : Nat) (p : List R) : Im K m (K.zero :: p) = Im K (m + 1) p := by
  show K.add (K.mul K.zero _) _ = _
  rw [K.mul_comm, mul_zero, K.zero_add]

/-- **Linearity.** -/
theorem I_add (p q : List R) : I K (addL K p q) = K.add (I K p) (I K q) := Im_add K 0 p q
theorem I_smul (c : R) (p : List R) : I K (smulL K c p) = K.mul c (I K p) := Im_smul K c 0 p

/-- **Normalisation.** -/
theorem I_one : I K [K.one] = K.one := by
  show K.add (K.mul K.one (K.add K.one K.zero)) K.zero = K.one
  rw [add_zero', K.one_mul, add_zero']

end Integral

end SU2Haar

#print axioms SU2Haar.last_step
#print axioms SU2Haar.P_symm
#print axioms SU2Haar.first_return
#print axioms SU2Haar.catalan
#print axioms SU2Haar.mom_values
#print axioms SU2Haar.odd_zero
#print axioms SU2Haar.I_add
#print axioms SU2Haar.I_one

ym/SU2Char.lean

/-
  ym/SU2Char — T45, S2: SU(2) characters χ_n = U_n (Chebyshev, second kind) and their orthonormality under the
  algebraic Haar integral of S1.

  As polynomials in y = tr U (coefficient lists over any commutative ring):
    chiC 0 = 1, chiC 1 = y, chiC (n+2) = y·chiC (n+1) − chiC n        (χ_n(U) = U_n(tr U/2) = sin((n+1)θ)/sin θ)
    chi_rec        ev (χ_{n+2}) y + ev (χ_n) y = y · ev (χ_{n+1}) y      (χ_1 χ_{n+1} = χ_{n+2} + χ_n)
    walk_chi       Σ_i (χ_k)_i · P(m+i, s, 0) = P(m, s, k)            (the characters diagonalise the walk)
    invariance     I χ_n = δ_{n0}                                       (Haar invariance on class functions)
    orthogonality  I(χ_k χ_n) = δ_{kn}
    positivity     I(p · p) = Σ_k a_k² ≥ 0 for every p = Σ_k a_k χ_k
  All from the Catalan moments of S1; no analysis.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.SU2Haar

namespace SU2Char

open DetChain SU2Haar

variable {R : Type} (K : CRing R)

/-- `dot m p f = Σ_i p_i f(m+i)` -/
def dot : Nat → List R → (Nat → R) → R
  | _, [], _ => K.zero
  | m, c :: p, f => K.add (K.mul c (f m)) (dot (m + 1) p f)

def chiC : Nat → List R
  | 0 => [K.one]
  | 1 => [K.zero, K.one]
  | n + 2 => addL K (K.zero :: chiC (n + 1)) (smulL K (K.neg K.one) (chiC n))

def mulL : List R → List R → List R
  | [], _ => []
  | a :: p, q => addL K (smulL K a q) (K.zero :: mulL p q)

def ev : List R → R → R
  | [], _ => K.zero
  | c :: p, y => K.add c (K.mul y (ev p y))

def cN (n : Nat) : R := LibRing.castN K n

theorem cN_add (a : Nat) : ∀ b, cN K (a + b) = K.add (cN K a) (cN K b)
  | 0 => by show cN K a = K.add (cN K a) K.zero; rw [add_zero']
  | b + 1 => by
    show K.add K.one (cN K (a + b)) = K.add (cN K a) (K.add K.one (cN K b))
    rw [cN_add a b]; lib_ring

/-! ### evaluation and the recurrence -/

theorem ev_addL (y : R) : ∀ p q : List R, ev K (addL K p q) y = K.add (ev K p y) (ev K q y)
  | [], q => by show ev K q y = K.add K.zero _; rw [K.zero_add]
  | a :: p, [] => by show ev K (a :: p) y = K.add _ K.zero; rw [add_zero']
  | a :: p, b :: q => by
    show K.add (K.add a b) (K.mul y (ev K (addL K p q) y)) = K.add (K.add a (K.mul y (ev K p y))) (K.add b (K.mul y (ev K q y)))
    rw [ev_addL y p q]; lib_ring

theorem ev_smulL (c y : R) : ∀ p : List R, ev K (smulL K c p) y = K.mul c (ev K p y)
  | [] => by show K.zero = K.mul c K.zero; rw [mul_zero]
  | a :: p => by
    show K.add (K.mul c a) (K.mul y (ev K (smulL K c p) y)) = K.mul c (K.add a (K.mul y (ev K p y)))
    rw [ev_smulL c y p]; lib_ring

/-- **Character recurrence** χ_1 χ_{n+1} = χ_{n+2} + χ_n. -/
theorem chi_rec (y : R) (n : Nat) :
    K.add (ev K (chiC K (n + 2)) y) (ev K (chiC K n) y) = K.mul y (ev K (chiC K (n + 1)) y) := by
  show K.add (ev K (addL K (K.zero :: chiC K (n + 1)) (smulL K (K.neg K.one) (chiC K n))) y) _ = _
  rw [ev_addL, ev_smulL]
  show K.add (K.add (K.add K.zero (K.mul y (ev K (chiC K (n + 1)) y))) (K.mul (K.neg K.one) (ev K (chiC K n) y)))
    (ev K (chiC K n) y) = _
  lib_ring

/-! ### the functional `dot` -/

theorem dot_addL (f : Nat → R) : ∀ (m : Nat) (p q : List R), dot K m (addL K p q) f = K.add (dot K m p f) (dot K m q f)
  | _, [], q => by show dot K _ q f = K.add K.zero _; rw [K.zero_add]
  | _, a :: p, [] => by show dot K _ (a :: p) f = K.add _ K.zero; rw [add_zero']
  | m, a :: p, b :: q => by
    show K.add (K.mul (K.add a b) (f m)) (dot K (m + 1) (addL K p q) f)
      = K.add (K.add (K.mul a (f m)) (dot K (m + 1) p f)) (K.add (K.mul b (f m)) (dot K (m + 1) q f))
    rw [dot_addL f (m + 1) p q]; lib_ring

theorem dot_smulL (f : Nat → R) (c : R) : ∀ (m : Nat) (p : List R), dot K m (smulL K c p) f = K.mul c (dot K m p f)
  | _, [] => by show K.zero = K.mul c K.zero; rw [mul_zero]
  | m, a :: p => by
    show K.add (K.mul (K.mul c a) (f m)) (dot K (m + 1) (smulL K c p) f) = K.mul c (K.add (K.mul a (f m)) (dot K (m + 1) p f))
    rw [dot_smulL f c (m + 1) p]; lib_ring

theorem dot_shift (f : Nat → R) (m : Nat) (p : List R) : dot K m (K.zero :: p) f = dot K (m + 1) p f := by
  show K.add (K.mul K.zero (f m)) _ = _
  rw [K.mul_comm, mul_zero, K.zero_add]

theorem dot_ext_ge (f g : Nat → R) : ∀ (m : Nat) (p : List R), (∀ j, m ≤ j → f j = g j) → dot K m p f = dot K m p g
  | _, [], _ => rfl
  | m, c :: p, h => by
    show K.add (K.mul c (f m)) (dot K (m + 1) p f) = K.add (K.mul c (g m)) (dot K (m + 1) p g)
    rw [h m (Nat.le_refl m), dot_ext_ge f g (m + 1) p (fun j hj => h j (Nat.le_of_succ_le hj))]

theorem dot_ext (f g : Nat → R) (h : ∀ j, f j = g j) (m : Nat) (p : List R) : dot K m p f = dot K m p g :=
  dot_ext_ge K f g m p (fun j _ => h j)

theorem dot_fadd (f g : Nat → R) : ∀ (m : Nat) (p : List R),
    dot K m p (fun j => K.add (f j) (g j)) = K.add (dot K m p f) (dot K m p g)
  | _, [] => by show K.zero = K.add K.zero K.zero; rw [K.zero_add]
  | m, c :: p => by
    show K.add (K.mul c (K.add (f m) (g m))) (dot K (m + 1) p _)
      = K.add (K.add (K.mul c (f m)) (dot K (m + 1) p f)) (K.add (K.mul c (g m)) (dot K (m + 1) p g))
    rw [dot_fadd f g (m + 1) p]; lib_ring

theorem dot_fsmul (f : Nat → R) (c : R) : ∀ (m : Nat) (p : List R),
    dot K m p (fun j => K.mul c (f j)) = K.mul c (dot K m p f)
  | _, [] => by show K.zero = K.mul c K.zero; rw [mul_zero]
  | m, a :: p => by
    show K.add (K.mul a (K.mul c (f m))) (dot K (m + 1) p _) = K.mul c (K.add (K.mul a (f m)) (dot K (m + 1) p f))
    rw [dot_fsmul f c (m + 1) p]; lib_ring

theorem dot_fzero : ∀ (m : Nat) (p : List R), dot K m p (fun _ => K.zero) = K.zero
  | _, [] => rfl
  | m, c :: p => by
    show K.add (K.mul c K.zero) (dot K (m + 1) p _) = K.zero
    rw [dot_fzero (m + 1) p, mul_zero, K.zero_add]

/-- products: `dot m (p q) f = dot m p (i ↦ dot i q f)` -/
theorem dot_mulL (f : Nat → R) (q : List R) : ∀ (m : Nat) (p : List R),
    dot K m (mulL K p q) f = dot K m p (fun i => dot K i q f)
  | _, [] => rfl
  | m, a :: p => by
    show dot K m (addL K (smulL K a q) (K.zero :: mulL K p q)) f = K.add (K.mul a (dot K m q f)) (dot K (m + 1) p _)
    rw [dot_addL, dot_smulL, dot_shift, dot_mulL f q (m + 1) p]

/-- double sums commute -/
theorem dot_swap (g : Nat → Nat → R) (n : Nat) (q : List R) : ∀ (m : Nat) (p : List R),
    dot K m p (fun i => dot K n q (g i)) = dot K n q (fun j => dot K m p (fun i => g i j))
  | m, [] => (dot_fzero K n q).symm
  | m, a :: p => by
    show K.add (K.mul a (dot K n q (g m))) (dot K (m + 1) p _) = _
    rw [dot_swap g n q (m + 1) p, ← dot_fsmul, ← dot_fadd]
    rfl

/-! ### the characters diagonalise the walk -/

/-- **Σ_i (χ_k)_i P(m+i, s, 0) = P(m, s, k).** -/
theorem walk_chi (s : Nat) : ∀ k m, dot K m (chiC K k) (fun i => cN K (P i s 0)) = cN K (P m s k)
  | 0, m => by
    show K.add (K.mul K.one (cN K (P m s 0))) K.zero = _
    rw [add_zero', K.one_mul]
  | 1, m => by
    show K.add (K.mul K.zero _) (K.add (K.mul K.one (cN K (P (m + 1) s 0))) K.zero) = _
    rw [K.mul_comm K.zero, mul_zero, K.zero_add, add_zero', K.one_mul, (last_step m s).2]
  | k + 2, m => by
    show dot K m (addL K (K.zero :: chiC K (k + 1)) (smulL K (K.neg K.one) (chiC K k))) _ = _
    rw [dot_addL, dot_shift, dot_smulL, walk_chi s (k + 1) (m + 1), walk_chi s k m, (last_step m s).1 k, cN_add]
    lib_ring

/-- moments in `dot` form -/
theorem I_dot (p : List R) : I K p = dot K 0 p (fun i => cN K (P i 0 0)) := by
  suffices h : ∀ m (p : List R), Im K m p = dot K m p (fun i => cN K (P i 0 0)) from h 0 p
  intro m p
  induction p generalizing m with
  | nil => rfl
  | cons c p ih => show K.add _ (Im K (m + 1) p) = K.add _ (dot K (m + 1) p _); rw [ih (m + 1)]; rfl

/-- **Invariance:** I χ_n = δ_{n0}. -/
theorem invariance (n : Nat) : I K (chiC K n) = cN K (dl 0 n) := by
  rw [I_dot, walk_chi K 0 n 0]; rfl

theorem I_mul_dot (p q : List R) : I K (mulL K p q) = dot K 0 p (fun i => dot K i q (fun j => cN K (P j 0 0))) := by
  rw [I_dot, dot_mulL]

/-- **Orthogonality:** I(χ_k χ_n) = δ_{kn}. -/
theorem orthogonality (k n : Nat) : I K (mulL K (chiC K k) (chiC K n)) = cN K (dl n k) := by
  rw [I_mul_dot]
  rw [dot_ext K _ (fun i => cN K (P i n 0)) (fun i => by rw [walk_chi K 0 n i, P_symm]) 0 (chiC K k)]
  rw [walk_chi K n k 0]
  rfl

/-! ### positivity -/

/-- `comboK k a = Σ_j a_j χ_{k+j}` -/
def comboK : Nat → List R → List R
  | _, [] => []
  | k, c :: a => addL K (smulL K c (chiC K k)) (comboK (k + 1) a)

theorem dot_combo (f : Nat → R) (m : Nat) : ∀ (k : Nat) (a : List R),
    dot K m (comboK K k a) f = dot K k a (fun j => dot K m (chiC K j) f)
  | _, [] => rfl
  | k, c :: a => by
    show dot K m (addL K (smulL K c (chiC K k)) (comboK K (k + 1) a)) f = K.add (K.mul c _) (dot K (k + 1) a _)
    rw [dot_addL, dot_smulL, dot_combo f m (k + 1) a]

def ssq : List R → R
  | [] => K.zero
  | c :: a => K.add (K.mul c c) (ssq a)

theorem dl_lt : ∀ j i, j < i → dl j i = 0
  | 0, 0, h => absurd h (Nat.lt_irrefl 0)
  | 0, _ + 1, _ => rfl
  | _ + 1, 0, h => absurd h (Nat.not_succ_le_zero _)
  | j + 1, i + 1, h => dl_lt j i (Nat.lt_of_succ_lt_succ h)

theorem dl_gt : ∀ j i, i < j → dl j i = 0
  | j, i, h => by rw [dl_symm]; exact dl_lt i j h

theorem dl_self : ∀ j, dl j j = 1
  | 0 => rfl
  | j + 1 => dl_self j

theorem dot_dl_zero (j : Nat) : ∀ (k : Nat) (a : List R), j < k → dot K k a (fun i => cN K (dl j i)) = K.zero
  | _, [], _ => rfl
  | k, c :: a, h => by
    show K.add (K.mul c (cN K (dl j k))) (dot K (k + 1) a _) = K.zero
    rw [dl_lt j k h, dot_dl_zero j (k + 1) a (Nat.lt_trans h (Nat.lt_succ_self k))]
    show K.add (K.mul c K.zero) K.zero = K.zero
    rw [mul_zero, K.zero_add]

/-- **The diagonal form is a sum of squares.** -/
theorem diag_ssq : ∀ (k : Nat) (a : List R), dot K k a (fun j => dot K k a (fun i => cN K (dl j i))) = ssq K a
  | _, [] => rfl
  | k, c :: a => by
    show K.add (K.mul c (dot K k (c :: a) (fun i => cN K (dl k i)))) (dot K (k + 1) a (fun j => dot K k (c :: a) _))
      = K.add (K.mul c c) (ssq K a)
    have h1 : dot K k (c :: a) (fun i => cN K (dl k i)) = c := by
      show K.add (K.mul c (cN K (dl k k))) (dot K (k + 1) a _) = c
      rw [dl_self, dot_dl_zero K k (k + 1) a (Nat.lt_succ_self k)]
      show K.add (K.mul c (K.add K.one K.zero)) K.zero = c
      rw [add_zero', add_zero', K.mul_comm, K.one_mul]
    have h2 : dot K (k + 1) a (fun j => dot K k (c :: a) (fun i => cN K (dl j i)))
        = dot K (k + 1) a (fun j => dot K (k + 1) a (fun i => cN K (dl j i))) := by
      apply dot_ext_ge
      intro j hj
      show K.add (K.mul c (cN K (dl j k))) _ = _
      rw [dl_gt j k hj]
      show K.add (K.mul c K.zero) _ = _
      rw [mul_zero, K.zero_add]
    rw [h1, h2, diag_ssq (k + 1) a]

/-- **I(p²) = Σ a_k²** for p = Σ_k a_k χ_k. -/
theorem norm_sq (a : List R) : I K (mulL K (comboK K 0 a) (comboK K 0 a)) = ssq K a := by
  rw [I_mul_dot]
  -- inner: dot i A μ0 = Σ_j a_j P(i, j, 0)
  have inner : ∀ i, dot K i (comboK K 0 a) (fun j => cN K (P j 0 0)) = dot K 0 a (fun j => cN K (P i j 0)) := by
    intro i
    rw [dot_combo]
    exact dot_ext K _ _ (fun j => by rw [walk_chi K 0 j i, P_symm]) 0 a
  rw [dot_ext K _ _ inner 0 (comboK K 0 a), dot_swap K (fun i j => cN K (P i j 0)) 0 a 0 (comboK K 0 a)]
  rw [dot_ext K _ (fun j => dot K 0 a (fun i => cN K (dl j i))) (fun j => by
    rw [dot_combo]
    exact dot_ext K _ _ (fun l => by rw [walk_chi K j l 0]; rfl) 0 a) 0 a]
  exact diag_ssq K 0 a

/-- **Positivity of the Haar integral.** -/
theorem positivity (O : YMChain.ORing K) (a : List R) : O.le K.zero (I K (mulL K (comboK K 0 a) (comboK K 0 a))) := by
  rw [norm_sq]
  induction a with
  | nil => exact O.le_refl _
  | cons c a ih =>
    have := O.add_le_add _ _ _ _ (QuatUnit.sq_nn K O c) ih
    rw [K.zero_add] at this; exact this

end SU2Char

#print axioms SU2Char.chi_rec
#print axioms SU2Char.walk_chi
#print axioms SU2Char.invariance
#print axioms SU2Char.orthogonality
#print axioms SU2Char.norm_sq
#print axioms SU2Char.positivity

ym/SU2CharExp.lean

/-
  ym/SU2CharExp — T45, S3: the character expansion of the SU(2) Wilson weight e^{β χ₁/2} and rational bounds on its
  coefficients.

  With t = β/2 and y = χ₁ = tr U: I(e^{t y} χ_n) = Σ_m t^m/m! · I(y^m χ_n) = Σ_m t^m/m! · P(m, 0, n) (S2: walk_chi);
  only m = n + 2k contribute (P(m,0,n) = 0 for m < n or m − n odd). So the character coefficient is
      c_n = Σ_k t^{n+2k}/(n+2k)! · P(n+2k, 0, n)          (truncated at k < K below; c_n = (n+1)·2I_{n+1}(β)/β).
  Combinatorics (ℕ):
    ch_fact       C(a+b, b) · a! · b! = (a+b)!
    fact_mul_le   a! · b! ≤ (a+b)!
    P_le_ch       P(n+2k, 0, n) ≤ C(n+2k, k)           (walks staying ≥ 0 are among all walks)
    key_nat       P(n+2k, 0, n) · n! · k! · k! ≤ (n+2k)!
  Ordered field (division by factorials):
    c_nonneg      0 ≤ c_n
    c0_ge_one     c_0 ≥ 1
    moment_chi    I(y^m χ_n) = P(m, 0, n)    (so c_n is the Haar integral of e^{ty} χ_n, term by term)
    c_bound       c_n ≤ Σ_k t^{n+2k}/(n! k! k!)   ( = (t^n/n!) Σ_k t^{2k}/(k!)², a truncated I₀(2t) )
    ratio_bound   0 ≤ c_n / c_0 ≤ Σ_k t^{n+2k}/(n! k! k!)  — the strong-coupling bound c_n/c_0 = O((β/2)^n/n!)

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.SU2Char

namespace SU2CharExp

open DetChain SU2Haar SU2Char

def fact : Nat → Nat
  | 0 => 1
  | n + 1 => fact n * (n + 1)

def ch : Nat → Nat → Nat
  | _, 0 => 1
  | 0, _ + 1 => 0
  | n + 1, k + 1 => ch n k + ch n (k + 1)

theorem ch_zero_r : ∀ n, ch n 0 = 1
  | 0 => rfl
  | _ + 1 => rfl

theorem succ_ne (a : Nat) : a + 1 ≠ 0 := fun h => Nat.noConfusion h

theorem ch_self : ∀ n, ch n n = 1
  | 0 => rfl
  | n + 1 => by
    show ch n n + ch n (n + 1) = 1
    rw [ch_self n, ch_zero_gt n]
where
  ch_zero_gt : ∀ n, ch n (n + 1) = 0
    | 0 => rfl
    | n + 1 => by show ch n (n + 1) + ch n (n + 2) = 0; rw [ch_zero_gt n, ch_gt2 n]
  ch_gt2 : ∀ n, ch n (n + 2) = 0
    | 0 => rfl
    | n + 1 => by show ch n (n + 2) + ch n (n + 3) = 0; rw [ch_gt2 n, ch_gt3 n]
  ch_gt3 : ∀ n, ch n (n + 3) = 0 := fun n => ch_gt n 3 (Nat.succ_pos 2)
  ch_gt : ∀ n j, 0 < j → ch n (n + j) = 0
    | 0, 0, h => absurd h (Nat.lt_irrefl 0)
    | 0, _ + 1, _ => rfl
    | n + 1, j, h => by
      have e1 : n + 1 + j = (n + j) + 1 := by lib_nat
      rw [e1]
      cases j with
      | zero => exact absurd h (Nat.lt_irrefl 0)
      | succ j =>
        show ch n (n + (j + 1)) + ch n (n + (j + 1) + 1) = 0
        rw [ch_gt n (j + 1) h, show n + (j + 1) + 1 = n + (j + 2) by lib_nat, ch_gt n (j + 2) (Nat.succ_pos _)]

/-- one step of `C(a+b, b) · a! · b! = (a+b)!`, given the identity one level down -/
theorem ch_fact_step (N : Nat) (ih : ∀ a b, a + b = N → ch (a + b) b * fact a * fact b = fact (a + b)) :
    ∀ a b, a + b = N + 1 → ch (a + b) b * fact a * fact b = fact (a + b)
  | a, 0, _ => by
    rw [Nat.add_zero, ch_zero_r]; show 1 * fact a * 1 = fact a; lib_nat
  | 0, b + 1, _ => by
    rw [Nat.zero_add, ch_self]
    show 1 * 1 * fact (b + 1) = fact (b + 1); lib_nat
  | a + 1, b + 1, h => by
    have hN : a + b + 1 = N := by
      have : a + 1 + (b + 1) = (a + b + 1) + 1 := by lib_nat
      rw [this] at h; exact Nat.succ.inj h
    have h1 : (a + 1) + b = a + b + 1 := by lib_nat
    have h2 : a + (b + 1) = a + b + 1 := by lib_nat
    have f1 := ih (a + 1) b (by rw [h1]; exact hN)
    have f2 := ih a (b + 1) (by rw [h2]; exact hN)
    rw [h1] at f1
    rw [h2] at f2
    have e : a + 1 + (b + 1) = a + b + 1 + 1 := by lib_nat
    rw [e]
    show (ch (a + b + 1) b + ch (a + b + 1) (b + 1)) * (fact a * (a + 1)) * (fact b * (b + 1))
      = fact (a + b + 1) * (a + b + 1 + 1)
    have g1 : ch (a + b + 1) b * (fact a * (a + 1)) * (fact b * (b + 1)) = fact (a + b + 1) * (b + 1) := by
      rw [← f1]; show _ = ch (a + b + 1) b * (fact a * (a + 1)) * fact b * (b + 1); lib_nat
    have g2 : ch (a + b + 1) (b + 1) * (fact a * (a + 1)) * (fact b * (b + 1)) = fact (a + b + 1) * (a + 1) := by
      rw [← f2]; show _ = ch (a + b + 1) (b + 1) * fact a * (fact b * (b + 1)) * (a + 1); lib_nat
    have dist : ∀ x y A B : Nat, (x + y) * A * B = x * A * B + y * A * B := fun x y A B => by lib_nat
    rw [dist, g1, g2]
    lib_nat

/-- `C(a+b, b) · a! · b! = (a+b)!` -/
theorem ch_fact (N : Nat) : ∀ a b, a + b = N → ch (a + b) b * fact a * fact b = fact (a + b) := by
  induction N with
  | zero =>
    intro a b h
    cases b with
    | zero => rw [Nat.add_zero, ch_zero_r]; show 1 * fact a * 1 = fact a; lib_nat
    | succ b => exact absurd (show a + b + 1 = 0 by rw [← h]; lib_nat) (succ_ne _)
  | succ N ih => exact ch_fact_step N ih

theorem ch_pos : ∀ n k, k ≤ n → 1 ≤ ch n k
  | n, 0, _ => by rw [ch_zero_r]; exact Nat.le_refl 1
  | 0, _ + 1, h => absurd h (Nat.not_succ_le_zero _)
  | n + 1, k + 1, h => by
    show 1 ≤ ch n k + ch n (k + 1)
    exact Nat.le_trans (ch_pos n k (Nat.le_of_succ_le_succ h)) (Nat.le_add_right _ _)

theorem fact_mul_le (a b : Nat) : fact a * fact b ≤ fact (a + b) := by
  rw [← ch_fact (a + b) a b rfl]
  have := ch_pos (a + b) b (Nat.le_add_left b a)
  have h := Nat.mul_le_mul_right (fact a * fact b) this
  rw [Nat.one_mul] at h
  have e : ch (a + b) b * fact a * fact b = ch (a + b) b * (fact a * fact b) := by lib_nat
  rw [e]; exact h

/-- **I(y^m χ_n) = P(m, 0, n)**: the Haar moments of y^m against the characters are walk counts. -/
theorem moment_chi {R : Type} (K : CRing R) (m n : Nat) : Im K m (chiC K n) = cN K (P m 0 n) := by
  have h : ∀ m (p : List R), Im K m p = dot K m p (fun i => cN K (P i 0 0)) := by
    intro m p
    induction p generalizing m with
    | nil => rfl
    | cons c p ih => show K.add _ (Im K (m + 1) p) = K.add _ (dot K (m + 1) p _); rw [ih (m + 1)]; rfl
  rw [h, walk_chi K 0 n m]

/-- walks cannot go further than their length -/
theorem P_far : ∀ m t, m < t → P m 0 t = 0
  | 0, 0, h => absurd h (Nat.lt_irrefl 0)
  | 0, _ + 1, _ => rfl
  | m + 1, t, h => by
    cases t with
    | zero => exact absurd h (Nat.not_succ_le_zero _)
    | succ t =>
      rw [(last_step m 0).1 t, P_far m t (Nat.lt_of_succ_lt_succ h),
        P_far m (t + 2) (Nat.lt_trans (Nat.lt_of_succ_lt_succ h) (Nat.lt_succ_of_lt (Nat.lt_succ_self t)))]

/-- **Walks staying ≥ 0 are among all walks:** P(n+2k, 0, n) ≤ C(n+2k, k). -/
theorem P_le_ch : ∀ m n k, n + 2 * k = m → P m 0 n ≤ ch m k
  | 0, n, k, h => by
    cases n with
    | succ n => exact absurd (show n + 2 * k + 1 = 0 by rw [← h]; lib_nat) (succ_ne _)
    | zero =>
      cases k with
      | succ k => exact absurd (show 2 * k + 1 + 1 = 0 by rw [← h]; lib_nat) (succ_ne _)
      | zero => exact Nat.le_refl _
  | m + 1, 0, k, h => by
    -- P(m+1, 0, 0) = P(m, 0, 1), m = 1 + 2(k−1)
    rw [(last_step m 0).2]
    cases k with
    | zero => exact absurd (show m + 1 = 0 by rw [← h]) (succ_ne m)
    | succ k =>
      have hm : 1 + 2 * k = m := by
        have : 0 + 2 * (k + 1) = (1 + 2 * k) + 1 := by lib_nat
        rw [this] at h; exact Nat.succ.inj h
      have := P_le_ch m 1 k hm
      show P m 0 1 ≤ ch m k + ch m (k + 1)
      exact Nat.le_trans this (Nat.le_add_right _ _)
  | m + 1, n + 1, k, h => by
    rw [(last_step m 0).1 n]
    have hm : n + 2 * k = m := by
      have : n + 1 + 2 * k = (n + 2 * k) + 1 := by lib_nat
      rw [this] at h; exact Nat.succ.inj h
    have a1 := P_le_ch m n k hm
    cases k with
    | zero =>
      have z : P m 0 (n + 2) = 0 := P_far m (n + 2) (by rw [← hm]; exact Nat.lt_succ_of_lt (Nat.lt_succ_self _))
      rw [z, Nat.add_zero]
      show P m 0 n ≤ 1
      have := a1; rw [ch_zero_r] at this; exact this
    | succ k =>
      have a2 := P_le_ch m (n + 2) k (by rw [← hm]; lib_nat)
      show P m 0 n + P m 0 (n + 2) ≤ ch m k + ch m (k + 1)
      rw [Nat.add_comm (ch m k)]
      exact Nat.add_le_add a1 a2

/-- **P(n+2k, 0, n) · n! · k! · k! ≤ (n+2k)!** -/
theorem key_nat (n k : Nat) : P (n + 2 * k) 0 n * fact n * fact k * fact k ≤ fact (n + 2 * k) := by
  have e : n + 2 * k = (n + k) + k := by lib_nat
  have cf := ch_fact ((n + k) + k) (n + k) k rfl
  rw [← e] at cf
  have p1 := P_le_ch (n + 2 * k) n k rfl
  have m1 := fact_mul_le n k
  -- P n! k! k! ≤ P (n+k)! k! ≤ C (n+k)! k! = (n+2k)!
  have s1 : P (n + 2 * k) 0 n * fact n * fact k * fact k ≤ P (n + 2 * k) 0 n * fact (n + k) * fact k := by
    have e2 : P (n + 2 * k) 0 n * fact n * fact k * fact k = P (n + 2 * k) 0 n * (fact n * fact k) * fact k := by lib_nat
    rw [e2]
    exact Nat.mul_le_mul_right _ (Nat.mul_le_mul_left _ m1)
  have s2 : P (n + 2 * k) 0 n * fact (n + k) * fact k ≤ ch (n + 2 * k) k * fact (n + k) * fact k :=
    Nat.mul_le_mul_right _ (Nat.mul_le_mul_right _ p1)
  rw [cf] at s2
  exact Nat.le_trans s1 s2

/-! ## Coefficients in an ordered field -/

section Field
variable {R : Type} (K : CRing R) (O : YMChain.ORing K) (F : OField.OF K O)

open LinkIntegral

theorem cN_nn (h1 : O.le K.zero K.one) : ∀ n, O.le K.zero (cN K n)
  | 0 => O.le_refl _
  | n + 1 => by
    show O.le K.zero (K.add K.one (cN K n))
    have := O.add_le_add _ _ _ _ h1 (cN_nn h1 n); rw [K.zero_add] at this; exact this

theorem cN_mono (h1 : O.le K.zero K.one) (a b : Nat) (h : a ≤ b) : O.le (cN K a) (cN K b) := by
  obtain ⟨c, e⟩ := Nat.le.dest h
  rw [← e, cN_add]
  have := O.add_le_add _ _ _ _ (O.le_refl (cN K a)) (cN_nn K O h1 c)
  rw [add_zero'] at this; exact this

theorem cN_mul (a : Nat) : ∀ b, cN K (a * b) = K.mul (cN K a) (cN K b)
  | 0 => by show K.zero = K.mul (cN K a) K.zero; rw [mul_zero]
  | b + 1 => by
    show cN K (a * b + a) = K.mul (cN K a) (K.add K.one (cN K b))
    rw [cN_add, cN_mul a b]; lib_ring

theorem cN_one : cN K 1 = K.one := by show K.add K.one K.zero = K.one; rw [add_zero']

theorem fact_pos : ∀ n, 1 ≤ fact n
  | 0 => Nat.le_refl 1
  | n + 1 => by
    show 1 ≤ fact n * (n + 1)
    exact Nat.mul_le_mul (fact_pos n) (Nat.succ_le_succ (Nat.zero_le n))

theorem pos_of_ge_one (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n : Nat) (h : 1 ≤ n) :
    O.le K.zero (cN K n) ∧ cN K n ≠ K.zero := by
  have g := cN_mono K O h1 1 n h
  rw [cN_one] at g
  exact ⟨O.le_trans _ _ _ h1 g, fun e => h10 (O.le_antisymm _ _ (by rw [← e]; exact g) h1)⟩

/-- inverse uniqueness: `x y = 1 ⇒ y = x⁻¹` -/
theorem inv_uniq (x y : R) (hx : x ≠ K.zero) (h : K.mul x y = K.one) : y = F.inv x := by
  have e := F.mul_inv x hx
  calc y = K.mul y (K.mul x (F.inv x)) := by rw [e, K.mul_comm, K.one_mul]
    _ = K.mul (K.mul x y) (F.inv x) := by lib_ring
    _ = F.inv x := by rw [h, K.one_mul]

/-- `a·A⁻¹ ≤ b·B⁻¹` from `a·B ≤ b·A` (A, B > 0) -/
theorem frac_le (a b A B : R) (hA : O.le K.zero A) (hA0 : A ≠ K.zero) (hB : O.le K.zero B) (hB0 : B ≠ K.zero)
    (h : O.le (K.mul a B) (K.mul b A)) : O.le (K.mul a (F.inv A)) (K.mul b (F.inv B)) := by
  have m := O.mul_le_mul_right _ _ _ h (O.mul_nonneg _ _ (F.inv_pos A hA hA0) (F.inv_pos B hB hB0))
  have eL : K.mul (K.mul a B) (K.mul (F.inv A) (F.inv B)) = K.mul (K.mul a (F.inv A)) (K.mul B (F.inv B)) := by lib_ring
  have eR : K.mul (K.mul b A) (K.mul (F.inv A) (F.inv B)) = K.mul (K.mul b (F.inv B)) (K.mul A (F.inv A)) := by lib_ring
  rw [eL, eR, F.mul_inv B hB0, F.mul_inv A hA0, K.mul_comm _ K.one, K.one_mul, K.mul_comm _ K.one, K.one_mul] at m
  exact m

/-- `t^n / n!` -/
def w (t : R) (n : Nat) : R := K.mul (pwR K t n) (F.inv (cN K (fact n)))

/-- the k-th term of the character coefficient c_n -/
def term (t : R) (n k : Nat) : R := K.mul (w K O F t (n + 2 * k)) (cN K (P (n + 2 * k) 0 n))

/-- its bound `t^{n+2k} / (n! k! k!)` -/
def bnd (t : R) (n k : Nat) : R := K.mul (pwR K t (n + 2 * k)) (F.inv (cN K (fact n * fact k * fact k)))

/-- `c_n` truncated: Σ_{k<Kk} t^{n+2k}/(n+2k)! · P(n+2k, 0, n) -/
def cS (t : R) (n : Nat) : Nat → R
  | 0 => K.zero
  | Kk + 1 => K.add (cS t n Kk) (term K O F t n Kk)

def bS (t : R) (n : Nat) : Nat → R
  | 0 => K.zero
  | Kk + 1 => K.add (bS t n Kk) (bnd K O F t n Kk)

theorem term_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n k : Nat) :
    O.le K.zero (term K O F t n k) := by
  have pf := pos_of_ge_one K O h1 h10 _ (fact_pos (n + 2 * k))
  exact O.mul_nonneg _ _ (O.mul_nonneg _ _ (AreaLower.pw_nn K O t ht h1 _) (F.inv_pos _ pf.1 pf.2)) (cN_nn K O h1 _)

/-- **Termwise bound.** -/
theorem term_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n k : Nat) :
    O.le (term K O F t n k) (bnd K O F t n k) := by
  let m := n + 2 * k
  have e : term K O F t n k = K.mul (K.mul (pwR K t m) (cN K (P m 0 n))) (F.inv (cN K (fact m))) := by
    show K.mul (K.mul (pwR K t m) (F.inv (cN K (fact m)))) (cN K (P m 0 n)) = _
    lib_ring
  rw [e]
  have pA := pos_of_ge_one K O h1 h10 _ (fact_pos m)
  have pB := pos_of_ge_one K O h1 h10 (fact n * fact k * fact k)
    (Nat.mul_le_mul (Nat.mul_le_mul (fact_pos n) (fact_pos k)) (fact_pos k))
  apply frac_le K O F _ _ _ _ pA.1 pA.2 pB.1 pB.2
  -- t^m P · (n!k!k!) ≤ t^m · m!
  have kn := cN_mono K O h1 _ _ (key_nat n k)
  have en : P m 0 n * fact n * fact k * fact k = P m 0 n * (fact n * fact k * fact k) := by lib_nat
  rw [en, cN_mul] at kn
  have := O.mul_le_mul_right _ _ _ kn (AreaLower.pw_nn K O t ht h1 m)
  rw [K.mul_comm _ (pwR K t m), K.mul_comm (cN K (fact m)) (pwR K t m), ← K.mul_assoc] at this
  exact this

theorem cS_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n : Nat) :
    ∀ Kk, O.le K.zero (cS K O F t n Kk)
  | 0 => O.le_refl _
  | Kk + 1 => by
    have := O.add_le_add _ _ _ _ (cS_nn h1 h10 t ht n Kk) (term_nn K O F h1 h10 t ht n Kk)
    rw [K.zero_add] at this; exact this

/-- **c_n ≤ Σ_k t^{n+2k}/(n! k! k!).** -/
theorem c_bound (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n : Nat) :
    ∀ Kk, O.le (cS K O F t n Kk) (bS K O F t n Kk)
  | 0 => O.le_refl _
  | Kk + 1 => O.add_le_add _ _ _ _ (c_bound h1 h10 t ht n Kk) (term_le K O F h1 h10 t ht n Kk)

/-- **c_0 ≥ 1.** -/
theorem c0_ge_one (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) :
    ∀ Kk, O.le K.one (cS K O F t 0 (Kk + 1))
  | 0 => by
    have e : term K O F t 0 0 = K.one := by
      show K.mul (K.mul K.one (F.inv (cN K 1))) (cN K 1) = K.one
      rw [cN_one, ← inv_uniq K O F K.one K.one h10 (K.one_mul K.one), K.one_mul, K.one_mul]
    show O.le K.one (K.add K.zero (term K O F t 0 0))
    rw [e, K.zero_add]; exact O.le_refl _
  | Kk + 1 => by
    have := O.add_le_add _ _ _ _ (c0_ge_one h1 h10 t ht Kk) (term_nn K O F h1 h10 t ht 0 (Kk + 1))
    rw [add_zero'] at this; exact this

/-- **c_n / c_0 ≤ Σ_k t^{n+2k}/(n! k! k!) = (t^n/n!) · Σ_k t^{2k}/(k!)².** -/
theorem ratio_bound (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n Kk : Nat) :
    O.le K.zero (K.mul (cS K O F t n (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))) ∧
    O.le (K.mul (cS K O F t n (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))) (bS K O F t n (Kk + 1)) := by
  have c0 := c0_ge_one K O F h1 h10 t ht Kk
  have c0p : O.le K.zero (cS K O F t 0 (Kk + 1)) := O.le_trans _ _ _ h1 c0
  have c0n : cS K O F t 0 (Kk + 1) ≠ K.zero := fun e => h10 (O.le_antisymm _ _ (by rw [← e]; exact c0) h1)
  have ip := F.inv_pos _ c0p c0n
  have cn := cS_nn K O F h1 h10 t ht n (Kk + 1)
  refine ⟨O.mul_nonneg _ _ cn ip, ?_⟩
  -- c0⁻¹ ≤ 1
  have i1 : O.le (F.inv (cS K O F t 0 (Kk + 1))) K.one := by
    have m := O.mul_le_mul_right _ _ _ c0 ip
    rw [K.one_mul, F.mul_inv _ c0n] at m; exact m
  have s := O.mul_le_mul_right _ _ _ i1 cn
  rw [K.one_mul, K.mul_comm] at s
  exact O.le_trans _ _ _ s (c_bound K O F h1 h10 t ht n (Kk + 1))

end Field

end SU2CharExp

#print axioms SU2CharExp.ch_fact
#print axioms SU2CharExp.P_le_ch
#print axioms SU2CharExp.key_nat
#print axioms SU2CharExp.moment_chi
#print axioms SU2CharExp.term_le
#print axioms SU2CharExp.c0_ge_one
#print axioms SU2CharExp.ratio_bound

ym/SU2Wilson2D.lean

/-
  ym/SU2Wilson2D — T45, S4: the exact 2D SU(2) Wilson loop, ⟨W⟩ = (c₁/(2c₀))^{Area}, in every volume.

  Take the plaquette weight to be a polynomial class function of y = tr U, w(U) = Σ_i p_i (tr U)^i (every truncation
  Σ_{m<N} t^m/m! y^m of e^{β tr U/2} is one). Its character coefficients (S2/S3) are
      c_n(p) = Σ_i p_i · P(i, 0, n)  = I(p · χ_n)                     (coeff_chi)
  For any normalised two-sided-invariant integral μ on the group whose values on polynomial class functions are the
  S1 integral I (the semicircle law of tr U — for SU(2) this is the Weyl integration formula), and the two-dimensional
  fundamental representation, Lad3.rect_char gives on every R × T rectangle of the 2D lattice, every r0 and boundary f:
      ∫ Π_p w(U_p) · tr(U_W) = 2 λ^{RT},   ∫ Π_p w(U_p) = c₀^{RT},   2λ = c₁
  i.e. ⟨W⟩ = ⟨tr U_W / 2⟩ = (c₁/(2c₀))^{RT} — the area law with string tension −log(c₁/(2c₀)), exactly, in every volume.
    ev_mulL        evaluation of products of class polynomials
    coeff_chi      I(p · χ_n) = Σ_i p_i P(i, 0, n)
    wilson_2d      the statement above

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.SU2CharExp
import ym.Lad3

namespace SU2Wilson2D

open YMChain LinkIntegral Strip2D Sites RectInt SU2Haar SU2Char

variable {R : Type} (K : DetChain.CRing R)

theorem ev_mulL (y : R) (q : List R) : ∀ p : List R, ev K (mulL K p q) y = K.mul (ev K p y) (ev K q y)
  | [] => by show K.zero = K.mul K.zero _; rw [K.mul_comm, DetChain.mul_zero]
  | a :: p => by
    show ev K (addL K (smulL K a q) (K.zero :: mulL K p q)) y = K.mul (K.add a (K.mul y (ev K p y))) (ev K q y)
    rw [ev_addL, ev_smulL]
    show K.add (K.mul a (ev K q y)) (K.add K.zero (K.mul y (ev K (mulL K p q) y))) = _
    rw [ev_mulL y q p]; lib_ring

/-- **Character coefficients of a polynomial class weight.** -/
theorem coeff_chi (p : List R) (n : Nat) : I K (mulL K p (chiC K n)) = dot K 0 p (fun i => cN K (P i 0 n)) := by
  rw [I_mul_dot]
  exact dot_ext K _ _ (fun i => walk_chi K 0 n i) 0 p

theorem ev_chi1 (y : R) : ev K (chiC K 1) y = y := by
  show K.add K.zero (K.mul y (K.add K.one (K.mul y K.zero))) = y
  rw [DetChain.mul_zero, DetChain.add_zero', K.zero_add, K.mul_comm, K.one_mul]

/-- **The exact 2D SU(2) Wilson loop.** -/
theorem wilson_2d {G : Type} (Gp : Grp G) {ι : Type} [DecidableEq ι] (Rr : Nat) (ρ : G → ι → ι → R) (idx : List ι)
    (μ : (G → R) → R) (hμ : Lin K μ) (hμ1 : μ (fun _ => K.one) = K.one)
    (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (hr : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul g x)) = μ f)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hone : ∀ i j, ρ Gp.one i j = delta K i j)
    (hirr : ∀ M : ι → ι → R, (∀ h i j, sumK K (fun k => K.mul (ρ h i k) (M k j)) idx
      = sumK K (fun k => K.mul (M i k) (ρ h k j)) idx) → ∃ c, ∀ i j, M i j = K.mul c (delta K i j))
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i)
    (hdim : sumK K (fun _ => K.one) idx = K.add K.one K.one)
    (htrc : ∀ x y, sumK K (fun i => ρ (Gp.mul (Gp.mul x y) (Gp.inv x)) i i) idx = sumK K (fun i => ρ y i i) idx)
    (hcompat : ∀ q : List R, μ (fun g => ev K q (sumK K (fun i => ρ g i i) idx)) = I K q)
    (p : List R) :
    let w : G → R := fun g => ev K p (sumK K (fun i => ρ g i i) idx)
    ∃ lam, K.mul (K.add K.one K.one) lam = dot K 0 p (fun i => cN K (P i 0 1)) ∧
      ∀ (T r0 : Nat) (f : S → G),
        IntS μ (sitesR Rr r0 T) (fun f' => K.mul (plaqProd K Gp Rr w (Hf f') (Vf f') r0 T) (trW K Gp Rr ρ idx f' r0 T)) f
          = K.mul (K.add K.one K.one) (pwR K lam (Rr * T)) ∧
        IntS μ (sitesR Rr r0 T) (fun f' => plaqProd K Gp Rr w (Hf f') (Vf f') r0 T) f
          = pwR K (dot K 0 p (fun i => cN K (P i 0 0))) (Rr * T) := by
  intro w
  have hw : ∀ x y, w (Gp.mul (Gp.mul x y) (Gp.inv x)) = w y := fun x y => by
    show ev K p _ = ev K p _; rw [htrc]
  obtain ⟨lam, hlam, hrect⟩ := Lad3.rect_char K Gp Rr w ρ idx μ hμ hμ1 hl hr hom hone hw hirr hsel1
  have c1 : μ (fun g => K.mul (w g) (sumK K (fun i => ρ g i i) idx)) = dot K 0 p (fun i => cN K (P i 0 1)) := by
    rw [← coeff_chi, ← hcompat]
    exact Lin.ext hμ _ _ (fun g => by
      show K.mul (ev K p _) _ = ev K (mulL K p (chiC K 1)) _
      rw [ev_mulL, ev_chi1])
  have c0 : μ w = dot K 0 p (fun i => cN K (P i 0 0)) := by
    rw [hcompat p, I_dot]
  rw [hdim] at hlam
  refine ⟨lam, hlam.trans c1, fun T r0 f => ?_⟩
  have h := hrect T r0 f
  rw [hdim, c0] at h
  exact h

end SU2Wilson2D

#print axioms SU2Wilson2D.ev_mulL
#print axioms SU2Wilson2D.coeff_chi
#print axioms SU2Wilson2D.wilson_2d

ym/Gauss.lean

/-
  ym/Gauss — T45, H0 (Haar on SU(2) for polynomials), step 1: the Gaussian moment functional and integration by parts.

  Monomials in n variables are exponent lists α. The standard Gaussian moments are
      G(x^α) = Π_i m(α_i),   m(2k) = (2k−1)!!,  m(odd) = 0.
    gm_succ2       m(a+2) = (a+1)·m(a)
    G_ibp          G(x_i · x^α) = α_i · G(x^{α − e_i})     (integration by parts, at the level of monomials)
  A linear functional with Λ(1) = 1 and this rule is G (next step); the rule is preserved by orthogonal substitutions
  (O Oᵀ = I), which gives rotation invariance — and left multiplication by a unit quaternion is orthogonal on ℝ⁴, so
  the normalised sphere functional is the Haar integral of SU(2) on polynomial functions (H0).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Projective

namespace Gauss

open SU2Char

/-- one-dimensional Gaussian moments over ℕ: m(0) = 1, m(1) = 0, m(a+2) = (a+1) m(a) -/
def gm : Nat → Nat
  | 0 => 1
  | 1 => 0
  | a + 2 => (a + 1) * gm a

theorem gm_succ2 (a : Nat) : gm (a + 2) = (a + 1) * gm a := rfl

/-- moments of a monomial -/
def GM : List Nat → Nat
  | [] => 1
  | a :: α => gm a * GM α

/-- raise / lower the i-th exponent -/
def incAt : Nat → List Nat → List Nat
  | _, [] => []
  | 0, a :: α => (a + 1) :: α
  | i + 1, a :: α => a :: incAt i α

def decAt : Nat → List Nat → List Nat
  | _, [] => []
  | 0, a :: α => (a - 1) :: α
  | i + 1, a :: α => a :: decAt i α

def nthE : Nat → List Nat → Nat
  | _, [] => 0
  | 0, a :: _ => a
  | i + 1, _ :: α => nthE i α

/-- **Integration by parts on monomials:** G(x_i x^α) = α_i · G(x^{α − e_i}). -/
theorem G_ibp : ∀ (i : Nat) (α : List Nat), i < α.length → GM (incAt i α) = nthE i α * GM (decAt i α)
  | _, [], h => absurd h (Nat.not_lt_zero _)
  | 0, a :: α, _ => by
    show gm (a + 1) * GM α = a * (gm (a - 1) * GM α)
    cases a with
    | zero => show 0 * GM α = 0 * _; rw [Nat.zero_mul, Nat.zero_mul]
    | succ a =>
      show gm (a + 2) * GM α = (a + 1) * (gm a * GM α)
      rw [gm_succ2]; lib_nat
  | i + 1, a :: α, h => by
    show gm a * GM (incAt i α) = nthE i α * (gm a * GM (decAt i α))
    rw [G_ibp i α (Nat.lt_of_succ_lt_succ h)]; lib_nat

theorem gm_odd : ∀ k, gm (2 * k + 1) = 0
  | 0 => rfl
  | k + 1 => by
    show gm (2 * k + 1 + 2) = 0
    rw [gm_succ2, gm_odd k, Nat.mul_zero]

theorem gm_values : gm 2 = 1 ∧ gm 4 = 3 ∧ gm 6 = 15 ∧ gm 8 = 105 := by decide

end Gauss

#print axioms Gauss.G_ibp
#print axioms Gauss.gm_odd

ym/GaussPoly.lean

/-
  ym/GaussPoly — T45, H0 step 2: the Gaussian functional on polynomials and its uniqueness.

  A polynomial in n variables over a commutative ring is a list of terms (c, α) (coefficient, exponent list of length n).
    Gp p = Σ c · G(x^α)                     linear functional (Gp_app)
    xi i p, dP i p                          multiplication by x_i, partial derivative ∂_i (termwise)
    Gp_ibp                                  Gp(x_i p) = Gp(∂_i p)
    unique_mono                             any Λ, additive on term lists, homogeneous on single terms, with Λ(1) = 1 and
                                            Λ(x_i p) = Λ(∂_i p), equals G on every monomial (induction on the degree)
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Gauss

namespace GaussPoly

open SU2Char Gauss

variable {R : Type} (K : DetChain.CRing R)

abbrev Term (R : Type) := R × List Nat

def Gp : List (Term R) → R
  | [] => K.zero
  | t :: p => K.add (K.mul t.1 (cN K (GM t.2))) (Gp p)

theorem Gp_app : ∀ p q : List (Term R), Gp K (p ++ q) = K.add (Gp K p) (Gp K q)
  | [], q => (K.zero_add _).symm
  | t :: p, q => by
    show K.add _ (Gp K (p ++ q)) = K.add (K.add _ (Gp K p)) (Gp K q)
    rw [Gp_app p q]; lib_ring

def xi (i : Nat) (p : List (Term R)) : List (Term R) := p.map (fun t => (t.1, incAt i t.2))

def dP (i : Nat) (p : List (Term R)) : List (Term R) := p.map (fun t => (K.mul t.1 (cN K (nthE i t.2)), decAt i t.2))

/-- every term has at least i+1 variables -/
def Wide (i : Nat) : List (Term R) → Prop
  | [] => True
  | t :: p => i < t.2.length ∧ Wide i p

/-- **Integration by parts.** -/
theorem Gp_ibp (i : Nat) : ∀ p : List (Term R), Wide i p → Gp K (xi i p) = Gp K (dP K i p)
  | [], _ => rfl
  | t :: p, h => by
    show K.add (K.mul t.1 (cN K (GM (incAt i t.2)))) (Gp K (xi i p))
      = K.add (K.mul (K.mul t.1 (cN K (nthE i t.2))) (cN K (GM (decAt i t.2)))) (Gp K (dP K i p))
    rw [Gp_ibp i p h.2, G_ibp i t.2 h.1, SU2CharExp.cN_mul, K.mul_assoc]

def sumL : List Nat → Nat
  | [] => 0
  | a :: α => a + sumL α

theorem len_decAt : ∀ (i : Nat) (α : List Nat), (decAt i α).length = α.length
  | 0, [] => rfl
  | _ + 1, [] => rfl
  | 0, _ :: _ => rfl
  | i + 1, _ :: α => congrArg Nat.succ (len_decAt i α)

theorem incAt_decAt : ∀ (i : Nat) (α : List Nat), 1 ≤ nthE i α → incAt i (decAt i α) = α
  | 0, [], h => absurd h (Nat.not_succ_le_zero 0)
  | _ + 1, [], h => absurd h (Nat.not_succ_le_zero 0)
  | 0, a :: α, h => by
    cases a with
    | zero => exact absurd h (Nat.not_succ_le_zero 0)
    | succ a => rfl
  | i + 1, a :: α, h => by
    show a :: incAt i (decAt i α) = a :: α
    rw [incAt_decAt i α h]

theorem sum_decAt : ∀ (i : Nat) (α : List Nat), 1 ≤ nthE i α → sumL (decAt i α) + 1 = sumL α
  | 0, [], h => absurd h (Nat.not_succ_le_zero 0)
  | _ + 1, [], h => absurd h (Nat.not_succ_le_zero 0)
  | 0, a :: α, h => by
    cases a with
    | zero => exact absurd h (Nat.not_succ_le_zero 0)
    | succ a => show a + sumL α + 1 = a + 1 + sumL α; lib_nat
  | i + 1, a :: α, h => by
    show a + sumL (decAt i α) + 1 = a + sumL α
    rw [Nat.add_assoc, sum_decAt i α h]

theorem exists_pos : ∀ α : List Nat, 1 ≤ sumL α → ∃ i, i < α.length ∧ 1 ≤ nthE i α
  | [], h => absurd h (Nat.not_succ_le_zero 0)
  | a :: α, h => by
    cases a with
    | succ a => exact ⟨0, Nat.succ_pos _, Nat.succ_le_succ (Nat.zero_le a)⟩
    | zero =>
      have h' : 1 ≤ sumL α := by
        have : sumL (0 :: α) = sumL α := Nat.zero_add _
        rw [this] at h; exact h
      obtain ⟨i, hi, hp⟩ := exists_pos α h'
      exact ⟨i + 1, Nat.succ_lt_succ hi, hp⟩

def zerosL : Nat → List Nat
  | 0 => []
  | n + 1 => 0 :: zerosL n

theorem zero_sum : ∀ α : List Nat, sumL α = 0 → α = zerosL α.length
  | [], _ => rfl
  | a :: α, h => by
    cases a with
    | succ a => exact absurd h (by show a + 1 + sumL α ≠ 0; intro e; exact Nat.noConfusion (show (a + sumL α) + 1 = 0 by rw [← e]; lib_nat))
    | zero =>
      have h' : sumL α = 0 := by have : sumL (0 :: α) = sumL α := Nat.zero_add _; rw [← this]; exact h
      show 0 :: α = 0 :: zerosL α.length
      rw [← zero_sum α h']

theorem GM_zeros : ∀ n, GM (zerosL n) = 1
  | 0 => rfl
  | n + 1 => by show gm 0 * GM (zerosL n) = 1; rw [GM_zeros n]; rfl

/-- **Uniqueness of the Gaussian functional on monomials.** -/
theorem unique_mono (Λ : List (Term R) → R) (n : Nat)
    (hlin : ∀ (c : R) (α : List Nat), Λ [(c, α)] = K.mul c (Λ [(K.one, α)]))
    (hone : Λ [(K.one, zerosL n)] = K.one)
    (hibp : ∀ (i : Nat) (β : List Nat), i < n → β.length = n → Λ (xi i [(K.one, β)]) = Λ (dP K i [(K.one, β)])) :
    ∀ d (α : List Nat), α.length = n → sumL α ≤ d → Λ [(K.one, α)] = cN K (GM α) := by
  intro d
  induction d with
  | zero =>
    intro α hl hs
    have h0 : sumL α = 0 := Nat.le_antisymm hs (Nat.zero_le _)
    rw [zero_sum α h0, hl, hone, GM_zeros]; exact (SU2CharExp.cN_one K).symm
  | succ d ih =>
    intro α hl hs
    cases Nat.lt_or_ge (sumL α) 1 with
    | inl h0 =>
      have h0' : sumL α = 0 := Nat.le_antisymm (Nat.le_of_lt_succ h0) (Nat.zero_le _)
      exact ih α hl (by rw [h0']; exact Nat.zero_le _)
    | inr hpos =>
      obtain ⟨i, hi, hp⟩ := exists_pos α hpos
      let β := decAt i α
      have eα : incAt i β = α := incAt_decAt i α hp
      have hβl : β.length = n := (len_decAt i α).trans hl
      have hsb : sumL β + 1 = sumL α := sum_decAt i α hp
      have step : Λ [(K.one, α)] = Λ [(K.mul K.one (cN K (nthE i β)), decAt i β)] := by
        have := hibp i β (by rw [← hl]; exact hi) hβl
        rw [← eα]; exact this
      rw [step, hlin, K.one_mul]
      have gib := G_ibp i β (by rw [hβl, ← hl]; exact hi)
      rw [eα] at gib
      rw [gib, SU2CharExp.cN_mul]
      cases Nat.lt_or_ge (nthE i β) 1 with
      | inl hz =>
        have z : nthE i β = 0 := Nat.le_antisymm (Nat.le_of_lt_succ hz) (Nat.zero_le _)
        rw [z, show cN K 0 = K.zero from rfl, K.mul_comm K.zero, DetChain.mul_zero, K.mul_comm K.zero, DetChain.mul_zero]
      | inr hb =>
        have hsc : sumL (decAt i β) + 1 = sumL β := sum_decAt i β hb
        rw [ih (decAt i β) ((len_decAt i β).trans hβl) (by
          have : sumL (decAt i β) + 1 + 1 = sumL α := by rw [hsc, hsb]
          have h2 : sumL (decAt i β) + 2 ≤ d + 1 := by rw [show sumL (decAt i β) + 2 = sumL (decAt i β) + 1 + 1 from rfl, this]; exact hs
          exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.succ_le_succ (Nat.le_succ _)) h2))]

/-! ### products and the Leibniz rule -/

def addE : List Nat → List Nat → List Nat
  | [], β => β
  | a :: α, [] => a :: α
  | a :: α, b :: β => (a + b) :: addE α β

theorem nth_addE : ∀ (j : Nat) (α β : List Nat), nthE j (addE α β) = nthE j α + nthE j β
  | 0, [], [] => rfl
  | _ + 1, [], [] => rfl
  | 0, [], _ :: _ => (Nat.zero_add _).symm
  | _ + 1, [], _ :: _ => (Nat.zero_add _).symm
  | 0, _ :: _, [] => rfl
  | _ + 1, _ :: _, [] => rfl
  | 0, _ :: _, _ :: _ => rfl
  | j + 1, _ :: α, _ :: β => nth_addE j α β

theorem dec_addE_l : ∀ (j : Nat) (α β : List Nat), 1 ≤ nthE j α → decAt j (addE α β) = addE (decAt j α) β
  | 0, [], _, h => absurd h (Nat.not_succ_le_zero 0)
  | _ + 1, [], _, h => absurd h (Nat.not_succ_le_zero 0)
  | 0, a :: α, [], h => rfl
  | j + 1, a :: α, [], h => rfl
  | 0, a :: α, b :: β, h => by
    cases a with
    | zero => exact absurd h (Nat.not_succ_le_zero 0)
    | succ a => show (a + 1 + b - 1) :: _ = (a + b) :: _; rw [show a + 1 + b = a + b + 1 by lib_nat]; rfl
  | j + 1, a :: α, b :: β, h => by
    show (a + b) :: decAt j (addE α β) = (a + b) :: addE (decAt j α) β
    rw [dec_addE_l j α β h]

theorem dec_addE_r : ∀ (j : Nat) (α β : List Nat), 1 ≤ nthE j β → decAt j (addE α β) = addE α (decAt j β)
  | 0, _, [], h => absurd h (Nat.not_succ_le_zero 0)
  | _ + 1, _, [], h => absurd h (Nat.not_succ_le_zero 0)
  | 0, [], b :: β, h => rfl
  | j + 1, [], b :: β, h => rfl
  | 0, a :: α, b :: β, h => by
    cases b with
    | zero => exact absurd h (Nat.not_succ_le_zero 0)
    | succ b => show (a + (b + 1) - 1) :: _ = (a + b) :: _; rw [show a + (b + 1) = a + b + 1 by lib_nat]; rfl
  | j + 1, a :: α, b :: β, h => by
    show (a + b) :: decAt j (addE α β) = (a + b) :: addE α (decAt j β)
    rw [dec_addE_r j α β h]

def mulT (t s : Term R) : Term R := (K.mul t.1 s.1, addE t.2 s.2)

def mulP : List (Term R) → List (Term R) → List (Term R)
  | [], _ => []
  | t :: A, B => B.map (mulT K t) ++ mulP A B

theorem Gp_map_mulT (t : Term R) (f : Term R → Term R) : ∀ B : List (Term R),
    Gp K (B.map f) = Gp K (B.map f) := fun _ => rfl

theorem Gp_mulP_cons (t : Term R) (A B : List (Term R)) : Gp K (mulP K (t :: A) B) = K.add (Gp K (B.map (mulT K t))) (Gp K (mulP K A B)) :=
  Gp_app K _ _

/-- Leibniz for a pair of terms, at the level of Gp -/
theorem leib_TT (j : Nat) (t s : Term R) :
    Gp K (dP K j [mulT K t s]) = K.add (Gp K [mulT K (K.mul t.1 (cN K (nthE j t.2)), decAt j t.2) s])
      (Gp K [mulT K t (K.mul s.1 (cN K (nthE j s.2)), decAt j s.2)]) := by
  obtain ⟨c, α⟩ := t
  obtain ⟨d, β⟩ := s
  show K.add (K.mul (K.mul (K.mul c d) (cN K (nthE j (addE α β)))) (cN K (GM (decAt j (addE α β))))) K.zero
    = K.add (K.add (K.mul (K.mul (K.mul c (cN K (nthE j α))) d) (cN K (GM (addE (decAt j α) β)))) K.zero)
        (K.add (K.mul (K.mul c (K.mul d (cN K (nthE j β)))) (cN K (GM (addE α (decAt j β))))) K.zero)
  rw [nth_addE, cN_add]
  cases Nat.lt_or_ge (nthE j α) 1 with
  | inl ha =>
    have za : nthE j α = 0 := Nat.le_antisymm (Nat.le_of_lt_succ ha) (Nat.zero_le _)
    rw [za]
    cases Nat.lt_or_ge (nthE j β) 1 with
    | inl hb =>
      have zb : nthE j β = 0 := Nat.le_antisymm (Nat.le_of_lt_succ hb) (Nat.zero_le _)
      rw [zb]; show K.add (K.mul (K.mul (K.mul c d) (K.add K.zero K.zero)) _) K.zero
        = K.add (K.add (K.mul (K.mul (K.mul c K.zero) d) _) K.zero) (K.add (K.mul (K.mul c (K.mul d K.zero)) _) K.zero)
      lib_ring
    | inr hb =>
      rw [← dec_addE_r j α β hb]
      show K.add (K.mul (K.mul (K.mul c d) (K.add K.zero (cN K (nthE j β)))) _) K.zero
        = K.add (K.add (K.mul (K.mul (K.mul c K.zero) d) _) K.zero) (K.add (K.mul (K.mul c (K.mul d (cN K (nthE j β)))) _) K.zero)
      lib_ring
  | inr ha =>
    cases Nat.lt_or_ge (nthE j β) 1 with
    | inl hb =>
      have zb : nthE j β = 0 := Nat.le_antisymm (Nat.le_of_lt_succ hb) (Nat.zero_le _)
      rw [zb, ← dec_addE_l j α β ha]
      show K.add (K.mul (K.mul (K.mul c d) (K.add (cN K (nthE j α)) K.zero)) _) K.zero
        = K.add (K.add (K.mul (K.mul (K.mul c (cN K (nthE j α))) d) _) K.zero) (K.add (K.mul (K.mul c (K.mul d K.zero)) _) K.zero)
      lib_ring
    | inr hb =>
      rw [← dec_addE_l j α β ha, ← dec_addE_r j α β hb]
      lib_ring

theorem Gp_cons' (x : Term R) (L : List (Term R)) : Gp K (x :: L) = K.add (Gp K [x]) (Gp K L) := by
  show K.add _ (Gp K L) = K.add (K.add _ K.zero) (Gp K L); rw [DetChain.add_zero']

theorem dP_app (j : Nat) (L1 L2 : List (Term R)) : dP K j (L1 ++ L2) = dP K j L1 ++ dP K j L2 := by
  induction L1 with
  | nil => rfl
  | cons x L1 ih => show _ :: dP K j (L1 ++ L2) = _ :: (dP K j L1 ++ dP K j L2); rw [ih]

def tder (j : Nat) (t : Term R) : Term R := (K.mul t.1 (cN K (nthE j t.2)), decAt j t.2)

theorem leib_TL (j : Nat) (t : Term R) : ∀ B : List (Term R),
    Gp K (dP K j (B.map (mulT K t))) = K.add (Gp K (B.map (mulT K (tder K j t)))) (Gp K ((dP K j B).map (mulT K t)))
  | [] => by show K.zero = K.add K.zero K.zero; rw [K.zero_add]
  | s :: B => by
    have ih := leib_TL j t B
    show Gp K (dP K j [mulT K t s] ++ dP K j (B.map (mulT K t))) = K.add (Gp K (mulT K (tder K j t) s :: B.map _))
      (Gp K (mulT K t (tder K j s) :: (dP K j B).map (mulT K t)))
    rw [Gp_app, ih, leib_TT, Gp_cons' K (mulT K (tder K j t) s), Gp_cons' K (mulT K t (tder K j s))]
    show K.add (K.add (Gp K [mulT K (tder K j t) s]) (Gp K [mulT K t (tder K j s)])) _ = _
    lib_ring

/-- **Leibniz rule (at the level of Gp).** -/
theorem leib (j : Nat) (B : List (Term R)) : ∀ A : List (Term R),
    Gp K (dP K j (mulP K A B)) = K.add (Gp K (mulP K (dP K j A) B)) (Gp K (mulP K A (dP K j B)))
  | [] => by show K.zero = K.add K.zero K.zero; rw [K.zero_add]
  | t :: A => by
    show Gp K (dP K j (B.map (mulT K t) ++ mulP K A B)) = K.add (Gp K (B.map (mulT K (tder K j t)) ++ mulP K (dP K j A) B))
      (Gp K ((dP K j B).map (mulT K t) ++ mulP K A (dP K j B)))
    rw [dP_app, Gp_app, Gp_app, Gp_app, leib_TL, leib j B A]
    lib_ring

/-! ### the bilinear form Gp(A·B) and its symmetry -/

theorem addE_comm : ∀ α β : List Nat, addE α β = addE β α
  | [], [] => rfl
  | [], _ :: _ => rfl
  | _ :: _, [] => rfl
  | a :: α, b :: β => by show (a + b) :: addE α β = (b + a) :: addE β α; rw [Nat.add_comm, addE_comm α β]

/-- Σ_{s ∈ B} c_t c_s G(x^{α_t + α_s}) -/
def row (t : Term R) : List (Term R) → R
  | [] => K.zero
  | s :: B => K.add (K.mul (K.mul t.1 s.1) (cN K (GM (addE t.2 s.2)))) (row t B)

def Bf : List (Term R) → List (Term R) → R
  | [], _ => K.zero
  | t :: A, B => K.add (row K t B) (Bf A B)

theorem Gp_row (t : Term R) : ∀ B : List (Term R), Gp K (B.map (mulT K t)) = row K t B
  | [] => rfl
  | s :: B => by show K.add _ (Gp K (B.map (mulT K t))) = K.add _ (row K t B); rw [Gp_row t B]; rfl

theorem Gp_mulP (B : List (Term R)) : ∀ A : List (Term R), Gp K (mulP K A B) = Bf K A B
  | [] => rfl
  | t :: A => by
    show Gp K (B.map (mulT K t) ++ mulP K A B) = K.add (row K t B) (Bf K A B)
    rw [Gp_app, Gp_row, Gp_mulP B A]

/-- column sums: Σ_{t ∈ A} c_t c_s G(x^{α_t+α_s}) -/
def col (s : Term R) : List (Term R) → R
  | [] => K.zero
  | t :: A => K.add (K.mul (K.mul t.1 s.1) (cN K (GM (addE t.2 s.2)))) (col s A)

theorem Bf_cons_r (s : Term R) (B : List (Term R)) : ∀ A : List (Term R), Bf K A (s :: B) = K.add (col K s A) (Bf K A B)
  | [] => (K.zero_add _).symm
  | t :: A => by
    show K.add (K.add _ (row K t B)) (Bf K A (s :: B)) = K.add (K.add _ (col K s A)) (K.add (row K t B) (Bf K A B))
    rw [Bf_cons_r s B A]; lib_ring

theorem row_col (s : Term R) : ∀ A : List (Term R), col K s A = row K s A
  | [] => rfl
  | t :: A => by
    show K.add (K.mul (K.mul t.1 s.1) (cN K (GM (addE t.2 s.2)))) (col K s A)
      = K.add (K.mul (K.mul s.1 t.1) (cN K (GM (addE s.2 t.2)))) (row K s A)
    rw [row_col s A, addE_comm, K.mul_comm t.1]

/-- **Symmetry:** Gp(A·B) = Gp(B·A). -/
theorem Bf_comm : ∀ A B : List (Term R), Bf K A B = Bf K B A
  | A, [] => by
    induction A with
    | nil => rfl
    | cons t A ih => show K.add (row K t []) (Bf K A []) = K.zero; rw [ih]; show K.add K.zero K.zero = K.zero; rw [K.zero_add]
  | A, s :: B => by
    rw [Bf_cons_r, Bf_comm A B, row_col]; rfl

theorem addE_assoc : ∀ α β γ : List Nat, addE (addE α β) γ = addE α (addE β γ)
  | [], _, _ => rfl
  | a :: α, [], [] => rfl
  | a :: α, [], c :: γ => rfl
  | a :: α, b :: β, [] => rfl
  | a :: α, b :: β, c :: γ => by
    show (a + b + c) :: addE (addE α β) γ = (a + (b + c)) :: addE α (addE β γ)
    rw [Nat.add_assoc, addE_assoc α β γ]

theorem row_app (t : Term R) (L1 L2 : List (Term R)) : row K t (L1 ++ L2) = K.add (row K t L1) (row K t L2) := by
  induction L1 with
  | nil => exact (K.zero_add _).symm
  | cons s L1 ih => show K.add _ (row K t (L1 ++ L2)) = K.add (K.add _ (row K t L1)) _; rw [ih]; lib_ring

theorem Bf_app_l (A1 A2 B : List (Term R)) : Bf K (A1 ++ A2) B = K.add (Bf K A1 B) (Bf K A2 B) := by
  induction A1 with
  | nil => exact (K.zero_add _).symm
  | cons t A1 ih => show K.add _ (Bf K (A1 ++ A2) B) = K.add (K.add _ (Bf K A1 B)) _; rw [ih]; lib_ring

theorem row_map (t s : Term R) : ∀ C : List (Term R), row K t (C.map (mulT K s)) = row K (mulT K t s) C
  | [] => rfl
  | u :: C => by
    show K.add (K.mul (K.mul t.1 (K.mul s.1 u.1)) (cN K (GM (addE t.2 (addE s.2 u.2))))) (row K t (C.map (mulT K s)))
      = K.add (K.mul (K.mul (K.mul t.1 s.1) u.1) (cN K (GM (addE (addE t.2 s.2) u.2)))) (row K (mulT K t s) C)
    rw [row_map t s C, addE_assoc]
    congr 1; lib_ring

theorem row_mulP (t : Term R) (C : List (Term R)) : ∀ B : List (Term R),
    row K t (mulP K B C) = Bf K (B.map (mulT K t)) C
  | [] => rfl
  | s :: B => by
    show row K t (C.map (mulT K s) ++ mulP K B C) = K.add (row K (mulT K t s) C) (Bf K (B.map (mulT K t)) C)
    rw [row_app, row_map, row_mulP t C B]

/-- **Associativity:** Gp((A·B)·C) = Gp(A·(B·C)). -/
theorem Bf_assoc (B C : List (Term R)) : ∀ A : List (Term R), Bf K (mulP K A B) C = Bf K A (mulP K B C)
  | [] => rfl
  | t :: A => by
    show Bf K (B.map (mulT K t) ++ mulP K A B) C = K.add (row K t (mulP K B C)) (Bf K A (mulP K B C))
    rw [Bf_app_l, row_mulP, Bf_assoc B C A]

/-! ### test-equivalence and its congruence properties -/

/-- all exponent lists have length n -/
def LenP (n : Nat) : List (Term R) → Prop
  | [] => True
  | t :: p => t.2.length = n ∧ LenP n p

theorem lenP_app (n : Nat) : ∀ p q : List (Term R), LenP n p → LenP n q → LenP n (p ++ q)
  | [], _, _, hq => hq
  | _ :: p, q, hp, hq => ⟨hp.1, lenP_app n p q hp.2 hq⟩

theorem len_addE : ∀ α β : List Nat, α.length = β.length → (addE α β).length = α.length
  | [], [], _ => rfl
  | [], _ :: _, h => Nat.noConfusion h
  | _ :: _, [], h => Nat.noConfusion h
  | _ :: α, _ :: β, h => congrArg Nat.succ (len_addE α β (Nat.succ.inj h))

theorem lenP_map (n : Nat) (t : Term R) (ht : t.2.length = n) : ∀ B : List (Term R), LenP n B → LenP n (B.map (mulT K t))
  | [], _ => trivial
  | s :: B, h => ⟨by show (addE t.2 s.2).length = n; rw [len_addE _ _ (ht.trans h.1.symm), ht], lenP_map n t ht B h.2⟩

theorem lenP_mulP (n : Nat) (B : List (Term R)) (hB : LenP n B) : ∀ A : List (Term R), LenP n A → LenP n (mulP K A B)
  | [], _ => trivial
  | t :: A, h => lenP_app n _ _ (lenP_map K n t h.1 B hB) (lenP_mulP n B hB A h.2)

theorem len_incAt : ∀ (i : Nat) (α : List Nat), (incAt i α).length = α.length
  | 0, [] => rfl
  | _ + 1, [] => rfl
  | 0, _ :: _ => rfl
  | i + 1, _ :: α => congrArg Nat.succ (len_incAt i α)

theorem lenP_xi (n i : Nat) : ∀ p : List (Term R), LenP n p → LenP n (xi i p)
  | [], _ => trivial
  | t :: p, h => ⟨(len_incAt i t.2).trans h.1, lenP_xi n i p h.2⟩

theorem lenP_dP (n i : Nat) : ∀ p : List (Term R), LenP n p → LenP n (dP K i p)
  | [], _ => trivial
  | t :: p, h => ⟨(len_decAt i t.2).trans h.1, lenP_dP n i p h.2⟩

theorem wide_of_len (n i : Nat) (hi : i < n) : ∀ p : List (Term R), LenP n p → Wide i p
  | [], _ => trivial
  | t :: p, h => ⟨by rw [h.1]; exact hi, wide_of_len n i hi p h.2⟩

theorem inc_addE : ∀ (j : Nat) (α β : List Nat), j < β.length → α.length = β.length →
    incAt j (addE α β) = addE α (incAt j β)
  | _, _, [], h, _ => absurd h (Nat.not_lt_zero _)
  | _, [], _ :: _, _, h => Nat.noConfusion h
  | 0, a :: α, b :: β, _, _ => by show (a + b + 1) :: _ = (a + (b + 1)) :: _; rw [Nat.add_assoc]
  | j + 1, a :: α, b :: β, h, hl => by
    show (a + b) :: incAt j (addE α β) = (a + b) :: addE α (incAt j β)
    rw [inc_addE j α β (Nat.lt_of_succ_lt_succ h) (Nat.succ.inj hl)]

theorem xi_map (n j : Nat) (hj : j < n) (t : Term R) (ht : t.2.length = n) : ∀ B : List (Term R), LenP n B →
    xi j (B.map (mulT K t)) = (xi j B).map (mulT K t)
  | [], _ => rfl
  | s :: B, h => by
    show (K.mul t.1 s.1, incAt j (addE t.2 s.2)) :: xi j (B.map (mulT K t)) = (K.mul t.1 s.1, addE t.2 (incAt j s.2)) :: _
    rw [inc_addE j t.2 s.2 (by rw [h.1]; exact hj) (ht.trans h.1.symm), xi_map n j hj t ht B h.2]
    rfl

theorem xi_app (j : Nat) (L1 L2 : List (Term R)) : xi j (L1 ++ L2) = xi j L1 ++ xi j L2 := by
  induction L1 with
  | nil => rfl
  | cons x L1 ih => show _ :: xi j (L1 ++ L2) = _ :: (xi j L1 ++ xi j L2); rw [ih]

theorem xi_mulP (n j : Nat) (hj : j < n) (B : List (Term R)) (hB : LenP n B) : ∀ A : List (Term R), LenP n A →
    xi j (mulP K A B) = mulP K A (xi j B)
  | [], _ => rfl
  | t :: A, h => by
    show xi j (B.map (mulT K t) ++ mulP K A B) = (xi j B).map (mulT K t) ++ mulP K A (xi j B)
    rw [xi_app, xi_map K n j hj t h.1 B hB, xi_mulP n j hj B hB A h.2]

/-- **The derivative under the pairing:** Gp(∂_j p · r) = Gp(p · x_j r) − Gp(p · ∂_j r). -/
theorem dP_pair (n j : Nat) (hj : j < n) (p r : List (Term R)) (hp : LenP n p) (hr : LenP n r) :
    Bf K (dP K j p) r = K.add (Bf K p (xi j r)) (K.neg (Bf K p (dP K j r))) := by
  have l := leib K j r p
  have ibp := Gp_ibp K j (mulP K p r) (wide_of_len n j hj _ (lenP_mulP K n r hr p hp))
  rw [xi_mulP K n j hj r hr p hp, Gp_mulP] at ibp
  rw [← ibp, Gp_mulP, Gp_mulP] at l
  rw [← Gp_mulP, ← Gp_mulP K (dP K j r) p] at *
  rw [Gp_mulP, Gp_mulP] at l ⊢
  rw [l]; lib_ring

/-- test-equivalence among polynomials in n variables -/
def Eqv (n : Nat) (p q : List (Term R)) : Prop := ∀ r : List (Term R), LenP n r → Bf K p r = Bf K q r

theorem eqv_mul_r (n : Nat) (p q s : List (Term R)) (hs : LenP n s) (h : Eqv K n p q) :
    Eqv K n (mulP K p s) (mulP K q s) := fun r hr => by
  rw [Bf_assoc, Bf_assoc]; exact h _ (lenP_mulP K n r hr s hs)

theorem eqv_mul_l (n : Nat) (p q s : List (Term R)) (hs : LenP n s) (h : Eqv K n p q) :
    Eqv K n (mulP K s p) (mulP K s q) := fun r hr => by
  calc Bf K (mulP K s p) r = Bf K s (mulP K p r) := Bf_assoc K p r s
    _ = Bf K (mulP K p r) s := Bf_comm K _ _
    _ = Bf K p (mulP K r s) := Bf_assoc K r s p
    _ = Bf K q (mulP K r s) := h _ (lenP_mulP K n s hs r hr)
    _ = Bf K (mulP K q r) s := (Bf_assoc K r s q).symm
    _ = Bf K s (mulP K q r) := Bf_comm K _ _
    _ = Bf K (mulP K s q) r := (Bf_assoc K q r s).symm

theorem eqv_dP (n j : Nat) (hj : j < n) (p q : List (Term R)) (hp : LenP n p) (hq : LenP n q) (h : Eqv K n p q) :
    Eqv K n (dP K j p) (dP K j q) := fun r hr => by
  rw [dP_pair K n j hj p r hp hr, dP_pair K n j hj q r hq hr, h _ (lenP_xi n j r hr), h _ (lenP_dP K n j r hr)]

/-! ### Leibniz up to test-equivalence -/

theorem row_single_eq (c d e : R) (δ : List Nat) (h : e = K.add c d) : ∀ r : List (Term R),
    row K (e, δ) r = K.add (row K (c, δ) r) (row K (d, δ) r)
  | [] => (K.zero_add _).symm
  | u :: r => by
    show K.add (K.mul (K.mul e u.1) (cN K (GM (addE δ u.2)))) (row K (e, δ) r) = K.add (K.add (K.mul (K.mul c u.1) (cN K (GM (addE δ u.2)))) (row K (c, δ) r)) (K.add (K.mul (K.mul d u.1) (cN K (GM (addE δ u.2)))) (row K (d, δ) r))
    rw [row_single_eq c d e δ h r, h]; lib_ring

theorem row_zero_coef (δ : List Nat) : ∀ r : List (Term R), row K (K.zero, δ) r = K.zero
  | [] => rfl
  | u :: r => by
    show K.add (K.mul (K.mul K.zero u.1) _) (row K (K.zero, δ) r) = K.zero
    rw [row_zero_coef δ r, K.mul_comm K.zero, DetChain.mul_zero, K.mul_comm K.zero, DetChain.mul_zero, K.zero_add]

theorem row_coef (x y : R) (δ : List Nat) (h : x = y) (r : List (Term R)) : row K (x, δ) r = row K (y, δ) r := by rw [h]

/-- Leibniz for two terms, tested against any r -/
theorem leibT_row (j : Nat) (t s : Term R) (r : List (Term R)) :
    row K (K.mul (K.mul t.1 s.1) (cN K (nthE j (addE t.2 s.2))), decAt j (addE t.2 s.2)) r
      = K.add (row K (mulT K (tder K j t) s) r) (row K (mulT K t (tder K j s)) r) := by
  obtain ⟨c, α⟩ := t
  obtain ⟨d, β⟩ := s
  show row K (K.mul (K.mul c d) (cN K (nthE j (addE α β))), decAt j (addE α β)) r
    = K.add (row K (K.mul (K.mul c (cN K (nthE j α))) d, addE (decAt j α) β) r) (row K (K.mul c (K.mul d (cN K (nthE j β))), addE α (decAt j β)) r)
  rw [nth_addE, cN_add]
  have z0 : cN K 0 = K.zero := rfl
  generalize ea : nthE j α = A at *
  generalize eb : nthE j β = B at *
  cases Nat.lt_or_ge A 1 with
  | inl ha =>
    have za : A = 0 := Nat.le_antisymm (Nat.le_of_lt_succ ha) (Nat.zero_le _)
    subst za
    rw [z0, row_coef K (K.mul (K.mul c K.zero) d) K.zero _ (by lib_ring), row_zero_coef, K.zero_add]
    cases Nat.lt_or_ge B 1 with
    | inl hb =>
      have zb : B = 0 := Nat.le_antisymm (Nat.le_of_lt_succ hb) (Nat.zero_le _)
      subst zb
      rw [z0, row_coef K (K.mul (K.mul c d) K.zero) K.zero _ (by lib_ring),
        row_coef K (K.mul c (K.mul d K.zero)) K.zero _ (by lib_ring), row_zero_coef, row_zero_coef, K.zero_add]
    | inr hb =>
      rw [← dec_addE_r j α β (by rw [eb]; exact hb)]
      rw [K.zero_add]
      exact row_coef K (K.mul (K.mul c d) (cN K B)) (K.mul c (K.mul d (cN K B))) _ (by lib_ring) r
  | inr ha =>
    cases Nat.lt_or_ge B 1 with
    | inl hb =>
      have zb : B = 0 := Nat.le_antisymm (Nat.le_of_lt_succ hb) (Nat.zero_le _)
      subst zb
      rw [z0, row_coef K (K.mul c (K.mul d K.zero)) K.zero _ (by lib_ring), row_zero_coef, DetChain.add_zero',
        ← dec_addE_l j α β (by rw [ea]; exact ha)]
      rw [DetChain.add_zero']
      exact row_coef K (K.mul (K.mul c d) (cN K A)) (K.mul (K.mul c (cN K A)) d) _ (by lib_ring) r
    | inr hb =>
      rw [← dec_addE_l j α β (by rw [ea]; exact ha), ← dec_addE_r j α β (by rw [eb]; exact hb)]
      exact row_single_eq K (K.mul (K.mul c (cN K A)) d) (K.mul c (K.mul d (cN K B))) _ _ (by lib_ring) r

theorem Bf_single (t : Term R) (r : List (Term R)) : Bf K [t] r = row K t r := by
  show K.add (row K t r) K.zero = row K t r; rw [DetChain.add_zero']

theorem leibT_L (j : Nat) (t : Term R) (r : List (Term R)) : ∀ B : List (Term R),
    Bf K (dP K j (B.map (mulT K t))) r = K.add (Bf K (B.map (mulT K (tder K j t))) r) (Bf K ((dP K j B).map (mulT K t)) r)
  | [] => by show K.zero = K.add K.zero K.zero; rw [K.zero_add]
  | s :: B => by
    show K.add (row K _ r) (Bf K (dP K j (B.map (mulT K t))) r)
      = K.add (K.add (row K (mulT K (tder K j t) s) r) (Bf K (B.map (mulT K (tder K j t))) r))
          (K.add (row K (mulT K t (tder K j s)) r) (Bf K ((dP K j B).map (mulT K t)) r))
    rw [leibT_L j t r B]
    show K.add (row K (K.mul (K.mul t.1 s.1) (cN K (nthE j (addE t.2 s.2))), decAt j (addE t.2 s.2)) r) _ = _
    rw [leibT_row]
    generalize row K (mulT K (tder K j t) s) r = x1
    generalize row K (mulT K t (tder K j s)) r = x2
    generalize Bf K (List.map (mulT K (tder K j t)) B) r = x3
    generalize Bf K (List.map (mulT K t) (dP K j B)) r = x4
    lib_ring

/-- **Leibniz up to test-equivalence.** -/
theorem leib_eqv (n j : Nat) (A B : List (Term R)) :
    Eqv K n (dP K j (mulP K A B)) (mulP K (dP K j A) B ++ mulP K A (dP K j B)) := fun r _ => by
  rw [Bf_app_l]
  induction A with
  | nil => show K.zero = K.add K.zero K.zero; rw [K.zero_add]
  | cons t A ih =>
    show Bf K (dP K j (B.map (mulT K t) ++ mulP K A B)) r
      = K.add (Bf K (B.map (mulT K (tder K j t)) ++ mulP K (dP K j A) B) r) (Bf K ((dP K j B).map (mulT K t) ++ mulP K A (dP K j B)) r)
    rw [dP_app, Bf_app_l, Bf_app_l, Bf_app_l, leibT_L, ih]; lib_ring

/-! ### the algebra of test-equivalence -/

theorem eqv_refl (n : Nat) (p : List (Term R)) : Eqv K n p p := fun _ _ => rfl
theorem eqv_symm (n : Nat) (p q : List (Term R)) (h : Eqv K n p q) : Eqv K n q p := fun r hr => (h r hr).symm
theorem eqv_trans (n : Nat) (p q s : List (Term R)) (h1 : Eqv K n p q) (h2 : Eqv K n q s) : Eqv K n p s :=
  fun r hr => (h1 r hr).trans (h2 r hr)

theorem eqv_app (n : Nat) (p q p' q' : List (Term R)) (h : Eqv K n p q) (h' : Eqv K n p' q') :
    Eqv K n (p ++ p') (q ++ q') := fun r hr => by rw [Bf_app_l, Bf_app_l, h r hr, h' r hr]

def scaleT (c : R) (p : List (Term R)) : List (Term R) := p.map (fun s => (K.mul c s.1, s.2))

theorem row_scale (c : R) (t : Term R) : ∀ r : List (Term R), row K (K.mul c t.1, t.2) r = K.mul c (row K t r)
  | [] => by show K.zero = K.mul c K.zero; rw [DetChain.mul_zero]
  | u :: r => by
    show K.add (K.mul (K.mul (K.mul c t.1) u.1) (cN K (GM (addE t.2 u.2)))) (row K (K.mul c t.1, t.2) r)
      = K.mul c (K.add (K.mul (K.mul t.1 u.1) (cN K (GM (addE t.2 u.2)))) (row K t r))
    rw [row_scale c t r]; lib_ring

theorem Bf_scale (c : R) (r : List (Term R)) : ∀ p : List (Term R), Bf K (scaleT K c p) r = K.mul c (Bf K p r)
  | [] => by show K.zero = K.mul c K.zero; rw [DetChain.mul_zero]
  | t :: p => by
    show K.add (row K (K.mul c t.1, t.2) r) (Bf K (scaleT K c p) r) = K.mul c (K.add (row K t r) (Bf K p r))
    rw [row_scale, Bf_scale c r p, K.distrib]

theorem eqv_scale (n : Nat) (c : R) (p q : List (Term R)) (h : Eqv K n p q) : Eqv K n (scaleT K c p) (scaleT K c q) :=
  fun r hr => by rw [Bf_scale, Bf_scale, h r hr]

/-- row of a product term is symmetric -/
theorem row_mulT_comm (t s : Term R) : ∀ r : List (Term R), row K (mulT K t s) r = row K (mulT K s t) r
  | [] => rfl
  | u :: r => by
    show K.add (K.mul (K.mul (K.mul t.1 s.1) u.1) (cN K (GM (addE (addE t.2 s.2) u.2)))) (row K (mulT K t s) r)
      = K.add (K.mul (K.mul (K.mul s.1 t.1) u.1) (cN K (GM (addE (addE s.2 t.2) u.2)))) (row K (mulT K s t) r)
    rw [row_mulT_comm t s r, addE_comm t.2 s.2, K.mul_comm t.1 s.1]

theorem Bf_map_mulT (t : Term R) (r : List (Term R)) : ∀ B : List (Term R),
    Bf K (B.map (mulT K t)) r = Bf K (B.map (fun s => mulT K s t)) r
  | [] => rfl
  | s :: B => by
    show K.add (row K (mulT K t s) r) _ = K.add (row K (mulT K s t) r) _
    rw [row_mulT_comm, Bf_map_mulT t r B]

/-- Σ_{a ∈ A} Σ_{b ∈ B} f(a, b) = Σ_{b ∈ B} Σ_{a ∈ A} f(a, b) for f = row of the product term -/
theorem Bf_mulP_swap (r : List (Term R)) : ∀ A B : List (Term R),
    Bf K (mulP K A B) r = Bf K (mulP K B A) r
  | [], B => by
    show K.zero = Bf K (mulP K B []) r
    induction B with
    | nil => rfl
    | cons s B ih => show K.zero = Bf K (mulP K B []) r; exact ih
  | t :: A, B => by
    show Bf K (B.map (mulT K t) ++ mulP K A B) r = Bf K (mulP K B (t :: A)) r
    rw [Bf_app_l, Bf_mulP_swap r A B]
    -- Bf (B.map (mulT t)) r + Bf (mulP B A) r = Bf (mulP B (t :: A)) r
    clear Bf_mulP_swap
    induction B with
    | nil => show K.add K.zero K.zero = K.zero; rw [K.zero_add]
    | cons s B ih =>
      show K.add (K.add (row K (mulT K t s) r) (Bf K (B.map (mulT K t)) r)) (Bf K ((A.map (mulT K s)) ++ mulP K B A) r)
        = Bf K ((t :: A).map (mulT K s) ++ mulP K B (t :: A)) r
      rw [Bf_app_l, Bf_app_l, ← ih]
      show _ = K.add (K.add (row K (mulT K s t) r) (Bf K (A.map (mulT K s)) r)) _
      rw [row_mulT_comm]; lib_ring

theorem eqv_mul_comm (n : Nat) (A B : List (Term R)) : Eqv K n (mulP K A B) (mulP K B A) := fun r _ => Bf_mulP_swap K r A B

theorem mulT_assoc (x y z : Term R) : mulT K (mulT K x y) z = mulT K x (mulT K y z) := by
  show (K.mul (K.mul x.1 y.1) z.1, addE (addE x.2 y.2) z.2) = (K.mul x.1 (K.mul y.1 z.1), addE x.2 (addE y.2 z.2))
  rw [K.mul_assoc, addE_assoc]

theorem map_app' {α β : Type} (f : α → β) : ∀ L1 L2 : List α, (L1 ++ L2).map f = L1.map f ++ L2.map f
  | [], _ => rfl
  | x :: L1, L2 => by show f x :: (L1 ++ L2).map f = f x :: (L1.map f ++ L2.map f); rw [map_app' f L1 L2]

theorem app_assoc' {α : Type} : ∀ L1 L2 L3 : List α, (L1 ++ L2) ++ L3 = L1 ++ (L2 ++ L3)
  | [], _, _ => rfl
  | x :: L1, L2, L3 => by show x :: ((L1 ++ L2) ++ L3) = x :: (L1 ++ (L2 ++ L3)); rw [app_assoc' L1 L2 L3]

theorem mulP_app_l (L1 L2 Z : List (Term R)) : mulP K (L1 ++ L2) Z = mulP K L1 Z ++ mulP K L2 Z := by
  induction L1 with
  | nil => rfl
  | cons x L1 ih => show Z.map (mulT K x) ++ mulP K (L1 ++ L2) Z = (Z.map (mulT K x) ++ mulP K L1 Z) ++ mulP K L2 Z; rw [ih, app_assoc']

theorem map_mulT_map (x y : Term R) : ∀ Z : List (Term R), (Z.map (mulT K y)).map (mulT K x) = Z.map (mulT K (mulT K x y))
  | [] => rfl
  | z :: Z => by
    show mulT K x (mulT K y z) :: _ = mulT K (mulT K x y) z :: _
    rw [mulT_assoc, map_mulT_map x y Z]

theorem mulP_map (x : Term R) (Z : List (Term R)) : ∀ Y : List (Term R), mulP K (Y.map (mulT K x)) Z = (mulP K Y Z).map (mulT K x)
  | [] => rfl
  | y :: Y => by
    show Z.map (mulT K (mulT K x y)) ++ mulP K (Y.map (mulT K x)) Z = (Z.map (mulT K y) ++ mulP K Y Z).map (mulT K x)
    rw [map_app', map_mulT_map, mulP_map x Z Y]

/-- **The product of term lists is associative, as lists.** -/
theorem mulP_assoc (Y Z : List (Term R)) : ∀ X : List (Term R), mulP K (mulP K X Y) Z = mulP K X (mulP K Y Z)
  | [] => rfl
  | x :: X => by
    show mulP K (Y.map (mulT K x) ++ mulP K X Y) Z = (mulP K Y Z).map (mulT K x) ++ mulP K X (mulP K Y Z)
    rw [mulP_app_l, mulP_map, mulP_assoc Y Z X]

theorem eqv_mul_assoc (n : Nat) (A B C : List (Term R)) : Eqv K n (mulP K (mulP K A B) C) (mulP K A (mulP K B C)) := by
  rw [mulP_assoc]; exact eqv_refl K n _

end GaussPoly

#print axioms GaussPoly.Gp_ibp
#print axioms GaussPoly.unique_mono
#print axioms GaussPoly.leib_TT
#print axioms GaussPoly.leib
#print axioms GaussPoly.Bf_comm
#print axioms GaussPoly.Bf_assoc
#print axioms GaussPoly.dP_pair
#print axioms GaussPoly.eqv_dP
#print axioms GaussPoly.eqv_mul_l
#print axioms GaussPoly.leib_eqv
#print axioms GaussPoly.eqv_mul_comm
#print axioms GaussPoly.eqv_mul_assoc

ym/GaussSubst.lean

/-
  ym/GaussSubst — T45, H0 step 4: linear forms, their powers and derivatives (up to test-equivalence).

  n variables, a matrix O (entries O k j). Unit exponents e_j = incAt j 0, the constant polynomial one = [(1, 0)],
    lin O k  = Σ_{j<n} O_kj x_j,   powP L a = L^a.
    dP_unit         ∂_j x_m = δ_{jm}
    dP_lin          ∂_j L_k ≡ O_kj · 1
    scale_mulP      (c·A)·B = c·(A·B)
    dP_pow          ∂_j L^{a+1} ≡ (a+1) · L^a · ∂_j L
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GaussPoly

namespace GaussSubst

open SU2Char Gauss GaussPoly

variable {R : Type} (K : DetChain.CRing R)

theorem nth_zeros : ∀ (j n : Nat), nthE j (zerosL n) = 0
  | 0, 0 => rfl
  | _ + 1, 0 => rfl
  | 0, _ + 1 => rfl
  | j + 1, n + 1 => nth_zeros j n

theorem len_zeros : ∀ n, (zerosL n).length = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (len_zeros n)

/-- x_m as an exponent list -/
def unitE (n m : Nat) : List Nat := incAt m (zerosL n)

theorem nth_unit_self : ∀ (n m : Nat), m < n → nthE m (unitE n m) = 1
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | _ + 1, 0, _ => rfl
  | n + 1, m + 1, h => nth_unit_self n m (Nat.lt_of_succ_lt_succ h)

theorem nth_unit_ne : ∀ (n m j : Nat), j ≠ m → nthE j (unitE n m) = 0
  | 0, 0, 0, h => absurd rfl h
  | 0, 0, _ + 1, _ => rfl
  | 0, _ + 1, 0, _ => rfl
  | 0, _ + 1, _ + 1, _ => rfl
  | _ + 1, 0, 0, h => absurd rfl h
  | n + 1, 0, j + 1, _ => nth_zeros j n
  | n + 1, m + 1, 0, _ => rfl
  | n + 1, m + 1, j + 1, h => nth_unit_ne n m j (fun e => h (by rw [e]))

theorem dec_unit_self : ∀ (n m : Nat), m < n → decAt m (unitE n m) = zerosL n
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | _ + 1, 0, _ => rfl
  | n + 1, m + 1, h => by
    show 0 :: decAt m (unitE n m) = 0 :: zerosL n
    rw [dec_unit_self n m (Nat.lt_of_succ_lt_succ h)]

def one (n : Nat) : List (Term R) := [(K.one, zerosL n)]

def linF (n : Nat) (O : Nat → Nat → R) (k : Nat) : Nat → List (Term R)
  | 0 => []
  | m + 1 => linF n O k m ++ [(O k m, unitE n m)]

def lin (n : Nat) (O : Nat → Nat → R) (k : Nat) : List (Term R) := linF n O k n

def powP (n : Nat) (L : List (Term R)) : Nat → List (Term R)
  | 0 => one K n
  | a + 1 => mulP K (powP n L a) L

theorem app_nil {α : Type} : ∀ L : List α, L ++ [] = L
  | [] => rfl
  | x :: L => by show x :: (L ++ []) = x :: L; rw [app_nil L]

theorem eqv_zero_term (n : Nat) (δ : List Nat) : Eqv K n [(K.zero, δ)] [] := fun r _ => by
  rw [Bf_single, row_zero_coef]; rfl

theorem dP_app' (j : Nat) (L1 L2 : List (Term R)) : dP K j (L1 ++ L2) = dP K j L1 ++ dP K j L2 := dP_app K j L1 L2

/-- **∂_j of the m-term partial linear form.** -/
theorem dP_linF (n : Nat) (O : Nat → Nat → R) (k j : Nat) (hj : j < n) : ∀ m, m ≤ n →
    Eqv K n (dP K j (linF n O k m)) (if j < m then scaleT K (O k j) (one K n) else [])
  | 0, _ => by
    show Eqv K n [] (if j < 0 then _ else [])
    rw [MaskSum.ineg (Nat.not_lt_zero j)]; exact eqv_refl K n _
  | m + 1, hm => by
    show Eqv K n (dP K j (linF n O k m ++ [(O k m, unitE n m)])) _
    rw [dP_app']
    have ih := dP_linF n O k j hj m (Nat.le_of_succ_le hm)
    by_cases e : j = m
    · -- the new term is ∂_j x_j = 1
      have nl : ¬ j < m := by rw [e]; exact Nat.lt_irrefl m
      rw [MaskSum.ineg nl] at ih
      rw [MaskSum.ip (show j < m + 1 by rw [e]; exact Nat.lt_succ_self m)]
      have t : dP K j [(O k m, unitE n m)] = scaleT K (O k j) (one K n) := by
        show [(K.mul (O k m) (cN K (nthE j (unitE n m))), decAt j (unitE n m))] = [(K.mul (O k j) K.one, zerosL n)]
        rw [e, nth_unit_self n m (by rw [← e]; exact hj), dec_unit_self n m (by rw [← e]; exact hj), SU2CharExp.cN_one]
      rw [t]
      exact eqv_app K n _ _ _ _ ih (eqv_refl K n _)
    · have t : Eqv K n (dP K j [(O k m, unitE n m)]) [] := by
        show Eqv K n [(K.mul (O k m) (cN K (nthE j (unitE n m))), decAt j (unitE n m))] []
        rw [nth_unit_ne n m j e, show cN K 0 = K.zero from rfl, DetChain.mul_zero]
        exact eqv_zero_term K n _
      have h2 := eqv_app K n _ _ _ _ ih t
      by_cases hl : j < m
      · rw [MaskSum.ip hl] at h2; rw [MaskSum.ip (Nat.lt_succ_of_lt hl)]
        refine eqv_trans K n _ _ _ h2 ?_
        rw [app_nil]; exact eqv_refl K n _
      · rw [MaskSum.ineg hl] at h2
        rw [MaskSum.ineg (fun h => hl (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) e))]
        exact h2

/-- **∂_j L_k ≡ O_kj.** -/
theorem dP_lin (n : Nat) (O : Nat → Nat → R) (k j : Nat) (hj : j < n) :
    Eqv K n (dP K j (lin n O k)) (scaleT K (O k j) (one K n)) := by
  have := dP_linF K n O k j hj n (Nat.le_refl n)
  rw [MaskSum.ip hj] at this; exact this

theorem len_unit (n m : Nat) : (unitE n m).length = n := (len_incAt m (zerosL n)).trans (len_zeros n)

theorem lenP_one (n : Nat) : LenP n (one K n) := ⟨len_zeros n, trivial⟩

theorem lenP_linF (n : Nat) (O : Nat → Nat → R) (k : Nat) : ∀ m, LenP n (linF n O k m)
  | 0 => trivial
  | m + 1 => lenP_app n _ _ (lenP_linF n O k m) ⟨len_unit n m, trivial⟩

theorem lenP_pow (n : Nat) (L : List (Term R)) (hL : LenP n L) : ∀ a, LenP n (powP K n L a)
  | 0 => lenP_one K n
  | a + 1 => lenP_mulP K n L hL _ (lenP_pow n L hL a)

theorem lenP_scale (n : Nat) (c : R) : ∀ p : List (Term R), LenP n p → LenP n (scaleT K c p)
  | [], _ => trivial
  | _ :: p, h => ⟨h.1, lenP_scale n c p h.2⟩

theorem scale_map (c : R) (t : Term R) : ∀ B : List (Term R), B.map (mulT K (K.mul c t.1, t.2)) = scaleT K c (B.map (mulT K t))
  | [] => rfl
  | s :: B => by
    show (K.mul (K.mul c t.1) s.1, addE t.2 s.2) :: _ = (K.mul c (K.mul t.1 s.1), addE t.2 s.2) :: _
    rw [K.mul_assoc, scale_map c t B]
    rfl

theorem scale_app (c : R) (L1 L2 : List (Term R)) : scaleT K c (L1 ++ L2) = scaleT K c L1 ++ scaleT K c L2 :=
  map_app' _ L1 L2

/-- **(c·A)·B = c·(A·B).** -/
theorem scale_mulP (c : R) (B : List (Term R)) : ∀ A : List (Term R), mulP K (scaleT K c A) B = scaleT K c (mulP K A B)
  | [] => rfl
  | t :: A => by
    show B.map (mulT K (K.mul c t.1, t.2)) ++ mulP K (scaleT K c A) B = scaleT K c (B.map (mulT K t) ++ mulP K A B)
    rw [scale_map, scale_mulP c B A, scale_app]

theorem eqv_scale_one (n : Nat) (p : List (Term R)) : Eqv K n (scaleT K K.one p) p := fun r _ => by
  rw [Bf_scale, K.one_mul]

theorem eqv_add_scale (n : Nat) (c : R) (p : List (Term R)) : Eqv K n (scaleT K c p ++ p) (scaleT K (K.add c K.one) p) :=
  fun r _ => by rw [Bf_app_l, Bf_scale, Bf_scale]; lib_ring

theorem dP_one (n j : Nat) : Eqv K n (dP K j (one K n)) [] := by
  show Eqv K n [(K.mul K.one (cN K (nthE j (zerosL n))), decAt j (zerosL n))] []
  rw [nth_zeros, show cN K 0 = K.zero from rfl, DetChain.mul_zero]
  exact eqv_zero_term K n _

theorem mulP_nil (B : List (Term R)) : mulP K [] B = [] := rfl

/-- **∂_j L^{a+1} ≡ (a+1) · L^a · ∂_j L.** -/
theorem dP_pow (n j : Nat) (L : List (Term R)) (hL : LenP n L) : ∀ a,
    Eqv K n (dP K j (powP K n L (a + 1))) (scaleT K (cN K (a + 1)) (mulP K (powP K n L a) (dP K j L)))
  | 0 => by
    show Eqv K n (dP K j (mulP K (one K n) L)) _
    refine eqv_trans K n _ _ _ (leib_eqv K n j (one K n) L) ?_
    have h1 : Eqv K n (mulP K (dP K j (one K n)) L) [] := by
      have := eqv_mul_r K n _ [] L hL (dP_one K n j)
      rw [mulP_nil] at this; exact this
    have h2 := eqv_app K n _ _ _ _ h1 (eqv_refl K n (mulP K (one K n) (dP K j L)))
    refine eqv_trans K n _ _ _ h2 ?_
    show Eqv K n (mulP K (one K n) (dP K j L)) (scaleT K (K.add K.one K.zero) (mulP K (one K n) (dP K j L)))
    rw [DetChain.add_zero']
    exact eqv_symm K n _ _ (eqv_scale_one K n _)
  | a + 1 => by
    have ih := dP_pow n j L hL a
    let P := powP K n L a
    let dL := dP K j L
    have hP := lenP_pow K n L hL a
    have hdL := lenP_dP K n j L hL
    show Eqv K n (dP K j (mulP K (powP K n L (a + 1)) L)) _
    refine eqv_trans K n _ _ _ (leib_eqv K n j _ L) ?_
    -- first summand
    have s1 : Eqv K n (mulP K (dP K j (powP K n L (a + 1))) L) (scaleT K (cN K (a + 1)) (mulP K (powP K n L (a + 1)) dL)) := by
      refine eqv_trans K n _ _ _ (eqv_mul_r K n _ _ L hL ih) ?_
      rw [scale_mulP]
      refine eqv_scale K n _ _ _ ?_
      refine eqv_trans K n _ _ _ (eqv_mul_assoc K n P dL L) ?_
      refine eqv_trans K n _ _ _ (eqv_mul_l K n _ _ P hP (eqv_mul_comm K n dL L)) ?_
      exact eqv_symm K n _ _ (eqv_mul_assoc K n P L dL)
    refine eqv_trans K n _ _ _ (eqv_app K n _ _ _ _ s1 (eqv_refl K n _)) ?_
    have := eqv_add_scale K n (cN K (a + 1)) (mulP K (powP K n L (a + 1)) dL)
    have e : cN K (a + 1 + 1) = K.add (cN K (a + 1)) K.one := by
      show K.add K.one (cN K (a + 1)) = _; rw [K.add_comm]
    rw [e]; exact this

/-! ### substitution of monomials and the chain rule -/

/-- x^α ↦ Π_i L_{k+i}^{α_i} -/
def substM (n : Nat) (O : Nat → Nat → R) : Nat → List Nat → List (Term R)
  | _, [] => one K n
  | k, a :: α => mulP K (powP K n (lin n O k) a) (substM n O (k + 1) α)

theorem lenP_substM (n : Nat) (O : Nat → Nat → R) : ∀ (k : Nat) (α : List Nat), LenP n (substM K n O k α)
  | _, [] => lenP_one K n
  | k, a :: α => lenP_mulP K n _ (lenP_substM n O (k + 1) α) _ (lenP_pow K n _ (lenP_linF n O k n) a)

theorem scale_scale (c d : R) (p : List (Term R)) : scaleT K c (scaleT K d p) = scaleT K (K.mul c d) p := by
  induction p with
  | nil => rfl
  | cons t p ih => show (K.mul c (K.mul d t.1), t.2) :: scaleT K c (scaleT K d p) = (K.mul (K.mul c d) t.1, t.2) :: scaleT K (K.mul c d) p; rw [ih, K.mul_assoc]

theorem map_mulT_scale (c : R) (t : Term R) : ∀ B : List (Term R), (scaleT K c B).map (mulT K t) = scaleT K c (B.map (mulT K t))
  | [] => rfl
  | s :: B => by
    show (K.mul t.1 (K.mul c s.1), addE t.2 s.2) :: (scaleT K c B).map (mulT K t)
      = (K.mul c (K.mul t.1 s.1), addE t.2 s.2) :: scaleT K c (B.map (mulT K t))
    rw [map_mulT_scale c t B]
    have : K.mul t.1 (K.mul c s.1) = K.mul c (K.mul t.1 s.1) := by lib_ring
    rw [this]

/-- A·(c·B) = c·(A·B) -/
theorem mulP_scale_r (c : R) (B : List (Term R)) : ∀ A : List (Term R), mulP K A (scaleT K c B) = scaleT K c (mulP K A B)
  | [] => rfl
  | t :: A => by
    show (scaleT K c B).map (mulT K t) ++ mulP K A (scaleT K c B) = scaleT K c (B.map (mulT K t) ++ mulP K A B)
    rw [map_mulT_scale, mulP_scale_r c B A, scale_app]

theorem eqv_scale_zero (n : Nat) (p : List (Term R)) : Eqv K n (scaleT K K.zero p) [] := fun r _ => by
  rw [Bf_scale, K.mul_comm, DetChain.mul_zero]; rfl

theorem addE_zeros : ∀ (α : List Nat), addE α (zerosL α.length) = α
  | [] => rfl
  | a :: α => by show (a + 0) :: addE α (zerosL α.length) = a :: α; rw [addE_zeros α]; rfl

theorem mulP_one (n : Nat) : ∀ p : List (Term R), LenP n p → mulP K p (one K n) = scaleT K K.one p
  | [], _ => rfl
  | t :: p, h => by
    show (K.mul t.1 K.one, addE t.2 (zerosL n)) :: mulP K p (one K n) = (K.mul K.one t.1, t.2) :: scaleT K K.one p
    rw [show addE t.2 (zerosL n) = t.2 by rw [← h.1, addE_zeros], mulP_one n p h.2, K.mul_comm t.1 K.one]

/-- the chain-rule right-hand side Σ_i O_{k+i,j} α_i · substM k (α − e_i) -/
def chainT (n : Nat) (O : Nat → Nat → R) (j : Nat) : Nat → List Nat → List (Term R)
  | _, [] => []
  | k, a :: α => scaleT K (K.mul (O k j) (cN K a)) (substM K n O k ((a - 1) :: α)) ++
      mulP K (powP K n (lin n O k) a) (chainT n O j (k + 1) α)

/-- **Chain rule** ∂_j (x^α ∘ O) ≡ Σ_i O_{k+i,j} α_i (x^{α−e_i} ∘ O). -/
theorem chain (n : Nat) (O : Nat → Nat → R) (j : Nat) (hj : j < n) :
    ∀ (k : Nat) (α : List Nat), Eqv K n (dP K j (substM K n O k α)) (chainT K n O j k α)
  | _, [] => dP_one K n j
  | k, a :: α => by
    have ih := chain n O j hj (k + 1) α
    let L := lin n O k
    have hL : LenP n L := lenP_linF n O k n
    let S := substM K n O (k + 1) α
    have hS : LenP n S := lenP_substM K n O (k + 1) α
    show Eqv K n (dP K j (mulP K (powP K n L a) S)) _
    refine eqv_trans K n _ _ _ (leib_eqv K n j _ S) ?_
    refine eqv_app K n _ _ _ _ ?_ (eqv_mul_l K n _ _ _ (lenP_pow K n L hL a) ih)
    cases a with
    | zero =>
      have h1 : Eqv K n (mulP K (dP K j (one K n)) S) [] := by
        have := eqv_mul_r K n _ [] S hS (dP_one K n j); rw [mulP_nil] at this; exact this
      refine eqv_trans K n _ _ _ h1 (eqv_symm K n _ _ ?_)
      show Eqv K n (scaleT K (K.mul (O k j) K.zero) _) []
      rw [DetChain.mul_zero]; exact eqv_scale_zero K n _
    | succ b =>
      -- ∂_j L^{b+1} · S ≡ (b+1) L^b (O_kj · 1) S ≡ O_kj (b+1) L^b S
      have d1 := eqv_mul_r K n _ _ S hS (dP_pow K n j L hL b)
      refine eqv_trans K n _ _ _ d1 ?_
      rw [scale_mulP]
      have d2 : Eqv K n (mulP K (mulP K (powP K n L b) (dP K j L)) S)
          (mulP K (mulP K (powP K n L b) (scaleT K (O k j) (one K n))) S) :=
        eqv_mul_r K n _ _ S hS (eqv_mul_l K n _ _ _ (lenP_pow K n L hL b) (dP_lin K n O k j hj))
      refine eqv_trans K n _ _ _ (eqv_scale K n _ _ _ d2) ?_
      rw [mulP_scale_r, mulP_one K n _ (lenP_pow K n L hL b), scale_mulP, scale_mulP, scale_scale, scale_scale]
      show Eqv K n (scaleT K (K.mul (K.mul (cN K (b + 1)) (O k j)) K.one) (mulP K (powP K n L b) S))
        (scaleT K (K.mul (O k j) (cN K (b + 1))) (mulP K (powP K n L b) S))
      have e : K.mul (K.mul (cN K (b + 1)) (O k j)) K.one = K.mul (O k j) (cN K (b + 1)) := by lib_ring
      rw [e]; exact eqv_refl K n _

end GaussSubst

#print axioms GaussSubst.dP_lin
#print axioms GaussSubst.dP_pow
#print axioms GaussSubst.chain

ym/GaussRot.lean

/-
  ym/GaussRot — T45, H0 step 6: rotation invariance of the Gaussian moment functional.

  For a matrix O with orthonormal rows (Σ_m O_im O_lm = δ_il, i, l < n) and the substitution x ↦ Ox,
    Gp_lin        G(L_i · S) = Σ_m O_im G(x_m · S)
    mix_chain     Σ_m O_im (chain-rule sum for ∂_m) ≡ the δ-collapsed sum           (orthogonality)
    rot_ibp       G(x_i x^α ∘ O) = α_i G(x^{α−e_i} ∘ O)                            (IBP survives the substitution)
    rot_inv       G(x^α ∘ O) = G(x^α)                                               (uniqueness, unique_mono)
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GaussSubst

namespace GaussRot

open SU2Char Gauss GaussPoly GaussSubst

variable {R : Type} (K : DetChain.CRing R)

def sumK (f : Nat → R) : Nat → R
  | 0 => K.zero
  | m + 1 => K.add (sumK f m) (f m)

theorem sumK_ext (f g : Nat → R) (h : ∀ x, f x = g x) : ∀ m, sumK K f m = sumK K g m
  | 0 => rfl
  | m + 1 => by show K.add (sumK K f m) (f m) = K.add (sumK K g m) (g m); rw [sumK_ext f g h m, h m]

theorem sumK_extB (f g : Nat → R) : ∀ m, (∀ x, x < m → f x = g x) → sumK K f m = sumK K g m
  | 0, _ => rfl
  | m + 1, h => by
    show K.add (sumK K f m) (f m) = K.add (sumK K g m) (g m)
    rw [sumK_extB f g m (fun x hx => h x (Nat.lt_succ_of_lt hx)), h m (Nat.lt_succ_self m)]

theorem sumK_add (f g : Nat → R) : ∀ m, sumK K (fun x => K.add (f x) (g x)) m = K.add (sumK K f m) (sumK K g m)
  | 0 => by show K.zero = K.add K.zero K.zero; rw [K.zero_add]
  | m + 1 => by
    show K.add (sumK K (fun x => K.add (f x) (g x)) m) (K.add (f m) (g m)) = K.add (K.add (sumK K f m) (f m)) (K.add (sumK K g m) (g m))
    rw [sumK_add f g m]
    generalize sumK K f m = a; generalize sumK K g m = b; generalize f m = c; generalize g m = d
    lib_ring

theorem sumK_mul (f : Nat → R) (c : R) : ∀ m, sumK K (fun x => K.mul (f x) c) m = K.mul (sumK K f m) c
  | 0 => by show K.zero = K.mul K.zero c; rw [K.mul_comm, DetChain.mul_zero]
  | m + 1 => by
    show K.add (sumK K (fun x => K.mul (f x) c) m) (K.mul (f m) c) = K.mul (K.add (sumK K f m) (f m)) c
    rw [sumK_mul f c m]
    generalize sumK K f m = a; generalize f m = b
    lib_ring

/-- orthonormal rows -/
def Orth (n : Nat) (O : Nat → Nat → R) : Prop :=
  ∀ i l, i < n → l < n → sumK K (fun m => K.mul (O i m) (O l m)) n = if i = l then K.one else K.zero

theorem Gp_scale (c : R) : ∀ p : List (Term R), Gp K (scaleT K c p) = K.mul c (Gp K p)
  | [] => by show K.zero = K.mul c K.zero; rw [DetChain.mul_zero]
  | t :: p => by
    show K.add (K.mul (K.mul c t.1) (cN K (GM t.2))) (Gp K (scaleT K c p)) = K.mul c (K.add (K.mul t.1 (cN K (GM t.2))) (Gp K p))
    rw [Gp_scale c p]; lib_ring

theorem Gp_eq_Bf (n : Nat) (p : List (Term R)) (hp : LenP n p) : Gp K p = Bf K p (one K n) := by
  rw [← Gp_mulP, mulP_one K n p hp, Gp_scale, K.one_mul]

theorem Bf_mulP_left (P X r : List (Term R)) : Bf K (mulP K P X) r = Bf K X (mulP K P r) := by
  calc Bf K (mulP K P X) r = Bf K (mulP K X P) r := Bf_mulP_swap K r P X
    _ = Bf K X (mulP K P r) := Bf_assoc K P r X

/-! ### the substituted linear form against a polynomial -/

theorem addE_zeros_l : ∀ (β : List Nat), addE (zerosL β.length) β = β
  | [] => rfl
  | b :: β => by show (0 + b) :: addE (zerosL β.length) β = b :: β; rw [addE_zeros_l β, Nat.zero_add]

theorem addE_unit (n m : Nat) (hm : m < n) (β : List Nat) (hb : β.length = n) : addE (unitE n m) β = incAt m β := by
  show addE (incAt m (zerosL n)) β = incAt m β
  rw [addE_comm, ← inc_addE m β (zerosL n) (by rw [len_zeros]; exact hm) (by rw [len_zeros]; exact hb), ← hb, addE_comm,
    addE_zeros_l]

theorem Gp_map_unit (n m : Nat) (hm : m < n) (c : R) : ∀ S : List (Term R), LenP n S →
    Gp K (S.map (mulT K (c, unitE n m))) = K.mul c (Gp K (xi m S))
  | [], _ => by show K.zero = K.mul c K.zero; rw [DetChain.mul_zero]
  | t :: S, h => by
    show K.add (K.mul (K.mul c t.1) (cN K (GM (addE (unitE n m) t.2)))) (Gp K (S.map (mulT K (c, unitE n m))))
      = K.mul c (K.add (K.mul t.1 (cN K (GM (incAt m t.2)))) (Gp K (xi m S)))
    rw [addE_unit n m hm t.2 h.1, Gp_map_unit n m hm c S h.2]; lib_ring

theorem Gp_linF (n : Nat) (O : Nat → Nat → R) (i : Nat) (S : List (Term R)) (hS : LenP n S) :
    ∀ m, m ≤ n → Gp K (mulP K (linF n O i m) S) = sumK K (fun x => K.mul (O i x) (Gp K (xi x S))) m
  | 0, _ => rfl
  | m + 1, hm => by
    show Gp K (mulP K (linF n O i m ++ [(O i m, unitE n m)]) S) = K.add (sumK K _ m) (K.mul (O i m) (Gp K (xi m S)))
    rw [mulP_app_l, Gp_app, Gp_linF n O i S hS m (Nat.le_of_succ_le hm)]
    show _ = K.add _ _
    have e : Gp K (mulP K [(O i m, unitE n m)] S) = K.mul (O i m) (Gp K (xi m S)) := by
      show Gp K (S.map (mulT K (O i m, unitE n m)) ++ []) = _
      rw [Gp_app, Gp_map_unit K n m hm (O i m) S hS]
      show K.add _ K.zero = _; rw [DetChain.add_zero']
    rw [e]

/-! ### orthogonality collapses the chain-rule sums -/

/-- the δ-collapsed sum: α_{i−k} · x^{α−e_{i−k}} ∘ O -/
def tgt (n : Nat) (O : Nat → Nat → R) (i : Nat) : Nat → List Nat → List (Term R)
  | _, [] => []
  | k, a :: α => (if i = k then scaleT K (cN K a) (substM K n O k ((a - 1) :: α)) else []) ++
      mulP K (powP K n (lin n O k) a) (tgt n O i (k + 1) α)

theorem mix_chain (n : Nat) (O : Nat → Nat → R) (hO : Orth K n O) (i : Nat) (hi : i < n) :
    ∀ (α : List Nat) (k : Nat), k + α.length ≤ n → ∀ r : List (Term R), LenP n r →
    sumK K (fun m => K.mul (O i m) (Bf K (chainT K n O m k α) r)) n = Bf K (tgt K n O i k α) r
  | [], k, _, r, _ => by
    show sumK K (fun m => K.mul (O i m) K.zero) n = K.zero
    rw [sumK_ext K _ (fun m => K.mul K.zero K.zero) (fun x => by rw [DetChain.mul_zero, DetChain.mul_zero]) n,
      sumK_mul, DetChain.mul_zero]
  | a :: α, k, hk, r, hr => by
    have hkn : k < n := Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right k α.length)) hk
    have hk' : k + 1 + α.length ≤ n := by
      have : k + 1 + α.length = k + (α.length + 1) := by lib_nat
      rw [this]; exact hk
    let P := powP K n (lin n O k) a
    have hP : LenP n P := lenP_pow K n _ (lenP_linF n O k n) a
    have hr' : LenP n (mulP K P r) := lenP_mulP K n r hr P hP
    have ih := mix_chain n O hO i hi α (k + 1) hk' (mulP K P r) hr'
    let B1 := Bf K (substM K n O k ((a - 1) :: α)) r
    have e1 : ∀ m, Bf K (chainT K n O m k (a :: α)) r
        = K.add (K.mul (K.mul (O k m) (cN K a)) B1) (Bf K (chainT K n O m (k + 1) α) (mulP K P r)) := by
      intro m
      show Bf K (scaleT K (K.mul (O k m) (cN K a)) (substM K n O k ((a - 1) :: α)) ++ mulP K P (chainT K n O m (k + 1) α)) r = _
      rw [Bf_app_l, Bf_scale, Bf_mulP_left]
    have e2 : Bf K (tgt K n O i k (a :: α)) r = K.add (Bf K (if i = k then scaleT K (cN K a) (substM K n O k ((a - 1) :: α)) else []) r)
        (Bf K (tgt K n O i (k + 1) α) (mulP K P r)) := by
      show Bf K (_ ++ mulP K P (tgt K n O i (k + 1) α)) r = _
      rw [Bf_app_l, Bf_mulP_left]
    calc sumK K (fun m => K.mul (O i m) (Bf K (chainT K n O m k (a :: α)) r)) n
        = sumK K (fun m => K.add (K.mul (K.mul (O i m) (O k m)) (K.mul (cN K a) B1))
            (K.mul (O i m) (Bf K (chainT K n O m (k + 1) α) (mulP K P r)))) n :=
          sumK_ext K _ _ (fun m => by
            rw [e1 m]
            generalize O i m = x; generalize O k m = y; generalize cN K a = z; generalize B1 = w
            generalize Bf K (chainT K n O m (k + 1) α) (mulP K P r) = v
            lib_ring) n
      _ = K.add (sumK K (fun m => K.mul (K.mul (O i m) (O k m)) (K.mul (cN K a) B1)) n)
            (sumK K (fun m => K.mul (O i m) (Bf K (chainT K n O m (k + 1) α) (mulP K P r))) n) := sumK_add K _ _ n
      _ = K.add (K.mul (sumK K (fun m => K.mul (O i m) (O k m)) n) (K.mul (cN K a) B1))
            (Bf K (tgt K n O i (k + 1) α) (mulP K P r)) := by rw [sumK_mul, ih]
      _ = Bf K (tgt K n O i k (a :: α)) r := by
          rw [e2, hO i k hi hkn]
          by_cases h : i = k
          · rw [if_pos h, if_pos h, Bf_scale, K.one_mul]
          · rw [if_neg h, if_neg h]
            show K.add (K.mul K.zero _) _ = K.add K.zero _
            rw [K.mul_comm, DetChain.mul_zero]

theorem tgt_low (n : Nat) (O : Nat → Nat → R) (i : Nat) :
    ∀ (α : List Nat) (k : Nat), i < k → ∀ r : List (Term R), Bf K (tgt K n O i k α) r = K.zero
  | [], _, _, _ => rfl
  | a :: α, k, h, r => by
    show Bf K (_ ++ mulP K (powP K n (lin n O k) a) (tgt K n O i (k + 1) α)) r = K.zero
    rw [Bf_app_l, Bf_mulP_left, if_neg (Nat.ne_of_lt h), tgt_low n O i α (k + 1) (Nat.lt_succ_of_lt h)]
    show K.add K.zero K.zero = K.zero; rw [K.zero_add]

theorem tgt_at (n : Nat) (O : Nat → Nat → R) :
    ∀ (α : List Nat) (k j : Nat) (r : List (Term R)),
    Bf K (tgt K n O (k + j) k α) r = K.mul (cN K (nthE j α)) (Bf K (substM K n O k (decAt j α)) r)
  | [], k, j, r => by
    have : nthE j ([] : List Nat) = 0 := by cases j <;> rfl
    rw [this]; show K.zero = K.mul K.zero _; rw [K.mul_comm, DetChain.mul_zero]
  | a :: α, k, 0, r => by
    show Bf K ((if k = k then scaleT K (cN K a) (substM K n O k ((a - 1) :: α)) else []) ++
      mulP K (powP K n (lin n O k) a) (tgt K n O k (k + 1) α)) r = K.mul (cN K a) (Bf K (substM K n O k ((a - 1) :: α)) r)
    rw [if_pos rfl, Bf_app_l, Bf_scale, Bf_mulP_left, tgt_low K n O k α (k + 1) (Nat.lt_succ_self k), DetChain.add_zero']
  | a :: α, k, j + 1, r => by
    have e : k + (j + 1) = k + 1 + j := by lib_nat
    rw [e]
    have hne : k + 1 + j ≠ k := Nat.ne_of_gt (Nat.lt_of_lt_of_le (Nat.lt_succ_self k) (Nat.le_add_right (k + 1) j))
    show Bf K ((if k + 1 + j = k then scaleT K (cN K a) (substM K n O k ((a - 1) :: α)) else []) ++
      mulP K (powP K n (lin n O k) a) (tgt K n O (k + 1 + j) (k + 1) α)) r
      = K.mul (cN K (nthE j α)) (Bf K (mulP K (powP K n (lin n O k) a) (substM K n O (k + 1) (decAt j α))) r)
    rw [if_neg hne, Bf_app_l, Bf_mulP_left, Bf_mulP_left, tgt_at n O α (k + 1) j]
    show K.add K.zero _ = _; rw [K.zero_add]

/-! ### raising an exponent multiplies by the linear form -/

theorem subst_inc (n : Nat) (O : Nat → Nat → R) :
    ∀ (α : List Nat) (k i : Nat), i < α.length →
    Eqv K n (substM K n O k (incAt i α)) (mulP K (lin n O (k + i)) (substM K n O k α))
  | [], _, _, h => absurd h (Nat.not_lt_zero _)
  | a :: α, k, 0, _ => by
    let L := lin n O k
    have hL : LenP n L := lenP_linF n O k n
    let P := powP K n L a
    have hP : LenP n P := lenP_pow K n L hL a
    let S := substM K n O (k + 1) α
    have hS : LenP n S := lenP_substM K n O (k + 1) α
    show Eqv K n (mulP K (mulP K P L) S) (mulP K L (mulP K P S))
    rw [mulP_assoc]
    refine eqv_trans K n _ _ _ (eqv_mul_l K n _ _ P hP (eqv_mul_comm K n L S)) ?_
    rw [← mulP_assoc]
    exact eqv_mul_comm K n _ _
  | a :: α, k, i + 1, h => by
    have ih := subst_inc n O α (k + 1) i (Nat.lt_of_succ_lt_succ h)
    have e : k + (i + 1) = k + 1 + i := by lib_nat
    rw [e]
    let L := lin n O (k + 1 + i)
    have hL : LenP n L := lenP_linF n O (k + 1 + i) n
    let P := powP K n (lin n O k) a
    have hP : LenP n P := lenP_pow K n _ (lenP_linF n O k n) a
    let S := substM K n O (k + 1) α
    have hS : LenP n S := lenP_substM K n O (k + 1) α
    show Eqv K n (mulP K P (substM K n O (k + 1) (incAt i α))) (mulP K L (mulP K P S))
    refine eqv_trans K n _ _ _ (eqv_mul_l K n _ _ P hP ih) ?_
    rw [← mulP_assoc, ← mulP_assoc]
    exact eqv_mul_r K n _ _ S hS (eqv_mul_comm K n P L)

/-! ### integration by parts survives the orthogonal substitution -/

theorem rot_ibp (n : Nat) (O : Nat → Nat → R) (hO : Orth K n O) (α : List Nat) (hl : α.length = n) (i : Nat) (hi : i < n) :
    Gp K (substM K n O 0 (incAt i α)) = K.mul (cN K (nthE i α)) (Gp K (substM K n O 0 (decAt i α))) := by
  let S := substM K n O 0 α
  have hS : LenP n S := lenP_substM K n O 0 α
  have hone := lenP_one K n
  have si := subst_inc K n O α 0 i (by rw [hl]; exact hi)
  rw [Nat.zero_add] at si
  have ta := tgt_at K n O α 0 i (one K n)
  rw [Nat.zero_add] at ta
  rw [Gp_eq_Bf K n _ (lenP_substM K n O 0 _), si _ hone, ← Gp_eq_Bf K n (mulP K (lin n O i) S) (lenP_mulP K n S hS _ (lenP_linF n O i n)),
    Gp_eq_Bf K n _ (lenP_substM K n O 0 _), ← ta]
  show Gp K (mulP K (linF n O i n) S) = _
  rw [Gp_linF K n O i S hS n (Nat.le_refl n)]
  rw [← mix_chain K n O hO i hi α 0 (by rw [Nat.zero_add, hl]; exact Nat.le_refl n) (one K n) hone]
  refine sumK_extB K _ _ n (fun m hm => ?_)
  rw [Gp_ibp K m S (wide_of_len n m hm S hS), Gp_eq_Bf K n _ (lenP_dP K n m S hS), chain K n O m hm 0 α (one K n) hone]

/-! ### rotation invariance -/

def substP (n : Nat) (O : Nat → Nat → R) : List (Term R) → List (Term R)
  | [] => []
  | t :: p => scaleT K t.1 (substM K n O 0 t.2) ++ substP n O p

theorem Gp_substP1 (n : Nat) (O : Nat → Nat → R) (c : R) (α : List Nat) :
    Gp K (substP K n O [(c, α)]) = K.mul c (Gp K (substM K n O 0 α)) := by
  show Gp K (scaleT K c (substM K n O 0 α) ++ []) = _
  rw [Gp_app, Gp_scale]; show K.add _ K.zero = _; rw [DetChain.add_zero']

theorem Gp_mulP_one (n : Nat) (X : List (Term R)) (hX : LenP n X) : Gp K (mulP K (one K n) X) = Gp K X := by
  rw [Gp_mulP, Bf_comm, ← Gp_eq_Bf K n X hX]

theorem Gp_subst_zeros (n : Nat) (O : Nat → Nat → R) : ∀ (m k : Nat), Gp K (substM K n O k (zerosL m)) = K.one
  | 0, _ => by
    show K.add (K.mul K.one (cN K (GM (zerosL n)))) K.zero = K.one
    rw [GM_zeros, SU2CharExp.cN_one, K.one_mul, DetChain.add_zero']
  | m + 1, k => by
    show Gp K (mulP K (one K n) (substM K n O (k + 1) (zerosL m))) = K.one
    rw [Gp_mulP_one K n _ (lenP_substM K n O (k + 1) _), Gp_subst_zeros n O m (k + 1)]

/-- **Rotation invariance of the Gaussian moments:** for O with orthonormal rows, G(x^α ∘ O) = G(x^α). -/
theorem rot_inv (n : Nat) (O : Nat → Nat → R) (hO : Orth K n O) (α : List Nat) (hl : α.length = n) :
    Gp K (substM K n O 0 α) = cN K (GM α) := by
  have u := unique_mono K (fun p => Gp K (substP K n O p)) n
    (fun c β => by
      show Gp K (substP K n O [(c, β)]) = K.mul c (Gp K (substP K n O [(K.one, β)]))
      rw [Gp_substP1, Gp_substP1, K.one_mul])
    (by show Gp K (substP K n O [(K.one, zerosL n)]) = K.one
        rw [Gp_substP1, K.one_mul, Gp_subst_zeros])
    (fun i β hi hb => by
      show Gp K (substP K n O [(K.one, incAt i β)]) = Gp K (substP K n O [(K.mul K.one (cN K (nthE i β)), decAt i β)])
      rw [Gp_substP1, Gp_substP1, rot_ibp K n O hO β hb i hi, K.one_mul, K.one_mul])
    (sumL α) α hl (Nat.le_refl _)
  have e : Gp K (substP K n O [(K.one, α)]) = Gp K (substM K n O 0 α) := by rw [Gp_substP1, K.one_mul]
  rw [← e]; exact u

end GaussRot

#print axioms GaussRot.rot_ibp
#print axioms GaussRot.rot_inv

ym/GaussQuat.lean

/-
  ym/GaussQuat — T45, H0 step 7: unit quaternions act orthogonally on ℝ⁴, so the Gaussian moments are SU(2)-bi-invariant.

  For q = a + b i + c j + d k with a² + b² + c² + d² = 1, the matrices of x ↦ q x and x ↦ x q have orthonormal rows
  (qL_orth, qR_orth); with GaussRot.rot_inv, G(x^α ∘ L_q) = G(x^α ∘ R_q) = G(x^α) (bi_inv).
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GaussRot

namespace GaussQuat

open SU2Char Gauss GaussPoly GaussSubst GaussRot

variable {R : Type} (K : DetChain.CRing R)

/-- left multiplication by q -/
def qL (a b c d : R) : Nat → Nat → R
  | 0, 0 => a | 0, 1 => K.neg b | 0, 2 => K.neg c | 0, 3 => K.neg d
  | 1, 0 => b | 1, 1 => a | 1, 2 => K.neg d | 1, 3 => c
  | 2, 0 => c | 2, 1 => d | 2, 2 => a | 2, 3 => K.neg b
  | 3, 0 => d | 3, 1 => K.neg c | 3, 2 => b | 3, 3 => a
  | _, _ => K.zero

/-- right multiplication by q -/
def qR (a b c d : R) : Nat → Nat → R
  | 0, 0 => a | 0, 1 => K.neg b | 0, 2 => K.neg c | 0, 3 => K.neg d
  | 1, 0 => b | 1, 1 => a | 1, 2 => d | 1, 3 => K.neg c
  | 2, 0 => c | 2, 1 => K.neg d | 2, 2 => a | 2, 3 => b
  | 3, 0 => d | 3, 1 => c | 3, 2 => K.neg b | 3, 3 => a
  | _, _ => K.zero

theorem lt4 : ∀ i : Nat, i < 4 → i = 0 ∨ i = 1 ∨ i = 2 ∨ i = 3
  | 0, _ => Or.inl rfl
  | 1, _ => Or.inr (Or.inl rfl)
  | 2, _ => Or.inr (Or.inr (Or.inl rfl))
  | 3, _ => Or.inr (Or.inr (Or.inr rfl))
  | n + 4, h => absurd (Nat.lt_of_le_of_lt (Nat.le_add_left 4 n) h) (Nat.lt_irrefl 4)

theorem sum4 (f : Nat → R) : sumK K f 4 = K.add (K.add (K.add (K.add K.zero (f 0)) (f 1)) (f 2)) (f 3) := rfl

def nrm (a b c d : R) : R := K.add (K.add (K.add (K.mul a a) (K.mul b b)) (K.mul c c)) (K.mul d d)

theorem qL_orth (a b c d : R) (hn : nrm K a b c d = K.one) : Orth K 4 (qL K a b c d) := by
  intro i l hi hl
  rw [sum4, ← hn]
  unfold nrm
  rcases lt4 i hi with h | h | h | h <;> rcases lt4 l hl with g | g | g | g <;> subst h <;> subst g <;>
    first
      | (rw [if_pos rfl]; simp only [qL]; lib_ring)
      | (rw [if_neg (by decide)]; simp only [qL]; lib_ring)

theorem qR_orth (a b c d : R) (hn : nrm K a b c d = K.one) : Orth K 4 (qR K a b c d) := by
  intro i l hi hl
  rw [sum4, ← hn]
  unfold nrm
  rcases lt4 i hi with h | h | h | h <;> rcases lt4 l hl with g | g | g | g <;> subst h <;> subst g <;>
    first
      | (rw [if_pos rfl]; simp only [qR]; lib_ring)
      | (rw [if_neg (by decide)]; simp only [qR]; lib_ring)

/-- **SU(2)-bi-invariance of the Gaussian moments on ℝ⁴ = ℍ.** -/
theorem bi_inv (a b c d : R) (hn : nrm K a b c d = K.one) (α : List Nat) (hl : α.length = 4) :
    Gp K (substM K 4 (qL K a b c d) 0 α) = cN K (GM α) ∧ Gp K (substM K 4 (qR K a b c d) 0 α) = cN K (GM α) :=
  ⟨rot_inv K 4 _ (qL_orth K a b c d hn) α hl, rot_inv K 4 _ (qR_orth K a b c d hn) α hl⟩

end GaussQuat

#print axioms GaussQuat.qL_orth
#print axioms GaussQuat.bi_inv

ym/GaussCatalan.lean

/-
  ym/GaussCatalan — T45, H0 step 8: the SU(2) walk moments are Catalan numbers, matched to Gaussian moments.

    P_far          walks of m steps from s cannot reach t > m + s
    refl           reflection: P_m(0 → t) + C(m, a − 1) = C(m, a)     (m = t + 2a)
    ch_mid         C(2b+1, b+1) = C(2b+1, b)
    mom_fact       mom(2k) · k! · (k+1)! = (2k)!                     (mom(2k) = C_k, SU2Haar)
    fact_double    (2k)! = 2^k · k! · (2k−1)!!
    catalan_gauss  2^k · m(2k) = mom(2k) · (k+1)!
  so the moments C_k/4^k of tr U/2 under the SU(2) Haar measure are m(2k)/(2^k (k+1)!) = G(x₀^{2k}) / E|x|^{2k} on ℝ⁴.
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GaussQuat

namespace GaussCatalan

open SU2Haar SU2CharExp Gauss

theorem addR : ∀ (c a b : Nat), a + c = b + c → a = b
  | 0, _, _, h => h
  | c + 1, a, b, h => addR c a b (Nat.succ.inj h)

def cp : Nat → Nat → Nat
  | _, 0 => 0
  | m, a + 1 => ch m a

theorem pascal_cp : ∀ m b, ch (m + 1) b = ch m b + cp m b
  | m, 0 => by rw [ch_zero_r, ch_zero_r]; rfl
  | m, b + 1 => by show ch m b + ch m (b + 1) = ch m (b + 1) + ch m b; rw [Nat.add_comm]

theorem dl_ne : ∀ s t, s < t → dl s t = 0
  | _, 0, h => absurd h (Nat.not_lt_zero _)
  | 0, _ + 1, _ => rfl
  | s + 1, t + 1, h => dl_ne s t (Nat.lt_of_succ_lt_succ h)

theorem P_far : ∀ m s t, m + s < t → P m s t = 0
  | 0, s, t, h => dl_ne s t (by rw [Nat.zero_add] at h; exact h)
  | m + 1, 0, t, h => P_far m 1 t h
  | m + 1, s + 1, t, h => by
    show P m s t + P m (s + 2) t = 0
    have e1 : m + 1 + (s + 1) = m + s + 2 := by lib_nat
    have e2 : m + 1 + (s + 1) = m + (s + 2) := by lib_nat
    rw [P_far m s t (Nat.lt_of_le_of_lt (Nat.le_add_right (m + s) 2) (by rw [← e1]; exact h)),
      P_far m (s + 2) t (by rw [← e2]; exact h)]

theorem ch_mid (b : Nat) : ch (2 * b + 1) (b + 1) = ch (2 * b + 1) b := by
  have c1 := ch_fact _ (b + 1) b rfl
  have c2 := ch_fact _ b (b + 1) rfl
  have e1 : b + 1 + b = 2 * b + 1 := by lib_nat
  have e2 : b + (b + 1) = 2 * b + 1 := by lib_nat
  rw [e1] at c1; rw [e2] at c2
  have h := c2.trans c1.symm
  have pos : 0 < fact b * fact (b + 1) := Nat.mul_le_mul (fact_pos b) (fact_pos (b + 1))
  apply Nat.eq_of_mul_eq_mul_right pos
  generalize ch (2 * b + 1) (b + 1) = u at h ⊢
  generalize ch (2 * b + 1) b = v at h ⊢
  generalize fact b = p at h ⊢
  generalize fact (b + 1) = q at h ⊢
  calc u * (p * q) = u * p * q := by lib_nat
    _ = v * q * p := h
    _ = v * (p * q) := by lib_nat

/-- **Reflection principle** for walks on ℕ. -/
theorem refl : ∀ m a t, t + 2 * a = m → P m 0 t + cp m a = ch m a := by
  intro m
  induction m with
  | zero =>
    intro a t h
    cases a with
    | zero =>
      have ht : t = 0 := h
      subst ht; rfl
    | succ b => exact Nat.noConfusion h
  | succ m ih =>
    intro a t h
    cases t with
    | succ t' =>
      have h' : t' + 2 * a = m := Nat.succ.inj ((show t' + 2 * a + 1 = t' + 1 + 2 * a by lib_nat).trans h)
      rw [(last_step m 0).1 t']
      cases a with
      | zero =>
        have i0 := ih 0 t' h'
        have hm : t' = m := h'
        rw [P_far m 0 (t' + 2) (by rw [← hm]; exact Nat.lt_succ_of_lt (Nat.lt_succ_self t'))]
        show P m 0 t' + 0 + 0 = ch (m + 1) 0
        rw [ch_zero_r]; rw [ch_zero_r] at i0; exact i0
      | succ b =>
        have i1 : P m 0 t' + ch m b = ch m (b + 1) := ih (b + 1) t' h'
        have i2 := ih b (t' + 2) (by rw [← h']; lib_nat)
        show P m 0 t' + P m 0 (t' + 2) + ch (m + 1) b = ch m b + ch m (b + 1)
        rw [pascal_cp m b]
        generalize P m 0 t' = x at i1 ⊢
        generalize P m 0 (t' + 2) = y at i2 ⊢
        generalize cp m b = w at i2 ⊢
        generalize ch m (b + 1) = v at i1 ⊢
        generalize ch m b = z at i1 i2 ⊢
        rw [← i1, ← i2]; lib_nat
    | zero =>
      cases a with
      | zero => exact Nat.noConfusion h
      | succ b =>
        have hm : m = 2 * b + 1 := Nat.succ.inj (h.symm.trans (show 0 + 2 * (b + 1) = 2 * b + 1 + 1 by lib_nat))
        rw [(last_step m 0).2]
        have i1 := ih b 1 (by rw [hm]; lib_nat)
        show P m 0 1 + ch (m + 1) b = ch m b + ch m (b + 1)
        rw [pascal_cp m b]
        have sy : ch m (b + 1) = ch m b := by rw [hm]; exact ch_mid b
        rw [sy]
        generalize P m 0 1 = x at i1 ⊢
        generalize cp m b = w at i1 ⊢
        generalize ch m b = z at i1 ⊢
        rw [← i1]; lib_nat

/-- **Catalan:** mom(2k) · k! · (k+1)! = (2k)!. -/
theorem mom_fact : ∀ k, mom (2 * k) * fact k * fact (k + 1) = fact (2 * k)
  | 0 => rfl
  | j + 1 => by
    have r := refl (2 * (j + 1)) (j + 1) 0 (by lib_nat)
    have A := ch_fact _ (j + 1) (j + 1) rfl
    have B := ch_fact _ (j + 2) j rfl
    have eA : j + 1 + (j + 1) = 2 * (j + 1) := by lib_nat
    have eB : j + 2 + j = 2 * (j + 1) := by lib_nat
    rw [eA] at A; rw [eB] at B
    show P (2 * (j + 1)) 0 0 * fact (j + 1) * fact (j + 1 + 1) = fact (2 * (j + 1))
    have r' : P (2 * (j + 1)) 0 0 + ch (2 * (j + 1)) j = ch (2 * (j + 1)) (j + 1) := r
    rw [← A]
    rw [← A, ← r'] at B
    show _ * (fact j * (j + 1)) * (fact j * (j + 1) * (j + 1 + 1)) = _ * (fact j * (j + 1)) * (fact j * (j + 1))
    change ch (2 * (j + 1)) j * (fact j * (j + 1) * (j + 1 + 1)) * fact j
      = (P (2 * (j + 1)) 0 0 + ch (2 * (j + 1)) j) * (fact j * (j + 1)) * (fact j * (j + 1)) at B
    rw [← r']
    generalize P (2 * (j + 1)) 0 0 = M at B ⊢
    generalize ch (2 * (j + 1)) j = c at B ⊢
    generalize fact j = x at B ⊢
    have hB : c * x * x * (j + 1) + c * x * x * (j + 1) * (j + 1)
        = M * x * x * (j + 1) * (j + 1) + c * x * x * (j + 1) * (j + 1) := by
      calc _ = c * (x * (j + 1) * (j + 1 + 1)) * x := by lib_nat
        _ = (M + c) * (x * (j + 1)) * (x * (j + 1)) := B
        _ = _ := by lib_nat
    have u := addR _ _ _ hB
    calc M * (x * (j + 1)) * (x * (j + 1) * (j + 1 + 1))
        = M * x * x * (j + 1) * (j + 1) + (M * x * x * (j + 1) * (j + 1)) * (j + 1) := by lib_nat
      _ = M * x * x * (j + 1) * (j + 1) + (c * x * x * (j + 1)) * (j + 1) := by rw [u]
      _ = (M + c) * (x * (j + 1)) * (x * (j + 1)) := by lib_nat

/-- (2k)! = 2^k · k! · (2k−1)!! -/
theorem fact_double : ∀ k, fact (2 * k) = 2 ^ k * fact k * gm (2 * k)
  | 0 => rfl
  | k + 1 => by
    have e : 2 * (k + 1) = 2 * k + 1 + 1 := by lib_nat
    rw [e]
    show fact (2 * k) * (2 * k + 1) * (2 * k + 1 + 1) = 2 ^ k * 2 * (fact k * (k + 1)) * ((2 * k + 1) * gm (2 * k))
    rw [fact_double k]
    generalize 2 ^ k = p
    generalize fact k = f
    generalize gm (2 * k) = g
    lib_nat

/-- **The Gaussian moments of ℝ⁴ reproduce the SU(2) walk moments:** 2^k · m(2k) = mom(2k) · (k+1)!. -/
theorem catalan_gauss (k : Nat) : 2 ^ k * gm (2 * k) = mom (2 * k) * fact (k + 1) := by
  apply Nat.eq_of_mul_eq_mul_right (fact_pos k)
  have h := (mom_fact k).trans (fact_double k)
  generalize mom (2 * k) = M at h ⊢
  generalize 2 ^ k = p at h ⊢
  generalize fact k = f at h ⊢
  generalize fact (k + 1) = g at h ⊢
  generalize gm (2 * k) = q at h ⊢
  calc p * q * f = p * f * q := by lib_nat
    _ = M * f * g := h.symm
    _ = M * g * f := by lib_nat

theorem catalan_check : 2 ^ 3 * gm 6 = mom 6 * SU2CharExp.fact 4 := by decide

end GaussCatalan

#print axioms GaussCatalan.refl
#print axioms GaussCatalan.mom_fact
#print axioms GaussCatalan.catalan_gauss

ym/GaussSphere.lean

/-
  ym/GaussSphere — T45, H0 step 9: the radial moments of ℝ⁴ and the sphere normalisation of the SU(2) moments.

  r² = Σ_{i<n} x_i² as a term list. For Q homogeneous of degree d in n variables,
    G(Q · r²) = (d + n) · G(Q)                 (each x_i² contributes α_i + 1, Σ_i α_i = d)
  hence on ℝ⁴
    norm_moment     G(|x|^{2k}) = 2^k (k+1)!
    haar_sphere     4^k · G(x₀^{2k}) = mom(2k) · G(|x|^{2k})
  i.e. the SU(2) Haar moments of tr U = 2x₀ (walk counts mom(2k) = C_k, SU2Haar) are the normalised averages
  G((2x₀)^{2k}) / G(|x|^{2k}) of the rotation- and SU(2)-bi-invariant functional G (GaussRot, GaussQuat).
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GaussCatalan

namespace GaussSphere

open SU2Char SU2CharExp SU2Haar Gauss GaussPoly GaussSubst GaussRot

variable {R : Type} (K : DetChain.CRing R)

/-! ### Nat bookkeeping -/

def sumN (f : Nat → Nat) : Nat → Nat
  | 0 => 0
  | m + 1 => sumN f m + f m

theorem sumN_shift (f : Nat → Nat) : ∀ m, sumN f (m + 1) = f 0 + sumN (fun i => f (i + 1)) m
  | 0 => by show 0 + f 0 = f 0; exact Nat.zero_add _
  | m + 1 => by
    show sumN f (m + 1) + f (m + 1) = f 0 + (sumN (fun i => f (i + 1)) m + f (m + 1))
    rw [sumN_shift f m]; lib_nat

theorem sum_nth : ∀ α : List Nat, sumN (fun i => nthE i α + 1) α.length = sumL α + α.length
  | [] => rfl
  | a :: α => by
    show sumN (fun i => nthE i (a :: α) + 1) (α.length + 1) = (a + sumL α) + (α.length + 1)
    rw [sumN_shift]
    show (a + 1) + sumN (fun i => nthE i α + 1) α.length = _
    rw [sum_nth α]; lib_nat

theorem nth_inc_self : ∀ (i : Nat) (α : List Nat), i < α.length → nthE i (incAt i α) = nthE i α + 1
  | _, [], h => absurd h (Nat.not_lt_zero _)
  | 0, _ :: _, _ => rfl
  | i + 1, _ :: α, h => nth_inc_self i α (Nat.lt_of_succ_lt_succ h)

theorem dec_inc : ∀ (i : Nat) (α : List Nat), decAt i (incAt i α) = α
  | 0, [] => rfl
  | _ + 1, [] => rfl
  | 0, _ :: _ => rfl
  | i + 1, a :: α => by show a :: decAt i (incAt i α) = a :: α; rw [dec_inc i α]

theorem sum_incAt : ∀ (i : Nat) (α : List Nat), i < α.length → sumL (incAt i α) = sumL α + 1
  | _, [], h => absurd h (Nat.not_lt_zero _)
  | 0, a :: α, _ => by show (a + 1) + sumL α = (a + sumL α) + 1; lib_nat
  | i + 1, a :: α, h => by
    show a + sumL (incAt i α) = (a + sumL α) + 1
    rw [sum_incAt i α (Nat.lt_of_succ_lt_succ h)]; lib_nat

theorem sum_addE : ∀ α β : List Nat, sumL (addE α β) = sumL α + sumL β
  | [], β => (Nat.zero_add _).symm
  | a :: α, [] => rfl
  | a :: α, b :: β => by
    show (a + b) + sumL (addE α β) = (a + sumL α) + (b + sumL β)
    rw [sum_addE α β]; lib_nat

theorem sum_zeros : ∀ n, sumL (zerosL n) = 0
  | 0 => rfl
  | n + 1 => by show 0 + sumL (zerosL n) = 0; rw [sum_zeros n]

/-- G(x_i² x^α) = (α_i + 1) G(x^α) -/
theorem GM_sq (i : Nat) (α : List Nat) (h : i < α.length) : GM (incAt i (incAt i α)) = (nthE i α + 1) * GM α := by
  rw [G_ibp i (incAt i α) (by rw [len_incAt]; exact h), nth_inc_self i α h, dec_inc]

/-! ### r² and homogeneity -/

def sq (n i : Nat) : List Nat := incAt i (unitE n i)

def r2F (n : Nat) : Nat → List (Term R)
  | 0 => []
  | m + 1 => r2F n m ++ [(K.one, sq n m)]

def r2 (n : Nat) : List (Term R) := r2F K n n

theorem addE_sq (n m : Nat) (hm : m < n) (α : List Nat) (hl : α.length = n) : addE α (sq n m) = incAt m (incAt m α) := by
  have hz : (zerosL n).length = n := len_zeros n
  have h1 : m < (incAt m (zerosL n)).length := by rw [len_incAt, hz]; exact hm
  show addE α (incAt m (incAt m (zerosL n))) = _
  rw [← inc_addE m α _ h1 (by rw [len_incAt, hz, hl]), ← inc_addE m α _ (by rw [hz]; exact hm) (by rw [hz, hl]), ← hl,
    addE_zeros]

def HomP (n d : Nat) : List (Term R) → Prop
  | [] => True
  | t :: p => t.2.length = n ∧ sumL t.2 = d ∧ HomP n d p

theorem hom_app (n d : Nat) : ∀ p q : List (Term R), HomP n d p → HomP n d q → HomP n d (p ++ q)
  | [], _, _, hq => hq
  | _ :: p, q, hp, hq => ⟨hp.1, hp.2.1, hom_app n d p q hp.2.2 hq⟩

theorem hom_map (n d e : Nat) (t : Term R) (ht : t.2.length = n) (hs : sumL t.2 = d) :
    ∀ B : List (Term R), HomP n e B → HomP n (d + e) (B.map (mulT K t))
  | [], _ => trivial
  | s :: B, h => ⟨(len_addE t.2 s.2 (ht.trans h.1.symm)).trans ht, by show sumL (addE t.2 s.2) = _; rw [sum_addE, hs, h.2.1],
      hom_map n d e t ht hs B h.2.2⟩

theorem hom_mulP (n d e : Nat) (B : List (Term R)) (hB : HomP n e B) :
    ∀ A : List (Term R), HomP n d A → HomP n (d + e) (mulP K A B)
  | [], _ => trivial
  | t :: A, h => hom_app n _ _ _ (hom_map K n d e t h.1 h.2.1 B hB) (hom_mulP n d e B hB A h.2.2)

theorem hom_r2F (n : Nat) : ∀ m, m ≤ n → HomP n 2 (r2F K n m)
  | 0, _ => trivial
  | m + 1, hm => by
    have hz : (zerosL n).length = n := len_zeros n
    have hu : (unitE n m).length = n := (len_incAt m _).trans hz
    refine hom_app n 2 _ _ (hom_r2F n m (Nat.le_of_succ_le hm)) ⟨(len_incAt m _).trans hu, ?_, trivial⟩
    show sumL (incAt m (incAt m (zerosL n))) = 2
    rw [sum_incAt m _ (by rw [len_incAt, hz]; exact hm), sum_incAt m _ (by rw [hz]; exact hm), sum_zeros]

theorem hom_pow (n : Nat) : ∀ k, HomP n (2 * k) (powP K n (r2 K n) k)
  | 0 => ⟨len_zeros n, sum_zeros n, trivial⟩
  | k + 1 => by
    have := hom_mulP K n (2 * k) 2 (r2 K n) (hom_r2F K n n (Nat.le_refl n)) _ (hom_pow n k)
    rw [show 2 * k + 2 = 2 * (k + 1) by lib_nat] at this
    exact this

/-! ### the radial recursion -/

theorem sumK_cN (f : Nat → Nat) : ∀ m, sumK K (fun i => cN K (f i)) m = cN K (sumN f m)
  | 0 => rfl
  | m + 1 => by
    show K.add (sumK K (fun i => cN K (f i)) m) (cN K (f m)) = cN K (sumN f m + f m)
    rw [sumK_cN f m, cN_add]

theorem row_r2F (n : Nat) (t : Term R) (ht : t.2.length = n) : ∀ m, m ≤ n →
    row K t (r2F K n m) = K.mul (sumK K (fun i => cN K (nthE i t.2 + 1)) m) (K.mul t.1 (cN K (GM t.2)))
  | 0, _ => by show K.zero = K.mul K.zero _; rw [K.mul_comm, DetChain.mul_zero]
  | m + 1, hm => by
    rw [show r2F K n (m + 1) = r2F K n m ++ [(K.one, sq n m)] from rfl, row_app, row_r2F n t ht m (Nat.le_of_succ_le hm)]
    show K.add _ (K.add (K.mul (K.mul t.1 K.one) (cN K (GM (addE t.2 (sq n m))))) K.zero)
      = K.mul (K.add (sumK K (fun i => cN K (nthE i t.2 + 1)) m) (cN K (nthE m t.2 + 1))) _
    rw [addE_sq n m hm t.2 ht, GM_sq m t.2 (by rw [ht]; exact hm), cN_mul]
    generalize sumK K (fun i => cN K (nthE i t.2 + 1)) m = s
    generalize cN K (nthE m t.2 + 1) = a
    generalize cN K (GM t.2) = g
    lib_ring

theorem Gp_mul_r2 (n d : Nat) : ∀ Q : List (Term R), HomP n d Q → Gp K (mulP K Q (r2 K n)) = K.mul (cN K (d + n)) (Gp K Q)
  | [], _ => by show K.zero = K.mul _ K.zero; rw [DetChain.mul_zero]
  | t :: Q, h => by
    rw [Gp_mulP]
    show K.add (row K t (r2 K n)) (Bf K Q (r2 K n)) = K.mul (cN K (d + n)) (K.add (K.mul t.1 (cN K (GM t.2))) (Gp K Q))
    rw [← Gp_mulP, Gp_mul_r2 n d Q h.2.2]
    show K.add (row K t (r2F K n n)) _ = _
    rw [row_r2F K n t h.1 n (Nat.le_refl n), sumK_cN, ← h.1, sum_nth, h.2.1, h.1]
    generalize cN K (d + n) = c
    generalize Gp K Q = q
    generalize K.mul t.1 (cN K (GM t.2)) = g
    lib_ring

/-- **Radial moments of ℝ⁴:** G(|x|^{2k}) = 2^k (k+1)!. -/
theorem norm_moment : ∀ k, Gp K (powP K 4 (r2 K 4) k) = cN K (2 ^ k * fact (k + 1))
  | 0 => by
    show K.add (K.mul K.one (cN K (GM (zerosL 4)))) K.zero = cN K 1
    rw [GM_zeros, cN_one, K.one_mul, DetChain.add_zero']
  | k + 1 => by
    show Gp K (mulP K (powP K 4 (r2 K 4) k) (r2 K 4)) = _
    rw [Gp_mul_r2 K 4 (2 * k) _ (hom_pow K 4 k), norm_moment k, ← cN_mul]
    congr 1
    show (2 * k + 4) * (2 ^ k * fact (k + 1)) = 2 ^ k * 2 * (fact (k + 1) * (k + 1 + 1))
    generalize 2 ^ k = p
    generalize fact (k + 1) = f
    lib_nat

theorem pow4 : ∀ k : Nat, 4 ^ k = 2 ^ k * 2 ^ k
  | 0 => rfl
  | k + 1 => by
    show 4 ^ k * 4 = 2 ^ k * 2 * (2 ^ k * 2)
    rw [pow4 k]; generalize 2 ^ k = p; lib_nat

/-- **The SU(2) Haar moments are the normalised Gaussian averages on ℝ⁴:**
    4^k · G(x₀^{2k}) = mom(2k) · G(|x|^{2k}),   i.e.  ∫ (tr U)^{2k} dU = G((2x₀)^{2k}) / G(|x|^{2k}). -/
theorem haar_sphere (k : Nat) :
    K.mul (cN K (4 ^ k)) (Gp K [(K.one, [2 * k, 0, 0, 0])]) = K.mul (cN K (mom (2 * k))) (Gp K (powP K 4 (r2 K 4) k)) := by
  rw [norm_moment, ← cN_mul]
  show K.mul (cN K (4 ^ k)) (K.add (K.mul K.one (cN K (gm (2 * k) * (1 * (1 * (1 * 1)))))) K.zero) = _
  rw [K.one_mul, DetChain.add_zero', ← cN_mul]
  congr 1
  rw [pow4]
  have c := GaussCatalan.catalan_gauss k
  generalize 2 ^ k = p at c ⊢
  generalize gm (2 * k) = g at c ⊢
  generalize mom (2 * k) = M at c ⊢
  generalize fact (k + 1) = f at c ⊢
  calc p * p * (g * (1 * (1 * (1 * 1)))) = p * (p * g) := by lib_nat
    _ = p * (M * f) := by rw [c]
    _ = M * (p * f) := by lib_nat

end GaussSphere

#print axioms GaussSphere.norm_moment
#print axioms GaussSphere.haar_sphere

90.5 H0: normalised sphere functional on \(\mathbb R^4\)

\(G\) is invariant under orthogonal and unit-quaternion substitutions on all polynomials, and \(S(Q)=G(Q)/G(|x|^{2k})\) is consistent on \(S^3\).

\[ G(p\circ O)=G(p),\qquad G(Q|x|^2)\,G(|x|^{2k})=G(Q)\,G(|x|^{2k+2})\ (\deg Q=2k). \]

ym/GaussH0.lean

/-
  ym/GaussH0 — T45, H0 step 10: the normalised sphere functional on all polynomials of ℝ⁴.

    rot_inv_poly     G(p ∘ O) = G(p) for every polynomial p (term list of exponent length n), O orthonormal
    bi_inv_poly      G(p ∘ L_q) = G(p ∘ R_q) = G(p) for unit quaternions q
    sphere_consist   G(Q |x|²) · G(|x|^{2k}) = G(Q) · G(|x|^{2k+2}) for Q homogeneous of degree 2k on ℝ⁴,
                     so S(Q) = G(Q)/G(|x|^{2k}) does not change when Q is multiplied by |x|² (= 1 on S³):
                     S is a well-defined, normalised, SU(2)-bi-invariant functional on polynomial functions on S³.
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GaussSphere

namespace GaussH0

open SU2Char SU2CharExp Gauss GaussPoly GaussSubst GaussRot GaussQuat GaussSphere

variable {R : Type} (K : DetChain.CRing R)

theorem rot_inv_poly (n : Nat) (O : Nat → Nat → R) (hO : Orth K n O) :
    ∀ p : List (Term R), LenP n p → Gp K (substP K n O p) = Gp K p
  | [], _ => rfl
  | t :: p, h => by
    show Gp K (scaleT K t.1 (substM K n O 0 t.2) ++ substP K n O p) = K.add (K.mul t.1 (cN K (GM t.2))) (Gp K p)
    rw [Gp_app, Gp_scale, rot_inv K n O hO t.2 h.1, rot_inv_poly n O hO p h.2]

theorem bi_inv_poly (a b c d : R) (hn : nrm K a b c d = K.one) (p : List (Term R)) (hp : LenP 4 p) :
    Gp K (substP K 4 (qL K a b c d) p) = Gp K p ∧ Gp K (substP K 4 (qR K a b c d) p) = Gp K p :=
  ⟨rot_inv_poly K 4 _ (qL_orth K a b c d hn) p hp, rot_inv_poly K 4 _ (qR_orth K a b c d hn) p hp⟩

/-- **Sphere consistency** of the normalisation. -/
theorem sphere_consist (k : Nat) (Q : List (Term R)) (hQ : HomP 4 (2 * k) Q) :
    K.mul (Gp K (mulP K Q (r2 K 4))) (Gp K (powP K 4 (r2 K 4) k))
      = K.mul (Gp K Q) (Gp K (powP K 4 (r2 K 4) (k + 1))) := by
  show _ = K.mul (Gp K Q) (Gp K (mulP K (powP K 4 (r2 K 4) k) (r2 K 4)))
  rw [Gp_mul_r2 K 4 (2 * k) Q hQ, Gp_mul_r2 K 4 (2 * k) _ (hom_pow K 4 k)]
  generalize cN K (2 * k + 4) = c
  generalize Gp K Q = q
  generalize Gp K (powP K 4 (r2 K 4) k) = g
  lib_ring

end GaussH0

#print axioms GaussH0.rot_inv_poly
#print axioms GaussH0.bi_inv_poly
#print axioms GaussH0.sphere_consist

#print axioms: 3 theorems, no axioms.

90.6 H0: Hermite orthogonality on \(\mathbb R^n\)

With \(He_\alpha =\prod _i(x_i-\partial _i)^{\alpha _i}1\): Rodrigues formula and orthogonality for the Gaussian functional.

\[ G(He_\alpha \,q)=G(\partial ^\alpha q),\qquad G(He_\alpha He_\beta )=\alpha !\,\delta _{\alpha \beta }. \]

ym/GaussHermite.lean

/-
  ym/GaussHermite — T45, H0 positivity, step 1: the Gaussian adjoint and the Rodrigues formula.

  Creation operator a*_i f = x_i f − ∂_i f; Hermite polynomials He_k(x_i) = (a*_i)^k 1.
    gp_comm     G(f g) = G(g f)
    adj         G((a*_i f) g) = G(f ∂_i g)                    (IBP + Leibniz)
    rodrigues   G(((a*_i)^k f) g) = G(f ∂_i^k g)
    herm_dk     G(He_k(x_i) · g) = G(∂_i^k g)
  Next: [∂_i, a*_i] = 1, hence G(He_α He_β) = α! δ_αβ and G(p²) = Σ α! c_α² ≥ 0 (positivity of the Haar functional).
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GaussH0

namespace GaussHermite

open SU2Char Gauss GaussPoly GaussSubst GaussRot

variable {R : Type} (K : DetChain.CRing R)

def cre (i : Nat) (f : List (Term R)) : List (Term R) := xi i f ++ scaleT K (K.neg K.one) (dP K i f)

def creK (i : Nat) : Nat → List (Term R) → List (Term R)
  | 0, f => f
  | k + 1, f => cre K i (creK i k f)

def dPk (i : Nat) : Nat → List (Term R) → List (Term R)
  | 0, g => g
  | k + 1, g => dP K i (dPk i k g)

theorem lenP_cre (n i : Nat) (f : List (Term R)) (hf : LenP n f) : LenP n (cre K i f) :=
  lenP_app n _ _ (lenP_xi n i f hf) (lenP_scale K n _ _ (lenP_dP K n i f hf))

theorem lenP_creK (n i : Nat) : ∀ k (f : List (Term R)), LenP n f → LenP n (creK K i k f)
  | 0, _, h => h
  | k + 1, f, h => lenP_cre K n i _ (lenP_creK n i k f h)

theorem lenP_dPk (n i : Nat) : ∀ k (g : List (Term R)), LenP n g → LenP n (dPk K i k g)
  | 0, _, h => h
  | k + 1, g, h => lenP_dP K n i _ (lenP_dPk n i k g h)

theorem dPk_comm (i : Nat) : ∀ k (g : List (Term R)), dPk K i k (dP K i g) = dP K i (dPk K i k g)
  | 0, _ => rfl
  | k + 1, g => by show dP K i (dPk K i k (dP K i g)) = dP K i (dP K i (dPk K i k g)); rw [dPk_comm i k g]

theorem gp_comm (A B : List (Term R)) : Gp K (mulP K A B) = Gp K (mulP K B A) := by
  rw [Gp_mulP, Gp_mulP, Bf_comm]

/-- **Gaussian adjoint:** G((x_i f − ∂_i f) g) = G(f ∂_i g). -/
theorem adj (n i : Nat) (hi : i < n) (f g : List (Term R)) (hf : LenP n f) (hg : LenP n g) :
    Gp K (mulP K (cre K i f) g) = Gp K (mulP K f (dP K i g)) := by
  have hgf : LenP n (mulP K g f) := lenP_mulP K n f hf g hg
  have X : Gp K (mulP K (xi i f) g) = K.add (Gp K (mulP K f (dP K i g))) (Gp K (mulP K (dP K i f) g)) := by
    rw [gp_comm, ← xi_mulP K n i hi f hf g hg, Gp_ibp K i _ (wide_of_len n i hi _ hgf),
      Gp_eq_Bf K n _ (lenP_dP K n i _ hgf), leib_eqv K n i g f (one K n) (lenP_one K n), Bf_app_l,
      ← Gp_eq_Bf K n _ (lenP_mulP K n f hf _ (lenP_dP K n i g hg)),
      ← Gp_eq_Bf K n _ (lenP_mulP K n _ (lenP_dP K n i f hf) g hg), gp_comm K (dP K i g) f, gp_comm K g (dP K i f)]
  show Gp K (mulP K (xi i f ++ scaleT K (K.neg K.one) (dP K i f)) g) = _
  rw [mulP_app_l, Gp_app, scale_mulP, Gp_scale, X]
  generalize Gp K (mulP K f (dP K i g)) = z
  generalize Gp K (mulP K (dP K i f) g) = y
  lib_ring

/-- **Rodrigues formula.** -/
theorem rodrigues (n i : Nat) (hi : i < n) : ∀ k (f g : List (Term R)), LenP n f → LenP n g →
    Gp K (mulP K (creK K i k f) g) = Gp K (mulP K f (dPk K i k g))
  | 0, _, _, _, _ => rfl
  | k + 1, f, g, hf, hg => by
    show Gp K (mulP K (cre K i (creK K i k f)) g) = Gp K (mulP K f (dP K i (dPk K i k g)))
    rw [adj K n i hi _ g (lenP_creK K n i k f hf) hg, rodrigues n i hi k f (dP K i g) hf (lenP_dP K n i g hg), dPk_comm]

/-- G(He_k(x_i) · g) = G(∂_i^k g). -/
theorem herm_dk (n i : Nat) (hi : i < n) (k : Nat) (g : List (Term R)) (hg : LenP n g) :
    Gp K (mulP K (creK K i k (one K n)) g) = Gp K (dPk K i k g) := by
  rw [rodrigues K n i hi k _ g (lenP_one K n) hg, Gp_mulP_one K n _ (lenP_dPk K n i k g hg)]

/-! ### commutation [∂_i, x_i] = 1 and the Hermite ladder -/

theorem comm_row (i : Nat) (c : R) (α : List Nat) (hα : i < α.length) (r : List (Term R)) :
    row K (K.mul c (cN K (nthE i (incAt i α))), decAt i (incAt i α)) r
      = K.add (row K (K.mul c (cN K (nthE i α)), incAt i (decAt i α)) r) (row K (c, α) r) := by
  rw [GaussSphere.nth_inc_self i α hα, GaussSphere.dec_inc, cN_add, SU2CharExp.cN_one]
  cases Nat.lt_or_ge (nthE i α) 1 with
  | inl h0 =>
    have z : nthE i α = 0 := Nat.le_antisymm (Nat.le_of_lt_succ h0) (Nat.zero_le _)
    rw [z, show cN K 0 = K.zero from rfl, row_coef K (K.mul c K.zero) K.zero _ (by lib_ring), row_zero_coef, K.zero_add]
    rw [K.zero_add]; exact row_coef K (K.mul c K.one) c α (by lib_ring) r
  | inr h1 =>
    rw [incAt_decAt i α h1]
    exact row_single_eq K (K.mul c (cN K (nthE i α))) c (K.mul c (K.add (cN K (nthE i α)) K.one)) α (by lib_ring) r

theorem comm_xi (n i : Nat) (hi : i < n) : ∀ f : List (Term R), LenP n f → ∀ r : List (Term R),
    Bf K (dP K i (xi i f)) r = K.add (Bf K (xi i (dP K i f)) r) (Bf K f r)
  | [], _, _ => (K.zero_add _).symm
  | t :: f, h, r => by
    show K.add (row K (K.mul t.1 (cN K (nthE i (incAt i t.2))), decAt i (incAt i t.2)) r) (Bf K (dP K i (xi i f)) r)
      = K.add (K.add (row K (K.mul t.1 (cN K (nthE i t.2)), incAt i (decAt i t.2)) r) (Bf K (xi i (dP K i f)) r))
          (K.add (row K t r) (Bf K f r))
    rw [comm_row K i t.1 t.2 (by rw [h.1]; exact hi) r, comm_xi n i hi f h.2 r]
    generalize row K (K.mul t.1 (cN K (nthE i t.2)), incAt i (decAt i t.2)) r = a
    generalize Bf K (xi i (dP K i f)) r = b
    generalize Bf K f r = d
    generalize row K t r = e
    lib_ring

theorem dP_scale (i : Nat) (c : R) : ∀ p : List (Term R), dP K i (scaleT K c p) = scaleT K c (dP K i p)
  | [] => rfl
  | t :: p => by
    show (K.mul (K.mul c t.1) (cN K (nthE i t.2)), decAt i t.2) :: dP K i (scaleT K c p)
      = (K.mul c (K.mul t.1 (cN K (nthE i t.2))), decAt i t.2) :: scaleT K c (dP K i p)
    rw [K.mul_assoc, dP_scale i c p]

theorem xi_scale (i : Nat) (c : R) : ∀ p : List (Term R), xi i (scaleT K c p) = scaleT K c (xi i p)
  | [] => rfl
  | t :: p => by
    show (K.mul c t.1, incAt i t.2) :: xi i (scaleT K c p) = (K.mul c t.1, incAt i t.2) :: scaleT K c (xi i p)
    rw [xi_scale i c p]

/-- **[∂_i, a*_i] = 1** up to test-equivalence. -/
theorem comm_cre (n i : Nat) (hi : i < n) (f : List (Term R)) (hf : LenP n f) :
    Eqv K n (dP K i (cre K i f)) (cre K i (dP K i f) ++ f) := fun r _ => by
  show Bf K (dP K i (xi i f ++ scaleT K (K.neg K.one) (dP K i f))) r
    = Bf K ((xi i (dP K i f) ++ scaleT K (K.neg K.one) (dP K i (dP K i f))) ++ f) r
  rw [dP_app, Bf_app_l, Bf_app_l, Bf_app_l, dP_scale, Bf_scale, comm_xi K n i hi f hf r]
  generalize Bf K (xi i (dP K i f)) r = a
  generalize Bf K f r = b
  generalize Bf K (dP K i (dP K i f)) r = d
  lib_ring

/-- multiplying by x_i is symmetric under G -/
theorem Gp_xi_unit (n i : Nat) (hi : i < n) : ∀ X : List (Term R), LenP n X →
    Gp K (xi i X) = Bf K X [(K.one, unitE n i)]
  | [], _ => rfl
  | t :: X, h => by
    show K.add (K.mul t.1 (cN K (GM (incAt i t.2)))) (Gp K (xi i X))
      = K.add (K.add (K.mul (K.mul t.1 K.one) (cN K (GM (addE t.2 (unitE n i))))) K.zero) (Bf K X [(K.one, unitE n i)])
    rw [addE_comm, addE_unit n i hi t.2 h.1, Gp_xi_unit n i hi X h.2, DetChain.add_zero', K.mul_comm t.1 K.one, K.one_mul]

theorem eqv_xi (n i : Nat) (hi : i < n) (p q : List (Term R)) (hp : LenP n p) (hq : LenP n q) (h : Eqv K n p q) :
    Eqv K n (xi i p) (xi i q) := fun r hr => by
  have e : LenP n [(K.one, unitE n i)] := ⟨(len_incAt i _).trans (len_zeros n), trivial⟩
  have key : ∀ s : List (Term R), LenP n s → Bf K (xi i s) r = Bf K s (mulP K r [(K.one, unitE n i)]) := fun s hs => by
    rw [← Gp_mulP, gp_comm, ← xi_mulP K n i hi s hs r hr, Gp_xi_unit K n i hi _ (lenP_mulP K n s hs r hr),
      Bf_mulP_swap, Bf_assoc]
  rw [key p hp, key q hq, h _ (lenP_mulP K n _ e r hr)]

theorem eqv_cre (n i : Nat) (hi : i < n) (p q : List (Term R)) (hp : LenP n p) (hq : LenP n q) (h : Eqv K n p q) :
    Eqv K n (cre K i p) (cre K i q) :=
  eqv_app K n _ _ _ _ (eqv_xi K n i hi p q hp hq h) (eqv_scale K n _ _ _ (eqv_dP K n i hi p q hp hq h))

theorem cre_scale (n i : Nat) (c : R) (p : List (Term R)) : Eqv K n (cre K i (scaleT K c p)) (scaleT K c (cre K i p)) :=
  fun r _ => by
    show Bf K (xi i (scaleT K c p) ++ scaleT K (K.neg K.one) (dP K i (scaleT K c p))) r
      = Bf K (scaleT K c (xi i p ++ scaleT K (K.neg K.one) (dP K i p))) r
    rw [xi_scale, dP_scale, scale_app, Bf_app_l, Bf_app_l, Bf_scale, Bf_scale, Bf_scale, Bf_scale, Bf_scale]
    generalize Bf K (xi i p) r = a
    generalize Bf K (dP K i p) r = b
    lib_ring

def He (n i m : Nat) : List (Term R) := creK K i m (one K n)

theorem lenP_He (n i m : Nat) : LenP n (He K n i m) := lenP_creK K n i m _ (lenP_one K n)

/-- **∂_i He_{m+1} ≡ (m+1) He_m.** -/
theorem dP_He (n i : Nat) (hi : i < n) : ∀ m, Eqv K n (dP K i (He K n i (m + 1))) (scaleT K (cN K (m + 1)) (He K n i m))
  | 0 => by
    show Eqv K n (dP K i (cre K i (one K n))) _
    refine eqv_trans K n _ _ _ (comm_cre K n i hi _ (lenP_one K n)) ?_
    have h1 : Eqv K n (cre K i (dP K i (one K n))) (cre K i []) :=
      eqv_cre K n i hi _ _ (lenP_dP K n i _ (lenP_one K n)) trivial (dP_one K n i)
    refine eqv_trans K n _ _ _ (eqv_app K n _ _ _ _ h1 (eqv_refl K n (one K n))) ?_
    show Eqv K n (one K n) (scaleT K (cN K 1) (one K n))
    rw [SU2CharExp.cN_one]; exact eqv_symm K n _ _ (eqv_scale_one K n _)
  | m + 1 => by
    have ih := dP_He n i hi m
    have hm := lenP_He K n i m
    have hm1 := lenP_He K n i (m + 1)
    show Eqv K n (dP K i (cre K i (He K n i (m + 1)))) _
    refine eqv_trans K n _ _ _ (comm_cre K n i hi _ hm1) ?_
    have h1 := eqv_cre K n i hi _ _ (lenP_dP K n i _ hm1) (lenP_scale K n _ _ hm) ih
    have h2 := eqv_trans K n _ _ _ h1 (cre_scale K n i (cN K (m + 1)) (He K n i m))
    refine eqv_trans K n _ _ _ (eqv_app K n _ _ _ _ h2 (eqv_refl K n (He K n i (m + 1)))) ?_
    show Eqv K n (scaleT K (cN K (m + 1)) (He K n i (m + 1)) ++ He K n i (m + 1)) (scaleT K (cN K (m + 1 + 1)) (He K n i (m + 1)))
    have e : cN K (m + 1 + 1) = K.add (cN K (m + 1)) K.one := by
      show K.add K.one (cN K (m + 1)) = _; rw [K.add_comm]
    rw [e]; exact eqv_add_scale K n _ _

/-! ### orthogonality of one-variable Hermite polynomials -/

theorem eqv_dPk (n i : Nat) (hi : i < n) : ∀ k (p q : List (Term R)), LenP n p → LenP n q → Eqv K n p q →
    Eqv K n (dPk K i k p) (dPk K i k q)
  | 0, _, _, _, _, h => h
  | k + 1, p, q, hp, hq, h =>
    eqv_dP K n i hi _ _ (lenP_dPk K n i k p hp) (lenP_dPk K n i k q hq) (eqv_dPk n i hi k p q hp hq h)

theorem dPk_scale (i : Nat) (c : R) : ∀ k (p : List (Term R)), dPk K i k (scaleT K c p) = scaleT K c (dPk K i k p)
  | 0, _ => rfl
  | k + 1, p => by show dP K i (dPk K i k (scaleT K c p)) = _; rw [dPk_scale i c k p, dP_scale]; rfl

theorem dPk_add (i : Nat) (g : List (Term R)) (a : Nat) : ∀ b, dPk K i (a + b) g = dPk K i b (dPk K i a g)
  | 0 => rfl
  | b + 1 => by show dP K i (dPk K i (a + b) g) = dP K i (dPk K i b (dPk K i a g)); rw [dPk_add i g a b]

theorem dPk_nil (i : Nat) : ∀ k, dPk K i k ([] : List (Term R)) = []
  | 0 => rfl
  | k + 1 => by show dP K i (dPk K i k []) = []; rw [dPk_nil i k]; rfl

/-- falling factorial (m+k)!/m! -/
def ff : Nat → Nat → Nat
  | _, 0 => 1
  | m, k + 1 => ff (m + 1) k * (m + 1)

theorem ff_fact : ∀ k m, ff m k * SU2CharExp.fact m = SU2CharExp.fact (m + k)
  | 0, m => by show 1 * SU2CharExp.fact m = SU2CharExp.fact m; exact Nat.one_mul _
  | k + 1, m => by
    show ff (m + 1) k * (m + 1) * SU2CharExp.fact m = _
    have h := ff_fact k (m + 1)
    have e : m + 1 + k = m + (k + 1) := by lib_nat
    rw [e] at h; rw [← h]
    show _ = ff (m + 1) k * (SU2CharExp.fact m * (m + 1))
    generalize ff (m + 1) k = a; generalize SU2CharExp.fact m = b; lib_nat

/-- **∂_i^k He_{m+k} ≡ (m+k)!/m! · He_m.** -/
theorem dPk_He (n i : Nat) (hi : i < n) : ∀ k m, Eqv K n (dPk K i k (He K n i (m + k))) (scaleT K (cN K (ff m k)) (He K n i m))
  | 0, m => by show Eqv K n (He K n i m) (scaleT K (cN K 1) _); rw [SU2CharExp.cN_one]; exact eqv_symm K n _ _ (eqv_scale_one K n _)
  | k + 1, m => by
    have e : m + (k + 1) = (m + 1) + k := by lib_nat
    rw [e]
    have ih := dPk_He n i hi k (m + 1)
    show Eqv K n (dP K i (dPk K i k (He K n i (m + 1 + k)))) _
    refine eqv_trans K n _ _ _ (eqv_dP K n i hi _ _ (lenP_dPk K n i k _ (lenP_He K n i _))
      (lenP_scale K n _ _ (lenP_He K n i _)) ih) ?_
    rw [dP_scale]
    refine eqv_trans K n _ _ _ (eqv_scale K n _ _ _ (dP_He K n i hi m)) ?_
    rw [scale_scale, ← SU2CharExp.cN_mul]
    exact eqv_refl K n _

theorem dPk_He_low (n i : Nat) (hi : i < n) (m j : Nat) : Eqv K n (dPk K i (m + 1 + j) (He K n i m)) [] := by
  rw [dPk_add]
  have h0 : Eqv K n (dPk K i (m + 1) (He K n i m)) [] := by
    show Eqv K n (dP K i (dPk K i m (He K n i m))) []
    have a := dPk_He K n i hi m 0
    rw [Nat.zero_add] at a
    refine eqv_trans K n _ _ _ (eqv_dP K n i hi _ _ (lenP_dPk K n i m _ (lenP_He K n i m))
      (lenP_scale K n _ _ (lenP_He K n i 0)) a) ?_
    rw [dP_scale]
    have := eqv_scale K n (cN K (ff 0 m)) _ _ (dP_one K n i)
    exact this
  have := eqv_dPk K n i hi j _ [] (lenP_dPk K n i (m + 1) _ (lenP_He K n i m)) trivial h0
  rw [dPk_nil] at this; exact this

theorem Gp_eqv (n : Nat) (p q : List (Term R)) (hp : LenP n p) (hq : LenP n q) (h : Eqv K n p q) : Gp K p = Gp K q := by
  rw [Gp_eq_Bf K n p hp, Gp_eq_Bf K n q hq, h _ (lenP_one K n)]

/-- **Hermite orthogonality:** G(He_m(x_i)²) = m!. -/
theorem herm_sq (n i : Nat) (hi : i < n) (m : Nat) :
    Gp K (mulP K (He K n i m) (He K n i m)) = cN K (SU2CharExp.fact m) := by
  show Gp K (mulP K (creK K i m (one K n)) (He K n i m)) = _
  rw [herm_dk K n i hi m _ (lenP_He K n i m)]
  have a := dPk_He K n i hi m 0
  rw [Nat.zero_add] at a
  rw [Gp_eqv K n _ _ (lenP_dPk K n i m _ (lenP_He K n i m)) (lenP_scale K n _ _ (lenP_He K n i 0)) a, Gp_scale]
  show K.mul _ (K.add (K.mul K.one (cN K (GM (zerosL n)))) K.zero) = _
  rw [GM_zeros, SU2CharExp.cN_one, K.one_mul, DetChain.add_zero', K.mul_comm, K.one_mul]
  have f := ff_fact m 0
  rw [Nat.zero_add] at f
  rw [← f]; exact congrArg (cN K) (Nat.mul_one _).symm

theorem split_lt (a : Nat) : ∀ b, a < b → ∃ j, b = a + 1 + j
  | 0, h => absurd h (Nat.not_lt_zero _)
  | b + 1, h => by
    cases Nat.lt_or_ge a b with
    | inl hl => obtain ⟨j, hj⟩ := split_lt a b hl; exact ⟨j + 1, by rw [hj]; rfl⟩
    | inr hg => exact ⟨0, congrArg Nat.succ (Nat.le_antisymm hg (Nat.le_of_lt_succ h))⟩

/-- **Hermite orthogonality:** G(He_k(x_i) He_m(x_i)) = 0 for k ≠ m. -/
theorem herm_orth (n i : Nat) (hi : i < n) (k m : Nat) (h : k ≠ m) :
    Gp K (mulP K (He K n i k) (He K n i m)) = K.zero := by
  have low : ∀ a b, a < b → Gp K (mulP K (He K n i b) (He K n i a)) = K.zero := by
    intro a b hab
    obtain ⟨j, hj⟩ := split_lt a b hab
    show Gp K (mulP K (creK K i b (one K n)) (He K n i a)) = _
    rw [herm_dk K n i hi b _ (lenP_He K n i a), hj,
      Gp_eqv K n _ ([] : List (Term R)) (lenP_dPk K n i _ _ (lenP_He K n i a)) trivial (dPk_He_low K n i hi a j)]
    rfl
  cases Nat.lt_or_ge k m with
  | inl hl => rw [gp_comm]; exact low k m hl
  | inr hg =>
    cases Nat.lt_or_ge m k with
    | inl hl => exact low m k hl
    | inr hk => exact absurd (Nat.le_antisymm hk hg) h

/-! ### multi-index Hermite polynomials and the Rodrigues formula -/

/-- He_α(x_k, x_{k+1}, …) = Π_i He_{α_i}(x_{k+i}) -/
def Hm (n : Nat) : Nat → List Nat → List (Term R)
  | _, [] => one K n
  | k, a :: α => creK K k a (Hm n (k + 1) α)

/-- ∂^α along x_k, x_{k+1}, … -/
def Dm : Nat → List Nat → List (Term R) → List (Term R)
  | _, [], g => g
  | k, a :: α, g => Dm (k + 1) α (dPk K k a g)

theorem lenP_Hm (n : Nat) : ∀ k α, LenP n (Hm K n k α)
  | _, [] => lenP_one K n
  | k, a :: α => lenP_creK K n k a _ (lenP_Hm n (k + 1) α)

theorem lenP_Dm (n : Nat) : ∀ k α (g : List (Term R)), LenP n g → LenP n (Dm K k α g)
  | _, [], _, h => h
  | k, a :: α, g, h => lenP_Dm n (k + 1) α _ (lenP_dPk K n k a g h)

/-- **Multi-index Rodrigues formula:** G(He_α · q) = G(∂^α q). -/
theorem rodrigues_m (n : Nat) : ∀ (k : Nat) (α : List Nat) (q : List (Term R)), k + α.length ≤ n → LenP n q →
    Gp K (mulP K (Hm K n k α) q) = Gp K (Dm K k α q)
  | _, [], q, _, hq => Gp_mulP_one K n q hq
  | k, a :: α, q, hk, hq => by
    have hkn : k < n := Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right k α.length)) hk
    have hk' : k + 1 + α.length ≤ n := by
      have : k + 1 + α.length = k + (α.length + 1) := by lib_nat
      rw [this]; exact hk
    show Gp K (mulP K (creK K k a (Hm K n (k + 1) α)) q) = Gp K (Dm K (k + 1) α (dPk K k a q))
    rw [rodrigues K n k hkn a _ q (lenP_Hm K n (k + 1) α) hq,
      rodrigues_m n (k + 1) α _ hk' (lenP_dPk K n k a q hq)]

/-! ### different coordinates commute -/

theorem nth_inc_ne : ∀ (i j : Nat) (α : List Nat), i ≠ j → nthE j (incAt i α) = nthE j α
  | 0, 0, _, h => absurd rfl h
  | 0, _ + 1, [], _ => rfl
  | _ + 1, 0, [], _ => rfl
  | _ + 1, _ + 1, [], _ => rfl
  | 0, _ + 1, _ :: _, _ => rfl
  | _ + 1, 0, _ :: _, _ => rfl
  | i + 1, j + 1, _ :: α, h => nth_inc_ne i j α (fun e => h (congrArg Nat.succ e))

theorem nth_dec_ne : ∀ (i j : Nat) (α : List Nat), i ≠ j → nthE j (decAt i α) = nthE j α
  | 0, 0, _, h => absurd rfl h
  | 0, _ + 1, [], _ => rfl
  | _ + 1, 0, [], _ => rfl
  | _ + 1, _ + 1, [], _ => rfl
  | 0, _ + 1, _ :: _, _ => rfl
  | _ + 1, 0, _ :: _, _ => rfl
  | i + 1, j + 1, _ :: α, h => nth_dec_ne i j α (fun e => h (congrArg Nat.succ e))

theorem dec_inc_comm : ∀ (i j : Nat) (α : List Nat), i ≠ j → decAt j (incAt i α) = incAt i (decAt j α)
  | 0, 0, _, h => absurd rfl h
  | 0, _ + 1, [], _ => rfl
  | _ + 1, 0, [], _ => rfl
  | _ + 1, _ + 1, [], _ => rfl
  | 0, _ + 1, _ :: _, _ => rfl
  | _ + 1, 0, _ :: _, _ => rfl
  | i + 1, j + 1, a :: α, h => by
    show a :: decAt j (incAt i α) = a :: incAt i (decAt j α)
    rw [dec_inc_comm i j α (fun e => h (congrArg Nat.succ e))]

theorem dec_dec_comm : ∀ (i j : Nat) (α : List Nat), i ≠ j → decAt j (decAt i α) = decAt i (decAt j α)
  | 0, 0, _, h => absurd rfl h
  | 0, _ + 1, [], _ => rfl
  | _ + 1, 0, [], _ => rfl
  | _ + 1, _ + 1, [], _ => rfl
  | 0, _ + 1, _ :: _, _ => rfl
  | _ + 1, 0, _ :: _, _ => rfl
  | i + 1, j + 1, a :: α, h => by
    show a :: decAt j (decAt i α) = a :: decAt i (decAt j α)
    rw [dec_dec_comm i j α (fun e => h (congrArg Nat.succ e))]

theorem dP_xi_cross (i j : Nat) (h : i ≠ j) : ∀ f : List (Term R), dP K j (xi i f) = xi i (dP K j f)
  | [] => rfl
  | t :: f => by
    show (K.mul t.1 (cN K (nthE j (incAt i t.2))), decAt j (incAt i t.2)) :: dP K j (xi i f)
      = (K.mul t.1 (cN K (nthE j t.2)), incAt i (decAt j t.2)) :: xi i (dP K j f)
    rw [nth_inc_ne i j t.2 h, dec_inc_comm i j t.2 h, dP_xi_cross i j h f]

theorem dP_dP_cross (i j : Nat) (h : i ≠ j) : ∀ f : List (Term R), dP K j (dP K i f) = dP K i (dP K j f)
  | [] => rfl
  | t :: f => by
    show (K.mul (K.mul t.1 (cN K (nthE i t.2))) (cN K (nthE j (decAt i t.2))), decAt j (decAt i t.2)) :: dP K j (dP K i f)
      = (K.mul (K.mul t.1 (cN K (nthE j t.2))) (cN K (nthE i (decAt j t.2))), decAt i (decAt j t.2)) :: dP K i (dP K j f)
    rw [nth_dec_ne i j t.2 h, nth_dec_ne j i t.2 (fun e => h e.symm), dec_dec_comm i j t.2 h, dP_dP_cross i j h f,
      K.mul_assoc, K.mul_assoc, K.mul_comm (cN K (nthE i t.2))]

theorem dP_cre_cross (i j : Nat) (h : i ≠ j) (f : List (Term R)) : dP K j (cre K i f) = cre K i (dP K j f) := by
  show dP K j (xi i f ++ scaleT K (K.neg K.one) (dP K i f)) = xi i (dP K j f) ++ scaleT K (K.neg K.one) (dP K i (dP K j f))
  rw [dP_app, dP_xi_cross K i j h, dP_scale, dP_dP_cross K i j h]

theorem dP_creK_cross (i j : Nat) (h : i ≠ j) : ∀ c (f : List (Term R)), dP K j (creK K i c f) = creK K i c (dP K j f)
  | 0, _ => rfl
  | c + 1, f => by show dP K j (cre K i (creK K i c f)) = cre K i (creK K i c (dP K j f)); rw [dP_cre_cross K i j h, dP_creK_cross i j h c f]

theorem dPk_creK_cross (i j : Nat) (h : i ≠ j) (c : Nat) : ∀ a (f : List (Term R)), dPk K j a (creK K i c f) = creK K i c (dPk K j a f)
  | 0, _ => rfl
  | a + 1, f => by show dP K j (dPk K j a (creK K i c f)) = _; rw [dPk_creK_cross i j h c a f, dP_creK_cross K i j h]; rfl

theorem Dm_creK (k c : Nat) (X : List (Term R)) : ∀ (l : Nat) (α : List Nat), k < l →
    Dm K l α (creK K k c X) = creK K k c (Dm K l α X)
  | _, [], _ => rfl
  | l, a :: α, h => by
    show Dm K (l + 1) α (dPk K l a (creK K k c X)) = creK K k c (Dm K (l + 1) α (dPk K l a X))
    rw [dPk_creK_cross K k l (Nat.ne_of_lt h) c a X, Dm_creK k c _ (l + 1) α (Nat.lt_succ_of_lt h)]

theorem cre_nil (i : Nat) : cre K i ([] : List (Term R)) = [] := rfl

theorem creK_nil (i : Nat) : ∀ c, creK K i c ([] : List (Term R)) = []
  | 0 => rfl
  | c + 1 => by show cre K i (creK K i c []) = []; rw [creK_nil i c]; rfl

theorem eqv_creK (n i : Nat) (hi : i < n) : ∀ c (p q : List (Term R)), LenP n p → LenP n q → Eqv K n p q →
    Eqv K n (creK K i c p) (creK K i c q)
  | 0, _, _, _, _, h => h
  | c + 1, p, q, hp, hq, h =>
    eqv_cre K n i hi _ _ (lenP_creK K n i c p hp) (lenP_creK K n i c q hq) (eqv_creK n i hi c p q hp hq h)

/-- ∂_k He_β(x_l, …) ≡ 0 for k < l -/
theorem dP_Hm_low (n k : Nat) : ∀ (l : Nat) (β : List Nat), k < l → l + β.length ≤ n → Eqv K n (dP K k (Hm K n l β)) []
  | _, [], _, _ => dP_one K n k
  | l, b :: β, h, hl => by
    have hln : l < n := Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right l β.length)) hl
    have hl' : l + 1 + β.length ≤ n := by
      have : l + 1 + β.length = l + (β.length + 1) := by lib_nat
      rw [this]; exact hl
    show Eqv K n (dP K k (creK K l b (Hm K n (l + 1) β))) []
    rw [dP_creK_cross K l k (fun e => Nat.ne_of_lt h e.symm)]
    have := eqv_creK K n l hln b _ [] (lenP_dP K n k _ (lenP_Hm K n (l + 1) β)) trivial
      (dP_Hm_low n k (l + 1) β (Nat.lt_succ_of_lt h) hl')
    rw [creK_nil] at this; exact this

/-! ### the ladder on a factor independent of x_k -/

theorem dP_creK_F (n k : Nat) (hk : k < n) (F : List (Term R)) (hF : LenP n F) (h0 : Eqv K n (dP K k F) []) :
    ∀ m, Eqv K n (dP K k (creK K k (m + 1) F)) (scaleT K (cN K (m + 1)) (creK K k m F))
  | 0 => by
    show Eqv K n (dP K k (cre K k F)) _
    refine eqv_trans K n _ _ _ (comm_cre K n k hk _ hF) ?_
    have h1 : Eqv K n (cre K k (dP K k F)) (cre K k []) := eqv_cre K n k hk _ _ (lenP_dP K n k _ hF) trivial h0
    refine eqv_trans K n _ _ _ (eqv_app K n _ _ _ _ h1 (eqv_refl K n F)) ?_
    show Eqv K n F (scaleT K (cN K 1) F)
    rw [SU2CharExp.cN_one]; exact eqv_symm K n _ _ (eqv_scale_one K n _)
  | m + 1 => by
    have ih := dP_creK_F n k hk F hF h0 m
    have hm := lenP_creK K n k m F hF
    have hm1 := lenP_creK K n k (m + 1) F hF
    show Eqv K n (dP K k (cre K k (creK K k (m + 1) F))) _
    refine eqv_trans K n _ _ _ (comm_cre K n k hk _ hm1) ?_
    have h1 := eqv_cre K n k hk _ _ (lenP_dP K n k _ hm1) (lenP_scale K n _ _ hm) ih
    have h2 := eqv_trans K n _ _ _ h1 (cre_scale K n k (cN K (m + 1)) (creK K k m F))
    refine eqv_trans K n _ _ _ (eqv_app K n _ _ _ _ h2 (eqv_refl K n (creK K k (m + 1) F))) ?_
    show Eqv K n (scaleT K (cN K (m + 1)) (creK K k (m + 1) F) ++ creK K k (m + 1) F) (scaleT K (cN K (m + 1 + 1)) (creK K k (m + 1) F))
    have e : cN K (m + 1 + 1) = K.add (cN K (m + 1)) K.one := by
      show K.add K.one (cN K (m + 1)) = _; rw [K.add_comm]
    rw [e]; exact eqv_add_scale K n _ _

theorem dPk_creK_F (n k : Nat) (hk : k < n) (F : List (Term R)) (hF : LenP n F) (h0 : Eqv K n (dP K k F) []) :
    ∀ a m, Eqv K n (dPk K k a (creK K k (m + a) F)) (scaleT K (cN K (ff m a)) (creK K k m F))
  | 0, m => by show Eqv K n (creK K k m F) (scaleT K (cN K 1) _); rw [SU2CharExp.cN_one]; exact eqv_symm K n _ _ (eqv_scale_one K n _)
  | a + 1, m => by
    have e : m + (a + 1) = (m + 1) + a := by lib_nat
    rw [e]
    have ih := dPk_creK_F n k hk F hF h0 a (m + 1)
    show Eqv K n (dP K k (dPk K k a (creK K k (m + 1 + a) F))) _
    refine eqv_trans K n _ _ _ (eqv_dP K n k hk _ _ (lenP_dPk K n k a _ (lenP_creK K n k _ F hF))
      (lenP_scale K n _ _ (lenP_creK K n k _ F hF)) ih) ?_
    rw [dP_scale]
    refine eqv_trans K n _ _ _ (eqv_scale K n _ _ _ (dP_creK_F K n k hk F hF h0 m)) ?_
    rw [scale_scale, ← SU2CharExp.cN_mul]
    exact eqv_refl K n _

theorem dPk_creK_low (n k : Nat) (hk : k < n) (F : List (Term R)) (hF : LenP n F) (h0 : Eqv K n (dP K k F) []) (m j : Nat) :
    Eqv K n (dPk K k (m + 1 + j) (creK K k m F)) [] := by
  rw [dPk_add]
  have h1 : Eqv K n (dPk K k (m + 1) (creK K k m F)) [] := by
    show Eqv K n (dP K k (dPk K k m (creK K k m F))) []
    have a := dPk_creK_F K n k hk F hF h0 m 0
    rw [Nat.zero_add] at a
    refine eqv_trans K n _ _ _ (eqv_dP K n k hk _ _ (lenP_dPk K n k m _ (lenP_creK K n k m F hF))
      (lenP_scale K n _ _ (lenP_creK K n k 0 F hF)) a) ?_
    rw [dP_scale]
    exact eqv_scale K n (cN K (ff 0 m)) _ _ h0
  have := eqv_dPk K n k hk j _ [] (lenP_dPk K n k (m + 1) _ (lenP_creK K n k m F hF)) trivial h1
  rw [dPk_nil] at this; exact this

/-! ### multi-index orthogonality -/

def factL : List Nat → Nat
  | [] => 1
  | a :: α => SU2CharExp.fact a * factL α

theorem Dm_scale (c : R) : ∀ (l : Nat) (α : List Nat) (Y : List (Term R)), Dm K l α (scaleT K c Y) = scaleT K c (Dm K l α Y)
  | _, [], _ => rfl
  | l, a :: α, Y => by show Dm K (l + 1) α (dPk K l a (scaleT K c Y)) = _; rw [dPk_scale, Dm_scale c (l + 1) α]; rfl

theorem Dm_nil : ∀ (l : Nat) (α : List Nat), Dm K l α ([] : List (Term R)) = []
  | _, [] => rfl
  | l, a :: α => by show Dm K (l + 1) α (dPk K l a []) = []; rw [dPk_nil, Dm_nil (l + 1) α]

theorem eqv_Dm (n : Nat) : ∀ (l : Nat) (α : List Nat) (p q : List (Term R)), l + α.length ≤ n → LenP n p → LenP n q →
    Eqv K n p q → Eqv K n (Dm K l α p) (Dm K l α q)
  | _, [], _, _, _, _, _, h => h
  | l, a :: α, p, q, hl, hp, hq, h => by
    have hln : l < n := Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right l α.length)) hl
    have hl' : l + 1 + α.length ≤ n := by
      have : l + 1 + α.length = l + (α.length + 1) := by lib_nat
      rw [this]; exact hl
    exact eqv_Dm n (l + 1) α _ _ hl' (lenP_dPk K n l a p hp) (lenP_dPk K n l a q hq) (eqv_dPk K n l hln a p q hp hq h)

theorem Gp_creK_pos (n k : Nat) (hk : k < n) (c : Nat) (X : List (Term R)) (hX : LenP n X) :
    Gp K (creK K k (c + 1) X) = K.zero := by
  have hY : Eqv K n (dPk K k (c + 1) (one K n)) [] := by
    have := dPk_creK_low K n k hk (one K n) (lenP_one K n) (dP_one K n k) 0 c
    rw [show 0 + 1 + c = c + 1 by lib_nat] at this; exact this
  rw [← Gp_mulP_one K n _ (lenP_creK K n k _ X hX), gp_comm, rodrigues K n k hk (c + 1) X _ hX (lenP_one K n), Gp_mulP,
    Bf_comm, hY X hX]
  rfl

theorem split_le (a : Nat) : ∀ b, a ≤ b → ∃ c, b = c + a
  | b, h => by
    cases Nat.lt_or_ge a b with
    | inl hl => obtain ⟨j, hj⟩ := split_lt a b hl; exact ⟨1 + j, by rw [hj]; lib_nat⟩
    | inr hg => exact ⟨0, by rw [Nat.zero_add]; exact Nat.le_antisymm hg h⟩

/-- one coordinate of the multi-index computation -/
theorem step_le (n k : Nat) (a c : Nat) (α β : List Nat) (hl : k + (β.length + 1) ≤ n) (hab : α.length = β.length) :
    Gp K (Dm K k (a :: α) (Hm K n k ((c + a) :: β)))
      = K.mul (cN K (ff c a)) (Gp K (creK K k c (Dm K (k + 1) α (Hm K n (k + 1) β)))) := by
  have hkn : k < n := Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right k β.length)) hl
  have hl' : k + 1 + α.length ≤ n := by
    rw [hab]; have : k + 1 + β.length = k + (β.length + 1) := by lib_nat
    rw [this]; exact hl
  have hl'' : k + 1 + β.length ≤ n := by rw [← hab]; exact hl'
  let H := Hm K n (k + 1) β
  have hH : LenP n H := lenP_Hm K n (k + 1) β
  have h0 := dP_Hm_low K n k (k + 1) β (Nat.lt_succ_self k) hl''
  show Gp K (Dm K (k + 1) α (dPk K k a (creK K k (c + a) H))) = _
  have e := dPk_creK_F K n k hkn H hH h0 a c
  have e2 := eqv_Dm K n (k + 1) α _ _ hl' (lenP_dPk K n k a _ (lenP_creK K n k _ H hH))
    (lenP_scale K n _ _ (lenP_creK K n k c H hH)) e
  rw [Gp_eqv K n _ _ (lenP_Dm K n _ α _ (lenP_dPk K n k a _ (lenP_creK K n k _ H hH)))
    (lenP_Dm K n _ α _ (lenP_scale K n _ _ (lenP_creK K n k c H hH))) e2, Dm_scale, Gp_scale,
    Dm_creK K k c H (k + 1) α (Nat.lt_succ_self k)]

theorem step_gt (n k : Nat) (b j : Nat) (α β : List Nat) (hl : k + (β.length + 1) ≤ n) (hab : α.length = β.length) :
    Gp K (Dm K k ((b + 1 + j) :: α) (Hm K n k (b :: β))) = K.zero := by
  have hkn : k < n := Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right k β.length)) hl
  have hl' : k + 1 + α.length ≤ n := by
    rw [hab]; have : k + 1 + β.length = k + (β.length + 1) := by lib_nat
    rw [this]; exact hl
  have hl'' : k + 1 + β.length ≤ n := by rw [← hab]; exact hl'
  let H := Hm K n (k + 1) β
  have hH : LenP n H := lenP_Hm K n (k + 1) β
  have h0 := dP_Hm_low K n k (k + 1) β (Nat.lt_succ_self k) hl''
  show Gp K (Dm K (k + 1) α (dPk K k (b + 1 + j) (creK K k b H))) = _
  have e := eqv_Dm K n (k + 1) α _ [] hl' (lenP_dPk K n k _ _ (lenP_creK K n k b H hH)) trivial
    (dPk_creK_low K n k hkn H hH h0 b j)
  rw [Dm_nil] at e
  rw [Gp_eqv K n _ [] (lenP_Dm K n _ α _ (lenP_dPk K n k _ _ (lenP_creK K n k b H hH))) trivial e]; rfl

/-- **Multi-index orthogonality, diagonal:** G(∂^α He_α) = α!. -/
theorem diag_m (n : Nat) : ∀ (k : Nat) (α : List Nat), k + α.length ≤ n → Gp K (Dm K k α (Hm K n k α)) = cN K (factL α)
  | _, [], _ => by
    show K.add (K.mul K.one (cN K (GM (zerosL n)))) K.zero = cN K 1
    rw [GM_zeros, K.one_mul, DetChain.add_zero']
  | k, a :: α, hl => by
    have hl' : k + 1 + α.length ≤ n := by
      have : k + 1 + α.length = k + (α.length + 1) := by lib_nat
      rw [this]; exact hl
    have s := step_le K n k a 0 α α hl rfl
    rw [Nat.zero_add] at s
    rw [s]
    show K.mul _ (Gp K (Dm K (k + 1) α (Hm K n (k + 1) α))) = cN K (SU2CharExp.fact a * factL α)
    rw [diag_m n (k + 1) α hl', SU2CharExp.cN_mul]
    have f := ff_fact a 0
    rw [Nat.zero_add] at f
    rw [← f]; exact congrArg (fun z => K.mul (cN K z) _) (Nat.mul_one _).symm

/-- **Multi-index orthogonality, off-diagonal:** G(∂^α He_β) = 0 for α ≠ β of the same length. -/
theorem off_m (n : Nat) : ∀ (k : Nat) (α β : List Nat), α.length = β.length → k + β.length ≤ n → α ≠ β →
    Gp K (Dm K k α (Hm K n k β)) = K.zero
  | _, [], [], _, _, h => absurd rfl h
  | _, [], _ :: _, hl, _, _ => Nat.noConfusion hl
  | _, _ :: _, [], hl, _, _ => Nat.noConfusion hl
  | k, a :: α, b :: β, hl, hn, h => by
    have hab : α.length = β.length := Nat.succ.inj hl
    have hn' : k + 1 + β.length ≤ n := by
      have : k + 1 + β.length = k + (β.length + 1) := by lib_nat
      rw [this]; exact hn
    cases Nat.lt_or_ge b a with
    | inl hgt =>
      obtain ⟨j, hj⟩ := split_lt b a hgt
      rw [hj]; exact step_gt K n k b j α β hn hab
    | inr hle =>
      obtain ⟨c, hc⟩ := split_le a b hle
      rw [hc, step_le K n k a c α β hn hab]
      cases c with
      | succ c' =>
        rw [Gp_creK_pos K n k (Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right k β.length)) hn) c' _
          (lenP_Dm K n _ α _ (lenP_Hm K n (k + 1) β)), DetChain.mul_zero]
      | zero =>
        have hαβ : α ≠ β := fun e => h (by rw [hc, Nat.zero_add, e])
        show K.mul _ (Gp K (Dm K (k + 1) α (Hm K n (k + 1) β))) = K.zero
        rw [off_m n (k + 1) α β hab hn' hαβ, DetChain.mul_zero]

/-- **G(He_α He_β) = α! δ_αβ** on ℝⁿ (α, β of length n). -/
theorem herm_m (n : Nat) (α β : List Nat) (ha : α.length = n) (hb : β.length = n) :
    Gp K (mulP K (Hm K n 0 α) (Hm K n 0 β)) = (if α = β then cN K (factL α) else K.zero) := by
  rw [rodrigues_m K n 0 α _ (by rw [Nat.zero_add, ha]; exact Nat.le_refl n) (lenP_Hm K n 0 β)]
  by_cases h : α = β
  · rw [if_pos h, ← h]; exact diag_m K n 0 α (by rw [Nat.zero_add, ha]; exact Nat.le_refl n)
  · rw [if_neg h]; exact off_m K n 0 α β (ha.trans hb.symm) (by rw [Nat.zero_add, hb]; exact Nat.le_refl n) h

/-! ### x_i He_α ≡ He_{α+e_i} + α_i He_{α−e_i} -/

theorem inc_inc_comm : ∀ (i k : Nat) (α : List Nat), incAt i (incAt k α) = incAt k (incAt i α)
  | 0, 0, _ => rfl
  | 0, _ + 1, [] => rfl
  | _ + 1, 0, [] => rfl
  | _ + 1, _ + 1, [] => rfl
  | 0, _ + 1, _ :: _ => rfl
  | _ + 1, 0, _ :: _ => rfl
  | i + 1, k + 1, a :: α => by
    show a :: incAt i (incAt k α) = a :: incAt k (incAt i α); rw [inc_inc_comm i k α]

theorem xi_xi_comm (i k : Nat) : ∀ f : List (Term R), xi i (xi k f) = xi k (xi i f)
  | [] => rfl
  | t :: f => by
    show (t.1, incAt i (incAt k t.2)) :: xi i (xi k f) = (t.1, incAt k (incAt i t.2)) :: xi k (xi i f)
    rw [inc_inc_comm i k t.2, xi_xi_comm i k f]

theorem xi_cre_cross (i k : Nat) (h : k ≠ i) (f : List (Term R)) : xi i (cre K k f) = cre K k (xi i f) := by
  show xi i (xi k f ++ scaleT K (K.neg K.one) (dP K k f)) = xi k (xi i f) ++ scaleT K (K.neg K.one) (dP K k (xi i f))
  rw [xi_app, xi_xi_comm i k, xi_scale, dP_xi_cross K i k (fun e => h e.symm)]

theorem xi_creK_cross (i k : Nat) (h : k ≠ i) : ∀ a (f : List (Term R)), xi i (creK K k a f) = creK K k a (xi i f)
  | 0, _ => rfl
  | a + 1, f => by
    show xi i (cre K k (creK K k a f)) = cre K k (creK K k a (xi i f))
    rw [xi_cre_cross K i k h, xi_creK_cross i k h a f]

theorem eqv_cre_app (n k : Nat) (p q : List (Term R)) : Eqv K n (cre K k (p ++ q)) (cre K k p ++ cre K k q) := fun r _ => by
  show Bf K (xi k (p ++ q) ++ scaleT K (K.neg K.one) (dP K k (p ++ q))) r
    = Bf K ((xi k p ++ scaleT K (K.neg K.one) (dP K k p)) ++ (xi k q ++ scaleT K (K.neg K.one) (dP K k q))) r
  rw [xi_app, dP_app]
  simp only [Bf_app_l, scale_app]
  generalize Bf K (xi k p) r = a; generalize Bf K (xi k q) r = b
  generalize Bf K (scaleT K (K.neg K.one) (dP K k p)) r = c; generalize Bf K (scaleT K (K.neg K.one) (dP K k q)) r = d
  lib_ring

theorem eqv_creK_app (n k : Nat) (hk : k < n) : ∀ a (p q : List (Term R)), LenP n p → LenP n q →
    Eqv K n (creK K k a (p ++ q)) (creK K k a p ++ creK K k a q)
  | 0, _, _, _, _ => eqv_refl K n _
  | a + 1, p, q, hp, hq =>
    eqv_trans K n _ _ _ (eqv_cre K n k hk _ _ (lenP_creK K n k a _ (lenP_app n p q hp hq))
      (lenP_app n _ _ (lenP_creK K n k a p hp) (lenP_creK K n k a q hq)) (eqv_creK_app n k hk a p q hp hq))
      (eqv_cre_app K n k _ _)

theorem eqv_creK_scale (n k : Nat) (hk : k < n) (c : R) : ∀ a (p : List (Term R)), LenP n p →
    Eqv K n (creK K k a (scaleT K c p)) (scaleT K c (creK K k a p))
  | 0, _, _ => eqv_refl K n _
  | a + 1, p, hp =>
    eqv_trans K n _ _ _ (eqv_cre K n k hk _ _ (lenP_creK K n k a _ (lenP_scale K n c p hp))
      (lenP_scale K n c _ (lenP_creK K n k a p hp)) (eqv_creK_scale n k hk c a p hp)) (cre_scale K n k c _)

theorem xi_eq_cre (n i : Nat) (f : List (Term R)) : Eqv K n (xi i f) (cre K i f ++ dP K i f) := fun r _ => by
  show Bf K (xi i f) r = Bf K ((xi i f ++ scaleT K (K.neg K.one) (dP K i f)) ++ dP K i f) r
  rw [Bf_app_l, Bf_app_l, Bf_scale]
  generalize Bf K (xi i f) r = a; generalize Bf K (dP K i f) r = b
  lib_ring

/-- **x_{k+j} He_γ ≡ He_{γ+e_j} + γ_j He_{γ−e_j}.** -/
theorem xi_Hm (n : Nat) : ∀ (k : Nat) (γ : List Nat) (j : Nat), j < γ.length → k + γ.length ≤ n →
    Eqv K n (xi (k + j) (Hm K n k γ)) (Hm K n k (incAt j γ) ++ scaleT K (cN K (nthE j γ)) (Hm K n k (decAt j γ)))
  | _, [], _, h, _ => absurd h (Nat.not_lt_zero _)
  | k, a :: γ, 0, _, hl => by
    have hkn : k < n := Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right k γ.length)) hl
    have hl' : k + 1 + γ.length ≤ n := by
      have : k + 1 + γ.length = k + (γ.length + 1) := by lib_nat
      rw [this]; exact hl
    let H := Hm K n (k + 1) γ
    have hH : LenP n H := lenP_Hm K n (k + 1) γ
    have h0 := dP_Hm_low K n k (k + 1) γ (Nat.lt_succ_self k) hl'
    show Eqv K n (xi k (creK K k a H)) (creK K k (a + 1) H ++ scaleT K (cN K a) (creK K k (a - 1) H))
    refine eqv_trans K n _ _ _ (xi_eq_cre K n k _) (eqv_app K n _ _ _ _ (eqv_refl K n _) ?_)
    cases a with
    | zero =>
      refine eqv_trans K n _ _ _ h0 (eqv_symm K n _ _ ?_)
      exact eqv_scale_zero K n _
    | succ m => exact dP_creK_F K n k hkn H hH h0 m
  | k, a :: γ, j + 1, hj, hl => by
    have hkn : k < n := Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_add_right k γ.length)) hl
    have hl' : k + 1 + γ.length ≤ n := by
      have : k + 1 + γ.length = k + (γ.length + 1) := by lib_nat
      rw [this]; exact hl
    have ih := xi_Hm n (k + 1) γ j (Nat.lt_of_succ_lt_succ hj) hl'
    have e : k + (j + 1) = k + 1 + j := by lib_nat
    rw [e]
    have hne : k ≠ k + 1 + j := Nat.ne_of_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_self k) (Nat.le_add_right (k + 1) j))
    show Eqv K n (xi (k + 1 + j) (creK K k a (Hm K n (k + 1) γ)))
      (creK K k a (Hm K n (k + 1) (incAt j γ)) ++ scaleT K (cN K (nthE j γ)) (creK K k a (Hm K n (k + 1) (decAt j γ))))
    rw [xi_creK_cross K _ k hne]
    have l1 := lenP_Hm K n (k + 1) (incAt j γ)
    have l2 := lenP_Hm K n (k + 1) (decAt j γ)
    refine eqv_trans K n _ _ _ (eqv_creK K n k hkn a _ _ (lenP_xi n _ _ (lenP_Hm K n (k + 1) γ))
      (lenP_app n _ _ l1 (lenP_scale K n _ _ l2)) ih) ?_
    refine eqv_trans K n _ _ _ (eqv_creK_app K n k hkn a _ _ l1 (lenP_scale K n _ _ l2)) ?_
    exact eqv_app K n _ _ _ _ (eqv_refl K n _) (eqv_creK_scale K n k hkn _ a _ l2)

end GaussHermite

#print axioms GaussHermite.adj
#print axioms GaussHermite.rodrigues
#print axioms GaussHermite.herm_dk
#print axioms GaussHermite.comm_cre
#print axioms GaussHermite.dP_He
#print axioms GaussHermite.herm_sq
#print axioms GaussHermite.herm_orth
#print axioms GaussHermite.rodrigues_m
#print axioms GaussHermite.dPk_creK_low
#print axioms GaussHermite.Dm_creK
#print axioms GaussHermite.dP_Hm_low
#print axioms GaussHermite.herm_m
#print axioms GaussHermite.xi_Hm

#print axioms: 60 theorems, no axioms.

90.7 H0: Gaussian positivity on squares

Every polynomial is a Hermite combination, so the Gram form is a sum of squares and \(G\) is positive on squares.

\[ p\equiv \sum _s c_sHe_{\alpha _s},\qquad G(p^2)=\sum _\alpha \alpha !\Big (\sum _{\alpha _s=\alpha }c_s\Big )^2\ge 0. \]

ym/GaussPos.lean

/-
  ym/GaussPos — T45, H0 positivity, step 6: every polynomial is a Hermite combination, and G(p²) ≥ 0.

    poly_exp     p ≡ Σ_s c_s He_{α_s}                    (x_i He_α ≡ He_{α+e_i} + α_i He_{α−e_i}, by induction on degree)
    Gp_ev        G(Σ_s c_s He_{α_s} · Σ_t d_t He_{β_t}) = Σ_{s,t} c_s d_t α_s! [α_s = β_t]
    Qf_nn        Σ_{s,t} c_s c_t α_s! [α_s = α_t] = Σ_α α! (Σ_{α_s = α} c_s)² ≥ 0
    gauss_pos    G(p²) ≥ 0 for every polynomial p, in any ordered ring
  (Sum-of-squares positivity of the Gaussian, hence of the sphere functional on squares of polynomials.)
  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GaussHermite
import ym.QuatUnit

namespace GaussPos

open SU2Char Gauss GaussPoly GaussSubst GaussRot GaussHermite YMChain

variable {R : Type} (K : DetChain.CRing R)

def ev (n : Nat) : List (R × List Nat) → List (Term R)
  | [] => []
  | t :: E => scaleT K t.1 (Hm K n 0 t.2) ++ ev n E

def LenH (n : Nat) : List (R × List Nat) → Prop
  | [] => True
  | t :: E => t.2.length = n ∧ LenH n E

theorem lenP_ev (n : Nat) : ∀ E, LenP n (ev K n E)
  | [] => trivial
  | t :: E => lenP_app n _ _ (lenP_scale K n _ _ (lenP_Hm K n 0 t.2)) (lenP_ev n E)

theorem lenH_app (n : Nat) : ∀ E F : List (R × List Nat), LenH n E → LenH n F → LenH n (E ++ F)
  | [], _, _, h => h
  | _ :: E, F, h, g => ⟨h.1, lenH_app n E F h.2 g⟩

theorem ev_app (n : Nat) : ∀ E F, ev K n (E ++ F) = ev K n E ++ ev K n F
  | [], _ => rfl
  | t :: E, F => by
    show scaleT K t.1 (Hm K n 0 t.2) ++ ev K n (E ++ F) = (scaleT K t.1 (Hm K n 0 t.2) ++ ev K n E) ++ ev K n F
    rw [ev_app n E F, app_assoc']

def xiH (i : Nat) : List (R × List Nat) → List (R × List Nat)
  | [] => []
  | t :: E => (t.1, incAt i t.2) :: (K.mul t.1 (cN K (nthE i t.2)), decAt i t.2) :: xiH i E

theorem lenH_xiH (n i : Nat) : ∀ E, LenH n E → LenH n (xiH K i E)
  | [], _ => trivial
  | t :: E, h => ⟨(len_incAt i t.2).trans h.1, (len_decAt i t.2).trans h.1, lenH_xiH n i E h.2⟩

theorem xi_ev (n i : Nat) (hi : i < n) : ∀ E, LenH n E → Eqv K n (xi i (ev K n E)) (ev K n (xiH K i E))
  | [], _ => eqv_refl K n []
  | t :: E, h => by
    show Eqv K n (xi i (scaleT K t.1 (Hm K n 0 t.2) ++ ev K n E))
      (scaleT K t.1 (Hm K n 0 (incAt i t.2)) ++ (scaleT K (K.mul t.1 (cN K (nthE i t.2))) (Hm K n 0 (decAt i t.2))
        ++ ev K n (xiH K i E)))
    rw [xi_app, xi_scale, ← app_assoc']
    refine eqv_app K n _ _ _ _ ?_ (xi_ev n i hi E h.2)
    have x := xi_Hm K n 0 t.2 i (by rw [h.1]; exact hi) (by rw [Nat.zero_add, h.1]; exact Nat.le_refl n)
    rw [Nat.zero_add] at x
    refine eqv_trans K n _ _ _ (eqv_scale K n t.1 _ _ x) ?_
    rw [scale_app, scale_scale]
    exact eqv_refl K n _

theorem Hm_zeros (n : Nat) : ∀ m k, Hm K n k (zerosL m) = one K n
  | 0, _ => rfl
  | m + 1, k => Hm_zeros n m (k + 1)

theorem mono_exp (n : Nat) (c : R) : ∀ d (α : List Nat), α.length = n → sumL α ≤ d →
    ∃ E, LenH n E ∧ Eqv K n [(c, α)] (ev K n E) := by
  intro d
  induction d with
  | zero =>
    intro α hl hs
    have h0 : sumL α = 0 := Nat.le_antisymm hs (Nat.zero_le _)
    refine ⟨[(c, α)], ⟨hl, trivial⟩, fun r _ => ?_⟩
    have ez : α = zerosL n := by rw [zero_sum α h0, hl]
    show Bf K [(c, α)] r = Bf K (scaleT K c (Hm K n 0 α) ++ []) r
    rw [ez, Hm_zeros, app_nil]
    show K.add (row K (c, zerosL n) r) K.zero = K.add (row K (K.mul c K.one, zerosL n) r) K.zero
    rw [row_coef K (K.mul c K.one) c _ (by lib_ring)]
  | succ d ih =>
    intro α hl hs
    cases Nat.lt_or_ge (sumL α) 1 with
    | inl h0 =>
      have z : sumL α = 0 := Nat.le_antisymm (Nat.le_of_lt_succ h0) (Nat.zero_le _)
      exact ih α hl (by rw [z]; exact Nat.zero_le _)
    | inr hpos =>
      obtain ⟨i, hi, hp⟩ := exists_pos α hpos
      have hβl : (decAt i α).length = n := (len_decAt i α).trans hl
      have hsb : sumL (decAt i α) + 1 = sumL α := sum_decAt i α hp
      obtain ⟨E, hE, eq⟩ := ih (decAt i α) hβl (Nat.le_of_succ_le_succ (show sumL (decAt i α) + 1 ≤ d + 1 by rw [hsb]; exact hs))
      refine ⟨xiH K i E, lenH_xiH K n i E hE, ?_⟩
      have hin : i < n := by rw [← hl]; exact hi
      have x : xi i [(c, decAt i α)] = [(c, α)] := by
        show [(c, incAt i (decAt i α))] = _; rw [incAt_decAt i α hp]
      rw [← x]
      exact eqv_trans K n _ _ _ (eqv_xi K n i hin _ _ ⟨hβl, trivial⟩ (lenP_ev K n E) eq) (xi_ev K n i hin E hE)

/-- **Every polynomial is a combination of Hermite polynomials.** -/
theorem poly_exp (n : Nat) : ∀ p : List (Term R), LenP n p → ∃ E, LenH n E ∧ Eqv K n p (ev K n E)
  | [], _ => ⟨[], trivial, eqv_refl K n []⟩
  | t :: p, h => by
    obtain ⟨E1, h1, e1⟩ := mono_exp K n t.1 (sumL t.2) t.2 h.1 (Nat.le_refl _)
    obtain ⟨E2, h2, e2⟩ := poly_exp n p h.2
    refine ⟨E1 ++ E2, lenH_app n E1 E2 h1 h2, ?_⟩
    rw [ev_app]
    show Eqv K n ([(t.1, t.2)] ++ p) _
    exact eqv_app K n _ _ _ _ e1 e2

/-! ### the Gram form in the Hermite basis -/

def kr (α β : List Nat) : R := if α = β then cN K (factL α) else K.zero

def rowQ (s : R × List Nat) : List (R × List Nat) → R
  | [] => K.zero
  | t :: F => K.add (K.mul (K.mul s.1 t.1) (kr K s.2 t.2)) (rowQ s F)

def Qf : List (R × List Nat) → List (R × List Nat) → R
  | [], _ => K.zero
  | s :: E, F => K.add (rowQ K s F) (Qf E F)

theorem gp_app_r (X A B : List (Term R)) : Gp K (mulP K X (A ++ B)) = K.add (Gp K (mulP K X A)) (Gp K (mulP K X B)) := by
  rw [gp_comm, mulP_app_l, Gp_app, gp_comm K A, gp_comm K B]

theorem g_row (n : Nat) (s : R × List Nat) (hs : s.2.length = n) : ∀ F, LenH n F →
    Gp K (mulP K (scaleT K s.1 (Hm K n 0 s.2)) (ev K n F)) = rowQ K s F
  | [], _ => by
    show Gp K (mulP K _ []) = K.zero
    rw [gp_comm]; rfl
  | t :: F, h => by
    show Gp K (mulP K _ (scaleT K t.1 (Hm K n 0 t.2) ++ ev K n F)) = K.add _ (rowQ K s F)
    rw [gp_app_r, g_row n s hs F h.2, scale_mulP, mulP_scale_r, Gp_scale, Gp_scale, herm_m K n s.2 t.2 hs h.1, ← K.mul_assoc]
    rfl

theorem Gp_ev (n : Nat) (F : List (R × List Nat)) (hF : LenH n F) : ∀ E, LenH n E →
    Gp K (mulP K (ev K n E) (ev K n F)) = Qf K E F
  | [], _ => rfl
  | s :: E, h => by
    show Gp K (mulP K (scaleT K s.1 (Hm K n 0 s.2) ++ ev K n E) (ev K n F)) = K.add (rowQ K s F) (Qf K E F)
    rw [mulP_app_l, Gp_app, g_row K n s h.1 F hF, Gp_ev n F hF E h.2]

/-! ### positivity of the Gram form -/

def sel (α : List Nat) : List (R × List Nat) → List (R × List Nat)
  | [] => []
  | t :: E => if t.2 = α then t :: sel α E else sel α E

def rej (α : List Nat) : List (R × List Nat) → List (R × List Nat)
  | [] => []
  | t :: E => if t.2 = α then rej α E else t :: rej α E

def sumC : List (R × List Nat) → R
  | [] => K.zero
  | t :: E => K.add t.1 (sumC E)

theorem rowQ_split (α : List Nat) (s : R × List Nat) : ∀ F, rowQ K s F = K.add (rowQ K s (sel α F)) (rowQ K s (rej α F))
  | [] => (K.zero_add _).symm
  | t :: F => by
    have ih := rowQ_split α s F
    by_cases h : t.2 = α
    · have e1 : sel α (t :: F) = t :: sel α F := if_pos h
      have e2 : rej α (t :: F) = rej α F := if_pos h
      rw [e1, e2]
      show K.add _ (rowQ K s F) = K.add (K.add _ (rowQ K s (sel α F))) _
      rw [ih]
      generalize K.mul (K.mul s.1 t.1) (kr K s.2 t.2) = a
      generalize rowQ K s (sel α F) = b; generalize rowQ K s (rej α F) = c
      lib_ring
    · have e1 : sel α (t :: F) = sel α F := if_neg h
      have e2 : rej α (t :: F) = t :: rej α F := if_neg h
      rw [e1, e2]
      show K.add _ (rowQ K s F) = K.add _ (K.add _ (rowQ K s (rej α F)))
      rw [ih]
      generalize K.mul (K.mul s.1 t.1) (kr K s.2 t.2) = a
      generalize rowQ K s (sel α F) = b; generalize rowQ K s (rej α F) = c
      lib_ring

theorem Qf_split_r (α : List Nat) (F : List (R × List Nat)) : ∀ E, Qf K E F = K.add (Qf K E (sel α F)) (Qf K E (rej α F))
  | [] => (K.zero_add _).symm
  | s :: E => by
    show K.add (rowQ K s F) (Qf K E F) = K.add (K.add (rowQ K s (sel α F)) (Qf K E (sel α F))) (K.add (rowQ K s (rej α F)) (Qf K E (rej α F)))
    rw [rowQ_split K α s F, Qf_split_r α F E]
    generalize rowQ K s (sel α F) = a; generalize rowQ K s (rej α F) = b
    generalize Qf K E (sel α F) = c; generalize Qf K E (rej α F) = d
    lib_ring

theorem Qf_split_l (α : List Nat) (F : List (R × List Nat)) : ∀ E, Qf K E F = K.add (Qf K (sel α E) F) (Qf K (rej α E) F)
  | [] => (K.zero_add _).symm
  | s :: E => by
    have ih := Qf_split_l α F E
    by_cases h : s.2 = α
    · have e1 : sel α (s :: E) = s :: sel α E := if_pos h
      have e2 : rej α (s :: E) = rej α E := if_pos h
      rw [e1, e2]
      show K.add (rowQ K s F) (Qf K E F) = K.add (K.add (rowQ K s F) (Qf K (sel α E) F)) _
      rw [ih]
      generalize rowQ K s F = a; generalize Qf K (sel α E) F = b; generalize Qf K (rej α E) F = c
      lib_ring
    · have e1 : sel α (s :: E) = sel α E := if_neg h
      have e2 : rej α (s :: E) = s :: rej α E := if_neg h
      rw [e1, e2]
      show K.add (rowQ K s F) (Qf K E F) = K.add _ (K.add (rowQ K s F) (Qf K (rej α E) F))
      rw [ih]
      generalize rowQ K s F = a; generalize Qf K (sel α E) F = b; generalize Qf K (rej α E) F = c
      lib_ring

theorem term_zero (a b : R) (r : R) : K.add (K.mul (K.mul a b) K.zero) r = r := by
  rw [DetChain.mul_zero, K.zero_add]

theorem rowQ_rej_zero (α : List Nat) (s : R × List Nat) (hs : s.2 = α) : ∀ F, rowQ K s (rej α F) = K.zero
  | [] => rfl
  | t :: F => by
    by_cases h : t.2 = α
    · have e : rej α (t :: F) = rej α F := if_pos h
      rw [e]; exact rowQ_rej_zero α s hs F
    · have e : rej α (t :: F) = t :: rej α F := if_neg h
      rw [e]
      show K.add (K.mul (K.mul s.1 t.1) (kr K s.2 t.2)) (rowQ K s (rej α F)) = K.zero
      have k0 : kr K s.2 t.2 = K.zero := if_neg (fun e => h (e.symm.trans hs))
      rw [k0, term_zero, rowQ_rej_zero α s hs F]

theorem rowQ_sel_zero (α : List Nat) (s : R × List Nat) (hs : s.2 ≠ α) : ∀ F, rowQ K s (sel α F) = K.zero
  | [] => rfl
  | t :: F => by
    by_cases h : t.2 = α
    · have e : sel α (t :: F) = t :: sel α F := if_pos h
      rw [e]
      show K.add (K.mul (K.mul s.1 t.1) (kr K s.2 t.2)) (rowQ K s (sel α F)) = K.zero
      have k0 : kr K s.2 t.2 = K.zero := if_neg (fun e => hs (e.trans h))
      rw [k0, term_zero, rowQ_sel_zero α s hs F]
    · have e : sel α (t :: F) = sel α F := if_neg h
      rw [e]; exact rowQ_sel_zero α s hs F

theorem Qf_sel_rej (α : List Nat) (F : List (R × List Nat)) : ∀ E, Qf K (sel α E) (rej α F) = K.zero
  | [] => rfl
  | s :: E => by
    by_cases h : s.2 = α
    · have e : sel α (s :: E) = s :: sel α E := if_pos h
      rw [e]; show K.add (rowQ K s (rej α F)) (Qf K (sel α E) (rej α F)) = K.zero
      rw [rowQ_rej_zero K α s h F, Qf_sel_rej α F E, K.zero_add]
    · have e : sel α (s :: E) = sel α E := if_neg h
      rw [e]; exact Qf_sel_rej α F E

theorem Qf_rej_sel (α : List Nat) (F : List (R × List Nat)) : ∀ E, Qf K (rej α E) (sel α F) = K.zero
  | [] => rfl
  | s :: E => by
    by_cases h : s.2 = α
    · have e : rej α (s :: E) = rej α E := if_pos h
      rw [e]; exact Qf_rej_sel α F E
    · have e : rej α (s :: E) = s :: rej α E := if_neg h
      rw [e]; show K.add (rowQ K s (sel α F)) (Qf K (rej α E) (sel α F)) = K.zero
      rw [rowQ_sel_zero K α s h F, Qf_rej_sel α F E, K.zero_add]

def AllI (α : List Nat) : List (R × List Nat) → Prop
  | [] => True
  | t :: E => t.2 = α ∧ AllI α E

theorem allI_sel (α : List Nat) : ∀ E : List (R × List Nat), AllI α (sel α E)
  | [] => trivial
  | t :: E => by
    by_cases h : t.2 = α
    · have e : sel α (t :: E) = t :: sel α E := if_pos h
      rw [e]; exact ⟨h, allI_sel α E⟩
    · have e : sel α (t :: E) = sel α E := if_neg h
      rw [e]; exact allI_sel α E

theorem rowQ_all (α : List Nat) (s : R × List Nat) (hs : s.2 = α) : ∀ F, AllI α F →
    rowQ K s F = K.mul (K.mul s.1 (sumC K F)) (cN K (factL α))
  | [], _ => by show K.zero = K.mul (K.mul s.1 K.zero) _; rw [DetChain.mul_zero, K.mul_comm, DetChain.mul_zero]
  | t :: F, h => by
    show K.add (K.mul (K.mul s.1 t.1) (kr K s.2 t.2)) (rowQ K s F) = K.mul (K.mul s.1 (K.add t.1 (sumC K F))) _
    have k1 : kr K s.2 t.2 = cN K (factL α) := by
      show (if s.2 = t.2 then cN K (factL s.2) else K.zero) = _
      rw [if_pos (hs.trans h.1.symm), hs]
    rw [k1, rowQ_all α s hs F h.2]
    generalize cN K (factL α) = w; generalize sumC K F = S
    lib_ring

theorem Qf_all (α : List Nat) (F : List (R × List Nat)) (hF : AllI α F) : ∀ E, AllI α E →
    Qf K E F = K.mul (K.mul (sumC K E) (sumC K F)) (cN K (factL α))
  | [], _ => by show K.zero = K.mul (K.mul K.zero _) _; rw [K.mul_comm K.zero, DetChain.mul_zero, K.mul_comm, DetChain.mul_zero]
  | s :: E, h => by
    show K.add (rowQ K s F) (Qf K E F) = K.mul (K.mul (K.add s.1 (sumC K E)) (sumC K F)) _
    rw [rowQ_all K α s h.1 F hF, Qf_all α F hF E h.2]
    generalize cN K (factL α) = w; generalize sumC K F = S; generalize sumC K E = T
    lib_ring

theorem len_rej (α : List Nat) : ∀ E : List (R × List Nat), (rej α E).length ≤ E.length
  | [] => Nat.le_refl 0
  | t :: E => by
    by_cases h : t.2 = α
    · have e : rej α (t :: E) = rej α E := if_pos h
      rw [e]; exact Nat.le_succ_of_le (len_rej α E)
    · have e : rej α (t :: E) = t :: rej α E := if_neg h
      rw [e]; exact Nat.succ_le_succ (len_rej α E)

variable (O : ORing K)

theorem one_nn : O.le K.zero K.one := by
  have := QuatUnit.sq_nn K O K.one; rw [K.one_mul] at this; exact this

/-- **The Gram form is positive:** Σ_{s,t} c_s c_t α_s! [α_s = α_t] ≥ 0. -/
theorem Qf_nn : ∀ (N : Nat) (E : List (R × List Nat)), E.length ≤ N → O.le K.zero (Qf K E E)
  | 0, [], _ => O.le_refl _
  | 0, _ :: _, h => absurd h (Nat.not_succ_le_zero _)
  | _ + 1, [], _ => O.le_refl _
  | N + 1, s :: E, h => by
    let α := s.2
    have split : Qf K (s :: E) (s :: E) = K.add (Qf K (sel α (s :: E)) (sel α (s :: E))) (Qf K (rej α (s :: E)) (rej α (s :: E))) := by
      rw [Qf_split_l K α (s :: E) (s :: E), Qf_split_r K α (s :: E) (sel α (s :: E)), Qf_split_r K α (s :: E) (rej α (s :: E)),
        Qf_sel_rej, Qf_rej_sel, DetChain.add_zero', K.zero_add]
    rw [split]
    have A : O.le K.zero (Qf K (sel α (s :: E)) (sel α (s :: E))) := by
      rw [Qf_all K α _ (allI_sel α _) _ (allI_sel α _)]
      exact O.mul_nonneg _ _ (QuatUnit.sq_nn K O _) (SU2CharExp.cN_nn K O (one_nn K O) _)
    have e : rej α (s :: E) = rej α E := if_pos rfl
    have B : O.le K.zero (Qf K (rej α (s :: E)) (rej α (s :: E))) := by
      rw [e]; exact Qf_nn N (rej α E) (Nat.le_trans (len_rej α E) (Nat.le_of_succ_le_succ h))
    have := O.add_le_add _ _ _ _ A B
    rw [K.zero_add] at this; exact this

/-- **Positivity of the Gaussian functional on squares:** G(p²) ≥ 0. -/
theorem gauss_pos (n : Nat) (p : List (Term R)) (hp : LenP n p) : O.le K.zero (Gp K (mulP K p p)) := by
  obtain ⟨E, hE, e⟩ := poly_exp K n p hp
  have hv := lenP_ev K n E
  have q : Gp K (mulP K p p) = Gp K (mulP K (ev K n E) (ev K n E)) := by
    rw [Gp_mulP, Gp_mulP, e p hp, Bf_comm, e _ hv]
  rw [q, Gp_ev K n E hE E hE]
  exact Qf_nn K O E.length E (Nat.le_refl _)

end GaussPos

#print axioms GaussPos.poly_exp
#print axioms GaussPos.Qf_nn
#print axioms GaussPos.gauss_pos

#print axioms: 26 theorems, no axioms.

90.8 L4: strong coupling on \(\mathbb Z^d\), uniform in volume and dimension

Statement. On every finite set of plaquettes of \(\mathbb Z^d\), with \(D_g=8d\),

\[ |\langle U_pU_q\rangle -\langle U_p\rangle \langle U_q\rangle |\le \tfrac 12(2K_1)^2(\kappa ^2(1+y))^{\hat s}u^{R} \quad \text {whenever }\mathrm {dist}_\infty (p,q)>R+1, \]
and the Wilson weight satisfies \(e^{\beta t}=1+f\), \(|f|\le 2\beta \) for \(|t|\le 1\), \(0\le \beta \le 1\), while the normalised weight obeys \(1-4\beta \le e^{\beta (t-1)}\le 1\) for small \(\beta \) (every truncation): \(c_0=1\), \(-4\beta \le f\le 0\), \(m=1-4\beta \).

Key steps. Degree \(\le 4\cdot 2d\) (ZdDegree.deg_Zd); closure steps vs. distance (ZdDist); GapZd.gap_Zd; weight bounds from the exponential tail (WeightBound.weight_bound, WeightBound.weight_le_one). Cited: H0 for \(SU(2)\), completeness of \(\mathbb R\) (limit of the weight series).

Lean files.

ym/GapZd.lean

/-
  ym/GapZd — T45, cycle 81 (Lad4, any compact group with a Haar structure, every dimension d): the strong-coupling
  mass gap on Z^d, uniformly in the volume.

  gap_Zd: GapH.gap_H on any finite set of plaquettes of Z^d (duplicate-free, as link-incidence vectors over a
  duplicate-free link list), with the degree hypothesis discharged by ZdDegree (Dg = 4·(2d)) and the far-apart
  hypothesis by ZdDist (ℓ∞ distance of base points > R + 1). Conclusion: for observables U_p, U_q on two plaquettes,
    −B Z₁² ≤ 2 (Z_{pq} Z₁ − Z_p Z_q) ≤ B Z₁²,   B = (2K₁)² (κ²)^ŝ (1+y)^ŝ u^R,
  i.e. |⟨U_pU_q⟩ − ⟨U_p⟩⟨U_q⟩| ≤ ½ B u^R with R + 2 ≤ dist(p, q): exponential clustering with rate −ln u per lattice
  unit, the constants depending on d only through Dg = 8d. For d = 3 this is rung L4 (and the gap part of L5 for d = 4)
  for SU(2), given H0 and the strong-coupling weight bounds.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.ZdDist

namespace GapZd

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) {G : Type} {L : Nat} (H : Haar K O G L)

theorem gap_Zd (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (d : Nat) (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (f : Nat → List G → R) (Up Uq : List G → R) (c0 m κ ε K1 u x y : R) (Rr : Nat)
    (hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f i)) (hUp : depOn ((vec LL p0).map nzb) Up) (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hfaG : ∀ i, H.adm (f i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (hc0 : O.le K.zero c0) (hc1 : O.le c0 K.one) (hm : O.le K.zero m) (hm1 : O.le m K.one)
    (hκ1 : O.le K.one κ) (hκm : K.mul κ m = K.one)
    (hw : ∀ i x, O.le m (K.add c0 (f i x))) (hw1 : ∀ i x, O.le (K.add c0 (f i x)) K.one)
    (he : O.le K.zero ε) (hf : ∀ i x, O.le (K.neg ε) (f i x) ∧ O.le (f i x) ε)
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hu0 : O.le K.zero u) (hu1 : O.le u K.one) (hx : O.le K.zero x) (hy : O.le K.zero y)
    (hsmall : O.le (K.mul (K.add (K.add ε ε) (K.mul ε ε)) (pwR K (K.mul κ κ) (4 * (2 * d) + 1))) (K.mul u x))
    (hxy : O.le (K.mul x (pwR K (K.add K.one y) (4 * (2 * d)))) y)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let w : Nat → List G → R := fun i x => K.add c0 (f i x)
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let s := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) s))
      (K.mul (pwR K (K.add K.one y) s) (pwR K u Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))  := by
  have hlenP : (Pn.map (vec LL)).length = Pn.length := StripAxial.len_map_g _ _
  have hvl : ∀ p, (vec LL p).length = L := fun p => (StripAxial.len_map_g _ LL).trans hL
  exact gap_H K O H DH h1 h10 (Pn.map (vec LL)) (fun i hi => by rw [nthL_map LL Pn i (hlenP ▸ hi)]; exact hvl _) (vec LL p0) (vec LL q0) (hvl p0) (hvl q0) hpq
    f Up Uq c0 m κ ε K1 u x y (4 * (2 * d)) Rr hfd hUp hUq hfaG hUpa hUqa hc0 hc1 hm hm1 hκ1 hκm hw hw1 he hf hK1 hUpb hUqb
    hu0 hu1 hx hy
    (fun r hr => by
      obtain ⟨p, hp, e⟩ := HaarFin.memL_map' (vec LL) r Pn hr
      rw [e]; exact deg_Zd d LL hLL Pn hnd hv p)
    hsmall hxy (far_ok LL Pn p0 q0 Rr hdist) M (hM.trans hlenP.symm)

end GapZd

#print axioms GapZd.gap_Zd

ym/WeightBound.lean

/-
  ym/WeightBound — T45, cycle 101 (Lad5 input "SU(2) weight bounds"): the Wilson plaquette weight is 1 + f, |f| ≤ 2β.

  For t = Re tr U/2 ∈ [−1, 1] and the exponential coefficients c_k = β^k/k! (c₀ = 1, c_{k+1}(k+1) = β c_k):
    weight_bound    for 0 ≤ β ≤ 1 and every truncation N ≥ 1:  S_N(t) = 1 + f with −2β ≤ f ≤ 2β
    weight_le_one   for s = t − 1 ≤ 0, −1 ≤ βs, 2β(−s) ≤ 3: every truncation S_{2+m}(s) of e^{βs} is ≤ 1
  so the normalised weight e^{β(t−1)} = c₀ + f with c₀ = 1, −4β ≤ f ≤ 0, m = 1 − 4β (the exact hypotheses of AreaG/GapG).
  So the fluctuation of the plaquette weight is O(β) uniformly in the link variables: the smallness hypothesis
  ε = 2β → 0 of the strong-coupling theorems (AreaG, GapG/GapZd, Lad5) holds for the Wilson action at small β, with
  m = 1 − 2β. (Limit N → ∞ by completeness of ℝ.)

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.ExpTail

namespace WeightBound

open YMChain LinkIntegral Signed Geom WilsonGram Indep ExpTail

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- **Weight bound.** -/
theorem weight_bound (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (c : Nat → R) (β t : R)
    (hc : ∀ k, K.mul (c (k + 1)) (natR K (k + 1)) = K.mul β (c k)) (hc0 : ∀ k, O.le K.zero (c k))
    (hc00 : c 0 = K.one) (hβ : O.le K.zero β) (hβ1 : O.le β K.one) (ht : O.le (K.neg K.one) t ∧ O.le t K.one)
    (m : Nat) :
    series K c t (1 + m) = K.add K.one (dsum K c t 1 m) ∧
      O.le (K.neg (K.mul (two K) β)) (dsum K c t 1 m) ∧ O.le (dsum K c t 1 m) (K.mul (two K) β) := by
  have hN : O.le (K.mul (two K) (K.mul β K.one)) (natR K (1 + 1)) := by
    have e2 : natR K (1 + 1) = K.mul (two K) K.one := by
      show K.add (K.add K.zero K.one) K.one = K.mul (K.add K.one K.one) K.one
      rw [K.zero_add, K.mul_comm, K.one_mul]
    rw [e2, K.mul_comm β K.one, K.one_mul]
    have two0 : O.le K.zero (two K) := by
      have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
    rw [K.mul_comm (two K) β, K.mul_comm (two K) K.one]
    exact O.mul_le_mul_right _ _ _ hβ1 two0
  have ec := exp_cauchy K O h1 h10 c β K.one t hc hc0 h1 ht 1 hN m
  have c1 : c 1 = β := by
    have := hc 0
    rw [hc00, K.mul_comm β K.one, K.one_mul] at this
    have e1 : natR K (0 + 1) = K.one := by show K.add K.zero K.one = K.one; rw [K.zero_add]
    rw [e1, K.mul_comm, K.one_mul] at this
    exact this
  have ea : aa K c K.one 1 = β := by
    show K.mul (c 1) (K.mul K.one K.one) = β
    rw [K.one_mul, K.mul_comm, K.one_mul, c1]
  have es : series K c t 1 = K.one := by
    show K.add K.zero (K.mul (c 0) K.one) = K.one
    rw [K.zero_add, hc00, K.one_mul]
  rw [ea] at ec
  refine ⟨?_, ec.1, ec.2.1⟩
  rw [ec.2.2, es]

/-- **The normalised Wilson weight is at most 1:** for s ≤ 0 (s = t − 1, t = Re tr U/2), −1 ≤ βs and 2β(−s) ≤ 3, every
  truncation S_{2+m}(s) of e^{βs} is ≤ 1. With weight_bound (T = −s ≤ 2): 1 − 4β ≤ S ≤ 1, i.e. c₀ = 1, f ∈ [−4β, 0]. -/
theorem weight_le_one (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (c : Nat → R) (β s : R)
    (hc : ∀ k, K.mul (c (k + 1)) (natR K (k + 1)) = K.mul β (c k)) (hc0 : ∀ k, O.le K.zero (c k))
    (hc00 : c 0 = K.one) (hβ : O.le K.zero β) (hs : O.le s K.zero) (hbs : O.le (K.neg K.one) (K.mul β s))
    (hN : O.le (K.mul (two K) (K.mul β (K.neg s))) (natR K (2 + 1))) (m : Nat) :
    O.le (series K c s (2 + m)) K.one := by
  have hT : O.le K.zero (K.neg s) := le_neg_of K O s hs
  have ht : O.le (K.neg (K.neg s)) s ∧ O.le s (K.neg s) := by
    rw [neg_neg' K]; exact ⟨O.le_refl _, O.le_trans _ _ _ hs hT⟩
  have ec := exp_cauchy K O h1 h10 c β (K.neg s) s hc hc0 hT ht 2 hN m
  have c1 : c 1 = β := by
    have := hc 0
    rw [hc00, K.mul_comm β K.one, K.one_mul] at this
    have e1 : natR K (0 + 1) = K.one := by show K.add K.zero K.one = K.one; rw [K.zero_add]
    rw [e1, K.mul_comm, K.one_mul] at this
    exact this
  have c2 : K.mul (two K) (c 2) = K.mul β β := by
    have := hc 1
    have e2 : natR K (1 + 1) = two K := by show K.add (K.add K.zero K.one) K.one = _; rw [K.zero_add]; rfl
    rw [e2, c1, K.mul_comm] at this
    exact this
  have es : series K c s 2 = K.add K.one (K.mul β s) := by
    show K.add (K.add K.zero (K.mul (c 0) K.one)) (K.mul (c 1) (K.mul K.one s)) = _
    rw [K.zero_add, hc00, K.one_mul, K.one_mul, c1]
  have ea : K.mul (two K) (aa K c (K.neg s) 2) = K.mul (K.mul β s) (K.mul β s) := by
    show K.mul (two K) (K.mul (c 2) (K.mul (K.mul K.one (K.neg s)) (K.neg s))) = _
    rw [K.one_mul, CenterExp.neg_mul_neg, ← K.mul_assoc, c2, K.mul_assoc, ← K.mul_assoc β s s,
      K.mul_comm (K.mul β s) s, ← K.mul_assoc, K.mul_assoc]
  rw [ec.2.2, es]
  have up := ec.2.1
  rw [ea] at up
  -- 1 + x + d ≤ 1 + x + x² = 1 + x(1 + x) ≤ 1
  let x := K.mul β s
  have hx : O.le x K.zero := by
    have := O.mul_le_mul_right _ _ _ hs hβ
    rw [K.mul_comm, DetChain.zero_mul'] at this; exact this
  have h1x : O.le K.zero (K.add K.one x) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) hbs
    rw [K.add_neg] at this; exact this
  have key : O.le (K.add x (K.mul x x)) K.zero := by
    have p := O.mul_nonneg _ _ (le_neg_of K O x hx) h1x
    rw [neg_mul' K, K.distrib, K.mul_comm x K.one, K.one_mul] at p
    have := neg_le_neg K O _ _ p
    rw [neg_neg' K] at this
    have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
    rw [e] at this; exact this
  have s1 := O.add_le_add _ _ _ _ (O.le_refl (K.add K.one x)) up
  refine O.le_trans _ _ _ s1 ?_
  rw [K.add_assoc]
  have := O.add_le_add _ _ _ _ (O.le_refl K.one) key
  rw [DetChain.add_zero'] at this
  exact this

end WeightBound

#print axioms WeightBound.weight_bound
#print axioms WeightBound.weight_le_one

90.9 L5: four-dimensional strong coupling

Statement. For \(d=4\) and \(r=u(D_g+2)<1\): for all volumes \(\Lambda '\supseteq \Lambda \supseteq \) the \(K\)-ball around \(O\),

\[ (1-r)\,|\langle O\rangle _{\Lambda '}-\langle O\rangle _{\Lambda }|\le \kappa C s\,r^{K}, \]
so \(\langle O\rangle _\Lambda \) converges as \(\Lambda \uparrow \mathbb Z^4\); the limit keeps the gap of L4, lattice symmetries and reflection positivity, the latter because every truncation of the Wilson crossing weight is a nonnegative sum of Gram kernels and the truncations converge uniformly:
\[ \sum _{x,y}F(x)F(y)\sum _{k<N}\tfrac {\beta ^k}{k!}(x\cdot y)^k\ge 0,\qquad |S_{N+m}(t)-S_N(t)|\le 2\beta ^NT^N/N!\ \ (|t|\le T,\ 2\beta T\le N+1). \]

Explicit constants (S5, S6). For \(\beta \le \beta _0=2^{-103}\), on every finite set of plaquettes of \(\mathbb Z^4\): \(|\langle U_pU_q\rangle -\langle U_p\rangle \langle U_q\rangle |\le \tfrac 12(2K_1)^2(2\kappa ^2)^{\hat s}\,2^{-R}\) (SU2Strong4D.gap_4d) and \(|\langle W\rangle |\le K_\chi (2\kappa )^s2^{-RT}\) (SU2Strong4D.area_4d); with Cauchy completeness as a structure hypothesis the infinite-volume limit exists with rate \(Dr^n\) and keeps these bounds (InfVol4D.inf_vol, gap_inf).

Key steps. Volume step \(\langle O\rangle _{\Lambda +y}-\langle O\rangle _\Lambda =\mathrm {cov}/\langle w_y\rangle \) (OField, Volume, Lad5); ball sizes \(s(D_g+2)^k\) (BallSize); shells and geometric tail (ShellSum, Geom, Lad5Cauchy.uniform_cauchy); limits of RP and symmetries (OSLimit, SymLimit); the clustering bound passes to the infinite-volume limit (GapLimit.gap_limit: two-sided bounds survive limits) and forces a spectral gap of the transfer matrix: if \(\lambda ^nq\le Cu^nq\) for an eigenvector orthogonal to the vacuum then \(\lambda \le u\) (SpecGap.eig_gap, Archimedean ring); Gram truncations (WilsonGram.wilson_rp), \(\mathrm {Re\,tr}(gh^\dagger )=2g\cdot h\) (QuatTrace.retr_dot), approximation (RPLimit.rp_approx), uniform Cauchy (ExpTail.exp_cauchy). Cited: completeness of \(\mathbb R\), H0.

Lean files.

ym/OField.lean

/-
  ym/OField — T45, cycle 82 (Lad5 step 1): ordered fields, expectations and the volume step.

  OF: an ordered ring with inverses of nonzero elements (ℚ, ℝ, …), inverse positive on positives.
    E Z_X Z = Z_X · Z⁻¹                       expectation
    cov_bound      from −B Z² ≤ 2(Z_{pq} Z − Z_p Z_q) ≤ B Z²:  −B ≤ 2(⟨pq⟩ − ⟨p⟩⟨q⟩) ≤ B
    step_bound     if ⟨w⟩ ≥ m > 0 and |⟨Ow⟩ − ⟨O⟩⟨w⟩| ≤ c, then |⟨Ow⟩/⟨w⟩ − ⟨O⟩| ≤ c κ  (κ m = 1)
  Adding a plaquette y to a volume multiplies the integrand by its weight w_y: ⟨O⟩_{Λ+y} = ⟨O w_y⟩_Λ / ⟨w_y⟩_Λ, so the
  change of an expectation under adding a plaquette is a covariance divided by ⟨w_y⟩ ≥ m — bounded by the clustering
  theorem when y is far from O.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GapZd

namespace OField

open YMChain Signed

variable {R : Type} (K : DetChain.CRing R)

structure OF (O : ORing K) where
  inv : R → R
  mul_inv : ∀ a, a ≠ K.zero → K.mul a (inv a) = K.one
  inv_pos : ∀ a, O.le K.zero a → a ≠ K.zero → O.le K.zero (inv a)

variable (O : ORing K) (F : OF K O)

def E (zx z : R) : R := K.mul zx (F.inv z)

theorem inv_ne (a : R) (ha : a ≠ K.zero) (h10 : K.one ≠ K.zero) : F.inv a ≠ K.zero := fun h => by
  have := F.mul_inv a ha; rw [h, DetChain.mul_zero] at this; exact h10 this.symm

/-- Divide a two-sided bound by a positive element. -/
theorem div_bnd (x c z : R) (hz0 : O.le K.zero z) (hz : z ≠ K.zero) (h10 : K.one ≠ K.zero)
    (h : O.le (K.neg (K.mul c z)) x ∧ O.le x (K.mul c z)) :
    O.le (K.neg c) (K.mul x (F.inv z)) ∧ O.le (K.mul x (F.inv z)) c := by
  have hi := F.inv_pos z hz0 hz
  have e : K.mul (K.mul c z) (F.inv z) = c := by rw [K.mul_assoc, F.mul_inv z hz, K.mul_comm, K.one_mul]
  have a := O.mul_le_mul_right _ _ _ h.1 hi
  have b := O.mul_le_mul_right _ _ _ h.2 hi
  rw [neg_mul', e] at a
  rw [e] at b
  exact ⟨a, b⟩

/-- **Covariance bound in ratio form.** -/
theorem cov_bound (zpq zp zq z B : R) (hz0 : O.le K.zero z) (hz : z ≠ K.zero) (h10 : K.one ≠ K.zero)
    (h : O.le (K.neg (K.mul B (K.mul z z))) (K.add (K.add (K.mul zpq z) (K.neg (K.mul zp zq)))
          (K.add (K.mul zpq z) (K.neg (K.mul zp zq)))) ∧
         O.le (K.add (K.add (K.mul zpq z) (K.neg (K.mul zp zq))) (K.add (K.mul zpq z) (K.neg (K.mul zp zq))))
          (K.mul B (K.mul z z))) :
    let c := K.add (E K O F zpq z) (K.neg (K.mul (E K O F zp z) (E K O F zq z)))
    O.le (K.neg B) (K.add c c) ∧ O.le (K.add c c) B := by
  intro c
  have hzz0 : O.le K.zero (K.mul z z) := O.mul_nonneg _ _ hz0 hz0
  have hzz : K.mul z z ≠ K.zero := fun e => hz (O.pos_mul_eq_zero z z hz0 hz e)
  have d := div_bnd K O F _ B (K.mul z z) hzz0 hzz h10 h
  -- (D + D)/(z z) = c + c
  have ei : F.inv (K.mul z z) = K.mul (F.inv z) (F.inv z) := by
    have u : K.mul (K.mul z z) (K.mul (F.inv z) (F.inv z)) = K.one := by
      rw [K.mul_assoc, ← K.mul_assoc z (F.inv z), F.mul_inv z hz, K.one_mul, F.mul_inv z hz]
    have v := F.mul_inv (K.mul z z) hzz
    -- uniqueness of inverses
    calc F.inv (K.mul z z) = K.mul (F.inv (K.mul z z)) K.one :=
          (show K.mul (F.inv (K.mul z z)) K.one = F.inv (K.mul z z) by rw [K.mul_comm]; exact K.one_mul _).symm
      _ = K.mul (F.inv (K.mul z z)) (K.mul (K.mul z z) (K.mul (F.inv z) (F.inv z))) := by rw [u]
      _ = K.mul (K.mul (K.mul z z) (F.inv (K.mul z z))) (K.mul (F.inv z) (F.inv z)) := by
          rw [← K.mul_assoc, K.mul_comm (F.inv (K.mul z z))]
      _ = _ := by rw [v, K.one_mul]
  have ec : K.mul (K.add (K.add (K.mul zpq z) (K.neg (K.mul zp zq))) (K.add (K.mul zpq z) (K.neg (K.mul zp zq))))
      (F.inv (K.mul z z)) = K.add c c := by
    have one : K.mul (K.add (K.mul zpq z) (K.neg (K.mul zp zq))) (K.mul (F.inv z) (F.inv z)) = c := by
      show _ = K.add (K.mul zpq (F.inv z)) (K.neg (K.mul (K.mul zp (F.inv z)) (K.mul zq (F.inv z))))
      rw [DetChain.right_distrib, neg_mul']
      congr 1
      · rw [← K.mul_assoc, K.mul_assoc zpq z, F.mul_inv z hz, K.mul_comm zpq K.one, K.one_mul]
      · congr 1
        rw [K.mul_assoc, ← K.mul_assoc zq (F.inv z) (F.inv z), K.mul_comm zq (F.inv z), K.mul_assoc (F.inv z),
          ← K.mul_assoc zp, K.mul_comm (F.inv z) zq]
    rw [ei, DetChain.right_distrib, one]
  rw [ec] at d
  exact d

/-- **One volume step.** -/
theorem step_bound (eow eo ew c m κ : R) (h10 : K.one ≠ K.zero) (hm : O.le K.zero m) (hmne : m ≠ K.zero)
    (hκm : K.mul κ m = K.one) (hκ : O.le K.zero κ) (hw : O.le m ew)
    (hc : O.le (K.neg c) (K.add eow (K.neg (K.mul eo ew))) ∧ O.le (K.add eow (K.neg (K.mul eo ew))) c)
    (hc0 : O.le K.zero c) :
    O.le (K.neg (K.mul c κ)) (K.add (K.mul eow (F.inv ew)) (K.neg eo)) ∧
      O.le (K.add (K.mul eow (F.inv ew)) (K.neg eo)) (K.mul c κ) := by
  have hew0 : O.le K.zero ew := O.le_trans _ _ _ hm hw
  have hewne : ew ≠ K.zero := fun e => by
    rw [e] at hw; exact hmne (O.le_antisymm _ _ hw hm)
  have hi := F.inv_pos ew hew0 hewne
  -- eow/ew − eo = (eow − eo ew)/ew
  have e : K.add (K.mul eow (F.inv ew)) (K.neg eo) = K.mul (K.add eow (K.neg (K.mul eo ew))) (F.inv ew) := by
    rw [DetChain.right_distrib, neg_mul', K.mul_assoc, F.mul_inv ew hewne, K.mul_comm eo K.one, K.one_mul]
  rw [e]
  -- 1/ew ≤ κ
  have hiκ : O.le (F.inv ew) κ := by
    have a := O.mul_le_mul_right m ew (K.mul κ (F.inv ew)) hw (O.mul_nonneg _ _ hκ hi)
    have e1 : K.mul m (K.mul κ (F.inv ew)) = F.inv ew := by rw [← K.mul_assoc, K.mul_comm m κ, hκm, K.one_mul]
    have e2 : K.mul ew (K.mul κ (F.inv ew)) = κ := by
      rw [K.mul_comm κ, ← K.mul_assoc, F.mul_inv ew hewne, K.one_mul]
    rw [e1, e2] at a; exact a
  have b := bnd_mul K O _ _ _ _ hc0 hi hc ⟨O.le_trans _ _ _ (by
      have := neg_le_neg K O _ _ hi
      have e0 : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
      rw [e0] at this; exact this) hi, O.le_refl _⟩
  have cκ : O.le (K.mul c (F.inv ew)) (K.mul c κ) := by
    rw [K.mul_comm c, K.mul_comm c]; exact O.mul_le_mul_right _ _ _ hiκ hc0
  exact ⟨O.le_trans _ _ _ (neg_le_neg K O _ _ cκ) b.1, O.le_trans _ _ _ b.2 cκ⟩

end OField

#print axioms OField.div_bnd
#print axioms OField.cov_bound
#print axioms OField.step_bound

ym/Volume.lean

/-
  ym/Volume — T45, cycle 83 (Lad5 step 2): adding plaquettes to the volume.

    upd_upd         (M[y := true])[y := false] = M  when y ∉ M
    vol_add         Z_X(M ∪ {y}) = Z_{X·w_y}(M)                         (Haar structure, masks)
    tele            |a_{k+1} − a_k| ≤ δ for all k < n  ⇒  |a_n − a_0| ≤ n δ
  With OField.step_bound and the clustering bound (GapZd.gap_Zd) for O and w_y: as the volume grows by plaquettes at
  distance > R + 1 from O, every expectation ⟨O⟩ moves by at most (number of added plaquettes) · κ · B/2 · u^R —
  the uniform Cauchy estimate behind the infinite-volume limit.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.OField

namespace Volume

open YMChain LinkIntegral Seeds PosRatio HaarStr DupAlgH Vanish Signed

variable {R : Type} (K : DetChain.CRing R)

theorem upd_upd : ∀ (M : List Bool) (y : Nat), nthB M y = false → upd (upd M y true) y false = M
  | [], _, _ => rfl
  | false :: _, 0, _ => rfl
  | true :: _, 0, h => absurd h Bool.noConfusion
  | b :: M, y + 1, h => by show b :: upd (upd M y true) y false = b :: M; rw [upd_upd M y h]

theorem nth_upd_self : ∀ (M : List Bool) (y : Nat) (b : Bool), y < M.length → nthB (upd M y b) y = b
  | [], _, _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, 0, _, _ => rfl
  | _ :: M, y + 1, b, h => nth_upd_self M y b (Nat.lt_of_succ_lt_succ h)

/-- **Adding a plaquette multiplies the integrand by its weight.** -/
theorem vol_add {G : Type} (O : ORing K) {L : Nat} (H : Haar K O G L) (w : Nat → List G → R) (M : List Bool) (y : Nat)
    (hy : y < M.length) (hMy : nthB M y = false) (X : List G → R) :
    ZxH K O H w (upd M y true) X = ZxH K O H w M (fun x => K.mul (X x) (w y x)) := by
  unfold ZxH
  refine H.I_ext _ _ (fun x => ?_)
  rw [prodW_pick K w x 0 (upd M y true) y (by rw [len_upd]; exact hy) (nth_upd_self M y true hy), Nat.zero_add,
    upd_upd M y hMy, K.mul_assoc]

variable (O : ORing K)

/-- **Telescoping.** -/
theorem tele (a : Nat → R) (δ : R) (hδ : O.le K.zero δ) :
    ∀ n, (∀ k, k < n → O.le (K.neg δ) (K.add (a (k + 1)) (K.neg (a k))) ∧ O.le (K.add (a (k + 1)) (K.neg (a k))) δ) →
      O.le (K.neg (K.mul (Indep.natR K n) δ)) (K.add (a n) (K.neg (a 0))) ∧
        O.le (K.add (a n) (K.neg (a 0))) (K.mul (Indep.natR K n) δ)
  | 0, _ => by
    show O.le (K.neg (K.mul K.zero δ)) (K.add (a 0) (K.neg (a 0))) ∧ O.le (K.add (a 0) (K.neg (a 0))) (K.mul K.zero δ)
    rw [DetChain.zero_mul', K.add_neg]
    have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
    rw [e]; exact ⟨O.le_refl _, O.le_refl _⟩
  | n + 1, h => by
    have ih := tele a δ hδ n (fun k hk => h k (Nat.lt_succ_of_lt hk))
    have st := h n (Nat.lt_succ_self n)
    -- a(n+1) − a 0 = (a(n+1) − a n) + (a n − a 0)
    have e : K.add (a (n + 1)) (K.neg (a 0)) = K.add (K.add (a (n + 1)) (K.neg (a n))) (K.add (a n) (K.neg (a 0))) := by
      rw [K.add_assoc, ← K.add_assoc (K.neg (a n)), K.add_comm (K.neg (a n)) (a n), K.add_neg, K.zero_add]
    have eN : K.mul (Indep.natR K (n + 1)) δ = K.add δ (K.mul (Indep.natR K n) δ) := by
      show K.mul (K.add (Indep.natR K n) K.one) δ = _
      rw [DetChain.right_distrib, K.one_mul, K.add_comm]
    rw [e, eN]
    have b := GapG.bnd_add K O _ _ _ _ st ih
    exact b

end Volume

#print axioms Volume.vol_add
#print axioms Volume.tele

ym/Lad5.lean

/-
  ym/Lad5 — T45, cycle 84 (Lad5 step 3): the infinite-volume Cauchy estimate at strong coupling.

  Over a Haar structure and an ordered field. E_M(X) = Z_X(M)/Z_1(M).
    inv_mul', inv_inv'          inverse algebra
    vol_step      if ⟨w_y⟩_M ≥ m (κm = 1) and |⟨O w_y⟩_M − ⟨O⟩_M ⟨w_y⟩_M| ≤ c, then |⟨O⟩_{M∪{y}} − ⟨O⟩_M| ≤ c κ
    cauchy        for volumes M_0 ⊂ M_1 ⊂ ⋯ ⊂ M_n, each adding one plaquette y_k with the covariance of (O, w_{y_k}) in M_k
                  bounded by c:  |⟨O⟩_{M_n} − ⟨O⟩_{M_0}| ≤ n c κ
  With GapZd.gap_Zd, c = B/2 · u^R for plaquettes at distance > R + 1 from O; for boxes the number of added plaquettes
  grows polynomially while u^R decays exponentially, so ⟨O⟩_Λ is Cauchy as Λ ↑ Z^d — the infinite-volume limit
  exists in any complete ordered field (ℝ; completeness is the cited input), uniformly in the boundary condition.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Volume

namespace Lad5

open YMChain LinkIntegral Seeds PosRatio HaarStr DupAlgH OField Volume Signed

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OF K O)

theorem inv_unique' (a b : R) (ha : a ≠ K.zero) (h : K.mul a b = K.one) : F.inv a = b := by
  calc F.inv a = K.mul (F.inv a) K.one := (show K.mul (F.inv a) K.one = F.inv a by rw [K.mul_comm]; exact K.one_mul _).symm
    _ = K.mul (F.inv a) (K.mul a b) := by rw [h]
    _ = K.mul (K.mul a (F.inv a)) b := by rw [← K.mul_assoc, K.mul_comm (F.inv a) a]
    _ = b := by rw [F.mul_inv a ha, K.one_mul]

/-- `(a / b) / (c / b) = a / c`. -/
theorem div_div (a b c : R) (hb : b ≠ K.zero) (hc : c ≠ K.zero) :
    K.mul (K.mul a (F.inv b)) (F.inv (K.mul c (F.inv b))) = K.mul a (F.inv c) := by
  have e : F.inv (K.mul c (F.inv b)) = K.mul b (F.inv c) := inv_unique' K O F _ _ (fun h => by
      have this : K.mul (K.mul c (F.inv b)) b = K.mul K.zero b := congrArg (fun t => K.mul t b) h
      rw [K.mul_assoc, K.mul_comm (F.inv b) b, F.mul_inv b hb, K.mul_comm c K.one, K.one_mul, DetChain.zero_mul'] at this
      exact hc this)
    (by rw [K.mul_assoc, ← K.mul_assoc (F.inv b) b, K.mul_comm (F.inv b) b, F.mul_inv b hb, K.one_mul, F.mul_inv c hc])
  rw [e, K.mul_assoc, ← K.mul_assoc (F.inv b) b, K.mul_comm (F.inv b) b, F.mul_inv b hb, K.one_mul]

variable {G : Type} {L : Nat} (H : Haar K O G L)

/-- **One volume step.** -/
theorem vol_step (h10 : K.one ≠ K.zero) (w : Nat → List G → R) (M : List Bool) (y : Nat)
    (hy : y < M.length) (hMy : nthB M y = false) (Ob : List G → R)
    (m κ c : R) (hm : O.le K.zero m) (hmne : m ≠ K.zero) (hκm : K.mul κ m = K.one) (hκ : O.le K.zero κ)
    (hc0 : O.le K.zero c)
    (hZ0 : O.le K.zero (ZxH K O H w M (fun _ => K.one))) (hZ : ZxH K O H w M (fun _ => K.one) ≠ K.zero)
    (hEw : O.le m (E K O F (ZxH K O H w M (w y)) (ZxH K O H w M (fun _ => K.one))))
    (hcov : let Z := ZxH K O H w M (fun _ => K.one)
      O.le (K.neg c) (K.add (E K O F (ZxH K O H w M (fun x => K.mul (Ob x) (w y x))) Z)
        (K.neg (K.mul (E K O F (ZxH K O H w M Ob) Z) (E K O F (ZxH K O H w M (w y)) Z)))) ∧
      O.le (K.add (E K O F (ZxH K O H w M (fun x => K.mul (Ob x) (w y x))) Z)
        (K.neg (K.mul (E K O F (ZxH K O H w M Ob) Z) (E K O F (ZxH K O H w M (w y)) Z)))) c) :
    let Z := ZxH K O H w M (fun _ => K.one)
    let Z' := ZxH K O H w (upd M y true) (fun _ => K.one)
    O.le (K.neg (K.mul c κ)) (K.add (E K O F (ZxH K O H w (upd M y true) Ob) Z') (K.neg (E K O F (ZxH K O H w M Ob) Z))) ∧
      O.le (K.add (E K O F (ZxH K O H w (upd M y true) Ob) Z') (K.neg (E K O F (ZxH K O H w M Ob) Z))) (K.mul c κ) := by
  intro Z Z'
  have sb := step_bound K O F _ _ _ c m κ h10 hm hmne hκm hκ hEw hcov hc0
  -- rewrite the new-volume expectation
  have eO : ZxH K O H w (upd M y true) Ob = ZxH K O H w M (fun x => K.mul (Ob x) (w y x)) := vol_add K O H w M y hy hMy Ob
  have e1 : Z' = ZxH K O H w M (w y) := by
    show ZxH K O H w (upd M y true) (fun _ => K.one) = _
    rw [vol_add K O H w M y hy hMy]
    exact H.I_ext _ _ (fun x => by show K.mul (K.mul K.one (w y x)) _ = _; rw [K.one_mul])
  have hEw0 : O.le K.zero (E K O F (ZxH K O H w M (w y)) Z) := O.le_trans _ _ _ hm hEw
  have hZwne : ZxH K O H w M (w y) ≠ K.zero := fun h => by
    have : E K O F (ZxH K O H w M (w y)) Z = K.zero := by unfold E; rw [h, DetChain.zero_mul']
    rw [this] at hEw; exact hmne (O.le_antisymm _ _ hEw hm)
  show O.le _ (K.add (K.mul (ZxH K O H w (upd M y true) Ob) (F.inv Z')) _) ∧ O.le (K.add (K.mul (ZxH K O H w (upd M y true) Ob) (F.inv Z')) _) _
  rw [eO, e1, ← div_div K O F _ Z _ hZ hZwne]
  exact sb

/-- **Cauchy estimate along a growing sequence of volumes.** -/
theorem cauchy (a : Nat → R) (δ : R) (hδ : O.le K.zero δ) (n : Nat)
    (hstep : ∀ k, k < n → O.le (K.neg δ) (K.add (a (k + 1)) (K.neg (a k))) ∧ O.le (K.add (a (k + 1)) (K.neg (a k))) δ) :
    O.le (K.neg (K.mul (Indep.natR K n) δ)) (K.add (a n) (K.neg (a 0))) ∧
      O.le (K.add (a n) (K.neg (a 0))) (K.mul (Indep.natR K n) δ) :=
  tele K O a δ hδ n hstep

end Lad5

#print axioms Lad5.div_div
#print axioms Lad5.vol_step
#print axioms Lad5.cauchy

ym/BallSize.lean

/-
  ym/BallSize — T45, cycle 85 (Lad5 step 4): closure balls grow at most geometrically.

  For seeds σ (s = number of seed plaquettes) and a plaquette list P of degree ≤ Dg:
    cnt_vAux        |A ∪ {r}| ≤ |A| + |{r}|
    cnt_andB_le     |S ∧ X| ≤ |X|
    iter_step_cnt   |iter (k+1)| ≤ s + (Dg + 1) |iter k|
    ball_size       |ball k| ≤ s (Dg + 2)^k
  So the number of plaquettes reached in k closure steps is at most s (Dg+2)^k: summing the volume steps of Lad5 over
  shells gives a geometric series with ratio (Dg + 2) u, convergent at strong coupling.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Lad5
import FTAChain

namespace BallSize

open YMChain Closure Factor Seeds Count PolyBound Reach AreaUpper

theorem cnt_vAux (r : List Nat → Bool) : ∀ (P : List (List Nat)) (A : List Bool),
    cnt (vAux r P A) ≤ cnt A + cnt (P.map r)
  | [], _ => Nat.zero_le _
  | _ :: _, [] => Nat.zero_le _
  | q :: P, a :: A => by
    have ih := cnt_vAux r P A
    show cnt ((a || r q) :: vAux r P A) ≤ cnt (a :: A) + cnt (r q :: P.map r)
    cases a <;> cases e : r q
    · exact ih
    · show cnt _ + 1 ≤ cnt A + (cnt (P.map r) + 1); exact Nat.succ_le_succ ih
    · show cnt _ + 1 ≤ cnt A + 1 + cnt (P.map r); rw [Nat.succ_add]; exact Nat.succ_le_succ ih
    · show cnt _ + 1 ≤ cnt A + 1 + (cnt (P.map r) + 1); rw [Nat.succ_add]; exact Nat.succ_le_succ (Nat.le_succ_of_le ih)

theorem cnt_andB_le : ∀ S X : List Bool, cnt (andB S X) ≤ cnt X
  | [], _ => Nat.zero_le _
  | _ :: _, [] => Nat.le_refl _
  | s :: S, x :: X => by
    have ih := cnt_andB_le S X
    cases s <;> cases x
    · exact ih
    · exact Nat.le_succ_of_le ih
    · exact ih
    · exact Nat.succ_le_succ ih

theorem iter_step_cnt (σ : List Nat → Bool) (P : List (List Nat)) (Dg : Nat)
    (hdeg : ∀ p, memL p P → cnt (P.map (fun q => touch q p)) ≤ Dg) (S : List Bool) (k : Nat) :
    cnt (iter (Vs σ P) P.length S (k + 1)) ≤ cnt (P.map σ) + (Dg + 1) * cnt (iter (Vs σ P) P.length S k) := by
  let A := iter (Vs σ P) P.length S k
  show cnt (andB S (vAux (fun q => σ q || anyT P A q) P A)) ≤ _
  refine Nat.le_trans (cnt_andB_le _ _) (Nat.le_trans (cnt_vAux _ P A) ?_)
  have h2 := Nat.le_trans (cnt_map_or σ (fun q => anyT P A q) P) (Nat.add_le_add_left (cnt_anyT P Dg P A hdeg) _)
  -- cnt A + (s + Dg cnt A) ≤ s + (Dg+1) cnt A
  refine Nat.le_trans (Nat.add_le_add_left h2 _) (Nat.le_of_eq ?_)
  rw [Nat.succ_mul, Nat.add_comm (cnt A) (cnt (P.map σ) + Dg * cnt A), Nat.add_assoc]

theorem pow_ge_one (b : Nat) (hb : 1 ≤ b) : ∀ k, 1 ≤ b ^ k
  | 0 => Nat.le_refl 1
  | k + 1 => by
    show 1 ≤ b ^ k * b
    exact Nat.le_trans (pow_ge_one b hb k) (Nat.le_mul_of_pos_right _ hb)

/-- **Ball size.** -/
theorem ball_size (σ : List Nat → Bool) (P : List (List Nat)) (Dg : Nat)
    (hdeg : ∀ p, memL p P → cnt (P.map (fun q => touch q p)) ≤ Dg) :
    ∀ k, cnt (ball σ P k) ≤ cnt (P.map σ) * (Dg + 2) ^ k
  | 0 => by
    show cnt (fz P.length) ≤ _
    rw [cnt_fz]; exact Nat.zero_le _
  | k + 1 => by
    have ih := ball_size σ P Dg hdeg k
    refine Nat.le_trans (iter_step_cnt σ P Dg hdeg (tr P.length) k) ?_
    let s := cnt (P.map σ)
    let b := (Dg + 2) ^ k
    have hb : 1 ≤ b := pow_ge_one _ (Nat.succ_le_succ (Nat.zero_le _)) k
    -- s + (Dg+1) * c ≤ s + (Dg+1) * (s b) ≤ s b + (Dg+1) s b = s b (Dg+2)
    have t1 : s + (Dg + 1) * cnt (ball σ P k) ≤ s * b + (Dg + 1) * (s * b) :=
      Nat.add_le_add (Nat.le_mul_of_pos_right s hb) (Nat.mul_le_mul_left _ ih)
    refine Nat.le_trans t1 (Nat.le_of_eq ?_)
    show s * b + (Dg + 1) * (s * b) = s * (b * (Dg + 2))
    rw [Nat.mul_comm (Dg + 1) (s * b), Nat.add_comm, ← Nat.mul_succ, FTAChain.nmul_assoc]

end BallSize

#print axioms BallSize.iter_step_cnt
#print axioms BallSize.ball_size

ym/ShellSum.lean

/-
  ym/ShellSum — T45, cycle 86 (Lad5 step 5): summing volume steps by shells.

    tele_var       |a_{k+1} − a_k| ≤ δ_k for k < n  ⇒  |a_n − a_0| ≤ Σ_{k<n} δ_k
    rng N          [0, …, N−1]
    group_sum      Σ_{y ∈ ys} f(sh y) = Σ_{k < N} f(k) · #{y ∈ ys : sh y = k}      (all shells < N)
    trueIdx        the positions where a mask is true; its length is cnt
    shell_count    if every y with sh y = k lies in the mask B_k, then #{y ∈ ys : sh y = k} ≤ cnt B_k   (ys distinct)

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.BallSize

namespace ShellSum

open YMChain Closure Seeds InjCount Signed

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

def rng : Nat → List Nat
  | 0 => []
  | n + 1 => rng n ++ [n]

/-- **Telescoping with variable steps.** -/
theorem tele_var (a δ : Nat → R) : ∀ n, (∀ k, k < n → O.le (K.neg (δ k)) (K.add (a (k + 1)) (K.neg (a k))) ∧
      O.le (K.add (a (k + 1)) (K.neg (a k))) (δ k)) →
    O.le (K.neg (sumK K δ (rng n))) (K.add (a n) (K.neg (a 0))) ∧
      O.le (K.add (a n) (K.neg (a 0))) (sumK K δ (rng n))
  | 0, _ => by
    show O.le (K.neg K.zero) (K.add (a 0) (K.neg (a 0))) ∧ O.le (K.add (a 0) (K.neg (a 0))) K.zero
    rw [K.add_neg]
    have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
    rw [e]; exact ⟨O.le_refl _, O.le_refl _⟩
  | n + 1, h => by
    have ih := tele_var a δ n (fun k hk => h k (Nat.lt_succ_of_lt hk))
    have st := h n (Nat.lt_succ_self n)
    have e : K.add (a (n + 1)) (K.neg (a 0)) = K.add (K.add (a (n + 1)) (K.neg (a n))) (K.add (a n) (K.neg (a 0))) := by
      rw [K.add_assoc, ← K.add_assoc (K.neg (a n)), K.add_comm (K.neg (a n)) (a n), K.add_neg, K.zero_add]
    have es : sumK K δ (rng (n + 1)) = K.add (δ n) (sumK K δ (rng n)) := by
      show sumK K δ (rng n ++ [n]) = _
      rw [MaskSum.sumK_app]
      show K.add (sumK K δ (rng n)) (K.add (δ n) K.zero) = _
      rw [DetChain.add_zero', K.add_comm]
    rw [e, es]
    exact GapG.bnd_add K O _ _ _ _ st ih

theorem mem_rng : ∀ (N k : Nat), memL k (rng N) → k < N
  | 0, _, h => h.elim
  | N + 1, k, h => by
    cases GPerm.memL_app k (rng N) [N] h with
    | inl h' => exact Nat.lt_succ_of_lt (mem_rng N k h')
    | inr h' => cases h' with
      | inl e => rw [e]; exact Nat.lt_succ_self N
      | inr e => exact e.elim

/-- selecting one index from `rng N` -/
theorem sel_rng (f : Nat → R) : ∀ (N j : Nat), j < N →
    sumK K (fun k => if j = k then f k else K.zero) (rng N) = f j
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | N + 1, j, h => by
    show sumK K _ (rng N ++ [N]) = f j
    rw [MaskSum.sumK_app]
    show K.add _ (K.add (if j = N then f N else K.zero) K.zero) = f j
    rw [DetChain.add_zero']
    by_cases e : j = N
    · rw [MaskSum.ip e, e]
      rw [(MaskSum.sumK_ext_mem K _ (fun _ => K.zero) (rng N) (fun k hk =>
          MaskSum.ineg (fun h' => Nat.lt_irrefl N (by have := mem_rng N k hk; rw [← h'] at this; exact this)))).trans (MaskSum.sum_zero K _), K.zero_add]
    · rw [MaskSum.ineg e, DetChain.add_zero']
      exact sel_rng f N j (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) e)

def ccount (sh : Nat → Nat) (k : Nat) (ys : List Nat) : Nat := cnt (ys.map (fun y => decide (sh y = k)))

/-- **Grouping by shells.** -/
theorem group_sum (f : Nat → R) (sh : Nat → Nat) (N : Nat) : ∀ ys : List Nat, (∀ y, memL y ys → sh y < N) →
    sumK K (fun y => f (sh y)) ys = sumK K (fun k => K.mul (f k) (Indep.natR K (ccount sh k ys))) (rng N)
  | [], _ => by
    show K.zero = _
    exact ((sumK_ext K _ (fun _ => K.zero) (fun k => DetChain.mul_zero K _) _).trans (MaskSum.sum_zero K _)).symm
  | y :: ys, h => by
    show K.add (f (sh y)) (sumK K (fun y => f (sh y)) ys) = _
    rw [group_sum f sh N ys (fun y' hy' => h y' (Or.inr hy'))]
    have e : ∀ k, K.mul (f k) (Indep.natR K (ccount sh k (y :: ys)))
        = K.add (if sh y = k then f k else K.zero) (K.mul (f k) (Indep.natR K (ccount sh k ys))) := by
      intro k
      show K.mul (f k) (Indep.natR K (cnt (decide (sh y = k) :: ys.map (fun y => decide (sh y = k))))) = _
      by_cases c : sh y = k
      · rw [MaskSum.ip c, decide_eq_true c]
        show K.mul (f k) (K.add (Indep.natR K (ccount sh k ys)) K.one) = _
        rw [K.distrib, K.mul_comm (f k) K.one, K.one_mul, K.add_comm]
      · rw [MaskSum.ineg c, decide_eq_false c, K.zero_add]; rfl
    rw [sumK_ext K _ _ e, sumK_add, sel_rng K f N (sh y) (h y (Or.inl rfl))]

def tI : List Bool → Nat → List Nat
  | [], _ => []
  | true :: B, o => o :: tI B (o + 1)
  | false :: B, o => tI B (o + 1)

theorem len_tI : ∀ (B : List Bool) (o : Nat), (tI B o).length = cnt B
  | [], _ => rfl
  | true :: B, o => congrArg Nat.succ (len_tI B (o + 1))
  | false :: B, o => len_tI B (o + 1)

theorem mem_tI : ∀ (B : List Bool) (o y : Nat), nthB B y = true → memL (o + y) (tI B o)
  | [], _, _, h => absurd h Bool.noConfusion
  | true :: _, o, 0, _ => Or.inl rfl
  | false :: _, _, 0, h => absurd h Bool.noConfusion
  | true :: B, o, y + 1, h => Or.inr (by have := mem_tI B (o + 1) y h; rw [Nat.add_assoc, Nat.add_comm 1 y] at this; exact this)
  | false :: B, o, y + 1, h => by have := mem_tI B (o + 1) y h; rw [Nat.add_assoc, Nat.add_comm 1 y] at this; exact this

/-- **Counting one shell.** -/
theorem shell_count (sh : Nat → Nat) (k : Nat) (B : List Bool) (ys : List Nat) (hnd : nodupL ys)
    (h : ∀ y, memL y ys → sh y = k → nthB B y = true) : ccount sh k ys ≤ cnt B := by
  rw [← len_tI B 0]
  exact count_inj (fun y => decide (sh y = k)) (fun y => y) ys (tI B 0) hnd
    (fun y hy hφ => by have := mem_tI B 0 y (h y hy (of_decide_eq_true hφ)); rw [Nat.zero_add] at this; exact this)
    (fun _ _ _ _ _ _ e => e)

end ShellSum

#print axioms ShellSum.tele_var
#print axioms ShellSum.sel_rng
#print axioms ShellSum.group_sum
#print axioms ShellSum.shell_count

ym/Geom.lean

/-
  ym/Geom — T45, cycle 87 (Lad5 step 6): natural-number casts and the geometric tail.

    natR_add, natR_mul, natR_pow, natR_mono      casts of ℕ into the ordered ring
    geom_tail        0 ≤ r ≤ 1:  (1 − r) · Σ_{K ≤ k < N} r^k ≤ r^K
    shell_bound      if every y ∈ ys has K ≤ sh y < N and the k-th shell has at most s β^k elements, then
                     Σ_{y ∈ ys} C u^{sh y} ≤ C s Σ_{K ≤ k < N} (u β)^k
  With β = Dg + 2 (BallSize) and r = u(Dg + 2) < 1 at strong coupling, the total change of ⟨O⟩ when plaquettes outside
  the K-ball are added is at most κ C s r^K/(1 − r), uniformly in the final volume.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.ShellSum

namespace Geom

open YMChain LinkIntegral Indep Signed ShellSum Closure

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem natR_add (a : Nat) : ∀ b, natR K (a + b) = K.add (natR K a) (natR K b)
  | 0 => by show natR K a = K.add (natR K a) K.zero; rw [DetChain.add_zero']
  | b + 1 => by show K.add (natR K (a + b)) K.one = K.add (natR K a) (K.add (natR K b) K.one); rw [natR_add a b, K.add_assoc]

theorem natR_mul (a : Nat) : ∀ b, natR K (a * b) = K.mul (natR K a) (natR K b)
  | 0 => (DetChain.mul_zero K _).symm
  | b + 1 => by
    show natR K (a * b + a) = K.mul (natR K a) (K.add (natR K b) K.one)
    rw [natR_add, natR_mul a b, K.distrib, K.mul_comm (natR K a) K.one, K.one_mul]

theorem natR_pow (a : Nat) : ∀ k, natR K (a ^ k) = pwR K (natR K a) k
  | 0 => by show K.add K.zero K.one = K.one; rw [K.zero_add]
  | k + 1 => by show natR K (a ^ k * a) = K.mul (pwR K (natR K a) k) (natR K a); rw [natR_mul, natR_pow a k]

theorem natR_nn (h1 : O.le K.zero K.one) : ∀ a, O.le K.zero (natR K a)
  | 0 => O.le_refl _
  | a + 1 => by have := O.add_le_add _ _ _ _ (natR_nn h1 a) h1; rw [K.zero_add] at this; exact this

theorem natR_mono (h1 : O.le K.zero K.one) (a b : Nat) (h : a ≤ b) : O.le (natR K a) (natR K b) := by
  obtain ⟨c, e⟩ := Nat.le.dest h
  rw [← e, natR_add]
  have := O.add_le_add _ _ _ _ (O.le_refl (natR K a)) (natR_nn K O h1 c)
  rw [DetChain.add_zero'] at this; exact this

/-- `Σ_{K ≤ k < N} r^k` -/
def tail (r : R) (Kk : Nat) : Nat → R
  | 0 => K.zero
  | N + 1 => K.add (tail r Kk N) (if Kk ≤ N then pwR K r N else K.zero)

theorem tail_zero (r : R) (Kk : Nat) : ∀ N, N ≤ Kk → tail K r Kk N = K.zero
  | 0, _ => rfl
  | N + 1, h => by
    show K.add (tail K r Kk N) (if Kk ≤ N then pwR K r N else K.zero) = K.zero
    rw [tail_zero r Kk N (Nat.le_of_succ_le h), MaskSum.ineg (fun h' => Nat.not_succ_le_self N (Nat.le_trans h h')), K.zero_add]

/-- **Geometric tail.** -/
theorem geom_tail (h1 : O.le K.zero K.one) (r : R) (hr0 : O.le K.zero r) (hr1 : O.le r K.one) (Kk : Nat) :
    ∀ N, O.le (K.mul (K.add K.one (K.neg r)) (tail K r Kk N)) (pwR K r Kk) := by
  -- (1 − r) Σ_{K≤k<N} r^k = r^K − r^N (N ≥ K) or 0
  have key : ∀ N, K.add (K.mul (K.add K.one (K.neg r)) (tail K r Kk N)) (if Kk ≤ N then pwR K r N else pwR K r Kk)
      = pwR K r Kk := by
    intro N
    induction N with
    | zero =>
      show K.add (K.mul _ K.zero) (if Kk ≤ 0 then pwR K r 0 else pwR K r Kk) = _
      rw [DetChain.mul_zero, K.zero_add]
      by_cases e : Kk ≤ 0
      · rw [MaskSum.ip e, Nat.le_zero.mp e]
      · rw [MaskSum.ineg e]
    | succ N ih =>
      show K.add (K.mul (K.add K.one (K.neg r)) (K.add (tail K r Kk N) (if Kk ≤ N then pwR K r N else K.zero)))
        (if Kk ≤ N + 1 then pwR K r (N + 1) else pwR K r Kk) = _
      by_cases e : Kk ≤ N
      · rw [MaskSum.ip e, MaskSum.ip (Nat.le_succ_of_le e)] at *
        rw [K.distrib, K.add_assoc, ← ih]
        congr 1
        -- (1 − r) r^N + r^{N+1} = r^N
        show K.add (K.mul (K.add K.one (K.neg r)) (pwR K r N)) (K.mul (pwR K r N) r) = pwR K r N
        rw [DetChain.right_distrib, K.one_mul, neg_mul', K.mul_comm r, K.add_assoc, K.add_comm (K.neg _), K.add_neg,
          DetChain.add_zero']
      · have tz : tail K r Kk N = K.zero := tail_zero K r Kk N (Nat.le_of_lt (Nat.lt_of_not_le e))
        rw [MaskSum.ineg e, tz, DetChain.add_zero', DetChain.mul_zero, K.zero_add]
        by_cases e2 : Kk ≤ N + 1
        · rw [MaskSum.ip e2, Nat.le_antisymm e2 (Nat.lt_of_not_le e)]
        · rw [MaskSum.ineg e2]
  intro N
  have := key N
  have hp : O.le K.zero (if Kk ≤ N then pwR K r N else pwR K r Kk) := by
    by_cases e : Kk ≤ N
    · rw [MaskSum.ip e]; exact AreaLower.pw_nn K O r hr0 h1 N
    · rw [MaskSum.ineg e]; exact AreaLower.pw_nn K O r hr0 h1 Kk
  have a := O.add_le_add _ _ _ _ (O.le_refl (K.mul (K.add K.one (K.neg r)) (tail K r Kk N))) hp
  rw [DetChain.add_zero', this] at a
  exact a

theorem rng_tail (r c : R) (Kk : Nat) : ∀ N,
    sumK K (fun k => if Kk ≤ k then K.mul c (pwR K r k) else K.zero) (rng N) = K.mul c (tail K r Kk N)
  | 0 => (DetChain.mul_zero K c).symm
  | N + 1 => by
    show sumK K _ (rng N ++ [N]) = K.mul c (K.add (tail K r Kk N) (if Kk ≤ N then pwR K r N else K.zero))
    rw [MaskSum.sumK_app, rng_tail r c Kk N, K.distrib]
    congr 1
    show K.add (if Kk ≤ N then K.mul c (pwR K r N) else K.zero) K.zero = _
    rw [DetChain.add_zero']
    by_cases e : Kk ≤ N
    · rw [MaskSum.ip e, MaskSum.ip e]
    · rw [MaskSum.ineg e, MaskSum.ineg e, DetChain.mul_zero]

theorem ccount_zero (sh : Nat → Nat) (k : Nat) : ∀ ys : List Nat, (∀ y, memL y ys → sh y ≠ k) → ccount sh k ys = 0
  | [], _ => rfl
  | y :: ys, h => by
    show cnt (decide (sh y = k) :: _) = 0
    rw [decide_eq_false (h y (Or.inl rfl))]
    exact ccount_zero sh k ys (fun y' hy' => h y' (Or.inr hy'))

/-- **Shell bound.** -/
theorem shell_bound (h1 : O.le K.zero K.one) (C u : R) (hC : O.le K.zero C) (hu : O.le K.zero u)
    (sh : Nat → Nat) (Kk N s β : Nat) (ys : List Nat)
    (hsh : ∀ y, memL y ys → Kk ≤ sh y ∧ sh y < N) (hcnt : ∀ k, ccount sh k ys ≤ s * β ^ k) :
    O.le (sumK K (fun y => K.mul C (pwR K u (sh y))) ys)
      (K.mul (K.mul C (natR K s)) (tail K (K.mul u (natR K β)) Kk N)) := by
  rw [group_sum K (fun k => K.mul C (pwR K u k)) sh N ys (fun y hy => (hsh y hy).2), ← rng_tail K]
  refine MaskSum.sumK_le K O _ _ (rng N) (fun k _ => ?_)
  by_cases e : Kk ≤ k
  · rw [MaskSum.ip e]
    have hm := natR_mono K O h1 _ _ (hcnt k)
    rw [natR_mul, natR_pow] at hm
    have t := O.mul_le_mul_right _ _ _ hm (O.mul_nonneg _ _ hC (AreaLower.pw_nn K O u hu h1 k))
    rw [K.mul_comm (natR K (ccount sh k ys)), K.mul_comm (K.mul (natR K s) (pwR K (natR K β) k))] at t
    have mul4 : ∀ a b c d : R, K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := fun a b c d => by
      rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]
    refine O.le_trans _ _ _ t ?_
    rw [GKS2.pwR_mul, mul4]
    exact O.le_refl _
  · rw [MaskSum.ineg e, ccount_zero sh k ys (fun y hy h => e (h ▸ (hsh y hy).1))]
    show O.le (K.mul (K.mul C (pwR K u k)) K.zero) K.zero
    rw [DetChain.mul_zero]; exact O.le_refl _

end Geom

#print axioms Geom.natR_pow
#print axioms Geom.natR_mono
#print axioms Geom.geom_tail
#print axioms Geom.shell_bound

ym/Lad5Cauchy.lean

/-
  ym/Lad5Cauchy — T45, cycle 88 (Lad5 step 7): the uniform infinite-volume Cauchy estimate.

  a k = ⟨O⟩ in the volume after adding the plaquettes y 0, …, y (k−1) (distinct). If each step moves ⟨O⟩ by at most
  κ C u^{sh(y k)} (Lad5.vol_step with the clustering bound of GapZd.gap_Zd, sh = closure shell of the added plaquette
  relative to O), every added plaquette lies outside the K-ball (sh ≥ K), and shells are counted by balls
  (#shell k ≤ s β^k, BallSize + ShellSum.shell_count with β = Dg + 2), then with r = uβ ≤ 1:
    uniform_cauchy     (1 − r) · |⟨O⟩_{final} − ⟨O⟩_{initial}| ≤ κ C s r^K
  independently of the number of added plaquettes. So, at strong coupling (r < 1), ⟨O⟩_Λ is Cauchy as Λ exhausts the
  lattice (any order, any boundary); in a complete field (ℝ, cited) the infinite-volume limit exists.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Geom

namespace Lad5Cauchy

open YMChain LinkIntegral Indep Signed ShellSum Geom Closure InjCount

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem sum_rng_map (g : Nat → R) (y : Nat → Nat) : ∀ n, sumK K (fun k => g (y k)) (rng n) = sumK K g ((rng n).map y)
  | n => (MaskSum.sumK_map K g y (rng n)).symm

/-- **Uniform Cauchy estimate.** -/
theorem uniform_cauchy (h1 : O.le K.zero K.one) (a : Nat → R) (y : Nat → Nat) (n : Nat)
    (κ C u : R) (hκ : O.le K.zero κ) (hC : O.le K.zero C) (hu : O.le K.zero u)
    (sh : Nat → Nat) (Kk N s β : Nat)
    (hstep : ∀ k, k < n → O.le (K.neg (K.mul (K.mul κ C) (pwR K u (sh (y k))))) (K.add (a (k + 1)) (K.neg (a k))) ∧
      O.le (K.add (a (k + 1)) (K.neg (a k))) (K.mul (K.mul κ C) (pwR K u (sh (y k)))))
    (hsh : ∀ k, k < n → Kk ≤ sh (y k) ∧ sh (y k) < N)
    (hcnt : ∀ k, ccount sh k ((rng n).map y) ≤ s * β ^ k)
    (hr1 : O.le (K.mul u (natR K β)) K.one) :
    let r := K.mul u (natR K β)
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) := by
  intro r T
  have tv := tele_var K O a (fun k => K.mul (K.mul κ C) (pwR K u (sh (y k)))) n hstep
  rw [sum_rng_map K (fun z => K.mul (K.mul κ C) (pwR K u (sh z))) y n] at tv
  have hmem : ∀ z, memL z ((rng n).map y) → Kk ≤ sh z ∧ sh z < N := fun z hz => by
    obtain ⟨k, hk, e⟩ := HaarFin.memL_map' y z (rng n) hz
    rw [e]; exact hsh k (mem_rng n k hk)
  have sb := shell_bound K O h1 (K.mul κ C) u (O.mul_nonneg _ _ hκ hC) hu sh Kk N s β ((rng n).map y) hmem hcnt
  refine ⟨O.le_trans _ _ _ (neg_le_neg K O _ _ sb) tv.1, O.le_trans _ _ _ tv.2 sb, ?_⟩
  -- (1 − r) T = (κ C s) ((1 − r) tail) ≤ (κ C s) r^K
  have hr0 : O.le K.zero r := O.mul_nonneg _ _ hu (natR_nn K O h1 β)
  have gt := geom_tail K O h1 r hr0 hr1 Kk N
  have hc : O.le K.zero (K.mul (K.mul κ C) (natR K s)) := O.mul_nonneg _ _ (O.mul_nonneg _ _ hκ hC) (natR_nn K O h1 s)
  have := O.mul_le_mul_right _ _ _ gt hc
  show O.le (K.mul (K.add K.one (K.neg r)) (K.mul (K.mul (K.mul κ C) (natR K s)) (tail K r Kk N))) _
  let c := K.mul (K.mul κ C) (natR K s)
  rw [← K.mul_assoc, K.mul_comm (K.add K.one (K.neg r)) c, K.mul_assoc,
    K.mul_comm c (K.mul (K.add K.one (K.neg r)) (tail K r Kk N)), K.mul_comm c (pwR K r Kk)]
  exact this

end Lad5Cauchy

#print axioms Lad5Cauchy.uniform_cauchy

ym/OSLimit.lean

/-
  ym/OSLimit — T45, cycle 2 (towards (A)): reflection positivity survives the continuum limit.

  Over an ordered commutative ring (YMChain.ORing), with x < y meaning x ≤ y ∧ x ≠ y:
    nonneg_of_approx   if every x_n ≥ 0 and for every ε > 0 some x_n < x + ε, then x ≥ 0
    rp_limit           if every lattice spacing gives a reflection-positive form
                       (∀ c, Q_n(c) ≥ 0) and Q_n(c) approaches Q(c) in this one-sided sense,
                       then Q is reflection positive
    rp_limit_kernel    the same for Gram kernels Σ_{x,y} F(x) F(y) K_n(x, y) (YMChain.rp_nonneg at
                       every spacing, positivity of the limit)
  So OS positivity of a continuum limit is automatic once the correlation functions converge:
  the remaining parts of (A) are existence of the limit (tightness / uniform bounds) and
  Euclidean invariance.

  The case split on x = 0 uses a DecidableEq instance on the ring (no classical axiom).
  Lean 4 core only, proofs by hand, no axioms.
-/
import YMChain

namespace OSLimit

open YMChain

variable {R : Type} [DecidableEq R] (K : DetChain.CRing R) (O : ORing K)

def Lt (x y : R) : Prop := O.le x y ∧ x ≠ y

omit [DecidableEq R] in
/-- `x ≤ 0` gives `0 ≤ -x`. -/
theorem neg_nonneg (x : R) (h : O.le x K.zero) : O.le K.zero (K.neg x) := by
  have := O.add_le_add _ _ _ _ h (O.le_refl (K.neg x))
  rw [K.add_neg, K.zero_add] at this
  exact this

omit [DecidableEq R] in
theorem neg_ne_zero (x : R) (h : x ≠ K.zero) : K.neg x ≠ K.zero := fun hn => by
  apply h
  have e : K.add x (K.neg x) = K.zero := K.add_neg x
  rw [hn, DetChain.add_zero'] at e
  exact e

/-- **Nonnegativity passes to limits.** -/
theorem nonneg_of_approx (xs : Nat → R) (x : R) (hn : ∀ n, O.le K.zero (xs n))
    (happ : ∀ ε, Lt K O K.zero ε → ∃ n, Lt K O (xs n) (K.add x ε)) : O.le K.zero x := by
  cases O.le_total K.zero x with
  | inl h => exact h
  | inr h =>
    by_cases hx : x = K.zero
    · rw [hx]; exact O.le_refl _
    · have hε : Lt K O K.zero (K.neg x) := ⟨neg_nonneg K O x h, fun e => neg_ne_zero K x hx e.symm⟩
      obtain ⟨n, hle, hne⟩ := happ (K.neg x) hε
      rw [K.add_neg] at hle hne
      exact absurd (O.le_antisymm _ _ hle (hn n)) hne

/-- **Reflection positivity of the limit form.** `Q n c` is the lattice form at spacing `n`
    (for instance `Σ c_i c_j ⟨θ F_i F_j⟩`), `Q∞ c` the limit. -/
theorem rp_limit {Cf : Type} (Q : Nat → Cf → R) (Qlim : Cf → R)
    (hpos : ∀ n c, O.le K.zero (Q n c))
    (happ : ∀ c ε, Lt K O K.zero ε → ∃ n, Lt K O (Q n c) (K.add (Qlim c) ε)) (c : Cf) :
    O.le K.zero (Qlim c) :=
  nonneg_of_approx K O (fun n => Q n c) (Qlim c) (fun n => hpos n c) (happ c)

/-- **Gram kernels at every spacing give a positive limit.** At spacing `n` the crossing kernel is a
    Gram kernel with nonnegative coefficients (YMChain.rp_nonneg); if the reflection-positivity
    sums approach `S F`, then `S F ≥ 0`. -/
theorem rp_limit_kernel {X : Type} (Ln : Nat → List (R × (X → R))) (P : Positivity K)
    (hP : ∀ r, P.nn r → O.le K.zero r) (xs : List X)
    (hL : ∀ n, AllNN K P (Ln n)) (S : (X → R) → R)
    (happ : ∀ F ε, Lt K O K.zero ε → ∃ n,
      Lt K O (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (ker K (Ln n) x y)) xs) xs)
        (K.add (S F) ε)) (F : X → R) :
    O.le K.zero (S F) :=
  nonneg_of_approx K O _ (S F) (fun n => hP _ (rp_nonneg K P F xs (Ln n) (hL n))) (happ F)

end OSLimit

#print axioms OSLimit.neg_nonneg
#print axioms OSLimit.neg_ne_zero
#print axioms OSLimit.nonneg_of_approx
#print axioms OSLimit.rp_limit
#print axioms OSLimit.rp_limit_kernel

ym/SymLimit.lean

/-
  ym/SymLimit — T45, cycle 4 (towards (A)): lattice symmetries survive the continuum limit.

  Ordered commutative ring (YMChain.ORing) with an element h (a half: h + h = 1) and decidable
  equality. Two-sided closeness: Near x y ε := x ≤ y + ε ∧ y ≤ x + ε.
    le_of_near_all     if x ≤ y + ε for every ε > 0, then x ≤ y
    lim_unique         a sequence that comes ε-near both x and y, for every ε > 0, forces x = y
    sym_limit          if every lattice correlation is invariant under σ (S n (σ c) = S n c) and
                       S n → S two-sidedly at every c, then S (σ c) = S c
  So hypercubic invariance, gauge invariance and reflection symmetry hold in any continuum limit;
  full rotation invariance needs restoration (the lattice has only the hypercubic group).

  Lean 4 core only, proofs by hand, no axioms.
-/
import YMChain

namespace SymLimit

open YMChain

variable {R : Type} [DecidableEq R] (K : DetChain.CRing R) (O : ORing K)

def Pos (x : R) : Prop := O.le K.zero x ∧ x ≠ K.zero

def Near (x y ε : R) : Prop := O.le x (K.add y ε) ∧ O.le y (K.add x ε)

omit [DecidableEq R] in
theorem add_le_add_left' (a b c : R) (h : O.le a b) : O.le (K.add c a) (K.add c b) := by
  have := O.add_le_add _ _ _ _ h (O.le_refl c)
  rw [K.add_comm a c, K.add_comm b c] at this
  exact this

omit [DecidableEq R] in
theorem sub_nonneg_of_le (x y : R) (h : O.le x y) : O.le K.zero (K.add y (K.neg x)) := by
  have := O.add_le_add _ _ _ _ h (O.le_refl (K.neg x))
  rw [K.add_neg] at this
  exact this

/-- If `x ≤ y + ε` for every `ε > 0` then `x ≤ y` (with a half `h`, `0 ≤ h`, `h ≠ 0`). -/
theorem le_of_near_all (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (hne : h ≠ K.zero)
    (x y : R) (hx : ∀ ε, Pos K O ε → O.le x (K.add y ε)) : O.le x y := by
  cases O.le_total x y with
  | inl hxy => exact hxy
  | inr hyx =>
    -- y ≤ x; δ = x − y ≥ 0; if δ = 0 done; else ε = h δ gives x ≤ y + hδ, i.e. δ ≤ hδ, so hδ ≤ 0
    by_cases hd : K.add x (K.neg y) = K.zero
    · have : x = y := by
        have e := congrArg (fun t => K.add t y) hd
        show x = y
        have e' : K.add (K.add x (K.neg y)) y = K.add K.zero y := e
        rw [K.add_assoc, DetChain.add_neg' K y, DetChain.add_zero', K.zero_add] at e'
        exact e'
      rw [this]; exact O.le_refl _
    · have hδ : O.le K.zero (K.add x (K.neg y)) := sub_nonneg_of_le K O y x hyx
      have hε : Pos K O (K.mul h (K.add x (K.neg y))) :=
        ⟨O.mul_nonneg _ _ h0 hδ, fun e => hd (O.pos_mul_eq_zero _ _ h0 hne e)⟩
      have h1 := hx _ hε
      -- x − y ≤ h (x − y)
      have h2 := O.add_le_add _ _ _ _ h1 (O.le_refl (K.neg y))
      rw [K.add_comm y, K.add_assoc, K.add_neg, DetChain.add_zero'] at h2
      -- (h + h)(x−y) = x − y ≤ h(x−y)  ⇒  h(x−y) ≤ 0
      have h3 : O.le (K.add (K.mul h (K.add x (K.neg y))) (K.mul h (K.add x (K.neg y))))
          (K.add K.zero (K.mul h (K.add x (K.neg y)))) := by
        rw [← DetChain.right_distrib, hh, K.one_mul, K.zero_add]; exact h2
      have h4 : O.le (K.mul h (K.add x (K.neg y))) K.zero := by
        have := O.add_le_add _ _ _ _ h3 (O.le_refl (K.neg (K.mul h (K.add x (K.neg y)))))
        rw [K.add_assoc, K.add_neg, DetChain.add_zero', K.add_assoc, K.add_neg, DetChain.add_zero'] at this
        exact this
      exact absurd (O.le_antisymm _ _ h4 hε.1) hε.2

/-- **Limits are unique.** -/
theorem lim_unique (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (hne : h ≠ K.zero)
    (xs : Nat → R) (x y : R)
    (hx : ∀ ε, Pos K O ε → ∃ n, Near K O (xs n) x (K.mul h ε) ∧ Near K O (xs n) y (K.mul h ε)) : x = y := by
  have half_sum : ∀ ε, K.add (K.mul h ε) (K.mul h ε) = ε := fun ε => by
    rw [← DetChain.right_distrib, hh, K.one_mul]
  have key : ∀ a b : R, (∀ ε, Pos K O ε → ∃ n, Near K O (xs n) a (K.mul h ε) ∧ Near K O (xs n) b (K.mul h ε)) →
      O.le a b := fun a b hab => le_of_near_all K O h hh h0 hne a b (fun ε hε => by
        obtain ⟨n, ⟨_, ha2⟩, ⟨hb1, _⟩⟩ := hab ε hε
        -- a ≤ x_n + hε ≤ b + hε + hε = b + ε
        have := O.le_trans _ _ _ ha2 (O.add_le_add _ _ _ _ hb1 (O.le_refl (K.mul h ε)))
        rw [K.add_assoc, half_sum] at this
        exact this)
  exact O.le_antisymm _ _ (key x y hx) (key y x (fun ε hε => by
    obtain ⟨n, hn1, hn2⟩ := hx ε hε
    exact ⟨n, hn2, hn1⟩))

/-- **Symmetries pass to the limit.** -/
theorem sym_limit {Cf : Type} (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (hne : h ≠ K.zero)
    (S : Nat → Cf → R) (Slim : Cf → R) (σ : Cf → Cf)
    (hinv : ∀ n c, S n (σ c) = S n c)
    (hconv : ∀ c ε, Pos K O ε → ∃ N, ∀ n, N ≤ n → Near K O (S n c) (Slim c) (K.mul h ε)) (c : Cf) :
    Slim (σ c) = Slim c :=
  lim_unique K O h hh h0 hne (fun n => S n c) (Slim (σ c)) (Slim c) (fun ε hε => by
    obtain ⟨N1, h1⟩ := hconv (σ c) ε hε
    obtain ⟨N2, h2⟩ := hconv c ε hε
    refine ⟨N1 + N2, ?_, h2 (N1 + N2) (Nat.le_add_left N2 N1)⟩
    have := h1 (N1 + N2) (Nat.le_add_right N1 N2)
    rw [hinv] at this
    exact this)

end SymLimit

#print axioms SymLimit.add_le_add_left'
#print axioms SymLimit.sub_nonneg_of_le
#print axioms SymLimit.le_of_near_all
#print axioms SymLimit.lim_unique
#print axioms SymLimit.sym_limit

ym/WilsonGram.lean

/-
  ym/WilsonGram — T45, cycle 97 (node L3', Lad5 input): the Wilson crossing kernel is a nonnegative sum of Gram kernels.

  For link variables written in real coordinates (SU(2) = unit quaternions, g·h = Re tr(g h†)/… = Σ_a g_a h_a), the
  crossing weight of a reflection-crossing plaquette is e^{β g·h}. Here, in any commutative ring with a positivity
  predicate:
    dot_gram        g·h = Σ_a g_a h_a is a Gram kernel with coefficients 1
    pow_gram        (ker L)^k is a Gram kernel (Gram lists multiply, YMChain.gram_mul), coefficients stay ≥ 0
    series_gram     Σ_{k<N} c_k (g·h)^k with c_k ≥ 0 is a Gram kernel with nonnegative coefficients
    wilson_rp       hence Σ_x Σ_y F(x) F(y) Σ_{k<N} c_k (x·y)^k ≥ 0  (reflection positivity, every truncation)
  With c_k = β^k/k! the truncations converge to e^{β g·h}; positivity passes to the limit (closedness of ≥ 0 in ℝ,
  the only cited step). This replaces the citation L3' (Osterwalder–Seiler) by a proof up to that limit.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Flow2D

namespace WilsonGram

open YMChain LinkIntegral

variable {R : Type} (K : DetChain.CRing R) {X : Type}

def oneG : List (R × (X → R)) := [(K.one, fun _ => K.one)]

def scaleG (c : R) (L : List (R × (X → R))) : List (R × (X → R)) := L.map (fun p => (K.mul c p.1, p.2))

def powG (L : List (R × (X → R))) : Nat → List (R × (X → R))
  | 0 => oneG K
  | k + 1 => prodG K (powG L k) L

def dotG {α : Type} (A : List α) (v : X → α → R) : List (R × (X → R)) := A.map (fun a => (K.one, fun x => v x a))

def seriesG (c : Nat → R) (L : List (R × (X → R))) : Nat → List (R × (X → R))
  | 0 => []
  | N + 1 => seriesG c L N ++ scaleG K (c N) (powG K L N)

def series (c : Nat → R) (t : R) : Nat → R
  | 0 => K.zero
  | N + 1 => K.add (series c t N) (K.mul (c N) (pwR K t N))

theorem ker_app (L1 L2 : List (R × (X → R))) (x y : X) : ker K (L1 ++ L2) x y = K.add (ker K L1 x y) (ker K L2 x y) :=
  sumK_append K _ L1 L2

theorem ker_scale (c : R) (L : List (R × (X → R))) (x y : X) : ker K (scaleG K c L) x y = K.mul c (ker K L x y) := by
  show sumK K _ (L.map _) = _
  rw [sumK_map]
  show _ = K.mul c (sumK K _ L)
  rw [mul_sumK]
  exact sumK_ext K _ _ (fun p => by
    show K.mul (K.mul (K.mul c p.1) (p.2 x)) (p.2 y) = K.mul c (K.mul (K.mul p.1 (p.2 x)) (p.2 y))
    rw [K.mul_assoc c p.1, K.mul_assoc c]) L

theorem ker_one (x y : X) : ker K (oneG K) x y = K.one := by
  show K.add (K.mul (K.mul K.one K.one) K.one) K.zero = K.one
  rw [DetChain.add_zero', K.one_mul, K.one_mul]

theorem ker_pow (L : List (R × (X → R))) (x y : X) : ∀ k, ker K (powG K L k) x y = pwR K (ker K L x y) k
  | 0 => ker_one K x y
  | k + 1 => by
    show ker K (prodG K (powG K L k) L) x y = K.mul (pwR K (ker K L x y) k) (ker K L x y)
    rw [gram_mul, ker_pow L x y k]

theorem ker_dot {α : Type} (A : List α) (v : X → α → R) (x y : X) :
    ker K (dotG K A v) x y = sumK K (fun a => K.mul (v x a) (v y a)) A := by
  show sumK K _ (A.map _) = _
  rw [sumK_map]
  exact sumK_ext K _ _ (fun a => by show K.mul (K.mul K.one (v x a)) (v y a) = _; rw [K.one_mul]) A

theorem ker_series (c : Nat → R) (L : List (R × (X → R))) (x y : X) :
    ∀ N, ker K (seriesG K c L N) x y = series K c (ker K L x y) N
  | 0 => rfl
  | N + 1 => by
    show ker K (seriesG K c L N ++ scaleG K (c N) (powG K L N)) x y = _
    rw [ker_app, ker_scale, ker_pow, ker_series c L x y N]
    rfl

theorem allNN_app (P : Positivity K) : ∀ L1 L2 : List (R × (X → R)), AllNN K P L1 → AllNN K P L2 → AllNN K P (L1 ++ L2)
  | [], _, _, h2 => h2
  | _ :: L1, L2, h1, h2 => ⟨h1.1, allNN_app P L1 L2 h1.2 h2⟩

theorem allNN_scale (P : Positivity K) (c : R) (hc : P.nn c) : ∀ L : List (R × (X → R)), AllNN K P L →
    AllNN K P (scaleG K c L)
  | [], _ => trivial
  | _ :: L, h => ⟨P.nn_mul _ _ hc h.1, allNN_scale P c hc L h.2⟩

theorem allNN_one (P : Positivity K) : AllNN K P (oneG K (X := X)) := by
  refine ⟨?_, trivial⟩
  have := P.nn_sq K.one
  rw [K.one_mul] at this
  exact this

theorem allNN_pow (P : Positivity K) (L : List (R × (X → R))) (hL : AllNN K P L) : ∀ k, AllNN K P (powG K L k)
  | 0 => allNN_one K P
  | k + 1 => gram_mul_nn K P _ _ (allNN_pow P L hL k) hL

theorem allNN_dot {α : Type} (P : Positivity K) (A : List α) (v : X → α → R) : AllNN K P (dotG K A v) := by
  have h1 : P.nn K.one := by have := P.nn_sq K.one; rw [K.one_mul] at this; exact this
  show AllNN K P (A.map _)
  induction A with
  | nil => trivial
  | cons a A ih => exact ⟨h1, ih⟩

theorem allNN_series (P : Positivity K) (c : Nat → R) (hc : ∀ k, P.nn (c k)) (L : List (R × (X → R)))
    (hL : AllNN K P L) : ∀ N, AllNN K P (seriesG K c L N)
  | 0 => trivial
  | N + 1 => allNN_app K P _ _ (allNN_series P c hc L hL N) (allNN_scale K P _ (hc N) _ (allNN_pow K P L hL N))

/-- **Reflection positivity of every truncation of the Wilson crossing kernel.** -/
theorem wilson_rp {α : Type} (P : Positivity K) (A : List α) (v : X → α → R) (c : Nat → R) (hc : ∀ k, P.nn (c k))
    (F : X → R) (xs : List X) (N : Nat) :
    P.nn (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y))
      (series K c (sumK K (fun a => K.mul (v x a) (v y a)) A) N)) xs) xs) := by
  have h := rp_nonneg K P F xs (seriesG K c (dotG K A v) N) (allNN_series K P c hc _ (allNN_dot K P A v) N)
  have e : ∀ x y, ker K (seriesG K c (dotG K A v) N) x y = series K c (sumK K (fun a => K.mul (v x a) (v y a)) A) N :=
    fun x y => by rw [ker_series, ker_dot]
  rw [sumK_ext K _ _ (fun x => sumK_ext K _ _ (fun y => by rw [e x y]) xs)] at h
  exact h

end WilsonGram

#print axioms WilsonGram.ker_series
#print axioms WilsonGram.allNN_series
#print axioms WilsonGram.wilson_rp

ym/QuatTrace.lean

/-
  ym/QuatTrace — T45, cycle 98 (node L3' for SU(2)): Re tr(g h†) = 2 g·h in quaternion coordinates.

  An SU(2) matrix is g = [[a + i b, c + i d], [−c + i d, a − i b]] with (a, b, c, d) a unit quaternion. Complex numbers
  are pairs over any commutative ring; re((x, y)(u, v)) = x u − y v.
    entry11, entry22   real parts of the diagonal entries of g h†
    retr_dot           Re tr(g h†) = 2 (a a' + b b' + c c' + d d')
  For unitary h, h⁻¹ = h†, so the crossing weight e^{(β/2) Re tr(g h⁻¹)} = e^{β g·h} is the power series of a Gram kernel
  (WilsonGram.wilson_rp with the four quaternion coordinates): reflection positivity of the SU(2) Wilson action, every
  truncation, with no citation besides N → ∞.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.WilsonGram

namespace QuatTrace

open YMChain

variable {R : Type} (K : DetChain.CRing R)

def cre (x y u v : R) : R := K.add (K.mul x u) (K.neg (K.mul y v))

theorem mul_neg_r (x y : R) : K.mul x (K.neg y) = K.neg (K.mul x y) := by
  rw [K.mul_comm, Signed.neg_mul' K, K.mul_comm]

/-- real part of (g h†)₁₁ -/
def entry11 (a b c d a' b' c' d' : R) : R := K.add (cre K a b a' (K.neg b')) (cre K c d c' (K.neg d'))

/-- real part of (g h†)₂₂ -/
def entry22 (a b c d a' b' c' d' : R) : R := K.add (cre K (K.neg c) d (K.neg c') (K.neg d')) (cre K a (K.neg b) a' b')

def dot (a b c d a' b' c' d' : R) : R := K.add (K.add (K.mul a a') (K.mul b b')) (K.add (K.mul c c') (K.mul d d'))

theorem e11 (a b c d a' b' c' d' : R) : entry11 K a b c d a' b' c' d' = dot K a b c d a' b' c' d' := by
  unfold entry11 cre dot
  rw [mul_neg_r, mul_neg_r, Signed.neg_neg' K, Signed.neg_neg' K]

theorem e22 (a b c d a' b' c' d' : R) : entry22 K a b c d a' b' c' d' = dot K a b c d a' b' c' d' := by
  unfold entry22 cre dot
  rw [CenterExp.neg_mul_neg, mul_neg_r, Signed.neg_neg' K, Signed.neg_mul', Signed.neg_neg' K, K.add_comm]

/-- **Re tr(g h†) = 2 g·h.** -/
theorem retr_dot (a b c d a' b' c' d' : R) :
    K.add (entry11 K a b c d a' b' c' d') (entry22 K a b c d a' b' c' d')
      = K.add (dot K a b c d a' b' c' d') (dot K a b c d a' b' c' d') := by
  rw [e11, e22]

end QuatTrace

#print axioms QuatTrace.retr_dot

ym/RPLimit.lean

/-
  ym/RPLimit — T45, cycle 99 (node L3', last step): reflection positivity survives uniform kernel approximation.

  If K∞ = K_N + D_N with |D_N(x, y)| ≤ e_N and |F(x)| ≤ B(x), then
    rp_approx      Σ_x Σ_y F(x) F(y) K∞(x, y) ≥ Σ_x Σ_y F(x) F(y) K_N(x, y) − e_N (Σ_x B(x))²
  so with the truncations K_N of WilsonGram (each giving a nonnegative form) and e_N → 0 (the exponential tail of a
  bounded kernel), the Wilson form is ≥ −ε for every ε > 0, i.e. ≥ 0. Only the convergence of the exponential series
  (e_N → 0) remains cited.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.QuatTrace

namespace RPLimit

open YMChain Signed

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) {X : Type}

/-- **Approximate reflection positivity.** -/
theorem rp_approx (h1 : O.le K.zero K.one) (F B : X → R) (xs : List X) (Kinf KN D : X → X → R) (e : R)
    (he : O.le K.zero e) (hB : ∀ x, O.le K.zero (B x)) (hF : ∀ x, O.le (K.neg (B x)) (F x) ∧ O.le (F x) (B x))
    (hK : ∀ x y, Kinf x y = K.add (KN x y) (D x y)) (hD : ∀ x y, O.le (K.neg e) (D x y) ∧ O.le (D x y) e)
    (hN : O.le K.zero (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (KN x y)) xs) xs)) :
    O.le (K.neg (sumK K (fun x => sumK K (fun y => K.mul (K.mul (B x) (B y)) e) xs) xs))
      (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (Kinf x y)) xs) xs) := by
  have split : sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (Kinf x y)) xs) xs
      = K.add (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (KN x y)) xs) xs)
          (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (D x y)) xs) xs) := by
    rw [← sumK_add]
    refine sumK_ext K _ _ (fun x => ?_) xs
    rw [← sumK_add]
    exact sumK_ext K _ _ (fun y => by rw [hK, K.distrib]) xs
  have bd := bnd_sum K O _ _ xs (fun x _ => bnd_sum K O _ _ xs (fun y _ =>
    bnd_mul K O _ _ _ _ (O.mul_nonneg _ _ (hB x) (hB y)) he (bnd_mul K O _ _ _ _ (hB x) (hB y) (hF x) (hF y)) (hD x y)))
  rw [split]
  have := O.add_le_add _ _ _ _ hN bd.1
  rw [K.zero_add] at this
  exact this

end RPLimit

#print axioms RPLimit.rp_approx

ym/ExpTail.lean

/-
  ym/ExpTail — T45, cycle 100 (node L3', the convergence step): the exponential series is uniformly Cauchy.

  Coefficients with c_{k+1}(k + 1) = β c_k (c_k = β^k/k!), |t| ≤ T, a_k = c_k T^k. If 2βT ≤ N + 1 then for k ≥ N
    halve          2 a_{k+1} ≤ a_k
    halve_pow      2^j a_{N+j} ≤ a_N
    tail_geom      Σ_{j<m} a_{N+j} + 2 a_{N+m} ≤ 2 a_N
    exp_cauchy     |Σ_{k<N+m} c_k t^k − Σ_{k<N} c_k t^k| ≤ 2 a_N      (every m, uniformly in |t| ≤ T)
  and a_{N+j} ≤ a_N/2^j → 0. So the truncations of e^{βt} are uniformly Cauchy on |t| ≤ T with an explicit rate; the
  limit exists by completeness of ℝ (the only cited input), and RPLimit.rp_approx then gives RP of the Wilson action.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.RPLimit

namespace ExpTail

open YMChain LinkIntegral Signed Geom WilsonGram Indep

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

def two : R := K.add K.one K.one

theorem natR_pos (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (k : Nat) :
    O.le K.zero (natR K (k + 1)) ∧ natR K (k + 1) ≠ K.zero := by
  refine ⟨natR_nn K O h1 _, fun h => h10 ?_⟩
  have m := natR_mono K O h1 1 (k + 1) (Nat.succ_le_succ (Nat.zero_le k))
  rw [h] at m
  have e1 : natR K 1 = K.one := by show K.add K.zero K.one = K.one; rw [K.zero_add]
  rw [e1] at m
  exact O.le_antisymm _ _ m h1

/-- **Ratio test step.** -/
theorem halve (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (c : Nat → R) (β T : R)
    (hc : ∀ k, K.mul (c (k + 1)) (natR K (k + 1)) = K.mul β (c k)) (hc0 : ∀ k, O.le K.zero (c k))
    (hT : O.le K.zero T) (N : Nat) (hN : O.le (K.mul (two K) (K.mul β T)) (natR K (N + 1)))
    (k : Nat) (hk : N ≤ k) :
    O.le (K.mul (two K) (K.mul (c (k + 1)) (pwR K T (k + 1)))) (K.mul (c k) (pwR K T k)) := by
  have hp := natR_pos K O h1 h10 k
  apply O.pos_cancel _ _ _ hp.1 hp.2
  -- LHS · (k+1) = a_k (2βT),  RHS · (k+1) = a_k (k+1)
  have e : K.mul (K.mul (two K) (K.mul (c (k + 1)) (pwR K T (k + 1)))) (natR K (k + 1))
      = K.mul (K.mul (c k) (pwR K T k)) (K.mul (two K) (K.mul β T)) := by
    show K.mul (K.mul (two K) (K.mul (c (k + 1)) (K.mul (pwR K T k) T))) (natR K (k + 1)) = _
    have l1 : K.mul (K.mul (two K) (K.mul (c (k + 1)) (K.mul (pwR K T k) T))) (natR K (k + 1))
        = K.mul (two K) (K.mul β (K.mul (K.mul (c k) (pwR K T k)) T)) := by
      rw [K.mul_assoc, K.mul_assoc, K.mul_comm (K.mul (pwR K T k) T) (natR K (k + 1)), ← K.mul_assoc (c (k + 1)), hc,
        K.mul_assoc β, ← K.mul_assoc (c k)]
    have r1 : K.mul (K.mul (c k) (pwR K T k)) (K.mul (two K) (K.mul β T))
        = K.mul (two K) (K.mul β (K.mul (K.mul (c k) (pwR K T k)) T)) := by
      rw [K.mul_comm (K.mul (c k) (pwR K T k)), K.mul_assoc, K.mul_assoc β, K.mul_comm T]
    rw [l1, r1]
  rw [e]
  have ha : O.le K.zero (K.mul (c k) (pwR K T k)) := O.mul_nonneg _ _ (hc0 k) (AreaLower.pw_nn K O T hT h1 k)
  have m := natR_mono K O h1 (N + 1) (k + 1) (Nat.succ_le_succ hk)
  have := O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ hN m) ha
  rw [K.mul_comm _ (K.mul (c k) _), K.mul_comm (natR K (k + 1))] at this
  exact this

def aa (c : Nat → R) (T : R) (k : Nat) : R := K.mul (c k) (pwR K T k)

def tsum (c : Nat → R) (T : R) (N : Nat) : Nat → R
  | 0 => K.zero
  | m + 1 => K.add (tsum c T N m) (aa K c T (N + m))

def dsum (c : Nat → R) (t : R) (N : Nat) : Nat → R
  | 0 => K.zero
  | m + 1 => K.add (dsum c t N m) (K.mul (c (N + m)) (pwR K t (N + m)))

theorem two_mul (x : R) : K.mul (two K) x = K.add x x := by
  show K.mul (K.add K.one K.one) x = _
  rw [DetChain.right_distrib, K.one_mul]

/-- **Geometric tail.** -/
theorem tail_geom (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (c : Nat → R) (β T : R)
    (hc : ∀ k, K.mul (c (k + 1)) (natR K (k + 1)) = K.mul β (c k)) (hc0 : ∀ k, O.le K.zero (c k))
    (hT : O.le K.zero T) (N : Nat) (hN : O.le (K.mul (two K) (K.mul β T)) (natR K (N + 1))) :
    ∀ m, O.le (K.add (tsum K c T N m) (K.mul (two K) (aa K c T (N + m)))) (K.mul (two K) (aa K c T N))
  | 0 => by show O.le (K.add K.zero _) _; rw [K.zero_add]; exact O.le_refl _
  | m + 1 => by
    have ih := tail_geom h1 h10 c β T hc hc0 hT N hN m
    have hv := halve K O h1 h10 c β T hc hc0 hT N hN (N + m) (Nat.le_add_right N m)
    show O.le (K.add (K.add (tsum K c T N m) (aa K c T (N + m))) (K.mul (two K) (aa K c T (N + m + 1)))) _
    have s := O.add_le_add _ _ _ _ (O.le_refl (K.add (tsum K c T N m) (aa K c T (N + m)))) hv
    refine O.le_trans _ _ _ s ?_
    rw [K.add_assoc]
    show O.le (K.add (tsum K c T N m) (K.add (aa K c T (N + m)) (aa K c T (N + m)))) _
    rw [← two_mul K (aa K c T (N + m))]
    exact ih

theorem pw_bnd (h1 : O.le K.zero K.one) (t T : R) (hT : O.le K.zero T) (ht : O.le (K.neg T) t ∧ O.le t T) :
    ∀ k, O.le (K.neg (pwR K T k)) (pwR K t k) ∧ O.le (pwR K t k) (pwR K T k)
  | 0 => ⟨O.le_trans _ _ _ (neg_le_neg K O _ _ h1) (by
      have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
      rw [e]; exact h1), O.le_refl _⟩
  | k + 1 => bnd_mul K O _ _ _ _ (AreaLower.pw_nn K O T hT h1 k) hT (pw_bnd h1 t T hT ht k) ht

theorem dsum_bnd (h1 : O.le K.zero K.one) (c : Nat → R) (hc0 : ∀ k, O.le K.zero (c k)) (t T : R)
    (hT : O.le K.zero T) (ht : O.le (K.neg T) t ∧ O.le t T) (N : Nat) :
    ∀ m, O.le (K.neg (tsum K c T N m)) (dsum K c t N m) ∧ O.le (dsum K c t N m) (tsum K c T N m)
  | 0 => by
    have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
    show O.le (K.neg K.zero) K.zero ∧ O.le K.zero K.zero
    rw [e]; exact ⟨O.le_refl _, O.le_refl _⟩
  | m + 1 => by
    have ih := dsum_bnd h1 c hc0 t T hT ht N m
    have hck : O.le (K.neg (c (N + m))) (c (N + m)) ∧ O.le (c (N + m)) (c (N + m)) := by
      have e : K.neg K.zero = K.zero := by have := K.add_neg K.zero; rw [K.zero_add] at this; exact this
      have := neg_le_neg K O _ _ (hc0 (N + m)); rw [e] at this
      exact ⟨O.le_trans _ _ _ this (hc0 _), O.le_refl _⟩
    have b := bnd_mul K O _ _ _ _ (hc0 _) (AreaLower.pw_nn K O T hT h1 _) hck (pw_bnd K O h1 t T hT ht (N + m))
    show O.le (K.neg (K.add (tsum K c T N m) (aa K c T (N + m)))) (K.add (dsum K c t N m) _) ∧
      O.le (K.add (dsum K c t N m) _) (K.add (tsum K c T N m) (aa K c T (N + m)))
    rw [neg_add' K]
    exact ⟨O.add_le_add _ _ _ _ ih.1 b.1, O.add_le_add _ _ _ _ ih.2 b.2⟩

theorem series_split (c : Nat → R) (t : R) (N : Nat) :
    ∀ m, series K c t (N + m) = K.add (series K c t N) (dsum K c t N m)
  | 0 => (DetChain.add_zero' K _).symm
  | m + 1 => by
    show K.add (series K c t (N + m)) (K.mul (c (N + m)) (pwR K t (N + m)))
      = K.add (series K c t N) (K.add (dsum K c t N m) (K.mul (c (N + m)) (pwR K t (N + m))))
    rw [series_split c t N m, K.add_assoc]

/-- **The exponential series is uniformly Cauchy:** |S_{N+m}(t) − S_N(t)| ≤ 2 a_N for |t| ≤ T. -/
theorem exp_cauchy (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (c : Nat → R) (β T t : R)
    (hc : ∀ k, K.mul (c (k + 1)) (natR K (k + 1)) = K.mul β (c k)) (hc0 : ∀ k, O.le K.zero (c k))
    (hT : O.le K.zero T) (ht : O.le (K.neg T) t ∧ O.le t T)
    (N : Nat) (hN : O.le (K.mul (two K) (K.mul β T)) (natR K (N + 1))) (m : Nat) :
    O.le (K.neg (K.mul (two K) (aa K c T N))) (dsum K c t N m) ∧ O.le (dsum K c t N m) (K.mul (two K) (aa K c T N)) ∧
      series K c t (N + m) = K.add (series K c t N) (dsum K c t N m) := by
  have g := tail_geom K O h1 h10 c β T hc hc0 hT N hN m
  have hpos : O.le K.zero (K.mul (two K) (aa K c T (N + m))) := by
    rw [two_mul]
    have a0 : O.le K.zero (aa K c T (N + m)) := O.mul_nonneg _ _ (hc0 _) (AreaLower.pw_nn K O T hT h1 _)
    have := O.add_le_add _ _ _ _ a0 a0; rw [K.zero_add] at this; exact this
  have t1 : O.le (tsum K c T N m) (K.mul (two K) (aa K c T N)) := by
    have := O.add_le_add _ _ _ _ (O.le_refl (tsum K c T N m)) hpos
    rw [DetChain.add_zero'] at this
    exact O.le_trans _ _ _ this g
  have d := dsum_bnd K O h1 c hc0 t T hT ht N m
  exact ⟨O.le_trans _ _ _ (neg_le_neg K O _ _ t1) d.1, O.le_trans _ _ _ d.2 t1, series_split K c t N m⟩

end ExpTail

#print axioms ExpTail.halve
#print axioms ExpTail.tail_geom
#print axioms ExpTail.exp_cauchy

ym/GapLimit.lean

/-
  ym/GapLimit — T45, cycle 104 (Lad5, OS clustering in infinite volume): two-sided bounds pass to limits.

  With a half h (h + h = 1) and decidable equality on the ring (SymLimit conventions):
    bound_limit    if −B ≤ x_n ≤ B for all n and x_n comes ε-near x for every ε > 0, then −B ≤ x ≤ B
    gap_limit      the finite-volume clustering bound |⟨U_pU_q⟩_Λ − ⟨U_p⟩_Λ⟨U_q⟩_Λ| ≤ C u^R (GapZd, uniform in Λ) holds
                   for the infinite-volume limit (Lad5Cauchy gives the convergence of each expectation)
  So the infinite-volume 4D strong-coupling state has exponential clustering (mass gap ≥ −log u per lattice unit) — the
  OS clustering axiom — in addition to reflection positivity (RPLimit/OSLimit) and lattice symmetries (SymLimit).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.QuatUnit
import ym.SymLimit

namespace GapLimit

open YMChain SymLimit

variable {R : Type} [DecidableEq R] (K : DetChain.CRing R) (O : ORing K)

/-- **Two-sided bounds survive limits.** -/
theorem bound_limit (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (hne : h ≠ K.zero)
    (xs : Nat → R) (x B : R) (hb : ∀ n, O.le (K.neg B) (xs n) ∧ O.le (xs n) B)
    (hx : ∀ ε, SymLimit.Pos K O ε → ∃ n, Near K O (xs n) x ε) :
    O.le (K.neg B) x ∧ O.le x B := by
  constructor
  · -- −B ≤ x_n ≤ x + ε for every ε
    apply le_of_near_all K O h hh h0 hne
    intro ε hε
    obtain ⟨n, hn⟩ := hx ε hε
    exact O.le_trans _ _ _ (hb n).1 hn.1
  · apply le_of_near_all K O h hh h0 hne
    intro ε hε
    obtain ⟨n, hn⟩ := hx ε hε
    exact O.le_trans _ _ _ hn.2 (O.add_le_add _ _ _ _ (hb n).2 (O.le_refl ε))

/-- **Clustering in infinite volume.** If every finite-volume covariance obeys the uniform clustering bound and the
  covariances converge, the limit covariance obeys it too. -/
theorem gap_limit (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (hne : h ≠ K.zero)
    (cov : Nat → R) (covInf C u : R) (Rr : Nat)
    (hb : ∀ n, O.le (K.neg (K.mul C (LinkIntegral.pwR K u Rr))) (cov n) ∧ O.le (cov n) (K.mul C (LinkIntegral.pwR K u Rr)))
    (hx : ∀ ε, SymLimit.Pos K O ε → ∃ n, Near K O (cov n) covInf ε) :
    O.le (K.neg (K.mul C (LinkIntegral.pwR K u Rr))) covInf ∧ O.le covInf (K.mul C (LinkIntegral.pwR K u Rr)) :=
  bound_limit K O h hh h0 hne cov covInf _ hb hx

end GapLimit

#print axioms GapLimit.bound_limit
#print axioms GapLimit.gap_limit

ym/SpecGap.lean

/-
  ym/SpecGap — T45, cycle 105 (Lad5, OS reconstruction step): exponential clustering forces a spectral gap.

  In an Archimedean ordered ring with decidable equality:
    lower_pow      u ≥ 0, δ ≥ 0:  u^n (u + n δ) ≤ (u + δ)^{n+1}
    rate_le        if λ^n ≤ C u^n for all n (u > 0), then λ ≤ u
    eig_gap        if v is an eigenvector of the transfer matrix with eigenvalue λ ≥ 0, ⟨v, v⟩ = q > 0 and the
                   clustering bound ⟨v, T^n v⟩ ≤ C u^n q holds (v ⊥ vacuum, from GapZd/GapLimit through reflection
                   positivity), then λ ≤ u: every excited state has energy −log λ ≥ −log u > 0 (the mass gap).
  This is the step "clustering ⇒ gap in the reconstructed Hamiltonian" of Osterwalder–Schrader, for the lattice
  transfer matrix.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.GapLimit

namespace SpecGap

open YMChain LinkIntegral Signed Indep Geom

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- `u^n (u + n δ) ≤ (u + δ)^{n+1}` -/
theorem lower_pow (h1 : O.le K.zero K.one) (u δ : R) (hu : O.le K.zero u) (hδ : O.le K.zero δ) :
    ∀ n, O.le (K.mul (pwR K u n) (K.add u (K.mul (natR K n) δ))) (pwR K (K.add u δ) (n + 1))
  | 0 => by
    show O.le (K.mul K.one (K.add u (K.mul K.zero δ))) (K.mul K.one (K.add u δ))
    rw [K.mul_comm K.zero δ, DetChain.mul_zero, DetChain.add_zero', K.one_mul, K.one_mul]
    have := O.add_le_add _ _ _ _ (O.le_refl u) hδ
    rw [DetChain.add_zero'] at this; exact this
  | n + 1 => by
    have ih := lower_pow h1 u δ hu hδ n
    have hud : O.le K.zero (K.add u δ) := by
      have := O.add_le_add _ _ _ _ hu hδ; rw [K.zero_add] at this; exact this
    -- (u+δ)^{n+2} ≥ u^n (u + nδ)(u + δ) ≥ u^{n+1}(u + (n+1)δ)
    have s1 := O.mul_le_mul_right _ _ _ ih hud
    refine O.le_trans _ _ _ ?_ s1
    -- u^{n+1}(u + (n+1)δ) ≤ u^n (u + nδ)(u + δ)
    have pn := AreaLower.pw_nn K O u hu h1 n
    have e1 : K.mul (pwR K u (n + 1)) (K.add u (K.mul (natR K (n + 1)) δ))
        = K.mul (pwR K u n) (K.add (K.mul u u) (K.add (K.mul u (K.mul (natR K n) δ)) (K.mul u δ))) := by
      show K.mul (K.mul (pwR K u n) u) (K.add u (K.mul (K.add (natR K n) K.one) δ)) = _
      rw [DetChain.right_distrib K (natR K n) K.one δ, K.one_mul, K.mul_assoc (pwR K u n) u,
        K.distrib u u, K.distrib u (K.mul (natR K n) δ) δ]
    have e2 : K.mul (K.mul (pwR K u n) (K.add u (K.mul (natR K n) δ))) (K.add u δ)
        = K.mul (pwR K u n) (K.add (K.add (K.mul u u) (K.add (K.mul u (K.mul (natR K n) δ)) (K.mul u δ)))
            (K.mul (K.mul (natR K n) δ) δ)) := by
      rw [K.mul_assoc (pwR K u n), DetChain.right_distrib K u (K.mul (natR K n) δ) (K.add u δ),
        K.distrib u u δ, K.distrib (K.mul (natR K n) δ) u δ, K.mul_comm (K.mul (natR K n) δ) u]
      rw [← K.add_assoc (K.add (K.mul u u) (K.mul u δ)) (K.mul u (K.mul (natR K n) δ)),
        K.add_assoc (K.mul u u) (K.mul u δ), K.add_comm (K.mul u δ) (K.mul u (K.mul (natR K n) δ))]
    rw [e1, e2]
    have nn : O.le K.zero (K.mul (K.mul (natR K n) δ) δ) :=
      O.mul_nonneg _ _ (O.mul_nonneg _ _ (natR_nn K O h1 n) hδ) hδ
    have a := O.add_le_add _ _ _ _ (O.le_refl (K.add (K.mul u u) (K.add (K.mul u (K.mul (natR K n) δ)) (K.mul u δ)))) nn
    rw [DetChain.add_zero'] at a
    have := O.mul_le_mul_right _ _ _ a pn
    rw [K.mul_comm _ (pwR K u n), K.mul_comm _ (pwR K u n)] at this
    exact this

theorem pw_ne (h1 : O.le K.zero K.one) (u : R) (hu : O.le K.zero u) (hu0 : u ≠ K.zero) :
    ∀ n, pwR K u n ≠ K.zero
  | 0 => fun h => hu0 (by
      have e : (K.one : R) = K.zero := h
      rw [← K.one_mul u, e, DetChain.zero_mul'])
  | n + 1 => fun h => hu0 (O.pos_mul_eq_zero _ _ (AreaLower.pw_nn K O u hu h1 n) (pw_ne h1 u hu hu0 n) h)

/-- **A uniform exponential bound fixes the rate.** -/
theorem rate_le [DecidableEq R] (h1 : O.le K.zero K.one)
    (harch : ∀ δ M : R, O.le K.zero δ → δ ≠ K.zero → ∃ n, ¬ O.le (K.mul (natR K n) δ) M)
    (lam u C : R) (hl : O.le K.zero lam) (hu : O.le K.zero u) (hu0 : u ≠ K.zero)
    (hb : ∀ n, O.le (pwR K lam n) (K.mul C (pwR K u n))) : O.le lam u := by
  cases O.le_total lam u with
  | inl h => exact h
  | inr h =>
    let δ := K.add lam (K.neg u)
    have hδ : O.le K.zero δ := by
      have := O.add_le_add _ _ _ _ h (O.le_refl (K.neg u)); rw [K.add_neg] at this; exact this
    have hlam : lam = K.add u δ := by
      show lam = K.add u (K.add lam (K.neg u))
      rw [K.add_comm lam, ← K.add_assoc, K.add_neg, K.zero_add]
    by_cases hz : δ = K.zero
    · rw [hlam, hz, DetChain.add_zero']; exact O.le_refl _
    · exfalso
      -- u^n (u + nδ) ≤ λ^{n+1} ≤ C u^{n+1} = u^n (C u), cancel u^n > 0: u + nδ ≤ C u for every n
      have bound : ∀ n, O.le (K.mul (natR K n) δ) (K.mul C u) := by
        intro n
        have lp := lower_pow K O h1 u δ hu hδ n
        rw [← hlam] at lp
        have c := O.le_trans _ _ _ lp (hb (n + 1))
        have e : K.mul C (pwR K u (n + 1)) = K.mul (pwR K u n) (K.mul C u) := by
          show K.mul C (K.mul (pwR K u n) u) = _
          rw [← K.mul_assoc, K.mul_comm C, K.mul_assoc]
        rw [e] at c
        have pn := AreaLower.pw_nn K O u hu h1 n
        have pnz : pwR K u n ≠ K.zero := pw_ne K O h1 u hu hu0 n
        rw [K.mul_comm (pwR K u n), K.mul_comm (pwR K u n)] at c
        have c' := O.pos_cancel _ _ _ pn pnz c
        have a := O.add_le_add _ _ _ _ (O.le_refl (K.mul (natR K n) δ)) hu
        rw [DetChain.add_zero', K.add_comm] at a
        exact O.le_trans _ _ _ a c'
      obtain ⟨n, hn⟩ := harch δ (K.mul C u) hδ hz
      exact hn (bound n)

/-- **Spectral gap from clustering.** -/
theorem eig_gap [DecidableEq R] (h1 : O.le K.zero K.one)
    (harch : ∀ δ M : R, O.le K.zero δ → δ ≠ K.zero → ∃ n, ¬ O.le (K.mul (natR K n) δ) M)
    (lam u C q : R) (hl : O.le K.zero lam) (hu : O.le K.zero u) (hu0 : u ≠ K.zero)
    (hq : O.le K.zero q) (hq0 : q ≠ K.zero)
    (hclust : ∀ n, O.le (K.mul (pwR K lam n) q) (K.mul (K.mul C (pwR K u n)) q)) : O.le lam u :=
  rate_le K O h1 harch lam u C hl hu hu0 (fun n => O.pos_cancel _ _ _ hq hq0 (hclust n))

end SpecGap

#print axioms SpecGap.lower_pow
#print axioms SpecGap.rate_le
#print axioms SpecGap.eig_gap

ym/SU2Strong4D.lean

/-
  ym/SU2Strong4D — T45, S5: 4D SU(2) at strong coupling — explicit β₀, volume-uniform clustering (mass gap).

  The abstract cluster theorem (GapZd.gap_Zd, from the L2 machinery) needs, for d = 4 (D_g = 32):
      hsmall   (2ε + ε²)(κ²)^{33} ≤ u x,        hxy   x (1 + y)^{32} ≤ y,
  where the plaquette weights are c₀ + f with m ≤ c₀ + f ≤ 1, |f| ≤ ε, κ m = 1. For the Wilson action the weight
  bounds are WeightBound/QuatUnit (ε = 4β, m = 1 − 4β; the character coefficients of S3 give the same scale c_n/c_0 =
  O((β/2)^n/n!)). Here, with u = ½, x = 2^{−32}, y = 1:
    small_4d       ε · 2^{101} ≤ 1, κ m = 1, m ≥ ½  ⇒  hsmall and hxy hold
    beta_consts    β · 2^{103} ≤ 1  ⇒  ε = 4β and m = 1 − 4β satisfy ε · 2^{101} ≤ 1 and m ≥ ½
    gap_4d         for β ≤ β₀ = 2^{−103}: on every finite set of plaquettes of Z⁴ (any volume, any boundary),
                   |⟨U_pU_q⟩ − ⟨U_p⟩⟨U_q⟩| ≤ ½ (2K₁)² (κ²·2)^ŝ · 2^{−R}   whenever dist_∞(p, q) > R + 1
    area_4d        for the same β and any m_A below which polymers vanish (center charge: m_A = RT for an R × T loop),
                   |⟨W⟩| ≤ K_χ (2κ)^s 2^{−m_A}, uniformly in the volume (the area law)
  — exponential clustering with mass ≥ log 2 per lattice unit, uniformly in the volume: the strong-coupling mass gap
  of 4D SU(2) lattice gauge theory (Osterwalder–Seiler), machine-checked with explicit rational constants, given a Haar
  structure H on the group (for SU(2): its class-function part is S1/S2).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.SU2Wilson2D
import ym.GapZd

namespace SU2Strong4D

open YMChain LinkIntegral Signed

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

def two : R := K.add K.one K.one

theorem pw_mono (h1 : O.le K.zero K.one) (a b : R) (ha : O.le K.zero a) (hab : O.le a b) :
    ∀ n, O.le (pwR K a n) (pwR K b n)
  | 0 => O.le_refl _
  | n + 1 => by
    have ih := pw_mono h1 a b ha hab n
    have hb : O.le K.zero b := O.le_trans _ _ _ ha hab
    have s1 := O.mul_le_mul_right _ _ _ ih ha
    have s2 := O.mul_le_mul_right _ _ _ hab (AreaLower.pw_nn K O b hb h1 n)
    rw [K.mul_comm a, K.mul_comm b] at s2
    exact O.le_trans _ _ _ s1 s2

theorem pw_one : ∀ n, pwR K K.one n = K.one
  | 0 => rfl
  | n + 1 => by show K.mul (pwR K K.one n) K.one = K.one; rw [pw_one n, K.one_mul]

theorem pw_ge_one (h1 : O.le K.zero K.one) (a : R) (ha : O.le K.one a) : ∀ n, O.le K.one (pwR K a n)
  | 0 => O.le_refl _
  | n + 1 => by
    have ih := pw_ge_one h1 a ha n
    have s := O.mul_le_mul_right _ _ _ ih (O.le_trans _ _ _ h1 ha)
    rw [K.one_mul] at s
    exact O.le_trans _ _ _ ha s

/-- **The smallness hypotheses for d = 4 with u = ½, x = 2^{−32}, y = 1.** -/
theorem small_4d (h1 : O.le K.zero K.one) (h ε κ m : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (he : O.le K.zero ε) (hε : O.le (K.mul ε (pwR K (two K) 101)) K.one)
    (hκ0 : O.le K.zero κ) (hκm : K.mul κ m = K.one) (hm : O.le h m) :
    O.le (K.mul (K.add (K.add ε ε) (K.mul ε ε)) (pwR K (K.mul κ κ) (4 * (2 * 4) + 1))) (K.mul h (pwR K h 32)) ∧
    O.le (K.mul (pwR K h 32) (pwR K (K.add K.one K.one) (4 * (2 * 4)))) K.one := by
  have two_h : K.mul (two K) h = K.one := by show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
  have t0 : O.le K.zero (two K) := by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
  have t1 : O.le K.one (two K) := by have := O.add_le_add _ _ _ _ (O.le_refl K.one) h1; rw [DetChain.add_zero'] at this; exact this
  -- κ ≤ 2
  have kh : O.le (K.mul κ h) K.one := by
    have := O.mul_le_mul_right _ _ _ hm hκ0; rw [K.mul_comm h, K.mul_comm m, hκm] at this; exact this
  have k2 : O.le κ (two K) := by
    have e : κ = K.add (K.mul κ h) (K.mul κ h) := by rw [← K.distrib, hh, K.mul_comm, K.one_mul]
    rw [e]; exact O.add_le_add _ _ _ _ kh kh
  have kk : O.le (K.mul κ κ) (K.mul (two K) (two K)) := by
    have a := O.mul_le_mul_right _ _ _ k2 hκ0
    have b := O.mul_le_mul_right _ _ _ k2 t0
    rw [K.mul_comm (two K) κ] at a
    exact O.le_trans _ _ _ a b
  have p1 := pw_mono K O h1 _ _ (O.mul_nonneg _ _ hκ0 hκ0) kk (4 * (2 * 4) + 1)
  -- ε ≤ 1, 2ε + ε² ≤ 4ε
  have e1 : O.le ε K.one := by
    have g := pw_ge_one K O h1 _ t1 101
    have := O.mul_le_mul_right _ _ _ g he
    rw [K.one_mul, K.mul_comm] at this
    exact O.le_trans _ _ _ this hε
  have p2 : O.le (K.add (K.add ε ε) (K.mul ε ε)) (K.mul (K.mul (two K) (two K)) ε) := by
    have ee : O.le (K.mul ε ε) ε := by
      have := O.mul_le_mul_right _ _ _ e1 he; rw [K.one_mul] at this; exact this
    have := O.add_le_add _ _ _ _ (O.le_refl (K.add ε ε)) (O.le_trans _ _ _ ee
      (by have := O.add_le_add _ _ _ _ (O.le_refl ε) he; rw [DetChain.add_zero'] at this; exact this))
    have eqx : K.add (K.add ε ε) (K.add ε ε) = K.mul (K.mul (two K) (two K)) ε := by
      show _ = K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) ε
      lib_ring
    rw [eqx] at this; exact this
  have pnn : O.le K.zero (pwR K (K.mul κ κ) (4 * (2 * 4) + 1)) := AreaLower.pw_nn K O _ (O.mul_nonneg _ _ hκ0 hκ0) h1 _
  have q1 := O.mul_le_mul_right _ _ _ p2 pnn
  have q2 := O.mul_le_mul_right _ _ _ p1 (O.mul_nonneg _ _ (O.mul_nonneg _ _ t0 t0) he)
  rw [K.mul_comm (pwR K (K.mul κ κ) _), K.mul_comm (pwR K (K.mul (two K) (two K)) _)] at q2
  have q := O.le_trans _ _ _ q1 q2
  -- (4ε)(4)^{33} = ε 2^{68} and h·h^{32}·2^{101}·ε ... compare via ε 2^{101} ≤ 1
  have hp : O.le K.zero (K.mul h (pwR K h 32)) := O.mul_nonneg _ _ h0 (AreaLower.pw_nn K O h h0 h1 32)
  have r := O.mul_le_mul_right _ _ _ hε hp
  rw [K.one_mul] at r
  have eq : K.mul (K.mul (K.mul (two K) (two K)) ε) (pwR K (K.mul (two K) (two K)) (4 * (2 * 4) + 1))
      = K.mul (K.mul ε (pwR K (two K) 101)) (K.mul h (pwR K h 32)) := by
    have a1 : pwR K (K.mul (two K) (two K)) (4 * (2 * 4) + 1) = K.mul (pwR K (two K) 33) (pwR K (two K) 33) :=
      GKS2.pwR_mul K _ _ 33
    have a2 : pwR K (two K) 101 = K.mul (K.mul (K.mul (pwR K (two K) 33) (pwR K (two K) 33)) (K.mul (two K) (two K)))
        (pwR K (two K) 33) := by
      rw [← GKS.pwR_add, show K.mul (two K) (two K) = pwR K (two K) 2 by
        show _ = K.mul (K.mul K.one (two K)) (two K); rw [K.one_mul], ← GKS.pwR_add, ← GKS.pwR_add]
    have a3 : K.mul h (pwR K h 32) = pwR K h 33 := by
      show _ = K.mul (pwR K h 32) h; rw [K.mul_comm]
    have a4 : K.mul (pwR K (two K) 33) (pwR K h 33) = K.one := by
      rw [← GKS2.pwR_mul, two_h, pw_one]
    rw [a1, a2, a3]
    have : K.mul (K.mul ε (K.mul (K.mul (K.mul (pwR K (two K) 33) (pwR K (two K) 33)) (K.mul (two K) (two K)))
        (pwR K (two K) 33))) (pwR K h 33)
        = K.mul (K.mul (K.mul ε (K.mul (two K) (two K))) (K.mul (pwR K (two K) 33) (pwR K (two K) 33)))
            (K.mul (pwR K (two K) 33) (pwR K h 33)) := by lib_ring
    rw [this, a4, K.mul_comm _ K.one, K.one_mul]
    lib_ring
  rw [eq] at q
  refine ⟨O.le_trans _ _ _ q r, ?_⟩
  -- h^{32} 2^{32} = 1
  rw [← GKS2.pwR_mul, K.mul_comm, show K.add K.one K.one = two K from rfl, two_h, pw_one]
  exact O.le_refl _

/-- **The Wilson-action constants for β ≤ 2^{−103}.** -/
theorem beta_consts (h1 : O.le K.zero K.one) (h β : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (hβ : O.le K.zero β) (hβ0 : O.le (K.mul β (pwR K (two K) 103)) K.one) :
    let ε := K.mul (K.mul (two K) (two K)) β
    let m := K.add K.one (K.neg ε)
    O.le K.zero ε ∧ O.le (K.mul ε (pwR K (two K) 101)) K.one ∧ O.le h m := by
  intro ε m
  have t0 : O.le K.zero (two K) := by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
  have t1 : O.le K.one (two K) := by have := O.add_le_add _ _ _ _ (O.le_refl K.one) h1; rw [DetChain.add_zero'] at this; exact this
  have e0 : O.le K.zero ε := O.mul_nonneg _ _ (O.mul_nonneg _ _ t0 t0) hβ
  have eq1 : K.mul ε (pwR K (two K) 101) = K.mul β (pwR K (two K) 103) := by
    show K.mul (K.mul (K.mul (two K) (two K)) β) (pwR K (two K) 101) = K.mul β (K.mul (K.mul (pwR K (two K) 101) (two K)) (two K))
    lib_ring
  refine ⟨e0, by rw [eq1]; exact hβ0, ?_⟩
  -- 4β ≤ ½: from 2^{103} β ≤ 1 and 2^{100} ≥ 1, 8β ≤ 1
  have g := pw_ge_one K O h1 _ t1 100
  have s := O.mul_le_mul_right _ _ _ g (O.mul_nonneg _ _ hβ (O.mul_nonneg _ _ (O.mul_nonneg _ _ t0 t0) t0))
  rw [K.one_mul] at s
  have eq2 : K.mul (pwR K (two K) 100) (K.mul β (K.mul (K.mul (two K) (two K)) (two K))) = K.mul β (pwR K (two K) 103) := by
    show _ = K.mul β (K.mul (K.mul (K.mul (pwR K (two K) 100) (two K)) (two K)) (two K))
    lib_ring
  rw [eq2] at s
  have b8 := O.le_trans _ _ _ s hβ0
  -- h ≤ 1 − 4β  ⇔  4β + h ≤ 1 ; 8β ≤ 1 = h + h  ⇒ 4β ≤ h
  have e8 : K.mul β (K.mul (K.mul (two K) (two K)) (two K)) = K.add ε ε := by
    show K.mul β (K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one))
      = K.add (K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) β) (K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) β)
    lib_ring
  rw [e8, ← hh] at b8
  -- multiply ε + ε ≤ h + h by h: ε (h + h) ≤ h (h + h), i.e. ε ≤ h
  have b4 : O.le ε h := by
    have mm := O.mul_le_mul_right _ _ _ b8 h0
    have l1 : K.mul (K.add ε ε) h = K.mul ε (K.add h h) := by lib_ring
    have l2 : K.mul (K.add h h) h = K.mul h (K.add h h) := by lib_ring
    rw [l1, l2, hh, K.mul_comm ε K.one, K.one_mul, K.mul_comm h K.one, K.one_mul] at mm
    exact mm
  have := O.add_le_add _ _ _ _ b4 (O.le_refl (K.add h (K.neg ε)))
  have l3 : K.add ε (K.add h (K.neg ε)) = h := by lib_ring
  have l4 : K.add h (K.add h (K.neg ε)) = K.add (K.add h h) (K.neg ε) := by lib_ring
  rw [l3, l4, hh] at this
  exact this

open GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Dup DupAlg AreaG Join Reach
  HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist in
/-- **The 4D SU(2) strong-coupling mass gap with explicit constants:** for ε ≤ 2^{−101} (e.g. the Wilson action at
  β ≤ 2^{−103}, beta_consts), every finite set of plaquettes of Z⁴ has
  −B Z₁² ≤ 2(Z_{pq}Z₁ − Z_pZ_q) ≤ B Z₁², B = (2K₁)²(κ²)^ŝ 2^ŝ (½)^R, for dist_∞(p, q) > R + 1. -/
theorem gap_4d {G : Type} {L : Nat} (H : Haar K O G L) (DH : DupHaar K O H) (h1 : O.le K.zero K.one)
    (h10 : K.one ≠ K.zero)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq) (hpq : disjN (vec LL p0) (vec LL q0))
    (f : Nat → List G → R) (Up Uq : List G → R) (c0 m κ ε K1 h : R) (Rr : Nat)
    (hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hfaG : ∀ i, H.adm (f i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (hc0 : O.le K.zero c0) (hc1 : O.le c0 K.one) (hm : O.le K.zero m) (hm1 : O.le m K.one)
    (hκ1 : O.le K.one κ) (hκm : K.mul κ m = K.one)
    (hw : ∀ i x, O.le m (K.add c0 (f i x))) (hw1 : ∀ i x, O.le (K.add c0 (f i x)) K.one)
    (he : O.le K.zero ε) (hf : ∀ i x, O.le (K.neg ε) (f i x) ∧ O.le (f i x) ε)
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hh : K.add h h = K.one) (h0 : O.le K.zero h) (hmh : O.le h m)
    (hε : O.le (K.mul ε (pwR K (two K) 101)) K.one)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1) (M : List Bool) (hM : M.length = Pn.length) :
    let w : Nat → List G → R := fun i x => K.add c0 (f i x)
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let s := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) s))
      (K.mul (pwR K (K.add K.one K.one) s) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  have hκ0 : O.le K.zero κ := O.le_trans _ _ _ h1 hκ1
  have sm := small_4d K O h1 h ε κ m hh h0 he hε hκ0 hκm hmh
  have hu1 : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  exact GapZd.gap_Zd K O H DH h1 h10 4 LL hLL hL Pn hnd hv p0 q0 hpq f Up Uq c0 m κ ε K1 h (pwR K h 32) K.one Rr
    hfd hUp hUq hfaG hUpa hUqa hc0 hc1 hm hm1 hκ1 hκm hw hw1 he hf hK1 hUpb hUqb h0 hu1
    (AreaLower.pw_nn K O h h0 h1 32) h1 sm.1 sm.2 hdist M hM

open GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Dup DupAlg AreaG Join Reach
  HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist in
/-- **The 4D SU(2) strong-coupling area law with explicit constants:** for ε ≤ 2^{−101} and every polymer of fewer than
  m_A plaquettes having zero weight (center charge: m_A = RT for an R × T loop, CenterExp/RectLoop),
  −B Z₀ ≤ Z_W ≤ B Z₀ with B = K_χ κ^s 2^s (½)^{m_A}, on every finite set of plaquettes of Z⁴. -/
theorem area_4d {G : Type} {L : Nat} (H : Haar K O G L) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (n : List Nat) (hn : n.length = L)
    (f : Nat → List G → R) (χ : List G → R) (c0 m κ ε Kχ h : R) (mA : Nat)
    (hχd : depOn (n.map nzb) χ) (hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f i)) (hfa : ∀ i, H.adm (f i))
    (hχa : H.adm χ)
    (hc0 : O.le K.zero c0) (hc1 : O.le c0 K.one) (hm : O.le K.zero m) (hm1 : O.le m K.one)
    (hκ1 : O.le K.one κ) (hκm : K.mul κ m = K.one)
    (hw : ∀ i x, O.le m (K.add c0 (f i x))) (hw1 : ∀ i x, O.le (K.add c0 (f i x)) K.one)
    (he : O.le K.zero ε) (hf : ∀ i x, O.le (K.neg ε) (f i x) ∧ O.le (f i x) ε)
    (hK : O.le K.zero Kχ) (hχ : ∀ x, O.le (K.neg Kχ) (χ x) ∧ O.le (χ x) Kχ)
    (hh : K.add h h = K.one) (h0 : O.le K.zero h) (hmh : O.le h m)
    (hε : O.le (K.mul ε (pwR K (two K) 101)) K.one)
    (hvan : ∀ A : List Bool, A.length = (Pn.map (vec LL)).length → clS (fun q => touch q n) (Pn.map (vec LL)) A = A →
      cnt A < mA → aWH K O H f χ A = K.zero)
    (M : List Bool) (hM : M.length = (Pn.map (vec LL)).length) :
    let P := Pn.map (vec LL)
    let B := K.mul (K.mul Kχ (pwR K κ (cnt (P.map (fun q => touch q n)))))
      (K.mul (pwR K (K.add K.one K.one) (cnt (P.map (fun q => touch q n)))) (pwR K h mA))
    O.le (K.neg (K.mul B (Z0H K O H f c0 M))) (ZWH K O H f χ c0 M) ∧
      O.le (ZWH K O H f χ c0 M) (K.mul B (Z0H K O H f c0 M)) := by
  intro P B
  have hκ0 : O.le K.zero κ := O.le_trans _ _ _ h1 hκ1
  have sm := small_4d K O h1 h ε κ m hh h0 he hε hκ0 hκm hmh
  have hu1 : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  -- ε κ^{33} ≤ (2ε + ε²)(κ²)^{33}
  have kk : O.le κ (K.mul κ κ) := by
    have := O.mul_le_mul_right _ _ _ hκ1 hκ0; rw [K.one_mul] at this; exact this
  have p1 := pw_mono K O h1 κ (K.mul κ κ) hκ0 kk (4 * (2 * 4) + 1)
  have e2 : O.le ε (K.add (K.add ε ε) (K.mul ε ε)) := by
    have a := O.add_le_add _ _ _ _ (O.le_refl ε) he
    rw [DetChain.add_zero'] at a
    have b := O.add_le_add _ _ _ _ a (O.mul_nonneg _ _ he he)
    rw [DetChain.add_zero'] at b
    exact b
  have q1 := O.mul_le_mul_right _ _ _ e2 (AreaLower.pw_nn K O κ hκ0 h1 (4 * (2 * 4) + 1))
  have q2 := O.mul_le_mul_right _ _ _ p1 (O.le_trans _ _ _ he e2)
  rw [K.mul_comm (pwR K κ _), K.mul_comm (pwR K (K.mul κ κ) _)] at q2
  have hsmall := O.le_trans _ _ _ (O.le_trans _ _ _ q1 q2) sm.1
  have hlenP : P.length = Pn.length := StripAxial.len_map_g _ _
  have hvl : ∀ p, (vec LL p).length = L := fun p => (StripAxial.len_map_g _ LL).trans hL
  exact area_H K O H h1 h10 P n f χ c0 m κ ε Kχ h (pwR K h 32) K.one (4 * (2 * 4)) mA
    (fun i hi => by rw [nthL_map LL Pn i (hlenP ▸ hi)]; exact hvl _) hn hχd hfd hfa hχa hc0 hc1 hm hm1 hκ1 hκm hw hw1
    he hf hK hχ h0 hu1 (AreaLower.pw_nn K O h h0 h1 32) h1
    (fun r hr => by
      obtain ⟨p, hp, e⟩ := HaarFin.memL_map' (vec LL) r Pn hr
      rw [e]; exact deg_Zd 4 LL hLL Pn hnd hv p)
    hsmall sm.2 hvan M hM

end SU2Strong4D

#print axioms SU2Strong4D.small_4d
#print axioms SU2Strong4D.beta_consts
#print axioms SU2Strong4D.gap_4d
#print axioms SU2Strong4D.area_4d

ym/InfVol4D.lean

/-
  ym/InfVol4D — T45, S6: the infinite-volume limit of the 4D SU(2) strong-coupling theory.

  ~/lean/lib_shared has no real numbers yet (requested in REQUESTS.md). Completeness is therefore a structure
  hypothesis (a field of a structure, not an axiom): CComplete gives a limit to every Cauchy sequence.
    near_tri       |x − z| ≤ a, |y − z| ≤ b  ⇒  |x − y| ≤ a + b
    two_pow_ge     n ≤ 2^n
    cauchy_of_rate |a(n') − a(n)| ≤ D r^n for n' ≥ n, 0 ≤ r ≤ ½, Archimedean  ⇒  (a n) is Cauchy
    inf_vol        the limit L = lim a exists, a(n) → L, and |a(n) − L| ≤ D r^n (explicit rate)
    gap_inf        a uniform two-sided bound on finite-volume covariances (SU2Strong4D.gap_4d) holds for the limit
  The rate hypothesis is what Lad5Cauchy.uniform_cauchy gives for volumes Λ_n containing the n-ball around the
  observable (D = κCs/(1 − r), r = u(D_g + 2)); so expectations of local observables converge as Λ ↑ Z⁴, and the limit
  state keeps the exponential clustering (mass gap) and the area law bounds.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.SU2Strong4D
import ym.GapLimit

namespace InfVol4D

open YMChain LinkIntegral SymLimit Indep Geom

variable {R : Type} [DecidableEq R] (K : DetChain.CRing R) (O : ORing K)

/-- Cauchy completeness, as a structure (the real numbers are an instance). -/
structure CComplete where
  lim : (Nat → R) → R
  conv : ∀ a : Nat → R, (∀ ε, SymLimit.Pos K O ε → ∃ N, ∀ n m, N ≤ n → N ≤ m → Near K O (a n) (a m) ε) →
    ∀ ε, SymLimit.Pos K O ε → ∃ N, ∀ n, N ≤ n → Near K O (a n) (lim a) ε

omit [DecidableEq R] in
theorem near_tri (x y z a b : R) (h1 : Near K O x z a) (h2 : Near K O y z b) : Near K O x y (K.add a b) := by
  constructor
  · -- x ≤ z + a ≤ (y + b) + a
    have s := O.le_trans _ _ _ h1.1 (O.add_le_add _ _ _ _ h2.2 (O.le_refl a))
    have e : K.add (K.add y b) a = K.add y (K.add a b) := by lib_ring
    rw [e] at s; exact s
  · have s := O.le_trans _ _ _ h2.1 (O.add_le_add _ _ _ _ h1.2 (O.le_refl b))
    have e : K.add (K.add x a) b = K.add x (K.add a b) := by lib_ring
    rw [e] at s; exact s

omit [DecidableEq R] in
theorem two_pow_ge (h1 : O.le K.zero K.one) : ∀ n, O.le (natR K n) (pwR K (SU2Strong4D.two K) n)
  | 0 => h1
  | n + 1 => by
    have ih := two_pow_ge h1 n
    have g := SU2Strong4D.pw_ge_one K O h1 (SU2Strong4D.two K)
      (by have := O.add_le_add _ _ _ _ (O.le_refl K.one) h1; rw [DetChain.add_zero'] at this; exact this) n
    -- n + 1 ≤ 2^n + 2^n = 2^{n+1}
    have s := O.add_le_add _ _ _ _ ih g
    show O.le (K.add (natR K n) K.one) (K.mul (pwR K (SU2Strong4D.two K) n) (K.add K.one K.one))
    have e : K.mul (pwR K (SU2Strong4D.two K) n) (K.add K.one K.one)
        = K.add (pwR K (SU2Strong4D.two K) n) (pwR K (SU2Strong4D.two K) n) := by lib_ring
    rw [e]; exact s

omit [DecidableEq R] in
theorem near_weaken (x y a b : R) (h : Near K O x y a) (hab : O.le a b) : Near K O x y b :=
  ⟨O.le_trans _ _ _ h.1 (O.add_le_add _ _ _ _ (O.le_refl y) hab), O.le_trans _ _ _ h.2 (O.add_le_add _ _ _ _ (O.le_refl x) hab)⟩

omit [DecidableEq R] in
theorem near_symm (x y a : R) (h : Near K O x y a) : Near K O y x a := ⟨h.2, h.1⟩

/-- **A geometric Cauchy rate gives a Cauchy sequence.** -/
theorem cauchy_of_rate (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (harch : ∀ x : R, ∃ n, O.le x (natR K n))
    (a : Nat → R) (D r : R) (hD : O.le K.zero D) (hr0 : O.le K.zero r) (hrh : O.le r h)
    (hstep : ∀ n n', n ≤ n' → Near K O (a n') (a n) (K.mul D (pwR K r n))) :
    ∀ ε, SymLimit.Pos K O ε → ∃ N, ∀ n m, N ≤ n → N ≤ m → Near K O (a n) (a m) ε := by
  intro ε hε
  have hne : h ≠ K.zero := fun e => h10 (by rw [← hh, e, K.zero_add])
  let δ := K.mul ε h
  have hδ0 : O.le K.zero δ := O.mul_nonneg _ _ hε.1 h0
  have hδne : δ ≠ K.zero := fun e => hne (O.pos_mul_eq_zero _ _ hε.1 hε.2 e)
  obtain ⟨N, hN⟩ := harch (K.mul D (F.inv δ))
  -- D ≤ N δ
  have d1 : O.le D (K.mul (natR K N) δ) := by
    have := O.mul_le_mul_right _ _ _ hN hδ0
    rw [K.mul_assoc, K.mul_comm (F.inv δ), F.mul_inv δ hδne, K.mul_comm D K.one, K.one_mul] at this
    exact this
  -- D h^N ≤ δ
  have hpN : O.le K.zero (pwR K h N) := AreaLower.pw_nn K O h h0 h1 N
  have d2 : O.le (K.mul D (pwR K h N)) δ := by
    have s1 := O.mul_le_mul_right _ _ _ d1 hpN
    have s2 := O.mul_le_mul_right _ _ _ (two_pow_ge K O h1 N) (O.mul_nonneg _ _ hδ0 hpN)
    have e1 : K.mul (K.mul (natR K N) δ) (pwR K h N) = K.mul (natR K N) (K.mul δ (pwR K h N)) := by lib_ring
    have e2 : K.mul (pwR K (SU2Strong4D.two K) N) (K.mul δ (pwR K h N))
        = K.mul δ (pwR K (K.mul (SU2Strong4D.two K) h) N) := by
      rw [GKS2.pwR_mul]; lib_ring
    have th : K.mul (SU2Strong4D.two K) h = K.one := by
      show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
    rw [e1] at s1
    have e3 : K.mul δ K.one = δ := by rw [K.mul_comm, K.one_mul]
    rw [e2, th, SU2Strong4D.pw_one, e3] at s2
    exact O.le_trans _ _ _ s1 s2
  have d3 : O.le (K.mul D (pwR K r N)) δ := by
    have := O.mul_le_mul_right _ _ _ (SU2Strong4D.pw_mono K O h1 r h hr0 hrh N) hD
    rw [K.mul_comm, K.mul_comm (pwR K h N)] at this
    exact O.le_trans _ _ _ this d2
  refine ⟨N, fun n m hn hm => ?_⟩
  have a1 := near_weaken K O _ _ _ _ (hstep N n hn) d3
  have a2 := near_weaken K O _ _ _ _ (hstep N m hm) d3
  have t := near_tri K O _ _ _ _ _ a1 a2
  have e : K.add δ δ = ε := by show K.add (K.mul ε h) (K.mul ε h) = ε; rw [← K.distrib, hh, K.mul_comm, K.one_mul]
  rw [e] at t; exact t

/-- **The infinite-volume limit exists, with an explicit rate.** -/
theorem inf_vol (C : CComplete K O) (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (harch : ∀ x : R, ∃ n, O.le x (natR K n))
    (a : Nat → R) (D r : R) (hD : O.le K.zero D) (hr0 : O.le K.zero r) (hrh : O.le r h)
    (hstep : ∀ n n', n ≤ n' → Near K O (a n') (a n) (K.mul D (pwR K r n))) :
    (∀ ε, SymLimit.Pos K O ε → ∃ N, ∀ n, N ≤ n → Near K O (a n) (C.lim a) ε) ∧
      ∀ n, Near K O (a n) (C.lim a) (K.mul D (pwR K r n)) := by
  have hc := C.conv a (cauchy_of_rate K O F h1 h10 h hh h0 harch a D r hD hr0 hrh hstep)
  have hne : h ≠ K.zero := fun e => h10 (by rw [← hh, e, K.zero_add])
  refine ⟨hc, fun n => ?_⟩
  -- for every ε: |a n − L| ≤ D r^n + ε
  have key : ∀ ε, SymLimit.Pos K O ε → Near K O (a n) (C.lim a) (K.add (K.mul D (pwR K r n)) ε) := by
    intro ε hε
    obtain ⟨N, hN⟩ := hc ε hε
    let n' := N + n
    have b1 := hN n' (Nat.le_add_right N n)
    have b2 := hstep n n' (Nat.le_add_left n N)
    exact near_tri K O _ _ _ _ _ (near_symm K O _ _ _ b2) (near_symm K O _ _ _ b1)
  constructor
  · apply le_of_near_all K O h hh h0 hne
    intro ε hε
    have := (key ε hε).1
    rw [← K.add_assoc] at this; exact this
  · apply le_of_near_all K O h hh h0 hne
    intro ε hε
    have := (key ε hε).2
    rw [← K.add_assoc] at this; exact this

/-- **Clustering survives the infinite-volume limit.** -/
theorem gap_inf (C : CComplete K O) (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (harch : ∀ x : R, ∃ n, O.le x (natR K n))
    (cov : Nat → R) (D r B : R) (hD : O.le K.zero D) (hr0 : O.le K.zero r) (hrh : O.le r h)
    (hstep : ∀ n n', n ≤ n' → Near K O (cov n') (cov n) (K.mul D (pwR K r n)))
    (hb : ∀ n, O.le (K.neg B) (cov n) ∧ O.le (cov n) B) :
    O.le (K.neg B) (C.lim cov) ∧ O.le (C.lim cov) B := by
  have hne : h ≠ K.zero := fun e => h10 (by rw [← hh, e, K.zero_add])
  have lv := inf_vol K O C F h1 h10 h hh h0 harch cov D r hD hr0 hrh hstep
  exact GapLimit.bound_limit K O h hh h0 hne cov (C.lim cov) B hb
    (fun ε hε => by obtain ⟨N, hN⟩ := lv.1 ε hε; exact ⟨N, hN N (Nat.le_refl N)⟩)

end InfVol4D

#print axioms InfVol4D.cauchy_of_rate
#print axioms InfVol4D.inf_vol
#print axioms InfVol4D.gap_inf

90.10 L6: one block-spin step

Statement. The decimation/blocking map sends gauge-covariant, positive, reflection-positive lattice theories to theories with the same three properties on the coarse lattice; a \(2\,q^{-s}\) decay at fine distance \(s\) becomes \(2\,(2^{2^n})^{-t}\) at coarse distance \(t\) after \(n\) steps in the strong-coupling range.

Key steps. YMChain.block_gauge, block_weight_pos, block_rp, block_decay; decimation (Decimation).

Lean files.

YMChain.lean

/-
  YMChain.lean
  A four-dimensional interacting lattice gauge field built from the chain (T16).

  Lean 4 core plus FTAChain (pairs, `rem`, `iter`, laws of + and *).
  Proofs are hand-written terms, rewriting and case splits closed by `rfl`;
  no omega / simp / ring / decide / norm_num / linarith.

  Design (made here):
  * Gauge group: the unit quaternions with integer coordinates,
    ±1, ±i, ±j, ±k (the group Q8), written as a sign and a unit.
    `toVec` gives the integer coordinates, and the product agrees with the
    quaternion product of those coordinates (`mul_is_quaternion`).
  * A general group is passed by its operations and laws (`Grp`).
  * Space-time: any set of sites with four shifts that commute; the torus
    (Z/L)^4 with `rem` is an instance (`torus_comm`).
  * Field: a group element on every link; gauge transformation
    `U(x,μ) ↦ g(x) U(x,μ) g(x+μ)⁻¹`.
  * Action: count the plaquettes (2-cells, counted as in SimplexChain) whose
    holonomy is not 1, or more generally sum any class function of the
    holonomies. Weight `t^S`, measure = counting measure on configurations.

  Proved here:
    grp laws for Q8 (q8_assoc, q8_one_mul, q8_mul_one, q8_inv_mul, q8_mul_inv)
    mul_is_quaternion        Q8 product = integer quaternion product
    q8_noncomm               i j ≠ j i
    inv_mul_rev, conj_mul, conj_inv   group lemmas from the laws
    hol_gauge                holonomy of a transformed field = conjugate holonomy
    action_gauge_inv         S(gU) = S(U): every lattice, every g, every U,
                             every class function
    gauge_inverse            transforming by g then by g⁻¹ gives U back
                             (the counting measure is gauge invariant)
    weight_gauge_inv         t^S(gU) = t^S(U)
    torus_comm, torus_action_gauge_inv   the (Z/L)^4 instance, every L
    defect_class, wilson_class   both plaquette functions are class functions
    interaction              the plaquette weight is not a sum of one-link terms
    factor_sum               at t = 1 (no action), observables on disjoint link
                             sets are uncorrelated: Σ Σ F G = (Σ F)(Σ G)

  Added for T23 (reflection positivity, transfer matrix, strong coupling):
    rp_identity              Σ_x Σ_y F(x) F(y) K(x,y) = Σ_k c_k (Σ_x F(x) g_k(x))²
                             for a Gram kernel K(x,y) = Σ_k c_k g_k(x) g_k(y), any commutative ring
    rp_nonneg                with c_k ≥ 0 (any positivity predicate): ⟨θF · F⟩ ≥ 0
    gram_mul, gram_mul_nn    the product of Gram kernels is a Gram kernel, coefficients ≥ 0
    delta_gram               [g = h] = Σ_k [g = k][h = k]  (finite group, no repeats)
    defect_gram              the defect weight a + b [g = h] of a crossing plaquette is Gram
    transfer_pos             transfer-matrix entries q^(non-defects) are positive
    words_length             there are k^n words of length n over k letters
    surface_count            DFS codes of surfaces of area A: (D+1)^(2A) = ((D+1)^2)^A
    geom_telescope           q T(N) + c^(N+1) = q^(N+1) + c T(N),  T(N) = Σ_{j≤N} c^j q^(N-j)
    geom_bound               2c ≤ q ⇒ T(N) ≤ 2 q^N
    decay_bound              Σ_{A≥t} c^A q^(-A) ≤ 2 (c/q)^t, written in integers; no L appears

  Added for T25:
    c_441, gap_explicit      with c = 441 and activity ≤ 1/q, q ≥ 882 (β ≤ β₀ = 1/1764 for the
                             Wilson action): surface sum ≤ 2·2^(-t), decay rate ≥ ln 2, every L
    argmin, sum_zero_each, sumK_sub, mul_neg_r, swap3
    perron_simple            positive matrix, positive eigenvector u: every eigenvector with
                             the same eigenvalue is a multiple of u (the top eigenvalue is simple)

  Added for T27:
    perron_dominant          |μ| ≤ λ for every eigenvalue μ (positive matrix, positive eigenvector)
    perron_gap               every eigenvalue μ ≠ λ has |μ| < λ: a strict gap on each finite lattice
    sq, sq_eigen, pos_sum, sum_le, abs_sum, argmax   the steps (T² is positive; same eigenvectors)
    ker_symm                 Gram kernels, hence transfer matrices built from them, are symmetric
    sh2_comm, coarse         the coarse lattice of a decimation step
    block_gauge              blocking commutes with gauge transformations (coarse gauge covariance)
    block_weight_pos         blocked weights stay positive
    block_rp                 blocked observables keep reflection positivity
    doeblin                  rows P_ij = r_ij + δ q_j, Σ_j r_ij = e: spread(Pf) ≤ e · spread(f)
                             (Hilbert/Doeblin contraction; a gap ≥ -ln(1-δ) for stochastic P)
    pow_add_nat, pow_mul_nat, block_decay, block_decay_explicit
                             a 2·q^(-s) bound at fine distance s gives 2·(2^(2^n))^(-t) at coarse
                             distance t after n decimation steps (strong-coupling range q ≥ 882)
-/
import DetChain

namespace YMChain
open FTAChain

/-! ## A group, given by its operations and laws -/

structure Grp (G : Type) where
  mul : G → G → G
  inv : G → G
  one : G
  assoc : ∀ a b c, mul (mul a b) c = mul a (mul b c)
  one_mul : ∀ a, mul one a = a
  mul_one : ∀ a, mul a one = a
  inv_mul : ∀ a, mul (inv a) a = one
  mul_inv : ∀ a, mul a (inv a) = one

section Group
variable {G : Type} (K : Grp G)

theorem cancel_left (a b c : G) : K.mul (K.mul a (K.inv b)) (K.mul b c) = K.mul a c := by
  rw [K.assoc, ← K.assoc (K.inv b) b c, K.inv_mul, K.one_mul]

theorem inv_unique (a b : G) (h : K.mul a b = K.one) : K.inv a = b := by
  have e : K.mul (K.mul (K.inv a) a) b = K.mul (K.inv a) K.one := by rw [K.assoc, h]
  rw [K.inv_mul, K.one_mul, K.mul_one] at e
  exact e.symm

theorem inv_mul_rev (a b : G) : K.inv (K.mul a b) = K.mul (K.inv b) (K.inv a) := by
  apply inv_unique
  rw [K.assoc, ← K.assoc b (K.inv b) (K.inv a), K.mul_inv, K.one_mul, K.mul_inv]

theorem inv_inv (a : G) : K.inv (K.inv a) = a := inv_unique K (K.inv a) a (K.inv_mul a)

/-- `(g u h⁻¹)(h v k⁻¹) = g (u v) k⁻¹`. -/
theorem conj_mul (g u h v k : G) :
    K.mul (K.mul (K.mul g u) (K.inv h)) (K.mul (K.mul h v) (K.inv k))
      = K.mul (K.mul g (K.mul u v)) (K.inv k) := by
  rw [← K.assoc (K.mul (K.mul g u) (K.inv h)) (K.mul h v) (K.inv k),
    K.assoc (K.mul g u) (K.inv h) (K.mul h v), ← K.assoc (K.inv h) h v, K.inv_mul, K.one_mul,
    K.assoc g u v]

/-- `(g u h⁻¹)⁻¹ = h u⁻¹ g⁻¹`. -/
theorem conj_inv (g u h : G) :
    K.inv (K.mul (K.mul g u) (K.inv h)) = K.mul (K.mul h (K.inv u)) (K.inv g) := by
  rw [inv_mul_rev, inv_inv, inv_mul_rev, K.assoc]

end Group

/-! ## Lattice, field, gauge transformation -/

inductive D4 where
  | d0
  | d1
  | d2
  | d3

/-- Sites with four shifts that commute. -/
structure Lat (S : Type) where
  sh : S → D4 → S
  comm : ∀ x μ ν, sh (sh x μ) ν = sh (sh x ν) μ

section Field
variable {G : Type} (K : Grp G) {S : Type} (Λ : Lat S)

/-- The holonomy round the plaquette at `x` in the plane `(μ, ν)`. -/
def hol (U : S → D4 → G) (x : S) (μ ν : D4) : G :=
  K.mul (K.mul (K.mul (U x μ) (U (Λ.sh x μ) ν)) (K.inv (U (Λ.sh x ν) μ))) (K.inv (U x ν))

/-- The gauge transformation of a field. -/
def gauge (g : S → G) (U : S → D4 → G) : S → D4 → G :=
  fun x μ => K.mul (K.mul (g x) (U x μ)) (K.inv (g (Λ.sh x μ)))

/-- **The holonomy of a transformed field is the conjugated holonomy.** -/
theorem hol_gauge (g : S → G) (U : S → D4 → G) (x : S) (μ ν : D4) :
    hol K Λ (gauge K Λ g U) x μ ν = K.mul (K.mul (g x) (hol K Λ U x μ ν)) (K.inv (g x)) := by
  show K.mul (K.mul (K.mul
      (K.mul (K.mul (g x) (U x μ)) (K.inv (g (Λ.sh x μ))))
      (K.mul (K.mul (g (Λ.sh x μ)) (U (Λ.sh x μ) ν)) (K.inv (g (Λ.sh (Λ.sh x μ) ν)))))
      (K.inv (K.mul (K.mul (g (Λ.sh x ν)) (U (Λ.sh x ν) μ)) (K.inv (g (Λ.sh (Λ.sh x ν) μ))))))
      (K.inv (K.mul (K.mul (g x) (U x ν)) (K.inv (g (Λ.sh x ν))))) = _
  rw [conj_inv, conj_inv, conj_mul, Λ.comm x ν μ, conj_mul, conj_mul]
  rfl

/-- `gauge g⁻¹ (gauge g U) = U`: a gauge transformation is a bijection of
    configurations, so the counting measure does not see it. -/
theorem gauge_inverse (g : S → G) (U : S → D4 → G) (x : S) (μ : D4) :
    gauge K Λ (fun y => K.inv (g y)) (gauge K Λ g U) x μ = U x μ := by
  show K.mul (K.mul (K.inv (g x)) (K.mul (K.mul (g x) (U x μ)) (K.inv (g (Λ.sh x μ)))))
      (K.inv (K.inv (g (Λ.sh x μ)))) = U x μ
  rw [inv_inv, ← K.assoc (K.inv (g x)), ← K.assoc (K.inv (g x)), K.inv_mul, K.one_mul, K.assoc,
    K.inv_mul, K.mul_one]

/-- A class function: unchanged by conjugation. -/
def IsClass (f : G → Nat) : Prop := ∀ a b, f (K.mul (K.mul a b) (K.inv a)) = f b

/-- The six planes `μ < ν`. -/
def planes : List (D4 × D4) :=
  [(.d0, .d1), (.d0, .d2), (.d0, .d3), (.d1, .d2), (.d1, .d3), (.d2, .d3)]

def lsum {A : Type} (F : A → Nat) : List A → Nat
  | [] => 0
  | a :: as => F a + lsum F as

theorem lsum_ext {A : Type} (F H : A → Nat) (h : ∀ a, F a = H a) : ∀ l, lsum F l = lsum H l
  | [] => rfl
  | a :: as => by
    show F a + lsum F as = H a + lsum H as
    rw [h a, lsum_ext F H h as]

/-- **The action**: sum of `f` over the plaquettes at the listed sites. -/
def action (f : G → Nat) (sites : List S) (U : S → D4 → G) : Nat :=
  lsum (fun x => lsum (fun p => f (hol K Λ U x p.1 p.2)) planes) sites

/-- **Gauge invariance of the action**, for every list of sites, every gauge
    transformation, every field and every class function. -/
theorem action_gauge_inv (f : G → Nat) (hf : IsClass K f) (sites : List S)
    (g : S → G) (U : S → D4 → G) : action K Λ f sites (gauge K Λ g U) = action K Λ f sites U :=
  lsum_ext _ _ (fun x => lsum_ext _ _ (fun p => by
    show f (hol K Λ (gauge K Λ g U) x p.1 p.2) = f (hol K Λ U x p.1 p.2)
    rw [hol_gauge]
    exact hf (g x) _) planes) sites

/-- The Boltzmann weight `t^S` is gauge invariant. -/
theorem weight_gauge_inv (f : G → Nat) (hf : IsClass K f) (sites : List S) (t : Nat)
    (g : S → G) (U : S → D4 → G) :
    t ^ action K Λ f sites (gauge K Λ g U) = t ^ action K Λ f sites U := by
  rw [action_gauge_inv K Λ f hf sites g U]

end Field

/-! ## The torus (Z/L)^4 -/

structure Site where
  c0 : Nat
  c1 : Nat
  c2 : Nat
  c3 : Nat

def up (L n : Nat) : Nat := rem L (n + 1)

def tsh (L : Nat) (x : Site) : D4 → Site
  | .d0 => ⟨up L x.c0, x.c1, x.c2, x.c3⟩
  | .d1 => ⟨x.c0, up L x.c1, x.c2, x.c3⟩
  | .d2 => ⟨x.c0, x.c1, up L x.c2, x.c3⟩
  | .d3 => ⟨x.c0, x.c1, x.c2, up L x.c3⟩

theorem torus_comm (L : Nat) (x : Site) (μ ν : D4) : tsh L (tsh L x μ) ν = tsh L (tsh L x ν) μ := by
  cases μ <;> cases ν <;> rfl

def torus (L : Nat) : Lat Site := ⟨tsh L, torus_comm L⟩

/-- All sites of (Z/L)^4. -/
def sites (L : Nat) : List Site :=
  (List.range L).flatMap fun a => (List.range L).flatMap fun b =>
    (List.range L).flatMap fun c => (List.range L).map fun d => ⟨a, b, c, d⟩

theorem sites_length : (sites 2).length = 16 ∧ (sites 3).length = 81 := ⟨rfl, rfl⟩

theorem torus_action_gauge_inv {G : Type} (K : Grp G) (f : G → Nat) (hf : IsClass K f) (L : Nat)
    (g : Site → G) (U : Site → D4 → G) :
    action K (torus L) f (sites L) (gauge K (torus L) g U) = action K (torus L) f (sites L) U :=
  action_gauge_inv K (torus L) f hf (sites L) g U

/-! ## Q8: the unit quaternions with integer coordinates -/

inductive U4 where
  | e
  | i
  | j
  | k

/-- Product of units: a sign flip and a unit (`ij = k`, `ji = -k`, `i² = -1`, ...). -/
def umul : U4 → U4 → Bool × U4
  | .e, u => (false, u)
  | .i, .e => (false, .i)
  | .i, .i => (true, .e)
  | .i, .j => (false, .k)
  | .i, .k => (true, .j)
  | .j, .e => (false, .j)
  | .j, .i => (true, .k)
  | .j, .j => (true, .e)
  | .j, .k => (false, .i)
  | .k, .e => (false, .k)
  | .k, .i => (false, .j)
  | .k, .j => (true, .i)
  | .k, .k => (true, .e)

def bxor : Bool → Bool → Bool
  | false, b => b
  | true, false => true
  | true, true => false

/-- `±u`: `neg = true` means the minus sign. -/
structure Q8 where
  neg : Bool
  u : U4

def qmul (a b : Q8) : Q8 := ⟨bxor (bxor a.neg b.neg) (umul a.u b.u).1, (umul a.u b.u).2⟩

def isE : U4 → Bool
  | .e => true
  | .i => false
  | .j => false
  | .k => false

/-- `u⁻¹ = -u` for `u = i, j, k`, and `1⁻¹ = 1`. -/
def qinv (a : Q8) : Q8 := ⟨bxor a.neg (!(isE a.u)), a.u⟩

def qone : Q8 := ⟨false, .e⟩

theorem q8_assoc (a b c : Q8) : qmul (qmul a b) c = qmul a (qmul b c) := by
  cases a with
  | mk s u => cases b with
    | mk t v => cases c with
      | mk r w => cases s <;> cases t <;> cases r <;> cases u <;> cases v <;> cases w <;> rfl

theorem q8_one_mul (a : Q8) : qmul qone a = a := by
  cases a with
  | mk s u => cases s <;> cases u <;> rfl

theorem q8_mul_one (a : Q8) : qmul a qone = a := by
  cases a with
  | mk s u => cases s <;> cases u <;> rfl

theorem q8_inv_mul (a : Q8) : qmul (qinv a) a = qone := by
  cases a with
  | mk s u => cases s <;> cases u <;> rfl

theorem q8_mul_inv (a : Q8) : qmul a (qinv a) = qone := by
  cases a with
  | mk s u => cases s <;> cases u <;> rfl

def Q8grp : Grp Q8 := ⟨qmul, qinv, qone, q8_assoc, q8_one_mul, q8_mul_one, q8_inv_mul, q8_mul_inv⟩

/-- Integer coordinates `(w, x, y, z)` of `w + x i + y j + z k`. -/
def toVec (a : Q8) : Int × Int × Int × Int :=
  let s : Int := match a.neg with
    | false => 1
    | true => -1
  match a.u with
  | .e => (s, 0, 0, 0)
  | .i => (0, s, 0, 0)
  | .j => (0, 0, s, 0)
  | .k => (0, 0, 0, s)

/-- The quaternion product of coordinates. -/
def vmul (p q : Int × Int × Int × Int) : Int × Int × Int × Int :=
  let (a1, b1, c1, d1) := p
  let (a2, b2, c2, d2) := q
  (a1 * a2 - b1 * b2 - c1 * c2 - d1 * d2,
   a1 * b2 + b1 * a2 + c1 * d2 - d1 * c2,
   a1 * c2 - b1 * d2 + c1 * a2 + d1 * b2,
   a1 * d2 + b1 * c2 - c1 * b2 + d1 * a2)

def norm2 (p : Int × Int × Int × Int) : Int :=
  p.1 * p.1 + p.2.1 * p.2.1 + p.2.2.1 * p.2.2.1 + p.2.2.2 * p.2.2.2

/-- **Q8 is the group of unit integer quaternions**: its product is the
    quaternion product, and every element has norm 1. -/
theorem mul_is_quaternion (a b : Q8) : toVec (qmul a b) = vmul (toVec a) (toVec b) := by
  cases a with
  | mk s u => cases b with
    | mk t v => cases s <;> cases t <;> cases u <;> cases v <;> rfl

theorem unit_norm (a : Q8) : norm2 (toVec a) = 1 := by
  cases a with
  | mk s u => cases s <;> cases u <;> rfl

def qi : Q8 := ⟨false, .i⟩
def qj : Q8 := ⟨false, .j⟩

theorem q8_noncomm : qmul qi qj ≠ qmul qj qi := fun h =>
  Bool.noConfusion (congrArg (fun a => a.neg) h)

/-! ## Plaquette functions -/

/-- The defect: 1 when the holonomy is not 1. -/
def defect (a : Q8) : Nat :=
  match a.neg, a.u with
  | false, .e => 0
  | false, .i => 1
  | false, .j => 1
  | false, .k => 1
  | true, _ => 1

/-- `2 (1 - Re a)` for a unit quaternion: 0, 2, 4 on the classes 1, ±i ±j ±k, -1
    (the Wilson plaquette function, as a natural number). -/
def wilson2 (a : Q8) : Nat :=
  match a.neg, a.u with
  | false, .e => 0
  | true, .e => 4
  | _, .i => 2
  | _, .j => 2
  | _, .k => 2

theorem defect_class : IsClass Q8grp defect := by
  intro a b
  cases a with
  | mk s u => cases b with
    | mk t v => cases s <;> cases t <;> cases u <;> cases v <;> rfl

theorem wilson_class : IsClass Q8grp wilson2 := by
  intro a b
  cases a with
  | mk s u => cases b with
    | mk t v => cases s <;> cases t <;> cases u <;> cases v <;> rfl

/-- The weight of one plaquette from its four links. -/
def pw (a b c d : Q8) : Nat := defect (qmul (qmul (qmul a b) (qinv c)) (qinv d))

/-- Eight terms in two different groupings. -/
theorem regroup (x x' y y' z w : Nat) :
    x + y + z + w + (x' + y' + z + w) = x + y' + z + w + (x' + y + z + w) := by
  rw [nadd_assoc (x + y) z w, nadd_assoc (x' + y') z w, nadd_assoc (x + y') z w,
    nadd_assoc (x' + y) z w, mid4 (x + y) (z + w) (x' + y') (z + w),
    mid4 (x + y') (z + w) (x' + y) (z + w), mid4 x y x' y', mid4 x y' x' y, nadd_comm y' y]

/-- **Interaction.** The plaquette weight is not a sum of one-link terms:
    there are no `h1 h2 h3 h4` with `pw a b c d = h1 a + h2 b + h3 c + h4 d`.
    (`pw i i 1 1 = 1` since `i² = -1`, while `pw 1 1 1 1 = 0` and
    `pw i 1 1 1 = pw 1 i 1 1 = 1`; a sum of one-link terms would give `1 + 0 = 1 + 1`.) -/
theorem interaction :
    ¬ ∃ h1 h2 h3 h4 : Q8 → Nat, ∀ a b c d, pw a b c d = h1 a + h2 b + h3 c + h4 d := by
  intro ⟨h1, h2, h3, h4, h⟩
  have s : pw qi qi qone qone + pw qone qone qone qone = pw qi qone qone qone + pw qone qi qone qone := by
    rw [h qi qi qone qone, h qone qone qone qone, h qi qone qone qone, h qone qi qone qone]
    exact regroup _ _ _ _ _ _
  have s12 : (1 : Nat) = 2 := s
  exact succ_ne_zero' 0 (succ_inj' s12).symm

/-! ## No action: observables on disjoint link sets do not correlate -/

/-- `Σ_{a ∈ A} Σ_{b ∈ B} F a * H b = (Σ F) (Σ H)`. -/
theorem factor_sum {A B : Type} (F : A → Nat) (H : B → Nat) (bs : List B) :
    ∀ as : List A, lsum (fun a => lsum (fun b => F a * H b) bs) as = lsum F as * lsum H bs
  | [] => (nzero_mul _).symm
  | a :: as => by
    show lsum (fun b => F a * H b) bs + lsum (fun a => lsum (fun b => F a * H b) bs) as
      = (F a + lsum F as) * lsum H bs
    rw [factor_sum F H bs as, nright_distrib, mul_lsum]
where
  mul_lsum : ∀ (c : Nat) (l : List B), lsum (fun b => c * H b) l = c * lsum H l := fun c l => by
    induction l with
    | nil => rfl
    | cons b l ih =>
      show c * H b + lsum (fun b => c * H b) l = c * (H b + lsum H l)
      rw [ih, nleft_distrib]

/-! ## Reflection positivity, for Gram kernels -/

section RP
variable {R : Type} (K : DetChain.CRing R)

/-- A positivity predicate on the ring: squares, sums and products of nonnegatives. -/
structure Positivity where
  nn : R → Prop
  nn_sq : ∀ x, nn (K.mul x x)
  nn_add : ∀ a b, nn a → nn b → nn (K.add a b)
  nn_mul : ∀ a b, nn a → nn b → nn (K.mul a b)
  nn_zero : nn K.zero

def sumK {A : Type} (f : A → R) : List A → R
  | [] => K.zero
  | a :: as => K.add (f a) (sumK f as)

theorem sumK_ext {A : Type} (f g : A → R) (h : ∀ a, f a = g a) : ∀ l, sumK K f l = sumK K g l
  | [] => rfl
  | a :: as => by
    show K.add (f a) (sumK K f as) = K.add (g a) (sumK K g as)
    rw [h a, sumK_ext f g h as]

theorem sumK_add {A : Type} (f g : A → R) :
    ∀ l, sumK K (fun a => K.add (f a) (g a)) l = K.add (sumK K f l) (sumK K g l)
  | [] => (K.zero_add K.zero).symm
  | a :: as => by
    show K.add (K.add (f a) (g a)) (sumK K (fun a => K.add (f a) (g a)) as)
      = K.add (K.add (f a) (sumK K f as)) (K.add (g a) (sumK K g as))
    rw [sumK_add f g as, DetChain.add_mid4]

theorem mul_sumK {A : Type} (c : R) (f : A → R) :
    ∀ l, K.mul c (sumK K f l) = sumK K (fun a => K.mul c (f a)) l
  | [] => DetChain.mul_zero K c
  | a :: as => by
    show K.mul c (K.add (f a) (sumK K f as)) = K.add (K.mul c (f a)) (sumK K (fun a => K.mul c (f a)) as)
    rw [K.distrib, mul_sumK c f as]

theorem sumK_mul {A : Type} (c : R) (f : A → R) (l : List A) :
    K.mul (sumK K f l) c = sumK K (fun a => K.mul (f a) c) l := by
  rw [K.mul_comm, mul_sumK]
  exact sumK_ext K _ _ (fun a => K.mul_comm _ _) l

theorem sumK_swap {A B : Type} (g : A → B → R) (bs : List B) :
    ∀ as : List A, sumK K (fun a => sumK K (fun b => g a b) bs) as
      = sumK K (fun b => sumK K (fun a => g a b) as) bs
  | [] => (zeros bs).symm
  | a :: as => by
    show K.add (sumK K (fun b => g a b) bs) (sumK K (fun a => sumK K (fun b => g a b) bs) as)
      = sumK K (fun b => K.add (g a b) (sumK K (fun a => g a b) as)) bs
    rw [sumK_swap g bs as, sumK_add]
where
  zeros : ∀ l : List B, sumK K (fun _ => K.zero) l = K.zero
    | [] => rfl
    | _ :: l => by
      show K.add K.zero (sumK K (fun _ => K.zero) l) = K.zero
      rw [zeros l, K.zero_add]

/-- A Gram kernel `K(x,y) = Σ_k c_k g_k(x) g_k(y)`, given by its list of `(c_k, g_k)`. -/
def ker {X : Type} (L : List (R × (X → R))) (x y : X) : R :=
  sumK K (fun p => K.mul (K.mul p.1 (p.2 x)) (p.2 y)) L

theorem rearr (a b c d e : R) :
    K.mul (K.mul a b) (K.mul (K.mul c d) e) = K.mul c (K.mul (K.mul a d) (K.mul b e)) := by
  rw [K.mul_assoc c d e, ← K.mul_assoc, K.mul_comm (K.mul a b) c, K.mul_assoc c, ← K.mul_assoc (K.mul a b) d,
    K.mul_assoc a b d, K.mul_comm b d, ← K.mul_assoc a d b, K.mul_assoc (K.mul a d) b e]

/-- **Reflection positivity as a sum of squares.**
    `Σ_x Σ_y F(x) F(y) K(x,y) = Σ_k c_k (Σ_x F(x) g_k(x))²`. -/
theorem rp_identity {X : Type} (F : X → R) (xs : List X) (L : List (R × (X → R))) :
    sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (ker K L x y)) xs) xs
      = sumK K (fun p => K.mul p.1 (K.mul (sumK K (fun x => K.mul (F x) (p.2 x)) xs)
          (sumK K (fun x => K.mul (F x) (p.2 x)) xs))) L := by
  have e1 : ∀ x y, K.mul (K.mul (F x) (F y)) (ker K L x y)
      = sumK K (fun p => K.mul p.1 (K.mul (K.mul (F x) (p.2 x)) (K.mul (F y) (p.2 y)))) L := fun x y => by
    show K.mul (K.mul (F x) (F y)) (sumK K _ L) = _
    rw [mul_sumK]
    exact sumK_ext K _ _ (fun p => rearr K _ _ _ _ _) L
  rw [sumK_ext K _ _ (fun x => sumK_ext K _ _ (fun y => e1 x y) xs) xs]
  rw [sumK_ext K _ _ (fun x => sumK_swap K _ L xs) xs, sumK_swap]
  refine sumK_ext K _ _ (fun p => ?_) L
  show sumK K (fun x => sumK K (fun y => K.mul p.1 (K.mul ((fun x => K.mul (F x) (p.2 x)) x)
      ((fun x => K.mul (F x) (p.2 x)) y))) xs) xs = _
  generalize (fun x => K.mul (F x) (p.2 x)) = a
  have inner : ∀ x, sumK K (fun y => K.mul p.1 (K.mul (a x) (a y))) xs
      = K.mul p.1 (K.mul (a x) (sumK K a xs)) := fun x => by
    rw [mul_sumK K (a x) a xs, mul_sumK K p.1]
  rw [sumK_ext K _ _ inner xs, sumK_mul K (sumK K a xs) a xs, mul_sumK K p.1]

/-- All coefficients are nonnegative. -/
def AllNN {X : Type} (P : Positivity K) : List (R × (X → R)) → Prop
  | [] => True
  | p :: ps => P.nn p.1 ∧ AllNN P ps

theorem sumK_nn {A : Type} (P : Positivity K) (f : A → R) (h : ∀ a, P.nn (f a)) : ∀ l, P.nn (sumK K f l)
  | [] => P.nn_zero
  | a :: as => P.nn_add _ _ (h a) (sumK_nn P f h as)

theorem sumK_nn_list {X : Type} (P : Positivity K) (f : R × (X → R) → R) :
    ∀ L : List (R × (X → R)), AllNN K P L → (∀ p, P.nn p.1 → P.nn (f p)) → P.nn (sumK K f L)
  | [], _, _ => P.nn_zero
  | p :: ps, h, hf => P.nn_add _ _ (hf p h.1) (sumK_nn_list P f ps h.2 hf)

/-- **⟨θF · F⟩ ≥ 0** for a Gram kernel with nonnegative coefficients. -/
theorem rp_nonneg {X : Type} (P : Positivity K) (F : X → R) (xs : List X) (L : List (R × (X → R)))
    (hL : AllNN K P L) :
    P.nn (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (ker K L x y)) xs) xs) := by
  rw [rp_identity]
  exact sumK_nn_list K P _ L hL (fun p hp => P.nn_mul _ _ hp (P.nn_sq _))

/-- Product of Gram lists. -/
def prodG {X : Type} : List (R × (X → R)) → List (R × (X → R)) → List (R × (X → R))
  | [], _ => []
  | p :: ps, L2 => L2.map (fun q => (K.mul p.1 q.1, fun x => K.mul (p.2 x) (q.2 x))) ++ prodG ps L2

theorem sumK_append {A : Type} (f : A → R) : ∀ a b : List A, sumK K f (a ++ b) = K.add (sumK K f a) (sumK K f b)
  | [], b => (K.zero_add _).symm
  | x :: a, b => by
    show K.add (f x) (sumK K f (a ++ b)) = K.add (K.add (f x) (sumK K f a)) (sumK K f b)
    rw [sumK_append f a b, K.add_assoc]

theorem sumK_map {A B : Type} (f : B → R) (g : A → B) : ∀ l : List A, sumK K f (l.map g) = sumK K (fun a => f (g a)) l
  | [] => rfl
  | a :: as => by
    show K.add (f (g a)) (sumK K f (as.map g)) = K.add (f (g a)) (sumK K (fun a => f (g a)) as)
    rw [sumK_map f g as]

theorem mul_mid4 (a b c d : R) : K.mul (K.mul a b) (K.mul c d) = K.mul (K.mul a c) (K.mul b d) := by
  rw [K.mul_assoc, ← K.mul_assoc b c d, K.mul_comm b c, K.mul_assoc c b d, ← K.mul_assoc]

theorem rearr2 (c1 a1 b1 c2 a2 b2 : R) :
    K.mul (K.mul (K.mul c1 c2) (K.mul a1 a2)) (K.mul b1 b2)
      = K.mul (K.mul (K.mul c1 a1) b1) (K.mul (K.mul c2 a2) b2) := by
  rw [K.mul_assoc, mul_mid4 K a1 a2 b1 b2, mul_mid4 K c1 c2 (K.mul a1 b1) (K.mul a2 b2),
    ← K.mul_assoc c1 a1 b1, ← K.mul_assoc c2 a2 b2]

/-- **The product of two Gram kernels is the Gram kernel of the product list.** -/
theorem gram_mul {X : Type} (L1 L2 : List (R × (X → R))) (x y : X) :
    ker K (prodG K L1 L2) x y = K.mul (ker K L1 x y) (ker K L2 x y) := by
  induction L1 with
  | nil => exact (DetChain.zero_mul' K _).symm
  | cons p ps ih =>
    show sumK K _ (L2.map _ ++ prodG K ps L2) = K.mul (K.add _ (ker K ps x y)) (ker K L2 x y)
    rw [sumK_append, sumK_map]
    show K.add _ (ker K (prodG K ps L2) x y) = _
    rw [ih, DetChain.right_distrib]
    congr 1
    show _ = K.mul _ (sumK K _ L2)
    rw [mul_sumK]
    exact sumK_ext K _ _ (fun q => rearr2 K _ _ _ _ _ _) L2

theorem gram_mul_nn {X : Type} (P : Positivity K) :
    ∀ L1 L2 : List (R × (X → R)), AllNN K P L1 → AllNN K P L2 → AllNN K P (prodG K L1 L2)
  | [], _, _, _ => trivial
  | p :: ps, L2, h1, h2 => by
    show AllNN K P (L2.map _ ++ prodG K ps L2)
    exact app_nn _ _ (map_nn p h1.1 L2 h2) (gram_mul_nn P ps L2 h1.2 h2)
where
  map_nn (p : R × (X → R)) (hp : P.nn p.1) :
      ∀ L2 : List (R × (X → R)), AllNN K P L2 →
        AllNN K P (L2.map (fun q => (K.mul p.1 q.1, fun x => K.mul (p.2 x) (q.2 x))))
    | [], _ => trivial
    | q :: qs, h => ⟨P.nn_mul _ _ hp h.1, map_nn p hp qs h.2⟩
  app_nn : ∀ (a b : List (R × (X → R))), AllNN K P a → AllNN K P b → AllNN K P (a ++ b)
    | [], _, _, hb => hb
    | _ :: a, b, ha, hb => ⟨ha.1, app_nn a b ha.2 hb⟩

/-- Indicator in the ring. -/
def indR : Bool → R
  | true => K.one
  | false => K.zero

def memL {G : Type} : G → List G → Prop
  | _, [] => False
  | a, b :: bs => a = b ∨ memL a bs

def distinctL {G : Type} : List G → Prop
  | [] => True
  | a :: as => (¬ memL a as) ∧ distinctL as

/-- **The diagonal is Gram**: for a list `Gs` with no repeats containing `g`,
    `Σ_{k∈Gs} [g = k][h = k] = [g = h]`. -/
theorem delta_gram {G : Type} (eqb : G → G → Bool) (sound : ∀ a b, eqb a b = true → a = b)
    (refl : ∀ a, eqb a a = true) (symm : ∀ a b, eqb a b = eqb b a) (g h : G) :
    ∀ Gs : List G, distinctL Gs → memL g Gs →
      sumK K (fun k => K.mul (indR K (eqb g k)) (indR K (eqb h k))) Gs = indR K (eqb g h)
  | [], _, hm => False.elim hm
  | k :: ks, hd, hm => by
    show K.add (K.mul (indR K (eqb g k)) (indR K (eqb h k))) (sumK K _ ks) = _
    cases e : eqb g k with
    | true =>
      have gk : g = k := sound g k e
      rw [← gk] at hd
      have rest : sumK K (fun k => K.mul (indR K (eqb g k)) (indR K (eqb h k))) ks = K.zero :=
        zero_rest ks hd.1
      rw [rest, DetChain.add_zero', ← gk, symm h g]
      exact DetChain.CRing.one_mul K _
    | false =>
      have hm' : memL g ks := by
        cases hm with
        | inl hg => rw [hg, refl] at e; exact Bool.noConfusion e
        | inr hr => exact hr
      rw [show K.mul (indR K false) (indR K (eqb h k)) = K.zero from DetChain.zero_mul' K _, K.zero_add]
      exact delta_gram eqb sound refl symm g h ks hd.2 hm'
where
  zero_rest : ∀ ks : List G, ¬ memL g ks →
      sumK K (fun k => K.mul (indR K (eqb g k)) (indR K (eqb h k))) ks = K.zero
    | [], _ => rfl
    | k :: ks, hn => by
      show K.add (K.mul (indR K (eqb g k)) (indR K (eqb h k))) (sumK K _ ks) = K.zero
      cases e : eqb g k with
      | true => exact absurd (Or.inl (sound g k e)) hn
      | false =>
        rw [show K.mul (indR K false) (indR K (eqb h k)) = K.zero from DetChain.zero_mul' K _, K.zero_add]
        exact zero_rest ks (fun hm => hn (Or.inr hm))

/-- **The defect weight of a crossing plaquette is a Gram kernel**:
    `a + b [g = h] = a·1·1 + Σ_k b [g = k][h = k]`, with coefficients `a, b`. -/
theorem defect_gram {G : Type} (eqb : G → G → Bool) (sound : ∀ a b, eqb a b = true → a = b)
    (refl : ∀ a, eqb a a = true) (symm : ∀ a b, eqb a b = eqb b a) (a b : R) (g h : G) (Gs : List G)
    (hd : distinctL Gs) (hg : memL g Gs) :
    ker K ((a, fun _ => K.one) :: Gs.map (fun k => (b, fun x => indR K (eqb x k)))) g h
      = K.add a (K.mul b (indR K (eqb g h))) := by
  show K.add (K.mul (K.mul a K.one) K.one) (sumK K _ (Gs.map _)) = _
  rw [sumK_map, DetChain.CRing.mul_comm K a K.one, DetChain.CRing.one_mul, DetChain.CRing.mul_comm K a K.one,
    DetChain.CRing.one_mul, ← delta_gram K eqb sound refl symm g h Gs hd hg, mul_sumK]
  congr 1
  exact sumK_ext K _ _ (fun k => DetChain.CRing.mul_assoc K _ _ _) Gs

end RP

/-! ## Transfer matrix: positive entries -/

/-- `Π_i q^(n_i)` with `q ≥ 1`: every entry of the transfer matrix (weight of a
    time step, written as `q` to the number of non-defective plaquettes) is ≥ 1. -/
def prodPow (q : Nat) : List Nat → Nat
  | [] => 1
  | n :: ns => q ^ n * prodPow q ns

theorem pow_pos' (q : Nat) (hq : 0 < q) : ∀ n, 0 < q ^ n
  | 0 => Nat.zero_lt_succ 0
  | n + 1 => Nat.mul_le_mul (pow_pos' q hq n) hq

theorem transfer_pos (q : Nat) (hq : 0 < q) : ∀ ns, 0 < prodPow q ns
  | [] => Nat.zero_lt_succ 0
  | n :: ns => Nat.mul_le_mul (pow_pos' q hq n) (transfer_pos q hq ns)

/-! ## Counting surfaces -/

/-- All words of length `n` over the letters `as`. -/
def consAll {α : Type} : List α → List (List α) → List (List α)
  | [], _ => []
  | a :: as, ws => ws.map (List.cons a) ++ consAll as ws

def words {α : Type} (as : List α) : Nat → List (List α)
  | 0 => [[]]
  | n + 1 => consAll as (words as n)

theorem len_app2 {β : Type} : ∀ a b : List β, (a ++ b).length = a.length + b.length
  | [], b => (Nat.zero_add b.length).symm
  | _ :: a, b => by
    show (a ++ b).length + 1 = a.length + 1 + b.length
    rw [len_app2 a b, Nat.add_right_comm]

theorem len_map2 {β γ : Type} (f : β → γ) : ∀ a : List β, (a.map f).length = a.length
  | [] => rfl
  | _ :: a => congrArg (· + 1) (len_map2 f a)

theorem consAll_length {α : Type} (ws : List (List α)) : ∀ as : List α, (consAll as ws).length = as.length * ws.length
  | [] => (nzero_mul _).symm
  | _ :: as => by
    show (ws.map _ ++ consAll as ws).length = (as.length + 1) * ws.length
    rw [len_app2, len_map2, consAll_length ws as, nsucc_mul, nadd_comm]

/-- **There are `k^n` words of length `n` over `k` letters.** -/
theorem words_length {α : Type} (as : List α) : ∀ n, (words as n).length = as.length ^ n
  | 0 => rfl
  | n + 1 => by
    show (consAll as (words as n)).length = as.length ^ n * as.length
    rw [consAll_length, words_length as n, nmul_comm]

/-- **Surface count.** A connected plaquette surface of area `A` is coded by a
    depth-first walk on its plaquettes: `2A` letters, each a neighbour direction
    (`D` of them) or a step back. There are `(D+1)^(2A) = ((D+1)^2)^A` codes. -/
theorem surface_count {α : Type} (dirs : List α) (back : α) (A : Nat) :
    (words (back :: dirs) (A + A)).length = ((dirs.length + 1) * (dirs.length + 1)) ^ A := by
  rw [words_length]
  show (dirs.length + 1) ^ (A + A) = _
  exact pow_double (dirs.length + 1) A
where
  pow_double (k : Nat) : ∀ A, k ^ (A + A) = (k * k) ^ A
    | 0 => rfl
    | A + 1 => by
      rw [← Nat.add_assoc (A + 1) A 1, Nat.add_right_comm A 1 A]
      show k ^ (A + A) * k * k = (k * k) ^ A * (k * k)
      rw [pow_double k A, nmul_assoc]

/-! ## The geometric bound: decay independent of L -/

/-- `T(N) = Σ_{j ≤ N} c^j q^(N-j)`. -/
def geomT (c q : Nat) : Nat → Nat
  | 0 => 1
  | N + 1 => q * geomT c q N + c ^ (N + 1)

/-- **Telescoping**: `q T(N) + c^(N+1) = q^(N+1) + c T(N)`. -/
theorem geom_telescope (c q : Nat) : ∀ N, q * geomT c q N + c ^ (N + 1) = q ^ (N + 1) + c * geomT c q N
  | 0 => by
    show q * 1 + 1 * c = 1 * q + c * 1
    rw [nmul_one, nmul_one, Nat.one_mul, Nat.one_mul]
  | N + 1 => by
    have ih := geom_telescope c q N
    show q * (q * geomT c q N + c ^ (N + 1)) + c ^ (N + 1) * c
      = q ^ (N + 1) * q + c * (q * geomT c q N + c ^ (N + 1))
    have L : q * (q * geomT c q N + c ^ (N + 1)) + c ^ (N + 1) * c
        = q ^ (N + 1) * q + c * (q * geomT c q N) + c * c ^ (N + 1) := by
      rw [ih, nleft_distrib, nmul_comm q (q ^ (N + 1)), nmul_comm (c ^ (N + 1)) c, ← nmul_assoc q c,
        nmul_comm q c, nmul_assoc c q]
    rw [L, nleft_distrib c (q * geomT c q N) (c ^ (N + 1)), nadd_assoc]

theorem le_cancel {a b : Nat} (h : a + a ≤ b + a) : a ≤ b := by
  cases Nat.lt_or_ge b a with
  | inl hlt => exact absurd h (Nat.not_le_of_lt (Nat.add_lt_add_right hlt a))
  | inr hge => exact hge

/-- **Geometric bound**: if `2c ≤ q` then `T(N) ≤ 2 q^N`. -/
theorem geom_bound (c q : Nat) (hq : 0 < q) (hc : 2 * c ≤ q) (N : Nat) : geomT c q N ≤ 2 * q ^ N := by
  have tel := geom_telescope c q N
  have h1 : q * geomT c q N ≤ q ^ (N + 1) + c * geomT c q N :=
    tel ▸ Nat.le_add_right (q * geomT c q N) (c ^ (N + 1))
  have h2 : 2 * (q * geomT c q N) ≤ 2 * q ^ (N + 1) + q * geomT c q N := by
    have a := Nat.mul_le_mul_left 2 h1
    rw [nleft_distrib, ← nmul_assoc 2 c] at a
    exact Nat.le_trans a (Nat.add_le_add (Nat.le_refl _) (Nat.mul_le_mul_right _ hc))
  rw [Nat.two_mul (q * geomT c q N)] at h2
  have h4 : q * geomT c q N ≤ 2 * q ^ (N + 1) := le_cancel h2
  have e : q * (2 * q ^ N) = 2 * q ^ (N + 1) := by
    rw [← nmul_assoc, nmul_comm q 2, nmul_assoc, nmul_comm q (q ^ N)]
    rfl
  have h3 : q * geomT c q N ≤ q * (2 * q ^ N) := by
    rw [e]
    exact h4
  exact Nat.le_of_mul_le_mul_left h3 hq

/-- **Decay with a rate independent of L.** If a correlation at distance `t` is
    bounded by a sum over surfaces of area `A ≥ t` with at most `c^A` surfaces of
    area `A`, each of weight `ε^A = q^(-A)`, then (multiplying by `q^(t+N)`)
    `Σ_{A=t}^{t+N} c^A q^(t+N-A) ≤ 2 c^t q^N`, i.e. the sum is `≤ 2 (c/q)^t`.
    Nothing here depends on the lattice size. -/
theorem decay_bound (c q t : Nat) (hq : 0 < q) (hc : 2 * c ≤ q) (N : Nat) :
    c ^ t * geomT c q N ≤ 2 * c ^ t * q ^ N := by
  have h := Nat.mul_le_mul_left (c ^ t) (geom_bound c q hq hc N)
  rw [← nmul_assoc, nmul_comm (c ^ t) 2] at h
  exact h

/-! ## An explicit β₀ -/

/-- In four dimensions a plaquette shares an edge with `D = 20` others; the
    surface constant is `c = (D+1)² = 441`. -/
theorem c_441 : (20 + 1) * (20 + 1) = 441 := rfl

theorem mul_pow' (a b : Nat) : ∀ t, (a * b) ^ t = a ^ t * b ^ t
  | 0 => rfl
  | t + 1 => by
    show (a * b) ^ t * (a * b) = a ^ t * a * (b ^ t * b)
    rw [mul_pow' a b t, mmid4]

/-- **Strong coupling with an explicit β₀.** Let every plaquette of a polymer
    carry activity at most `ε = 1/q`. For the Wilson action `ε ≤ 1 - e^(-2β) ≤ 2β`,
    for the defect action `ε ≤ 1 - e^(-β) ≤ β`. If `q ≥ 882`, i.e. `β ≤ β₀ = 1/1764`
    (Wilson) or `β ≤ 1/882` (defect), then for every `t` and every cut-off `N`:
    the surface sum `Σ_{A=t}^{t+N} 441^A q^(t+N-A)` is at most `2 · 441^t q^N`, and
    `2^t · 441^t ≤ q^t`. Together: the sum is `≤ 2 · 2^(-t)`, a decay rate `≥ ln 2`
    in every finite volume. No lattice size enters. -/
theorem gap_explicit (q : Nat) (hq : 882 ≤ q) (t N : Nat) :
    441 ^ t * geomT 441 q N ≤ 2 * 441 ^ t * q ^ N ∧ 2 ^ t * 441 ^ t ≤ q ^ t := by
  have hq0 : 0 < q := Nat.lt_of_lt_of_le (Nat.zero_lt_succ 881) hq
  refine ⟨decay_bound 441 q t hq0 hq N, ?_⟩
  rw [← mul_pow']
  exact Nat.pow_le_pow_left hq t

/-! ## Perron: the top eigenvalue of a positive matrix is simple -/

section Perron
variable {R : Type} (K : DetChain.CRing R)

/-- An order on the ring, given by its laws. -/
structure ORing where
  le : R → R → Prop
  le_refl : ∀ a, le a a
  le_trans : ∀ a b c, le a b → le b c → le a c
  le_antisymm : ∀ a b, le a b → le b a → a = b
  le_total : ∀ a b, le a b ∨ le b a
  add_le_add : ∀ a b c d, le a b → le c d → le (K.add a c) (K.add b d)
  mul_nonneg : ∀ a b, le K.zero a → le K.zero b → le K.zero (K.mul a b)
  mul_le_mul_right : ∀ a b c, le a b → le K.zero c → le (K.mul a c) (K.mul b c)
  pos_cancel : ∀ a b c, le K.zero c → c ≠ K.zero → le (K.mul a c) (K.mul b c) → le a b
  pos_mul_eq_zero : ∀ a b, le K.zero a → a ≠ K.zero → K.mul a b = K.zero → b = K.zero

variable (O : ORing K)

def Pos (a : R) : Prop := O.le K.zero a ∧ a ≠ K.zero

/-- A minimiser of the ratios `w_i / u_i`, in cross-multiplied form. -/
theorem argmin (u w : Nat → R) (hu : ∀ i, Pos K O (u i)) :
    ∀ (a : Nat) (rest : List Nat), ∃ k, memL k (a :: rest) ∧
      ∀ j, memL j (a :: rest) → O.le (K.mul (w k) (u j)) (K.mul (w j) (u k))
  | a, [] => ⟨a, Or.inl rfl, fun j hj => by
      cases hj with
      | inl h => rw [h]; exact O.le_refl _
      | inr h => exact False.elim h⟩
  | a, b :: rest => by
    obtain ⟨k', hk'm, hk'⟩ := argmin u w hu b rest
    cases O.le_total (K.mul (w a) (u k')) (K.mul (w k') (u a)) with
    | inl hle =>
      refine ⟨a, Or.inl rfl, fun j hj => ?_⟩
      cases hj with
      | inl h => rw [h]; exact O.le_refl _
      | inr h =>
        -- w_a u_j u_k' ≤ w_k' u_a u_j ≤ ... ≤ w_j u_a u_k'
        have h1 := O.mul_le_mul_right _ _ (u j) hle (hu j).1
        have h2 := O.mul_le_mul_right _ _ (u a) (hk' j h) (hu a).1
        apply O.pos_cancel _ _ (u k') (hu k').1 (hu k').2
        have e1 : K.mul (K.mul (w a) (u j)) (u k') = K.mul (K.mul (w a) (u k')) (u j) := by
          rw [K.mul_assoc, K.mul_comm (u j), ← K.mul_assoc]
        have e2 : K.mul (K.mul (w k') (u a)) (u j) = K.mul (K.mul (w k') (u j)) (u a) := by
          rw [K.mul_assoc, K.mul_comm (u a), ← K.mul_assoc]
        have e3 : K.mul (K.mul (w j) (u k')) (u a) = K.mul (K.mul (w j) (u a)) (u k') := by
          rw [K.mul_assoc, K.mul_comm (u k'), ← K.mul_assoc]
        rw [e1]
        exact O.le_trans _ _ _ h1 (e2 ▸ (e3 ▸ h2))
    | inr hge =>
      refine ⟨k', Or.inr hk'm, fun j hj => ?_⟩
      cases hj with
      | inl h => rw [h]; exact hge
      | inr h => exact hk' j h

/-- A sum of nonnegative terms that vanishes has every term zero. -/
theorem sum_zero_each (f : Nat → R) :
    ∀ l : List Nat, (∀ j, memL j l → O.le K.zero (f j)) → sumK K f l = K.zero →
      ∀ j, memL j l → f j = K.zero
  | [], _, _, _, hj => False.elim hj
  | a :: rest, hnn, hs, j, hj => by
    have hrest : O.le K.zero (sumK K f rest) := nn_sum rest (fun i hi => hnn i (Or.inr hi))
    have hs' : K.add (f a) (sumK K f rest) = K.zero := hs
    have ha0 : f a = K.zero := by
      apply O.le_antisymm _ _ _ (hnn a (Or.inl rfl))
      have := O.add_le_add _ _ _ _ (O.le_refl (f a)) hrest
      rw [DetChain.add_zero', hs'] at this
      exact this
    have hr0 : sumK K f rest = K.zero := by
      rw [ha0, K.zero_add] at hs'
      exact hs'
    cases hj with
    | inl h => rw [h]; exact ha0
    | inr h => exact sum_zero_each f rest (fun i hi => hnn i (Or.inr hi)) hr0 j h
where
  nn_sum : ∀ l : List Nat, (∀ j, memL j l → O.le K.zero (f j)) → O.le K.zero (sumK K f l)
    | [], _ => O.le_refl _
    | a :: rest, h => by
      have := O.add_le_add _ _ _ _ (h a (Or.inl rfl)) (nn_sum rest (fun i hi => h i (Or.inr hi)))
      rw [K.zero_add] at this
      exact this

theorem sumK_sub (f g : Nat → R) :
    ∀ l : List Nat, sumK K (fun j => K.add (f j) (K.neg (g j))) l = K.add (sumK K f l) (K.neg (sumK K g l))
  | [] => by
    show K.zero = K.add K.zero (K.neg K.zero)
    rw [K.add_neg]
  | a :: rest => by
    show K.add (K.add (f a) (K.neg (g a))) (sumK K _ rest)
      = K.add (K.add (f a) (sumK K f rest)) (K.neg (K.add (g a) (sumK K g rest)))
    rw [sumK_sub f g rest, neg_add'' (g a) (sumK K g rest), DetChain.add_mid4]
where
  neg_add'' : ∀ x y : R, K.neg (K.add x y) = K.add (K.neg x) (K.neg y) := fun x y => by
    apply Eq.symm
    apply DetChain.neg_unique
    rw [DetChain.add_mid4, DetChain.add_neg', DetChain.add_neg', K.zero_add]

theorem mul_neg_r (a b : R) : K.mul a (K.neg b) = K.neg (K.mul a b) := by
  rw [K.mul_comm, DetChain.neg_mul_left, K.mul_comm]

theorem swap3 (a b c : R) : K.mul a (K.mul b c) = K.mul b (K.mul a c) := by
  rw [← K.mul_assoc, K.mul_comm a b, K.mul_assoc]

/-- **The Perron eigenvalue has a one-dimensional eigenspace.** Let `T` have
    positive entries on the index list, `u` a positive eigenvector with
    eigenvalue `λ`, and `w` any eigenvector with the same eigenvalue. Then `w` is
    proportional to `u`: for some index `k`, `u_k w_j = w_k u_j` for all `j`. -/
theorem perron_simple (T : Nat → Nat → R) (u w : Nat → R) (lam : R) (a : Nat) (rest : List Nat)
    (hT : ∀ i j, Pos K O (T i j)) (hu : ∀ i, Pos K O (u i))
    (heu : ∀ i, sumK K (fun j => K.mul (T i j) (u j)) (a :: rest) = K.mul lam (u i))
    (hew : ∀ i, sumK K (fun j => K.mul (T i j) (w j)) (a :: rest) = K.mul lam (w i)) :
    ∃ k, memL k (a :: rest) ∧ ∀ j, memL j (a :: rest) → K.mul (u k) (w j) = K.mul (w k) (u j) := by
  obtain ⟨k, hkm, hk⟩ := argmin K O u w hu a rest
  refine ⟨k, hkm, ?_⟩
  -- z_j = u_k w_j - w_k u_j ≥ 0
  let z : Nat → R := fun j => K.add (K.mul (u k) (w j)) (K.neg (K.mul (w k) (u j)))
  have hz : ∀ j, memL j (a :: rest) → O.le K.zero (z j) := fun j hj => by
    have h := O.add_le_add _ _ _ _ (hk j hj) (O.le_refl (K.neg (K.mul (w k) (u j))))
    have e1 : K.add (K.mul (w k) (u j)) (K.neg (K.mul (w k) (u j))) = K.zero := K.add_neg _
    have e2 : K.mul (w j) (u k) = K.mul (u k) (w j) := K.mul_comm _ _
    rw [e1, e2] at h
    exact h
  -- Σ_j T_kj z_j = u_k (λ w_k) - w_k (λ u_k) = 0
  have hsum : sumK K (fun j => K.mul (T k j) (z j)) (a :: rest) = K.zero := by
    have e : sumK K (fun j => K.mul (T k j) (z j)) (a :: rest)
        = sumK K (fun j => K.add (K.mul (u k) (K.mul (T k j) (w j)))
            (K.neg (K.mul (w k) (K.mul (T k j) (u j))))) (a :: rest) :=
      sumK_ext K _ _ (fun j => by
        show K.mul (T k j) (K.add (K.mul (u k) (w j)) (K.neg (K.mul (w k) (u j)))) = _
        rw [K.distrib, mul_neg_r K, swap3 K (T k j) (u k) (w j), swap3 K (T k j) (w k) (u j)]) (a :: rest)
    rw [e, sumK_sub, ← mul_sumK, ← mul_sumK, hew k, heu k, swap3 K (u k) lam (w k),
      swap3 K (w k) lam (u k), K.mul_comm (w k) (u k), K.add_neg]
  have each := sum_zero_each K O (fun j => K.mul (T k j) (z j)) (a :: rest)
    (fun j hj => O.mul_nonneg _ _ (hT k j).1 (hz j hj)) hsum
  intro j hj
  have hz0 : z j = K.zero := O.pos_mul_eq_zero _ _ (hT k j).1 (hT k j).2 (each j hj)
  have := DetChain.neg_unique K _ _ hz0
  rw [DetChain.neg_neg'] at this
  exact this

end Perron

/-! ## T27: dominance and a strict gap; Gram symmetry; block spin -/

section Gap
variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- Absolute value and the extra laws used for the gap (an ordered field gives them). -/
structure AbsLaws where
  abs : R → R
  abs_nonneg : ∀ a, O.le K.zero (abs a)
  abs_mul : ∀ a b, abs (K.mul a b) = K.mul (abs a) (abs b)
  abs_add : ∀ a b, O.le (abs (K.add a b)) (K.add (abs a) (abs b))
  abs_of_nonneg : ∀ a, O.le K.zero a → abs a = a
  abs_eq_zero : ∀ a, abs a = K.zero → a = K.zero
  mul_self_nonneg : ∀ a, O.le K.zero (K.mul a a)
  mul_right_cancel : ∀ a b c, c ≠ K.zero → K.mul a c = K.mul b c → a = b

variable (A : AbsLaws K O)

theorem sum_le (f g : Nat → R) : ∀ l : List Nat, (∀ j, memL j l → O.le (f j) (g j)) →
    O.le (sumK K f l) (sumK K g l)
  | [], _ => O.le_refl _
  | a :: rest, h => O.add_le_add _ _ _ _ (h a (Or.inl rfl)) (sum_le f g rest (fun j hj => h j (Or.inr hj)))

theorem abs_sum (f : Nat → R) : ∀ l : List Nat, O.le (A.abs (sumK K f l)) (sumK K (fun j => A.abs (f j)) l)
  | [] => by
    show O.le (A.abs K.zero) K.zero
    rw [A.abs_of_nonneg _ (O.le_refl _)]
    exact O.le_refl _
  | a :: rest => O.le_trans _ _ _ (A.abs_add _ _) (O.add_le_add _ _ _ _ (O.le_refl _) (abs_sum f rest))

/-- A maximiser of `|w_i| / u_i`, cross-multiplied. -/
theorem argmax (u v : Nat → R) (hu : ∀ i, Pos K O (u i)) :
    ∀ (a : Nat) (rest : List Nat), ∃ k, memL k (a :: rest) ∧
      ∀ j, memL j (a :: rest) → O.le (K.mul (v j) (u k)) (K.mul (v k) (u j))
  | a, [] => ⟨a, Or.inl rfl, fun j hj => by
      cases hj with
      | inl h => rw [h]; exact O.le_refl _
      | inr h => exact False.elim h⟩
  | a, b :: rest => by
    obtain ⟨k', hk'm, hk'⟩ := argmax u v hu b rest
    cases O.le_total (K.mul (v k') (u a)) (K.mul (v a) (u k')) with
    | inl hle =>
      refine ⟨a, Or.inl rfl, fun j hj => ?_⟩
      cases hj with
      | inl h => rw [h]; exact O.le_refl _
      | inr h =>
        have h1 := O.mul_le_mul_right _ _ (u a) (hk' j h) (hu a).1
        have h2 := O.mul_le_mul_right _ _ (u j) hle (hu j).1
        apply O.pos_cancel _ _ (u k') (hu k').1 (hu k').2
        have e1 : K.mul (K.mul (v j) (u a)) (u k') = K.mul (K.mul (v j) (u k')) (u a) := by
          rw [K.mul_assoc, K.mul_comm (u a), ← K.mul_assoc]
        have e2 : K.mul (K.mul (v k') (u j)) (u a) = K.mul (K.mul (v k') (u a)) (u j) := by
          rw [K.mul_assoc, K.mul_comm (u j), ← K.mul_assoc]
        have e3 : K.mul (K.mul (v a) (u k')) (u j) = K.mul (K.mul (v a) (u j)) (u k') := by
          rw [K.mul_assoc, K.mul_comm (u k'), ← K.mul_assoc]
        rw [e1]
        exact O.le_trans _ _ _ h1 (e2 ▸ (e3 ▸ h2))
    | inr hge =>
      refine ⟨k', Or.inr hk'm, fun j hj => ?_⟩
      cases hj with
      | inl h => rw [h]; exact hge
      | inr h => exact hk' j h

/-- **Dominance.** For a matrix with positive entries, a positive eigenvector `u`
    (eigenvalue `λ`) and any nonzero eigenvector `w` (eigenvalue `μ`): `|μ| ≤ λ`. -/
theorem perron_dominant (T : Nat → Nat → R) (u w : Nat → R) (lam mu : R) (a : Nat) (rest : List Nat)
    (hT : ∀ i j, Pos K O (T i j)) (hu : ∀ i, Pos K O (u i))
    (heu : ∀ i, sumK K (fun j => K.mul (T i j) (u j)) (a :: rest) = K.mul lam (u i))
    (hew : ∀ i, sumK K (fun j => K.mul (T i j) (w j)) (a :: rest) = K.mul mu (w i))
    (i0 : Nat) (hi0 : memL i0 (a :: rest)) (hw0 : w i0 ≠ K.zero) :
    O.le (A.abs mu) lam := by
  obtain ⟨k, hkm, hk⟩ := argmax K O u (fun j => A.abs (w j)) hu a rest
  -- |w_k| ≠ 0
  have hwk : A.abs (w k) ≠ K.zero := fun h0 => by
    have h := hk i0 hi0
    rw [h0, DetChain.zero_mul' K] at h
    have h' : O.le (K.mul (A.abs (w i0)) (u k)) (K.mul K.zero (u k)) := by
      rw [DetChain.zero_mul' K]; exact h
    have hle := O.pos_cancel _ _ _ (hu k).1 (hu k).2 h'
    exact hw0 (A.abs_eq_zero _ (O.le_antisymm _ _ hle (A.abs_nonneg _)))
  -- |μ| |w_k| ≤ Σ_j T_kj |w_j|
  have s1 : O.le (K.mul (A.abs mu) (A.abs (w k))) (sumK K (fun j => K.mul (T k j) (A.abs (w j))) (a :: rest)) := by
    rw [← A.abs_mul, ← hew k]
    refine O.le_trans _ _ _ (abs_sum K O A _ _) (sum_le K O _ _ _ (fun j _ => ?_))
    rw [A.abs_mul, A.abs_of_nonneg _ (hT k j).1]
    exact O.le_refl _
  -- (Σ_j T_kj |w_j|) u_k ≤ (Σ_j T_kj u_j) |w_k| = λ u_k |w_k|
  have s2 : O.le (K.mul (sumK K (fun j => K.mul (T k j) (A.abs (w j))) (a :: rest)) (u k))
      (K.mul (K.mul lam (u k)) (A.abs (w k))) := by
    rw [← heu k, sumK_mul, sumK_mul]
    refine sum_le K O _ _ _ (fun j hj => ?_)
    show O.le (K.mul (K.mul (T k j) (A.abs (w j))) (u k)) (K.mul (K.mul (T k j) (u j)) (A.abs (w k)))
    rw [K.mul_assoc, K.mul_assoc]
    have := O.mul_le_mul_right _ _ (T k j) (hk j hj) (hT k j).1
    rw [K.mul_comm (K.mul (A.abs (w j)) (u k)) (T k j), K.mul_comm (K.mul (A.abs (w k)) (u j)) (T k j)] at this
    rw [K.mul_comm (u j) (A.abs (w k))]
    exact this
  have s3 := O.le_trans _ _ _ (O.mul_le_mul_right _ _ (u k) s1 (hu k).1) s2
  -- cancel u_k, then |w_k|
  have e : K.mul (K.mul lam (u k)) (A.abs (w k)) = K.mul (K.mul lam (A.abs (w k))) (u k) := by
    rw [K.mul_assoc, K.mul_comm (u k), ← K.mul_assoc]
  rw [e] at s3
  have s4 := O.pos_cancel _ _ _ (hu k).1 (hu k).2 s3
  exact O.pos_cancel _ _ _ (A.abs_nonneg _) hwk s4

/-- `T²` with entries `Σ_k T_ik T_kj`. -/
def sq (T : Nat → Nat → R) (idx : List Nat) (i j : Nat) : R := sumK K (fun k => K.mul (T i k) (T k j)) idx

theorem pos_sum (f : Nat → R) (a : Nat) (rest : List Nat) (h : ∀ j, Pos K O (f j)) :
    Pos K O (sumK K f (a :: rest)) := by
  have nn : ∀ l : List Nat, O.le K.zero (sumK K f l) := fun l =>
    sum_zero_each.nn_sum K O f l (fun j _ => (h j).1)
  refine ⟨nn _, fun h0 => ?_⟩
  exact (h a).2 (sum_zero_each K O f (a :: rest) (fun j _ => (h j).1) h0 a (Or.inl rfl))

theorem sq_eigen (T : Nat → Nat → R) (v : Nat → R) (c : R) (idx : List Nat)
    (hev : ∀ i, sumK K (fun j => K.mul (T i j) (v j)) idx = K.mul c (v i)) (i : Nat) :
    sumK K (fun j => K.mul (sq K T idx i j) (v j)) idx = K.mul (K.mul c c) (v i) := by
  have e1 : sumK K (fun j => K.mul (sq K T idx i j) (v j)) idx
      = sumK K (fun j => sumK K (fun k => K.mul (T i k) (K.mul (T k j) (v j))) idx) idx :=
    sumK_ext K _ _ (fun j => by
      show K.mul (sumK K _ idx) (v j) = _
      rw [sumK_mul]
      exact sumK_ext K _ _ (fun k => K.mul_assoc _ _ _) idx) idx
  rw [e1, sumK_swap]
  have e2 : sumK K (fun k => sumK K (fun j => K.mul (T i k) (K.mul (T k j) (v j))) idx) idx
      = sumK K (fun k => K.mul (T i k) (K.mul c (v k))) idx :=
    sumK_ext K _ _ (fun k => by rw [← mul_sumK, hev k]) idx
  rw [e2]
  have e3 : sumK K (fun k => K.mul (T i k) (K.mul c (v k))) idx = K.mul c (sumK K (fun k => K.mul (T i k) (v k)) idx) := by
    rw [mul_sumK]
    exact sumK_ext K _ _ (fun k => swap3 K _ _ _) idx
  rw [e3, hev i, K.mul_assoc]

/-- **Strict gap on a finite lattice.** With a positive eigenvector `u` for `λ`,
    every eigenvalue `μ ≠ λ` (nonzero eigenvector) satisfies `|μ| ≤ λ` and `|μ| ≠ λ`. -/
theorem perron_gap (T : Nat → Nat → R) (u w : Nat → R) (lam mu : R) (a : Nat) (rest : List Nat)
    (hT : ∀ i j, Pos K O (T i j)) (hu : ∀ i, Pos K O (u i))
    (heu : ∀ i, sumK K (fun j => K.mul (T i j) (u j)) (a :: rest) = K.mul lam (u i))
    (hew : ∀ i, sumK K (fun j => K.mul (T i j) (w j)) (a :: rest) = K.mul mu (w i))
    (i0 : Nat) (hi0 : memL i0 (a :: rest)) (hw0 : w i0 ≠ K.zero) (hne : mu ≠ lam) :
    O.le (A.abs mu) lam ∧ A.abs mu ≠ lam := by
  refine ⟨perron_dominant K O A T u w lam mu a rest hT hu heu hew i0 hi0 hw0, fun habs => hne ?_⟩
  -- μ² = λ²
  have hsq : K.mul mu mu = K.mul lam lam := by
    rw [← A.abs_of_nonneg _ (A.mul_self_nonneg mu), A.abs_mul, habs]
  -- T² is positive, u and w are eigenvectors of T² for λ²
  have hS : ∀ i j, Pos K O (sq K T (a :: rest) i j) := fun i j =>
    pos_sum K O _ a rest (fun k => ⟨O.mul_nonneg _ _ (hT i k).1 (hT k j).1,
      fun h0 => (hT k j).2 (O.pos_mul_eq_zero _ _ (hT i k).1 (hT i k).2 h0)⟩)
  have heu2 := sq_eigen K T u lam (a :: rest) heu
  have hew2 : ∀ i, sumK K (fun j => K.mul (sq K T (a :: rest) i j) (w j)) (a :: rest) = K.mul (K.mul lam lam) (w i) :=
    fun i => (sq_eigen K T w mu (a :: rest) hew i).trans (by rw [hsq])
  obtain ⟨k, _, hk⟩ := perron_simple K O (sq K T (a :: rest)) u w (K.mul lam lam) a rest hS hu heu2 hew2
  -- u_k (μ w_i) = u_k (λ w_i)
  have key : K.mul (u k) (K.mul mu (w i0)) = K.mul (u k) (K.mul lam (w i0)) := by
    rw [← hew i0, mul_sumK]
    have e : sumK K (fun j => K.mul (u k) (K.mul (T i0 j) (w j))) (a :: rest)
        = sumK K (fun j => K.mul (w k) (K.mul (T i0 j) (u j))) (a :: rest) :=
      sumK_ext' (g := fun j => K.mul (u k) (K.mul (T i0 j) (w j)))
        (h := fun j => K.mul (w k) (K.mul (T i0 j) (u j))) (fun j hj => by
        show K.mul (u k) (K.mul (T i0 j) (w j)) = K.mul (w k) (K.mul (T i0 j) (u j))
        rw [swap3 K (u k), hk j hj, ← swap3 K (T i0 j) (w k) (u j)]) (a :: rest) (fun j hj => hj)
    rw [e, ← mul_sumK, heu i0, swap3 K (w k) lam (u i0), ← hk i0 hi0, swap3 K lam (u k) (w i0)]
  have key2 : K.mul (K.mul mu (w i0)) (u k) = K.mul (K.mul lam (w i0)) (u k) := by
    rw [K.mul_comm _ (u k), K.mul_comm _ (u k)]
    exact key
  have key3 := A.mul_right_cancel _ _ _ (hu k).2 key2
  have key4 : K.mul mu (w i0) = K.mul lam (w i0) := key3
  exact A.mul_right_cancel _ _ _ hw0 key4
where
  sumK_ext' {g h : Nat → R} (hgh : ∀ j, memL j (a :: rest) → g j = h j) :
      ∀ l : List Nat, (∀ j, memL j l → memL j (a :: rest)) → sumK K g l = sumK K h l := by
    intro l
    induction l with
    | nil => intro _; rfl
    | cons b l ih =>
      intro hl
      show K.add (g b) (sumK K g l) = K.add (h b) (sumK K h l)
      rw [hgh b (hl b (Or.inl rfl)), ih (fun j hj => hl j (Or.inr hj))]

/-- **Doeblin contraction** (the finite-dimensional form of the Hilbert-metric
    contraction). Write the rows of `P` as `P_ij = r_ij + δ q_j` with `r ≥ 0` and the
    same row mass `Σ_j r_ij = e` for every row. If `m ≤ f ≤ M` then for any two rows
    `(Pf)_i + e·m ≤ (Pf)_k + e·M`: the spread of `Pf` is at most `e` times the
    spread of `f`. For a stochastic `P` (`Σq = 1`) one has `e = 1 - δ`, so `n`
    steps shrink every spread by `(1-δ)^n`: a gap `≥ -ln(1-δ)`. -/
theorem doeblin (P r : Nat → Nat → R) (q f : Nat → R) (δ e M m : R) (idx : List Nat)
    (hP : ∀ i j, P i j = K.add (r i j) (K.mul δ (q j)))
    (hr : ∀ i j, O.le K.zero (r i j))
    (he : ∀ i, sumK K (r i) idx = e)
    (hM : ∀ j, memL j idx → O.le (f j) M) (hm : ∀ j, memL j idx → O.le m (f j))
    (i k : Nat) :
    O.le (K.add (sumK K (fun j => K.mul (P i j) (f j)) idx) (K.mul e m))
         (K.add (sumK K (fun j => K.mul (P k j) (f j)) idx) (K.mul e M)) := by
  have split : ∀ a, sumK K (fun j => K.mul (P a j) (f j)) idx
      = K.add (sumK K (fun j => K.mul (r a j) (f j)) idx) (sumK K (fun j => K.mul (K.mul δ (q j)) (f j)) idx) := by
    intro a
    rw [← sumK_add]
    exact sumK_ext K _ _ (fun j => by
      show K.mul (P a j) (f j) = K.add (K.mul (r a j) (f j)) (K.mul (K.mul δ (q j)) (f j))
      rw [hP a j, K.mul_comm, K.distrib, K.mul_comm (f j), K.mul_comm (f j)]) idx
  -- Σ_j r_aj f_j ≤ e M and e m ≤ Σ_j r_aj f_j
  have up : ∀ a, O.le (sumK K (fun j => K.mul (r a j) (f j)) idx) (K.mul e M) := by
    intro a
    rw [← he a, sumK_mul]
    refine sum_le K O _ _ idx (fun j hj => ?_)
    show O.le (K.mul (r a j) (f j)) (K.mul (r a j) M)
    rw [K.mul_comm (r a j), K.mul_comm (r a j)]
    exact O.mul_le_mul_right _ _ _ (hM j hj) (hr a j)
  have lo : ∀ a, O.le (K.mul e m) (sumK K (fun j => K.mul (r a j) (f j)) idx) := by
    intro a
    rw [← he a, sumK_mul]
    refine sum_le K O _ _ idx (fun j hj => ?_)
    show O.le (K.mul (r a j) m) (K.mul (r a j) (f j))
    rw [K.mul_comm (r a j), K.mul_comm (r a j)]
    exact O.mul_le_mul_right _ _ _ (hm j hj) (hr a j)
  rw [split i, split k]
  -- (A_i + Q) + e m ≤ (A_k + Q) + e M
  have h1 : O.le (K.add (sumK K (fun j => K.mul (r i j) (f j)) idx) (K.mul e m))
      (K.add (K.mul e M) (sumK K (fun j => K.mul (r k j) (f j)) idx)) :=
    O.add_le_add _ _ _ _ (up i) (lo k)
  have h2 := O.add_le_add _ _ _ _ h1 (O.le_refl (sumK K (fun j => K.mul (K.mul δ (q j)) (f j)) idx))
  have eL : ∀ x y z : R, K.add (K.add x z) y = K.add (K.add x y) z := fun x y z => by
    rw [K.add_assoc, K.add_comm z, ← K.add_assoc]
  have eR : ∀ x y z : R, K.add (K.add x y) z = K.add (K.add y z) x := fun x y z => by
    rw [K.add_comm x y, K.add_assoc, K.add_comm x z, ← K.add_assoc]
  rw [eL (sumK K (fun j => K.mul (r i j) (f j)) idx) (sumK K (fun j => K.mul (K.mul δ (q j)) (f j)) idx) (K.mul e m),
    eR (K.mul e M) (sumK K (fun j => K.mul (r k j) (f j)) idx) (sumK K (fun j => K.mul (K.mul δ (q j)) (f j)) idx)] at h2
  exact h2

end Gap

/-- A Gram kernel is symmetric: the transfer matrix built from crossing kernels is symmetric. -/
theorem ker_symm {R X : Type} (K : DetChain.CRing R) (L : List (R × (X → R))) (x y : X) :
    ker K L x y = ker K L y x :=
  sumK_ext K _ _ (fun p => by
    show K.mul (K.mul p.1 (p.2 x)) (p.2 y) = K.mul (K.mul p.1 (p.2 y)) (p.2 x)
    rw [K.mul_assoc, K.mul_comm (p.2 x), ← K.mul_assoc]) L

/-! ### Block spin: one decimation step -/

section Block
variable {G : Type} (K : Grp G) {S : Type} (Λ : Lat S)

/-- Coarse shift: two fine steps. -/
def sh2 (x : S) (μ : D4) : S := Λ.sh (Λ.sh x μ) μ

theorem sh2_comm (x : S) (μ ν : D4) : sh2 Λ (sh2 Λ x μ) ν = sh2 Λ (sh2 Λ x ν) μ := by
  show Λ.sh (Λ.sh (Λ.sh (Λ.sh x μ) μ) ν) ν = Λ.sh (Λ.sh (Λ.sh (Λ.sh x ν) ν) μ) μ
  rw [Λ.comm (Λ.sh (Λ.sh x μ) μ) ν ν |>.symm.symm]
  rw [Λ.comm (Λ.sh x μ) μ ν, Λ.comm x μ ν, Λ.comm (Λ.sh (Λ.sh x ν) μ) μ ν, Λ.comm (Λ.sh x ν) μ ν,
    Λ.comm x ν ν]

/-- The coarse lattice (shifts by two). -/
def coarse : Lat S := ⟨sh2 Λ, sh2_comm Λ⟩

/-- **Decimation**: the coarse link is the product of the two fine links along it. -/
def block (U : S → D4 → G) : S → D4 → G := fun x μ => K.mul (U x μ) (U (Λ.sh x μ) μ)

/-- **Gauge covariance of the block step**: blocking a gauge-transformed field is
    the coarse gauge transform of the blocked field. -/
theorem block_gauge (g : S → G) (U : S → D4 → G) (x : S) (μ : D4) :
    block K Λ (gauge K Λ g U) x μ = gauge K (coarse Λ) g (block K Λ U) x μ := by
  show K.mul (K.mul (K.mul (g x) (U x μ)) (K.inv (g (Λ.sh x μ))))
      (K.mul (K.mul (g (Λ.sh x μ)) (U (Λ.sh x μ) μ)) (K.inv (g (Λ.sh (Λ.sh x μ) μ))))
    = K.mul (K.mul (g x) (K.mul (U x μ) (U (Λ.sh x μ) μ))) (K.inv (g (Λ.sh (Λ.sh x μ) μ)))
  exact conj_mul K _ _ _ _ _

end Block

/-- **Positivity is kept**: the blocked weight `Σ_{U ↦ V} W(U)` of a coarse field is
    positive when every fine weight is positive and the fibre is nonempty. -/
theorem block_weight_pos {X : Type} (W : X → Nat) (hW : ∀ x, 0 < W x) :
    ∀ (x : X) (fibre : List X), 0 < lsum W (x :: fibre)
  | x, _ => Nat.lt_of_lt_of_le (hW x) (Nat.le_add_right _ _)

/-- **Reflection positivity is kept**: an observable of the blocked field is an
    observable `G ∘ B` of the fine field, so `⟨θ(G∘B) · (G∘B)⟩ ≥ 0`. -/
theorem block_rp {R X Y : Type} (K : DetChain.CRing R) (P : Positivity K) (Bl : X → Y) (Gf : Y → R)
    (xs : List X) (L : List (R × (X → R))) (hL : AllNN K P L) :
    P.nn (sumK K (fun x => sumK K (fun y => K.mul (K.mul (Gf (Bl x)) (Gf (Bl y))) (ker K L x y)) xs) xs) :=
  rp_nonneg K P (fun x => Gf (Bl x)) xs L hL

/-! ### The bridge across block steps (strong-coupling range) -/

theorem pow_add_nat (a m : Nat) : ∀ n : Nat, a ^ (m + n) = a ^ m * a ^ n
  | 0 => (Nat.mul_one _).symm
  | n + 1 => by
    show a ^ (m + n) * a = a ^ m * (a ^ n * a)
    rw [pow_add_nat a m n, nmul_assoc]

theorem pow_mul_nat (a : Nat) (b : Nat) : ∀ c, (a ^ b) ^ c = a ^ (b * c)
  | 0 => rfl
  | c + 1 => by
    show (a ^ b) ^ c * a ^ b = a ^ (b * c + b)
    rw [pow_mul_nat a b c, pow_add_nat]

/-- **Decay survives blocking.** In the strong-coupling range (`q ≥ 2`, e.g. `q ≥ 882`
    from `gap_explicit`) a correlation bounded by `2 · q^(-s)` at fine distance `s`
    is, after `n` decimation steps, bounded at coarse distance `t` (fine distance
    `2^n t`) by `2 · (2^(2^n))^(-t)`: the decay rate per coarse unit is at least
    `2^n ln 2`. -/
theorem block_decay (q n t : Nat) (hq : 2 ≤ q) : (2 ^ (2 ^ n)) ^ t ≤ q ^ (2 ^ n * t) := by
  rw [pow_mul_nat]
  exact Nat.pow_le_pow_left hq _

/-- Iterating: `n` block steps from the explicit β₀ keep `2^(2^n t) ≤ 882^(2^n t)`. -/
theorem block_decay_explicit (q n t : Nat) (hq : 882 ≤ q) : (2 ^ (2 ^ n)) ^ t ≤ q ^ (2 ^ n * t) :=
  block_decay q n t (Nat.le_trans (Nat.le_add_left 2 880) hq)

end YMChain

#print axioms YMChain.cancel_left
#print axioms YMChain.inv_unique
#print axioms YMChain.inv_mul_rev
#print axioms YMChain.inv_inv
#print axioms YMChain.conj_mul
#print axioms YMChain.conj_inv
#print axioms YMChain.hol_gauge
#print axioms YMChain.gauge_inverse
#print axioms YMChain.lsum_ext
#print axioms YMChain.action_gauge_inv
#print axioms YMChain.weight_gauge_inv
#print axioms YMChain.torus_comm
#print axioms YMChain.sites_length
#print axioms YMChain.torus_action_gauge_inv
#print axioms YMChain.q8_assoc
#print axioms YMChain.q8_one_mul
#print axioms YMChain.q8_mul_one
#print axioms YMChain.q8_inv_mul
#print axioms YMChain.q8_mul_inv
#print axioms YMChain.mul_is_quaternion
#print axioms YMChain.unit_norm
#print axioms YMChain.q8_noncomm
#print axioms YMChain.defect_class
#print axioms YMChain.wilson_class
#print axioms YMChain.regroup
#print axioms YMChain.interaction
#print axioms YMChain.factor_sum
#print axioms YMChain.sumK_ext
#print axioms YMChain.sumK_add
#print axioms YMChain.mul_sumK
#print axioms YMChain.sumK_mul
#print axioms YMChain.sumK_swap
#print axioms YMChain.rearr
#print axioms YMChain.rp_identity
#print axioms YMChain.sumK_nn
#print axioms YMChain.sumK_nn_list
#print axioms YMChain.rp_nonneg
#print axioms YMChain.sumK_append
#print axioms YMChain.sumK_map
#print axioms YMChain.mul_mid4
#print axioms YMChain.rearr2
#print axioms YMChain.gram_mul
#print axioms YMChain.gram_mul_nn
#print axioms YMChain.delta_gram
#print axioms YMChain.defect_gram
#print axioms YMChain.pow_pos'
#print axioms YMChain.transfer_pos
#print axioms YMChain.len_app2
#print axioms YMChain.len_map2
#print axioms YMChain.consAll_length
#print axioms YMChain.words_length
#print axioms YMChain.surface_count
#print axioms YMChain.geom_telescope
#print axioms YMChain.le_cancel
#print axioms YMChain.geom_bound
#print axioms YMChain.decay_bound
#print axioms YMChain.c_441
#print axioms YMChain.mul_pow'
#print axioms YMChain.gap_explicit
#print axioms YMChain.argmin
#print axioms YMChain.sum_zero_each
#print axioms YMChain.sumK_sub
#print axioms YMChain.mul_neg_r
#print axioms YMChain.swap3
#print axioms YMChain.perron_simple
#print axioms YMChain.sum_le
#print axioms YMChain.abs_sum
#print axioms YMChain.argmax
#print axioms YMChain.perron_dominant
#print axioms YMChain.pos_sum
#print axioms YMChain.sq_eigen
#print axioms YMChain.perron_gap
#print axioms YMChain.ker_symm
#print axioms YMChain.sh2_comm
#print axioms YMChain.block_gauge
#print axioms YMChain.block_weight_pos
#print axioms YMChain.block_rp
#print axioms YMChain.doeblin
#print axioms YMChain.pow_add_nat
#print axioms YMChain.pow_mul_nat
#print axioms YMChain.block_decay
#print axioms YMChain.block_decay_explicit

ym/Decimation.lean

/-
  ym/Decimation — T45, cycle 1 (R7): the block map on character coefficients, split into an
  idempotent part and a contracting part.

  Coefficients a : ℕ → R (a 0 is the trivial character, normalised to 1). Decimation of k
  plaquettes (exact in two dimensions, the decimation half of a Migdal–Kadanoff step in four):
      dec k a r = (a r)^k.
  Read/place pattern: hP = "project to the Haar point" (keep a 0, set the rest to 0) is idempotent,
  and decimation commutes with it (dec_proj). On the complement it contracts:
      pw_le_self      0 ≤ x ≤ 1  ⇒  x^(k+1) ≤ x
      pw_contract     0 ≤ x ≤ θ      ⇒  x^(k+1) ≤ θ^k · x
      dec_contract    if 0 ≤ a r ≤ θ ≤ 1 for every r ≠ 0, then dec (k+1) a r ≤ θ^k · a r for r ≠ 0,
                      and the ball {0 ≤ a r ≤ θ} is mapped into itself (dec_ball)
  NUMERIC_T45.txt: in four dimensions the bond-moving half (β ↦ 4β) undoes this on Wilson data
  for β ≥ 2, and the factor tends to 1 as β → ∞; the contraction is not uniform there.

  Lean 4 core only, proofs by hand, no axioms.
-/
import YMChain

namespace Decimation

open YMChain

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

def pw (x : R) : Nat → R
  | 0 => K.one
  | k + 1 => K.mul (pw x k) x

def dec (k : Nat) (a : Nat → R) (r : Nat) : R := pw K (a r) k

/-- Projection to the Haar point: keep the trivial coefficient. -/
def hP (a : Nat → R) : Nat → R
  | 0 => a 0
  | _ + 1 => K.zero

theorem hP_idem (a : Nat → R) (r : Nat) : hP K (hP K a) r = hP K a r := by
  cases r with
  | zero => rfl
  | succ r => rfl

theorem pw_one (k : Nat) : pw K K.one k = K.one := by
  induction k with
  | zero => rfl
  | succ k ih => show K.mul (pw K K.one k) K.one = K.one; rw [ih, K.one_mul]

theorem pw_zero_succ (k : Nat) : pw K K.zero (k + 1) = K.zero := by
  show K.mul (pw K K.zero k) K.zero = K.zero
  exact DetChain.mul_zero K _

/-- Decimation commutes with the projection. -/
theorem dec_proj (k : Nat) (a : Nat → R) (r : Nat) :
    dec K (k + 1) (hP K a) r = hP K (dec K (k + 1) a) r := by
  cases r with
  | zero => show pw K (a 0) (k + 1) = pw K (a 0) (k + 1); rfl
  | succ r => exact pw_zero_succ K k

theorem pw_nonneg (x : R) (h0 : O.le K.zero x) (h1z : O.le K.zero K.one) : ∀ k, O.le K.zero (pw K x k)
  | 0 => h1z
  | k + 1 => O.mul_nonneg _ _ (pw_nonneg x h0 h1z k) h0

theorem pw_le_one (x : R) (h0 : O.le K.zero x) (h1 : O.le x K.one) (h1z : O.le K.zero K.one) :
    ∀ k, O.le (pw K x k) K.one
  | 0 => O.le_refl _
  | k + 1 => by
    show O.le (K.mul (pw K x k) x) K.one
    have a1 := O.mul_le_mul_right _ _ x (pw_le_one x h0 h1 h1z k) h0
    rw [K.one_mul] at a1
    exact O.le_trans _ _ _ a1 h1

theorem pw_mono (x y : R) (hx : O.le K.zero x) (hxy : O.le x y) (h1z : O.le K.zero K.one) :
    ∀ k, O.le (pw K x k) (pw K y k)
  | 0 => O.le_refl _
  | k + 1 => by
    show O.le (K.mul (pw K x k) x) (K.mul (pw K y k) y)
    have hy : O.le K.zero y := O.le_trans _ _ _ hx hxy
    have s1 := O.mul_le_mul_right _ _ x (pw_mono x y hx hxy h1z k) hx
    have s2 := O.mul_le_mul_right _ _ (pw K y k) hxy (pw_nonneg K O y hy h1z k)
    rw [K.mul_comm x (pw K y k), K.mul_comm y (pw K y k)] at s2
    exact O.le_trans _ _ _ s1 s2

/-- `x^(k+1) ≤ θ^k x` for `0 ≤ x ≤ θ ≤ 1`. -/
theorem pw_contract (x θ : R) (h0 : O.le K.zero x) (hxθ : O.le x θ)
    (h1z : O.le K.zero K.one) (k : Nat) :
    O.le (pw K x (k + 1)) (K.mul (pw K θ k) x) :=
  O.mul_le_mul_right _ _ x (pw_mono K O x θ h0 hxθ h1z k) h0

theorem pw_le_self (x : R) (h0 : O.le K.zero x) (h1 : O.le x K.one) (h1z : O.le K.zero K.one) (k : Nat) :
    O.le (pw K x (k + 1)) x := by
  have := O.mul_le_mul_right _ _ x (pw_le_one K O x h0 h1 h1z k) h0
  rw [K.one_mul] at this
  exact this

/-- **Contraction off the Haar point.** -/
theorem dec_contract (a : Nat → R) (θ : R) (h1z : O.le K.zero K.one)
    (hb : ∀ r, O.le K.zero (a (r + 1)) ∧ O.le (a (r + 1)) θ) (k r : Nat) :
    O.le (dec K (k + 1) a (r + 1)) (K.mul (pw K θ k) (a (r + 1))) :=
  pw_contract K O _ θ (hb r).1 (hb r).2 h1z k

/-- The ball `0 ≤ a r ≤ θ` (r ≠ 0) is mapped into itself. -/
theorem dec_ball (a : Nat → R) (θ : R) (h1z : O.le K.zero K.one)
    (hb : ∀ r, O.le K.zero (a (r + 1)) ∧ O.le (a (r + 1)) θ) (hθ1 : O.le θ K.one) (k r : Nat) :
    O.le K.zero (dec K (k + 1) a (r + 1)) ∧ O.le (dec K (k + 1) a (r + 1)) θ :=
  ⟨pw_nonneg K O _ (hb r).1 h1z (k + 1),
   O.le_trans _ _ _ (pw_le_self K O _ (hb r).1 (O.le_trans _ _ _ (hb r).2 hθ1) h1z k) (hb r).2⟩

end Decimation

#print axioms Decimation.hP_idem
#print axioms Decimation.pw_one
#print axioms Decimation.pw_zero_succ
#print axioms Decimation.dec_proj
#print axioms Decimation.pw_nonneg
#print axioms Decimation.pw_le_one
#print axioms Decimation.pw_mono
#print axioms Decimation.pw_contract
#print axioms Decimation.pw_le_self
#print axioms Decimation.dec_contract
#print axioms Decimation.dec_ball

90.11 L7: the weak–strong bridge

Statement. (Logic, all \(d\).) If each block step lowers the effective coupling by a uniform amount and strong coupling gives decay, every coupling gives decay (BridgeLogic). (2D, exact.) Gluing plaquettes multiplies Schur ratios, so \(k\) blockings give

\[ \mu (\mathrm {block}^k w\cdot \rho )=\lambda ^{4^k}\,I,\qquad r^{4^k}(1+4^k\delta )\le 1\ \text { if } r(1+\delta )\le 1, \]
and every coupling reaches any strong-coupling threshold in an explicit number of steps.

In character form the Migdal recursion is \(a_n\mapsto a_n(p^{b^{d-2}})^{b^2}\), \(a_n=c_n/((n+1)c_0)\); in \(d=2\) it is exact: \(k\) real \(2\times 2\) blockings give the \(k\)-th iterate of \(a\mapsto a^4\) (Migdal.block_exact).

Key steps. Block2D.conv_schur, Block2D.ratio_mul, Flow2D.blockIt_schur, Flow2D.bernoulli, Flow2D.flow_reaches. A quadratic decrement \(x_{k+1}\le x_k-cx_k^2\) of the coupling already gives \(1/x_k\ge 1/x_0+ck\) (Decrement.quad_decay), the form suggested by the measured Migdal–Kadanoff flow: for the MK map (\(b=2\), \(d=4\)) the effective coupling \(\beta _{\rm eff}=-1/\log \rho \) of the radius norm drops by at least \(0.141\) per step for \(\beta _{\rm eff}\in [1.9,21]\) (measured, ym/NUMERIC_R7NORM.txt), while no norm tested contracts uniformly. For the 4D Migdal recursion on \(SU(2)\) the strong-coupling end is now rigorous: fusion multiplicities are Clebsch–Gordan counts \(I(\chi _j\chi _s\chi _t)\in \{0,1\}\) (Fusion), so products keep positive type; on the cone \(\mathrm {Cone}(r)=\{A_0=1,\ 0\le A_n\le (n+1)r^{4n}\}\) products obey \(c_n\le 4r^{3n}\) (ConeBound), one Migdal step maps \(\mathrm {Cone}(2^{-8})\) into \(\mathrm {Cone}(2^{-12})\) and every iterate stays in \(\mathrm {Cone}(2^{-8})\) (MigdalCone.mk_forever), and Wilson weights with \(\beta \le 2^{-32}\) start there (WilsonCone); on the Wilson family the step halves \(a_1\) for \(\beta \le 1/8\) (MigdalStrong). Computer-assisted, kernel-checked: for the Wilson weight at \(\beta =\tfrac 12,1,2,3,4\) the (truncated) Migdal flow enters \(\mathrm {Cone}(1/16)\) after \(3,4,8,12,16\) steps and stays there forever (CertWilson.certified_forever; brackets of fixed-point naturals checked by Nat.ble, soundness in CertSound; data in CertB05–CertB4). Interval certificates (CertWilson.certified_interval: the character coefficients increase with \(t\)) cover every \(\beta \in [0,1]\) (CertCover.cover, CertCover2.cover01, 39 intervals); beyond that, interval widths multiply at each step and only the grid points \(\beta =2,3,4,8,16\) are certified. Open: in \(d=3,4\) the contraction of the real block-spin map (R7) and the uniform decrement (B1); for the Migdal recursion, β between the certified points above \(\tfrac 34\) (needs correlated arithmetic) and above 8.

Lean files.

ym/BridgeLogic.lean

/-
  ym/BridgeLogic — T45, cycle 3 (B): the bridge reduced to one hypothesis on the block map.

  The coupling after k block steps is measured in units of a decrement δ: a level lev k ∈ ℕ.
  Hypothesis (H_dec): every block step lowers the level by at least one until the level is at most
  the strong-coupling threshold t0 (for β < β0 = 1/1764, YMChain.gap_explicit).
    reach_threshold   (H_dec) ⇒ some n ≤ lev 0 + 1 has lev n ≤ t0
    bridge_decay      (H_dec) + strong-coupling decay at every scale whose level is ≤ t0
                      + exactness of blocking (a coarse correlation at distance s is the fine one at
                      distance 2^n s, YMChain.block_rp / block_decay) ⇒ there is n ≤ lev 0 + 1 with
                      Corr(2^n s) · 2^s ≤ 2 for every s, at the original spacing and for every L.
  At fixed β this is a mass ≥ ln 2 / 2^n uniform in L. NUMERIC_T45B.txt measures (H_dec) for the
  Migdal–Kadanoff map of SU(2): δ ≥ 0.025 over β ∈ [1/48, 32]. For the real block-spin map (H_dec) is
  the remaining lemma.

  Lean 4 core only, proofs by hand, no axioms.
-/
import YMChain

namespace BridgeLogic

open YMChain

/-- (H_dec) for a sequence of levels. -/
def Hdec (lev : Nat → Nat) (t0 : Nat) : Prop := ∀ k, lev (k + 1) + 1 ≤ lev k ∨ lev k ≤ t0

theorem reach_aux (lev : Nat → Nat) (t0 : Nat) (h : Hdec lev t0) :
    ∀ m, (∃ n, n ≤ m ∧ lev n ≤ t0) ∨ lev m + m ≤ lev 0
  | 0 => Or.inr (Nat.le_refl _)
  | m + 1 => by
    cases reach_aux lev t0 h m with
    | inl hex =>
      obtain ⟨n, hn, hl⟩ := hex
      exact Or.inl ⟨n, Nat.le_succ_of_le hn, hl⟩
    | inr hm =>
      cases h m with
      | inl hd =>
        -- lev (m+1) + (m+1) = (lev (m+1) + 1) + m ≤ lev m + m ≤ lev 0
        refine Or.inr (Nat.le_trans ?_ hm)
        have e : lev (m + 1) + (m + 1) = lev (m + 1) + 1 + m := by
          rw [Nat.add_assoc, Nat.add_comm 1 m]
        rw [e]
        exact Nat.add_le_add_right hd m
      | inr ht => exact Or.inl ⟨m, Nat.le_succ m, ht⟩

theorem reach_threshold (lev : Nat → Nat) (t0 : Nat) (h : Hdec lev t0) :
    ∃ n, n ≤ lev 0 + 1 ∧ lev n ≤ t0 := by
  cases reach_aux lev t0 h (lev 0 + 1) with
  | inl hex => exact hex
  | inr hc =>
    -- lev (lev 0 + 1) + (lev 0 + 1) ≤ lev 0 is impossible
    exact absurd (Nat.le_trans (Nat.le_add_left (lev 0 + 1) _) hc) (Nat.not_succ_le_self (lev 0))

/-- **The bridge, given (H_dec).** `corr n s` is the connected correlation at block scale `n` and
    coarse distance `s`; `pow2 s` stands for 2^s in the ordered ring. -/
theorem bridge_decay {R : Type} (K : DetChain.CRing R) (O : ORing K)
    (lev : Nat → Nat) (t0 : Nat) (h : Hdec lev t0)
    (corr : Nat → Nat → R) (pow2 : Nat → R) (two : R)
    (hsc : ∀ n, lev n ≤ t0 → ∀ s, O.le (K.mul (corr n s) (pow2 s)) two)
    (hblock : ∀ n s, corr n s = corr 0 (2 ^ n * s)) :
    ∃ n, n ≤ lev 0 + 1 ∧ ∀ s, O.le (K.mul (corr 0 (2 ^ n * s)) (pow2 s)) two := by
  obtain ⟨n, hn, hl⟩ := reach_threshold lev t0 h
  exact ⟨n, hn, fun s => by rw [← hblock n s]; exact hsc n hl s⟩

end BridgeLogic

#print axioms BridgeLogic.reach_aux
#print axioms BridgeLogic.reach_threshold
#print axioms BridgeLogic.bridge_decay

ym/Bridge2D.lean

/-
  ym/Bridge2D — T45, cycle 5 (B, two dimensions): the bridge with an explicit number of steps.

  In two dimensions decimation of 2 × 2 plaquettes is exact: a ↦ a⁴ on every character coefficient
  (BridgeChain.iter4, Decimation.dec_contract: a_k ≤ a^(k+1)). With a = r/(r + d) (r, d ∈ ℕ, d ≥ 1),
  the coefficient falls below any threshold B/A after an explicit number of steps:
    bernoulli      (r + d)^(n+1) ≥ r^(n+1) + (n+1) d r^n
    steps_bound    for d ≥ 1, B ≥ 1 and n + 1 = r A:   r^(n+1) · A ≤ (r + d)^(n+1) · B
  i.e. a^(n+1) ≤ B/A. So from every coupling the two-dimensional flow enters the strong-coupling
  range (BridgeLogic) within r·A steps: (H_dec) holds in d = 2. The strong-coupling decay used by
  BridgeLogic (polymer bound S2, cited in YM_MAP) remains a hypothesis there.

  Lean 4 core only, proofs by hand, no axioms.
-/
import FTAChain

namespace Bridge2D

open FTAChain

theorem pow_succ' (x n : Nat) : x ^ (n + 1) = x ^ n * x := rfl

/-- **Bernoulli in ℕ.** -/
theorem bernoulli (r d : Nat) : ∀ n, r ^ (n + 1) + (n + 1) * d * r ^ n ≤ (r + d) ^ (n + 1)
  | 0 => by
    show 1 * r + 1 * d * 1 ≤ 1 * (r + d)
    rw [nmul_one, nmul_comm 1 r, nmul_comm 1 d, nmul_comm 1 (r + d), nmul_one, nmul_one, nmul_one]
    exact Nat.le_refl _
  | n + 1 => by
    have ih := bernoulli r d n
    have step := Nat.mul_le_mul_right (r + d) ih
    rw [← pow_succ' (r + d) (n + 1)] at step
    refine Nat.le_trans ?_ step
    -- X = r^(n+1), Y = (n+1) d r^n
    have e2 : (n + 1) * d * r ^ n * r = (n + 1) * d * r ^ (n + 1) := by
      rw [nmul_assoc ((n + 1) * d), ← pow_succ']
    have hR : (r ^ (n + 1) + (n + 1) * d * r ^ n) * (r + d)
        = (r ^ (n + 1 + 1) + ((n + 1) * d * r ^ (n + 1) + d * r ^ (n + 1))) + (n + 1) * d * r ^ n * d := by
      rw [nright_distrib, nleft_distrib, nleft_distrib, e2, nmul_comm (r ^ (n + 1)) d]
      show r ^ (n + 1 + 1) + d * r ^ (n + 1) + ((n + 1) * d * r ^ (n + 1) + (n + 1) * d * r ^ n * d) = _
      rw [nadd_assoc, ← nadd_assoc (d * r ^ (n + 1)), nadd_comm (d * r ^ (n + 1)) ((n + 1) * d * r ^ (n + 1)),
        ← nadd_assoc]
    have hL : (n + 1 + 1) * d * r ^ (n + 1) = (n + 1) * d * r ^ (n + 1) + d * r ^ (n + 1) := by
      rw [nsucc_mul (n + 1), nright_distrib]
    rw [hR, hL, ← nadd_assoc]
    exact Nat.le_add_right _ _

/-- **The number of steps.** With `n + 1 = r A`, `d ≥ 1`, `B ≥ 1`: `r^(n+1) A ≤ (r+d)^(n+1) B`. -/
theorem steps_bound (r d A B n : Nat) (hd : 1 ≤ d) (hB : 1 ≤ B) (hn : n + 1 = r * A) :
    r ^ (n + 1) * A ≤ (r + d) ^ (n + 1) * B := by
  -- r^(n+1) A = r^n (r A) = r^n (n+1) ≤ (n+1) d r^n · B ≤ (r+d)^(n+1) B
  have e : r ^ (n + 1) * A = (n + 1) * r ^ n := by
    rw [pow_succ', nmul_assoc, hn, nmul_comm]
  rw [e]
  have h1 : (n + 1) * r ^ n ≤ (n + 1) * d * r ^ n := by
    rw [nmul_assoc, nmul_comm d, ← nmul_assoc]
    have := Nat.mul_le_mul_left ((n + 1) * r ^ n) hd
    rw [nmul_one] at this
    exact this
  have h2 : (n + 1) * d * r ^ n ≤ (r + d) ^ (n + 1) :=
    Nat.le_trans (Nat.le_add_left _ _) (bernoulli r d n)
  have h3 : (r + d) ^ (n + 1) ≤ (r + d) ^ (n + 1) * B := by
    have := Nat.mul_le_mul_left ((r + d) ^ (n + 1)) hB
    rw [nmul_one] at this
    exact this
  exact Nat.le_trans h1 (Nat.le_trans h2 h3)

end Bridge2D

#print axioms Bridge2D.pow_succ'
#print axioms Bridge2D.bernoulli
#print axioms Bridge2D.steps_bound

ym/Block2D.lean

/-
  ym/Block2D — T45, cycle 89 (Lad7 in two dimensions): the exact block-spin flow of 2D gauge theory goes to strong
  coupling from every coupling.

  Convolution of plaquette weights (gluing two plaquettes along a link): (w₁ ⋆ w₂)(g) = μ(h ↦ w₁(h) w₂(h⁻¹ g)).
  With a left-invariant μ satisfying Fubini (swap of the two integrations — true for Haar on continuous functions, H0):
    conv_schur      μ((w₁ ⋆ w₂) ρ_ik) = Σ_j μ(w₁ ρ_ij) μ(w₂ ρ_jk)        (Schur averages multiply)
    ratio_mul       for Schur weights: λ(w₁ ⋆ w₂) = λ(w₁) λ(w₂), z(w₁ ⋆ w₂) = z(w₁) z(w₂)
  Blocking a 2 × 2 square of plaquettes (three convolutions) sends the Schur ratio λ/z ↦ (λ/z)^4; for the heat kernel
  t ↦ 4t. Since 0 ≤ λ/z ≤ 1 for an irreducible character bound, every coupling flows to the strong-coupling region in
  finitely many steps — the 2D analogue of the bridge hypothesis (H_dec) of BridgeLogic, exactly. In 3D/4D the
  corresponding statement (R7, B1-gap) is the open core of T45.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Lad5Cauchy

namespace Block2D

open YMChain LinkIntegral Strip2D

variable {R : Type} (K : DetChain.CRing R) {G : Type} (Gp : Grp G) {ι : Type} [DecidableEq ι]

def conv (μ : (G → R) → R) (w₁ w₂ : G → R) (g : G) : R := μ (fun h => K.mul (w₁ h) (w₂ (Gp.mul (Gp.inv h) g)))

/-- **Schur averages multiply under convolution.** -/
theorem conv_schur (μ : (G → R) → R) (hμ : Lin K μ) (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (hfub : ∀ F : G → G → R, μ (fun g => μ (fun h => F g h)) = μ (fun h => μ (fun g => F g h)))
    (ρ : G → ι → ι → R) (idx : List ι)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (w₁ w₂ : G → R) (i k : ι) :
    μ (fun g => K.mul (conv K Gp μ w₁ w₂ g) (ρ g i k))
      = sumK K (fun j => K.mul (μ (fun h => K.mul (w₁ h) (ρ h i j))) (μ (fun g => K.mul (w₂ g) (ρ g j k)))) idx := by
  unfold conv
  -- μ_g (μ_h w₁(h) w₂(h⁻¹g)) ρ(g) = μ_h w₁(h) μ_g w₂(h⁻¹g) ρ(g) = μ_h w₁(h) μ_g' w₂(g') ρ(h g')
  have e1 : μ (fun g => K.mul (μ (fun h => K.mul (w₁ h) (w₂ (Gp.mul (Gp.inv h) g)))) (ρ g i k))
      = μ (fun h => K.mul (w₁ h) (μ (fun g => K.mul (w₂ (Gp.mul (Gp.inv h) g)) (ρ g i k)))) := by
    rw [hμ.ext _ (fun g => μ (fun h => K.mul (K.mul (w₁ h) (w₂ (Gp.mul (Gp.inv h) g))) (ρ g i k))) (fun g => by
      rw [K.mul_comm, ← hμ.smul]; exact hμ.ext _ _ (fun h => K.mul_comm _ _)), hfub]
    exact hμ.ext _ _ (fun h => by
      rw [← hμ.smul]; exact hμ.ext _ _ (fun g => by rw [K.mul_assoc]))
  rw [e1]
  -- substitute g = h g'
  have e2 : ∀ h, μ (fun g => K.mul (w₂ (Gp.mul (Gp.inv h) g)) (ρ g i k))
      = sumK K (fun j => K.mul (ρ h i j) (μ (fun g => K.mul (w₂ g) (ρ g j k)))) idx := by
    intro h
    rw [← hl (fun g => K.mul (w₂ (Gp.mul (Gp.inv h) g)) (ρ g i k)) h]
    rw [hμ.ext _ (fun g => sumK K (fun j => K.mul (ρ h i j) (K.mul (w₂ g) (ρ g j k))) idx) (fun g => by
      show K.mul (w₂ (Gp.mul (Gp.inv h) (Gp.mul h g))) (ρ (Gp.mul h g) i k) = _
      rw [← Gp.assoc, Gp.inv_mul, Gp.one_mul, hom, mul_sumK]
      exact sumK_ext K _ _ (fun j => by rw [← K.mul_assoc, K.mul_comm (w₂ g), K.mul_assoc]) idx)]
    rw [mu_sum K μ hμ _ idx]
    exact sumK_ext K _ _ (fun j => hμ.smul _ _) idx
  rw [hμ.ext _ _ (fun h => by rw [e2 h, mul_sumK]), mu_sum K μ hμ _ idx]
  refine sumK_ext K _ _ (fun j => ?_) idx
  rw [hμ.ext _ (fun h => K.mul (μ (fun g => K.mul (w₂ g) (ρ g j k))) (K.mul (w₁ h) (ρ h i j))) (fun h => by
    rw [← K.mul_assoc, K.mul_comm]), hμ.smul, K.mul_comm]

/-- **Schur ratios multiply.** -/
theorem ratio_mul (μ : (G → R) → R) (hμ : Lin K μ) (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (hfub : ∀ F : G → G → R, μ (fun g => μ (fun h => F g h)) = μ (fun h => μ (fun g => F g h)))
    (ρ : G → ι → ι → R) (idx : List ι)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i)
    (w₁ w₂ : G → R) (l₁ l₂ : R)
    (h₁ : ∀ i k, μ (fun g => K.mul (w₁ g) (ρ g i k)) = K.mul l₁ (delta K i k))
    (h₂ : ∀ i k, μ (fun g => K.mul (w₂ g) (ρ g i k)) = K.mul l₂ (delta K i k)) (i k : ι) :
    μ (fun g => K.mul (conv K Gp μ w₁ w₂ g) (ρ g i k)) = K.mul (K.mul l₁ l₂) (delta K i k) := by
  rw [conv_schur K Gp μ hμ hl hfub ρ idx hom w₁ w₂ i k]
  rw [sumK_ext K _ _ (fun j => by rw [h₁, h₂])]
  rw [sumK_ext K _ _ (fun j => show K.mul (K.mul l₁ (delta K i j)) (K.mul l₂ (delta K j k))
      = K.mul (K.mul l₁ l₂) (K.mul (delta K i j) (delta K j k)) by
    rw [K.mul_assoc, ← K.mul_assoc (delta K i j), K.mul_comm (delta K i j) l₂, K.mul_assoc l₂, ← K.mul_assoc l₁])]
  rw [← mul_sumK, hsel1]

end Block2D

#print axioms Block2D.conv_schur
#print axioms Block2D.ratio_mul

ym/Flow2D.lean

/-
  ym/Flow2D — T45, cycle 96 (Lad7 in 2D, step 2): the iterated block flow reaches any strong-coupling threshold.

    blockIt        k-fold 2 × 2 blocking of a plaquette weight: w ↦ (w ⋆ w) ⋆ (w ⋆ w)
    blockIt_schur  if μ(w ρ_ik) = λ δ_ik then μ(blockIt k w · ρ_ik) = λ^{4^k} δ_ik
    bernoulli      0 ≤ r, r(1 + δ) ≤ 1, 0 ≤ δ  ⇒  r^N (1 + N δ) ≤ 1
    flow_reaches   so after k blockings the Schur ratio r^{4^k} satisfies r^{4^k}(1 + 4^k δ) ≤ 1: for any strong-coupling
                   threshold u0, the k with 4^k δ u0 ≥ 1 − u0 brings the effective coupling below u0.
  The 2D bridge (L7 in two dimensions) with explicit step count, from Block2D.ratio_mul.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.CenterExp

namespace Flow2D

open YMChain LinkIntegral Strip2D Block2D Indep Geom

variable {R : Type} (K : DetChain.CRing R) {G : Type} (Gp : Grp G) {ι : Type} [DecidableEq ι]

def blockIt (μ : (G → R) → R) (w : G → R) : Nat → G → R
  | 0 => w
  | k + 1 => let v := blockIt μ w k; conv K Gp μ (conv K Gp μ v v) (conv K Gp μ v v)

theorem four_mul (n : Nat) : n * 4 = n + n + n + n := by
  rw [Nat.mul_succ, Nat.mul_succ, Nat.mul_succ, Nat.mul_one]

/-- **Iterated blocking raises the Schur ratio to the power 4^k.** -/
theorem blockIt_schur (μ : (G → R) → R) (hμ : Lin K μ) (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (hfub : ∀ F : G → G → R, μ (fun g => μ (fun h => F g h)) = μ (fun h => μ (fun g => F g h)))
    (ρ : G → ι → ι → R) (idx : List ι)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i)
    (w : G → R) (lam : R) (hw : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k)) :
    ∀ n i k, μ (fun g => K.mul (blockIt K Gp μ w n g) (ρ g i k)) = K.mul (pwR K lam (4 ^ n)) (delta K i k)
  | 0, i, k => by
    show μ (fun g => K.mul (w g) (ρ g i k)) = K.mul (K.mul K.one lam) (delta K i k)
    rw [hw, K.one_mul]
  | n + 1, i, k => by
    have ih := blockIt_schur μ hμ hl hfub ρ idx hom hsel1 w lam hw n
    have c1 := ratio_mul K Gp μ hμ hl hfub ρ idx hom hsel1 (blockIt K Gp μ w n) (blockIt K Gp μ w n) _ _ ih ih
    have c2 := ratio_mul K Gp μ hμ hl hfub ρ idx hom hsel1 _ _ _ _ c1 c1 i k
    show μ (fun g => K.mul (conv K Gp μ (conv K Gp μ (blockIt K Gp μ w n) (blockIt K Gp μ w n))
      (conv K Gp μ (blockIt K Gp μ w n) (blockIt K Gp μ w n)) g) (ρ g i k)) = _
    rw [c2]
    show _ = K.mul (pwR K lam (4 ^ n * 4)) (delta K i k)
    rw [four_mul, GKS.pwR_add, GKS.pwR_add, GKS.pwR_add]
    congr 1
    rw [← K.mul_assoc (K.mul (pwR K lam (4 ^ n)) (pwR K lam (4 ^ n))) (pwR K lam (4 ^ n)) (pwR K lam (4 ^ n))]

/-- **Bernoulli's inequality for a contraction.** -/
theorem bernoulli (O : ORing K) (h1 : O.le K.zero K.one) (r δ : R) (hr : O.le K.zero r) (hδ : O.le K.zero δ)
    (hrd : O.le (K.mul r (K.add K.one δ)) K.one) :
    ∀ N, O.le (K.mul (pwR K r N) (K.add K.one (K.mul (natR K N) δ))) K.one
  | 0 => by
    show O.le (K.mul K.one (K.add K.one (K.mul K.zero δ))) K.one
    rw [K.one_mul, K.mul_comm, DetChain.mul_zero, DetChain.add_zero']
    exact O.le_refl _
  | N + 1 => by
    -- r^{N+1}(1 + (N+1)δ) ≤ r^{N+1}(1 + Nδ)(1 + δ) = [r^N(1 + Nδ)] [r(1 + δ)] ≤ 1
    have ih := bernoulli O h1 r δ hr hδ hrd N
    have hpN : O.le K.zero (pwR K r N) := AreaLower.pw_nn K O r hr h1 N
    have hNd : O.le K.zero (K.mul (natR K N) δ) := O.mul_nonneg _ _ (natR_nn K O h1 N) hδ
    -- (1 + Nδ)(1 + δ) = 1 + (N+1)δ + Nδ²  ≥ 1 + (N+1)δ
    have expand : K.mul (K.add K.one (K.mul (natR K N) δ)) (K.add K.one δ)
        = K.add (K.add K.one (K.mul (natR K (N + 1)) δ)) (K.mul (K.mul (natR K N) δ) δ) := by
      show _ = K.add (K.add K.one (K.mul (K.add (natR K N) K.one) δ)) (K.mul (K.mul (natR K N) δ) δ)
      let A := K.mul (natR K N) δ
      have l1 : K.mul (K.add K.one A) (K.add K.one δ) = K.add (K.add K.one δ) (K.add A (K.mul A δ)) := by
        rw [DetChain.right_distrib K K.one A (K.add K.one δ), K.one_mul, K.distrib A K.one δ,
          K.mul_comm A K.one, K.one_mul]
      have r1 : K.mul (K.add (natR K N) K.one) δ = K.add A δ := by
        rw [DetChain.right_distrib K (natR K N) K.one δ, K.one_mul]
      rw [l1, r1, K.add_assoc, K.add_assoc, K.add_assoc, ← K.add_assoc δ, K.add_comm δ A, K.add_assoc A]
    have ge : O.le (K.add K.one (K.mul (natR K (N + 1)) δ))
        (K.mul (K.add K.one (K.mul (natR K N) δ)) (K.add K.one δ)) := by
      rw [expand]
      have := O.add_le_add _ _ _ _ (O.le_refl (K.add K.one (K.mul (natR K (N + 1)) δ))) (O.mul_nonneg _ _ hNd hδ)
      rw [DetChain.add_zero'] at this; exact this
    have hpN1 : O.le K.zero (pwR K r (N + 1)) := AreaLower.pw_nn K O r hr h1 (N + 1)
    have s1 := O.mul_le_mul_right _ _ _ ge hpN1
    rw [K.mul_comm _ (pwR K r (N + 1)), K.mul_comm _ (pwR K r (N + 1))] at s1
    refine O.le_trans _ _ _ s1 ?_
    -- r^{N+1} (1+Nδ)(1+δ) = [r^N (1+Nδ)] [r (1+δ)]
    have eq : K.mul (pwR K r (N + 1)) (K.mul (K.add K.one (K.mul (natR K N) δ)) (K.add K.one δ))
        = K.mul (K.mul (pwR K r N) (K.add K.one (K.mul (natR K N) δ))) (K.mul r (K.add K.one δ)) := by
      show K.mul (K.mul (pwR K r N) r) _ = _
      rw [K.mul_assoc, K.mul_assoc, ← K.mul_assoc r, K.mul_comm r (K.add K.one _), K.mul_assoc]
    rw [eq]
    have hrd0 : O.le K.zero (K.mul r (K.add K.one δ)) := by
      have h1d : O.le K.zero (K.add K.one δ) := by
        have := O.add_le_add _ _ _ _ h1 hδ; rw [K.zero_add] at this; exact this
      exact O.mul_nonneg _ _ hr h1d
    have t1 := O.mul_le_mul_right _ _ _ ih hrd0
    rw [K.one_mul] at t1
    exact O.le_trans _ _ _ t1 hrd

/-- **The 2D block flow reaches strong coupling:** after n blockings the Schur ratio r^{4^n} obeys
  r^{4^n} (1 + 4^n δ) ≤ 1. -/
theorem flow_reaches (O : ORing K) (h1 : O.le K.zero K.one) (r δ : R) (hr : O.le K.zero r) (hδ : O.le K.zero δ)
    (hrd : O.le (K.mul r (K.add K.one δ)) K.one) (n : Nat) :
    O.le (K.mul (pwR K r (4 ^ n)) (K.add K.one (K.mul (natR K (4 ^ n)) δ))) K.one :=
  bernoulli K O h1 r δ hr hδ hrd (4 ^ n)

end Flow2D

#print axioms Flow2D.blockIt_schur
#print axioms Flow2D.bernoulli
#print axioms Flow2D.flow_reaches

ym/Decrement.lean

/-
  ym/Decrement — T45, cycle 107 (B1 form of R7e): a quadratic decrement of the coupling gives algebraic decay.

  The measured MK data (R7e, NUMERIC_R7NORM) show a per-step factor 1 − c x for the radius x: x_{k+1} ≤ x_k − c x_k².
  In any ordered ring, with 0 ≤ c x_k ≤ 1:
    key_ineq     t ≤ A, 0 ≤ B ≤ 1, 0 ≤ A  ⇒  t + A B ≤ A + B t + B (B A)
    quad_decay   x_k (1 + k c x_0) ≤ x_0 for every k
  i.e. 1/x_k ≥ 1/x_0 + c k: a uniform decrement of the inverse coupling per block step — exactly hypothesis (H_dec) of
  BridgeLogic in the variable 1/x. So R7e's shape (no uniform contraction, factor → 1) is still enough for the bridge if
  the quadratic decrement holds for the real block-spin map; that inequality is the remaining lemma (B1-gap).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.SpecGap

namespace Decrement

open YMChain LinkIntegral Signed Indep Geom

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem add_cancel_r (a b c : R) (h : O.le (K.add a c) (K.add b c)) : O.le a b := by
  have := O.add_le_add _ _ _ _ h (O.le_refl (K.neg c))
  rw [K.add_assoc, K.add_assoc, K.add_neg, DetChain.add_zero', DetChain.add_zero'] at this
  exact this

/-- `t + A B ≤ A + B t + B (B A)` for `t ≤ A`, `0 ≤ B ≤ 1`, `0 ≤ A`. -/
theorem key_ineq (A B t : R) (hB0 : O.le K.zero B) (hB1 : O.le B K.one) (ht : O.le t A) (hA : O.le K.zero A) :
    O.le (K.add t (K.mul A B)) (K.add (K.add A (K.mul B t)) (K.mul B (K.mul B A))) := by
  have h1B : O.le K.zero (K.add K.one (K.neg B)) := by
    have := O.add_le_add _ _ _ _ hB1 (O.le_refl (K.neg B)); rw [K.add_neg] at this; exact this
  have m := O.mul_le_mul_right _ _ _ ht h1B
  -- t(1 − B) + (B t + A B) ≤ A(1 − B) + (B t + A B)
  have s := O.add_le_add _ _ _ _ m (O.le_refl (K.add (K.mul B t) (K.mul A B)))
  have eL : K.add (K.mul t (K.add K.one (K.neg B))) (K.add (K.mul B t) (K.mul A B)) = K.add t (K.mul A B) := by
    rw [K.distrib, K.mul_comm t K.one, K.one_mul, ← K.add_assoc, K.add_assoc t, mul_neg_r', K.mul_comm t B,
      K.add_comm (K.neg (K.mul B t)), K.add_neg, DetChain.add_zero']
  have eR : K.add (K.mul A (K.add K.one (K.neg B))) (K.add (K.mul B t) (K.mul A B)) = K.add A (K.mul B t) := by
    rw [K.distrib, K.mul_comm A K.one, K.one_mul, K.add_comm (K.mul B t) (K.mul A B), ← K.add_assoc,
      K.add_assoc A, mul_neg_r', K.add_comm (K.neg (K.mul A B)), K.add_neg, DetChain.add_zero']
  rw [eL, eR] at s
  have nn : O.le K.zero (K.mul B (K.mul B A)) := O.mul_nonneg _ _ hB0 (O.mul_nonneg _ _ hB0 hA)
  have := O.add_le_add _ _ _ _ s nn
  rw [DetChain.add_zero'] at this
  exact this
where
  mul_neg_r' (x y : R) : K.mul x (K.neg y) = K.neg (K.mul x y) := by
    rw [K.mul_comm, neg_mul' K, K.mul_comm]

def D (c x0 : R) (k : Nat) : R := K.add K.one (K.mul (natR K k) (K.mul c x0))

/-- **Quadratic decrement ⇒ 1/x_k ≥ 1/x_0 + c k.** -/
theorem quad_decay (h1 : O.le K.zero K.one) (x : Nat → R) (c : R) (hc : O.le K.zero c)
    (hx : ∀ k, O.le K.zero (x k)) (hcx : ∀ k, O.le (K.mul c (x k)) K.one)
    (hstep : ∀ k, O.le (K.add (x (k + 1)) (K.mul c (K.mul (x k) (x k)))) (x k)) :
    ∀ k, O.le (K.mul (x k) (D K c (x 0) k)) (x 0)
  | 0 => by
    show O.le (K.mul (x 0) (K.add K.one (K.mul K.zero (K.mul c (x 0))))) (x 0)
    rw [K.mul_comm K.zero, DetChain.mul_zero, DetChain.add_zero', K.mul_comm, K.one_mul]
    exact O.le_refl _
  | k + 1 => by
    have ih := quad_decay h1 x c hc hx hcx hstep k
    let A := x 0
    let B := K.mul c (x k)
    let t := K.mul (x k) (D K c (x 0) k)
    have hD : D K c (x 0) (k + 1) = K.add (D K c (x 0) k) (K.mul c (x 0)) := by
      show K.add K.one (K.mul (K.add (natR K k) K.one) (K.mul c (x 0))) = K.add (K.add K.one _) _
      rw [DetChain.right_distrib, K.one_mul, K.add_assoc]
    have hD0 : O.le K.zero (D K c (x 0) (k + 1)) := by
      have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (natR_nn K O h1 (k + 1)) (O.mul_nonneg _ _ hc (hx 0)))
      rw [K.zero_add] at this; exact this
    -- (x_{k+1} + c x_k²) D' ≤ x_k D'
    have m := O.mul_le_mul_right _ _ _ (hstep k) hD0
    rw [DetChain.right_distrib] at m
    -- x_k D' = t + A B,  c x_k² D' = B t + B (B A)
    have e1 : K.mul (x k) (D K c (x 0) (k + 1)) = K.add t (K.mul A B) := by
      show _ = K.add (K.mul (x k) (D K c (x 0) k)) (K.mul (x 0) (K.mul c (x k)))
      rw [hD, K.distrib]
      congr 1
      rw [K.mul_comm (x k), K.mul_assoc, K.mul_comm (x 0) (K.mul c (x k)), K.mul_assoc, K.mul_comm (x k) (x 0)]
    have e2 : K.mul (K.mul c (K.mul (x k) (x k))) (D K c (x 0) (k + 1))
        = K.add (K.mul B t) (K.mul B (K.mul B A)) := by
      show _ = K.add (K.mul (K.mul c (x k)) (K.mul (x k) (D K c (x 0) k)))
        (K.mul (K.mul c (x k)) (K.mul (K.mul c (x k)) (x 0)))
      rw [← K.mul_assoc c (x k) (x k), K.mul_assoc (K.mul c (x k)) (x k), e1]
      show K.mul (K.mul c (x k)) (K.add (K.mul (x k) (D K c (x 0) k)) (K.mul (x 0) (K.mul c (x k)))) = _
      rw [K.distrib, K.mul_comm (x 0) (K.mul c (x k))]
    rw [e1, e2] at m
    have kq := key_ineq K O A B t (O.mul_nonneg _ _ hc (hx k)) (hcx k) ih (hx 0)
    have c2 := O.le_trans _ _ _ m kq
    rw [K.add_assoc A] at c2
    exact add_cancel_r K O _ _ _ c2

end Decrement

#print axioms Decrement.key_ineq
#print axioms Decrement.quad_decay

ym/Migdal.lean

/-
  ym/Migdal — T45, S7: one block-spin step in character form (Migdal–Kadanoff), exact in two dimensions.

  For a polynomial class weight p (in y = tr U) the character coefficients are c_n(p) = I(p χ_n) (S2/S4, coeff_chi) and
  the normalised coefficients a_n(p) = c_n(p) / ((n+1) c_0(p)). The Migdal recursion with scale factor b in d dimensions:
      bond moving   p ↦ p^{b^{d−2}}          (product of class polynomials, powL)
      decimation    a_n ↦ a_n^{b²}
    migdal b d p n := a_n(p^{b^{d−2}})^{b²}
    migdal_2d      in d = 2 bond moving is trivial: migdal b 2 p n = a_n(p)^{b²}
    total_mul      the trivial representation: μ(w₁ ⋆ w₂) = μ(w₁) μ(w₂)
    block_exact    for the real 2 × 2 blocking of the 2D lattice (Block2D.conv glued four times), every irreducible ρ
                   with Schur value λ (μ(w ρ) = λ I) and z = μ(w) ≠ 0: after k blockings the normalised coefficient is
                   λ^{4^k} / z^{4^k} = (λ/z)^{4^k} — the Migdal recursion with b = 2 iterated k times, exactly.
  In d = 3, 4 the bond-moving step is an approximation; its coefficient inequalities are the next target (b1q).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.InfVol4D
import ym.SU2Wilson2D

namespace Migdal

open YMChain LinkIntegral Strip2D Block2D Flow2D SU2Haar SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- powers of a class polynomial -/
def powL (p : List R) : Nat → List R
  | 0 => [K.one]
  | 1 => p
  | k + 2 => mulL K (powL p (k + 1)) p

/-- character coefficient c_n(p) = I(p χ_n) -/
def coef (p : List R) (n : Nat) : R := I K (mulL K p (chiC K n))

/-- normalised coefficient a_n = c_n / ((n+1) c_0) -/
def anorm (p : List R) (n : Nat) : R := K.mul (coef K p n) (F.inv (K.mul (cN K (n + 1)) (coef K p 0)))

/-- **The Migdal recursion** (bond moving by b^{d−2}, decimation by b²). -/
def migdal (b d : Nat) (p : List R) (n : Nat) : R := pwR K (anorm K O F (powL K p (b ^ (d - 2))) n) (b ^ 2)

/-- **In two dimensions bond moving is trivial.** -/
theorem migdal_2d (b : Nat) (p : List R) (n : Nat) : migdal K O F b 2 p n = pwR K (anorm K O F p n) (b ^ 2) := rfl

/-- iterating the 2D decimation a ↦ a⁴ -/
def iter4 (a : R) : Nat → R
  | 0 => a
  | k + 1 => pwR K (iter4 a k) 4

theorem pw_pw (a : R) (m : Nat) : ∀ n, pwR K (pwR K a m) n = pwR K a (m * n)
  | 0 => rfl
  | n + 1 => by
    show K.mul (pwR K (pwR K a m) n) (pwR K a m) = pwR K a (m * n + m)
    rw [pw_pw a m n, GKS.pwR_add]

theorem iter4_eq (a : R) : ∀ k, iter4 K a k = pwR K a (4 ^ k)
  | 0 => by show a = K.mul K.one a; rw [K.one_mul]
  | k + 1 => by show pwR K (iter4 K a k) 4 = pwR K a (4 ^ k * 4); rw [iter4_eq a k, pw_pw]

section Group
variable {G : Type} (Gp : Grp G)

/-- the trivial representation -/
def triv : G → Unit → Unit → R := fun _ _ _ => K.one

/-- **Total integrals multiply under convolution.** -/
theorem total_blockIt (μ : (G → R) → R) (hμ : Lin K μ) (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (hfub : ∀ F' : G → G → R, μ (fun g => μ (fun h => F' g h)) = μ (fun h => μ (fun g => F' g h)))
    (w : G → R) (k : Nat) : μ (blockIt K Gp μ w k) = pwR K (μ w) (4 ^ k) := by
  have hom : ∀ (g h : G) (i j : Unit), triv K g i j = sumK K (fun k => K.mul (triv K g i k) (triv K h k j)) [()] := by
    intro g h i j
    show K.one = K.add (K.mul K.one K.one) K.zero
    rw [DetChain.add_zero', K.one_mul]
  have hsel : ∀ (i : Unit) (x : Unit → R), sumK K (fun k => K.mul (delta K i k) (x k)) [()] = x i := by
    intro i x
    cases i
    show K.add (K.mul (delta K () ()) (x ())) K.zero = x ()
    rw [DetChain.add_zero']
    show K.mul (if () = () then K.one else K.zero) (x ()) = x ()
    rw [MaskSum.ip rfl, K.one_mul]
  have hw : ∀ (i j : Unit), μ (fun g => K.mul (w g) (triv K g i j)) = K.mul (μ w) (delta K i j) := by
    intro i j
    cases i; cases j
    show μ (fun g => K.mul (w g) K.one) = K.mul (μ w) (if () = () then K.one else K.zero)
    rw [MaskSum.ip rfl, K.mul_comm (μ w), K.one_mul]
    exact hμ.ext _ _ (fun g => by rw [K.mul_comm, K.one_mul])
  have b := blockIt_schur K Gp μ hμ hl hfub (triv K) [()] hom hsel w (μ w) hw k () ()
  rw [show delta K () () = K.one from MaskSum.ip rfl, K.mul_comm _ K.one, K.one_mul] at b
  rw [← b]
  exact hμ.ext _ _ (fun g => by show _ = K.mul _ K.one; rw [K.mul_comm, K.one_mul])

/-- **Exactness of the Migdal decimation in 2D.** For an irreducible ρ with Schur value λ and total weight z = μ(w):
  after k real 2 × 2 blockings the Schur value is λ^{4^k}, the total weight z^{4^k}, and the normalised coefficient
  λ^{4^k}/z^{4^k} equals the k-th Migdal iterate (a ↦ a⁴) of a = λ/z. -/
theorem block_exact {ι : Type} [DecidableEq ι] (h1 : O.le K.zero K.one)
    (μ : (G → R) → R) (hμ : Lin K μ) (hl : ∀ (f : G → R) (x : G), μ (fun g => f (Gp.mul x g)) = μ f)
    (hfub : ∀ F' : G → G → R, μ (fun g => μ (fun h => F' g h)) = μ (fun h => μ (fun g => F' g h)))
    (ρ : G → ι → ι → R) (idx : List ι)
    (hom : ∀ g h i j, ρ (Gp.mul g h) i j = sumK K (fun k => K.mul (ρ g i k) (ρ h k j)) idx)
    (hsel1 : ∀ (i : ι) (x : ι → R), sumK K (fun k => K.mul (delta K i k) (x k)) idx = x i)
    (w : G → R) (lam : R) (hw : ∀ i k, μ (fun g => K.mul (w g) (ρ g i k)) = K.mul lam (delta K i k))
    (hz0 : O.le K.zero (μ w)) (hz : μ w ≠ K.zero) (k : Nat) :
    (∀ i j, μ (fun g => K.mul (blockIt K Gp μ w k g) (ρ g i j)) = K.mul (pwR K lam (4 ^ k)) (delta K i j)) ∧
      μ (blockIt K Gp μ w k) = pwR K (μ w) (4 ^ k) ∧
      K.mul (pwR K lam (4 ^ k)) (F.inv (pwR K (μ w) (4 ^ k))) = iter4 K (K.mul lam (F.inv (μ w))) k := by
  refine ⟨fun i j => blockIt_schur K Gp μ hμ hl hfub ρ idx hom hsel1 w lam hw k i j,
    total_blockIt K Gp μ hμ hl hfub w k, ?_⟩
  have pne := SpecGap.pw_ne K O h1 (μ w) hz0 hz (4 ^ k)
  have hinv : F.inv (pwR K (μ w) (4 ^ k)) = pwR K (F.inv (μ w)) (4 ^ k) := by
    refine (SU2CharExp.inv_uniq K O F _ _ pne ?_).symm
    rw [← GKS2.pwR_mul, F.mul_inv _ hz, SU2Strong4D.pw_one]
  rw [hinv, ← GKS2.pwR_mul, iter4_eq]

end Group

end Migdal

#print axioms Migdal.migdal_2d
#print axioms Migdal.total_blockIt
#print axioms Migdal.block_exact

ym/MigdalStrong.lean

/-
  ym/MigdalStrong — T45, b1q (Migdal coefficient inequality, step 1): the 4D Migdal map contracts the fundamental
  coefficient at strong coupling, on the Wilson family.

  On Wilson weights e^{β tr U/2} bond moving by b^{d−2} = 4 (b = 2, d = 4) multiplies β by 4 (e^{a}e^{b} = e^{a+b}), and
  decimation raises the normalised coefficient to the power b² = 4, so the 4D Migdal map on the fundamental coefficient
  is a₁(β) ↦ a₁(4β)⁴ with a₁ = c₁/(2c₀) (S3: c_n = Σ_k t^{n+2k}/(n+2k)! P(n+2k,0,n), t = β/2). With truncations at any
  K + 1 terms:
    gsum_le_two    0 ≤ r ≤ ½ ⇒ Σ_{k<K} r^k ≤ 2
    bS_le          Σ_k t^{n+2k}/(n!k!k!) ≤ t^n Σ_k (t²)^k
    c0_le_two, c1_le, c1_ge   c₀(t) ≤ 2, c₁(T) ≤ 2T, c₁(t) ≥ t  (t², T² ≤ ½)
    migdal4_strong for 16 t ≤ 1 (β ≤ 1/8) and T = 4t:  c₁(T)⁴ · 2c₀(t) ≤ ½ · c₁(t) · (2c₀(T))⁴,
                   i.e. a₁(4β)⁴ ≤ ½ a₁(β): the Migdal map at least halves the fundamental coefficient.
  (At weak coupling the factor tends to 1, R7d/R7e; the remaining b1q target is the middle range.)

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Migdal

namespace MigdalStrong

open YMChain LinkIntegral SU2Haar SU2Char SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

def two : R := K.add K.one K.one

def gsum (r : R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (gsum r n) (pwR K r n)

theorem gsum_exact (r : R) : ∀ n, K.add (K.mul (K.add K.one (K.neg r)) (gsum K r n)) (pwR K r n) = K.one
  | 0 => by show K.add (K.mul _ K.zero) K.one = K.one; rw [DetChain.mul_zero, K.zero_add]
  | n + 1 => by
    have ih := gsum_exact r n
    show K.add (K.mul (K.add K.one (K.neg r)) (K.add (gsum K r n) (pwR K r n))) (K.mul (pwR K r n) r) = K.one
    have e : K.add (K.mul (K.add K.one (K.neg r)) (K.add (gsum K r n) (pwR K r n))) (K.mul (pwR K r n) r)
        = K.add (K.mul (K.add K.one (K.neg r)) (gsum K r n)) (pwR K r n) := by lib_ring
    rw [e, ih]

theorem gsum_nn (h1 : O.le K.zero K.one) (r : R) (hr : O.le K.zero r) : ∀ n, O.le K.zero (gsum K r n)
  | 0 => O.le_refl _
  | n + 1 => by
    have := O.add_le_add _ _ _ _ (gsum_nn h1 r hr n) (AreaLower.pw_nn K O r hr h1 n)
    rw [K.zero_add] at this; exact this

/-- **0 ≤ r ≤ ½ ⇒ Σ_{k<n} r^k ≤ 2.** -/
theorem gsum_le_two (h1 : O.le K.zero K.one) (h r : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (hr : O.le K.zero r) (hrh : O.le r h) (n : Nat) : O.le (gsum K r n) (two K) := by
  -- h · gsum ≤ (1 − r) gsum ≤ 1
  have e := gsum_exact K r n
  have g0 := gsum_nn K O h1 r hr n
  have hr1 : O.le h (K.add K.one (K.neg r)) := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) (O.add_le_add _ _ _ _ hrh (O.le_refl (K.neg r)))
    have e1 : K.add h (K.add r (K.neg r)) = h := by rw [K.add_neg, DetChain.add_zero']
    have e2 : K.add h (K.add h (K.neg r)) = K.add K.one (K.neg r) := by rw [← K.add_assoc, hh]
    rw [e1, e2] at this; exact this
  have s1 := O.mul_le_mul_right _ _ _ hr1 g0
  have s2 : O.le (K.mul (K.add K.one (K.neg r)) (gsum K r n)) K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl (K.mul (K.add K.one (K.neg r)) (gsum K r n))) (AreaLower.pw_nn K O r hr h1 n)
    rw [DetChain.add_zero', e] at this; exact this
  have s3 := O.le_trans _ _ _ s1 s2
  -- gsum = (h + h) gsum ≤ 1 + 1
  have := O.add_le_add _ _ _ _ s3 s3
  have e3 : K.add (K.mul h (gsum K r n)) (K.mul h (gsum K r n)) = gsum K r n := by
    rw [← DetChain.right_distrib, hh, K.one_mul]
  rw [e3] at this; exact this

theorem ne_of_one_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx : O.le K.one x) : x ≠ K.zero :=
  fun e => h10 (O.le_antisymm _ _ (by rw [← e]; exact hx) h1)

theorem inv_le_one (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx : O.le K.one x) :
    O.le (F.inv x) K.one := by
  have x0 : O.le K.zero x := O.le_trans _ _ _ h1 hx
  have hx0 := ne_of_one_le K O h1 h10 x hx
  have := O.mul_le_mul_right _ _ _ hx (F.inv_pos x x0 hx0)
  rw [K.one_mul, F.mul_inv x hx0] at this; exact this

/-- termwise: t^{n+2k}/(n!k!k!) ≤ t^n (t²)^k -/
theorem bnd_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n k : Nat) :
    O.le (bnd K O F t n k) (K.mul (pwR K t n) (pwR K (K.mul t t) k)) := by
  have pf := pos_of_ge_one K O h1 h10 (fact n * fact k * fact k)
    (Nat.mul_le_mul (Nat.mul_le_mul (fact_pos n) (fact_pos k)) (fact_pos k))
  have g1 : O.le K.one (cN K (fact n * fact k * fact k)) := by
    have := cN_mono K O h1 1 _ (Nat.mul_le_mul (Nat.mul_le_mul (fact_pos n) (fact_pos k)) (fact_pos k))
    rw [cN_one] at this; exact this
  have i1 := inv_le_one K O F h1 h10 _ g1
  have pn := AreaLower.pw_nn K O t ht h1 (n + 2 * k)
  have := O.mul_le_mul_right _ _ _ i1 pn
  rw [K.one_mul, K.mul_comm] at this
  have eq : pwR K t (n + 2 * k) = K.mul (pwR K t n) (pwR K (K.mul t t) k) := by
    have e2 : 2 * k = k + k := by lib_nat
    rw [e2, GKS.pwR_add, GKS.pwR_add, GKS2.pwR_mul]
  show O.le (K.mul (pwR K t (n + 2 * k)) (F.inv (cN K (fact n * fact k * fact k)))) _
  rw [eq]; rw [eq] at this; exact this

theorem bS_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n : Nat) :
    ∀ Kk, O.le (bS K O F t n Kk) (K.mul (pwR K t n) (gsum K (K.mul t t) Kk))
  | 0 => by show O.le K.zero (K.mul _ K.zero); rw [DetChain.mul_zero]; exact O.le_refl _
  | Kk + 1 => by
    have := O.add_le_add _ _ _ _ (bS_le h1 h10 t ht n Kk) (bnd_le K O F h1 h10 t ht n Kk)
    show O.le (K.add (bS K O F t n Kk) (bnd K O F t n Kk)) (K.mul (pwR K t n) (K.add (gsum K (K.mul t t) Kk) (pwR K (K.mul t t) Kk)))
    rw [K.distrib]; exact this

/-- `c₀(t) ≤ 2` for t² ≤ ½ -/
theorem c0_le_two (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h t : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (ht : O.le K.zero t) (htt : O.le (K.mul t t) h) (Kk : Nat) :
    O.le (cS K O F t 0 Kk) (two K) := by
  have a := O.le_trans _ _ _ (c_bound K O F h1 h10 t ht 0 Kk) (bS_le K O F h1 h10 t ht 0 Kk)
  rw [show pwR K t 0 = K.one from rfl, K.one_mul] at a
  exact O.le_trans _ _ _ a (gsum_le_two K O h1 h (K.mul t t) hh h0 (O.mul_nonneg _ _ ht ht) htt Kk)

/-- `c₁(T) ≤ 2T` for T² ≤ ½ -/
theorem c1_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h T : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (hT : O.le K.zero T) (hTT : O.le (K.mul T T) h) (Kk : Nat) :
    O.le (cS K O F T 1 Kk) (K.mul T (two K)) := by
  have a := O.le_trans _ _ _ (c_bound K O F h1 h10 T hT 1 Kk) (bS_le K O F h1 h10 T hT 1 Kk)
  rw [show pwR K T 1 = K.mul K.one T from rfl, K.one_mul] at a
  have g := gsum_le_two K O h1 h (K.mul T T) hh h0 (O.mul_nonneg _ _ hT hT) hTT Kk
  have := O.mul_le_mul_right _ _ _ g hT
  rw [K.mul_comm (gsum K (K.mul T T) Kk) T, K.mul_comm (two K) T] at this
  exact O.le_trans _ _ _ a this

/-- `c₁(t) ≥ t` -/
theorem c1_ge (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) :
    ∀ Kk, O.le t (cS K O F t 1 (Kk + 1))
  | 0 => by
    have e : term K O F t 1 0 = t := by
      show K.mul (K.mul (K.mul K.one t) (F.inv (cN K (fact 1)))) (cN K (P 1 0 1)) = t
      have f1 : fact 1 = 1 := rfl
      have p1 : P 1 0 1 = 1 := rfl
      rw [f1, p1, cN_one, ← inv_uniq K O F K.one K.one h10 (K.one_mul K.one), K.one_mul, K.mul_comm, K.one_mul,
        K.mul_comm, K.one_mul]
    show O.le t (K.add K.zero (term K O F t 1 0))
    rw [e, K.zero_add]; exact O.le_refl _
  | Kk + 1 => by
    have := O.add_le_add _ _ _ _ (c1_ge h1 h10 t ht Kk) (term_nn K O F h1 h10 t ht 1 (Kk + 1))
    rw [DetChain.add_zero'] at this; exact this

/-- **The 4D Migdal map halves the fundamental coefficient at strong coupling** (Wilson family, β ≤ 1/8):
  with t = β/2, T = 4t, c's truncated at Kk + 1 terms,  c₁(T)⁴ · 2c₀(t) ≤ ½ · c₁(t) · (2c₀(T))⁴. -/
theorem migdal4_strong (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h t : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (ht : O.le K.zero t)
    (h16 : O.le (K.mul (K.mul (two K) (two K)) (K.mul (K.mul (two K) (two K)) t)) K.one) (Kk : Nat) :
    let T := K.mul (K.mul (two K) (two K)) t
    O.le (K.mul (pwR K (cS K O F T 1 (Kk + 1)) 4) (K.mul (two K) (cS K O F t 0 (Kk + 1))))
      (K.mul (K.mul h (cS K O F t 1 (Kk + 1))) (pwR K (K.mul (two K) (cS K O F T 0 (Kk + 1))) 4)) := by
  intro T
  have t0 : O.le K.zero (two K) := by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
  have t1 : O.le K.one (two K) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) h1; rw [DetChain.add_zero'] at this; exact this
  have T0 : O.le K.zero T := O.mul_nonneg _ _ (O.mul_nonneg _ _ t0 t0) ht
  -- 16 t ≤ 1 ⇒ t ≤ 1, T ≤ 1/4: T², t² ≤ ½
  let s16 := K.mul (K.mul (two K) (two K)) (K.mul (K.mul (two K) (two K)) t)
  have hs0 : O.le K.zero s16 := O.mul_nonneg _ _ (O.mul_nonneg _ _ t0 t0) T0
  -- T ≤ T·4 = 16t·... use: T·T ≤ T·(4T) = (16 t)·T ≤ T ≤ ... ≤ h needs T ≤ h; from 16t ≤ 1: 4T ≤ 1 ⇒ T ≤ ¼ ≤ h
  have T4 : O.le (K.mul (K.mul (two K) (two K)) T) K.one := h16
  have Tle1 : O.le T K.one := by
    have g : O.le T (K.mul (K.mul (two K) (two K)) T) := by
      have := O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ t1 (by
        have := O.mul_le_mul_right _ _ _ t1 t0; rw [K.one_mul] at this; exact this)) T0
      rw [K.one_mul] at this; exact this
    exact O.le_trans _ _ _ g T4
  -- T·T ≤ h: 4T ≤ 1 ⇒ T·T·4 ≤ T ≤ 1 ⇒ (h+h)(h+h)·TT ≤ 1 ... simpler: TT·(2·2) ≤ T·1 ≤ 1, and 2h = 1 ⇒ TT ≤ h·h·... 
  have TT : O.le (K.mul T T) h := by
    -- T·T·(2·2) = T·(4T) ≤ T ≤ 1, and multiplying by h·h: T·T ≤ h·h ≤ h
    have a := O.mul_le_mul_right _ _ _ T4 T0
    rw [K.one_mul] at a
    have b := O.le_trans _ _ _ a Tle1
    have hh0 : O.le K.zero (K.mul h h) := O.mul_nonneg _ _ h0 h0
    have c := O.mul_le_mul_right _ _ _ b hh0
    have e1 : K.mul (K.mul (K.mul (K.mul (two K) (two K)) T) T) (K.mul h h) = K.mul (K.mul T T) (K.mul (K.mul (two K) h) (K.mul (two K) h)) := by lib_ring
    have th : K.mul (two K) h = K.one := by show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
    have e2 : K.mul K.one (K.mul h h) = K.mul h h := K.one_mul _
    have e3 : K.mul (K.mul T T) (K.mul K.one K.one) = K.mul T T := by lib_ring
    rw [e1, th, e2, e3] at c
    refine O.le_trans _ _ _ c ?_
    have hle : O.le h K.one := by
      have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
    have := O.mul_le_mul_right _ _ _ hle h0
    rw [K.one_mul] at this; exact this
  have tt : O.le (K.mul t t) h := by
    have tT : O.le t T := by
      have := O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ t1 (by
        have := O.mul_le_mul_right _ _ _ t1 t0; rw [K.one_mul] at this; exact this)) ht
      rw [K.one_mul] at this; exact this
    have := O.le_trans _ _ _ (O.mul_le_mul_right _ _ _ tT ht) (by
      have := O.mul_le_mul_right _ _ _ tT T0; rw [K.mul_comm t T] at this; exact this)
    exact O.le_trans _ _ _ this TT
  -- bounds
  have c1T := c1_le K O F h1 h10 h T hh h0 T0 TT (Kk + 1)
  have c1T0 := cS_nn K O F h1 h10 T T0 1 (Kk + 1)
  have c0t := c0_le_two K O F h1 h10 h t hh h0 ht tt (Kk + 1)
  have c0T := c0_ge_one K O F h1 h10 T T0 Kk
  have c1t := c1_ge K O F h1 h10 t ht Kk
  -- LHS ≤ (2T)⁴ · 4
  have L1 := SU2Strong4D.pw_mono K O h1 _ _ c1T0 c1T 4
  have L2 : O.le (K.mul (two K) (cS K O F t 0 (Kk + 1))) (K.mul (two K) (two K)) := by
    have := O.mul_le_mul_right _ _ _ c0t t0; rw [K.mul_comm _ (two K), K.mul_comm (two K) (two K)] at this; exact this
  have L := O.le_trans _ _ _ (O.mul_le_mul_right _ _ _ L1 (O.mul_nonneg _ _ t0 (cS_nn K O F h1 h10 t ht 0 (Kk + 1))))
    (by have := O.mul_le_mul_right _ _ _ L2 (AreaLower.pw_nn K O _ (O.mul_nonneg _ _ T0 t0) h1 4)
        rw [K.mul_comm _ (pwR K _ 4), K.mul_comm _ (pwR K _ 4)] at this; exact this)
  -- RHS ≥ h t 2⁴
  have R1 : O.le (pwR K (two K) 4) (pwR K (K.mul (two K) (cS K O F T 0 (Kk + 1))) 4) := by
    apply SU2Strong4D.pw_mono K O h1 _ _ t0
    have := O.mul_le_mul_right _ _ _ c0T t0; rw [K.one_mul, K.mul_comm] at this; exact this
  have R2 : O.le (K.mul h t) (K.mul h (cS K O F t 1 (Kk + 1))) := by
    have := O.mul_le_mul_right _ _ _ c1t h0; rw [K.mul_comm, K.mul_comm _ h] at this; exact this
  have Rb := O.le_trans _ _ _ (O.mul_le_mul_right _ _ _ R2 (AreaLower.pw_nn K O _ t0 h1 4))
    (by have := O.mul_le_mul_right _ _ _ R1 (O.mul_nonneg _ _ h0 (O.le_trans _ _ _ ht c1t))
        rw [K.mul_comm _ (K.mul h _), K.mul_comm _ (K.mul h _)] at this; exact this)
  -- numeric core: (2T)⁴ · 4 ≤ h t 2⁴, i.e. 16384 t⁴ ≤ 8 t
  refine O.le_trans _ _ _ L (O.le_trans _ _ _ ?_ Rb)
  -- (2T)^4·4 = (h·t·2^4) · (s16)^3 · h ... with s16 = 16 t ≤ 1
  have s3 : O.le (pwR K s16 3) K.one := by
    have := SU2Strong4D.pw_mono K O h1 _ _ hs0 h16 3; rw [SU2Strong4D.pw_one] at this; exact this
  have th : K.mul (two K) h = K.one := by show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
  have p4 : ∀ x : R, pwR K x 4 = K.mul (K.mul (K.mul (K.mul K.one x) x) x) x := fun x => rfl
  have p3 : ∀ x : R, pwR K x 3 = K.mul (K.mul (K.mul K.one x) x) x := fun x => rfl
  have core : K.mul (pwR K (K.mul T (two K)) 4) (K.mul (two K) (two K))
      = K.mul (K.mul (K.mul h t) (pwR K (two K) 4)) (K.mul (pwR K s16 3) h) := by
    calc K.mul (pwR K (K.mul T (two K)) 4) (K.mul (two K) (two K))
        = K.mul (K.mul (pwR K (K.mul T (two K)) 4) (K.mul (two K) (two K))) (K.mul (K.mul (two K) h) (K.mul (two K) h)) := by
          rw [th, K.one_mul, K.mul_comm _ K.one, K.one_mul]
      _ = K.mul (K.mul (K.mul h t) (pwR K (two K) 4)) (K.mul (pwR K s16 3) h) := by
          rw [p4, p4, p3]
          dsimp only [T, s16]
          lib_ring
  rw [core]
  have x1 : O.le (K.mul (pwR K s16 3) h) K.one := by
    have hle : O.le h K.one := by
      have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
    have := O.mul_le_mul_right _ _ _ s3 h0
    rw [K.one_mul] at this
    exact O.le_trans _ _ _ this hle
  have rn : O.le K.zero (K.mul (K.mul h t) (pwR K (two K) 4)) :=
    O.mul_nonneg _ _ (O.mul_nonneg _ _ h0 ht) (AreaLower.pw_nn K O _ t0 h1 4)
  have := O.mul_le_mul_right _ _ _ x1 rn
  rw [K.one_mul, K.mul_comm] at this
  exact this

end MigdalStrong

#print axioms MigdalStrong.gsum_le_two
#print axioms MigdalStrong.migdal4_strong

ym/Fusion.lean

/-
  ym/Fusion — T45, b1q step 2: SU(2) fusion multiplicities are nonnegative (Clebsch–Gordan), so bond moving keeps
  character coefficients nonnegative.

    cg j s t       the Clebsch–Gordan count, by the positive recursion cg(j+1, s+1, t) = cg(j, s, t) + δ_{t, s+j+2},
                   cg(0, s, t) = δ_{st}, cg(j+1, 0, t) = δ_{j+1, t}   (χ_{j+1}χ_{s+1} = χ_jχ_s + χ_{s+j+2})
    cg_rec         cg satisfies the character recurrence in j
    fusion         Σ_i (χ_j)_i P(i, s, t) = cg j s t
    triple         I(χ_j χ_s χ_t) = cg j s t ≥ 0
    coef_prod_nn   p = Σ a_j χ_j, q = Σ b_k χ_k with a, b ≥ 0  ⇒  every character coefficient I(p q χ_n) ≥ 0
  So the bond-moving half of the Migdal recursion (p ↦ p^m) preserves positive type in every dimension.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.MigdalStrong

namespace Fusion

open YMChain SU2Haar SU2Char

/-- Clebsch–Gordan count. -/
def cg : Nat → Nat → Nat → Nat
  | 0, s, t => dl s t
  | j + 1, 0, t => dl (j + 1) t
  | j + 1, s + 1, t => cg j s t + dl (s + j + 2) t

/-- **The character recurrence for cg.** -/
theorem cg_rec (j : Nat) : ∀ s t, (cg (j + 2) 0 t + cg j 0 t = cg (j + 1) 1 t) ∧
    cg (j + 2) (s + 1) t + cg j (s + 1) t = cg (j + 1) s t + cg (j + 1) (s + 2) t := by
  intro s t
  constructor
  · show dl (j + 2) t + cg j 0 t = cg j 0 t + dl (0 + j + 2) t
    have e : 0 + j + 2 = j + 2 := by lib_nat
    rw [e]
    cases j with
    | zero => exact Nat.add_comm _ _
    | succ j => exact Nat.add_comm _ _
  · show (cg (j + 1) s t + dl (s + (j + 1) + 2) t) + cg j (s + 1) t = cg (j + 1) s t + (cg j (s + 1) t + dl (s + 1 + j + 2) t)
    have e : s + (j + 1) + 2 = s + 1 + j + 2 := by lib_nat
    rw [e]; lib_nat

/-- **cg vanishes beyond the top of the Clebsch–Gordan range:** cg j s t = 0 for t > j + s. -/
theorem cg_top : ∀ j s t, j + s < t → cg j s t = 0
  | 0, s, t, h => SU2Char.dl_lt s t (by rw [Nat.zero_add] at h; exact h)
  | j + 1, 0, t, h => SU2Char.dl_lt (j + 1) t h
  | j + 1, s + 1, t, h => by
    show cg j s t + dl (s + j + 2) t = 0
    have e : j + 1 + (s + 1) = j + s + 2 := by lib_nat
    rw [e] at h
    have h1 : j + s < t := Nat.lt_trans (Nat.lt_trans (Nat.lt_succ_self _) (Nat.lt_succ_self _)) h
    have e2 : s + j + 2 = j + s + 2 := by lib_nat
    rw [cg_top j s t h1, e2, SU2Char.dl_lt _ t h]

theorem dl_le_one : ∀ s t, dl s t ≤ 1
  | 0, 0 => Nat.le_refl 1
  | 0, _ + 1 => Nat.zero_le 1
  | _ + 1, 0 => Nat.zero_le 1
  | s + 1, t + 1 => dl_le_one s t

theorem dl_ne : ∀ s t, s ≠ t → dl s t = 0
  | 0, 0, h => absurd rfl h
  | 0, _ + 1, _ => rfl
  | _ + 1, 0, _ => rfl
  | s + 1, t + 1, h => dl_ne s t (fun e => h (by rw [e]))

/-- **Multiplicity at most one.** -/
theorem cg_le_one : ∀ j s t, cg j s t ≤ 1
  | 0, s, t => dl_le_one s t
  | j + 1, 0, t => dl_le_one (j + 1) t
  | j + 1, s + 1, t => by
    show cg j s t + dl (s + j + 2) t ≤ 1
    by_cases e : s + j + 2 = t
    · have h : j + s < t := by
        rw [← e]
        have e3 : s + j + 2 = j + s + 1 + 1 := by lib_nat
        rw [e3]; exact Nat.lt_trans (Nat.lt_succ_self _) (Nat.lt_succ_self _)
      rw [cg_top j s t h, Nat.zero_add]; exact dl_le_one _ _
    · rw [dl_ne _ _ e, Nat.add_zero]; exact cg_le_one j s t

variable {R : Type} (K : DetChain.CRing R)

theorem dot_succ (f : Nat → R) : ∀ (m : Nat) (p : List R), dot K (m + 1) p f = dot K m p (fun i => f (i + 1))
  | _, [] => rfl
  | m, c :: p => by
    show K.add (K.mul c (f (m + 1))) (dot K (m + 1 + 1) p f) = K.add (K.mul c (f (m + 1))) (dot K (m + 1) p _)
    rw [dot_succ f (m + 1) p]

/-- `N j s t = Σ_i (χ_j)_i P(i, s, t)` -/
def N (j s t : Nat) : R := dot K 0 (chiC K j) (fun i => cN K (P i s t))

theorem N_rec (j s t : Nat) :
    (K.add (N K (j + 2) 0 t) (N K j 0 t) = N K (j + 1) 1 t) ∧
    K.add (N K (j + 2) (s + 1) t) (N K j (s + 1) t) = K.add (N K (j + 1) s t) (N K (j + 1) (s + 2) t) := by
  have base : ∀ s', N K (j + 2) s' t = K.add (dot K 0 (chiC K (j + 1)) (fun i => cN K (P (i + 1) s' t)))
      (K.mul (K.neg K.one) (N K j s' t)) := by
    intro s'
    show dot K 0 (addL K (K.zero :: chiC K (j + 1)) (smulL K (K.neg K.one) (chiC K j))) _ = _
    rw [dot_addL, dot_shift, dot_smulL, dot_succ]
    rfl
  constructor
  · rw [base 0]
    have e : dot K 0 (chiC K (j + 1)) (fun i => cN K (P (i + 1) 0 t)) = N K (j + 1) 1 t := rfl
    rw [e]; lib_ring
  · rw [base (s + 1)]
    have e : dot K 0 (chiC K (j + 1)) (fun i => cN K (P (i + 1) (s + 1) t))
        = K.add (N K (j + 1) s t) (N K (j + 1) (s + 2) t) := by
      show dot K 0 (chiC K (j + 1)) (fun i => cN K (P i s t + P i (s + 2) t)) = _
      rw [dot_ext K _ _ (fun i => cN_add K _ _) 0 _, dot_fadd]
      rfl
    rw [e]; lib_ring

/-- **Fusion:** Σ_i (χ_j)_i P(i, s, t) = cg j s t. -/
theorem fusion : ∀ j s t, N K j s t = cN K (cg j s t)
  | 0, s, t => by
    show K.add (K.mul K.one (cN K (P 0 s t))) K.zero = _
    rw [DetChain.add_zero', K.one_mul]; rfl
  | 1, s, t => by
    show K.add (K.mul K.zero _) (K.add (K.mul K.one (cN K (P 1 s t))) K.zero) = cN K (cg 1 s t)
    rw [K.mul_comm K.zero, DetChain.mul_zero, K.zero_add, DetChain.add_zero', K.one_mul]
    cases s with
    | zero => rfl
    | succ s =>
      show cN K (dl s t + dl (s + 2) t) = cN K (dl s t + dl (s + 0 + 2) t)
      rfl
  | j + 2, s, t => by
    cases s with
    | zero =>
      have r := (N_rec K j 0 t).1
      have c := (cg_rec j 0 t).1
      rw [fusion (j + 1) 1 t, fusion j 0 t] at r
      -- N(j+2,0,t) = cg(j+1,1,t) − cg(j,0,t)
      have : N K (j + 2) 0 t = K.add (cN K (cg (j + 1) 1 t)) (K.neg (cN K (cg j 0 t))) := by
        rw [← r]; lib_ring
      rw [this, ← c, cN_add]; lib_ring
    | succ s =>
      have r := (N_rec K j s t).2
      have c := (cg_rec j s t).2
      rw [fusion (j + 1) s t, fusion (j + 1) (s + 2) t, fusion j (s + 1) t] at r
      have : N K (j + 2) (s + 1) t = K.add (K.add (cN K (cg (j + 1) s t)) (cN K (cg (j + 1) (s + 2) t)))
          (K.neg (cN K (cg j (s + 1) t))) := by
        rw [← r]; lib_ring
      rw [this, ← cN_add, ← c, cN_add]; lib_ring

/-- **I(χ_j χ_s χ_t) = cg j s t.** -/
theorem triple (j s t : Nat) :
    I K (mulL K (mulL K (chiC K j) (chiC K s)) (chiC K t)) = cN K (cg j s t) := by
  rw [I_mul_dot, dot_mulL]
  rw [dot_ext K _ (fun i => cN K (P i s t)) (fun i => by
    rw [dot_ext K _ (fun l => cN K (P l t 0)) (fun l => by rw [walk_chi K 0 t l, P_symm]) i (chiC K s),
      walk_chi K t s i, P_symm]) 0 (chiC K j)]
  exact fusion K j s t

/-- **Positive type is preserved by products.** -/
theorem coef_prod_nn (O : ORing K) (h1 : O.le K.zero K.one) (a b : List R)
    (ha : ∀ x, memL x a → O.le K.zero x) (hb : ∀ x, memL x b → O.le K.zero x) (n : Nat) :
    O.le K.zero (I K (mulL K (mulL K (comboK K 0 a) (comboK K 0 b)) (chiC K n))) := by
  rw [I_mul_dot, dot_mulL, dot_combo]
  -- Σ_j a_j Σ_i (χ_j)_i dot i (q χ_n) μ0; inner: Σ_k b_k N(j?..) — use: dot i (mulL B χ_n) μ0 = Σ_k b_k P(i, ...)
  have inner : ∀ i, dot K i (comboK K 0 b) (fun l => dot K l (chiC K n) (fun j => cN K (P j 0 0)))
      = dot K 0 b (fun k => cN K (P i k n)) := by
    intro i
    rw [dot_combo]
    -- dot 0 b (fun k => dot i (chiC k) (fun l => dot l (chiC n) μ0))
    refine dot_ext K _ _ (fun k => ?_) 0 b
    rw [dot_ext K _ (fun l => cN K (P l n 0)) (fun l => by rw [walk_chi K 0 n l, P_symm]) i (chiC K k),
      walk_chi K n k i, P_symm]
  have step : ∀ j, dot K 0 (chiC K j) (fun i => dot K i (comboK K 0 b) (fun l => dot K l (chiC K n) (fun j => cN K (P j 0 0))))
      = dot K 0 b (fun k => cN K (cg j k n)) := by
    intro j
    rw [dot_ext K _ _ inner 0 (chiC K j), dot_swap]
    exact dot_ext K _ _ (fun k => fusion K j k n) 0 b
  rw [dot_ext K _ _ step 0 a]
  -- double sum of nonnegatives
  have nn : ∀ (m : Nat) (l : List R) (f : Nat → R), (∀ x, memL x l → O.le K.zero x) → (∀ i, O.le K.zero (f i)) →
      O.le K.zero (dot K m l f) := by
    intro m l f hl hf
    induction l generalizing m with
    | nil => exact O.le_refl _
    | cons c l ih =>
      have := O.add_le_add _ _ _ _ (O.mul_nonneg _ _ (hl c (Or.inl rfl)) (hf m)) (ih (m + 1) (fun x hx => hl x (Or.inr hx)))
      rw [K.zero_add] at this; exact this
  exact nn 0 a _ ha (fun j => nn 0 b _ hb (fun k => SU2CharExp.cN_nn K O h1 _))

end Fusion

#print axioms Fusion.cg_rec
#print axioms Fusion.fusion
#print axioms Fusion.triple
#print axioms Fusion.coef_prod_nn
#print axioms Fusion.cg_top
#print axioms Fusion.cg_le_one

ym/ConeBound.lean

/-
  ym/ConeBound — T45, b1q step 5: the product bound on the positive cone.

  A class weight p = Σ_j a_j χ_j (coefficient list a, entries a_j = d_j × normalised coefficient) lies in the cone
  Cone(r) when 0 ≤ a_j ≤ (j+1)·r^{4j}. For p, q in Cone(r) the character coefficients of the product
  (Fusion: c_n = Σ_{j,k} a_j b_k cg(j,k,n)) satisfy
    term_le        (j+1)(k+1) r^{4j} r^{4k} cg(j,k,n) ≤ r^{3n} · (j+1) r^j · (k+1) r^k      (cg = 0 unless n ≤ j+k)
    prod_bound     c_n(pq) ≤ r^{3n} · S_a · S_b,   S = Σ_j (j+1) r^j
    S_le_two       S ≤ 2 for 0 ≤ r ≤ ¼
    cone_product   c_n(pq) ≤ 4 r^{3n}
  So multiplying two weights of Cone(r) gives coefficients ≤ 4 r^{3n}: the exponent loses only a factor ¾ per product;
  after the two products of a 4D Migdal step (exponent (¾)²) the decimation power 4 gives exponent 9/4 > 1 —
  the strong-coupling contraction of the Migdal map on all positive-type weights (measured in NUMERIC_B1Q_CONE).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.Fusion

namespace ConeBound

open YMChain LinkIntegral SU2Haar SU2Char Fusion

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- cone condition at offset m -/
def Bd (r : R) : Nat → List R → Prop
  | _, [] => True
  | m, c :: a => (O.le K.zero c ∧ O.le c (K.mul (cN K (m + 1)) (pwR K r (4 * m)))) ∧ Bd r (m + 1) a

/-- `Sm f m L = Σ_{i<L} f(m+i)` -/
def Sm (f : Nat → R) : Nat → Nat → R
  | _, 0 => K.zero
  | m, L + 1 => K.add (f m) (Sm f (m + 1) L)

theorem dot_le (r : R) (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) : ∀ (m : Nat) (a : List R), Bd K O r m a →
    O.le (dot K m a f) (Sm K (fun j => K.mul (K.mul (cN K (j + 1)) (pwR K r (4 * j))) (f j)) m a.length)
  | _, [], _ => O.le_refl _
  | m, c :: a, h => O.add_le_add _ _ _ _ (O.mul_le_mul_right _ _ _ h.1.2 (hf m)) (dot_le r f hf (m + 1) a h.2)

theorem dot_nn (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) : ∀ (m : Nat) (a : List R),
    (∀ x, memL x a → O.le K.zero x) → O.le K.zero (dot K m a f)
  | _, [], _ => O.le_refl _
  | m, c :: a, h => by
    have := O.add_le_add _ _ _ _ (O.mul_nonneg _ _ (h c (Or.inl rfl)) (hf m)) (dot_nn f hf (m + 1) a (fun x hx => h x (Or.inr hx)))
    rw [K.zero_add] at this; exact this

theorem Bd_nn (r : R) : ∀ (m : Nat) (a : List R), Bd K O r m a → ∀ x, memL x a → O.le K.zero x
  | _, [], _, _, h => h.elim
  | m, c :: a, h, x, hx => by
    cases hx with
    | inl e => rw [e]; exact h.1.1
    | inr hx => exact Bd_nn r (m + 1) a h.2 x hx

theorem Sm_le (f g : Nat → R) (h : ∀ j, O.le (f j) (g j)) : ∀ m L, O.le (Sm K f m L) (Sm K g m L)
  | _, 0 => O.le_refl _
  | m, L + 1 => O.add_le_add _ _ _ _ (h m) (Sm_le f g h (m + 1) L)

theorem Sm_nn (f : Nat → R) (h : ∀ j, O.le K.zero (f j)) : ∀ m L, O.le K.zero (Sm K f m L)
  | _, 0 => O.le_refl _
  | m, L + 1 => by
    have := O.add_le_add _ _ _ _ (h m) (Sm_nn f h (m + 1) L); rw [K.zero_add] at this; exact this

theorem Sm_smul (c : R) (f : Nat → R) : ∀ m L, Sm K (fun j => K.mul c (f j)) m L = K.mul c (Sm K f m L)
  | _, 0 => by show K.zero = K.mul c K.zero; rw [DetChain.mul_zero]
  | m, L + 1 => by
    show K.add (K.mul c (f m)) (Sm K _ (m + 1) L) = K.mul c (K.add (f m) (Sm K f (m + 1) L))
    rw [Sm_smul c f (m + 1) L, K.distrib]

theorem Sm_mul_r (f : Nat → R) (d : R) : ∀ m L, Sm K (fun j => K.mul (f j) d) m L = K.mul (Sm K f m L) d
  | _, 0 => by show K.zero = K.mul K.zero d; rw [K.mul_comm, DetChain.mul_zero]
  | m, L + 1 => by
    show K.add (K.mul (f m) d) (Sm K _ (m + 1) L) = K.mul (K.add (f m) (Sm K f (m + 1) L)) d
    rw [Sm_mul_r f d (m + 1) L, DetChain.right_distrib]

theorem pw_anti (h1 : O.le K.zero K.one) (r : R) (hr0 : O.le K.zero r) (hr1 : O.le r K.one) (a b : Nat) (h : b ≤ a) :
    O.le (pwR K r a) (pwR K r b) := by
  obtain ⟨c, e⟩ := Nat.le.dest h
  rw [← e, GKS.pwR_add]
  have p := SU2Strong4D.pw_mono K O h1 r K.one hr0 hr1 c
  rw [SU2Strong4D.pw_one] at p
  have := O.mul_le_mul_right _ _ _ p (AreaLower.pw_nn K O r hr0 h1 b)
  rw [K.one_mul, K.mul_comm] at this; exact this

/-- **Termwise bound.** -/
theorem term_le (h1 : O.le K.zero K.one) (r : R) (hr0 : O.le K.zero r) (hr1 : O.le r K.one) (j k n : Nat) :
    O.le (K.mul (K.mul (pwR K r (4 * j)) (pwR K r (4 * k))) (cN K (cg j k n)))
      (K.mul (pwR K r (3 * n)) (K.mul (pwR K r j) (pwR K r k))) := by
  have hR : O.le K.zero (K.mul (pwR K r (3 * n)) (K.mul (pwR K r j) (pwR K r k))) :=
    O.mul_nonneg _ _ (AreaLower.pw_nn K O r hr0 h1 _) (O.mul_nonneg _ _ (AreaLower.pw_nn K O r hr0 h1 _) (AreaLower.pw_nn K O r hr0 h1 _))
  cases Nat.lt_or_ge (j + k) n with
  | inl hlt =>
    rw [cg_top j k n hlt]
    show O.le (K.mul _ K.zero) _
    rw [DetChain.mul_zero]; exact hR
  | inr hge =>
    have c1 : O.le (cN K (cg j k n)) K.one := by
      have := SU2CharExp.cN_mono K O h1 _ 1 (cg_le_one j k n); rw [SU2CharExp.cN_one] at this; exact this
    have e : K.mul (pwR K r (4 * j)) (pwR K r (4 * k)) = K.mul (pwR K r (3 * (j + k))) (K.mul (pwR K r j) (pwR K r k)) := by
      have e1 : 4 * j = 3 * j + j := by lib_nat
      have e2 : 4 * k = 3 * k + k := by lib_nat
      have e3 : 3 * (j + k) = 3 * j + 3 * k := by lib_nat
      rw [e1, e2, e3, GKS.pwR_add, GKS.pwR_add, GKS.pwR_add]; lib_ring
    have a := pw_anti K O h1 r hr0 hr1 (3 * (j + k)) (3 * n) (by
      have := Nat.mul_le_mul_left 3 hge; exact this)
    have pp : O.le K.zero (K.mul (pwR K r j) (pwR K r k)) := O.mul_nonneg _ _ (AreaLower.pw_nn K O r hr0 h1 _) (AreaLower.pw_nn K O r hr0 h1 _)
    have b := O.mul_le_mul_right _ _ _ a pp
    have lhs0 : O.le K.zero (K.mul (pwR K r (4 * j)) (pwR K r (4 * k))) :=
      O.mul_nonneg _ _ (AreaLower.pw_nn K O r hr0 h1 _) (AreaLower.pw_nn K O r hr0 h1 _)
    have c := O.mul_le_mul_right _ _ _ c1 lhs0
    rw [K.one_mul, K.mul_comm (cN K _)] at c
    rw [e] at c
    rw [e]
    exact O.le_trans _ _ _ c b

theorem dot_mono (f g : Nat → R) (h : ∀ j, O.le (f j) (g j)) : ∀ (m : Nat) (a : List R),
    (∀ x, memL x a → O.le K.zero x) → O.le (dot K m a f) (dot K m a g)
  | _, [], _ => O.le_refl _
  | m, c :: a, ha => O.add_le_add _ _ _ _
      (by have := O.mul_le_mul_right _ _ _ (h m) (ha c (Or.inl rfl)); rw [K.mul_comm _ c, K.mul_comm _ c] at this; exact this)
      (dot_mono f g h (m + 1) a (fun x hx => ha x (Or.inr hx)))

/-- **Product bound on the cone.** -/
theorem prod_bound (h1 : O.le K.zero K.one) (r : R) (hr0 : O.le K.zero r) (hr1 : O.le r K.one) (n : Nat)
    (a b : List R) (ha : Bd K O r 0 a) (hb : Bd K O r 0 b) :
    let u : Nat → R := fun j => K.mul (cN K (j + 1)) (pwR K r j)
    O.le (dot K 0 a (fun j => dot K 0 b (fun k => cN K (cg j k n))))
      (K.mul (pwR K r (3 * n)) (K.mul (Sm K u 0 a.length) (Sm K u 0 b.length))) := by
  intro u
  let w : Nat → R := fun j => K.mul (cN K (j + 1)) (pwR K r (4 * j))
  have cgnn : ∀ j k, O.le K.zero (cN K (cg j k n)) := fun j k => SU2CharExp.cN_nn K O h1 _
  have unn : ∀ j, O.le K.zero (u j) := fun j => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O r hr0 h1 _)
  have wnn : ∀ j, O.le K.zero (w j) := fun j => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O r hr0 h1 _)
  let Sb : Nat → R := fun j => Sm K (fun k => K.mul (w k) (cN K (cg j k n))) 0 b.length
  have Sbnn : ∀ j, O.le K.zero (Sb j) := fun j => Sm_nn K O _ (fun k => O.mul_nonneg _ _ (wnn k) (cgnn j k)) 0 _
  -- inner and outer majorants
  have s1 := dot_mono K O _ Sb (fun j => dot_le K O r _ (cgnn j) 0 b hb) 0 a (Bd_nn K O r 0 a ha)
  have s2 := dot_le K O r Sb Sbnn 0 a ha
  refine O.le_trans _ _ _ s1 (O.le_trans _ _ _ s2 ?_)
  -- termwise: w_j (w_k cg) ≤ r^{3n} u_j u_k
  have tw : ∀ j, O.le (K.mul (w j) (Sb j)) (K.mul (pwR K r (3 * n)) (K.mul (u j) (Sm K u 0 b.length))) := by
    intro j
    rw [← Sm_smul]
    have e : K.mul (pwR K r (3 * n)) (K.mul (u j) (Sm K u 0 b.length))
        = Sm K (fun k => K.mul (pwR K r (3 * n)) (K.mul (u j) (u k))) 0 b.length := by
      rw [← Sm_smul, ← Sm_smul]
    rw [e]
    refine Sm_le K O _ _ (fun k => ?_) 0 _
    have t := term_le K O h1 r hr0 hr1 j k n
    have cc : O.le K.zero (K.mul (cN K (j + 1)) (cN K (k + 1))) := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (SU2CharExp.cN_nn K O h1 _)
    have t2 := O.mul_le_mul_right _ _ _ t cc
    have eL : K.mul (K.mul (K.mul (pwR K r (4 * j)) (pwR K r (4 * k))) (cN K (cg j k n))) (K.mul (cN K (j + 1)) (cN K (k + 1)))
        = K.mul (w j) (K.mul (w k) (cN K (cg j k n))) := by show _ = K.mul (K.mul _ _) (K.mul (K.mul _ _) _); lib_ring
    have eR : K.mul (K.mul (pwR K r (3 * n)) (K.mul (pwR K r j) (pwR K r k))) (K.mul (cN K (j + 1)) (cN K (k + 1)))
        = K.mul (pwR K r (3 * n)) (K.mul (u j) (u k)) := by show _ = K.mul _ (K.mul (K.mul _ _) (K.mul _ _)); lib_ring
    rw [eL, eR] at t2; exact t2
  have s3 := Sm_le K O _ _ tw 0 a.length
  have e : Sm K (fun j => K.mul (pwR K r (3 * n)) (K.mul (u j) (Sm K u 0 b.length))) 0 a.length
      = K.mul (pwR K r (3 * n)) (K.mul (Sm K u 0 a.length) (Sm K u 0 b.length)) := by
    rw [Sm_smul, Sm_mul_r]
  rw [e] at s3; exact s3

theorem cN_le_pow2 (h1 : O.le K.zero K.one) : ∀ j, O.le (cN K (j + 1)) (pwR K (SU2Strong4D.two K) j)
  | 0 => by rw [SU2CharExp.cN_one]; exact O.le_refl _
  | j + 1 => by
    have ih := cN_le_pow2 h1 j
    have g := SU2Strong4D.pw_ge_one K O h1 (SU2Strong4D.two K)
      (by have := O.add_le_add _ _ _ _ (O.le_refl K.one) h1; rw [DetChain.add_zero'] at this; exact this) j
    have s := O.add_le_add _ _ _ _ g ih
    show O.le (K.add K.one (cN K (j + 1))) (K.mul (pwR K (SU2Strong4D.two K) j) (K.add K.one K.one))
    have e : K.mul (pwR K (SU2Strong4D.two K) j) (K.add K.one K.one)
        = K.add (pwR K (SU2Strong4D.two K) j) (pwR K (SU2Strong4D.two K) j) := by lib_ring
    rw [e]; exact s

/-- geometric: Σ_{i<L} x^{m+i} ≤ 2 x^m for 0 ≤ x ≤ ½ -/
theorem Sm_geom (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx : O.le (K.add x x) K.one) :
    ∀ L m, O.le (Sm K (fun j => pwR K x j) m L) (K.add (pwR K x m) (pwR K x m))
  | 0, m => by
    have p := AreaLower.pw_nn K O x hx0 h1 m
    have := O.add_le_add _ _ _ _ p p; rw [K.zero_add] at this; exact this
  | L + 1, m => by
    have ih := Sm_geom h1 x hx0 hx L (m + 1)
    show O.le (K.add (pwR K x m) (Sm K _ (m + 1) L)) _
    refine O.le_trans _ _ _ (O.add_le_add _ _ _ _ (O.le_refl _) ih) (O.add_le_add _ _ _ _ (O.le_refl _) ?_)
    show O.le (K.add (K.mul (pwR K x m) x) (K.mul (pwR K x m) x)) (pwR K x m)
    have := O.mul_le_mul_right _ _ _ hx (AreaLower.pw_nn K O x hx0 h1 m)
    rw [DetChain.right_distrib, K.one_mul, K.mul_comm x] at this; exact this

/-- **S ≤ 2 for 0 ≤ r ≤ ¼.** -/
theorem S_le_two (h1 : O.le K.zero K.one) (r : R) (hr0 : O.le K.zero r)
    (hr : O.le (K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) r) K.one) (L : Nat) :
    O.le (Sm K (fun j => K.mul (cN K (j + 1)) (pwR K r j)) 0 L) (SU2Strong4D.two K) := by
  let x := K.mul (SU2Strong4D.two K) r
  have t0 : O.le K.zero (SU2Strong4D.two K) := by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
  have hx0 : O.le K.zero x := O.mul_nonneg _ _ t0 hr0
  have hx : O.le (K.add x x) K.one := by
    have e : K.add x x = K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) r := by
      show K.add (K.mul (K.add K.one K.one) r) (K.mul (K.add K.one K.one) r) = K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) r
      lib_ring
    rw [e]; exact hr
  have m := Sm_le K O (fun j => K.mul (cN K (j + 1)) (pwR K r j)) (fun j => pwR K x j) (fun j => by
    have c := cN_le_pow2 K O h1 j
    have := O.mul_le_mul_right _ _ _ c (AreaLower.pw_nn K O r hr0 h1 j)
    rw [← GKS2.pwR_mul] at this; exact this) 0 L
  have g := Sm_geom K O h1 x hx0 hx L 0
  exact O.le_trans _ _ _ m g

/-- **The cone product bound:** for p, q in Cone(r), r ≤ ¼: every character coefficient of pq is ≤ 4 r^{3n}. -/
theorem cone_product (h1 : O.le K.zero K.one) (r : R) (hr0 : O.le K.zero r)
    (hr : O.le (K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) r) K.one) (n : Nat)
    (a b : List R) (ha : Bd K O r 0 a) (hb : Bd K O r 0 b) :
    O.le (dot K 0 a (fun j => dot K 0 b (fun k => cN K (cg j k n))))
      (K.mul (pwR K r (3 * n)) (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K))) := by
  have t0 : O.le K.zero (SU2Strong4D.two K) := by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
  have hr1 : O.le r K.one := by
    have g : O.le r (K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) r) := by
      have t1 : O.le K.one (SU2Strong4D.two K) := by
        have := O.add_le_add _ _ _ _ (O.le_refl K.one) h1; rw [DetChain.add_zero'] at this; exact this
      have f : O.le K.one (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) := by
        have := O.mul_le_mul_right _ _ _ t1 t0; rw [K.one_mul] at this; exact O.le_trans _ _ _ t1 this
      have := O.mul_le_mul_right _ _ _ f hr0; rw [K.one_mul] at this; exact this
    exact O.le_trans _ _ _ g hr
  have pb := prod_bound K O h1 r hr0 hr1 n a b ha hb
  refine O.le_trans _ _ _ pb ?_
  have sa := S_le_two K O h1 r hr0 hr a.length
  have sb := S_le_two K O h1 r hr0 hr b.length
  have sbn := Sm_nn K O (fun j => K.mul (cN K (j + 1)) (pwR K r j)) (fun j => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O r hr0 h1 j)) 0 b.length
  have m1 := O.mul_le_mul_right _ _ _ sa sbn
  have m2 := O.mul_le_mul_right _ _ _ sb t0
  rw [K.mul_comm _ (SU2Strong4D.two K), K.mul_comm _ (SU2Strong4D.two K)] at m2
  have m := O.le_trans _ _ _ m1 m2
  have p := O.mul_le_mul_right _ _ _ m (AreaLower.pw_nn K O r hr0 h1 (3 * n))
  rw [K.mul_comm _ (pwR K r _), K.mul_comm _ (pwR K r _)] at p
  exact p



end ConeBound

#print axioms ConeBound.dot_le
#print axioms ConeBound.term_le
#print axioms ConeBound.prod_bound
#print axioms ConeBound.S_le_two
#print axioms ConeBound.cone_product

ym/MigdalCone.lean

/-
  ym/MigdalCone — T45, b1q step 6: one 4D Migdal step maps the positive cone into a cone of higher decay.

  Weights are character-coefficient lists A = [A_0, A_1, …] (A_n = d_n × normalised coefficient), normalised A_0 = 1.
  Cone(r): 0 ≤ A_n ≤ (n+1) r^{4n} (Bd from ym/ConeBound).
    lmap, bd_of_fun   a list built from a function lies in the cone when each value does
    c0_ge_one         the product of two normalised positive weights has c_0 ≥ 1
    norm_prod_cone    A, B ∈ Cone(r) normalised, r ≤ ¼, s⁴ ≥ 4 r³  ⇒  normalise(A·B) ∈ Cone(s), first entry 1
    dec_cone          decimation A_n ↦ A_n⁴/d_n³ (normalised coefficient to the 4th power) maps Cone(s) into Cone(s⁴)
    mk_cone           one 4D Migdal step (two squarings = bond moving by 4, then decimation) maps Cone(r) into
                      Cone(r₂⁴) whenever r₁⁴ ≥ 4r³, r₂⁴ ≥ 4r₁³ and r, r₁ ≤ ¼; with r₂⁴ ≤ r the cone is invariant
                      (r₂^{16} ≈ 4⁷ r⁹: for small r the rate improves from r to ≈ (4⁷ r⁹)^{1/4}).
    mk_invariant      explicit: r = 2⁻⁸, r₁ = 2⁻⁵, r₂ = 2⁻³: one step maps Cone(2⁻⁸) into Cone(2⁻¹²) ⊆ Cone(2⁻⁸)
    mk_forever        hence every number of 4D Migdal steps stays in Cone(2⁻⁸) (mk_head keeps the leading 1)

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.ConeBound

namespace MigdalCone

open YMChain LinkIntegral SU2Haar SU2Char Fusion ConeBound

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

def lmap (f : Nat → R) : Nat → Nat → List R
  | _, 0 => []
  | m, L + 1 => f m :: lmap f (m + 1) L

theorem len_lmap (f : Nat → R) : ∀ m L, (lmap f m L).length = L
  | _, 0 => rfl
  | m, L + 1 => congrArg Nat.succ (len_lmap f (m + 1) L)

theorem bd_of_fun (r : R) (f : Nat → R)
    (h : ∀ i, O.le K.zero (f i) ∧ O.le (f i) (K.mul (cN K (i + 1)) (pwR K r (4 * i)))) :
    ∀ m L, Bd K O r m (lmap f m L)
  | _, 0 => trivial
  | m, L + 1 => ⟨h m, bd_of_fun r f h (m + 1) L⟩

/-- product coefficient c_n(A·B) = Σ_{j,k} A_j B_k cg(j,k,n) -/
def cgc (a b : List R) (n : Nat) : R := dot K 0 a (fun j => dot K 0 b (fun k => cN K (cg j k n)))

theorem dot_head_le (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) (c : R) (a : List R) (m : Nat)
    (ha : ∀ x, memL x a → O.le K.zero x) : O.le (K.mul c (f m)) (dot K m (c :: a) f) := by
  have := O.add_le_add _ _ _ _ (O.le_refl (K.mul c (f m))) (dot_nn K O f hf (m + 1) a ha)
  rw [DetChain.add_zero'] at this; exact this

/-- **c₀ ≥ 1** for normalised positive weights. -/
theorem c0_ge_one (h1 : O.le K.zero K.one) (a b : List R) (ha : ∀ x, memL x a → O.le K.zero x)
    (hb : ∀ x, memL x b → O.le K.zero x) : O.le K.one (cgc K (K.one :: a) (K.one :: b) 0) := by
  have cgnn : ∀ j k, O.le K.zero (cN K (cg j k 0)) := fun j k => SU2CharExp.cN_nn K O h1 _
  have inner : ∀ j, O.le K.zero (dot K 0 (K.one :: b) (fun k => cN K (cg j k 0))) := fun j =>
    dot_nn K O _ (cgnn j) 0 _ (fun x hx => by cases hx with | inl e => rw [e]; exact h1 | inr h => exact hb x h)
  have o := dot_head_le K O _ inner K.one a 0 ha
  have i0 := dot_head_le K O _ (cgnn 0) K.one b 0 hb
  have e : cN K (cg 0 0 0) = K.one := SU2CharExp.cN_one K
  rw [e, K.one_mul] at i0
  rw [K.one_mul] at o
  exact O.le_trans _ _ _ i0 o

/-- normalised product, truncated to length L -/
def nprod (a b : List R) (L : Nat) : List R :=
  lmap (fun n => K.mul (cgc K a b n) (F.inv (cgc K a b 0))) 0 L

def four : R := K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)

theorem four_ge_one (h1 : O.le K.zero K.one) : O.le K.one (four K) := by
  have t0 : O.le K.zero (SU2Strong4D.two K) := by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
  have t1 : O.le K.one (SU2Strong4D.two K) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) h1; rw [DetChain.add_zero'] at this; exact this
  have := O.mul_le_mul_right _ _ _ t1 t0; rw [K.one_mul] at this; exact O.le_trans _ _ _ t1 this

theorem four_le_pow (h1 : O.le K.zero K.one) (x : R) (hx : O.le K.zero x) (m : Nat) :
    O.le (K.mul (four K) (pwR K x (m + 1))) (pwR K (K.mul (four K) x) (m + 1)) := by
  have q1 := four_ge_one K O h1
  have g := SU2Strong4D.pw_ge_one K O h1 (four K) q1 m
  have xm : O.le K.zero (K.mul (four K) (pwR K x (m + 1))) :=
    O.mul_nonneg _ _ (O.le_trans _ _ _ h1 q1) (AreaLower.pw_nn K O x hx h1 _)
  have := O.mul_le_mul_right _ _ _ g xm
  rw [K.one_mul] at this
  have e : K.mul (pwR K (four K) m) (K.mul (four K) (pwR K x (m + 1))) = pwR K (K.mul (four K) x) (m + 1) := by
    rw [GKS2.pwR_mul]; show _ = K.mul (K.mul (pwR K (four K) m) (four K)) _; lib_ring
  rw [e] at this; exact this

/-- **The normalised product of two cone weights lies in a cone of rate s, s⁴ ≥ 4r³.** -/
theorem norm_prod_cone (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (r s : R) (hr0 : O.le K.zero r)
    (hr : O.le (K.mul (four K) r) K.one) (hs0 : O.le K.zero s)
    (hs : O.le (K.mul (four K) (pwR K r 3)) (pwR K s 4))
    (a b : List R) (ha : Bd K O r 0 (K.one :: a)) (hb : Bd K O r 0 (K.one :: b)) (L : Nat) :
    Bd K O s 0 (nprod K O F (K.one :: a) (K.one :: b) L) := by
  have c0 := c0_ge_one K O h1 a b (fun x hx => Bd_nn K O r 0 _ ha x (Or.inr hx)) (fun x hx => Bd_nn K O r 0 _ hb x (Or.inr hx))
  have c0p : O.le K.zero (cgc K (K.one :: a) (K.one :: b) 0) := O.le_trans _ _ _ h1 c0
  have c0n := MigdalStrong.ne_of_one_le K O h1 h10 _ c0
  have ip := F.inv_pos _ c0p c0n
  have i1 := MigdalStrong.inv_le_one K O F h1 h10 _ c0
  apply bd_of_fun
  intro n
  have cn : O.le K.zero (cgc K (K.one :: a) (K.one :: b) n) :=
    dot_nn K O _ (fun j => dot_nn K O _ (fun k => SU2CharExp.cN_nn K O h1 _) 0 _ (Bd_nn K O r 0 _ hb)) 0 _ (Bd_nn K O r 0 _ ha)
  refine ⟨O.mul_nonneg _ _ cn ip, ?_⟩
  cases n with
  | zero =>
    rw [F.mul_inv _ c0n]
    show O.le K.one (K.mul (K.add K.one K.zero) K.one)
    rw [DetChain.add_zero', K.one_mul]; exact O.le_refl _
  | succ m =>
    -- c·c0⁻¹ ≤ c ≤ 4 r^{3(m+1)} ≤ (4r³)^{m+1} ≤ s^{4(m+1)} ≤ (m+2) s^{4(m+1)}
    have a1 : O.le (K.mul (cgc K (K.one :: a) (K.one :: b) (m + 1)) (F.inv (cgc K (K.one :: a) (K.one :: b) 0)))
        (cgc K (K.one :: a) (K.one :: b) (m + 1)) := by
      have := O.mul_le_mul_right _ _ _ i1 cn; rw [K.one_mul, K.mul_comm] at this; exact this
    have a2 := cone_product K O h1 r hr0 hr (m + 1) _ _ ha hb
    have a3 : O.le (K.mul (pwR K r (3 * (m + 1))) (four K)) (pwR K (pwR K s 4) (m + 1)) := by
      rw [← Migdal.pw_pw, K.mul_comm]
      exact O.le_trans _ _ _ (four_le_pow K O h1 _ (AreaLower.pw_nn K O r hr0 h1 3) m)
        (SU2Strong4D.pw_mono K O h1 _ _ (O.mul_nonneg _ _ (O.le_trans _ _ _ h1 (four_ge_one K O h1)) (AreaLower.pw_nn K O r hr0 h1 3)) hs (m + 1))
    have a4 : O.le (pwR K (pwR K s 4) (m + 1)) (K.mul (cN K (m + 1 + 1)) (pwR K s (4 * (m + 1)))) := by
      rw [Migdal.pw_pw]
      have g := SU2CharExp.cN_mono K O h1 1 (m + 2) (Nat.succ_le_succ (Nat.zero_le _))
      rw [SU2CharExp.cN_one] at g
      have := O.mul_le_mul_right _ _ _ g (AreaLower.pw_nn K O s hs0 h1 (4 * (m + 1)))
      rw [K.one_mul] at this; exact this
    exact O.le_trans _ _ _ a1 (O.le_trans _ _ _ a2 (O.le_trans _ _ _ a3 a4))

/-- decimation: normalised coefficient to the 4th power, A_n ↦ A_n⁴ / d_n³ -/
def decM : Nat → List R → List R
  | _, [] => []
  | m, c :: a => K.mul (pwR K c 4) (pwR K (F.inv (cN K (m + 1))) 3) :: decM (m + 1) a

/-- **Decimation maps Cone(s) into Cone(s⁴).** -/
theorem dec_cone (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (s : R) (hs0 : O.le K.zero s) :
    ∀ (m : Nat) (a : List R), Bd K O s m a → Bd K O (pwR K s 4) m (decM K O F m a)
  | _, [], _ => trivial
  | m, c :: a, h => by
    refine ⟨?_, dec_cone h1 h10 s hs0 (m + 1) a h.2⟩
    have dpos := SU2CharExp.pos_of_ge_one K O h1 h10 (m + 1) (Nat.succ_le_succ (Nat.zero_le m))
    have ipos := F.inv_pos _ dpos.1 dpos.2
    refine ⟨O.mul_nonneg _ _ (AreaLower.pw_nn K O c h.1.1 h1 4) (AreaLower.pw_nn K O _ ipos h1 3), ?_⟩
    -- c⁴ ≤ (d s^{4m})⁴, times d⁻³
    have p := SU2Strong4D.pw_mono K O h1 c _ h.1.1 h.1.2 4
    have q := O.mul_le_mul_right _ _ _ p (AreaLower.pw_nn K O _ ipos h1 3)
    let d := cN K (m + 1)
    let i := F.inv d
    have e := F.mul_inv _ dpos.2
    have e1 : pwR K (K.mul d (pwR K s (4 * m))) 4 = K.mul (pwR K d 4) (pwR K (pwR K s (4 * m)) 4) := GKS2.pwR_mul K _ _ 4
    have e2 : pwR K (pwR K s (4 * m)) 4 = pwR K (pwR K s 4) (4 * m) := by
      rw [Migdal.pw_pw, Migdal.pw_pw, show 4 * m * 4 = 4 * (4 * m) by lib_nat]
    have e3 : K.mul (pwR K d 4) (pwR K i 3) = d := by
      have : K.mul (pwR K d 4) (pwR K i 3) = K.mul d (pwR K (K.mul d i) 3) := by
        show K.mul (K.mul (K.mul (K.mul (K.mul K.one d) d) d) d) (K.mul (K.mul (K.mul K.one i) i) i)
          = K.mul d (K.mul (K.mul (K.mul K.one (K.mul d i)) (K.mul d i)) (K.mul d i))
        lib_ring
      rw [this, e, SU2Strong4D.pw_one, K.mul_comm, K.one_mul]
    have eq : K.mul (pwR K (K.mul d (pwR K s (4 * m))) 4) (pwR K i 3) = K.mul d (pwR K (pwR K s 4) (4 * m)) := by
      rw [e1, e2]
      calc K.mul (K.mul (pwR K d 4) (pwR K (pwR K s 4) (4 * m))) (pwR K i 3)
          = K.mul (K.mul (pwR K d 4) (pwR K i 3)) (pwR K (pwR K s 4) (4 * m)) := by lib_ring
        _ = K.mul d (pwR K (pwR K s 4) (4 * m)) := by rw [e3]
    rw [eq] at q; exact q

theorem nprod_head (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (a b : List R)
    (ha : ∀ x, memL x a → O.le K.zero x) (hb : ∀ x, memL x b → O.le K.zero x) (L : Nat) :
    nprod K O F (K.one :: a) (K.one :: b) (L + 1)
      = K.one :: lmap (fun n => K.mul (cgc K (K.one :: a) (K.one :: b) n) (F.inv (cgc K (K.one :: a) (K.one :: b) 0))) 1 L := by
  have c0 := c0_ge_one K O h1 a b ha hb
  have c0n := MigdalStrong.ne_of_one_le K O h1 h10 _ c0
  show K.mul (cgc K _ _ 0) (F.inv (cgc K _ _ 0)) :: _ = _
  rw [F.mul_inv _ c0n]

/-- **One 4D Migdal step maps Cone(r) into Cone(r₂⁴).** Bond moving by 4 = two normalised squarings (rates r → r₁ → r₂,
  r₁⁴ ≥ 4r³, r₂⁴ ≥ 4r₁³), then decimation (rate r₂ → r₂⁴). -/
theorem mk_cone (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (r r1 r2 : R)
    (hr0 : O.le K.zero r) (hr : O.le (K.mul (four K) r) K.one)
    (hr10 : O.le K.zero r1) (hr1 : O.le (K.mul (four K) r1) K.one) (hr20 : O.le K.zero r2)
    (hs1 : O.le (K.mul (four K) (pwR K r 3)) (pwR K r1 4)) (hs2 : O.le (K.mul (four K) (pwR K r1 3)) (pwR K r2 4))
    (a : List R) (ha : Bd K O r 0 (K.one :: a)) (L2 L4 : Nat) :
    let P2 := nprod K O F (K.one :: a) (K.one :: a) (L2 + 1)
    let P4 := nprod K O F P2 P2 (L4 + 1)
    Bd K O (pwR K r2 4) 0 (decM K O F 0 P4) := by
  intro P2 P4
  have hann : ∀ x, memL x a → O.le K.zero x := fun x hx => Bd_nn K O r 0 _ ha x (Or.inr hx)
  have b2 := norm_prod_cone K O F h1 h10 r r1 hr0 hr hr10 hs1 a a ha ha (L2 + 1)
  let t2 := lmap (fun n => K.mul (cgc K (K.one :: a) (K.one :: a) n) (F.inv (cgc K (K.one :: a) (K.one :: a) 0))) 1 L2
  have eP2 : nprod K O F (K.one :: a) (K.one :: a) (L2 + 1) = K.one :: t2 := nprod_head K O F h1 h10 a a hann hann L2
  rw [eP2] at b2
  have b4 := norm_prod_cone K O F h1 h10 r1 r2 hr10 hr1 hr20 hs2 t2 t2 b2 b2 (L4 + 1)
  have eP4 : P4 = nprod K O F (K.one :: t2) (K.one :: t2) (L4 + 1) := by
    show nprod K O F (nprod K O F (K.one :: a) (K.one :: a) (L2 + 1)) (nprod K O F (K.one :: a) (K.one :: a) (L2 + 1)) _ = _
    rw [eP2]
  rw [eP4]
  exact dec_cone K O F h1 h10 r2 hr20 0 _ b4

/-- cones are nested: Cone(s) ⊆ Cone(r) for 0 ≤ s ≤ r -/
theorem bd_mono (h1 : O.le K.zero K.one) (s r : R) (hs0 : O.le K.zero s) (hsr : O.le s r) :
    ∀ (m : Nat) (a : List R), Bd K O s m a → Bd K O r m a
  | _, [], _ => trivial
  | m, c :: a, h => by
    refine ⟨⟨h.1.1, O.le_trans _ _ _ h.1.2 ?_⟩, bd_mono h1 s r hs0 hsr (m + 1) a h.2⟩
    have := O.mul_le_mul_right _ _ _ (SU2Strong4D.pw_mono K O h1 s r hs0 hsr (4 * m)) (SU2CharExp.cN_nn K O h1 (m + 1))
    rw [K.mul_comm _ (cN K _), K.mul_comm _ (cN K _)] at this; exact this

/-- decimation keeps the leading 1 -/
theorem decM_head (h10 : K.one ≠ K.zero) (a : List R) :
    decM K O F 0 (K.one :: a) = K.one :: decM K O F 1 a := by
  show K.mul (pwR K K.one 4) (pwR K (F.inv (cN K 1)) 3) :: _ = _
  rw [SU2Strong4D.pw_one, SU2CharExp.cN_one, ← SU2CharExp.inv_uniq K O F K.one K.one h10 (K.one_mul K.one),
    SU2Strong4D.pw_one, K.one_mul]

/-- one 4D Migdal step at fixed truncation lengths -/
def mk (L : Nat) (a : List R) : List R :=
  decM K O F 0 (nprod K O F (nprod K O F (K.one :: a) (K.one :: a) (L + 1)) (nprod K O F (K.one :: a) (K.one :: a) (L + 1)) (L + 1))

/-- **The strong-coupling cone is invariant:** with h = ½, one 4D Migdal step maps Cone(h⁸) into Cone(h¹²) ⊆ Cone(h⁸). -/
theorem mk_invariant (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (a : List R) (ha : Bd K O (pwR K h 8) 0 (K.one :: a)) (L : Nat) :
    Bd K O (pwR K h 12) 0 (mk K O F L a) ∧ Bd K O (pwR K h 8) 0 (mk K O F L a) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have th : K.mul (SU2Strong4D.two K) h = K.one := by
    show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
  have fh : K.mul (four K) (K.mul h h) = K.one := by
    show K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) (K.mul h h) = K.one
    have : K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) (K.mul h h)
        = K.mul (K.mul (SU2Strong4D.two K) h) (K.mul (SU2Strong4D.two K) h) := by lib_ring
    rw [this, th, K.one_mul]
  have pn : ∀ n, O.le K.zero (pwR K h n) := fun n => AreaLower.pw_nn K O h h0 h1 n
  have pa : ∀ m n, m ≤ n → O.le (pwR K h n) (pwR K h m) := fun m n hmn => pw_anti K O h1 h h0 hle n m hmn
  -- 4 h^k ≤ h^{k−2}
  have four_h : ∀ k, K.mul (four K) (pwR K h (k + 2)) = pwR K h k := by
    intro k
    show K.mul (four K) (K.mul (K.mul (pwR K h k) h) h) = pwR K h k
    have : K.mul (four K) (K.mul (K.mul (pwR K h k) h) h) = K.mul (pwR K h k) (K.mul (four K) (K.mul h h)) := by lib_ring
    rw [this, fh, K.mul_comm, K.one_mul]
  have fr : O.le (K.mul (four K) (pwR K h 8)) K.one := by
    rw [four_h 6]; have := pa 0 6 (Nat.zero_le _); exact this
  have fr1 : O.le (K.mul (four K) (pwR K h 5)) K.one := by
    rw [four_h 3]; exact pa 0 3 (Nat.zero_le _)
  have s1 : O.le (K.mul (four K) (pwR K (pwR K h 8) 3)) (pwR K (pwR K h 5) 4) := by
    rw [Migdal.pw_pw, Migdal.pw_pw, show 8 * 3 = 22 + 2 from rfl, four_h 22]; exact pa (5 * 4) 22 (by decide)
  have s2 : O.le (K.mul (four K) (pwR K (pwR K h 5) 3)) (pwR K (pwR K h 3) 4) := by
    rw [Migdal.pw_pw, Migdal.pw_pw, show 5 * 3 = 13 + 2 from rfl, four_h 13]; exact pa (3 * 4) 13 (by decide)
  have b := mk_cone K O F h1 h10 (pwR K h 8) (pwR K h 5) (pwR K h 3) (pn 8) fr (pn 5) fr1 (pn 3) s1 s2 a ha L L
  have e : pwR K (pwR K h 3) 4 = pwR K h 12 := by rw [Migdal.pw_pw]
  rw [e] at b
  exact ⟨b, bd_mono K O h1 _ _ (pn 12) (pa 8 12 (by decide)) 0 _ b⟩

theorem mk_head (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (r : R) (a : List R) (ha : Bd K O r 0 (K.one :: a))
    (L : Nat) : mk K O F L a = K.one :: (mk K O F L a).tail := by
  have hann : ∀ x, memL x a → O.le K.zero x := fun x hx => Bd_nn K O r 0 _ ha x (Or.inr hx)
  let t2 := lmap (fun n => K.mul (cgc K (K.one :: a) (K.one :: a) n) (F.inv (cgc K (K.one :: a) (K.one :: a) 0))) 1 L
  have eP2 : nprod K O F (K.one :: a) (K.one :: a) (L + 1) = K.one :: t2 := nprod_head K O F h1 h10 a a hann hann L
  -- entries of t2 are ≥ 0
  have t2nn : ∀ x, memL x t2 → O.le K.zero x := by
    have b := norm_prod_cone K O F h1 h10 r r
    intro x hx
    have c0 := c0_ge_one K O h1 a a hann hann
    have c0n := MigdalStrong.ne_of_one_le K O h1 h10 _ c0
    have ip := F.inv_pos _ (O.le_trans _ _ _ h1 c0) c0n
    have gen : ∀ m L' x, memL x (lmap (fun n => K.mul (cgc K (K.one :: a) (K.one :: a) n) (F.inv (cgc K (K.one :: a) (K.one :: a) 0))) m L') → O.le K.zero x := by
      intro m L'
      induction L' generalizing m with
      | zero => intro x hx; exact hx.elim
      | succ L' ih =>
        intro x hx
        cases hx with
        | inl e =>
          rw [e]
          exact O.mul_nonneg _ _ (dot_nn K O _ (fun j => dot_nn K O _ (fun k => SU2CharExp.cN_nn K O h1 _) 0 _
            (fun y hy => by cases hy with | inl e' => rw [e']; exact h1 | inr h' => exact hann y h')) 0 _
            (fun y hy => by cases hy with | inl e' => rw [e']; exact h1 | inr h' => exact hann y h')) ip
        | inr h' => exact ih (m + 1) x h'
    exact gen 1 L x hx
  have eP4 := nprod_head K O F h1 h10 t2 t2 t2nn t2nn L
  have E : ∃ t, mk K O F L a = K.one :: t := ⟨_, by
    show decM K O F 0 (nprod K O F (nprod K O F (K.one :: a) (K.one :: a) (L + 1)) (nprod K O F (K.one :: a) (K.one :: a) (L + 1)) (L + 1)) = _
    rw [eP2, eP4, decM_head K O F h10]⟩
  obtain ⟨t, E⟩ := E
  rw [E]; rfl

theorem mk_head_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (a : List R)
    (hann : ∀ x, memL x a → O.le K.zero x) (L : Nat) : mk K O F L a = K.one :: (mk K O F L a).tail := by
  let t2 := lmap (fun n => K.mul (cgc K (K.one :: a) (K.one :: a) n) (F.inv (cgc K (K.one :: a) (K.one :: a) 0))) 1 L
  have eP2 : nprod K O F (K.one :: a) (K.one :: a) (L + 1) = K.one :: t2 := nprod_head K O F h1 h10 a a hann hann L
  -- entries of t2 are ≥ 0
  have t2nn : ∀ x, memL x t2 → O.le K.zero x := by
    intro x hx
    have c0 := c0_ge_one K O h1 a a hann hann
    have c0n := MigdalStrong.ne_of_one_le K O h1 h10 _ c0
    have ip := F.inv_pos _ (O.le_trans _ _ _ h1 c0) c0n
    have gen : ∀ m L' x, memL x (lmap (fun n => K.mul (cgc K (K.one :: a) (K.one :: a) n) (F.inv (cgc K (K.one :: a) (K.one :: a) 0))) m L') → O.le K.zero x := by
      intro m L'
      induction L' generalizing m with
      | zero => intro x hx; exact hx.elim
      | succ L' ih =>
        intro x hx
        cases hx with
        | inl e =>
          rw [e]
          exact O.mul_nonneg _ _ (dot_nn K O _ (fun j => dot_nn K O _ (fun k => SU2CharExp.cN_nn K O h1 _) 0 _
            (fun y hy => by cases hy with | inl e' => rw [e']; exact h1 | inr h' => exact hann y h')) 0 _
            (fun y hy => by cases hy with | inl e' => rw [e']; exact h1 | inr h' => exact hann y h')) ip
        | inr h' => exact ih (m + 1) x h'
    exact gen 1 L x hx
  have eP4 := nprod_head K O F h1 h10 t2 t2 t2nn t2nn L
  have E : ∃ t, mk K O F L a = K.one :: t := ⟨_, by
    show decM K O F 0 (nprod K O F (nprod K O F (K.one :: a) (K.one :: a) (L + 1)) (nprod K O F (K.one :: a) (K.one :: a) (L + 1)) (L + 1)) = _
    rw [eP2, eP4, decM_head K O F h10]⟩
  obtain ⟨t, E⟩ := E
  rw [E]; rfl

/-- iterated Migdal steps, kept in the form 1 :: tail -/
def mkIt (L : Nat) (a : List R) : Nat → List R
  | 0 => a
  | k + 1 => (mk K O F L (mkIt L a k)).tail

/-- **The strong-coupling cone Cone(2⁻⁸) is invariant under every number of 4D Migdal steps.** -/
theorem mk_forever (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (a : List R) (ha : Bd K O (pwR K h 8) 0 (K.one :: a)) (L : Nat) :
    ∀ k, Bd K O (pwR K h 8) 0 (K.one :: mkIt K O F L a k)
  | 0 => ha
  | k + 1 => by
    have ih := mk_forever h1 h10 h hh h0 a ha L k
    have inv := (mk_invariant K O F h1 h10 h hh h0 _ ih L).2
    rw [mk_head K O F h1 h10 (pwR K h 8) _ ih L] at inv
    exact inv

/-- **A larger invariant cone:** with h = ½, one 4D Migdal step maps Cone(h⁴) = Cone(1/16) into itself
  (r = h⁴, r₁ = h², r₂ = h; both rate conditions hold, the second with equality). -/
theorem mk_invariant16 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (a : List R) (ha : Bd K O (pwR K h 4) 0 (K.one :: a)) (L : Nat) :
    Bd K O (pwR K h 4) 0 (mk K O F L a) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have th : K.mul (SU2Strong4D.two K) h = K.one := by
    show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
  have fh : K.mul (four K) (K.mul h h) = K.one := by
    show K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) (K.mul h h) = K.one
    have : K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) (K.mul h h)
        = K.mul (K.mul (SU2Strong4D.two K) h) (K.mul (SU2Strong4D.two K) h) := by lib_ring
    rw [this, th, K.one_mul]
  have pn : ∀ n, O.le K.zero (pwR K h n) := fun n => AreaLower.pw_nn K O h h0 h1 n
  have pa : ∀ m n, m ≤ n → O.le (pwR K h n) (pwR K h m) := fun m n hmn => pw_anti K O h1 h h0 hle n m hmn
  have four_h : ∀ k, K.mul (four K) (pwR K h (k + 2)) = pwR K h k := by
    intro k
    show K.mul (four K) (K.mul (K.mul (pwR K h k) h) h) = pwR K h k
    have : K.mul (four K) (K.mul (K.mul (pwR K h k) h) h) = K.mul (pwR K h k) (K.mul (four K) (K.mul h h)) := by lib_ring
    rw [this, fh, K.mul_comm, K.one_mul]
  have fr : O.le (K.mul (four K) (pwR K h 4)) K.one := by rw [four_h 2]; exact pa 0 2 (Nat.zero_le _)
  have fr1 : O.le (K.mul (four K) (pwR K h 2)) K.one := by rw [four_h 0]; exact O.le_refl _
  have s1 : O.le (K.mul (four K) (pwR K (pwR K h 4) 3)) (pwR K (pwR K h 2) 4) := by
    rw [Migdal.pw_pw, Migdal.pw_pw, show 4 * 3 = 10 + 2 from rfl, four_h 10]; exact pa (2 * 4) 10 (by decide)
  have s2 : O.le (K.mul (four K) (pwR K (pwR K h 2) 3)) (pwR K (pwR K h 1) 4) := by
    rw [Migdal.pw_pw, Migdal.pw_pw, show 2 * 3 = 4 + 2 from rfl, four_h 4]; exact O.le_refl _
  have b := mk_cone K O F h1 h10 (pwR K h 4) (pwR K h 2) (pwR K h 1) (pn 4) fr (pn 2) fr1 (pn 1) s1 s2 a ha L L
  rw [Migdal.pw_pw] at b
  exact b

/-- every iterate stays in Cone(1/16) -/
theorem mk_forever16 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (a : List R) (ha : Bd K O (pwR K h 4) 0 (K.one :: a)) (L : Nat) :
    ∀ k, Bd K O (pwR K h 4) 0 (K.one :: mkIt K O F L a k)
  | 0 => ha
  | k + 1 => by
    have ih := mk_forever16 h1 h10 h hh h0 a ha L k
    have inv := mk_invariant16 K O F h1 h10 h hh h0 _ ih L
    rw [mk_head K O F h1 h10 (pwR K h 4) _ ih L] at inv
    exact inv

end MigdalCone

#print axioms MigdalCone.bd_of_fun
#print axioms MigdalCone.c0_ge_one
#print axioms MigdalCone.norm_prod_cone
#print axioms MigdalCone.dec_cone
#print axioms MigdalCone.mk_cone
#print axioms MigdalCone.mk_invariant
#print axioms MigdalCone.mk_forever
#print axioms MigdalCone.mk_forever16

ym/WilsonCone.lean

/-
  ym/WilsonCone — T45, b1q step 8: Wilson weights at small β start in the invariant cone.

  The Wilson weight e^{t tr U} (t = β/2) has normalised character coefficients A_n = c_n/c_0 (S3, at any truncation
  Kk + 1), with 0 ≤ A_n ≤ Σ_k t^{n+2k}/(n!k!k!) ≤ 2 tⁿ (ratio_bound, bS_le, gsum_le_two). For t ≤ 2⁻³³:
    two_tn_le      2 t^{m+1} ≤ (m+2)·2^{−32(m+1)}
    wilson_cone    the list [A_0, …, A_{L−1}] lies in Cone(2⁻⁸) and starts with 1
    wilson_forever every iterate of the 4D Migdal map started from the Wilson weight at β ≤ 2⁻³² stays in Cone(2⁻⁸)
  — the Migdal flow from small β is confined to the strong-coupling cone forever (MigdalCone.mk_forever).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.MigdalCone

namespace WilsonCone

open YMChain LinkIntegral SU2Haar SU2Char SU2CharExp ConeBound MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem two_tn_le (h1 : O.le K.zero K.one) (h t : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (ht0 : O.le K.zero t) (ht : O.le t (pwR K h 33)) (m : Nat) :
    O.le (K.mul (SU2Strong4D.two K) (pwR K t (m + 1))) (K.mul (cN K (m + 1 + 1)) (pwR K (pwR K h 8) (4 * (m + 1)))) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have th : K.mul (SU2Strong4D.two K) h = K.one := by
    show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
  have t0 : O.le K.zero (SU2Strong4D.two K) := by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
  -- 2 t^{m+1} ≤ 2 h^{33(m+1)} = h^{32(m+1)} · h^m ≤ h^{32(m+1)} ≤ (m+2) h^{32(m+1)}
  have p := SU2Strong4D.pw_mono K O h1 t _ ht0 ht (m + 1)
  have q := O.mul_le_mul_right _ _ _ p t0
  rw [K.mul_comm _ (SU2Strong4D.two K), K.mul_comm _ (SU2Strong4D.two K)] at q
  have e : K.mul (SU2Strong4D.two K) (pwR K (pwR K h 33) (m + 1)) = K.mul (pwR K (pwR K h 8) (4 * (m + 1))) (pwR K h m) := by
    rw [Migdal.pw_pw, Migdal.pw_pw, show 33 * (m + 1) = 8 * (4 * (m + 1)) + m + 1 by lib_nat]
    show _ = K.mul _ (pwR K h m)
    rw [show 8 * (4 * (m + 1)) + m + 1 = (8 * (4 * (m + 1)) + m) + 1 from rfl]
    show K.mul (SU2Strong4D.two K) (K.mul (pwR K h (8 * (4 * (m + 1)) + m)) h) = _
    rw [GKS.pwR_add]
    have : K.mul (SU2Strong4D.two K) (K.mul (K.mul (pwR K h (8 * (4 * (m + 1)))) (pwR K h m)) h)
        = K.mul (K.mul (pwR K h (8 * (4 * (m + 1)))) (pwR K h m)) (K.mul (SU2Strong4D.two K) h) := by lib_ring
    rw [this, th, K.mul_comm _ K.one, K.one_mul]
  rw [e] at q
  have hn := AreaLower.pw_nn K O (pwR K h 8) (AreaLower.pw_nn K O h h0 h1 8) h1 (4 * (m + 1))
  have r1 : O.le (K.mul (pwR K (pwR K h 8) (4 * (m + 1))) (pwR K h m)) (pwR K (pwR K h 8) (4 * (m + 1))) := by
    have := O.mul_le_mul_right _ _ _ (pw_anti K O h1 h h0 hle m 0 (Nat.zero_le m)) hn
    show O.le _ _
    rw [K.mul_comm (pwR K h m)] at this
    have e1 : pwR K h 0 = K.one := rfl
    rw [e1, K.mul_comm K.one, K.mul_comm _ K.one, K.one_mul] at this
    exact this
  have r2 : O.le (pwR K (pwR K h 8) (4 * (m + 1))) (K.mul (cN K (m + 1 + 1)) (pwR K (pwR K h 8) (4 * (m + 1)))) := by
    have g := cN_mono K O h1 1 (m + 2) (Nat.succ_le_succ (Nat.zero_le _))
    rw [cN_one] at g
    have := O.mul_le_mul_right _ _ _ g hn; rw [K.one_mul] at this; exact this
  exact O.le_trans _ _ _ q (O.le_trans _ _ _ r1 r2)

theorem two_tn_le_gen (qq : Nat) (h1 : O.le K.zero K.one) (h t : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (ht0 : O.le K.zero t) (ht : O.le t (pwR K h (4 * qq + 1))) (m : Nat) :
    O.le (K.mul (SU2Strong4D.two K) (pwR K t (m + 1))) (K.mul (cN K (m + 1 + 1)) (pwR K (pwR K h qq) (4 * (m + 1)))) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have th : K.mul (SU2Strong4D.two K) h = K.one := by
    show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
  have t0 : O.le K.zero (SU2Strong4D.two K) := by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
  -- 2 t^{m+1} ≤ 2 h^{33(m+1)} = h^{32(m+1)} · h^m ≤ h^{32(m+1)} ≤ (m+2) h^{32(m+1)}
  have p := SU2Strong4D.pw_mono K O h1 t _ ht0 ht (m + 1)
  have q := O.mul_le_mul_right _ _ _ p t0
  rw [K.mul_comm _ (SU2Strong4D.two K), K.mul_comm _ (SU2Strong4D.two K)] at q
  have e : K.mul (SU2Strong4D.two K) (pwR K (pwR K h (4 * qq + 1)) (m + 1)) = K.mul (pwR K (pwR K h qq) (4 * (m + 1))) (pwR K h m) := by
    rw [Migdal.pw_pw, Migdal.pw_pw, show (4 * qq + 1) * (m + 1) = qq * (4 * (m + 1)) + m + 1 by lib_nat]
    show _ = K.mul _ (pwR K h m)
    rw [show qq * (4 * (m + 1)) + m + 1 = (qq * (4 * (m + 1)) + m) + 1 from rfl]
    show K.mul (SU2Strong4D.two K) (K.mul (pwR K h (qq * (4 * (m + 1)) + m)) h) = _
    rw [GKS.pwR_add]
    have : K.mul (SU2Strong4D.two K) (K.mul (K.mul (pwR K h (qq * (4 * (m + 1)))) (pwR K h m)) h)
        = K.mul (K.mul (pwR K h (qq * (4 * (m + 1)))) (pwR K h m)) (K.mul (SU2Strong4D.two K) h) := by lib_ring
    rw [this, th, K.mul_comm _ K.one, K.one_mul]
  rw [e] at q
  have hn := AreaLower.pw_nn K O (pwR K h qq) (AreaLower.pw_nn K O h h0 h1 qq) h1 (4 * (m + 1))
  have r1 : O.le (K.mul (pwR K (pwR K h qq) (4 * (m + 1))) (pwR K h m)) (pwR K (pwR K h qq) (4 * (m + 1))) := by
    have := O.mul_le_mul_right _ _ _ (pw_anti K O h1 h h0 hle m 0 (Nat.zero_le m)) hn
    show O.le _ _
    rw [K.mul_comm (pwR K h m)] at this
    have e1 : pwR K h 0 = K.one := rfl
    rw [e1, K.mul_comm K.one, K.mul_comm _ K.one, K.one_mul] at this
    exact this
  have r2 : O.le (pwR K (pwR K h qq) (4 * (m + 1))) (K.mul (cN K (m + 1 + 1)) (pwR K (pwR K h qq) (4 * (m + 1)))) := by
    have g := cN_mono K O h1 1 (m + 2) (Nat.succ_le_succ (Nat.zero_le _))
    rw [cN_one] at g
    have := O.mul_le_mul_right _ _ _ g hn; rw [K.one_mul] at this; exact this
  exact O.le_trans _ _ _ q (O.le_trans _ _ _ r1 r2)

/-- the normalised Wilson character coefficients, truncated -/
def wl (t : R) (Kk L : Nat) : List R :=
  lmap (fun n => K.mul (cS K O F t n (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))) 0 L

/-- **Wilson weights at β = 2t ≤ 2⁻³² lie in the invariant cone Cone(2⁻⁸).** -/
theorem wilson_cone (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h t : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (ht0 : O.le K.zero t) (ht : O.le t (pwR K h 33)) (Kk L : Nat) :
    Bd K O (pwR K h 8) 0 (wl K O F t Kk L) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have th : O.le t h := O.le_trans _ _ _ ht (pw_anti K O h1 h h0 hle 33 1 (by decide) |> fun x => by
    have e : pwR K h 1 = h := by show K.mul K.one h = h; rw [K.one_mul]
    rw [e] at x; exact x)
  have tt : O.le (K.mul t t) h := by
    have a := O.mul_le_mul_right _ _ _ th ht0
    have c := O.mul_le_mul_right _ _ _ th h0
    rw [K.mul_comm t h] at c
    have b := O.mul_le_mul_right _ _ _ hle h0
    rw [K.one_mul] at b
    exact O.le_trans _ _ _ (O.le_trans _ _ _ a c) b
  apply bd_of_fun
  intro n
  have rb := ratio_bound K O F h1 h10 t ht0 n Kk
  refine ⟨rb.1, ?_⟩
  cases n with
  | zero =>
    have c0 := c0_ge_one K O F h1 h10 t ht0 Kk
    rw [F.mul_inv _ (MigdalStrong.ne_of_one_le K O h1 h10 _ c0)]
    show O.le K.one (K.mul (K.add K.one K.zero) K.one)
    rw [DetChain.add_zero', K.one_mul]; exact O.le_refl _
  | succ m =>
    have b1 := MigdalStrong.bS_le K O F h1 h10 t ht0 (m + 1) (Kk + 1)
    have g := MigdalStrong.gsum_le_two K O h1 h (K.mul t t) hh h0 (O.mul_nonneg _ _ ht0 ht0) tt (Kk + 1)
    have b2 := O.mul_le_mul_right _ _ _ g (AreaLower.pw_nn K O t ht0 h1 (m + 1))
    rw [K.mul_comm _ (pwR K t _), K.mul_comm (MigdalStrong.two K) (pwR K t _)] at b2
    have tw := two_tn_le K O h1 h t hh h0 ht0 ht m
    rw [K.mul_comm (SU2Strong4D.two K)] at tw
    exact O.le_trans _ _ _ rb.2 (O.le_trans _ _ _ b1 (O.le_trans _ _ _ b2 tw))

/-- **The 4D Migdal flow from the Wilson weight at β ≤ 2⁻³² stays in the strong-coupling cone forever.** -/
theorem wilson_forever (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h t : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (ht0 : O.le K.zero t) (ht : O.le t (pwR K h 33)) (Kk L ML : Nat) :
    ∀ k, Bd K O (pwR K h 8) 0 (K.one :: mkIt K O F ML (wl K O F t Kk (L + 1)).tail k) := by
  have w := wilson_cone K O F h1 h10 h t hh h0 ht0 ht Kk (L + 1)
  have c0 := c0_ge_one K O F h1 h10 t ht0 Kk
  have hd : wl K O F t Kk (L + 1) = K.one :: (wl K O F t Kk (L + 1)).tail := by
    show K.mul _ (F.inv _) :: _ = K.one :: _
    rw [F.mul_inv _ (MigdalStrong.ne_of_one_le K O h1 h10 _ c0)]
    rfl
  rw [hd] at w
  exact mk_forever K O F h1 h10 h hh h0 _ w ML

/-- **Wilson weights at β ≤ 2⁻¹⁶ stay in the invariant cone Cone(1/16) under every 4D Migdal step.** -/
theorem wilson_forever16 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h t : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (ht0 : O.le K.zero t) (ht : O.le t (pwR K h 17)) (Kk L ML : Nat) :
    ∀ k, Bd K O (pwR K h 4) 0 (K.one :: mkIt K O F ML (wl K O F t Kk (L + 1)).tail k) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have th : O.le t h := O.le_trans _ _ _ ht (by
    have x := pw_anti K O h1 h h0 hle 17 1 (by decide)
    have e : pwR K h 1 = h := by show K.mul K.one h = h; rw [K.one_mul]
    rw [e] at x; exact x)
  have tt : O.le (K.mul t t) h := by
    have a := O.mul_le_mul_right _ _ _ th ht0
    have c := O.mul_le_mul_right _ _ _ th h0
    rw [K.mul_comm t h] at c
    have b := O.mul_le_mul_right _ _ _ hle h0
    rw [K.one_mul] at b
    exact O.le_trans _ _ _ (O.le_trans _ _ _ a c) b
  have w : Bd K O (pwR K h 4) 0 (wl K O F t Kk (L + 1)) := by
    apply bd_of_fun
    intro n
    have rb := ratio_bound K O F h1 h10 t ht0 n Kk
    refine ⟨rb.1, ?_⟩
    cases n with
    | zero =>
      have c0 := c0_ge_one K O F h1 h10 t ht0 Kk
      rw [F.mul_inv _ (MigdalStrong.ne_of_one_le K O h1 h10 _ c0)]
      show O.le K.one (K.mul (K.add K.one K.zero) K.one)
      rw [DetChain.add_zero', K.one_mul]; exact O.le_refl _
    | succ m =>
      have b1 := MigdalStrong.bS_le K O F h1 h10 t ht0 (m + 1) (Kk + 1)
      have g := MigdalStrong.gsum_le_two K O h1 h (K.mul t t) hh h0 (O.mul_nonneg _ _ ht0 ht0) tt (Kk + 1)
      have b2 := O.mul_le_mul_right _ _ _ g (AreaLower.pw_nn K O t ht0 h1 (m + 1))
      rw [K.mul_comm _ (pwR K t _), K.mul_comm (MigdalStrong.two K) (pwR K t _)] at b2
      have tw := two_tn_le_gen K O 4 h1 h t hh h0 ht0 ht m
      rw [K.mul_comm (SU2Strong4D.two K)] at tw
      exact O.le_trans _ _ _ rb.2 (O.le_trans _ _ _ b1 (O.le_trans _ _ _ b2 tw))
  have c0 := c0_ge_one K O F h1 h10 t ht0 Kk
  have hd : wl K O F t Kk (L + 1) = K.one :: (wl K O F t Kk (L + 1)).tail := by
    show K.mul _ (F.inv _) :: _ = K.one :: _
    rw [F.mul_inv _ (MigdalStrong.ne_of_one_le K O h1 h10 _ c0)]
    rfl
  rw [hd] at w
  exact mk_forever16 K O F h1 h10 h hh h0 _ w ML

end WilsonCone

#print axioms WilsonCone.two_tn_le
#print axioms WilsonCone.wilson_cone
#print axioms WilsonCone.wilson_forever
#print axioms WilsonCone.wilson_forever16

ym/CertSound.lean

/-
  ym/CertSound — T45, middle range, step 1: soundness of fixed-point bounds for sums (the kernel-checked certificate).

  A list x of field elements is bracketed at scale E by natural-number lists l, u when, entrywise,
      cN(l_n) ≤ x_n · E ≤ cN(u_n)   and   0 ≤ x_n.
  The certificate (work/cert) supplies such brackets; the Lean kernel checks the natural-number inequalities with
  Nat.ble (axiom-free, kernel-accelerated). Here:
    dotN           Σ_i l_i · g(m+i) over ℕ
    dot_bracket    if a is bracketed by (la, ua) at scale E and g is bracketed by (gl, gu) at scale E', then
                   cN(Σ la_i gl_i) ≤ (Σ a_i g_i) · (E E') ≤ cN(Σ ua_i gu_i)
  (used twice for the Clebsch–Gordan product sums c_n = Σ_j a_j Σ_k b_k cg(j,k,n)).

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.WilsonCone

namespace CertSound

open YMChain LinkIntegral SU2Haar SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- bracket predicate -/
def Brk (E : R) : List R → List Nat → List Nat → Prop
  | [], [], [] => True
  | [], [], _ :: _ => False
  | [], _ :: _, _ => False
  | _ :: _, [], _ => False
  | _ :: _, _ :: _, [] => False
  | x :: xs, l :: ls, u :: us => (O.le K.zero x ∧ O.le (cN K l) (K.mul x E) ∧ O.le (K.mul x E) (cN K u)) ∧ Brk E xs ls us

def dotN : Nat → List Nat → (Nat → Nat) → Nat
  | _, [], _ => 0
  | m, c :: l, g => c * g m + dotN (m + 1) l g

/-- **Bracketing a sum.** -/
theorem dot_bracket (h1 : O.le K.zero K.one) (E E' : R) (hE : O.le K.zero E) (hE' : O.le K.zero E')
    (g : Nat → R) (gl gu : Nat → Nat)
    (hg : ∀ j, O.le K.zero (g j) ∧ O.le (cN K (gl j)) (K.mul (g j) E') ∧ O.le (K.mul (g j) E') (cN K (gu j))) :
    ∀ (m : Nat) (a : List R) (la ua : List Nat), Brk K O E a la ua →
      O.le K.zero (dot K m a g) ∧
      O.le (cN K (dotN m la gl)) (K.mul (dot K m a g) (K.mul E E')) ∧
      O.le (K.mul (dot K m a g) (K.mul E E')) (cN K (dotN m ua gu))
  | _, [], [], [], _ => ⟨O.le_refl _, by
      show O.le K.zero (K.mul K.zero _); rw [K.mul_comm, DetChain.mul_zero]; exact O.le_refl _, by
      show O.le (K.mul K.zero _) K.zero; rw [K.mul_comm, DetChain.mul_zero]; exact O.le_refl _⟩
  | m, x :: a, l :: la, u :: ua, h => by
    obtain ⟨⟨hx0, hxl, hxu⟩, hr⟩ := h
    obtain ⟨g0, gl', gu'⟩ := hg m
    obtain ⟨r0, rl, ru⟩ := dot_bracket h1 E E' hE hE' g gl gu hg (m + 1) a la ua hr
    have ex : K.mul (dot K m (x :: a) g) (K.mul E E')
        = K.add (K.mul (K.mul x E) (K.mul (g m) E')) (K.mul (dot K (m + 1) a g) (K.mul E E')) := by
      show K.mul (K.add (K.mul x (g m)) (dot K (m + 1) a g)) (K.mul E E') = _
      lib_ring
    refine ⟨?_, ?_, ?_⟩
    · have := O.add_le_add _ _ _ _ (O.mul_nonneg _ _ hx0 g0) r0; rw [K.zero_add] at this; exact this
    · rw [ex]
      show O.le (cN K (l * gl m + dotN (m + 1) la gl)) _
      rw [SU2Char.cN_add, SU2CharExp.cN_mul]
      refine O.add_le_add _ _ _ _ ?_ rl
      -- cN l · cN (gl m) ≤ (x E) · (g E')
      have a1 := O.mul_le_mul_right _ _ _ hxl (SU2CharExp.cN_nn K O h1 (gl m))
      have a2 := O.mul_le_mul_right _ _ _ gl' (O.mul_nonneg _ _ hx0 hE)
      rw [K.mul_comm (cN K (gl m)) (K.mul x E), K.mul_comm (K.mul (g m) E') (K.mul x E)] at a2
      exact O.le_trans _ _ _ a1 a2
    · rw [ex]
      show O.le _ (cN K (u * gu m + dotN (m + 1) ua gu))
      rw [SU2Char.cN_add, SU2CharExp.cN_mul]
      refine O.add_le_add _ _ _ _ ?_ ru
      have a1 := O.mul_le_mul_right _ _ _ gu' (O.mul_nonneg _ _ hx0 hE)
      rw [K.mul_comm (K.mul (g m) E') (K.mul x E), K.mul_comm (cN K (gu m)) (K.mul x E)] at a1
      have a2 := O.mul_le_mul_right _ _ _ hxu (SU2CharExp.cN_nn K O h1 (gu m))
      exact O.le_trans _ _ _ a1 a2
  | _, [], [], _ :: _, h => h.elim
  | _, [], _ :: _, _, h => h.elim
  | _, _ :: _, [], _, h => h.elim
  | _, _ :: _, _ :: _, [], h => h.elim

/-- natural-number Clebsch–Gordan sum -/
def cgcN (la lb : List Nat) (n : Nat) : Nat := dotN 0 la (fun j => dotN 0 lb (fun k => Fusion.cg j k n))

/-- **Bracketing a Clebsch–Gordan product sum** at scale E·(E·1). -/
theorem cgc_bracket (h1 : O.le K.zero K.one) (E : R) (hE : O.le K.zero E) (a b : List R) (la ua lb ub : List Nat)
    (ha : Brk K O E a la ua) (hb : Brk K O E b lb ub) (n : Nat) :
    O.le K.zero (MigdalCone.cgc K a b n) ∧
    O.le (cN K (cgcN la lb n)) (K.mul (MigdalCone.cgc K a b n) (K.mul E (K.mul E K.one))) ∧
    O.le (K.mul (MigdalCone.cgc K a b n) (K.mul E (K.mul E K.one))) (cN K (cgcN ua ub n)) := by
  have inner : ∀ j, O.le K.zero (dot K 0 b (fun k => cN K (Fusion.cg j k n))) ∧
      O.le (cN K (dotN 0 lb (fun k => Fusion.cg j k n))) (K.mul (dot K 0 b (fun k => cN K (Fusion.cg j k n))) (K.mul E K.one)) ∧
      O.le (K.mul (dot K 0 b (fun k => cN K (Fusion.cg j k n))) (K.mul E K.one)) (cN K (dotN 0 ub (fun k => Fusion.cg j k n))) := by
    intro j
    exact dot_bracket K O h1 E K.one hE h1 (fun k => cN K (Fusion.cg j k n)) (fun k => Fusion.cg j k n)
      (fun k => Fusion.cg j k n)
      (fun k => ⟨SU2CharExp.cN_nn K O h1 _, by rw [K.mul_comm, K.one_mul]; exact O.le_refl _,
        by rw [K.mul_comm, K.one_mul]; exact O.le_refl _⟩) 0 b lb ub hb
  exact dot_bracket K O h1 E (K.mul E K.one) hE (by rw [K.mul_comm, K.one_mul]; exact hE) _ _ _ inner 0 a la ua ha

variable (F : OField.OF K O)

/-- **Bracketing a quotient.** If cN(SLp) ≤ p·E₂ ≤ cN(SUp), cN(SLq) ≤ q·E₂ ≤ cN(SUq), q > 0, E₂ > 0 and the natural
  numbers satisfy u·SLq ≥ SUp·D, l·SUq ≤ SLp·D, then cN(l) ≤ (p/q)·cN(D) ≤ cN(u). -/
theorem quot_bracket (h1 : O.le K.zero K.one) (p q E2 : R) (SLp SUp SLq SUq D l u : Nat)
    (hp0 : O.le K.zero p) (hq0 : O.le K.zero q) (hq : q ≠ K.zero) (hE0 : O.le K.zero E2) (hE : E2 ≠ K.zero)
    (hpL : O.le (cN K SLp) (K.mul p E2)) (hpU : O.le (K.mul p E2) (cN K SUp))
    (hqL : O.le (cN K SLq) (K.mul q E2)) (hqU : O.le (K.mul q E2) (cN K SUq))
    (cu : SUp * D ≤ u * SLq) (cl : l * SUq ≤ SLp * D) :
    O.le K.zero (K.mul p (F.inv q)) ∧
    O.le (cN K l) (K.mul (K.mul p (F.inv q)) (cN K D)) ∧ O.le (K.mul (K.mul p (F.inv q)) (cN K D)) (cN K u) := by
  have iq := F.inv_pos q hq0 hq
  have qi := F.mul_inv q hq
  have dn := SU2CharExp.cN_nn K O h1 D
  refine ⟨O.mul_nonneg _ _ hp0 iq, ?_, ?_⟩
  · -- l q E2 ≤ p D E2  ⇒  l q ≤ p D  ⇒  l ≤ (p/q) D
    have a1 := O.mul_le_mul_right _ _ _ hqU (SU2CharExp.cN_nn K O h1 l)
    have a2 := SU2CharExp.cN_mono K O h1 _ _ cl
    rw [SU2CharExp.cN_mul, SU2CharExp.cN_mul] at a2
    have a3 := O.mul_le_mul_right _ _ _ hpL dn
    have chain := O.le_trans _ _ _ (O.le_trans _ _ _ (by rw [K.mul_comm (K.mul q E2) (cN K l), K.mul_comm (cN K SUq) (cN K l)] at a1; exact a1) a2) a3
    -- chain : cN l · (q E2) ≤ (p E2) · cN D
    have e1 : K.mul (cN K l) (K.mul q E2) = K.mul (K.mul (cN K l) q) E2 := by lib_ring
    have e2 : K.mul (K.mul p E2) (cN K D) = K.mul (K.mul p (cN K D)) E2 := by lib_ring
    rw [e1, e2] at chain
    have c := O.pos_cancel _ _ _ hE0 hE chain
    have m := O.mul_le_mul_right _ _ _ c iq
    have e3 : K.mul (K.mul (cN K l) q) (F.inv q) = cN K l := by
      rw [K.mul_assoc, qi, K.mul_comm, K.one_mul]
    have e4 : K.mul (K.mul p (cN K D)) (F.inv q) = K.mul (K.mul p (F.inv q)) (cN K D) := by lib_ring
    rw [e3, e4] at m; exact m
  · have a1 := O.mul_le_mul_right _ _ _ hpU dn
    have a2 := SU2CharExp.cN_mono K O h1 _ _ cu
    rw [SU2CharExp.cN_mul, SU2CharExp.cN_mul] at a2
    have a3 := O.mul_le_mul_right _ _ _ hqL (SU2CharExp.cN_nn K O h1 u)
    rw [K.mul_comm (cN K SLq), K.mul_comm (K.mul q E2)] at a3
    have chain := O.le_trans _ _ _ (O.le_trans _ _ _ a1 a2) a3
    -- chain : (p E2) cN D ≤ cN u · (q E2)
    have e1 : K.mul (K.mul p E2) (cN K D) = K.mul (K.mul p (cN K D)) E2 := by lib_ring
    have e2 : K.mul (cN K u) (K.mul q E2) = K.mul (K.mul (cN K u) q) E2 := by lib_ring
    rw [e1, e2] at chain
    have c := O.pos_cancel _ _ _ hE0 hE chain
    have m := O.mul_le_mul_right _ _ _ c iq
    have e3 : K.mul (K.mul (cN K u) q) (F.inv q) = cN K u := by
      rw [K.mul_assoc, qi, K.mul_comm, K.one_mul]
    have e4 : K.mul (K.mul p (cN K D)) (F.inv q) = K.mul (K.mul p (F.inv q)) (cN K D) := by lib_ring
    rw [e3, e4] at m; exact m

theorem cN_pow (a : Nat) : ∀ k, cN K (a ^ k) = pwR K (cN K a) k
  | 0 => SU2CharExp.cN_one K
  | k + 1 => by show cN K (a ^ k * a) = K.mul (pwR K (cN K a) k) (cN K a); rw [SU2CharExp.cN_mul, cN_pow a k]

/-- **Bracketing the decimation** x = y⁴ / d³ (d = cN(n+1)): from cN(l) ≤ y·cN(D) ≤ cN(u) and the natural-number checks
  u⁴ ≤ u'·(n+1)³·D³, l'·(n+1)³·D³ ≤ l⁴, get cN(l') ≤ x·cN(D) ≤ cN(u'). -/
theorem dec_bracket (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (y : R) (n D l u l' u' : Nat) (hD : 1 ≤ D)
    (hy0 : O.le K.zero y) (hyL : O.le (cN K l) (K.mul y (cN K D))) (hyU : O.le (K.mul y (cN K D)) (cN K u))
    (cu : u ^ 4 ≤ u' * (n + 1) ^ 3 * D ^ 3) (cl : l' * (n + 1) ^ 3 * D ^ 3 ≤ l ^ 4) :
    let x := K.mul (pwR K y 4) (pwR K (F.inv (cN K (n + 1))) 3)
    O.le K.zero x ∧ O.le (cN K l') (K.mul x (cN K D)) ∧ O.le (K.mul x (cN K D)) (cN K u') := by
  intro x
  have dp := SU2CharExp.pos_of_ge_one K O h1 h10 (n + 1) (Nat.succ_le_succ (Nat.zero_le n))
  have Dp := SU2CharExp.pos_of_ge_one K O h1 h10 D hD
  let d := cN K (n + 1)
  let i := F.inv d
  have ip := F.inv_pos d dp.1 dp.2
  have di := F.mul_inv d dp.2
  have D3p : O.le K.zero (pwR K (cN K D) 3) := AreaLower.pw_nn K O _ Dp.1 h1 3
  have D3n : pwR K (cN K D) 3 ≠ K.zero := SpecGap.pw_ne K O h1 _ Dp.1 Dp.2 3
  have i3p : O.le K.zero (pwR K i 3) := AreaLower.pw_nn K O _ ip h1 3
  -- key identity: x·D·D³·d³ = (yD)⁴
  have key : K.mul (K.mul (K.mul x (cN K D)) (pwR K (cN K D) 3)) (pwR K d 3) = pwR K (K.mul y (cN K D)) 4 := by
    show K.mul (K.mul (K.mul (K.mul (pwR K y 4) (pwR K i 3)) (cN K D)) (pwR K (cN K D) 3)) (pwR K d 3) = _
    have : K.mul (K.mul (K.mul (K.mul (pwR K y 4) (pwR K i 3)) (cN K D)) (pwR K (cN K D) 3)) (pwR K d 3)
        = K.mul (K.mul (pwR K y 4) (K.mul (cN K D) (pwR K (cN K D) 3))) (pwR K (K.mul d i) 3) := by
      rw [GKS2.pwR_mul]; lib_ring
    rw [this, di, SU2Strong4D.pw_one, K.mul_comm _ K.one, K.one_mul, GKS2.pwR_mul]
    show _ = K.mul (pwR K y 4) (K.mul (K.mul (K.mul (K.mul K.one (cN K D)) (cN K D)) (cN K D)) (cN K D))
    show K.mul (pwR K y 4) (K.mul (cN K D) (K.mul (K.mul (K.mul K.one (cN K D)) (cN K D)) (cN K D))) = _
    lib_ring
  have x0 : O.le K.zero x := O.mul_nonneg _ _ (AreaLower.pw_nn K O y hy0 h1 4) i3p
  have yD0 : O.le K.zero (K.mul y (cN K D)) := O.mul_nonneg _ _ hy0 Dp.1
  have d3p : O.le K.zero (pwR K d 3) := AreaLower.pw_nn K O _ dp.1 h1 3
  have d3n : pwR K d 3 ≠ K.zero := SpecGap.pw_ne K O h1 _ dp.1 dp.2 3
  have Dd0 : O.le K.zero (K.mul (pwR K (cN K D) 3) (pwR K d 3)) := O.mul_nonneg _ _ D3p d3p
  have Ddn : K.mul (pwR K (cN K D) 3) (pwR K d 3) ≠ K.zero := fun e => D3n (O.pos_mul_eq_zero _ _ d3p d3n
    (by rw [K.mul_comm]; exact e))
  have keyR : K.mul (K.mul x (cN K D)) (K.mul (pwR K (cN K D) 3) (pwR K d 3)) = pwR K (K.mul y (cN K D)) 4 := by
    rw [← key]; lib_ring
  refine ⟨x0, ?_, ?_⟩
  · -- cN l' · (D³ d³) ≤ cN(l')(n+1)³D³ ≤ l⁴ ≤ (yD)⁴ = x D (D³ d³)
    have c1 := SU2CharExp.cN_mono K O h1 _ _ cl
    rw [SU2CharExp.cN_mul, SU2CharExp.cN_mul, cN_pow, cN_pow, cN_pow] at c1
    have c2 := SU2Strong4D.pw_mono K O h1 _ _ (SU2CharExp.cN_nn K O h1 l) hyL 4
    have c3 := O.le_trans _ _ _ c1 c2
    rw [← keyR] at c3
    have e : K.mul (K.mul (cN K l') (pwR K d 3)) (pwR K (cN K D) 3) = K.mul (cN K l') (K.mul (pwR K (cN K D) 3) (pwR K d 3)) := by lib_ring
    rw [e] at c3
    exact O.pos_cancel _ _ _ Dd0 Ddn c3
  · have c1 := SU2CharExp.cN_mono K O h1 _ _ cu
    rw [SU2CharExp.cN_mul, SU2CharExp.cN_mul, cN_pow, cN_pow, cN_pow] at c1
    have c2 := SU2Strong4D.pw_mono K O h1 _ _ yD0 hyU 4
    have c3 := O.le_trans _ _ _ c2 c1
    rw [← keyR] at c3
    have e : K.mul (K.mul (cN K u') (pwR K d 3)) (pwR K (cN K D) 3) = K.mul (cN K u') (K.mul (pwR K (cN K D) 3) (pwR K d 3)) := by lib_ring
    rw [e] at c3
    exact O.pos_cancel _ _ _ Dd0 Ddn c3

/-- the certificate check for a normalised product -/
def checkQ (la ua lb ub : List Nat) (D : Nat) : Nat → List Nat → List Nat → Bool
  | _, [], [] => true
  | _, [], _ :: _ => false
  | _, _ :: _, [] => false
  | m, l :: ls, u :: us =>
    (Nat.ble (cgcN ua ub m * D) (u * cgcN la lb 0) && Nat.ble (l * cgcN ua ub 0) (cgcN la lb m * D)) &&
      checkQ la ua lb ub D (m + 1) ls us

theorem and_true_l {a b : Bool} (h : (a && b) = true) : a = true := by cases a <;> cases b <;> first | rfl | exact Bool.noConfusion h
theorem and_true_r {a b : Bool} (h : (a && b) = true) : b = true := by cases a <;> cases b <;> first | rfl | exact Bool.noConfusion h

/-- **A passing product check brackets the normalised product.** -/
theorem nprod_bracket (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (D : Nat) (hD : 1 ≤ D)
    (a b : List R) (la ua lb ub : List Nat) (ha : Brk K O (cN K D) a la ua) (hb : Brk K O (cN K D) b lb ub)
    (h0 : 1 ≤ cgcN la lb 0) :
    ∀ (m : Nat) (l' u' : List Nat), checkQ la ua lb ub D m l' u' = true →
      Brk K O (cN K D) (MigdalCone.lmap (fun n => K.mul (MigdalCone.cgc K a b n) (F.inv (MigdalCone.cgc K a b 0))) m l'.length) l' u'
  | _, [], [], _ => trivial
  | _, [], _ :: _, h => Bool.noConfusion h
  | _, _ :: _, [], h => Bool.noConfusion h
  | m, l :: ls, u :: us, h => by
    have hc := and_true_l h
    have hr := and_true_r h
    have cu := Nat.le_of_ble_eq_true (and_true_l hc)
    have cl := Nat.le_of_ble_eq_true (and_true_r hc)
    have Dp := SU2CharExp.pos_of_ge_one K O h1 h10 D hD
    let E2 := K.mul (cN K D) (K.mul (cN K D) K.one)
    have E20 : O.le K.zero E2 := O.mul_nonneg _ _ Dp.1 (O.mul_nonneg _ _ Dp.1 h1)
    have E2n : E2 ≠ K.zero := by
      intro e
      have : K.mul (cN K D) K.one = K.zero := O.pos_mul_eq_zero _ _ Dp.1 Dp.2 e
      rw [K.mul_comm, K.one_mul] at this; exact Dp.2 this
    obtain ⟨p0, pL, pU⟩ := cgc_bracket K O h1 (cN K D) Dp.1 a b la ua lb ub ha hb m
    obtain ⟨q0, qL, qU⟩ := cgc_bracket K O h1 (cN K D) Dp.1 a b la ua lb ub ha hb 0
    have qn : MigdalCone.cgc K a b 0 ≠ K.zero := by
      intro e
      rw [e, K.mul_comm, DetChain.mul_zero] at qL
      have g := SU2CharExp.cN_mono K O h1 1 _ h0
      rw [SU2CharExp.cN_one] at g
      exact h10 (O.le_antisymm _ _ (O.le_trans _ _ _ g qL) h1)
    have qb := quot_bracket K O F h1 (MigdalCone.cgc K a b m) (MigdalCone.cgc K a b 0) E2
      (cgcN la lb m) (cgcN ua ub m) (cgcN la lb 0) (cgcN ua ub 0) D l u p0 q0 qn E20 E2n pL pU qL qU
      cu cl
    exact ⟨⟨qb.1, qb.2.1, qb.2.2⟩, nprod_bracket h1 h10 D hD a b la ua lb ub ha hb h0 (m + 1) ls us hr⟩

/-- the certificate check for the decimation -/
def checkD (D : Nat) : Nat → List Nat → List Nat → List Nat → List Nat → Bool
  | _, [], [], [], [] => true
  | m, l :: ls, u :: us, l' :: ls', u' :: us' =>
    (Nat.ble (u ^ 4) (u' * (m + 1) ^ 3 * D ^ 3) && Nat.ble (l' * (m + 1) ^ 3 * D ^ 3) (l ^ 4)) && checkD D (m + 1) ls us ls' us'
  | _, [], [], [], _ :: _ => false
  | _, [], [], _ :: _, _ => false
  | _, [], _ :: _, _, _ => false
  | _, _ :: _, [], _, _ => false
  | _, _ :: _, _ :: _, [], _ => false
  | _, _ :: _, _ :: _, _ :: _, [] => false

/-- **A passing decimation check brackets the decimated list.** -/
theorem dec_list_bracket (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (D : Nat) (hD : 1 ≤ D) :
    ∀ (m : Nat) (y : List R) (l u l' u' : List Nat), Brk K O (cN K D) y l u → checkD D m l u l' u' = true →
      Brk K O (cN K D) (MigdalCone.decM K O F m y) l' u'
  | _, [], [], [], [], [], _, _ => trivial
  | m, x :: y, l :: ls, u :: us, l' :: ls', u' :: us', hb, hc => by
    have c1 := and_true_l hc
    have cu := Nat.le_of_ble_eq_true (and_true_l c1)
    have cl := Nat.le_of_ble_eq_true (and_true_r c1)
    have db := dec_bracket K O F h1 h10 x m D l u l' u' hD hb.1.1 hb.1.2.1 hb.1.2.2 cu cl
    exact ⟨⟨db.1, db.2.1, db.2.2⟩, dec_list_bracket h1 h10 D hD (m + 1) y ls us ls' us' hb.2 (and_true_r hc)⟩
  | _, [], [], [], [], _ :: _, _, h => Bool.noConfusion h
  | _, [], [], [], _ :: _, _, _, h => Bool.noConfusion h
  | _, [], [], _ :: _, _, _, h, _ => h.elim
  | _, [], _ :: _, _, _, _, h, _ => h.elim
  | _, _ :: _, [], _, _, _, h, _ => h.elim
  | _, _ :: _, _ :: _, [], _, _, h, _ => h.elim
  | _, _ :: _, _ :: _, _ :: _, [], _, _, h => Bool.noConfusion h
  | _, _ :: _, _ :: _, _ :: _, _ :: _, [], _, h => Bool.noConfusion h

/-- the certificate check for one 4D Migdal step -/
def checkStep (D L : Nat) (la ua l2 u2 l4 u4 lo uo : List Nat) : Bool :=
  Nat.ble 1 (cgcN la la 0) && checkQ la ua la ua D 0 l2 u2 && Nat.beq l2.length (L + 1) &&
  Nat.ble 1 (cgcN l2 l2 0) && checkQ l2 u2 l2 u2 D 0 l4 u4 && Nat.beq l4.length (L + 1) &&
  checkD D 0 l4 u4 lo uo

/-- **A passing step check brackets the output of one Migdal step.** -/
theorem step_bracket (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (D L : Nat) (hD : 1 ≤ D)
    (a : List R) (la ua l2 u2 l4 u4 lo uo : List Nat) (ha : Brk K O (cN K D) (K.one :: a) la ua)
    (hc : checkStep D L la ua l2 u2 l4 u4 lo uo = true) :
    Brk K O (cN K D) (MigdalCone.mk K O F L a) lo uo := by
  have c6 := and_true_l hc; have c7 := and_true_r hc
  have c5 := and_true_l c6; have h6 := and_true_r c6
  have c4 := and_true_l c5; have h5 := and_true_r c5
  have c3 := and_true_l c4; have h4 := and_true_r c4
  have c2 := and_true_l c3; have h3 := and_true_r c3
  have c1 := and_true_l c2; have h2 := and_true_r c2
  have s0 := Nat.le_of_ble_eq_true c1
  have s2 := Nat.le_of_ble_eq_true h4
  have len2 : l2.length = L + 1 := Nat.eq_of_beq_eq_true h3
  have len4 : l4.length = L + 1 := Nat.eq_of_beq_eq_true h6
  have b2 := nprod_bracket K O F h1 h10 D hD _ _ la ua la ua ha ha s0 0 l2 u2 h2
  rw [len2] at b2
  have b4 := nprod_bracket K O F h1 h10 D hD _ _ l2 u2 l2 u2 b2 b2 s2 0 l4 u4 h5
  rw [len4] at b4
  exact dec_list_bracket K O F h1 h10 D hD 0 _ l4 u4 lo uo b4 c7

/-- the cone check: u_m · 2^{16m} ≤ (m+1) · D -/
def checkCone (D : Nat) : Nat → List Nat → Bool
  | _, [] => true
  | m, u :: us => Nat.ble (u * 2 ^ (16 * m)) ((m + 1) * D) && checkCone D (m + 1) us

/-- **Upper brackets passing the cone check put the list in Cone(1/16).** -/
theorem cone_of_check (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (D : Nat) (hD : 1 ≤ D) :
    ∀ (m : Nat) (x : List R) (l u : List Nat), Brk K O (cN K D) x l u → checkCone D m u = true →
      ConeBound.Bd K O (pwR K h 4) m x
  | _, [], [], [], _, _ => trivial
  | m, y :: x, l :: ls, u :: us, hb, hc => by
    refine ⟨⟨hb.1.1, ?_⟩, cone_of_check h1 h10 h hh h0 D hD (m + 1) x ls us hb.2 (and_true_r hc)⟩
    have c := Nat.le_of_ble_eq_true (and_true_l hc)
    have Dp := SU2CharExp.pos_of_ge_one K O h1 h10 D hD
    have two_eq : cN K 2 = SU2Strong4D.two K := by
      show K.add K.one (K.add K.one K.zero) = K.add K.one K.one; rw [DetChain.add_zero']
    have g := SU2CharExp.cN_mono K O h1 _ _ c
    rw [SU2CharExp.cN_mul, SU2CharExp.cN_mul, cN_pow, two_eq] at g
    -- y D 2^{16m} ≤ u 2^{16m} ≤ (m+1) D
    have pw0 : O.le K.zero (pwR K (SU2Strong4D.two K) (16 * m)) :=
      AreaLower.pw_nn K O _ (by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this) h1 _
    have a1 := O.mul_le_mul_right _ _ _ hb.1.2.2 pw0
    have a2 := O.le_trans _ _ _ a1 g
    have e : K.mul (K.mul y (cN K D)) (pwR K (SU2Strong4D.two K) (16 * m))
        = K.mul (K.mul y (pwR K (SU2Strong4D.two K) (16 * m))) (cN K D) := by lib_ring
    rw [e] at a2
    have a3 := O.pos_cancel _ _ _ Dp.1 Dp.2 a2
    -- multiply by h^{16m}: y ≤ (m+1) h^{16m}
    have hp := AreaLower.pw_nn K O h h0 h1 (16 * m)
    have a4 := O.mul_le_mul_right _ _ _ a3 hp
    have th : K.mul (SU2Strong4D.two K) h = K.one := by
      show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
    have e2 : K.mul (K.mul y (pwR K (SU2Strong4D.two K) (16 * m))) (pwR K h (16 * m)) = y := by
      rw [K.mul_assoc, ← GKS2.pwR_mul, th, SU2Strong4D.pw_one, K.mul_comm, K.one_mul]
    rw [e2] at a4
    rw [Migdal.pw_pw, show 4 * (4 * m) = 16 * m by lib_nat]
    exact a4
  | _, [], [], _ :: _, h, _ => h.elim
  | _, [], _ :: _, _, h, _ => h.elim
  | _, _ :: _, [], _, h, _ => h.elim
  | _, _ :: _, _ :: _, [], h, _ => h.elim

structure StepData where
  l2 : List Nat
  u2 : List Nat
  l4 : List Nat
  u4 : List Nat
  lo : List Nat
  uo : List Nat

def checkChain (D L : Nat) : List Nat → List Nat → List StepData → Bool
  | _, ua, [] => checkCone D 1 ua.tail
  | la, ua, s :: ss => checkStep D L la ua s.l2 s.u2 s.l4 s.u4 s.lo s.uo && checkChain D L s.lo s.uo ss

theorem mkIt_shift (L : Nat) (a : List R) : ∀ k, MigdalCone.mkIt K O F L a (k + 1)
    = MigdalCone.mkIt K O F L (MigdalCone.mk K O F L a).tail k
  | 0 => rfl
  | k + 1 => by
    show (MigdalCone.mk K O F L (MigdalCone.mkIt K O F L a (k + 1))).tail = (MigdalCone.mk K O F L (MigdalCone.mkIt K O F L _ k)).tail
    rw [mkIt_shift L a k]

theorem brk_nn (E : R) : ∀ (x : List R) (l u : List Nat), Brk K O E x l u → ∀ y, memL y x → O.le K.zero y
  | [], [], [], _, _, h => h.elim
  | z :: x, _ :: l, _ :: u, hb, y, hy => by
    cases hy with
    | inl e => rw [e]; exact hb.1.1
    | inr h => exact brk_nn E x l u hb.2 y h
  | [], [], _ :: _, h, _, _ => h.elim
  | [], _ :: _, _, h, _, _ => h.elim
  | _ :: _, [], _, h, _, _ => h.elim
  | _ :: _, _ :: _, [], h, _, _ => h.elim

/-- **Soundness of the chain certificate:** a passing chain check from a bracket of the start puts the k-th Migdal
  iterate (k = number of steps in the certificate) in Cone(1/16). -/
theorem chain_sound (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (D L : Nat) (hD : 1 ≤ D) :
    ∀ (ss : List StepData) (a : List R) (la ua : List Nat), Brk K O (cN K D) (K.one :: a) la ua →
      checkChain D L la ua ss = true → ConeBound.Bd K O (pwR K h 4) 0 (K.one :: MigdalCone.mkIt K O F L a ss.length)
  | [], a, la, ua, hb, hc => by
    cases la with
    | nil => cases ua with
      | nil => exact hb.elim
      | cons _ _ => exact hb.elim
    | cons l la =>
      cases ua with
      | nil => exact hb.elim
      | cons u ua =>
        refine ⟨⟨h1, ?_⟩, cone_of_check K O h1 h10 h hh h0 D hD 1 a la ua hb.2 hc⟩
        show O.le K.one (K.mul (cN K 1) K.one)
        rw [SU2CharExp.cN_one, K.one_mul]; exact O.le_refl _
  | s :: ss, a, la, ua, hb, hc => by
    have st := step_bracket K O F h1 h10 D L hD a la ua s.l2 s.u2 s.l4 s.u4 s.lo s.uo hb (and_true_l hc)
    have hann : ∀ x, memL x a → O.le K.zero x := fun x hx => brk_nn K O _ _ _ _ hb x (Or.inr hx)
    rw [MigdalCone.mk_head_nn K O F h1 h10 a hann L] at st
    have ih := chain_sound h1 h10 h hh h0 D L hD ss _ s.lo s.uo st (and_true_r hc)
    show ConeBound.Bd K O (pwR K h 4) 0 (K.one :: MigdalCone.mkIt K O F L a (ss.length + 1))
    rw [mkIt_shift]
    exact ih

/-! ### exact leading entry (the normalised coefficient A₀ = 1) -/

theorem brk_head (D : Nat) (h1 : O.le K.zero K.one) (x : List R) (l u : List Nat) (hb : Brk K O (cN K D) x l u) :
    Brk K O (cN K D) (K.one :: x) (D :: l) (D :: u) := by
  refine ⟨⟨h1, ?_, ?_⟩, hb⟩ <;> (rw [K.one_mul]; exact O.le_refl _)

/-- step data with exact head D (only the tails are stored) -/
structure StepT where
  l2 : List Nat
  u2 : List Nat
  l4 : List Nat
  u4 : List Nat
  lo : List Nat
  uo : List Nat

def checkStepT (D L : Nat) (la ua : List Nat) (s : StepT) : Bool :=
  Nat.ble 1 (cgcN (D :: la) (D :: la) 0) && checkQ (D :: la) (D :: ua) (D :: la) (D :: ua) D 1 s.l2 s.u2 &&
  Nat.beq s.l2.length L && Nat.ble 1 (cgcN (D :: s.l2) (D :: s.l2) 0) &&
  checkQ (D :: s.l2) (D :: s.u2) (D :: s.l2) (D :: s.u2) D 1 s.l4 s.u4 && Nat.beq s.l4.length L &&
  checkD D 1 s.l4 s.u4 s.lo s.uo

theorem step_bracketT (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (D L : Nat) (hD : 1 ≤ D)
    (a : List R) (la ua : List Nat) (ha : Brk K O (cN K D) a la ua) (s : StepT)
    (hc : checkStepT D L la ua s = true) :
    ∃ t, MigdalCone.mk K O F L a = K.one :: t ∧ Brk K O (cN K D) t s.lo s.uo := by
  have c6 := and_true_l hc; have c7 := and_true_r hc
  have c5 := and_true_l c6; have h6 := and_true_r c6
  have c4 := and_true_l c5; have h5 := and_true_r c5
  have c3 := and_true_l c4; have h4 := and_true_r c4
  have c2 := and_true_l c3; have h3 := and_true_r c3
  have c1 := and_true_l c2; have h2 := and_true_r c2
  have hb := brk_head K O D h1 a la ua ha
  have hann : ∀ x, memL x a → O.le K.zero x := fun x hx => brk_nn K O _ _ _ _ ha x hx
  have len2 : s.l2.length = L := Nat.eq_of_beq_eq_true h3
  have len4 : s.l4.length = L := Nat.eq_of_beq_eq_true h6
  -- P2 = 1 :: t2
  let f2 := fun n => K.mul (MigdalCone.cgc K (K.one :: a) (K.one :: a) n) (F.inv (MigdalCone.cgc K (K.one :: a) (K.one :: a) 0))
  have b2 := nprod_bracket K O F h1 h10 D hD _ _ _ _ _ _ hb hb (Nat.le_of_ble_eq_true c1) 1 s.l2 s.u2 h2
  rw [len2] at b2
  have e2 : MigdalCone.nprod K O F (K.one :: a) (K.one :: a) (L + 1) = K.one :: MigdalCone.lmap f2 1 L :=
    MigdalCone.nprod_head K O F h1 h10 a a hann hann L
  have hb2 := brk_head K O D h1 _ _ _ b2
  have t2nn : ∀ x, memL x (MigdalCone.lmap f2 1 L) → O.le K.zero x := fun x hx => brk_nn K O _ _ _ _ b2 x hx
  let f4 := fun n => K.mul (MigdalCone.cgc K (K.one :: MigdalCone.lmap f2 1 L) (K.one :: MigdalCone.lmap f2 1 L) n)
    (F.inv (MigdalCone.cgc K (K.one :: MigdalCone.lmap f2 1 L) (K.one :: MigdalCone.lmap f2 1 L) 0))
  have b4 := nprod_bracket K O F h1 h10 D hD _ _ _ _ _ _ hb2 hb2 (Nat.le_of_ble_eq_true h4) 1 s.l4 s.u4 h5
  rw [len4] at b4
  have e4 : MigdalCone.nprod K O F (K.one :: MigdalCone.lmap f2 1 L) (K.one :: MigdalCone.lmap f2 1 L) (L + 1)
      = K.one :: MigdalCone.lmap f4 1 L := MigdalCone.nprod_head K O F h1 h10 _ _ t2nn t2nn L
  have bo := dec_list_bracket K O F h1 h10 D hD 1 _ s.l4 s.u4 s.lo s.uo b4 c7
  refine ⟨MigdalCone.decM K O F 1 (MigdalCone.lmap f4 1 L), ?_, bo⟩
  show MigdalCone.decM K O F 0 (MigdalCone.nprod K O F (MigdalCone.nprod K O F (K.one :: a) (K.one :: a) (L + 1))
    (MigdalCone.nprod K O F (K.one :: a) (K.one :: a) (L + 1)) (L + 1)) = _
  rw [e2, e4, MigdalCone.decM_head K O F h10]

def checkChainT (D L : Nat) : List Nat → List Nat → List StepT → Bool
  | _, ua, [] => checkCone D 1 ua
  | la, ua, s :: ss => checkStepT D L la ua s && checkChainT D L s.lo s.uo ss

theorem chain_soundT (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (D L : Nat) (hD : 1 ≤ D) :
    ∀ (ss : List StepT) (a : List R) (la ua : List Nat), Brk K O (cN K D) a la ua →
      checkChainT D L la ua ss = true → ConeBound.Bd K O (pwR K h 4) 0 (K.one :: MigdalCone.mkIt K O F L a ss.length)
  | [], a, la, ua, hb, hc => by
    refine ⟨⟨h1, ?_⟩, cone_of_check K O h1 h10 h hh h0 D hD 1 a la ua hb hc⟩
    show O.le K.one (K.mul (cN K 1) K.one)
    rw [SU2CharExp.cN_one, K.one_mul]; exact O.le_refl _
  | s :: ss, a, la, ua, hb, hc => by
    obtain ⟨t, et, bt⟩ := step_bracketT K O F h1 h10 D L hD a la ua hb s (and_true_l hc)
    have ih := chain_soundT h1 h10 h hh h0 D L hD ss t s.lo s.uo bt (and_true_r hc)
    show ConeBound.Bd K O (pwR K h 4) 0 (K.one :: MigdalCone.mkIt K O F L a (ss.length + 1))
    rw [mkIt_shift, et]
    exact ih

end CertSound

#print axioms CertSound.dot_bracket
#print axioms CertSound.quot_bracket
#print axioms CertSound.dec_bracket
#print axioms CertSound.cgc_bracket
#print axioms CertSound.nprod_bracket
#print axioms CertSound.dec_list_bracket
#print axioms CertSound.step_bracket
#print axioms CertSound.cone_of_check
#print axioms CertSound.chain_sound
#print axioms CertSound.chain_soundT

ym/CertWilson.lean

/-
  ym/CertWilson — T45, middle range, Lean side step 6: exact Wilson start values for the certificate.

  For t = a/b (t · cN(b) = cN(a)), the truncated character coefficients of the Wilson weight are exact rationals:
    Wn / Dn      W n 0 = 0,  W n (K+1) = (W n K + P(n+2K,0,n)·a^{n+2K}) · b²(n+2K+1)(n+2K+2),
                 Dn n K = b^{n+2K} · (n+2K)!
    cS_exact     cS t n K · cN(Dn n K) = cN(W n K)
    exact_quot   p·cN(Pd) = cN(Pn), q·cN(Qd) = cN(Qn), Qn, Pd, Qd ≥ 1 and natural checks
                 Pn·Qd·D ≤ u·(Qn·Pd), l·(Qn·Pd) ≤ Pn·Qd·D  ⇒  cN(l) ≤ (p/q)·cN(D) ≤ cN(u)
    checkStart / start_bracket   the Wilson list WilsonCone.wl is bracketed by the supplied (la, ua)
    certified_forever            start bracket + passing chain certificate ⇒ the 4D Migdal flow from the Wilson weight
                                 enters Cone(1/16) after the certified steps and stays there forever.

  Lean 4 core only, proofs by hand, no axioms.
-/
import ym.CertSound

namespace CertWilson

open YMChain LinkIntegral SU2Haar SU2Char SU2CharExp CertSound

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

def Wn (a b n : Nat) : Nat → Nat
  | 0 => 0
  | k + 1 => (Wn a b n k + P (n + 2 * k) 0 n * a ^ (n + 2 * k)) * (b * b * (n + 2 * k + 1) * (n + 2 * k + 2))

def Dn (b n k : Nat) : Nat := b ^ (n + 2 * k) * fact (n + 2 * k)

theorem fact_succ2 (m : Nat) : fact (m + 2) = fact m * (m + 1) * (m + 2) := rfl

/-- **Exact values.** -/
theorem cS_exact (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (a b n : Nat)
    (ht : K.mul t (cN K b) = cN K a) : ∀ k, K.mul (cS K O F t n k) (cN K (Dn b n k)) = cN K (Wn a b n k)
  | 0 => by show K.mul K.zero _ = cN K 0; rw [K.mul_comm, DetChain.mul_zero]; rfl
  | k + 1 => by
    have ih := cS_exact h1 h10 t a b n ht k
    let m := n + 2 * k
    have fp := pos_of_ge_one K O h1 h10 (fact m) (fact_pos m)
    have fi := F.mul_inv _ fp.2
    have eD : Dn b n (k + 1) = Dn b n k * (b * b * (m + 1) * (m + 2)) := by
      show b ^ (n + 2 * (k + 1)) * fact (n + 2 * (k + 1)) = b ^ m * fact m * (b * b * (m + 1) * (m + 2))
      rw [show n + 2 * (k + 1) = n + 2 * k + 2 by lib_nat, fact_succ2]
      show b ^ m * b * b * (fact m * (m + 1) * (m + 2)) = _
      lib_nat
    have eW : Wn a b n (k + 1) = (Wn a b n k + P m 0 n * a ^ m) * (b * b * (m + 1) * (m + 2)) := rfl
    have tm : K.mul (term K O F t n k) (cN K (Dn b n k)) = cN K (P m 0 n * a ^ m) := by
      show K.mul (K.mul (K.mul (pwR K t m) (F.inv (cN K (fact m)))) (cN K (P m 0 n))) (cN K (b ^ m * fact m)) = _
      rw [cN_mul, cN_mul, CertSound.cN_pow, CertSound.cN_pow, ← ht, GKS2.pwR_mul]
      have : K.mul (K.mul (K.mul (pwR K t m) (F.inv (cN K (fact m)))) (cN K (P m 0 n))) (K.mul (pwR K (cN K b) m) (cN K (fact m)))
          = K.mul (K.mul (cN K (P m 0 n)) (K.mul (pwR K t m) (pwR K (cN K b) m))) (K.mul (cN K (fact m)) (F.inv (cN K (fact m)))) := by lib_ring
      rw [this, fi, K.mul_comm _ K.one, K.one_mul]
    show K.mul (K.add (cS K O F t n k) (term K O F t n k)) (cN K (Dn b n (k + 1))) = cN K (Wn a b n (k + 1))
    rw [eD, eW, cN_mul K (Dn b n k) (b * b * (m + 1) * (m + 2)), cN_mul K (Wn a b n k + P m 0 n * a ^ m) (b * b * (m + 1) * (m + 2)), cN_add]
    have : K.mul (K.add (cS K O F t n k) (term K O F t n k)) (K.mul (cN K (Dn b n k)) (cN K (b * b * (m + 1) * (m + 2))))
        = K.mul (K.add (K.mul (cS K O F t n k) (cN K (Dn b n k))) (K.mul (term K O F t n k) (cN K (Dn b n k))))
            (cN K (b * b * (m + 1) * (m + 2))) := by lib_ring
    rw [this, ih, tm]

/-- **Bracket of an exact quotient.** -/
theorem exact_quot (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (p q : R) (Pn Pd Qn Qd D l u : Nat)
    (hp0 : O.le K.zero p) (hq0 : O.le K.zero q) (hPd : 1 ≤ Pd) (hQd : 1 ≤ Qd) (hQn : 1 ≤ Qn)
    (ep : K.mul p (cN K Pd) = cN K Pn) (eq : K.mul q (cN K Qd) = cN K Qn)
    (cu : Pn * Qd * D ≤ u * (Qn * Pd)) (cl : l * (Qn * Pd) ≤ Pn * Qd * D) :
    O.le K.zero (K.mul p (F.inv q)) ∧
    O.le (cN K l) (K.mul (K.mul p (F.inv q)) (cN K D)) ∧ O.le (K.mul (K.mul p (F.inv q)) (cN K D)) (cN K u) := by
  have Pp := pos_of_ge_one K O h1 h10 Pd hPd
  have Qp := pos_of_ge_one K O h1 h10 Qd hQd
  let E2 := K.mul (cN K Pd) (cN K Qd)
  have E20 : O.le K.zero E2 := O.mul_nonneg _ _ Pp.1 Qp.1
  have E2n : E2 ≠ K.zero := fun e => Qp.2 (O.pos_mul_eq_zero _ _ Pp.1 Pp.2 e)
  have pE : K.mul p E2 = cN K (Pn * Qd) := by
    show K.mul p (K.mul (cN K Pd) (cN K Qd)) = _
    rw [cN_mul, ← ep]; lib_ring
  have qE : K.mul q E2 = cN K (Qn * Pd) := by
    show K.mul q (K.mul (cN K Pd) (cN K Qd)) = _
    rw [cN_mul, ← eq]; lib_ring
  have qn : q ≠ K.zero := by
    intro e
    rw [e, K.mul_comm, DetChain.mul_zero] at eq
    have g := cN_mono K O h1 1 Qn hQn
    rw [cN_one, ← eq] at g
    exact h10 (O.le_antisymm _ _ g h1)
  exact CertSound.quot_bracket K O F h1 p q E2 (Pn * Qd) (Pn * Qd) (Qn * Pd) (Qn * Pd) D l u hp0 hq0 qn E20 E2n
    (by rw [pE]; exact O.le_refl _) (by rw [pE]; exact O.le_refl _) (by rw [qE]; exact O.le_refl _)
    (by rw [qE]; exact O.le_refl _) cu cl

theorem fact_ge (n : Nat) : 1 ≤ fact n := fact_pos n

theorem pow_ge (b k : Nat) (hb : 1 ≤ b) : 1 ≤ b ^ k := by
  induction k with
  | zero => exact Nat.le_refl 1
  | succ k ih => exact Nat.mul_le_mul ih hb

/-- the start check -/
def checkStart (a b Kk D : Nat) : Nat → List Nat → List Nat → Bool
  | _, [], [] => true
  | _, [], _ :: _ => false
  | _, _ :: _, [] => false
  | m, l :: ls, u :: us =>
    (Nat.ble (Wn a b m (Kk + 1) * Dn b 0 (Kk + 1) * D) (u * (Wn a b 0 (Kk + 1) * Dn b m (Kk + 1))) &&
      Nat.ble (l * (Wn a b 0 (Kk + 1) * Dn b m (Kk + 1))) (Wn a b m (Kk + 1) * Dn b 0 (Kk + 1) * D)) &&
      checkStart a b Kk D (m + 1) ls us

/-- **The Wilson start is bracketed.** -/
theorem start_bracket (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (a b Kk D : Nat)
    (hb : 1 ≤ b) (ht : K.mul t (cN K b) = cN K a) (hQn : 1 ≤ Wn a b 0 (Kk + 1)) :
    ∀ (m : Nat) (l u : List Nat), checkStart a b Kk D m l u = true →
      Brk K O (cN K D) (MigdalCone.lmap (fun n => K.mul (cS K O F t n (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))) m l.length) l u
  | _, [], [], _ => trivial
  | _, [], _ :: _, h => Bool.noConfusion h
  | _, _ :: _, [], h => Bool.noConfusion h
  | m, l :: ls, u :: us, h => by
    have hc := and_true_l h
    have cu := Nat.le_of_ble_eq_true (and_true_l hc)
    have cl := Nat.le_of_ble_eq_true (and_true_r hc)
    have eq := exact_quot K O F h1 h10 (cS K O F t m (Kk + 1)) (cS K O F t 0 (Kk + 1))
      (Wn a b m (Kk + 1)) (Dn b m (Kk + 1)) (Wn a b 0 (Kk + 1)) (Dn b 0 (Kk + 1)) D l u
      (cS_nn K O F h1 h10 t ht0 m (Kk + 1)) (cS_nn K O F h1 h10 t ht0 0 (Kk + 1))
      (Nat.mul_le_mul (pow_ge b _ hb) (fact_ge _)) (Nat.mul_le_mul (pow_ge b _ hb) (fact_ge _)) hQn
      (cS_exact K O F h1 h10 t a b m ht (Kk + 1)) (cS_exact K O F h1 h10 t a b 0 ht (Kk + 1)) cu cl
    exact ⟨⟨eq.1, eq.2.1, eq.2.2⟩, start_bracket h1 h10 t ht0 a b Kk D hb ht hQn (m + 1) ls us (and_true_r h)⟩

theorem mkIt_add (L : Nat) (x : List R) (s : Nat) : ∀ j, MigdalCone.mkIt K O F L x (s + j)
    = MigdalCone.mkIt K O F L (MigdalCone.mkIt K O F L x s) j
  | 0 => rfl
  | j + 1 => by
    show (MigdalCone.mk K O F L (MigdalCone.mkIt K O F L x (s + j))).tail = (MigdalCone.mk K O F L (MigdalCone.mkIt K O F L _ j)).tail
    rw [mkIt_add L x s j]

/-- **Certified: the 4D Migdal flow from the Wilson weight at t = a/b enters Cone(1/16) after the certified number of
  steps and stays there forever.** -/
theorem certified_forever (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (t : R) (ht0 : O.le K.zero t) (a b Kk D ML : Nat) (hb : 1 ≤ b) (hD : 1 ≤ D)
    (ht : K.mul t (cN K b) = cN K a) (l0 : Nat) (la ua : List Nat) (ss : List StepData)
    (hQn : Nat.ble 1 (Wn a b 0 (Kk + 1)) = true) (hS : checkStart a b Kk D 0 (l0 :: la) ua = true)
    (hC : checkChain D ML (l0 :: la) ua ss = true) :
    ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: MigdalCone.mkIt K O F ML (WilsonCone.wl K O F t Kk (la.length + 1)).tail (ss.length + j)) := by
  have sb := start_bracket K O F h1 h10 t ht0 a b Kk D hb ht (Nat.le_of_ble_eq_true hQn) 0 (l0 :: la) ua hS
  have c0 := c0_ge_one K O F h1 h10 t ht0 Kk
  have hd : WilsonCone.wl K O F t Kk (la.length + 1) = K.one :: (WilsonCone.wl K O F t Kk (la.length + 1)).tail := by
    show K.mul _ (F.inv _) :: _ = K.one :: _
    rw [F.mul_inv _ (MigdalStrong.ne_of_one_le K O h1 h10 _ c0)]
    rfl
  have sb' : Brk K O (cN K D) (WilsonCone.wl K O F t Kk (la.length + 1)) (l0 :: la) ua := sb
  rw [hd] at sb'
  have cs := chain_sound K O F h1 h10 h hh h0 D ML hD ss _ (l0 :: la) ua sb' hC
  intro j
  rw [mkIt_add]
  exact MigdalCone.mk_forever16 K O F h1 h10 h hh h0 _ cs ML j

/-- **Certified (exact head version).** -/
theorem certified_foreverT (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (t : R) (ht0 : O.le K.zero t) (a b Kk D ML : Nat) (hb : 1 ≤ b) (hD : 1 ≤ D)
    (ht : K.mul t (cN K b) = cN K a) (la ua : List Nat) (ss : List StepT)
    (hQn : Nat.ble 1 (Wn a b 0 (Kk + 1)) = true) (hS : checkStart a b Kk D 1 la ua = true)
    (hC : checkChainT D ML la ua ss = true) :
    ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: MigdalCone.mkIt K O F ML (WilsonCone.wl K O F t Kk (la.length + 1)).tail (ss.length + j)) := by
  have sb := start_bracket K O F h1 h10 t ht0 a b Kk D hb ht (Nat.le_of_ble_eq_true hQn) 1 la ua hS
  have c0 := c0_ge_one K O F h1 h10 t ht0 Kk
  have hd : (WilsonCone.wl K O F t Kk (la.length + 1)).tail
      = MigdalCone.lmap (fun n => K.mul (cS K O F t n (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))) 1 la.length := rfl
  have cs := chain_soundT K O F h1 h10 h hh h0 D ML hD ss _ la ua sb hC
  intro j
  rw [mkIt_add, hd]

  exact MigdalCone.mk_forever16 K O F h1 h10 h hh h0 _ cs ML j

/-! ### intervals of couplings -/

/-- the truncated character coefficients increase with t -/
theorem cS_mono (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t t' : R) (ht0 : O.le K.zero t) (htt : O.le t t')
    (n : Nat) : ∀ k, O.le (cS K O F t n k) (cS K O F t' n k)
  | 0 => O.le_refl _
  | k + 1 => by
    refine O.add_le_add _ _ _ _ (cS_mono h1 h10 t t' ht0 htt n k) ?_
    show O.le (K.mul (K.mul (pwR K t (n + 2 * k)) _) _) (K.mul (K.mul (pwR K t' (n + 2 * k)) _) _)
    have fp := pos_of_ge_one K O h1 h10 (fact (n + 2 * k)) (fact_pos _)
    have c := O.mul_nonneg _ _ (F.inv_pos _ fp.1 fp.2) (cN_nn K O h1 (P (n + 2 * k) 0 n))
    have p := SU2Strong4D.pw_mono K O h1 t t' ht0 htt (n + 2 * k)
    have := O.mul_le_mul_right _ _ _ p c
    have e1 : ∀ s : R, K.mul (K.mul (pwR K s (n + 2 * k)) (F.inv (cN K (fact (n + 2 * k))))) (cN K (P (n + 2 * k) 0 n))
        = K.mul (pwR K s (n + 2 * k)) (K.mul (F.inv (cN K (fact (n + 2 * k)))) (cN K (P (n + 2 * k) 0 n))) := fun s => by lib_ring
    rw [e1, e1]; exact this

/-- start check for an interval t ∈ [a1/b1, a2/b2] -/
def checkStartI (a1 b1 a2 b2 Kk D : Nat) : Nat → List Nat → List Nat → Bool
  | _, [], [] => true
  | _, [], _ :: _ => false
  | _, _ :: _, [] => false
  | m, l :: ls, u :: us =>
    let Pn1 := Wn a1 b1 m (Kk + 1); let Pd1 := Dn b1 m (Kk + 1)
    let Pn2 := Wn a2 b2 m (Kk + 1); let Pd2 := Dn b2 m (Kk + 1)
    let Qn1 := Wn a1 b1 0 (Kk + 1); let Qd1 := Dn b1 0 (Kk + 1)
    let Qn2 := Wn a2 b2 0 (Kk + 1); let Qd2 := Dn b2 0 (Kk + 1)
    -- p ∈ [Pn1/Pd1, Pn2/Pd2], q ∈ [Qn1/Qd1, Qn2/Qd2], scale E = Pd1 Pd2 Qd1 Qd2
    (Nat.ble (Pn2 * Pd1 * Qd1 * Qd2 * D) (u * (Qn1 * Qd2 * Pd1 * Pd2)) &&
      Nat.ble (l * (Qn2 * Qd1 * Pd1 * Pd2)) (Pn1 * Pd2 * Qd1 * Qd2 * D)) &&
      checkStartI a1 b1 a2 b2 Kk D (m + 1) ls us

theorem sandwich_scale (h1 : O.le K.zero K.one) (p plo phi : R) (A1 B1 A2 B2 X : Nat)
    (hlo : O.le plo p) (hhi : O.le p phi) (e1 : K.mul plo (cN K A1) = cN K B1) (e2 : K.mul phi (cN K A2) = cN K B2) :
    O.le (cN K (B1 * A2 * X)) (K.mul p (cN K (A1 * A2 * X))) ∧ O.le (K.mul p (cN K (A1 * A2 * X))) (cN K (B2 * A1 * X)) := by
  have E0 := cN_nn K O h1 (A1 * A2 * X)
  constructor
  · have m := O.mul_le_mul_right _ _ _ hlo E0
    have e : K.mul plo (cN K (A1 * A2 * X)) = cN K (B1 * A2 * X) := by
      rw [cN_mul, cN_mul, cN_mul, cN_mul, ← e1]; lib_ring
    rw [e] at m; exact m
  · have m := O.mul_le_mul_right _ _ _ hhi E0
    have e : K.mul phi (cN K (A1 * A2 * X)) = cN K (B2 * A1 * X) := by
      rw [cN_mul, cN_mul, cN_mul, cN_mul, ← e2]; lib_ring
    rw [e] at m; exact m

/-- **The Wilson list for every t ∈ [t₁, t₂] is bracketed.** -/
theorem start_bracketI (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t t1 t2 : R) (ht10 : O.le K.zero t1)
    (h1t : O.le t1 t) (ht2 : O.le t t2) (a1 b1 a2 b2 Kk D : Nat) (hb1 : 1 ≤ b1) (hb2 : 1 ≤ b2)
    (e1 : K.mul t1 (cN K b1) = cN K a1) (e2 : K.mul t2 (cN K b2) = cN K a2) (hQn : 1 ≤ Wn a1 b1 0 (Kk + 1)) :
    ∀ (m : Nat) (l u : List Nat), checkStartI a1 b1 a2 b2 Kk D m l u = true →
      Brk K O (cN K D) (MigdalCone.lmap (fun n => K.mul (cS K O F t n (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))) m l.length) l u
  | _, [], [], _ => trivial
  | _, [], _ :: _, h => Bool.noConfusion h
  | _, _ :: _, [], h => Bool.noConfusion h
  | m, l :: ls, u :: us, h => by
    have hc := and_true_l h
    have cu := Nat.le_of_ble_eq_true (and_true_l hc)
    have cl := Nat.le_of_ble_eq_true (and_true_r hc)
    have ht0 : O.le K.zero t := O.le_trans _ _ _ ht10 h1t
    let Pd1 := Dn b1 m (Kk + 1); let Pd2 := Dn b2 m (Kk + 1); let Qd1 := Dn b1 0 (Kk + 1); let Qd2 := Dn b2 0 (Kk + 1)
    have sp := sandwich_scale K O h1 (cS K O F t m (Kk + 1)) (cS K O F t1 m (Kk + 1)) (cS K O F t2 m (Kk + 1))
      Pd1 (Wn a1 b1 m (Kk + 1)) Pd2 (Wn a2 b2 m (Kk + 1)) (Qd1 * Qd2)
      (cS_mono K O F h1 h10 t1 t ht10 h1t m _) (cS_mono K O F h1 h10 t t2 ht0 ht2 m _)
      (cS_exact K O F h1 h10 t1 a1 b1 m e1 _) (cS_exact K O F h1 h10 t2 a2 b2 m e2 _)
    have sq := sandwich_scale K O h1 (cS K O F t 0 (Kk + 1)) (cS K O F t1 0 (Kk + 1)) (cS K O F t2 0 (Kk + 1))
      Qd1 (Wn a1 b1 0 (Kk + 1)) Qd2 (Wn a2 b2 0 (Kk + 1)) (Pd1 * Pd2)
      (cS_mono K O F h1 h10 t1 t ht10 h1t 0 _) (cS_mono K O F h1 h10 t t2 ht0 ht2 0 _)
      (cS_exact K O F h1 h10 t1 a1 b1 0 e1 _) (cS_exact K O F h1 h10 t2 a2 b2 0 e2 _)
    have eE : Qd1 * Qd2 * (Pd1 * Pd2) = Pd1 * Pd2 * (Qd1 * Qd2) := by lib_nat
    rw [eE] at sq
    let E2 := cN K (Pd1 * Pd2 * (Qd1 * Qd2))
    have Dpos : ∀ b n, 1 ≤ b → 1 ≤ Dn b n (Kk + 1) := fun b n hb => Nat.mul_le_mul (pow_ge b _ hb) (fact_ge _)
    have Ep : 1 ≤ Pd1 * Pd2 * (Qd1 * Qd2) :=
      Nat.mul_le_mul (Nat.mul_le_mul (Dpos b1 m hb1) (Dpos b2 m hb2)) (Nat.mul_le_mul (Dpos b1 0 hb1) (Dpos b2 0 hb2))
    have Epos := pos_of_ge_one K O h1 h10 _ Ep
    have q0 : O.le K.zero (cS K O F t 0 (Kk + 1)) := cS_nn K O F h1 h10 t ht0 0 _
    have qn : cS K O F t 0 (Kk + 1) ≠ K.zero := by
      intro e
      have lo := sq.1
      rw [e, K.mul_comm, DetChain.mul_zero] at lo
      have g := cN_mono K O h1 1 (Wn a1 b1 0 (Kk + 1) * Qd2 * (Pd1 * Pd2))
        (Nat.mul_le_mul (Nat.mul_le_mul hQn (Dpos b2 0 hb2)) (Nat.mul_le_mul (Dpos b1 m hb1) (Dpos b2 m hb2)))
      rw [cN_one] at g
      exact h10 (O.le_antisymm _ _ (O.le_trans _ _ _ g lo) h1)
    have qb := CertSound.quot_bracket K O F h1 (cS K O F t m (Kk + 1)) (cS K O F t 0 (Kk + 1)) E2
      (Wn a1 b1 m (Kk + 1) * Pd2 * (Qd1 * Qd2)) (Wn a2 b2 m (Kk + 1) * Pd1 * (Qd1 * Qd2))
      (Wn a1 b1 0 (Kk + 1) * Qd2 * (Pd1 * Pd2)) (Wn a2 b2 0 (Kk + 1) * Qd1 * (Pd1 * Pd2)) D l u
      (cS_nn K O F h1 h10 t ht0 m _) q0 qn Epos.1 Epos.2 sp.1 sp.2 sq.1 sq.2
      (by
        have e : Wn a2 b2 m (Kk + 1) * Pd1 * (Qd1 * Qd2) * D = Wn a2 b2 m (Kk + 1) * Pd1 * Qd1 * Qd2 * D := by lib_nat
        have e' : u * (Wn a1 b1 0 (Kk + 1) * Qd2 * (Pd1 * Pd2)) = u * (Wn a1 b1 0 (Kk + 1) * Qd2 * Pd1 * Pd2) := by lib_nat
        rw [e, e']; exact cu)
      (by
        have e : l * (Wn a2 b2 0 (Kk + 1) * Qd1 * (Pd1 * Pd2)) = l * (Wn a2 b2 0 (Kk + 1) * Qd1 * Pd1 * Pd2) := by lib_nat
        have e' : Wn a1 b1 m (Kk + 1) * Pd2 * (Qd1 * Qd2) * D = Wn a1 b1 m (Kk + 1) * Pd2 * Qd1 * Qd2 * D := by lib_nat
        rw [e, e']; exact cl)
    exact ⟨⟨qb.1, qb.2.1, qb.2.2⟩, start_bracketI h1 h10 t t1 t2 ht10 h1t ht2 a1 b1 a2 b2 Kk D hb1 hb2 e1 e2 hQn (m + 1) ls us (and_true_r h)⟩

/-- **Certified for every t in an interval.** -/
theorem certified_interval (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (t t1 t2 : R) (ht10 : O.le K.zero t1) (h1t : O.le t1 t) (ht2 : O.le t t2)
    (a1 b1 a2 b2 Kk D ML : Nat) (hb1 : 1 ≤ b1) (hb2 : 1 ≤ b2) (hD : 1 ≤ D)
    (e1 : K.mul t1 (cN K b1) = cN K a1) (e2 : K.mul t2 (cN K b2) = cN K a2) (la ua : List Nat) (ss : List StepT)
    (hQn : Nat.ble 1 (Wn a1 b1 0 (Kk + 1)) = true) (hS : checkStartI a1 b1 a2 b2 Kk D 1 la ua = true)
    (hC : checkChainT D ML la ua ss = true) :
    ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: MigdalCone.mkIt K O F ML (WilsonCone.wl K O F t Kk (la.length + 1)).tail (ss.length + j)) := by
  have sb := start_bracketI K O F h1 h10 t t1 t2 ht10 h1t ht2 a1 b1 a2 b2 Kk D hb1 hb2 e1 e2
    (Nat.le_of_ble_eq_true hQn) 1 la ua hS
  have hd : (WilsonCone.wl K O F t Kk (la.length + 1)).tail
      = MigdalCone.lmap (fun n => K.mul (cS K O F t n (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))) 1 la.length := rfl
  have cs := chain_soundT K O F h1 h10 h hh h0 D ML hD ss _ la ua sb hC
  intro j
  rw [mkIt_add, hd]
  exact MigdalCone.mk_forever16 K O F h1 h10 h hh h0 _ cs ML j

end CertWilson

#print axioms CertWilson.cS_exact
#print axioms CertWilson.start_bracket
#print axioms CertWilson.certified_forever
#print axioms CertWilson.certified_foreverT
#print axioms CertWilson.certified_interval

90.12 Every \(\beta \in [0,4]\) enters the strong-coupling cone

Proved: the truncated 4D Migdal flow from the Wilson weight enters \(\mathrm {Cone}(1/16)\) and stays there, for every \(\beta \in [0,4]\) (39 interval and 118 Taylor-model certificates, kernel-checked).

\[ 0\le t\le 2,\ \beta =2t \;\Longrightarrow \; \exists k\ \forall j,\ \mathrm {Bd}_{1/16}\big (\mathrm {mkIt}^{\,k+j}(\mathrm {wl}(t,29,11))\big ) \]

ym/Cover04.lean

/-
  ym/Cover04 — T45, B1q: every β ∈ [0, 4] (t = β/2 ∈ [0, 2]). CertCover29.cover01 (interval certificates, t ≤ 1/2)
  and TMCoverAll.cover_mid (Taylor-model certificates, 1/2 ≤ t ≤ 2), both at truncation Kk = 29, joined by CoverBridge.
  Lean 4 core only, no axioms.
-/
import ym.CertCover29
import ym.TMCoverAll
import ym.CoverBridge

namespace Cover04

open YMChain SU2Char CertCover MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **Every β ∈ [0, 4]: the truncated 4D Migdal flow (Kk = 29, L = 10) from the Wilson weight enters Cone(1/16)
  and stays there forever.** -/
theorem cover04 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 2)) :
    ∃ k, ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t 29 11).tail (k + j)) := by
  cases O.le_total t (rat K O F 1 2) with
  | inl hle =>
    obtain ⟨k, _, hk⟩ := CertCover29.cover01 K O F h1 h10 h hh h0 t ht0 hle
    exact ⟨k, hk⟩
  | inr hge =>
    exact TMCoverAll.cover_mid K O F h1 h10 h hh h0 t
      (CoverBridge.half_le K O F h1 h10 1018517988167243043134222844204689080525734196832968125318070224677190649881668353091698688 t hge)
      (CoverBridge.le_two K O h1 2037035976334486086268445688409378161051468393665936250636140449354381299763336706183397376 t ht)

end Cover04

#print axioms Cover04.cover04

#print axioms: 5 theorems, no axioms.

90.13 Cone weights are close to one

Proved: for a weight in \(\mathrm {Cone}(1/16)\) and every trace \(y\in [-2,2]\), \(|w(y)-1|\le 2^{-13}\) (character bound by algebra).

\[ |\chi _n(y)|\le n+1,\qquad \Big |\sum _{n\ge 1}a_n\chi _n(y)\Big |\le 2h^{14}=2^{-13}\quad (h=\tfrac 12) \]

ym/ConeGap.lean

/-
  ym/ConeGap — T45, G3: a weight in the cone gives the small-activity input of the strong-coupling gap (GapZd).
  For coefficients a_m, a_{m+1}, … in Cone(r) (0 ≤ a_n ≤ (n+1) r^{4n}) and functions χ_n with |χ_n| ≤ n+1
  (SU(2) characters), f = Σ a_n χ_n satisfies −ε ≤ f ≤ ε with ε = 2·(4r⁴)^m, whenever 2·4r⁴ ≤ 1.
  With r = 1/16 and m = 1: ε = 2⁻¹³. Lean 4 core only, no axioms.
-/
import ym.ConeBound
import ym.MigdalCone

namespace ConeGap

open YMChain LinkIntegral SU2Char ConeBound

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem neg_add (x y : R) : K.neg (K.add x y) = K.add (K.neg x) (K.neg y) := by
  apply Eq.symm
  apply DetChain.neg_unique
  rw [DetChain.add_mid4, DetChain.add_neg', DetChain.add_neg', K.zero_add]

theorem neg_le (a b : R) (h : O.le a b) : O.le (K.neg b) (K.neg a) := by
  have := O.add_le_add _ _ _ _ h (O.le_refl (K.add (K.neg a) (K.neg b)))
  have e1 : K.add a (K.add (K.neg a) (K.neg b)) = K.neg b := by
    rw [← K.add_assoc, K.add_comm a, DetChain.add_neg', K.zero_add]
  have e2 : K.add b (K.add (K.neg a) (K.neg b)) = K.neg a := by
    rw [K.add_comm (K.neg a), ← K.add_assoc, K.add_comm b, DetChain.add_neg', K.zero_add]
  rw [e1, e2] at this; exact this

/-- |Σ a_n χ_n| ≤ Σ a_n g_n for a_n ≥ 0 and |χ_n| ≤ g_n. -/
theorem dot_abs (g : Nat → R) (χ : Nat → R) (hχ : ∀ j, O.le (K.neg (g j)) (χ j) ∧ O.le (χ j) (g j)) :
    ∀ (m : Nat) (a : List R), (∀ x, memL x a → O.le K.zero x) →
      O.le (K.neg (SU2Char.dot K m a g)) (SU2Char.dot K m a χ) ∧ O.le (SU2Char.dot K m a χ) (SU2Char.dot K m a g)
  | _, [], _ => by
    refine ⟨?_, O.le_refl _⟩
    show O.le (K.neg K.zero) K.zero
    have e : K.neg K.zero = K.zero := by
      have := DetChain.add_neg' K K.zero; rw [DetChain.add_zero'] at this; exact this
    rw [e]; exact O.le_refl _
  | m, c :: a, h => by
    have hc : O.le K.zero c := h c (Or.inl rfl)
    obtain ⟨l, u⟩ := dot_abs g χ hχ (m + 1) a (fun x hx => h x (Or.inr hx))
    have l1 := O.mul_le_mul_right _ _ c (hχ m).1 hc
    have u1 := O.mul_le_mul_right _ _ c (hχ m).2 hc
    rw [DetChain.neg_mul_left] at l1
    constructor
    · show O.le (K.neg (K.add (K.mul c (g m)) (SU2Char.dot K (m + 1) a g))) (K.add (K.mul c (χ m)) (SU2Char.dot K (m + 1) a χ))
      rw [neg_add, K.mul_comm c, K.mul_comm c]
      exact O.add_le_add _ _ _ _ l1 l
    · show O.le (K.add (K.mul c (χ m)) (SU2Char.dot K (m + 1) a χ)) (K.add (K.mul c (g m)) (SU2Char.dot K (m + 1) a g))
      rw [K.mul_comm c, K.mul_comm c]
      exact O.add_le_add _ _ _ _ u1 u

theorem le_mul_both (a b c d : R) (hb : O.le K.zero b) (hc : O.le K.zero c) (h1 : O.le a b) (h2 : O.le c d) :
    O.le (K.mul a c) (K.mul b d) := by
  have s1 := O.mul_le_mul_right _ _ c h1 hc
  have s2 := O.mul_le_mul_right _ _ b h2 hb
  rw [K.mul_comm d, K.mul_comm c b] at s2
  exact O.le_trans _ _ _ s1 s2

/-- **Cone(r) ⇒ Σ (n+1) a_n ≤ 2 x^m, x = 4 r⁴, for 2x ≤ 1.** -/
theorem eps_bound (h1 : O.le K.zero K.one) (r x : R) (hr0 : O.le K.zero r)
    (hx : K.mul (MigdalCone.four K) (pwR K r 4) = x) (hxx : O.le (K.add x x) K.one)
    (m : Nat) (a : List R) (ha : Bd K O r m a) :
    O.le (SU2Char.dot K m a (fun j => cN K (j + 1))) (K.add (pwR K x m) (pwR K x m)) := by
  have hcn : ∀ j, O.le K.zero (cN K (j + 1)) := fun j => SU2CharExp.cN_nn K O h1 _
  have d := dot_le K O r (fun j => cN K (j + 1)) hcn m a ha
  have two0 : O.le K.zero (SU2Strong4D.two K) := by
    have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
  have hx0 : O.le K.zero x := by
    rw [← hx]
    exact O.mul_nonneg _ _ (O.mul_nonneg _ _ two0 two0) (AreaLower.pw_nn K O r hr0 h1 4)
  have term : ∀ j, O.le (K.mul (K.mul (cN K (j + 1)) (pwR K r (4 * j))) (cN K (j + 1))) (pwR K x j) := by
    intro j
    have p2 := AreaLower.pw_nn K O (SU2Strong4D.two K) two0 h1 j
    have pr := AreaLower.pw_nn K O r hr0 h1 (4 * j)
    have a1 := le_mul_both K O _ _ _ _ (O.mul_nonneg _ _ p2 pr) (hcn j)
      (O.mul_le_mul_right _ _ _ (cN_le_pow2 K O h1 j) pr) (cN_le_pow2 K O h1 j)
    have e : K.mul (K.mul (pwR K (SU2Strong4D.two K) j) (pwR K r (4 * j))) (pwR K (SU2Strong4D.two K) j) = pwR K x j := by
      rw [← hx, GKS2.pwR_mul]
      show _ = K.mul (pwR K (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) j) (pwR K (pwR K r 4) j)
      rw [GKS2.pwR_mul, AreaG.pw_exp_mul]
      show K.mul (K.mul (pwR K (SU2Strong4D.two K) j) (pwR K (pwR K r 4) j)) (pwR K (SU2Strong4D.two K) j)
        = K.mul (K.mul (pwR K (SU2Strong4D.two K) j) (pwR K (SU2Strong4D.two K) j)) (pwR K (pwR K r 4) j)
      rw [K.mul_assoc, K.mul_comm (pwR K (pwR K r 4) j), ← K.mul_assoc]
    rw [e] at a1; exact a1
  have s := Sm_le K O _ (fun j => pwR K x j) term m a.length
  have g := Sm_geom K O h1 x hx0 hxx a.length m
  exact O.le_trans _ _ _ d (O.le_trans _ _ _ s g)

/-- **Small activity from the cone:** f = Σ_{n ≥ m} a_n χ_n with a ∈ Cone(r), |χ_n| ≤ n+1 obeys |f| ≤ 2 (4r⁴)^m. -/
theorem activity (h1 : O.le K.zero K.one) (r x : R) (hr0 : O.le K.zero r)
    (hx : K.mul (MigdalCone.four K) (pwR K r 4) = x) (hxx : O.le (K.add x x) K.one)
    (m : Nat) (a : List R) (ha : Bd K O r m a) (χ : Nat → R)
    (hχ : ∀ j, O.le (K.neg (cN K (j + 1))) (χ j) ∧ O.le (χ j) (cN K (j + 1))) :
    O.le (K.neg (K.add (pwR K x m) (pwR K x m))) (SU2Char.dot K m a χ) ∧
      O.le (SU2Char.dot K m a χ) (K.add (pwR K x m) (pwR K x m)) := by
  have e := eps_bound K O h1 r x hr0 hx hxx m a ha
  obtain ⟨l, u⟩ := dot_abs K O (fun j => cN K (j + 1)) χ hχ m a (Bd_nn K O r m a ha)
  refine ⟨O.le_trans _ _ _ ?_ l, O.le_trans _ _ _ u e⟩
  exact neg_le K O _ _ e

/-- **Cone(1/16), from index 1: |Σ_{n≥1} a_n χ_n| ≤ 2⁻¹³ (h = 1/2: ε = h¹⁴ + h¹⁴).** -/
theorem activity16 (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (a : List R) (ha : Bd K O (pwR K h 4) 1 a) (χ : Nat → R)
    (hχ : ∀ j, O.le (K.neg (cN K (j + 1))) (χ j) ∧ O.le (χ j) (cN K (j + 1))) :
    O.le (K.neg (K.add (pwR K h 14) (pwR K h 14))) (SU2Char.dot K 1 a χ) ∧
      O.le (SU2Char.dot K 1 a χ) (K.add (pwR K h 14) (pwR K h 14)) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have fh : K.mul (MigdalCone.four K) (K.mul h h) = K.one := by
    show K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.mul h h) = K.one
    have e : K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.mul h h)
        = K.mul (K.add h h) (K.add h h) := by lib_ring
    rw [e, hh, K.one_mul]
  have hx : K.mul (MigdalCone.four K) (pwR K (pwR K h 4) 4) = pwR K h 14 := by
    rw [Migdal.pw_pw]
    show K.mul (MigdalCone.four K) (pwR K h (14 + 2)) = pwR K h 14
    rw [GKS.pwR_add]
    show K.mul (MigdalCone.four K) (K.mul (pwR K h 14) (K.mul (K.mul K.one h) h)) = pwR K h 14
    rw [K.one_mul, K.mul_comm (pwR K h 14), ← K.mul_assoc, fh, K.one_mul]
  have hxx : O.le (K.add (pwR K h 14) (pwR K h 14)) K.one := by
    have e : K.add (pwR K h 14) (pwR K h 14) = pwR K h 13 := by
      show K.add (K.mul (pwR K h 13) h) (K.mul (pwR K h 13) h) = pwR K h 13
      have d : K.add (K.mul (pwR K h 13) h) (K.mul (pwR K h 13) h) = K.mul (pwR K h 13) (K.add h h) := by lib_ring
      rw [d, hh, K.mul_comm, K.one_mul]
    rw [e]; exact pw_anti K O h1 h h0 hle 13 0 (by decide)
  have := activity K O h1 (pwR K h 4) (pwR K h 14) (AreaLower.pw_nn K O h h0 h1 4) hx hxx 1 a ha χ hχ
  rw [show pwR K (pwR K h 14) 1 = K.mul K.one (pwR K h 14) from rfl, K.one_mul] at this
  exact this

end ConeGap

#print axioms ConeGap.dot_abs
#print axioms ConeGap.eps_bound
#print axioms ConeGap.activity
#print axioms ConeGap.activity16

ym/ChiBound.lean

/-
  ym/ChiBound — T45, G3: the SU(2) character bound |χ_n(y)| ≤ n + 1 for −2 ≤ y ≤ 2 (y = tr U), by algebra only.
  Invariant: χ_{n+1}² + χ_n² − y χ_{n+1} χ_n = 1 (from χ_{n+2} = y χ_{n+1} − χ_n). With |y| ≤ 2 the form dominates
  (χ_{n+1} ∓ χ_n)², so |χ_{n+1}| ≤ |χ_n| + 1. Lean 4 core only, no axioms.
-/
import ym.ConeGap

namespace ChiBound

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- the n-th character at trace y -/
def c (y : R) (n : Nat) : R := ev K (chiC K n) y

theorem c_rec (y : R) (n : Nat) : c K y (n + 2) = K.add (K.mul y (c K y (n + 1))) (K.neg (c K y n)) := by
  have h := chi_rec K y n
  have e : c K y (n + 2) = K.add (K.add (c K y (n + 2)) (c K y n)) (K.neg (c K y n)) := by
    rw [K.add_assoc, K.add_comm (c K y n), DetChain.add_neg', DetChain.add_zero']
  rw [e]; unfold c; rw [h]

theorem c0 (y : R) : c K y 0 = K.one := by
  show K.add K.one (K.mul y K.zero) = K.one; lib_ring

theorem c1 (y : R) : c K y 1 = y := by
  show K.add K.zero (K.mul y (K.add K.one (K.mul y K.zero))) = y; lib_ring

/-- the invariant form -/
def Q (y u v : R) : R := K.add (K.add (K.mul u u) (K.mul v v)) (K.neg (K.mul y (K.mul u v)))

theorem Q_inv (y : R) : ∀ n, Q K y (c K y (n + 1)) (c K y n) = K.one
  | 0 => by
    rw [c1, c0]; show K.add (K.add (K.mul y y) (K.mul K.one K.one)) (K.neg (K.mul y (K.mul y K.one))) = K.one; lib_ring
  | n + 1 => by
    have ih := Q_inv y n
    rw [c_rec]
    have e : Q K y (K.add (K.mul y (c K y (n + 1))) (K.neg (c K y n))) (c K y (n + 1)) = Q K y (c K y (n + 1)) (c K y n) := by
      unfold Q; lib_ring
    rw [e]; exact ih

theorem neg_le (a b : R) (h : O.le a b) : O.le (K.neg b) (K.neg a) := ConeGap.neg_le K O a b h

/-- x² ≤ 1 ⇒ −1 ≤ x ≤ 1 -/
theorem sq_le_one (h1 : O.le K.zero K.one) (x : R) (hx : O.le (K.mul x x) K.one) :
    O.le (K.neg K.one) x ∧ O.le x K.one := by
  have up : ∀ z : R, O.le (K.mul z z) K.one → O.le z K.one := by
    intro z hz
    cases O.le_total z K.one with
    | inl h => exact h
    | inr h =>
      have z0 : O.le K.zero z := O.le_trans _ _ _ h1 h
      have := O.mul_le_mul_right _ _ z h z0
      rw [K.one_mul] at this
      exact O.le_trans _ _ _ this hz
  refine ⟨?_, up x hx⟩
  have hn : O.le (K.mul (K.neg x) (K.neg x)) K.one := by
    have e : K.mul (K.neg x) (K.neg x) = K.mul x x := by lib_ring
    rw [e]; exact hx
  have := neg_le K O _ _ (up (K.neg x) hn)
  rw [DetChain.neg_neg'] at this; exact this

/-- a = b + e, e ≤ 0 ⇒ a ≤ b -/
theorem le_of_eq_add (a b e : R) (hab : a = K.add b e) (he : O.le e K.zero) : O.le a b := by
  have := O.add_le_add _ _ _ _ (O.le_refl b) he
  rw [DetChain.add_zero', ← hab] at this; exact this

/-- p ≤ q ⇒ p + (−q) ≤ 0 -/
theorem sub_nonpos (p q : R) (h : O.le p q) : O.le (K.add p (K.neg q)) K.zero := by
  have := O.add_le_add _ _ _ _ h (O.le_refl (K.neg q))
  rw [K.add_comm q, DetChain.add_neg'] at this; exact this

/-- **One step:** Q(u, v) = 1, |y| ≤ 2, |v| ≤ k ⇒ |u| ≤ 1 + k. -/
theorem step (h1 : O.le K.zero K.one) (y u v k : R)
    (hy : O.le (K.neg (K.add K.one K.one)) y ∧ O.le y (K.add K.one K.one)) (hQ : Q K y u v = K.one)
    (hv : O.le (K.neg k) v ∧ O.le v k) :
    O.le (K.neg (K.add K.one k)) u ∧ O.le u (K.add K.one k) := by
  cases O.le_total K.zero (K.mul u v) with
  | inl hp =>
    -- (u − v)² ≤ 1
    have m := O.mul_le_mul_right _ _ _ hy.2 hp
    have sq : O.le (K.mul (K.add u (K.neg v)) (K.add u (K.neg v))) K.one := by
      have e : K.mul (K.add u (K.neg v)) (K.add u (K.neg v))
          = K.add (Q K y u v) (K.add (K.mul y (K.mul u v)) (K.neg (K.mul (K.add K.one K.one) (K.mul u v)))) := by
        unfold Q; lib_ring
      rw [hQ] at e
      exact le_of_eq_add K O _ _ _ e (sub_nonpos K O _ _ m)
    obtain ⟨l, r⟩ := sq_le_one K O h1 _ sq
    constructor
    · have := O.add_le_add _ _ _ _ l hv.1
      have e : K.add (K.add u (K.neg v)) v = u := by lib_ring
      have e2 : K.add (K.neg K.one) (K.neg k) = K.neg (K.add K.one k) := (ConeGap.neg_add K _ _).symm
      rw [e, e2] at this; exact this
    · have := O.add_le_add _ _ _ _ r hv.2
      have e : K.add (K.add u (K.neg v)) v = u := by lib_ring
      rw [e] at this; exact this
  | inr hn =>
    -- (u + v)² ≤ 1
    have hn' : O.le K.zero (K.neg (K.mul u v)) := by
      have := neg_le K O _ _ hn
      have e : K.neg K.zero = K.zero := by
        have := DetChain.add_neg' K K.zero; rw [DetChain.add_zero'] at this; exact this
      rw [e] at this; exact this
    have m := O.mul_le_mul_right _ _ _ hy.1 hn'
    have sq : O.le (K.mul (K.add u v) (K.add u v)) K.one := by
      have e : K.mul (K.add u v) (K.add u v)
          = K.add (Q K y u v) (K.add (K.mul (K.neg (K.add K.one K.one)) (K.neg (K.mul u v))) (K.neg (K.mul y (K.neg (K.mul u v))))) := by
        unfold Q; lib_ring
      rw [hQ] at e
      exact le_of_eq_add K O _ _ _ e (sub_nonpos K O _ _ m)
    obtain ⟨l, r⟩ := sq_le_one K O h1 _ sq
    have nv := neg_le K O _ _ hv.1
    rw [DetChain.neg_neg'] at nv
    have nv2 := neg_le K O _ _ hv.2
    constructor
    · have := O.add_le_add _ _ _ _ l nv2
      have e : K.add (K.add u v) (K.neg v) = u := by lib_ring
      have e2 : K.add (K.neg K.one) (K.neg k) = K.neg (K.add K.one k) := (ConeGap.neg_add K _ _).symm
      rw [e, e2] at this; exact this
    · have := O.add_le_add _ _ _ _ r nv
      have e : K.add (K.add u v) (K.neg v) = u := by lib_ring
      rw [e] at this; exact this

/-- **|χ_n(y)| ≤ n + 1 for |y| ≤ 2.** -/
theorem chi_bound (h1 : O.le K.zero K.one) (y : R)
    (hy : O.le (K.neg (K.add K.one K.one)) y ∧ O.le y (K.add K.one K.one)) :
    ∀ n, O.le (K.neg (cN K (n + 1))) (c K y n) ∧ O.le (c K y n) (cN K (n + 1)) := by
  have two : ∀ n, (O.le (K.neg (cN K (n + 1))) (c K y n) ∧ O.le (c K y n) (cN K (n + 1))) ∧
      (O.le (K.neg (cN K (n + 2))) (c K y (n + 1)) ∧ O.le (c K y (n + 1)) (cN K (n + 2))) := by
    intro n
    induction n with
    | zero =>
      have e1 : cN K 1 = K.one := SU2CharExp.cN_one K
      have e2 : cN K 2 = K.add K.one K.one := by show K.add K.one (K.add K.one K.zero) = _; rw [DetChain.add_zero']
      rw [e1, e2, c0, c1]
      exact ⟨⟨O.le_trans _ _ _ (neg_le K O _ _ h1) (by
        have e : K.neg K.zero = K.zero := by
          have := DetChain.add_neg' K K.zero; rw [DetChain.add_zero'] at this; exact this
        rw [e]; exact h1), O.le_refl _⟩, hy⟩
    | succ n ih =>
      refine ⟨ih.2, ?_⟩
      have s := step K O h1 y (c K y (n + 2)) (c K y (n + 1)) (cN K (n + 2)) hy (Q_inv K y (n + 1)) ih.2
      exact s
  exact fun n => (two n).1

/-- **G3: a weight in Cone(1/16) is uniformly close to 1.** For K.one :: a ∈ Cone(1/16) (h = 1/2) and every trace
  y ∈ [−2, 2], the plaquette weight w(y) = Σ_n a_n χ_n(y) = 1 + Σ_{n≥1} a_n χ_n(y) satisfies |w(y) − 1| ≤ 2·h¹⁴ = 2⁻¹³:
  the small-activity input (ε) of the strong-coupling gap GapZd.gap_Zd. -/
theorem weight_near_one (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (a : List R) (ha : ConeBound.Bd K O (pwR K h 4) 0 (K.one :: a)) (y : R)
    (hy : O.le (K.neg (K.add K.one K.one)) y ∧ O.le y (K.add K.one K.one)) :
    SU2Char.dot K 0 (K.one :: a) (c K y) = K.add K.one (SU2Char.dot K 1 a (c K y)) ∧
    O.le (K.neg (K.add (pwR K h 14) (pwR K h 14))) (SU2Char.dot K 1 a (c K y)) ∧
      O.le (SU2Char.dot K 1 a (c K y)) (K.add (pwR K h 14) (pwR K h 14)) := by
  refine ⟨?_, ConeGap.activity16 K O h1 h hh h0 a ha.2 (c K y) (chi_bound K O h1 y hy)⟩
  show K.add (K.mul K.one (c K y 0)) (SU2Char.dot K 1 a (c K y)) = _
  rw [K.one_mul, c0]

end ChiBound

#print axioms ChiBound.Q_inv
#print axioms ChiBound.chi_bound
#print axioms ChiBound.weight_near_one

#print axioms: 8 theorems, no axioms.

90.14 Cone weights have a uniform gap on \(\mathbb Z^4\)

Proved: a plaquette weight within \(2^{-13}\) of \(1\) satisfies every hypothesis of gap_Zd at \(d=4\); connected correlations decay like \(2^{-R}\).

\[ |w-1|\le 2^{-13}\ \Longrightarrow \ |\langle U_pU_q\rangle -\langle U_p\rangle \langle U_q\rangle |\le \tfrac 12 B\,2^{-R},\quad \kappa =1+2^{-10} \]

ym/GapInputs.lean

/-
  ym/GapInputs — T45, G4: the scalar hypotheses of the strong-coupling gap GapZd.gap_Zd at d = 4 hold for a weight
  within ε₀ = 2⁻¹³ of 1 (ChiBound.weight_near_one): with u = 1/2, x = 2⁻⁷, y = 2⁻⁶, κ = 1 + 2⁻¹⁰:
    hxy    x (1 + y)^{32} ≤ y              (Bernoulli: (1+z)^n (1 − n z) ≤ 1)
    hsmall (2ε + ε²)(κ²)^{33} ≤ u x        (ε = 2⁻¹³)
  and the normalisation w ↦ w/(1 + ε₀) gives c₀, m = 1/κ with m ≤ w ≤ 1. Lean 4 core only, no axioms.
-/
import ym.ChiBound

namespace GapInputs

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

abbrev two : R := K.add K.one K.one

/-- **Bernoulli, upper form:** (1 + z)^n (1 − n z) ≤ 1 for z ≥ 0. -/
theorem bern (h1 : O.le K.zero K.one) (z : R) (hz : O.le K.zero z) :
    ∀ n, O.le (K.mul (pwR K (K.add K.one z) n) (K.add K.one (K.neg (K.mul (cN K n) z)))) K.one
  | 0 => by
    show O.le (K.mul K.one (K.add K.one (K.neg (K.mul K.zero z)))) K.one
    have e : K.mul K.one (K.add K.one (K.neg (K.mul K.zero z))) = K.one := by lib_ring
    rw [e]; exact O.le_refl _
  | n + 1 => by
    have ih := bern h1 z hz n
    have P0 : O.le K.zero (pwR K (K.add K.one z) n) := by
      apply AreaLower.pw_nn K O _ _ h1
      have := O.add_le_add _ _ _ _ h1 hz; rw [K.zero_add] at this; exact this
    have N0 : O.le K.zero (K.add K.one (cN K n)) := by
      have := O.add_le_add _ _ _ _ h1 (SU2CharExp.cN_nn K O h1 n); rw [K.zero_add] at this; exact this
    have ee : O.le K.zero (K.mul (pwR K (K.add K.one z) n) (K.mul (K.add K.one (cN K n)) (K.mul z z))) :=
      O.mul_nonneg _ _ P0 (O.mul_nonneg _ _ N0 (O.mul_nonneg _ _ hz hz))
    have np : O.le (K.neg (K.mul (pwR K (K.add K.one z) n) (K.mul (K.add K.one (cN K n)) (K.mul z z)))) K.zero := by
      have := ChiBound.neg_le K O _ _ ee
      have e : K.neg K.zero = K.zero := by
        have := DetChain.add_neg' K K.zero; rw [DetChain.add_zero'] at this; exact this
      rw [e] at this; exact this
    have eq : K.mul (pwR K (K.add K.one z) (n + 1)) (K.add K.one (K.neg (K.mul (cN K (n + 1)) z)))
        = K.add (K.mul (pwR K (K.add K.one z) n) (K.add K.one (K.neg (K.mul (cN K n) z))))
            (K.neg (K.mul (pwR K (K.add K.one z) n) (K.mul (K.add K.one (cN K n)) (K.mul z z)))) := by
      show K.mul (K.mul (pwR K (K.add K.one z) n) (K.add K.one z)) (K.add K.one (K.neg (K.mul (K.add K.one (cN K n)) z))) = _
      lib_ring
    exact O.le_trans _ _ _ (ChiBound.le_of_eq_add K O _ _ _ eq np) ih

theorem cN_pow2 : ∀ k, cN K (2 ^ k) = pwR K (two K) k
  | 0 => SU2CharExp.cN_one K
  | k + 1 => by
    show cN K (2 ^ k * 2) = K.mul (pwR K (two K) k) (two K)
    rw [SU2CharExp.cN_mul, cN_pow2 k]
    show K.mul _ (K.add K.one (K.add K.one K.zero)) = _
    rw [DetChain.add_zero']

/-- 2^k h^k = 1 for h + h = 1 -/
theorem pow2_h (h : R) (hh : K.add h h = K.one) (k : Nat) : K.mul (pwR K (two K) k) (pwR K h k) = K.one := by
  rw [← GKS2.pwR_mul]
  have : K.mul (two K) h = K.one := by
    show K.mul (K.add K.one K.one) h = K.one
    have e : K.mul (K.add K.one K.one) h = K.add h h := by lib_ring
    rw [e, hh]
  rw [this, pw_one']

theorem hle_one (_h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) : O.le h K.one := by
  have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this

theorem one_sub_h (h : R) (hh : K.add h h = K.one) : K.add K.one (K.neg h) = h := by
  rw [← hh]; lib_ring

/-- c·z ≤ h ⇒ h ≤ 1 − c·z -/
theorem h_le_sub (h : R) (hh : K.add h h = K.one) (cz : R) (hc : O.le cz h) : O.le h (K.add K.one (K.neg cz)) := by
  have := O.add_le_add _ _ _ _ (O.le_refl K.one) (ChiBound.neg_le K O _ _ hc)
  rw [one_sub_h K h hh] at this; exact this

/-- **hxy at d = 4:** 2⁻⁷ (1 + 2⁻⁶)^{32} ≤ 2⁻⁶. -/
theorem hxy4 (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) :
    O.le (K.mul (pwR K h 7) (pwR K (K.add K.one (pwR K h 6)) (4 * (2 * 4)))) (pwR K h 6) := by
  have p6 := AreaLower.pw_nn K O h h0 h1 6
  have b : O.le (K.mul (pwR K (K.add K.one (pwR K h 6)) 32) (K.add K.one (K.neg (K.mul (cN K (2 ^ 5)) (pwR K h 6))))) K.one :=
    bern K O h1 (pwR K h 6) p6 32
  have e1 : K.mul (cN K (2 ^ 5)) (pwR K h 6) = h := by
    rw [cN_pow2]
    show K.mul (pwR K (two K) 5) (K.mul (pwR K h 5) h) = h
    rw [← K.mul_assoc, pow2_h K h hh, K.one_mul]
  rw [e1, one_sub_h K h hh] at b
  have := O.mul_le_mul_right _ _ (pwR K h 6) b p6
  rw [K.one_mul] at this
  have e : K.mul (pwR K h 7) (pwR K (K.add K.one (pwR K h 6)) (4 * (2 * 4)))
      = K.mul (K.mul (pwR K (K.add K.one (pwR K h 6)) 32) h) (pwR K h 6) := by
    show K.mul (K.mul (pwR K h 6) h) (pwR K (K.add K.one (pwR K h 6)) 32) = _
    lib_ring
  rw [e]; exact this

/-- κ^66 · h ≤ 1 for κ = 1 + 2⁻¹⁰ -/
theorem k66 (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) :
    O.le (K.mul (pwR K (K.add K.one (pwR K h 10)) 66) h) K.one := by
  have hl := hle_one K O h1 h hh h0
  have p10 := AreaLower.pw_nn K O h h0 h1 10
  have b := bern K O h1 (pwR K h 10) p10 66
  have c1 : O.le (K.mul (cN K 66) (pwR K h 10)) (K.mul (cN K (2 ^ 7)) (pwR K h 10)) :=
    O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 66 (2 ^ 7) (by decide)) p10
  have e1 : K.mul (cN K (2 ^ 7)) (pwR K h 10) = pwR K h 3 := by
    rw [cN_pow2]
    show K.mul (pwR K (two K) 7) (pwR K h (7 + 3)) = pwR K h 3
    rw [GKS.pwR_add, ← K.mul_assoc, pow2_h K h hh, K.one_mul]
  rw [e1] at c1
  have c2 : O.le (pwR K h 3) h := by
    have := ConeBound.pw_anti K O h1 h h0 hl 3 1 (by decide)
    rw [show pwR K h 1 = K.mul K.one h from rfl, K.one_mul] at this; exact this
  have hs := h_le_sub K O h hh _ (O.le_trans _ _ _ c1 c2)
  have P0 : O.le K.zero (pwR K (K.add K.one (pwR K h 10)) 66) := by
    apply AreaLower.pw_nn K O _ _ h1
    have := O.add_le_add _ _ _ _ h1 p10; rw [K.zero_add] at this; exact this
  have m := O.mul_le_mul_right _ _ _ hs P0
  rw [K.mul_comm h, K.mul_comm (K.add K.one _)] at m
  exact O.le_trans _ _ _ m b

/-- **hsmall at d = 4:** (2ε + ε²)(κ²)^{33} ≤ u x for ε = 2⁻¹³, κ = 1 + 2⁻¹⁰, u = 2⁻¹, x = 2⁻⁷. -/
theorem hsmall4 (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) :
    O.le (K.mul (K.add (K.add (pwR K h 13) (pwR K h 13)) (K.mul (pwR K h 13) (pwR K h 13)))
        (pwR K (K.mul (K.add K.one (pwR K h 10)) (K.add K.one (pwR K h 10))) (4 * (2 * 4) + 1)))
      (K.mul h (pwR K h 7)) := by
  have hl := hle_one K O h1 h hh h0
  have pe := AreaLower.pw_nn K O h h0 h1 13
  have e1 : O.le (pwR K h 13) K.one := by
    have := ConeBound.pw_anti K O h1 h h0 hl 13 0 (by decide); exact this
  -- ε² ≤ ε, so 2ε + ε² ≤ 4ε = h¹¹
  have sq : O.le (K.mul (pwR K h 13) (pwR K h 13)) (pwR K h 13) := by
    have := O.mul_le_mul_right _ _ _ e1 pe; rw [K.one_mul] at this; exact this
  have s3 : O.le (K.add (K.add (pwR K h 13) (pwR K h 13)) (K.mul (pwR K h 13) (pwR K h 13)))
      (K.add (K.add (pwR K h 13) (pwR K h 13)) (K.add (pwR K h 13) (pwR K h 13))) := by
    apply O.add_le_add _ _ _ _ (O.le_refl _)
    have := O.add_le_add _ _ _ _ sq pe; rw [DetChain.add_zero'] at this
    exact this
  have e4 : K.add (K.add (pwR K h 13) (pwR K h 13)) (K.add (pwR K h 13) (pwR K h 13)) = pwR K h 11 := by
    show K.add (K.add (K.mul (K.mul (pwR K h 11) h) h) (K.mul (K.mul (pwR K h 11) h) h))
      (K.add (K.mul (K.mul (pwR K h 11) h) h) (K.mul (K.mul (pwR K h 11) h) h)) = pwR K h 11
    have e : K.add (K.add (K.mul (K.mul (pwR K h 11) h) h) (K.mul (K.mul (pwR K h 11) h) h))
        (K.add (K.mul (K.mul (pwR K h 11) h) h) (K.mul (K.mul (pwR K h 11) h) h))
        = K.mul (pwR K h 11) (K.mul (K.add h h) (K.add h h)) := by lib_ring
    rw [e, hh, K.one_mul, K.mul_comm, K.one_mul]
  rw [e4] at s3
  have ek : pwR K (K.mul (K.add K.one (pwR K h 10)) (K.add K.one (pwR K h 10))) (4 * (2 * 4) + 1)
      = pwR K (K.add K.one (pwR K h 10)) 66 := by
    rw [GKS2.pwR_mul, ← GKS.pwR_add]
  rw [ek]
  have P0 : O.le K.zero (pwR K (K.add K.one (pwR K h 10)) 66) := by
    apply AreaLower.pw_nn K O _ _ h1
    have := O.add_le_add _ _ _ _ h1 (AreaLower.pw_nn K O h h0 h1 10); rw [K.zero_add] at this; exact this
  have a := O.mul_le_mul_right _ _ _ s3 P0
  -- h¹¹ κ⁶⁶ = h¹⁰ (κ⁶⁶ h) ≤ h¹⁰ ≤ h⁸ = h · h⁷
  have k := k66 K O h1 h hh h0
  have p10 := AreaLower.pw_nn K O h h0 h1 10
  have b := O.mul_le_mul_right _ _ (pwR K h 10) k p10
  rw [K.one_mul] at b
  have e5 : K.mul (pwR K h 11) (pwR K (K.add K.one (pwR K h 10)) 66)
      = K.mul (K.mul (pwR K (K.add K.one (pwR K h 10)) 66) h) (pwR K h 10) := by
    show K.mul (K.mul (pwR K h 10) h) _ = _; lib_ring
  rw [e5] at a
  have c := ConeBound.pw_anti K O h1 h h0 hl 10 8 (by decide)
  have e6 : K.mul h (pwR K h 7) = pwR K h 8 := by show _ = K.mul (pwR K h 7) h; rw [K.mul_comm]
  rw [e6]
  exact O.le_trans _ _ _ a (O.le_trans _ _ _ b c)

/-- 1 + 2⁻¹³ ≤ (1 + 2⁻¹⁰)(1 − 2⁻¹³) -/
theorem kappa_ineq (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) :
    O.le (K.add K.one (pwR K h 13)) (K.mul (K.add K.one (pwR K h 10)) (K.add K.one (K.neg (pwR K h 13)))) := by
  have hl := hle_one K O h1 h hh h0
  have e2 : K.add (pwR K h 13) (pwR K h 13) = pwR K h 12 := by
    show K.add (K.mul (pwR K h 12) h) (K.mul (pwR K h 12) h) = pwR K h 12
    have d : K.add (K.mul (pwR K h 12) h) (K.mul (pwR K h 12) h) = K.mul (pwR K h 12) (K.add h h) := by lib_ring
    rw [d, hh, K.mul_comm, K.one_mul]
  have e23 : K.mul (pwR K h 10) (pwR K h 13) = pwR K h 23 := (GKS.pwR_add K h 10 13).symm
  have e11 : K.add (pwR K h 12) (pwR K h 12) = pwR K h 11 := by
    show K.add (K.mul (pwR K h 11) h) (K.mul (pwR K h 11) h) = pwR K h 11
    have d : K.add (K.mul (pwR K h 11) h) (K.mul (pwR K h 11) h) = K.mul (pwR K h 11) (K.add h h) := by lib_ring
    rw [d, hh, K.mul_comm, K.one_mul]
  have X : O.le (K.add (pwR K h 12) (pwR K h 23)) (pwR K h 10) := by
    have a := O.add_le_add _ _ _ _ (O.le_refl (pwR K h 12)) (ConeBound.pw_anti K O h1 h h0 hl 23 12 (by decide))
    rw [e11] at a
    exact O.le_trans _ _ _ a (ConeBound.pw_anti K O h1 h h0 hl 11 10 (by decide))
  have br := ChiBound.sub_nonpos K O _ _ X
  have br0 : O.le K.zero (K.add (pwR K h 10) (K.neg (K.add (pwR K h 12) (pwR K h 23)))) := by
    have := ChiBound.neg_le K O _ _ br
    have e : K.neg K.zero = K.zero := by
      have := DetChain.add_neg' K K.zero; rw [DetChain.add_zero'] at this; exact this
    rw [e] at this
    have e' : K.neg (K.add (K.add (pwR K h 12) (pwR K h 23)) (K.neg (pwR K h 10)))
        = K.add (pwR K h 10) (K.neg (K.add (pwR K h 12) (pwR K h 23))) := by lib_ring
    rw [e'] at this; exact this
  have eq : K.mul (K.add K.one (pwR K h 10)) (K.add K.one (K.neg (pwR K h 13)))
      = K.add (K.add K.one (pwR K h 13)) (K.add (pwR K h 10)
          (K.neg (K.add (K.add (pwR K h 13) (pwR K h 13)) (K.mul (pwR K h 10) (pwR K h 13))))) := by lib_ring
  rw [eq, e2, e23]
  have := O.add_le_add _ _ _ _ (O.le_refl (K.add K.one (pwR K h 13))) br0
  rw [DetChain.add_zero'] at this; exact this

section Norm
variable (F : OField.OF K O)

/-- **Normalisation:** for a weight 1 + v with |v| ≤ ε = 2⁻¹³, s = 1/(1 + ε), κ = 1 + 2⁻¹⁰, m = 1/κ give every
  scalar hypothesis of GapZd.gap_Zd on (c₀, f) = (s, s v). -/
theorem norm_ok (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) :
    let e := pwR K h 13
    let s := F.inv (K.add K.one e)
    let κ := K.add K.one (pwR K h 10)
    let m := F.inv κ
    (O.le K.zero s ∧ O.le s K.one) ∧ (O.le K.zero m ∧ O.le m K.one) ∧ O.le K.one κ ∧ K.mul κ m = K.one ∧
    ∀ v, O.le (K.neg e) v → O.le v e →
      (O.le m (K.add s (K.mul s v)) ∧ O.le (K.add s (K.mul s v)) K.one) ∧
      (O.le (K.neg e) (K.mul s v) ∧ O.le (K.mul s v) e) := by
  intro e s κ m
  have pe : O.le K.zero e := AreaLower.pw_nn K O h h0 h1 13
  have ge1 : ∀ a : R, O.le K.zero a → O.le K.one (K.add K.one a) := fun a ha => by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) ha; rw [DetChain.add_zero'] at this; exact this
  have ne0 : ∀ a : R, O.le K.one a → a ≠ K.zero := fun a ha hz => by
    rw [hz] at ha; exact h10 (O.le_antisymm _ _ ha h1)
  have nn : ∀ a : R, O.le K.one a → O.le K.zero a := fun a ha => O.le_trans _ _ _ h1 ha
  -- inverse of something ≥ 1 lies in [0, 1]
  have invb : ∀ a : R, O.le K.one a → O.le K.zero (F.inv a) ∧ O.le (F.inv a) K.one := fun a ha => by
    have p := F.inv_pos a (nn a ha) (ne0 a ha)
    refine ⟨p, ?_⟩
    have := O.mul_le_mul_right _ _ _ ha p
    rw [K.one_mul, F.mul_inv a (ne0 a ha)] at this; exact this
  have E1 := ge1 e pe
  have K1 : O.le K.one κ := ge1 _ (AreaLower.pw_nn K O h h0 h1 10)
  have sb := invb _ E1
  have mb := invb _ K1
  have km : K.mul κ m = K.one := F.mul_inv κ (ne0 κ K1)
  have se : K.mul (K.add K.one e) s = K.one := F.mul_inv _ (ne0 _ E1)
  refine ⟨sb, mb, K1, km, ?_⟩
  intro v hv1 hv2
  have hsv : K.add s (K.mul s v) = K.mul s (K.add K.one v) := by lib_ring
  -- m ≤ s (1 − e)
  have ms : O.le m (K.mul s (K.add K.one (K.neg e))) := by
    have c0 : O.le K.zero (K.mul κ (K.add K.one e)) := O.mul_nonneg _ _ (nn κ K1) (nn _ E1)
    have c1 : O.le K.one (K.mul κ (K.add K.one e)) := by
      have := O.mul_le_mul_right _ _ _ K1 (nn _ E1); rw [K.one_mul] at this; exact O.le_trans _ _ _ E1 this
    apply O.pos_cancel _ _ _ c0 (ne0 _ c1)
    have l : K.mul m (K.mul κ (K.add K.one e)) = K.add K.one e := by
      rw [← K.mul_assoc, K.mul_comm m, km, K.one_mul]
    have r : K.mul (K.mul s (K.add K.one (K.neg e))) (K.mul κ (K.add K.one e)) = K.mul κ (K.add K.one (K.neg e)) := by
      have : K.mul (K.mul s (K.add K.one (K.neg e))) (K.mul κ (K.add K.one e))
          = K.mul (K.mul (K.add K.one e) s) (K.mul κ (K.add K.one (K.neg e))) := by lib_ring
      rw [this, se, K.one_mul]
    rw [l, r]; exact kappa_ineq K O h1 h hh h0
  -- 1 − e ≤ 1 + v ≤ 1 + e
  have lo : O.le (K.add K.one (K.neg e)) (K.add K.one v) := O.add_le_add _ _ _ _ (O.le_refl _) hv1
  have hi : O.le (K.add K.one v) (K.add K.one e) := O.add_le_add _ _ _ _ (O.le_refl _) hv2
  have slo := O.mul_le_mul_right _ _ _ lo sb.1
  have shi := O.mul_le_mul_right _ _ _ hi sb.1
  rw [K.mul_comm _ s, K.mul_comm _ s] at slo shi
  rw [K.mul_comm _ s] at se
  refine ⟨⟨?_, ?_⟩, ⟨?_, ?_⟩⟩
  · rw [hsv]; exact O.le_trans _ _ _ ms slo
  · rw [hsv]; rw [se] at shi; exact shi
  · -- −e ≤ −(s e) ≤ s v
    have a := O.mul_le_mul_right _ _ _ hv1 sb.1
    rw [DetChain.neg_mul_left, K.mul_comm e, K.mul_comm v] at a
    have b := O.mul_le_mul_right _ _ _ sb.2 pe
    rw [K.one_mul] at b
    exact O.le_trans _ _ _ (ChiBound.neg_le K O _ _ b) a
  · have a := O.mul_le_mul_right _ _ _ hv2 sb.1
    rw [K.mul_comm v, K.mul_comm e] at a
    have b := O.mul_le_mul_right _ _ _ sb.2 pe
    rw [K.one_mul] at b
    exact O.le_trans _ _ _ a b

end Norm

end GapInputs

#print axioms GapInputs.bern
#print axioms GapInputs.pow2_h
#print axioms GapInputs.hxy4
#print axioms GapInputs.k66
#print axioms GapInputs.hsmall4
#print axioms GapInputs.kappa_ineq
#print axioms GapInputs.norm_ok

ym/GapCone.lean

/-
  ym/GapCone — T45, G4: the strong-coupling gap on Z⁴ for any plaquette weight within 2⁻¹³ of 1.
  For a weight 1 + f₀ with |f₀| ≤ 2⁻¹³ (ChiBound.weight_near_one: every weight in Cone(1/16)), normalised by
  s = 1/(1 + 2⁻¹³), GapZd.gap_Zd at d = 4 holds with κ = 1 + 2⁻¹⁰, u = 1/2, x = 2⁻⁷, y = 2⁻⁶ (GapInputs):
  connected correlations of plaquettes at distance R + 2 are ≤ ½ B 2⁻ᴿ, uniformly in the volume.
  Lean 4 core only, no axioms.
-/
import ym.GapZd
import ym.GapInputs

namespace GapCone

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Strong-coupling gap on Z⁴ for weights within 2⁻¹³ of 1** (normalised weight s(1 + f₀), decay 2⁻ᴿ). -/
theorem gap_cone (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (f0 : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hfaG : ∀ i, H.adm (f0 i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (hf0 : ∀ i x, O.le (K.neg (pwR K h 13)) (f0 i x) ∧ O.le (f0 i x) (pwR K h 13))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro s κ
  obtain ⟨sb, mb, hκ, km, hw⟩ := GapInputs.norm_ok K O F h1 h10 h hh h0
  have hfd' : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (fun x => K.mul s (f0 i x)) :=
    fun i c c' hc => by show K.mul s (f0 i c) = K.mul s (f0 i c'); rw [hfd i c c' hc]
  have hfa' : ∀ i, H.adm (fun x => K.mul s (f0 i x)) := fun i => H.adm_mul _ _ (H.adm_const s) (hfaG i)
  exact GapZd.gap_Zd K O H DH h1 h10 4 LL hLL hL Pn hnd hv p0 q0 hpq (fun i x => K.mul s (f0 i x)) Up Uq
    s (F.inv κ) κ (pwR K h 13) K1 h (pwR K h 7) (pwR K h 6) Rr hfd' hUp hUq hfa' hUpa hUqa
    sb.1 sb.2 mb.1 mb.2 hκ km
    (fun i x => ((hw (f0 i x) (hf0 i x).1 (hf0 i x).2).1).1)
    (fun i x => ((hw (f0 i x) (hf0 i x).1 (hf0 i x).2).1).2)
    (AreaLower.pw_nn K O h h0 h1 13)
    (fun i x => (hw (f0 i x) (hf0 i x).1 (hf0 i x).2).2)
    hK1 hUpb hUqb h0 (GapInputs.hle_one K O h1 h hh h0)
    (AreaLower.pw_nn K O h h0 h1 7) (AreaLower.pw_nn K O h h0 h1 6)
    (GapInputs.hsmall4 K O h1 h hh h0) (GapInputs.hxy4 K O h1 h hh h0) hdist M hM

end GapCone

#print axioms GapCone.gap_cone

ym/GapFlow.lean

/-
  ym/GapFlow — T45, G4: from "the flowed weights enter Cone(1/16)" to "the flowed weights have a volume-uniform
  gap on Z⁴". For a sequence of coefficient lists a_k with K.one :: a_k ∈ Cone(1/16) for all k ≥ k₀, and plaquette
  traces tr_i ∈ [−2, 2], the plaquette weights w_k = 1 + Σ_{n≥1} a_{k,n} χ_n(tr_i) satisfy GapCone.gap_cone for every
  k ≥ k₀. Lean 4 core only, no axioms.
-/
import ym.GapCone

namespace GapFlow

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

theorem adm_ev (g : List G → R) (hg : H.adm g) : ∀ p : List R, H.adm (fun x => SU2Char.ev K p (g x))
  | [] => H.adm_const K.zero
  | c :: p => H.adm_add _ _ (H.adm_const c) (H.adm_mul _ _ hg (adm_ev g hg p))

theorem adm_dot (g : List G → R) (hg : H.adm g) :
    ∀ (m : Nat) (a : List R), H.adm (fun x => SU2Char.dot K m a (ChiBound.c K (g x)))
  | _, [] => H.adm_const K.zero
  | m, c :: a => H.adm_add _ _ (H.adm_mul _ _ (H.adm_const c) (adm_ev K O H g hg (SU2Char.chiC K m))) (adm_dot g hg (m + 1) a)

theorem h14 (h : R) (hh : K.add h h = K.one) : K.add (pwR K h 14) (pwR K h 14) = pwR K h 13 := by
  show K.add (K.mul (pwR K h 13) h) (K.mul (pwR K h 13) h) = pwR K h 13
  have d : K.add (K.mul (pwR K h 13) h) (K.mul (pwR K h 13) h) = K.mul (pwR K h 13) (K.add h h) := by lib_ring
  rw [d, hh, K.mul_comm, K.one_mul]

/-- **Cone ⇒ gap, for every later flow step.** -/
theorem gap_of_cone (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (a : Nat → List R) (k0 : Nat) (hc : ∀ j, ConeBound.Bd K O (pwR K h 4) 0 (K.one :: a (k0 + j)))
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) (j : Nat) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (a (k0 + j)) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro f0
  have hf0 : ∀ i x, O.le (K.neg (pwR K h 13)) (f0 i x) ∧ O.le (f0 i x) (pwR K h 13) := fun i x => by
    have := (ChiBound.weight_near_one K O h1 h hh h0 (a (k0 + j)) (hc j) (tr i x) (htr i x)).2
    rw [h14 K h hh] at this; exact this
  have hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i) :=
    fun i c c' hcc => by show SU2Char.dot K 1 _ (ChiBound.c K (tr i c)) = _; rw [htd i c c' hcc]
  exact GapCone.gap_cone K O F H DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq f0 Up Uq K1 Rr hfd hUp hUq
    (fun i => adm_dot K O H (tr i) (hta i) 1 (a (k0 + j))) hUpa hUqa hf0 hK1 hUpb hUqb hdist M hM

end GapFlow

#print axioms GapFlow.gap_of_cone

#print axioms: 9 theorems, no axioms.

90.15 The truncation is uniformly Cauchy

Proved: for \(\beta \le 8\) every truncated character coefficient changes by at most \(2^{-84}\) beyond 29 terms.

\[ c_n^{(29)}(t)\le c_n^{(29+m)}(t)\le c_n^{(29)}(t)+2^{-84}\qquad (0\le t\le 4,\ m\ge 0) \]

ym/TruncTail.lean

/-
  ym/TruncTail — T45, G2: the truncation of the SU(2) character coefficients is uniformly Cauchy.
  c_n(t) truncated at Kk terms (SU2CharExp.cS) and its termwise bound bnd(k) = t^{n+2k}/(n! k! k!):
    bnd_halve   2 t² ≤ (k+1)²  ⇒  2 bnd(k+1) ≤ bnd(k)
    tail        2 t² ≤ (Kk+1)² ⇒  cS(Kk) ≤ cS(Kk + m) ≤ cS(Kk) + 2 bnd(Kk)   for every m
  (for β = 2t ≤ 8 and Kk = 29: 2t² ≤ 32 ≤ 900). Lean 4 core only, no axioms.
-/
import ym.SU2CharExp

namespace TruncTail

open YMChain LinkIntegral SU2Char SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem bnd_halve (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n k : Nat)
    (hk : O.le (K.add (K.mul t t) (K.mul t t)) (cN K ((k + 1) * (k + 1)))) :
    O.le (K.add (bnd K O F t n (k + 1)) (bnd K O F t n (k + 1))) (bnd K O F t n k) := by
  have pX := pos_of_ge_one K O h1 h10 (fact n * fact (k + 1) * fact (k + 1))
    (Nat.mul_le_mul (Nat.mul_le_mul (fact_pos n) (fact_pos _)) (fact_pos _))
  have pY := pos_of_ge_one K O h1 h10 (fact n * fact k * fact k)
    (Nat.mul_le_mul (Nat.mul_le_mul (fact_pos n) (fact_pos _)) (fact_pos _))
  have e2 : K.add (bnd K O F t n (k + 1)) (bnd K O F t n (k + 1))
      = K.mul (K.add (pwR K t (n + 2 * (k + 1))) (pwR K t (n + 2 * (k + 1)))) (F.inv (cN K (fact n * fact (k + 1) * fact (k + 1)))) := by
    show K.add (K.mul _ _) (K.mul _ _) = _; lib_ring
  rw [e2]
  apply frac_le K O F _ _ _ _ pX.1 pX.2 pY.1 pY.2
  have en : n + 2 * (k + 1) = (n + 2 * k) + 2 := by lib_nat
  have ef : fact n * fact (k + 1) * fact (k + 1) = (fact n * fact k * fact k) * ((k + 1) * (k + 1)) := by
    show fact n * (fact k * (k + 1)) * (fact k * (k + 1)) = _; lib_nat
  rw [en, GKS.pwR_add, ef, cN_mul K (fact n * fact k * fact k) ((k + 1) * (k + 1))]
  have pm := AreaLower.pw_nn K O t ht h1 (n + 2 * k)
  have := O.mul_le_mul_right _ _ (K.mul (pwR K t (n + 2 * k)) (cN K (fact n * fact k * fact k))) hk
    (O.mul_nonneg _ _ pm pY.1)
  have eL : K.mul (K.add (K.mul (pwR K t (n + 2 * k)) (pwR K t 2)) (K.mul (pwR K t (n + 2 * k)) (pwR K t 2)))
      (cN K (fact n * fact k * fact k))
      = K.mul (K.add (K.mul t t) (K.mul t t)) (K.mul (pwR K t (n + 2 * k)) (cN K (fact n * fact k * fact k))) := by
    show K.mul (K.add (K.mul _ (K.mul (K.mul K.one t) t)) (K.mul _ (K.mul (K.mul K.one t) t))) _ = _; lib_ring
  have eR : K.mul (pwR K t (n + 2 * k)) (K.mul (cN K (fact n * fact k * fact k)) (cN K ((k + 1) * (k + 1))))
      = K.mul (cN K ((k + 1) * (k + 1))) (K.mul (pwR K t (n + 2 * k)) (cN K (fact n * fact k * fact k))) := by lib_ring
  rw [eL, eR]; exact this

/-- **Uniform tail bound.** -/
theorem tail (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n Kk : Nat)
    (hK : O.le (K.add (K.mul t t) (K.mul t t)) (cN K ((Kk + 1) * (Kk + 1)))) :
    ∀ m, O.le (cS K O F t n Kk) (cS K O F t n (Kk + m)) ∧
      O.le (K.add (cS K O F t n (Kk + m)) (K.add (bnd K O F t n (Kk + m)) (bnd K O F t n (Kk + m))))
        (K.add (cS K O F t n Kk) (K.add (bnd K O F t n Kk) (bnd K O F t n Kk)))
  | 0 => ⟨O.le_refl _, O.le_refl _⟩
  | m + 1 => by
    obtain ⟨l, u⟩ := tail h1 h10 t ht n Kk hK m
    have hk : O.le (K.add (K.mul t t) (K.mul t t)) (cN K ((Kk + m + 1) * (Kk + m + 1))) :=
      O.le_trans _ _ _ hK (cN_mono K O h1 _ _ (Nat.mul_le_mul (Nat.succ_le_succ (Nat.le_add_right Kk m))
        (Nat.succ_le_succ (Nat.le_add_right Kk m))))
    have hb := bnd_halve K O F h1 h10 t ht n (Kk + m) hk
    have tl := term_le K O F h1 h10 t ht n (Kk + m)
    have tn := term_nn K O F h1 h10 t ht n (Kk + m)
    constructor
    · have := O.add_le_add _ _ _ _ l tn
      rw [DetChain.add_zero'] at this; exact this
    · refine O.le_trans _ _ _ ?_ u
      show O.le (K.add (K.add (cS K O F t n (Kk + m)) (term K O F t n (Kk + m)))
        (K.add (bnd K O F t n (Kk + m + 1)) (bnd K O F t n (Kk + m + 1)))) _
      rw [K.add_assoc]
      exact O.add_le_add _ _ _ _ (O.le_refl _) (O.add_le_add _ _ _ _ tl hb)

end TruncTail

#print axioms TruncTail.bnd_halve
#print axioms TruncTail.tail

ym/TruncNum.lean

/-
  ym/TruncNum — T45, G2b: for 0 ≤ t ≤ 4 (β ≤ 8) the truncation tail of every character coefficient at Kk = 29 is
  ≤ 2·2⁻⁸⁵: bnd(t, n, 29) = t^{n+58}/(n! 29! 29!) ≤ 2⁻⁸⁵, from 4^{n+58}·2⁸⁵ ≤ n!·29!² (all n).
  With TruncTail.tail: cS(29) ≤ cS(29 + m) ≤ cS(29) + 2⁻⁸⁴ for every m. Lean 4 core only, no axioms.
-/
import ym.TruncTail
import ym.GapInputs
import ym.CertSound

namespace TruncNum

open YMChain LinkIntegral SU2Char SU2CharExp

theorem step (A B C T n : Nat) (ih : A * T ≤ B * C * C) : A * 4 * T ≤ B * (n + 4 + 1) * C * C := by
  have e1 : A * 4 * T = 4 * (A * T) := by lib_nat
  have e2 : B * (n + 4 + 1) * C * C = (n + 4 + 1) * (B * C * C) := by lib_nat
  rw [e1, e2]
  exact Nat.mul_le_mul (Nat.le_add_left 4 (n + 1)) ih

theorem fs (m : Nat) : fact (m + 1) = fact m * (m + 1) := rfl

theorem nat4 : ∀ n, 4 ^ (n + 4 + 58) * 2 ^ 85 ≤ fact (n + 4) * fact 29 * fact 29
  | 0 => by decide
  | n + 1 => by
    have x1 : n + 1 + 4 + 58 = (n + 4 + 58) + 1 := by lib_nat
    have x2 : n + 1 + 4 = (n + 4) + 1 := by lib_nat
    rw [x1, x2, Nat.pow_succ, fs]
    exact step _ _ _ _ n (nat4 n)

theorem nat_all : ∀ n, 4 ^ (n + 58) * 2 ^ 85 ≤ fact n * fact 29 * fact 29
  | 0 => by decide
  | 1 => by decide
  | 2 => by decide
  | 3 => by decide
  | n + 4 => nat4 n

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **bnd(t, n, 29) ≤ 2⁻⁸⁵ for 0 ≤ t ≤ 4.** -/
theorem bnd29 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (n : Nat) :
    O.le (bnd K O F t n 29) (pwR K h 85) := by
  let D := fact n * fact 29 * fact 29
  have pD := pos_of_ge_one K O h1 h10 D (Nat.mul_le_mul (Nat.mul_le_mul (fact_pos n) (fact_pos _)) (fact_pos _))
  have iD := F.inv_pos _ pD.1 pD.2
  -- t^{n+58} ≤ cN(4^{n+58})
  have p1 : O.le (pwR K t (n + 58)) (cN K (4 ^ (n + 58))) := by
    rw [CertSound.cN_pow]; exact SU2Strong4D.pw_mono K O h1 t (cN K 4) ht0 ht (n + 58)
  -- cN(4^{n+58}) · cN(2^85) ≤ cN D
  have p2 : O.le (K.mul (cN K (4 ^ (n + 58))) (cN K (2 ^ 85))) (cN K D) := by
    rw [← cN_mul]; exact cN_mono K O h1 _ _ (nat_all n)
  have hp := AreaLower.pw_nn K O h h0 h1 85
  have one : K.mul (cN K (2 ^ 85)) (pwR K h 85) = K.one := by
    rw [GapInputs.cN_pow2]; exact GapInputs.pow2_h K h hh 85
  -- X·D⁻¹ = (X·2^85)·D⁻¹·h^85 ≤ D·D⁻¹·h^85 = h^85
  have a1 := O.mul_le_mul_right _ _ (F.inv (cN K D)) (O.le_trans _ _ _
    (O.mul_le_mul_right _ _ _ p1 (SU2CharExp.cN_nn K O h1 _)) p2) iD
  have a2 := O.mul_le_mul_right _ _ (pwR K h 85) a1 hp
  rw [F.mul_inv _ pD.2, K.one_mul] at a2
  have e : bnd K O F t n 29 = K.mul (K.mul (K.mul (pwR K t (n + 58)) (cN K (2 ^ 85))) (F.inv (cN K D))) (pwR K h 85) := by
    show K.mul (pwR K t (n + 2 * 29)) (F.inv (cN K D)) = _
    have e' : K.mul (K.mul (K.mul (pwR K t (n + 58)) (cN K (2 ^ 85))) (F.inv (cN K D))) (pwR K h 85)
        = K.mul (K.mul (pwR K t (n + 58)) (F.inv (cN K D))) (K.mul (cN K (2 ^ 85)) (pwR K h 85)) := by lib_ring
    rw [e', one, K.mul_comm _ K.one, K.one_mul]
  rw [e]; exact a2

/-- **Truncation error at Kk = 29, β ≤ 8:** cS(29) ≤ cS(29 + m) ≤ cS(29) + 2⁻⁸⁴. -/
theorem trunc29 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (n m : Nat) :
    O.le (cS K O F t n 29) (cS K O F t n (29 + m)) ∧
      O.le (cS K O F t n (29 + m)) (K.add (cS K O F t n 29) (K.add (pwR K h 85) (pwR K h 85))) := by
  have hK : O.le (K.add (K.mul t t) (K.mul t t)) (cN K ((29 + 1) * (29 + 1))) := by
    have tt := O.le_trans _ _ _ (O.mul_le_mul_right _ _ t ht ht0)
      (by rw [K.mul_comm]; exact O.mul_le_mul_right _ _ (cN K 4) ht (SU2CharExp.cN_nn K O h1 4))
    have s := O.add_le_add _ _ _ _ tt tt
    refine O.le_trans _ _ _ s ?_
    rw [← cN_mul, ← SU2Char.cN_add]
    exact cN_mono K O h1 _ _ (by decide)
  obtain ⟨l, u⟩ := TruncTail.tail K O F h1 h10 t ht0 n 29 hK m
  refine ⟨l, ?_⟩
  have b := bnd29 K O F h1 h10 h hh h0 t ht0 ht n
  have bnn : O.le K.zero (K.add (bnd K O F t n (29 + m)) (bnd K O F t n (29 + m))) := by
    have := O.add_le_add _ _ _ _ (O.le_trans _ _ _ (term_nn K O F h1 h10 t ht0 n (29 + m)) (term_le K O F h1 h10 t ht0 n (29 + m)))
      (O.le_trans _ _ _ (term_nn K O F h1 h10 t ht0 n (29 + m)) (term_le K O F h1 h10 t ht0 n (29 + m)))
    rw [K.zero_add] at this; exact this
  have u2 : O.le (cS K O F t n (29 + m)) (K.add (cS K O F t n 29) (K.add (bnd K O F t n 29) (bnd K O F t n 29))) := by
    have := O.add_le_add _ _ _ _ (O.le_refl (cS K O F t n (29 + m))) bnn
    rw [DetChain.add_zero'] at this; exact O.le_trans _ _ _ this u
  exact O.le_trans _ _ _ u2 (O.add_le_add _ _ _ _ (O.le_refl _) (O.add_le_add _ _ _ _ b b))

end TruncNum

#print axioms TruncNum.nat4
#print axioms TruncNum.nat_all
#print axioms TruncNum.bnd29
#print axioms TruncNum.trunc29

#print axioms: 6 theorems, no axioms.

90.16 Uniform in the truncation, \(\beta \le 1\)

Proved: for \(\beta \in [0,1]\) the flow enters \(\mathrm {Cone}(1/16)\) within 8 steps for every truncation \(29+m\) at once.

\[ \forall m\ \forall t\in [0,\tfrac 12]\ \exists k\le 8\ \forall j:\ \mathrm {Bd}_{1/16}\big (\mathrm {mkIt}^{k+j}(\mathrm {wl}(t,29+m,11))\big ) \]

ym/CertWilsonT.lean

/-
  ym/CertWilsonT — T45, G2c-1: interval certificates that hold for every truncation Kk' ≥ Kk + 1 at once.
  The start check pads the upper end-point values by a tail ε with ε·Tn = 1:
    p_n(t) = cS(t, n, Kk') ∈ [cS(t₁, n, Kk+1), cS(t₂, n, Kk+1) + ε]
  whenever cS(t, n, Kk+1) ≤ cS(t, n, Kk') ≤ cS(t, n, Kk+1) + ε (TruncTail / TruncNum). The rest of the certificate
  (CertSound.checkChainT) is unchanged. Lean 4 core only, no axioms.
-/
import ym.CertWilson

namespace CertWilsonT

open YMChain LinkIntegral SU2Haar SU2Char SU2CharExp CertSound CertWilson

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- start check for t ∈ [a1/b1, a2/b2], upper values padded by 1/Tn -/
def checkStartIT (a1 b1 a2 b2 Kk D Tn : Nat) : Nat → List Nat → List Nat → Bool
  | _, [], [] => true
  | _, [], _ :: _ => false
  | _, _ :: _, [] => false
  | m, l :: ls, u :: us =>
    let Pn1 := Wn a1 b1 m (Kk + 1); let Pd1 := Dn b1 m (Kk + 1)
    let Pn2 := Wn a2 b2 m (Kk + 1) * Tn + Dn b2 m (Kk + 1); let Pd2 := Dn b2 m (Kk + 1) * Tn
    let Qn1 := Wn a1 b1 0 (Kk + 1); let Qd1 := Dn b1 0 (Kk + 1)
    let Qn2 := Wn a2 b2 0 (Kk + 1) * Tn + Dn b2 0 (Kk + 1); let Qd2 := Dn b2 0 (Kk + 1) * Tn
    (Nat.ble (Pn2 * Pd1 * Qd1 * Qd2 * D) (u * (Qn1 * Qd2 * Pd1 * Pd2)) &&
      Nat.ble (l * (Qn2 * Qd1 * Pd1 * Pd2)) (Pn1 * Pd2 * Qd1 * Qd2 * D)) &&
      checkStartIT a1 b1 a2 b2 Kk D Tn (m + 1) ls us

theorem pad_exact (t2 ε : R) (a2 b2 n Kk Tn : Nat) (hε : K.mul ε (cN K Tn) = K.one)
    (e : K.mul (cS K O F t2 n (Kk + 1)) (cN K (Dn b2 n (Kk + 1))) = cN K (Wn a2 b2 n (Kk + 1))) :
    K.mul (K.add (cS K O F t2 n (Kk + 1)) ε) (cN K (Dn b2 n (Kk + 1) * Tn))
      = cN K (Wn a2 b2 n (Kk + 1) * Tn + Dn b2 n (Kk + 1)) := by
  rw [cN_add, cN_mul, cN_mul, ← e]
  have : K.mul (K.add (cS K O F t2 n (Kk + 1)) ε) (K.mul (cN K (Dn b2 n (Kk + 1))) (cN K Tn))
      = K.add (K.mul (K.mul (cS K O F t2 n (Kk + 1)) (cN K (Dn b2 n (Kk + 1)))) (cN K Tn))
          (K.mul (K.mul ε (cN K Tn)) (cN K (Dn b2 n (Kk + 1)))) := by lib_ring
  rw [this, hε, K.one_mul]

/-- **Bracket for every truncation Kk' between Kk + 1 and the tail bound.** -/
theorem start_bracketIT (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t t1 t2 ε : R) (ht10 : O.le K.zero t1)
    (h1t : O.le t1 t) (ht2 : O.le t t2) (a1 b1 a2 b2 Kk D Tn Kk' : Nat) (hb1 : 1 ≤ b1) (hb2 : 1 ≤ b2) (hTn : 1 ≤ Tn)
    (e1 : K.mul t1 (cN K b1) = cN K a1) (e2 : K.mul t2 (cN K b2) = cN K a2) (hQn : 1 ≤ Wn a1 b1 0 (Kk + 1))
    (hε : K.mul ε (cN K Tn) = K.one)
    (hlo : ∀ n, O.le (cS K O F t n (Kk + 1)) (cS K O F t n Kk'))
    (hup : ∀ n, O.le (cS K O F t n Kk') (K.add (cS K O F t n (Kk + 1)) ε)) :
    ∀ (m : Nat) (l u : List Nat), checkStartIT a1 b1 a2 b2 Kk D Tn m l u = true →
      Brk K O (cN K D) (MigdalCone.lmap (fun n => K.mul (cS K O F t n Kk') (F.inv (cS K O F t 0 Kk'))) m l.length) l u
  | _, [], [], _ => trivial
  | _, [], _ :: _, h => Bool.noConfusion h
  | _, _ :: _, [], h => Bool.noConfusion h
  | m, l :: ls, u :: us, h => by
    have hc := and_true_l h
    have cu := Nat.le_of_ble_eq_true (and_true_l hc)
    have cl := Nat.le_of_ble_eq_true (and_true_r hc)
    have ht0 : O.le K.zero t := O.le_trans _ _ _ ht10 h1t
    let Pd1 := Dn b1 m (Kk + 1); let Pd2 := Dn b2 m (Kk + 1) * Tn
    let Qd1 := Dn b1 0 (Kk + 1); let Qd2 := Dn b2 0 (Kk + 1) * Tn
    let Pn2 := Wn a2 b2 m (Kk + 1) * Tn + Dn b2 m (Kk + 1)
    let Qn2 := Wn a2 b2 0 (Kk + 1) * Tn + Dn b2 0 (Kk + 1)
    have lo : ∀ n, O.le (cS K O F t1 n (Kk + 1)) (cS K O F t n Kk') := fun n =>
      O.le_trans _ _ _ (cS_mono K O F h1 h10 t1 t ht10 h1t n _) (hlo n)
    have hi : ∀ n, O.le (cS K O F t n Kk') (K.add (cS K O F t2 n (Kk + 1)) ε) := fun n =>
      O.le_trans _ _ _ (hup n) (O.add_le_add _ _ _ _ (cS_mono K O F h1 h10 t t2 ht0 ht2 n _) (O.le_refl ε))
    have sp := sandwich_scale K O h1 (cS K O F t m Kk') (cS K O F t1 m (Kk + 1)) (K.add (cS K O F t2 m (Kk + 1)) ε)
      Pd1 (Wn a1 b1 m (Kk + 1)) Pd2 Pn2 (Qd1 * Qd2)
      (lo m) (hi m) (cS_exact K O F h1 h10 t1 a1 b1 m e1 _)
      (pad_exact K O F t2 ε a2 b2 m Kk Tn hε (cS_exact K O F h1 h10 t2 a2 b2 m e2 _))
    have sq := sandwich_scale K O h1 (cS K O F t 0 Kk') (cS K O F t1 0 (Kk + 1)) (K.add (cS K O F t2 0 (Kk + 1)) ε)
      Qd1 (Wn a1 b1 0 (Kk + 1)) Qd2 Qn2 (Pd1 * Pd2)
      (lo 0) (hi 0) (cS_exact K O F h1 h10 t1 a1 b1 0 e1 _)
      (pad_exact K O F t2 ε a2 b2 0 Kk Tn hε (cS_exact K O F h1 h10 t2 a2 b2 0 e2 _))
    have eE : Qd1 * Qd2 * (Pd1 * Pd2) = Pd1 * Pd2 * (Qd1 * Qd2) := by lib_nat
    rw [eE] at sq
    let E2 := cN K (Pd1 * Pd2 * (Qd1 * Qd2))
    have Dpos : ∀ b n, 1 ≤ b → 1 ≤ Dn b n (Kk + 1) := fun b n hb => Nat.mul_le_mul (pow_ge b _ hb) (fact_ge _)
    have DposT : ∀ b n, 1 ≤ b → 1 ≤ Dn b n (Kk + 1) * Tn := fun b n hb => Nat.mul_le_mul (Dpos b n hb) hTn
    have Ep : 1 ≤ Pd1 * Pd2 * (Qd1 * Qd2) :=
      Nat.mul_le_mul (Nat.mul_le_mul (Dpos b1 m hb1) (DposT b2 m hb2)) (Nat.mul_le_mul (Dpos b1 0 hb1) (DposT b2 0 hb2))
    have Epos := pos_of_ge_one K O h1 h10 _ Ep
    have q0 : O.le K.zero (cS K O F t 0 Kk') :=
      O.le_trans _ _ _ (cS_nn K O F h1 h10 t ht0 0 _) (hlo 0)
    have qn : cS K O F t 0 Kk' ≠ K.zero := by
      intro e
      have lo' := sq.1
      rw [e, K.mul_comm, DetChain.mul_zero] at lo'
      have g := cN_mono K O h1 1 (Wn a1 b1 0 (Kk + 1) * Qd2 * (Pd1 * Pd2))
        (Nat.mul_le_mul (Nat.mul_le_mul hQn (DposT b2 0 hb2)) (Nat.mul_le_mul (Dpos b1 m hb1) (DposT b2 m hb2)))
      rw [cN_one] at g
      exact h10 (O.le_antisymm _ _ (O.le_trans _ _ _ g lo') h1)
    have qb := CertSound.quot_bracket K O F h1 (cS K O F t m Kk') (cS K O F t 0 Kk') E2
      (Wn a1 b1 m (Kk + 1) * Pd2 * (Qd1 * Qd2)) (Pn2 * Pd1 * (Qd1 * Qd2))
      (Wn a1 b1 0 (Kk + 1) * Qd2 * (Pd1 * Pd2)) (Qn2 * Qd1 * (Pd1 * Pd2)) D l u
      (O.le_trans _ _ _ (cS_nn K O F h1 h10 t ht0 m _) (hlo m)) q0 qn Epos.1 Epos.2 sp.1 sp.2 sq.1 sq.2
      (by
        have e : Pn2 * Pd1 * (Qd1 * Qd2) * D = Pn2 * Pd1 * Qd1 * Qd2 * D := by lib_nat
        have e' : u * (Wn a1 b1 0 (Kk + 1) * Qd2 * (Pd1 * Pd2)) = u * (Wn a1 b1 0 (Kk + 1) * Qd2 * Pd1 * Pd2) := by lib_nat
        rw [e, e']; exact cu)
      (by
        have e : l * (Qn2 * Qd1 * (Pd1 * Pd2)) = l * (Qn2 * Qd1 * Pd1 * Pd2) := by lib_nat
        have e' : Wn a1 b1 m (Kk + 1) * Pd2 * (Qd1 * Qd2) * D = Wn a1 b1 m (Kk + 1) * Pd2 * Qd1 * Qd2 * D := by lib_nat
        rw [e, e']; exact cl)
    exact ⟨⟨qb.1, qb.2.1, qb.2.2⟩, start_bracketIT h1 h10 t t1 t2 ε ht10 h1t ht2 a1 b1 a2 b2 Kk D Tn Kk' hb1 hb2 hTn
      e1 e2 hQn hε hlo hup (m + 1) ls us (and_true_r h)⟩

/-- **Certified for every t in an interval and every truncation Kk' within the tail bound.** -/
theorem certified_intervalT (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (t t1 t2 ε : R) (ht10 : O.le K.zero t1) (h1t : O.le t1 t) (ht2 : O.le t t2)
    (a1 b1 a2 b2 Kk D Tn ML : Nat) (hb1 : 1 ≤ b1) (hb2 : 1 ≤ b2) (hD : 1 ≤ D) (hTn : 1 ≤ Tn)
    (e1 : K.mul t1 (cN K b1) = cN K a1) (e2 : K.mul t2 (cN K b2) = cN K a2) (hε : K.mul ε (cN K Tn) = K.one)
    (la ua : List Nat) (ss : List StepT)
    (hQn : Nat.ble 1 (Wn a1 b1 0 (Kk + 1)) = true) (hS : checkStartIT a1 b1 a2 b2 Kk D Tn 1 la ua = true)
    (hC : checkChainT D ML la ua ss = true) (Kk2 : Nat)
    (hlo : ∀ n, O.le (cS K O F t n (Kk + 1)) (cS K O F t n (Kk2 + 1)))
    (hup : ∀ n, O.le (cS K O F t n (Kk2 + 1)) (K.add (cS K O F t n (Kk + 1)) ε)) :
    ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: MigdalCone.mkIt K O F ML (WilsonCone.wl K O F t Kk2 (la.length + 1)).tail (ss.length + j)) := by
  have sb := start_bracketIT K O F h1 h10 t t1 t2 ε ht10 h1t ht2 a1 b1 a2 b2 Kk D Tn (Kk2 + 1) hb1 hb2 hTn e1 e2
    (Nat.le_of_ble_eq_true hQn) hε hlo hup 1 la ua hS
  have hd : (WilsonCone.wl K O F t Kk2 (la.length + 1)).tail
      = MigdalCone.lmap (fun n => K.mul (cS K O F t n (Kk2 + 1)) (F.inv (cS K O F t 0 (Kk2 + 1)))) 1 la.length := rfl
  have cs := chain_soundT K O F h1 h10 h hh h0 D ML hD ss _ la ua sb hC
  intro j
  rw [mkIt_add, hd]
  exact MigdalCone.mk_forever16 K O F h1 h10 h hh h0 _ cs ML j

end CertWilsonT

#print axioms CertWilsonT.start_bracketIT
#print axioms CertWilsonT.certified_intervalT

ym/TruncCert.lean

/-
  ym/TruncCert — T45, G2c: the tail hypotheses of CertWilsonT.certified_intervalT at Kk = 29, ε = 2⁻⁸⁴:
  for 0 ≤ t ≤ 4 and every mm, cS(t, n, 30) ≤ cS(t, n, 30 + mm) ≤ cS(t, n, 30) + 2⁻⁸⁴. Lean 4 core only, no axioms.
-/
import ym.TruncNum

namespace TruncCert

open YMChain LinkIntegral SU2Char SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem h85 (h : R) (hh : K.add h h = K.one) : K.add (pwR K h 85) (pwR K h 85) = pwR K h 84 := by
  show K.add (K.mul (pwR K h 84) h) (K.mul (pwR K h 84) h) = pwR K h 84
  have d : K.add (K.mul (pwR K h 84) h) (K.mul (pwR K h 84) h) = K.mul (pwR K h 84) (K.add h h) := by lib_ring
  rw [d, hh, K.mul_comm, K.one_mul]

theorem tail_hyp (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (mm n : Nat) :
    O.le (cS K O F t n (29 + 1)) (cS K O F t n (29 + mm + 1)) ∧
      O.le (cS K O F t n (29 + mm + 1)) (K.add (cS K O F t n (29 + 1)) (pwR K h 84)) := by
  have t32 : O.le (K.add (K.mul t t) (K.mul t t)) (cN K (4 * 4 + 4 * 4)) := by
    have tt := O.le_trans _ _ _ (O.mul_le_mul_right _ _ t ht ht0)
      (by rw [K.mul_comm]; exact O.mul_le_mul_right _ _ (cN K 4) ht (SU2CharExp.cN_nn K O h1 4))
    refine O.le_trans _ _ _ (O.add_le_add _ _ _ _ tt tt) ?_
    rw [SU2Char.cN_add, cN_mul]; exact O.le_refl _
  have hK : ∀ Kk, 4 * 4 + 4 * 4 ≤ (Kk + 1) * (Kk + 1) →
      O.le (K.add (K.mul t t) (K.mul t t)) (cN K ((Kk + 1) * (Kk + 1))) := fun Kk hk =>
    O.le_trans _ _ _ t32 (cN_mono K O h1 _ _ hk)
  have e1 : 29 + mm + 1 = 30 + mm := by lib_nat
  have e2 : 29 + mm + 1 = 29 + (mm + 1) := by lib_nat
  constructor
  · rw [e1]; exact (TruncTail.tail K O F h1 h10 t ht0 n 30 (hK 30 (by decide)) mm).1
  · have u := (TruncNum.trunc29 K O F h1 h10 h hh h0 t ht0 ht n (mm + 1)).2
    rw [h85 K h hh] at u
    have l := (TruncTail.tail K O F h1 h10 t ht0 n 29 (hK 29 (by decide)) 1).1
    rw [e2]
    exact O.le_trans _ _ _ u (O.add_le_add _ _ _ _ l (O.le_refl _))

end TruncCert

#print axioms TruncCert.tail_hyp

ym/TMCertT.lean

/-
  ym/TMCertT — T45, G2c-2: Taylor-model certificates valid for every truncation Kk' ≥ Kk + 1 at once: the start
  values get their remainder radius enlarged by p = S·ε, where ε bounds the truncation tail
  (cS(Kk+1) ≤ cS(Kk') ≤ cS(Kk+1) + ε, TruncCert). Lean 4 core only, no axioms.
-/
import ym.TMCert

namespace TMCertT

open YMChain SU2Char Signed TMZ TMOpsZ TMChain MigdalCone LinkIntegral Projective ProjMK TMCert

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)
variable (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)

def padT (x : TM) (p : Nat) : TM := (x.1, x.2 + p)

def startTP (S D Kk L : Nat) (x : TM) (p : Nat) : Bool × List TM :=
  lmapB (fun n => ((cST S D x n (Kk + 1)).1, padT (cST S D x n (Kk + 1)).2 p)) 0 L

def modelCP (S D L Kk T0 H p : Nat) (gs : List TM) : Bool :=
  (startTP S D Kk (L + 1) (tT T0 H) p).1 && (posT (padT (cST S D (tT T0 H) 0 (Kk + 1)).2 p)
    && chainC S D L (startTP S D Kk (L + 1) (tT T0 H) p).2 gs)

theorem rep_pad (S : Nat) (u y z : R) (cs : List (Nat × Nat)) (r p : Nat) (hy : Rep K O S u y cs r)
    (hz : AbsLe K O (cN K p) (K.mul (cN K S) (K.add z (K.neg y)))) : Rep K O S u z cs (r + p) := by
  unfold Rep at *
  have := abs_add K O _ _ _ _ hy hz
  have e : K.add (K.add (K.mul (cN K S) y) (K.neg (evZ K u cs))) (K.mul (cN K S) (K.add z (K.neg y)))
      = K.add (K.mul (cN K S) z) (K.neg (evZ K u cs)) := by lib_ring
  rw [e, ← cN_add] at this; exact this

include h1 in
theorem abs_scaled (S : Nat) (ε : R) (p : Nat) (y z : R) (hε : K.mul (cN K S) ε = cN K p)
    (hlo : O.le y z) (hup : O.le z (K.add y ε)) : AbsLe K O (cN K p) (K.mul (cN K S) (K.add z (K.neg y))) := by
  have pS := SU2CharExp.cN_nn K O h1 S
  have d0 : O.le K.zero (K.add z (K.neg y)) := nn_sub_of_le K O _ _ hlo
  have d1 : O.le K.zero (K.add (K.add y ε) (K.neg z)) := nn_sub_of_le K O _ _ hup
  have m0 := O.mul_nonneg _ _ pS d0
  have m1 := O.mul_nonneg _ _ pS d1
  constructor
  · exact nn_add K O _ _ (by rw [← hε]; exact O.mul_nonneg _ _ pS (by
      have := O.add_le_add _ _ _ _ d0 d1; rw [K.zero_add] at this
      exact nn_eq K O _ _ (by lib_ring) this)) m0
  · rw [← hε]; exact nn_eq K O _ _ (by lib_ring) m1

include h1 h10 in
/-- **One Taylor model certifies an interval of couplings:** if the checker accepts, then for every t with
    |S t − T₀| ≤ H the Migdal flow from the Wilson weight at β = 2t is in Cone(1/16) after #g + 1 steps, forever. -/
theorem cert_modelT (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (S D L Kk T0 H : Nat) (hS : 1 ≤ S) (hH : 1 ≤ H)
    (p : Nat) (gs : List TM) (hc : modelCP S D L Kk T0 H p gs = true) (t ε : R) (hε : K.mul (cN K S) ε = cN K p) (Kk2 : Nat)
    (hlo : ∀ n, O.le (SU2CharExp.cS K O F t n (Kk + 1)) (SU2CharExp.cS K O F t n (Kk2 + 1)))
    (hup : ∀ n, O.le (SU2CharExp.cS K O F t n (Kk2 + 1)) (K.add (SU2CharExp.cS K O F t n (Kk + 1)) ε))
    (ht1 : O.le (cN K T0) (K.add (K.mul (cN K S) t) (cN K H)))
    (ht2 : O.le (K.mul (cN K S) t) (K.add (cN K T0) (cN K H))) :
    ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: mkIt K O F L (WilsonCone.wl K O F t Kk2 (L + 1)).tail (gs.length + 1 + j)) := by
  have ⟨c1, r1⟩ := band _ _ hc
  have ⟨c2, c3⟩ := band _ _ r1
  -- the variable u = (S t − T₀)/H, |u| ≤ 1
  obtain ⟨s, hs⟩ : ∃ s, H = s + 1 := ⟨H - 1, (Nat.succ_pred_eq_of_pos hH).symm⟩
  have nH : cN K H ≠ K.zero := by rw [hs]; exact cN_succ_ne K O h1 h10 s
  have pH := SU2CharExp.cN_nn K O h1 H
  let u := K.mul (K.add (K.mul (cN K S) t) (K.neg (cN K T0))) (F.inv (cN K H))
  have iH := F.inv_pos _ pH nH
  have hu : AbsLe K O K.one u := by
    have a1 := nn_sub_of_le K O _ _ ht1
    have a2 := nn_sub_of_le K O _ _ ht2
    have m1 := O.mul_nonneg _ _ a1 iH
    have m2 := O.mul_nonneg _ _ a2 iH
    have e1 : K.mul (K.add (K.add (K.mul (cN K S) t) (cN K H)) (K.neg (cN K T0))) (F.inv (cN K H)) = K.add K.one u := by
      show _ = K.add K.one (K.mul (K.add (K.mul (cN K S) t) (K.neg (cN K T0))) (F.inv (cN K H)))
      rw [← F.mul_inv _ nH]; lib_ring
    have e2 : K.mul (K.add (K.add (cN K T0) (cN K H)) (K.neg (K.mul (cN K S) t))) (F.inv (cN K H)) = K.add K.one (K.neg u) := by
      show _ = K.add K.one (K.neg (K.mul (K.add (K.mul (cN K S) t) (K.neg (cN K T0))) (F.inv (cN K H))))
      rw [← F.mul_inv _ nH]; lib_ring
    rw [e1] at m1; rw [e2] at m2
    exact ⟨m1, m2⟩
  have hx : Rep K O S u t (tT T0 H).1 (tT T0 H).2 := by
    show AbsLe K O K.zero (K.add (K.mul (cN K S) t) (K.neg (K.add (K.add (cN K T0) (K.neg K.zero))
      (K.mul u (K.add (K.add (cN K H) (K.neg K.zero)) (K.mul u K.zero))))))
    have e : K.mul u (cN K H) = K.add (K.mul (cN K S) t) (K.neg (cN K T0)) := by
      show K.mul (K.mul (K.add (K.mul (cN K S) t) (K.neg (cN K T0))) (F.inv (cN K H))) (cN K H) = _
      rw [K.mul_assoc, K.mul_comm (F.inv _), F.mul_inv _ nH, K.mul_comm, K.one_mul]
    have z : K.add (K.mul (cN K S) t) (K.neg (K.add (K.add (cN K T0) (K.neg K.zero))
        (K.mul u (K.add (K.add (cN K H) (K.neg K.zero)) (K.mul u K.zero))))) = K.zero := by
      rw [show K.mul u (K.add (K.add (cN K H) (K.neg K.zero)) (K.mul u K.zero)) = K.mul u (cN K H) by lib_ring, e]; lib_ring
    rw [z]
    exact ⟨nn_eq K O _ _ (by lib_ring) (O.le_refl K.zero), nn_eq K O _ _ (by lib_ring) (O.le_refl K.zero)⟩
  -- the start
  have pad : ∀ n, (cST S D (tT T0 H) n (Kk + 1)).1 = true →
      Rep K O S u (SU2CharExp.cS K O F t n (Kk2 + 1)) (padT (cST S D (tT T0 H) n (Kk + 1)).2 p).1
        (padT (cST S D (tT T0 H) n (Kk + 1)).2 p).2 := fun n hn =>
    rep_pad K O S u _ _ _ _ p (cST_sound K O F h1 h10 S D hS u t hu _ hx n (Kk + 1) hn)
      (abs_scaled K O h1 S ε p _ _ hε (hlo n) (hup n))
  have hV := lmapB_sound K O S u (fun n => SU2CharExp.cS K O F t n (Kk2 + 1))
    (fun n => ((cST S D (tT T0 H) n (Kk + 1)).1, padT (cST S D (tT T0 H) n (Kk + 1)).2 p)) (fun n hn => pad n hn) 0 (L + 1) c1
  have c0p := pos_sound K O F h1 h10 S hS u _ hu _ (pad 0 (band _ _ c1).1) c2
  have nc0 := c0p.2
  have form : lmap (fun n => SU2CharExp.cS K O F t n (Kk2 + 1)) 0 (L + 1)
      = scaleL K (SU2CharExp.cS K O F t 0 (Kk2 + 1)) (K.one :: (WilsonCone.wl K O F t Kk2 (L + 1)).tail) := by
    show _ = scaleL K _ (K.one :: lmap (fun n => K.mul (SU2CharExp.cS K O F t n (Kk2 + 1)) (F.inv (SU2CharExp.cS K O F t 0 (Kk2 + 1)))) 1 L)
    show SU2CharExp.cS K O F t 0 (Kk2 + 1) :: lmap _ 1 L = K.mul _ K.one :: scaleL K _ (lmap _ 1 L)
    rw [lmap_scale, K.mul_comm _ K.one, K.one_mul]
    congr 1
    exact lmap_ext _ _ (fun n => by rw [K.mul_comm (SU2CharExp.cS K O F t n (Kk2 + 1)), ← K.mul_assoc, F.mul_inv _ nc0, K.one_mul]) 1 L
  rw [form] at hV
  have cb := chain_sound K O F h1 h10 S D L hS u hu h hh h0 gs _ _ _ nc0 hV c3
  intro j
  rw [CertWilson.mkIt_add K O F]
  exact mk_forever16 K O F h1 h10 h hh h0 _ cb L j

end TMCertT

#print axioms TMCertT.cert_modelT

ym/TMCoverT.lean

/-
  ym/TMCoverT — T45, G2c-2: TMCover for padded start values (TMCertT): a contiguous list of accepted padded models
  covers its interval for every truncation Kk' within the tail bound ε (S·ε = p). Lean 4 core only, no axioms.
-/
import ym.TMCover
import ym.TMCertT

namespace TMCoverT

open YMChain SU2Char TMZ TMOpsZ TMChain TMCert TMCertT TMCover MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

def allCP (S D L Kk p : Nat) : List Model → Bool
  | [] => true
  | m :: ms => modelCP S D L Kk m.1 m.2.1 p m.2.2 && (Nat.ble 1 m.2.1 && allCP S D L Kk p ms)

theorem allCP_app (S D L Kk p : Nat) : ∀ x y : List Model, allCP S D L Kk p (x ++ y) = (allCP S D L Kk p x && allCP S D L Kk p y)
  | [], _ => rfl
  | m :: x, y => by
    show (modelCP S D L Kk m.1 m.2.1 p m.2.2 && (Nat.ble 1 m.2.1 && allCP S D L Kk p (x ++ y))) = _
    rw [allCP_app S D L Kk p x y]
    show _ = ((modelCP S D L Kk m.1 m.2.1 p m.2.2 && (Nat.ble 1 m.2.1 && allCP S D L Kk p x)) && allCP S D L Kk p y)
    rw [band_assoc, band_assoc]

def startOKP (S D L Kk T0 H p : Nat) (V0 : List TM) : Bool :=
  (startTP S D Kk (L + 1) (tT T0 H) p).1 && (posT (padT (cST S D (tT T0 H) 0 (Kk + 1)).2 p)
    && decide ((startTP S D Kk (L + 1) (tT T0 H) p).2 = V0))

theorem modelCP_of (S D L Kk T0 H p : Nat) (V0 gs : List TM) (h : startOKP S D L Kk T0 H p V0 = true)
    (h' : chainC S D L V0 gs = true) : modelCP S D L Kk T0 H p gs = true := by
  have ⟨c1, r1⟩ := band _ _ h
  have ⟨c2, c3⟩ := band _ _ r1
  have e : (startTP S D Kk (L + 1) (tT T0 H) p).2 = V0 := of_decide_eq_true c3
  show ((startTP S D Kk (L + 1) (tT T0 H) p).1 && (posT (padT (cST S D (tT T0 H) 0 (Kk + 1)).2 p)
    && chainC S D L (startTP S D Kk (L + 1) (tT T0 H) p).2 gs)) = true
  rw [c1, c2, e, h']; rfl

theorem allCP_one (S D L Kk p : Nat) (m : Model) (h : modelCP S D L Kk m.1 m.2.1 p m.2.2 = true)
    (hH : Nat.ble 1 m.2.1 = true) : allCP S D L Kk p [m] = true := by
  show (modelCP S D L Kk m.1 m.2.1 p m.2.2 && (Nat.ble 1 m.2.1 && true)) = true
  rw [h, hH]; rfl

variable (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)

include h1 h10 in
/-- **A contiguous list of accepted padded models covers its interval, for every truncation within the tail.** -/
theorem cover_listT (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (S D L Kk p : Nat) (hS : 1 ≤ S) (t ε : R)
    (hε : K.mul (cN K S) ε = cN K p) (Kk2 : Nat)
    (hlo : ∀ n, O.le (SU2CharExp.cS K O F t n (Kk + 1)) (SU2CharExp.cS K O F t n (Kk2 + 1)))
    (hup : ∀ n, O.le (SU2CharExp.cS K O F t n (Kk2 + 1)) (K.add (SU2CharExp.cS K O F t n (Kk + 1)) ε)) :
    ∀ (ms : List Model) (m : Model), allCP S D L Kk p (m :: ms) = true → contig (m :: ms) = true →
    O.le (cN K m.1) (K.add (K.mul (cN K S) t) (cN K m.2.1)) → O.le (K.mul (cN K S) t) (cN K (lastHi m ms)) →
    ∃ k, ∀ j, ConeBound.Bd K O (pwR K h 4) 0 (K.one :: mkIt K O F L (WilsonCone.wl K O F t Kk2 (L + 1)).tail (k + j)) := by
  intro ms
  induction ms with
  | nil =>
    intro m hc _ hlo' hhi
    have ⟨c1, r1⟩ := band _ _ hc
    have ⟨c2, _⟩ := band _ _ r1
    have hhi' : O.le (K.mul (cN K S) t) (K.add (cN K m.1) (cN K m.2.1)) := by rw [← cN_add]; exact hhi
    exact ⟨_, cert_modelT K O F h1 h10 h hh h0 S D L Kk m.1 m.2.1 hS (Nat.le_of_ble_eq_true c2) p m.2.2 c1 t ε hε Kk2
      hlo hup hlo' hhi'⟩
  | cons m' ms ih =>
    intro m hc hg hlo' hhi
    have ⟨c1, r1⟩ := band _ _ hc
    have ⟨c2, c3⟩ := band _ _ r1
    have ⟨g1, g2⟩ := band _ _ hg
    have eq : m.1 + m.2.1 + m'.2.1 = m'.1 := Nat.eq_of_beq_eq_true g1
    cases O.le_total (K.mul (cN K S) t) (K.add (cN K m.1) (cN K m.2.1)) with
    | inl hle => exact ⟨_, cert_modelT K O F h1 h10 h hh h0 S D L Kk m.1 m.2.1 hS (Nat.le_of_ble_eq_true c2) p m.2.2 c1 t ε
        hε Kk2 hlo hup hlo' hle⟩
    | inr hge =>
      apply ih m' c3 g2 _ hhi
      rw [← eq, cN_add, cN_add]
      have := O.add_le_add _ _ _ _ hge (O.le_refl (cN K m'.2.1))
      exact this

end TMCoverT

#print axioms TMCoverT.cover_listT
#print axioms TMCoverT.modelCP_of

#print axioms: 8 theorems, no axioms.

90.17 Uniform in the truncation, \(\beta \le 4\)

Proved: for every \(\beta \in [0,4]\) and every truncation \(29+m\) the flow enters \(\mathrm {Cone}(1/16)\) and stays there.

\[ \forall m\ \forall t\in [0,2]\ \exists k\ \forall j:\ \mathrm {Bd}_{1/16}\big (\mathrm {mkIt}^{k+j}(\mathrm {wl}(t,29+m,11))\big ) \]

ym/Cover04T.lean

/-
  ym/Cover04T — T45, G2c: every β ∈ [0, 4] and every truncation Kk' = 29 + m: the 4D Migdal flow (L = 10) from the
  Wilson weight enters Cone(1/16) and stays there forever, uniformly in m (CertCover29T.cover01T for t ≤ 1/2,
  TMCoverPA.cover_mid for 1/2 ≤ t ≤ 2; tails TruncCert.tail_hyp). Lean 4 core only, no axioms.
-/
import ym.CertCover29T
import ym.TMCoverPA
import ym.CoverBridge

namespace Cover04T

open YMChain SU2Char CertCover MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem pad_eq (h : R) (hh : K.add h h = K.one) (A B : Nat) :
    K.mul (cN K (2 ^ (A + B))) (pwR K h B) = cN K (2 ^ A) := by
  rw [GapInputs.cN_pow2, GapInputs.cN_pow2, GKS.pwR_add, K.mul_assoc, GapInputs.pow2_h K h hh B, K.mul_comm, K.one_mul]

/-- **Every β ∈ [0, 4], uniformly in the truncation 29 + m.** -/
theorem cover04T (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 2)) (m : Nat) :
    ∃ k, ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + m) 11).tail (k + j)) := by
  have ht4 : O.le t (cN K 4) := O.le_trans _ _ _ ht (SU2CharExp.cN_mono K O h1 2 4 (by decide))
  cases O.le_total t (rat K O F 1 2) with
  | inl hle =>
    obtain ⟨k, _, hk⟩ := CertCover29T.cover01T K O F h1 h10 h hh h0 t ht0 hle ht4 m
    exact ⟨k, hk⟩
  | inr hge =>
    have tl := fun n => TruncCert.tail_hyp K O F h1 h10 h hh h0 t ht0 ht4 m n
    exact TMCoverPA.cover_mid K O F h1 h10 h hh h0 t
      (CoverBridge.half_le K O F h1 h10 (2 ^ 299) t hge)
      (CoverBridge.le_two K O h1 (2 ^ 300) t ht)
      (pwR K h 84) (pad_eq K h hh 216 84) (29 + m) (fun n => (tl n).1) (fun n => (tl n).2)

end Cover04T

#print axioms Cover04T.cover04T

#print axioms: 1 theorem, no axioms.

90.18 Migdal flow from \(\beta \le 4\) has a uniform gap

Proved: for every \(\beta \in [0,4]\) and truncation \(29+m\), every Migdal step after some \(k_0\) gives a volume-uniform gap on \(\mathbb Z^4\).

\[ \exists k_0\ \forall j:\ |\langle U_pU_q\rangle _{w_{k_0+j}}-\langle U_p\rangle \langle U_q\rangle |\le \tfrac 12 B\,2^{-R} \]

ym/GapFlow04T.lean

/-
  ym/GapFlow04T — T45, G4: every β ∈ [0, 4] and every truncation 29 + m: after finitely many 4D Migdal steps (L = 10) the
  effective plaquette weight of SU(2) Wilson theory has a volume-uniform mass gap on Z⁴ (GapFlow.gap_of_cone with
  Cover04T.cover04T), and so does every later step. Lean 4 core only, no axioms.
-/
import ym.Cover04T
import ym.GapFlow

namespace GapFlow04T

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **β ∈ [0, 4] ⇒ the Migdal-flowed weight has a volume-uniform gap on Z⁴ from some step k₀ on.** -/
theorem gap_flow04 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (SU2Char.cN K 2)) (mm : Nat) :
    ∃ k0, ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + mm) 11).tail (k0 + j)) :=
  Cover04T.cover04T K O F h1 h10 h hh h0 t ht0 ht mm

/-- the gap statement for each later step, with a_k the flowed coefficients -/
theorem gap_flow04_step (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (SU2Char.cN K 2)) (mm : Nat) :
    ∃ k0, ∀ j, ∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let a := MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + mm) 11).tail (k0 + j)
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 a (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  obtain ⟨k0, hc⟩ := Cover04T.cover04T K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, fun j LL hLL hL Pn hnd hv p0 q0 hpq tr Up Uq K1 Rr htd hUp hUq hta hUpa hUqa htr hK1 hUpb hUqb hdist M hM =>
    GapFlow.gap_of_cone K O F H DH h1 h10 h hh h0 (fun k => MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + mm) 11).tail k)
      k0 hc LL hLL hL Pn hnd hv p0 q0 hpq tr Up Uq K1 Rr htd hUp hUq hta hUpa hUqa htr hK1 hUpb hUqb hdist M hM j⟩

end GapFlow04T

#print axioms GapFlow04T.gap_flow04_step

#print axioms: 1 theorem, no axioms.

90.19 Wilson weight at \(\beta \le 2^{-16}\): gap on \(\mathbb Z^d\), \(d\le 4\)

Proved: the truncated SU(2) Wilson weight at \(\beta \le 2^{-16}\) gives a volume-uniform gap on \(\mathbb Z^d\) for every \(d\le 4\).

\[ \beta \le 2^{-16},\ d\le 4\ \Longrightarrow \ |\langle U_pU_q\rangle -\langle U_p\rangle \langle U_q\rangle |\le \tfrac 12 B\,2^{-R} \]

ym/GapConeD.lean

/-
  ym/GapConeD — T45: the strong-coupling gap on Z^d, every d ≤ 4, for any plaquette weight within 2⁻¹³ of 1.
  For a weight 1 + f₀ with |f₀| ≤ 2⁻¹³ (ChiBound.weight_near_one: every weight in Cone(1/16)), normalised by
  s = 1/(1 + 2⁻¹³), GapZd.gap_Zd at d = 4 holds with κ = 1 + 2⁻¹⁰, u = 1/2, x = 2⁻⁷, y = 2⁻⁶ (GapInputs):
  connected correlations of plaquettes at distance R + 2 are ≤ ½ B 2⁻ᴿ, uniformly in the volume.
  Lean 4 core only, no axioms.
-/
import ym.GapZd
import ym.GapInputs
import ym.Count

namespace GapConeD

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)


theorem le_pow (h1 : O.le K.zero K.one) (z : R) (hz : O.le K.one z) (a b : Nat) (hab : a ≤ b) :
    O.le (pwR K z a) (pwR K z b) := by
  obtain ⟨c, e⟩ := Nat.le.dest hab
  rw [← e]; exact Count.pw_mono K O h1 z hz a c

theorem hsmall_d (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (d : Nat) (hd : d ≤ 4) :
    O.le (K.mul (K.add (K.add (pwR K h 13) (pwR K h 13)) (K.mul (pwR K h 13) (pwR K h 13)))
        (pwR K (K.mul (K.add K.one (pwR K h 10)) (K.add K.one (pwR K h 10))) (4 * (2 * d) + 1)))
      (K.mul h (pwR K h 7)) := by
  have p10 := AreaLower.pw_nn K O h h0 h1 10
  have k1 : O.le K.one (K.add K.one (pwR K h 10)) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) p10; rw [DetChain.add_zero'] at this; exact this
  have kk : O.le K.one (K.mul (K.add K.one (pwR K h 10)) (K.add K.one (pwR K h 10))) := by
    have := O.mul_le_mul_right _ _ _ k1 (O.le_trans _ _ _ h1 k1); rw [K.one_mul] at this
    exact O.le_trans _ _ _ k1 this
  have pe := AreaLower.pw_nn K O h h0 h1 13
  have c0 : O.le K.zero (K.add (K.add (pwR K h 13) (pwR K h 13)) (K.mul (pwR K h 13) (pwR K h 13))) := by
    have a := O.add_le_add _ _ _ _ pe pe; rw [K.zero_add] at a
    have b := O.add_le_add _ _ _ _ a (O.mul_nonneg _ _ pe pe); rw [K.zero_add] at b; exact b
  have m := le_pow K O h1 _ kk (4 * (2 * d) + 1) (4 * (2 * 4) + 1)
    (Nat.succ_le_succ (Nat.mul_le_mul_left 4 (Nat.mul_le_mul_left 2 hd)))
  have := O.mul_le_mul_right _ _ _ m c0
  rw [K.mul_comm _ (K.add (K.add _ _) _), K.mul_comm _ (K.add (K.add _ _) _)] at this
  exact O.le_trans _ _ _ this (GapInputs.hsmall4 K O h1 h hh h0)

theorem hxy_d (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (d : Nat) (hd : d ≤ 4) :
    O.le (K.mul (pwR K h 7) (pwR K (K.add K.one (pwR K h 6)) (4 * (2 * d)))) (pwR K h 6) := by
  have p6 := AreaLower.pw_nn K O h h0 h1 6
  have y1 : O.le K.one (K.add K.one (pwR K h 6)) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) p6; rw [DetChain.add_zero'] at this; exact this
  have m := le_pow K O h1 _ y1 (4 * (2 * d)) (4 * (2 * 4)) (Nat.mul_le_mul_left 4 (Nat.mul_le_mul_left 2 hd))
  have := O.mul_le_mul_right _ _ _ m (AreaLower.pw_nn K O h h0 h1 7)
  rw [K.mul_comm _ (pwR K h 7), K.mul_comm _ (pwR K h 7)] at this
  exact O.le_trans _ _ _ this (GapInputs.hxy4 K O h1 h hh h0)

/-- **Strong-coupling gap on Z^d (d ≤ 4) for weights within 2⁻¹³ of 1** (normalised weight s(1 + f₀), decay 2⁻ᴿ). -/
theorem gap_cone_d (d : Nat) (hd : d ≤ 4) (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (f0 : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hfaG : ∀ i, H.adm (f0 i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (hf0 : ∀ i x, O.le (K.neg (pwR K h 13)) (f0 i x) ∧ O.le (f0 i x) (pwR K h 13))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro s κ
  obtain ⟨sb, mb, hκ, km, hw⟩ := GapInputs.norm_ok K O F h1 h10 h hh h0
  have hfd' : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (fun x => K.mul s (f0 i x)) :=
    fun i c c' hc => by show K.mul s (f0 i c) = K.mul s (f0 i c'); rw [hfd i c c' hc]
  have hfa' : ∀ i, H.adm (fun x => K.mul s (f0 i x)) := fun i => H.adm_mul _ _ (H.adm_const s) (hfaG i)
  exact GapZd.gap_Zd K O H DH h1 h10 d LL hLL hL Pn hnd hv p0 q0 hpq (fun i x => K.mul s (f0 i x)) Up Uq
    s (F.inv κ) κ (pwR K h 13) K1 h (pwR K h 7) (pwR K h 6) Rr hfd' hUp hUq hfa' hUpa hUqa
    sb.1 sb.2 mb.1 mb.2 hκ km
    (fun i x => ((hw (f0 i x) (hf0 i x).1 (hf0 i x).2).1).1)
    (fun i x => ((hw (f0 i x) (hf0 i x).1 (hf0 i x).2).1).2)
    (AreaLower.pw_nn K O h h0 h1 13)
    (fun i x => (hw (f0 i x) (hf0 i x).1 (hf0 i x).2).2)
    hK1 hUpb hUqb h0 (GapInputs.hle_one K O h1 h hh h0)
    (AreaLower.pw_nn K O h h0 h1 7) (AreaLower.pw_nn K O h h0 h1 6)
    (hsmall_d K O h1 h hh h0 d hd) (hxy_d K O h1 h hh h0 d hd) hdist M hM

end GapConeD

#print axioms GapConeD.hsmall_d
#print axioms GapConeD.hxy_d
#print axioms GapConeD.gap_cone_d

ym/WilsonGap.lean

/-
  ym/WilsonGap — T45, G8: the (truncated) SU(2) Wilson plaquette weight itself, no flow: for β = 2t ≤ 2⁻³² and every
  truncation Kk, the weight 1 + Σ_{n≥1} (c_n/c_0) χ_n(tr U) lies in Cone(2⁻⁸) ⊂ Cone(1/16) (WilsonCone.wilson_cone; for
  β ≤ 2⁻¹⁶ directly in Cone(1/16), WilsonCone.wilson_forever16 at step 0: wilson_gap16), so
  GapConeD.gap_cone_d gives a volume-uniform gap on Z^d for every d ≤ 4. Lean 4 core only, no axioms.
-/
import ym.GapConeD
import ym.GapFlow
import ym.WilsonCone

namespace WilsonGap

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Wilson weight at β ≤ 2⁻³², any truncation Kk: volume-uniform gap on Z^d, d ≤ 4.** -/
theorem wilson_gap (d : Nat) (hd : d ≤ 4) (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (pwR K h 33)) (Kk : Nat)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (WilsonCone.wl K O F t Kk 11).tail (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro f0
  have hc : ConeBound.Bd K O (pwR K h 4) 0 (K.one :: (WilsonCone.wl K O F t Kk 11).tail) := by
    have hle : O.le h K.one := GapInputs.hle_one K O h1 h hh h0
    have w := WilsonCone.wilson_cone K O F h1 h10 h t hh h0 ht0 ht Kk 11
    have w4 := MigdalCone.bd_mono K O h1 (pwR K h 8) (pwR K h 4) (AreaLower.pw_nn K O h h0 h1 8)
      (ConeBound.pw_anti K O h1 h h0 hle 8 4 (by decide)) 0 _ w
    have c0 := SU2CharExp.c0_ge_one K O F h1 h10 t ht0 Kk
    have hd1 : WilsonCone.wl K O F t Kk 11 = K.one :: (WilsonCone.wl K O F t Kk 11).tail := by
      show K.mul (SU2CharExp.cS K O F t 0 (Kk + 1)) (F.inv (SU2CharExp.cS K O F t 0 (Kk + 1))) :: _ = _
      rw [F.mul_inv _ (MigdalStrong.ne_of_one_le K O h1 h10 _ c0)]; rfl
    rw [hd1] at w4; exact w4
  have hf0 : ∀ i x, O.le (K.neg (pwR K h 13)) (f0 i x) ∧ O.le (f0 i x) (pwR K h 13) := fun i x => by
    have := (ChiBound.weight_near_one K O h1 h hh h0 _ hc (tr i x) (htr i x)).2
    rw [GapFlow.h14 K h hh] at this; exact this
  have hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i) :=
    fun i c c' hcc => by show SU2Char.dot K 1 _ (ChiBound.c K (tr i c)) = _; rw [htd i c c' hcc]
  exact GapConeD.gap_cone_d K O F H d hd DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq f0 Up Uq K1 Rr hfd hUp hUq
    (fun i => GapFlow.adm_dot K O H (tr i) (hta i) 1 _) hUpa hUqa hf0 hK1 hUpb hUqb hdist M hM

/-- **Wilson weight at β ≤ 2⁻¹⁶, any truncation Kk: volume-uniform gap on Z^d, d ≤ 4.** -/
theorem wilson_gap16 (d : Nat) (hd : d ≤ 4) (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (pwR K h 17)) (Kk : Nat)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (WilsonCone.wl K O F t Kk 11).tail (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro f0
  have hc : ConeBound.Bd K O (pwR K h 4) 0 (K.one :: (WilsonCone.wl K O F t Kk 11).tail) :=
    WilsonCone.wilson_forever16 K O F h1 h10 h t hh h0 ht0 ht Kk 10 10 0
  have hf0 : ∀ i x, O.le (K.neg (pwR K h 13)) (f0 i x) ∧ O.le (f0 i x) (pwR K h 13) := fun i x => by
    have := (ChiBound.weight_near_one K O h1 h hh h0 _ hc (tr i x) (htr i x)).2
    rw [GapFlow.h14 K h hh] at this; exact this
  have hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i) :=
    fun i c c' hcc => by show SU2Char.dot K 1 _ (ChiBound.c K (tr i c)) = _; rw [htd i c c' hcc]
  exact GapConeD.gap_cone_d K O F H d hd DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq f0 Up Uq K1 Rr hfd hUp hUq
    (fun i => GapFlow.adm_dot K O H (tr i) (hta i) 1 _) hUpa hUqa hf0 hK1 hUpb hUqb hdist M hM

end WilsonGap

#print axioms WilsonGap.wilson_gap
#print axioms WilsonGap.wilson_gap16

#print axioms: 5 theorems, no axioms.

90.20 Gap in every dimension, explicit smallness

Proved: if \(8d\le 2^k\), every plaquette weight within \(2^{-(k+7)}\) of \(1\) has a volume-uniform gap on \(\mathbb Z^d\).

\[ |w-1|\le 2^{-(k+7)},\ 8d\le 2^k\ \Longrightarrow \ |\langle U_pU_q\rangle -\langle U_p\rangle \langle U_q\rangle |\le \tfrac 12 B\,2^{-R} \]

ym/GapInputsK.lean

/-
  ym/GapInputsK — T45, G9: the scalar hypotheses of GapZd.gap_Zd in every dimension d, with constants depending on
  k where 8d ≤ 2^k and 16d + 2 ≤ 2^{k+2}: ε = 2^{-(k+7)}, κ = 1 + 2^{-(k+4)}, u = 1/2, x = 2^{-(k+2)}, y = 2^{-(k+1)}.
    hxyK     x (1 + y)^{8d} ≤ y                (Bernoulli with n = 2^k)
    hsmallK  (2ε + ε²)(κ²)^{8d+1} ≤ u x         (Bernoulli with n = 2^{k+2})
    normK    w ↦ w/(1 + ε) gives c₀, m = 1/κ with m ≤ w ≤ 1 for |w − 1| ≤ ε
  Lean 4 core only, no axioms.
-/
import ym.GapInputs
import ym.GapConeD

namespace GapInputsK

open YMChain LinkIntegral SU2Char GapInputs

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem pw_succ (h : R) (n : Nat) : pwR K h (n + 1) = K.mul (pwR K h n) h := rfl

/-- 2^k · h^{k+j} = h^j -/
theorem pow2_hk (h : R) (hh : K.add h h = K.one) (k j : Nat) : K.mul (cN K (2 ^ k)) (pwR K h (k + j)) = pwR K h j := by
  rw [cN_pow2, GKS.pwR_add, ← K.mul_assoc, pow2_h K h hh k, K.one_mul]

/-- p + p = h^{n} when p = h^{n+1} -/
theorem dbl (h : R) (hh : K.add h h = K.one) (n : Nat) : K.add (pwR K h (n + 1)) (pwR K h (n + 1)) = pwR K h n := by
  show K.add (K.mul (pwR K h n) h) (K.mul (pwR K h n) h) = pwR K h n
  have d : K.add (K.mul (pwR K h n) h) (K.mul (pwR K h n) h) = K.mul (pwR K h n) (K.add h h) := by lib_ring
  rw [d, hh, K.mul_comm, K.one_mul]

theorem hxyK (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (d k : Nat)
    (hd : 4 * (2 * d) ≤ 2 ^ k) :
    O.le (K.mul (pwR K h (k + 2)) (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d)))) (pwR K h (k + 1)) := by
  have py := AreaLower.pw_nn K O h h0 h1 (k + 1)
  have y1 : O.le K.one (K.add K.one (pwR K h (k + 1))) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) py; rw [DetChain.add_zero'] at this; exact this
  have b := bern K O h1 (pwR K h (k + 1)) py (2 ^ k)
  rw [pow2_hk K h hh k 1, show pwR K h 1 = h from by show K.mul K.one h = h; rw [K.one_mul], one_sub_h K h hh] at b
  have m := GapConeD.le_pow K O h1 _ y1 _ _ hd
  have P0 := AreaLower.pw_nn K O _ (O.le_trans _ _ _ h1 y1) h1 (4 * (2 * d))
  -- x (1+y)^{8d} = h^{k+1} · ((1+y)^{8d} h) ≤ h^{k+1} · ((1+y)^{2^k} h) ≤ h^{k+1}
  have a := O.mul_le_mul_right _ _ h m h0
  have c := O.le_trans _ _ _ a b
  have := O.mul_le_mul_right _ _ (pwR K h (k + 1)) c py
  rw [K.one_mul] at this
  have e : K.mul (pwR K h (k + 2)) (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d)))
      = K.mul (K.mul (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d))) h) (pwR K h (k + 1)) := by
    show K.mul (K.mul (pwR K h (k + 1)) h) _ = _; lib_ring
  rw [e]; exact this

/-- κ^{2^{k+2}} · h ≤ 1 for κ = 1 + h^{k+4} -/
theorem kpow (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (k : Nat) :
    O.le (K.mul (pwR K (K.add K.one (pwR K h (k + 4))) (2 ^ (k + 2))) h) K.one := by
  have hl := hle_one K O h1 h hh h0
  have pz := AreaLower.pw_nn K O h h0 h1 (k + 4)
  have b := bern K O h1 (pwR K h (k + 4)) pz (2 ^ (k + 2))
  have e : K.mul (cN K (2 ^ (k + 2))) (pwR K h (k + 4)) = pwR K h 2 := by
    rw [show k + 4 = (k + 2) + 2 from by lib_nat]; exact pow2_hk K h hh (k + 2) 2
  rw [e] at b
  have c2 : O.le (pwR K h 2) h := by
    have := ConeBound.pw_anti K O h1 h h0 hl 2 1 (by decide)
    rw [show pwR K h 1 = h from by show K.mul K.one h = h; rw [K.one_mul]] at this; exact this
  have hs := h_le_sub K O h hh _ c2
  have P0 : O.le K.zero (pwR K (K.add K.one (pwR K h (k + 4))) (2 ^ (k + 2))) := by
    apply AreaLower.pw_nn K O _ _ h1
    have := O.add_le_add _ _ _ _ h1 pz; rw [K.zero_add] at this; exact this
  have m := O.mul_le_mul_right _ _ _ hs P0
  rw [K.mul_comm h, K.mul_comm (K.add K.one _)] at m
  exact O.le_trans _ _ _ m b

theorem hsmallK (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (d k : Nat)
    (hd : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + 2)) :
    O.le (K.mul (K.add (K.add (pwR K h (k + 7)) (pwR K h (k + 7))) (K.mul (pwR K h (k + 7)) (pwR K h (k + 7))))
        (pwR K (K.mul (K.add K.one (pwR K h (k + 4))) (K.add K.one (pwR K h (k + 4)))) (4 * (2 * d) + 1)))
      (K.mul h (pwR K h (k + 2))) := by
  have hl := hle_one K O h1 h hh h0
  have pe := AreaLower.pw_nn K O h h0 h1 (k + 7)
  have pz := AreaLower.pw_nn K O h h0 h1 (k + 4)
  have k1 : O.le K.one (K.add K.one (pwR K h (k + 4))) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) pz; rw [DetChain.add_zero'] at this; exact this
  have e1 : O.le (pwR K h (k + 7)) K.one := ConeBound.pw_anti K O h1 h h0 hl (k + 7) 0 (Nat.zero_le _)
  have sq : O.le (K.mul (pwR K h (k + 7)) (pwR K h (k + 7))) (pwR K h (k + 7)) := by
    have := O.mul_le_mul_right _ _ _ e1 pe; rw [K.one_mul] at this; exact this
  have pp : O.le (pwR K h (k + 7)) (K.add (pwR K h (k + 7)) (pwR K h (k + 7))) := by
    have := O.add_le_add _ _ _ _ (O.le_refl (pwR K h (k + 7))) pe; rw [DetChain.add_zero'] at this; exact this
  have d6 : K.add (pwR K h (k + 7)) (pwR K h (k + 7)) = pwR K h (k + 6) := dbl K h hh (k + 6)
  have d5 : K.add (pwR K h (k + 6)) (pwR K h (k + 6)) = pwR K h (k + 5) := dbl K h hh (k + 5)
  have s3 : O.le (K.add (K.add (pwR K h (k + 7)) (pwR K h (k + 7))) (K.mul (pwR K h (k + 7)) (pwR K h (k + 7))))
      (pwR K h (k + 5)) := by
    have a := O.add_le_add _ _ _ _ (O.le_refl (K.add (pwR K h (k + 7)) (pwR K h (k + 7)))) (O.le_trans _ _ _ sq pp)
    rw [d6, d5] at a
    rw [d6]; exact a
  -- (κ²)^{8d+1} = κ^{2(8d+1)} ≤ κ^{2^{k+2}}
  have ek : pwR K (K.mul (K.add K.one (pwR K h (k + 4))) (K.add K.one (pwR K h (k + 4)))) (4 * (2 * d) + 1)
      = pwR K (K.add K.one (pwR K h (k + 4))) (2 * (4 * (2 * d) + 1)) := by
    rw [GKS2.pwR_mul, ← GKS.pwR_add, show (4 * (2 * d) + 1) + (4 * (2 * d) + 1) = 2 * (4 * (2 * d) + 1) from by lib_nat]
  rw [ek]
  have m := GapConeD.le_pow K O h1 _ k1 _ _ hd
  have P0 : O.le K.zero (pwR K (K.add K.one (pwR K h (k + 4))) (2 * (4 * (2 * d) + 1))) :=
    AreaLower.pw_nn K O _ (O.le_trans _ _ _ h1 k1) h1 _
  have a := O.mul_le_mul_right _ _ _ s3 P0
  -- h^{k+5} κ^N = h^{k+4} (κ^N h) ≤ h^{k+4} (κ^{2^{k+2}} h) ≤ h^{k+4} ≤ h^{k+3} = h · h^{k+2}
  have b := O.mul_le_mul_right _ _ h m h0
  have c := O.le_trans _ _ _ b (kpow K O h1 h hh h0 k)
  have d4 := O.mul_le_mul_right _ _ (pwR K h (k + 4)) c pz
  rw [K.one_mul] at d4
  have e5 : K.mul (pwR K h (k + 5)) (pwR K (K.add K.one (pwR K h (k + 4))) (2 * (4 * (2 * d) + 1)))
      = K.mul (K.mul (pwR K (K.add K.one (pwR K h (k + 4))) (2 * (4 * (2 * d) + 1))) h) (pwR K h (k + 4)) := by
    show K.mul (K.mul (pwR K h (k + 4)) h) _ = _; lib_ring
  rw [e5] at a
  have f := ConeBound.pw_anti K O h1 h h0 hl (k + 4) (k + 3) (Nat.le_succ _)
  have e6 : K.mul h (pwR K h (k + 2)) = pwR K h (k + 3) := by show _ = K.mul (pwR K h (k + 2)) h; rw [K.mul_comm]
  rw [e6]
  exact O.le_trans _ _ _ a (O.le_trans _ _ _ d4 f)

theorem kappa_ineqK (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (k : Nat) :
    O.le (K.add K.one (pwR K h (k + 7))) (K.mul (K.add K.one (pwR K h (k + 4))) (K.add K.one (K.neg (pwR K h (k + 7))))) := by
  have hl := hle_one K O h1 h hh h0
  have e2 : K.add (pwR K h (k + 7)) (pwR K h (k + 7)) = pwR K h (k + 6) := dbl K h hh (k + 6)
  have e23 : K.mul (pwR K h (k + 4)) (pwR K h (k + 7)) = pwR K h ((k + 4) + (k + 7)) := (GKS.pwR_add K h (k + 4) (k + 7)).symm
  have e11 : K.add (pwR K h (k + 6)) (pwR K h (k + 6)) = pwR K h (k + 5) := dbl K h hh (k + 5)
  have X : O.le (K.add (pwR K h (k + 6)) (pwR K h ((k + 4) + (k + 7)))) (pwR K h (k + 4)) := by
    have a := O.add_le_add _ _ _ _ (O.le_refl (pwR K h (k + 6)))
      (ConeBound.pw_anti K O h1 h h0 hl ((k + 4) + (k + 7)) (k + 6) (Nat.le.intro (show (k + 6) + (k + 5) = (k + 4) + (k + 7) by lib_nat)))
    rw [e11] at a
    exact O.le_trans _ _ _ a (ConeBound.pw_anti K O h1 h h0 hl (k + 5) (k + 4) (Nat.le_succ _))
  have br := ChiBound.sub_nonpos K O _ _ X
  have br0 : O.le K.zero (K.add (pwR K h (k + 4)) (K.neg (K.add (pwR K h (k + 6)) (pwR K h ((k + 4) + (k + 7)))))) := by
    have := ChiBound.neg_le K O _ _ br
    have e : K.neg K.zero = K.zero := by
      have := DetChain.add_neg' K K.zero; rw [DetChain.add_zero'] at this; exact this
    rw [e] at this
    have e' : K.neg (K.add (K.add (pwR K h (k + 6)) (pwR K h ((k + 4) + (k + 7)))) (K.neg (pwR K h (k + 4))))
        = K.add (pwR K h (k + 4)) (K.neg (K.add (pwR K h (k + 6)) (pwR K h ((k + 4) + (k + 7))))) := by lib_ring
    rw [e'] at this; exact this
  have eq : K.mul (K.add K.one (pwR K h (k + 4))) (K.add K.one (K.neg (pwR K h (k + 7))))
      = K.add (K.add K.one (pwR K h (k + 7))) (K.add (pwR K h (k + 4))
          (K.neg (K.add (K.add (pwR K h (k + 7)) (pwR K h (k + 7))) (K.mul (pwR K h (k + 4)) (pwR K h (k + 7)))))) := by lib_ring
  rw [eq, e2, e23]
  have := O.add_le_add _ _ _ _ (O.le_refl (K.add K.one (pwR K h (k + 7)))) br0
  rw [DetChain.add_zero'] at this; exact this

section Norm
variable (F : OField.OF K O)

/-- **Normalisation (k):** for a weight 1 + v with |v| ≤ ε = 2^{-(k+7)}, s = 1/(1 + ε), κ = 1 + 2^{-(k+4)}, m = 1/κ give every
  scalar hypothesis of GapZd.gap_Zd on (c₀, f) = (s, s v). -/
theorem norm_okK (k : Nat) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) :
    let e := pwR K h (k + 7)
    let s := F.inv (K.add K.one e)
    let κ := K.add K.one (pwR K h (k + 4))
    let m := F.inv κ
    (O.le K.zero s ∧ O.le s K.one) ∧ (O.le K.zero m ∧ O.le m K.one) ∧ O.le K.one κ ∧ K.mul κ m = K.one ∧
    ∀ v, O.le (K.neg e) v → O.le v e →
      (O.le m (K.add s (K.mul s v)) ∧ O.le (K.add s (K.mul s v)) K.one) ∧
      (O.le (K.neg e) (K.mul s v) ∧ O.le (K.mul s v) e) := by
  intro e s κ m
  have pe : O.le K.zero e := AreaLower.pw_nn K O h h0 h1 (k + 7)
  have ge1 : ∀ a : R, O.le K.zero a → O.le K.one (K.add K.one a) := fun a ha => by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) ha; rw [DetChain.add_zero'] at this; exact this
  have ne0 : ∀ a : R, O.le K.one a → a ≠ K.zero := fun a ha hz => by
    rw [hz] at ha; exact h10 (O.le_antisymm _ _ ha h1)
  have nn : ∀ a : R, O.le K.one a → O.le K.zero a := fun a ha => O.le_trans _ _ _ h1 ha
  -- inverse of something ≥ 1 lies in [0, 1]
  have invb : ∀ a : R, O.le K.one a → O.le K.zero (F.inv a) ∧ O.le (F.inv a) K.one := fun a ha => by
    have p := F.inv_pos a (nn a ha) (ne0 a ha)
    refine ⟨p, ?_⟩
    have := O.mul_le_mul_right _ _ _ ha p
    rw [K.one_mul, F.mul_inv a (ne0 a ha)] at this; exact this
  have E1 := ge1 e pe
  have K1 : O.le K.one κ := ge1 _ (AreaLower.pw_nn K O h h0 h1 (k + 4))
  have sb := invb _ E1
  have mb := invb _ K1
  have km : K.mul κ m = K.one := F.mul_inv κ (ne0 κ K1)
  have se : K.mul (K.add K.one e) s = K.one := F.mul_inv _ (ne0 _ E1)
  refine ⟨sb, mb, K1, km, ?_⟩
  intro v hv1 hv2
  have hsv : K.add s (K.mul s v) = K.mul s (K.add K.one v) := by lib_ring
  -- m ≤ s (1 − e)
  have ms : O.le m (K.mul s (K.add K.one (K.neg e))) := by
    have c0 : O.le K.zero (K.mul κ (K.add K.one e)) := O.mul_nonneg _ _ (nn κ K1) (nn _ E1)
    have c1 : O.le K.one (K.mul κ (K.add K.one e)) := by
      have := O.mul_le_mul_right _ _ _ K1 (nn _ E1); rw [K.one_mul] at this; exact O.le_trans _ _ _ E1 this
    apply O.pos_cancel _ _ _ c0 (ne0 _ c1)
    have l : K.mul m (K.mul κ (K.add K.one e)) = K.add K.one e := by
      rw [← K.mul_assoc, K.mul_comm m, km, K.one_mul]
    have r : K.mul (K.mul s (K.add K.one (K.neg e))) (K.mul κ (K.add K.one e)) = K.mul κ (K.add K.one (K.neg e)) := by
      have : K.mul (K.mul s (K.add K.one (K.neg e))) (K.mul κ (K.add K.one e))
          = K.mul (K.mul (K.add K.one e) s) (K.mul κ (K.add K.one (K.neg e))) := by lib_ring
      rw [this, se, K.one_mul]
    rw [l, r]; exact kappa_ineqK K O h1 h hh h0 k
  -- 1 − e ≤ 1 + v ≤ 1 + e
  have lo : O.le (K.add K.one (K.neg e)) (K.add K.one v) := O.add_le_add _ _ _ _ (O.le_refl _) hv1
  have hi : O.le (K.add K.one v) (K.add K.one e) := O.add_le_add _ _ _ _ (O.le_refl _) hv2
  have slo := O.mul_le_mul_right _ _ _ lo sb.1
  have shi := O.mul_le_mul_right _ _ _ hi sb.1
  rw [K.mul_comm _ s, K.mul_comm _ s] at slo shi
  rw [K.mul_comm _ s] at se
  refine ⟨⟨?_, ?_⟩, ⟨?_, ?_⟩⟩
  · rw [hsv]; exact O.le_trans _ _ _ ms slo
  · rw [hsv]; rw [se] at shi; exact shi
  · -- −e ≤ −(s e) ≤ s v
    have a := O.mul_le_mul_right _ _ _ hv1 sb.1
    rw [DetChain.neg_mul_left, K.mul_comm e, K.mul_comm v] at a
    have b := O.mul_le_mul_right _ _ _ sb.2 pe
    rw [K.one_mul] at b
    exact O.le_trans _ _ _ (ChiBound.neg_le K O _ _ b) a
  · have a := O.mul_le_mul_right _ _ _ hv2 sb.1
    rw [K.mul_comm v, K.mul_comm e] at a
    have b := O.mul_le_mul_right _ _ _ sb.2 pe
    rw [K.one_mul] at b
    exact O.le_trans _ _ _ a b

end Norm

end GapInputsK

#print axioms GapInputsK.hxyK
#print axioms GapInputsK.kpow
#print axioms GapInputsK.hsmallK
#print axioms GapInputsK.kappa_ineqK
#print axioms GapInputsK.norm_okK

ym/GapConeK.lean

/-
  ym/GapConeK — T45, G9: the strong-coupling gap on Z^d in every dimension d, uniformly in the volume: if
  8d ≤ 2^k and 2(8d+1) ≤ 2^{k+2}, every plaquette weight within 2^{-(k+7)} of 1 (≈ 1/(1024 d)), normalised by
  s = 1/(1 + 2^{-(k+7)}), satisfies GapZd.gap_Zd with κ = 1 + 2^{-(k+4)}, u = 1/2, x = 2^{-(k+2)}, y = 2^{-(k+1)}
  (GapInputsK): connected plaquette correlations at distance R + 2 are ≤ ½ B 2⁻ᴿ.
  Lean 4 core only, no axioms.
-/
import ym.GapZd
import ym.GapInputsK

namespace GapConeK

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Strong-coupling gap on Z^d, every d, for weights within 2^{-(k+7)} of 1 (8d ≤ 2^k).** -/
theorem gap_cone_k (d k : Nat) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + 2)) (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (f0 : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hfaG : ∀ i, H.adm (f0 i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (hf0 : ∀ i x, O.le (K.neg (pwR K h (k + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + 7)))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let s := F.inv (K.add K.one (pwR K h (k + 7)))
    let κ := K.add K.one (pwR K h (k + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro s κ
  obtain ⟨sb, mb, hκ, km, hw⟩ := GapInputsK.norm_okK K O F k h1 h10 h hh h0
  have hfd' : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (fun x => K.mul s (f0 i x)) :=
    fun i c c' hc => by show K.mul s (f0 i c) = K.mul s (f0 i c'); rw [hfd i c c' hc]
  have hfa' : ∀ i, H.adm (fun x => K.mul s (f0 i x)) := fun i => H.adm_mul _ _ (H.adm_const s) (hfaG i)
  exact GapZd.gap_Zd K O H DH h1 h10 d LL hLL hL Pn hnd hv p0 q0 hpq (fun i x => K.mul s (f0 i x)) Up Uq
    s (F.inv κ) κ (pwR K h (k + 7)) K1 h (pwR K h (k + 2)) (pwR K h (k + 1)) Rr hfd' hUp hUq hfa' hUpa hUqa
    sb.1 sb.2 mb.1 mb.2 hκ km
    (fun i x => ((hw (f0 i x) (hf0 i x).1 (hf0 i x).2).1).1)
    (fun i x => ((hw (f0 i x) (hf0 i x).1 (hf0 i x).2).1).2)
    (AreaLower.pw_nn K O h h0 h1 (k + 7))
    (fun i x => (hw (f0 i x) (hf0 i x).1 (hf0 i x).2).2)
    hK1 hUpb hUqb h0 (GapInputs.hle_one K O h1 h hh h0)
    (AreaLower.pw_nn K O h h0 h1 (k + 2)) (AreaLower.pw_nn K O h h0 h1 (k + 1))
    (GapInputsK.hsmallK K O h1 h hh h0 d k hd16) (GapInputsK.hxyK K O h1 h hh h0 d k hd8) hdist M hM

end GapConeK

#print axioms GapConeK.gap_cone_k

#print axioms: 6 theorems, no axioms.

90.21 Wilson weight: gap in every dimension

Proved: for \(t\le 2^{-(4j+5)}\), \(k+7\le 4j+1\), \(8d\le 2^k\), the truncated Wilson weight has a volume-uniform gap on \(\mathbb Z^d\).

\[ \beta \lesssim 2^{-13}/(8d)\ \Longrightarrow \ |\langle U_pU_q\rangle -\langle U_p\rangle \langle U_q\rangle |\le \tfrac 12 B\,2^{-R} \]

ym/WilsonGapK.lean

/-
  ym/WilsonGapK — T45, G10: the truncated SU(2) Wilson weight itself, every dimension: for t ≤ 2^{-(4(j+1)+1)} the
  weight lies in Cone(2^{-(j+1)}) (WilsonCone.two_tn_le_gen), hence |w − 1| ≤ 2^{-(4j+1)} (ConeGap.activity), and if
  k + 7 ≤ 4j + 1 and 8d ≤ 2^k, GapConeK.gap_cone_k gives a volume-uniform gap on Z^d. Lean 4 core only, no axioms.
-/
import ym.GapConeK
import ym.WilsonGap

namespace WilsonGapK

open YMChain LinkIntegral SU2Char SU2CharExp WilsonCone MigdalCone ConeBound

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **Wilson weights at t ≤ 2^{-(4(j+1)+1)} lie in Cone(2^{-(j+1)}).** -/
theorem wilson_cone_j (j : Nat) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h t : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (ht0 : O.le K.zero t) (ht : O.le t (pwR K h (4 * (j + 1) + 1))) (Kk L : Nat) :
    Bd K O (pwR K h (j + 1)) 0 (K.one :: (wl K O F t Kk (L + 1)).tail) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have th : O.le t h := O.le_trans _ _ _ ht (by
    have x := pw_anti K O h1 h h0 hle (4 * (j + 1) + 1) 1 (Nat.succ_le_succ (Nat.zero_le _))
    have e : pwR K h 1 = h := by show K.mul K.one h = h; rw [K.one_mul]
    rw [e] at x; exact x)
  have tt : O.le (K.mul t t) h := by
    have a := O.mul_le_mul_right _ _ _ th ht0
    have c := O.mul_le_mul_right _ _ _ th h0
    rw [K.mul_comm t h] at c
    have b := O.mul_le_mul_right _ _ _ hle h0
    rw [K.one_mul] at b
    exact O.le_trans _ _ _ (O.le_trans _ _ _ a c) b
  have w : Bd K O (pwR K h (j + 1)) 0 (wl K O F t Kk (L + 1)) := by
    apply bd_of_fun
    intro n
    have rb := ratio_bound K O F h1 h10 t ht0 n Kk
    refine ⟨rb.1, ?_⟩
    cases n with
    | zero =>
      have c0 := c0_ge_one K O F h1 h10 t ht0 Kk
      rw [F.mul_inv _ (MigdalStrong.ne_of_one_le K O h1 h10 _ c0)]
      show O.le K.one (K.mul (K.add K.one K.zero) K.one)
      rw [DetChain.add_zero', K.one_mul]; exact O.le_refl _
    | succ m =>
      have b1 := MigdalStrong.bS_le K O F h1 h10 t ht0 (m + 1) (Kk + 1)
      have g := MigdalStrong.gsum_le_two K O h1 h (K.mul t t) hh h0 (O.mul_nonneg _ _ ht0 ht0) tt (Kk + 1)
      have b2 := O.mul_le_mul_right _ _ _ g (AreaLower.pw_nn K O t ht0 h1 (m + 1))
      rw [K.mul_comm _ (pwR K t _), K.mul_comm (MigdalStrong.two K) (pwR K t _)] at b2
      have tw := two_tn_le_gen K O (j + 1) h1 h t hh h0 ht0 ht m
      rw [K.mul_comm (SU2Strong4D.two K)] at tw
      exact O.le_trans _ _ _ rb.2 (O.le_trans _ _ _ b1 (O.le_trans _ _ _ b2 tw))
  have c0 := c0_ge_one K O F h1 h10 t ht0 Kk
  have hd : wl K O F t Kk (L + 1) = K.one :: (wl K O F t Kk (L + 1)).tail := by
    show K.mul _ (F.inv _) :: _ = K.one :: _
    rw [F.mul_inv _ (MigdalStrong.ne_of_one_le K O h1 h10 _ c0)]
    rfl
  rw [hd] at w; exact w

open GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist

variable {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Wilson weight at t ≤ 2^{-(4(j+1)+1)}, any truncation: volume-uniform gap on Z^d whenever 8d ≤ 2^k, k + 7 ≤ 4j + 1.** -/
theorem wilson_gap_k (d k j : Nat) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + 2))
    (hkj : k + 7 ≤ 4 * j + 1) (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (pwR K h (4 * (j + 1) + 1))) (Kk : Nat)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (WilsonCone.wl K O F t Kk 11).tail (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (k + 7)))
    let κ := K.add K.one (pwR K h (k + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro f0
  have hc := wilson_cone_j K O F j h1 h10 h t hh h0 ht0 ht Kk 10
  have hl := GapInputs.hle_one K O h1 h hh h0
  have hx : K.mul (MigdalCone.four K) (pwR K (pwR K h (j + 1)) 4) = pwR K h (4 * j + 2) := by
    rw [Migdal.pw_pw, show (j + 1) * 4 = (4 * j + 2) + 2 from by lib_nat, GKS.pwR_add]
    show K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.mul (pwR K h (4 * j + 2)) (K.mul (K.mul K.one h) h)) = _
    have e : K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.mul (pwR K h (4 * j + 2)) (K.mul (K.mul K.one h) h))
        = K.mul (pwR K h (4 * j + 2)) (K.mul (K.add h h) (K.add h h)) := by lib_ring
    rw [e, hh, K.one_mul, K.mul_comm, K.one_mul]
  have x2 : K.add (pwR K h (4 * j + 2)) (pwR K h (4 * j + 2)) = pwR K h (4 * j + 1) := GapInputsK.dbl K h hh (4 * j + 1)
  have hxx : O.le (K.add (pwR K h (4 * j + 2)) (pwR K h (4 * j + 2))) K.one := by
    rw [x2]; exact ConeBound.pw_anti K O h1 h h0 hl (4 * j + 1) 0 (Nat.zero_le _)
  have epsle : O.le (pwR K h (4 * j + 1)) (pwR K h (k + 7)) := ConeBound.pw_anti K O h1 h h0 hl _ _ hkj
  have hf0 : ∀ i x, O.le (K.neg (pwR K h (k + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + 7)) := fun i x => by
    have := ConeGap.activity K O h1 (pwR K h (j + 1)) (pwR K h (4 * j + 2)) (AreaLower.pw_nn K O h h0 h1 _) hx hxx 1
      _ hc.2 (ChiBound.c K (tr i x)) (ChiBound.chi_bound K O h1 (tr i x) (htr i x))
    rw [show pwR K (pwR K h (4 * j + 2)) 1 = K.mul K.one (pwR K h (4 * j + 2)) from rfl, K.one_mul, x2] at this
    exact ⟨O.le_trans _ _ _ (ChiBound.neg_le K O _ _ epsle) this.1, O.le_trans _ _ _ this.2 epsle⟩
  have hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i) :=
    fun i c c' hcc => by show SU2Char.dot K 1 _ (ChiBound.c K (tr i c)) = _; rw [htd i c c' hcc]
  exact GapConeK.gap_cone_k K O F H d k hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq f0 Up Uq K1 Rr hfd hUp hUq
    (fun i => GapFlow.adm_dot K O H (tr i) (hta i) 1 _) hUpa hUqa hf0 hK1 hUpb hUqb hdist M hM

end WilsonGapK

#print axioms WilsonGapK.wilson_cone_j
#print axioms WilsonGapK.wilson_gap_k

#print axioms: 2 theorems, no axioms.

90.22 Infinite-volume Cauchy estimate near \(w=1\)

Proved: for weights within \(2^{-(k+7)}\) of \(1\) on \(\mathbb Z^d\) (\(8d\le 2^k\)), expectations change by at most \(T\) when far plaquettes are added, uniformly in the volume.

\[ (1-r)\,\big |\langle O\rangle _{\Lambda '}-\langle O\rangle _{\Lambda }\big |\le \kappa C s\, r^{K},\qquad r=\beta _{\rm sh}/2 \]

ym/WilsonVol.lean

/-
  ym/WilsonVol — T45, G11 step 1: positivity inputs of Lad5.vol_step for weights m ≤ w ≤ 1.
    Z_ge     m^{#M} ≤ Z_M = ⟨1⟩ unnormalised, so Z_M > 0
    Ew_ge    m ≤ ⟨w_y⟩_M = Z_M(w_y)/Z_M
  Lean 4 core only, no axioms.
-/
import ym.Lad5
import ym.GapConeK

namespace WilsonVol

open YMChain LinkIntegral Seeds PosRatio HaarStr HaarPoly DupAlgH OField Volume Signed Indep Closure

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

theorem Z_ge (h1 : O.le K.zero K.one) (w : Nat → List G → R) (hwa : ∀ i, H.adm (w i)) (m : R) (hm : O.le K.zero m)
    (hm1 : O.le m K.one) (hw : ∀ k c, O.le m (w k c)) (M : List Bool) :
    O.le (pwR K m (cnt M)) (ZxH K O H w M (fun _ => K.one)) := by
  have := I_mono K O H (fun _ => pwR K m (cnt M)) (fun x => K.mul K.one (prodW K w 0 M x)) (H.adm_const _)
    (H.adm_mul _ _ (H.adm_const _) (adm_prodW K O H w hwa 0 M))
    (fun x => by rw [K.one_mul]; exact prodW_ge K O w m hm h1 hm1 hw x 0 M)
  rw [I_const] at this; exact this

theorem Z_pos (h1 : O.le K.zero K.one) (w : Nat → List G → R) (hwa : ∀ i, H.adm (w i)) (m : R)
    (hm : O.le K.zero m) (hmne : m ≠ K.zero) (hm1 : O.le m K.one) (hw : ∀ k c, O.le m (w k c)) (M : List Bool) :
    O.le K.zero (ZxH K O H w M (fun _ => K.one)) ∧ ZxH K O H w M (fun _ => K.one) ≠ K.zero := by
  have g := Z_ge K O H h1 w hwa m hm hm1 hw M
  have pm : O.le K.zero (pwR K m (cnt M)) := AreaLower.pw_nn K O m hm h1 _
  have pne : pwR K m (cnt M) ≠ K.zero := SpecGap.pw_ne K O h1 m hm hmne (cnt M)
  refine ⟨O.le_trans _ _ _ pm g, fun e => pne (O.le_antisymm _ _ (by rw [e] at g; exact g) pm)⟩

theorem Ew_ge (h1 : O.le K.zero K.one) (w : Nat → List G → R) (hwa : ∀ i, H.adm (w i)) (m : R)
    (hm : O.le K.zero m) (hmne : m ≠ K.zero) (hm1 : O.le m K.one) (hw : ∀ k c, O.le m (w k c)) (M : List Bool) (y : Nat) :
    O.le m (E K O F (ZxH K O H w M (w y)) (ZxH K O H w M (fun _ => K.one))) := by
  have zp := Z_pos K O H h1 w hwa m hm hmne hm1 hw M
  have hw0 : ∀ k c, O.le K.zero (w k c) := fun k c => O.le_trans _ _ _ hm (hw k c)
  have a := I_mono K O H (fun x => K.mul m (K.mul K.one (prodW K w 0 M x))) (fun x => K.mul (w y x) (prodW K w 0 M x))
    (H.adm_mul _ _ (H.adm_const _) (H.adm_mul _ _ (H.adm_const _) (adm_prodW K O H w hwa 0 M)))
    (H.adm_mul _ _ (hwa y) (adm_prodW K O H w hwa 0 M))
    (fun x => by rw [K.one_mul]; exact O.mul_le_mul_right _ _ _ (hw y x) (prodW_nn K O w h1 hw0 x 0 M))
  rw [H.I_smul _ _ (H.adm_mul _ _ (H.adm_const _) (adm_prodW K O H w hwa 0 M))] at a
  have b := O.mul_le_mul_right _ _ _ a (F.inv_pos _ zp.1 zp.2)
  have zdef : H.I (fun c => K.mul K.one (prodW K w 0 M c)) = ZxH K O H w M (fun _ => K.one) := rfl
  rw [zdef, K.mul_assoc, F.mul_inv _ zp.2, K.mul_comm m K.one, K.one_mul] at b
  exact b

end WilsonVol

#print axioms WilsonVol.Z_pos
#print axioms WilsonVol.Ew_ge

ym/WilsonVolStep.lean

/-
  ym/WilsonVolStep — T45, G11 step 2: one volume step for a weight within 2^{-(k+7)} of 1 in dimension d (8d ≤ 2^k):
  adding a plaquette y far from the observable moves ⟨O⟩ by at most κ·(B/2), B the clustering constant of
  GapConeK.gap_cone_k (Lad5.vol_step + WilsonVol + OField.cov_bound). Lean 4 core only, no axioms.
-/
import ym.WilsonVol

namespace WilsonVolStep

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

theorem vol_step_k (d k : Nat) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + 2)) (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (f0 : Nat → List G → R) (Up : List G → R) (y : Nat) (K1 : R) (Rr : Nat)
    (hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) (fun x => K.add (F.inv (K.add K.one (pwR K h (k + 7)))) (K.mul (F.inv (K.add K.one (pwR K h (k + 7)))) (f0 y x))))
    (hfaG : ∀ i, H.adm (f0 i)) (hUpa : H.adm Up)
    (hf0 : ∀ i x, O.le (K.neg (pwR K h (k + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + 7)))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) (hy : y < M.length) (hMy : nthB M y = false) :
    let s := F.inv (K.add K.one (pwR K h (k + 7)))
    let κ := K.add K.one (pwR K h (k + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + 1))) sc) (pwR K h Rr))
    let Z := ZxH K O H w M (fun _ => K.one)
    let Z' := ZxH K O H w (upd M y true) (fun _ => K.one)
    O.le (K.neg (K.mul (K.mul h B) κ)) (K.add (E K O F (ZxH K O H w (upd M y true) Up) Z') (K.neg (E K O F (ZxH K O H w M Up) Z))) ∧
      O.le (K.add (E K O F (ZxH K O H w (upd M y true) Up) Z') (K.neg (E K O F (ZxH K O H w M Up) Z))) (K.mul (K.mul h B) κ) := by
  intro s κ w sc B Z Z'
  obtain ⟨sb, mb, hκ, km, hw⟩ := GapInputsK.norm_okK K O F k h1 h10 h hh h0
  have hwb : ∀ i x, (O.le (F.inv κ) (w i x) ∧ O.le (w i x) K.one) := fun i x => (hw (f0 i x) (hf0 i x).1 (hf0 i x).2).1
  have hwa : ∀ i, H.adm (w i) := fun i => H.adm_add _ _ (H.adm_const s) (H.adm_mul _ _ (H.adm_const s) (hfaG i))
  have hmne : F.inv κ ≠ K.zero := fun e => by rw [e, DetChain.mul_zero] at km; exact h10 km.symm
  have hw1 : ∀ x, O.le (K.neg K1) (w y x) ∧ O.le (w y x) K1 := fun x => by
    have l := O.le_trans _ _ _ mb.1 (hwb y x).1
    have u := O.le_trans _ _ _ (hwb y x).2 hK1
    have nk : O.le (K.neg K1) K.zero := by
      have := ChiBound.neg_le K O _ _ (O.le_trans _ _ _ h1 hK1)
      have e : K.neg K.zero = K.zero := by
        have := DetChain.add_neg' K K.zero; rw [DetChain.add_zero'] at this; exact this
      rw [e] at this; exact this
    exact ⟨O.le_trans _ _ _ nk l, u⟩
  have g := GapConeK.gap_cone_k K O F H d k hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq f0 Up (w y) K1 Rr
    hfd hUp hUq hfaG hUpa (hwa y) hf0 (O.le_trans _ _ _ h1 hK1) hUpb hw1 hdist M hM
  have zp := WilsonVol.Z_pos K O H h1 w hwa (F.inv κ) mb.1 hmne mb.2 (fun i x => (hwb i x).1) M
  have cb := cov_bound K O F _ _ _ _ B zp.1 zp.2 h10 g
  -- c + c ≤ B ⇒ c ≤ h B
  have half : ∀ c : R, O.le (K.neg B) (K.add c c) ∧ O.le (K.add c c) B →
      O.le (K.neg (K.mul h B)) c ∧ O.le c (K.mul h B) := fun c hc => by
    have ec : K.mul h (K.add c c) = c := by
      have : K.mul h (K.add c c) = K.mul (K.add h h) c := by lib_ring
      rw [this, hh, K.one_mul]
    have u := O.mul_le_mul_right _ _ h hc.2 h0
    have l := O.mul_le_mul_right _ _ h hc.1 h0
    rw [K.mul_comm (K.add c c) h, ec, K.mul_comm B h] at u
    rw [K.mul_comm (K.add c c) h, ec, K.mul_comm (K.neg B) h, YMChain.mul_neg_r K h B] at l
    exact ⟨l, u⟩
  have hc0 : O.le K.zero (K.mul h B) := by
    have k0 := O.le_trans _ _ _ h1 hK1
    have kk := O.add_le_add _ _ _ _ k0 k0; rw [K.zero_add] at kk
    have κ0 : O.le K.zero κ := O.le_trans _ _ _ h1 hκ
    have b1 := O.mul_nonneg _ _ (O.mul_nonneg _ _ kk kk) (AreaLower.pw_nn K O _ (O.mul_nonneg _ _ κ0 κ0) h1 sc)
    have y0 : O.le K.zero (K.add K.one (pwR K h (k + 1))) := by
      have := O.add_le_add _ _ _ _ h1 (AreaLower.pw_nn K O h h0 h1 (k + 1)); rw [K.zero_add] at this; exact this
    have b2 := O.mul_nonneg _ _ (AreaLower.pw_nn K O _ y0 h1 sc) (AreaLower.pw_nn K O h h0 h1 Rr)
    exact O.mul_nonneg _ _ h0 (O.mul_nonneg _ _ b1 b2)
  exact Lad5.vol_step K O F H h10 w M y hy hMy Up (F.inv κ) κ (K.mul h B) mb.1 hmne
    (by rw [K.mul_comm]; exact (by rw [K.mul_comm]; exact km)) (O.le_trans _ _ _ h1 hκ) hc0 zp.1 zp.2
    (WilsonVol.Ew_ge K O F H h1 w hwa (F.inv κ) mb.1 hmne mb.2 (fun i x => (hwb i x).1) M y) (half _ cb)

end WilsonVolStep

#print axioms WilsonVolStep.vol_step_k

ym/TouchCount.lean

/-
  ym/TouchCount — T45, G11 step 3a: the number of plaquettes touching p ∪ q is at most 2·8d on Z^d.
    nzb_add_eq    nzb (a + b) = nzb a || nzb b
    touch_zadd    touch r (p + q) = touch r p || touch r q
    cnt_or        #(f ∨ g) ≤ #f + #g
    sc_le         cnt (touch · (p + q)) ≤ 8d + 8d   (ZdDegree.deg_Zd twice)
  Lean 4 core only, no axioms.
-/
import ym.ZdDegree

namespace TouchCount

open YMChain Closure Factor GKS Indep InjCount ZdLinks Degree ZdDegree

theorem nzb_add_eq : ∀ a b : Nat, nzb (a + b) = (nzb a || nzb b)
  | 0, 0 => rfl
  | 0, _ + 1 => rfl
  | _ + 1, 0 => rfl
  | _ + 1, _ + 1 => rfl

theorem or_false' (b : Bool) : (b || false) = b := by cases b <;> rfl

theorem touch_zadd : ∀ r p q : List Nat, touch r (zadd p q) = (touch r p || touch r q)
  | [], _, _ => rfl
  | _ :: _, [], q => by show touch _ q = (false || touch _ q); rfl
  | a :: x, b :: y, [] => by show touch (a :: x) (b :: y) = (touch (a :: x) (b :: y) || false); rw [or_false']
  | a :: x, b :: y, c :: z => by
    show ((nzb a && nzb (b + c)) || touch x (zadd y z)) = (((nzb a && nzb b) || touch x y) || ((nzb a && nzb c) || touch x z))
    rw [nzb_add_eq, touch_zadd x y z]
    cases nzb a <;> cases nzb b <;> cases nzb c <;> cases touch x y <;> cases touch x z <;> rfl

theorem cnt_or {α : Type} (f g : α → Bool) : ∀ l : List α, cnt (l.map (fun r => f r || g r)) ≤ cnt (l.map f) + cnt (l.map g)
  | [] => Nat.le_refl 0
  | a :: l => by
    have ih := cnt_or f g l
    show cnt ((f a || g a) :: l.map _) ≤ cnt (f a :: l.map f) + cnt (g a :: l.map g)
    cases hf : f a <;> cases hg : g a
    · exact ih
    · show cnt _ + 1 ≤ cnt _ + (cnt _ + 1); exact Nat.succ_le_succ ih
    · show cnt _ + 1 ≤ (cnt _ + 1) + cnt _
      rw [Nat.succ_add]; exact Nat.succ_le_succ ih
    · show cnt _ + 1 ≤ (cnt _ + 1) + (cnt _ + 1)
      rw [Nat.succ_add]; exact Nat.succ_le_succ (Nat.le_trans ih (Nat.add_le_add_left (Nat.le_succ _) _))

/-- **At most 16d plaquettes touch p ∪ q.** -/
theorem sc_le (d : Nat) (LL : List Lk) (hLL : nodupL LL) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p q : Pq) :
    cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p) (vec LL q)))) ≤ 4 * (2 * d) + 4 * (2 * d) := by
  have e : ∀ r, touch r (zadd (vec LL p) (vec LL q)) = (touch r (vec LL p) || touch r (vec LL q)) := fun r => touch_zadd r _ _
  rw [cnt_map_eq _ _ e]
  exact Nat.le_trans (cnt_or _ _ _) (Nat.add_le_add (deg_Zd d LL hLL Pn hnd hv p) (deg_Zd d LL hLL Pn hnd hv q))

end TouchCount

#print axioms TouchCount.touch_zadd
#print axioms TouchCount.cnt_or
#print axioms TouchCount.sc_le

ym/WilsonCauchy.lean

/-
  ym/WilsonCauchy — T45, G11: uniform infinite-volume Cauchy estimate for weights within 2^{-(k+7)} of 1 on Z^d
  (8d ≤ 2^k): wilson_cauchy = Lad5Cauchy.uniform_cauchy fed with WilsonVolStep.vol_step_k and B_le. Step 3b: the clustering constant of gap_cone_k is C·2^{-R} with C independent of the
  plaquettes: B = (2K₁)²(κ²)^{sc}(1+y)^{sc} h^R ≤ (2K₁)²(κ²)^{16d}(1+y)^{16d} h^R since sc ≤ 16d (TouchCount.sc_le)
  and κ², 1 + y ≥ 1. Lean 4 core only, no axioms.
-/
import ym.WilsonVolStep
import ym.TouchCount
import ym.Lad5Cauchy
import ym.WilsonGapK

namespace WilsonCauchy

open YMChain LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem B_le (h1 : O.le K.zero K.one) (K1 κ y h : R) (hK1 : O.le K.zero K1) (hκ : O.le K.one κ) (hy : O.le K.zero y)
    (h0 : O.le K.zero h) (sc D Rr : Nat) (hsc : sc ≤ D) :
    O.le (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
        (K.mul (pwR K (K.add K.one y) sc) (pwR K h Rr)))
      (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) D))
        (K.mul (pwR K (K.add K.one y) D) (pwR K h Rr))) := by
  have κ0 : O.le K.zero κ := O.le_trans _ _ _ h1 hκ
  have kk : O.le K.one (K.mul κ κ) := by
    have := O.mul_le_mul_right _ _ _ hκ κ0; rw [K.one_mul] at this; exact O.le_trans _ _ _ hκ this
  have y1 : O.le K.one (K.add K.one y) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) hy; rw [DetChain.add_zero'] at this; exact this
  have a := GapConeD.le_pow K O h1 _ kk sc D hsc
  have b := GapConeD.le_pow K O h1 _ y1 sc D hsc
  have kk0 := O.le_trans _ _ _ h1 kk
  have y0 := O.le_trans _ _ _ h1 y1
  have k2 : O.le K.zero (K.add K1 K1) := by have := O.add_le_add _ _ _ _ hK1 hK1; rw [K.zero_add] at this; exact this
  have c0 := O.mul_nonneg _ _ k2 k2
  have hr := AreaLower.pw_nn K O h h0 h1 Rr
  have l := Gap.mul_le_mul4 K O _ _ _ _ c0 (AreaLower.pw_nn K O _ kk0 h1 sc) (AreaLower.pw_nn K O _ kk0 h1 D) (O.le_refl _) a
  have r := Gap.mul_le_mul4 K O _ _ _ _ (AreaLower.pw_nn K O _ y0 h1 sc) hr hr b (O.le_refl _)
  exact Gap.mul_le_mul4 K O _ _ _ _ (O.mul_nonneg _ _ c0 (AreaLower.pw_nn K O _ kk0 h1 sc))
    (O.mul_nonneg _ _ (AreaLower.pw_nn K O _ y0 h1 sc) hr)
    (O.mul_nonneg _ _ (AreaLower.pw_nn K O _ y0 h1 D) hr) l r

open GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume

variable (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Uniform infinite-volume Cauchy estimate** for the normalised weight s(1 + f₀), |f₀| ≤ 2^{-(k+7)}, on Z^d
  (8d ≤ 2^k): adding plaquettes y 0, …, y (n−1) (volumes Ms), each at shell distance sh (y i) from the observable's
  plaquette p0, changes ⟨Ob⟩ by at most T, (1 − r)T ≤ κ C s r^{Kk}, independently of n (r = β/2). -/
theorem wilson_cauchy (d k : Nat) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (f0 : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hfaG : ∀ i, H.adm (f0 i)) (hUpa : H.adm Ob)
    (hf0 : ∀ i x, O.le (K.neg (pwR K h (k + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + 7)))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hUq : ∀ i, i < n → depOn ((vec LL (q i)).map nzb) (fun x => K.add (F.inv (K.add K.one (pwR K h (k + 7))))
      (K.mul (F.inv (K.add K.one (pwR K h (k + 7)))) (f0 (y i) x))))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N s β : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (hcnt : ∀ j, ShellSum.ccount sh j ((ShellSum.rng n).map y) ≤ s * β ^ j)
    (hr1 : O.le (K.mul h (natR K β)) K.one) :
    let sw := F.inv (K.add K.one (pwR K h (k + 7)))
    let κ := K.add K.one (pwR K h (k + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let r := K.mul h (natR K β)
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) := by
  intro sw κ w a C r T
  have hκ : O.le K.one κ := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) (AreaLower.pw_nn K O h h0 h1 (k + 4)); rw [DetChain.add_zero'] at this; exact this
  have κ0 := O.le_trans _ _ _ h1 hκ
  have K10 := O.le_trans _ _ _ h1 hK1
  have y0 := AreaLower.pw_nn K O h h0 h1 (k + 1)
  have C0 : O.le K.zero C := by
    have kk : O.le K.zero (K.mul κ κ) := O.mul_nonneg _ _ κ0 κ0
    have k2 : O.le K.zero (K.add K1 K1) := by have := O.add_le_add _ _ _ _ K10 K10; rw [K.zero_add] at this; exact this
    have yy : O.le K.zero (K.add K.one (pwR K h (k + 1))) := by
      have := O.add_le_add _ _ _ _ h1 y0; rw [K.zero_add] at this; exact this
    exact O.mul_nonneg _ _ h0 (O.mul_nonneg _ _ (O.mul_nonneg _ _ (O.mul_nonneg _ _ k2 k2) (AreaLower.pw_nn K O _ kk h1 _))
      (AreaLower.pw_nn K O _ yy h1 _))
  apply Lad5Cauchy.uniform_cauchy K O h1 a y n κ C h κ0 C0 h0 sh Kk N s β _ hsh hcnt hr1
  intro i hi
  have vs := WilsonVolStep.vol_step_k K O F H d k hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 (q i) (hpq i hi)
    f0 Ob (y i) K1 (sh (y i)) hfd hUp (hUq i hi) hfaG hUpa hf0 hK1 hUpb (hdist i hi) (Ms i) (hlen i hi) (hyl i hi) (hyM i hi)
  have bl := B_le K O h1 K1 κ (pwR K h (k + 1)) h K10 hκ y0 h0 _ _ (sh (y i))
    (TouchCount.sc_le d LL hLL Pn hnd hv p0 (q i))
  -- (h B) κ ≤ (κ C) h^{sh}
  have mb := O.mul_le_mul_right _ _ κ (O.mul_le_mul_right _ _ h bl h0 |> fun x => by rw [K.mul_comm _ h, K.mul_comm _ h] at x; exact x) κ0
  have eq : K.mul (K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (K.mul (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))) (pwR K h (sh (y i)))))) κ
      = K.mul (K.mul κ C) (pwR K h (sh (y i))) := by
    show _ = K.mul (K.mul κ (K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d)))))) (pwR K h (sh (y i)))
    lib_ring
  rw [eq] at mb
  show O.le _ (K.add (E K O F (ZxH K O H w (Ms (i + 1)) Ob) (ZxH K O H w (Ms (i + 1)) (fun _ => K.one))) _) ∧
    O.le (K.add (E K O F (ZxH K O H w (Ms (i + 1)) Ob) (ZxH K O H w (Ms (i + 1)) (fun _ => K.one))) _) _
  rw [hMs i]
  exact ⟨O.le_trans _ _ _ (ChiBound.neg_le K O _ _ mb) vs.1, O.le_trans _ _ _ vs.2 mb⟩

/-- **G12: the uniform infinite-volume Cauchy estimate for the (truncated) SU(2) Wilson weight itself**, every
  dimension, β ≤ 2·2^{-(4(j+1)+1)} with k + 7 ≤ 4j + 1, 8d ≤ 2^k. -/
theorem wilson_cauchy_W (d k j : Nat) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + 2)) (hkj : k + 7 ≤ 4 * j + 1)
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (pwR K h (4 * (j + 1) + 1))) (Kk' : Nat)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hUq : ∀ i, i < n → depOn ((vec LL (q i)).map nzb) (tr (y i)))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N s β : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (hcnt : ∀ j, ShellSum.ccount sh j ((ShellSum.rng n).map y) ≤ s * β ^ j)
    (hr1 : O.le (K.mul h (natR K β)) K.one) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (WilsonCone.wl K O F t Kk' 11).tail (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + 7)))
    let κ := K.add K.one (pwR K h (k + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let r := K.mul h (natR K β)
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) := by
  intro f0
  have hc := WilsonGapK.wilson_cone_j K O F j h1 h10 h t hh h0 ht0 ht Kk' 10
  have hl := GapInputs.hle_one K O h1 h hh h0
  have hx : K.mul (MigdalCone.four K) (pwR K (pwR K h (j + 1)) 4) = pwR K h (4 * j + 2) := by
    rw [Migdal.pw_pw, show (j + 1) * 4 = (4 * j + 2) + 2 from by lib_nat, GKS.pwR_add]
    show K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.mul (pwR K h (4 * j + 2)) (K.mul (K.mul K.one h) h)) = _
    have e : K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.mul (pwR K h (4 * j + 2)) (K.mul (K.mul K.one h) h))
        = K.mul (pwR K h (4 * j + 2)) (K.mul (K.add h h) (K.add h h)) := by lib_ring
    rw [e, hh, K.one_mul, K.mul_comm, K.one_mul]
  have x2 : K.add (pwR K h (4 * j + 2)) (pwR K h (4 * j + 2)) = pwR K h (4 * j + 1) := GapInputsK.dbl K h hh (4 * j + 1)
  have hxx : O.le (K.add (pwR K h (4 * j + 2)) (pwR K h (4 * j + 2))) K.one := by
    rw [x2]; exact ConeBound.pw_anti K O h1 h h0 hl (4 * j + 1) 0 (Nat.zero_le _)
  have epsle : O.le (pwR K h (4 * j + 1)) (pwR K h (k + 7)) := ConeBound.pw_anti K O h1 h h0 hl _ _ hkj
  have hf0 : ∀ i x, O.le (K.neg (pwR K h (k + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + 7)) := fun i x => by
    have := ConeGap.activity K O h1 (pwR K h (j + 1)) (pwR K h (4 * j + 2)) (AreaLower.pw_nn K O h h0 h1 _) hx hxx 1
      _ hc.2 (ChiBound.c K (tr i x)) (ChiBound.chi_bound K O h1 (tr i x) (htr i x))
    rw [show pwR K (pwR K h (4 * j + 2)) 1 = K.mul K.one (pwR K h (4 * j + 2)) from rfl, K.one_mul, x2] at this
    exact ⟨O.le_trans _ _ _ (ChiBound.neg_le K O _ _ epsle) this.1, O.le_trans _ _ _ this.2 epsle⟩
  have hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i) :=
    fun i c c' hcc => by show SU2Char.dot K 1 _ (ChiBound.c K (tr i c)) = _; rw [htd i c c' hcc]
  have hUq' : ∀ i, i < n → depOn ((vec LL (q i)).map nzb) (fun x => K.add (F.inv (K.add K.one (pwR K h (k + 7))))
      (K.mul (F.inv (K.add K.one (pwR K h (k + 7)))) (f0 (y i) x))) := fun i hi c c' hcc => by
    show K.add _ (K.mul _ (SU2Char.dot K 1 _ (ChiBound.c K (tr (y i) c)))) = _
    rw [hUq i hi c c' hcc]
  exact wilson_cauchy K O F H d k hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 f0 Ob K1 hfd hUp
    (fun i => GapFlow.adm_dot K O H (tr i) (hta i) 1 _) hUpa hf0 hK1 hUpb n y q sh Ms hMs hlen hyl hyM hpq hUq' hdist
    Kk N s β hsh hcnt hr1

end WilsonCauchy

#print axioms WilsonCauchy.B_le
#print axioms WilsonCauchy.wilson_cauchy
#print axioms WilsonCauchy.wilson_cauchy_W

#print axioms: 8 theorems, no axioms.

90.23 Cauchy estimate with distance-counted shells

Proved: with decay \(2^{-(a+1)}\) and \(2^{-(a+1)}3^d\le 1\), the infinite-volume Cauchy estimate holds with shells counted by distance.

\[ |w-1|\le 2^{-(k+a+7)}\ \Longrightarrow \ (1-r)\,|\langle O\rangle _{\Lambda '}-\langle O\rangle _\Lambda |\le \kappa C s\,r^{K},\ r=2^{-(a+1)}3^{d} \]

ym/GapConeKA.lean

/-
  ym/GapConeKA — T45, G14: gap_cone_k with decay rate u = 2^{-(a+1)} per lattice unit (ε = 2^{-(k+a+7)}): if
  8d ≤ 2^k and 2(8d+1) ≤ 2^{k+2}, every plaquette weight within 2^{-(k+7)} of 1 (≈ 1/(1024 d)), normalised by
  s = 1/(1 + 2^{-(k+7)}), satisfies GapZd.gap_Zd with κ = 1 + 2^{-(k+4)}, u = 1/2, x = 2^{-(k+2)}, y = 2^{-(k+1)}
  (GapInputsK): connected plaquette correlations at distance R + 2 are ≤ ½ B 2⁻ᴿ.
  Lean 4 core only, no axioms.
-/
import ym.GapZd
import ym.GapInputsK
import lib.Semi

namespace GapConeKA

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Strong-coupling gap on Z^d, every d, for weights within 2^{-(k+7)} of 1 (8d ≤ 2^k).** -/
theorem gap_cone_ka (d k a : Nat) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + a + 2)) (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (f0 : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hfaG : ∀ i, H.adm (f0 i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (hf0 : ∀ i x, O.le (K.neg (pwR K h (k + a + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + a + 7)))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let s := F.inv (K.add K.one (pwR K h (k + a + 7)))
    let κ := K.add K.one (pwR K h (k + a + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + 1))) sc) (pwR K (pwR K h (a + 1)) Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro s κ
  have hsm : O.le (K.mul (K.add (K.add (pwR K h (k + a + 7)) (pwR K h (k + a + 7))) (K.mul (pwR K h (k + a + 7)) (pwR K h (k + a + 7))))
      (pwR K (K.mul κ κ) (4 * (2 * d) + 1))) (K.mul (pwR K h (a + 1)) (pwR K h (k + 2))) := by
    have := GapInputsK.hsmallK K O h1 h hh h0 d (k + a) hd16
    have e : K.mul h (pwR K h (k + a + 2)) = K.mul (pwR K h (a + 1)) (pwR K h (k + 2)) := by
      rw [← GKS.pwR_add, show a + 1 + (k + 2) = (k + a + 2) + 1 from by lib_nat]
      show _ = K.mul (pwR K h (k + a + 2)) h; rw [K.mul_comm]
    rw [e] at this; exact this
  obtain ⟨sb, mb, hκ, km, hw⟩ := GapInputsK.norm_okK K O F (k + a) h1 h10 h hh h0
  have hfd' : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (fun x => K.mul s (f0 i x)) :=
    fun i c c' hc => by show K.mul s (f0 i c) = K.mul s (f0 i c'); rw [hfd i c c' hc]
  have hfa' : ∀ i, H.adm (fun x => K.mul s (f0 i x)) := fun i => H.adm_mul _ _ (H.adm_const s) (hfaG i)
  exact GapZd.gap_Zd K O H DH h1 h10 d LL hLL hL Pn hnd hv p0 q0 hpq (fun i x => K.mul s (f0 i x)) Up Uq
    s (F.inv κ) κ (pwR K h (k + a + 7)) K1 (pwR K h (a + 1)) (pwR K h (k + 2)) (pwR K h (k + 1)) Rr hfd' hUp hUq hfa' hUpa hUqa
    sb.1 sb.2 mb.1 mb.2 hκ km
    (fun i x => ((hw (f0 i x) (hf0 i x).1 (hf0 i x).2).1).1)
    (fun i x => ((hw (f0 i x) (hf0 i x).1 (hf0 i x).2).1).2)
    (AreaLower.pw_nn K O h h0 h1 (k + a + 7))
    (fun i x => (hw (f0 i x) (hf0 i x).1 (hf0 i x).2).2)
    hK1 hUpb hUqb (AreaLower.pw_nn K O h h0 h1 (a + 1))
    (ConeBound.pw_anti K O h1 h h0 (GapInputs.hle_one K O h1 h hh h0) (a + 1) 0 (Nat.zero_le _))
    (AreaLower.pw_nn K O h h0 h1 (k + 2)) (AreaLower.pw_nn K O h h0 h1 (k + 1))
    hsm (GapInputsK.hxyK K O h1 h hh h0 d k hd8) hdist M hM

end GapConeKA

#print axioms GapConeKA.gap_cone_ka

ym/DistCount.lean

/-
  ym/DistCount — T45, G18a: counting lattice points by ℓ∞ distance, translation-invariantly.
    win a m = [a − m + j | j < 2m + 1]      mem_win: a ≤ c + m, c ≤ a + m ⇒ c ∈ win a m;  len_win = 2m + 1
  Lean 4 core only, no axioms.
-/
import ym.Cube
import lib.NatSub
import ym.ZdDist
import ym.ShellSum

namespace DistCount

open YMChain Cube Degree ZdDist ZdLinks Closure InjCount ShellSum

def win (a m : Nat) : List Nat := (rng0 (2 * m + 1)).map (fun j => j + a - m)

theorem len_map'' {α β : Type} (f : α → β) : ∀ l : List α, (l.map f).length = l.length
  | [] => rfl
  | _ :: l => by show (List.map f l).length + 1 = l.length + 1; rw [len_map'' f l]

theorem len_app {α : Type} : ∀ (l m : List α), (l ++ m).length = l.length + m.length
  | [], m => (Nat.zero_add _).symm
  | _ :: l, m => by
    show (l ++ m).length + 1 = (l.length + 1) + m.length
    rw [len_app l m, Nat.succ_add]

theorem len_rng0 : ∀ n, (rng0 n).length = n
  | 0 => rfl
  | n + 1 => by
    show (rng0 n ++ [n]).length = n + 1
    rw [len_app, len_rng0 n]; rfl

theorem len_win (a m : Nat) : (win a m).length = 2 * m + 1 := by
  unfold win; rw [len_map'', len_rng0]

theorem le_cancel_l : ∀ (a t s : Nat), a + t ≤ a + s → t ≤ s
  | 0, t, s, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | a + 1, t, s, h => by
    rw [Nat.succ_add, Nat.succ_add] at h
    exact le_cancel_l a t s (Nat.le_of_succ_le_succ h)

theorem mem_win (a m c : Nat) (h1 : a ≤ c + m) (h2 : c ≤ a + m) : memL c (win a m) := by
  obtain ⟨t, ht⟩ := LibNatSub.le_ex h1
  -- c + m = a + t, t ≤ 2m
  have ht2 : t ≤ 2 * m := by
    have : a + t ≤ a + 2 * m := by
      rw [← ht, show 2 * m = m + m from by lib_nat, ← Nat.add_assoc]
      exact Nat.add_le_add_right h2 m
    exact le_cancel_l a t (2 * m) this
  have e : t + a - m = c := by
    rw [Nat.add_comm t a, ← ht]; exact LibNatSub.add_sub_r c m
  rw [← e]
  exact mem_map_f (fun j => j + a - m) (rng0 (2 * m + 1)) t (mem_rng0 _ t (Nat.lt_succ_of_le ht2))

def winCube : List Nat → Nat → List (List Nat)
  | [], _ => [[]]
  | a :: x, m => consAll (winCube x m) (win a m)

theorem len_consAll (vs : List (List Nat)) : ∀ cs : List Nat, (consAll vs cs).length = cs.length * vs.length
  | [] => (Nat.zero_mul _).symm
  | c :: cs => by
    show (vs.map (fun v => c :: v) ++ consAll vs cs).length = (cs.length + 1) * vs.length
    rw [len_app, len_map'', len_consAll vs cs, Nat.succ_mul, Nat.add_comm]

theorem len_winCube (m : Nat) : ∀ x : List Nat, (winCube x m).length = (2 * m + 1) ^ x.length
  | [] => rfl
  | a :: x => by
    show (consAll (winCube x m) (win a m)).length = (2 * m + 1) ^ (x.length + 1)
    rw [len_consAll, len_win, len_winCube m x, Nat.pow_succ, Nat.mul_comm]

theorem mem_winCube (m : Nat) : ∀ (x y : List Nat), y.length = x.length → dcl m x y → memL y (winCube x m)
  | [], [], _, _ => Or.inl rfl
  | [], _ :: _, h, _ => absurd h Nat.noConfusion
  | _ :: _, [], h, _ => absurd h Nat.noConfusion
  | a :: x, c :: y, h, hd => by
    have h0 := hd 0
    have ht : dcl m x y := fun i => hd (i + 1)
    exact mem_consAll (winCube x m) y (mem_winCube m x y (Nat.succ.inj h) ht) (win a m) c
      (mem_win a m c h0.1 h0.2)

/-- plaquettes with base within distance m of x₀ -/
def winP (x0 : List Nat) (m d : Nat) : List Pq := withPairs (pairs d) (winCube x0 m)

theorem len_withPairs (ps : List (Nat × Nat)) : ∀ xs : List (List Nat), (withPairs ps xs).length = xs.length * ps.length
  | [] => (Nat.zero_mul _).symm
  | x :: xs => by
    show (ps.map (fun ab => (x, ab.1, ab.2)) ++ withPairs ps xs).length = (xs.length + 1) * ps.length
    rw [len_app, len_map'', len_withPairs ps xs, Nat.succ_mul, Nat.add_comm]

theorem len_pairs_le : ∀ d, (pairs d).length ≤ d * d
  | 0 => Nat.le_refl 0
  | d + 1 => by
    show (pairs d ++ (rng0 d).map (fun a => (a, d))).length ≤ (d + 1) * (d + 1)
    rw [len_app, len_map'', len_rng0]
    have ih := len_pairs_le d
    have e : (d + 1) * (d + 1) = d * d + d + (d + 1) := by lib_nat
    rw [e]
    exact Nat.le_trans (Nat.add_le_add_right ih d) (Nat.le_add_right _ _)

theorem mem_winP (x0 : List Nat) (m d : Nat) (q : Pq) (hl : q.1.length = x0.length) (hd : dcl m x0 q.1)
    (hab : q.2.1 < q.2.2) (hb : q.2.2 < d) : memL q (winP x0 m d) :=
  mem_withPairs (pairs d) (q.2.1, q.2.2) (mem_pairs d _ _ hab hb) (winCube x0 m) q.1 (mem_winCube m x0 q.1 hl hd)

/-- **Shell count by distance.** If the indices ys are distinct, index z carries the plaquette plaq z, distinct indices
  carry distinct plaquettes, and every index of shell k sits within distance m of x₀ (as a valid plaquette of Z^d with
  base of the right length), then #shell k ≤ (2m+1)^{#x₀} · d². -/
theorem shell_count_dist (sh : Nat → Nat) (k : Nat) (plaq : Nat → Pq) (x0 : List Nat) (m d : Nat)
    (ys : List Nat) (hnd : nodupL ys)
    (hin : ∀ z, memL z ys → sh z = k → (plaq z).1.length = x0.length ∧ dcl m x0 (plaq z).1 ∧
      (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < d)
    (hinj : ∀ z z', memL z ys → memL z' ys → plaq z = plaq z' → z = z') :
    ccount sh k ys ≤ (2 * m + 1) ^ x0.length * (d * d) := by
  have c := count_inj (fun z => decide (sh z = k)) plaq ys (winP x0 m d) hnd
    (fun z hz hφ => by
      obtain ⟨h1, h2, h3, h4⟩ := hin z hz (of_decide_eq_true hφ)
      exact mem_winP x0 m d _ h1 h2 h3 h4)
    (fun z z' hz hz' _ _ e => hinj z z' hz hz' e)
  unfold winP at c
  rw [len_withPairs, len_winCube] at c
  exact Nat.le_trans c (Nat.mul_le_mul_left _ (len_pairs_le d))

theorem lin_le_pow3 : ∀ k, 2 * k + 5 ≤ 5 * 3 ^ k
  | 0 => Nat.le_refl 5
  | k + 1 => by
    have ih := lin_le_pow3 k
    have e1 : 2 * (k + 1) + 5 = (2 * k + 5) + 2 := by lib_nat
    have e2 : 5 * 3 ^ (k + 1) = 5 * 3 ^ k + (5 * 3 ^ k + 5 * 3 ^ k) := by rw [Nat.pow_succ]; lib_nat
    rw [e1, e2]
    have p1 : 1 ≤ 5 * 3 ^ k := Nat.le_trans (Nat.succ_le_succ (Nat.zero_le (2 * k + 4))) ih
    exact Nat.add_le_add ih (Nat.add_le_add p1 p1)

theorem mul_pow' (a b : Nat) : ∀ d, (a * b) ^ d = a ^ d * b ^ d
  | 0 => rfl
  | d + 1 => by rw [Nat.pow_succ, Nat.pow_succ, Nat.pow_succ, mul_pow' a b d]; lib_nat

theorem pow_pow_comm (a : Nat) : ∀ k d, (a ^ k) ^ d = (a ^ d) ^ k
  | k, 0 => by show 1 = (1 : Nat) ^ k; exact (Nat.one_pow k).symm
  | k, d + 1 => by
    rw [Nat.pow_succ, pow_pow_comm a k d, Nat.pow_succ a d, mul_pow']

/-- **Exponential shell bound:** #shell k ≤ (d² 5^{#x₀}) (3^{#x₀})^k when shell k lies within distance k + 2. -/
theorem shell_exp (sh : Nat → Nat) (plaq : Nat → Pq) (x0 : List Nat) (d : Nat) (ys : List Nat) (hnd : nodupL ys)
    (hin : ∀ k z, memL z ys → sh z = k → (plaq z).1.length = x0.length ∧ dcl (k + 2) x0 (plaq z).1 ∧
      (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < d)
    (hinj : ∀ z z', memL z ys → memL z' ys → plaq z = plaq z' → z = z') :
    ∀ k, ccount sh k ys ≤ (d * d * 5 ^ x0.length) * (3 ^ x0.length) ^ k := by
  intro k
  have c := shell_count_dist sh k plaq x0 (k + 2) d ys hnd (hin k) hinj
  have b : (2 * (k + 2) + 1) ^ x0.length ≤ 5 ^ x0.length * (3 ^ x0.length) ^ k := by
    rw [show 2 * (k + 2) + 1 = 2 * k + 5 from by lib_nat, ← pow_pow_comm, ← mul_pow']
    exact Nat.pow_le_pow_left (lin_le_pow3 k) _
  refine Nat.le_trans c ?_
  have := Nat.mul_le_mul_right (d * d) b
  rw [show d * d * 5 ^ x0.length * (3 ^ x0.length) ^ k = 5 ^ x0.length * (3 ^ x0.length) ^ k * (d * d) from by lib_nat]
  exact this

end DistCount

#print axioms DistCount.len_win
#print axioms DistCount.mem_win
#print axioms DistCount.len_winCube
#print axioms DistCount.mem_winCube
#print axioms DistCount.shell_count_dist
#print axioms DistCount.shell_exp
#print axioms DistCount.lin_le_pow3
#print axioms DistCount.mul_pow'
#print axioms DistCount.pow_pow_comm

ym/WilsonCauchyD.lean

/-
  ym/WilsonCauchyD — T45, G18d: the uniform infinite-volume Cauchy estimate with shells counted by distance
  (DistCount.shell_exp: #shell k ≤ d²5^{d'}(3^{d'})^k when shell k lies within distance k + 2 of p₀, d' = #p₀.1) and decay
  2^{-(a+1)} with 2^{-(a+1)}·3^{d'} ≤ 1. Unlike WilsonCauchyA (ball-counted shells), the distance hypotheses here are
  jointly satisfiable: dist(p₀, y) = sh(y) + 2. Lean 4 core only, no axioms.
-/
import ym.WilsonCauchy
import ym.WilsonVolStepA
import ym.DistCount

namespace WilsonCauchyD

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **G18d: uniform infinite-volume Cauchy estimate with decay 2^{-(a+1)} and distance-counted shells** (|f₀| ≤ 2^{-(k+a+7)}) on Z^d
  (8d ≤ 2^k): adding plaquettes y 0, …, y (n−1) (volumes Ms), each at shell distance sh (y i) from the observable's
  plaquette p0, changes ⟨Ob⟩ by at most T, (1 − r)T ≤ κ C s r^{Kk}, independently of n (r = β/2). -/
theorem wilson_cauchy_d (d k ad : Nat) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (f0 : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hfaG : ∀ i, H.adm (f0 i)) (hUpa : H.adm Ob)
    (hf0 : ∀ i x, O.le (K.neg (pwR K h (k + ad + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + ad + 7)))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hUq : ∀ i, i < n → depOn ((vec LL (q i)).map nzb) (fun x => K.add (F.inv (K.add K.one (pwR K h (k + ad + 7))))
      (K.mul (F.inv (K.add K.one (pwR K h (k + ad + 7)))) (f0 (y i) x))))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (plaq : Nat → Pq) (hndy : nodupL ((ShellSum.rng n).map y))
    (hshell : ∀ kk z, memL z ((ShellSum.rng n).map y) → sh z = kk → (plaq z).1.length = p0.1.length ∧
      dcl (kk + 2) p0.1 (plaq z).1 ∧ (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < d)
    (hinj : ∀ z z', memL z ((ShellSum.rng n).map y) → memL z' ((ShellSum.rng n).map y) → plaq z = plaq z' → z = z')
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one) :
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) := by
  intro sw κ w a C s r T
  have hκ : O.le K.one κ := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) (AreaLower.pw_nn K O h h0 h1 (k + ad + 4)); rw [DetChain.add_zero'] at this; exact this
  have κ0 := O.le_trans _ _ _ h1 hκ
  have K10 := O.le_trans _ _ _ h1 hK1
  have y0 := AreaLower.pw_nn K O h h0 h1 (k + 1)
  have C0 : O.le K.zero C := by
    have kk : O.le K.zero (K.mul κ κ) := O.mul_nonneg _ _ κ0 κ0
    have k2 : O.le K.zero (K.add K1 K1) := by have := O.add_le_add _ _ _ _ K10 K10; rw [K.zero_add] at this; exact this
    have yy : O.le K.zero (K.add K.one (pwR K h (k + 1))) := by
      have := O.add_le_add _ _ _ _ h1 y0; rw [K.zero_add] at this; exact this
    exact O.mul_nonneg _ _ h0 (O.mul_nonneg _ _ (O.mul_nonneg _ _ (O.mul_nonneg _ _ k2 k2) (AreaLower.pw_nn K O _ kk h1 _))
      (AreaLower.pw_nn K O _ yy h1 _))
  apply Lad5Cauchy.uniform_cauchy K O h1 a y n κ C (pwR K h (ad + 1)) κ0 C0 (AreaLower.pw_nn K O h h0 h1 (ad + 1)) sh Kk N s (3 ^ p0.1.length) _ hsh
    (DistCount.shell_exp sh plaq p0.1 d _ hndy hshell hinj) hr1
  intro i hi
  have vs := WilsonVolStepA.vol_step_ka K O F H d k ad hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 (q i) (hpq i hi)
    f0 Ob (y i) K1 (sh (y i)) hfd hUp (hUq i hi) hfaG hUpa hf0 hK1 hUpb (hdist i hi) (Ms i) (hlen i hi) (hyl i hi) (hyM i hi)
  have bl := B_le K O h1 K1 κ (pwR K h (k + 1)) (pwR K h (ad + 1)) K10 hκ y0 (AreaLower.pw_nn K O h h0 h1 (ad + 1)) _ _ (sh (y i))
    (TouchCount.sc_le d LL hLL Pn hnd hv p0 (q i))
  -- (h B) κ ≤ (κ C) h^{sh}
  have mb := O.mul_le_mul_right _ _ κ (O.mul_le_mul_right _ _ h bl h0 |> fun x => by rw [K.mul_comm _ h, K.mul_comm _ h] at x; exact x) κ0
  have eq : K.mul (K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (K.mul (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))) (pwR K (pwR K h (ad + 1)) (sh (y i)))))) κ
      = K.mul (K.mul κ C) (pwR K (pwR K h (ad + 1)) (sh (y i))) := by
    show _ = K.mul (K.mul κ (K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d)))))) (pwR K (pwR K h (ad + 1)) (sh (y i)))
    lib_ring
  rw [eq] at mb
  show O.le _ (K.add (E K O F (ZxH K O H w (Ms (i + 1)) Ob) (ZxH K O H w (Ms (i + 1)) (fun _ => K.one))) _) ∧
    O.le (K.add (E K O F (ZxH K O H w (Ms (i + 1)) Ob) (ZxH K O H w (Ms (i + 1)) (fun _ => K.one))) _) _
  rw [hMs i]
  exact ⟨O.le_trans _ _ _ (ChiBound.neg_le K O _ _ mb) vs.1, O.le_trans _ _ _ vs.2 mb⟩

end WilsonCauchyD

#print axioms WilsonCauchyD.wilson_cauchy_d

#print axioms: 9 theorems, no axioms.

90.24 Wilson theory at \(\beta \le 2^{-32}\): infinite-volume Cauchy estimate on \(\mathbb Z^4\)

Proved: the truncated Wilson weight at \(\beta \le 2^{-32}\) lies in \(\mathrm {Cone}(2^{-8})\), so expectations are uniformly Cauchy as the volume grows.

\[ (1-r)\,|\langle O\rangle _{\Lambda '}-\langle O\rangle _\Lambda |\le \kappa C s\,r^{K},\qquad r=2^{-7}\cdot 81 \]

ym/SupportVec.lean

/-
  ym/SupportVec — T45, G16: the support used for the weights (Poly.spP, padded to L links) equals the link-incidence
  support of the plaquette: if the j-th incidence vector is vec LL q and LL.length = L, then
  spP L (Pn.map (vec LL)) j = (vec LL q).map nzb, so depOn transfers (hUq of the Cauchy theorems follows from htd).
  Lean 4 core only, no axioms.
-/
import ym.ZdDegree

namespace SupportVec

open YMChain Closure Factor Seeds Indep Poly ZdLinks ZdDegree

variable {R : Type} {G : Type}

theorem len_map {α β : Type} (f : α → β) : ∀ l : List α, (l.map f).length = l.length
  | [] => rfl
  | _ :: l => by show (List.map f l).length + 1 = l.length + 1; rw [len_map f l]

theorem len_vec (LL : List Lk) (q : Pq) : (vec LL q).length = LL.length := len_map _ LL

theorem spP_vec (L : Nat) (LL : List Lk) (hL : LL.length = L) (Pn : List Pq) (j : Nat) (q : Pq)
    (hq : nthL (Pn.map (vec LL)) j = vec LL q) : spP L (Pn.map (vec LL)) j = (vec LL q).map nzb := by
  unfold spP
  rw [hq]
  exact padB_eq L _ (by rw [len_map, len_vec, hL])

theorem depOn_vec (L : Nat) (LL : List Lk) (hL : LL.length = L) (Pn : List Pq) (j : Nat) (q : Pq)
    (hq : nthL (Pn.map (vec LL)) j = vec LL q) (F : List G → R)
    (h : depOn (spP L (Pn.map (vec LL)) j) F) : depOn ((vec LL q).map nzb) F := by
  rw [← spP_vec L LL hL Pn j q hq]; exact h

end SupportVec

#print axioms SupportVec.spP_vec
#print axioms SupportVec.depOn_vec

ym/CauchyCone8.lean

/-
  ym/CauchyCone8 — T45, G15: a weight in Cone(2⁻⁸) is within 2⁻²⁹ of 1 (ConeGap.activity with r = 2⁻⁸), so the uniform
  infinite-volume Cauchy estimate WilsonCauchyA.wilson_cauchy_a applies whenever k + a + 7 ≤ 29 (d = 4: k = a = 5,
  decay 2⁻⁶ beats the ball growth 34). Lean 4 core only, no axioms.
-/
import ym.WilsonCauchyD
import ym.SupportVec

namespace CauchyCone8

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Cone(2⁻⁸) ⇒ uniform infinite-volume Cauchy estimate** (plaquette weight 1 + Σ_{n≥1} cf_n χ_n(tr)). -/
theorem cauchy_of_cone8 (d k ad : Nat) (hka : k + ad + 7 ≤ 29) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (cf : List R) (hcf : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: cf))
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hq : ∀ i, i < n → nthL (Pn.map (vec LL)) (y i) = vec LL (q i))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (plaq : Nat → Pq) (hndy : nodupL ((ShellSum.rng n).map y))
    (hshell : ∀ kk z, memL z ((ShellSum.rng n).map y) → sh z = kk → (plaq z).1.length = p0.1.length ∧
      dcl (kk + 2) p0.1 (plaq z).1 ∧ (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < d)
    (hinj : ∀ z z', memL z ((ShellSum.rng n).map y) → memL z' ((ShellSum.rng n).map y) → plaq z = plaq z' → z = z')
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) := by
  intro f0
  have hl := GapInputs.hle_one K O h1 h hh h0
  have hx : K.mul (MigdalCone.four K) (pwR K (pwR K h 8) 4) = pwR K h 30 := by
    rw [Migdal.pw_pw, show 8 * 4 = 30 + 2 from rfl, GKS.pwR_add]
    show K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.mul (pwR K h 30) (K.mul (K.mul K.one h) h)) = _
    have e : K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.mul (pwR K h 30) (K.mul (K.mul K.one h) h))
        = K.mul (pwR K h 30) (K.mul (K.add h h) (K.add h h)) := by lib_ring
    rw [e, hh, K.one_mul, K.mul_comm, K.one_mul]
  have x2 : K.add (pwR K h 30) (pwR K h 30) = pwR K h 29 := GapInputsK.dbl K h hh 29
  have hxx : O.le (K.add (pwR K h 30) (pwR K h 30)) K.one := by
    rw [x2]; exact ConeBound.pw_anti K O h1 h h0 hl 29 0 (Nat.zero_le _)
  have epsle : O.le (pwR K h 29) (pwR K h (k + ad + 7)) := ConeBound.pw_anti K O h1 h h0 hl _ _ hka
  have hf0 : ∀ i x, O.le (K.neg (pwR K h (k + ad + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + ad + 7)) := fun i x => by
    have := ConeGap.activity K O h1 (pwR K h 8) (pwR K h 30) (AreaLower.pw_nn K O h h0 h1 _) hx hxx 1
      _ hcf.2 (ChiBound.c K (tr i x)) (ChiBound.chi_bound K O h1 (tr i x) (htr i x))
    rw [show pwR K (pwR K h 30) 1 = K.mul K.one (pwR K h 30) from rfl, K.one_mul, x2] at this
    exact ⟨O.le_trans _ _ _ (ChiBound.neg_le K O _ _ epsle) this.1, O.le_trans _ _ _ this.2 epsle⟩
  have hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i) :=
    fun i c c' hcc => by show SU2Char.dot K 1 _ (ChiBound.c K (tr i c)) = _; rw [htd i c c' hcc]
  have hUq' : ∀ i, i < n → depOn ((vec LL (q i)).map nzb) (fun x => K.add (F.inv (K.add K.one (pwR K h (k + ad + 7))))
      (K.mul (F.inv (K.add K.one (pwR K h (k + ad + 7)))) (f0 (y i) x))) := fun i hi c c' hcc => by
    show K.add _ (K.mul _ (SU2Char.dot K 1 _ (ChiBound.c K (tr (y i) c)))) = _
    rw [SupportVec.depOn_vec L LL hL Pn (y i) (q i) (hq i hi) (tr (y i)) (htd (y i)) c c' hcc]
  exact WilsonCauchyD.wilson_cauchy_d K O F H d k ad hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 f0 Ob K1 hfd hUp
    (fun i => GapFlow.adm_dot K O H (tr i) (hta i) 1 _) hUpa hf0 hK1 hUpb n y q sh Ms hMs hlen hyl hyM hpq hUq' hdist
    Kk N hsh plaq hndy hshell hinj hr1

end CauchyCone8

#print axioms CauchyCone8.cauchy_of_cone8

ym/WilsonCauchy8.lean

/-
  ym/WilsonCauchy8 — T45: the truncated SU(2) Wilson weight itself at β ≤ 2⁻³² (every truncation) lies in Cone(2⁻⁸)
  (WilsonCone.wilson_cone), so CauchyCone8.cauchy_of_cone8 applies: the uniform infinite-volume Cauchy estimate on Z^d
  with decay 2^{-(a+1)} and distance-counted shells (d = 4: k = 5, a = 6). Lean 4 core only, no axioms.
-/
import ym.CauchyCone8

namespace WilsonCauchy8

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

theorem wilson_cauchy8 (d k ad : Nat) (hka : k + ad + 7 ≤ 29) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (pwR K h 33)) (Kk' : Nat) :
    ∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hq : ∀ i, i < n → nthL (Pn.map (vec LL)) (y i) = vec LL (q i))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (plaq : Nat → Pq) (hndy : nodupL ((ShellSum.rng n).map y))
    (hshell : ∀ kk z, memL z ((ShellSum.rng n).map y) → sh z = kk → (plaq z).1.length = p0.1.length ∧
      dcl (kk + 2) p0.1 (plaq z).1 ∧ (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < d)
    (hinj : ∀ z z', memL z ((ShellSum.rng n).map y) → memL z' ((ShellSum.rng n).map y) → plaq z = plaq z' → z = z')
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (WilsonCone.wl K O F t Kk' 11).tail (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) := by
  intro LL hLL hL Pn hnd hv p0
  have hle := GapInputs.hle_one K O h1 h hh h0
  have w := WilsonCone.wilson_cone K O F h1 h10 h t hh h0 ht0 ht Kk' 11
  have c0 := SU2CharExp.c0_ge_one K O F h1 h10 t ht0 Kk'
  have hd1 : WilsonCone.wl K O F t Kk' 11 = K.one :: (WilsonCone.wl K O F t Kk' 11).tail := by
    show K.mul (SU2CharExp.cS K O F t 0 (Kk' + 1)) (F.inv (SU2CharExp.cS K O F t 0 (Kk' + 1))) :: _ = _
    rw [F.mul_inv _ (MigdalStrong.ne_of_one_le K O h1 h10 _ c0)]; rfl
  rw [hd1] at w
  exact CauchyCone8.cauchy_of_cone8 K O F H d k ad hka hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 _ w

end WilsonCauchy8

#print axioms WilsonCauchy8.wilson_cauchy8

#print axioms: 4 theorems, no axioms.

90.25 Wilson theory at \(\beta \le 2^{-32}\): Cauchy estimate along a filling order

Proved: adding far plaquettes in any injective order, expectations of the Wilson theory on \(\mathbb Z^4\) are uniformly Cauchy; only the filling data remain as hypotheses.

\[ (1-r)\,|\langle O\rangle _{M_n}-\langle O\rangle _{M_0}|\le \kappa C s\,r^{K},\qquad r=2^{-7}\cdot 81 \]

ym/LinfDist.lean

/-
  ym/LinfDist — T45, G19a: the ℓ∞ distance of base points as a function, characterising ZdDist.dcl:
    dcl_dist     dcl (dist x y) x y
    dist_le      dcl k x y ⇒ dist x y ≤ k
  so that with sh(y) := dist(p₀, y) − 2 the shell hypotheses of WilsonCauchyD hold automatically. Lean 4 core only, no axioms.
-/
import ym.ZdDist
import lib.NatSub

namespace LinfDist

open YMChain Degree ZdDist

def mx (a b : Nat) : Nat := if Nat.ble a b = true then b else a

theorem le_mx_l (a b : Nat) : a ≤ mx a b := by
  unfold mx; cases h : Nat.ble a b
  · exact Nat.le_refl a
  · exact Nat.le_of_ble_eq_true h

theorem le_mx_r (a b : Nat) : b ≤ mx a b := by
  unfold mx; cases h : Nat.ble a b
  · have : ¬ a ≤ b := fun hab => by rw [Nat.ble_eq_true_of_le hab] at h; exact Bool.noConfusion h
    exact Nat.le_of_lt (Nat.lt_of_not_le this)
  · exact Nat.le_refl b

theorem mx_le (a b k : Nat) (ha : a ≤ k) (hb : b ≤ k) : mx a b ≤ k := by
  unfold mx; cases Nat.ble a b <;> assumption

/-- |a − b| -/
def ad (a b : Nat) : Nat := (a - b) + (b - a)

theorem ad_le1 (a b : Nat) : a ≤ b + ad a b := by
  cases Nat.le_total a b with
  | inl h => exact Nat.le_trans h (Nat.le_add_right _ _)
  | inr h =>
    unfold ad
    rw [LibNatSub.sub_eq_zero h, Nat.add_zero, Nat.add_comm]
    exact Nat.le_of_eq (LibNatSub.sub_add h).symm

theorem ad_le2 (a b : Nat) : b ≤ a + ad a b := by
  cases Nat.le_total a b with
  | inr h => exact Nat.le_trans h (Nat.le_add_right _ _)
  | inl h =>
    unfold ad
    rw [LibNatSub.sub_eq_zero h, Nat.zero_add, Nat.add_comm]
    exact Nat.le_of_eq (LibNatSub.sub_add h).symm

theorem le_cancel_r : ∀ (s t a : Nat), t + a ≤ s + a → t ≤ s
  | _, _, 0, h => h
  | s, t, a + 1, h => le_cancel_r s t a (Nat.le_of_succ_le_succ h)

theorem sub_le_of (a b k : Nat) (hab : a ≤ b) (h : b ≤ a + k) : b - a ≤ k := by
  apply le_cancel_r k (b - a) a
  rw [LibNatSub.sub_add hab, Nat.add_comm k a]; exact h

theorem ad_le (a b k : Nat) (h1 : a ≤ b + k) (h2 : b ≤ a + k) : ad a b ≤ k := by
  cases Nat.le_total a b with
  | inl h => unfold ad; rw [LibNatSub.sub_eq_zero h, Nat.zero_add]; exact sub_le_of a b k h h2
  | inr h => unfold ad; rw [LibNatSub.sub_eq_zero h, Nat.add_zero]; exact sub_le_of b a k h h1

def hd : List Nat → Nat
  | [] => 0
  | b :: _ => b

def tl : List Nat → List Nat
  | [] => []
  | _ :: y => y

def maxL : List Nat → Nat
  | [] => 0
  | b :: y => mx b (maxL y)

/-- ℓ∞ distance of coordinate lists (missing coordinates count as 0, as in nthN); structural in the first list -/
def dist : List Nat → List Nat → Nat
  | [], y => maxL y
  | a :: x, y => mx (ad a (hd y)) (dist x (tl y))

theorem nthN_hd : ∀ y : List Nat, nthN y 0 = hd y
  | [] => rfl
  | _ :: _ => rfl

theorem nthN_tl : ∀ (y : List Nat) (i : Nat), nthN y (i + 1) = nthN (tl y) i
  | [], _ => rfl
  | _ :: _, _ => rfl

theorem le_maxL : ∀ (y : List Nat) (i : Nat), nthN y i ≤ maxL y
  | [], _ => Nat.zero_le _
  | _ :: _, 0 => le_mx_l _ _
  | _ :: y, i + 1 => Nat.le_trans (le_maxL y i) (le_mx_r _ _)

theorem maxL_le (k : Nat) : ∀ y : List Nat, (∀ i, nthN y i ≤ k) → maxL y ≤ k
  | [], _ => Nat.zero_le _
  | _ :: y, h => mx_le _ _ _ (h 0) (maxL_le k y (fun i => h (i + 1)))

theorem dcl_dist : ∀ (x y : List Nat), dcl (dist x y) x y
  | [], y, i => ⟨Nat.zero_le _, by show nthN y i ≤ 0 + maxL y; rw [Nat.zero_add]; exact le_maxL y i⟩
  | a :: x, y, i => by
    show nthN (a :: x) i ≤ nthN y i + mx (ad a (hd y)) (dist x (tl y)) ∧ nthN y i ≤ nthN (a :: x) i + mx (ad a (hd y)) (dist x (tl y))
    cases i with
    | zero =>
      rw [nthN_hd y]
      exact ⟨Nat.le_trans (ad_le1 a (hd y)) (Nat.add_le_add_left (le_mx_l _ _) _),
        Nat.le_trans (ad_le2 a (hd y)) (Nat.add_le_add_left (le_mx_l _ _) _)⟩
    | succ i =>
      rw [nthN_tl y i]
      have := dcl_dist x (tl y) i
      exact ⟨Nat.le_trans this.1 (Nat.add_le_add_left (le_mx_r _ _) _), Nat.le_trans this.2 (Nat.add_le_add_left (le_mx_r _ _) _)⟩

theorem dist_le (k : Nat) : ∀ (x y : List Nat), dcl k x y → dist x y ≤ k
  | [], y, h => maxL_le k y (fun i => by have := (h i).2; rw [show nthN [] i = 0 from rfl, Nat.zero_add] at this; exact this)
  | a :: x, y, h => by
    show mx (ad a (hd y)) (dist x (tl y)) ≤ k
    apply mx_le
    · have h0 := h 0; rw [nthN_hd y] at h0; exact ad_le a (hd y) k h0.1 h0.2
    · exact dist_le k x (tl y) (fun i => by have := h (i + 1); rw [nthN_tl y i] at this; exact this)

/-- **Shells from the distance:** at distance D = s + 2, the base point is within s + 2 and not within s + 1. -/
theorem shell_of_dist (x y : List Nat) (s : Nat) (h : dist x y = s + 2) : dcl (s + 2) x y ∧ ¬ dcl (s + 1) x y :=
  ⟨h ▸ dcl_dist x y, fun hd => by
    have := dist_le (s + 1) x y hd
    rw [h] at this; exact Nat.not_succ_le_self _ this⟩

end LinfDist

#print axioms LinfDist.dcl_dist
#print axioms LinfDist.dist_le
#print axioms LinfDist.shell_of_dist

ym/VolOrder.lean

/-
  ym/VolOrder — T45, G19c-1: indexing a plaquette list. nthP Pn z is the z-th plaquette;
    nthL_vec      nthL (Pn.map (vec LL)) z = vec LL (nthP Pn z)
    nthP_mem      z < #Pn ⇒ nthP Pn z ∈ Pn
    nthP_inj      Pn duplicate-free, z, z' < #Pn, nthP Pn z = nthP Pn z' ⇒ z = z'
  Lean 4 core only, no axioms.
-/
import ym.CauchyDist
import ym.Cube

namespace VolOrder

open YMChain Seeds ZdLinks ZdDegree InjCount

def nthP : List Pq → Nat → Pq
  | [], _ => ([], 0, 0)
  | p :: _, 0 => p
  | _ :: Pn, z + 1 => nthP Pn z

theorem nthL_vec (LL : List Lk) : ∀ (Pn : List Pq) (z : Nat), z < Pn.length → nthL (Pn.map (vec LL)) z = vec LL (nthP Pn z)
  | [], _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, 0, _ => rfl
  | _ :: Pn, z + 1, h => nthL_vec LL Pn z (Nat.lt_of_succ_lt_succ h)

theorem nthP_mem : ∀ (Pn : List Pq) (z : Nat), z < Pn.length → memL (nthP Pn z) Pn
  | [], _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, 0, _ => Or.inl rfl
  | _ :: Pn, z + 1, h => Or.inr (nthP_mem Pn z (Nat.lt_of_succ_lt_succ h))

theorem nthP_inj : ∀ (Pn : List Pq), nodupL Pn → ∀ (z z' : Nat), z < Pn.length → z' < Pn.length →
    nthP Pn z = nthP Pn z' → z = z'
  | [], _, _, _, h, _, _ => absurd h (Nat.not_lt_zero _)
  | _ :: _, _, 0, 0, _, _, _ => rfl
  | p :: Pn, hnd, 0, z' + 1, _, h', e => by
    have e' : p = nthP Pn z' := e
    exact absurd (by rw [e']; exact nthP_mem Pn z' (Nat.lt_of_succ_lt_succ h')) hnd.1
  | p :: Pn, hnd, z + 1, 0, h, _, e => by
    have e' : p = nthP Pn z := e.symm
    exact absurd (by rw [e']; exact nthP_mem Pn z (Nat.lt_of_succ_lt_succ h)) hnd.1
  | _ :: Pn, hnd, z + 1, z' + 1, h, h', e =>
    congrArg Nat.succ (nthP_inj Pn hnd.2 z z' (Nat.lt_of_succ_lt_succ h) (Nat.lt_of_succ_lt_succ h') e)

/-- volumes: start from M0 and switch on y 0, y 1, … in turn -/
def Ms (M0 : List Bool) (y : Nat → Nat) : Nat → List Bool
  | 0 => M0
  | i + 1 => upd (Ms M0 y i) (y i) true

theorem Ms_len (M0 : List Bool) (y : Nat → Nat) : ∀ i, (Ms M0 y i).length = M0.length
  | 0 => rfl
  | i + 1 => by show (upd (Ms M0 y i) (y i) true).length = _; rw [len_upd, Ms_len M0 y i]

/-- an index not among y 0, …, y (i−1) keeps its initial value -/
theorem Ms_keep (M0 : List Bool) (y : Nat → Nat) (hy : ∀ j, y j < M0.length) (z : Nat) :
    ∀ i, (∀ j, j < i → y j ≠ z) → nthB (Ms M0 y i) z = nthB M0 z
  | 0, _ => rfl
  | i + 1, h => by
    show nthB (upd (Ms M0 y i) (y i) true) z = _
    rw [nth_upd _ _ _ _ (by rw [Ms_len]; exact hy i)]
    have hne : z ≠ y i := fun e => h i (Nat.lt_succ_self i) e.symm
    cases Nat.decEq z (y i) with
    | isTrue e => exact absurd e hne
    | isFalse ne =>
      rw [if_neg ne]
      exact Ms_keep M0 y hy z i (fun j hj => h j (Nat.lt_succ_of_lt hj))

/-- **Each added index is still switched off when it is added.** -/
theorem Ms_off (M0 : List Bool) (y : Nat → Nat) (hy : ∀ j, y j < M0.length) (n : Nat)
    (hoff : ∀ i, i < n → nthB M0 (y i) = false) (hinj : ∀ i j, i < n → j < n → y i = y j → i = j) :
    ∀ i, i < n → nthB (Ms M0 y i) (y i) = false := fun i hi => by
  rw [Ms_keep M0 y hy (y i) i (fun j hj e => Nat.ne_of_lt hj (hinj j i (Nat.lt_trans hj hi) hi e))]
  exact hoff i hi

theorem nodup_snoc : ∀ (l : List Nat) (a : Nat), nodupL l → ¬ memL a l → nodupL (l ++ [a])
  | [], _, _, _ => ⟨fun h => h, trivial⟩
  | b :: l, a, h, ha => by
    refine ⟨fun hm => ?_, nodup_snoc l a h.2 (fun hm => ha (Or.inr hm))⟩
    rcases Cube.memL_append_inv l [a] b hm with h1 | h2
    · exact h.1 h1
    · cases h2 with
      | inl e => exact ha (Or.inl e.symm)
      | inr f => exact f

theorem mem_map_rng (y : Nat → Nat) (n : Nat) (z : Nat) (h : memL z ((ShellSum.rng n).map y)) : ∃ i, i < n ∧ z = y i := by
  obtain ⟨i, hi, e⟩ := HaarFin.memL_map' y z (ShellSum.rng n) h
  exact ⟨i, ShellSum.mem_rng n i hi, e⟩

theorem map_snoc (y : Nat → Nat) : ∀ (l : List Nat) (a : Nat), (l ++ [a]).map y = l.map y ++ [y a]
  | [], _ => rfl
  | b :: l, a => by show y b :: (l ++ [a]).map y = y b :: (l.map y ++ [y a]); rw [map_snoc y l a]

/-- **Distinct indices.** -/
theorem nodup_map_rng (y : Nat → Nat) : ∀ n, (∀ i j, i < n → j < n → y i = y j → i = j) → nodupL ((ShellSum.rng n).map y)
  | 0, _ => trivial
  | n + 1, h => by
    show nodupL ((ShellSum.rng n ++ [n]).map y)
    rw [map_snoc]
    apply nodup_snoc
    · exact nodup_map_rng y n (fun i j hi hj e => h i j (Nat.lt_succ_of_lt hi) (Nat.lt_succ_of_lt hj) e)
    · intro hm
      obtain ⟨i, hi, e⟩ := mem_map_rng y n (y n) hm
      exact Nat.ne_of_lt hi (h i n (Nat.lt_succ_of_lt hi) (Nat.lt_succ_self n) e.symm)

end VolOrder

#print axioms VolOrder.nthL_vec
#print axioms VolOrder.nthP_inj
#print axioms VolOrder.Ms_off
#print axioms VolOrder.nodup_map_rng

ym/FarDisj.lean

/-
  ym/FarDisj — T45, G19d: plaquettes at ℓ∞ distance ≥ 2 share no link, so their incidence vectors are disjoint
  (the hypothesis hpq of CauchyBox). A shared link puts both base points within one step of its base point.
  Lean 4 core only, no axioms.
-/
import ym.LinfDist
import ym.ZdDegree

namespace FarDisj

open YMChain Degree ZdLinks ZdDist ZdDegree Factor

theorem shf_ge : ∀ (x : List Nat) (c i : Nat), nthN x i ≤ nthN (shf x c) i
  | [], _, _ => Nat.le_refl _
  | _ :: _, 0, 0 => Nat.le_succ _
  | _ :: _, 0, _ + 1 => Nat.le_refl _
  | _ :: _, _ + 1, 0 => Nat.le_refl _
  | _ :: x, c + 1, i + 1 => shf_ge x c i

theorem shf_le : ∀ (x : List Nat) (c i : Nat), nthN (shf x c) i ≤ nthN x i + 1
  | [], _, _ => Nat.le_succ _
  | _ :: _, 0, 0 => Nat.le_refl _
  | _ :: _, 0, _ + 1 => Nat.le_succ _
  | _ :: _, _ + 1, 0 => Nat.le_succ _
  | _ :: x, c + 1, i + 1 => shf_le x c i

/-- the base of a link of the plaquette (x, a, b) is x, x + e_a or x + e_b: within one step of x -/
theorem base_near (z x : List Nat) (μ a b : Nat) (h : linkIn (z, μ) (x, a, b) = true) :
    ∀ i, nthN x i ≤ nthN z i ∧ nthN z i ≤ nthN x i + 1 := by
  intro i
  rcases linkIn_cases z μ x a b h with ⟨_, e | e⟩ | ⟨_, e | e⟩
  · rw [e]; exact ⟨Nat.le_refl _, Nat.le_succ _⟩
  · rw [e]; exact ⟨shf_ge x b i, shf_le x b i⟩
  · rw [e]; exact ⟨Nat.le_refl _, Nat.le_succ _⟩
  · rw [e]; exact ⟨shf_ge x a i, shf_le x a i⟩

theorem near_of_shared (l : Lk) (p q : Pq) (hp : linkIn l p = true) (hq : linkIn l q = true) : dcl 1 p.1 q.1 := by
  intro i
  have a := base_near l.1 p.1 l.2 p.2.1 p.2.2 hp i
  have b := base_near l.1 q.1 l.2 q.2.1 q.2.2 hq i
  exact ⟨Nat.le_trans a.1 b.2, Nat.le_trans b.1 a.2⟩

theorem disj_vec : ∀ (LL : List Lk) (p q : Pq), (∀ l, linkIn l p = true → linkIn l q = true → False) →
    disjN (vec LL p) (vec LL q)
  | [], _, _, _ => trivial
  | l :: LL, p, q, h => by
    show (((if linkIn l p = true then 1 else 0) = 0 ∨ (if linkIn l q = true then 1 else 0) = 0) ∧ disjN (vec LL p) (vec LL q))
    refine ⟨?_, disj_vec LL p q h⟩
    cases hp : linkIn l p with
    | false => exact Or.inl rfl
    | true =>
      cases hq : linkIn l q with
      | false => exact Or.inr rfl
      | true => exact False.elim (h l hp hq)

/-- **Far plaquettes are link-disjoint.** -/
theorem far_disj (LL : List Lk) (p q : Pq) (h : 2 ≤ LinfDist.dist p.1 q.1) : disjN (vec LL p) (vec LL q) :=
  disj_vec LL p q (fun l hp hq => by
    have := LinfDist.dist_le 1 p.1 q.1 (near_of_shared l p q hp hq)
    exact Nat.not_succ_le_self 1 (Nat.le_trans h this))

end FarDisj

#print axioms FarDisj.near_of_shared
#print axioms FarDisj.far_disj

ym/CauchyBox.lean

/-
  ym/CauchyBox — T45, G19c: CauchyDist.cauchy_cone8_dist with the volumes and indexing built from VolOrder: plaquettes
  nthP Pn z, volumes Ms M0 y (switch on y 0, y 1, …). Remaining hypotheses: the start mask M0 has the added indices
  switched off, y is injective on [0, n) and in range, the added plaquettes are at distance in [K + 2, N + 2) from p₀
  (link-disjointness from p₀ follows: FarDisj). Lean 4 core only, no axioms.
-/
import ym.CauchyDist
import ym.VolOrder
import ym.FarDisj

namespace CauchyBox

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Cone(2⁻⁸) ⇒ uniform infinite-volume Cauchy estimate along any injective filling order of the far plaquettes.** -/
theorem cauchy_box (d k ad : Nat) (hka : k + ad + 7 ≤ 29) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (cf : List R) (hcf : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: cf))
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (hp0 : p0.1.length = d)
    (n : Nat) (y : Nat → Nat) (M0 : List Bool) (hM0 : M0.length = Pn.length)
    (hyl : ∀ j, y j < Pn.length) (hoff : ∀ i, i < n → nthB M0 (y i) = false)
    (hyinj : ∀ i j, i < n → j < n → y i = y j → i = j)
    (Kk N : Nat) (hfar : ∀ i, i < n → Kk + 2 ≤ LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 ∧
      LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 < N + 2)
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (VolOrder.Ms M0 y i) Ob) (ZxH K O H w (VolOrder.Ms M0 y i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) := by
  intro f0
  have hy : ∀ j, y j < M0.length := fun j => by rw [hM0]; exact hyl j
  have hvalid : ∀ z, memL z ((ShellSum.rng n).map y) → (VolOrder.nthP Pn z).1.length = p0.1.length ∧
      (VolOrder.nthP Pn z).2.1 < (VolOrder.nthP Pn z).2.2 ∧ (VolOrder.nthP Pn z).2.2 < d := fun z hz => by
    obtain ⟨i, _, e⟩ := VolOrder.mem_map_rng y n z hz
    rw [e]
    obtain ⟨v1, v2, v3⟩ := hv _ (VolOrder.nthP_mem Pn (y i) (hyl i))
    exact ⟨by rw [v1, hp0], v2, v3⟩
  have hinj : ∀ z z', memL z ((ShellSum.rng n).map y) → memL z' ((ShellSum.rng n).map y) →
      VolOrder.nthP Pn z = VolOrder.nthP Pn z' → z = z' := fun z z' hz hz' e => by
    obtain ⟨i, _, ei⟩ := VolOrder.mem_map_rng y n z hz
    obtain ⟨j, _, ej⟩ := VolOrder.mem_map_rng y n z' hz'
    rw [ei, ej] at e ⊢
    exact VolOrder.nthP_inj Pn hnd (y i) (y j) (hyl i) (hyl j) e
  exact CauchyDist.cauchy_cone8_dist K O F H d k ad hka hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 cf hcf tr Ob K1
    htd hUp hta hUpa htr hK1 hUpb n y (fun i => VolOrder.nthP Pn (y i)) (VolOrder.Ms M0 y) (fun _ => rfl)
    (fun i _ => by rw [VolOrder.Ms_len, hM0]) (fun i _ => by rw [VolOrder.Ms_len]; exact hy i)
    (VolOrder.Ms_off M0 y hy n hoff hyinj)
    (fun i hi => FarDisj.far_disj LL p0 _ (Nat.le_trans (Nat.le_add_left 2 Kk) (hfar i hi).1)) (fun i _ => VolOrder.nthL_vec LL Pn (y i) (hyl i)) Kk N hfar
    (VolOrder.nthP Pn) (VolOrder.nodup_map_rng y n hyinj) (fun _ _ => rfl) hvalid hinj hr1

end CauchyBox

#print axioms CauchyBox.cauchy_box

ym/WilsonBox8.lean

/-
  ym/WilsonBox8 — T45: the truncated SU(2) Wilson theory itself at β ≤ 2⁻³² (every truncation): the uniform
  infinite-volume Cauchy estimate along any injective filling order of far plaquettes (CauchyBox with the Wilson
  weight in Cone(2⁻⁸), WilsonCone.wilson_cone). d = 4: k = 5, a = 6. Lean 4 core only, no axioms.
-/
import ym.CauchyBox

namespace WilsonBox8

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

theorem wilson_box8 (d k ad : Nat) (hka : k + ad + 7 ≤ 29) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (pwR K h 33)) (Kk' : Nat)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (hp0 : p0.1.length = d)
    (n : Nat) (y : Nat → Nat) (M0 : List Bool) (hM0 : M0.length = Pn.length)
    (hyl : ∀ j, y j < Pn.length) (hoff : ∀ i, i < n → nthB M0 (y i) = false)
    (hyinj : ∀ i j, i < n → j < n → y i = y j → i = j)
    (Kk N : Nat) (hfar : ∀ i, i < n → Kk + 2 ≤ LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 ∧
      LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 < N + 2)
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (WilsonCone.wl K O F t Kk' 11).tail (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (VolOrder.Ms M0 y i) Ob) (ZxH K O H w (VolOrder.Ms M0 y i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) := by
  intro f0
  have w := WilsonCone.wilson_cone K O F h1 h10 h t hh h0 ht0 ht Kk' 11
  have c0 := SU2CharExp.c0_ge_one K O F h1 h10 t ht0 Kk'
  have hd1 : WilsonCone.wl K O F t Kk' 11 = K.one :: (WilsonCone.wl K O F t Kk' 11).tail := by
    show K.mul (SU2CharExp.cS K O F t 0 (Kk' + 1)) (F.inv (SU2CharExp.cS K O F t 0 (Kk' + 1))) :: _ = _
    rw [F.mul_inv _ (MigdalStrong.ne_of_one_le K O h1 h10 _ c0)]; rfl
  rw [hd1] at w
  exact CauchyBox.cauchy_box K O F H d k ad hka hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 _ w tr Ob K1 htd hUp hta hUpa htr
    hK1 hUpb hp0 n y M0 hM0 hyl hoff hyinj Kk N hfar hr1

end WilsonBox8

#print axioms WilsonBox8.wilson_box8

#print axioms: 11 theorems, no axioms.

90.26 Flow from \(\beta \le 4\) into \(\mathrm {Cone}(2^{-8})\), and the Cauchy estimate

Proved: for every \(\beta \in [0,4]\) and truncation \(29+m\) the flow enters \(\mathrm {Cone}(2^{-8})\), so the flowed weight satisfies the uniform infinite-volume Cauchy estimate on \(\mathbb Z^4\).

\[ \forall m\ \forall t\in [0,2]\ \exists k_0\ \forall j:\ \mathrm {Bd}_{2^{-8}}\big (\mathrm {mkIt}^{k_0+j}(\mathrm {wl}(t,29+m,11))\big ),\quad (1-r)|\langle O\rangle _{M_n}-\langle O\rangle _{M_0}|\le \kappa Cs\,r^K \]

ym/TMCertQ.lean

/-
  ym/TMCertQ — T45, G15: the Taylor-model chain checker with a stronger final cone: finalCq q checks
  2^{4qn} W_n ≤ (n+1) W₀, giving Cone(2^{-q}) (q = 4 is TMCert.finalC, Cone(1/16)). Lean 4 core only, no axioms.
-/
import ym.TMCert

namespace TMCertQ

open YMChain SU2Char Signed TMZ TMOpsZ TMChain MigdalCone LinkIntegral Projective ProjMK TMCert

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)
variable (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)

def finalCq (q : Nat) (W0 : TM) : Nat → List TM → Bool
  | _, [] => true
  | n, Wn :: Ws => nnT (addT (scaleN (n + 1) W0) (negT (scaleN (2 ^ (q * (4 * n))) Wn))) && finalCq q W0 (n + 1) Ws

include h1 h10 in
theorem finalCq_sound (q : Nat) (S : Nat) (hS : 1 ≤ S) (u : R) (hu : AbsLe K O K.one u) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (w0 : R) (W0 : TM) (hw : Rep K O S u w0 W0.1 W0.2) :
    ∀ (n : Nat) (ws : List R) (Ws : List TM), RepL K O S u ws Ws → AllNN K O ws → finalCq q W0 n Ws = true →
    BdW K O (pwR K h q) w0 n ws
  | _, [], [], _, _, _ => trivial
  | _, [], _ :: _, hr, _, _ => False.elim hr
  | _, _ :: _, [], hr, _, _ => False.elim hr
  | n, c :: ws, Wn :: Ws, hr, hn, hc => by
    have ⟨c2, c3⟩ := band _ _ hc
    have nn := hn.1
    have d := addT_sound K O S u _ _ _ _ (scaleN_sound K O h1 S u w0 (n + 1) W0 hw)
      (negT_sound K O S u _ _ (scaleN_sound K O h1 S u c (2 ^ (q * (4 * n))) Wn hr.1))
    have dn := cancelS K O h1 h10 S hS _ (nn_sound K O h1 S hS h10 u _ hu _ d c2)
    have hp : O.le K.zero (pwR K (pwR K h q) (4 * n)) := by
      rw [Migdal.pw_pw]; exact AreaLower.pw_nn K O h h0 h1 _
    have m := O.mul_nonneg _ _ dn hp
    have tp : K.mul (cN K (2 ^ (q * (4 * n)))) (pwR K (pwR K h q) (4 * n)) = K.one := by
      rw [Migdal.pw_pw]; exact two_pow K h hh _
    have e : K.mul (K.add (K.mul (cN K (n + 1)) w0) (K.neg (K.mul (cN K (2 ^ (q * (4 * n)))) c))) (pwR K (pwR K h q) (4 * n))
        = K.add (K.mul (K.mul (cN K (n + 1)) (pwR K (pwR K h q) (4 * n))) w0) (K.neg (K.mul (K.mul (cN K (2 ^ (q * (4 * n)))) (pwR K (pwR K h q) (4 * n))) c)) := by lib_ring
    rw [e, tp, K.one_mul] at m
    exact ⟨⟨nn, le_of_nn K O _ _ m⟩, finalCq_sound q S hS u hu h hh h0 w0 W0 hw (n + 1) ws Ws hr.2 hn.2 c3⟩

/-! ### the chain -/

def finalAllQ (q : Nat) : List TM → Bool
  | [] => false
  | W0 :: Ws => posT W0 && finalCq q W0 1 Ws

def chainCQ (q S D L : Nat) : List TM → List TM → Bool
  | Vs, [] => (WmT S D L Vs).1 && (headsC S D L Vs && finalAllQ q (WmT S D L Vs).2)
  | Vs, g :: gs => (WmT S D L Vs).1 && (headsC S D L Vs && (posT g
      && ((mulMap S D g (WmT S D L Vs).2).1 && chainCQ q S D L (mulMap S D g (WmT S D L Vs).2).2 gs)))

include h1 h10 in
/-- **Soundness of the chain checker.** -/
theorem chain_soundQ (q S D L : Nat) (hS : 1 ≤ S) (u : R) (hu : AbsLe K O K.one u) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) : ∀ (gs Vs : List TM) (a : List R) (c : R), c ≠ K.zero →
    RepL K O S u (scaleL K c (K.one :: a)) Vs → chainCQ q S D L Vs gs = true →
    ConeBound.Bd K O (pwR K h q) 0 (K.one :: mkIt K O F L a (gs.length + 1)) := by
  intro gs
  induction gs with
  | nil =>
    intro Vs a c hc hV hch
    have ⟨c1, r1⟩ := band _ _ hch
    have ⟨c2, c3⟩ := band _ _ r1
    have hW := WmT_sound K O F h1 h10 S D L hS u hu _ Vs hV c1
    have ⟨hP, hQ⟩ := heads_transfer K L c _ (heads_sound K O F h1 h10 S D L hS u hu _ Vs hV c2).1
      (heads_sound K O F h1 h10 S D L hS u hu _ Vs hV c2).2
    obtain ⟨κ, hκ⟩ := mk_proj K O F h10 L a hP hQ c hc
    have hm1 := mk_head1 K O F h10 L a hP hQ
    have hNN : AllNN K O (Wm K O F L (scaleL K c (K.one :: a))) := decM_nn K O F h1 h10 0 _
    generalize hWv : Wm K O F L (scaleL K c (K.one :: a)) = W at hW hκ hNN
    generalize hWs : (WmT S D L Vs).2 = Ws at hW c3
    cases W with
    | nil => cases Ws with
      | nil => exact Bool.noConfusion c3
      | cons _ _ => exact False.elim hW
    | cons w0 ws =>
      cases Ws with
      | nil => exact False.elim hW
      | cons W0 Ws' =>
        have ⟨f1, f2⟩ := band _ _ c3
        have ⟨pw0, nw0⟩ := pos_sound K O F h1 h10 S hS u w0 hu W0 hW.1 f1
        have bw := finalCq_sound K O h1 h10 q S hS u hu h hh h0 w0 W0 hW.1 1 ws Ws' hW.2 hNN.2 f2
        have e := hm1.symm.trans hκ
        have kw : K.one = K.mul κ w0 := (List.cons.inj e).1
        have κi : κ = F.inv w0 := SU2CharExp.inv_uniq K O F w0 κ nw0 (by rw [K.mul_comm]; exact kw.symm)
        have κp : O.le K.zero κ := by rw [κi]; exact F.inv_pos _ pw0 nw0
        have b0 : BdW K O (pwR K h q) w0 0 (w0 :: ws) := by
          refine ⟨⟨pw0, ?_⟩, bw⟩
          show O.le w0 (K.mul (K.mul (cN K 1) K.one) w0)
          rw [SU2CharExp.cN_one, K.one_mul, K.one_mul]; exact O.le_refl w0
        have bd := bd_scale K O (pwR K h q) κ w0 κp kw.symm 0 (w0 :: ws) b0
        rw [← hκ, hm1] at bd
        exact bd
  | cons g gs ih =>
    intro Vs a c hc hV hch
    have ⟨c1, r1⟩ := band _ _ hch
    have ⟨c2, r2⟩ := band _ _ r1
    have ⟨c3, r3⟩ := band _ _ r2
    have ⟨c4, c5⟩ := band _ _ r3
    have hW := WmT_sound K O F h1 h10 S D L hS u hu _ Vs hV c1
    have ⟨hP, hQ⟩ := heads_transfer K L c _ (heads_sound K O F h1 h10 S D L hS u hu _ Vs hV c2).1
      (heads_sound K O F h1 h10 S D L hS u hu _ Vs hV c2).2
    obtain ⟨κ, hκ⟩ := mk_proj K O F h10 L a hP hQ c hc
    have hm1 := mk_head1 K O F h10 L a hP hQ
    have ⟨pγ, nγ⟩ := pos_sound K O F h1 h10 S hS u _ hu g (gv_rep K O F h1 h10 S hS u g) c3
    have hV' := mulMap_sound K O h1 h10 S D hS u _ hu g (gv_rep K O F h1 h10 S hS u g) _ _ hW c4
    -- κ ≠ 0 from the head
    have hd : ∃ w0 ws, Wm K O F L (scaleL K c (K.one :: a)) = w0 :: ws := ⟨_, _, rfl⟩
    obtain ⟨w0, ws, hw⟩ := hd
    have e := hm1.symm.trans hκ
    rw [hw] at e
    have kw : K.one = K.mul κ w0 := (List.cons.inj e).1
    have nκ : κ ≠ K.zero := fun z => by rw [z, K.mul_comm, DetChain.mul_zero] at kw; exact h10 kw
    have Wform : Wm K O F L (scaleL K c (K.one :: a)) = scaleL K (F.inv κ) (K.one :: (mk K O F L a).tail) := by
      rw [← hm1, hκ, scale_scaleL, K.mul_comm, F.mul_inv _ nκ, scaleL_one]
    rw [Wform, scale_scaleL] at hV'
    have nc : K.mul (gv K O F S u g) (F.inv κ) ≠ K.zero := mul_ne K O F _ _ nγ (inv_ne K O F h10 _ nκ)
    have r := ih _ (mk K O F L a).tail _ nc hV' c5
    rw [← CertSound.mkIt_shift] at r
    exact r

/-! ### the Wilson start -/

end TMCertQ

#print axioms TMCertQ.finalCq_sound
#print axioms TMCertQ.chain_soundQ

ym/TMCertT8.lean

/-
  ym/TMCertT8 — T45, G15: TMCertT with the stronger final check (TMCertQ, q = 8): the flow is in Cone(2⁻⁸) and stays
  there (MigdalCone.mk_forever). From TMCertT: certificates valid for every truncation at once: the start
  values get their remainder radius enlarged by p = S·ε, where ε bounds the truncation tail
  (cS(Kk+1) ≤ cS(Kk') ≤ cS(Kk+1) + ε, TruncCert). Lean 4 core only, no axioms.
-/
import ym.TMCertT
import ym.TMCertQ

namespace TMCertT8

open YMChain SU2Char Signed TMZ TMOpsZ TMChain MigdalCone LinkIntegral Projective ProjMK TMCert TMCertT TMCertQ

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)
variable (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)

def modelCP8 (S D L Kk T0 H p : Nat) (gs : List TM) : Bool :=
  (startTP S D Kk (L + 1) (tT T0 H) p).1 && (posT (padT (cST S D (tT T0 H) 0 (Kk + 1)).2 p)
    && chainCQ 8 S D L (startTP S D Kk (L + 1) (tT T0 H) p).2 gs)

include h1 h10 in
/-- **One Taylor model certifies an interval of couplings:** if the checker accepts, then for every t with
    |S t − T₀| ≤ H the Migdal flow from the Wilson weight at β = 2t is in Cone(1/16) after #g + 1 steps, forever. -/
theorem cert_modelT8 (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (S D L Kk T0 H : Nat) (hS : 1 ≤ S) (hH : 1 ≤ H)
    (p : Nat) (gs : List TM) (hc : modelCP8 S D L Kk T0 H p gs = true) (t ε : R) (hε : K.mul (cN K S) ε = cN K p) (Kk2 : Nat)
    (hlo : ∀ n, O.le (SU2CharExp.cS K O F t n (Kk + 1)) (SU2CharExp.cS K O F t n (Kk2 + 1)))
    (hup : ∀ n, O.le (SU2CharExp.cS K O F t n (Kk2 + 1)) (K.add (SU2CharExp.cS K O F t n (Kk + 1)) ε))
    (ht1 : O.le (cN K T0) (K.add (K.mul (cN K S) t) (cN K H)))
    (ht2 : O.le (K.mul (cN K S) t) (K.add (cN K T0) (cN K H))) :
    ∀ j, ConeBound.Bd K O (pwR K h 8) 0
      (K.one :: mkIt K O F L (WilsonCone.wl K O F t Kk2 (L + 1)).tail (gs.length + 1 + j)) := by
  have ⟨c1, r1⟩ := band _ _ hc
  have ⟨c2, c3⟩ := band _ _ r1
  -- the variable u = (S t − T₀)/H, |u| ≤ 1
  obtain ⟨s, hs⟩ : ∃ s, H = s + 1 := ⟨H - 1, (Nat.succ_pred_eq_of_pos hH).symm⟩
  have nH : cN K H ≠ K.zero := by rw [hs]; exact cN_succ_ne K O h1 h10 s
  have pH := SU2CharExp.cN_nn K O h1 H
  let u := K.mul (K.add (K.mul (cN K S) t) (K.neg (cN K T0))) (F.inv (cN K H))
  have iH := F.inv_pos _ pH nH
  have hu : AbsLe K O K.one u := by
    have a1 := nn_sub_of_le K O _ _ ht1
    have a2 := nn_sub_of_le K O _ _ ht2
    have m1 := O.mul_nonneg _ _ a1 iH
    have m2 := O.mul_nonneg _ _ a2 iH
    have e1 : K.mul (K.add (K.add (K.mul (cN K S) t) (cN K H)) (K.neg (cN K T0))) (F.inv (cN K H)) = K.add K.one u := by
      show _ = K.add K.one (K.mul (K.add (K.mul (cN K S) t) (K.neg (cN K T0))) (F.inv (cN K H)))
      rw [← F.mul_inv _ nH]; lib_ring
    have e2 : K.mul (K.add (K.add (cN K T0) (cN K H)) (K.neg (K.mul (cN K S) t))) (F.inv (cN K H)) = K.add K.one (K.neg u) := by
      show _ = K.add K.one (K.neg (K.mul (K.add (K.mul (cN K S) t) (K.neg (cN K T0))) (F.inv (cN K H))))
      rw [← F.mul_inv _ nH]; lib_ring
    rw [e1] at m1; rw [e2] at m2
    exact ⟨m1, m2⟩
  have hx : Rep K O S u t (tT T0 H).1 (tT T0 H).2 := by
    show AbsLe K O K.zero (K.add (K.mul (cN K S) t) (K.neg (K.add (K.add (cN K T0) (K.neg K.zero))
      (K.mul u (K.add (K.add (cN K H) (K.neg K.zero)) (K.mul u K.zero))))))
    have e : K.mul u (cN K H) = K.add (K.mul (cN K S) t) (K.neg (cN K T0)) := by
      show K.mul (K.mul (K.add (K.mul (cN K S) t) (K.neg (cN K T0))) (F.inv (cN K H))) (cN K H) = _
      rw [K.mul_assoc, K.mul_comm (F.inv _), F.mul_inv _ nH, K.mul_comm, K.one_mul]
    have z : K.add (K.mul (cN K S) t) (K.neg (K.add (K.add (cN K T0) (K.neg K.zero))
        (K.mul u (K.add (K.add (cN K H) (K.neg K.zero)) (K.mul u K.zero))))) = K.zero := by
      rw [show K.mul u (K.add (K.add (cN K H) (K.neg K.zero)) (K.mul u K.zero)) = K.mul u (cN K H) by lib_ring, e]; lib_ring
    rw [z]
    exact ⟨nn_eq K O _ _ (by lib_ring) (O.le_refl K.zero), nn_eq K O _ _ (by lib_ring) (O.le_refl K.zero)⟩
  -- the start
  have pad : ∀ n, (cST S D (tT T0 H) n (Kk + 1)).1 = true →
      Rep K O S u (SU2CharExp.cS K O F t n (Kk2 + 1)) (padT (cST S D (tT T0 H) n (Kk + 1)).2 p).1
        (padT (cST S D (tT T0 H) n (Kk + 1)).2 p).2 := fun n hn =>
    TMCertT.rep_pad K O S u _ _ _ _ p (cST_sound K O F h1 h10 S D hS u t hu _ hx n (Kk + 1) hn)
      (TMCertT.abs_scaled K O h1 S ε p _ _ hε (hlo n) (hup n))
  have hV := lmapB_sound K O S u (fun n => SU2CharExp.cS K O F t n (Kk2 + 1))
    (fun n => ((cST S D (tT T0 H) n (Kk + 1)).1, padT (cST S D (tT T0 H) n (Kk + 1)).2 p)) (fun n hn => pad n hn) 0 (L + 1) c1
  have c0p := pos_sound K O F h1 h10 S hS u _ hu _ (pad 0 (band _ _ c1).1) c2
  have nc0 := c0p.2
  have form : lmap (fun n => SU2CharExp.cS K O F t n (Kk2 + 1)) 0 (L + 1)
      = scaleL K (SU2CharExp.cS K O F t 0 (Kk2 + 1)) (K.one :: (WilsonCone.wl K O F t Kk2 (L + 1)).tail) := by
    show _ = scaleL K _ (K.one :: lmap (fun n => K.mul (SU2CharExp.cS K O F t n (Kk2 + 1)) (F.inv (SU2CharExp.cS K O F t 0 (Kk2 + 1)))) 1 L)
    show SU2CharExp.cS K O F t 0 (Kk2 + 1) :: lmap _ 1 L = K.mul _ K.one :: scaleL K _ (lmap _ 1 L)
    rw [lmap_scale, K.mul_comm _ K.one, K.one_mul]
    congr 1
    exact lmap_ext _ _ (fun n => by rw [K.mul_comm (SU2CharExp.cS K O F t n (Kk2 + 1)), ← K.mul_assoc, F.mul_inv _ nc0, K.one_mul]) 1 L
  rw [form] at hV
  have cb := chain_soundQ K O F h1 h10 8 S D L hS u hu h hh h0 gs _ _ _ nc0 hV c3
  intro j
  rw [CertWilson.mkIt_add K O F]
  exact mk_forever K O F h1 h10 h hh h0 _ cb L j

end TMCertT8

#print axioms TMCertT8.cert_modelT8

ym/TMCoverT8.lean

/-
  ym/TMCoverT8 — T45, G15: TMCoverT with the Cone(2⁻⁸) final check (TMCertT8). From TMCoverT: TMCover for padded start values: a contiguous list of accepted padded models
  covers its interval for every truncation Kk' within the tail bound ε (S·ε = p). Lean 4 core only, no axioms.
-/
import ym.TMCoverT
import ym.TMCertT8

namespace TMCoverT8

open YMChain SU2Char TMZ TMOpsZ TMChain TMCert TMCertT TMCertQ TMCertT8 TMCover TMCoverT MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

def allCP8 (S D L Kk p : Nat) : List Model → Bool
  | [] => true
  | m :: ms => modelCP8 S D L Kk m.1 m.2.1 p m.2.2 && (Nat.ble 1 m.2.1 && allCP8 S D L Kk p ms)

theorem allCP8_app (S D L Kk p : Nat) : ∀ x y : List Model, allCP8 S D L Kk p (x ++ y) = (allCP8 S D L Kk p x && allCP8 S D L Kk p y)
  | [], _ => rfl
  | m :: x, y => by
    show (modelCP8 S D L Kk m.1 m.2.1 p m.2.2 && (Nat.ble 1 m.2.1 && allCP8 S D L Kk p (x ++ y))) = _
    rw [allCP8_app S D L Kk p x y]
    show _ = ((modelCP8 S D L Kk m.1 m.2.1 p m.2.2 && (Nat.ble 1 m.2.1 && allCP8 S D L Kk p x)) && allCP8 S D L Kk p y)
    rw [band_assoc, band_assoc]

theorem chainCQ_step (S D L : Nat) (Vs Vs' : List TM) (g : TM) (gs : List TM) (h : stepOK S D L Vs g Vs' = true)
    (h' : chainCQ 8 S D L Vs' gs = true) : chainCQ 8 S D L Vs (g :: gs) = true := by
  have ⟨c1, r1⟩ := band _ _ h
  have ⟨c2, r2⟩ := band _ _ r1
  have ⟨c3, r3⟩ := band _ _ r2
  have ⟨c4, c5⟩ := band _ _ r3
  have e : (mulMap S D g (WmT S D L Vs).2).2 = Vs' := of_decide_eq_true c5
  show ((WmT S D L Vs).1 && (headsC S D L Vs && (posT g && ((mulMap S D g (WmT S D L Vs).2).1
    && chainCQ 8 S D L (mulMap S D g (WmT S D L Vs).2).2 gs)))) = true
  rw [c1, c2, c3, c4, e, h']; rfl

theorem modelCP8_of (S D L Kk T0 H p : Nat) (V0 gs : List TM) (h : startOKP S D L Kk T0 H p V0 = true)
    (h' : chainCQ 8 S D L V0 gs = true) : modelCP8 S D L Kk T0 H p gs = true := by
  have ⟨c1, r1⟩ := band _ _ h
  have ⟨c2, c3⟩ := band _ _ r1
  have e : (startTP S D Kk (L + 1) (tT T0 H) p).2 = V0 := of_decide_eq_true c3
  show ((startTP S D Kk (L + 1) (tT T0 H) p).1 && (posT (padT (cST S D (tT T0 H) 0 (Kk + 1)).2 p)
    && chainCQ 8 S D L (startTP S D Kk (L + 1) (tT T0 H) p).2 gs)) = true
  rw [c1, c2, e, h']; rfl

theorem allCP8_one (S D L Kk p : Nat) (m : Model) (h : modelCP8 S D L Kk m.1 m.2.1 p m.2.2 = true)
    (hH : Nat.ble 1 m.2.1 = true) : allCP8 S D L Kk p [m] = true := by
  show (modelCP8 S D L Kk m.1 m.2.1 p m.2.2 && (Nat.ble 1 m.2.1 && true)) = true
  rw [h, hH]; rfl

variable (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)

include h1 h10 in
/-- **A contiguous list of accepted padded models covers its interval, for every truncation within the tail.** -/
theorem cover_listT8 (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (S D L Kk p : Nat) (hS : 1 ≤ S) (t ε : R)
    (hε : K.mul (cN K S) ε = cN K p) (Kk2 : Nat)
    (hlo : ∀ n, O.le (SU2CharExp.cS K O F t n (Kk + 1)) (SU2CharExp.cS K O F t n (Kk2 + 1)))
    (hup : ∀ n, O.le (SU2CharExp.cS K O F t n (Kk2 + 1)) (K.add (SU2CharExp.cS K O F t n (Kk + 1)) ε)) :
    ∀ (ms : List Model) (m : Model), allCP8 S D L Kk p (m :: ms) = true → contig (m :: ms) = true →
    O.le (cN K m.1) (K.add (K.mul (cN K S) t) (cN K m.2.1)) → O.le (K.mul (cN K S) t) (cN K (lastHi m ms)) →
    ∃ k, ∀ j, ConeBound.Bd K O (pwR K h 8) 0 (K.one :: mkIt K O F L (WilsonCone.wl K O F t Kk2 (L + 1)).tail (k + j)) := by
  intro ms
  induction ms with
  | nil =>
    intro m hc _ hlo' hhi
    have ⟨c1, r1⟩ := band _ _ hc
    have ⟨c2, _⟩ := band _ _ r1
    have hhi' : O.le (K.mul (cN K S) t) (K.add (cN K m.1) (cN K m.2.1)) := by rw [← cN_add]; exact hhi
    exact ⟨_, cert_modelT8 K O F h1 h10 h hh h0 S D L Kk m.1 m.2.1 hS (Nat.le_of_ble_eq_true c2) p m.2.2 c1 t ε hε Kk2
      hlo hup hlo' hhi'⟩
  | cons m' ms ih =>
    intro m hc hg hlo' hhi
    have ⟨c1, r1⟩ := band _ _ hc
    have ⟨c2, c3⟩ := band _ _ r1
    have ⟨g1, g2⟩ := band _ _ hg
    have eq : m.1 + m.2.1 + m'.2.1 = m'.1 := Nat.eq_of_beq_eq_true g1
    cases O.le_total (K.mul (cN K S) t) (K.add (cN K m.1) (cN K m.2.1)) with
    | inl hle => exact ⟨_, cert_modelT8 K O F h1 h10 h hh h0 S D L Kk m.1 m.2.1 hS (Nat.le_of_ble_eq_true c2) p m.2.2 c1 t ε
        hε Kk2 hlo hup hlo' hle⟩
    | inr hge =>
      apply ih m' c3 g2 _ hhi
      rw [← eq, cN_add, cN_add]
      have := O.add_le_add _ _ _ _ hge (O.le_refl (cN K m'.2.1))
      exact this

end TMCoverT8

#print axioms TMCoverT8.cover_listT8
#print axioms TMCoverT8.modelCP8_of
#print axioms TMCoverT8.chainCQ_step

ym/CertSoundQ.lean

/-
  ym/CertSoundQ — T45, G15: the interval-certificate chain with the stronger final cone check u_m 2^{4qm} ≤ (m+1)D,
  giving Cone(2^{-q}) (q = 4 is CertSound.checkCone). Lean 4 core only, no axioms.
-/
import ym.CertSound

namespace CertSoundQ

open YMChain LinkIntegral SU2Haar SU2Char CertSound

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- the cone check: u_m · 2^{16m} ≤ (m+1) · D -/
def checkConeQ (q D : Nat) : Nat → List Nat → Bool
  | _, [] => true
  | m, u :: us => Nat.ble (u * 2 ^ (q * (4 * m))) ((m + 1) * D) && checkConeQ q D (m + 1) us

/-- **Upper brackets passing the cone check put the list in Cone(1/16).** -/
theorem cone_of_checkQ (q : Nat) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (D : Nat) (hD : 1 ≤ D) :
    ∀ (m : Nat) (x : List R) (l u : List Nat), Brk K O (cN K D) x l u → checkConeQ q D m u = true →
      ConeBound.Bd K O (pwR K h q) m x
  | _, [], [], [], _, _ => trivial
  | m, y :: x, l :: ls, u :: us, hb, hc => by
    refine ⟨⟨hb.1.1, ?_⟩, cone_of_checkQ q h1 h10 h hh h0 D hD (m + 1) x ls us hb.2 (and_true_r hc)⟩
    have c := Nat.le_of_ble_eq_true (and_true_l hc)
    have Dp := SU2CharExp.pos_of_ge_one K O h1 h10 D hD
    have two_eq : cN K 2 = SU2Strong4D.two K := by
      show K.add K.one (K.add K.one K.zero) = K.add K.one K.one; rw [DetChain.add_zero']
    have g := SU2CharExp.cN_mono K O h1 _ _ c
    rw [SU2CharExp.cN_mul, SU2CharExp.cN_mul, cN_pow, two_eq] at g
    -- y D 2^{16m} ≤ u 2^{16m} ≤ (m+1) D
    have pw0 : O.le K.zero (pwR K (SU2Strong4D.two K) (q * (4 * m))) :=
      AreaLower.pw_nn K O _ (by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this) h1 _
    have a1 := O.mul_le_mul_right _ _ _ hb.1.2.2 pw0
    have a2 := O.le_trans _ _ _ a1 g
    have e : K.mul (K.mul y (cN K D)) (pwR K (SU2Strong4D.two K) (q * (4 * m)))
        = K.mul (K.mul y (pwR K (SU2Strong4D.two K) (q * (4 * m)))) (cN K D) := by lib_ring
    rw [e] at a2
    have a3 := O.pos_cancel _ _ _ Dp.1 Dp.2 a2
    -- multiply by h^{16m}: y ≤ (m+1) h^{16m}
    have hp := AreaLower.pw_nn K O h h0 h1 (q * (4 * m))
    have a4 := O.mul_le_mul_right _ _ _ a3 hp
    have th : K.mul (SU2Strong4D.two K) h = K.one := by
      show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
    have e2 : K.mul (K.mul y (pwR K (SU2Strong4D.two K) (q * (4 * m)))) (pwR K h (q * (4 * m))) = y := by
      rw [K.mul_assoc, ← GKS2.pwR_mul, th, SU2Strong4D.pw_one, K.mul_comm, K.one_mul]
    rw [e2] at a4
    rw [Migdal.pw_pw]
    exact a4
  | _, [], [], _ :: _, h, _ => h.elim
  | _, [], _ :: _, _, h, _ => h.elim
  | _, _ :: _, [], _, h, _ => h.elim
  | _, _ :: _, _ :: _, [], h, _ => h.elim

variable (F : OField.OF K O)

def checkChainTQ (q D L : Nat) : List Nat → List Nat → List StepT → Bool
  | _, ua, [] => checkConeQ q D 1 ua
  | la, ua, s :: ss => checkStepT D L la ua s && checkChainTQ q D L s.lo s.uo ss

theorem chain_soundTQ (q : Nat) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (D L : Nat) (hD : 1 ≤ D) :
    ∀ (ss : List StepT) (a : List R) (la ua : List Nat), Brk K O (cN K D) a la ua →
      checkChainTQ q D L la ua ss = true → ConeBound.Bd K O (pwR K h q) 0 (K.one :: MigdalCone.mkIt K O F L a ss.length)
  | [], a, la, ua, hb, hc => by
    refine ⟨⟨h1, ?_⟩, cone_of_checkQ K O q h1 h10 h hh h0 D hD 1 a la ua hb hc⟩
    show O.le K.one (K.mul (cN K 1) K.one)
    rw [SU2CharExp.cN_one, K.one_mul]; exact O.le_refl _
  | s :: ss, a, la, ua, hb, hc => by
    obtain ⟨t, et, bt⟩ := step_bracketT K O F h1 h10 D L hD a la ua hb s (and_true_l hc)
    have ih := chain_soundTQ q h1 h10 h hh h0 D L hD ss t s.lo s.uo bt (and_true_r hc)
    show ConeBound.Bd K O (pwR K h q) 0 (K.one :: MigdalCone.mkIt K O F L a (ss.length + 1))
    rw [mkIt_shift, et]
    exact ih

end CertSoundQ

#print axioms CertSoundQ.cone_of_checkQ
#print axioms CertSoundQ.chain_soundTQ

ym/CertWilsonT8.lean

/-
  ym/CertWilsonT8 — T45, G15: CertWilsonT.certified_intervalT with the Cone(2⁻⁸) final check (CertSoundQ, q = 8):
  the flow is in Cone(2⁻⁸) and stays there (MigdalCone.mk_forever). Lean 4 core only, no axioms.
-/
import ym.CertWilsonT
import ym.CertSoundQ

namespace CertWilsonT8

open YMChain LinkIntegral SU2Haar SU2Char SU2CharExp CertSound CertWilson CertWilsonT

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **Certified for every t in an interval and every truncation Kk' within the tail bound.** -/
theorem certified_intervalT8 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (t t1 t2 ε : R) (ht10 : O.le K.zero t1) (h1t : O.le t1 t) (ht2 : O.le t t2)
    (a1 b1 a2 b2 Kk D Tn ML : Nat) (hb1 : 1 ≤ b1) (hb2 : 1 ≤ b2) (hD : 1 ≤ D) (hTn : 1 ≤ Tn)
    (e1 : K.mul t1 (cN K b1) = cN K a1) (e2 : K.mul t2 (cN K b2) = cN K a2) (hε : K.mul ε (cN K Tn) = K.one)
    (la ua : List Nat) (ss : List StepT)
    (hQn : Nat.ble 1 (Wn a1 b1 0 (Kk + 1)) = true) (hS : checkStartIT a1 b1 a2 b2 Kk D Tn 1 la ua = true)
    (hC : CertSoundQ.checkChainTQ 8 D ML la ua ss = true) (Kk2 : Nat)
    (hlo : ∀ n, O.le (cS K O F t n (Kk + 1)) (cS K O F t n (Kk2 + 1)))
    (hup : ∀ n, O.le (cS K O F t n (Kk2 + 1)) (K.add (cS K O F t n (Kk + 1)) ε)) :
    ∀ j, ConeBound.Bd K O (pwR K h 8) 0
      (K.one :: MigdalCone.mkIt K O F ML (WilsonCone.wl K O F t Kk2 (la.length + 1)).tail (ss.length + j)) := by
  have sb := CertWilsonT.start_bracketIT K O F h1 h10 t t1 t2 ε ht10 h1t ht2 a1 b1 a2 b2 Kk D Tn (Kk2 + 1) hb1 hb2 hTn e1 e2
    (Nat.le_of_ble_eq_true hQn) hε hlo hup 1 la ua hS
  have hd : (WilsonCone.wl K O F t Kk2 (la.length + 1)).tail
      = MigdalCone.lmap (fun n => K.mul (cS K O F t n (Kk2 + 1)) (F.inv (cS K O F t 0 (Kk2 + 1)))) 1 la.length := rfl
  have cs := CertSoundQ.chain_soundTQ K O F 8 h1 h10 h hh h0 D ML hD ss _ la ua sb hC
  intro j
  rw [mkIt_add, hd]
  exact MigdalCone.mk_forever K O F h1 h10 h hh h0 _ cs ML j

end CertWilsonT8

#print axioms CertWilsonT8.certified_intervalT8

ym/Cover04T8.lean

/-
  ym/Cover04T8 — T45, G2c: every β ∈ [0, 4] and every truncation Kk' = 29 + m: the 4D Migdal flow (L = 10) from the
  Wilson weight enters Cone(2⁻⁸) and stays there forever, uniformly in m (CertCover29T8.cover01T8 for t ≤ 1/2,
  TMCoverPA8.cover_mid for 1/2 ≤ t ≤ 2; tails TruncCert.tail_hyp). Lean 4 core only, no axioms.
-/
import ym.CertCover29T8
import ym.TMCoverPA8
import ym.CoverBridge


set_option exponentiation.threshold 4096
set_option maxRecDepth 100000

namespace Cover04T8

open YMChain SU2Char CertCover MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem pad_eq (h : R) (hh : K.add h h = K.one) (A B : Nat) :
    K.mul (cN K (2 ^ (A + B))) (pwR K h B) = cN K (2 ^ A) := by
  rw [GapInputs.cN_pow2, GapInputs.cN_pow2, GKS.pwR_add, K.mul_assoc, GapInputs.pow2_h K h hh B, K.mul_comm, K.one_mul]

/-- **Every β ∈ [0, 4], uniformly in the truncation 29 + m.** -/
theorem cover04T8 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 2)) (m : Nat) :
    ∃ k, ∀ j, ConeBound.Bd K O (pwR K h 8) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + m) 11).tail (k + j)) := by
  have ht4 : O.le t (cN K 4) := O.le_trans _ _ _ ht (SU2CharExp.cN_mono K O h1 2 4 (by decide))
  cases O.le_total t (rat K O F 1 2) with
  | inl hle =>
    obtain ⟨k, _, hk⟩ := CertCover29T8.cover01T8 K O F h1 h10 h hh h0 t ht0 hle ht4 m
    exact ⟨k, hk⟩
  | inr hge =>
    have tl := fun n => TruncCert.tail_hyp K O F h1 h10 h hh h0 t ht0 ht4 m n
    exact TMCoverPA8.cover_mid K O F h1 h10 h hh h0 t
      (CoverBridge.half_le K O F h1 h10 (2 ^ 599) t hge)
      (CoverBridge.le_two K O h1 (2 ^ 600) t ht)
      (pwR K h 84) (pad_eq K h hh 516 84) (29 + m) (fun n => (tl n).1) (fun n => (tl n).2)

end Cover04T8

#print axioms Cover04T8.cover04T8

ym/GapBoxFlow04.lean

/-
  ym/GapBoxFlow04 — T45: every β ∈ [0, 4] and every truncation 29 + m: from some Migdal step k₀ on, the flowed
  SU(2) plaquette weight lies in Cone(2⁻⁸) (Cover04T8) and satisfies the uniform infinite-volume Cauchy estimate along
  any injective filling order of far plaquettes (CauchyBox; d = 4: k = 5, a = 6). Lean 4 core only, no axioms.
-/
import ym.CauchyBox
import ym.Cover04T8

namespace GapBoxFlow04

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

theorem box_flow04 (d k ad : Nat) (hka : k + ad + 7 ≤ 29) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 2)) (mm : Nat) :
    ∃ k0, ∀ jj, ∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (hp0 : p0.1.length = d)
    (n : Nat) (y : Nat → Nat) (M0 : List Bool) (hM0 : M0.length = Pn.length)
    (hyl : ∀ j, y j < Pn.length) (hoff : ∀ i, i < n → nthB M0 (y i) = false)
    (hyinj : ∀ i j, i < n → j < n → y i = y j → i = j)
    (Kk N : Nat) (hfar : ∀ i, i < n → Kk + 2 ≤ LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 ∧
      LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 < N + 2)
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (VolOrder.Ms M0 y i) Ob) (ZxH K O H w (VolOrder.Ms M0 y i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) := by
  obtain ⟨k0, hk0⟩ := Cover04T8.cover04T8 K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, fun jj LL hLL hL Pn hnd hv p0 => CauchyBox.cauchy_box K O F H d k ad hka hd8 hd16 DH h1 h10 h hh h0
    LL hLL hL Pn hnd hv p0 _ (hk0 jj)⟩

end GapBoxFlow04

#print axioms GapBoxFlow04.box_flow04

#print axioms: 10 theorems, no axioms.

90.27 Clustering for the Cauchy weight

Proved: if the character coefficients lie in \(\mathrm {Cone}(2^{-8})\), the weight of the Cauchy estimate has exponential clustering in every finite volume.

\[ 2|\langle U_pU_q\rangle Z-\langle U_p\rangle \langle U_q\rangle |\le B\,2^{-R}Z^2,\qquad \mathrm {dist}(p,q)>R \]

ym/GapCone8.lean

/-
  ym/GapCone8 — T45, G19: the exponential clustering bound for the SAME weight as the Cauchy estimate
  CauchyCone8.cauchy_of_cone8: plaquette weight s + s·Σ cf_n χ_n(tr) with s = 1/(1 + 2^{-(k+a+7)}), cf in Cone(2⁻⁸).
  Lean 4 core only, no axioms.
-/
import ym.CauchyCone8
import ym.GapConeK

namespace GapCone8

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Cone(2⁻⁸) ⇒ exponential clustering in every finite volume**, for the weight of `cauchy_of_cone8`. -/
theorem gap_of_cone8 (d k ad : Nat) (hka : k + ad + 7 ≤ 29) (hd8 : 4 * (2 * d) ≤ 2 ^ (k + ad))
    (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (cf : List R) (hcf : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: cf))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + ad + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro f0
  have hl := GapInputs.hle_one K O h1 h hh h0
  have hx : K.mul (MigdalCone.four K) (pwR K (pwR K h 8) 4) = pwR K h 30 := by
    rw [Migdal.pw_pw, show 8 * 4 = 30 + 2 from rfl, GKS.pwR_add]
    show K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.mul (pwR K h 30) (K.mul (K.mul K.one h) h)) = _
    have e : K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.mul (pwR K h 30) (K.mul (K.mul K.one h) h))
        = K.mul (pwR K h 30) (K.mul (K.add h h) (K.add h h)) := by lib_ring
    rw [e, hh, K.one_mul, K.mul_comm, K.one_mul]
  have x2 : K.add (pwR K h 30) (pwR K h 30) = pwR K h 29 := GapInputsK.dbl K h hh 29
  have hxx : O.le (K.add (pwR K h 30) (pwR K h 30)) K.one := by
    rw [x2]; exact ConeBound.pw_anti K O h1 h h0 hl 29 0 (Nat.zero_le _)
  have epsle : O.le (pwR K h 29) (pwR K h (k + ad + 7)) := ConeBound.pw_anti K O h1 h h0 hl _ _ hka
  have hf0 : ∀ i x, O.le (K.neg (pwR K h (k + ad + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + ad + 7)) := fun i x => by
    have := ConeGap.activity K O h1 (pwR K h 8) (pwR K h 30) (AreaLower.pw_nn K O h h0 h1 _) hx hxx 1
      _ hcf.2 (ChiBound.c K (tr i x)) (ChiBound.chi_bound K O h1 (tr i x) (htr i x))
    rw [show pwR K (pwR K h 30) 1 = K.mul K.one (pwR K h 30) from rfl, K.one_mul, x2] at this
    exact ⟨O.le_trans _ _ _ (ChiBound.neg_le K O _ _ epsle) this.1, O.le_trans _ _ _ this.2 epsle⟩
  have hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i) :=
    fun i c c' hcc => by show SU2Char.dot K 1 _ (ChiBound.c K (tr i c)) = _; rw [htd i c c' hcc]
  exact GapConeK.gap_cone_k K O F H d (k + ad) hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq f0 Up Uq K1 Rr
    hfd hUp hUq (fun i => GapFlow.adm_dot K O H (tr i) (hta i) 1 _) hUpa hUqa hf0 hK1 hUpb hUqb hdist M hM

end GapCone8

#print axioms GapCone8.gap_of_cone8

#print axioms: 1 theorem, no axioms.

90.28 One weight with the Cauchy estimate and clustering on \(\mathbb Z^4\)

Proved: on \(\mathbb Z^4\), coefficients in \(\mathrm {Cone}(2^{-8})\) give one plaquette weight with both the uniform Cauchy estimate and exponential clustering.

\[ \mathrm {Bd}_{2^{-8}}(cf)\ \Rightarrow \ |\langle O\rangle _{M_n}-\langle O\rangle _{M_0}|\le T\ \wedge \ 2|\langle U_pU_q\rangle Z-\langle U_p\rangle \langle U_q\rangle |\le B2^{-R}Z^2 \]

ym/GapBothGen.lean

/-
  ym/GapBothGen — T45, G20 (hypothesis form): every β ∈ [0, 4] and every truncation 29 + m: from one Migdal step k₀ on, the SAME flowed
  SU(2) plaquette weight has both the uniform infinite-volume Cauchy estimate (CauchyCone8) and exponential clustering
  in every finite volume (GapCone8). Lean 4 core only, no axioms.
-/
import ym.GapCone8

namespace GapBothGen

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- Cone(2⁻⁸) coefficients ⇒ Cauchy estimate and clustering for one weight. -/
theorem both_of_cone8 (d k ad : Nat) (hka : k + ad + 7 ≤ 29) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd8' : 4 * (2 * d) ≤ 2 ^ (k + ad))
    (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (cf : List R) (hcf : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: cf)) :
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hq : ∀ i, i < n → nthL (Pn.map (vec LL)) (y i) = vec LL (q i))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (plaq : Nat → Pq) (hndy : nodupL ((ShellSum.rng n).map y))
    (hshell : ∀ kk z, memL z ((ShellSum.rng n).map y) → sh z = kk → (plaq z).1.length = p0.1.length ∧
      dcl (kk + 2) p0.1 (plaq z).1 ∧ (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < d)
    (hinj : ∀ z z', memL z ((ShellSum.rng n).map y) → memL z' ((ShellSum.rng n).map y) → plaq z = plaq z' → z = z')
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + ad + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) := by
  refine ⟨fun LL hLL hL Pn hnd hv p0 => CauchyCone8.cauchy_of_cone8 K O F H d k ad hka hd8 hd16 DH h1 h10 h hh h0
    LL hLL hL Pn hnd hv p0 _ hcf, ?_⟩
  intro LL hLL hL Pn hnd hv p0 q0 hpq
  exact GapCone8.gap_of_cone8 K O F H d k ad hka hd8' hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq cf hcf

end GapBothGen

#print axioms GapBothGen.both_of_cone8

ym/GapBothD4.lean

/-
  ym/GapBothD4 — T45, G21 (d = 4, k = 5, a = 6 fixed; numeric side conditions discharged): every β ∈ [0, 4] and every truncation 29 + m: from one Migdal step k₀ on, the SAME flowed
  SU(2) plaquette weight has both the uniform infinite-volume Cauchy estimate (CauchyCone8) and exponential clustering
  in every finite volume (GapCone8). Lean 4 core only, no axioms.
-/
import ym.GapCone8
import ym.GapBothGen

namespace GapBothD4

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Z⁴, decay 2⁻⁷**: Cone(2⁻⁸) coefficients ⇒ Cauchy estimate and clustering for one weight. -/
theorem both_d4 (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (cf : List R) (hcf : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: cf)) :
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hq : ∀ i, i < n → nthL (Pn.map (vec LL)) (y i) = vec LL (q i))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (plaq : Nat → Pq) (hndy : nodupL ((ShellSum.rng n).map y))
    (hshell : ∀ kk z, memL z ((ShellSum.rng n).map y) → sh z = kk → (plaq z).1.length = p0.1.length ∧
      dcl (kk + 2) p0.1 (plaq z).1 ∧ (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < 4)
    (hinj : ∀ z z', memL z ((ShellSum.rng n).map y) → memL z' ((ShellSum.rng n).map y) → plaq z = plaq z' → z = z')
    (hr1 : O.le (K.mul (pwR K h (6 + 1)) (natR K (3 ^ p0.1.length))) K.one),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * 4) + 4 * (2 * 4))))
      (pwR K (K.add K.one (pwR K h (5 + 1))) (4 * (2 * 4) + 4 * (2 * 4))))
    let s := 4 * 4 * 5 ^ p0.1.length
    let r := K.mul (pwR K h (6 + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (5 + 6 + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) := by
  exact GapBothGen.both_of_cone8 K O F H 4 5 6 (by decide) (by decide) (by decide) (by decide) DH h1 h10 h hh h0 cf hcf

end GapBothD4

#print axioms GapBothD4.both_d4

#print axioms: 2 theorems, no axioms.

90.29 Box order on \(\mathbb Z^4\) without numeric side conditions

Proved: on \(\mathbb Z^4\) with box filling order, \(\mathrm {Cone}(2^{-8})\) coefficients give the Cauchy estimate and clustering, with \(2^{-7}\cdot 81\le 1\) proved.

\[ \mathrm {Bd}_{2^{-8}}(cf)\ \Rightarrow \ \text {Cauchy}_{\mathrm {box}}\wedge \text {Clustering},\qquad 2^{-7}\cdot 3^4\le 1 \]

ym/R7.lean

/-
  ym/R7 — T45: on Z⁴ the Cauchy decay beats the shell growth: 2⁻⁷ · 3⁴ ≤ 1 (81 ≤ 128). Lean 4 core only, no axioms.
-/
import ym.Geom
import ym.ConeBound

namespace R7

open YMChain LinkIntegral Indep

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem two_eq : natR K 2 = K.add K.one K.one := by
  show K.add (K.add K.zero K.one) K.one = _
  rw [K.zero_add]

/-- `h · 2 = 1` ⇒ `h^7 · 3^4 ≤ 1`. -/
theorem r7_le (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) :
    O.le (K.mul (pwR K h 7) (natR K (3 ^ 4))) K.one := by
  have m : O.le (natR K (3 ^ 4)) (natR K (2 ^ 7)) := Geom.natR_mono K O h1 _ _ (by decide)
  have a := O.mul_le_mul_right _ _ (pwR K h 7) m (AreaLower.pw_nn K O h h0 h1 7)
  rw [K.mul_comm (natR K (3 ^ 4)), K.mul_comm (natR K (2 ^ 7))] at a
  have e : K.mul (pwR K h 7) (natR K (2 ^ 7)) = K.one := by
    rw [Geom.natR_pow, two_eq, ← GKS2.pwR_mul]
    have : K.mul h (K.add K.one K.one) = K.one := by
      rw [K.distrib, K.mul_comm h K.one, K.one_mul, hh]
    rw [this]; exact LinkIntegral.pw_one' K 7
  rw [e] at a; exact a

end R7

#print axioms R7.r7_le

ym/GapBothBoxD4.lean

/-
  ym/GapBothBoxD4 — T45, G22: Z⁴, box filling order (CauchyBox) — all numeric conditions, including the decay 2⁻⁷·81 ≤ 1, discharged: every β ∈ [0, 4] and every truncation 29 + m: from one Migdal step k₀ on, the SAME flowed
  SU(2) plaquette weight has both the uniform infinite-volume Cauchy estimate (CauchyCone8) and exponential clustering
  in every finite volume (GapCone8). Lean 4 core only, no axioms.
-/
import ym.GapBothGen
import ym.CauchyBox
import ym.R7

namespace GapBothBoxD4

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Z⁴**: Cone(2⁻⁸) coefficients ⇒ one weight with the box-order infinite-volume Cauchy estimate and clustering. -/
theorem both_box_d4 (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (cf : List R) (hcf : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: cf)) :
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (hp0 : p0.1.length = 4)
    (n : Nat) (y : Nat → Nat) (M0 : List Bool) (hM0 : M0.length = Pn.length)
    (hyl : ∀ j, y j < Pn.length) (hoff : ∀ i, i < n → nthB M0 (y i) = false)
    (hyinj : ∀ i j, i < n → j < n → y i = y j → i = j)
    (Kk N : Nat) (hfar : ∀ i, i < n → Kk + 2 ≤ LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 ∧
      LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 < N + 2),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (VolOrder.Ms M0 y i) Ob) (ZxH K O H w (VolOrder.Ms M0 y i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * 4) + 4 * (2 * 4))))
      (pwR K (K.add K.one (pwR K h (5 + 1))) (4 * (2 * 4) + 4 * (2 * 4))))
    let s := 4 * 4 * 5 ^ p0.1.length
    let r := K.mul (pwR K h (6 + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (5 + 6 + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) := by
  refine ⟨fun LL hLL hL Pn hnd hv p0 tr Ob K1 htd hUp hta hUpa htr hK1 hUpb hp0 n y M0 hM0 hyl hoff hyinj Kk N hfar => ?_, (GapBothGen.both_of_cone8 K O F H 4 5 6 (by decide) (by decide) (by decide) (by decide) DH h1 h10 h hh h0 cf hcf).2⟩
  have hr1 : O.le (K.mul (pwR K h (6 + 1)) (natR K (3 ^ p0.1.length))) K.one := by
    rw [hp0]; exact R7.r7_le K O h1 h hh h0
  exact CauchyBox.cauchy_box K O F H 4 5 6 (by decide) (by decide) (by decide) DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 cf hcf
    tr Ob K1 htd hUp hta hUpa htr hK1 hUpb hp0 n y M0 hM0 hyl hoff hyinj Kk N hfar hr1

end GapBothBoxD4

#print axioms GapBothBoxD4.both_box_d4

#print axioms: 2 theorems, no axioms.

90.30 \(\beta \le 4\): one flowed weight with the Cauchy estimate and clustering

Proved: for every \(\beta \in [0,4]\) and truncation \(29+m\), from one Migdal step on, the flowed weight on \(\mathbb Z^4\) has both the Cauchy estimate and clustering.

\[ \forall m\ \forall t\in [0,2]\ \exists k_0\ \forall j:\ \text {Cauchy}_{\mathrm {box}}\big (w_{k_0+j}\big )\wedge \text {Clustering}\big (w_{k_0+j}\big ) \]

ym/GapBothFlow04.lean

/-
  ym/GapBothFlow04 — T45, G20: every β ∈ [0, 4] and every truncation 29 + m: from one Migdal step k₀ on, the SAME flowed
  SU(2) plaquette weight has both the uniform infinite-volume Cauchy estimate (CauchyCone8) and exponential clustering
  in every finite volume (GapCone8). Lean 4 core only, no axioms.
-/
import ym.GapBothGen
import ym.Cover04T8

namespace GapBothFlow04

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **β ∈ [0,4], every truncation: one flowed weight with the Cauchy estimate and clustering.** -/
theorem both_flow04 (d k ad : Nat) (hka : k + ad + 7 ≤ 29) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd8' : 4 * (2 * d) ≤ 2 ^ (k + ad))
    (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 2)) (mm : Nat) :
    ∃ k0, ∀ jj,
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hq : ∀ i, i < n → nthL (Pn.map (vec LL)) (y i) = vec LL (q i))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (plaq : Nat → Pq) (hndy : nodupL ((ShellSum.rng n).map y))
    (hshell : ∀ kk z, memL z ((ShellSum.rng n).map y) → sh z = kk → (plaq z).1.length = p0.1.length ∧
      dcl (kk + 2) p0.1 (plaq z).1 ∧ (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < d)
    (hinj : ∀ z z', memL z ((ShellSum.rng n).map y) → memL z' ((ShellSum.rng n).map y) → plaq z = plaq z' → z = z')
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + ad + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) := by
  obtain ⟨k0, hk0⟩ := Cover04T8.cover04T8 K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, fun jj => GapBothGen.both_of_cone8 K O F H d k ad hka hd8 hd8' hd16 DH h1 h10 h hh h0 _ (hk0 jj)⟩

end GapBothFlow04

#print axioms GapBothFlow04.both_flow04

ym/GapBothBoxFlow04.lean

/-
  ym/GapBothBoxFlow04 — T45, G22: Z⁴, β ∈ [0,4], every truncation 29 + m: one flowed weight with the box-order Cauchy
  estimate and clustering (GapBothBoxD4 + Cover04T8): every β ∈ [0, 4] and every truncation 29 + m: from one Migdal step k₀ on, the SAME flowed
  SU(2) plaquette weight has both the uniform infinite-volume Cauchy estimate (CauchyCone8) and exponential clustering
  in every finite volume (GapCone8). Lean 4 core only, no axioms.
-/
import ym.GapBothBoxD4
import ym.Cover04T8

namespace GapBothBoxFlow04

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Z⁴, β ∈ [0,4], every truncation**: from one Migdal step on, one weight with the box-order infinite-volume Cauchy estimate and clustering. -/
theorem both_box_flow04 (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 2)) (mm : Nat) :
    ∃ k0, ∀ jj,
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (hp0 : p0.1.length = 4)
    (n : Nat) (y : Nat → Nat) (M0 : List Bool) (hM0 : M0.length = Pn.length)
    (hyl : ∀ j, y j < Pn.length) (hoff : ∀ i, i < n → nthB M0 (y i) = false)
    (hyinj : ∀ i j, i < n → j < n → y i = y j → i = j)
    (Kk N : Nat) (hfar : ∀ i, i < n → Kk + 2 ≤ LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 ∧
      LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 < N + 2),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (VolOrder.Ms M0 y i) Ob) (ZxH K O H w (VolOrder.Ms M0 y i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * 4) + 4 * (2 * 4))))
      (pwR K (K.add K.one (pwR K h (5 + 1))) (4 * (2 * 4) + 4 * (2 * 4))))
    let s := 4 * 4 * 5 ^ p0.1.length
    let r := K.mul (pwR K h (6 + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (5 + 6 + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) := by
  obtain ⟨k0, hk0⟩ := Cover04T8.cover04T8 K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, fun jj => GapBothBoxD4.both_box_d4 K O F H DH h1 h10 h hh h0 _ (hk0 jj)⟩

end GapBothBoxFlow04

#print axioms GapBothBoxFlow04.both_box_flow04

#print axioms: 2 theorems, no axioms.

90.31 At most 17 Migdal steps into \(\mathrm {Cone}(2^{-8})\) for \(\beta \le 4\)

Proved: for every \(\beta \in [0,4]\) and every truncation \(29+m\), the flow enters \(\mathrm {Cone}(2^{-8})\) within 17 steps.

\[ \forall m\ \forall t\in [0,2]\ \exists k\le 17\ \forall j:\ \mathrm {Bd}_{2^{-8}}\big (\mathrm {mkIt}^{k+j}(\mathrm {wl}(t,29+m,11))\big ) \]

ym/Cover04T8B.lean

/-
  ym/Cover04T8B — T45, G2c: every β ∈ [0, 4] and every truncation Kk' = 29 + m: the 4D Migdal flow (L = 10) from the
  Wilson weight enters Cone(2⁻⁸) after at most 17 steps and stays there, uniformly in m (CertCover29T8.cover01T8 for t ≤ 1/2,
  TMCoverPA8B.cover_midB for 1/2 ≤ t ≤ 2; tails TruncCert.tail_hyp). Lean 4 core only, no axioms.
-/
import ym.CertCover29T8
import ym.TMCoverPA8B
import ym.CoverBridge


set_option exponentiation.threshold 4096
set_option maxRecDepth 100000

namespace Cover04T8B

open YMChain SU2Char CertCover MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem pad_eq (h : R) (hh : K.add h h = K.one) (A B : Nat) :
    K.mul (cN K (2 ^ (A + B))) (pwR K h B) = cN K (2 ^ A) := by
  rw [GapInputs.cN_pow2, GapInputs.cN_pow2, GKS.pwR_add, K.mul_assoc, GapInputs.pow2_h K h hh B, K.mul_comm, K.one_mul]

/-- **Every β ∈ [0, 4], uniformly in the truncation 29 + m.** -/
theorem cover04T8B (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 2)) (m : Nat) :
    ∃ k, k ≤ 17 ∧ ∀ j, ConeBound.Bd K O (pwR K h 8) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + m) 11).tail (k + j)) := by
  have ht4 : O.le t (cN K 4) := O.le_trans _ _ _ ht (SU2CharExp.cN_mono K O h1 2 4 (by decide))
  cases O.le_total t (rat K O F 1 2) with
  | inl hle =>
    obtain ⟨k, hk9, hk⟩ := CertCover29T8.cover01T8 K O F h1 h10 h hh h0 t ht0 hle ht4 m
    exact ⟨k, Nat.le_trans hk9 (by decide), hk⟩
  | inr hge =>
    have tl := fun n => TruncCert.tail_hyp K O F h1 h10 h hh h0 t ht0 ht4 m n
    exact TMCoverPA8B.cover_midB K O F h1 h10 h hh h0 t
      (CoverBridge.half_le K O F h1 h10 (2 ^ 599) t hge)
      (CoverBridge.le_two K O h1 (2 ^ 600) t ht)
      (pwR K h 84) (pad_eq K h hh 516 84) (29 + m) (fun n => (tl n).1) (fun n => (tl n).2)

end Cover04T8B

#print axioms Cover04T8B.cover04T8B

#print axioms: 2 theorems, no axioms.

90.32 \(\beta \le 4\): Cauchy estimate and clustering after at most 17 steps

Proved: for every \(\beta \in [0,4]\) and truncation \(29+m\), after at most 17 Migdal steps the flowed weight on \(\mathbb Z^4\) has the box-order Cauchy estimate and clustering.

\[ \forall m\ \forall t\in [0,2]\ \exists k_0\le 17\ \forall j:\ \text {Cauchy}_{\mathrm {box}}(w_{k_0+j})\wedge \text {Clustering}(w_{k_0+j}) \]

ym/GapBothBoxFlow04B.lean

/-
  ym/GapBothBoxFlow04B — T45, G22: Z⁴, β ∈ [0,4], every truncation 29 + m: one flowed weight with the box-order Cauchy
  estimate and clustering (GapBothBoxD4 + Cover04T8B: k₀ ≤ 17): every β ∈ [0, 4] and every truncation 29 + m: from one Migdal step k₀ on, the SAME flowed
  SU(2) plaquette weight has both the uniform infinite-volume Cauchy estimate (CauchyCone8) and exponential clustering
  in every finite volume (GapCone8). Lean 4 core only, no axioms.
-/
import ym.GapBothBoxD4
import ym.Cover04T8B

namespace GapBothBoxFlow04B

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Z⁴, β ∈ [0,4], every truncation**: from one Migdal step on, one weight with the box-order infinite-volume Cauchy estimate and clustering. -/
theorem both_box_flow04B (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 2)) (mm : Nat) :
    ∃ k0, k0 ≤ 17 ∧ ∀ jj,
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (hp0 : p0.1.length = 4)
    (n : Nat) (y : Nat → Nat) (M0 : List Bool) (hM0 : M0.length = Pn.length)
    (hyl : ∀ j, y j < Pn.length) (hoff : ∀ i, i < n → nthB M0 (y i) = false)
    (hyinj : ∀ i j, i < n → j < n → y i = y j → i = j)
    (Kk N : Nat) (hfar : ∀ i, i < n → Kk + 2 ≤ LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 ∧
      LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 < N + 2),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (VolOrder.Ms M0 y i) Ob) (ZxH K O H w (VolOrder.Ms M0 y i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * 4) + 4 * (2 * 4))))
      (pwR K (K.add K.one (pwR K h (5 + 1))) (4 * (2 * 4) + 4 * (2 * 4))))
    let s := 4 * 4 * 5 ^ p0.1.length
    let r := K.mul (pwR K h (6 + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (29 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (5 + 6 + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) := by
  obtain ⟨k0, hk17, hk0⟩ := Cover04T8B.cover04T8B K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, hk17, fun jj => GapBothBoxD4.both_box_d4 K O F H DH h1 h10 h hh h0 _ (hk0 jj)⟩

end GapBothBoxFlow04B

#print axioms GapBothBoxFlow04B.both_box_flow04B

#print axioms: 1 theorem, no axioms.

90.33 \(\beta \le 8\): into \(\mathrm {Cone}(1/16)\) within 30 steps, and the gap

Proved: for every \(\beta \in [0,8]\) and truncation \(40+m\), the flow enters \(\mathrm {Cone}(1/16)\) within 30 Migdal steps, and from then on the flowed weight has a volume-uniform gap on \(\mathbb Z^4\).

\[ \forall m\ \forall t\in [0,4]\ \exists k\le 30\ \forall j:\ \mathrm {Bd}_{1/16}\big (\mathrm {mkIt}^{k+j}(\mathrm {wl}(t,40+m,11))\big ),\quad 2|\langle U_pU_q\rangle Z-\langle U_p\rangle \langle U_q\rangle |\le B2^{-R}Z^2 \]

ym/Cover08T.lean

/-
  ym/Cover08T — T45, G1 + G2c: every β ∈ [0, 8] and every truncation Kk' = 40 + m: the 4D Migdal flow (L = 10) from
  the Wilson weight enters Cone(1/16) and stays there forever, uniformly in m.
    t ≤ 1/2        CertCover29T.cover01T  (interval certificates, base Kk = 29, tail 2⁻⁸⁴), m' = 11 + m
    1/2 ≤ t ≤ 2    TMCoverPA.cover_mid    (degree-12 Taylor models, base Kk = 29, tail 2⁻⁸⁴, S = 2³⁰⁰)
    2 ≤ t ≤ 4      TMCoverPB.cover_mid    (degree-20 Taylor models, base Kk = 40, tail 2⁻¹⁵⁰, S = 2⁵⁰⁰)
  Lean 4 core only, no axioms.
-/
import ym.CertCover29T
import ym.TMCoverPA
import ym.TMCoverPB
import ym.CoverBridge
import ym.CoverBridge2
import ym.TruncCert40


set_option exponentiation.threshold 4096
set_option maxRecDepth 100000

namespace Cover08T

open YMChain SU2Char CertCover MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem pad_eq (h : R) (hh : K.add h h = K.one) (A B : Nat) :
    K.mul (cN K (2 ^ (A + B))) (pwR K h B) = cN K (2 ^ A) := by
  rw [GapInputs.cN_pow2, GapInputs.cN_pow2, GKS.pwR_add, K.mul_assoc, GapInputs.pow2_h K h hh B, K.mul_comm, K.one_mul]

/-- **Every β ∈ [0, 8], uniformly in the truncation 40 + m.** -/
theorem cover08T (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (m : Nat) :
    ∃ k, ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + m) 11).tail (k + j)) := by
  have em : 29 + (11 + m) = 40 + m := by lib_nat
  cases O.le_total t (rat K O F 1 2) with
  | inl hle =>
    obtain ⟨k, _, hk⟩ := CertCover29T.cover01T K O F h1 h10 h hh h0 t ht0 hle ht (11 + m)
    rw [em] at hk; exact ⟨k, hk⟩
  | inr hge =>
    cases O.le_total t (cN K 2) with
    | inl hle2 =>
      have tl := fun n => TruncCert.tail_hyp K O F h1 h10 h hh h0 t ht0 ht (11 + m) n
      rw [em] at tl
      exact TMCoverPA.cover_mid K O F h1 h10 h hh h0 t
        (CoverBridge.half_le K O F h1 h10 (2 ^ 299) t hge)
        (CoverBridge.le_two K O h1 (2 ^ 300) t hle2)
        (pwR K h 84) (pad_eq K h hh 216 84) (40 + m) (fun n => (tl n).1) (fun n => (tl n).2)
    | inr hge2 =>
      have tl := fun n => TruncCert40.tail_hyp K O F h1 h10 h hh h0 t ht0 ht m n
      exact TMCoverPB.cover_mid K O F h1 h10 h hh h0 t
        (CoverBridge2.ge_c K O h1 (2 ^ 500) 2 t hge2) (CoverBridge2.le_c K O h1 (2 ^ 500) 4 t ht)
        (pwR K h 150) (pad_eq K h hh 350 150) (40 + m) (fun n => (tl n).1) (fun n => (tl n).2)

end Cover08T

#print axioms Cover08T.cover08T

ym/Cover08TB.lean

/-
  ym/Cover08TB — T45, G1 + G2c: every β ∈ [0, 8] and every truncation Kk' = 40 + m: the 4D Migdal flow (L = 10) from
  the Wilson weight is in Cone(1/16) after at most 30 steps and stays there forever, uniformly in m.
    t ≤ 1/2        CertCover29T.cover01T  (interval certificates, base Kk = 29, tail 2⁻⁸⁴), m' = 11 + m
    1/2 ≤ t ≤ 2    TMCoverPA.cover_mid    (degree-12 Taylor models, base Kk = 29, tail 2⁻⁸⁴, S = 2³⁰⁰)
    2 ≤ t ≤ 4      TMCoverPB.cover_mid    (degree-20 Taylor models, base Kk = 40, tail 2⁻¹⁵⁰, S = 2⁵⁰⁰)
  Lean 4 core only, no axioms.
-/
import ym.CertCover29T
import ym.TMCoverPAB
import ym.TMCoverPBB
import ym.CoverBridge
import ym.CoverBridge2
import ym.TruncCert40


set_option exponentiation.threshold 4096
set_option maxRecDepth 100000

namespace Cover08TB

open YMChain SU2Char CertCover MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem pad_eq (h : R) (hh : K.add h h = K.one) (A B : Nat) :
    K.mul (cN K (2 ^ (A + B))) (pwR K h B) = cN K (2 ^ A) := by
  rw [GapInputs.cN_pow2, GapInputs.cN_pow2, GKS.pwR_add, K.mul_assoc, GapInputs.pow2_h K h hh B, K.mul_comm, K.one_mul]

/-- **Every β ∈ [0, 8], uniformly in the truncation 40 + m.** -/
theorem cover08TB (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (m : Nat) :
    ∃ k, k ≤ 30 ∧ ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + m) 11).tail (k + j)) := by
  have em : 29 + (11 + m) = 40 + m := by lib_nat
  cases O.le_total t (rat K O F 1 2) with
  | inl hle =>
    obtain ⟨k, hk8, hk⟩ := CertCover29T.cover01T K O F h1 h10 h hh h0 t ht0 hle ht (11 + m)
    rw [em] at hk; exact ⟨k, Nat.le_trans hk8 (by decide), hk⟩
  | inr hge =>
    cases O.le_total t (cN K 2) with
    | inl hle2 =>
      have tl := fun n => TruncCert.tail_hyp K O F h1 h10 h hh h0 t ht0 ht (11 + m) n
      rw [em] at tl
      obtain ⟨k, hk, hc⟩ := TMCoverPAB.cover_midB K O F h1 h10 h hh h0 t
        (CoverBridge.half_le K O F h1 h10 (2 ^ 299) t hge)
        (CoverBridge.le_two K O h1 (2 ^ 300) t hle2)
        (pwR K h 84) (pad_eq K h hh 216 84) (40 + m) (fun n => (tl n).1) (fun n => (tl n).2)
      exact ⟨k, Nat.le_trans hk (by decide), hc⟩
    | inr hge2 =>
      have tl := fun n => TruncCert40.tail_hyp K O F h1 h10 h hh h0 t ht0 ht m n
      exact TMCoverPBB.cover_midB K O F h1 h10 h hh h0 t
        (CoverBridge2.ge_c K O h1 (2 ^ 500) 2 t hge2) (CoverBridge2.le_c K O h1 (2 ^ 500) 4 t ht)
        (pwR K h 150) (pad_eq K h hh 350 150) (40 + m) (fun n => (tl n).1) (fun n => (tl n).2)

end Cover08TB

#print axioms Cover08TB.cover08TB

ym/GapFlow08T.lean

/-
  ym/GapFlow08T — T45, G4: every β ∈ [0, 8] and every truncation 40 + m: after finitely many 4D Migdal steps (L = 10) the
  effective plaquette weight of SU(2) Wilson theory has a volume-uniform mass gap on Z⁴ (GapFlow.gap_of_cone with
  Cover08T.cover08T), and so does every later step. Lean 4 core only, no axioms.
-/
import ym.Cover08T
import ym.GapFlow

namespace GapFlow08T

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **β ∈ [0, 8] ⇒ the Migdal-flowed weight has a volume-uniform gap on Z⁴ from some step k₀ on.** -/
theorem gap_flow08 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (SU2Char.cN K 4)) (mm : Nat) :
    ∃ k0, ∀ j, ConeBound.Bd K O (pwR K h 4) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + mm) 11).tail (k0 + j)) :=
  Cover08T.cover08T K O F h1 h10 h hh h0 t ht0 ht mm

/-- the gap statement for each later step, with a_k the flowed coefficients -/
theorem gap_flow08_step (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (SU2Char.cN K 4)) (mm : Nat) :
    ∃ k0, ∀ j, ∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let a := MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + mm) 11).tail (k0 + j)
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 a (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  obtain ⟨k0, hc⟩ := Cover08T.cover08T K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, fun j LL hLL hL Pn hnd hv p0 q0 hpq tr Up Uq K1 Rr htd hUp hUq hta hUpa hUqa htr hK1 hUpb hUqb hdist M hM =>
    GapFlow.gap_of_cone K O F H DH h1 h10 h hh h0 (fun k => MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + mm) 11).tail k)
      k0 hc LL hLL hL Pn hnd hv p0 q0 hpq tr Up Uq K1 Rr htd hUp hUq hta hUpa hUqa htr hK1 hUpb hUqb hdist M hM j⟩

end GapFlow08T

#print axioms GapFlow08T.gap_flow08_step

#print axioms: 15 theorems, no axioms.

90.34 \(\beta \le 8\): into \(\mathrm {Cone}(2^{-8})\) within 30 steps, Cauchy estimate and clustering

Proved: for every \(\beta \in [0,8]\) and truncation \(40+m\), the flow enters \(\mathrm {Cone}(2^{-8})\) within 30 steps, and from then on the flowed weight on \(\mathbb Z^4\) has the Cauchy estimate and clustering.

\[ \forall m\ \forall t\in [0,4]\ \exists k\le 30\ \forall j:\ \mathrm {Bd}_{2^{-8}}(w_{k+j}),\qquad \exists k_0\ \forall j:\ \text {Cauchy}_{\mathrm {box}}(w_{k_0+j})\wedge \text {Clustering}(w_{k_0+j}) \]

ym/Cover08T8.lean

/-
  ym/Cover08T8 — T45, G1 + G2c: every β ∈ [0, 8] and every truncation Kk' = 40 + m: the 4D Migdal flow (L = 10) from
  the Wilson weight enters Cone(2⁻⁸) and stays there forever, uniformly in m (final check q = 8; S = 2⁶⁰⁰ / 2^{PB_B}).
    t ≤ 1/2        CertCover29T.cover01T  (interval certificates, base Kk = 29, tail 2⁻⁸⁴), m' = 11 + m
    1/2 ≤ t ≤ 2    TMCoverPA8.cover_mid    (degree-12 Taylor models, base Kk = 29, tail 2⁻⁸⁴, S = 2³⁰⁰)
    2 ≤ t ≤ 4      TMCoverPB8.cover_mid    (degree-20 Taylor models, base Kk = 40, tail 2⁻¹⁵⁰, S = 2⁵⁰⁰)
  Lean 4 core only, no axioms.
-/
import ym.CertCover29T8
import ym.TMCoverPA8
import ym.TMCoverPB8
import ym.CoverBridge
import ym.CoverBridge2
import ym.TruncCert40


set_option exponentiation.threshold 4096
set_option maxRecDepth 100000

namespace Cover08T8

open YMChain SU2Char CertCover MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem pad_eq (h : R) (hh : K.add h h = K.one) (A B : Nat) :
    K.mul (cN K (2 ^ (A + B))) (pwR K h B) = cN K (2 ^ A) := by
  rw [GapInputs.cN_pow2, GapInputs.cN_pow2, GKS.pwR_add, K.mul_assoc, GapInputs.pow2_h K h hh B, K.mul_comm, K.one_mul]

/-- **Every β ∈ [0, 8], uniformly in the truncation 40 + m.** -/
theorem cover08T8 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (m : Nat) :
    ∃ k, ∀ j, ConeBound.Bd K O (pwR K h 8) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + m) 11).tail (k + j)) := by
  have em : 29 + (11 + m) = 40 + m := by lib_nat
  cases O.le_total t (rat K O F 1 2) with
  | inl hle =>
    obtain ⟨k, _, hk⟩ := CertCover29T8.cover01T8 K O F h1 h10 h hh h0 t ht0 hle ht (11 + m)
    rw [em] at hk; exact ⟨k, hk⟩
  | inr hge =>
    cases O.le_total t (cN K 2) with
    | inl hle2 =>
      have tl := fun n => TruncCert.tail_hyp K O F h1 h10 h hh h0 t ht0 ht (11 + m) n
      rw [em] at tl
      exact TMCoverPA8.cover_mid K O F h1 h10 h hh h0 t
        (CoverBridge.half_le K O F h1 h10 (2 ^ 599) t hge)
        (CoverBridge.le_two K O h1 (2 ^ 600) t hle2)
        (pwR K h 84) (pad_eq K h hh 516 84) (40 + m) (fun n => (tl n).1) (fun n => (tl n).2)
    | inr hge2 =>
      have tl := fun n => TruncCert40.tail_hyp K O F h1 h10 h hh h0 t ht0 ht m n
      exact TMCoverPB8.cover_mid K O F h1 h10 h hh h0 t
        (CoverBridge2.ge_c K O h1 (2 ^ 700) 2 t hge2) (CoverBridge2.le_c K O h1 (2 ^ 700) 4 t ht)
        (pwR K h 150) (pad_eq K h hh 550 150) (40 + m) (fun n => (tl n).1) (fun n => (tl n).2)

end Cover08T8

#print axioms Cover08T8.cover08T8

ym/Cover08T8B.lean

/-
  ym/Cover08T8B — T45, G1 + G2c: every β ∈ [0, 8] and every truncation Kk' = 40 + m: the 4D Migdal flow (L = 10) from
  the Wilson weight enters Cone(2⁻⁸) and stays there forever, uniformly in m (final check q = 8; S = 2⁶⁰⁰ / 2^{PB_B}).
    t ≤ 1/2        CertCover29T.cover01T  (interval certificates, base Kk = 29, tail 2⁻⁸⁴), m' = 11 + m
    1/2 ≤ t ≤ 2    TMCoverPA8.cover_mid    (degree-12 Taylor models, base Kk = 29, tail 2⁻⁸⁴, S = 2³⁰⁰)
    2 ≤ t ≤ 4      TMCoverPB8.cover_mid    (degree-20 Taylor models, base Kk = 40, tail 2⁻¹⁵⁰, S = 2⁵⁰⁰)
  Lean 4 core only, no axioms.
-/
import ym.CertCover29T8
import ym.TMCoverPA8B
import ym.TMCoverPB8B
import ym.CoverBridge
import ym.CoverBridge2
import ym.TruncCert40


set_option exponentiation.threshold 4096
set_option maxRecDepth 100000

namespace Cover08T8B

open YMChain SU2Char CertCover MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem pad_eq (h : R) (hh : K.add h h = K.one) (A B : Nat) :
    K.mul (cN K (2 ^ (A + B))) (pwR K h B) = cN K (2 ^ A) := by
  rw [GapInputs.cN_pow2, GapInputs.cN_pow2, GKS.pwR_add, K.mul_assoc, GapInputs.pow2_h K h hh B, K.mul_comm, K.one_mul]

/-- **Every β ∈ [0, 8], uniformly in the truncation 40 + m.** -/
theorem cover08T8 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (m : Nat) :
    ∃ k, k ≤ 30 ∧ ∀ j, ConeBound.Bd K O (pwR K h 8) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + m) 11).tail (k + j)) := by
  have em : 29 + (11 + m) = 40 + m := by lib_nat
  cases O.le_total t (rat K O F 1 2) with
  | inl hle =>
    obtain ⟨k, hk9, hk⟩ := CertCover29T8.cover01T8 K O F h1 h10 h hh h0 t ht0 hle ht (11 + m)
    rw [em] at hk; exact ⟨k, Nat.le_trans hk9 (by decide), hk⟩
  | inr hge =>
    cases O.le_total t (cN K 2) with
    | inl hle2 =>
      have tl := fun n => TruncCert.tail_hyp K O F h1 h10 h hh h0 t ht0 ht (11 + m) n
      rw [em] at tl
      obtain ⟨k, hk, hb⟩ := TMCoverPA8B.cover_midB K O F h1 h10 h hh h0 t
        (CoverBridge.half_le K O F h1 h10 (2 ^ 599) t hge)
        (CoverBridge.le_two K O h1 (2 ^ 600) t hle2)
        (pwR K h 84) (pad_eq K h hh 516 84) (40 + m) (fun n => (tl n).1) (fun n => (tl n).2)
      exact ⟨k, Nat.le_trans hk (by decide), hb⟩
    | inr hge2 =>
      have tl := fun n => TruncCert40.tail_hyp K O F h1 h10 h hh h0 t ht0 ht m n
      exact TMCoverPB8B.cover_midB K O F h1 h10 h hh h0 t
        (CoverBridge2.ge_c K O h1 (2 ^ 700) 2 t hge2) (CoverBridge2.le_c K O h1 (2 ^ 700) 4 t ht)
        (pwR K h 150) (pad_eq K h hh 550 150) (40 + m) (fun n => (tl n).1) (fun n => (tl n).2)

end Cover08T8B

#print axioms Cover08T8B.cover08T8

ym/GapBothBoxFlow08.lean

/-
  ym/GapBothBoxFlow08 — T45, G22: Z⁴, β ∈ [0,8], every truncation 40 + m: one flowed weight with the box-order Cauchy
  estimate and clustering (GapBothBoxD4 + Cover08T8): every β ∈ [0, 4] and every truncation 40 + m: from one Migdal step k₀ on, the SAME flowed
  SU(2) plaquette weight has both the uniform infinite-volume Cauchy estimate (CauchyCone8) and exponential clustering
  in every finite volume (GapCone8). Lean 4 core only, no axioms.
-/
import ym.GapBothBoxD4
import ym.Cover08T8

namespace GapBothBoxFlow08

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Z⁴, β ∈ [0,8], every truncation**: from one Migdal step on, one weight with the box-order infinite-volume Cauchy estimate and clustering. -/
theorem both_box_flow08 (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (mm : Nat) :
    ∃ k0, ∀ jj,
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (hp0 : p0.1.length = 4)
    (n : Nat) (y : Nat → Nat) (M0 : List Bool) (hM0 : M0.length = Pn.length)
    (hyl : ∀ j, y j < Pn.length) (hoff : ∀ i, i < n → nthB M0 (y i) = false)
    (hyinj : ∀ i j, i < n → j < n → y i = y j → i = j)
    (Kk N : Nat) (hfar : ∀ i, i < n → Kk + 2 ≤ LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 ∧
      LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 < N + 2),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (VolOrder.Ms M0 y i) Ob) (ZxH K O H w (VolOrder.Ms M0 y i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * 4) + 4 * (2 * 4))))
      (pwR K (K.add K.one (pwR K h (5 + 1))) (4 * (2 * 4) + 4 * (2 * 4))))
    let s := 4 * 4 * 5 ^ p0.1.length
    let r := K.mul (pwR K h (6 + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (5 + 6 + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) := by
  obtain ⟨k0, hk0⟩ := Cover08T8.cover08T8 K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, fun jj => GapBothBoxD4.both_box_d4 K O F H DH h1 h10 h hh h0 _ (hk0 jj)⟩

end GapBothBoxFlow08

#print axioms GapBothBoxFlow08.both_box_flow08

ym/GapBothBoxFlow08B.lean

/-
  ym/GapBothBoxFlow08B — T45, G22: Z⁴, β ∈ [0,8], every truncation 40 + m: one flowed weight with the box-order Cauchy
  estimate and clustering (GapBothBoxD4 + Cover08T8B: k₀ ≤ 30): every β ∈ [0, 4] and every truncation 40 + m: from one Migdal step k₀ on, the SAME flowed
  SU(2) plaquette weight has both the uniform infinite-volume Cauchy estimate (CauchyCone8) and exponential clustering
  in every finite volume (GapCone8). Lean 4 core only, no axioms.
-/
import ym.GapBothBoxD4
import ym.Cover08T8B

namespace GapBothBoxFlow08B

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Z⁴, β ∈ [0,8], every truncation**: from one Migdal step on, one weight with the box-order infinite-volume Cauchy estimate and clustering. -/
theorem both_box_flow08B (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (mm : Nat) :
    ∃ k0, k0 ≤ 30 ∧ ∀ jj,
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (hp0 : p0.1.length = 4)
    (n : Nat) (y : Nat → Nat) (M0 : List Bool) (hM0 : M0.length = Pn.length)
    (hyl : ∀ j, y j < Pn.length) (hoff : ∀ i, i < n → nthB M0 (y i) = false)
    (hyinj : ∀ i j, i < n → j < n → y i = y j → i = j)
    (Kk N : Nat) (hfar : ∀ i, i < n → Kk + 2 ≤ LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 ∧
      LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 < N + 2),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (VolOrder.Ms M0 y i) Ob) (ZxH K O H w (VolOrder.Ms M0 y i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * 4) + 4 * (2 * 4))))
      (pwR K (K.add K.one (pwR K h (5 + 1))) (4 * (2 * 4) + 4 * (2 * 4))))
    let s := 4 * 4 * 5 ^ p0.1.length
    let r := K.mul (pwR K h (6 + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + mm) 11).tail (k0 + jj)) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (5 + 6 + 7)))
    let κ := K.add K.one (pwR K h (5 + 6 + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (5 + 6 + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) := by
  obtain ⟨k0, hk30, hk0⟩ := Cover08T8B.cover08T8 K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, hk30, fun jj => GapBothBoxD4.both_box_d4 K O F H DH h1 h10 h hh h0 _ (hk0 jj)⟩

end GapBothBoxFlow08B

#print axioms GapBothBoxFlow08B.both_box_flow08B

#print axioms: 7 theorems, no axioms.

90.35 Gap of the full flow for \(\beta \le 24\) and \(\beta \le 32\)

After 305 (resp. 273) kernel-checked rungs the full Migdal–Kadanoff flow is in RC(1/7) and has a volume-uniform gap.

\[ \beta =2t\le 24\ (\text {resp. }32):\quad |\langle U_pU_q\rangle -\langle U_p\rangle \langle U_q\rangle |\le \tfrac 12 B\,2^{-R}\ \text {at every step}\ge 309\ (\text {resp. }277) \]

ym/GapFlow24.lean

/-
  ym/GapFlow24 — T45, G78j: the volume-uniform gap of the Migdal–Kadanoff flow for β = 2t ≤ 24 (all characters,
  every list length M + 2 ≥ 4097): the certified ladder LadderMain24 (planD) puts the flow in RC(1/7); GapFlow8.cone_gen
  gives the cone Bd(h⁴) from 4 steps later on; GapFlow.gap_of_cone gives the gap. Lean 4 core only, no axioms.
-/
import ym.LadderMain24
import ym.GapFlow8

namespace GapFlow24

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist
  SU2Char HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- the number of rungs of the β ≤ 24 ladder -/
def K24 : Nat := 305

/-- the flow after the ladder -/
def X24 (t : R) (M j : Nat) : List R :=
  FlowIter.itX K O F (FlowIter.itX K O F (WilsonCone.wl K O F t 319 (M + 2)) M K24) (FlowIter.itM M K24) j

theorem cone24 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero) (ht8 : O.le t (cN K 12)) (M : Nat) (hM : 4095 ≤ M) :
    ∀ j, ConeBound.Bd K O (pwR K h 4) 0 (K.one :: GapFlow8.nrm K O F (X24 K O F t M (4 + j))) := by
  have w0 := WilsonFlowBig.wilson_flow64 K O F h1 h10 t ht0 (O.le_trans _ _ _ ht8 (SU2CharExp.cN_mono K O h1 _ _ (by decide : 12 ≤ 32))) htne M hM 0
  have Lf := LadderFin.ladder_fin K O F h1 h10 _ M w0.1 w0.2.1 w0.2.2 (LadderMain24.Ref K O F M) K24
    (LadderMain24.rungs K O F h1 h10 M hM) (LadderMain24.start K O F h1 h10 t ht0 htne ht8 M hM) K24 (Nat.le_refl _)
  have s0 := RefScalar.rat_nn K O F h1 h10 1 7 (by decide)
  have r0 := FlowRC.below_RC K O _ s0 _ _ Lf.1 (LadderMain24.finish K O F h1 h10 M hM)
  exact GapFlow8.cone_gen K O F h1 h10 h hh h0 _ _ Lf.2.1 Lf.2.2.1 Lf.2.2.2 r0

/-- **G78j: the volume-uniform gap of the flowed SU(2) plaquette weight for β = 2t ≤ 24** (MK model, every list
  length M + 2 ≥ 4097, every step K24 + 4 + j): connected plaquette correlations ≤ ½·B·2⁻ᴿ (GapFlow.gap_of_cone) -/
theorem gap24 (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero) (ht8 : O.le t (cN K 12)) (M : Nat) (hM : 4095 ≤ M)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (Ml : List Bool) (hMl : Ml.length = Pn.length) (j : Nat) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (GapFlow8.nrm K O F (X24 K O F t M (4 + j))) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w Ml (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w Ml (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w Ml Up) (ZxH K O H w Ml Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w Ml (fun _ => K.one)) (ZxH K O H w Ml (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w Ml (fun _ => K.one)) (ZxH K O H w Ml (fun _ => K.one)))) :=
  GapFlow.gap_of_cone K O F H DH h1 h10 h hh h0 (fun j => GapFlow8.nrm K O F (X24 K O F t M j)) 4
    (cone24 K O F h1 h10 h hh h0 t ht0 htne ht8 M hM) LL hLL hL Pn hnd hv p0 q0 hpq tr Up Uq K1 Rr htd hUp hUq
    hta hUpa hUqa htr hK1 hUpb hUqb hdist Ml hMl j

end GapFlow24

#print axioms GapFlow24.cone24
#print axioms GapFlow24.gap24

ym/GapFlow32.lean

/-
  ym/GapFlow32 — T45, G78j: the volume-uniform gap of the Migdal–Kadanoff flow for β = 2t ≤ 32 (all characters,
  every list length M + 2 ≥ 4097): the certified ladder LadderMain32 (planE) puts the flow in RC(1/7); GapFlow8.cone_gen
  gives the cone Bd(h⁴) from 4 steps later on; GapFlow.gap_of_cone gives the gap. Lean 4 core only, no axioms.
-/
import ym.LadderMain32
import ym.GapFlow8

namespace GapFlow32

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist
  SU2Char HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- the number of rungs of the β ≤ 32 ladder -/
def K32 : Nat := 273

/-- the flow after the ladder -/
def X32 (t : R) (M j : Nat) : List R :=
  FlowIter.itX K O F (FlowIter.itX K O F (WilsonCone.wl K O F t 319 (M + 2)) M K32) (FlowIter.itM M K32) j

theorem cone32 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero) (ht8 : O.le t (cN K 16)) (M : Nat) (hM : 4095 ≤ M) :
    ∀ j, ConeBound.Bd K O (pwR K h 4) 0 (K.one :: GapFlow8.nrm K O F (X32 K O F t M (4 + j))) := by
  have w0 := WilsonFlowBig.wilson_flow64 K O F h1 h10 t ht0 (O.le_trans _ _ _ ht8 (SU2CharExp.cN_mono K O h1 _ _ (by decide : 16 ≤ 32))) htne M hM 0
  have Lf := LadderFin.ladder_fin K O F h1 h10 _ M w0.1 w0.2.1 w0.2.2 (LadderMain32.Ref K O F M) K32
    (LadderMain32.rungs K O F h1 h10 M hM) (LadderMain32.start K O F h1 h10 t ht0 htne ht8 M hM) K32 (Nat.le_refl _)
  have s0 := RefScalar.rat_nn K O F h1 h10 1 7 (by decide)
  have r0 := FlowRC.below_RC K O _ s0 _ _ Lf.1 (LadderMain32.finish K O F h1 h10 M hM)
  exact GapFlow8.cone_gen K O F h1 h10 h hh h0 _ _ Lf.2.1 Lf.2.2.1 Lf.2.2.2 r0

/-- **G78j: the volume-uniform gap of the flowed SU(2) plaquette weight for β = 2t ≤ 32** (MK model, every list
  length M + 2 ≥ 4097, every step K32 + 4 + j): connected plaquette correlations ≤ ½·B·2⁻ᴿ (GapFlow.gap_of_cone) -/
theorem gap32 (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero) (ht8 : O.le t (cN K 16)) (M : Nat) (hM : 4095 ≤ M)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (Ml : List Bool) (hMl : Ml.length = Pn.length) (j : Nat) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (GapFlow8.nrm K O F (X32 K O F t M (4 + j))) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w Ml (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w Ml (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w Ml Up) (ZxH K O H w Ml Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w Ml (fun _ => K.one)) (ZxH K O H w Ml (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w Ml (fun _ => K.one)) (ZxH K O H w Ml (fun _ => K.one)))) :=
  GapFlow.gap_of_cone K O F H DH h1 h10 h hh h0 (fun j => GapFlow8.nrm K O F (X32 K O F t M j)) 4
    (cone32 K O F h1 h10 h hh h0 t ht0 htne ht8 M hM) LL hLL hL Pn hnd hv p0 q0 hpq tr Up Uq K1 Rr htd hUp hUq
    hta hUpa hUqa htr hK1 hUpb hUqb hdist Ml hMl j

end GapFlow32

#print axioms GapFlow32.cone32
#print axioms GapFlow32.gap32

#print axioms: 4 theorems, no axioms.

90.36 Discrete Gaussian variance bound

The second moment of the discrete Gaussian weight \(y^{m^2}\) is at most \(1/(1-y^2)\), with explicit boundary terms.

\[ 0\le y\le 1:\quad (1-y^2)\sum _{m=1}^{N}2m^2y^{m^2}+(N+1)y^{N^2}+Ny^{(N+1)^2}\le 1+2\sum _{m=1}^{N}y^{m^2} \]

ym/GaussVar.lean

/-
  ym/GaussVar — T45, G78m 6f-A′-1: the discrete Gaussian second moment is at most 1/(1 − y²).
    gs       gs t n = Σ_{i<n} t^{2i}  (gs t (n+1) = 1 + t² gs t n)
    gs_top   gs t (n+1) = gs t n + t^{2n}
    amgm1    2 t^m ≤ 1 + t^{2m}
    gs_low   (n + 1) t^n ≤ gs t (n + 1)            (AM–GM, step n → n + 2)
    gs_mul   (1 − t²) gs t n = 1 − t^{2n}
    pair     2 m² (1 − y²) y^{m²} ≤ m (y^{(m−1)²} − y^{(m+1)²})   (m = N + 1)
    var_le   (1 − y²) Σ_{m=1}^{N} 2 m² y^{m²} + (N+1) y^{N²} + N y^{(N+1)²} ≤ ϑ₃⁽ᴺ⁾(y) = U_N(y^{c²})
  The identity behind var_le is the telescoping ϑ₃⁽ᴺ⁾ − (boundary) = Σ_{m ≤ N} m (y^{(m−1)²} − y^{(m+1)²});
  each term dominates 2m²(1 − y²) y^{m²} by AM–GM on the pairs y^{2i}, y^{2(2m−1−i)}.
  Lean 4 core only, no axioms.
-/
import ym.ThetaLow
import ym.ThetaBound
import ym.GKS
import ym.SU2CharExp

namespace GaussVar

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- gs t n = Σ_{i<n} t^{2i} -/
def gs (t : R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add K.one (K.mul (K.mul t t) (gs t n))

/-- t^{2(n+1)} = t² t^{2n} -/
theorem pw2 (t : R) (n : Nat) : pwR K t (2 * (n + 1)) = K.mul (K.mul t t) (pwR K t (2 * n)) := by
  rw [show 2 * (n + 1) = 2 * n + 2 by lib_nat, GKS.pwR_add K t (2 * n) 2]
  show K.mul (pwR K t (2 * n)) (K.mul (K.mul K.one t) t) = _
  generalize pwR K t (2 * n) = p
  lib_ring

/-- gs t (n+1) = gs t n + t^{2n} -/
theorem gs_top (t : R) : ∀ n, gs K t (n + 1) = K.add (gs K t n) (pwR K t (2 * n))
  | 0 => by
    show K.add K.one (K.mul (K.mul t t) K.zero) = K.add K.zero K.one
    lib_ring
  | n + 1 => by
    show K.add K.one (K.mul (K.mul t t) (gs K t (n + 1))) = K.add (gs K t (n + 1)) (pwR K t (2 * (n + 1)))
    rw [pw2 K t n]
    show K.add K.one (K.mul (K.mul t t) (gs K t (n + 1)))
      = K.add (K.add K.one (K.mul (K.mul t t) (gs K t n))) (K.mul (K.mul t t) (pwR K t (2 * n)))
    rw [gs_top t n]
    generalize gs K t n = g
    generalize pwR K t (2 * n) = p
    lib_ring

/-- AM–GM: 2 t^m ≤ 1 + t^{2m} -/
theorem amgm1 (t : R) (m : Nat) :
    O.le (K.mul (AltDiff.two K) (pwR K t m)) (K.add K.one (pwR K t (2 * m))) := by
  apply ThetaLow.le_of_sub K O
  have e : K.add (K.add K.one (pwR K t (2 * m))) (K.neg (K.mul (AltDiff.two K) (pwR K t m)))
      = K.mul (K.add K.one (K.neg (pwR K t m))) (K.add K.one (K.neg (pwR K t m))) := by
    rw [show 2 * m = m + m by lib_nat, GKS.pwR_add K t m m]
    show K.add (K.add K.one (K.mul (pwR K t m) (pwR K t m))) (K.neg (K.mul (K.add K.one K.one) (pwR K t m))) = _
    generalize pwR K t m = p
    lib_ring
  rw [e]
  exact ThetaBound.sq_nn K O _

/-- **(n + 1) t^n ≤ Σ_{i ≤ n} t^{2i}** for 0 ≤ t -/
theorem gs_low (t : R) (ht : O.le K.zero t) : ∀ n, O.le (K.mul (cN K (n + 1)) (pwR K t n)) (gs K t (n + 1))
  | 0 => by
    have e : K.mul (cN K (0 + 1)) (pwR K t 0) = gs K t (0 + 1) := by
      show K.mul (K.add K.one K.zero) K.one = K.add K.one (K.mul (K.mul t t) K.zero)
      lib_ring
    rw [e]; exact O.le_refl _
  | 1 => by
    have a := amgm1 K O t 1
    have e1 : K.mul (AltDiff.two K) (pwR K t 1) = K.mul (cN K (1 + 1)) (pwR K t 1) := by
      show K.mul (K.add K.one K.one) (pwR K t 1) = K.mul (K.add K.one (K.add K.one K.zero)) (pwR K t 1)
      generalize pwR K t 1 = p
      lib_ring
    have e2 : K.add K.one (pwR K t (2 * 1)) = gs K t (1 + 1) := by
      show K.add K.one (K.mul (K.mul K.one t) t) = K.add K.one (K.mul (K.mul t t) (K.add K.one (K.mul (K.mul t t) K.zero)))
      lib_ring
    rw [e1, e2] at a
    exact a
  | n + 2 => by
    have a := amgm1 K O t (n + 2)
    have b := O.mul_le_mul_right _ _ _ (gs_low t ht n) (O.mul_nonneg _ _ ht ht)
    have c := O.add_le_add _ _ _ _ a b
    have eL : K.add (K.mul (AltDiff.two K) (pwR K t (n + 2))) (K.mul (K.mul (cN K (n + 1)) (pwR K t n)) (K.mul t t))
        = K.mul (cN K (n + 2 + 1)) (pwR K t (n + 2)) := by
      show K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (pwR K t n) t) t)) (K.mul (K.mul (cN K (n + 1)) (pwR K t n)) (K.mul t t))
        = K.mul (K.add K.one (K.add K.one (cN K (n + 1)))) (K.mul (K.mul (pwR K t n) t) t)
      generalize pwR K t n = p
      generalize cN K (n + 1) = q
      lib_ring
    have eR : K.add (K.add K.one (pwR K t (2 * (n + 2)))) (K.mul (gs K t (n + 1)) (K.mul t t))
        = gs K t (n + 2 + 1) := by
      show K.add (K.add K.one (pwR K t (2 * (n + 1 + 1)))) (K.mul (gs K t (n + 1)) (K.mul t t))
        = K.add K.one (K.mul (K.mul t t) (gs K t (n + 1 + 1)))
      rw [gs_top K t (n + 1), pw2 K t (n + 1)]
      generalize gs K t (n + 1) = g
      generalize pwR K t (2 * (n + 1)) = p
      lib_ring
    rw [eL, eR] at c
    exact c

/-- (1 − t²) gs t n = 1 − t^{2n} -/
theorem gs_mul (t : R) : ∀ n, K.mul (K.add K.one (K.neg (K.mul t t))) (gs K t n) = K.add K.one (K.neg (pwR K t (2 * n)))
  | 0 => by
    show K.mul (K.add K.one (K.neg (K.mul t t))) K.zero = K.add K.one (K.neg K.one)
    lib_ring
  | n + 1 => by
    show K.mul (K.add K.one (K.neg (K.mul t t))) (K.add K.one (K.mul (K.mul t t) (gs K t n)))
      = K.add K.one (K.neg (pwR K t (2 * (n + 1))))
    have ih := gs_mul t n
    rw [pw2 K t n]
    have e : K.mul (K.add K.one (K.neg (K.mul t t))) (K.add K.one (K.mul (K.mul t t) (gs K t n)))
        = K.add (K.add K.one (K.neg (K.mul t t)))
            (K.mul (K.mul t t) (K.mul (K.add K.one (K.neg (K.mul t t))) (gs K t n))) := by
      generalize gs K t n = g
      lib_ring
    rw [e, ih]
    generalize pwR K t (2 * n) = p
    lib_ring

/-- **pair**: 2 (N+1)² (1 − y²) y^{(N+1)²} ≤ (N+1) (y^{N²} − y^{(N+2)²}) for 0 ≤ y ≤ 1 -/
theorem pair (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) (N : Nat) :
    O.le (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1)))
        (K.mul (K.add K.one (K.neg (K.mul y y))) (pwR K y ((N + 1) * (N + 1))))))
      (K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1)))))) := by
  have hyy : O.le (K.mul y y) K.one := by
    have := O.mul_le_mul_right _ _ _ hy1 hy0
    rw [show K.mul K.one y = y by lib_ring] at this
    exact O.le_trans _ _ _ this hy1
  have h1y : O.le K.zero (K.add K.one (K.neg (K.mul y y))) := AltSum.le_sub K O _ _ hyy
  have g := gs_low K O y hy0 (2 * N + 1)
  have g1 := O.mul_le_mul_right _ _ _ g h1y
  have g2 := O.mul_le_mul_right _ _ _ g1 (ThetaDup5.pw_nn K O y hy0 h1 (N * N))
  have g3 := O.mul_le_mul_right _ _ _ g2 (SU2CharExp.cN_nn K O h1 (N + 1))
  have eL : K.mul (K.mul (K.mul (K.mul (cN K (2 * N + 1 + 1)) (pwR K y (2 * N + 1))) (K.add K.one (K.neg (K.mul y y))))
        (pwR K y (N * N))) (cN K (N + 1))
      = K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1)))
        (K.mul (K.add K.one (K.neg (K.mul y y))) (pwR K y ((N + 1) * (N + 1))))) := by
    rw [show 2 * N + 1 + 1 = (N + 1) + (N + 1) by lib_nat, SU2Char.cN_add K (N + 1),
      show (N + 1) * (N + 1) = N * N + (2 * N + 1) by lib_nat, GKS.pwR_add K y (N * N) (2 * N + 1)]
    show _ = K.mul (K.add K.one K.one) _
    generalize cN K (N + 1) = c
    generalize pwR K y (2 * N + 1) = p
    generalize pwR K y (N * N) = q
    lib_ring
  have eR : K.mul (K.mul (K.mul (gs K y (2 * N + 1 + 1)) (K.add K.one (K.neg (K.mul y y)))) (pwR K y (N * N))) (cN K (N + 1))
      = K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1))))) := by
    have m := gs_mul K y (2 * N + 1 + 1)
    have e1 : K.mul (K.mul (K.mul (gs K y (2 * N + 1 + 1)) (K.add K.one (K.neg (K.mul y y)))) (pwR K y (N * N))) (cN K (N + 1))
        = K.mul (K.mul (K.mul (K.add K.one (K.neg (K.mul y y))) (gs K y (2 * N + 1 + 1))) (pwR K y (N * N))) (cN K (N + 1)) := by
      generalize gs K y (2 * N + 1 + 1) = g
      lib_ring
    rw [e1, m, show (N + 1 + 1) * (N + 1 + 1) = N * N + 2 * (2 * N + 1 + 1) by lib_nat, GKS.pwR_add K y (N * N) (2 * (2 * N + 1 + 1))]
    generalize cN K (N + 1) = c
    generalize pwR K y (2 * (2 * N + 1 + 1)) = p
    generalize pwR K y (N * N) = q
    lib_ring
  rw [eL, eR] at g3
  exact g3

/-- V y N = Σ_{m=1}^{N} 2 m² y^{m²} -/
def V (y : R) : Nat → R
  | 0 => K.zero
  | N + 1 => K.add (V y N) (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1))) (pwR K y ((N + 1) * (N + 1)))))

/-- **discrete Gaussian variance bound**:
  (1 − y²) Σ_{m=1}^{N} 2m² y^{m²} + (N+1) y^{N²} + N y^{(N+1)²} ≤ ϑ₃⁽ᴺ⁾(y) for 0 ≤ y ≤ 1 -/
theorem var_le (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) : ∀ N,
    O.le (K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y N))
        (K.add (K.mul (cN K (N + 1)) (pwR K y (N * N))) (K.mul (cN K N) (pwR K y ((N + 1) * (N + 1))))))
      (AltDiff.tU K (fun c => pwR K y (c * c)) N)
  | 0 => by
    have e : K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y 0))
        (K.add (K.mul (cN K (0 + 1)) (pwR K y (0 * 0))) (K.mul (cN K 0) (pwR K y ((0 + 1) * (0 + 1)))))
        = AltDiff.tU K (fun c => pwR K y (c * c)) 0 := by
      show K.add (K.mul (K.add K.one (K.neg (K.mul y y))) K.zero)
          (K.add (K.mul (K.add K.one K.zero) (pwR K y (0 * 0))) (K.mul K.zero (pwR K y ((0 + 1) * (0 + 1)))))
          = pwR K y (0 * 0)
      generalize pwR K y (0 * 0) = p
      generalize pwR K y ((0 + 1) * (0 + 1)) = r
      lib_ring
    rw [e]; exact O.le_refl _
  | N + 1 => by
    have ih := var_le h1 y hy0 hy1 N
    have pr := pair K O h1 y hy0 hy1 N
    have s := O.add_le_add _ _ _ _ ih pr
    have s2 := O.add_le_add _ _ _ _ s (O.le_refl (K.add (K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))))
      (K.neg (K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1)))))))))
    have eL : K.add (K.add (K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y N))
          (K.add (K.mul (cN K (N + 1)) (pwR K y (N * N))) (K.mul (cN K N) (pwR K y ((N + 1) * (N + 1))))))
          (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1)))
            (K.mul (K.add K.one (K.neg (K.mul y y))) (pwR K y ((N + 1) * (N + 1)))))))
        (K.add (K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))))
          (K.neg (K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1))))))))
        = K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y (N + 1)))
          (K.add (K.mul (cN K (N + 1 + 1)) (pwR K y ((N + 1) * (N + 1))))
            (K.mul (cN K (N + 1)) (pwR K y ((N + 1 + 1) * (N + 1 + 1))))) := by
      show _ = K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (K.add (V K y N) (K.mul (K.add K.one K.one)
            (K.mul (K.mul (K.add K.one (cN K N)) (K.add K.one (cN K N))) (pwR K y ((N + 1) * (N + 1)))))))
          (K.add (K.mul (K.add K.one (K.add K.one (cN K N))) (pwR K y ((N + 1) * (N + 1))))
            (K.mul (K.add K.one (cN K N)) (pwR K y ((N + 1 + 1) * (N + 1 + 1)))))
      show K.add (K.add (K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y N))
          (K.add (K.mul (K.add K.one (cN K N)) (pwR K y (N * N))) (K.mul (cN K N) (pwR K y ((N + 1) * (N + 1))))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul (K.add K.one (cN K N)) (K.add K.one (cN K N)))
            (K.mul (K.add K.one (K.neg (K.mul y y))) (pwR K y ((N + 1) * (N + 1)))))))
        (K.add (K.mul (K.add K.one K.one) (pwR K y ((N + 1) * (N + 1))))
          (K.neg (K.mul (K.add K.one (cN K N)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1))))))))
        = _
      generalize V K y N = v
      generalize cN K N = c
      generalize pwR K y (N * N) = a
      generalize pwR K y ((N + 1) * (N + 1)) = b
      generalize pwR K y ((N + 1 + 1) * (N + 1 + 1)) = d
      lib_ring
    have eR : K.add (K.add (AltDiff.tU K (fun c => pwR K y (c * c)) N)
          (K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1)))))))
        (K.add (K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))))
          (K.neg (K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1))))))))
        = AltDiff.tU K (fun c => pwR K y (c * c)) (N + 1) := by
      show _ = K.add (AltDiff.tU K (fun c => pwR K y (c * c)) N) (K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))))
      generalize AltDiff.tU K (fun c => pwR K y (c * c)) N = T
      generalize K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1))))) = P
      generalize K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))) = W
      lib_ring
    rw [eL, eR] at s2
    exact s2

end GaussVar

#print axioms GaussVar.pw2
#print axioms GaussVar.gs_top
#print axioms GaussVar.amgm1
#print axioms GaussVar.gs_low
#print axioms GaussVar.gs_mul
#print axioms GaussVar.pair
#print axioms GaussVar.var_le

#print axioms: 7 theorems, no axioms.

90.37 Slack in the variance bound

The gap in the discrete Gaussian variance bound is at least an explicit positive sum of the true size \(\sqrt {s}\).

\[ (1-x^2)V_N+(N+1)x^{N^2}+Nx^{(N+1)^2}+\sum _{M<N}(M+1)(1-x^2)(1-x)^2x^{M^2+2M}\sum _{i\le M}(2i+1)^2\le \vartheta _3^{(N)}(x) \]

ym/GaussVar2.lean

/-
  ym/GaussVar2 — T45, G78m 6f-A′-1b: a lower bound for the slack in the discrete Gaussian variance bound.
    gs1        gs1 x n = Σ_{i<n} x^i
    gs1_mul    (1 − x) gs1 x n = 1 − x^n
    gs1_low    (2r+1) x^r ≤ gs1 x (2r+1)                       (AM–GM on x^i, x^{2r−i})
    one_sub    (2r+1)(1 − x) x^r ≤ 1 − x^{2r+1}
    oddSq M    = Σ_{i ≤ M} (2i+1)²
    resid      (1 − x)² x^{2M} oddSq M ≤ Σ_{i<2M+2} x^{2i} − (2M+2) x^{2M+1}      (Q_{m+1} = x² Q_m + (1 − x^{2m+1})²)
    slack_eq   (N+1)(x^{N²} − x^{(N+2)²}) = 2(N+1)²(1 − x²) x^{(N+1)²} + (N+1)(1 − x²) x^{N²} Q_{N+1}
    slack_low  wl x N + 2(N+1)²(1 − x²) x^{(N+1)²} ≤ (N+1)(x^{N²} − x^{(N+2)²}),
               wl x N = (N+1)(1 − x²)(1 − x)² x^{N² + 2N} oddSq N
    var_low    (1 − x²) V_N + (N+1) x^{N²} + N x^{(N+1)²} + Σ_{M<N} wl x M ≤ ϑ₃⁽ᴺ⁾(x)
  With oddSq N = (N+1)(2N+1)(2N+3)/3 the slack sum is ≈ √π·√s for x = e^{−s} → 1 (the true size of
  ϑ₃ − (1 − x²)Σ c² x^{c²}), which beats the ϑ₄ part of 6f-A′-1 (ThetaBound3) by a factor ≥ 3 for s ≤ 0.235.
  Lean 4 core only, no axioms.
-/
import ym.GaussVar
import ym.ThetaBound3

namespace GaussVar2

open YMChain LinkIntegral SU2Char GaussVar

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- gs1 x n = Σ_{i<n} x^i -/
def gs1 (x : R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add K.one (K.mul x (gs1 x n))

/-- gs1 x (n+1) = gs1 x n + x^n -/
theorem gs1_top (x : R) : ∀ n, gs1 K x (n + 1) = K.add (gs1 K x n) (pwR K x n)
  | 0 => by
    show K.add K.one (K.mul x K.zero) = K.add K.zero K.one
    lib_ring
  | n + 1 => by
    show K.add K.one (K.mul x (gs1 K x (n + 1))) = K.add (K.add K.one (K.mul x (gs1 K x n))) (K.mul (pwR K x n) x)
    rw [gs1_top x n]
    generalize gs1 K x n = g
    generalize pwR K x n = p
    lib_ring

/-- (1 − x) gs1 x n = 1 − x^n -/
theorem gs1_mul (x : R) : ∀ n, K.mul (K.add K.one (K.neg x)) (gs1 K x n) = K.add K.one (K.neg (pwR K x n))
  | 0 => by
    show K.mul (K.add K.one (K.neg x)) K.zero = K.add K.one (K.neg K.one)
    lib_ring
  | n + 1 => by
    have ih := gs1_mul x n
    show K.mul (K.add K.one (K.neg x)) (K.add K.one (K.mul x (gs1 K x n))) = K.add K.one (K.neg (K.mul (pwR K x n) x))
    have e : K.mul (K.add K.one (K.neg x)) (K.add K.one (K.mul x (gs1 K x n)))
        = K.add (K.add K.one (K.neg x)) (K.mul x (K.mul (K.add K.one (K.neg x)) (gs1 K x n))) := by
      generalize gs1 K x n = g
      lib_ring
    rw [e, ih]
    generalize pwR K x n = p
    lib_ring

/-- **(2r+1) x^r ≤ Σ_{i ≤ 2r} x^i** for 0 ≤ x -/
theorem gs1_low (x : R) (hx : O.le K.zero x) : ∀ r, O.le (K.mul (cN K (2 * r + 1)) (pwR K x r)) (gs1 K x (2 * r + 1))
  | 0 => by
    have e : K.mul (cN K (2 * 0 + 1)) (pwR K x 0) = gs1 K x (2 * 0 + 1) := by
      show K.mul (K.add K.one K.zero) K.one = K.add K.one (K.mul x K.zero)
      lib_ring
    rw [e]; exact O.le_refl _
  | r + 1 => by
    have a := amgm1 K O x (r + 1)
    have b := O.mul_le_mul_right _ _ _ (gs1_low x hx r) hx
    have c := O.add_le_add _ _ _ _ a b
    have eL : K.add (K.mul (AltDiff.two K) (pwR K x (r + 1))) (K.mul (K.mul (cN K (2 * r + 1)) (pwR K x r)) x)
        = K.mul (cN K (2 * (r + 1) + 1)) (pwR K x (r + 1)) := by
      rw [show 2 * (r + 1) + 1 = (2 * r + 1) + 1 + 1 by lib_nat]
      show K.add (K.mul (K.add K.one K.one) (K.mul (pwR K x r) x)) (K.mul (K.mul (cN K (2 * r + 1)) (pwR K x r)) x)
        = K.mul (K.add K.one (K.add K.one (cN K (2 * r + 1)))) (K.mul (pwR K x r) x)
      generalize pwR K x r = p
      generalize cN K (2 * r + 1) = q
      lib_ring
    have eR : K.add (K.add K.one (pwR K x (2 * (r + 1)))) (K.mul (gs1 K x (2 * r + 1)) x)
        = gs1 K x (2 * (r + 1) + 1) := by
      rw [show 2 * (r + 1) + 1 = (2 * r + 1) + 1 + 1 by lib_nat, show 2 * (r + 1) = (2 * r + 1) + 1 by lib_nat]
      show _ = K.add K.one (K.mul x (gs1 K x (2 * r + 1 + 1)))
      rw [gs1_top K x (2 * r + 1)]
      show K.add (K.add K.one (K.mul (pwR K x (2 * r + 1)) x)) (K.mul (gs1 K x (2 * r + 1)) x)
        = K.add K.one (K.mul x (K.add (gs1 K x (2 * r + 1)) (pwR K x (2 * r + 1))))
      generalize gs1 K x (2 * r + 1) = g
      generalize pwR K x (2 * r + 1) = p
      lib_ring
    rw [eL, eR] at c
    exact c

/-- **(2r+1)(1 − x) x^r ≤ 1 − x^{2r+1}** for 0 ≤ x ≤ 1 -/
theorem one_sub (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (r : Nat) :
    O.le (K.mul (cN K (2 * r + 1)) (K.mul (K.add K.one (K.neg x)) (pwR K x r))) (K.add K.one (K.neg (pwR K x (2 * r + 1)))) := by
  have h1x : O.le K.zero (K.add K.one (K.neg x)) := AltSum.le_sub K O _ _ hx1
  have g := O.mul_le_mul_right _ _ _ (gs1_low K O x hx0 r) h1x
  have m := gs1_mul K x (2 * r + 1)
  rw [show K.mul (gs1 K x (2 * r + 1)) (K.add K.one (K.neg x)) = K.mul (K.add K.one (K.neg x)) (gs1 K x (2 * r + 1)) by
    generalize gs1 K x (2 * r + 1) = q; lib_ring, m] at g
  rw [show K.mul (cN K (2 * r + 1)) (K.mul (K.add K.one (K.neg x)) (pwR K x r))
      = K.mul (K.mul (cN K (2 * r + 1)) (pwR K x r)) (K.add K.one (K.neg x)) by
    generalize cN K (2 * r + 1) = c; generalize pwR K x r = p; lib_ring]
  exact g

/-- oddSq M = Σ_{i ≤ M} (2i+1)² -/
def oddSq : Nat → R
  | 0 => K.one
  | M + 1 => K.add (oddSq M) (K.mul (cN K (2 * M + 3)) (cN K (2 * M + 3)))

theorem oddSq_nn (h1 : O.le K.zero K.one) : ∀ M, O.le K.zero (oddSq K M)
  | 0 => h1
  | M + 1 => by
    have := O.add_le_add _ _ _ _ (oddSq_nn h1 M) (ThetaBound.sq_nn K O (cN K (2 * M + 3)))
    rw [K.zero_add] at this
    exact this

/-- **AM–GM residual**: (1 − x)² x^{2M} Σ_{i≤M}(2i+1)² ≤ Σ_{i<2M+2} x^{2i} − (2M+2) x^{2M+1} -/
theorem resid (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) : ∀ M,
    O.le (K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul (pwR K x (2 * M)) (oddSq K M)))
      (K.add (gs K x (2 * M + 2)) (K.neg (K.mul (cN K (2 * M + 2)) (pwR K x (2 * M + 1)))))
  | 0 => by
    have e : K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul (pwR K x (2 * 0)) (oddSq K 0))
        = K.add (gs K x (2 * 0 + 2)) (K.neg (K.mul (cN K (2 * 0 + 2)) (pwR K x (2 * 0 + 1)))) := by
      show K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul K.one K.one)
        = K.add (K.add K.one (K.mul (K.mul x x) (K.add K.one (K.mul (K.mul x x) K.zero))))
          (K.neg (K.mul (K.add K.one (K.add K.one K.zero)) (K.mul K.one x)))
      lib_ring
    rw [e]; exact O.le_refl _
  | M + 1 => by
    have ih := resid h1 x hx0 hx1 M
    have hxx : O.le K.zero (K.mul x x) := O.mul_nonneg _ _ hx0 hx0
    have a := O.mul_le_mul_right _ _ _ ih hxx
    -- (2M+3)(1−x)x^{M+1} ≤ 1 − x^{2M+3}, squared
    have os := one_sub K O x hx0 hx1 (M + 1)
    have hnn : O.le K.zero (K.mul (cN K (2 * (M + 1) + 1)) (K.mul (K.add K.one (K.neg x)) (pwR K x (M + 1)))) :=
      O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (O.mul_nonneg _ _ (AltSum.le_sub K O _ _ hx1) (ThetaDup5.pw_nn K O x hx0 h1 _))
    have hc : O.le K.zero (K.add K.one (K.neg (pwR K x (2 * (M + 1) + 1)))) := O.le_trans _ _ _ hnn os
    have hnc : O.le (K.neg (K.add K.one (K.neg (pwR K x (2 * (M + 1) + 1))))) K.zero := by
      apply ThetaLow.le_of_sub K O
      rw [show K.add K.zero (K.neg (K.neg (K.add K.one (K.neg (pwR K x (2 * (M + 1) + 1))))))
        = K.add K.one (K.neg (pwR K x (2 * (M + 1) + 1))) by lib_ring]
      exact hc
    have sq := ThetaBound.sq_le K O _ _ ⟨O.le_trans _ _ _ hnc hnn, os⟩
    have c := O.add_le_add _ _ _ _ a sq
    have eL : K.add (K.mul (K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul (pwR K x (2 * M)) (oddSq K M))) (K.mul x x))
          (K.mul (K.mul (cN K (2 * (M + 1) + 1)) (K.mul (K.add K.one (K.neg x)) (pwR K x (M + 1))))
            (K.mul (cN K (2 * (M + 1) + 1)) (K.mul (K.add K.one (K.neg x)) (pwR K x (M + 1)))))
        = K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul (pwR K x (2 * (M + 1))) (oddSq K (M + 1))) := by
      rw [show 2 * (M + 1) = (M + 1) + (M + 1) by lib_nat, GKS.pwR_add K x (M + 1) (M + 1),
        show (M + 1) + (M + 1) + 1 = 2 * M + 3 by lib_nat]
      show _ = K.mul _ (K.mul _ (K.add (oddSq K M) (K.mul (cN K (2 * M + 3)) (cN K (2 * M + 3)))))
      rw [show pwR K x (2 * M) = K.mul (pwR K x M) (pwR K x M) by
        rw [show 2 * M = M + M by lib_nat, GKS.pwR_add K x M M]]
      show K.add (K.mul (K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul (K.mul (pwR K x M) (pwR K x M)) (oddSq K M))) (K.mul x x))
          (K.mul (K.mul (cN K (2 * M + 3)) (K.mul (K.add K.one (K.neg x)) (K.mul (pwR K x M) x)))
            (K.mul (cN K (2 * M + 3)) (K.mul (K.add K.one (K.neg x)) (K.mul (pwR K x M) x))))
        = K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x)))
          (K.mul (K.mul (K.mul (pwR K x M) x) (K.mul (pwR K x M) x)) (K.add (oddSq K M) (K.mul (cN K (2 * M + 3)) (cN K (2 * M + 3)))))
      generalize pwR K x M = p
      generalize oddSq K M = o
      generalize cN K (2 * M + 3) = c
      lib_ring
    have eR : K.add (K.mul (K.add (gs K x (2 * M + 2)) (K.neg (K.mul (cN K (2 * M + 2)) (pwR K x (2 * M + 1))))) (K.mul x x))
          (K.mul (K.add K.one (K.neg (pwR K x (2 * (M + 1) + 1)))) (K.add K.one (K.neg (pwR K x (2 * (M + 1) + 1)))))
        = K.add (gs K x (2 * (M + 1) + 2)) (K.neg (K.mul (cN K (2 * (M + 1) + 2)) (pwR K x (2 * (M + 1) + 1)))) := by
      rw [show 2 * (M + 1) + 2 = (2 * M + 2) + 1 + 1 by lib_nat, show 2 * (M + 1) + 1 = 2 * M + 3 by lib_nat]
      show _ = K.add (K.add K.one (K.mul (K.mul x x) (gs K x (2 * M + 2 + 1))))
        (K.neg (K.mul (K.add K.one (K.add K.one (cN K (2 * M + 2)))) (pwR K x (2 * M + 3))))
      rw [gs_top K x (2 * M + 2), show 2 * (2 * M + 2) = (2 * M + 1) + (2 * M + 3) by lib_nat,
        GKS.pwR_add K x (2 * M + 1) (2 * M + 3)]
      show K.add (K.mul (K.add (gs K x (2 * M + 2)) (K.neg (K.mul (cN K (2 * M + 2)) (pwR K x (2 * M + 1))))) (K.mul x x))
          (K.mul (K.add K.one (K.neg (K.mul (K.mul (pwR K x (2 * M + 1)) x) x))) (K.add K.one (K.neg (K.mul (K.mul (pwR K x (2 * M + 1)) x) x))))
        = K.add (K.add K.one (K.mul (K.mul x x) (K.add (gs K x (2 * M + 2))
            (K.mul (pwR K x (2 * M + 1)) (K.mul (K.mul (pwR K x (2 * M + 1)) x) x)))))
          (K.neg (K.mul (K.add K.one (K.add K.one (cN K (2 * M + 2)))) (K.mul (K.mul (pwR K x (2 * M + 1)) x) x)))
      generalize gs K x (2 * M + 2) = g
      generalize pwR K x (2 * M + 1) = p
      generalize cN K (2 * M + 2) = c
      lib_ring
    rw [eL, eR] at c
    exact c

/-- the slack lower-bound term wl x N = (N+1)(1 − x²)(1 − x)² x^{N²} x^{2N} oddSq N -/
def wl (x : R) (N : Nat) : R :=
  K.mul (cN K (N + 1)) (K.mul (K.add K.one (K.neg (K.mul x x)))
    (K.mul (pwR K x (N * N)) (K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul (pwR K x (2 * N)) (oddSq K N)))))

/-- **slack lower bound** -/
theorem slack_low (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (N : Nat) :
    O.le (K.add (wl K x N) (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1)))
        (K.mul (K.add K.one (K.neg (K.mul x x))) (pwR K x ((N + 1) * (N + 1)))))))
      (K.mul (cN K (N + 1)) (K.add (pwR K x (N * N)) (K.neg (pwR K x ((N + 1 + 1) * (N + 1 + 1)))))) := by
  have q := ThetaBound3.sq_unit K O x hx0 hx1
  have h1x : O.le K.zero (K.add K.one (K.neg (K.mul x x))) := AltSum.le_sub K O _ _ q.2
  have r := resid K O h1 x hx0 hx1 N
  have r1 := O.mul_le_mul_right _ _ _ r (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 (N + 1))
    (O.mul_nonneg _ _ h1x (ThetaDup5.pw_nn K O x hx0 h1 (N * N))))
  have r2 := O.add_le_add _ _ _ _ r1 (O.le_refl (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1)))
        (K.mul (K.add K.one (K.neg (K.mul x x))) (pwR K x ((N + 1) * (N + 1)))))))
  have eL : K.add (K.mul (K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul (pwR K x (2 * N)) (oddSq K N)))
        (K.mul (cN K (N + 1)) (K.mul (K.add K.one (K.neg (K.mul x x))) (pwR K x (N * N)))))
        (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1)))
          (K.mul (K.add K.one (K.neg (K.mul x x))) (pwR K x ((N + 1) * (N + 1))))))
      = K.add (wl K x N) (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1)))
          (K.mul (K.add K.one (K.neg (K.mul x x))) (pwR K x ((N + 1) * (N + 1)))))) := by
    show _ = K.add (K.mul (cN K (N + 1)) (K.mul (K.add K.one (K.neg (K.mul x x)))
        (K.mul (pwR K x (N * N)) (K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul (pwR K x (2 * N)) (oddSq K N)))))) _
    generalize K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1)))
          (K.mul (K.add K.one (K.neg (K.mul x x))) (pwR K x ((N + 1) * (N + 1))))) = Z
    generalize cN K (N + 1) = c
    generalize pwR K x (N * N) = p
    generalize pwR K x (2 * N) = p2
    generalize oddSq K N = o
    lib_ring
  have eR : K.add (K.mul (K.add (gs K x (2 * N + 2)) (K.neg (K.mul (cN K (2 * N + 2)) (pwR K x (2 * N + 1)))))
        (K.mul (cN K (N + 1)) (K.mul (K.add K.one (K.neg (K.mul x x))) (pwR K x (N * N)))))
        (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1)))
          (K.mul (K.add K.one (K.neg (K.mul x x))) (pwR K x ((N + 1) * (N + 1))))))
      = K.mul (cN K (N + 1)) (K.add (pwR K x (N * N)) (K.neg (pwR K x ((N + 1 + 1) * (N + 1 + 1))))) := by
    have m := gs_mul K x (2 * N + 2)
    have e1 : K.mul (K.add (gs K x (2 * N + 2)) (K.neg (K.mul (cN K (2 * N + 2)) (pwR K x (2 * N + 1)))))
          (K.mul (cN K (N + 1)) (K.mul (K.add K.one (K.neg (K.mul x x))) (pwR K x (N * N))))
        = K.add (K.mul (K.mul (K.add K.one (K.neg (K.mul x x))) (gs K x (2 * N + 2))) (K.mul (cN K (N + 1)) (pwR K x (N * N))))
          (K.neg (K.mul (K.mul (cN K (2 * N + 2)) (pwR K x (2 * N + 1)))
            (K.mul (cN K (N + 1)) (K.mul (K.add K.one (K.neg (K.mul x x))) (pwR K x (N * N)))))) := by
      generalize gs K x (2 * N + 2) = g
      generalize cN K (2 * N + 2) = c2
      generalize pwR K x (2 * N + 1) = p1
      generalize cN K (N + 1) = c
      generalize pwR K x (N * N) = p
      lib_ring
    rw [e1, m, show 2 * N + 2 = (N + 1) + (N + 1) by lib_nat, SU2Char.cN_add K (N + 1),
      show (N + 1 + 1) * (N + 1 + 1) = N * N + 2 * ((N + 1) + (N + 1)) by lib_nat,
      GKS.pwR_add K x (N * N) (2 * ((N + 1) + (N + 1))),
      show (N + 1) * (N + 1) = N * N + (2 * N + 1) by lib_nat, GKS.pwR_add K x (N * N) (2 * N + 1)]
    show _ = K.mul (cN K (N + 1)) (K.add (pwR K x (N * N)) (K.neg (K.mul (pwR K x (N * N)) (pwR K x (2 * ((N + 1) + (N + 1)))))))
    generalize pwR K x (2 * ((N + 1) + (N + 1))) = p4
    generalize pwR K x (2 * N + 1) = p1
    generalize cN K (N + 1) = c
    generalize pwR K x (N * N) = p
    show K.add (K.add (K.mul (K.add K.one (K.neg p4)) (K.mul c p)) (K.neg (K.mul (K.mul (K.add c c) p1)
        (K.mul c (K.mul (K.add K.one (K.neg (K.mul x x))) p)))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul c c) (K.mul (K.add K.one (K.neg (K.mul x x))) (K.mul p p1))))
      = K.mul c (K.add p (K.neg (K.mul p p4)))
    lib_ring
  rw [eL, eR] at r2
  exact r2

/-- **variance bound with slack**: (1 − x²) V_N + (N+1) x^{N²} + N x^{(N+1)²} + Σ_{M<N} wl x M ≤ ϑ₃⁽ᴺ⁾(x) -/
def W (x : R) : Nat → R
  | 0 => K.zero
  | N + 1 => K.add (W x N) (wl K x N)

theorem var_low (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) : ∀ N,
    O.le (K.add (K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y N))
        (K.add (K.mul (cN K (N + 1)) (pwR K y (N * N))) (K.mul (cN K N) (pwR K y ((N + 1) * (N + 1)))))) (W K y N))
      (AltDiff.tU K (fun c => pwR K y (c * c)) N)
  | 0 => by
    have e : K.add (K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y 0))
        (K.add (K.mul (cN K (0 + 1)) (pwR K y (0 * 0))) (K.mul (cN K 0) (pwR K y ((0 + 1) * (0 + 1)))))) (W K y 0)
        = AltDiff.tU K (fun c => pwR K y (c * c)) 0 := by
      show K.add (K.add (K.mul (K.add K.one (K.neg (K.mul y y))) K.zero)
          (K.add (K.mul (K.add K.one K.zero) (pwR K y (0 * 0))) (K.mul K.zero (pwR K y ((0 + 1) * (0 + 1)))))) K.zero
          = pwR K y (0 * 0)
      generalize pwR K y (0 * 0) = p
      generalize pwR K y ((0 + 1) * (0 + 1)) = r
      lib_ring
    rw [e]; exact O.le_refl _
  | N + 1 => by
    have ih := var_low h1 y hy0 hy1 N
    have pr := slack_low K O h1 y hy0 hy1 N
    have s := O.add_le_add _ _ _ _ ih pr
    have s2 := O.add_le_add _ _ _ _ s (O.le_refl (K.add (K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))))
      (K.neg (K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1)))))))))
    have eL : K.add (K.add (K.add (K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y N))
          (K.add (K.mul (cN K (N + 1)) (pwR K y (N * N))) (K.mul (cN K N) (pwR K y ((N + 1) * (N + 1)))))) (W K y N))
          (K.add (wl K y N) (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1)))
            (K.mul (K.add K.one (K.neg (K.mul y y))) (pwR K y ((N + 1) * (N + 1))))))))
        (K.add (K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))))
          (K.neg (K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1))))))))
        = K.add (K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y (N + 1)))
          (K.add (K.mul (cN K (N + 1 + 1)) (pwR K y ((N + 1) * (N + 1))))
            (K.mul (cN K (N + 1)) (pwR K y ((N + 1 + 1) * (N + 1 + 1)))))) (W K y (N + 1)) := by
      show _ = K.add (K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (K.add (V K y N) (K.mul (K.add K.one K.one)
            (K.mul (K.mul (K.add K.one (cN K N)) (K.add K.one (cN K N))) (pwR K y ((N + 1) * (N + 1)))))))
          (K.add (K.mul (K.add K.one (K.add K.one (cN K N))) (pwR K y ((N + 1) * (N + 1))))
            (K.mul (K.add K.one (cN K N)) (pwR K y ((N + 1 + 1) * (N + 1 + 1)))))) (K.add (W K y N) (wl K y N))
      show K.add (K.add (K.add (K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y N))
          (K.add (K.mul (K.add K.one (cN K N)) (pwR K y (N * N))) (K.mul (cN K N) (pwR K y ((N + 1) * (N + 1)))))) (W K y N))
          (K.add (wl K y N) (K.mul (K.add K.one K.one) (K.mul (K.mul (K.add K.one (cN K N)) (K.add K.one (cN K N)))
            (K.mul (K.add K.one (K.neg (K.mul y y))) (pwR K y ((N + 1) * (N + 1))))))))
        (K.add (K.mul (K.add K.one K.one) (pwR K y ((N + 1) * (N + 1))))
          (K.neg (K.mul (K.add K.one (cN K N)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1))))))))
        = _
      generalize V K y N = v
      generalize W K y N = w
      generalize wl K y N = l
      generalize cN K N = c
      generalize pwR K y (N * N) = a
      generalize pwR K y ((N + 1) * (N + 1)) = b
      generalize pwR K y ((N + 1 + 1) * (N + 1 + 1)) = d
      lib_ring
    have eR : K.add (K.add (AltDiff.tU K (fun c => pwR K y (c * c)) N)
          (K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1)))))))
        (K.add (K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))))
          (K.neg (K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1))))))))
        = AltDiff.tU K (fun c => pwR K y (c * c)) (N + 1) := by
      show _ = K.add (AltDiff.tU K (fun c => pwR K y (c * c)) N) (K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))))
      generalize AltDiff.tU K (fun c => pwR K y (c * c)) N = T
      generalize K.mul (cN K (N + 1)) (K.add (pwR K y (N * N)) (K.neg (pwR K y ((N + 1 + 1) * (N + 1 + 1))))) = P
      generalize K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))) = Wv
      lib_ring
    rw [eL, eR] at s2
    exact s2

end GaussVar2

#print axioms GaussVar2.gs1_top
#print axioms GaussVar2.gs1_mul
#print axioms GaussVar2.gs1_low
#print axioms GaussVar2.one_sub
#print axioms GaussVar2.oddSq_nn
#print axioms GaussVar2.resid
#print axioms GaussVar2.slack_low
#print axioms GaussVar2.var_low

#print axioms: 8 theorems, no axioms.

90.38 A sharper lower bound for \(\vartheta _3\)

Bernoulli inside a power gives a lower bound for \(\vartheta _3\) within a few percent of its value.

\[ N(1-y)\le 1:\quad \sum _{|c|\le N}\bigl (1-|c|(1-y)\bigr )^{|c|}\le \vartheta _3^{(N)}(y) \]

ym/Th3Pow.lean

/-
  ym/Th3Pow — T45, G78m 6f-A′-1d: a sharper elementary lower bound for ϑ₃ (and for Gaussian weights).
    pw_pw     (y^a)^b = y^{ab}
    sq_low    0 ≤ y ≤ 1, k(1 − y) ≤ 1  ⇒  (1 − k(1 − y))^k ≤ y^{k²}          (Bernoulli inside, base monotone outside)
    tU_upto   termwise ≤ for c ≤ N ⇒ U_N ≤ U_N
    th3_pow   N(1 − y) ≤ 1  ⇒  U_N((1 − c(1 − y))^c) ≤ ϑ₃⁽ᴺ⁾(y)
  Numerically U_N((1 − c u)^c) with N = ⌊1/u⌋ is 92–98 % of ϑ₃ for s = −log y ∈ [0.005, 0.23]
  (ThetaLow.th3_low gives ≈ 43 %), which is what the size comparison of 6f-A′-1 needs.
  Lean 4 core only, no axioms.
-/
import ym.ThetaLow
import ym.AreaG
import ym.GKS

namespace Th3Pow

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- (y^a)^b = y^{ab} -/
theorem pw_pw (y : R) (a : Nat) : ∀ b, pwR K (pwR K y a) b = pwR K y (a * b)
  | 0 => rfl
  | b + 1 => by
    show K.mul (pwR K (pwR K y a) b) (pwR K y a) = pwR K y (a * b + a)
    rw [pw_pw y a b, GKS.pwR_add K y (a * b) a]

/-- **(1 − k(1 − y))^k ≤ y^{k²}** -/
theorem sq_low (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) (k : Nat)
    (hk : O.le K.zero (K.add K.one (K.neg (K.mul (cN K k) (K.add K.one (K.neg y)))))) :
    O.le (pwR K (K.add K.one (K.neg (K.mul (cN K k) (K.add K.one (K.neg y))))) k) (pwR K y (k * k)) := by
  have b := ThetaLow.bern K O h1 y hy0 hy1 k
  have m := AreaG.pw_base_mono K O h1 _ _ hk b k
  rw [pw_pw K y k k] at m
  exact m

/-- termwise ≤ on c ≤ N ⇒ U_N ≤ U_N -/
theorem tU_upto (h1 : O.le K.zero K.one) (f g : Nat → R) : ∀ N, (∀ c, c ≤ N → O.le (f c) (g c)) →
    O.le (AltDiff.tU K f N) (AltDiff.tU K g N)
  | 0, h => h 0 (Nat.le_refl 0)
  | N + 1, h => by
    have t := ThetaDup5.two_nn K O h1
    have ih := tU_upto h1 f g N (fun c hc => h c (Nat.le_succ_of_le hc))
    have m := O.mul_le_mul_right _ _ _ (h (N + 1) (Nat.le_refl _)) t
    rw [K.mul_comm (f (N + 1)), K.mul_comm (g (N + 1))] at m
    exact O.add_le_add _ _ _ _ ih m

/-- **ϑ₃ ≥ U_N((1 − c(1 − y))^c)** when N(1 − y) ≤ 1 -/
theorem th3_pow (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) (N : Nat)
    (hN : O.le K.zero (K.add K.one (K.neg (K.mul (cN K N) (K.add K.one (K.neg y)))))) :
    O.le (AltDiff.tU K (fun c => pwR K (K.add K.one (K.neg (K.mul (cN K c) (K.add K.one (K.neg y))))) c) N)
      (AltDiff.tU K (fun c => pwR K y (c * c)) N) := by
  have hu : O.le K.zero (K.add K.one (K.neg y)) := AltSum.le_sub K O _ _ hy1
  have key : ∀ c, c ≤ N → O.le (pwR K (K.add K.one (K.neg (K.mul (cN K c) (K.add K.one (K.neg y))))) c) (pwR K y (c * c)) := by
    intro c hc
    apply sq_low K O h1 y hy0 hy1 c
    have m := O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 c N hc) hu
    apply O.le_trans _ _ _ hN
    apply ThetaLow.le_of_sub K O
    rw [show K.add (K.add K.one (K.neg (K.mul (cN K c) (K.add K.one (K.neg y)))))
        (K.neg (K.add K.one (K.neg (K.mul (cN K N) (K.add K.one (K.neg y))))))
      = K.add (K.mul (cN K N) (K.add K.one (K.neg y))) (K.neg (K.mul (cN K c) (K.add K.one (K.neg y)))) by lib_ring]
    exact AltSum.le_sub K O _ _ m
  exact tU_upto K O h1 _ _ N key

end Th3Pow

#print axioms Th3Pow.pw_pw
#print axioms Th3Pow.sq_low
#print axioms Th3Pow.tU_upto
#print axioms Th3Pow.th3_pow

#print axioms: 4 theorems, no axioms.

90.39 Two comparisons for \(\vartheta _3\)

Squaring the argument lowers \(\vartheta _3\), and the Gaussian second moment is controlled by \(\vartheta _3\) itself.

\[ \vartheta _3(y^2)\le \vartheta _3(y),\qquad (1-y^2)\sum c^2y^{c^2}\le \vartheta _3(y) \]

ym/N0Aux.lean

/-
  ym/N0Aux — T45, G78m 6f-A′-1d (band step R1): two comparison facts that remove every ϑ₃ upper bound
  from the size comparison.
    th3_sq_le   0 ≤ y ≤ 1  ⇒  ϑ₃⁽ᴺ⁾(y²) ≤ ϑ₃⁽ᴺ⁾(y)
    mom_eq      U_N(c² y^{c²}) = V_N(y)  (GaussVar.V)
    mom_le      (1 − y²) U_N(c² y^{c²}) ≤ ϑ₃⁽ᴺ⁾(y)          (GaussVar.var_le, boundary ≥ 0)
  Lean 4 core only, no axioms.
-/
import ym.Th3Pow
import ym.GaussVar
import ym.ThetaBound3

namespace N0Aux

open YMChain LinkIntegral SU2Char GaussVar

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- ϑ₃(y²) ≤ ϑ₃(y) -/
theorem th3_sq_le (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) (N : Nat) :
    O.le (AltDiff.tU K (fun c => pwR K (K.mul y y) (c * c)) N) (AltDiff.tU K (fun c => pwR K y (c * c)) N) := by
  apply Th3Pow.tU_upto K O h1
  intro c _
  show O.le (pwR K (K.mul y y) (c * c)) (pwR K y (c * c))
  rw [ThetaBound.pw_sq K y (c * c)]
  exact ConeBound.pw_anti K O h1 y hy0 hy1 _ _ (by
    rw [show 2 * (c * c) = c * c + c * c by lib_nat]; exact Nat.le_add_right _ _)

/-- U_N(c² y^{c²}) = V_N(y) -/
theorem mom_eq (y : R) : ∀ N, AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K y (c * c))) N = V K y N
  | 0 => by
    show K.mul (cN K (0 * 0)) (pwR K y (0 * 0)) = K.zero
    show K.mul K.zero (pwR K y (0 * 0)) = K.zero
    generalize pwR K y (0 * 0) = p
    lib_ring
  | N + 1 => by
    show K.add (AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K y (c * c))) N)
        (K.mul (AltDiff.two K) (K.mul (cN K ((N + 1) * (N + 1))) (pwR K y ((N + 1) * (N + 1)))))
      = K.add (V K y N) (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1))) (pwR K y ((N + 1) * (N + 1)))))
    rw [mom_eq y N, SU2CharExp.cN_mul K (N + 1) (N + 1)]

/-- (1 − y²) U_N(c² y^{c²}) ≤ ϑ₃⁽ᴺ⁾(y) -/
theorem mom_le (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) (N : Nat) :
    O.le (K.mul (K.add K.one (K.neg (K.mul y y))) (AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K y (c * c))) N))
      (AltDiff.tU K (fun c => pwR K y (c * c)) N) := by
  rw [mom_eq K y N]
  have v := var_le K O h1 y hy0 hy1 N
  have b1 := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 (N + 1)) (ThetaDup5.pw_nn K O y hy0 h1 (N * N))
  have b2 := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 N) (ThetaDup5.pw_nn K O y hy0 h1 ((N + 1) * (N + 1)))
  have bb := O.add_le_add _ _ _ _ b1 b2
  rw [K.zero_add] at bb
  have t := O.add_le_add _ _ _ _ (O.le_refl (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y N))) bb
  rw [show K.add (K.mul (K.add K.one (K.neg (K.mul y y))) (V K y N)) K.zero
    = K.mul (K.add K.one (K.neg (K.mul y y))) (V K y N) by lib_ring] at t
  exact O.le_trans _ _ _ t v

end N0Aux

#print axioms N0Aux.th3_sq_le
#print axioms N0Aux.mom_eq
#print axioms N0Aux.mom_le

#print axioms: 3 theorems, no axioms.

90.40 \(\vartheta _3\) at squared arguments

Lower bounds for \(\vartheta _3(y^{2^j})\), \(j\le 3\), in terms of \(1-y\) alone.

\[ 1-z\le v,\ k\le N:\ (2k+1)(1-k^2v)\le \vartheta _3^{(N)}(z);\qquad 1-y^{2^j}\le 2^j(1-y) \]

ym/N0Band.lean

/-
  ym/N0Band — T45, G78m 6f-A′-1d (band step R1): lower bounds for ϑ₃ at y, y², y⁴, y⁸ in terms of u = 1 − y alone.
    sq_gap        0 ≤ y ≤ 1  ⇒  1 − y² ≤ 2(1 − y)
    th3_low_gap   1 − z ≤ v, k ≤ N  ⇒  (2k+1)(1 − k² v) ≤ ϑ₃⁽ᴺ⁾(z)
    gap4, gap8    1 − y⁴ ≤ 4(1 − y), 1 − y⁸ ≤ 8(1 − y)   (nested squares; with th3_low_gap: ϑ₃(y^{2^j}) ≥ (2k+1)(1 − 2^j k²(1 − y)))
  On the band (1 − y) ∈ [1/(4K²), 1/K²] the choice k = ⌊K/2^{j/2+1}⌋ makes every factor ≥ 3/4.
  Lean 4 core only, no axioms.
-/
import ym.ThetaLow
import ym.ThetaBound3

namespace N0Band

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- 1 − y² ≤ 2(1 − y) -/
theorem sq_gap (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) :
    O.le (K.add K.one (K.neg (K.mul y y))) (K.mul (cN K 2) (K.add K.one (K.neg y))) := by
  apply ThetaLow.le_of_sub K O
  have h := O.mul_nonneg _ _ (AltSum.le_sub K O _ _ hy1) (AltSum.le_sub K O _ _ hy1)
  rw [show K.add (K.mul (cN K 2) (K.add K.one (K.neg y))) (K.neg (K.add K.one (K.neg (K.mul y y))))
    = K.mul (K.add K.one (K.neg y)) (K.add K.one (K.neg y)) by
      show K.add (K.mul (K.add K.one (K.add K.one K.zero)) (K.add K.one (K.neg y))) (K.neg (K.add K.one (K.neg (K.mul y y)))) = _
      lib_ring]
  exact h

/-- (2k+1)(1 − k² v) ≤ ϑ₃(z) when 1 − z ≤ v -/
theorem th3_low_gap (h1 : O.le K.zero K.one) (z : R) (hz0 : O.le K.zero z) (hz1 : O.le z K.one) (v : R)
    (hv : O.le (K.add K.one (K.neg z)) v) (k N : Nat) (hk : k ≤ N) :
    O.le (K.mul (cN K (2 * k + 1)) (K.add K.one (K.neg (K.mul (cN K (k * k)) v))))
      (AltDiff.tU K (fun c => pwR K z (c * c)) N) := by
  have t := ThetaLow.th3_low K O h1 z hz0 hz1 k N hk
  apply O.le_trans _ _ _ _ t
  have m := O.mul_le_mul_right _ _ _ hv (SU2CharExp.cN_nn K O h1 (k * k))
  have d : O.le (K.add K.one (K.neg (K.mul (cN K (k * k)) v)))
      (K.add K.one (K.neg (K.mul (cN K (k * k)) (K.add K.one (K.neg z))))) := by
    apply ThetaLow.le_of_sub K O
    rw [show K.add (K.add K.one (K.neg (K.mul (cN K (k * k)) (K.add K.one (K.neg z)))))
        (K.neg (K.add K.one (K.neg (K.mul (cN K (k * k)) v))))
      = K.add (K.mul v (cN K (k * k))) (K.neg (K.mul (K.add K.one (K.neg z)) (cN K (k * k)))) by
        generalize cN K (k * k) = c; lib_ring]
    exact AltSum.le_sub K O _ _ m
  have e := O.mul_le_mul_right _ _ _ d (SU2CharExp.cN_nn K O h1 (2 * k + 1))
  rw [K.mul_comm _ (cN K (2 * k + 1)), K.mul_comm _ (cN K (2 * k + 1))] at e
  exact e

/-- 1 − y^{2^j} ≤ 2^j (1 − y), nested squares, j = 1, 2, 3 -/
theorem gap4 (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) :
    O.le (K.add K.one (K.neg (K.mul (K.mul y y) (K.mul y y)))) (K.mul (cN K 4) (K.add K.one (K.neg y))) := by
  have q := ThetaBound3.sq_unit K O y hy0 hy1
  have g2 := sq_gap K O y hy0 hy1
  have g4 := sq_gap K O (K.mul y y) q.1 q.2
  have c2 : O.le K.zero (cN K 2) := SU2CharExp.cN_nn K O (O.le_trans _ _ _ q.1 q.2) 2
  have m := O.mul_le_mul_right _ _ _ g2 c2
  apply O.le_trans _ _ _ g4
  rw [K.mul_comm (cN K 2) (K.add K.one (K.neg (K.mul y y)))]
  apply O.le_trans _ _ _ m
  rw [show K.mul (K.mul (cN K 2) (K.add K.one (K.neg y))) (cN K 2) = K.mul (cN K 4) (K.add K.one (K.neg y)) by
    show K.mul (K.mul (K.add K.one (K.add K.one K.zero)) (K.add K.one (K.neg y))) (K.add K.one (K.add K.one K.zero))
      = K.mul (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero)))) (K.add K.one (K.neg y))
    lib_ring]
  exact O.le_refl _

theorem gap8 (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) :
    O.le (K.add K.one (K.neg (K.mul (K.mul (K.mul y y) (K.mul y y)) (K.mul (K.mul y y) (K.mul y y)))))
      (K.mul (cN K 8) (K.add K.one (K.neg y))) := by
  have q := ThetaBound3.sq_unit K O y hy0 hy1
  have q4 := ThetaBound3.sq_unit K O (K.mul y y) q.1 q.2
  have g8 := sq_gap K O (K.mul (K.mul y y) (K.mul y y)) q4.1 q4.2
  have g4 := gap4 K O y hy0 hy1
  have c2 : O.le K.zero (cN K 2) := SU2CharExp.cN_nn K O (O.le_trans _ _ _ q.1 q.2) 2
  have m := O.mul_le_mul_right _ _ _ g4 c2
  apply O.le_trans _ _ _ g8
  rw [K.mul_comm (cN K 2) (K.add K.one (K.neg (K.mul (K.mul y y) (K.mul y y))))]
  apply O.le_trans _ _ _ m
  rw [show K.mul (K.mul (cN K 4) (K.add K.one (K.neg y))) (cN K 2) = K.mul (cN K 8) (K.add K.one (K.neg y)) by
    rw [show (8 : Nat) = 4 * 2 by rfl, SU2CharExp.cN_mul K 4 2]
    generalize cN K 4 = c4; generalize cN K 2 = c2; lib_ring]
  exact O.le_refl _

end N0Band

#print axioms N0Band.sq_gap
#print axioms N0Band.th3_low_gap
#print axioms N0Band.gap4
#print axioms N0Band.gap8

#print axioms: 4 theorems, no axioms.

90.41 Closed-form lower bounds for the slack

Each slack term has a Bernoulli lower bound, and the slack sum dominates each of its terms.

\[ (M+1)(1-x^2)(1-x)^2\bigl (1-(M^2+2M)(1-x)\bigr )\textstyle \sum _{i\le M}(2i+1)^2\le w_M(x)\le W_N(x)\quad (M<N) \]

ym/N0WLow.lean

/-
  ym/N0WLow — T45, G78m 6f-A′-1d (band step R1): closed-form lower bounds for the slack sum W of GaussVar2.
    wl_nn     0 ≤ wl x M
    W_mono    W x N ≤ W x (N + n)
    W_nn      0 ≤ W x N
    wl_low    (M+1)(1 − x²)(1 − x)² (1 − (M² + 2M)(1 − x)) oddSq M ≤ wl x M
    W_ge_wl   wl x M ≤ W x N  for M < N
  Lean 4 core only, no axioms.
-/
import ym.GaussVar2

namespace N0WLow

open YMChain LinkIntegral SU2Char GaussVar2

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem wl_nn (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (M : Nat) :
    O.le K.zero (wl K x M) := by
  have q := ThetaBound3.sq_unit K O x hx0 hx1
  have u := AltSum.le_sub K O _ _ hx1
  exact O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (O.mul_nonneg _ _ (AltSum.le_sub K O _ _ q.2)
    (O.mul_nonneg _ _ (ThetaDup5.pw_nn K O x hx0 h1 _) (O.mul_nonneg _ _ (O.mul_nonneg _ _ u u)
      (O.mul_nonneg _ _ (ThetaDup5.pw_nn K O x hx0 h1 _) (oddSq_nn K O h1 M)))))

theorem W_mono (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (N : Nat) :
    ∀ n, O.le (W K x N) (W K x (N + n))
  | 0 => O.le_refl _
  | n + 1 => by
    have ih := W_mono h1 x hx0 hx1 N n
    have := O.add_le_add _ _ _ _ ih (wl_nn K O h1 x hx0 hx1 (N + n))
    rw [show K.add (W K x N) K.zero = W K x N by lib_ring] at this
    exact this

theorem W_nn (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) : ∀ M, O.le K.zero (W K x M)
  | 0 => O.le_refl _
  | M + 1 => by
    have := O.add_le_add _ _ _ _ (W_nn h1 x hx0 hx1 M) (wl_nn K O h1 x hx0 hx1 M)
    rw [K.zero_add] at this
    exact this

theorem W_ge_wl (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (M n : Nat) :
    O.le (wl K x M) (W K x (M + 1 + n)) := by
  apply O.le_trans _ _ _ _ (W_mono K O h1 x hx0 hx1 (M + 1) n)
  have := O.add_le_add _ _ _ _ (W_nn K O h1 x hx0 hx1 M) (O.le_refl (wl K x M))
  rw [K.zero_add] at this
  exact this

/-- **Bernoulli lower bound for one slack term** -/
theorem wl_low (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (M : Nat) :
    O.le (K.mul (cN K (M + 1)) (K.mul (K.add K.one (K.neg (K.mul x x)))
        (K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x)))
          (K.mul (K.add K.one (K.neg (K.mul (cN K (M * M + 2 * M)) (K.add K.one (K.neg x))))) (oddSq K M)))))
      (wl K x M) := by
  have q := ThetaBound3.sq_unit K O x hx0 hx1
  have u := AltSum.le_sub K O _ _ hx1
  have b := ThetaLow.bern K O h1 x hx0 hx1 (M * M + 2 * M)
  rw [GKS.pwR_add K x (M * M) (2 * M)] at b
  have b2 := O.mul_le_mul_right _ _ _ b (oddSq_nn K O h1 M)
  have b3 := O.mul_le_mul_right _ _ _ b2 (O.mul_nonneg _ _ u u)
  have b4 := O.mul_le_mul_right _ _ _ b3 (AltSum.le_sub K O _ _ q.2)
  have b5 := O.mul_le_mul_right _ _ _ b4 (SU2CharExp.cN_nn K O h1 (M + 1))
  have eL : K.mul (K.mul (K.mul (K.mul (K.add K.one (K.neg (K.mul (cN K (M * M + 2 * M)) (K.add K.one (K.neg x))))) (oddSq K M))
        (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x)))) (K.add K.one (K.neg (K.mul x x)))) (cN K (M + 1))
      = K.mul (cN K (M + 1)) (K.mul (K.add K.one (K.neg (K.mul x x)))
        (K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x)))
          (K.mul (K.add K.one (K.neg (K.mul (cN K (M * M + 2 * M)) (K.add K.one (K.neg x))))) (oddSq K M)))) := by
    generalize K.add K.one (K.neg (K.mul (cN K (M * M + 2 * M)) (K.add K.one (K.neg x)))) = B
    generalize oddSq K M = o
    generalize cN K (M + 1) = c
    lib_ring
  have eR : K.mul (K.mul (K.mul (K.mul (K.mul (pwR K x (M * M)) (pwR K x (2 * M))) (oddSq K M))
        (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x)))) (K.add K.one (K.neg (K.mul x x)))) (cN K (M + 1))
      = wl K x M := by
    show _ = K.mul (cN K (M + 1)) (K.mul (K.add K.one (K.neg (K.mul x x)))
      (K.mul (pwR K x (M * M)) (K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul (pwR K x (2 * M)) (oddSq K M)))))
    generalize pwR K x (M * M) = p
    generalize pwR K x (2 * M) = p2
    generalize oddSq K M = o
    generalize cN K (M + 1) = c
    lib_ring
  rw [eL, eR] at b5
  exact b5

end N0WLow

#print axioms N0WLow.wl_nn
#print axioms N0WLow.W_mono
#print axioms N0WLow.W_nn
#print axioms N0WLow.W_ge_wl
#print axioms N0WLow.wl_low

#print axioms: 5 theorems, no axioms.

90.42 Summed slack lower bound

The slack sum dominates the sum of the Bernoulli lower bounds of its terms.

\[ \sum _{M<N}(M+1)(1-x^2)(1-x)^2\bigl (1-(M^2+2M)(1-x)\bigr )\textstyle \sum _{i\le M}(2i+1)^2\le W_N(x) \]

ym/N0WSum.lean

/-
  ym/N0WSum — T45, G78m 6f-A′-1d: the slack sum W dominates the sum of the Bernoulli lower bounds of its terms.
    wlow x M   = (M+1)(1 − x²)(1 − x)² (1 − (M² + 2M)(1 − x)) oddSq M
    Wlow x N   = Σ_{M<N} wlow x M
    W_ge_low   Wlow x N ≤ W x N        (N0WLow.wl_low termwise)
  Lean 4 core only, no axioms.
-/
import ym.N0WLow

namespace N0WSum

open YMChain LinkIntegral SU2Char GaussVar2

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

def wlow (x : R) (M : Nat) : R :=
  K.mul (cN K (M + 1)) (K.mul (K.add K.one (K.neg (K.mul x x)))
    (K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x)))
      (K.mul (K.add K.one (K.neg (K.mul (cN K (M * M + 2 * M)) (K.add K.one (K.neg x))))) (oddSq K M))))

def Wlow (x : R) : Nat → R
  | 0 => K.zero
  | N + 1 => K.add (Wlow x N) (wlow K x N)

theorem W_ge_low (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) : ∀ N,
    O.le (Wlow K x N) (W K x N)
  | 0 => O.le_refl _
  | N + 1 => O.add_le_add _ _ _ _ (W_ge_low h1 x hx0 hx1 N) (N0WLow.wl_low K O h1 x hx0 hx1 N)

end N0WSum

#print axioms N0WSum.W_ge_low

#print axioms: 1 theorem, no axioms.

90.43 Geometric decay of Gaussian weights

Elementary decay bounds that make the duplication box errors geometrically small.

\[ 0\le x\le 1:\quad x^n\bigl (1+n(1-x)\bigr )\le 1,\qquad x^{m^2}\bigl (1+m(1-x)\bigr )^m\le 1 \]

ym/N0Box.lean

/-
  ym/N0Box — T45, G78m 6f-A′-1d (band step R1): Gaussian weights decay geometrically, for the box errors.
    pw_inv     0 ≤ x ≤ 1  ⇒  x^n (1 + n(1 − x)) ≤ 1
    sq_decay   0 ≤ x ≤ 1  ⇒  x^{m²} (1 + m(1 − x))^m ≤ 1
  With m(1 − x) ≥ 2 the box errors (2N+1)·2x^{(N+1)²} of ThetaDup5.dup_err are ≤ (2N+1)·2·3^{−(N+1)}.
  Lean 4 core only, no axioms.
-/
import ym.Th3Pow
import ym.HeatClass
import ym.HeatDec

namespace N0Box

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- x^n (1 + n(1 − x)) ≤ 1 -/
theorem pw_inv (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) : ∀ n,
    O.le (K.mul (pwR K x n) (K.add K.one (K.mul (cN K n) (K.add K.one (K.neg x))))) K.one
  | 0 => by
    rw [show K.mul (pwR K x 0) (K.add K.one (K.mul (cN K 0) (K.add K.one (K.neg x)))) = K.one by
      show K.mul K.one (K.add K.one (K.mul K.zero (K.add K.one (K.neg x)))) = K.one
      lib_ring]
    exact O.le_refl _
  | n + 1 => by
    have ih := pw_inv h1 x hx0 hx1 n
    have u := AltSum.le_sub K O _ _ hx1
    have a := O.mul_le_mul_right _ _ _ ih hx0
    have p1 := HeatClass.pw_le_one K O x hx0 hx1 (n + 1)
    have b := O.mul_le_mul_right _ _ _ p1 u
    have c := O.add_le_add _ _ _ _ a b
    rw [show K.add (K.mul K.one x) (K.mul K.one (K.add K.one (K.neg x))) = K.one by lib_ring] at c
    apply O.le_trans _ _ _ _ c
    rw [show K.mul (pwR K x (n + 1)) (K.add K.one (K.mul (cN K (n + 1)) (K.add K.one (K.neg x))))
      = K.add (K.mul (K.mul (pwR K x n) (K.add K.one (K.mul (cN K n) (K.add K.one (K.neg x))))) x)
          (K.mul (pwR K x (n + 1)) (K.add K.one (K.neg x))) by
        show K.mul (K.mul (pwR K x n) x) (K.add K.one (K.mul (K.add K.one (cN K n)) (K.add K.one (K.neg x)))) = _
        show _ = K.add _ (K.mul (K.mul (pwR K x n) x) (K.add K.one (K.neg x)))
        generalize pwR K x n = p; generalize cN K n = c; lib_ring]
    exact O.le_refl _

/-- x^{m²} (1 + m(1 − x))^m ≤ 1 -/
theorem sq_decay (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (m : Nat) :
    O.le (K.mul (pwR K x (m * m)) (pwR K (K.add K.one (K.mul (cN K m) (K.add K.one (K.neg x)))) m)) K.one := by
  have i := pw_inv K O h1 x hx0 hx1 m
  have hp : O.le K.zero (K.mul (pwR K x m) (K.add K.one (K.mul (cN K m) (K.add K.one (K.neg x))))) :=
    O.mul_nonneg _ _ (ThetaDup5.pw_nn K O x hx0 h1 m)
      (by have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 m) (AltSum.le_sub K O _ _ hx1))
          rw [K.zero_add] at this; exact this)
  have m1 := AreaG.pw_base_mono K O h1 _ _ hp i m
  rw [HeatDec.pw_mul_pw, Th3Pow.pw_pw K x m m] at m1
  have e : pwR K K.one m = K.one := LinkIntegral.pw_one' K m
  rw [e] at m1
  exact m1

end N0Box

#print axioms N0Box.pw_inv
#print axioms N0Box.sq_decay

#print axioms: 2 theorems, no axioms.

90.44 Closing algebra for the first heat ratio

The scaled \(\vartheta _4\) bounds and one core inequality give the sign of the \(n=0\) lower bound.

\[ 4A_2^2A_4^2\beta '+(2+v)A_8^4\beta +2vA_2^2A_4^4A_8^4E\le S\,W\,v\ \Rightarrow \ W+(1+X)\,b'-b\ge 0 \]

ym/N0Close.lean

/-
  ym/N0Close — T45, G78m 6f-A′-1d: the closing algebra of the n = 0 size comparison (pure ordered-ring algebra).
  Inputs (A = ϑ₃(x), A₂ = ϑ₃(x²), A₄, A₈; X = x², v = 1 − X; b = ϑ₄(x), b′ = Σ(−1)^c c² x^{c²};
  a′ = Σ c² x^{c²}, U′ = Σ 2c² x^{2c²}; S = A A₂² A₄⁴ A₈⁴, β″ = A A₂ A₄² β′):
    S b ≤ A₈⁴ β                                     (ThetaBound3.th4_bound3 at x, times A₈⁴)
    −T ≤ S (A b′),  T = β″U′ + U′β″ + S E + a′ A₈⁴ β  (ThetaDBound3.th4d_scaled)
    v a′ ≤ A,  (1 − X²) U′ ≤ 2 A₂                    (N0Aux.mom_le at x and x²)
    core: 4 A₂²A₄² β′ + (2 + v) A₈⁴ β + 2v A₂²A₄⁴A₈⁴ E ≤ S · W · v   (i.e. A A₄² A₈⁴ W v ≳ 4β′ + 3β)
  Output:  0 ≤ W + (1 + X) b′ − b      (with N0Link.n0_low: Σ_{m<J} pairT x m ≥ 0, i.e. R_0 ≥ R_1)
  Proof: v (S A W − (1+X) T − A A₈⁴ β) = A·(core gap) + 2β″(2A₂ − (1−X²)U′) + S E v (1 − X)
         + A₈⁴ β ((2A − 2v a′) + v a′ (1 − X)), every bracket ≥ 0.
  Lean 4 core only, no axioms.
-/
import ym.ThetaLow
import ym.AltSum

namespace N0Close

open YMChain

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem nn_add (a b : R) (ha : O.le K.zero a) (hb : O.le K.zero b) : O.le K.zero (K.add a b) := by
  have := O.add_le_add _ _ _ _ ha hb
  rw [K.zero_add] at this
  exact this

theorem le_eq (a b : R) (h : a = b) : O.le a b := by rw [h]; exact O.le_refl _

theorem nn_sub (a b : R) (h : O.le b a) : O.le K.zero (K.add a (K.neg b)) := AltSum.le_sub K O _ _ h

/-- **closing step** -/
theorem close (A A2 A4 A8 W X b bp ap Up β β' E : R)
    (hA : O.le K.zero A) (hA2 : O.le K.zero A2) (hA4 : O.le K.zero A4) (hA8 : O.le K.zero A8)
    (hW : O.le K.zero W) (hX0 : O.le K.zero X) (hX1 : O.le X K.one)
    (hβ : O.le K.zero β) (hβ' : O.le K.zero β') (hE : O.le K.zero E) (hap : O.le K.zero ap) (hUp : O.le K.zero Up)
    (hm1 : O.le (K.mul (K.add K.one (K.neg X)) ap) A)
    (hm2 : O.le (K.mul (K.add K.one (K.neg (K.mul X X))) Up) (K.add A2 A2))
    (hb : O.le (K.mul (K.mul (K.mul A (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4)))) (K.mul (K.mul A8 A8) (K.mul A8 A8))) b)
      (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) β))
    (hd : O.le (K.neg (K.add (K.add (K.add
          (K.mul (K.mul (K.mul A (K.mul A2 (K.mul A4 A4))) β') Up) (K.mul Up (K.mul (K.mul A (K.mul A2 (K.mul A4 A4))) β')))
          (K.mul (K.mul (K.mul A (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4)))) (K.mul (K.mul A8 A8) (K.mul A8 A8))) E))
          (K.mul ap (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) β))))
        (K.mul (K.mul (K.mul A (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4)))) (K.mul (K.mul A8 A8) (K.mul A8 A8))) (K.mul A bp)))
    (hcore : O.le (K.add (K.add
          (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) β')))
          (K.mul (K.add (K.add K.one K.one) (K.add K.one (K.neg X))) (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) β)))
          (K.mul (K.add (K.add K.one (K.neg X)) (K.add K.one (K.neg X)))
            (K.mul (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4))) (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) E))))
        (K.mul (K.mul (K.mul A (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4)))) (K.mul (K.mul A8 A8) (K.mul A8 A8)))
          (K.mul W (K.add K.one (K.neg X)))))
    (hv : K.add K.one (K.neg X) ≠ K.zero)
    (hSA : K.mul (K.mul (K.mul A (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4)))) (K.mul (K.mul A8 A8) (K.mul A8 A8))) A ≠ K.zero) :
    O.le K.zero (K.add (K.add W (K.mul (K.add K.one X) bp)) (K.neg b)) := by
  have hv0 : O.le K.zero (K.add K.one (K.neg X)) := nn_sub K O _ _ hX1
  have h1X : O.le K.zero (K.add K.one X) := O.le_trans _ _ _ hv0 (by
    apply ThetaLow.le_of_sub K O
    rw [show K.add (K.add K.one X) (K.neg (K.add K.one (K.neg X))) = K.add X X by lib_ring]
    exact nn_add K O _ _ hX0 hX0)
  -- nonnegative atoms
  have p2 := O.mul_nonneg _ _ hA2 hA2
  have p4 := O.mul_nonneg _ _ (O.mul_nonneg _ _ hA4 hA4) (O.mul_nonneg _ _ hA4 hA4)
  have p8 := O.mul_nonneg _ _ (O.mul_nonneg _ _ hA8 hA8) (O.mul_nonneg _ _ hA8 hA8)
  have hS : O.le K.zero (K.mul (K.mul A (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4)))) (K.mul (K.mul A8 A8) (K.mul A8 A8))) :=
    O.mul_nonneg _ _ (O.mul_nonneg _ _ hA (O.mul_nonneg _ _ p2 p4)) p8
  have hB'' : O.le K.zero (K.mul (K.mul A (K.mul A2 (K.mul A4 A4))) β') :=
    O.mul_nonneg _ _ (O.mul_nonneg _ _ hA (O.mul_nonneg _ _ hA2 (O.mul_nonneg _ _ hA4 hA4))) hβ'
  -- the four brackets
  have k0 := O.mul_nonneg _ _ hA (nn_sub K O _ _ hcore)
  have k1 := O.mul_nonneg _ _ (nn_add K O _ _ hB'' hB'') (nn_sub K O _ _ hm2)
  have k2 := O.mul_nonneg _ _ (O.mul_nonneg _ _ hS hE) (O.mul_nonneg _ _ hv0 (nn_sub K O _ _ hX1))
  have k3 := O.mul_nonneg _ _ (O.mul_nonneg _ _ p8 hβ)
    (nn_add K O _ _ (nn_sub K O _ _ (O.add_le_add _ _ _ _ hm1 hm1)) (O.mul_nonneg _ _ (O.mul_nonneg _ _ hv0 hap) (nn_sub K O _ _ hX1)))
  have ksum := nn_add K O _ _ (nn_add K O _ _ k0 k1) (nn_add K O _ _ k2 k3)
  -- v · (S A W − (1+X) T − A A₈⁴ β) ≥ 0
  generalize hS' : K.mul (K.mul A (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4)))) (K.mul (K.mul A8 A8) (K.mul A8 A8)) = S at hb hd hSA hS k2 ksum hcore k0
  generalize hT : K.add (K.add (K.add
          (K.mul (K.mul (K.mul A (K.mul A2 (K.mul A4 A4))) β') Up) (K.mul Up (K.mul (K.mul A (K.mul A2 (K.mul A4 A4))) β')))
          (K.mul S E)) (K.mul ap (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) β)) = T at hd
  have eq : K.add (K.add (K.mul A (K.add (K.mul S
          (K.mul W (K.add K.one (K.neg X))))
        (K.neg (K.add (K.add
          (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) β')))
          (K.mul (K.add (K.add K.one K.one) (K.add K.one (K.neg X))) (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) β)))
          (K.mul (K.add (K.add K.one (K.neg X)) (K.add K.one (K.neg X)))
            (K.mul (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4))) (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) E)))))))
        (K.mul (K.add (K.mul (K.mul A (K.mul A2 (K.mul A4 A4))) β') (K.mul (K.mul A (K.mul A2 (K.mul A4 A4))) β'))
          (K.add (K.add A2 A2) (K.neg (K.mul (K.add K.one (K.neg (K.mul X X))) Up)))))
        (K.add (K.mul (K.mul S E) (K.mul (K.add K.one (K.neg X)) (K.add K.one (K.neg X))))
          (K.mul (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) β)
            (K.add (K.add (K.add A A) (K.neg (K.add (K.mul (K.add K.one (K.neg X)) ap) (K.mul (K.add K.one (K.neg X)) ap))))
              (K.mul (K.mul (K.add K.one (K.neg X)) ap) (K.add K.one (K.neg X))))))
      = K.mul (K.add K.one (K.neg X)) (K.add (K.add (K.mul (K.mul S A) W) (K.neg (K.mul (K.add K.one X) T)))
          (K.neg (K.mul A (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) β)))) := by
    rw [← hT, ← hS']
    lib_ring
  rw [eq] at ksum
  -- cancel v
  have r0 : O.le K.zero (K.add (K.add (K.mul (K.mul S A) W) (K.neg (K.mul (K.add K.one X) T)))
      (K.neg (K.mul A (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) β)))) := by
    apply O.pos_cancel _ _ _ hv0 hv
    exact O.le_trans _ _ _ (le_eq K O _ _ (by lib_ring)) (O.le_trans _ _ _ ksum (le_eq K O _ _ (by lib_ring)))
  -- S A Z ≥ r0-quantity
  have d1 := O.mul_le_mul_right _ _ _ hd h1X
  have d2 := O.mul_le_mul_right _ _ _ hb hA
  have tot := O.add_le_add _ _ _ _ r0 (O.add_le_add _ _ _ _ (nn_sub K O _ _ d1) (nn_sub K O _ _ d2))
  rw [K.zero_add, K.zero_add] at tot
  apply O.pos_cancel _ _ _ (O.mul_nonneg _ _ hS hA) hSA
  rw [show K.mul K.zero (K.mul S A) = K.zero by lib_ring]
  exact O.le_trans _ _ _ tot (le_eq K O _ _ (by lib_ring))

end N0Close

#print axioms N0Close.nn_add
#print axioms N0Close.le_eq
#print axioms N0Close.close

#print axioms: 3 theorems, no axioms.

90.45 The first heat ratio from one size inequality

With the actual theta sums inserted, the \(n=0\) monotonicity follows from a single explicit inequality.

\[ \mathrm {coreP}(x,2J+1)\ \Rightarrow \ \sum _{m<J}\mathrm {pair}_m(x)\ge 0 \]

ym/N0Inst.lean

/-
  ym/N0Inst — T45, G78m 6f-A′-1d: N0Close.close applied to the actual theta sums.
    Sc x N     = ϑ₃(x) ϑ₃(x²)² ϑ₃(x⁴)⁴ ϑ₃(x⁸)⁴           (the scale S)
    bet y N    = ((1 + B(y⁴))² + B(y²) ϑ₃(y⁴)²)² + B(y) C²  (the right side of ThetaBound3.th4_bound3)
    Ed x N     = (2N+1)·2Mn(N) x^{(N+1)²}                  (the box error of ThetaDBound3.th4d_scaled)
    coreP x N  : 4 ϑ₃(x²)²ϑ₃(x⁴)² bet(x²) + (2+v) ϑ₃(x⁸)⁴ bet(x) + 2v ϑ₃(x²)²ϑ₃(x⁴)⁴ϑ₃(x⁸)⁴ Ed ≤ Sc · W_N · v,  v = 1 − x²
    inst       coreP x N ⇒ 0 ≤ W_N + (1 + x²) T_N(c² x^{c²}) − ϑ₄⁽ᴺ⁾(x)
    n0_pos     coreP x (2J+1) ⇒ 0 ≤ Σ_{m<J} pairT x m        (R_0 ≥ R_1 at truncation J)
  (plus the non-degeneracy v ≠ 0, Sc·ϑ₃ ≠ 0).  What remains of 6f-A′-1 is the inequality coreP.
  Lean 4 core only, no axioms.
-/
import ym.N0Close
import ym.N0Link
import ym.N0Aux
import ym.N0WLow
import ym.ThetaDBound3

namespace N0Inst

open YMChain LinkIntegral SU2Char ThetaBound3

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- the scale S = ϑ₃(x) ϑ₃(x²)² ϑ₃(x⁴)⁴ ϑ₃(x⁸)⁴ -/
def Sc (x : R) (N : Nat) : R :=
  K.mul (K.mul (th3 K x N) (K.mul (K.mul (th3 K (K.mul x x) N) (th3 K (K.mul x x) N))
      (K.mul (K.mul (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N))
        (K.mul (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N)))))
    (K.mul (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N)
        (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N))
      (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N)
        (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N)))

/-- the right side of th4_bound3 -/
def bet (y : R) (N : Nat) : R :=
  K.add
    (K.mul
      (K.add (K.mul (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)) (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)))
        (K.mul (bx K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N))))
      (K.add (K.mul (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)) (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)))
        (K.mul (bx K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))))
    (K.mul (bx K y N)
      (K.mul (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))
             (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))))

/-- the box error of th4d_scaled -/
def Ed (x : R) (N : Nat) : R :=
  QBinom.sumN K (fun _ => K.mul (AltDiff.two K) (K.mul (cN K (ThetaDErr2.Mn N)) (pwR K x ((N + 1) * (N + 1))))) (2 * N + 1)

/-- the remaining size inequality -/
def coreP (x : R) (N : Nat) : Prop :=
  O.le (K.add (K.add
      (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one))
        (K.mul (K.mul (th3 K (K.mul x x) N) (th3 K (K.mul x x) N))
          (K.mul (K.mul (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N)) (bet K (K.mul x x) N))))
      (K.mul (K.add (K.add K.one K.one) (K.add K.one (K.neg (K.mul x x))))
        (K.mul (K.mul (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N)
            (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N))
          (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N)
            (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N))) (bet K x N))))
      (K.mul (K.add (K.add K.one (K.neg (K.mul x x))) (K.add K.one (K.neg (K.mul x x))))
        (K.mul (K.mul (K.mul (th3 K (K.mul x x) N) (th3 K (K.mul x x) N))
            (K.mul (K.mul (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N))
              (K.mul (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N))))
          (K.mul (K.mul (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N)
              (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N))
            (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N)
              (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N))) (Ed K x N)))))
    (K.mul (Sc K x N) (K.mul (GaussVar2.W K x N) (K.add K.one (K.neg (K.mul x x)))))

/-- scaled two-sided bound from a th4_bound3-type bound -/
theorem scale_bnd (h1 : O.le K.zero K.one) (c X β Bc : R) (hc : O.le K.zero c) (hl : O.le (K.neg Bc) X) (hu : O.le X β)
    (hB : O.le Bc β) : O.le (K.neg (K.mul c β)) (K.mul c X) ∧ O.le (K.mul c X) (K.mul c β) := by
  have nb : O.le (K.neg β) (K.neg Bc) := Signed.neg_le_neg K O _ _ hB
  exact ThetaDErr2.mul_bnd K O c β X hc ⟨O.le_trans _ _ _ nb hl, hu⟩

theorem bet_ge (y : R) (N : Nat) :
    O.le (K.mul (bx K y N)
      (K.mul (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))
             (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))))
      (bet K y N) := by
  have := O.add_le_add _ _ _ _ (ThetaBound.sq_nn K O
    (K.add (K.mul (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)) (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)))
        (K.mul (bx K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))))
    (O.le_refl (K.mul (bx K y N)
      (K.mul (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))
             (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N))))))
  rw [K.zero_add] at this
  exact this

/-- reshaping identities (pure algebra) -/
theorem resh1 (A A2 A4 A8 b : R) :
    K.mul (K.mul (K.mul A (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4)))) (K.mul (K.mul A8 A8) (K.mul A8 A8))) b
      = K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) (K.mul A (K.mul (K.mul (K.mul A2 (K.mul A4 A4)) (K.mul A2 (K.mul A4 A4))) b)) := by
  lib_ring

theorem resh2 (A A2 A4 A8 b : R) :
    K.mul (K.mul (K.mul A (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4)))) (K.mul (K.mul A8 A8) (K.mul A8 A8))) b
      = K.mul (K.mul A (K.mul A2 (K.mul A4 A4))) (K.mul A2 (K.mul (K.mul (K.mul A4 (K.mul A8 A8)) (K.mul A4 (K.mul A8 A8))) b)) := by
  lib_ring

theorem two_mul_tU (u : Nat → R) : ∀ N, AltDiff.tU K (fun c => K.mul (AltDiff.two K) (u c)) N = K.mul (AltDiff.two K) (AltDiff.tU K u N)
  | 0 => rfl
  | N + 1 => by
    show K.add (AltDiff.tU K (fun c => K.mul (AltDiff.two K) (u c)) N) (K.mul (AltDiff.two K) (K.mul (AltDiff.two K) (u (N + 1))))
      = K.mul (AltDiff.two K) (K.add (AltDiff.tU K u N) (K.mul (AltDiff.two K) (u (N + 1))))
    rw [two_mul_tU u N]
    generalize AltDiff.tU K u N = T
    generalize u (N + 1) = w
    generalize AltDiff.two K = t
    lib_ring

theorem tU_ext (f g : Nat → R) (h : ∀ c, f c = g c) : ∀ N, AltDiff.tU K f N = AltDiff.tU K g N
  | 0 => h 0
  | N + 1 => by
    show K.add (AltDiff.tU K f N) (K.mul (AltDiff.two K) (f (N + 1))) = K.add (AltDiff.tU K g N) (K.mul (AltDiff.two K) (g (N + 1)))
    rw [tU_ext f g h N, h (N + 1)]

/-- U′ = 2 Σ c² (x²)^{c²} -/
theorem Up_eq (x : R) (N : Nat) : AltDiff.tU K (ThetaDErr2.f2d K x) N
    = K.mul (AltDiff.two K) (AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K (K.mul x x) (c * c))) N) := by
  rw [← two_mul_tU K]
  apply tU_ext K
  intro c
  show K.mul (cN K (2 * (c * c))) (pwR K x (2 * (c * c))) = K.mul (AltDiff.two K) (K.mul (cN K (c * c)) (pwR K (K.mul x x) (c * c)))
  rw [ThetaBound.pw_sq K x (c * c), show 2 * (c * c) = c * c + c * c by lib_nat, SU2Char.cN_add K (c * c) (c * c)]
  show _ = K.mul (K.add K.one K.one) _
  generalize cN K (c * c) = q
  generalize pwR K x (c * c + c * c) = p
  lib_ring


/-- **instantiation**: coreP ⇒ 0 ≤ W + (1 + x²) b′ − b -/
theorem inst (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (N : Nat)
    (hcore : coreP K O x N) (hv : K.add K.one (K.neg (K.mul x x)) ≠ K.zero)
    (hSA : K.mul (Sc K x N) (th3 K x N) ≠ K.zero) :
    O.le K.zero (K.add (K.add (GaussVar2.W K x N)
        (K.mul (K.add K.one (K.mul x x)) (AltDiff.tS K (fun c => K.mul (cN K (c * c)) (pwR K x (c * c))) N)))
      (K.neg (AltDiff.tS K (fun c => pwR K x (c * c)) N))) := by
  have q2 := sq_unit K O x hx0 hx1
  have q4 := sq_unit K O (K.mul x x) q2.1 q2.2
  have q8 := sq_unit K O (K.mul (K.mul x x) (K.mul x x)) q4.1 q4.2
  have th3n : ∀ y, O.le K.zero y → O.le K.zero (th3 K y N) := fun y hy =>
    ThetaDBound.tU_nn K O h1 _ (fun c => ThetaDup5.pw_nn K O y hy h1 _) N
  have bxn : ∀ y, O.le K.zero y → O.le K.zero (bx K y N) := fun y hy =>
    ThetaBound.sumN_const_nn K O _ (O.mul_nonneg _ _ (ThetaDup5.two_nn K O h1) (ThetaDup5.pw_nn K O y hy h1 _)) _
  have betn : ∀ y, O.le K.zero y → O.le K.zero (bet K y N) := fun y hy =>
    O.le_trans _ _ _ (O.mul_nonneg _ _ (bxn y hy) (ThetaBound.sq_nn K O _)) (bet_ge K O y N)
  have hA := th3n x hx0
  have hA2 := th3n (K.mul x x) q2.1
  have hA4 := th3n (K.mul (K.mul x x) (K.mul x x)) q4.1
  have hA8 := th3n (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) q8.1
  have hE : O.le K.zero (Ed K x N) := ThetaBound.sumN_const_nn K O _
    (O.mul_nonneg _ _ (ThetaDup5.two_nn K O h1) (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (ThetaDup5.pw_nn K O x hx0 h1 _))) _
  have hW := N0WLow.W_nn K O h1 x hx0 hx1 N
  have hap : O.le K.zero (AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K x (c * c))) N) :=
    ThetaDBound.tU_nn K O h1 _ (fun c => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (ThetaDup5.pw_nn K O x hx0 h1 _)) N
  have hUp : O.le K.zero (AltDiff.tU K (ThetaDErr2.f2d K x) N) :=
    ThetaDBound.tU_nn K O h1 _ (fun c => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (ThetaDup5.pw_nn K O x hx0 h1 _)) N
  have hm1 : O.le (K.mul (K.add K.one (K.neg (K.mul x x))) (AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K x (c * c))) N))
      (th3 K x N) := N0Aux.mom_le K O h1 x hx0 hx1 N
  have hm2 : O.le (K.mul (K.add K.one (K.neg (K.mul (K.mul x x) (K.mul x x)))) (AltDiff.tU K (ThetaDErr2.f2d K x) N))
      (K.add (th3 K (K.mul x x) N) (th3 K (K.mul x x) N)) := by
    rw [Up_eq K x N]
    have m := N0Aux.mom_le K O h1 (K.mul x x) q2.1 q2.2 N
    change O.le _ (th3 K (K.mul x x) N) at m
    have mm := O.add_le_add _ _ _ _ m m
    generalize AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K (K.mul x x) (c * c))) N = M at mm ⊢
    generalize th3 K (K.mul x x) N = a2 at mm ⊢
    exact O.le_trans _ _ _ (N0Close.le_eq K O _ _ (by
      show K.mul (K.add K.one (K.neg (K.mul (K.mul x x) (K.mul x x)))) (K.mul (K.add K.one K.one) M)
        = K.add (K.mul (K.add K.one (K.neg (K.mul (K.mul x x) (K.mul x x)))) M) (K.mul (K.add K.one (K.neg (K.mul (K.mul x x) (K.mul x x)))) M)
      lib_ring)) mm
  -- scaled ϑ₄ bounds
  have t1 := th4_bound3 K O h1 x hx0 hx1 N
  have t2 := th4_bound3 K O h1 (K.mul x x) q2.1 q2.2 N
  have r1 := bet_ge K O x N
  have r2 := bet_ge K O (K.mul x x) N
  have c8 : O.le K.zero (K.mul (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N) (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N)) (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N) (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N))) :=
    O.mul_nonneg _ _ (O.mul_nonneg _ _ hA8 hA8) (O.mul_nonneg _ _ hA8 hA8)
  have c2 : O.le K.zero (K.mul (th3 K x N) (K.mul (th3 K (K.mul x x) N) (K.mul (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N)))) :=
    O.mul_nonneg _ _ hA (O.mul_nonneg _ _ hA2 (O.mul_nonneg _ _ hA4 hA4))
  have hb1 := scale_bnd K O h1 _ _ _ _ c8 t1.1 t1.2 r1
  have hb2 := scale_bnd K O h1 _ _ _ _ c2 t2.1 t2.2 r2
  rw [← resh1 K] at hb1
  rw [← resh2 K] at hb2
  have hb2eq : AltDiff.tS K (ThetaDup5.f2 K x) N = th4 K (K.mul x x) N :=
    ThetaBound.tS_ext K _ _ (fun c => (ThetaBound.pw_sq K x (c * c)).symm) N
  rw [← hb2eq] at hb2
  have hS : O.le K.zero (Sc K x N) := O.mul_nonneg _ _ (O.mul_nonneg _ _ hA
    (O.mul_nonneg _ _ (O.mul_nonneg _ _ hA2 hA2) (O.mul_nonneg _ _ (O.mul_nonneg _ _ hA4 hA4) (O.mul_nonneg _ _ hA4 hA4)))) c8
  have td := ThetaDBound3.th4d_scaled K O h1 x hx0 hx1 N (Sc K x N) _ _ hS (O.mul_nonneg _ _ c2 (betn _ q2.1)) hb1 hb2
  exact N0Close.close K O (th3 K x N) (th3 K (K.mul x x) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N) (GaussVar2.W K x N) (K.mul x x)
    (AltDiff.tS K (fun c => pwR K x (c * c)) N) (AltDiff.tS K (fun c => K.mul (cN K (c * c)) (pwR K x (c * c))) N)
    (AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K x (c * c))) N) (AltDiff.tU K (ThetaDErr2.f2d K x) N)
    (bet K x N) (bet K (K.mul x x) N) (Ed K x N)
    hA hA2 hA4 hA8 hW q2.1 q2.2 (betn x hx0) (betn _ q2.1) hE hap hUp hm1 hm2 hb1.2 td.1 hcore hv hSA

/-- **R_0 ≥ R_1 at truncation J, given the size inequality** -/
theorem n0_pos (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (J : Nat)
    (hcore : coreP K O x (2 * J + 1)) (hv : K.add K.one (K.neg (K.mul x x)) ≠ K.zero)
    (hSA : K.mul (Sc K x (2 * J + 1)) (th3 K x (2 * J + 1)) ≠ K.zero) :
    O.le K.zero (N0Split.sumT K x J) := by
  have i := inst K O h1 x hx0 hx1 (2 * J + 1) hcore hv hSA
  have l := N0Link.n0_low K O h1 x hx0 hx1 J
  have t := O.le_trans _ _ _ (O.le_trans _ _ _ i (N0Close.le_eq K O _ _ (by
    generalize GaussVar2.W K x (2 * J + 1) = w
    generalize AltDiff.tS K (fun c => K.mul (cN K (c * c)) (pwR K x (c * c))) (2 * J + 1) = bp
    generalize AltDiff.tS K (fun c => pwR K x (c * c)) (2 * J + 1) = b
    show _ = K.add w (K.add (K.mul (K.add K.one (K.mul x x)) bp) (K.neg b))
    lib_ring))) l
  generalize N0Split.sumT K x J = s at t
  apply N0Small.half_nn K O
  apply N0Small.half_nn K O
  exact O.le_trans _ _ _ t (N0Close.le_eq K O _ _ (by show K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) s = _; lib_ring))

end N0Inst

#print axioms N0Inst.scale_bnd
#print axioms N0Inst.bet_ge
#print axioms N0Inst.resh1
#print axioms N0Inst.resh2
#print axioms N0Inst.two_mul_tU
#print axioms N0Inst.tU_ext
#print axioms N0Inst.Up_eq
#print axioms N0Inst.inst
#print axioms N0Inst.n0_pos

#print axioms: 9 theorems, no axioms.

90.46 Scale-free form of the size inequality

Monotonicity of \(\vartheta _3\) under squaring removes the scale from the remaining inequality.

\[ 4\beta (x^2)+(2+v)\beta (x)+2vA_4^2A_8^4E\le A\,A_4^2A_8^4\,W\,v\ \Rightarrow \ \sum _{m<J}\mathrm {pair}_m(x)\ge 0 \]

ym/N0Core.lean

/-
  ym/N0Core — T45, G78m 6f-A′-1d: reduction of coreP to the scale-free inequality core2.
    mul_le2     0 ≤ a ≤ b, 0 ≤ c ≤ d ⇒ ac ≤ bd
    core_red    A₈ ≤ A₄ ≤ A₂, all ≥ 0, β ≥ 0, X ≤ 1, and
                core2: 4β′ + (2+v)β + 2v A₄²A₈⁴E ≤ A A₄² A₈⁴ · W v
                ⇒ 4A₂²A₄²β′ + (2+v)A₈⁴β + 2vA₂²A₄⁴A₈⁴E ≤ S · W v      (the coreP shape)
    coreP_of    core2 at the actual theta sums ⇒ N0Inst.coreP      (monotonicity N0Aux.th3_sq_le)
  Lean 4 core only, no axioms.
-/
import ym.N0Inst

namespace N0Core

open YMChain LinkIntegral SU2Char ThetaBound3

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem mul_le2 (a b c d : R) (ha : O.le K.zero a) (hc : O.le K.zero c) (hab : O.le a b) (hcd : O.le c d) :
    O.le (K.mul a c) (K.mul b d) := by
  have hb : O.le K.zero b := O.le_trans _ _ _ ha hab
  have m1 := O.mul_le_mul_right _ _ _ hab hc
  have m2 := O.mul_le_mul_right _ _ _ hcd hb
  rw [K.mul_comm d b, K.mul_comm c b] at m2
  exact O.le_trans _ _ _ m1 m2

theorem core_red (h1 : O.le K.zero K.one) (A A2 A4 A8 W X β β' E : R)
    (hA : O.le K.zero A) (hA8 : O.le K.zero A8) (h84 : O.le A8 A4) (h42 : O.le A4 A2)
    (hβ : O.le K.zero β) (hX1 : O.le X K.one)
    (h2 : O.le (K.add (K.add (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) β')
          (K.mul (K.add (K.add K.one K.one) (K.add K.one (K.neg X))) β))
          (K.mul (K.add (K.add K.one (K.neg X)) (K.add K.one (K.neg X))) (K.mul (K.mul A4 A4) (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) E))))
        (K.mul (K.mul A (K.mul (K.mul A4 A4) (K.mul (K.mul A8 A8) (K.mul A8 A8)))) (K.mul W (K.add K.one (K.neg X))))) :
    O.le (K.add (K.add
          (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) β')))
          (K.mul (K.add (K.add K.one K.one) (K.add K.one (K.neg X))) (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) β)))
          (K.mul (K.add (K.add K.one (K.neg X)) (K.add K.one (K.neg X)))
            (K.mul (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4))) (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) E))))
        (K.mul (K.mul (K.mul A (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4)))) (K.mul (K.mul A8 A8) (K.mul A8 A8)))
          (K.mul W (K.add K.one (K.neg X)))) := by
  have hA4 : O.le K.zero A4 := O.le_trans _ _ _ hA8 h84
  have hA2 : O.le K.zero A2 := O.le_trans _ _ _ hA4 h42
  have q24 : O.le K.zero (K.mul (K.mul A2 A2) (K.mul A4 A4)) := O.mul_nonneg _ _ (O.mul_nonneg _ _ hA2 hA2) (O.mul_nonneg _ _ hA4 hA4)
  -- A₈⁴ ≤ A₂² A₄²
  have p88 : O.le (K.mul A8 A8) (K.mul A2 A2) := mul_le2 K O _ _ _ _ hA8 hA8 (O.le_trans _ _ _ h84 h42) (O.le_trans _ _ _ h84 h42)
  have p84 : O.le (K.mul A8 A8) (K.mul A4 A4) := mul_le2 K O _ _ _ _ hA8 hA8 h84 h84
  have p8 : O.le (K.mul (K.mul A8 A8) (K.mul A8 A8)) (K.mul (K.mul A2 A2) (K.mul A4 A4)) :=
    mul_le2 K O _ _ _ _ (O.mul_nonneg _ _ hA8 hA8) (O.mul_nonneg _ _ hA8 hA8) p88 p84
  have hv : O.le K.zero (K.add K.one (K.neg X)) := AltSum.le_sub K O _ _ hX1
  have h2v : O.le K.zero (K.add (K.add K.one K.one) (K.add K.one (K.neg X))) :=
    N0Close.nn_add K O _ _ (N0Close.nn_add K O _ _ h1 h1) hv
  have d : O.le K.zero (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one (K.neg X))) β)
      (K.add (K.mul (K.mul A2 A2) (K.mul A4 A4)) (K.neg (K.mul (K.mul A8 A8) (K.mul A8 A8))))) :=
    O.mul_nonneg _ _ (O.mul_nonneg _ _ h2v hβ) (AltSum.le_sub K O _ _ p8)
  have m := O.mul_le_mul_right _ _ _ h2 q24
  have s1 := O.add_le_add _ _ _ _ (O.le_refl (K.add (K.add
          (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) β')))
          (K.mul (K.add (K.add K.one K.one) (K.add K.one (K.neg X))) (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) β)))
          (K.mul (K.add (K.add K.one (K.neg X)) (K.add K.one (K.neg X)))
            (K.mul (K.mul (K.mul A2 A2) (K.mul (K.mul A4 A4) (K.mul A4 A4))) (K.mul (K.mul (K.mul A8 A8) (K.mul A8 A8)) E))))) d
  rw [show ∀ z : R, K.add z K.zero = z from fun z => by lib_ring] at s1
  exact O.le_trans _ _ _ s1 (O.le_trans _ _ _ (N0Close.le_eq K O _ _ (by lib_ring)) (O.le_trans _ _ _ m (N0Close.le_eq K O _ _ (by lib_ring))))


/-- the scale-free size inequality -/
def core2P (x : R) (N : Nat) : Prop :=
  O.le (K.add (K.add (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (N0Inst.bet K (K.mul x x) N))
        (K.mul (K.add (K.add K.one K.one) (K.add K.one (K.neg (K.mul x x)))) (N0Inst.bet K x N)))
        (K.mul (K.add (K.add K.one (K.neg (K.mul x x))) (K.add K.one (K.neg (K.mul x x))))
          (K.mul (K.mul (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N)) (K.mul (K.mul (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N) (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N)) (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N) (th3 K (K.mul (K  -- (+89 chars; full line in the Zenodo source)
    (K.mul (K.mul (th3 K x N) (K.mul (K.mul (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N)) (K.mul (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N) (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N)) (K.mul (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N) (th  -- (+82 chars; full line in the Zenodo source)
      (K.mul (GaussVar2.W K x N) (K.add K.one (K.neg (K.mul x x)))))

theorem coreP_of (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (N : Nat)
    (h : core2P K O x N) : N0Inst.coreP K O x N := by
  have q2 := sq_unit K O x hx0 hx1
  have q4 := sq_unit K O (K.mul x x) q2.1 q2.2
  have q8 := sq_unit K O (K.mul (K.mul x x) (K.mul x x)) q4.1 q4.2
  have h84 : O.le (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N) := N0Aux.th3_sq_le K O h1 (K.mul (K.mul x x) (K.mul x x)) q4.1 q4.2 N
  have h42 : O.le (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul x x) N) := N0Aux.th3_sq_le K O h1 (K.mul x x) q2.1 q2.2 N
  have hA8 := ThetaDBound.tU_nn K O h1 _ (fun c => ThetaDup5.pw_nn K O (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) q8.1 h1 (c * c)) N
  have hA := ThetaDBound.tU_nn K O h1 _ (fun c => ThetaDup5.pw_nn K O x hx0 h1 (c * c)) N
  have bxn : ∀ y, O.le K.zero y → O.le K.zero (bx K y N) := fun y hy =>
    ThetaBound.sumN_const_nn K O _ (O.mul_nonneg _ _ (ThetaDup5.two_nn K O h1) (ThetaDup5.pw_nn K O y hy h1 _)) _
  have hβ : O.le K.zero (N0Inst.bet K x N) :=
    O.le_trans _ _ _ (O.mul_nonneg _ _ (bxn x hx0) (ThetaBound.sq_nn K O _)) (N0Inst.bet_ge K O x N)
  exact core_red K O h1 (th3 K x N) (th3 K (K.mul x x) N) (th3 K (K.mul (K.mul x x) (K.mul x x)) N) (th3 K (K.mul (K.mul (K.mul x x) (K.mul x x)) (K.mul (K.mul x x) (K.mul x x))) N) (GaussVar2.W K x N) (K.mul x x) (N0Inst.bet K x N) (N0Inst.bet K (K.mul x x) N) (N0Inst.Ed K x N)
    hA hA8 h84 h42 hβ q2.2 h

/-- **R_0 ≥ R_1 at truncation J from the scale-free inequality** -/
theorem n0_pos2 (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (J : Nat)
    (h : core2P K O x (2 * J + 1)) (hv : K.add K.one (K.neg (K.mul x x)) ≠ K.zero)
    (hSA : K.mul (N0Inst.Sc K x (2 * J + 1)) (th3 K x (2 * J + 1)) ≠ K.zero) :
    O.le K.zero (N0Split.sumT K x J) :=
  N0Inst.n0_pos K O h1 x hx0 hx1 J (coreP_of K O h1 x hx0 hx1 (2 * J + 1) h) hv hSA

end N0Core

#print axioms N0Core.mul_le2
#print axioms N0Core.core_red
#print axioms N0Core.coreP_of
#print axioms N0Core.n0_pos2

#print axioms: 4 theorems, no axioms.

90.47 Box-error bounds for the size inequality

Crude upper bounds that make the duplication box errors harmless for large truncation.

\[ \vartheta _3^{(N)}(y)\le 2N+1,\qquad \beta (y)\le \bigl ((1+b_0)^2+b_0T^2\bigr )^2+b_0T^6 \]

ym/N0Bet.lean

/-
  ym/N0Bet — T45, G78m 6f-A′-1d: upper bounds for the box-error quantities of core2P.
    th3_le    0 ≤ y ≤ 1  ⇒  ϑ₃⁽ᴺ⁾(y) ≤ 2N + 1
    bet_le    B(y), B(y²), B(y⁴) ≤ b₀ and ϑ₃⁽ᴺ⁾(y²), ϑ₃⁽ᴺ⁾(y⁴) ≤ T  ⇒  bet y ≤ ((1 + b₀)² + b₀T²)² + b₀T⁶
  Lean 4 core only, no axioms.
-/
import ym.N0Core

namespace N0Bet

open YMChain LinkIntegral SU2Char ThetaBound3

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem th3_le (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) : ∀ N,
    O.le (th3 K y N) (cN K (2 * N + 1))
  | 0 => by
    show O.le (pwR K y (0 * 0)) (K.add K.one K.zero)
    rw [show K.add K.one K.zero = K.one by lib_ring]
    exact HeatClass.pw_le_one K O y hy0 hy1 _
  | N + 1 => by
    have ih := th3_le h1 y hy0 hy1 N
    have p := HeatClass.pw_le_one K O y hy0 hy1 ((N + 1) * (N + 1))
    have t := O.mul_le_mul_right _ _ _ p (ThetaDup5.two_nn K O h1)
    have s := O.add_le_add _ _ _ _ ih t
    show O.le (K.add (th3 K y N) (K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1))))) _
    rw [K.mul_comm (AltDiff.two K)]
    apply O.le_trans _ _ _ s
    rw [show 2 * (N + 1) + 1 = (2 * N + 1) + 1 + 1 by lib_nat]
    exact N0Close.le_eq K O _ _ (by
      show K.add (cN K (2 * N + 1)) (K.mul K.one (K.add K.one K.one)) = K.add K.one (K.add K.one (cN K (2 * N + 1)))
      generalize cN K (2 * N + 1) = c
      lib_ring)

theorem bet_le (y b0 T : R) (N : Nat) (hb0 : O.le K.zero b0)
    (hB : O.le K.zero (bx K y N)) (hB2 : O.le K.zero (bx K (K.mul y y) N)) (hB4 : O.le K.zero (bx K (K.mul (K.mul y y) (K.mul y y)) N))
    (hA2 : O.le K.zero (th3 K (K.mul y y) N)) (hA4 : O.le K.zero (th3 K (K.mul (K.mul y y) (K.mul y y)) N))
    (uB : O.le (bx K y N) b0) (uB2 : O.le (bx K (K.mul y y) N) b0) (uB4 : O.le (bx K (K.mul (K.mul y y) (K.mul y y)) N) b0)
    (uA2 : O.le (th3 K (K.mul y y) N) T) (uA4 : O.le (th3 K (K.mul (K.mul y y) (K.mul y y)) N) T) (h1 : O.le K.zero K.one) :
    O.le (N0Inst.bet K y N)
      (K.add (K.mul (K.add (K.mul (K.add K.one b0) (K.add K.one b0)) (K.mul b0 (K.mul T T)))
                    (K.add (K.mul (K.add K.one b0) (K.add K.one b0)) (K.mul b0 (K.mul T T))))
        (K.mul b0 (K.mul (K.mul T (K.mul T T)) (K.mul T (K.mul T T))))) := by
  have m := N0Core.mul_le2 K O
  have n1B4 := N0Close.nn_add K O _ _ h1 hB4
  have l1 := N0Close.nn_add K O _ _ h1 hb0
  have u1 : O.le (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)) (K.add K.one b0) := O.add_le_add _ _ _ _ (O.le_refl _) uB4
  have q1 := m _ _ _ _ n1B4 n1B4 u1 u1
  have a44 := m _ _ _ _ hA4 hA4 uA4 uA4
  have q2 := m _ _ _ _ hB2 (O.mul_nonneg _ _ hA4 hA4) uB2 a44
  have Pn := N0Close.nn_add K O _ _ (O.mul_nonneg _ _ n1B4 n1B4) (O.mul_nonneg _ _ hB2 (O.mul_nonneg _ _ hA4 hA4))
  have Pu := O.add_le_add _ _ _ _ q1 q2
  have PP := m _ _ _ _ Pn Pn Pu Pu
  have hC := O.mul_nonneg _ _ hA2 (O.mul_nonneg _ _ hA4 hA4)
  have Cu := m _ _ _ _ hA2 (O.mul_nonneg _ _ hA4 hA4) uA2 a44
  have CC := m _ _ _ _ hC hC Cu Cu
  have BC := m _ _ _ _ hB (O.mul_nonneg _ _ hC hC) uB CC
  exact O.add_le_add _ _ _ _ PP BC

end N0Bet

#print axioms N0Bet.th3_le
#print axioms N0Bet.bet_le

#print axioms: 2 theorems, no axioms.

90.48 Three duplications for \(\vartheta _4\)

Iterating the duplication identity three times bounds \(\vartheta _4\) by a product of \(\vartheta _3\) values.

\[ -B(y)C^2\le \vartheta _3(y)\,C^2\,\vartheta _4(y)\le \beta _2^2+B(y)C^2,\qquad C=\vartheta _3(y^2)\vartheta _3(y^4)^2 \]

ym/ThetaBound3.lean

/-
  ym/ThetaBound3 — T45, G78m 6f-A′-1 (amplification): ϑ₄ is small, by three duplications.
  With a = ϑ₃⁽ᴺ⁾(y), b = ϑ₄⁽ᴺ⁾(y), B(y) = (2N+1)·2 y^{(N+1)²} (the box error of ThetaDup5.dup_err):
    dup_step   |c · ϑ₄⁽ᴺ⁾(y²)| ≤ β  ⇒  −B(y) c² ≤ a · c² · b ≤ β² + B(y) c²       (one duplication, any scale c)
    th4_bound3 with y₂ = y², y₄ = y⁴, C = ϑ₃(y₂) ϑ₃(y₄)², β₁ = 1 + B(y₄), β₂ = β₁² + B(y₂) ϑ₃(y₄)²:
               −B(y) C² ≤ ϑ₃(y) C² ϑ₄(y) ≤ β₂² + B(y) C²
  i.e. |ϑ₄(y)| ≲ 1/(ϑ₃(y) ϑ₃(y²)² ϑ₃(y⁴)⁴) = O((1 − y)^{7/2}) (ThetaBound.th4_bound is the two-step case).
  Lean 4 core only, no axioms.
-/
import ym.ThetaBound

namespace ThetaBound3

open YMChain LinkIntegral SU2Char ThetaDup5 ThetaBound

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- ϑ₃⁽ᴺ⁾(y) = U_N(y^{c²}) -/
def th3 (y : R) (N : Nat) : R := AltDiff.tU K (fun c => pwR K y (c * c)) N

/-- ϑ₄⁽ᴺ⁾(y) = T_N(y^{c²}) -/
def th4 (y : R) (N : Nat) : R := AltDiff.tS K (fun c => pwR K y (c * c)) N

/-- box error (2N+1)·2 y^{(N+1)²} -/
def bx (y : R) (N : Nat) : R :=
  QBinom.sumN K (fun _ => K.mul (AltDiff.two K) (pwR K y ((N + 1) * (N + 1)))) (2 * N + 1)

/-- 0 ≤ y ≤ 1 ⇒ 0 ≤ y² ≤ 1 -/
theorem sq_unit (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) :
    O.le K.zero (K.mul y y) ∧ O.le (K.mul y y) K.one := by
  refine ⟨O.mul_nonneg _ _ hy0 hy0, ?_⟩
  have := O.mul_le_mul_right _ _ _ hy1 hy0
  rw [K.one_mul] at this
  exact O.le_trans _ _ _ this hy1

/-- **one duplication at scale c** -/
theorem dup_step (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) (N : Nat)
    (c β : R) (hc : O.le (K.neg β) (K.mul c (th4 K (K.mul y y) N)) ∧ O.le (K.mul c (th4 K (K.mul y y) N)) β) :
    O.le (K.neg (K.mul (bx K y N) (K.mul c c))) (K.mul (th3 K y N) (K.mul (K.mul c c) (th4 K y N)))
    ∧ O.le (K.mul (th3 K y N) (K.mul (K.mul c c) (th4 K y N))) (K.add (K.mul β β) (K.mul (bx K y N) (K.mul c c))) := by
  obtain ⟨E, e, l, u⟩ := dup_err K O h1 y hy0 hy1 N
  have hb2 : AltDiff.tS K (f2 K y) N = th4 K (K.mul y y) N :=
    tS_ext K _ _ (fun c => (pw_sq K y (c * c)).symm) N
  rw [hb2] at e
  change K.mul (th3 K y N) (th4 K y N) = K.add (K.mul (th4 K (K.mul y y) N) (th4 K (K.mul y y) N)) E at e
  change O.le (K.neg (bx K y N)) E at l
  change O.le E (bx K y N) at u
  have hX := sq_le K O _ _ hc
  have nX := sq_nn K O (K.mul c (th4 K (K.mul y y) N))
  have hcc := sq_nn K O c
  have m1 := O.mul_le_mul_right _ _ _ u hcc
  have m0 := O.mul_le_mul_right _ _ _ l hcc
  generalize th3 K y N = a at e ⊢
  generalize th4 K y N = b at e ⊢
  generalize th4 K (K.mul y y) N = b2 at e hX nX ⊢
  generalize bx K y N = B at m0 m1 ⊢
  have key : K.mul a (K.mul (K.mul c c) b) = K.add (K.mul (K.mul c b2) (K.mul c b2)) (K.mul E (K.mul c c)) := by
    have s : K.mul a (K.mul (K.mul c c) b) = K.mul (K.mul c c) (K.mul a b) := by lib_ring
    rw [s, e]
    lib_ring
  rw [key]
  constructor
  · have := O.add_le_add _ _ _ _ nX m0
    rw [K.zero_add, show K.mul (K.neg B) (K.mul c c) = K.neg (K.mul B (K.mul c c)) by lib_ring] at this
    exact this
  · exact O.add_le_add _ _ _ _ hX m1

/-- **ϑ₄ is small, three duplications** -/
theorem th4_bound3 (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) (N : Nat) :
    O.le (K.neg (K.mul (bx K y N)
        (K.mul (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))
               (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N))))))
      (K.mul (th3 K y N)
        (K.mul (K.mul (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))
               (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N))))
          (th4 K y N)))
    ∧ O.le (K.mul (th3 K y N)
        (K.mul (K.mul (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))
               (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N))))
          (th4 K y N)))
      (K.add
        (K.mul
          (K.add (K.mul (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)) (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)))
            (K.mul (bx K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N))))
          (K.add (K.mul (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)) (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)))
            (K.mul (bx K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))))
        (K.mul (bx K y N)
          (K.mul (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))
                 (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))))) := by
  have q2 := sq_unit K O y hy0 hy1
  have q4 := sq_unit K O (K.mul y y) q2.1 q2.2
  have q8 := sq_unit K O (K.mul (K.mul y y) (K.mul y y)) q4.1 q4.2
  -- level 0: |ϑ₄(y⁸)| ≤ 1
  have hdec : ∀ c, O.le (pwR K (K.mul (K.mul (K.mul y y) (K.mul y y)) (K.mul (K.mul y y) (K.mul y y))) ((c + 1) * (c + 1)))
      (pwR K (K.mul (K.mul (K.mul y y) (K.mul y y)) (K.mul (K.mul y y) (K.mul y y))) (c * c)) := fun c =>
    ConeBound.pw_anti K O h1 _ q8.1 q8.2 _ _ (Nat.mul_le_mul (Nat.le_succ c) (Nat.le_succ c))
  have u8 := th4_unit K O h1 (fun c => pwR K (K.mul (K.mul (K.mul y y) (K.mul y y)) (K.mul (K.mul y y) (K.mul y y))) (c * c))
    hdec (fun c => pw_nn K O _ q8.1 h1 _) rfl N
  change O.le (K.neg K.one) (th4 K (K.mul (K.mul (K.mul y y) (K.mul y y)) (K.mul (K.mul y y) (K.mul y y))) N)
    ∧ O.le (th4 K (K.mul (K.mul (K.mul y y) (K.mul y y)) (K.mul (K.mul y y) (K.mul y y))) N) K.one at u8
  -- level 1 at y⁴, scale 1
  have hc1 : O.le (K.neg K.one) (K.mul K.one (th4 K (K.mul (K.mul (K.mul y y) (K.mul y y)) (K.mul (K.mul y y) (K.mul y y))) N))
      ∧ O.le (K.mul K.one (th4 K (K.mul (K.mul (K.mul y y) (K.mul y y)) (K.mul (K.mul y y) (K.mul y y))) N)) K.one := by
    rw [K.one_mul]; exact u8
  have s1 := dup_step K O h1 (K.mul (K.mul y y) (K.mul y y)) q4.1 q4.2 N K.one K.one hc1
  have hc2 : O.le (K.neg (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)))
        (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th4 K (K.mul (K.mul y y) (K.mul y y)) N))
      ∧ O.le (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th4 K (K.mul (K.mul y y) (K.mul y y)) N))
        (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)) := by
    have l := s1.1
    have u := s1.2
    generalize th3 K (K.mul (K.mul y y) (K.mul y y)) N = a4 at l u ⊢
    generalize th4 K (K.mul (K.mul y y) (K.mul y y)) N = b4 at l u ⊢
    generalize bx K (K.mul (K.mul y y) (K.mul y y)) N = B4 at l u ⊢
    rw [show K.mul a4 (K.mul (K.mul K.one K.one) b4) = K.mul a4 b4 by lib_ring,
      show K.neg (K.mul B4 (K.mul K.one K.one)) = K.neg B4 by lib_ring] at l
    rw [show K.mul a4 (K.mul (K.mul K.one K.one) b4) = K.mul a4 b4 by lib_ring,
      show K.add (K.mul K.one K.one) (K.mul B4 (K.mul K.one K.one)) = K.add K.one B4 by lib_ring] at u
    refine ⟨O.le_trans _ _ _ ?_ l, u⟩
    apply ThetaLow.le_of_sub K O
    rw [show K.add (K.neg B4) (K.neg (K.neg (K.add K.one B4))) = K.one by lib_ring]
    exact h1
  -- level 2 at y², scale ϑ₃(y⁴)
  have s2 := dup_step K O h1 (K.mul y y) q2.1 q2.2 N _ _ hc2
  have hc3 : O.le (K.neg (K.add (K.mul (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)) (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)))
            (K.mul (bx K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))))
        (K.mul (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))
          (th4 K (K.mul y y) N))
      ∧ O.le (K.mul (K.mul (th3 K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))
          (th4 K (K.mul y y) N))
        (K.add (K.mul (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)) (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N)))
            (K.mul (bx K (K.mul y y) N) (K.mul (th3 K (K.mul (K.mul y y) (K.mul y y)) N) (th3 K (K.mul (K.mul y y) (K.mul y y)) N)))) := by
    have l := s2.1
    have u := s2.2
    have nb := sq_nn K O (K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N))
    generalize K.add K.one (bx K (K.mul (K.mul y y) (K.mul y y)) N) = b1 at l u nb ⊢
    generalize th3 K (K.mul y y) N = a2 at l u ⊢
    generalize th4 K (K.mul y y) N = b2 at l u ⊢
    generalize th3 K (K.mul (K.mul y y) (K.mul y y)) N = a4 at l u ⊢
    generalize bx K (K.mul y y) N = B2 at l u ⊢
    rw [show K.mul a2 (K.mul (K.mul a4 a4) b2) = K.mul (K.mul a2 (K.mul a4 a4)) b2 by lib_ring] at l u
    refine ⟨O.le_trans _ _ _ ?_ l, u⟩
    apply ThetaLow.le_of_sub K O
    rw [show K.add (K.neg (K.mul B2 (K.mul a4 a4))) (K.neg (K.neg (K.add (K.mul b1 b1) (K.mul B2 (K.mul a4 a4)))))
      = K.mul b1 b1 by lib_ring]
    exact nb
  -- level 3 at y
  exact dup_step K O h1 y hy0 hy1 N _ _ hc3

end ThetaBound3

#print axioms ThetaBound3.sq_unit
#print axioms ThetaBound3.dup_step
#print axioms ThetaBound3.th4_bound3

#print axioms: 3 theorems, no axioms.

90.49 Scaled bound for the \(\vartheta _4\) second moment

The alternating second moment is bounded with the same scale as the \(\vartheta _4\) bounds.

\[ |Sb|\le \beta _1,\ |Sb_2|\le \beta _2\ \Rightarrow \ \Bigl |S\,\vartheta _3\sum (-1)^c c^2y^{c^2}\Bigr |\le 2\beta _2U'+SE'+a'\beta _1 \]

ym/ThetaDBound3.lean

/-
  ym/ThetaDBound3 — T45, G78m 6f-A′-1 (amplification for ϑ₄″-type sums): the scaled form of ThetaDBound.th4d_bound.
  The ϑ₄ bounds of ThetaBound3 come with a scale (|C·ϑ₄| ≲ β, no division), so the derivative bound is restated
  with the same scale S ≥ 0:
    th4d_scaled   |S b| ≤ β₁, |S b₂| ≤ β₂  ⇒  |S · a · b′| ≤ β₂U′ + U′β₂ + S E′ + a′β₁
  where a = ϑ₃⁽ᴺ⁾(y), b = ϑ₄⁽ᴺ⁾(y), b₂ = ϑ₄⁽ᴺ⁾(y²), a′ = U_N(c²y^{c²}), b′ = T_N(c²y^{c²}) = Σ(−1)^c c² y^{c²},
  U′ = U_N(2c² y^{2c²}), E′ = (2N+1)·2Mn(N) y^{(N+1)²}.  S = 1 gives back th4d_bound.
  Lean 4 core only, no axioms.
-/
import ym.ThetaDBound

namespace ThetaDBound3

open YMChain LinkIntegral SU2Char ThetaDup5 ThetaDErr2 ThetaDeriv ThetaDErr3 ThetaDBound

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- **scaled ϑ₄′ bound** -/
theorem th4d_scaled (h1 : O.le K.zero K.one) (y : R) (hy0 : O.le K.zero y) (hy1 : O.le y K.one) (N : Nat)
    (S β1 β2 : R) (hS : O.le K.zero S) (hβ2 : O.le K.zero β2)
    (hb : O.le (K.neg β1) (K.mul S (AltDiff.tS K (fun c => pwR K y (c * c)) N))
      ∧ O.le (K.mul S (AltDiff.tS K (fun c => pwR K y (c * c)) N)) β1)
    (hb2 : O.le (K.neg β2) (K.mul S (AltDiff.tS K (f2 K y) N)) ∧ O.le (K.mul S (AltDiff.tS K (f2 K y) N)) β2) :
    O.le (K.neg (K.add (K.add (K.add (K.mul β2 (AltDiff.tU K (f2d K y) N)) (K.mul (AltDiff.tU K (f2d K y) N) β2))
          (K.mul S (QBinom.sumN K (fun _ => K.mul (AltDiff.two K) (K.mul (cN K (Mn N)) (pwR K y ((N + 1) * (N + 1))))) (2 * N + 1))))
        (K.mul (AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K y (c * c))) N) β1)))
      (K.mul S (K.mul (AltDiff.tU K (fun c => pwR K y (c * c)) N) (AltDiff.tS K (fun c => K.mul (cN K (c * c)) (pwR K y (c * c))) N)))
    ∧ O.le (K.mul S (K.mul (AltDiff.tU K (fun c => pwR K y (c * c)) N) (AltDiff.tS K (fun c => K.mul (cN K (c * c)) (pwR K y (c * c))) N)))
      (K.add (K.add (K.add (K.mul β2 (AltDiff.tU K (f2d K y) N)) (K.mul (AltDiff.tU K (f2d K y) N) β2))
          (K.mul S (QBinom.sumN K (fun _ => K.mul (AltDiff.two K) (K.mul (cN K (Mn N)) (pwR K y ((N + 1) * (N + 1))))) (2 * N + 1))))
        (K.mul (AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K y (c * c))) N) β1)) := by
  have e := dup_deriv K y N
  have dE := derr K O h1 y hy0 hy1 N
  have hnn' : ∀ c, O.le K.zero (f2d K y c) := fun c =>
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (pw_nn K O y hy0 h1 _)
  have hU' := tU_nn K O h1 (f2d K y) hnn' N
  have hb2' := AltDiff.t_bnd K O (f2d K y) (f2d K y) (fun k => ⟨O.le_trans _ _ _ (by
      have := Signed.neg_le_neg K O _ _ (hnn' k)
      rw [show K.neg K.zero = K.zero by lib_ring] at this
      exact this) (hnn' k), O.le_refl _⟩) N
  have ha' : O.le K.zero (AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K y (c * c))) N) :=
    tU_nn K O h1 _ (fun c => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (pw_nn K O y hy0 h1 _)) N
  have hts : AltDiff.tS K (fun c => K.mul (cN K (2 * (c * c))) (pwR K y (2 * (c * c)))) N = AltDiff.tS K (f2d K y) N := rfl
  rw [hts] at e
  have dS := mul_bnd K O S _ _ hS dE
  generalize AltDiff.tU K (fun c => pwR K y (c * c)) N = a at e ⊢
  generalize AltDiff.tS K (fun c => K.mul (cN K (c * c)) (pwR K y (c * c))) N = b' at e ⊢
  generalize AltDiff.tU K (fun c => K.mul (cN K (c * c)) (pwR K y (c * c))) N = a' at e ha' ⊢
  generalize AltDiff.tS K (fun c => pwR K y (c * c)) N = b at e hb ⊢
  generalize AltDiff.tS K (f2 K y) N = b2 at e hb2 ⊢
  generalize AltDiff.tS K (f2d K y) N = b2' at e hb2' ⊢
  generalize AltDiff.tU K (f2d K y) N = U' at hb2' hU' ⊢
  generalize (errD K y N).2 = Er at e dS ⊢
  generalize QBinom.sumN K (fun _ => K.mul (AltDiff.two K) (K.mul (cN K (Mn N)) (pwR K y ((N + 1) * (N + 1))))) (2 * N + 1) = Eb at dS ⊢
  have key : K.mul S (K.mul a b') = K.add (K.add (K.add (K.mul (K.mul S b2) b2') (K.mul b2' (K.mul S b2))) (K.mul S Er))
      (K.neg (K.mul a' (K.mul S b))) := by
    have s : K.mul S (K.mul a b') = K.add (K.mul S (K.add (K.mul a b') (K.mul a' b))) (K.neg (K.mul a' (K.mul S b))) := by
      lib_ring
    rw [s, e]; lib_ring
  rw [key]
  have p1 := pb K O (K.mul S b2) b2' β2 U' hβ2 hb2 hb2'
  have p2 := pb K O b2' (K.mul S b2) U' β2 hU' hb2' hb2
  have p3 := neg_bnd K O _ _ (mul_bnd K O a' β1 (K.mul S b) ha' hb)
  exact add_bnd K O _ _ _ _ (add_bnd K O _ _ _ _ (add_bnd K O _ _ _ _ p1 p2) dS) p3

end ThetaDBound3

#print axioms ThetaDBound3.th4d_scaled

#print axioms: 1 theorem, no axioms.

90.50 The first heat ratio for small \(x\)

For \(2x^3\le 1\) every pair of the \(n=0\) monotonicity sum is nonnegative.

\[ 0\le x\le 1,\ 2x^3\le 1:\quad (2j)^2x^{(2j)^2+2}-((2j+1)^2-1)\,x^{(2j+1)^2}\ge 0\quad (j\ge 1) \]

ym/N0Small.lean

/-
  ym/N0Small — T45, G78m 6f-A′-1c: the n = 0 monotonicity sum is termwise nonnegative for small x.
  The target of 6f-A′-1 is T(x) = Σ_{j ≥ 1} [ (2j)² x^{(2j)²+2} − ((2j+1)² − 1) x^{(2j+1)²} ] ≥ 0 (R_0 ≥ R_1).
  With j = m + 1:
    pairT x m = (2m+2)² x^{(2m+2)²+2} − (2m+2)(2m+4) x^{(2m+3)²}
    half_nn     0 ≤ z + z ⇒ 0 ≤ z
    pair_small  0 ≤ x ≤ 1, 2x³ ≤ 1  ⇒  0 ≤ pairT x m      (x^{(2m+3)²} = x^{(2m+2)²+2} · x^{4m} · x³)
  So 6f-A′-1 only needs x³ > 1/2 (s = −log x < (log 2)/3 ≈ 0.231), where ThetaBound3 makes the ϑ₄ part small.
  Lean 4 core only, no axioms.
-/
import ym.ThetaBound3
import ym.HeatClass

namespace N0Small

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- (2m+2)² x^{(2m+2)²+2} − (2m+2)(2m+4) x^{(2m+3)²} -/
def pairT (x : R) (m : Nat) : R :=
  K.add (K.mul (cN K ((2 * m + 2) * (2 * m + 2))) (pwR K x ((2 * m + 2) * (2 * m + 2) + 2)))
    (K.neg (K.mul (cN K ((2 * m + 2) * (2 * m + 4))) (pwR K x ((2 * m + 3) * (2 * m + 3)))))

/-- 0 ≤ z + z ⇒ 0 ≤ z -/
theorem half_nn (z : R) (h : O.le K.zero (K.add z z)) : O.le K.zero z := by
  cases O.le_total K.zero z with
  | inl hz => exact hz
  | inr hz =>
    have hn : O.le K.zero (K.neg z) := by
      have := AltSum.le_sub K O K.zero z hz
      rw [show K.add K.zero (K.neg z) = K.neg z by lib_ring] at this
      exact this
    have := O.add_le_add _ _ _ _ h hn
    rw [K.zero_add, show K.add (K.add z z) (K.neg z) = z by lib_ring] at this
    exact this

/-- **small x**: every pair is nonnegative when 2x³ ≤ 1 -/
theorem pair_small (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one)
    (hx3 : O.le (K.add (pwR K x 3) (pwR K x 3)) K.one) (m : Nat) : O.le K.zero (pairT K x m) := by
  have hp := ThetaDup5.pw_nn K O x hx0 h1 ((2 * m + 2) * (2 * m + 2) + 2)
  have hr := ThetaDup5.pw_nn K O x hx0 h1 3
  have hq1 := HeatClass.pw_le_one K O x hx0 hx1 (4 * m)
  -- q r ≤ r
  have qr : O.le (K.mul (pwR K x (4 * m)) (pwR K x 3)) (pwR K x 3) := by
    have := O.mul_le_mul_right _ _ _ hq1 hr
    rw [K.one_mul] at this
    exact this
  have hca := SU2CharExp.cN_nn K O h1 ((2 * m + 2) * (2 * m + 4))
  have hcd := SU2CharExp.cN_nn K O h1 ((2 * m + 2) * (2 * m))
  -- 0 ≤ 1 − 2 q r
  have h12 : O.le K.zero (K.add K.one (K.neg (K.add (K.mul (pwR K x (4 * m)) (pwR K x 3)) (K.mul (pwR K x (4 * m)) (pwR K x 3))))) :=
    AltSum.le_sub K O _ _ (O.le_trans _ _ _ (O.add_le_add _ _ _ _ qr qr) hx3)
  have hz2 := O.add_le_add _ _ _ _ hcd (O.mul_nonneg _ _ hca h12)
  rw [K.zero_add] at hz2
  -- cN a + cN d = cN b + cN b
  have ecn : K.add (cN K ((2 * m + 2) * (2 * m + 4))) (cN K ((2 * m + 2) * (2 * m)))
      = K.add (cN K ((2 * m + 2) * (2 * m + 2))) (cN K ((2 * m + 2) * (2 * m + 2))) := by
    rw [← SU2Char.cN_add K, ← SU2Char.cN_add K,
      show (2 * m + 2) * (2 * m + 4) + (2 * m + 2) * (2 * m) = (2 * m + 2) * (2 * m + 2) + (2 * m + 2) * (2 * m + 2) by lib_nat]
  have hz : O.le K.zero (K.add (cN K ((2 * m + 2) * (2 * m + 2)))
      (K.neg (K.mul (cN K ((2 * m + 2) * (2 * m + 4))) (K.mul (pwR K x (4 * m)) (pwR K x 3))))) := by
    apply half_nn K O
    have e : K.add (K.add (cN K ((2 * m + 2) * (2 * m + 2)))
          (K.neg (K.mul (cN K ((2 * m + 2) * (2 * m + 4))) (K.mul (pwR K x (4 * m)) (pwR K x 3)))))
        (K.add (cN K ((2 * m + 2) * (2 * m + 2)))
          (K.neg (K.mul (cN K ((2 * m + 2) * (2 * m + 4))) (K.mul (pwR K x (4 * m)) (pwR K x 3)))))
        = K.add (cN K ((2 * m + 2) * (2 * m)))
          (K.mul (cN K ((2 * m + 2) * (2 * m + 4)))
            (K.add K.one (K.neg (K.add (K.mul (pwR K x (4 * m)) (pwR K x 3)) (K.mul (pwR K x (4 * m)) (pwR K x 3)))))) := by
      have e2 : K.add (cN K ((2 * m + 2) * (2 * m + 2))) (cN K ((2 * m + 2) * (2 * m + 2)))
          = K.add (cN K ((2 * m + 2) * (2 * m + 4))) (cN K ((2 * m + 2) * (2 * m))) := ecn.symm
      generalize cN K ((2 * m + 2) * (2 * m + 2)) = b at e2 ⊢
      generalize cN K ((2 * m + 2) * (2 * m + 4)) = a at e2 ⊢
      generalize cN K ((2 * m + 2) * (2 * m)) = d at e2 ⊢
      generalize pwR K x (4 * m) = q
      generalize pwR K x 3 = r
      have s : K.add (K.add b (K.neg (K.mul a (K.mul q r)))) (K.add b (K.neg (K.mul a (K.mul q r))))
          = K.add (K.add b b) (K.neg (K.add (K.mul a (K.mul q r)) (K.mul a (K.mul q r)))) := by lib_ring
      rw [s, e2]
      lib_ring
    rw [e]
    exact hz2
  have ep : pairT K x m = K.mul (pwR K x ((2 * m + 2) * (2 * m + 2) + 2))
      (K.add (cN K ((2 * m + 2) * (2 * m + 2)))
        (K.neg (K.mul (cN K ((2 * m + 2) * (2 * m + 4))) (K.mul (pwR K x (4 * m)) (pwR K x 3))))) := by
    show K.add (K.mul (cN K ((2 * m + 2) * (2 * m + 2))) (pwR K x ((2 * m + 2) * (2 * m + 2) + 2)))
      (K.neg (K.mul (cN K ((2 * m + 2) * (2 * m + 4))) (pwR K x ((2 * m + 3) * (2 * m + 3))))) = _
    rw [show (2 * m + 3) * (2 * m + 3) = ((2 * m + 2) * (2 * m + 2) + 2) + (4 * m + 3) by lib_nat,
      GKS.pwR_add K x ((2 * m + 2) * (2 * m + 2) + 2) (4 * m + 3), GKS.pwR_add K x (4 * m) 3]
    generalize pwR K x ((2 * m + 2) * (2 * m + 2) + 2) = p
    generalize pwR K x (4 * m) = q
    generalize pwR K x 3 = r
    generalize cN K ((2 * m + 2) * (2 * m + 2)) = b
    generalize cN K ((2 * m + 2) * (2 * m + 4)) = a
    lib_ring
  rw [ep]
  exact O.mul_nonneg _ _ hp hz

end N0Small

#print axioms N0Small.half_nn
#print axioms N0Small.pair_small

#print axioms: 2 theorems, no axioms.

90.51 Interval certificate for the first heat ratio

A Horner-form Nat checker certifies the \(n=0\) pair sum on rational cells.

\[ p\le Qx\le p',\ \mathrm {chk}(p,p',Q,0,J)=(P,M,D),\ M\le P\ \Rightarrow \ \sum _{m<J}\mathrm {pair}_m(x)\ge 0 \]

ym/PairCert.lean

/-
  ym/PairCert — T45, G78m 6f-A′-1d (middle range): an interval certificate for the n = 0 pair sum.
  With a_m = (2m+2)², b_m = (2m+2)(2m+4), e_m = (2m+2)² + 2, pairT x m = a_m x^{e_m} − b_m x^{e_m + 4m+3}
  (N0Small.pairT), and e_{m+1} = e_m + 8m + 12:
    Tr x m r      = a_m − b_m x^{4m+3} + x^{8m+12} Tr x (m+1) (r−1)     (Horner form, Tr x m 0 = 0)
    sumR x m r    = Σ_{i<r} pairT x (m+i)
    sumR_eq       sumR x m r = x^{e_m} · Tr x m r
    pw_lo / pw_hi cN p ≤ cN Q·x ⇒ cN p^n ≤ cN Q^n·x^n;   cN Q·x ≤ cN p′ ⇒ cN Q^n·x^n ≤ cN p′^n   (0 ≤ x)
    chk p p′ Q m r = (P, M, D)  (Nat): lower bound P − M of Q^D·Tr on the cell [p/Q, p′/Q]
    chk_sound     cN P ≤ cN M + cN Q^D · Tr x m r
    cell_pos      M ≤ P for chk p p′ (q+1) 0 J and p ≤ (q+1)x ≤ p′  ⇒  0 ≤ Σ_{m<J} pairT x m
  Lean 4 core only, no axioms.
-/
import ym.N0Split
import ym.N0Close
import ym.N0Core

namespace PairCert

open YMChain LinkIntegral SU2Char N0Small

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- Horner form of the pair sum from index m, r pairs -/
def Tr (x : R) : Nat → Nat → R
  | _, 0 => K.zero
  | m, r + 1 => K.add (K.add (cN K ((2 * m + 2) * (2 * m + 2)))
      (K.neg (K.mul (cN K ((2 * m + 2) * (2 * m + 4))) (pwR K x (4 * m + 3)))))
      (K.mul (pwR K x (8 * m + 12)) (Tr x (m + 1) r))

/-- Σ_{i<r} pairT x (m+i), front first -/
def sumR (x : R) : Nat → Nat → R
  | _, 0 => K.zero
  | m, r + 1 => K.add (pairT K x m) (sumR x (m + 1) r)

theorem sumR_eq (x : R) : ∀ r m, sumR K x m r = K.mul (pwR K x ((2 * m + 2) * (2 * m + 2) + 2)) (Tr K x m r)
  | 0, m => by
    show K.zero = K.mul (pwR K x ((2 * m + 2) * (2 * m + 2) + 2)) K.zero
    lib_ring
  | r + 1, m => by
    show K.add (pairT K x m) (sumR K x (m + 1) r) = _
    rw [sumR_eq x r (m + 1)]
    show K.add (K.add (K.mul (cN K ((2 * m + 2) * (2 * m + 2))) (pwR K x ((2 * m + 2) * (2 * m + 2) + 2)))
        (K.neg (K.mul (cN K ((2 * m + 2) * (2 * m + 4))) (pwR K x ((2 * m + 3) * (2 * m + 3))))))
        (K.mul (pwR K x ((2 * (m + 1) + 2) * (2 * (m + 1) + 2) + 2)) (Tr K x (m + 1) r))
      = K.mul (pwR K x ((2 * m + 2) * (2 * m + 2) + 2)) (K.add (K.add (cN K ((2 * m + 2) * (2 * m + 2)))
          (K.neg (K.mul (cN K ((2 * m + 2) * (2 * m + 4))) (pwR K x (4 * m + 3)))))
          (K.mul (pwR K x (8 * m + 12)) (Tr K x (m + 1) r)))
    rw [show (2 * m + 3) * (2 * m + 3) = ((2 * m + 2) * (2 * m + 2) + 2) + (4 * m + 3) by lib_nat,
      show (2 * (m + 1) + 2) * (2 * (m + 1) + 2) + 2 = ((2 * m + 2) * (2 * m + 2) + 2) + (8 * m + 12) by lib_nat,
      GKS.pwR_add K x _ (4 * m + 3), GKS.pwR_add K x _ (8 * m + 12)]
    generalize pwR K x ((2 * m + 2) * (2 * m + 2) + 2) = p
    generalize pwR K x (4 * m + 3) = q
    generalize pwR K x (8 * m + 12) = w
    generalize Tr K x (m + 1) r = t
    generalize cN K ((2 * m + 2) * (2 * m + 2)) = a
    generalize cN K ((2 * m + 2) * (2 * m + 4)) = b
    lib_ring

/-- sumT x J = sumR x 0 J -/
theorem sumR_snoc (x : R) : ∀ r m, sumR K x m (r + 1) = K.add (sumR K x m r) (pairT K x (m + r))
  | 0, m => by
    show K.add (pairT K x m) K.zero = K.add K.zero (pairT K x (m + 0))
    generalize pairT K x m = q
    show K.add q K.zero = K.add K.zero q
    lib_ring
  | r + 1, m => by
    show K.add (pairT K x m) (sumR K x (m + 1) (r + 1)) = K.add (K.add (pairT K x m) (sumR K x (m + 1) r)) (pairT K x (m + (r + 1)))
    rw [sumR_snoc x r (m + 1), show m + 1 + r = m + (r + 1) by lib_nat]
    generalize pairT K x m = q
    generalize sumR K x (m + 1) r = s
    generalize pairT K x (m + (r + 1)) = t
    lib_ring

theorem sumT_eq (x : R) : ∀ J, N0Split.sumT K x J = sumR K x 0 J
  | 0 => rfl
  | J + 1 => by
    show K.add (N0Split.sumT K x J) (pairT K x J) = _
    rw [sumT_eq x J, sumR_snoc K x J 0, Nat.zero_add]

theorem pw_lo (h1 : O.le K.zero K.one) (x : R) (p Q : Nat) (hx0 : O.le K.zero x) (hl : O.le (cN K p) (K.mul (cN K Q) x)) :
    ∀ n, O.le (cN K (p ^ n)) (K.mul (cN K (Q ^ n)) (pwR K x n))
  | 0 => by
    show O.le (cN K 1) (K.mul (cN K 1) K.one)
    rw [show K.mul (cN K 1) K.one = cN K 1 by lib_ring]
    exact O.le_refl _
  | n + 1 => by
    have ih := pw_lo h1 x p Q hx0 hl n
    have hp := SU2CharExp.cN_nn K O h1 p
    show O.le (cN K (p ^ n * p)) (K.mul (cN K (Q ^ n * Q)) (K.mul (pwR K x n) x))
    rw [SU2CharExp.cN_mul K, SU2CharExp.cN_mul K]
    have m := N0Core.mul_le2 K O _ _ _ _ (SU2CharExp.cN_nn K O h1 _) hp ih hl
    apply O.le_trans _ _ _ m
    exact N0Close.le_eq K O _ _ (by
      generalize cN K (Q ^ n) = A; generalize pwR K x n = B; generalize cN K Q = C; lib_ring)

theorem pw_hi (h1 : O.le K.zero K.one) (x : R) (p Q : Nat) (hx0 : O.le K.zero x) (hu : O.le (K.mul (cN K Q) x) (cN K p)) :
    ∀ n, O.le (K.mul (cN K (Q ^ n)) (pwR K x n)) (cN K (p ^ n))
  | 0 => by
    show O.le (K.mul (cN K 1) K.one) (cN K 1)
    rw [show K.mul (cN K 1) K.one = cN K 1 by lib_ring]
    exact O.le_refl _
  | n + 1 => by
    have ih := pw_hi h1 x p Q hx0 hu n
    have hq : O.le K.zero (K.mul (cN K Q) x) := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 Q) hx0
    show O.le (K.mul (cN K (Q ^ n * Q)) (K.mul (pwR K x n) x)) (cN K (p ^ n * p))
    rw [SU2CharExp.cN_mul K, SU2CharExp.cN_mul K]
    have m := N0Core.mul_le2 K O _ _ _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (ThetaDup5.pw_nn K O x hx0 h1 n)) hq ih hu
    apply O.le_trans _ _ _ _ m
    exact N0Close.le_eq K O _ _ (by
      generalize cN K (Q ^ n) = A; generalize pwR K x n = B; generalize cN K Q = C; lib_ring)


/-- the Nat certificate: (P, M, D) with Q^D · Tr ≥ P − M on the cell [p/Q, p′/Q] -/
def chk (p p' Q : Nat) : Nat → Nat → Nat × Nat × Nat
  | _, 0 => (0, 0, 0)
  | m, r + 1 =>
    ((2 * m + 2) * (2 * m + 2) * Q ^ ((chk p p' Q (m + 1) r).2.2 + (8 * m + 12))
        + (if Nat.ble (chk p p' Q (m + 1) r).2.1 (chk p p' Q (m + 1) r).1 then p else p') ^ (8 * m + 12) * (chk p p' Q (m + 1) r).1,
     (2 * m + 2) * (2 * m + 4) * p' ^ (4 * m + 3) * Q ^ ((chk p p' Q (m + 1) r).2.2 + (4 * m + 9))
        + (if Nat.ble (chk p p' Q (m + 1) r).2.1 (chk p p' Q (m + 1) r).1 then p else p') ^ (8 * m + 12) * (chk p p' Q (m + 1) r).2.1,
     (chk p p' Q (m + 1) r).2.2 + (8 * m + 12))

theorem cN_le_of_ble (h1 : O.le K.zero K.one) (a b : Nat) (h : Nat.ble a b = true) : O.le (cN K a) (cN K b) :=
  SU2CharExp.cN_mono K O h1 a b (Nat.le_of_ble_eq_true h)

/-- a·(P − M) ≤ c·(P − M) style step: scaled lower bound of x^k · (Q^{D1} Tr) -/
theorem scale_step (lo hi y P M : R) (hlo : O.le K.zero lo) (hy : O.le K.zero y) (hlo1 : O.le lo y) (hhi : O.le y hi)
    (Tq : R) (ih : O.le P (K.add M Tq)) (pos : Bool) (hpos : pos = true → O.le M P) (hneg : pos = false → O.le P M) :
    O.le (K.mul (if pos then lo else hi) P) (K.add (K.mul (if pos then lo else hi) M) (K.mul y Tq)) := by
  cases pos with
  | true =>
    show O.le (K.mul lo P) (K.add (K.mul lo M) (K.mul y Tq))
    have d := AltSum.le_sub K O _ _ (hpos rfl)
    have d2 := O.mul_le_mul_right _ _ _ hlo1 d
    have e := AltSum.le_sub K O _ _ ih
    have e2 := O.mul_nonneg _ _ hy e
    apply ThetaLow.le_of_sub K O
    have t := O.add_le_add _ _ _ _ (AltSum.le_sub K O _ _ d2) e2
    rw [K.zero_add] at t
    exact O.le_trans _ _ _ t (N0Close.le_eq K O _ _ (by lib_ring))
  | false =>
    show O.le (K.mul hi P) (K.add (K.mul hi M) (K.mul y Tq))
    have d := AltSum.le_sub K O _ _ (hneg rfl)
    have d2 := O.mul_le_mul_right _ _ _ hhi d
    have e := AltSum.le_sub K O _ _ ih
    have e2 := O.mul_nonneg _ _ hy e
    apply ThetaLow.le_of_sub K O
    have t := O.add_le_add _ _ _ _ (AltSum.le_sub K O _ _ d2) e2
    rw [K.zero_add] at t
    exact O.le_trans _ _ _ t (N0Close.le_eq K O _ _ (by lib_ring))


theorem scale_step2 (lo y P M Tq : R) (hy : O.le K.zero y) (ih : O.le P (K.add M Tq))
    (hcase : (O.le M P ∧ O.le lo y) ∨ (O.le P M ∧ O.le y lo)) :
    O.le (K.mul lo P) (K.add (K.mul lo M) (K.mul y Tq)) := by
  have e := AltSum.le_sub K O _ _ ih
  have e2 := O.mul_nonneg _ _ hy e
  apply ThetaLow.le_of_sub K O
  cases hcase with
  | inl h =>
    have d2 := O.mul_le_mul_right _ _ _ h.2 (AltSum.le_sub K O _ _ h.1)
    have t := O.add_le_add _ _ _ _ (AltSum.le_sub K O _ _ d2) e2
    rw [K.zero_add] at t
    exact O.le_trans _ _ _ t (N0Close.le_eq K O _ _ (by lib_ring))
  | inr h =>
    have d2 := O.mul_le_mul_right _ _ _ h.2 (AltSum.le_sub K O _ _ h.1)
    have t := O.add_le_add _ _ _ _ (AltSum.le_sub K O _ _ d2) e2
    rw [K.zero_add] at t
    exact O.le_trans _ _ _ t (N0Close.le_eq K O _ _ (by lib_ring))

theorem cN_pow (Q : Nat) : ∀ n, cN K (Q ^ n) = pwR K (cN K Q) n
  | 0 => by show K.add K.one K.zero = K.one; lib_ring
  | n + 1 => by
    show cN K (Q ^ n * Q) = K.mul (pwR K (cN K Q) n) (cN K Q)
    rw [SU2CharExp.cN_mul K, cN_pow Q n]

theorem ble_false (a b : Nat) (h : Nat.ble a b = false) : b ≤ a := by
  cases Nat.le_total b a with
  | inl hh => exact hh
  | inr hh =>
    have := Nat.ble_eq_true_of_le hh
    rw [h] at this
    exact Bool.noConfusion this

/-- **soundness of the Nat certificate** -/
theorem chk_sound (h1 : O.le K.zero K.one) (x : R) (p p' Q : Nat) (hx0 : O.le K.zero x)
    (hl : O.le (cN K p) (K.mul (cN K Q) x)) (hu : O.le (K.mul (cN K Q) x) (cN K p')) : ∀ r m,
    O.le (cN K (chk p p' Q m r).1)
      (K.add (cN K (chk p p' Q m r).2.1) (K.mul (cN K (Q ^ (chk p p' Q m r).2.2)) (Tr K x m r)))
  | 0, m => by
    show O.le K.zero (K.add K.zero (K.mul (cN K (Q ^ 0)) K.zero))
    exact N0Close.le_eq K O _ _ (by generalize cN K (Q ^ 0) = c; lib_ring)
  | r + 1, m => by
    have ih := chk_sound h1 x p p' Q hx0 hl hu r (m + 1)
    show O.le (cN K ((2 * m + 2) * (2 * m + 2) * Q ^ ((chk p p' Q (m + 1) r).2.2 + (8 * m + 12))
        + (if Nat.ble (chk p p' Q (m + 1) r).2.1 (chk p p' Q (m + 1) r).1 then p else p') ^ (8 * m + 12) * (chk p p' Q (m + 1) r).1))
      (K.add (cN K ((2 * m + 2) * (2 * m + 4) * p' ^ (4 * m + 3) * Q ^ ((chk p p' Q (m + 1) r).2.2 + (4 * m + 9))
        + (if Nat.ble (chk p p' Q (m + 1) r).2.1 (chk p p' Q (m + 1) r).1 then p else p') ^ (8 * m + 12) * (chk p p' Q (m + 1) r).2.1))
        (K.mul (cN K (Q ^ ((chk p p' Q (m + 1) r).2.2 + (8 * m + 12)))) (Tr K x m (r + 1))))
    -- case data
    have hcase : ∀ b : Bool, Nat.ble (chk p p' Q (m + 1) r).2.1 (chk p p' Q (m + 1) r).1 = b →
        (O.le (cN K (chk p p' Q (m + 1) r).2.1) (cN K (chk p p' Q (m + 1) r).1)
          ∧ O.le (cN K ((if b then p else p') ^ (8 * m + 12))) (K.mul (cN K (Q ^ (8 * m + 12))) (pwR K x (8 * m + 12))))
        ∨ (O.le (cN K (chk p p' Q (m + 1) r).1) (cN K (chk p p' Q (m + 1) r).2.1)
          ∧ O.le (K.mul (cN K (Q ^ (8 * m + 12))) (pwR K x (8 * m + 12))) (cN K ((if b then p else p') ^ (8 * m + 12)))) := by
      intro b hb
      cases b with
      | true => exact Or.inl ⟨cN_le_of_ble K O h1 _ _ hb, pw_lo K O h1 x p Q hx0 hl _⟩
      | false => exact Or.inr ⟨SU2CharExp.cN_mono K O h1 _ _ (ble_false _ _ hb), pw_hi K O h1 x p' Q hx0 hu _⟩
    have hc := hcase _ rfl
    generalize Nat.ble (chk p p' Q (m + 1) r).2.1 (chk p p' Q (m + 1) r).1 = bb at hc ⊢
    generalize (if bb = true then p else p') = xp at hc ⊢
    generalize chk p p' Q (m + 1) r = cc at ih hc ⊢
    obtain ⟨P1, M1, D1⟩ := cc
    show O.le _ (K.add _ (K.mul _ (K.add (K.add (cN K ((2 * m + 2) * (2 * m + 2)))
      (K.neg (K.mul (cN K ((2 * m + 2) * (2 * m + 4))) (pwR K x (4 * m + 3)))))
      (K.mul (pwR K x (8 * m + 12)) (Tr K x (m + 1) r)))))
    have st := scale_step2 K O (cN K (xp ^ (8 * m + 12))) (K.mul (cN K (Q ^ (8 * m + 12))) (pwR K x (8 * m + 12)))
      (cN K P1) (cN K M1) (K.mul (cN K (Q ^ D1)) (Tr K x (m + 1) r))
      (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (ThetaDup5.pw_nn K O x hx0 h1 _)) ih hc
    have hb := pw_hi K O h1 x p' Q hx0 hu (4 * m + 3)
    have hb2 := O.mul_le_mul_right _ _ _ hb (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 ((2 * m + 2) * (2 * m + 4)))
      (SU2CharExp.cN_nn K O h1 (Q ^ (D1 + (4 * m + 9)))))
    have t := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ st (AltSum.le_sub K O _ _ hb2))
      (O.le_refl (K.mul (cN K ((2 * m + 2) * (2 * m + 2))) (cN K (Q ^ (D1 + (8 * m + 12))))))
    generalize (2 * m + 2) * (2 * m + 2) = na at t ⊢
    generalize (2 * m + 2) * (2 * m + 4) = nb at t ⊢
    generalize p' ^ (4 * m + 3) = np at t ⊢
    generalize xp ^ (8 * m + 12) = nx at t ⊢
    show O.le (cN K (na * Q ^ (D1 + (8 * m + 12)) + nx * P1)) _
    rw [SU2Char.cN_add K (na * Q ^ (D1 + (8 * m + 12))), SU2CharExp.cN_mul K na, SU2CharExp.cN_mul K nx P1,
      SU2Char.cN_add K (nb * np * Q ^ (D1 + (4 * m + 9))), SU2CharExp.cN_mul K (nb * np), SU2CharExp.cN_mul K nb np,
      SU2CharExp.cN_mul K nx M1]
    rw [cN_pow K Q (D1 + (8 * m + 12)), cN_pow K Q (D1 + (4 * m + 9))] at t ⊢
    rw [cN_pow K Q D1, cN_pow K Q (8 * m + 12), cN_pow K Q (4 * m + 3)] at t
    have e8 : pwR K (cN K Q) (8 * m + 12) = K.mul (pwR K (cN K Q) (4 * m + 3)) (pwR K (cN K Q) (4 * m + 9)) := by
      rw [← GKS.pwR_add, show 4 * m + 3 + (4 * m + 9) = 8 * m + 12 by lib_nat]
    rw [e8] at t
    rw [GKS.pwR_add K (cN K Q) D1 (8 * m + 12), e8, GKS.pwR_add K (cN K Q) D1 (4 * m + 9)] at t ⊢
    generalize pwR K (cN K Q) D1 = g at t ⊢
    generalize pwR K (cN K Q) (4 * m + 3) = g3 at t ⊢
    generalize pwR K (cN K Q) (4 * m + 9) = g9 at t ⊢
    generalize pwR K x (4 * m + 3) = X3 at t ⊢
    generalize pwR K x (8 * m + 12) = Xk at t ⊢
    generalize Tr K x (m + 1) r = T1 at t ⊢
    generalize cN K na = a at t ⊢
    generalize cN K nb = b at t ⊢
    generalize cN K np = pp at t ⊢
    generalize cN K nx = c at t ⊢
    generalize cN K P1 = P at t ⊢
    generalize cN K M1 = M at t ⊢
    exact O.le_trans _ _ _ (N0Close.le_eq K O _ _ (by lib_ring)) (O.le_trans _ _ _ t (N0Close.le_eq K O _ _ (by lib_ring)))


theorem pw_ge_one (h1 : O.le K.zero K.one) (c : R) (hc : O.le K.one c) : ∀ n, O.le K.one (pwR K c n)
  | 0 => O.le_refl _
  | n + 1 => by
    have ih := pw_ge_one h1 c hc n
    have m := N0Core.mul_le2 K O _ _ _ _ h1 h1 ih hc
    rw [show K.mul K.one K.one = K.one by lib_ring] at m
    exact m

/-- **cell certificate**: on [p/Q, p′/Q] (Q = q+1) the first J pairs sum to ≥ 0 -/
theorem cell_pos (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (p p' q J : Nat) (hx0 : O.le K.zero x)
    (hl : O.le (cN K p) (K.mul (cN K (q + 1)) x)) (hu : O.le (K.mul (cN K (q + 1)) x) (cN K p'))
    (hc : Nat.ble (chk p p' (q + 1) 0 J).2.1 (chk p p' (q + 1) 0 J).1 = true) :
    O.le K.zero (N0Split.sumT K x J) := by
  have s := chk_sound K O h1 x p p' (q + 1) hx0 hl hu J 0
  have hm := cN_le_of_ble K O h1 _ _ hc
  rw [cN_pow K (q + 1)] at s
  have g1 : O.le K.one (cN K (q + 1)) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) (SU2CharExp.cN_nn K O h1 q)
    rw [show K.add K.one K.zero = K.one by lib_ring] at this
    exact this
  have gp := pw_ge_one K O h1 _ g1 (chk p p' (q + 1) 0 J).2.2
  generalize pwR K (cN K (q + 1)) (chk p p' (q + 1) 0 J).2.2 = g at s gp
  generalize cN K (chk p p' (q + 1) 0 J).1 = P at s hm
  generalize cN K (chk p p' (q + 1) 0 J).2.1 = M at s hm
  have hg0 : O.le K.zero g := O.le_trans _ _ _ h1 gp
  have hgne : g ≠ K.zero := by
    intro e; rw [e] at gp; exact h10 (O.le_antisymm _ _ gp h1)
  have hT : O.le K.zero (Tr K x 0 J) := by
    apply O.pos_cancel _ _ _ hg0 hgne
    have t := O.le_trans _ _ _ hm s
    have u := AltSum.le_sub K O _ _ t
    exact O.le_trans _ _ _ (N0Close.le_eq K O _ _ (by lib_ring)) (O.le_trans _ _ _ u (N0Close.le_eq K O _ _ (by
      generalize Tr K x 0 J = T; lib_ring)))
  rw [sumT_eq K x J, sumR_eq K x J 0]
  exact O.mul_nonneg _ _ (ThetaDup5.pw_nn K O x hx0 h1 _) hT

end PairCert

#print axioms PairCert.sumR_eq
#print axioms PairCert.sumR_snoc
#print axioms PairCert.sumT_eq
#print axioms PairCert.pw_lo
#print axioms PairCert.pw_hi
#print axioms PairCert.cN_le_of_ble
#print axioms PairCert.scale_step
#print axioms PairCert.scale_step2
#print axioms PairCert.cN_pow
#print axioms PairCert.ble_false
#print axioms PairCert.chk_sound
#print axioms PairCert.pw_ge_one
#print axioms PairCert.cell_pos

#print axioms: 13 theorems, no axioms.

90.52 Exact split of the first heat ratio

The \(n=0\) monotonicity sum is a full-lattice variance term plus a \(\vartheta _4\)-type alternating term, with no boundary term.

\[ U_{2J+1}\bigl (x^{c^2}(1-c^2(1-x^2))\bigr )+T_{2J+1}\bigl (x^{c^2}((1+x^2)c^2-1)\bigr )=4\sum _{m<J}\mathrm {pair}_m(x) \]

ym/N0Split.lean

/-
  ym/N0Split — T45, G78m 6f-A′-1d (first half): the n = 0 sum splits exactly into a full-ℤ part and a ϑ₄ part.
    fF x c = x^{c²} (1 − c² (1 − x²))          (full-ℤ Gaussian variance form, GaussVar)
    gA x c = x^{c²} ((1 + x²) c² − 1)           (ϑ₄-type, ThetaBound3 / ThetaDBound)
    sumT x J = Σ_{m<J} pairT x m                 (N0Small.pairT: the R_0 ≥ R_1 pairs)
    split      U_{2J+1}(fF x) + T_{2J+1}(gA x) = 4 · sumT x J      (no boundary term)
  Even c: fF + gA = 2c² x^{c²+2}; odd c: fF − gA = −2(c² − 1) x^{c²}; c = 0, 1 cancel.
  Lean 4 core only, no axioms.
-/
import ym.N0Small
import ym.ThetaDup

namespace N0Split

open YMChain LinkIntegral SU2Char N0Small

variable {R : Type} (K : DetChain.CRing R)

/-- x^{c²} (1 − c²(1 − x²)) -/
def fF (x : R) (c : Nat) : R :=
  K.mul (pwR K x (c * c)) (K.add K.one (K.neg (K.mul (cN K (c * c)) (K.add K.one (K.neg (K.mul x x))))))

/-- x^{c²} ((1 + x²) c² − 1) -/
def gA (x : R) (c : Nat) : R :=
  K.mul (pwR K x (c * c)) (K.add (K.mul (K.add K.one (K.mul x x)) (cN K (c * c))) (K.neg K.one))

/-- Σ_{m<J} pairT x m -/
def sumT (x : R) : Nat → R
  | 0 => K.zero
  | J + 1 => K.add (sumT x J) (pairT K x J)

/-- **exact split**: U_{2J+1}(fF) + T_{2J+1}(gA) = 4 Σ_{m<J} pairT -/
theorem split (x : R) : ∀ J, K.add (AltDiff.tU K (fF K x) (2 * J + 1)) (AltDiff.tS K (gA K x) (2 * J + 1))
    = K.mul (K.mul (AltDiff.two K) (AltDiff.two K)) (sumT K x J)
  | 0 => by
    show K.add (K.add (fF K x 0) (K.mul (AltDiff.two K) (fF K x 1)))
        (K.add (gA K x 0) (K.mul (AltDiff.two K) (K.mul (K.neg K.one) (gA K x 1))))
      = K.mul (K.mul (AltDiff.two K) (AltDiff.two K)) K.zero
    show K.add (K.add (K.mul K.one (K.add K.one (K.neg (K.mul K.zero (K.add K.one (K.neg (K.mul x x)))))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul K.one x)
            (K.add K.one (K.neg (K.mul (K.add K.one K.zero) (K.add K.one (K.neg (K.mul x x)))))))))
        (K.add (K.mul K.one (K.add (K.mul (K.add K.one (K.mul x x)) K.zero) (K.neg K.one)))
          (K.mul (K.add K.one K.one) (K.mul (K.neg K.one) (K.mul (K.mul K.one x)
            (K.add (K.mul (K.add K.one (K.mul x x)) (K.add K.one K.zero)) (K.neg K.one))))))
      = K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) K.zero
    lib_ring
  | J + 1 => by
    have ih := split x J
    show K.add (K.add (K.add (AltDiff.tU K (fF K x) (2 * J + 1)) (K.mul (AltDiff.two K) (fF K x (2 * (J + 1)))))
          (K.mul (AltDiff.two K) (fF K x (2 * (J + 1) + 1))))
        (K.add (K.add (AltDiff.tS K (gA K x) (2 * J + 1))
            (K.mul (AltDiff.two K) (K.mul (AltDiff.sg K (2 * (J + 1))) (gA K x (2 * (J + 1))))))
          (K.mul (AltDiff.two K) (K.mul (K.neg (AltDiff.sg K (2 * (J + 1)))) (gA K x (2 * (J + 1) + 1)))))
      = K.mul (K.mul (AltDiff.two K) (AltDiff.two K)) (K.add (sumT K x J) (pairT K x J))
    rw [ThetaDup.sg_two K (J + 1), show 2 * (J + 1) = 2 * J + 2 by lib_nat]
    show K.add (K.add (K.add (AltDiff.tU K (fF K x) (2 * J + 1))
            (K.mul (AltDiff.two K) (K.mul (pwR K x ((2 * J + 2) * (2 * J + 2)))
              (K.add K.one (K.neg (K.mul (cN K ((2 * J + 2) * (2 * J + 2))) (K.add K.one (K.neg (K.mul x x)))))))))
          (K.mul (AltDiff.two K) (K.mul (pwR K x ((2 * J + 2 + 1) * (2 * J + 2 + 1)))
              (K.add K.one (K.neg (K.mul (cN K ((2 * J + 2 + 1) * (2 * J + 2 + 1))) (K.add K.one (K.neg (K.mul x x)))))))))
        (K.add (K.add (AltDiff.tS K (gA K x) (2 * J + 1))
            (K.mul (AltDiff.two K) (K.mul K.one (K.mul (pwR K x ((2 * J + 2) * (2 * J + 2)))
              (K.add (K.mul (K.add K.one (K.mul x x)) (cN K ((2 * J + 2) * (2 * J + 2)))) (K.neg K.one))))))
          (K.mul (AltDiff.two K) (K.mul (K.neg K.one) (K.mul (pwR K x ((2 * J + 2 + 1) * (2 * J + 2 + 1)))
              (K.add (K.mul (K.add K.one (K.mul x x)) (cN K ((2 * J + 2 + 1) * (2 * J + 2 + 1)))) (K.neg K.one))))))
      = K.mul (K.mul (AltDiff.two K) (AltDiff.two K)) (K.add (sumT K x J)
          (K.add (K.mul (cN K ((2 * J + 2) * (2 * J + 2))) (K.mul (K.mul (pwR K x ((2 * J + 2) * (2 * J + 2))) x) x))
            (K.neg (K.mul (cN K ((2 * J + 2) * (2 * J + 4))) (pwR K x ((2 * J + 3) * (2 * J + 3)))))))
    rw [show 2 * J + 2 + 1 = 2 * J + 3 by lib_nat,
      show (2 * J + 3) * (2 * J + 3) = (2 * J + 2) * (2 * J + 4) + 1 by lib_nat, SU2Char.cN_add K _ 1]
    rw [show (2 * J + 2) * (2 * J + 4) + 1 = (2 * J + 3) * (2 * J + 3) by lib_nat]
    show K.add (K.add (K.add (AltDiff.tU K (fF K x) (2 * J + 1)) _) _) (K.add (K.add (AltDiff.tS K (gA K x) (2 * J + 1)) _) _)
      = K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) _
    generalize AltDiff.tU K (fF K x) (2 * J + 1) = U at ih ⊢
    generalize AltDiff.tS K (gA K x) (2 * J + 1) = S at ih ⊢
    generalize sumT K x J = sJ at ih ⊢
    generalize pwR K x ((2 * J + 2) * (2 * J + 2)) = pe
    generalize pwR K x ((2 * J + 3) * (2 * J + 3)) = po
    generalize cN K ((2 * J + 2) * (2 * J + 2)) = ce
    generalize cN K ((2 * J + 2) * (2 * J + 4)) = co
    have e : K.add (K.add (K.add U (K.mul (K.add K.one K.one) (K.mul pe (K.add K.one (K.neg (K.mul ce (K.add K.one (K.neg (K.mul x x)))))))))
          (K.mul (K.add K.one K.one) (K.mul po (K.add K.one (K.neg (K.mul (K.add co (K.add K.one K.zero)) (K.add K.one (K.neg (K.mul x x)))))))))
        (K.add (K.add S (K.mul (K.add K.one K.one) (K.mul K.one (K.mul pe (K.add (K.mul (K.add K.one (K.mul x x)) ce) (K.neg K.one))))))
          (K.mul (K.add K.one K.one) (K.mul (K.neg K.one) (K.mul po
            (K.add (K.mul (K.add K.one (K.mul x x)) (K.add co (K.add K.one K.zero))) (K.neg K.one))))))
        = K.add (K.add U S) (K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one))
            (K.add (K.mul ce (K.mul (K.mul pe x) x)) (K.neg (K.mul co po)))) := by lib_ring
    have ih' : K.add U S = K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) sJ := ih
    show K.add (K.add (K.add U (K.mul (K.add K.one K.one) (K.mul pe (K.add K.one (K.neg (K.mul ce (K.add K.one (K.neg (K.mul x x)))))))))
          (K.mul (K.add K.one K.one) (K.mul po (K.add K.one (K.neg (K.mul (K.add co (K.add K.one K.zero)) (K.add K.one (K.neg (K.mul x x)))))))))
        (K.add (K.add S (K.mul (K.add K.one K.one) (K.mul K.one (K.mul pe (K.add (K.mul (K.add K.one (K.mul x x)) ce) (K.neg K.one))))))
          (K.mul (K.add K.one K.one) (K.mul (K.neg K.one) (K.mul po
            (K.add (K.mul (K.add K.one (K.mul x x)) (K.add co (K.add K.one K.zero))) (K.neg K.one))))))
      = K.mul (K.mul (K.add K.one K.one) (K.add K.one K.one)) (K.add sJ
          (K.add (K.mul ce (K.mul (K.mul pe x) x)) (K.neg (K.mul co po))))
    rw [e, ih']
    lib_ring

end N0Split

#print axioms N0Split.split

#print axioms: 1 theorem, no axioms.

90.53 Lower bound for the first heat ratio

The \(n=0\) monotonicity sum is at least the explicit slack sum plus the two \(\vartheta _4\)-type terms.

\[ 4\sum _{m<J}\mathrm {pair}_m(x)\ge W_{2J+1}(x)+(1+x^2)\,T_{2J+1}(c^2x^{c^2})-\vartheta _4^{(2J+1)}(x) \]

ym/N0Link.lean

/-
  ym/N0Link — T45, G78m 6f-A′-1d: the two halves of N0Split in the forms of GaussVar2 and ThetaBound3 / ThetaDBound3.
    fF_eq   U_N(fF x) = ϑ₃⁽ᴺ⁾(x) − (1 − x²) V_N(x)
    F_low   U_N(fF x) ≥ Σ_{M<N} wl x M                    (GaussVar2.var_low; boundary terms ≥ 0)
    gA_eq   T_N(gA x) = (1 + x²) T_N(c² x^{c²}) − ϑ₄⁽ᴺ⁾(x)
    n0_low  4 Σ_{m<J} pairT x m ≥ W(2J+1) + (1 + x²) T_{2J+1}(c² x^{c²}) − ϑ₄⁽²ᴶ⁺¹⁾(x)
  Lean 4 core only, no axioms.
-/
import ym.N0Split
import ym.GaussVar2

namespace N0Link

open YMChain LinkIntegral SU2Char N0Split GaussVar GaussVar2

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- U_N(fF x) = ϑ₃⁽ᴺ⁾(x) − (1 − x²) V_N -/
theorem fF_eq (x : R) : ∀ N, AltDiff.tU K (fF K x) N
    = K.add (AltDiff.tU K (fun c => pwR K x (c * c)) N) (K.neg (K.mul (K.add K.one (K.neg (K.mul x x))) (V K x N)))
  | 0 => by
    show K.mul (pwR K x (0 * 0)) (K.add K.one (K.neg (K.mul (cN K (0 * 0)) (K.add K.one (K.neg (K.mul x x))))))
      = K.add (pwR K x (0 * 0)) (K.neg (K.mul (K.add K.one (K.neg (K.mul x x))) K.zero))
    show K.mul (pwR K x (0 * 0)) (K.add K.one (K.neg (K.mul K.zero (K.add K.one (K.neg (K.mul x x))))))
      = K.add (pwR K x (0 * 0)) (K.neg (K.mul (K.add K.one (K.neg (K.mul x x))) K.zero))
    generalize pwR K x (0 * 0) = p
    lib_ring
  | N + 1 => by
    have ih := fF_eq x N
    show K.add (AltDiff.tU K (fF K x) N) (K.mul (AltDiff.two K) (K.mul (pwR K x ((N + 1) * (N + 1)))
        (K.add K.one (K.neg (K.mul (cN K ((N + 1) * (N + 1))) (K.add K.one (K.neg (K.mul x x))))))))
      = K.add (K.add (AltDiff.tU K (fun c => pwR K x (c * c)) N) (K.mul (AltDiff.two K) (pwR K x ((N + 1) * (N + 1)))))
        (K.neg (K.mul (K.add K.one (K.neg (K.mul x x))) (K.add (V K x N)
          (K.mul (AltDiff.two K) (K.mul (K.mul (cN K (N + 1)) (cN K (N + 1))) (pwR K x ((N + 1) * (N + 1))))))))
    rw [ih, SU2CharExp.cN_mul K (N + 1) (N + 1)]
    generalize AltDiff.tU K (fun c => pwR K x (c * c)) N = T
    generalize V K x N = v
    generalize cN K (N + 1) = c
    generalize pwR K x ((N + 1) * (N + 1)) = p
    show K.add (K.add T (K.neg (K.mul (K.add K.one (K.neg (K.mul x x))) v)))
        (K.mul (K.add K.one K.one) (K.mul p (K.add K.one (K.neg (K.mul (K.mul c c) (K.add K.one (K.neg (K.mul x x))))))))
      = K.add (K.add T (K.mul (K.add K.one K.one) p))
        (K.neg (K.mul (K.add K.one (K.neg (K.mul x x))) (K.add v (K.mul (K.add K.one K.one) (K.mul (K.mul c c) p)))))
    lib_ring

/-- **F part ≥ slack sum** -/
theorem F_low (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (N : Nat) :
    O.le (W K x N) (AltDiff.tU K (fF K x) N) := by
  have v := var_low K O h1 x hx0 hx1 N
  have b1 := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 (N + 1)) (ThetaDup5.pw_nn K O x hx0 h1 (N * N))
  have b2 := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 N) (ThetaDup5.pw_nn K O x hx0 h1 ((N + 1) * (N + 1)))
  rw [fF_eq K x N]
  generalize AltDiff.tU K (fun c => pwR K x (c * c)) N = T at v ⊢
  generalize K.mul (K.add K.one (K.neg (K.mul x x))) (V K x N) = Z at v ⊢
  generalize K.mul (cN K (N + 1)) (pwR K x (N * N)) = B1 at v b1
  generalize K.mul (cN K N) (pwR K x ((N + 1) * (N + 1))) = B2 at v b2
  generalize W K x N = w at v ⊢
  apply ThetaLow.le_of_sub K O
  have s := AltSum.le_sub K O _ _ v
  have t := O.add_le_add _ _ _ _ s (O.add_le_add _ _ _ _ b1 b2)
  rw [K.zero_add, K.zero_add] at t
  rw [show K.add (K.add T (K.neg Z)) (K.neg w)
    = K.add (K.add T (K.neg (K.add (K.add Z (K.add B1 B2)) w))) (K.add B1 B2) by lib_ring]
  exact t

/-- T_N(gA x) = (1 + x²) T_N(c² x^{c²}) − ϑ₄⁽ᴺ⁾(x) -/
theorem gA_eq (x : R) : ∀ N, AltDiff.tS K (gA K x) N
    = K.add (K.mul (K.add K.one (K.mul x x)) (AltDiff.tS K (fun c => K.mul (cN K (c * c)) (pwR K x (c * c))) N))
        (K.neg (AltDiff.tS K (fun c => pwR K x (c * c)) N))
  | 0 => by
    show K.mul (pwR K x (0 * 0)) (K.add (K.mul (K.add K.one (K.mul x x)) (cN K (0 * 0))) (K.neg K.one))
      = K.add (K.mul (K.add K.one (K.mul x x)) (K.mul (cN K (0 * 0)) (pwR K x (0 * 0)))) (K.neg (pwR K x (0 * 0)))
    generalize pwR K x (0 * 0) = p
    generalize cN K (0 * 0) = c
    lib_ring
  | N + 1 => by
    have ih := gA_eq x N
    show K.add (AltDiff.tS K (gA K x) N) (K.mul (AltDiff.two K) (K.mul (AltDiff.sg K (N + 1))
        (K.mul (pwR K x ((N + 1) * (N + 1))) (K.add (K.mul (K.add K.one (K.mul x x)) (cN K ((N + 1) * (N + 1)))) (K.neg K.one)))))
      = K.add (K.mul (K.add K.one (K.mul x x)) (K.add (AltDiff.tS K (fun c => K.mul (cN K (c * c)) (pwR K x (c * c))) N)
            (K.mul (AltDiff.two K) (K.mul (AltDiff.sg K (N + 1)) (K.mul (cN K ((N + 1) * (N + 1))) (pwR K x ((N + 1) * (N + 1))))))))
        (K.neg (K.add (AltDiff.tS K (fun c => pwR K x (c * c)) N)
          (K.mul (AltDiff.two K) (K.mul (AltDiff.sg K (N + 1)) (pwR K x ((N + 1) * (N + 1)))))))
    rw [ih]
    generalize AltDiff.tS K (fun c => K.mul (cN K (c * c)) (pwR K x (c * c))) N = B
    generalize AltDiff.tS K (fun c => pwR K x (c * c)) N = b
    generalize AltDiff.sg K (N + 1) = g
    generalize cN K ((N + 1) * (N + 1)) = c
    generalize pwR K x ((N + 1) * (N + 1)) = p
    show K.add (K.add (K.mul (K.add K.one (K.mul x x)) B) (K.neg b))
        (K.mul (K.add K.one K.one) (K.mul g (K.mul p (K.add (K.mul (K.add K.one (K.mul x x)) c) (K.neg K.one)))))
      = K.add (K.mul (K.add K.one (K.mul x x)) (K.add B (K.mul (K.add K.one K.one) (K.mul g (K.mul c p)))))
        (K.neg (K.add b (K.mul (K.add K.one K.one) (K.mul g p))))
    lib_ring

/-- **n = 0 lower bound**: 4 Σ pairT ≥ W(2J+1) + (1 + x²) T(c² x^{c²}) − ϑ₄ -/
theorem n0_low (h1 : O.le K.zero K.one) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one) (J : Nat) :
    O.le (K.add (W K x (2 * J + 1))
        (K.add (K.mul (K.add K.one (K.mul x x)) (AltDiff.tS K (fun c => K.mul (cN K (c * c)) (pwR K x (c * c))) (2 * J + 1)))
          (K.neg (AltDiff.tS K (fun c => pwR K x (c * c)) (2 * J + 1)))))
      (K.mul (K.mul (AltDiff.two K) (AltDiff.two K)) (sumT K x J)) := by
  rw [← split K x J, ← gA_eq K x (2 * J + 1)]
  exact O.add_le_add _ _ _ _ (F_low K O h1 x hx0 hx1 (2 * J + 1)) (O.le_refl _)

end N0Link

#print axioms N0Link.fF_eq
#print axioms N0Link.F_low
#print axioms N0Link.gA_eq
#print axioms N0Link.n0_low

#print axioms: 4 theorems, no axioms.

90.54 Exponential decay in the cone

Inside Cone(\(2^{-8}\)) every Migdal step gains a factor \(2^{-4}\) in the cone rate.

\[ \mathrm {Bd}_{2^{-8}}(1{::}a)\ \Rightarrow \ \forall k:\ \mathrm {Bd}_{2^{-(4k+8)}}(1{::}\mathrm {mkIt}^k a) \]

ym/MigdalDecay.lean

/-
  ym/MigdalDecay — T45, G27: exponential decay inside the strong-coupling cone. With h = ½, one 4D Migdal step maps
  Cone(h^{4p+8}) into Cone(h^{4p+12}) (mk_cone with r = h^{4p+8}, r₁ = h^{2p+5}, r₂ = h^{p+3}); hence a flow that starts
  in Cone(2⁻⁸) lies in Cone(2^{-(4k+8)}) after k steps: every normalised character ratio decays like 2^{-4k}.
  Lean 4 core only, no axioms.
-/
import ym.MigdalCone
import lib.NatLe

namespace MigdalDecay

open YMChain LinkIntegral SU2Char ConeBound MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **One Migdal step gains a factor 2⁻⁴ in the cone rate:** Cone(h^{4p+8}) → Cone(h^{4p+12}). -/
theorem mk_gain (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (p : Nat) (a : List R) (ha : Bd K O (pwR K h (4 * p + 8)) 0 (K.one :: a)) (L : Nat) :
    Bd K O (pwR K h (4 * p + 12)) 0 (mk K O F L a) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have th : K.mul (SU2Strong4D.two K) h = K.one := by
    show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
  have fh : K.mul (four K) (K.mul h h) = K.one := by
    show K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) (K.mul h h) = K.one
    have : K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) (K.mul h h)
        = K.mul (K.mul (SU2Strong4D.two K) h) (K.mul (SU2Strong4D.two K) h) := by lib_ring
    rw [this, th, K.one_mul]
  have pn : ∀ n, O.le K.zero (pwR K h n) := fun n => AreaLower.pw_nn K O h h0 h1 n
  have pa : ∀ m n, m ≤ n → O.le (pwR K h n) (pwR K h m) := fun m n hmn => pw_anti K O h1 h h0 hle n m hmn
  have four_h : ∀ k, K.mul (four K) (pwR K h (k + 2)) = pwR K h k := by
    intro k
    show K.mul (four K) (K.mul (K.mul (pwR K h k) h) h) = pwR K h k
    have : K.mul (four K) (K.mul (K.mul (pwR K h k) h) h) = K.mul (pwR K h k) (K.mul (four K) (K.mul h h)) := by lib_ring
    rw [this, fh, K.mul_comm, K.one_mul]
  have e0 : 4 * p + 8 = (4 * p + 6) + 2 := by lib_nat
  have e1 : 2 * p + 5 = (2 * p + 3) + 2 := by lib_nat
  have fr : O.le (K.mul (four K) (pwR K h (4 * p + 8))) K.one := by
    rw [e0, four_h]; exact pa 0 _ (Nat.zero_le _)
  have fr1 : O.le (K.mul (four K) (pwR K h (2 * p + 5))) K.one := by
    rw [e1, four_h]; exact pa 0 _ (Nat.zero_le _)
  have s1 : O.le (K.mul (four K) (pwR K (pwR K h (4 * p + 8)) 3)) (pwR K (pwR K h (2 * p + 5)) 4) := by
    have e : (4 * p + 8) * 3 = (12 * p + 22) + 2 := by lib_nat
    rw [Migdal.pw_pw, Migdal.pw_pw, e, four_h]
    exact pa _ _ (by lib_nat_le)
  have s2 : O.le (K.mul (four K) (pwR K (pwR K h (2 * p + 5)) 3)) (pwR K (pwR K h (p + 3)) 4) := by
    have e : (2 * p + 5) * 3 = (6 * p + 13) + 2 := by lib_nat
    rw [Migdal.pw_pw, Migdal.pw_pw, e, four_h]
    exact pa _ _ (by lib_nat_le)
  have b := mk_cone K O F h1 h10 (pwR K h (4 * p + 8)) (pwR K h (2 * p + 5)) (pwR K h (p + 3)) (pn _) fr (pn _) fr1
    (pn _) s1 s2 a ha L L
  have e : (p + 3) * 4 = 4 * p + 12 := by lib_nat
  rw [Migdal.pw_pw, e] at b
  exact b

/-- **Exponential decay of the Migdal flow inside the cone:** if 1 :: a ∈ Cone(2⁻⁸), then after k steps the flow is in
  Cone(2^{-(4k+8)}). -/
theorem mk_decay (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (a : List R) (ha : Bd K O (pwR K h 8) 0 (K.one :: a)) (L : Nat) :
    ∀ k, Bd K O (pwR K h (4 * k + 8)) 0 (K.one :: mkIt K O F L a k)
  | 0 => by
    have e : 4 * 0 + 8 = 8 := by lib_nat
    rw [e]; exact ha
  | k + 1 => by
    have ih := mk_decay h1 h10 h hh h0 a ha L k
    have g := mk_gain K O F h1 h10 h hh h0 k _ ih L
    rw [mk_head K O F h1 h10 _ _ ih L] at g
    have e : 4 * (k + 1) + 8 = 4 * k + 12 := by lib_nat
    rw [e]; exact g

end MigdalDecay

#print axioms MigdalDecay.mk_gain
#print axioms MigdalDecay.mk_decay

#print axioms: 2 theorems, no axioms.

90.55 Activity decay along the flow

After \(k\) Migdal steps from Cone(\(2^{-8}\)) the non-trivial part of the weight is at most \(2\cdot 2^{-(16k+30)}\).

\[ \mathrm {Bd}_{2^{-8}}(1{::}a),\ |\chi _n|\le n+1\ \Rightarrow \ \Big |\sum _{n\ge 1}(\mathrm {mkIt}^k a)_n\chi _n\Big |\le 2\cdot 2^{-(16k+30)} \]

ym/ActivityDecay.lean

/-
  ym/ActivityDecay — T45, G29: the polymer activity of the Migdal flow decays exponentially. If 1 :: a ∈ Cone(2⁻⁸), then
  after k steps the non-trivial part f = Σ_{n≥1} a_n χ_n of the weight obeys |f| ≤ 2·2^{-(16k+30)} for every family χ with
  |χ_n| ≤ n+1 (MigdalDecay.mk_decay + ConeGap.activity with r = 2^{-(4k+8)}, x = 4r⁴ = 2^{-(16k+30)}).
  Lean 4 core only, no axioms.
-/
import ym.MigdalDecay
import ym.ConeGap

namespace ActivityDecay

open YMChain LinkIntegral SU2Char ConeBound MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem four_h (h : R) (hh : K.add h h = K.one) (k : Nat) : K.mul (four K) (pwR K h (k + 2)) = pwR K h k := by
  have th : K.mul (SU2Strong4D.two K) h = K.one := by
    show K.mul (K.add K.one K.one) h = K.one; rw [DetChain.right_distrib, K.one_mul, hh]
  have fh : K.mul (four K) (K.mul h h) = K.one := by
    show K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) (K.mul h h) = K.one
    have : K.mul (K.mul (SU2Strong4D.two K) (SU2Strong4D.two K)) (K.mul h h)
        = K.mul (K.mul (SU2Strong4D.two K) h) (K.mul (SU2Strong4D.two K) h) := by lib_ring
    rw [this, th, K.one_mul]
  show K.mul (four K) (K.mul (K.mul (pwR K h k) h) h) = pwR K h k
  have : K.mul (four K) (K.mul (K.mul (pwR K h k) h) h) = K.mul (pwR K h k) (K.mul (four K) (K.mul h h)) := by lib_ring
  rw [this, fh, K.mul_comm, K.one_mul]

theorem half_add (h : R) (hh : K.add h h = K.one) (n : Nat) : K.add (pwR K h (n + 1)) (pwR K h (n + 1)) = pwR K h n := by
  show K.add (K.mul (pwR K h n) h) (K.mul (pwR K h n) h) = pwR K h n
  have d : K.add (K.mul (pwR K h n) h) (K.mul (pwR K h n) h) = K.mul (pwR K h n) (K.add h h) := by lib_ring
  rw [d, hh, K.mul_comm, K.one_mul]

/-- **Activity decay along the flow:** |Σ_{n≥1} a_n^{(k)} χ_n| ≤ 2·2^{-(16k+30)}. -/
theorem activity_decay (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (a : List R) (ha : Bd K O (pwR K h 8) 0 (K.one :: a)) (L k : Nat) (χ : Nat → R)
    (hχ : ∀ j, O.le (K.neg (cN K (j + 1))) (χ j) ∧ O.le (χ j) (cN K (j + 1))) :
    O.le (K.neg (K.add (pwR K h (16 * k + 30)) (pwR K h (16 * k + 30)))) (SU2Char.dot K 1 (mkIt K O F L a k) χ) ∧
      O.le (SU2Char.dot K 1 (mkIt K O F L a k) χ) (K.add (pwR K h (16 * k + 30)) (pwR K h (16 * k + 30))) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have b := MigdalDecay.mk_decay K O F h1 h10 h hh h0 a ha L k
  have hx : K.mul (four K) (pwR K (pwR K h (4 * k + 8)) 4) = pwR K h (16 * k + 30) := by
    have e : (4 * k + 8) * 4 = (16 * k + 30) + 2 := by lib_nat
    rw [Migdal.pw_pw, e, four_h K h hh]
  have hxx : O.le (K.add (pwR K h (16 * k + 30)) (pwR K h (16 * k + 30))) K.one := by
    have e : 16 * k + 30 = (16 * k + 29) + 1 := by lib_nat
    rw [e, half_add K h hh]
    exact pw_anti K O h1 h h0 hle _ 0 (Nat.zero_le _)
  have r := ConeGap.activity K O h1 (pwR K h (4 * k + 8)) _ (AreaLower.pw_nn K O h h0 h1 _) hx hxx 1 _ b.2 χ hχ
  have p1 : pwR K (pwR K h (16 * k + 30)) 1 = pwR K h (16 * k + 30) := by
    show K.mul K.one (pwR K h (16 * k + 30)) = _; rw [K.one_mul]
  rw [p1] at r; exact r

end ActivityDecay

#print axioms ActivityDecay.four_h
#print axioms ActivityDecay.half_add
#print axioms ActivityDecay.activity_decay

#print axioms: 3 theorems, no axioms.

90.56 Clustering improves along the flow

After \(j\) Migdal steps from Cone(\(2^{-8}\)) the weight clusters with any rate \(2^{-(ad+1)}\) such that \(k+ad+7\le 16j+29\).

\[ \mathrm {Bd}_{2^{-8}}(1{::}a),\ 8d\le 2^{k+ad},\ k+ad+7\le 16j+29\ \Rightarrow \ |\langle U_pU_q\rangle _{w_j}-\langle U_p\rangle _{w_j}\langle U_q\rangle _{w_j}|\le B\,2^{-R} \]

ym/GapConeJ.lean

/-
  ym/GapConeJ — T45, G30: GapCone8.gap_of_cone8 for cf in Cone(2^{-(4j+8)}) — the cone reached after j Migdal steps
  (MigdalDecay.mk_decay). The admissible decay exponent grows with j: k + ad + 7 ≤ 16j + 29, so the clustering rate
  2^{-(ad+1)} per unit distance improves by 2⁻¹⁶ per step (gap_flow_j for the flowed weight).
  Lean 4 core only, no axioms.
-/
import ym.CauchyCone8
import ym.GapConeK
import ym.MigdalDecay
import ym.ActivityDecay

namespace GapConeJ

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Cone(2⁻⁸) ⇒ exponential clustering in every finite volume**, for the weight of `cauchy_of_cone8`. -/
theorem gap_of_coneJ (d k ad j : Nat) (hka : k + ad + 7 ≤ 16 * j + 29) (hd8 : 4 * (2 * d) ≤ 2 ^ (k + ad))
    (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (cf : List R) (hcf : ConeBound.Bd K O (pwR K h (4 * j + 8)) 0 (K.one :: cf))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + ad + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) := by
  intro f0
  have hl := GapInputs.hle_one K O h1 h hh h0
  have hx : K.mul (MigdalCone.four K) (pwR K (pwR K h (4 * j + 8)) 4) = pwR K h (16 * j + 30) := by
    have e : (4 * j + 8) * 4 = (16 * j + 30) + 2 := by lib_nat
    rw [Migdal.pw_pw, e, ActivityDecay.four_h K h hh]
  have e30 : 16 * j + 30 = (16 * j + 29) + 1 := by lib_nat
  have x2 : K.add (pwR K h (16 * j + 30)) (pwR K h (16 * j + 30)) = pwR K h (16 * j + 29) := by
    rw [e30]; exact ActivityDecay.half_add K h hh _
  have hxx : O.le (K.add (pwR K h (16 * j + 30)) (pwR K h (16 * j + 30))) K.one := by
    rw [x2]; exact ConeBound.pw_anti K O h1 h h0 hl _ 0 (Nat.zero_le _)
  have epsle : O.le (pwR K h (16 * j + 29)) (pwR K h (k + ad + 7)) := ConeBound.pw_anti K O h1 h h0 hl _ _ hka
  have hf0 : ∀ i x, O.le (K.neg (pwR K h (k + ad + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + ad + 7)) := fun i x => by
    have := ConeGap.activity K O h1 (pwR K h (4 * j + 8)) (pwR K h (16 * j + 30)) (AreaLower.pw_nn K O h h0 h1 _) hx hxx 1
      _ hcf.2 (ChiBound.c K (tr i x)) (ChiBound.chi_bound K O h1 (tr i x) (htr i x))
    rw [show pwR K (pwR K h (16 * j + 30)) 1 = K.mul K.one (pwR K h (16 * j + 30)) from rfl, K.one_mul, x2] at this
    exact ⟨O.le_trans _ _ _ (ChiBound.neg_le K O _ _ epsle) this.1, O.le_trans _ _ _ this.2 epsle⟩
  have hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i) :=
    fun i c c' hcc => by show SU2Char.dot K 1 _ (ChiBound.c K (tr i c)) = _; rw [htd i c c' hcc]
  exact GapConeK.gap_cone_k K O F H d (k + ad) hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq f0 Up Uq K1 Rr
    hfd hUp hUq (fun i => GapFlow.adm_dot K O H (tr i) (hta i) 1 _) hUpa hUqa hf0 hK1 hUpb hUqb hdist M hM

/-- **Clustering for the flowed weight after j Migdal steps**, rate improving with j. -/
theorem gap_flow_j (d k ad j : Nat) (hka : k + ad + 7 ≤ 16 * j + 29) (hd8 : 4 * (2 * d) ≤ 2 ^ (k + ad))
    (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (a : List R) (ha : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: a)) (Lm : Nat)
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F Lm a j) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + ad + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))) :=
  gap_of_coneJ K O F H d k ad j hka hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq (MigdalCone.mkIt K O F Lm a j)
    (MigdalDecay.mk_decay K O F h1 h10 h hh h0 a ha Lm j) tr Up Uq K1 Rr htd hUp hUq hta hUpa hUqa htr hK1 hUpb hUqb hdist M hM

end GapConeJ

#print axioms GapConeJ.gap_of_coneJ
#print axioms GapConeJ.gap_flow_j

#print axioms: 2 theorems, no axioms.

90.57 Cauchy estimate along the flow

After \(j\) Migdal steps from Cone(\(2^{-8}\)) the infinite-volume Cauchy estimate holds for every \(k+ad+7\le 16j+29\).

\[ \mathrm {Bd}_{2^{-8}}(1{::}a),\ k+ad+7\le 16j+29\ \Rightarrow \ |a_n-a_0|\le T(w_j) \]

ym/CauchyConeJ.lean

/-
  ym/CauchyConeJ — T45, G31: CauchyCone8.cauchy_of_cone8 for cf in Cone(2^{-(4j+8)}), the cone reached after j Migdal steps
  (MigdalDecay.mk_decay): the weight is within 2^{-(16j+29)} of 1, so the uniform infinite-volume Cauchy estimate holds
  whenever k + ad + 7 ≤ 16j + 29; cauchy_flow_j for the flowed weight. Lean 4 core only, no axioms.
-/
import ym.WilsonCauchyD
import ym.SupportVec
import ym.MigdalDecay
import ym.ActivityDecay

namespace CauchyConeJ

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Cone(2^{-(4j+8)}) ⇒ uniform infinite-volume Cauchy estimate** (plaquette weight 1 + Σ_{n≥1} cf_n χ_n(tr)). -/
theorem cauchy_of_coneJ (d k ad j : Nat) (hka : k + ad + 7 ≤ 16 * j + 29) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (cf : List R) (hcf : ConeBound.Bd K O (pwR K h (4 * j + 8)) 0 (K.one :: cf))
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hq : ∀ i, i < n → nthL (Pn.map (vec LL)) (y i) = vec LL (q i))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (plaq : Nat → Pq) (hndy : nodupL ((ShellSum.rng n).map y))
    (hshell : ∀ kk z, memL z ((ShellSum.rng n).map y) → sh z = kk → (plaq z).1.length = p0.1.length ∧
      dcl (kk + 2) p0.1 (plaq z).1 ∧ (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < d)
    (hinj : ∀ z z', memL z ((ShellSum.rng n).map y) → memL z' ((ShellSum.rng n).map y) → plaq z = plaq z' → z = z')
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) := by
  intro f0
  have hl := GapInputs.hle_one K O h1 h hh h0
  have hx : K.mul (MigdalCone.four K) (pwR K (pwR K h (4 * j + 8)) 4) = pwR K h (16 * j + 30) := by
    have e : (4 * j + 8) * 4 = (16 * j + 30) + 2 := by lib_nat
    rw [Migdal.pw_pw, e, ActivityDecay.four_h K h hh]
  have e30 : 16 * j + 30 = (16 * j + 29) + 1 := by lib_nat
  have x2 : K.add (pwR K h (16 * j + 30)) (pwR K h (16 * j + 30)) = pwR K h (16 * j + 29) := by
    rw [e30]; exact ActivityDecay.half_add K h hh _
  have hxx : O.le (K.add (pwR K h (16 * j + 30)) (pwR K h (16 * j + 30))) K.one := by
    rw [x2]; exact ConeBound.pw_anti K O h1 h h0 hl _ 0 (Nat.zero_le _)
  have epsle : O.le (pwR K h (16 * j + 29)) (pwR K h (k + ad + 7)) := ConeBound.pw_anti K O h1 h h0 hl _ _ hka
  have hf0 : ∀ i x, O.le (K.neg (pwR K h (k + ad + 7))) (f0 i x) ∧ O.le (f0 i x) (pwR K h (k + ad + 7)) := fun i x => by
    have := ConeGap.activity K O h1 (pwR K h (4 * j + 8)) (pwR K h (16 * j + 30)) (AreaLower.pw_nn K O h h0 h1 _) hx hxx 1
      _ hcf.2 (ChiBound.c K (tr i x)) (ChiBound.chi_bound K O h1 (tr i x) (htr i x))
    rw [show pwR K (pwR K h (16 * j + 30)) 1 = K.mul K.one (pwR K h (16 * j + 30)) from rfl, K.one_mul, x2] at this
    exact ⟨O.le_trans _ _ _ (ChiBound.neg_le K O _ _ epsle) this.1, O.le_trans _ _ _ this.2 epsle⟩
  have hfd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (f0 i) :=
    fun i c c' hcc => by show SU2Char.dot K 1 _ (ChiBound.c K (tr i c)) = _; rw [htd i c c' hcc]
  have hUq' : ∀ i, i < n → depOn ((vec LL (q i)).map nzb) (fun x => K.add (F.inv (K.add K.one (pwR K h (k + ad + 7))))
      (K.mul (F.inv (K.add K.one (pwR K h (k + ad + 7)))) (f0 (y i) x))) := fun i hi c c' hcc => by
    show K.add _ (K.mul _ (SU2Char.dot K 1 _ (ChiBound.c K (tr (y i) c)))) = _
    rw [SupportVec.depOn_vec L LL hL Pn (y i) (q i) (hq i hi) (tr (y i)) (htd (y i)) c c' hcc]
  exact WilsonCauchyD.wilson_cauchy_d K O F H d k ad hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 f0 Ob K1 hfd hUp
    (fun i => GapFlow.adm_dot K O H (tr i) (hta i) 1 _) hUpa hf0 hK1 hUpb n y q sh Ms hMs hlen hyl hyM hpq hUq' hdist
    Kk N hsh plaq hndy hshell hinj hr1

/-- **Cauchy estimate for the flowed weight after j Migdal steps.** -/
theorem cauchy_flow_j (d k ad j : Nat) (hka : k + ad + 7 ≤ 16 * j + 29) (hd8 : 4 * (2 * d) ≤ 2 ^ k) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (a0 : List R) (ha0 : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: a0)) (Lm : Nat)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hq : ∀ i, i < n → nthL (Pn.map (vec LL)) (y i) = vec LL (q i))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (plaq : Nat → Pq) (hndy : nodupL ((ShellSum.rng n).map y))
    (hshell : ∀ kk z, memL z ((ShellSum.rng n).map y) → sh z = kk → (plaq z).1.length = p0.1.length ∧
      dcl (kk + 2) p0.1 (plaq z).1 ∧ (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < d)
    (hinj : ∀ z z', memL z ((ShellSum.rng n).map y) → memL z' ((ShellSum.rng n).map y) → plaq z = plaq z' → z = z')
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F Lm a0 j) (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)) :=
  cauchy_of_coneJ K O F H d k ad j hka hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 (MigdalCone.mkIt K O F Lm a0 j)
    (MigdalDecay.mk_decay K O F h1 h10 h hh h0 a0 ha0 Lm j) tr Ob K1 htd hUp hta hUpa htr hK1 hUpb n y q sh Ms hMs hlen hyl
    hyM hpq hq hdist Kk N hsh plaq hndy hshell hinj hr1

end CauchyConeJ

#print axioms CauchyConeJ.cauchy_of_coneJ
#print axioms CauchyConeJ.cauchy_flow_j

#print axioms: 2 theorems, no axioms.

90.58 Z\(^4\): Cauchy estimate and clustering after \(j\) steps

On \(\mathbb Z^4\), after \(j\) Migdal steps from Cone(\(2^{-8}\)) one weight has both the Cauchy estimate and clustering with rate \(2^{-(7+16j)}\).

\[ \mathrm {Bd}_{2^{-8}}(1{::}a)\ \Rightarrow \ \forall j:\ \mathrm {Both}_{d=4,k=5,ad=6+16j}(\mathrm {mkIt}^j a) \]

ym/GapBothJ.lean

/-
  ym/GapBothJ — T45, G32: Cauchy estimate and clustering for the SAME weight after j Migdal steps (Both), from
  Cone(2^{-(4j+8)}) (both_of_coneJ), and on Z⁴ for the flow started in Cone(2⁻⁸) with k = 5 and clustering exponent
  ad = 6 + 16j (both_flow_d4_j): every numeric side condition discharged, the decay rate 2^{-(7+16j)} improving with j.
  Lean 4 core only, no axioms.
-/
import ym.GapConeJ
import ym.CauchyConeJ

set_option linter.unusedVariables false

namespace GapBothJ

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

theorem pow2_le_add (a : Nat) : ∀ b, 2 ^ a ≤ 2 ^ (a + b)
  | 0 => Nat.le_refl _
  | b + 1 => by
    have e : 2 ^ (a + (b + 1)) = 2 ^ (a + b) + 2 ^ (a + b) := by
      show 2 ^ (a + b) * 2 = 2 ^ (a + b) + 2 ^ (a + b); lib_nat
    exact Nat.le_trans (pow2_le_add a b) (LibNatLe.le_of_eq_add e)

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- Cauchy estimate (abstract volume order) and finite-volume clustering for the weight built from cf. -/
@[reducible] def Both (d k ad : Nat) (h : R) (cf : List R) : Prop :=
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (n : Nat) (y : Nat → Nat) (q : Nat → Pq) (sh : Nat → Nat) (Ms : Nat → List Bool)
    (hMs : ∀ i, Ms (i + 1) = upd (Ms i) (y i) true)
    (hlen : ∀ i, i < n → (Ms i).length = Pn.length) (hyl : ∀ i, i < n → y i < (Ms i).length)
    (hyM : ∀ i, i < n → nthB (Ms i) (y i) = false)
    (hpq : ∀ i, i < n → disjN (vec LL p0) (vec LL (q i)))
    (hq : ∀ i, i < n → nthL (Pn.map (vec LL)) (y i) = vec LL (q i))
    (hdist : ∀ i, i < n → ¬ dcl (sh (y i) + 1) p0.1 (q i).1)
    (Kk N : Nat) (hsh : ∀ i, i < n → Kk ≤ sh (y i) ∧ sh (y i) < N)
    (plaq : Nat → Pq) (hndy : nodupL ((ShellSum.rng n).map y))
    (hshell : ∀ kk z, memL z ((ShellSum.rng n).map y) → sh z = kk → (plaq z).1.length = p0.1.length ∧
      dcl (kk + 2) p0.1 (plaq z).1 ∧ (plaq z).2.1 < (plaq z).2.2 ∧ (plaq z).2.2 < d)
    (hinj : ∀ z z', memL z ((ShellSum.rng n).map y) → memL z' ((ShellSum.rng n).map y) → plaq z = plaq z' → z = z')
    (hr1 : O.le (K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))) K.one),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (Ms i) Ob) (ZxH K O H w (Ms i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * d) + 4 * (2 * d))))
      (pwR K (K.add K.one (pwR K h (k + 1))) (4 * (2 * d) + 4 * (2 * d))))
    let s := d * d * 5 ^ p0.1.length
    let r := K.mul (pwR K h (ad + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + ad + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) 

/-- Cone(2^{-(4j+8)}) coefficients ⇒ Cauchy estimate and clustering, for k + ad + 7 ≤ 16j + 29. -/
theorem both_of_coneJ (d k ad j : Nat) (hka : k + ad + 7 ≤ 16 * j + 29) (hd8 : 4 * (2 * d) ≤ 2 ^ k)
    (hd8' : 4 * (2 * d) ≤ 2 ^ (k + ad)) (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (cf : List R) (hcf : ConeBound.Bd K O (pwR K h (4 * j + 8)) 0 (K.one :: cf)) :
    Both K O F H d k ad h cf := by
  refine ⟨fun LL hLL hL Pn hnd hv p0 => CauchyConeJ.cauchy_of_coneJ K O F H d k ad j hka hd8 hd16 DH h1 h10 h hh h0
    LL hLL hL Pn hnd hv p0 _ hcf, ?_⟩
  intro LL hLL hL Pn hnd hv p0 q0 hpq
  exact GapConeJ.gap_of_coneJ K O F H d k ad j hka hd8' hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq cf hcf

/-- **Z⁴, after j Migdal steps from Cone(2⁻⁸): Cauchy estimate and clustering with exponent ad = 6 + 16j.** -/
theorem both_flow_d4_j (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (a0 : List R) (ha0 : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: a0)) (Lm j : Nat) :
    Both K O F H 4 5 (6 + 16 * j) h (MigdalCone.mkIt K O F Lm a0 j) := by
  have hka : 5 + (6 + 16 * j) + 7 ≤ 16 * j + 29 := by lib_nat_le
  have hd8' : 4 * (2 * 4) ≤ 2 ^ (5 + (6 + 16 * j)) :=
    Nat.le_trans (by decide) (pow2_le_add 5 (6 + 16 * j))
  have e : 5 + (6 + 16 * j) + 2 = 7 + (6 + 16 * j) := by lib_nat
  have hd16 : 2 * (4 * (2 * 4) + 1) ≤ 2 ^ (5 + (6 + 16 * j) + 2) := by
    rw [e]; exact Nat.le_trans (by decide) (pow2_le_add 7 (6 + 16 * j))
  exact both_of_coneJ K O F H 4 5 (6 + 16 * j) j hka (by decide) hd8' hd16 DH h1 h10 h hh h0 _
    (MigdalDecay.mk_decay K O F h1 h10 h hh h0 a0 ha0 Lm j)

end GapBothJ

#print axioms GapBothJ.pow2_le_add
#print axioms GapBothJ.both_of_coneJ
#print axioms GapBothJ.both_flow_d4_j

#print axioms: 3 theorems, no axioms.

90.59 Z\(^4\) box order after \(j\) steps

The box-order Cauchy estimate and clustering with rate \(2^{-(7+16j)}\) hold for the weight after \(j\) Migdal steps from Cone(\(2^{-8}\)).

\[ \mathrm {Bd}_{2^{-8}}(1{::}a)\ \Rightarrow \ \forall j:\ \text {Cauchy}_{\mathrm {box}}(w_j)\wedge \text {Clustering}_{2^{-(7+16j)}}(w_j) \]

ym/GapBothBoxJ.lean

/-
  ym/GapBothBoxJ — T45, G33: Z⁴, flow started in Cone(2⁻⁸): after j Migdal steps the SAME weight has the box-order
  infinite-volume Cauchy estimate and clustering, k = 5, clustering exponent ad = 6 + 16j (decay 2^{-(7+16j)});
  hr1 from R7.r7_le and h^{7+16j} ≤ h⁷. Lean 4 core only, no axioms.
-/
import ym.GapBothJ
import ym.CauchyBoxJ
import ym.R7

set_option linter.unusedVariables false

namespace GapBothBoxJ

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Z⁴, after j Migdal steps from Cone(2⁻⁸)**: box-order Cauchy estimate and clustering with decay 2^{-(7+16j)}. -/
theorem both_box_flow_d4_j (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (a0 : List R) (ha0 : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: a0)) (Lm j : Nat) :
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (hp0 : p0.1.length = 4)
    (n : Nat) (y : Nat → Nat) (M0 : List Bool) (hM0 : M0.length = Pn.length)
    (hyl : ∀ j, y j < Pn.length) (hoff : ∀ i, i < n → nthB M0 (y i) = false)
    (hyinj : ∀ i j, i < n → j < n → y i = y j → i = j)
    (Kk N : Nat) (hfar : ∀ i, i < n → Kk + 2 ≤ LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 ∧
      LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 < N + 2),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F Lm a0 j) (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (5 + (6 + 16 * j) + 7)))
    let κ := K.add K.one (pwR K h (5 + (6 + 16 * j) + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (VolOrder.Ms M0 y i) Ob) (ZxH K O H w (VolOrder.Ms M0 y i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * 4) + 4 * (2 * 4))))
      (pwR K (K.add K.one (pwR K h (5 + 1))) (4 * (2 * 4) + 4 * (2 * 4))))
    let s := 4 * 4 * 5 ^ p0.1.length
    let r := K.mul (pwR K h (6 + 16 * j + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (MigdalCone.mkIt K O F Lm a0 j) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (5 + (6 + 16 * j) + 7)))
    let κ := K.add K.one (pwR K h (5 + (6 + 16 * j) + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (5 + (6 + 16 * j) + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) := by
  have hka : 5 + (6 + 16 * j) + 7 ≤ 16 * j + 29 := by lib_nat_le
  have hd8' : 4 * (2 * 4) ≤ 2 ^ (5 + (6 + 16 * j)) :=
    Nat.le_trans (by decide) (GapBothJ.pow2_le_add 5 (6 + 16 * j))
  have e : 5 + (6 + 16 * j) + 2 = 7 + (6 + 16 * j) := by lib_nat
  have hd16 : 2 * (4 * (2 * 4) + 1) ≤ 2 ^ (5 + (6 + 16 * j) + 2) := by
    rw [e]; exact Nat.le_trans (by decide) (GapBothJ.pow2_le_add 7 (6 + 16 * j))
  have hcf := MigdalDecay.mk_decay K O F h1 h10 h hh h0 a0 ha0 Lm j
  refine ⟨fun LL hLL hL Pn hnd hv p0 tr Ob K1 htd hUp hta hUpa htr hK1 hUpb hp0 n y M0 hM0 hyl hoff hyinj Kk N hfar => ?_,
    (GapBothJ.both_of_coneJ K O F H 4 5 (6 + 16 * j) j hka (by decide) hd8' hd16 DH h1 h10 h hh h0 _ hcf).2⟩
  have hl := GapInputs.hle_one K O h1 h hh h0
  have hr1 : O.le (K.mul (pwR K h (6 + 16 * j + 1)) (natR K (3 ^ p0.1.length))) K.one := by
    rw [hp0]
    have m := ConeBound.pw_anti K O h1 h h0 hl (6 + 16 * j + 1) 7 (by lib_nat_le)
    have n0 : O.le K.zero (natR K (3 ^ 4)) := Geom.natR_nn K O h1 _
    exact O.le_trans _ _ _ (O.mul_le_mul_right _ _ _ m n0) (R7.r7_le K O h1 h hh h0)
  exact CauchyBoxJ.cauchy_boxJ K O F H 4 5 (6 + 16 * j) j hka (by decide) hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 _ hcf
    tr Ob K1 htd hUp hta hUpa htr hK1 hUpb hp0 n y M0 hM0 hyl hoff hyinj Kk N hfar hr1

end GapBothBoxJ

#print axioms GapBothBoxJ.both_box_flow_d4_j

#print axioms: 3 theorems, no axioms.

90.60 \(\beta \in [0,8]\): mass growing along the flow

For every \(\beta \in [0,8]\) the flow enters Cone(\(2^{-8}\)) at \(k_0\le 30\), and after \(k_0+j\) steps one weight has the box-order Cauchy estimate and clustering with rate \(2^{-(7+16j)}\).

\[ \forall m\ \forall t\in [0,4]\ \exists k_0\le 30\ \forall j:\ \text {Cauchy}_{\mathrm {box}}(w_{k_0+j})\wedge \text {Clustering}_{2^{-(7+16j)}}(w_{k_0+j}) \]

ym/FlowAfterJ.lean

/-
  ym/FlowAfterJ — T45, G34: the j-step results restarted at any step k where the flow enters Cone(2⁻⁸): mkIt_add
  (mkIt (k + j) = mkIt^j ∘ mkIt^k) and after_k — if 1 :: mkIt^k a ∈ Cone(2⁻⁸) then for every j the weight after k + j steps
  has the Z⁴ box-order Cauchy estimate and clustering with decay 2^{-(7+16j)} (BoxBoth j). Lean 4 core only, no axioms.
-/
import ym.GapBothBoxJ

set_option linter.unusedVariables false

namespace FlowAfterJ

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

theorem mkIt_add (Lm : Nat) (a : List R) (k : Nat) : ∀ j,
    MigdalCone.mkIt K O F Lm a (k + j) = MigdalCone.mkIt K O F Lm (MigdalCone.mkIt K O F Lm a k) j
  | 0 => rfl
  | j + 1 => by
    show (MigdalCone.mk K O F Lm (MigdalCone.mkIt K O F Lm a (k + j))).tail
      = (MigdalCone.mk K O F Lm (MigdalCone.mkIt K O F Lm (MigdalCone.mkIt K O F Lm a k) j)).tail
    rw [mkIt_add Lm a k j]

/-- Z⁴ box-order Cauchy estimate and clustering with exponent ad = 6 + 16j for the weight built from cf. -/
@[reducible] def BoxBoth (h : R) (j : Nat) (cf : List R) : Prop :=
    (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 : Pq)
    (tr : Nat → List G → R) (Ob : List G → R) (K1 : R)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Ob)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Ob)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.one K1) (hUpb : ∀ x, O.le (K.neg K1) (Ob x) ∧ O.le (Ob x) K1)
    (hp0 : p0.1.length = 4)
    (n : Nat) (y : Nat → Nat) (M0 : List Bool) (hM0 : M0.length = Pn.length)
    (hyl : ∀ j, y j < Pn.length) (hoff : ∀ i, i < n → nthB M0 (y i) = false)
    (hyinj : ∀ i j, i < n → j < n → y i = y j → i = j)
    (Kk N : Nat) (hfar : ∀ i, i < n → Kk + 2 ≤ LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 ∧
      LinfDist.dist p0.1 (VolOrder.nthP Pn (y i)).1 < N + 2),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let sw := F.inv (K.add K.one (pwR K h (5 + (6 + 16 * j) + 7)))
    let κ := K.add K.one (pwR K h (5 + (6 + 16 * j) + 4))
    let w : Nat → List G → R := fun i x => K.add sw (K.mul sw (f0 i x))
    let a : Nat → R := fun i => E K O F (ZxH K O H w (VolOrder.Ms M0 y i) Ob) (ZxH K O H w (VolOrder.Ms M0 y i) (fun _ => K.one))
    let C := K.mul h (K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) (4 * (2 * 4) + 4 * (2 * 4))))
      (pwR K (K.add K.one (pwR K h (5 + 1))) (4 * (2 * 4) + 4 * (2 * 4))))
    let s := 4 * 4 * 5 ^ p0.1.length
    let r := K.mul (pwR K h (6 + 16 * j + 1)) (natR K (3 ^ p0.1.length))
    let T := K.mul (K.mul (K.mul κ C) (natR K s)) (Geom.tail K r Kk N)
    O.le (K.neg T) (K.add (a n) (K.neg (a 0))) ∧ O.le (K.add (a n) (K.neg (a 0))) T ∧
      O.le (K.mul (K.add K.one (K.neg r)) T) (K.mul (K.mul (K.mul κ C) (natR K s)) (pwR K r Kk)))
    ∧ (∀ (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M : List Bool) (hM : M.length = Pn.length),
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (5 + (6 + 16 * j) + 7)))
    let κ := K.add K.one (pwR K h (5 + (6 + 16 * j) + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M Up) (ZxH K O H w M Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (5 + (6 + 16 * j) + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M (fun _ => K.one)) (ZxH K O H w M (fun _ => K.one)))))

/-- **Restart at the entrance step k:** Cone(2⁻⁸) at step k ⇒ BoxBoth j at step k + j, for every j. -/
theorem after_k (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (Lm : Nat) (a : List R) (k : Nat)
    (hk : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: MigdalCone.mkIt K O F Lm a k)) (j : Nat) :
    BoxBoth K O F H h j (MigdalCone.mkIt K O F Lm a (k + j)) := by
  rw [mkIt_add K O F Lm a k j]
  exact GapBothBoxJ.both_box_flow_d4_j K O F H DH h1 h10 h hh h0 _ hk Lm j

end FlowAfterJ

#print axioms FlowAfterJ.mkIt_add
#print axioms FlowAfterJ.after_k

ym/GapBothBoxFlow08J.lean

/-
  ym/GapBothBoxFlow08J — T45, G34: Z⁴, β ∈ [0,8], every truncation 40 + m: the flow enters Cone(2⁻⁸) at some step k₀ ≤ 30
  (Cover08T8B), and for every j the weight after k₀ + j steps has the box-order Cauchy estimate and clustering with decay
  2^{-(7+16j)} (FlowAfterJ.after_k): the lattice mass grows linearly along the flow. Lean 4 core only, no axioms.
-/
import ym.FlowAfterJ
import ym.Cover08T8B

namespace GapBothBoxFlow08J

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **Z⁴, β ∈ [0,8], every truncation:** ∃ k₀ ≤ 30, ∀ j, BoxBoth j at step k₀ + j (decay 2^{-(7+16j)}). -/
theorem both_box_flow08J (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (mm : Nat) :
    ∃ k0, k0 ≤ 30 ∧ ∀ j, FlowAfterJ.BoxBoth K O F H h j
      (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + mm) 11).tail (k0 + j)) := by
  obtain ⟨k0, hk, hk0⟩ := Cover08T8B.cover08T8 K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, hk, fun j => FlowAfterJ.after_k K O F H DH h1 h10 h hh h0 10 _ k0 (hk0 0) j⟩

end GapBothBoxFlow08J

#print axioms GapBothBoxFlow08J.both_box_flow08J

#print axioms: 3 theorems, no axioms.

90.61 Gram form of \(\chi _1,\chi _2\)

The crossing kernels of the first two non-trivial \(SU(2)\) characters are Gram kernels, hence reflection positive.

\[ 4\,(4(x\cdot y)^2-|x|^2|y|^2)=\sum _{a,b}(4x_ax_b-\delta _{ab}|x|^2)(4y_ay_b-\delta _{ab}|y|^2),\qquad \sum _{x,y}F(x)F(y)\,\chi _{1,2}(x,y)\ge 0 \]

ym/ChiGram.lean

/-
  ym/ChiGram — T45, G35 (first piece): for SU(2) = unit quaternions x ∈ R⁴, the crossing kernels of the first two
  non-trivial characters are Gram kernels with coefficient 1:
    chi1_gram   χ₁(g h†) = 2 (x·y)                    = ker of 8 coordinate functions
    chi2_gram   4·(4 (x·y)² − |x|²|y|²)               = Σ_{a,b} (4 x_a x_b − δ_ab |x|²)(4 y_a y_b − δ_ab |y|²)
  (on the unit sphere 4(x·y)² − 1 = U₂(x·y) = χ₂(g h†)), hence reflection positivity of both (rp_nonneg).
  Lean 4 core only, no axioms.
-/
import ym.WilsonGram
import lib.RingTac

namespace ChiGram

open YMChain LinkIntegral

variable {R : Type} (K : DetChain.CRing R)

abbrev Q4 (R : Type) := R × R × R × R

def c0 (x : Q4 R) : R := x.1
def c1 (x : Q4 R) : R := x.2.1
def c2 (x : Q4 R) : R := x.2.2.1
def c3 (x : Q4 R) : R := x.2.2.2
def cs : List (Q4 R → R) := [c0, c1, c2, c3]

def dot4 (x y : Q4 R) : R :=
  K.add (K.mul (c0 x) (c0 y)) (K.add (K.mul (c1 x) (c1 y)) (K.add (K.mul (c2 x) (c2 y)) (K.mul (c3 x) (c3 y))))

/-- Gram list with coefficient 1 from a list of functions -/
def gl (fs : List (Q4 R → R)) : List (R × (Q4 R → R)) := fs.map (fun f => (K.one, f))

theorem ker_gl (x y : Q4 R) : ∀ fs : List (Q4 R → R), ker K (gl K fs) x y = sumK K (fun f => K.mul (f x) (f y)) fs
  | [] => rfl
  | f :: fs => by
    show K.add (K.mul (K.mul K.one (f x)) (f y)) (ker K (gl K fs) x y) = K.add (K.mul (f x) (f y)) (sumK K _ fs)
    rw [ker_gl x y fs, K.one_mul]

theorem allNN_gl (P : Positivity K) : ∀ fs : List (Q4 R → R), AllNN K P (gl K fs)
  | [] => trivial
  | _ :: fs => ⟨by have := P.nn_sq K.one; rw [K.one_mul] at this; exact this, allNN_gl P fs⟩

/-- the 8 functions for χ₁ -/
def L1 : List (Q4 R → R) := cs ++ cs

theorem chi1_gram (x y : Q4 R) : ker K (gl K (L1 (R := R))) x y = K.add (dot4 K x y) (dot4 K x y) := by
  rw [ker_gl]
  show K.add (K.mul (c0 x) (c0 y)) (K.add (K.mul (c1 x) (c1 y)) (K.add (K.mul (c2 x) (c2 y)) (K.add (K.mul (c3 x) (c3 y))
    (K.add (K.mul (c0 x) (c0 y)) (K.add (K.mul (c1 x) (c1 y)) (K.add (K.mul (c2 x) (c2 y)) (K.add (K.mul (c3 x) (c3 y))
    K.zero))))))) = _
  unfold dot4; lib_ring

def four : R := K.add (K.add K.one K.one) (K.add K.one K.one)

/-- 4 x_a x_b − δ_ab |x|² -/
def tf (fa fb : Q4 R → R) (d : Bool) (x : Q4 R) : R :=
  cond d (K.add (K.mul (four K) (K.mul (fa x) (fb x))) (K.neg (dot4 K x x))) (K.mul (four K) (K.mul (fa x) (fb x)))

def L2 : List (Q4 R → R) :=
  [tf K c0 c0 true, tf K c0 c1 false, tf K c0 c2 false, tf K c0 c3 false,
   tf K c1 c0 false, tf K c1 c1 true, tf K c1 c2 false, tf K c1 c3 false,
   tf K c2 c0 false, tf K c2 c1 false, tf K c2 c2 true, tf K c2 c3 false,
   tf K c3 c0 false, tf K c3 c1 false, tf K c3 c2 false, tf K c3 c3 true]

theorem chi2_gram (x y : Q4 R) : ker K (gl K (L2 K)) x y =
    K.mul (four K) (K.add (K.mul (four K) (K.mul (dot4 K x y) (dot4 K x y))) (K.neg (K.mul (dot4 K x x) (dot4 K y y)))) := by
  rw [ker_gl]
  simp only [L2, sumK, tf, cond, four, dot4, c0, c1, c2, c3]
  lib_ring

/-- **Reflection positivity of the χ₁ and χ₂ crossing kernels.** -/
theorem chi12_rp (P : Positivity K) (F : Q4 R → R) (xs : List (Q4 R)) :
    P.nn (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (K.add (dot4 K x y) (dot4 K x y))) xs) xs) ∧
    P.nn (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (K.mul (four K)
      (K.add (K.mul (four K) (K.mul (dot4 K x y) (dot4 K x y))) (K.neg (K.mul (dot4 K x x) (dot4 K y y)))))) xs) xs) := by
  constructor
  · have h := rp_nonneg K P F xs _ (allNN_gl K P (L1 (R := R)))
    rw [sumK_ext K _ _ (fun x => sumK_ext K _ _ (fun y => by rw [chi1_gram K x y]) xs)] at h
    exact h
  · have h := rp_nonneg K P F xs _ (allNN_gl K P (L2 K))
    rw [sumK_ext K _ _ (fun x => sumK_ext K _ _ (fun y => by rw [chi2_gram K x y]) xs)] at h
    exact h

end ChiGram

#print axioms ChiGram.ker_gl
#print axioms ChiGram.allNN_gl
#print axioms ChiGram.chi1_gram
#print axioms ChiGram.chi2_gram
#print axioms ChiGram.chi12_rp

#print axioms: 5 theorems, no axioms.

90.62 Gram form of \(\chi _3\)

The \(\chi _3\) crossing kernel is a Gram kernel (traceless symmetric 3-tensors), hence reflection positive.

\[ 2\sum _{a,b,c}T'_{abc}(x)T'_{abc}(y)=9\,(8(x\cdot y)^3-4(x\cdot y)|x|^2|y|^2) \]

ym/ChiGram3.lean

/-
  ym/ChiGram3 — T45, G35 (n = 3): with the traceless symmetric 3-tensor T'_abc(x) = 6 x_a x_b x_c − |x|²(δ_ab x_c + δ_ac x_b + δ_bc x_a)
  in R⁴, 2 Σ T'(x)·T'(y) = 9 (8 (x·y)³ − 4 (x·y)|x|²|y|²) (= 9 U₃(x·y) = 9 χ₃(g h†) on S³): the χ₃ crossing kernel is a Gram
  kernel with coefficient 1 (each of the 64 functions listed twice), hence reflection positive. Lean 4 core only, no axioms.
-/
import ym.ChiGram

set_option maxRecDepth 200000

namespace ChiGram3

open YMChain LinkIntegral ChiGram

variable {R : Type} (K : DetChain.CRing R)

def L3a : List (Q4 R → R) :=
  [(fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c0 x) (c0 x)))) (K.neg (K.mul (dot4 K x x) (K.add (K.add (c0 x) (c0 x)) (c0 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c0 x) (c1 x)))) (K.neg (K.mul (dot4 K x x) ((c1 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c0 x) (c2 x)))) (K.neg (K.mul (dot4 K x x) ((c2 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c0 x) (c3 x)))) (K.neg (K.mul (dot4 K x x) ((c3 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c1 x) (c0 x)))) (K.neg (K.mul (dot4 K x x) ((c1 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c1 x) (c1 x)))) (K.neg (K.mul (dot4 K x x) ((c0 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c1 x) (c2 x)))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c1 x) (c3 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c2 x) (c0 x)))) (K.neg (K.mul (dot4 K x x) ((c2 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c2 x) (c1 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c2 x) (c2 x)))) (K.neg (K.mul (dot4 K x x) ((c0 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c2 x) (c3 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c3 x) (c0 x)))) (K.neg (K.mul (dot4 K x x) ((c3 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c3 x) (c1 x)))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c3 x) (c2 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c0 x) (K.mul (c3 x) (c3 x)))) (K.neg (K.mul (dot4 K x x) ((c0 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c0 x) (c0 x)))) (K.neg (K.mul (dot4 K x x) ((c1 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c0 x) (c1 x)))) (K.neg (K.mul (dot4 K x x) ((c0 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c0 x) (c2 x)))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c0 x) (c3 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c1 x) (c0 x)))) (K.neg (K.mul (dot4 K x x) ((c0 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c1 x) (c1 x)))) (K.neg (K.mul (dot4 K x x) (K.add (K.add (c1 x) (c1 x)) (c1 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c1 x) (c2 x)))) (K.neg (K.mul (dot4 K x x) ((c2 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c1 x) (c3 x)))) (K.neg (K.mul (dot4 K x x) ((c3 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c2 x) (c0 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c2 x) (c1 x)))) (K.neg (K.mul (dot4 K x x) ((c2 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c2 x) (c2 x)))) (K.neg (K.mul (dot4 K x x) ((c1 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c2 x) (c3 x)))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c3 x) (c0 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c3 x) (c1 x)))) (K.neg (K.mul (dot4 K x x) ((c3 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c3 x) (c2 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c1 x) (K.mul (c3 x) (c3 x)))) (K.neg (K.mul (dot4 K x x) ((c1 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c0 x) (c0 x)))) (K.neg (K.mul (dot4 K x x) ((c2 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c0 x) (c1 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c0 x) (c2 x)))) (K.neg (K.mul (dot4 K x x) ((c0 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c0 x) (c3 x)))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c1 x) (c0 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c1 x) (c1 x)))) (K.neg (K.mul (dot4 K x x) ((c2 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c1 x) (c2 x)))) (K.neg (K.mul (dot4 K x x) ((c1 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c1 x) (c3 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c2 x) (c0 x)))) (K.neg (K.mul (dot4 K x x) ((c0 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c2 x) (c1 x)))) (K.neg (K.mul (dot4 K x x) ((c1 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c2 x) (c2 x)))) (K.neg (K.mul (dot4 K x x) (K.add (K.add (c2 x) (c2 x)) (c2 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c2 x) (c3 x)))) (K.neg (K.mul (dot4 K x x) ((c3 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c3 x) (c0 x)))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c3 x) (c1 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c3 x) (c2 x)))) (K.neg (K.mul (dot4 K x x) ((c3 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c2 x) (K.mul (c3 x) (c3 x)))) (K.neg (K.mul (dot4 K x x) ((c2 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c0 x) (c0 x)))) (K.neg (K.mul (dot4 K x x) ((c3 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c0 x) (c1 x)))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c0 x) (c2 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c0 x) (c3 x)))) (K.neg (K.mul (dot4 K x x) ((c0 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c1 x) (c0 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c1 x) (c1 x)))) (K.neg (K.mul (dot4 K x x) ((c3 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c1 x) (c2 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c1 x) (c3 x)))) (K.neg (K.mul (dot4 K x x) ((c1 x))))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c2 x) (c0 x)))),
   (fun x => K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c2 x) (c1 x)))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c2 x) (c2 x)))) (K.neg (K.mul (dot4 K x x) ((c3 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c2 x) (c3 x)))) (K.neg (K.mul (dot4 K x x) ((c2 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c3 x) (c0 x)))) (K.neg (K.mul (dot4 K x x) ((c0 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c3 x) (c1 x)))) (K.neg (K.mul (dot4 K x x) ((c1 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c3 x) (c2 x)))) (K.neg (K.mul (dot4 K x x) ((c2 x))))),
   (fun x => K.add (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (c3 x) (K.mul (c3 x) (c3 x)))) (K.neg (K.mul (dot4 K x x) (K.add (K.add (c3 x) (c3 x)) (c3 x)))))]

def L3 : List (Q4 R → R) := L3a K ++ L3a K

def nine : R := K.add (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add (K.add K.one K.one) (K.add K.one K.one))) K.one

def U3h (x y : Q4 R) : R :=
  K.add (K.mul (K.add (four K) (four K)) (K.mul (dot4 K x y) (K.mul (dot4 K x y) (dot4 K x y))))
    (K.neg (K.mul (four K) (K.mul (dot4 K x y) (K.mul (dot4 K x x) (dot4 K y y)))))

theorem chi3_gram (x y : Q4 R) : ker K (gl K (L3 K)) x y = K.mul (nine K) (U3h K x y) := by
  rw [ker_gl]
  simp only [L3, L3a, List.cons_append, List.nil_append, sumK, U3h, nine, four, dot4, c0, c1, c2, c3]
  lib_ring

/-- **Reflection positivity of the χ₃ crossing kernel.** -/
theorem chi3_rp (P : Positivity K) (F : Q4 R → R) (xs : List (Q4 R)) :
    P.nn (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (K.mul (nine K) (U3h K x y))) xs) xs) := by
  have h := rp_nonneg K P F xs _ (allNN_gl K P (L3 K))
  rw [sumK_ext K _ _ (fun x => sumK_ext K _ _ (fun y => by rw [chi3_gram K x y]) xs)] at h
  exact h

end ChiGram3

#print axioms ChiGram3.chi3_gram
#print axioms ChiGram3.chi3_rp

#print axioms: 2 theorems, no axioms.

90.63 Reflection positivity up to \(\chi _3\)

Nonnegative combinations of the first four character kernels are reflection positive.

\[ a_i\ge 0\ \Rightarrow \ \sum _{x,y}F(x)F(y)\,\big (a_0+a_1\chi _1+a_2\,4\chi _2+a_3\,9\chi _3\big )(x,y)\ge 0 \]

ym/ChiRP3.lean

/-
  ym/ChiRP3 — T45, G35: a crossing kernel a₀ + a₁·χ₁ + a₂·(4χ₂) + a₃·(9χ₃) (homogeneous quaternion forms of ChiGram,
  ChiGram3) with a₀, …, a₃ ≥ 0 is a Gram kernel with nonnegative coefficients, hence reflection positive: weights with
  nonnegative character coefficients up to n = 3 (Migdal-cone weights truncated at L = 3). Lean 4 core only, no axioms.
-/
import ym.ChiGram3

namespace ChiRP3

open YMChain LinkIntegral ChiGram ChiGram3 WilsonGram

variable {R : Type} (K : DetChain.CRing R)

def chi2h (x y : Q4 R) : R :=
  K.mul (four K) (K.add (K.mul (four K) (K.mul (dot4 K x y) (dot4 K x y))) (K.neg (K.mul (dot4 K x x) (dot4 K y y))))

def kern (a0 a1 a2 a3 : R) (x y : Q4 R) : R :=
  K.add (K.mul a0 K.one) (K.add (K.mul a1 (K.add (dot4 K x y) (dot4 K x y)))
    (K.add (K.mul a2 (chi2h K x y)) (K.mul a3 (K.mul (nine K) (U3h K x y)))))

def LL (a0 a1 a2 a3 : R) : List (R × (Q4 R → R)) :=
  scaleG K a0 (oneG K) ++ (scaleG K a1 (gl K (L1 (R := R))) ++ (scaleG K a2 (gl K (L2 K)) ++ scaleG K a3 (gl K (L3 K))))

theorem ker_LL (a0 a1 a2 a3 : R) (x y : Q4 R) : ker K (LL K a0 a1 a2 a3) x y = kern K a0 a1 a2 a3 x y := by
  unfold LL kern chi2h
  rw [ker_app, ker_app, ker_app, ker_scale, ker_scale, ker_scale, ker_scale, ker_one, chi1_gram, chi2_gram, chi3_gram]

/-- **Reflection positivity for nonnegative character coefficients up to n = 3.** -/
theorem rp3 (P : Positivity K) (a0 a1 a2 a3 : R) (h0 : P.nn a0) (h1 : P.nn a1) (h2 : P.nn a2) (h3 : P.nn a3)
    (F : Q4 R → R) (xs : List (Q4 R)) :
    P.nn (sumK K (fun x => sumK K (fun y => K.mul (K.mul (F x) (F y)) (kern K a0 a1 a2 a3 x y)) xs) xs) := by
  have hL : AllNN K P (LL K a0 a1 a2 a3) :=
    allNN_app K P _ _ (allNN_scale K P _ h0 _ (allNN_one K P))
      (allNN_app K P _ _ (allNN_scale K P _ h1 _ (allNN_gl K P _))
        (allNN_app K P _ _ (allNN_scale K P _ h2 _ (allNN_gl K P _)) (allNN_scale K P _ h3 _ (allNN_gl K P _))))
  have h := rp_nonneg K P F xs _ hL
  rw [sumK_ext K _ _ (fun x => sumK_ext K _ _ (fun y => by rw [ker_LL K a0 a1 a2 a3 x y]) xs)] at h
  exact h

end ChiRP3

#print axioms ChiRP3.ker_LL
#print axioms ChiRP3.rp3

#print axioms: 2 theorems, no axioms.

90.64 Positivity of the flowed weight

After \(j\) steps from Cone(\(2^{-8}\)) the weight lies within \(2^{-(16j+29)}\) of \(1\), hence is nonnegative.

\[ 1-2^{-(16j+29)}\le 1+\sum _{n\ge 1}(\mathrm {mkIt}^j a)_n\chi _n\le 1+2^{-(16j+29)} \]

ym/WeightPosJ.lean

/-
  ym/WeightPosJ — T45, G37: the flowed plaquette weight stays strictly near 1, hence positive: if 1 :: a ∈ Cone(2⁻⁸),
  then after j Migdal steps 1 − 2^{-(16j+29)} ≤ 1 + Σ_{n≥1} a_n^{(j)} χ_n ≤ 1 + 2^{-(16j+29)} for every χ with |χ_n| ≤ n+1,
  in particular the weight is ≥ 0 (positivity input for reflection positivity / the transfer matrix). Lean 4 core only,
  no axioms.
-/
import ym.ActivityDecay

namespace WeightPosJ

open YMChain LinkIntegral SU2Char ConeBound MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **Two-sided bound and positivity of the flowed weight.** -/
theorem weight_pos_j (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (a : List R) (ha : Bd K O (pwR K h 8) 0 (K.one :: a)) (L j : Nat) (χ : Nat → R)
    (hχ : ∀ n, O.le (K.neg (cN K (n + 1))) (χ n) ∧ O.le (χ n) (cN K (n + 1))) :
    O.le (K.add K.one (K.neg (pwR K h (16 * j + 29)))) (K.add K.one (SU2Char.dot K 1 (mkIt K O F L a j) χ)) ∧
    O.le (K.add K.one (SU2Char.dot K 1 (mkIt K O F L a j) χ)) (K.add K.one (pwR K h (16 * j + 29))) ∧
    O.le K.zero (K.add K.one (SU2Char.dot K 1 (mkIt K O F L a j) χ)) := by
  have hl : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  obtain ⟨lo, hi⟩ := ActivityDecay.activity_decay K O F h1 h10 h hh h0 a ha L j χ hχ
  have e30 : 16 * j + 30 = (16 * j + 29) + 1 := by lib_nat
  have x2 : K.add (pwR K h (16 * j + 30)) (pwR K h (16 * j + 30)) = pwR K h (16 * j + 29) := by
    rw [e30]; exact ActivityDecay.half_add K h hh _
  rw [x2] at lo hi
  have l := O.add_le_add _ _ _ _ (O.le_refl K.one) lo
  have u := O.add_le_add _ _ _ _ (O.le_refl K.one) hi
  refine ⟨l, u, O.le_trans _ _ _ ?_ l⟩
  have p1 : O.le (pwR K h (16 * j + 29)) K.one := ConeBound.pw_anti K O h1 h h0 hl _ 0 (Nat.zero_le _)
  have n1 := O.add_le_add _ _ _ _ (O.le_refl K.one) (ConeGap.neg_le K O _ _ p1)
  rw [K.add_neg] at n1; exact n1

end WeightPosJ

#print axioms WeightPosJ.weight_pos_j

#print axioms: 1 theorem, no axioms.

90.65 Cone(\(2^{-6}\)) enters Cone(\(2^{-8}\))

One Migdal step maps Cone(\(2^{-6}\)) into Cone(\(2^{-8}\)).

\[ \mathrm {Bd}_{2^{-6}}(1{::}\mathrm {mkIt}^k a)\ \Rightarrow \ \forall j:\ \mathrm {Bd}_{2^{-8}}(1{::}\mathrm {mkIt}^{k+1+j} a) \]

ym/MigdalCone6.lean

/-
  ym/MigdalCone6 — T45, G38: one 4D Migdal step maps Cone(2⁻⁶) into Cone(2⁻⁸) (mk_cone with r = h⁶, r₁ = h⁴, r₂ = h²),
  so a flow that reaches Cone(2⁻⁶) at step k is in Cone(2⁻⁸) from step k + 1 on (cone6_forever). Lean 4 core only, no axioms.
-/
import ym.MigdalDecay
import ym.ActivityDecay

namespace MigdalCone6

open YMChain LinkIntegral SU2Char ConeBound MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem mk_6_8 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (a : List R) (ha : Bd K O (pwR K h 6) 0 (K.one :: a)) (L : Nat) :
    Bd K O (pwR K h 8) 0 (mk K O F L a) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have pn : ∀ n, O.le K.zero (pwR K h n) := fun n => AreaLower.pw_nn K O h h0 h1 n
  have pa : ∀ m n, m ≤ n → O.le (pwR K h n) (pwR K h m) := fun m n hmn => pw_anti K O h1 h h0 hle n m hmn
  have f4 := ActivityDecay.four_h K h hh
  have fr : O.le (K.mul (four K) (pwR K h 6)) K.one := by rw [f4 4]; exact pa 0 4 (Nat.zero_le _)
  have fr1 : O.le (K.mul (four K) (pwR K h 4)) K.one := by rw [f4 2]; exact pa 0 2 (Nat.zero_le _)
  have s1 : O.le (K.mul (four K) (pwR K (pwR K h 6) 3)) (pwR K (pwR K h 4) 4) := by
    rw [Migdal.pw_pw, Migdal.pw_pw, show 6 * 3 = 16 + 2 from rfl, f4 16]; exact O.le_refl _
  have s2 : O.le (K.mul (four K) (pwR K (pwR K h 4) 3)) (pwR K (pwR K h 2) 4) := by
    rw [Migdal.pw_pw, Migdal.pw_pw, show 4 * 3 = 10 + 2 from rfl, f4 10]; exact pa (2 * 4) 10 (by decide)
  have b := mk_cone K O F h1 h10 (pwR K h 6) (pwR K h 4) (pwR K h 2) (pn 6) fr (pn 4) fr1 (pn 2) s1 s2 a ha L L
  rw [Migdal.pw_pw] at b
  exact b

/-- **Cone(2⁻⁶) at step k ⇒ Cone(2⁻⁸) at every step k + 1 + j.** -/
theorem cone6_forever (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (L : Nat) (a : List R) (k : Nat)
    (hk : Bd K O (pwR K h 6) 0 (K.one :: mkIt K O F L a k)) :
    ∀ j, Bd K O (pwR K h 8) 0 (K.one :: mkIt K O F L a (k + 1 + j)) := by
  have b := mk_6_8 K O F h1 h10 h hh h0 _ hk L
  rw [mk_head K O F h1 h10 _ _ hk L] at b
  intro j
  have e : ∀ j, mkIt K O F L a (k + 1 + j) = mkIt K O F L (mkIt K O F L a (k + 1)) j := by
    intro j
    induction j with
    | zero => rfl
    | succ j ih =>
      show (mk K O F L (mkIt K O F L a (k + 1 + j))).tail = (mk K O F L (mkIt K O F L (mkIt K O F L a (k + 1)) j)).tail
      rw [ih]
  rw [e j]
  exact mk_forever K O F h1 h10 h hh h0 _ b L j

end MigdalCone6

#print axioms MigdalCone6.mk_6_8
#print axioms MigdalCone6.cone6_forever

#print axioms: 2 theorems, no axioms.

90.66 Fischer kernel of SU(2)

The degree-\(n\) Fischer kernel is a Gram sum with binomial weights (binomial theorem over complex pairs).

\[ \mathrm {Re}\,(a\bar c+b\bar d)^n=\sum _{k\le n}\binom nk\big (\mathrm {Re}\,m_k(g)\,\mathrm {Re}\,m_k(h)+\mathrm {Im}\,m_k(g)\,\mathrm {Im}\,m_k(h)\big ),\quad m_k=a^kb^{n-k} \]

ym/CxRing.lean

/-
  ym/CxRing — T45, G35b-1: the complex pairs R × R over any commutative ring K form a commutative ring (CxRing K),
  with real part re and conjugation. Lean 4 core only, no axioms.
-/
import ym.CxPair

namespace CxRing

open YMChain CxPair

variable {R : Type} (K : DetChain.CRing R)

def ring : DetChain.CRing (Cx R) where
  add := cadd K
  mul := cmul K
  neg := cneg K
  zero := (K.zero, K.zero)
  one := (K.one, K.zero)
  add_assoc := fun a b c => by dsimp only [cadd]; congr 1 <;> lib_ring
  add_comm := fun a b => by dsimp only [cadd]; congr 1 <;> lib_ring
  zero_add := fun a => by dsimp only [cadd]; congr 1 <;> lib_ring
  add_neg := fun a => by dsimp only [cadd, cneg]; congr 1 <;> lib_ring
  mul_assoc := fun a b c => cmul_assoc K a b c
  mul_comm := fun a b => cmul_comm K a b
  one_mul := fun a => by dsimp only [cmul]; congr 1 <;> lib_ring
  distrib := fun a b c => cmul_add K a b c

/-- real part of a product with a conjugate: Re(z w̄) = z₁w₁ + z₂w₂ (a Gram term) -/
theorem re_mul_conj (z w : Cx R) : (cmul K z (cconj K w)).1 = K.add (K.mul z.1 w.1) (K.mul z.2 w.2) := by
  dsimp only [cmul, cconj]; lib_ring

end CxRing

#print axioms CxRing.ring
#print axioms CxRing.re_mul_conj

ym/Fischer.lean

/-
  ym/Fischer — T45, G35b-1: the Fischer kernel of SU(2). For g = (a, b), h = (c, d) (complex pairs),
    Re((a c̄ + b d̄)ⁿ) = Σ_{k ≤ n} C(n,k) (Re m_k(g) Re m_k(h) + Im m_k(g) Im m_k(h)),   m_k(g) = a^k b^{n−k},
  a Gram kernel with nonnegative weights C(n,k) (binomial theorem LibBinom.binom over CxRing). Lean 4 core only, no axioms.
-/
import ym.CxRing
import lib.Binom

namespace Fischer

open YMChain CxPair LibRing LibSum

variable {R : Type} (K : DetChain.CRing R)

abbrev CK := CxRing.ring K

theorem pw_mul_conj (z w : Cx R) : ∀ k,
    pw (CK K) (cmul K z (cconj K w)) k = cmul K (pw (CK K) z k) (cconj K (pw (CK K) w k))
  | 0 => by
    show ((K.one, K.zero) : Cx R) = cmul K (K.one, K.zero) (cconj K (K.one, K.zero))
    dsimp only [cmul, cconj]; congr 1 <;> lib_ring
  | k + 1 => by
    show cmul K (pw (CK K) (cmul K z (cconj K w)) k) (cmul K z (cconj K w))
      = cmul K (cmul K (pw (CK K) z k) z) (cconj K (cmul K (pw (CK K) w k) w))
    rw [pw_mul_conj z w k]
    generalize pw (CK K) z k = P; generalize pw (CK K) w k = Q
    dsimp only [cmul, cconj]; congr 1 <;> lib_ring

theorem castN_cx : ∀ n, castN (CK K) n = (castN K n, K.zero)
  | 0 => rfl
  | n + 1 => by
    show cadd K (K.one, K.zero) (castN (CK K) n) = (K.add K.one (castN K n), K.zero)
    rw [castN_cx n]; dsimp only [cadd]; congr 1; lib_ring

theorem re_sum (f : Nat → Cx R) : ∀ l : List Nat,
    (sum (toSemi (CK K)) f l).1 = sum (toSemi K) (fun k => (f k).1) l
  | [] => rfl
  | k :: l => by
    show (cadd K (f k) (sum (toSemi (CK K)) f l)).1 = K.add (f k).1 (sum (toSemi K) (fun k => (f k).1) l)
    rw [← re_sum f l]; rfl

/-- monomial m_k(g) = a^k b^{n−k} -/
def mono (n k : Nat) (g : SU R) : Cx R := cmul K (pw (CK K) g.1 k) (pw (CK K) g.2 (n - k))

/-- the Fischer term: C(n,k)·Re(m_k(g) conj m_k(h)) -/
def fterm (n : Nat) (g h : SU R) (k : Nat) : R :=
  K.mul (castN K (LibBinom.C n k))
    (K.add (K.mul (mono K n k g).1 (mono K n k h).1) (K.mul (mono K n k g).2 (mono K n k h).2))

/-- **Fischer kernel as a Gram sum.** -/
theorem fischer (n : Nat) (g h : SU R) :
    (pw (CK K) (cadd K (cmul K g.1 (cconj K h.1)) (cmul K g.2 (cconj K h.2))) n).1
      = sum (toSemi K) (fterm K n g h) (rng (n + 1)) := by
  have b := LibBinom.binom (CK K) (cmul K g.1 (cconj K h.1)) (cmul K g.2 (cconj K h.2)) n
  have b' : pw (CK K) (cadd K (cmul K g.1 (cconj K h.1)) (cmul K g.2 (cconj K h.2))) n
      = sum (toSemi (CK K)) (LibBinom.T (CK K) (cmul K g.1 (cconj K h.1)) (cmul K g.2 (cconj K h.2)) n) (rng (n + 1)) := b
  rw [b', re_sum]
  apply sum_ext
  intro k
  show (cmul K (castN (CK K) (LibBinom.C n k)) (cmul K (pw (CK K) (cmul K g.1 (cconj K h.1)) k)
    (pw (CK K) (cmul K g.2 (cconj K h.2)) (n - k)))).1 = fterm K n g h k
  rw [pw_mul_conj, pw_mul_conj, castN_cx]
  unfold fterm mono
  generalize pw (CK K) g.1 k = A; generalize pw (CK K) h.1 k = B
  generalize pw (CK K) g.2 (n - k) = C; generalize pw (CK K) h.2 (n - k) = D
  dsimp only [cmul, cconj]; lib_ring

end Fischer

#print axioms Fischer.pw_mul_conj
#print axioms Fischer.castN_cx
#print axioms Fischer.re_sum
#print axioms Fischer.fischer

#print axioms: 6 theorems, no axioms.

90.67 Reflection positivity of every SU(2) character

For every \(n\) the character kernel \(\chi _n(gh^\dagger )\) is a Gram kernel with nonnegative weights, hence reflection positive.

\[ \chi _n(2x\cdot y)=\sum _{k,j\le n}\tfrac {\binom nj}{\binom nk}\big (\mathrm {Re}M_{kj}(g)\mathrm {Re}M_{kj}(h)+\mathrm {Im}M_{kj}(g)\mathrm {Im}M_{kj}(h)\big ),\qquad \sum _{x,y}F(x)F(y)\chi _n(2x\cdot y)\ge 0 \]

ym/GramChi.lean

/-
  ym/GramChi — T45, G35b (final identity): for unit quaternions g, h (pairs over an ordered field),
    χ_n(2 x·y) = Σ_{k,j ≤ n} w_{jk} (Re M_{kj}(g) Re M_{kj}(h) + Im M_{kj}(g) Im M_{kj}(h)),  w_{jk} = C(n,j)/C(n,k) > 0,
  where M = Mat(toM2 ·) is the degree-n representation matrix and x, y are the quaternion coordinates: the SU(2)
  character kernel χ_n(g h†) is a Gram kernel with nonnegative weights, for every n. Lean 4 core only, no axioms.
-/
import ym.SUTrace
import ym.ConjMat

namespace GramChi

open DetChain LibRing LibSum Homog HomogId HomogT MatTrace CxPair YMChain

variable {R : Type} (K : CRing R) (O : ORing K) (F : OField.OF K O)

abbrev CK := CxRing.ring K

theorem C_pos : ∀ n k, k < n + 1 → 1 ≤ LibBinom.C n k
  | _, 0, _ => by rw [LibBinom.C_zero_right]; exact Nat.le_refl 1
  | 0, k + 1, h => absurd (Nat.lt_of_succ_lt_succ h) (Nat.not_lt_zero k)
  | n + 1, k + 1, h => by
    rw [LibBinom.pascal]
    exact Nat.le_trans (C_pos n k (Nat.lt_of_succ_lt_succ h)) (Nat.le_add_right _ _)

theorem castC_ne (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n k : Nat) (hk : k < n + 1) :
    castN K (LibBinom.C n k) ≠ K.zero := by
  have h := C_pos n k hk
  obtain ⟨j, e⟩ : ∃ j, LibBinom.C n k = j + 1 := by
    cases hc : LibBinom.C n k with
    | zero => rw [hc] at h; exact absurd h (Nat.not_succ_le_zero 0)
    | succ j => exact ⟨j, rfl⟩
  rw [e]; exact IdThmR.cN_pos K O h1 h10 j

/-- weight w_{jk} = C(n,j)/C(n,k) -/
def w (n j k : Nat) : R := K.mul (castN K (LibBinom.C n j)) (F.inv (castN K (LibBinom.C n k)))

/-- transpose entries: Mat(Aᵀ)_{jk} = w_{jk} · Mat(A)_{kj} -/
theorem tr_entry (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n : Nat) (A : M2 (Cx R)) (j k : Nat)
    (hj : j < n + 1) (hk : k < n + 1) :
    Mat (CK K) (tr2 A) n j k = cmul K (w K O F n j k, K.zero) (Mat (CK K) A n k j) := by
  have hI := IdThmC.idThm_C K O h1 h10 n
  have t := mat_tr (CK K) n _ hI A k j hk hj
  rw [Fischer.castN_cx, Fischer.castN_cx] at t
  have hi := F.mul_inv _ (castC_ne K O h1 h10 n k hk)
  unfold w
  generalize Mat (CK K) (tr2 A) n j k = MT at t ⊢
  generalize Mat (CK K) A n k j = MA at t ⊢
  generalize castN K (LibBinom.C n k) = a at t hi ⊢
  generalize castN K (LibBinom.C n j) = b at t ⊢
  generalize F.inv a = ι at hi ⊢
  have t1 := congrArg Prod.fst t
  have t2 := congrArg Prod.snd t
  change (cmul K (b, K.zero) MA).1 = (cmul K (a, K.zero) MT).1 at t1
  change (cmul K (b, K.zero) MA).2 = (cmul K (a, K.zero) MT).2 at t2
  dsimp only [cmul] at t1 t2
  have r1 : MT.1 = K.mul ι (K.add (K.mul a MT.1) (K.neg (K.mul K.zero MT.2))) := by
    have : MT.1 = K.mul (K.mul a ι) MT.1 := by rw [hi, K.one_mul]
    exact this.trans (by lib_ring)
  have r2 : MT.2 = K.mul ι (K.add (K.mul a MT.2) (K.mul K.zero MT.1)) := by
    have : MT.2 = K.mul (K.mul a ι) MT.2 := by rw [hi, K.one_mul]
    exact this.trans (by lib_ring)
  rw [← t1] at r1; rw [← t2] at r2
  show MT = (K.add (K.mul (K.mul b ι) MA.1) (K.neg (K.mul K.zero MA.2)), K.add (K.mul (K.mul b ι) MA.2) (K.mul K.zero MA.1))
  have eT : MT = (MT.1, MT.2) := rfl
  rw [eT, r1, r2]
  congr 1 <;> lib_ring

theorem snrm_dag (h : SU R) : snrm K (sdag K h) = snrm K h := by
  unfold snrm sdag nrm; dsimp only [cconj, cneg]; lib_ring

/-- the Gram sum -/
def gsum (n : Nat) (g h : SU R) : R :=
  sum (toSemi K) (fun k => sum (toSemi K) (fun j => K.mul (w K O F n j k)
    (K.add (K.mul (Mat (CK K) (SUM2.toM2 K g) n k j).1 (Mat (CK K) (SUM2.toM2 K h) n k j).1)
           (K.mul (Mat (CK K) (SUM2.toM2 K g) n k j).2 (Mat (CK K) (SUM2.toM2 K h) n k j).2))) (rng (n + 1))) (rng (n + 1))

/-- **χ_n(g h†) is a Gram kernel**: χ_n(2 x·y) = Σ_{k,j} w_{jk} (Re M_{kj}(g) Re M_{kj}(h) + Im M_{kj}(g) Im M_{kj}(h)). -/
theorem gram_chi (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (g h : SU R) (hg : snrm K g = K.one)
    (hh : snrm K h = K.one) (n : Nat) :
    ChiBound.c K (K.add (ChiGram.dot4 K (q4 g) (q4 h)) (ChiGram.dot4 K (q4 g) (q4 h))) n = gsum K O F n g h := by
  have hu : snrm K (smul K g (sdag K h)) = K.one := by rw [snrm_mul, snrm_dag, hg, hh, K.one_mul]
  have tS := SUTrace.T_SU K O F h1 h10 (smul K g (sdag K h)) hu n
  rw [tr_dot] at tS
  have c1 := congrArg Prod.fst tS
  dsimp only at c1
  rw [← c1, SUM2.toM2_mul, T_comp (CK K) n _ (IdThmC.idThm_C K O h1 h10 n), SUM2.toM2_dag, Fischer.re_sum]
  unfold gsum
  apply sum_ext_mem; intro k hk
  rw [Fischer.re_sum]
  apply sum_ext_mem; intro j hj
  have hm := ConjMat.hom_Mat (CK K) (ConjMat.conj_hom K) (tr2 (SUM2.toM2 K h)) n j k
  have e1 : Mat (CK K) (SUM2.conjM K (tr2 (SUM2.toM2 K h))) n j k = cconj K (Mat (CK K) (tr2 (SUM2.toM2 K h)) n j k) :=
    hm.symm
  show (cmul K (Mat (CK K) (SUM2.conjM K (tr2 (SUM2.toM2 K h))) n j k) (Mat (CK K) (SUM2.toM2 K g) n k j)).1 = _
  rw [e1, tr_entry K O F h1 h10 n _ j k (LibBinom.mem_rng hj) (LibBinom.mem_rng hk)]
  generalize Mat (CK K) (SUM2.toM2 K h) n k j = Mh
  generalize Mat (CK K) (SUM2.toM2 K g) n k j = Mg
  dsimp only [cmul, cconj]; lib_ring

end GramChi

#print axioms GramChi.C_pos
#print axioms GramChi.tr_entry
#print axioms GramChi.gram_chi

ym/RPChi.lean

/-
  ym/RPChi — T45, G35c: reflection positivity of every SU(2) character kernel: for unit quaternions xs and any F,
    Σ_{x,y ∈ xs} F(x) F(y) χ_n(2 x·y) ≥ 0,
  via the Gram list of GramChi.gram_chi (weights C(n,j)/C(n,k) ≥ 0) and YMChain.rp_nonneg. Lean 4 core only, no axioms.
-/
import ym.GramChi
import ym.ChangeVar2D

namespace RPChi

open DetChain LibRing LibSum Homog HomogId CxPair YMChain GramChi

variable {R : Type} (K : CRing R) (O : ORing K) (F : OField.OF K O)

def Pos : Positivity K where
  nn := fun x => O.le K.zero x
  nn_sq := fun x => SUInv.sq_nn K O x
  nn_add := fun a b ha hb => by have := O.add_le_add _ _ _ _ ha hb; rw [K.zero_add] at this; exact this
  nn_mul := fun a b ha hb => O.mul_nonneg _ _ ha hb
  nn_zero := O.le_refl _

def re (n k j : Nat) (g : SU R) : R := (Mat (GramChi.CK K) (SUM2.toM2 K g) n k j).1
def im (n k j : Nat) (g : SU R) : R := (Mat (GramChi.CK K) (SUM2.toM2 K g) n k j).2

def LJ (n k : Nat) : List Nat → List (R × (SU R → R))
  | [] => []
  | j :: js => (w K O F n j k, re K n k j) :: (w K O F n j k, im K n k j) :: LJ n k js

def LK (n : Nat) : List Nat → List (R × (SU R → R))
  | [] => []
  | k :: ks => LJ K O F n k (rng (n + 1)) ++ LK n ks

theorem ker_LJ (n k : Nat) (g h : SU R) : ∀ js : List Nat, ker K (LJ K O F n k js) g h
    = sum (toSemi K) (fun j => K.mul (w K O F n j k) (K.add (K.mul (re K n k j g) (re K n k j h))
        (K.mul (im K n k j g) (im K n k j h)))) js
  | [] => rfl
  | j :: js => by
    show K.add (K.mul (K.mul (w K O F n j k) (re K n k j g)) (re K n k j h))
      (K.add (K.mul (K.mul (w K O F n j k) (im K n k j g)) (im K n k j h)) (ker K (LJ K O F n k js) g h)) = _
    rw [ker_LJ n k g h js]
    show _ = K.add (K.mul (w K O F n j k) (K.add (K.mul (re K n k j g) (re K n k j h)) (K.mul (im K n k j g) (im K n k j h))))
      (sum (toSemi K) (fun j => K.mul (w K O F n j k) (K.add (K.mul (re K n k j g) (re K n k j h))
        (K.mul (im K n k j g) (im K n k j h)))) js)
    generalize sum (toSemi K) (fun j => K.mul (w K O F n j k) (K.add (K.mul (re K n k j g) (re K n k j h))
        (K.mul (im K n k j g) (im K n k j h)))) js = Z
    lib_ring

theorem ker_LK (n : Nat) (g h : SU R) : ∀ ks : List Nat, ker K (LK K O F n ks) g h
    = sum (toSemi K) (fun k => sum (toSemi K) (fun j => K.mul (w K O F n j k) (K.add (K.mul (re K n k j g) (re K n k j h))
        (K.mul (im K n k j g) (im K n k j h)))) (rng (n + 1))) ks
  | [] => rfl
  | k :: ks => by
    show ker K (LJ K O F n k (rng (n + 1)) ++ LK K O F n ks) g h = _
    unfold ker; rw [sumK_append]
    show K.add (ker K _ g h) (ker K _ g h) = _
    rw [ker_LJ, ker_LK n g h ks]; rfl

theorem w_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n j k : Nat) (hk : k < n + 1) :
    O.le K.zero (w K O F n j k) :=
  O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (F.inv_pos _ (SU2CharExp.cN_nn K O h1 _) (castC_ne K O h1 h10 n k hk))

theorem allNN_LJ (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n k : Nat) (hk : k < n + 1) :
    ∀ js, AllNN K (Pos K O) (LJ K O F n k js)
  | [] => trivial
  | j :: js => ⟨w_nn K O F h1 h10 n j k hk, w_nn K O F h1 h10 n j k hk, allNN_LJ h1 h10 n k hk js⟩

theorem allNN_app' {X : Type} (P : Positivity K) : ∀ L1 L2 : List (R × (X → R)), AllNN K P L1 → AllNN K P L2 → AllNN K P (L1 ++ L2)
  | [], _, _, h2 => h2
  | _ :: L1, L2, h1, h2 => ⟨h1.1, allNN_app' P L1 L2 h1.2 h2⟩

theorem allNN_LK (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n : Nat) :
    ∀ ks, (∀ k, k ∈ ks → k < n + 1) → AllNN K (Pos K O) (LK K O F n ks)
  | [], _ => trivial
  | k :: ks, hks => allNN_app' K (Pos K O) _ _ (allNN_LJ K O F h1 h10 n k (hks k (List.Mem.head _)) _)
      (allNN_LK h1 h10 n ks (fun k' hk' => hks k' (List.Mem.tail _ hk')))

/-- **reflection positivity of the χ_n kernel on unit quaternions** -/
theorem rp_chi (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n : Nat) (Fx : SU R → R) (xs : List (SU R))
    (hu : ∀ x, memL x xs → snrm K x = K.one) :
    O.le K.zero (sumK K (fun x => sumK K (fun y => K.mul (K.mul (Fx x) (Fx y))
      (ChiBound.c K (K.add (ChiGram.dot4 K (q4 x) (q4 y)) (ChiGram.dot4 K (q4 x) (q4 y))) n)) xs) xs) := by
  have h := rp_nonneg K (Pos K O) Fx xs (LK K O F n (rng (n + 1)))
    (allNN_LK K O F h1 h10 n _ (fun k hk => LibBinom.mem_rng hk))
  have e : ∀ x, memL x xs → ∀ y, memL y xs → ker K (LK K O F n (rng (n + 1))) x y
      = ChiBound.c K (K.add (ChiGram.dot4 K (q4 x) (q4 y)) (ChiGram.dot4 K (q4 x) (q4 y))) n := by
    intro x hx y hy
    rw [ker_LK, gram_chi K O F h1 h10 x y (hu x hx) (hu y hy) n]; rfl
  rw [ChangeVar2D.sumK_ext_mem K _ _ xs (fun x hx => ChangeVar2D.sumK_ext_mem K _ _ xs (fun y hy => by
    show K.mul _ (ker K _ x y) = _; rw [e x hx y hy]))] at h
  exact h

end RPChi

#print axioms RPChi.ker_LK
#print axioms RPChi.rp_chi

#print axioms: 5 theorems, no axioms.

90.68 Reflection positivity of the flowed weight

Nonnegative character combinations, in particular the Migdal-flow plaquette weight, are reflection positive.

\[ s,\,cf_n\ge 0\ \Rightarrow \ \sum _{x,y}F(x)F(y)\Big (s+s\sum _{n\ge 1}cf_n\,\chi _n(2x\cdot y)\Big )\ge 0 \]

ym/RPDot.lean

/-
  ym/RPDot — T45, G35c: reflection positivity of every nonnegative character combination: for cf ≥ 0 and unit
  quaternions xs, Σ_{x,y} F(x)F(y)·dot m cf (n ↦ χ_n(2x·y)) ≥ 0 (dot m cf f = Σ_i cf_i f(m+i), the form of the
  Migdal-flow weights), and so is the full plaquette weight s·(1 + Σ_{n≥1} cf_n χ_n) for s ≥ 0. Lean 4 core only, no axioms.
-/
import ym.RPChi

namespace RPDot

open DetChain YMChain CxPair

variable {R : Type} (K : CRing R) (O : ORing K) (F : OField.OF K O)

/-- the double sum Σ_x Σ_y F(x)F(y)·k(x,y) -/
def Q (Fx : SU R → R) (xs : List (SU R)) (k : SU R → SU R → R) : R :=
  sumK K (fun x => sumK K (fun y => K.mul (K.mul (Fx x) (Fx y)) (k x y)) xs) xs

theorem Q_add (Fx : SU R → R) (xs : List (SU R)) (k1 k2 : SU R → SU R → R) :
    Q K Fx xs (fun x y => K.add (k1 x y) (k2 x y)) = K.add (Q K Fx xs k1) (Q K Fx xs k2) := by
  unfold Q
  rw [← sumK_add]
  apply sumK_ext; intro x
  rw [← sumK_add]
  apply sumK_ext; intro y
  show K.mul _ (K.add _ _) = K.add _ _; lib_ring

theorem Q_smul (Fx : SU R → R) (xs : List (SU R)) (c : R) (k : SU R → SU R → R) :
    Q K Fx xs (fun x y => K.mul c (k x y)) = K.mul c (Q K Fx xs k) := by
  unfold Q
  rw [mul_sumK]
  apply sumK_ext; intro x
  rw [mul_sumK]
  apply sumK_ext; intro y
  show K.mul _ (K.mul _ _) = K.mul _ (K.mul _ _); lib_ring

theorem szero {A : Type} : ∀ l : List A, sumK K (fun _ => K.zero) l = K.zero
  | [] => rfl
  | _ :: l => by show K.add K.zero (sumK K (fun _ => K.zero) l) = K.zero; rw [szero l, K.zero_add]

theorem Q_zero (Fx : SU R → R) (xs : List (SU R)) : Q K Fx xs (fun _ _ => K.zero) = K.zero := by
  unfold Q
  rw [sumK_ext K _ (fun _ => K.zero) (fun x => by
    rw [sumK_ext K _ (fun _ => K.zero) (fun y => by show K.mul _ K.zero = K.zero; lib_ring) xs]; exact szero K xs) xs]
  exact szero K xs

def chiK (n : Nat) (x y : SU R) : R :=
  ChiBound.c K (K.add (ChiGram.dot4 K (q4 x) (q4 y)) (ChiGram.dot4 K (q4 x) (q4 y))) n

/-- **nonnegative character combinations are reflection positive** -/
theorem rp_dot (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Fx : SU R → R) (xs : List (SU R))
    (hu : ∀ x, memL x xs → snrm K x = K.one) :
    ∀ (m : Nat) (cf : List R), (∀ c, memL c cf → O.le K.zero c) →
      O.le K.zero (Q K Fx xs (fun x y => SU2Char.dot K m cf (fun n => chiK K n x y)))
  | _, [], _ => by
    rw [show (fun (x y : SU R) => SU2Char.dot K _ [] (fun n => chiK K n x y)) = (fun _ _ => K.zero) from rfl, Q_zero]
    exact O.le_refl _
  | m, c :: cf, hc => by
    show O.le K.zero (Q K Fx xs (fun x y => K.add (K.mul c (chiK K m x y)) (SU2Char.dot K (m + 1) cf (fun n => chiK K n x y))))
    rw [Q_add, Q_smul]
    have a := O.mul_nonneg _ _ (hc c (Or.inl rfl)) (RPChi.rp_chi K O F h1 h10 m Fx xs hu)
    have b := rp_dot F h1 h10 Fx xs hu (m + 1) cf (fun c' h' => hc c' (Or.inr h'))
    have := O.add_le_add _ _ _ _ a b
    rw [K.zero_add] at this; exact this

theorem Q_ext (Fx : SU R → R) (xs : List (SU R)) (k1 k2 : SU R → SU R → R) (h : ∀ x y, k1 x y = k2 x y) :
    Q K Fx xs k1 = Q K Fx xs k2 := by
  unfold Q; apply sumK_ext; intro x; apply sumK_ext; intro y; show K.mul _ (k1 x y) = K.mul _ (k2 x y); rw [h]

/-- **the Migdal-flow plaquette weight s + s·Σ_{n≥1} cf_n χ_n is reflection positive** (s ≥ 0, cf ≥ 0) -/
theorem rp_weight (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Fx : SU R → R) (xs : List (SU R))
    (hu : ∀ x, memL x xs → snrm K x = K.one) (s : R) (hs : O.le K.zero s) (cf : List R) (hc : ∀ c, memL c cf → O.le K.zero c) :
    O.le K.zero (Q K Fx xs (fun x y => K.add s (K.mul s (SU2Char.dot K 1 cf (fun n => chiK K n x y))))) := by
  have e : ∀ x y, K.add s (K.mul s (SU2Char.dot K 1 cf (fun n => chiK K n x y)))
      = K.add (K.mul s (chiK K 0 x y)) (K.mul s (SU2Char.dot K 1 cf (fun n => chiK K n x y))) := fun x y => by
    unfold chiK; rw [ChiBound.c0]; lib_ring
  rw [Q_ext K Fx xs _ _ e, Q_add, Q_smul, Q_smul]
  have a := O.mul_nonneg _ _ hs (RPChi.rp_chi K O F h1 h10 0 Fx xs hu)
  have b := O.mul_nonneg _ _ hs (rp_dot K O F h1 h10 Fx xs hu 1 cf hc)
  have := O.add_le_add _ _ _ _ a b
  rw [K.zero_add] at this; exact this

end RPDot

#print axioms RPDot.Q_add
#print axioms RPDot.rp_dot
#print axioms RPDot.rp_weight

#print axioms: 3 theorems, no axioms.

90.69 Reflection positivity along the flow

Every weight in a cone, in particular every step of the flow from Cone(\(2^{-8}\)), is reflection positive.

\[ \mathrm {Bd}_{2^{-8}}(1{::}a)\ \Rightarrow \ \forall j:\ \sum _{x,y}F(x)F(y)\,w_j(x,y)\ge 0 \]

ym/RPFlow.lean

/-
  ym/RPFlow — T45, G39: reflection positivity along the Migdal flow: if 1 :: cf ∈ Cone(r) then the plaquette weight
  s + s·Σ_{n≥1} cf_n χ_n (s ≥ 0) is reflection positive on unit quaternions (rp_of_cone); in particular for every step
  j of the flow started in Cone(2⁻⁸) (rp_flow_j). Lean 4 core only, no axioms.
-/
import ym.RPDot
import ym.MigdalDecay

namespace RPFlow

open DetChain YMChain CxPair RPDot LinkIntegral

variable {R : Type} (K : CRing R) (O : ORing K) (F : OField.OF K O)

theorem rp_of_cone (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (r : R) (cf : List R)
    (hcf : ConeBound.Bd K O r 0 (K.one :: cf)) (Fx : SU R → R) (xs : List (SU R))
    (hu : ∀ x, memL x xs → snrm K x = K.one) (s : R) (hs : O.le K.zero s) :
    O.le K.zero (Q K Fx xs (fun x y => K.add s (K.mul s (SU2Char.dot K 1 cf (fun n => chiK K n x y))))) :=
  rp_weight K O F h1 h10 Fx xs hu s hs cf (fun c hc => ConeBound.Bd_nn K O r 0 _ hcf c (Or.inr hc))

/-- **every step of the flow from Cone(2⁻⁸) gives a reflection-positive weight** -/
theorem rp_flow_j (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (a : List R) (ha : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: a)) (L j : Nat) (Fx : SU R → R) (xs : List (SU R))
    (hu : ∀ x, memL x xs → snrm K x = K.one) (s : R) (hs : O.le K.zero s) :
    O.le K.zero (Q K Fx xs (fun x y => K.add s (K.mul s (SU2Char.dot K 1 (MigdalCone.mkIt K O F L a j) (fun n => chiK K n x y))))) :=
  rp_of_cone K O F h1 h10 _ _ (MigdalDecay.mk_decay K O F h1 h10 h hh h0 a ha L j) Fx xs hu s hs

end RPFlow

#print axioms RPFlow.rp_of_cone
#print axioms RPFlow.rp_flow_j

#print axioms: 2 theorems, no axioms.

90.70 One flowed weight: positivity, reflection positivity, Cauchy, clustering

After \(j\) Migdal steps from Cone(\(2^{-8}\)) one weight has all four properties on \(\mathbb Z^4\).

\[ w_j>0\ \wedge \ \mathrm {RP}(w_j)\ \wedge \ \text {Cauchy}(w_j)\ \wedge \ \text {Clustering}_{2^{-(7+16j)}}(w_j) \]

ym/OSBundleJ.lean

/-
  ym/OSBundleJ — T45, G41: one statement for the flowed weight after j Migdal steps from Cone(2⁻⁸) on Z⁴:
  (i) the weight is within 2^{-(16j+29)} of 1 (positive), (ii) it is reflection positive on unit quaternions,
  (iii) it has the infinite-volume Cauchy estimate and clustering with decay 2^{-(7+16j)}. Lean 4 core only, no axioms.
-/
import ym.GapBothJ
import ym.RPFlow
import ym.WeightPosJ

namespace OSBundleJ

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

theorem os_bundle_j (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (a0 : List R) (ha0 : ConeBound.Bd K O (pwR K h 8) 0 (K.one :: a0)) (Lm j : Nat) :
    (∀ χ : Nat → R, (∀ n, O.le (K.neg (cN K (n + 1))) (χ n) ∧ O.le (χ n) (cN K (n + 1))) →
      O.le K.zero (K.add K.one (SU2Char.dot K 1 (MigdalCone.mkIt K O F Lm a0 j) χ))) ∧
    (∀ (Fx : CxPair.SU R → R) (xs : List (CxPair.SU R)), (∀ x, memL x xs → CxPair.snrm K x = K.one) →
      ∀ s, O.le K.zero s → O.le K.zero (RPDot.Q K Fx xs (fun x y => K.add s (K.mul s
        (SU2Char.dot K 1 (MigdalCone.mkIt K O F Lm a0 j) (fun n => RPDot.chiK K n x y)))))) ∧
    GapBothJ.Both K O F H 4 5 (6 + 16 * j) h (MigdalCone.mkIt K O F Lm a0 j) :=
  ⟨fun χ hχ => (WeightPosJ.weight_pos_j K O F h1 h10 h hh h0 a0 ha0 Lm j χ hχ).2.2,
   fun Fx xs hu s hs => RPFlow.rp_flow_j K O F h1 h10 h hh h0 a0 ha0 Lm j Fx xs hu s hs,
   GapBothJ.both_flow_d4_j K O F H DH h1 h10 h hh h0 a0 ha0 Lm j⟩

end OSBundleJ

#print axioms OSBundleJ.os_bundle_j

#print axioms: 1 theorem, no axioms.

90.71 \(\beta \in [0,8]\): reflection positivity with Cauchy estimate and clustering

For every \(\beta \in [0,8]\) the flowed weight after \(k_0+j\) steps (\(k_0\le 30\)) is reflection positive and has the box-order Cauchy estimate and clustering with rate \(2^{-(7+16j)}\).

\[ \forall t\in [0,4]\ \exists k_0\le 30\ \forall j:\ \mathrm {RP}(w_{k_0+j})\wedge \text {Cauchy}_{\mathrm {box}}(w_{k_0+j})\wedge \text {Clustering}_{2^{-(7+16j)}}(w_{k_0+j}) \]

ym/GapBothBoxFlow08JR.lean

/-
  ym/GapBothBoxFlow08JR — T45, G40: Z⁴, every β ∈ [0,8], every truncation 40 + m: there is k₀ ≤ 30 such that for every j
  the flowed weight after k₀ + j Migdal steps is reflection positive (on unit quaternions, any normalisation s ≥ 0) AND
  has the box-order Cauchy estimate and clustering with decay 2^{-(7+16j)}. Lean 4 core only, no axioms.
-/
import ym.GapBothBoxFlow08J
import ym.RPFlow

namespace GapBothBoxFlow08JR

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **β ∈ [0,8]: reflection positivity + Cauchy estimate + clustering (decay 2^{-(7+16j)}) for one flowed weight.** -/
theorem both_rp_flow08J (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 4)) (mm : Nat) :
    ∃ k0, k0 ≤ 30 ∧ ∀ j,
      FlowAfterJ.BoxBoth K O F H h j (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + mm) 11).tail (k0 + j)) ∧
      ∀ (Fx : CxPair.SU R → R) (xs : List (CxPair.SU R)), (∀ x, memL x xs → CxPair.snrm K x = K.one) →
        ∀ s, O.le K.zero s → O.le K.zero (RPDot.Q K Fx xs (fun x y => K.add s (K.mul s
          (SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (40 + mm) 11).tail (k0 + j))
            (fun n => RPDot.chiK K n x y))))) := by
  obtain ⟨k0, hk, hk0⟩ := Cover08T8B.cover08T8 K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, hk, fun j => ⟨FlowAfterJ.after_k K O F H DH h1 h10 h hh h0 10 _ k0 (hk0 0) j,
    fun Fx xs hu s hs => RPFlow.rp_of_cone K O F h1 h10 _ _ (hk0 j) Fx xs hu s hs⟩⟩

end GapBothBoxFlow08JR

#print axioms GapBothBoxFlow08JR.both_rp_flow08J

#print axioms: 1 theorem, no axioms.

90.72 \(\beta \in [0,16]\): reflection positivity with Cauchy estimate and clustering

For every \(\beta \in [0,16]\) and truncation \(60+m\) the flowed weight after \(k_0+j\) steps (\(k_0\le 35\)) is reflection positive and has the box-order Cauchy estimate and clustering with rate \(2^{-(7+16j)}\).

\[ \forall t\in [0,8]\ \exists k_0\le 35\ \forall j:\ \mathrm {RP}(w_{k_0+j})\wedge \text {Cauchy}_{\mathrm {box}}(w_{k_0+j})\wedge \text {Clustering}_{2^{-(7+16j)}}(w_{k_0+j}) \]

ym/GapBothBoxFlow16JR.lean

/-
  ym/GapBothBoxFlow16JR — T45, G40: Z⁴, every β ∈ [0,16], every truncation 60 + m: there is k₀ ≤ 35 such that for every j
  the flowed weight after k₀ + j Migdal steps is reflection positive (on unit quaternions, any normalisation s ≥ 0) AND
  has the box-order Cauchy estimate and clustering with decay 2^{-(7+16j)}. Lean 4 core only, no axioms.
-/
import ym.GapBothBoxFlow16J
import ym.RPFlow

namespace GapBothBoxFlow16JR

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- **β ∈ [0,16]: reflection positivity + Cauchy estimate + clustering (decay 2^{-(7+16j)}) for one flowed weight.** -/
theorem both_rp_flow16J (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 8)) (mm : Nat) :
    ∃ k0, k0 ≤ 35 ∧ ∀ j,
      FlowAfterJ.BoxBoth K O F H h j (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (60 + mm) 11).tail (k0 + j)) ∧
      ∀ (Fx : CxPair.SU R → R) (xs : List (CxPair.SU R)), (∀ x, memL x xs → CxPair.snrm K x = K.one) →
        ∀ s, O.le K.zero s → O.le K.zero (RPDot.Q K Fx xs (fun x y => K.add s (K.mul s
          (SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (60 + mm) 11).tail (k0 + j))
            (fun n => RPDot.chiK K n x y))))) := by
  obtain ⟨k0, hk, hk0⟩ := Cover16T8B.cover16T8 K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, hk, fun j => ⟨FlowAfterJ.after_k K O F H DH h1 h10 h hh h0 10 _ k0 (hk0 0) j,
    fun Fx xs hu s hs => RPFlow.rp_of_cone K O F h1 h10 _ _ (hk0 j) Fx xs hu s hs⟩⟩

end GapBothBoxFlow16JR

#print axioms GapBothBoxFlow16JR.both_rp_flow16J

#print axioms: 1 theorem, no axioms.

90.73 Decimation on the heat tube

Decimation raises the ratios to the heat kernel to the 4th power and maps the tube \([lo,hi]\) to \([lo^4,hi^4]\).

\[ a_n=(n+1)q^{n(n+2)}r_n\ \Rightarrow \ \mathrm {dec}(a)_n=(n+1)(q^4)^{n(n+2)}r_n^4 \]

ym/HeatTube.lean

/-
  ym/HeatTube — T45, G44 (second exact piece): decimation acts on relative deviations from the heat kernel by 4th powers:
  if a_n = (n+1) q^{n(n+2)} · r_n then decM a = (n+1) (q⁴)^{n(n+2)} · r_n⁴. So the heat tube's ratio list is just
  raised to the 4th power by decimation (s ↦ 4s); only bond moving can move it. Lean 4 core only, no axioms.
-/
import ym.HeatDec

namespace HeatTube

open YMChain LinkIntegral SU2Char MigdalCone HeatDec

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- heat-kernel list times ratios r: entries (n+1) q^{n(n+2)} r_n from index m -/
def heatR (q : R) : Nat → List R → List R
  | _, [] => []
  | m, r :: rs => K.mul (K.mul (cN K (m + 1)) (pwR K q (m * (m + 2)))) r :: heatR q (m + 1) rs

/-- 4th power of each ratio -/
def pw4L : List R → List R
  | [] => []
  | r :: rs => pwR K r 4 :: pw4L rs

theorem len_pw4 : ∀ rs : List R, (pw4L K rs).length = rs.length
  | [] => rfl
  | _ :: rs => congrArg Nat.succ (len_pw4 rs)

/-- **decimation on the heat tube**: ratios go to their 4th powers, q ↦ q⁴ -/
theorem decM_heatR (h10 : K.one ≠ K.zero) (h1 : O.le K.zero K.one) (q : R) :
    ∀ m rs, decM K O F m (heatR K q m rs) = heatR K (pwR K q 4) m (pw4L K rs)
  | _, [] => rfl
  | m, r :: rs => by
    show K.mul (pwR K (K.mul (K.mul (cN K (m + 1)) (pwR K q (m * (m + 2)))) r) 4) (pwR K (F.inv (cN K (m + 1))) 3) :: _
      = K.mul (K.mul (cN K (m + 1)) (pwR K (pwR K q 4) (m * (m + 2)))) (pwR K r 4) :: _
    rw [decM_heatR h10 h1 q (m + 1) rs]
    congr 1
    have ne : cN K (m + 1) ≠ K.zero := IdThmR.cN_pos K O h1 h10 m
    have hi := F.mul_inv _ ne
    rw [pw_mul_pw, pw_mul_pw, Migdal.pw_pw, Migdal.pw_pw, show m * (m + 2) * 4 = 4 * (m * (m + 2)) by lib_nat]
    generalize pwR K q (4 * (m * (m + 2))) = X
    generalize pwR K r 4 = Y
    generalize F.inv (cN K (m + 1)) = ic at hi ⊢
    generalize cN K (m + 1) = c at hi ⊢
    show K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul K.one c) c) c) c) X) Y) (K.mul (K.mul (K.mul K.one ic) ic) ic)
      = K.mul (K.mul c X) Y
    have e : K.mul (K.mul (K.mul (K.mul (K.mul (K.mul (K.mul K.one c) c) c) c) X) Y) (K.mul (K.mul (K.mul K.one ic) ic) ic)
        = K.mul (K.mul (K.mul c X) Y) (K.mul (K.mul (K.mul c ic) (K.mul c ic)) (K.mul c ic)) := by lib_ring
    rw [e, hi]; lib_ring

/-- ratio 1 everywhere recovers HeatDec (pure heat kernel stays heat kernel) -/
def ones : Nat → List R
  | 0 => []
  | L + 1 => K.one :: ones L

theorem pw4_ones : ∀ L, pw4L K (ones K L) = ones K L
  | 0 => rfl
  | L + 1 => by
    show pwR K K.one 4 :: pw4L K (ones K L) = K.one :: ones K L
    rw [SU2Strong4D.pw_one, pw4_ones L]

theorem decM_heat_ones (h10 : K.one ≠ K.zero) (h1 : O.le K.zero K.one) (q : R) (m L : Nat) :
    decM K O F m (heatR K q m (ones K L)) = heatR K (pwR K q 4) m (ones K L) := by
  rw [decM_heatR K O F h10 h1, pw4_ones]

/-- every ratio lies in [lo, hi] -/
def inTube (lo hi : R) : List R → Prop
  | [] => True
  | r :: rs => (O.le lo r ∧ O.le r hi) ∧ inTube lo hi rs

/-- 4th powers keep a nonnegative tube: [lo, hi] ↦ [lo⁴, hi⁴] -/
theorem pw4_tube (h1 : O.le K.zero K.one) (lo hi : R) (hlo : O.le K.zero lo) :
    ∀ rs, inTube K O lo hi rs → inTube K O (pwR K lo 4) (pwR K hi 4) (pw4L K rs)
  | [] => fun _ => True.intro
  | r :: rs => fun h =>
    ⟨⟨SU2Strong4D.pw_mono K O h1 lo r hlo h.1.1 4,
      SU2Strong4D.pw_mono K O h1 r hi (O.le_trans _ _ _ hlo h.1.1) h.1.2 4⟩,
     pw4_tube h1 lo hi hlo rs h.2⟩

/-- **decimation maps the heat tube T(q; lo, hi) into T(q⁴; lo⁴, hi⁴)** -/
theorem decM_tube (h10 : K.one ≠ K.zero) (h1 : O.le K.zero K.one) (q lo hi : R) (hlo : O.le K.zero lo)
    (m : Nat) (rs : List R) (h : inTube K O lo hi rs) :
    ∃ rs', decM K O F m (heatR K q m rs) = heatR K (pwR K q 4) m rs' ∧
      inTube K O (pwR K lo 4) (pwR K hi 4) rs' ∧ rs'.length = rs.length :=
  ⟨pw4L K rs, decM_heatR K O F h10 h1 q m rs, pw4_tube K O h1 lo hi hlo rs h, len_pw4 K rs⟩

end HeatTube

#print axioms HeatTube.decM_heatR
#print axioms HeatTube.decM_heat_ones
#print axioms HeatTube.pw4_ones
#print axioms HeatTube.len_pw4
#print axioms HeatTube.pw4_tube
#print axioms HeatTube.decM_tube

#print axioms: 6 theorems, no axioms.

90.74 Bond-moving product on the heat tube

The product of two tube weights is sandwiched by the pure heat product.

\[ lo\,lo'\,c_n(H\cdot H)\le c_n(A\cdot B)\le hi\,hi'\,c_n(H\cdot H) \]

ym/BondTube.lean

/-
  ym/BondTube — T45, G44 (bond-moving piece 1): the character product of two heat-tube weights stays in the product tube
  of the pure heat-kernel product: if A_j = H_j r_j, B_k = H_k r'_k with r ∈ [lo, hi], r' ∈ [lo', hi'] (lo, lo' ≥ 0), then
    lo lo' · c_n(H·H) ≤ c_n(A·B) ≤ hi hi' · c_n(H·H)   for every n.
  So bond moving moves the tube only through the pure heat-kernel product c_n(H·H). Lean 4 core only, no axioms.
-/
import ym.HeatTube

namespace BondTube

open YMChain LinkIntegral SU2Char MigdalCone HeatDec HeatTube

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem ones_tube (h1 : O.le K.zero K.one) : ∀ L, inTube K O K.one K.one (ones K L)
  | 0 => True.intro
  | L + 1 => ⟨⟨O.le_refl _, O.le_refl _⟩, ones_tube h1 L⟩

theorem len_ones : ∀ L, (ones K L).length = L
  | 0 => rfl
  | L + 1 => congrArg Nat.succ (len_ones L)

theorem hw_nn (h1 : O.le K.zero K.one) (q : R) (hq : O.le K.zero q) (m : Nat) :
    O.le K.zero (K.mul (cN K (m + 1)) (pwR K q (m * (m + 2)))) :=
  O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O q hq h1 _)

theorem heatR_nn (h1 : O.le K.zero K.one) (q lo hi : R) (hq : O.le K.zero q) (hlo : O.le K.zero lo) :
    ∀ m rs, inTube K O lo hi rs → ∀ x, memL x (heatR K q m rs) → O.le K.zero x
  | _, [], _, _, hx => False.elim hx
  | m, r :: rs, h, x, hx => by
    cases hx with
    | inl e => rw [e]; exact O.mul_nonneg _ _ (hw_nn K O h1 q hq m) (O.le_trans _ _ _ hlo h.1.1)
    | inr hx => exact heatR_nn h1 q lo hi hq hlo (m + 1) rs h.2 x hx

/-- lower tube bound for a weighted sum -/
theorem dot_lo (h1 : O.le K.zero K.one) (q lo hi : R) (hq : O.le K.zero q) (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) :
    ∀ m rs, inTube K O lo hi rs →
      O.le (K.mul lo (dot K m (heatR K q m (ones K rs.length)) f)) (dot K m (heatR K q m rs) f)
  | _, [], _ => by show O.le (K.mul lo K.zero) K.zero; rw [DetChain.mul_zero]; exact O.le_refl _
  | m, r :: rs, h => by
    show O.le (K.mul lo (K.add (K.mul (K.mul (K.mul (cN K (m + 1)) (pwR K q (m * (m + 2)))) K.one) (f m))
        (dot K (m + 1) (heatR K q (m + 1) (ones K rs.length)) f)))
      (K.add (K.mul (K.mul (K.mul (cN K (m + 1)) (pwR K q (m * (m + 2)))) r) (f m)) (dot K (m + 1) (heatR K q (m + 1) rs) f))
    have ih := dot_lo h1 q lo hi hq f hf (m + 1) rs h.2
    generalize dot K (m + 1) (heatR K q (m + 1) (ones K rs.length)) f = D1 at ih ⊢
    generalize dot K (m + 1) (heatR K q (m + 1) rs) f = D2 at ih ⊢
    have hH := O.mul_nonneg _ _ (hw_nn K O h1 q hq m) (hf m)
    have s := O.mul_le_mul_right _ _ _ h.1.1 hH
    generalize K.mul (cN K (m + 1)) (pwR K q (m * (m + 2))) = H at s hH ⊢
    have e1 : K.mul lo (K.add (K.mul (K.mul H K.one) (f m)) D1) = K.add (K.mul lo (K.mul H (f m))) (K.mul lo D1) := by lib_ring
    have e2 : K.mul (K.mul H r) (f m) = K.mul r (K.mul H (f m)) := by lib_ring
    rw [e1, e2]
    exact O.add_le_add _ _ _ _ s ih

/-- upper tube bound for a weighted sum -/
theorem dot_hi (h1 : O.le K.zero K.one) (q lo hi : R) (hq : O.le K.zero q) (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) :
    ∀ m rs, inTube K O lo hi rs →
      O.le (dot K m (heatR K q m rs) f) (K.mul hi (dot K m (heatR K q m (ones K rs.length)) f))
  | _, [], _ => by show O.le K.zero (K.mul hi K.zero); rw [DetChain.mul_zero]; exact O.le_refl _
  | m, r :: rs, h => by
    show O.le (K.add (K.mul (K.mul (K.mul (cN K (m + 1)) (pwR K q (m * (m + 2)))) r) (f m)) (dot K (m + 1) (heatR K q (m + 1) rs) f))
      (K.mul hi (K.add (K.mul (K.mul (K.mul (cN K (m + 1)) (pwR K q (m * (m + 2)))) K.one) (f m))
        (dot K (m + 1) (heatR K q (m + 1) (ones K rs.length)) f)))
    have ih := dot_hi h1 q lo hi hq f hf (m + 1) rs h.2
    generalize dot K (m + 1) (heatR K q (m + 1) (ones K rs.length)) f = D1 at ih ⊢
    generalize dot K (m + 1) (heatR K q (m + 1) rs) f = D2 at ih ⊢
    have hH := O.mul_nonneg _ _ (hw_nn K O h1 q hq m) (hf m)
    have s := O.mul_le_mul_right _ _ _ h.1.2 hH
    generalize K.mul (cN K (m + 1)) (pwR K q (m * (m + 2))) = H at s hH ⊢
    have e1 : K.mul hi (K.add (K.mul (K.mul H K.one) (f m)) D1) = K.add (K.mul hi (K.mul H (f m))) (K.mul hi D1) := by lib_ring
    have e2 : K.mul (K.mul H r) (f m) = K.mul r (K.mul H (f m)) := by lib_ring
    rw [e1, e2]
    exact O.add_le_add _ _ _ _ s ih

/-- **bond-moving product, lower tube bound**: lo lo' c_n(H·H) ≤ c_n(A·B) -/
theorem cgc_lo (h1 : O.le K.zero K.one) (q lo hi lo' hi' : R) (hq : O.le K.zero q) (hlo : O.le K.zero lo)
    (hlo' : O.le K.zero lo') (rs rs' : List R) (h : inTube K O lo hi rs) (h' : inTube K O lo' hi' rs') (n : Nat) :
    O.le (K.mul (K.mul lo lo') (cgc K (heatR K q 0 (ones K rs.length)) (heatR K q 0 (ones K rs'.length)) n))
      (cgc K (heatR K q 0 rs) (heatR K q 0 rs') n) := by
  have gnn : ∀ j k, O.le K.zero (cN K (Fusion.cg j k n)) := fun j k => SU2CharExp.cN_nn K O h1 _
  have nnX := heatR_nn K O h1 q lo hi hq hlo 0 rs h
  have nnY1 := heatR_nn K O h1 q K.one K.one hq h1 0 _ (ones_tube K O h1 rs'.length)
  show O.le (K.mul (K.mul lo lo') (dot K 0 (heatR K q 0 (ones K rs.length))
      (fun j => dot K 0 (heatR K q 0 (ones K rs'.length)) (fun k => cN K (Fusion.cg j k n)))))
    (dot K 0 (heatR K q 0 rs) (fun j => dot K 0 (heatR K q 0 rs') (fun k => cN K (Fusion.cg j k n))))
  have s2 := ConeBound.dot_mono K O _ _ (fun j => dot_lo K O h1 q lo' hi' hq _ (gnn j) 0 rs' h') 0 _ nnX
  have fnn : ∀ j, O.le K.zero (K.mul lo' (dot K 0 (heatR K q 0 (ones K rs'.length)) (fun k => cN K (Fusion.cg j k n)))) :=
    fun j => O.mul_nonneg _ _ hlo' (ConeBound.dot_nn K O _ (gnn j) 0 _ nnY1)
  have s3 := dot_lo K O h1 q lo hi hq _ fnn 0 rs h
  rw [dot_fsmul] at s3
  rw [show K.mul (K.mul lo lo') (dot K 0 (heatR K q 0 (ones K rs.length))
      (fun j => dot K 0 (heatR K q 0 (ones K rs'.length)) (fun k => cN K (Fusion.cg j k n))))
    = K.mul lo (K.mul lo' (dot K 0 (heatR K q 0 (ones K rs.length))
      (fun j => dot K 0 (heatR K q 0 (ones K rs'.length)) (fun k => cN K (Fusion.cg j k n))))) from by lib_ring]
  exact O.le_trans _ _ _ s3 s2

/-- **bond-moving product, upper tube bound**: c_n(A·B) ≤ hi hi' c_n(H·H) -/
theorem cgc_hi (h1 : O.le K.zero K.one) (q lo hi lo' hi' : R) (hq : O.le K.zero q) (hlo : O.le K.zero lo)
    (hhi' : O.le K.zero hi') (rs rs' : List R) (h : inTube K O lo hi rs) (h' : inTube K O lo' hi' rs') (n : Nat) :
    O.le (cgc K (heatR K q 0 rs) (heatR K q 0 rs') n)
      (K.mul (K.mul hi hi') (cgc K (heatR K q 0 (ones K rs.length)) (heatR K q 0 (ones K rs'.length)) n)) := by
  have gnn : ∀ j k, O.le K.zero (cN K (Fusion.cg j k n)) := fun j k => SU2CharExp.cN_nn K O h1 _
  have nnX := heatR_nn K O h1 q lo hi hq hlo 0 rs h
  have nnY1 := heatR_nn K O h1 q K.one K.one hq h1 0 _ (ones_tube K O h1 rs'.length)
  show O.le (dot K 0 (heatR K q 0 rs) (fun j => dot K 0 (heatR K q 0 rs') (fun k => cN K (Fusion.cg j k n))))
    (K.mul (K.mul hi hi') (dot K 0 (heatR K q 0 (ones K rs.length))
      (fun j => dot K 0 (heatR K q 0 (ones K rs'.length)) (fun k => cN K (Fusion.cg j k n)))))
  have s2 := ConeBound.dot_mono K O _ _ (fun j => dot_hi K O h1 q lo' hi' hq _ (gnn j) 0 rs' h') 0 _ nnX
  have fnn : ∀ j, O.le K.zero (K.mul hi' (dot K 0 (heatR K q 0 (ones K rs'.length)) (fun k => cN K (Fusion.cg j k n)))) :=
    fun j => O.mul_nonneg _ _ hhi' (ConeBound.dot_nn K O _ (gnn j) 0 _ nnY1)
  have s3 := dot_hi K O h1 q lo hi hq _ fnn 0 rs h
  rw [dot_fsmul K _ hi' 0 (heatR K q 0 (ones K rs.length))] at s3
  rw [show K.mul (K.mul hi hi') (dot K 0 (heatR K q 0 (ones K rs.length))
      (fun j => dot K 0 (heatR K q 0 (ones K rs'.length)) (fun k => cN K (Fusion.cg j k n))))
    = K.mul hi (K.mul hi' (dot K 0 (heatR K q 0 (ones K rs.length))
      (fun j => dot K 0 (heatR K q 0 (ones K rs'.length)) (fun k => cN K (Fusion.cg j k n))))) from by lib_ring]
  exact O.le_trans _ _ _ s2 s3

end BondTube

#print axioms BondTube.ones_tube
#print axioms BondTube.heatR_nn
#print axioms BondTube.dot_lo
#print axioms BondTube.dot_hi
#print axioms BondTube.cgc_lo
#print axioms BondTube.cgc_hi

#print axioms: 6 theorems, no axioms.

90.75 Clebsch–Gordan split and heat pair weights

The SU(2) fusion count is a difference of U(1) indicators, and heat pair weights complete the square.

\[ cg(j,k,n)+[n+2\le |j-k|]=[n\le j+k],\qquad d^2Q^{M^2+d^2}+4H_jH_kq^2=M^2Q^{M^2+d^2} \]

ym/CgSplit.lean

/-
  ym/CgSplit — T45, G44 (exact heat-product formula, combinatorial core): the SU(2) Clebsch–Gordan count is a
  difference of two U(1)-type indicators,
    cg(j,k,n) + [n+2 ≤ |j−k|, same parity] = [n ≤ j+k, same parity].
  With the odd extension w_{−m} = −w_m this turns c_n(A·B) into a tail sum Σ_{M ≥ n+2, M ≡ n} (w^A ⋆ w^B)(M) of a U(1)
  convolution, where heat kernels factor exactly (Gaussian completion of squares). Lean 4 core only, no axioms.
-/
import ym.Fusion
import ym.MigdalCone

namespace CgSplit

open SU2Haar Fusion

/-- parity indicator: 1 if t is even -/
def ev : Nat → Nat
  | 0 => 1
  | 1 => 0
  | t + 2 => ev t

/-- lp n t = 1 iff n ≤ t and t − n is even -/
def lp : Nat → Nat → Nat
  | 0, t => ev t
  | _ + 1, 0 => 0
  | n + 1, t + 1 => lp n t

/-- |j − k| -/
def dist : Nat → Nat → Nat
  | 0, k => k
  | j + 1, 0 => j + 1
  | j + 1, k + 1 => dist j k

theorem lp_split : ∀ n t, lp n t = dl n t + lp (n + 2) t
  | 0, 0 => rfl
  | 0, 1 => rfl
  | 0, t + 2 => by
    show ev t = 0 + lp 0 t
    rw [Nat.zero_add]; rfl
  | n + 1, 0 => rfl
  | n + 1, t + 1 => lp_split n t

theorem lp_step : ∀ n t, lp n (t + 2) = lp n t + dl n (t + 2)
  | 0, t => by show ev t = ev t + 0; rfl
  | 1, 0 => rfl
  | n + 2, 0 => by
    show lp (n + 1) 1 = 0 + dl (n + 1) 1
    show lp n 0 = 0 + dl n 0
    rw [Nat.zero_add]
    cases n with
    | zero => rfl
    | succ n => rfl
  | n + 1, t + 1 => by
    show lp n (t + 2) = lp n t + dl n (t + 2)
    exact lp_step n t

/-- **cg as a difference of U(1) indicators** -/
theorem cg_split : ∀ j k n, cg j k n + lp (n + 2) (dist j k) = lp n (j + k)
  | 0, k, n => by
    show dl k n + lp (n + 2) k = lp n (0 + k)
    rw [Nat.zero_add, lp_split n k, SU2Haar.dl_symm]
  | j + 1, 0, n => by
    show dl (j + 1) n + lp (n + 2) (j + 1) = lp n (j + 1 + 0)
    rw [Nat.add_zero, lp_split n (j + 1), SU2Haar.dl_symm]
  | j + 1, k + 1, n => by
    show (cg j k n + dl (k + j + 2) n) + lp (n + 2) (dist j k) = lp n (j + 1 + (k + 1))
    have ih := cg_split j k n
    rw [show j + 1 + (k + 1) = j + k + 2 by lib_nat, lp_step, ← ih, show k + j + 2 = j + k + 2 by lib_nat,
      SU2Haar.dl_symm (j + k + 2) n]
    generalize cg j k n = a; generalize dl n (j + k + 2) = b; generalize lp (n + 2) (dist j k) = c
    lib_nat

/-- completing the square for a pair: (j+k)² = |j−k|² + 4jk -/
theorem sq_split : ∀ j k, (j + k) * (j + k) = dist j k * dist j k + 4 * (j * k)
  | 0, k => by show (0 + k) * (0 + k) = k * k + 4 * (0 * k); rw [Nat.zero_add, Nat.zero_mul, Nat.mul_zero, Nat.add_zero]
  | j + 1, 0 => rfl
  | j + 1, k + 1 => by
    show (j + 1 + (k + 1)) * (j + 1 + (k + 1)) = dist j k * dist j k + 4 * ((j + 1) * (k + 1))
    rw [show (j + 1 + (k + 1)) * (j + 1 + (k + 1)) = (j + k) * (j + k) + 4 * (j + k) + 4 by lib_nat, sq_split j k]
    generalize dist j k * dist j k = D
    lib_nat

/-- and 2(j² + k²) = (j+k)² + |j−k|² -/
theorem sq_sum : ∀ j k, 2 * (j * j + k * k) = (j + k) * (j + k) + dist j k * dist j k
  | 0, k => by show 2 * (0 * 0 + k * k) = (0 + k) * (0 + k) + k * k; rw [Nat.zero_add]; lib_nat
  | j + 1, 0 => by show 2 * ((j + 1) * (j + 1) + 0 * 0) = (j + 1 + 0) * (j + 1 + 0) + (j + 1) * (j + 1); rw [Nat.add_zero]; lib_nat
  | j + 1, k + 1 => by
    show 2 * ((j + 1) * (j + 1) + (k + 1) * (k + 1)) = (j + 1 + (k + 1)) * (j + 1 + (k + 1)) + dist j k * dist j k
    have ih := sq_sum j k
    rw [show 2 * ((j + 1) * (j + 1) + (k + 1) * (k + 1)) = 2 * (j * j + k * k) + 4 * (j + k) + 4 by lib_nat, ih,
      show (j + 1 + (k + 1)) * (j + 1 + (k + 1)) = (j + k) * (j + k) + 4 * (j + k) + 4 by lib_nat]
    generalize dist j k * dist j k = D; generalize (j + k) * (j + k) = S; generalize 4 * (j + k) = T
    lib_nat

/-- cg(j,k,0) = δ(j,k) -/
theorem cg_zero : ∀ j k, cg j k 0 = dl j k
  | 0, k => SU2Haar.dl_symm k 0
  | j + 1, 0 => rfl
  | j + 1, k + 1 => by
    show cg j k 0 + dl (k + j + 2) 0 = dl j k
    rw [cg_zero j k]; rfl

/-- cg(j,k,1) = δ(k, j+1) + δ(k+1, j) -/
theorem cg_one : ∀ j k, cg j k 1 = dl k (j + 1) + dl (k + 1) j
  | 0, 0 => rfl
  | 0, k + 1 => by
    show dl (k + 1) 1 = dl (k + 1) 1 + dl (k + 2) 0
    rfl
  | j + 1, 0 => by
    show dl (j + 1) 1 = dl 0 (j + 2) + dl 1 (j + 1)
    show dl (j + 1) 1 = 0 + dl 0 j
    rw [Nat.zero_add]
    cases j with
    | zero => rfl
    | succ j => rfl
  | j + 1, k + 1 => by
    show cg j k 1 + dl (k + j + 2) 1 = dl k (j + 1) + dl (k + 1) j
    rw [cg_one j k]
    show dl k (j + 1) + dl (k + 1) j + dl (k + j + 1) 0 = _
    rw [show k + j + 1 = (k + j) + 1 from rfl]
    rfl

section Ring
open YMChain LinkIntegral SU2Char MigdalCone
variable {R : Type} (K : DetChain.CRing R)

/-- **ring form**: c_n(A·B) + Σ A_j B_k [n+2 ≤ |j−k|] = Σ A_j B_k [n ≤ j+k] (same parity) -/
theorem cgc_split (a b : List R) (n : Nat) :
    K.add (cgc K a b n) (dot K 0 a (fun j => dot K 0 b (fun k => cN K (lp (n + 2) (dist j k)))))
      = dot K 0 a (fun j => dot K 0 b (fun k => cN K (lp n (j + k)))) := by
  show K.add (dot K 0 a (fun j => dot K 0 b (fun k => cN K (cg j k n))))
      (dot K 0 a (fun j => dot K 0 b (fun k => cN K (lp (n + 2) (dist j k))))) = _
  rw [← dot_fadd]
  apply dot_ext
  intro j
  rw [← dot_fadd]
  apply dot_ext
  intro k
  rw [← cN_add, cg_split]
end Ring

end CgSplit

#print axioms CgSplit.lp_split
#print axioms CgSplit.lp_step
#print axioms CgSplit.cg_split
#print axioms CgSplit.cgc_split
#print axioms CgSplit.sq_split
#print axioms CgSplit.sq_sum
#print axioms CgSplit.cg_zero
#print axioms CgSplit.cg_one

ym/HeatPair.lean

/-
  ym/HeatPair — T45, G44d (heat pair weight by completing the square): with q = Q², m = j+1, l = k+1, M = m+l, d = |m−l|,
    d² Q^{M²+d²} + 4 · H_j H_k q² = M² Q^{M²+d²},   H_j = (j+1) q^{j(j+2)},
  i.e. H_j H_k = q^{−2} (M² − d²)/4 · Q^{M²+d²}: the pair weight depends on (M, d) through a Gaussian in each.
  Lean 4 core only, no axioms.
-/
import ym.CgSplit
import ym.HeatDec

namespace HeatPair

open YMChain LinkIntegral SU2Char CgSplit

variable {R : Type} (K : DetChain.CRing R)

theorem heat_pair (Q : R) (j k : Nat) :
    K.add (K.mul (cN K (dist j k * dist j k)) (pwR K Q ((j + k + 2) * (j + k + 2) + dist j k * dist j k)))
      (K.mul (cN K 4) (K.mul (K.mul (K.mul (cN K (j + 1)) (pwR K (K.mul Q Q) (j * (j + 2))))
        (K.mul (cN K (k + 1)) (pwR K (K.mul Q Q) (k * (k + 2))))) (pwR K (K.mul Q Q) 2)))
    = K.mul (cN K ((j + k + 2) * (j + k + 2))) (pwR K Q ((j + k + 2) * (j + k + 2) + dist j k * dist j k)) := by
  have e1 : K.mul (K.mul (K.mul (cN K (j + 1)) (pwR K (K.mul Q Q) (j * (j + 2))))
        (K.mul (cN K (k + 1)) (pwR K (K.mul Q Q) (k * (k + 2))))) (pwR K (K.mul Q Q) 2)
      = K.mul (K.mul (cN K (j + 1)) (cN K (k + 1))) (pwR K (K.mul Q Q) (j * (j + 2) + k * (k + 2) + 2)) := by
    rw [GKS.pwR_add, GKS.pwR_add]; lib_ring
  have e2 : pwR K (K.mul Q Q) (j * (j + 2) + k * (k + 2) + 2)
      = pwR K Q ((j + k + 2) * (j + k + 2) + dist j k * dist j k) := by
    rw [HeatDec.pw_mul_pw, ← GKS.pwR_add]
    congr 1
    have s := sq_sum (j + 1) (k + 1)
    have d : dist (j + 1) (k + 1) = dist j k := rfl
    rw [d, show j + 1 + (k + 1) = j + k + 2 by lib_nat] at s
    rw [← s]; lib_nat
  have e3 : cN K ((j + k + 2) * (j + k + 2)) = K.add (cN K (dist j k * dist j k)) (K.mul (cN K 4) (K.mul (cN K (j + 1)) (cN K (k + 1)))) := by
    have s := sq_split (j + 1) (k + 1)
    have d : dist (j + 1) (k + 1) = dist j k := rfl
    rw [d, show j + 1 + (k + 1) = j + k + 2 by lib_nat] at s
    rw [s, cN_add, SU2CharExp.cN_mul K 4, SU2CharExp.cN_mul K (j + 1)]
  rw [e1, e2, e3]
  generalize pwR K Q ((j + k + 2) * (j + k + 2) + dist j k * dist j k) = X
  lib_ring

/-- shifting the start index of a weighted sum -/
theorem dot_off (f : Nat → R) : ∀ m p, dot K m p f = dot K 0 p (fun k => f (m + k))
  | 0, p => dot_ext K f _ (fun k => by rw [Nat.zero_add]) 0 p
  | m + 1, p => by
    rw [Fusion.dot_succ, dot_off (fun i => f (i + 1)) m p]
    exact dot_ext K _ _ (fun k => by show f (m + k + 1) = f (m + 1 + k); rw [show m + k + 1 = m + 1 + k by lib_nat]) 0 p

/-- **U(1) part is a Cauchy product**: Σ_{j,k} A_j B_k g(j+k) = Σ_T (A⋆B)_T g(T) -/
theorem dot_cauchy (g : Nat → R) (a b : List R) :
    dot K 0 a (fun j => dot K 0 b (fun k => g (j + k))) = dot K 0 (mulL K a b) g := by
  rw [dot_mulL]
  exact dot_ext K _ _ (fun j => (dot_off K g j b).symm) 0 a

/-- correlation sum Σ_{j,k} A_j B_k g(|j−k|) -/
def corrS (a b : List R) (g : Nat → R) : R := dot K 0 a (fun j => dot K 0 b (fun k => g (dist j k)))

/-- **recursion for the |j−k| part** (peeling the first entries) -/
theorem corrS_cons (x y : R) (a b : List R) (g : Nat → R) :
    corrS K (x :: a) (y :: b) g = K.add (K.add (K.add (K.mul (K.mul x y) (g 0)) (K.mul x (dot K 1 b g)))
      (K.mul y (dot K 1 a g))) (corrS K a b g) := by
  show K.add (K.mul x (K.add (K.mul y (g 0)) (dot K 1 b (fun k => g (dist 0 k)))))
      (dot K 1 a (fun j => dot K 0 (y :: b) (fun k => g (dist j k)))) = _
  rw [dot_off K _ 1 a]
  have e : ∀ j, dot K 0 (y :: b) (fun k => g (dist (1 + j) k))
      = K.add (K.mul y (g (1 + j))) (dot K 0 b (fun k => g (dist j k))) := by
    intro j
    rw [show 1 + j = j + 1 by lib_nat]
    show K.add (K.mul y (g (j + 1))) (dot K 1 b (fun k => g (dist (j + 1) k))) = _
    rw [dot_off K _ 1 b]
    congr 1
    exact dot_ext K _ _ (fun k => by rw [show 1 + k = k + 1 by lib_nat]; rfl) 0 b
  rw [dot_ext K _ _ e 0 a, dot_fadd, dot_ext K (fun j => K.mul y (g (1 + j))) (fun j => K.mul y (g (1 + j))) (fun _ => rfl),
    dot_fsmul, ← dot_off K g 1 a]
  show K.add (K.mul x (K.add (K.mul y (g 0)) (dot K 1 b g))) (K.add (K.mul y (dot K 1 a g)) (corrS K a b g)) = _
  generalize dot K 1 b g = B1; generalize dot K 1 a g = A1; generalize corrS K a b g = C
  lib_ring

/-- antidiagonal sum Σ_{j+k=T} f j k -/
def anti (f : Nat → Nat → R) : Nat → R
  | 0 => f 0 0
  | T + 1 => K.add (f 0 (T + 1)) (anti (fun j k => f (j + 1) k) T)

theorem anti_ext (f g : Nat → Nat → R) (h : ∀ j k, f j k = g j k) : ∀ T, anti K f T = anti K g T
  | 0 => h 0 0
  | T + 1 => by
    show K.add (f 0 (T + 1)) (anti K (fun j k => f (j + 1) k) T) = K.add (g 0 (T + 1)) (anti K (fun j k => g (j + 1) k) T)
    rw [h 0 (T + 1), anti_ext (fun j k => f (j + 1) k) (fun j k => g (j + 1) k) (fun j k => h (j + 1) k) T]

theorem anti_add (f g : Nat → Nat → R) : ∀ T, anti K (fun j k => K.add (f j k) (g j k)) T = K.add (anti K f T) (anti K g T)
  | 0 => rfl
  | T + 1 => by
    show K.add (K.add (f 0 (T + 1)) (g 0 (T + 1))) (anti K (fun j k => K.add (f (j + 1) k) (g (j + 1) k)) T)
      = K.add (K.add (f 0 (T + 1)) (anti K (fun j k => f (j + 1) k) T)) (K.add (g 0 (T + 1)) (anti K (fun j k => g (j + 1) k) T))
    rw [anti_add (fun j k => f (j + 1) k) (fun j k => g (j + 1) k) T]
    generalize anti K (fun j k => f (j + 1) k) T = A; generalize anti K (fun j k => g (j + 1) k) T = B
    lib_ring

/-- a factor depending only on j + k comes out of the antidiagonal sum -/
theorem anti_diag (c : Nat → R) (f : Nat → Nat → R) : ∀ T, anti K (fun j k => K.mul (c (j + k)) (f j k)) T = K.mul (c T) (anti K f T)
  | 0 => rfl
  | T + 1 => by
    show K.add (K.mul (c (0 + (T + 1))) (f 0 (T + 1))) (anti K (fun j k => K.mul (c (j + 1 + k)) (f (j + 1) k)) T)
      = K.mul (c (T + 1)) (K.add (f 0 (T + 1)) (anti K (fun j k => f (j + 1) k) T))
    rw [anti_ext K (fun j k => K.mul (c (j + 1 + k)) (f (j + 1) k)) (fun j k => K.mul (c (j + k + 1)) (f (j + 1) k))
      (fun j k => by rw [show j + 1 + k = j + k + 1 by lib_nat]) T,
      anti_diag (fun t => c (t + 1)) (fun j k => f (j + 1) k) T, Nat.zero_add]
    generalize anti K (fun j k => f (j + 1) k) T = A
    lib_ring

/-- **heat Cauchy coefficient, closed form**: with M = T+2, d = |j−k|,
    4 q² Σ_{j+k=T} H_j H_k + Q^{M²} Σ d² Q^{d²} = M² Q^{M²} Σ Q^{d²} -/
theorem heat_anti (Q : R) (T : Nat) :
    K.add (anti K (fun j k => K.mul (cN K 4) (K.mul (K.mul (K.mul (cN K (j + 1)) (pwR K (K.mul Q Q) (j * (j + 2))))
        (K.mul (cN K (k + 1)) (pwR K (K.mul Q Q) (k * (k + 2))))) (pwR K (K.mul Q Q) 2))) T)
      (K.mul (pwR K Q ((T + 2) * (T + 2))) (anti K (fun j k => K.mul (cN K (dist j k * dist j k)) (pwR K Q (dist j k * dist j k))) T))
    = K.mul (K.mul (cN K ((T + 2) * (T + 2))) (pwR K Q ((T + 2) * (T + 2))))
        (anti K (fun j k => pwR K Q (dist j k * dist j k)) T) := by
  have hp : ∀ j k, K.add (K.mul (pwR K Q ((j + k + 2) * (j + k + 2)))
        (K.mul (cN K (dist j k * dist j k)) (pwR K Q (dist j k * dist j k))))
      (K.mul (cN K 4) (K.mul (K.mul (K.mul (cN K (j + 1)) (pwR K (K.mul Q Q) (j * (j + 2))))
        (K.mul (cN K (k + 1)) (pwR K (K.mul Q Q) (k * (k + 2))))) (pwR K (K.mul Q Q) 2)))
      = K.mul (K.mul (cN K ((j + k + 2) * (j + k + 2))) (pwR K Q ((j + k + 2) * (j + k + 2)))) (pwR K Q (dist j k * dist j k)) := by
    intro j k
    have e := heat_pair K Q j k
    rw [GKS.pwR_add] at e
    generalize pwR K Q ((j + k + 2) * (j + k + 2)) = A at e ⊢
    generalize pwR K Q (dist j k * dist j k) = B at e ⊢
    generalize cN K (dist j k * dist j k) = D at e ⊢
    generalize cN K ((j + k + 2) * (j + k + 2)) = C at e ⊢
    rw [show K.mul A (K.mul D B) = K.mul D (K.mul A B) by lib_ring, show K.mul (K.mul C A) B = K.mul C (K.mul A B) by lib_ring]
    exact e
  have h2 := anti_ext K _ _ hp T
  rw [anti_add, anti_diag K (fun t => pwR K Q ((t + 2) * (t + 2))),
    anti_diag K (fun t => K.mul (cN K ((t + 2) * (t + 2))) (pwR K Q ((t + 2) * (t + 2))))] at h2
  rw [← h2, K.add_comm]

/-- coefficient of a list, zero past its end -/
def co : List R → Nat → R
  | [], _ => K.zero
  | x :: _, 0 => x
  | _ :: p, n + 1 => co p n

theorem co_addL : ∀ (p q : List R) n, co K (SU2Haar.addL K p q) n = K.add (co K p n) (co K q n)
  | [], q, n => by show co K q n = K.add K.zero (co K q n); rw [K.zero_add]
  | a :: p, [], n => by show co K (a :: p) n = K.add (co K (a :: p) n) K.zero; rw [DetChain.add_zero']
  | a :: p, b :: q, 0 => rfl
  | a :: p, b :: q, n + 1 => co_addL p q n

theorem co_smulL (c : R) : ∀ (p : List R) n, co K (SU2Haar.smulL K c p) n = K.mul c (co K p n)
  | [], n => by show K.zero = K.mul c K.zero; rw [DetChain.mul_zero]
  | a :: p, 0 => rfl
  | a :: p, n + 1 => co_smulL c p n

theorem anti_zero : ∀ (f : Nat → Nat → R), (∀ j k, f j k = K.zero) → ∀ T, anti K f T = K.zero
  | f, h, 0 => h 0 0
  | f, h, T + 1 => by
    show K.add (f 0 (T + 1)) (anti K (fun j k => f (j + 1) k) T) = K.zero
    rw [h 0 (T + 1), anti_zero (fun j k => f (j + 1) k) (fun j k => h (j + 1) k) T, K.zero_add]

/-- **Cauchy-product coefficients are antidiagonal sums** -/
theorem co_mulL : ∀ (a b : List R) T, co K (mulL K a b) T = anti K (fun j k => K.mul (co K a j) (co K b k)) T
  | [], b, T => (anti_zero K _ (fun j k => by show K.mul K.zero (co K b k) = K.zero; rw [K.mul_comm, DetChain.mul_zero]) T).symm
  | x :: a, b, 0 => by
    show co K (SU2Haar.addL K (SU2Haar.smulL K x b) (K.zero :: mulL K a b)) 0 = K.mul x (co K b 0)
    rw [co_addL, co_smulL]; show K.add (K.mul x (co K b 0)) K.zero = _; rw [DetChain.add_zero']
  | x :: a, b, T + 1 => by
    show co K (SU2Haar.addL K (SU2Haar.smulL K x b) (K.zero :: mulL K a b)) (T + 1)
      = K.add (K.mul x (co K b (T + 1))) (anti K (fun j k => K.mul (co K a j) (co K b k)) T)
    rw [co_addL, co_smulL]
    show K.add (K.mul x (co K b (T + 1))) (co K (mulL K a b) T) = _
    rw [co_mulL a b T]

/-- picking a coefficient with a Kronecker delta -/
theorem dot_dl : ∀ (b : List R) j, dot K 0 b (fun k => cN K (SU2Haar.dl j k)) = co K b j
  | [], _ => rfl
  | x :: b, 0 => by
    show K.add (K.mul x (cN K 1)) (dot K 1 b (fun k => cN K (SU2Haar.dl 0 k))) = x
    rw [dot_off K _ 1 b, dot_ext K _ (fun _ => K.zero) (fun k => by rw [show 1 + k = k + 1 by lib_nat]; rfl) 0 b,
      dot_fzero, DetChain.add_zero']
    rw [SU2CharExp.cN_one, K.mul_comm, K.one_mul]
  | x :: b, j + 1 => by
    show K.add (K.mul x (cN K 0)) (dot K 1 b (fun k => cN K (SU2Haar.dl (j + 1) k))) = co K b j
    rw [dot_off K _ 1 b, dot_ext K _ (fun k => cN K (SU2Haar.dl j k)) (fun k => by rw [show 1 + k = k + 1 by lib_nat]; rfl) 0 b,
      dot_dl b j]
    show K.add (K.mul x K.zero) (co K b j) = co K b j
    rw [DetChain.mul_zero, K.zero_add]

/-- **c₀(A·B) = Σ A_j B_j** -/
theorem cgc_zero (a b : List R) : MigdalCone.cgc K a b 0 = dot K 0 a (fun j => co K b j) := by
  show dot K 0 a (fun j => dot K 0 b (fun k => cN K (Fusion.cg j k 0))) = _
  exact dot_ext K _ _ (fun j => by rw [dot_ext K _ _ (fun k => by rw [cg_zero]) 0 b, dot_dl]) 0 a

/-- **c₁(A·B) = Σ A_j (B_{j+1} + B_{j−1})** -/
theorem cgc_one (a b : List R) : MigdalCone.cgc K a b 1 = dot K 0 a (fun j => K.add (co K b (j + 1)) (co K (K.zero :: b) j)) := by
  show dot K 0 a (fun j => dot K 0 b (fun k => cN K (Fusion.cg j k 1))) = _
  apply dot_ext
  intro j
  rw [dot_ext K _ _ (fun k => by rw [cg_one, cN_add]) 0 b, dot_fadd]
  congr 1
  · rw [dot_ext K _ _ (fun k => by rw [SU2Haar.dl_symm]) 0 b, dot_dl]
  · rw [← dot_dl K (K.zero :: b) j]
    show _ = K.add (K.mul K.zero (cN K (SU2Haar.dl j 0))) (dot K 1 b (fun k => cN K (SU2Haar.dl j k)))
    rw [dot_off K _ 1 b, K.mul_comm, DetChain.mul_zero, K.zero_add]
    exact dot_ext K _ _ (fun k => by rw [SU2Haar.dl_symm, show 1 + k = k + 1 by lib_nat]) 0 b

end HeatPair

#print axioms HeatPair.heat_pair
#print axioms HeatPair.dot_off
#print axioms HeatPair.dot_cauchy
#print axioms HeatPair.corrS_cons
#print axioms HeatPair.anti_diag
#print axioms HeatPair.heat_anti
#print axioms HeatPair.co_mulL
#print axioms HeatPair.cgc_zero
#print axioms HeatPair.cgc_one

#print axioms: 17 theorems, no axioms.

90.76 Gap of the fundamental ratio after bond moving

After bond moving, \(2c_0-c_1\) is exactly a Dirichlet form.

\[ 2c_0(A\cdot A)-c_1(A\cdot A)=A_0^2+\textstyle \sum _j(A_j-A_{j+1})^2 \]

ym/UFlow.lean

/-
  ym/UFlow — T45, G44e (fundamental-character ratio along bond moving): for any list A,
    c₁(A·A) ≤ 2 c₀(A·A)   (Σ_j A_j(A_{j+1} + A_{j−1}) ≤ 2 Σ_j A_j², Cauchy–Schwarz on adjacent pairs),
  i.e. u(A·A) = c₁/(2c₀) ≤ 1 after every bond move. Lean 4 core only, no axioms.
-/
import ym.HeatPair
import ym.QuatUnit

namespace UFlow

open YMChain LinkIntegral SU2Char HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- adjacent sum Σ A_j A_{j+1} -/
def adjS : List R → R
  | [] => K.zero
  | [_] => K.zero
  | x :: y :: r => K.add (K.mul x y) (adjS (y :: r))

/-- Σ A_j² -/
def sqS : List R → R
  | [] => K.zero
  | x :: r => K.add (K.mul x x) (sqS r)

theorem dot_co_self : ∀ a : List R, dot K 0 a (fun j => co K a j) = sqS K a
  | [] => rfl
  | x :: r => by
    show K.add (K.mul x x) (dot K 1 r (fun j => co K (x :: r) j)) = K.add (K.mul x x) (sqS K r)
    rw [dot_off K _ 1 r, dot_ext K _ (fun j => co K r j) (fun j => by rw [show 1 + j = j + 1 by lib_nat]; rfl) 0 r,
      dot_co_self r]

theorem dot_co_up : ∀ a : List R, dot K 0 a (fun j => co K a (j + 1)) = adjS K a
  | [] => rfl
  | [x] => by show K.add (K.mul x K.zero) K.zero = K.zero; rw [DetChain.mul_zero, K.zero_add]
  | x :: y :: r => by
    show K.add (K.mul x y) (dot K 1 (y :: r) (fun j => co K (x :: y :: r) (j + 1))) = K.add (K.mul x y) (adjS K (y :: r))
    rw [dot_off K _ 1 (y :: r), dot_ext K _ (fun j => co K (y :: r) (j + 1))
      (fun j => by rw [show 1 + j + 1 = j + 1 + 1 by lib_nat]; rfl) 0 (y :: r), dot_co_up (y :: r)]

theorem dot_co_cons : ∀ (x : R) (r : List R), dot K 0 r (fun j => co K (x :: r) j) = adjS K (x :: r)
  | _, [] => rfl
  | x, y :: r => by
    show K.add (K.mul y x) (dot K 1 r (fun j => co K (x :: y :: r) j)) = K.add (K.mul x y) (adjS K (y :: r))
    rw [dot_off K _ 1 r, dot_ext K _ (fun j => co K (y :: r) j) (fun j => by rw [show 1 + j = j + 1 by lib_nat]; rfl) 0 r,
      dot_co_cons y r, K.mul_comm]

theorem dot_co_down : ∀ a : List R, dot K 0 a (fun j => co K (K.zero :: a) j) = adjS K a
  | [] => rfl
  | x :: r => by
    show K.add (K.mul x K.zero) (dot K 1 r (fun j => co K (K.zero :: x :: r) j)) = adjS K (x :: r)
    rw [dot_off K _ 1 r, dot_ext K _ (fun j => co K (x :: r) j) (fun j => by rw [show 1 + j = j + 1 by lib_nat]; rfl) 0 r,
      dot_co_cons, DetChain.mul_zero, K.zero_add]

theorem amgm (x y : R) : O.le (K.add (K.mul x y) (K.mul x y)) (K.add (K.mul x x) (K.mul y y)) := by
  have h := O.add_le_add _ _ _ _ (QuatUnit.sq_nn K O (K.add x (K.neg y))) (O.le_refl (K.add (K.mul x y) (K.mul x y)))
  rw [K.zero_add] at h
  have e : K.add (K.mul (K.add x (K.neg y)) (K.add x (K.neg y))) (K.add (K.mul x y) (K.mul x y))
      = K.add (K.mul x x) (K.mul y y) := by lib_ring
  rw [e] at h; exact h

/-- 2 Σ A_jA_{j+1} + A₀² ≤ 2 Σ A_j² -/
theorem cs_adj : ∀ a : List R, O.le (K.add (K.add (adjS K a) (adjS K a)) (K.mul (co K a 0) (co K a 0)))
    (K.add (sqS K a) (sqS K a))
  | [] => by show O.le (K.add (K.add K.zero K.zero) (K.mul K.zero K.zero)) (K.add K.zero K.zero)
             rw [K.zero_add, DetChain.mul_zero, K.zero_add]; exact O.le_refl _
  | [x] => by
    show O.le (K.add (K.add K.zero K.zero) (K.mul x x)) (K.add (K.add (K.mul x x) K.zero) (K.add (K.mul x x) K.zero))
    rw [K.zero_add, K.zero_add, DetChain.add_zero']
    have := O.add_le_add _ _ _ _ (O.le_refl (K.mul x x)) (QuatUnit.sq_nn K O x)
    rw [DetChain.add_zero'] at this; exact this
  | x :: y :: r => by
    have ih := cs_adj (y :: r)
    have am := amgm K O x y
    show O.le (K.add (K.add (K.add (K.mul x y) (adjS K (y :: r))) (K.add (K.mul x y) (adjS K (y :: r)))) (K.mul x x))
      (K.add (K.add (K.mul x x) (sqS K (y :: r))) (K.add (K.mul x x) (sqS K (y :: r))))
    change O.le (K.add (K.add (adjS K (y :: r)) (adjS K (y :: r))) (K.mul y y)) (K.add (sqS K (y :: r)) (sqS K (y :: r))) at ih
    have s := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ am ih) (O.le_refl (K.mul x x))
    generalize adjS K (y :: r) = A at s ⊢; generalize sqS K (y :: r) = B at s ⊢
    have e1 : K.add (K.add (K.add (K.mul x y) (K.mul x y)) (K.add (K.add A A) (K.mul y y))) (K.mul x x)
        = K.add (K.add (K.add (K.add (K.mul x y) A) (K.add (K.mul x y) A)) (K.mul x x)) (K.mul y y) := by lib_ring
    have e2 : K.add (K.add (K.add (K.mul x x) (K.mul y y)) (K.add B B)) (K.mul x x)
        = K.add (K.add (K.add (K.mul x x) B) (K.add (K.mul x x) B)) (K.mul y y) := by lib_ring
    rw [e1, e2] at s
    have s2 := O.add_le_add _ _ _ _ s (O.le_refl (K.neg (K.mul y y)))
    have e3 : ∀ z, K.add (K.add z (K.mul y y)) (K.neg (K.mul y y)) = z := fun z => by lib_ring
    rw [e3, e3] at s2; exact s2

/-- **u(A·A) ≤ 1**: c₁(A·A) ≤ 2 c₀(A·A) -/
theorem c1_le_2c0 (a : List R) :
    O.le (MigdalCone.cgc K a a 1) (K.add (MigdalCone.cgc K a a 0) (MigdalCone.cgc K a a 0)) := by
  rw [cgc_one, cgc_zero, dot_fadd, dot_co_up, dot_co_down, dot_co_self]
  have h := cs_adj K O a
  have h2 := O.add_le_add _ _ _ _ (O.le_refl (K.add (adjS K a) (adjS K a))) (QuatUnit.sq_nn K O (co K a 0))
  rw [DetChain.add_zero'] at h2
  exact O.le_trans _ _ _ h2 h

/-- Dirichlet sum: Σ (A_j − A_{j+1})², with the last entry against 0 -/
def diffS : List R → R
  | [] => K.zero
  | [x] => K.mul x x
  | x :: y :: r => K.add (K.mul (K.add x (K.neg y)) (K.add x (K.neg y))) (diffS (y :: r))

/-- **Dirichlet identity**: 2 Σ A_j² = 2 Σ A_jA_{j+1} + A₀² + Σ (A_j − A_{j+1})² -/
theorem dirichlet : ∀ a : List R, K.add (sqS K a) (sqS K a)
    = K.add (K.add (K.add (adjS K a) (adjS K a)) (K.mul (co K a 0) (co K a 0))) (diffS K a)
  | [] => by show K.add K.zero K.zero = K.add (K.add (K.add K.zero K.zero) (K.mul K.zero K.zero)) K.zero; lib_ring
  | [x] => by
    show K.add (K.add (K.mul x x) K.zero) (K.add (K.mul x x) K.zero)
      = K.add (K.add (K.add K.zero K.zero) (K.mul x x)) (K.mul x x)
    lib_ring
  | x :: y :: r => by
    have ih := dirichlet (y :: r)
    change K.add (sqS K (y :: r)) (sqS K (y :: r))
      = K.add (K.add (K.add (adjS K (y :: r)) (adjS K (y :: r))) (K.mul y y)) (diffS K (y :: r)) at ih
    show K.add (K.add (K.mul x x) (sqS K (y :: r))) (K.add (K.mul x x) (sqS K (y :: r)))
      = K.add (K.add (K.add (K.add (K.mul x y) (adjS K (y :: r))) (K.add (K.mul x y) (adjS K (y :: r)))) (K.mul x x))
        (K.add (K.mul (K.add x (K.neg y)) (K.add x (K.neg y))) (diffS K (y :: r)))
    have e : K.add (K.add (K.mul x x) (sqS K (y :: r))) (K.add (K.mul x x) (sqS K (y :: r)))
        = K.add (K.add (K.mul x x) (K.mul x x)) (K.add (sqS K (y :: r)) (sqS K (y :: r))) := by lib_ring
    rw [e, ih]
    generalize adjS K (y :: r) = A; generalize diffS K (y :: r) = D
    lib_ring

/-- **gap of u after bond moving**: 2c₀(A·A) − c₁(A·A) = A₀² + Σ (A_j − A_{j+1})² -/
theorem u_gap (a : List R) :
    K.add (MigdalCone.cgc K a a 0) (MigdalCone.cgc K a a 0)
      = K.add (MigdalCone.cgc K a a 1) (K.add (K.mul (co K a 0) (co K a 0)) (diffS K a)) := by
  rw [cgc_one, cgc_zero, dot_fadd, dot_co_up, dot_co_down, dot_co_self, dirichlet]
  generalize adjS K a = A; generalize diffS K a = D
  lib_ring

end UFlow

#print axioms UFlow.amgm
#print axioms UFlow.cs_adj
#print axioms UFlow.c1_le_2c0
#print axioms UFlow.dirichlet
#print axioms UFlow.u_gap

#print axioms: 5 theorems, no axioms.

90.77 One heat-kernel bond move: \(s_{\mathrm {eff}}\ge s/2\)

The truncated heat-product inequality holds for small \(Q\), on \([0.6,0.97]\) by certificate, and for all \(Q<1\) under three explicit smallness conditions.

\[ \textstyle \sum _{m<n}4(m+1)(m+2)Q^{(2m+3)^2}\le \sum _{m<n}4(m+1)^2Q^{(2m+2)^2+2} \]

ym/SmallQ.lean

/-
  ym/SmallQ — T45, G44 route 8 (small-Q part, finite + geometric tail): if Q ≥ 0 and Q³ ≤ 1/4 (Q ≤ 0.63), then for every
  truncation M ≥ 1
    Σ_{m=1}^{M} 4m(m+1) Q^{4m²+4m+1}  ≤  Σ_{m=1}^{M} 4m² Q^{4m²+2},
  i.e. T(Q) ≥ 0 (one heat bond move gives s_eff ≥ s/2) in the strong-coupling range. The negative terms halve from one m
  to the next, so they sum to ≤ 2·8Q⁹ ≤ 4Q⁶. Lean 4 core only, no axioms.
-/
import ym.AltSum
import ym.Tail

namespace SmallQ

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- negative term, index m+1: 4(m+1)(m+2) Q^{4(m+1)²+4(m+1)+1} -/
def tN (Q : R) (m : Nat) : R := K.mul (cN K (4 * ((m + 1) * (m + 2)))) (pwR K Q (4 * ((m + 1) * (m + 1)) + 4 * (m + 1) + 1))

/-- positive term, index m+1: 4(m+1)² Q^{4(m+1)²+2} -/
def tP (Q : R) (m : Nat) : R := K.mul (cN K (4 * ((m + 1) * (m + 1)))) (pwR K Q (4 * ((m + 1) * (m + 1)) + 2))

def sumF (f : Nat → R) : Nat → Nat → R
  | _, 0 => K.zero
  | m, n + 1 => K.add (f m) (sumF f (m + 1) n)

theorem two_pow_h (h : R) (hh : K.add h h = K.one) : ∀ k, K.mul (cN K (2 ^ k)) (pwR K h k) = K.one
  | 0 => by show K.mul (cN K 1) K.one = K.one; rw [SU2CharExp.cN_one, K.one_mul]
  | k + 1 => by
    rw [show 2 ^ (k + 1) = 2 ^ k * 2 from rfl, SU2CharExp.cN_mul]
    show K.mul (K.mul (cN K (2 ^ k)) (cN K 2)) (K.mul (pwR K h k) h) = K.one
    have e2 : cN K 2 = K.add K.one K.one := by
      rw [show (2 : Nat) = 1 + 1 from rfl, cN_add, SU2CharExp.cN_one]
    rw [show K.mul (K.mul (cN K (2 ^ k)) (cN K 2)) (K.mul (pwR K h k) h)
        = K.mul (K.mul (cN K (2 ^ k)) (pwR K h k)) (K.mul (cN K 2) h) by lib_ring, two_pow_h h hh k, e2,
      show K.mul (K.add K.one K.one) h = K.add h h by lib_ring, hh, K.one_mul]

theorem pw_anti (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (a j : Nat) :
    O.le (pwR K Q (a + j)) (pwR K Q a) := by
  rw [GKS.pwR_add]
  have := O.mul_le_mul_right _ _ _ (Tail.pw_le_one K O h1 Q hQ0 hQ1 j) (AreaLower.pw_nn K O Q hQ0 h1 a)
  rw [K.one_mul, K.mul_comm] at this; exact this

/-- each negative term is at most half the previous one -/
theorem ratio (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (h16 : O.le (pwR K Q 16) (pwR K h 6)) (m : Nat) :
    O.le (K.add (tN K Q (m + 1)) (tN K Q (m + 1))) (tN K Q m) := by
  have eE : 4 * ((m + 1 + 1) * (m + 1 + 1)) + 4 * (m + 1 + 1) + 1 = (4 * ((m + 1) * (m + 1)) + 4 * (m + 1) + 1) + (16 + 8 * m) := by lib_nat
  have hY : O.le (pwR K Q (16 + 8 * m)) (pwR K h 6) := O.le_trans _ _ _ (pw_anti K O h1 Q hQ0 hQ1 16 (8 * m)) h16
  have eC : cN K (4 * ((m + 1 + 1) * (m + 1 + 2))) = K.mul (cN K (4 * (m + 2))) (cN K (m + 3)) := by
    rw [← SU2CharExp.cN_mul]; congr 1; lib_nat
  have eT : cN K (4 * ((m + 1) * (m + 2))) = K.mul (cN K (4 * (m + 2))) (cN K (m + 1)) := by
    rw [← SU2CharExp.cN_mul]; congr 1; lib_nat
  -- 2 (m+3) h⁶ ≤ m+1
  have c64 := two_pow_h K h hh 6
  have hc : O.le (K.mul (cN K (2 * (m + 3))) (pwR K h 6)) (cN K (m + 1)) := by
    have mono := SU2CharExp.cN_mono K O h1 (2 * (m + 3)) (2 ^ 6 * (m + 1)) (by
      show 2 * (m + 3) ≤ 64 * (m + 1)
      exact Nat.le_of_lt_succ (Nat.lt_succ_of_le (by
        rw [show 64 * (m + 1) = 2 * (m + 3) + (62 * m + 58) by lib_nat]; exact Nat.le_add_right _ _)))
    have s := O.mul_le_mul_right _ _ _ mono (AreaLower.pw_nn K O h h0 h1 6)
    rw [SU2CharExp.cN_mul K (2 ^ 6), show K.mul (K.mul (cN K (2 ^ 6)) (cN K (m + 1))) (pwR K h 6)
      = K.mul (cN K (m + 1)) (K.mul (cN K (2 ^ 6)) (pwR K h 6)) by lib_ring, c64, K.mul_comm (cN K (m + 1)), K.one_mul] at s
    exact s
  unfold tN
  rw [eE, GKS.pwR_add, eC, eT]
  have nX := AreaLower.pw_nn K O Q hQ0 h1 (4 * ((m + 1) * (m + 1)) + 4 * (m + 1) + 1)
  have nA := SU2CharExp.cN_nn K O h1 (4 * (m + 2))
  have nB := SU2CharExp.cN_nn K O h1 (m + 3)
  generalize pwR K Q (4 * ((m + 1) * (m + 1)) + 4 * (m + 1) + 1) = X at nX ⊢
  generalize pwR K Q (16 + 8 * m) = Y at hY ⊢
  -- LHS = A·(2(m+3))·X·Y ≤ A·(2(m+3))·X·h⁶ ≤ A·(m+1)·X
  have e2 : cN K (2 * (m + 3)) = K.add (cN K (m + 3)) (cN K (m + 3)) := by
    rw [show 2 * (m + 3) = (m + 3) + (m + 3) by lib_nat, cN_add]
  rw [e2] at hc
  have n2 : O.le K.zero (K.mul (K.mul (cN K (4 * (m + 2))) (K.add (cN K (m + 3)) (cN K (m + 3)))) X) :=
    O.mul_nonneg _ _ (O.mul_nonneg _ _ nA (by have := O.add_le_add _ _ _ _ nB nB; rw [K.zero_add] at this; exact this)) nX
  have s1 := O.mul_le_mul_right _ _ _ hY n2
  have s2 := O.mul_le_mul_right _ _ _ hc (O.mul_nonneg _ _ nA nX)
  have l1 : K.add (K.mul (K.mul (cN K (4 * (m + 2))) (cN K (m + 3))) (K.mul X Y)) (K.mul (K.mul (cN K (4 * (m + 2))) (cN K (m + 3))) (K.mul X Y))
      = K.mul Y (K.mul (K.mul (cN K (4 * (m + 2))) (K.add (cN K (m + 3)) (cN K (m + 3)))) X) := by lib_ring
  have l2 : K.mul (pwR K h 6) (K.mul (K.mul (cN K (4 * (m + 2))) (K.add (cN K (m + 3)) (cN K (m + 3)))) X)
      = K.mul (K.mul (K.add (cN K (m + 3)) (cN K (m + 3))) (pwR K h 6)) (K.mul (cN K (4 * (m + 2))) X) := by lib_ring
  have l3 : K.mul (cN K (m + 1)) (K.mul (cN K (4 * (m + 2))) X) = K.mul (K.mul (cN K (4 * (m + 2))) (cN K (m + 1))) X := by lib_ring
  rw [l1]
  rw [l2] at s1; rw [l3] at s2
  exact O.le_trans _ _ _ s1 s2

theorem tN_nn (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (m : Nat) : O.le K.zero (tN K Q m) :=
  O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O Q hQ0 h1 _)

/-- the negative sum is at most twice its first term -/
theorem neg_le (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (h16 : O.le (pwR K Q 16) (pwR K h 6)) :
    ∀ n m, O.le (sumF K (tN K Q) m n) (K.add (tN K Q m) (tN K Q m))
  | 0, m => by
    show O.le K.zero _
    have := O.add_le_add _ _ _ _ (tN_nn K O h1 Q hQ0 m) (tN_nn K O h1 Q hQ0 m); rw [K.zero_add] at this; exact this
  | n + 1, m => by
    have ih := neg_le h1 h hh h0 Q hQ0 hQ1 h16 n (m + 1)
    have r := ratio K O h1 h hh h0 Q hQ0 hQ1 h16 m
    exact O.add_le_add _ _ _ _ (O.le_refl _) (O.le_trans _ _ _ ih r)

theorem tP_nn (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (m : Nat) : O.le K.zero (tP K Q m) :=
  O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O Q hQ0 h1 _)

theorem sum_nn (f : Nat → R) (hf : ∀ m, O.le K.zero (f m)) : ∀ n m, O.le K.zero (sumF K f m n)
  | 0, _ => O.le_refl _
  | n + 1, m => by
    have := O.add_le_add _ _ _ _ (hf m) (sum_nn f hf n (m + 1)); rw [K.zero_add] at this; exact this

/-- **small-Q part of G44**: Q ≤ 1, Q³ ≤ 1/4 (h = ½) ⇒ Σ_{m≤M} 4m(m+1)Q^{4m²+4m+1} ≤ Σ_{m≤M} 4m²Q^{4m²+2} -/
theorem small_q (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (hQ3 : O.le (pwR K Q 3) (pwR K h 2)) (M : Nat) :
    O.le (sumF K (tN K Q) 0 (M + 1)) (sumF K (tP K Q) 0 (M + 1)) := by
  have c4 := two_pow_h K h hh 2
  have hh1 : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  -- Q¹⁶ ≤ Q¹⁵ = (Q³)⁵ ≤ (h²)⁵ = h¹⁰ ≤ h⁶
  have h16 : O.le (pwR K Q 16) (pwR K h 6) := by
    have a := pw_anti K O h1 Q hQ0 hQ1 15 1
    have b := SU2Strong4D.pw_mono K O h1 _ _ (AreaLower.pw_nn K O Q hQ0 h1 3) hQ3 5
    rw [Migdal.pw_pw, Migdal.pw_pw] at b
    have c := pw_anti K O h1 h h0 hh1 6 4
    exact O.le_trans _ _ _ a (O.le_trans _ _ _ b c)
  have neg := neg_le K O h1 h hh h0 Q hQ0 hQ1 h16 (M + 1) 0
  have pos : O.le (tP K Q 0) (sumF K (tP K Q) 0 (M + 1)) := by
    have := O.add_le_add _ _ _ _ (O.le_refl (tP K Q 0)) (sum_nn K O _ (tP_nn K O h1 Q hQ0) M 1)
    rw [DetChain.add_zero'] at this; exact this
  -- 2·tN 0 = 16 Q⁹ ≤ 4 Q⁶ = tP 0
  have key : O.le (K.add (tN K Q 0) (tN K Q 0)) (tP K Q 0) := by
    unfold tN tP
    have e1 : K.add (K.mul (cN K (4 * ((0 + 1) * (0 + 2)))) (pwR K Q (4 * ((0 + 1) * (0 + 1)) + 4 * (0 + 1) + 1)))
        (K.mul (cN K (4 * ((0 + 1) * (0 + 2)))) (pwR K Q (4 * ((0 + 1) * (0 + 1)) + 4 * (0 + 1) + 1)))
        = K.mul (K.mul (cN K 4) (pwR K Q 6)) (K.mul (cN K (2 ^ 2)) (pwR K Q 3)) := by
      rw [show 4 * ((0 + 1) * (0 + 1)) + 4 * (0 + 1) + 1 = 6 + 3 from rfl, GKS.pwR_add,
        show 4 * ((0 + 1) * (0 + 2)) = 8 from rfl, show (2 : Nat) ^ 2 = 4 from rfl]
      have e : K.mul (cN K 4) (cN K 4) = K.add (cN K 8) (cN K 8) := by
        rw [← SU2CharExp.cN_mul, ← cN_add]
      rw [show K.mul (K.mul (cN K 4) (pwR K Q 6)) (K.mul (cN K 4) (pwR K Q 3))
          = K.mul (K.mul (cN K 4) (cN K 4)) (K.mul (pwR K Q 6) (pwR K Q 3)) by lib_ring, e]
      generalize cN K 8 = a; generalize K.mul (pwR K Q 6) (pwR K Q 3) = b
      lib_ring
    rw [e1, show 4 * ((0 + 1) * (0 + 1)) + 2 = 6 from rfl, show 4 * ((0 + 1) * (0 + 1)) = 4 from rfl]
    have nn := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 4) (AreaLower.pw_nn K O Q hQ0 h1 6)
    have s := O.mul_le_mul_right _ _ _ hQ3 (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 (2 ^ 2)) nn)
    rw [show K.mul (pwR K h 2) (K.mul (cN K (2 ^ 2)) (K.mul (cN K 4) (pwR K Q 6)))
        = K.mul (K.mul (cN K 4) (pwR K Q 6)) (K.mul (cN K (2 ^ 2)) (pwR K h 2)) by lib_ring, c4,
      show K.mul (pwR K Q 3) (K.mul (cN K (2 ^ 2)) (K.mul (cN K 4) (pwR K Q 6)))
        = K.mul (K.mul (cN K 4) (pwR K Q 6)) (K.mul (cN K (2 ^ 2)) (pwR K Q 3)) by lib_ring,
      K.mul_comm _ K.one, K.one_mul] at s
    exact s
  exact O.le_trans _ _ _ neg (O.le_trans _ _ _ key pos)

end SmallQ
#print axioms SmallQ.small_q
#print axioms SmallQ.two_pow_h
#print axioms SmallQ.ratio
#print axioms SmallQ.neg_le

ym/TripleH.lean

/-
  ym/TripleH — T45, G44 (error budget pieces): tail of the positive heat terms. If 2(n+2)² Q^{8n+12} ≤ (n+1)² then
  tP(m+1) ≤ tP(m)/2 for all m ≥ n, hence Σ_{k<r} tP(n+k) ≤ 2 tP(n). Lean 4 core only, no axioms.
-/
import ym.TripleE

namespace TripleH

open YMChain LinkIntegral SU2Char SmallQ QBinom

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

def CondP (Q : R) (m : Nat) : Prop :=
  O.le (K.mul (cN K (2 * ((m + 2) * (m + 2)))) (pwR K Q (8 * m + 12))) (cN K ((m + 1) * (m + 1)))

theorem ratioP (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (m : Nat) (hc : CondP K O Q m) :
    O.le (K.add (tP K Q (m + 1)) (tP K Q (m + 1))) (tP K Q m) := by
  unfold CondP at hc
  unfold tP
  rw [show 4 * ((m + 1 + 1) * (m + 1 + 1)) + 2 = (4 * ((m + 1) * (m + 1)) + 2) + (8 * m + 12) by lib_nat, GKS.pwR_add,
    show cN K (4 * ((m + 1 + 1) * (m + 1 + 1))) = K.mul (cN K 2) (cN K (2 * ((m + 2) * (m + 2)))) by rw [← SU2CharExp.cN_mul]; congr 1; lib_nat,
    show cN K (4 * ((m + 1) * (m + 1))) = K.mul (cN K 4) (cN K ((m + 1) * (m + 1))) by rw [← SU2CharExp.cN_mul]]
  have nX := AreaLower.pw_nn K O Q hQ0 h1 (4 * ((m + 1) * (m + 1)) + 2)
  have s := O.mul_le_mul_right _ _ _ hc (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 4) nX)
  have e4 : cN K 4 = K.mul (cN K 2) (cN K 2) := by rw [← SU2CharExp.cN_mul]
  have e2 : cN K 2 = K.add K.one K.one := by rw [show (2 : Nat) = 1 + 1 from rfl, cN_add, SU2CharExp.cN_one]
  rw [e4, e2] at s ⊢
  generalize pwR K Q (4 * ((m + 1) * (m + 1)) + 2) = X at s nX ⊢
  generalize pwR K Q (8 * m + 12) = Y at s ⊢
  generalize cN K (2 * ((m + 2) * (m + 2))) = A at s ⊢
  generalize cN K ((m + 1) * (m + 1)) = B at s ⊢
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · lib_ring

theorem condP_up (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (hQ1 : O.le Q K.one) (n : Nat) (H : CondP K O Q n) : ∀ j, CondP K O Q (n + j) := by
  intro j
  unfold CondP at H ⊢
  refine GridQ.cancel K O F (cN K ((n + 1) * (n + 1))) _ _ (SU2CharExp.cN_nn K O h1 _)
    (by rw [show (n + 1) * (n + 1) = n * n + 2 * n + 1 by lib_nat]; exact GridQ.cN_pos K O h1 h10 _) ?_
  have nat : (n + 1) * (n + 1) * (2 * ((n + j + 2) * (n + j + 2))) ≤ (n + j + 1) * (n + j + 1) * (2 * ((n + 2) * (n + 2))) :=
    Nat.le.intro (k := 2 * (2 * ((n + 1) * (n + j + 2)) * j + j * j)) (by lib_nat)
  have c1 := SU2CharExp.cN_mono K O h1 _ _ nat
  rw [SU2CharExp.cN_mul K ((n + 1) * (n + 1)), SU2CharExp.cN_mul K ((n + j + 1) * (n + j + 1))] at c1
  have qm : O.le (pwR K Q (8 * (n + j) + 12)) (pwR K Q (8 * n + 12)) := by
    rw [show 8 * (n + j) + 12 = (8 * n + 12) + 8 * j by lib_nat]; exact SmallQ.pw_anti K O h1 Q hQ0 hQ1 _ _
  have s1 := O.mul_le_mul_right _ _ _ c1 (AreaLower.pw_nn K O Q hQ0 h1 (8 * (n + j) + 12))
  have s2 := O.mul_le_mul_right _ _ _ qm (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 ((n + j + 1) * (n + j + 1))) (SU2CharExp.cN_nn K O h1 (2 * ((n + 2) * (n + 2)))))
  have s3 := O.mul_le_mul_right _ _ _ H (SU2CharExp.cN_nn K O h1 ((n + j + 1) * (n + j + 1)))
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
    (O.le_trans _ _ _ s2 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s3 (TripleE.le_of_eq' K O _ _ ?_))))))
  all_goals lib_ring

/-- **tail of the positive terms** -/
theorem tailP (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (hQ1 : O.le Q K.one) (n : Nat) (H : CondP K O Q n) :
    ∀ r j, O.le (sumN K (fun k => tP K Q (n + j + k)) r) (K.add (tP K Q (n + j)) (tP K Q (n + j)))
  | 0, j => by
    show O.le K.zero _
    have := O.add_le_add _ _ _ _ (tP_nn K O h1 Q hQ0 (n + j)) (tP_nn K O h1 Q hQ0 (n + j)); rw [K.zero_add] at this; exact this
  | r + 1, j => by
    rw [sumN_shift]
    have ih := tailP F h1 h10 Q hQ0 hQ1 n H r (j + 1)
    have rt := ratioP K O h1 Q hQ0 (n + j) (condP_up K O F h1 h10 Q hQ0 hQ1 n H j)
    rw [show n + (j + 1) = n + j + 1 by lib_nat] at ih
    rw [Nat.add_zero]
    have e : sumN K (fun k => tP K Q (n + j + (k + 1))) r = sumN K (fun k => tP K Q (n + j + 1 + k)) r :=
      sumN_ext K _ _ (fun k => by rw [show n + j + (k + 1) = n + j + 1 + k by lib_nat]) r
    rw [e]
    exact O.add_le_add _ _ _ _ (O.le_refl _) (O.le_trans _ _ _ ih rt)

theorem sg_le (h1 : O.le K.zero K.one) (q : R) (hq0 : O.le K.zero q) (hq1 : O.le q K.one) (u : Nat) :
    ∀ k, O.le (QBinomBd.sg K q u k) (K.mul (cN K k) (pwR K q u))
  | 0 => by show O.le K.zero (K.mul K.zero _); rw [K.mul_comm, DetChain.mul_zero]; exact O.le_refl _
  | k + 1 => by
    show O.le (K.add (QBinomBd.sg K q u k) (pwR K q (u + k + 1))) (K.mul (cN K (k + 1)) (pwR K q u))
    have a := SmallQ.pw_anti K O h1 q hq0 hq1 u (k + 1)
    rw [show u + (k + 1) = u + k + 1 by lib_nat] at a
    have := O.add_le_add _ _ _ _ (sg_le h1 q hq0 hq1 u k) a
    rw [show K.add (K.mul (cN K k) (pwR K q u)) (pwR K q u) = K.mul (cN K (k + 1)) (pwR K q u) by
      rw [cN_add, SU2CharExp.cN_one]; lib_ring] at this
    exact this

/-- one weight defect: G₀ − [2N, N+k] ≤ G₀·k·q^u (N = k+u) -/
theorem defect (h1 : O.le K.zero K.one) (q : R) (hq0 : O.le K.zero q) (hq1 : O.le q K.one) (k u : Nat) :
    O.le (K.add (G K q (2 * (k + u)) (k + u)) (K.neg (G K q (2 * (k + u)) (k + u + k))))
      (K.mul (G K q (2 * (k + u)) (k + u)) (K.mul (cN K k) (pwR K q u))) := by
  have lo := QBinomBd.central_lo K O h1 q hq0 hq1 k u
  have w := QBinomBd.weier K O h1 q hq0 hq1 u k
  have sg := sg_le K O h1 q hq0 hq1 u k
  have G0 := QBinomBd.G_nn K O h1 q hq0 (2 * (k + u)) (k + u)
  generalize G K q (2 * (k + u)) (k + u) = A at lo G0 ⊢
  generalize G K q (2 * (k + u)) (k + u + k) = B at lo ⊢
  generalize QBinom.pr K q u k = P at lo w
  generalize QBinomBd.sg K q u k = S at w sg
  -- A − B ≤ A − P A = A (1 − P) ≤ A S ≤ A·k q^u
  have s1 := O.add_le_add _ _ _ _ (O.le_refl (K.add A (K.neg (K.mul P A)))) lo
  rw [show K.add (K.add A (K.neg (K.mul P A))) (K.mul P A) = A by lib_ring] at s1
  have s2 : O.le (K.add A (K.neg B)) (K.add A (K.neg (K.mul P A))) := by
    have := O.add_le_add _ _ _ _ s1 (O.le_refl (K.neg B))
    rw [show K.add (K.add (K.add A (K.neg (K.mul P A))) B) (K.neg B) = K.add A (K.neg (K.mul P A)) by lib_ring] at this; exact this
  have s3 : O.le (K.add K.one (K.neg P)) S := by
    have := O.add_le_add _ _ _ _ w (O.le_refl (K.add S (K.neg P)))
    rw [show K.add (K.add K.one (K.neg S)) (K.add S (K.neg P)) = K.add K.one (K.neg P) by lib_ring,
      show K.add P (K.add S (K.neg P)) = S by lib_ring] at this; exact this
  have s4 := O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ s3 sg) G0
  rw [show K.mul (K.add K.one (K.neg P)) A = K.add A (K.neg (K.mul P A)) by lib_ring, K.mul_comm (K.mul (cN K k) (pwR K q u)) A] at s4
  exact O.le_trans _ _ _ s2 s4

/-- **weight error E1** ≤ G₀·q^{2r}·Σ_{m<n} (2m+3)·tN(m) -/
theorem E1_le (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (n r : Nat) :
    O.le (sumN K (fun m => K.mul (K.add (G K (K.mul Q Q) (2 * (2 * (n + r) + 1)) (2 * (n + r) + 1))
        (K.neg (G K (K.mul Q Q) (2 * (2 * (n + r) + 1)) (2 * (n + r) + 1 + (2 * m + 3))))) (tN K Q m)) n)
      (K.mul (K.mul (G K (K.mul Q Q) (2 * (2 * (n + r) + 1)) (2 * (n + r) + 1)) (pwR K (K.mul Q Q) (2 * r)))
        (sumN K (fun m => K.mul (cN K (2 * m + 3)) (tN K Q m)) n)) := by
  have hq0 : O.le K.zero (K.mul Q Q) := O.mul_nonneg _ _ hQ0 hQ0
  have hq1 : O.le (K.mul Q Q) K.one := by
    have := O.mul_le_mul_right _ _ _ hQ1 hQ0; rw [K.one_mul] at this; exact O.le_trans _ _ _ this hQ1
  rw [← sumN_mul]
  apply TripleE.sumN_mono_lt K O _ _ n
  intro m hm
  obtain ⟨d, hd⟩ := Nat.le.dest hm
  have e : 2 * (n + r) + 1 = (2 * m + 3) + (2 * d + 2 * r) := by rw [← hd]; lib_nat
  rw [e]
  have df := defect K O h1 (K.mul Q Q) hq0 hq1 (2 * m + 3) (2 * d + 2 * r)
  have qa : O.le (pwR K (K.mul Q Q) (2 * d + 2 * r)) (pwR K (K.mul Q Q) (2 * r)) := by
    rw [show 2 * d + 2 * r = 2 * r + 2 * d by lib_nat]; exact SmallQ.pw_anti K O h1 _ hq0 hq1 _ _
  have G0 := QBinomBd.G_nn K O h1 (K.mul Q Q) hq0 (2 * (2 * m + 3 + (2 * d + 2 * r))) (2 * m + 3 + (2 * d + 2 * r))
  have tn := SmallQ.tN_nn K O h1 Q hQ0 m
  have s1 := O.mul_le_mul_right _ _ _ df tn
  have s2 := O.mul_le_mul_right _ _ _ qa (O.mul_nonneg _ _ (O.mul_nonneg _ _ G0 (SU2CharExp.cN_nn K O h1 (2 * m + 3))) tn)
  refine O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ ?_)))
  · lib_ring
  · lib_ring

/-- **large-Q heat inequality from three explicit smallness conditions** -/
theorem big_q (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (hQ1 : O.le Q K.one) (hQ : Q ≠ K.zero) (hQne : Q ≠ K.one) (hqne : K.mul Q Q ≠ K.one) (n r : Nat)
    (hm : O.le K.zero (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one)))
    (hc : CondP K O Q n)
    (hA : O.le (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (TripleMom.Pp K (K.neg K.one) (TripleCoef.aQ K Q) (2 * (n + r) + 1))
        (K.add K.one (K.neg (K.mul (K.add K.one (K.mul Q Q)) (TripleMom.Lp K O F (K.neg K.one) (TripleCoef.aQ K Q) (2 * (n + r) + 1)))))))
      (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one)))
    (hB : O.le (K.mul (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.add K.one K.one) (K.add K.one K.one)))
        (K.mul (pwR K (K.mul Q Q) (2 * r)) (sumN K (fun m => K.mul (cN K (2 * m + 3)) (tN K Q m)) n)))
      (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one)))
    (hC : O.le (K.mul (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.add K.one K.one) (K.add K.one K.one)))
        (K.add (tP K Q n) (tP K Q n)))
      (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one))) :
    O.le (SmallQ.sumF K (tN K Q) 0 n) (SmallQ.sumF K (tP K Q) 0 n) := by
  have hq0 : O.le K.zero (K.mul Q Q) := O.mul_nonneg _ _ hQ0 hQ0
  have G0ge : O.le K.one (G K (K.mul Q Q) (2 * (2 * (n + r) + 1)) (2 * (n + r) + 1)) := by
    have := QBinomBd.G_ge_one K O h1 (K.mul Q Q) hq0 (2 * (n + r) + 1) (2 * (n + r) + 1)
    rw [show 2 * (2 * (n + r) + 1) = 2 * (n + r) + 1 + (2 * (n + r) + 1) by lib_nat]; exact this
  have e1 := E1_le K O h1 Q hQ0 hQ1 n r
  have e2 := tailP K O F h1 h10 Q hQ0 hQ1 n hc r 0
  rw [Nat.add_zero] at e2
  apply TripleE.assemble K O F h1 h10 Q hQ0 hQ1 hQ hQne hqne n r hm
  generalize G K (K.mul Q Q) (2 * (2 * (n + r) + 1)) (2 * (n + r) + 1) = G0 at G0ge e1 ⊢
  generalize sumN K (fun m => K.mul (K.add G0 (K.neg (G K (K.mul Q Q) (2 * (2 * (n + r) + 1)) (2 * (n + r) + 1 + (2 * m + 3))))) (tN K Q m)) n = E1 at e1 ⊢
  generalize sumN K (fun k => tP K Q (n + k)) r = E2 at e2 ⊢
  generalize K.mul (TripleMom.Pp K (K.neg K.one) (TripleCoef.aQ K Q) (2 * (n + r) + 1))
    (K.add K.one (K.neg (K.mul (K.add K.one (K.mul Q Q)) (TripleMom.Lp K O F (K.neg K.one) (TripleCoef.aQ K Q) (2 * (n + r) + 1))))) = PX at hA ⊢
  generalize K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one) = mg at hm hA hB hC ⊢
  rw [show K.mul (K.mul G0 (pwR K (K.mul Q Q) (2 * r))) (sumN K (fun m => K.mul (cN K (2 * m + 3)) (tN K Q m)) n)
      = K.mul G0 (K.mul (pwR K (K.mul Q Q) (2 * r)) (sumN K (fun m => K.mul (cN K (2 * m + 3)) (tN K Q m)) n)) by lib_ring] at e1
  generalize K.mul (pwR K (K.mul Q Q) (2 * r)) (sumN K (fun m => K.mul (cN K (2 * m + 3)) (tN K Q m)) n) = W at hB e1
  generalize K.add (tP K Q n) (tP K Q n) = T2 at hC e2
  have t0 : O.le K.zero (K.add K.one K.one) := by have := O.add_le_add _ _ _ _ h1 h1; rw [K.zero_add] at this; exact this
  have three0 : O.le K.zero (K.add (K.add K.one K.one) K.one) := by have := O.add_le_add _ _ _ _ t0 h1; rw [K.zero_add] at this; exact this
  have four0 : O.le K.zero (K.mul (K.add K.one K.one) (K.add K.one K.one)) := O.mul_nonneg _ _ t0 t0
  have three1 : O.le K.one (K.add (K.add K.one K.one) K.one) := by
    have := O.add_le_add _ _ _ _ (O.le_refl K.one) t0
    rw [DetChain.add_zero', show K.add K.one (K.add K.one K.one) = K.add (K.add K.one K.one) K.one by lib_ring] at this; exact this
  generalize K.mul (K.add K.one K.one) (K.add K.one K.one) = four at four0 hB hC ⊢
  have G00 : O.le K.zero G0 := O.le_trans _ _ _ h1 G0ge
  have d1 := AltSum.le_sub K O _ _ hA
  have d2 : O.le K.zero (K.add (K.mul G0 mg) (K.neg mg)) := by
    have := O.mul_le_mul_right _ _ _ G0ge hm; rw [K.one_mul] at this; exact AltSum.le_sub K O _ _ this
  have d3 : O.le K.zero (K.add (K.mul G0 mg) (K.neg (K.mul G0 (K.mul (K.mul (K.add (K.add K.one K.one) K.one) four) W)))) := by
    have := O.mul_le_mul_right _ _ _ hB G00; rw [K.mul_comm _ G0, K.mul_comm _ G0] at this; exact AltSum.le_sub K O _ _ this
  have d4 : O.le K.zero (K.add (K.mul G0 mg) (K.neg (K.mul G0 (K.mul (K.mul (K.add (K.add K.one K.one) K.one) four) T2)))) := by
    have := O.mul_le_mul_right _ _ _ hC G00; rw [K.mul_comm _ G0, K.mul_comm _ G0] at this; exact AltSum.le_sub K O _ _ this
  have d5 := O.mul_nonneg _ _ (O.mul_nonneg _ _ three0 four0) (AltSum.le_sub K O _ _ e1)
  have d6 := O.mul_nonneg _ _ (O.mul_nonneg _ _ (O.mul_nonneg _ _ three0 four0) G00) (AltSum.le_sub K O _ _ e2)
  have tot := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ d1 d2) d3) d4) d5) d6
  rw [K.zero_add, K.zero_add, K.zero_add, K.zero_add, K.zero_add] at tot
  have tne : K.add (K.add K.one K.one) K.one ≠ K.zero := fun h => h10 (O.le_antisymm _ _ (by rw [← h]; exact three1) h1)
  apply GridQ.cancel K O F (K.add (K.add K.one K.one) K.one) _ _ three0 tne
  have := O.add_le_add _ _ _ _ tot (O.le_refl (K.mul (K.add (K.add K.one K.one) K.one) (K.add (K.add PX (K.mul four E1)) (K.mul four (K.mul G0 E2)))))
  rw [K.zero_add] at this
  refine O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)
  lib_ring

/-- the Lambert margin is nonnegative: 2/(1+Q) − 1 ≥ 0 for 0 ≤ Q ≤ 1 -/
theorem margin_nn (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (hQ1 : O.le Q K.one) : O.le K.zero (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one)) := by
  have two0 := Lambert.one_pos K O h1 K.one h1
  have two_ne := Lambert.pos_ne K O h10 h1 K.one h1
  have le2 : O.le (K.add K.one Q) (K.add K.one K.one) := O.add_le_add _ _ _ _ (O.le_refl _) hQ1
  have ia := Lambert.inv_anti K O F _ _ (Lambert.one_pos K O h1 Q hQ0) (Lambert.pos_ne K O h10 h1 Q hQ0) two_ne le2
  have i2 := F.mul_inv _ two_ne
  have s := O.add_le_add _ _ _ _ ia ia
  rw [show K.add (F.inv (K.add K.one K.one)) (F.inv (K.add K.one K.one)) = K.mul (K.add K.one K.one) (F.inv (K.add K.one K.one)) by lib_ring, i2] at s
  exact AltSum.le_sub K O _ _ s

end TripleH

#print axioms TripleH.ratioP
#print axioms TripleH.condP_up
#print axioms TripleH.tailP
#print axioms TripleH.defect
#print axioms TripleH.E1_le
#print axioms TripleH.big_q
#print axioms TripleH.margin_nn

#print axioms: 13 theorems, no axioms.

90.78 Heat bond move for every \(Q\)

On every cell \(4K(1-Q)\le 1\le (4K+1)(1-Q)\), \(K\ge 15\), the heat bond-move inequality holds for truncations \(n\ge 4K\); with the certificates for \(Q\le 0.99\) this covers all \(0<Q<1\).

\[ \textstyle 4K\delta \le 1\le (4K+1)\delta ,\ n\ge 4K\ \Rightarrow \ \sum _{m<n}t_N(m)\le \sum _{m<n}t_P(m) \]

ym/BigQK.lean

/-
  ym/BigQK — T45, G44g (integer-parameter cells): for 1−Q = δ with 4K·δ ≤ 1 ≤ (4K+1)·δ, K ≥ 15, the hypotheses of
  Bern.big_q3 hold (J = K, x = ½, r = (4K+1)·2(n+1), any truncation n ≥ 4K). Part 1: margin identity, halving, x = ½.
  Lean 4 core only, no axioms.
-/
import ym.BigK

namespace BigQK

open YMChain LinkIntegral SU2Char TripleCoef

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- m′·(1+Q) = 1 − Q -/
theorem margin_eq (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q) :
    K.mul (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one)) (K.add K.one Q) = K.add K.one (K.neg Q) := by
  have i := F.mul_inv _ (Lambert.pos_ne K O h10 h1 Q hQ0)
  rw [show K.mul (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one)) (K.add K.one Q)
      = K.add (K.add (K.mul (K.add K.one Q) (F.inv (K.add K.one Q))) (K.mul (K.add K.one Q) (F.inv (K.add K.one Q)))) (K.neg (K.add K.one Q)) by lib_ring, i]
  lib_ring

/-- halving: if m·δ ≥ 1 then Q^m ≤ ½ -/
theorem halve (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q h : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (hh : K.add h h = K.one) (m : Nat) (hm : O.le K.one (K.mul (cN K m) (K.add K.one (K.neg Q)))) : O.le (pwR K Q m) h := by
  have d0 := QBinomBd.one_sub_nn K O _ hQ1
  have qd : O.le (K.mul Q (K.add K.one (K.add K.one (K.neg Q)))) K.one := by
    have := AltSum.sub_le K O K.one _ (QuatUnit.sq_nn K O (K.add K.one (K.neg Q)))
    rw [show K.add K.one (K.neg (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q)))) = K.mul Q (K.add K.one (K.add K.one (K.neg Q))) by lib_ring] at this
    exact this
  have b := Bern.bern K O h1 Q _ hQ0 d0 qd m
  have p0 := AreaLower.pw_nn K O Q hQ0 h1 m
  have s := O.mul_le_mul_right _ _ _ (O.add_le_add _ _ _ _ (O.le_refl K.one) hm) p0
  rw [K.mul_comm (K.add K.one K.one) (pwR K Q m), K.mul_comm (K.add K.one (K.mul (cN K m) (K.add K.one (K.neg Q)))) (pwR K Q m)] at s
  have two : O.le (K.mul (pwR K Q m) (K.add K.one K.one)) K.one := O.le_trans _ _ _ s b
  have t0 : O.le K.zero (K.add K.one K.one) := Lambert.one_pos K O h1 _ h1
  have tne : K.add K.one K.one ≠ K.zero := Lambert.pos_ne K O h10 h1 _ h1
  apply GridQ.cancel K O F (K.add K.one K.one) _ _ t0 tne
  rw [show K.mul (K.add K.one K.one) h = K.add h h by lib_ring, hh, K.mul_comm]; exact two

/-- x = ½ works for J = K: ½ ≤ Q^{2k+1} for k < K when 4K·δ ≤ 1 -/
theorem half_le_a (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q h : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (hh : K.add h h = K.one) (Kk : Nat) (hK1 : O.le (K.mul (cN K (4 * Kk)) (K.add K.one (K.neg Q))) K.one) :
    ∀ k, k < Kk → O.le h (aQ K Q k) := by
  intro k hk
  have bl := Bern.bern_lo K O h1 Q hQ0 hQ1 (2 * Kk)
  obtain ⟨d, hd⟩ := Nat.le.dest hk
  have pa : O.le (pwR K Q (2 * Kk)) (aQ K Q k) := by
    unfold aQ; rw [show 2 * Kk = (2 * k + 1) + (2 * d + 1) by rw [← hd]; lib_nat]; exact SmallQ.pw_anti K O h1 Q hQ0 hQ1 _ _
  refine O.le_trans _ _ _ ?_ (O.le_trans _ _ _ bl pa)
  -- h ≤ 1 − 2K δ  ⇐  2·(2Kδ) ≤ 1 = 2h
  have t0 : O.le K.zero (K.add K.one K.one) := Lambert.one_pos K O h1 _ h1
  have tne : K.add K.one K.one ≠ K.zero := Lambert.pos_ne K O h10 h1 _ h1
  have c4 : cN K (4 * Kk) = K.mul (K.add K.one K.one) (cN K (2 * Kk)) := by
    rw [show 4 * Kk = 2 * (2 * Kk) by lib_nat, SU2CharExp.cN_mul, show (2 : Nat) = 1 + 1 from rfl, cN_add, SU2CharExp.cN_one]
  rw [c4] at hK1
  have hx : O.le (K.mul (cN K (2 * Kk)) (K.add K.one (K.neg Q))) h := by
    apply GridQ.cancel K O F (K.add K.one K.one) _ _ t0 tne
    rw [show K.mul (K.add K.one K.one) h = K.add h h by lib_ring, hh, show K.mul (K.add K.one K.one) (K.mul (cN K (2 * Kk)) (K.add K.one (K.neg Q)))
      = K.mul (K.mul (K.add K.one K.one) (cN K (2 * Kk))) (K.add K.one (K.neg Q)) by lib_ring]; exact hK1
  have := O.add_le_add _ _ _ _ hx (O.le_refl (K.add h (K.neg (K.mul (cN K (2 * Kk)) (K.add K.one (K.neg Q))))))
  rw [show K.add (K.mul (cN K (2 * Kk)) (K.add K.one (K.neg Q))) (K.add h (K.neg (K.mul (cN K (2 * Kk)) (K.add K.one (K.neg Q))))) = h by lib_ring,
    show K.add h (K.add h (K.neg (K.mul (cN K (2 * Kk)) (K.add K.one (K.neg Q))))) = K.add (K.add h h) (K.neg (K.mul (cN K (2 * Kk)) (K.add K.one (K.neg Q)))) by lib_ring, hh] at this
  exact this

theorem pow4 : ∀ k, 4 ^ k = 2 ^ (k + k)
  | 0 => rfl
  | k + 1 => by
    rw [show k + 1 + (k + 1) = (k + k) + 2 by lib_nat]
    show 4 ^ k * 4 = 2 ^ (k + k) * 2 * 2
    rw [pow4 k]; generalize 2 ^ (k + k) = x; lib_nat

/-- **θ₄ hypothesis of big_q3 on the cell K** -/
theorem hP_K (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q h : R) (hQ0 : O.le K.zero Q)
    (hQ1 : O.le Q K.one) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (b : Nat)
    (hK2 : O.le K.one (K.mul (cN K (4 * (b + 15) + 1)) (K.add K.one (K.neg Q)))) :
    O.le (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (pwR K (K.add K.one (K.neg h)) ((b + 15) + (b + 15)))
        (K.add (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.add K.one (K.neg (K.mul Q Q))))
          (K.mul (K.add K.one K.one) (K.add K.one K.one)))))
      (K.mul (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one))
        (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.add K.one (K.neg (K.mul Q Q))))) := by
  have e1h : K.add K.one (K.neg h) = h := by rw [← hh]; lib_ring
  rw [e1h]
  -- RHS = δ⁴
  have me := margin_eq K O F h1 h10 Q hQ0
  have rhs : K.mul (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one))
      (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.add K.one (K.neg (K.mul Q Q))))
      = K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) := by
    rw [show K.add K.one (K.neg (K.mul Q Q)) = K.mul (K.add K.one (K.neg Q)) (K.add K.one Q) by lib_ring,
      show K.mul (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one))
        (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.mul (K.add K.one (K.neg Q)) (K.add K.one Q)))
        = K.mul (K.mul (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one)) (K.add K.one Q))
          (K.mul (K.mul (K.add K.one (K.neg Q)) (K.add K.one (K.neg Q))) (K.add K.one (K.neg Q))) by lib_ring, me]
    lib_ring
  rw [rhs]
  have d0 := QBinomBd.one_sub_nn K O _ hQ1
  have d1 := QBinomBd.one_sub_le K O _ hQ0
  have q2 : O.le K.zero (K.mul Q Q) := O.mul_nonneg _ _ hQ0 hQ0
  have E1 := QBinomBd.one_sub_le K O _ q2
  have E0 : O.le K.zero (K.add K.one (K.neg (K.mul Q Q))) := by
    rw [show K.add K.one (K.neg (K.mul Q Q)) = K.mul (K.add K.one (K.neg Q)) (K.add K.one Q) by lib_ring]
    exact O.mul_nonneg _ _ d0 (Lambert.one_pos K O h1 Q hQ0)
  generalize K.add K.one (K.neg (K.mul Q Q)) = E at E0 E1 ⊢
  generalize K.add K.one (K.neg Q) = d at d0 d1 hK2 ⊢
  -- constants as casts
  have c3 : K.add (K.add K.one K.one) K.one = cN K 3 := by show _ = K.add K.one (K.add K.one (K.add K.one K.zero)); lib_ring
  have c4 : K.mul (K.add K.one K.one) (K.add K.one K.one) = cN K 4 := by
    show _ = K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero))); lib_ring
  rw [c3, c4]
  -- H = h^{2K}, cN(4^K)·H = 1
  have tp := SmallQ.two_pow_h K h hh ((b + 15) + (b + 15))
  have H0 := AreaLower.pw_nn K O h h0 h1 ((b + 15) + (b + 15))
  generalize pwR K h ((b + 15) + (b + 15)) = H at tp H0 ⊢
  -- (4K+1)^4 δ^4 ≥ 1
  have hm4 := SU2Strong4D.pw_mono K O h1 K.one _ h1 hK2 4
  rw [SU2Strong4D.pw_one, HeatDec.pw_mul_pw, GridCell.cN_pow] at hm4
  have nat := BigK.n1 b
  have cn := SU2CharExp.cN_mono K O h1 _ _ nat
  rw [SU2CharExp.cN_mul] at cn
  have X4 : (4 * (b + 15) + 1) ^ 4 = (4 * (b + 15) + 1) * (4 * (b + 15) + 1) * (4 * (b + 15) + 1) * (4 * (b + 15) + 1) := by
    show 1 * _ * _ * _ * _ = _; rw [Nat.one_mul]
  rw [X4] at hm4
  generalize cN K ((4 * (b + 15) + 1) * (4 * (b + 15) + 1) * (4 * (b + 15) + 1) * (4 * (b + 15) + 1)) = X at hm4 cn
  have dd0 : O.le K.zero (K.mul d d) := O.mul_nonneg _ _ d0 d0
  have dd1 : O.le (K.mul d d) K.one := by have := O.mul_le_mul_right _ _ _ d1 d0; rw [K.one_mul] at this; exact O.le_trans _ _ _ this d1
  have DE1 : O.le (K.mul (K.mul d d) E) K.one := by have := O.mul_le_mul_right _ _ _ dd1 E0; rw [K.one_mul] at this; exact O.le_trans _ _ _ this E1
  have d4 : pwR K d 4 = K.mul (K.mul d d) (K.mul d d) := by show K.mul (K.mul (K.mul (K.mul K.one d) d) d) d = _; lib_ring
  rw [d4] at hm4
  -- chain
  have s1 : O.le (K.mul (cN K 3) (K.mul H (K.add (K.mul (K.mul d d) E) (cN K 4)))) (K.mul (cN K 30) H) := by
    have a := O.add_le_add _ _ _ _ DE1 (O.le_refl (cN K 4))
    have b5 := O.mul_le_mul_right _ _ _ a (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 3) H0)
    have c15 : O.le (K.mul (K.add K.one (cN K 4)) (K.mul (cN K 3) H)) (K.mul (cN K 30) H) := by
      rw [show K.add K.one (cN K 4) = cN K 5 by rw [show (5 : Nat) = 1 + 4 from rfl, cN_add, SU2CharExp.cN_one],
        show K.mul (cN K 5) (K.mul (cN K 3) H) = K.mul (K.mul (cN K 5) (cN K 3)) H by lib_ring, ← SU2CharExp.cN_mul]
      exact O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 15 30 (by decide)) H0
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ b5 c15)
    lib_ring
  have s2 := O.mul_le_mul_right _ _ _ hm4 (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 30) H0)
  rw [K.one_mul] at s2
  have s3 := O.mul_le_mul_right _ _ _ cn (O.mul_nonneg _ _ H0 (O.mul_nonneg _ _ dd0 dd0))
  refine O.le_trans _ _ _ s1 (O.le_trans _ _ _ s2 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s3 (TripleE.le_of_eq' K O _ _ ?_))))
  · lib_ring
  · rw [show K.mul (cN K (4 ^ (b + 15))) (K.mul H (K.mul (K.mul d d) (K.mul d d))) = K.mul (K.mul (cN K (2 ^ ((b + 15) + (b + 15)))) H) (K.mul (K.mul d d) (K.mul d d)) by rw [pow4]; lib_ring, tp]
    lib_ring

/-- Q^M ≤ ½ for M ≥ 4K+1 -/
theorem qM (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q h : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (hh : K.add h h = K.one) (Kk M : Nat) (hM : 4 * Kk + 1 ≤ M)
    (hK2 : O.le K.one (K.mul (cN K (4 * Kk + 1)) (K.add K.one (K.neg Q)))) : O.le (pwR K Q M) h := by
  obtain ⟨e, he⟩ := Nat.le.dest hM
  rw [← he]
  exact O.le_trans _ _ _ (SmallQ.pw_anti K O h1 Q hQ0 hQ1 _ _) (halve K O F h1 h10 Q h hQ0 hQ1 hh _ hK2)

/-- **CondP on the cell** (n+1 ≥ 4K+1) -/
theorem condP_K (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q h : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (hh : K.add h h = K.one) (h0 : O.le K.zero h) (Kk n : Nat) (hM : 4 * Kk + 1 ≤ n + 1)
    (hK2 : O.le K.one (K.mul (cN K (4 * Kk + 1)) (K.add K.one (K.neg Q)))) : TripleH.CondP K O Q n := by
  unfold TripleH.CondP
  have q := qM K O F h1 h10 Q h hQ0 hQ1 hh Kk (n + 1) hM hK2
  -- Q^{8n+12} ≤ Q^{8(n+1)} = (Q^{n+1})^8 ≤ h^8
  have a1 : O.le (pwR K Q (8 * n + 12)) (pwR K Q ((n + 1) * 8)) := by
    rw [show 8 * n + 12 = (n + 1) * 8 + 4 by lib_nat]; exact SmallQ.pw_anti K O h1 Q hQ0 hQ1 _ _
  rw [← Migdal.pw_pw] at a1
  have a2 := SU2Strong4D.pw_mono K O h1 _ _ (AreaLower.pw_nn K O Q hQ0 h1 _) q 8
  have a := O.le_trans _ _ _ a1 a2
  have tp := SmallQ.two_pow_h K h hh 8
  have nat : 2 * ((n + 2) * (n + 2)) ≤ 2 ^ 8 * ((n + 1) * (n + 1)) := by
    have e1 : 2 * ((n + 2) * (n + 2)) ≤ 8 * ((n + 1) * (n + 1)) := Nat.le.intro (k := 6 * (n * n) + 8 * n) (by lib_nat)
    exact Nat.le_trans e1 (Nat.mul_le_mul_right _ (by decide))
  have c := SU2CharExp.cN_mono K O h1 _ _ nat
  rw [SU2CharExp.cN_mul K (2 ^ 8)] at c
  have s1 := O.mul_le_mul_right _ _ _ a (SU2CharExp.cN_nn K O h1 (2 * ((n + 2) * (n + 2))))
  have s2 := O.mul_le_mul_right _ _ _ c (AreaLower.pw_nn K O h h0 h1 8)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
    (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ ?_))))
  · lib_ring
  · lib_ring
  · rw [show K.mul (K.mul (cN K (2 ^ 8)) (cN K ((n + 1) * (n + 1)))) (pwR K h 8)
        = K.mul (cN K ((n + 1) * (n + 1))) (K.mul (cN K (2 ^ 8)) (pwR K h 8)) by lib_ring, tp]; lib_ring

theorem pow16 : ∀ k, 16 ^ k = 2 ^ (4 * k)
  | 0 => rfl
  | k + 1 => by
    rw [show 4 * (k + 1) = 4 * k + 4 by lib_nat]
    show 16 ^ k * 16 = 2 ^ (4 * k) * 2 * 2 * 2 * 2
    rw [pow16 k]; generalize 2 ^ (4 * k) = x; lib_nat

theorem c12 : K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.add K.one K.one) (K.add K.one K.one)) = cN K 12 := by
  rw [show (12 : Nat) = 3 * 4 from rfl, SU2CharExp.cN_mul]
  show _ = K.mul (K.add K.one (K.add K.one (K.add K.one K.zero))) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero))))
  lib_ring

/-- **tail hypothesis of big_q3 on the cell** (M = n+1 ≥ 4K+1) -/
theorem hC_K (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q h : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (hh : K.add h h = K.one) (h0 : O.le K.zero h) (Kk a : Nat) (hM : 4 * Kk + 1 ≤ a + 4)
    (hK2 : O.le K.one (K.mul (cN K (4 * Kk + 1)) (K.add K.one (K.neg Q)))) :
    O.le (K.mul (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.add K.one K.one) (K.add K.one K.one)))
        (K.add (SmallQ.tP K Q (a + 3)) (SmallQ.tP K Q (a + 3))))
      (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one)) := by
  rw [c12]
  have q := qM K O F h1 h10 Q h hQ0 hQ1 hh Kk (a + 4) hM hK2
  -- tP(a+3) ≤ cN(4M²)·h^{4M}
  have tq : O.le (SmallQ.tP K Q (a + 3)) (K.mul (cN K (4 * ((a + 4) * (a + 4)))) (pwR K h (4 * (a + 4)))) := by
    unfold SmallQ.tP
    have e1 : O.le (pwR K Q (4 * ((a + 3 + 1) * (a + 3 + 1)) + 2)) (pwR K Q ((a + 4) * (4 * (a + 4)))) := by
      rw [show 4 * ((a + 3 + 1) * (a + 3 + 1)) + 2 = (a + 4) * (4 * (a + 4)) + 2 by lib_nat]; exact SmallQ.pw_anti K O h1 Q hQ0 hQ1 _ _
    rw [← Migdal.pw_pw] at e1
    have e2 := SU2Strong4D.pw_mono K O h1 _ _ (AreaLower.pw_nn K O Q hQ0 h1 _) q (4 * (a + 4))
    have := O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ e1 e2) (SU2CharExp.cN_nn K O h1 (4 * ((a + 3 + 1) * (a + 3 + 1))))
    rw [K.mul_comm, K.mul_comm (pwR K h _)] at this
    rw [show a + 3 + 1 = a + 4 from rfl] at this; exact this
  -- the exponential: cN(16^M)·h^{4M} = 1 and 384 M³ ≤ 16^M
  have tp := SmallQ.two_pow_h K h hh (4 * (a + 4))
  rw [← pow16] at tp
  have nat := BigK.n4 a
  have cn := SU2CharExp.cN_mono K O h1 _ _ nat
  have H0 := AreaLower.pw_nn K O h h0 h1 (4 * (a + 4))
  generalize pwR K h (4 * (a + 4)) = H at tq tp H0
  -- margin
  have me := margin_eq K O F h1 h10 Q hQ0
  have Mδ : O.le K.one (K.mul (cN K (a + 4)) (K.add K.one (K.neg Q))) := by
    have := O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 _ _ hM) (QBinomBd.one_sub_nn K O _ hQ1)
    exact O.le_trans _ _ _ hK2 this
  have oq0 := Lambert.one_pos K O h1 Q hQ0
  have oqne := Lambert.pos_ne K O h10 h1 Q hQ0
  have M0 := SU2CharExp.cN_nn K O h1 (a + 4)
  have Mne : cN K (a + 4) ≠ K.zero := by
    rw [show a + 4 = (a + 3) + 1 from rfl]; exact GridQ.cN_pos K O h1 h10 _
  apply GridQ.cancel K O F (K.mul (K.add K.one Q) (cN K (a + 4))) _ _ (O.mul_nonneg _ _ oq0 M0)
    (fun e => Mne (O.pos_mul_eq_zero _ _ oq0 oqne e))
  -- RHS ≥ 1
  have rhs : K.mul (K.mul (K.add K.one Q) (cN K (a + 4))) (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one))
      = K.mul (cN K (a + 4)) (K.add K.one (K.neg Q)) := by
    rw [← me]; lib_ring
  rw [rhs]
  refine O.le_trans _ _ _ ?_ Mδ
  -- LHS ≤ 2·M·12·2·4M²·H = 192 M³ H ≤ 384 M³ H ≤ 16^M H = 1
  have tP0 := SmallQ.tP_nn K O h1 Q hQ0 (a + 3)
  have oq2 : O.le (K.add K.one Q) (K.add K.one K.one) := O.add_le_add _ _ _ _ (O.le_refl _) hQ1
  generalize SmallQ.tP K Q (a + 3) = T at tq tP0
  have s1 := O.mul_le_mul_right _ _ _ oq2 (O.mul_nonneg _ _ M0 (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 12)
    (by have := O.add_le_add _ _ _ _ tP0 tP0; rw [K.zero_add] at this; exact this)))
  have s2 := O.mul_le_mul_right _ _ _ tq (O.mul_nonneg _ _ (O.mul_nonneg _ _ (Lambert.one_pos K O h1 _ h1) M0) (SU2CharExp.cN_nn K O h1 24))
  have s3 : O.le (K.mul (cN K 192) (K.mul (cN K ((a + 4) * ((a + 4) * (a + 4)))) H)) (K.mul (cN K (384 * ((a + 4) * (a + 4) * (a + 4)))) H) := by
    rw [SU2CharExp.cN_mul K 384, show K.mul (cN K 192) (K.mul (cN K ((a + 4) * ((a + 4) * (a + 4)))) H) = K.mul (K.mul (cN K 192) (cN K ((a + 4) * ((a + 4) * (a + 4))))) H by lib_ring,
      show (a + 4) * ((a + 4) * (a + 4)) = (a + 4) * (a + 4) * (a + 4) by lib_nat]
    exact O.mul_le_mul_right _ _ _ (O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 192 384 (by decide)) (SU2CharExp.cN_nn K O h1 _)) H0
  have s4 := O.mul_le_mul_right _ _ _ cn H0
  rw [tp] at s4
  have c2 : K.add K.one K.one = cN K 2 := by show _ = K.add K.one (K.add K.one K.zero); lib_ring
  have eqA : K.mul (K.mul (K.add K.one Q) (cN K (a + 4))) (K.mul (cN K 12) (K.add T T))
      = K.mul (K.add K.one Q) (K.mul (cN K (a + 4)) (K.mul (cN K 12) (K.add T T))) := by lib_ring
  have eqB : K.mul (K.add K.one K.one) (K.mul (cN K (a + 4)) (K.mul (cN K 12) (K.add T T)))
      = K.mul T (K.mul (K.mul (K.add K.one K.one) (cN K (a + 4))) (cN K 24)) := by
    rw [show (24 : Nat) = 12 + 12 from rfl, cN_add K 12 12]; lib_ring
  have eqC : K.mul (K.mul (cN K (4 * ((a + 4) * (a + 4)))) H) (K.mul (K.mul (K.add K.one K.one) (cN K (a + 4))) (cN K 24))
      = K.mul (cN K 192) (K.mul (cN K ((a + 4) * ((a + 4) * (a + 4)))) H) := by
    rw [c2, SU2CharExp.cN_mul K 4, SU2CharExp.cN_mul K (a + 4) ((a + 4) * (a + 4)), SU2CharExp.cN_mul K (a + 4) (a + 4),
      show (192 : Nat) = 2 * 24 * 4 from rfl, SU2CharExp.cN_mul K (2 * 24), SU2CharExp.cN_mul K 2]
    lib_ring
  rw [eqA]
  refine O.le_trans _ _ _ s1 ?_
  rw [eqB]
  refine O.le_trans _ _ _ s2 ?_
  rw [eqC]
  exact O.le_trans _ _ _ s3 s4

theorem big_aux (M P : Nat) (hM : 1 ≤ M) (h : 384 * (M * M * M) ≤ P) : 192 * (M * (M * M * M * M)) ≤ P * P := by
  have e : M * (M * M * M * M) = M * M * M * (M * M) := by lib_nat
  rw [e]
  have m2 : M * M ≤ 384 * (M * M * M) := by
    have : M * M * 1 ≤ M * M * M := Nat.mul_le_mul_left _ hM
    rw [Nat.mul_one] at this
    exact Nat.le_trans this (Nat.le_mul_of_pos_left _ (by decide))
  have p2 : M * M ≤ P := Nat.le_trans m2 h
  calc 192 * (M * M * M * (M * M)) ≤ 384 * (M * M * M * (M * M)) := Nat.mul_le_mul_right _ (by decide)
    _ = 384 * (M * M * M) * (M * M) := BigK.massoc _ _ _
    _ ≤ P * (M * M) := Nat.mul_le_mul_right _ h
    _ ≤ P * P := Nat.mul_le_mul_left _ p2

theorem poly_aux (n : Nat) : n * ((2 * n + 1) * (4 * (n * (n + 1)))) ≤ 8 * ((n + 1) * (n + 1) * (n + 1) * (n + 1)) := by
  have l1 : n ≤ n + 1 := Nat.le_succ _
  have l2 : 2 * n + 1 ≤ 2 * (n + 1) := Nat.le.intro (k := 1) (by lib_nat)
  have := Nat.mul_le_mul l1 (Nat.mul_le_mul l2 (Nat.mul_le_mul_left 4 (Nat.mul_le_mul l1 (Nat.le_refl (n + 1)))))
  refine Nat.le_trans this (Nat.le_of_eq ?_)
  generalize n + 1 = M; lib_nat

theorem pow_dbl (c : Nat) : ∀ m, c ^ (m + m) = c ^ m * c ^ m
  | 0 => rfl
  | m + 1 => by
    rw [show m + 1 + (m + 1) = (m + m) + 1 + 1 by lib_nat]
    show c ^ (m + m) * c * c = c ^ m * c * (c ^ m * c)
    rw [pow_dbl c m]; generalize c ^ m = x; lib_nat

/-- **weight hypothesis of big_q3 on the cell** (n = a+3, r = (4K+1)·2(a+4)) -/
theorem hB_K (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q h : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (hh : K.add h h = K.one) (h0 : O.le K.zero h) (Kk a : Nat) (hM : 4 * Kk + 1 ≤ a + 4)
    (hK2 : O.le K.one (K.mul (cN K (4 * Kk + 1)) (K.add K.one (K.neg Q)))) :
    O.le (K.mul (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.add K.one K.one) (K.add K.one K.one)))
        (K.mul (pwR K (K.mul Q Q) (2 * ((4 * Kk + 1) * (2 * (a + 4))))) (cN K ((a + 3) * ((2 * (a + 3) + 1) * (4 * ((a + 3) * (a + 3 + 1))))))))
      (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one)) := by
  rw [c12, TripleCoef.pw2]
  -- Q^{4r} ≤ h^{8M}
  have q := halve K O F h1 h10 Q h hQ0 hQ1 hh _ hK2
  have e1 : pwR K Q (2 * ((4 * Kk + 1) * (2 * (a + 4))) + 2 * ((4 * Kk + 1) * (2 * (a + 4))))
      = pwR K (pwR K Q (4 * Kk + 1)) (8 * (a + 4)) := by
    rw [Migdal.pw_pw]; congr 1; lib_nat
  rw [e1]
  have qh := SU2Strong4D.pw_mono K O h1 _ _ (AreaLower.pw_nn K O Q hQ0 h1 _) q (8 * (a + 4))
  -- polynomial ≤ 8 M⁴
  have poly := poly_aux (a + 3)
  have cp := SU2CharExp.cN_mono K O h1 _ _ poly
  -- exponential: cN(16^{2M}) h^{8M} = 1, 192 M⁵ ≤ 16^{2M}
  have tp := SmallQ.two_pow_h K h hh (4 * ((a + 4) + (a + 4)))
  rw [← pow16, pow_dbl, show 4 * ((a + 4) + (a + 4)) = 8 * (a + 4) by lib_nat] at tp
  have n4 := BigK.n4 a
  have big : 192 * ((a + 4) * ((a + 4) * (a + 4) * (a + 4) * (a + 4))) ≤ 16 ^ (a + 4) * 16 ^ (a + 4) := by
    exact big_aux (a + 4) (16 ^ (a + 4)) (Nat.le.intro (k := a + 3) (by lib_nat)) n4
  rw [show a + 3 + 1 = a + 4 from rfl] at cp
  have cb := SU2CharExp.cN_mono K O h1 _ _ big
  have H0 := AreaLower.pw_nn K O h h0 h1 (8 * (a + 4))
  have X0 := AreaLower.pw_nn K O (pwR K Q (4 * Kk + 1)) (AreaLower.pw_nn K O Q hQ0 h1 _) h1 (8 * (a + 4))
  generalize pwR K h (8 * (a + 4)) = H at qh tp H0
  generalize pwR K (pwR K Q (4 * Kk + 1)) (8 * (a + 4)) = X at qh X0 ⊢
  have Pn0 := SU2CharExp.cN_nn K O h1 ((a + 3) * ((2 * (a + 3) + 1) * (4 * ((a + 3) * (a + 3 + 1)))))
  generalize cN K ((a + 3) * ((2 * (a + 3) + 1) * (4 * ((a + 3) * (a + 3 + 1))))) = Pn at cp Pn0 ⊢
  -- margin side
  have me := margin_eq K O F h1 h10 Q hQ0
  have Mδ : O.le K.one (K.mul (cN K (a + 4)) (K.add K.one (K.neg Q))) := by
    have := O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 _ _ hM) (QBinomBd.one_sub_nn K O _ hQ1)
    exact O.le_trans _ _ _ hK2 this
  have oq0 := Lambert.one_pos K O h1 Q hQ0
  have oqne := Lambert.pos_ne K O h10 h1 Q hQ0
  have M0 := SU2CharExp.cN_nn K O h1 (a + 4)
  have Mne : cN K (a + 4) ≠ K.zero := by rw [show a + 4 = (a + 3) + 1 from rfl]; exact GridQ.cN_pos K O h1 h10 _
  apply GridQ.cancel K O F (K.mul (K.add K.one Q) (cN K (a + 4))) _ _ (O.mul_nonneg _ _ oq0 M0)
    (fun e => Mne (O.pos_mul_eq_zero _ _ oq0 oqne e))
  rw [show K.mul (K.mul (K.add K.one Q) (cN K (a + 4))) (K.add (K.add (F.inv (K.add K.one Q)) (F.inv (K.add K.one Q))) (K.neg K.one))
      = K.mul (cN K (a + 4)) (K.add K.one (K.neg Q)) by rw [← me]; lib_ring]
  refine O.le_trans _ _ _ ?_ Mδ
  have oq2 : O.le (K.add K.one Q) (K.add K.one K.one) := O.add_le_add _ _ _ _ (O.le_refl _) hQ1
  have c120 := SU2CharExp.cN_nn K O h1 12
  -- (1+Q) M 12 X Pn ≤ 2 M 12 H Pn ≤ 2 M 12 H cN(8M⁴) = cN(192 M⁵) H ≤ cN(16^M 16^M) H = 1
  have s1 := O.mul_le_mul_right _ _ _ oq2 (O.mul_nonneg _ _ M0 (O.mul_nonneg _ _ c120 (O.mul_nonneg _ _ X0 Pn0)))
  have s2 := O.mul_le_mul_right _ _ _ qh (O.mul_nonneg _ _ (O.mul_nonneg _ _ (Lambert.one_pos K O h1 _ h1) M0) (O.mul_nonneg _ _ c120 Pn0))
  have s3 := O.mul_le_mul_right _ _ _ cp (O.mul_nonneg _ _ (O.mul_nonneg _ _ (Lambert.one_pos K O h1 _ h1) M0) (O.mul_nonneg _ _ c120 H0))
  have s4 := O.mul_le_mul_right _ _ _ cb H0
  rw [tp] at s4
  have c2 : K.add K.one K.one = cN K 2 := by show _ = K.add K.one (K.add K.one K.zero); lib_ring
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
    (O.le_trans _ _ _ s2 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s3 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) s4))))))
  · lib_ring
  · lib_ring
  · lib_ring
  · rw [c2, SU2CharExp.cN_mul K 192, SU2CharExp.cN_mul K 8, SU2CharExp.cN_mul K (a + 4) ((a + 4) * (a + 4) * (a + 4) * (a + 4)),
      show (192 : Nat) = 2 * 12 * 8 from rfl, SU2CharExp.cN_mul K (2 * 12), SU2CharExp.cN_mul K 2]
    lib_ring

/-- **G44, large-Q side on every cell K ≥ 15**: if 4K·(1−Q) ≤ 1 ≤ (4K+1)·(1−Q) and the truncation n satisfies
    n + 1 ≥ 4K + 1, then one heat bond move gives s_eff ≥ s/2: Σ_{m<n} tN(m) ≤ Σ_{m<n} tP(m). -/
theorem big_K (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (hQ1 : O.le Q K.one) (hQ : Q ≠ K.zero) (hQne : Q ≠ K.one) (hqne : K.mul Q Q ≠ K.one) (b a : Nat)
    (hM : 4 * (b + 15) + 1 ≤ a + 4)
    (hK1 : O.le (K.mul (cN K (4 * (b + 15))) (K.add K.one (K.neg Q))) K.one)
    (hK2 : O.le K.one (K.mul (cN K (4 * (b + 15) + 1)) (K.add K.one (K.neg Q)))) :
    O.le (SmallQ.sumF K (SmallQ.tN K Q) 0 (a + 3)) (SmallQ.sumF K (SmallQ.tP K Q) 0 (a + 3)) := by
  have two0 : O.le K.zero (K.add K.one K.one) := Lambert.one_pos K O h1 _ h1
  have twone : K.add K.one K.one ≠ K.zero := Lambert.pos_ne K O h10 h1 _ h1
  have hh : K.add (F.inv (K.add K.one K.one)) (F.inv (K.add K.one K.one)) = K.one := by
    rw [show K.add (F.inv (K.add K.one K.one)) (F.inv (K.add K.one K.one)) = K.mul (K.add K.one K.one) (F.inv (K.add K.one K.one)) by lib_ring]
    exact F.mul_inv _ twone
  have h0 := F.inv_pos _ two0 twone
  generalize F.inv (K.add K.one K.one) = h at hh h0
  have l1 : ∀ x : Nat, x ≤ 4 * x + 1 := fun x => Nat.le.intro (k := 3 * x + 1) (by lib_nat)
  have l2 : ∀ a R : Nat, a + 4 ≤ 2 * (a + 3 + R) + 1 := fun a R => Nat.le.intro (k := a + 2 * R + 3) (by lib_nat)
  have hJ : b + 15 ≤ 2 * (a + 3 + (4 * (b + 15) + 1) * (2 * (a + 4))) + 1 :=
    Nat.le_trans (Nat.le_trans (l1 (b + 15)) hM) (l2 a _)
  obtain ⟨j, hj⟩ := Nat.le.dest hJ
  exact Bern.big_q3 K O F h1 h10 Q h hQ0 hQ1 hQ hQne hqne h0 (a + 3) ((4 * (b + 15) + 1) * (2 * (a + 4))) (b + 15) j hj.symm
    (half_le_a K O F h1 h10 Q h hQ0 hQ1 hh (b + 15) hK1)
    (condP_K K O F h1 h10 Q h hQ0 hQ1 hh h0 (b + 15) (a + 3) hM hK2)
    (hP_K K O F h1 h10 Q h hQ0 hQ1 hh h0 b hK2)
    (hB_K K O F h1 h10 Q h hQ0 hQ1 hh h0 (b + 15) a hM hK2)
    (hC_K K O F h1 h10 Q h hQ0 hQ1 hh h0 (b + 15) a hM hK2)

end BigQK

#print axioms BigQK.margin_eq
#print axioms BigQK.halve
#print axioms BigQK.half_le_a
#print axioms BigQK.hP_K
#print axioms BigQK.condP_K
#print axioms BigQK.hC_K
#print axioms BigQK.hB_K
#print axioms BigQK.big_K

#print axioms: 7 theorems, no axioms.

90.79 Decimation and the likelihood-ratio order

Decimation preserves the likelihood-ratio order and maps a one-sided comparison with \(K_q\) to one with \(K_{q^4}\).

\[ a_{n+1}b_n\le a_nb_{n+1}\ \forall n\ \Rightarrow \ (\mathrm {dec}\,a)_{n+1}(\mathrm {dec}\,b)_n\le (\mathrm {dec}\,a)_n(\mathrm {dec}\,b)_{n+1} \]

ym/DecLR.lean

/-
  ym/DecLR — T45, G46: decimation preserves the likelihood-ratio order. If a_{n+1} b_n ≤ a_n b_{n+1} for all n (b/a
  nondecreasing, all entries ≥ 0) then the same holds after decM (entries ↦ entry⁴·w_n with the same weights).
  Lean 4 core only, no axioms.
-/
import ym.TripleE
import ym.HeatPair
import ym.SecondRing

namespace DecLR

open YMChain LinkIntegral SU2Char MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

def w (m : Nat) : R := pwR K (F.inv (cN K (m + 1))) 3

theorem co_decM : ∀ (a : List R) m n, co K (decM K O F m a) n = K.mul (pwR K (co K a n) 4) (w K O F (m + n))
  | [], m, n => by show K.zero = K.mul (pwR K K.zero 4) _; show K.zero = K.mul (K.mul (K.mul (K.mul (K.mul K.one K.zero) K.zero) K.zero) K.zero) _; lib_ring
  | x :: a, m, 0 => rfl
  | x :: a, m, n + 1 => by
    show co K (decM K O F (m + 1) a) n = _
    rw [co_decM a (m + 1) n, show m + 1 + n = m + (n + 1) by lib_nat]; rfl

/-- **LR order is preserved by decimation** -/
theorem dec_LR (h1 : O.le K.zero K.one) (a b : List R) (m : Nat) (ha : ∀ n, O.le K.zero (co K a n)) (hb : ∀ n, O.le K.zero (co K b n))
    (hw : ∀ n, O.le K.zero (w K O F n))
    (h : ∀ n, O.le (K.mul (co K a (n + 1)) (co K b n)) (K.mul (co K a n) (co K b (n + 1)))) :
    ∀ n, O.le (K.mul (co K (decM K O F m a) (n + 1)) (co K (decM K O F m b) n))
      (K.mul (co K (decM K O F m a) n) (co K (decM K O F m b) (n + 1))) := by
  intro n
  rw [co_decM, co_decM, co_decM, co_decM]
  have p := SU2Strong4D.pw_mono K O h1 _ _ (O.mul_nonneg _ _ (ha (n + 1)) (hb n)) (h n) 4
  rw [HeatDec.pw_mul_pw, HeatDec.pw_mul_pw] at p
  have ww := O.mul_nonneg _ _ (hw (m + (n + 1))) (hw (m + n))
  have s := O.mul_le_mul_right _ _ _ p ww
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · lib_ring

/-- **L3: decimation keeps a one-sided LR comparison with the heat kernel, q ↦ q⁴** (a list of length L) -/
theorem dec_LR_heat (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (q : R) (hq : O.le K.zero q) (a : List R) (L : Nat)
    (ha : ∀ n, O.le K.zero (co K a n)) (hw : ∀ n, O.le K.zero (w K O F n))
    (h : ∀ n, O.le (K.mul (co K a (n + 1)) (co K (HeatDec.heat K q 0 L) n)) (K.mul (co K a n) (co K (HeatDec.heat K q 0 L) (n + 1)))) :
    ∀ n, O.le (K.mul (co K (decM K O F 0 a) (n + 1)) (co K (HeatDec.heat K (pwR K q 4) 0 L) n))
      (K.mul (co K (decM K O F 0 a) n) (co K (HeatDec.heat K (pwR K q 4) 0 L) (n + 1))) := by
  have hb : ∀ n, O.le K.zero (co K (HeatDec.heat K q 0 L) n) := by
    intro n
    cases Nat.lt_or_ge n L with
    | inl hl =>
      rw [SecondRing.co_heat K q L 0 n hl]
      exact O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O q hq h1 _)
    | inr hg =>
      have : ∀ (l : List R) (k : Nat), l.length ≤ k → co K l k = K.zero := by
        intro l; induction l with
        | nil => intro k _; rfl
        | cons x l ih => intro k hk; cases k with
          | zero => exact absurd hk (Nat.not_succ_le_zero _)
          | succ k => exact ih k (Nat.le_of_succ_le_succ hk)
      rw [this _ n (by rw [SecondRing.len_heat]; exact hg)]; exact O.le_refl _
  have d := dec_LR K O F h1 a (HeatDec.heat K q 0 L) 0 ha hb hw h
  rw [HeatDec.decM_heat K O F h10 h1 q 0 L] at d
  exact d

/-- LR comparison a ≼ b at one index: a_{n+1} b_n ≤ a_n b_{n+1} -/
def LRat (a b : Nat → R) (n : Nat) : Prop := O.le (K.mul (a (n + 1)) (b n)) (K.mul (a n) (b (n + 1)))

/-- **transitivity** through a positive middle sequence -/
theorem lr_trans (F : OField.OF K O) (a b c : Nat → R) (hb : ∀ n, O.le K.zero (b n)) (hbne : ∀ n, b n ≠ K.zero)
    (ha : ∀ n, O.le K.zero (a n)) (hc : ∀ n, O.le K.zero (c n)) (n : Nat)
    (h1 : LRat K O a b n) (h2 : LRat K O b c n) : LRat K O a c n := by
  unfold LRat at *
  -- multiply h1 by c_{n+1}, h2 by a_n: a_{n+1} b_n c_n ≤ a_n b_{n+1} c_n ≤ ... use b_n b_{n+1} > 0
  apply GridQ.cancel K O F (K.mul (b n) (b (n + 1))) _ _ (O.mul_nonneg _ _ (hb n) (hb (n + 1)))
    (fun e => hbne (n + 1) (O.pos_mul_eq_zero _ _ (hb n) (hbne n) e))
  have s1 := O.mul_le_mul_right _ _ _ h1 (O.mul_nonneg _ _ (hb (n + 1)) (hc n))
  have s2 := O.mul_le_mul_right _ _ _ h2 (O.mul_nonneg _ _ (ha n) (hb (n + 1)))
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
    (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ ?_))))
  · lib_ring
  · lib_ring
  · lib_ring

/-- **u bound**: a ≼ b at 0 with a₀ = b₀ = 1 gives a₁ ≤ b₁ -/
theorem lr_u (a b : Nat → R) (h : LRat K O a b 0) (ha : a 0 = K.one) (hb : b 0 = K.one) : O.le (a 1) (b 1) := by
  unfold LRat at h; rw [ha, hb, K.mul_comm, K.one_mul, K.one_mul] at h; exact h

/-- **LR order is closed under pointwise products** (for L0: the Bessel weight as a product of LR-ordered factors) -/
theorem lr_mul (a b c d : Nat → R) (n : Nat) (hc : O.le K.zero (c (n + 1))) (hd : O.le K.zero (d n))
    (ha : O.le K.zero (a n)) (hb1 : O.le K.zero (b (n + 1)))
    (h1 : LRat K O a b n) (h2 : LRat K O c d n) :
    LRat K O (fun m => K.mul (a m) (c m)) (fun m => K.mul (b m) (d m)) n := by
  unfold LRat at *
  show O.le (K.mul (K.mul (a (n + 1)) (c (n + 1))) (K.mul (b n) (d n))) (K.mul (K.mul (a n) (c n)) (K.mul (b (n + 1)) (d (n + 1))))
  have s1 := O.mul_le_mul_right _ _ _ h1 (O.mul_nonneg _ _ hc hd)
  have s2 := O.mul_le_mul_right _ _ _ h2 (O.mul_nonneg _ _ ha hb1)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
    (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ ?_))))
  · lib_ring
  · lib_ring
  · lib_ring

end DecLR

#print axioms DecLR.co_decM
#print axioms DecLR.dec_LR
#print axioms DecLR.dec_LR_heat
#print axioms DecLR.lr_trans
#print axioms DecLR.lr_u
#print axioms DecLR.lr_mul

#print axioms: 6 theorems, no axioms.

90.80 A narrower weight keeps a smaller first ratio

For \(X_j=Y_jg_j\) with \(Y\) the truncated heat kernel and \(g\) nonincreasing, \(u(X)\le u(Y)\).

\[ \Big (2\sum _j X_jX_{j+1}\Big )\sum _j Y_j^2\ \le \ \Big (\sum _j X_j^2\Big )\Big (2\sum _j Y_jY_{j+1}\Big ) \]

ym/L1Zero.lean

/-
  ym/L1Zero — T45, G44-L1 at n = 0: if X_j = Y_j g_j with g nonincreasing and ≥ 0, and the half-boundary ratios of Y
  satisfy t_m ≤ t_{Mx} (Y_{Mx+1} = 0), then u(X) ≤ u(Y) in cleared form:
    (2Σ_{j≤Mx} X_jX_{j+1})·Σ_{j≤Mx} Y_j² ≤ (Σ_{j≤Mx} X_j²)·N_{Mx}(Y),   N_{Mx}(Y) = 2Σ Y_jY_{j+1}.
  Proof: 2g_jg_{j+1} ≤ g_j² + g_{j+1}², reindexing, and Abel's inequality whose prefix sums are T·D_m − D·N_m ≥ 0.
  Lean 4 core only, no axioms.
-/
import ym.HalfRatio
import ym.AbelLR

namespace L1Zero

open YMChain LinkIntegral SU2Char QBinom HalfRatio

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem sum_le (f g : Nat → R) (h : ∀ j, O.le (f j) (g j)) : ∀ n, O.le (sumN K f n) (sumN K g n)
  | 0 => O.le_refl _
  | n + 1 => O.add_le_add _ _ _ _ (sum_le f g h n) (h n)

def pm (Y : Nat → R) : Nat → R
  | 0 => K.zero
  | j + 1 => K.mul (Y j) (Y (j + 1))

def cc (Y : Nat → R) (j : Nat) : R := K.add (K.mul (Y j) (Y (j + 1))) (pm K Y j)

theorem pre (Y : Nat → R) : ∀ m, sumN K (cc K Y) (m + 1) = Nn K Y m
  | 0 => by
    show K.add K.zero (K.add (K.mul (Y 0) (Y 1)) K.zero) = K.add (K.mul (two K) K.zero) (K.mul (Y 0) (Y 1))
    lib_ring
  | m + 1 => by
    show K.add (sumN K (cc K Y) (m + 1)) (K.add (K.mul (Y (m + 1)) (Y (m + 2))) (K.mul (Y m) (Y (m + 1)))) = Nn K Y (m + 1)
    rw [pre Y m, Nn_succ]; lib_ring

theorem reidx (Y G : Nat → R) : ∀ n,
    sumN K (fun j => K.mul (K.mul (Y j) (Y (j + 1))) (K.add (G j) (G (j + 1)))) n
      = K.add (sumN K (fun j => K.mul (cc K Y j) (G j)) n) (K.mul (pm K Y n) (G n))
  | 0 => by show K.zero = K.add K.zero (K.mul K.zero (G 0)); lib_ring
  | n + 1 => by
    show K.add (sumN K _ n) (K.mul (K.mul (Y n) (Y (n + 1))) (K.add (G n) (G (n + 1))))
      = K.add (K.add (sumN K (fun j => K.mul (cc K Y j) (G j)) n) (K.mul (K.add (K.mul (Y n) (Y (n + 1))) (pm K Y n)) (G n)))
          (K.mul (K.mul (Y n) (Y (n + 1))) (G (n + 1)))
    rw [reidx Y G n]; lib_ring

theorem sum_sub (f g : Nat → R) (a b : R) : ∀ n,
    sumN K (fun j => K.add (K.mul a (f j)) (K.neg (K.mul b (g j)))) n = K.add (K.mul a (sumN K f n)) (K.neg (K.mul b (sumN K g n)))
  | 0 => by show K.zero = K.add (K.mul a K.zero) (K.neg (K.mul b K.zero)); lib_ring
  | n + 1 => by
    show K.add (sumN K (fun j => K.add (K.mul a (f j)) (K.neg (K.mul b (g j)))) n) (K.add (K.mul a (f n)) (K.neg (K.mul b (g n))))
      = K.add (K.mul a (K.add (sumN K f n) (f n))) (K.neg (K.mul b (K.add (sumN K g n) (g n))))
    rw [sum_sub f g a b n]; lib_ring

/-- **L1 at n = 0 (abstract)** -/
theorem l1_zero (Y g : Nat → R) (hY : ∀ j, O.le K.zero (Y j)) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (Mx : Nat) (hz : Y (Mx + 1) = K.zero)
    (hc : ∀ m, m ≤ Mx → O.le (K.mul (Nn K Y m) (Dd K Y Mx)) (K.mul (Nn K Y Mx) (Dd K Y m))) :
    O.le (K.mul (K.mul (two K) (sumN K (fun j => K.mul (K.mul (Y j) (g j)) (K.mul (Y (j + 1)) (g (j + 1)))) (Mx + 1))) (Dd K Y Mx))
      (K.mul (sumN K (fun j => K.mul (K.mul (Y j) (g j)) (K.mul (Y j) (g j))) (Mx + 1)) (Nn K Y Mx)) := by
  have hD := Dd_nn K O Y hY Mx
  have hG : ∀ m, O.le ((fun j => K.mul (g j) (g j)) (m + 1)) ((fun j => K.mul (g j) (g j)) m) := by
    intro m
    have s1 := O.mul_le_mul_right _ _ _ (hg m) (hg0 (m + 1))
    have s2 := O.mul_le_mul_right _ _ _ (hg m) (hg0 m)
    exact O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (K.mul_comm _ _)) s2)
  have hG0 : ∀ m, O.le K.zero ((fun j => K.mul (g j) (g j)) m) := fun m => O.mul_nonneg _ _ (hg0 m) (hg0 m)
  -- step 1: AM-GM
  have s1 : O.le (K.mul (two K) (sumN K (fun j => K.mul (K.mul (Y j) (g j)) (K.mul (Y (j + 1)) (g (j + 1)))) (Mx + 1)))
      (sumN K (fun j => K.mul (K.mul (Y j) (Y (j + 1))) (K.add (K.mul (g j) (g j)) (K.mul (g (j + 1)) (g (j + 1))))) (Mx + 1)) := by
    rw [← sumN_mul]
    apply sum_le K O
    intro j
    have sq := O.mul_nonneg _ _ (O.mul_nonneg _ _ (hY j) (hY (j + 1)))
      (O.mul_nonneg (K.add (g j) (K.neg (g (j + 1)))) _ (HeatV.sub_nn K O _ _ (hg j)) (HeatV.sub_nn K O _ _ (hg j)))
    show O.le (K.mul (two K) (K.mul (K.mul (Y j) (g j)) (K.mul (Y (j + 1)) (g (j + 1)))))
      (K.mul (K.mul (Y j) (Y (j + 1))) (K.add (K.mul (g j) (g j)) (K.mul (g (j + 1)) (g (j + 1)))))
    refine O.le_trans _ _ _ (HeatV.le_add_nn K O _ _ sq) (TripleE.le_of_eq' K O _ _ ?_)
    unfold two; lib_ring
  have s1' := O.mul_le_mul_right _ _ _ s1 hD
  rw [reidx K Y (fun j => K.mul (g j) (g j)) (Mx + 1), show pm K Y (Mx + 1) = K.zero by
    show K.mul (Y Mx) (Y (Mx + 1)) = K.zero; rw [hz]; lib_ring] at s1'
  -- Abel with d_j = N·Y_j² − D·c_j
  have ab := AbelLR.abel K O (fun j => K.add (K.mul (Nn K Y Mx) (K.mul (Y j) (Y j))) (K.neg (K.mul (Dd K Y Mx) (cc K Y j))))
    (fun j => K.mul (g j) (g j)) hG hG0 (Mx + 1) (by
      intro m hm
      cases m with
      | zero => exact O.le_refl _
      | succ m' =>
        rw [sum_sub K (fun j => K.mul (Y j) (Y j)) (cc K Y) (Nn K Y Mx) (Dd K Y Mx) (m' + 1), pre]
        have h := hc m' (Nat.le_of_succ_le_succ hm)
        have e : K.mul (Dd K Y Mx) (Nn K Y m') = K.mul (Nn K Y m') (Dd K Y Mx) := K.mul_comm _ _
        have e2 : K.mul (Nn K Y Mx) (sumN K (fun j => K.mul (Y j) (Y j)) (m' + 1)) = K.mul (Nn K Y Mx) (Dd K Y m') := rfl
        rw [e, e2]
        exact HeatV.sub_nn K O _ _ h)
  have split : sumN K (fun j => K.mul (K.add (K.mul (Nn K Y Mx) (K.mul (Y j) (Y j))) (K.neg (K.mul (Dd K Y Mx) (cc K Y j)))) (K.mul (g j) (g j))) (Mx + 1)
      = K.add (K.mul (Nn K Y Mx) (sumN K (fun j => K.mul (K.mul (Y j) (Y j)) (K.mul (g j) (g j))) (Mx + 1)))
          (K.neg (K.mul (Dd K Y Mx) (sumN K (fun j => K.mul (cc K Y j) (K.mul (g j) (g j))) (Mx + 1)))) := by
    rw [← sum_sub]; exact sumN_ext K _ _ (fun j => by lib_ring) _
  rw [split] at ab
  have fin : O.le (K.mul (Dd K Y Mx) (sumN K (fun j => K.mul (cc K Y j) (K.mul (g j) (g j))) (Mx + 1)))
      (K.mul (Nn K Y Mx) (sumN K (fun j => K.mul (K.mul (Y j) (Y j)) (K.mul (g j) (g j))) (Mx + 1))) := by
    have t := O.add_le_add _ _ _ _ ab (O.le_refl (K.mul (Dd K Y Mx) (sumN K (fun j => K.mul (cc K Y j) (K.mul (g j) (g j))) (Mx + 1))))
    rw [show K.add K.zero (K.mul (Dd K Y Mx) (sumN K (fun j => K.mul (cc K Y j) (K.mul (g j) (g j))) (Mx + 1)))
      = K.mul (Dd K Y Mx) (sumN K (fun j => K.mul (cc K Y j) (K.mul (g j) (g j))) (Mx + 1)) by lib_ring] at t
    refine O.le_trans _ _ _ t (TripleE.le_of_eq' K O _ _ ?_); lib_ring
  have e3 : sumN K (fun j => K.mul (K.mul (Y j) (g j)) (K.mul (Y j) (g j))) (Mx + 1)
      = sumN K (fun j => K.mul (K.mul (Y j) (Y j)) (K.mul (g j) (g j))) (Mx + 1) := sumN_ext K _ _ (fun j => by lib_ring) _
  rw [e3]
  refine O.le_trans _ _ _ s1' (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ fin (TripleE.le_of_eq' K O _ _ ?_)))
  · lib_ring
  · lib_ring

theorem ex_add : ∀ m n : Nat, m ≤ n → ∃ d, n = m + d
  | 0, n, _ => ⟨n, (Nat.zero_add n).symm⟩
  | m + 1, 0, h => absurd h (Nat.not_succ_le_zero _)
  | m + 1, n + 1, h => by
    have ⟨d, e⟩ := ex_add m n (Nat.le_of_succ_le_succ h)
    exact ⟨d, by rw [e]; lib_nat⟩

/-- **L1 at n = 0 for the truncated heat kernel**: X = Yt·g (g nonincreasing ≥ 0) has u(X) ≤ u(Yt) -/
theorem heat_l1_zero (F : OField.OF K O) (h1 : O.le K.zero K.one) (q : R) (hq0 : O.le K.zero q) (hq1 : O.le (pwR K q 2) K.one)
    (hYne : ∀ j, Yh K q j ≠ K.zero) (M : Nat)
    (hcut : O.le (K.mul (K.add (Yh K q M) (Yh K q (M + 2))) (Yh K q (M + 2))) (K.mul (Yh K q (M + 1)) (Yh K q (M + 1))))
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m)) :
    O.le (K.mul (K.mul (two K) (sumN K (fun j => K.mul (K.mul (Yt K q (M + 3) j) (g j)) (K.mul (Yt K q (M + 3) (j + 1)) (g (j + 1)))) (M + 3)))
        (Dd K (Yt K q (M + 3)) (M + 2)))
      (K.mul (sumN K (fun j => K.mul (K.mul (Yt K q (M + 3) j) (g j)) (K.mul (Yt K q (M + 3) j) (g j))) (M + 3)) (Nn K (Yt K q (M + 3)) (M + 2))) :=
  l1_zero K O (Yt K q (M + 3)) g (Yt_nn K O h1 q hq0 (M + 3)) hg hg0 (M + 2) (Yt_ge K q (M + 3) (M + 3) (Nat.le_refl _))
    (fun m hm => by
      have ⟨d, e⟩ := ex_add m (M + 2) hm
      rw [e]; exact heat_trunc_chain K O F h1 q hq0 hq1 hYne M hcut m d)

/-- **L1 at n = 0 for any v-monotone weight** (route 6b: compare with the heat MK trajectory, which is v-monotone):
  Y ≥ 0, Y_j ≠ 0 for j ≤ Mx, Y_j = 0 for j > Mx, t_0 ≤ v_0, v_m ≤ v_{m+1} for m + 1 ≤ Mx; X = Y·g, g nonincreasing ⇒ u(X) ≤ u(Y). -/
theorem vv_l1_zero (F : OField.OF K O) (Y g : Nat → R) (hY : ∀ j, O.le K.zero (Y j)) (Mx : Nat)
    (hYne : ∀ j, j ≤ Mx → Y j ≠ K.zero) (hz : ∀ j, Mx < j → Y j = K.zero)
    (h0 : TV K O Y 0) (hv : ∀ m, m + 1 ≤ Mx → VV K O Y m)
    (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m)) :
    O.le (K.mul (K.mul (two K) (sumN K (fun j => K.mul (K.mul (Y j) (g j)) (K.mul (Y (j + 1)) (g (j + 1)))) (Mx + 1))) (Dd K Y Mx))
      (K.mul (sumN K (fun j => K.mul (K.mul (Y j) (g j)) (K.mul (Y j) (g j))) (Mx + 1)) (Nn K Y Mx)) := by
  have up := tv_upto K O F Y hY Mx hYne h0 hv
  have tv : ∀ m, TV K O Y m := by
    intro m
    cases Nat.lt_or_ge Mx (m + 1) with
    | inl hl =>
      unfold TV
      rw [hz (m + 1) hl]
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring : K.mul (Nn K Y m) K.zero = K.zero)) ?_
      have := O.add_le_add _ _ _ _ (hY m) (hY (m + 2))
      rw [show K.add K.zero K.zero = K.zero by lib_ring] at this
      exact O.mul_nonneg _ _ this (Dd_nn K O Y hY m)
    | inr hg' => exact up m (Nat.le_of_succ_le hg')
  have hY0 : Y 0 ≠ K.zero := hYne 0 (Nat.zero_le _)
  exact l1_zero K O Y g hY hg hg0 Mx (hz (Mx + 1) (Nat.lt_succ_self _))
    (fun m hm => by
      have ⟨d, e⟩ := ex_add m Mx hm
      rw [e]; exact t_chain K O F Y hY hY0 m (fun k _ => t_mono K O Y hY k (tv k)) d)

end L1Zero

#print axioms L1Zero.pre
#print axioms L1Zero.reidx
#print axioms L1Zero.l1_zero
#print axioms L1Zero.ex_add
#print axioms L1Zero.heat_l1_zero
#print axioms L1Zero.vv_l1_zero

#print axioms: 5 theorems, no axioms.

90.81 Row ratios of the bond-moving product

For a weight with \(v_m=(Y_m+Y_{m+2})/(2Y_{m+1})\) nondecreasing, the window-sum ratios are nondecreasing.

\[ \Delta (d,\ell +1)=\Delta (d,\ell )+\sum _{i\le \ell }2Y_{d+2i+1}Y_{d+2\ell +2}\,(v_{d+2\ell +1}-v_{d+2i})\ \ge 0 \]

ym/Window.lean

/-
  ym/Window — T45, G44-L1 (ii-a): window sums P(c,ℓ) = Σ_{i<ℓ} Y_{c+2i} and the exact recursion
    Δ(d,ℓ+1) = Δ(d,ℓ) + Σ_{i≤ℓ} D(d+2i, d+2ℓ+1),
    Δ(d,ℓ) = P(d+1,ℓ+1)P(d+1,ℓ) − P(d,ℓ+1)P(d+2,ℓ),
    D(x,y) = (Y_y + Y_{y+2})Y_{x+1} − (Y_x + Y_{x+2})Y_{y+1}   (= 2Y_{x+1}Y_{y+1}(v_y − v_x)).
  So if v is nondecreasing (D(x,y) ≥ 0 for x ≤ y) then Δ ≥ 0: the row ratio Ψ_{n+1}(a)/Ψ_n(a) of the bond-moving
  product is nondecreasing in a, for every n. Lean 4 core only, no axioms.
-/
import ym.L1Zero

namespace Window

open YMChain LinkIntegral SU2Char QBinom

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

def P (Y : Nat → R) (c ℓ : Nat) : R := sumN K (fun i => Y (c + 2 * i)) ℓ

def Dl (Y : Nat → R) (x y : Nat) : R :=
  K.add (K.mul (K.add (Y y) (Y (y + 2))) (Y (x + 1))) (K.neg (K.mul (K.add (Y x) (Y (x + 2))) (Y (y + 1))))

def Del (Y : Nat → R) (d ℓ : Nat) : R :=
  K.add (K.mul (P K Y (d + 1) (ℓ + 1)) (P K Y (d + 1) ℓ)) (K.neg (K.mul (P K Y d (ℓ + 1)) (P K Y (d + 2) ℓ)))

theorem P_succ (Y : Nat → R) (c ℓ : Nat) : P K Y c (ℓ + 1) = K.add (P K Y c ℓ) (Y (c + 2 * ℓ)) := rfl

theorem P_shift (Y : Nat → R) (d ℓ : Nat) : P K Y d (ℓ + 1) = K.add (Y d) (P K Y (d + 2) ℓ) := by
  unfold P; rw [sumN_shift]
  congr 1
  exact sumN_ext K _ _ (fun i => by rw [show d + 2 * (i + 1) = d + 2 + 2 * i by lib_nat]) ℓ

/-- closed form of the inner sum -/
theorem inner_sum (Y : Nat → R) (d z : Nat) : ∀ ℓ,
    sumN K (fun i => Dl K Y (d + 2 * i) z) ℓ
      = K.add (K.mul (K.add (Y z) (Y (z + 2))) (P K Y (d + 1) ℓ)) (K.neg (K.mul (Y (z + 1)) (K.add (P K Y d ℓ) (P K Y (d + 2) ℓ))))
  | 0 => by show K.zero = K.add (K.mul _ K.zero) (K.neg (K.mul _ (K.add K.zero K.zero))); lib_ring
  | ℓ + 1 => by
    show K.add (sumN K (fun i => Dl K Y (d + 2 * i) z) ℓ) (Dl K Y (d + 2 * ℓ) z) = _
    rw [inner_sum Y d z ℓ, P_succ, P_succ, P_succ]
    unfold Dl
    rw [show d + 2 * ℓ + 1 = d + 1 + 2 * ℓ by lib_nat, show d + 2 * ℓ + 2 = d + 2 + 2 * ℓ by lib_nat]
    lib_ring

theorem Del_zero (Y : Nat → R) (d : Nat) : Del K Y d 0 = K.zero := by
  show K.add (K.mul (P K Y (d + 1) 1) K.zero) (K.neg (K.mul (P K Y d 1) K.zero)) = K.zero; lib_ring

/-- **the recursion** -/
theorem Del_succ (Y : Nat → R) (d ℓ : Nat) :
    Del K Y d (ℓ + 1) = K.add (Del K Y d ℓ) (sumN K (fun i => Dl K Y (d + 2 * i) (d + 2 * ℓ + 1)) (ℓ + 1)) := by
  rw [inner_sum]
  unfold Del
  rw [P_succ K Y (d + 1) (ℓ + 1), P_succ K Y d (ℓ + 1), P_succ K Y (d + 1) ℓ, P_succ K Y (d + 2) ℓ, P_shift K Y d ℓ]
  rw [show d + 1 + 2 * (ℓ + 1) = d + 2 * ℓ + 1 + 2 by lib_nat, show d + 2 * (ℓ + 1) = d + 2 * ℓ + 1 + 1 by lib_nat,
    show d + 1 + 2 * ℓ = d + 2 * ℓ + 1 by lib_nat, show d + 2 + 2 * ℓ = d + 2 * ℓ + 1 + 1 by lib_nat]
  lib_ring

/-- D(x, x+k) ≥ 0 from v nondecreasing -/
theorem dl_nn (F : OField.OF K O) (Y : Nat → R) (hY : ∀ j, O.le K.zero (Y j)) (hYne : ∀ j, Y (j + 1) ≠ K.zero)
    (hv : ∀ m, HalfRatio.VV K O Y m) (x : Nat) : ∀ k, O.le K.zero (Dl K Y x (x + k))
  | 0 => by unfold Dl; rw [Nat.add_zero, show K.add (K.mul (K.add (Y x) (Y (x + 2))) (Y (x + 1))) (K.neg (K.mul (K.add (Y x) (Y (x + 2))) (Y (x + 1)))) = K.zero by lib_ring]; exact O.le_refl _
  | k + 1 => by
    have ih := dl_nn F Y hY hYne hv x k
    have h2 := HeatV.sub_nn K O _ _ (hv (x + k))
    unfold Dl at *
    -- A_x B_{x+k} ≤ A_{x+k} B_x and A_{x+k} B_{x+k+1} ≤ A_{x+k+1} B_{x+k}
    have i1 : O.le (K.mul (K.add (Y x) (Y (x + 2))) (Y (x + k + 1))) (K.mul (K.add (Y (x + k)) (Y (x + k + 2))) (Y (x + 1))) := by
      have := O.add_le_add _ _ _ _ ih (O.le_refl (K.mul (K.add (Y x) (Y (x + 2))) (Y (x + k + 1))))
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
    have i2 : O.le (K.mul (K.add (Y (x + k)) (Y (x + k + 2))) (Y (x + k + 2))) (K.mul (K.add (Y (x + k + 1)) (Y (x + k + 3))) (Y (x + k + 1))) := hv (x + k)
    apply HeatV.sub_nn K O
    rw [show x + (k + 1) = x + k + 1 from rfl, show x + k + 1 + 1 = x + k + 2 from rfl, show x + k + 1 + 2 = x + k + 3 from rfl]
    apply GridQ.cancel K O F (Y (x + k + 1)) _ _ (hY _) (hYne (x + k))
    have s1 := O.mul_le_mul_right _ _ _ i1 (hY (x + k + 2))
    have s2 := O.mul_le_mul_right _ _ _ i2 (hY (x + 1))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
      (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ ?_)))) <;> lib_ring

/-- **row-ratio monotonicity**: Δ(d, ℓ) ≥ 0 for every window length -/
theorem Del_nn (F : OField.OF K O) (Y : Nat → R) (hY : ∀ j, O.le K.zero (Y j)) (hYne : ∀ j, Y (j + 1) ≠ K.zero)
    (hv : ∀ m, HalfRatio.VV K O Y m) (d : Nat) : ∀ ℓ, O.le K.zero (Del K Y d ℓ)
  | 0 => by rw [Del_zero]; exact O.le_refl _
  | ℓ + 1 => by
    rw [Del_succ]
    have ih := Del_nn F Y hY hYne hv d ℓ
    have hs : ∀ n, n ≤ ℓ + 1 → O.le K.zero (sumN K (fun i => Dl K Y (d + 2 * i) (d + 2 * ℓ + 1)) n) := by
      intro n; induction n with
      | zero => intro _; exact O.le_refl _
      | succ n ihn =>
        intro hn
        have hi : d + 2 * n ≤ d + 2 * ℓ + 1 := by
          have : n ≤ ℓ := Nat.le_of_succ_le_succ hn
          exact Nat.le_trans (Nat.add_le_add_left (Nat.mul_le_mul_left 2 this) d) (Nat.le_succ _)
        have ⟨k, e⟩ := L1Zero.ex_add _ _ hi
        have t := dl_nn K O F Y hY hYne hv (d + 2 * n) k
        rw [← e] at t
        have := O.add_le_add _ _ _ _ (ihn (Nat.le_of_succ_le hn)) t
        rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
    have := O.add_le_add _ _ _ _ ih (hs (ℓ + 1) (Nat.le_refl _))
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this

/-- **heat kernel**: for every window length the row ratio of the bond-moving product is nondecreasing -/
theorem heat_Del_nn (F : OField.OF K O) (h1 : O.le K.zero K.one) (q : R) (hq0 : O.le K.zero q) (hq1 : O.le (pwR K q 2) K.one)
    (hYne : ∀ j, HalfRatio.Yh K q j ≠ K.zero) (d ℓ : Nat) : O.le K.zero (Del K (HalfRatio.Yh K q) d ℓ) :=
  Del_nn K O F (HalfRatio.Yh K q) (fun j => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O q hq0 h1 _)) (fun j => hYne (j + 1))
    (HalfRatio.heat_VV K O h1 q hq0 hq1) d ℓ

/-- **prefix sums inherit a nondecreasing ratio**: if r_j p_{j+1} ≤ r_{j+1} p_j for all j (p ≥ 0) then
  (Σ_{j<N} r_j) p_N ≤ r_N (Σ_{j<N} p_j)  and  (Σ_{j<N} r)(Σ_{j<N+1} p) ≤ (Σ_{j<N+1} r)(Σ_{j<N} p) -/
theorem head_vs_next (F : OField.OF K O) (r p : Nat → R) (hp : ∀ j, O.le K.zero (p j)) (hpne : ∀ j, p j ≠ K.zero)
    (h : ∀ j, O.le (K.mul (r j) (p (j + 1))) (K.mul (r (j + 1)) (p j))) :
    ∀ N, O.le (K.mul (sumN K r N) (p N)) (K.mul (r N) (sumN K p N))
  | 0 => by show O.le (K.mul K.zero (p 0)) (K.mul (r 0) K.zero); rw [K.mul_comm K.zero, DetChain.mul_zero, DetChain.mul_zero]; exact O.le_refl _
  | N + 1 => by
    have ih := head_vs_next F r p hp hpne h N
    show O.le (K.mul (K.add (sumN K r N) (r N)) (p (N + 1))) (K.mul (r (N + 1)) (K.add (sumN K p N) (p N)))
    apply GridQ.cancel K O F (p N) _ _ (hp N) (hpne N)
    have hS : O.le K.zero (sumN K p N) := by
      have : ∀ k, O.le K.zero (sumN K p k) := by
        intro k; induction k with
        | zero => exact O.le_refl _
        | succ k ihk => have := O.add_le_add _ _ _ _ ihk (hp k); rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
      exact this N
    have s1 := O.mul_le_mul_right _ _ _ ih (hp (N + 1))
    have s2 := O.mul_le_mul_right _ _ _ (h N) (hS)
    have s3 := O.mul_le_mul_right _ _ _ (h N) (hp N)
    have s := O.add_le_add _ _ _ _ (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) s2)) s3
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

theorem head_mono (F : OField.OF K O) (r p : Nat → R) (hp : ∀ j, O.le K.zero (p j)) (hpne : ∀ j, p j ≠ K.zero)
    (h : ∀ j, O.le (K.mul (r j) (p (j + 1))) (K.mul (r (j + 1)) (p j))) (N : Nat) :
    O.le (K.mul (sumN K r N) (sumN K p (N + 1))) (K.mul (sumN K r (N + 1)) (sumN K p N)) := by
  have t := head_vs_next K O F r p hp hpne h N
  show O.le (K.mul (sumN K r N) (K.add (sumN K p N) (p N))) (K.mul (K.add (sumN K r N) (r N)) (sumN K p N))
  have := O.add_le_add _ _ _ _ (O.le_refl (K.mul (sumN K r N) (sumN K p N))) t
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

/-- truncated version: Y positive below L and zero from L on -/
theorem dl_nn_t (F : OField.OF K O) (Y : Nat → R) (hY : ∀ j, O.le K.zero (Y j)) (L : Nat)
    (hpos : ∀ j, j + 1 < L → Y (j + 1) ≠ K.zero) (hz : ∀ j, L ≤ j → Y j = K.zero)
    (hv : ∀ m, HalfRatio.VV K O Y m) (x : Nat) : ∀ k, O.le K.zero (Dl K Y x (x + k))
  | 0 => by unfold Dl; rw [Nat.add_zero, show K.add (K.mul (K.add (Y x) (Y (x + 2))) (Y (x + 1))) (K.neg (K.mul (K.add (Y x) (Y (x + 2))) (Y (x + 1)))) = K.zero by lib_ring]; exact O.le_refl _
  | k + 1 => by
    cases Nat.lt_or_ge (x + k + 2) L with
    | inr hge =>
      unfold Dl
      rw [show x + (k + 1) + 1 = x + k + 2 from rfl, hz (x + k + 2) hge]
      rw [show K.add (K.mul (K.add (Y (x + (k + 1))) (Y (x + (k + 1) + 2))) (Y (x + 1))) (K.neg (K.mul (K.add (Y x) (Y (x + 2))) K.zero))
        = K.mul (K.add (Y (x + (k + 1))) (Y (x + (k + 1) + 2))) (Y (x + 1)) by lib_ring]
      have := O.add_le_add _ _ _ _ (hY (x + (k + 1))) (hY (x + (k + 1) + 2))
      rw [show K.add K.zero K.zero = K.zero by lib_ring] at this
      exact O.mul_nonneg _ _ this (hY _)
    | inl hlt =>
      have ih := dl_nn_t F Y hY L hpos hz hv x k
      unfold Dl at *
      have i1 : O.le (K.mul (K.add (Y x) (Y (x + 2))) (Y (x + k + 1))) (K.mul (K.add (Y (x + k)) (Y (x + k + 2))) (Y (x + 1))) := by
        have := O.add_le_add _ _ _ _ ih (O.le_refl (K.mul (K.add (Y x) (Y (x + 2))) (Y (x + k + 1))))
        refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
      have i2 : O.le (K.mul (K.add (Y (x + k)) (Y (x + k + 2))) (Y (x + k + 2))) (K.mul (K.add (Y (x + k + 1)) (Y (x + k + 3))) (Y (x + k + 1))) := hv (x + k)
      apply HeatV.sub_nn K O
      rw [show x + (k + 1) = x + k + 1 from rfl, show x + k + 1 + 1 = x + k + 2 from rfl, show x + k + 1 + 2 = x + k + 3 from rfl]
      apply GridQ.cancel K O F (Y (x + k + 1)) _ _ (hY _) (hpos (x + k) (Nat.lt_trans (Nat.lt_succ_self _) hlt))
      have s1 := O.mul_le_mul_right _ _ _ i1 (hY (x + k + 2))
      have s2 := O.mul_le_mul_right _ _ _ i2 (hY (x + 1))
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
        (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ ?_)))) <;> lib_ring

theorem Del_nn_t (F : OField.OF K O) (Y : Nat → R) (hY : ∀ j, O.le K.zero (Y j)) (L : Nat)
    (hpos : ∀ j, j + 1 < L → Y (j + 1) ≠ K.zero) (hz : ∀ j, L ≤ j → Y j = K.zero)
    (hv : ∀ m, HalfRatio.VV K O Y m) (d : Nat) : ∀ ℓ, O.le K.zero (Del K Y d ℓ)
  | 0 => by rw [Del_zero]; exact O.le_refl _
  | ℓ + 1 => by
    rw [Del_succ]
    have ih := Del_nn_t F Y hY L hpos hz hv d ℓ
    have hs : ∀ n, n ≤ ℓ + 1 → O.le K.zero (sumN K (fun i => Dl K Y (d + 2 * i) (d + 2 * ℓ + 1)) n) := by
      intro n; induction n with
      | zero => intro _; exact O.le_refl _
      | succ n ihn =>
        intro hn
        have hi : d + 2 * n ≤ d + 2 * ℓ + 1 := by
          have : n ≤ ℓ := Nat.le_of_succ_le_succ hn
          exact Nat.le_trans (Nat.add_le_add_left (Nat.mul_le_mul_left 2 this) d) (Nat.le_succ _)
        have ⟨k, e⟩ := L1Zero.ex_add _ _ hi
        have t := dl_nn_t K O F Y hY L hpos hz hv (d + 2 * n) k
        rw [← e] at t
        have := O.add_le_add _ _ _ _ (ihn (Nat.le_of_succ_le hn)) t
        rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
    have := O.add_le_add _ _ _ _ ih (hs (ℓ + 1) (Nat.le_refl _))
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this

end Window

#print axioms Window.P_shift
#print axioms Window.inner_sum
#print axioms Window.Del_succ
#print axioms Window.dl_nn
#print axioms Window.Del_nn
#print axioms Window.heat_Del_nn
#print axioms Window.head_vs_next
#print axioms Window.head_mono
#print axioms Window.dl_nn_t
#print axioms Window.Del_nn_t
#print axioms Window.P_succ
#print axioms Window.Del_zero

#print axioms: 12 theorems, no axioms.

90.82 Bond moving preserves the likelihood-ratio order

For \(v\)-monotone lists \(X=Y\cdot g\) with \(g\) nonincreasing, the bond-moving products satisfy \(X\cdot X\preceq _{LR}Y\cdot Y\).

\[ (X\!\cdot \! X)_{n+1}(Y\!\cdot \! Y)_n\ \le \ (X\!\cdot \! X)_n(Y\!\cdot \! Y)_{n+1}\qquad \text {for all }n \]

ym/L1Full.lean

/-
  ym/L1Full — T45, G44-L1 assembly for equal-length lists. Pairwise row comparisons from consecutive ones (zero rows
  form a prefix), and the one-factor step X·B ≼_LR Y·B for any v-monotone list B, every level n.
  Lean 4 core only, no axioms.
-/
import ym.OneStep
import ym.CgSplit

namespace L1Full

open YMChain LinkIntegral SU2Char QBinom HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- pairwise from consecutive, when the zero rows form a prefix [0, s) -/
theorem pairwise (F : OField.OF K O) (P Q : Nat → R) (hP : ∀ m, O.le K.zero (P m)) (hQ : ∀ m, O.le K.zero (Q m))
    (s M : Nat) (hz : ∀ m, m < s → P m = K.zero ∧ Q m = K.zero) (hne : ∀ m, s ≤ m → m < M → P m ≠ K.zero)
    (hc : ∀ m, O.le (K.mul (Q m) (P (m + 1))) (K.mul (Q (m + 1)) (P m))) :
    ∀ i k, i + k ≤ M → O.le (K.mul (Q i) (P (i + k))) (K.mul (Q (i + k)) (P i))
  | i, 0, _ => by rw [Nat.add_zero, K.mul_comm]; exact O.le_refl _
  | i, k + 1, hk => by
    cases Nat.lt_or_ge i s with
    | inl hi =>
      rw [(hz i hi).1, (hz i hi).2, K.mul_comm K.zero, DetChain.mul_zero, DetChain.mul_zero]; exact O.le_refl _
    | inr hi =>
      have ih := pairwise F P Q hP hQ s M hz hne hc i k (Nat.le_of_succ_le hk)
      have c := hc (i + k)
      show O.le (K.mul (Q i) (P (i + k + 1))) (K.mul (Q (i + k + 1)) (P i))
      apply GridQ.cancel K O F (P (i + k)) _ _ (hP _) (hne _ (Nat.le_trans hi (Nat.le_add_right _ _)) hk)
      have s1 := O.mul_le_mul_right _ _ _ ih (hP (i + k + 1))
      have s2 := O.mul_le_mul_right _ _ _ c (hP i)
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
        (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ ?_)))) <;> lib_ring

theorem P_ge_first (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) (c : Nat) : ∀ l, O.le (f c) (Window.P K f c (l + 1))
  | 0 => by show O.le (f c) (K.add K.zero (f (c + 2 * 0))); rw [show K.add K.zero (f (c + 2 * 0)) = f c by rw [Nat.mul_zero, Nat.add_zero]; lib_ring]; exact O.le_refl _
  | l + 1 => by rw [Window.P_succ]; exact O.le_trans _ _ _ (P_ge_first f hf c l) (HeatV.le_add_nn K O _ _ (hf _))

theorem P_zero_far (b : List R) (c : Nat) (hc : b.length ≤ c) : ∀ l, Window.P K (co K b) c l = K.zero
  | 0 => rfl
  | l + 1 => by
    rw [Window.P_succ, P_zero_far b c hc l, HeatLR.co_out K b _ (Nat.le_trans hc (Nat.le_add_right _ _))]; lib_ring

theorem cancel_add : ∀ m a b : Nat, m + a = m + b → a = b
  | 0, a, b, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | m + 1, a, b, h => cancel_add m a b (Nat.succ.inj (by show m + a + 1 = m + b + 1; rw [show m + a + 1 = m + 1 + a by lib_nat, show m + b + 1 = m + 1 + b by lib_nat]; exact h))

theorem cancel_le : ∀ m a b : Nat, m + a ≤ m + b → a ≤ b
  | 0, a, b, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | m + 1, a, b, h => cancel_le m a b (Nat.le_of_succ_le_succ (by show m + a + 1 ≤ m + b + 1; rw [show m + a + 1 = m + 1 + a by lib_nat, show m + b + 1 = m + 1 + b by lib_nat]; exact h))

/-- row sums are ≥ the first window entry, and vanish when the window starts past the list -/
theorem Psi_ge (b : List R) (hb : ∀ j, O.le K.zero (co K b j)) (m n : Nat) :
    (m ≤ n → ∀ e, n = m + e → O.le (co K b e) (CgWindow.Psi K b m n)) ∧
    (n ≤ m → ∀ d, m = n + d → O.le (co K b d) (CgWindow.Psi K b m n)) := by
  constructor
  · intro _ e he; subst he; rw [CgWindow.Psi_lo]; exact P_ge_first K O _ hb e m
  · intro _ d hd; subst hd; rw [CgWindow.Psi_hi]; exact P_ge_first K O _ hb d n

theorem ne_of_ge (x y : R) (hx : O.le K.zero x) (hxy : O.le x y) (hne : x ≠ K.zero) : y ≠ K.zero := by
  intro e; rw [e] at hxy; exact hne (O.le_antisymm _ _ hxy hx)

/-- **one bond-moving factor, equal lengths**: X·B ≼_LR Y·B at every level n, for B any v-monotone list
  at least as long as X, Y -/
theorem one_step_eq (F : OField.OF K O) (B : List R) (hb : ∀ j, O.le K.zero (co K B j))
    (hpos : ∀ j, j < B.length → co K B j ≠ K.zero) (hv : ∀ m, HalfRatio.VV K O (co K B) m)
    (h0 : O.le (K.mul (co K B 1) (co K B 1)) (K.mul (K.add (co K B 0) (co K B 2)) (co K B 0)))
    (M : Nat) (hM : M + 1 ≤ B.length) (Xl Yl : List R) (hX : Xl.length = M + 1) (hYl : Yl.length = M + 1)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K Yl k) (g k)) (hy : ∀ k, O.le K.zero (co K Yl k)) (n : Nat) :
    O.le (K.mul (cgc K Xl B (n + 1)) (cgc K Yl B n)) (K.mul (cgc K Xl B n) (cgc K Yl B (n + 1))) := by
  let Pm := fun m => CgWindow.Psi K B m n
  let Qm := fun m => CgWindow.Psi K B m (n + 1)
  have hP : ∀ m, O.le K.zero (Pm m) := fun m => OneStep.Psi_nn' K O B hb m n
  have hQ : ∀ m, O.le K.zero (Qm m) := fun m => OneStep.Psi_nn' K O B hb m (n + 1)
  have hc : ∀ m, O.le (K.mul (Qm m) (Pm (m + 1))) (K.mul (Qm (m + 1)) (Pm m)) := fun m => by
    have d := OneStep.det_nn_t K O F B hb hpos hv h0 m n
    have d' : O.le K.zero (K.add (K.mul (Qm (m + 1)) (Pm m)) (K.neg (K.mul (Qm m) (Pm (m + 1))))) := d
    have := O.add_le_add _ _ _ _ d' (O.le_refl (K.mul (Qm m) (Pm (m + 1))))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  -- zero prefix
  have nonz : ∀ m, m ≤ M → (∀ e, n = m + e → e < B.length) → Pm m ≠ K.zero := by
    intro m hm hst
    cases Nat.lt_or_ge n m with
    | inl hl =>
      have ⟨d, hd⟩ := L1Zero.ex_add n m (Nat.le_of_lt hl)
      have g1 := (Psi_ge K O B hb m n).2 (Nat.le_of_lt hl) d hd
      have hdm : d ≤ m := by rw [hd]; exact Nat.le_add_left d n
      exact ne_of_ge K O _ _ (hb d) g1 (hpos d (Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_trans hdm hm)) hM))
    | inr hg =>
      have ⟨e, he⟩ := L1Zero.ex_add m n hg
      have g1 := (Psi_ge K O B hb m n).1 hg e he
      exact ne_of_ge K O _ _ (hb e) g1 (hpos e (hst e he))
  have zer : ∀ m e, B.length ≤ e → CgWindow.Psi K B m (m + e) = K.zero := by
    intro m e h
    rw [CgWindow.Psi_lo]; exact P_zero_far K B e h (m + 1)
  have nz : ∃ s, (∀ m, m < s → Pm m = K.zero ∧ Qm m = K.zero) ∧ (∀ m, s ≤ m → m ≤ M → Pm m ≠ K.zero) := by
    cases Nat.lt_or_ge n B.length with
    | inl hl =>
      refine ⟨0, fun m h => absurd h (Nat.not_lt_zero _), fun m _ hm => nonz m hm (fun e he => ?_)⟩
      exact Nat.lt_of_le_of_lt (by rw [he]; exact Nat.le_add_left e m) hl
    | inr hg =>
      have ⟨t, ht⟩ := L1Zero.ex_add B.length n hg
      refine ⟨t + 1, fun m hm => ?_, fun m hs hm => nonz m hm (fun e he => ?_)⟩
      · have ⟨u, hu⟩ := L1Zero.ex_add m t (Nat.le_of_lt_succ hm)
        have e1 : n = m + (B.length + u) := by rw [ht, hu]; lib_nat
        have e2 : n + 1 = m + (B.length + u + 1) := by rw [e1]; lib_nat
        refine ⟨?_, ?_⟩
        · show CgWindow.Psi K B m n = K.zero
          rw [e1]; exact zer m _ (Nat.le_add_right _ _)
        · show CgWindow.Psi K B m (n + 1) = K.zero
          rw [e2]; exact zer m _ (Nat.le_trans (Nat.le_add_right _ u) (Nat.le_succ _))
      · have ⟨v, hv'⟩ := L1Zero.ex_add (t + 1) m hs
        have q : t + (v + e + 1) = t + B.length := by
          rw [show t + (v + e + 1) = (t + 1 + v) + e by lib_nat, ← hv', ← he, ht]; lib_nat
        have q2 := cancel_add t _ _ q
        rw [← q2]; exact Nat.lt_succ_of_le (Nat.le_add_left e v)
  have ⟨s, hz, hne⟩ := nz
  have pw := pairwise K O F Pm Qm hP hQ s M hz (fun m hs hm => hne m hs (Nat.le_of_lt hm)) hc
  have of := OneFactor.one_factor_pw K O (fun k => K.mul (co K Yl k) (Qm k)) (fun k => K.mul (co K Yl k) (Pm k)) g (M + 1)
    (fun i j hij hj => by
      have ⟨k, e⟩ := L1Zero.ex_add i j (Nat.le_of_lt hij)
      have t := pw i k (by rw [← e]; exact Nat.le_of_lt_succ hj)
      rw [← e] at t
      have u := O.mul_le_mul_right _ _ _ t (O.mul_nonneg _ _ (hy i) (hy j))
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ u (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring)
    hg hg0
  rw [OneStep.cgc_sum K Xl B M (n + 1) hX, OneStep.cgc_sum K Yl B M n hYl, OneStep.cgc_sum K Xl B M n hX,
    OneStep.cgc_sum K Yl B M (n + 1) hYl]
  have ex : ∀ t, sumN K (fun j => K.mul (co K Xl j) (CgWindow.Psi K B j t)) (M + 1)
      = sumN K (fun k => K.mul (K.mul (co K Yl k) (CgWindow.Psi K B k t)) (g k)) (M + 1) :=
    fun t => sumN_ext K _ _ (fun k => by rw [hxy]; lib_ring) _
  rw [ex, ex]
  exact of

theorem dist_symm : ∀ j k, CgSplit.dist j k = CgSplit.dist k j
  | 0, 0 => rfl
  | 0, k + 1 => rfl
  | j + 1, 0 => rfl
  | j + 1, k + 1 => dist_symm j k

theorem cancel_add_r : ∀ c a b : Nat, a + c = b + c → a = b
  | c, a, b, h => cancel_add c a b (by rw [Nat.add_comm c a, Nat.add_comm c b]; exact h)

theorem cg_comm (j k n : Nat) : Fusion.cg j k n = Fusion.cg k j n := by
  have h1 := CgSplit.cg_split j k n
  have h2 := CgSplit.cg_split k j n
  rw [dist_symm k j, Nat.add_comm k j, ← h1] at h2
  exact (cancel_add_r _ _ _ h2).symm

theorem cgc_comm (a b : List R) (n : Nat) : cgc K a b n = cgc K b a n := by
  show dot K 0 a (fun j => dot K 0 b (fun k => cN K (Fusion.cg j k n))) = dot K 0 b (fun j => dot K 0 a (fun k => cN K (Fusion.cg j k n)))
  rw [dot_swap K (fun j k => cN K (Fusion.cg j k n))]
  exact dot_ext K _ _ (fun k => dot_ext K _ _ (fun j => by rw [cg_comm]) 0 a) 0 b

/-- a list is "v-good": nonnegative, nonzero within its length, truncated coefficient function v-monotone, base condition -/
def Good (b : List R) : Prop :=
  (∀ j, O.le K.zero (co K b j)) ∧ (∀ j, j < b.length → co K b j ≠ K.zero) ∧ (∀ m, HalfRatio.VV K O (co K b) m) ∧
    O.le (K.mul (co K b 1) (co K b 1)) (K.mul (K.add (co K b 0) (co K b 2)) (co K b 0))

/-- **L1 (bond moving preserves the LR comparison)**: X = Y·g with g nonincreasing, X and Y v-good of equal length:
  X·X ≼_LR Y·Y at level n (wherever (X·Y)_n ≠ 0) -/
theorem l1 (F : OField.OF K O) (Xl Yl : List R) (hXg : Good K O Xl) (hYg : Good K O Yl) (M : Nat)
    (hX : Xl.length = M + 1) (hYl : Yl.length = M + 1)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K Yl k) (g k)) (n : Nat) (hBn : cgc K Xl Yl n ≠ K.zero) :
    O.le (K.mul (cgc K Xl Xl (n + 1)) (cgc K Yl Yl n)) (K.mul (cgc K Xl Xl n) (cgc K Yl Yl (n + 1))) := by
  have ⟨xb, xp, xv, x0⟩ := hXg
  have ⟨yb, yp, yv, y0⟩ := hYg
  -- step 1: fixed factor X
  have s1 := one_step_eq K O F Xl xb xp xv x0 M (by rw [hX]; exact Nat.le_refl _) Xl Yl hX hYl g hg hg0 hxy yb n
  -- step 2: fixed factor Y
  have s2 := one_step_eq K O F Yl yb yp yv y0 M (by rw [hYl]; exact Nat.le_refl _) Xl Yl hX hYl g hg hg0 hxy yb n
  rw [cgc_comm K Yl Xl, cgc_comm K Yl Xl] at s1
  have nnB : ∀ t, O.le K.zero (cgc K Xl Yl t) := fun t => by
    rw [OneStep.cgc_sum K Xl Yl M t hX]
    exact OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (xb j) (OneStep.Psi_nn' K O Yl yb j t)) _
  have nnX : ∀ t, O.le K.zero (cgc K Xl Xl t) := fun t => by
    rw [OneStep.cgc_sum K Xl Xl M t hX]
    exact OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (xb j) (OneStep.Psi_nn' K O Xl xb j t)) _
  have nnY : ∀ t, O.le K.zero (cgc K Yl Yl t) := fun t => by
    rw [OneStep.cgc_sum K Yl Yl M t hYl]
    exact OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (yb j) (OneStep.Psi_nn' K O Yl yb j t)) _
  apply GridQ.cancel K O F (cgc K Xl Yl n) _ _ (nnB n) hBn
  have t1 := O.mul_le_mul_right _ _ _ s1 (nnY n)
  have t2 := O.mul_le_mul_right _ _ _ s2 (nnX n)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ t1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
    (O.le_trans _ _ _ t2 (TripleE.le_of_eq' K O _ _ ?_)))) <;> lib_ring

/-- (X·Y)_n ≥ X_0 Y_n, so it is nonzero for n within the list length -/
theorem cgc_ge (Xl Yl : List R) (M : Nat) (hX : Xl.length = M + 1) (hx : ∀ j, O.le K.zero (co K Xl j))
    (hy : ∀ j, O.le K.zero (co K Yl j)) (n : Nat) : O.le (K.mul (co K Xl 0) (co K Yl n)) (cgc K Xl Yl n) := by
  rw [OneStep.cgc_sum K Xl Yl M n hX, sumN_shift]
  have e : CgWindow.Psi K Yl 0 (0 + n) = Window.P K (co K Yl) n 1 := CgWindow.Psi_lo K Yl 0 n
  rw [Nat.zero_add] at e
  rw [e]
  have h1 : Window.P K (co K Yl) n 1 = co K Yl n := by show K.add K.zero (co K Yl (n + 2 * 0)) = _; rw [Nat.mul_zero, Nat.add_zero]; lib_ring
  rw [h1]
  exact HeatV.le_add_nn K O _ _ (OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (hx _) (OneStep.Psi_nn' K O Yl hy _ n)) M)

/-- **L1 at every level inside the list length** -/
theorem l1_in (F : OField.OF K O) (Xl Yl : List R) (hXg : Good K O Xl) (hYg : Good K O Yl) (M : Nat)
    (hX : Xl.length = M + 1) (hYl : Yl.length = M + 1)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K Yl k) (g k)) (n : Nat) (hn : n ≤ M) :
    O.le (K.mul (cgc K Xl Xl (n + 1)) (cgc K Yl Yl n)) (K.mul (cgc K Xl Xl n) (cgc K Yl Yl (n + 1))) := by
  have ⟨xb, xp, _, _⟩ := hXg
  have ⟨yb, yp, _, _⟩ := hYg
  have lo := cgc_ge K O Xl Yl M hX xb yb n
  have pos : K.mul (co K Xl 0) (co K Yl n) ≠ K.zero := by
    intro z
    have x0 : co K Xl 0 ≠ K.zero := xp 0 (by rw [hX]; exact Nat.zero_lt_succ _)
    exact yp n (by rw [hYl]; exact Nat.lt_succ_of_le hn) (O.pos_mul_eq_zero _ _ (xb 0) x0 z)
  exact l1 K O F Xl Yl hXg hYg M hX hYl g hg hg0 hxy n (ne_of_ge K O _ _ (O.mul_nonneg _ _ (xb 0) (yb n)) lo pos)

/-- **CG row recursion** (χ₁ acting on either factor): Ψ_{m+1}(j+2) + Ψ_{m+1}(j) = Ψ_m(j+1) + Ψ_{m+2}(j+1) -/
theorem Psi_rec (b : List R) (j m : Nat) :
    K.add (CgWindow.Psi K b (j + 2) (m + 1)) (CgWindow.Psi K b j (m + 1))
      = K.add (CgWindow.Psi K b (j + 1) m) (CgWindow.Psi K b (j + 1) (m + 2)) := by
  cases Nat.lt_or_ge j m with
  | inl hl =>
    have ⟨c, hc⟩ := L1Zero.ex_add (j + 1) m hl
    subst hc
    have e1 : CgWindow.Psi K b (j + 2) (j + 1 + c + 1) = Window.P K (co K b) c (j + 3) := by
      rw [show j + 1 + c + 1 = (j + 2) + c by lib_nat, CgWindow.Psi_lo]
    have e2 : CgWindow.Psi K b j (j + 1 + c + 1) = Window.P K (co K b) (c + 2) (j + 1) := by
      rw [show j + 1 + c + 1 = j + (c + 2) by lib_nat, CgWindow.Psi_lo]
    have e3 : CgWindow.Psi K b (j + 1) (j + 1 + c) = Window.P K (co K b) c (j + 2) := by
      rw [CgWindow.Psi_lo]
    have e4 : CgWindow.Psi K b (j + 1) (j + 1 + c + 2) = Window.P K (co K b) (c + 2) (j + 2) := by
      rw [show j + 1 + c + 2 = (j + 1) + (c + 2) by lib_nat, CgWindow.Psi_lo]
    rw [e1, e2, e3, e4, show j + 3 = (j + 2) + 1 from rfl, Window.P_shift K _ c (j + 2), Window.P_shift K _ c (j + 1)]
    lib_ring
  | inr hg =>
    cases Nat.lt_or_ge m j with
    | inl hl =>
      have ⟨d, hd⟩ := L1Zero.ex_add (m + 1) j hl
      subst hd
      have e1 : CgWindow.Psi K b (m + 1 + d + 2) (m + 1) = Window.P K (co K b) (d + 2) (m + 2) := by
        rw [show m + 1 + d + 2 = (m + 1) + (d + 2) by lib_nat, CgWindow.Psi_hi]
      have e2 : CgWindow.Psi K b (m + 1 + d) (m + 1) = Window.P K (co K b) d (m + 2) := by
        rw [CgWindow.Psi_hi]
      have e3 : CgWindow.Psi K b (m + 1 + d + 1) m = Window.P K (co K b) (d + 2) (m + 1) := by
        rw [show m + 1 + d + 1 = m + (d + 2) by lib_nat, CgWindow.Psi_hi]
      have e4 : CgWindow.Psi K b (m + 1 + d + 1) (m + 2) = Window.P K (co K b) d (m + 3) := by
        rw [show m + 1 + d + 1 = (m + 2) + d by lib_nat, CgWindow.Psi_hi]
      rw [e1, e2, e3, e4, show m + 3 = (m + 2) + 1 from rfl, Window.P_shift K _ d (m + 2), Window.P_shift K _ d (m + 1)]
      lib_ring
    | inr hg2 =>
      have e : j = m := Nat.le_antisymm hg2 hg
      subst e
      have e1 : CgWindow.Psi K b (j + 2) (j + 1) = Window.P K (co K b) 1 (j + 2) := by
        rw [show j + 2 = (j + 1) + 1 from rfl, CgWindow.Psi_hi]
      have e2 : CgWindow.Psi K b j (j + 1) = Window.P K (co K b) 1 (j + 1) := CgWindow.Psi_lo K b j 1
      have e3 : CgWindow.Psi K b (j + 1) j = Window.P K (co K b) 1 (j + 1) := CgWindow.Psi_hi K b j 1
      have e4 : CgWindow.Psi K b (j + 1) (j + 2) = Window.P K (co K b) 1 (j + 2) := CgWindow.Psi_lo K b (j + 1) 1
      rw [e1, e2, e3, e4]; lib_ring

/-- χ₁ times a coefficient function: (χ₁x)_0 = x_1, (χ₁x)_{k+1} = x_k + x_{k+2} -/
def chi1f (x : Nat → R) : Nat → R
  | 0 => x 1
  | k + 1 => K.add (x k) (x (k + 2))

theorem Psi_rec0 (b : List R) (m : Nat) :
    CgWindow.Psi K b 1 (m + 1) = K.add (CgWindow.Psi K b 0 m) (CgWindow.Psi K b 0 (m + 2)) := by
  have e1 : CgWindow.Psi K b 1 (m + 1) = Window.P K (co K b) m 2 := by
    have := CgWindow.Psi_lo K b 1 m; rw [Nat.add_comm 1 m] at this; exact this
  have e2 : CgWindow.Psi K b 0 m = Window.P K (co K b) m 1 := by
    have := CgWindow.Psi_lo K b 0 m; rw [Nat.zero_add] at this; exact this
  have e3 : CgWindow.Psi K b 0 (m + 2) = Window.P K (co K b) (m + 2) 1 := by
    have := CgWindow.Psi_lo K b 0 (m + 2); rw [Nat.zero_add] at this; exact this
  have p1 : ∀ c, Window.P K (co K b) c 1 = co K b c := fun c => by
    show K.add K.zero (co K b (c + 2 * 0)) = _; rw [Nat.mul_zero, Nat.add_zero]; lib_ring
  rw [e1, e2, e3, Window.P_shift K _ m 1, p1, p1]

/-- **χ₁ moves between the factors** (sum form) -/
theorem chi1_sum (b : List R) (x : Nat → R) (M m : Nat) (hz1 : x (M + 1) = K.zero) (hz2 : x (M + 2) = K.zero) :
    sumN K (fun k => K.mul (chi1f K x k) (CgWindow.Psi K b k (m + 1))) (M + 2)
      = sumN K (fun j => K.mul (x j) (K.add (CgWindow.Psi K b j m) (CgWindow.Psi K b j (m + 2)))) (M + 1) := by
  -- Ψ' k = Ψ_{m+1}(k)
  have hrec : ∀ j, K.add (CgWindow.Psi K b (j + 1) m) (CgWindow.Psi K b (j + 1) (m + 2))
      = K.add (CgWindow.Psi K b (j + 2) (m + 1)) (CgWindow.Psi K b j (m + 1)) := fun j => (Psi_rec K b j m).symm
  rw [sumN_shift K _ (M + 1), sumN_shift K (fun j => K.mul (x j) (K.add (CgWindow.Psi K b j m) (CgWindow.Psi K b j (m + 2)))) M]
  show K.add (K.mul (x 1) (CgWindow.Psi K b 0 (m + 1)))
      (sumN K (fun k => K.mul (K.add (x k) (x (k + 2))) (CgWindow.Psi K b (k + 1) (m + 1))) (M + 1)) = _
  rw [sumN_ext K _ (fun k => K.add (K.mul (x k) (CgWindow.Psi K b (k + 1) (m + 1))) (K.mul (x (k + 2)) (CgWindow.Psi K b (k + 1) (m + 1))))
      (fun k => by lib_ring) (M + 1), sumN_add, sumN_shift K (fun k => K.mul (x k) (CgWindow.Psi K b (k + 1) (m + 1))) M]
  rw [sumN_ext K (fun k => K.mul (x (k + 1)) (K.add (CgWindow.Psi K b (k + 1) m) (CgWindow.Psi K b (k + 1) (m + 2))))
      (fun k => K.add (K.mul (x (k + 1)) (CgWindow.Psi K b (k + 2) (m + 1))) (K.mul (x (k + 1)) (CgWindow.Psi K b k (m + 1))))
      (fun k => by rw [hrec]; lib_ring) M, sumN_add, ← Psi_rec0]
  -- remaining: x_1 Ψ'(0) + Σ_{k<M+1} x_{k+2} Ψ'(k+1) = Σ_{j<M} x_{j+1} Ψ'(j)  (up to the common terms)
  have key : K.add (K.mul (x 1) (CgWindow.Psi K b 0 (m + 1))) (sumN K (fun k => K.mul (x (k + 2)) (CgWindow.Psi K b (k + 1) (m + 1))) (M + 1))
      = sumN K (fun k => K.mul (x (k + 1)) (CgWindow.Psi K b k (m + 1))) M := by
    cases M with
    | zero =>
      show K.add (K.mul (x 1) _) (K.add K.zero (K.mul (x (0 + 2)) _)) = K.zero
      rw [show (0 : Nat) + 2 = 2 from rfl, show x 1 = x (0 + 1) from rfl, hz1, hz2]; lib_ring
    | succ M' =>
      rw [sumN_shift K (fun k => K.mul (x (k + 1)) (CgWindow.Psi K b k (m + 1))) M']
      show K.add _ (K.add (K.add (sumN K (fun k => K.mul (x (k + 2)) (CgWindow.Psi K b (k + 1) (m + 1))) M')
          (K.mul (x (M' + 2)) (CgWindow.Psi K b (M' + 1) (m + 1)))) (K.mul (x (M' + 1 + 2)) (CgWindow.Psi K b (M' + 1 + 1) (m + 1))))
        = K.add (K.mul (x 1) (CgWindow.Psi K b 0 (m + 1))) (sumN K (fun k => K.mul (x (k + 2)) (CgWindow.Psi K b (k + 1) (m + 1))) M')
      rw [show M' + 2 = M' + 1 + 1 from rfl, hz1, show M' + 1 + 2 = M' + 1 + 2 from rfl, hz2]
      lib_ring
  rw [← key]
  have eS : sumN K (fun k => K.mul (x (k + 1)) (CgWindow.Psi K b (k + 1 + 1) (m + 1))) M
      = sumN K (fun k => K.mul (x (k + 1)) (CgWindow.Psi K b (k + 2) (m + 1))) M := rfl
  rw [eS, show (0 : Nat) + 1 = 1 from rfl]
  lib_ring

/-- **general one-factor step** (weights y over M+1 rows, fixed v-good factor B, M ≤ n + |B|) -/
theorem one_step_gen (F : OField.OF K O) (B : List R) (hBg : Good K O B) (M n : Nat) (hM : M ≤ n + B.length)
    (y g : Nat → R) (hy : ∀ k, O.le K.zero (y k)) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m)) :
    O.le (K.mul (sumN K (fun k => K.mul (K.mul (y k) (CgWindow.Psi K B k (n + 1))) (g k)) (M + 1))
        (sumN K (fun k => K.mul (y k) (CgWindow.Psi K B k n)) (M + 1)))
      (K.mul (sumN K (fun k => K.mul (K.mul (y k) (CgWindow.Psi K B k n)) (g k)) (M + 1))
        (sumN K (fun k => K.mul (y k) (CgWindow.Psi K B k (n + 1))) (M + 1))) := by
  have ⟨hb, hpos, hv, h0⟩ := hBg
  let Pm := fun m => CgWindow.Psi K B m n
  let Qm := fun m => CgWindow.Psi K B m (n + 1)
  have hP : ∀ m, O.le K.zero (Pm m) := fun m => OneStep.Psi_nn' K O B hb m n
  have hQ : ∀ m, O.le K.zero (Qm m) := fun m => OneStep.Psi_nn' K O B hb m (n + 1)
  have hc : ∀ m, O.le (K.mul (Qm m) (Pm (m + 1))) (K.mul (Qm (m + 1)) (Pm m)) := fun m => by
    have d := OneStep.det_nn_t K O F B hb hpos hv h0 m n
    have d' : O.le K.zero (K.add (K.mul (Qm (m + 1)) (Pm m)) (K.neg (K.mul (Qm m) (Pm (m + 1))))) := d
    have := O.add_le_add _ _ _ _ d' (O.le_refl (K.mul (Qm m) (Pm (m + 1))))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  have nonz : ∀ m, m < M → (∀ e, n = m + e → e < B.length) → Pm m ≠ K.zero := by
    intro m hm hst
    cases Nat.lt_or_ge n m with
    | inl hl =>
      have ⟨d, hd⟩ := L1Zero.ex_add n m (Nat.le_of_lt hl)
      have g1 := (Psi_ge K O B hb m n).2 (Nat.le_of_lt hl) d hd
      have hdl : d < B.length := by
        have : n + d < n + B.length := by rw [← hd]; exact Nat.lt_of_lt_of_le hm hM
        exact Nat.lt_of_add_lt_add_left this
      exact ne_of_ge K O _ _ (hb d) g1 (hpos d hdl)
    | inr hg' =>
      have ⟨e, he⟩ := L1Zero.ex_add m n hg'
      have g1 := (Psi_ge K O B hb m n).1 hg' e he
      exact ne_of_ge K O _ _ (hb e) g1 (hpos e (hst e he))
  have zer : ∀ m e, B.length ≤ e → CgWindow.Psi K B m (m + e) = K.zero := by
    intro m e h
    rw [CgWindow.Psi_lo]; exact P_zero_far K B e h (m + 1)
  have nz : ∃ s, (∀ m, m < s → Pm m = K.zero ∧ Qm m = K.zero) ∧ (∀ m, s ≤ m → m < M → Pm m ≠ K.zero) := by
    cases Nat.lt_or_ge n B.length with
    | inl hl =>
      refine ⟨0, fun m h => absurd h (Nat.not_lt_zero _), fun m _ hm => nonz m hm (fun e he => ?_)⟩
      exact Nat.lt_of_le_of_lt (by rw [he]; exact Nat.le_add_left e m) hl
    | inr hg' =>
      have ⟨t, ht⟩ := L1Zero.ex_add B.length n hg'
      refine ⟨t + 1, fun m hm => ?_, fun m hs hm => nonz m hm (fun e he => ?_)⟩
      · have ⟨u, hu⟩ := L1Zero.ex_add m t (Nat.le_of_lt_succ hm)
        have e1 : n = m + (B.length + u) := by rw [ht, hu]; lib_nat
        have e2 : n + 1 = m + (B.length + u + 1) := by rw [e1]; lib_nat
        refine ⟨?_, ?_⟩
        · show CgWindow.Psi K B m n = K.zero
          rw [e1]; exact zer m _ (Nat.le_add_right _ _)
        · show CgWindow.Psi K B m (n + 1) = K.zero
          rw [e2]; exact zer m _ (Nat.le_trans (Nat.le_add_right _ u) (Nat.le_succ _))
      · have ⟨v, hv'⟩ := L1Zero.ex_add (t + 1) m hs
        have q : t + (v + e + 1) = t + B.length := by
          rw [show t + (v + e + 1) = (t + 1 + v) + e by lib_nat, ← hv', ← he, ht]; lib_nat
        have q2 := cancel_add t _ _ q
        rw [← q2]; exact Nat.lt_succ_of_le (Nat.le_add_left e v)
  have ⟨s, hz, hne⟩ := nz
  have pw := pairwise K O F Pm Qm hP hQ s M hz hne hc
  exact OneFactor.one_factor_pw K O (fun k => K.mul (y k) (Qm k)) (fun k => K.mul (y k) (Pm k)) g (M + 1)
    (fun i j hij hj => by
      have ⟨k, e⟩ := L1Zero.ex_add i j (Nat.le_of_lt hij)
      have t := pw i k (by rw [← e]; exact Nat.le_of_lt_succ hj)
      rw [← e] at t
      have u := O.mul_le_mul_right _ _ _ t (O.mul_nonneg _ _ (hy i) (hy j))
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ u (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring)
    hg hg0

/-- **bond moving keeps v-monotonicity** (levels m ≥ 0 of VV for c = X·X, i.e. c ≼ χ₁c at n = m + 1) -/
theorem prod_VV (F : OField.OF K O) (X : List R) (hXg : Good K O X) (M : Nat) (hX : X.length = M + 2) (m : Nat) :
    O.le (K.mul (K.add (cgc K X X m) (cgc K X X (m + 2))) (cgc K X X (m + 2)))
      (K.mul (K.add (cgc K X X (m + 1)) (cgc K X X (m + 3))) (cgc K X X (m + 1))) := by
  have ⟨hb, hpos, hv, h0⟩ := hXg
  let x := co K X
  let y := chi1f K x
  have hz : ∀ j, M + 2 ≤ j → x j = K.zero := fun j h => HeatLR.co_out K X j (by rw [hX]; exact h)
  have hxp : ∀ j, j < M + 2 → x j ≠ K.zero := fun j h => hpos j (by rw [hX]; exact h)
  have hy : ∀ k, O.le K.zero (y k) := fun k => by
    cases k with
    | zero => exact hb 1
    | succ k => have := O.add_le_add _ _ _ _ (hb k) (hb (k + 2)); rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  have hyne : ∀ k, k ≤ M + 2 → y k ≠ K.zero := fun k hk => by
    cases k with
    | zero => exact hxp 1 (Nat.succ_lt_succ (Nat.zero_lt_succ _))
    | succ k =>
      intro e
      have lo := HeatV.le_add_nn K O (x k) (x (k + 2)) (hb (k + 2))
      have : y (k + 1) = K.add (x k) (x (k + 2)) := rfl
      rw [← this, e] at lo
      exact hxp k (Nat.lt_of_lt_of_le (Nat.lt_succ_self k) hk) (O.le_antisymm _ _ lo (hb k))
  let g := fun k => K.mul (x k) (F.inv (y k))
  have hg0 : ∀ k, O.le K.zero (g k) := fun k => by
    cases Nat.lt_or_ge k (M + 3) with
    | inl h => exact O.mul_nonneg _ _ (hb k) (F.inv_pos _ (hy k) (hyne k (Nat.le_of_lt_succ h)))
    | inr h => show O.le K.zero (K.mul (x k) _); rw [hz k (Nat.le_trans (Nat.le_succ _) h)]; rw [K.mul_comm, DetChain.mul_zero]; exact O.le_refl _
  have gy : ∀ k, k ≤ M + 2 → K.mul (y k) (g k) = x k := fun k hk => by
    show K.mul (y k) (K.mul (x k) (F.inv (y k))) = x k
    rw [show K.mul (y k) (K.mul (x k) (F.inv (y k))) = K.mul (x k) (K.mul (y k) (F.inv (y k))) by lib_ring, F.mul_inv _ (hyne k hk)]; lib_ring
  have hg : ∀ k, O.le (g (k + 1)) (g k) := fun k => by
    cases Nat.lt_or_ge (k + 1) (M + 2) with
    | inr h => show O.le (K.mul (x (k + 1)) _) _; rw [hz (k + 1) h, K.mul_comm, DetChain.mul_zero]; exact hg0 k
    | inl h =>
      -- cross form x_{k+1} y_k ≤ x_k y_{k+1}
      have cross : O.le (K.mul (x (k + 1)) (y k)) (K.mul (x k) (y (k + 1))) := by
        cases k with
        | zero => show O.le (K.mul (x 1) (x 1)) (K.mul (x 0) (K.add (x 0) (x 2))); rw [K.mul_comm (x 0)]; exact h0
        | succ j =>
          have := hv j
          unfold HalfRatio.VV at this
          show O.le (K.mul (x (j + 2)) (K.add (x j) (x (j + 2)))) (K.mul (x (j + 1)) (K.add (x (j + 1)) (x (j + 3))))
          rw [K.mul_comm (x (j + 2)), K.mul_comm (x (j + 1))]; exact this
      have iy := F.inv_pos _ (hy k) (hyne k (Nat.le_of_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt h))))
      have iy1 := F.inv_pos _ (hy (k + 1)) (hyne (k + 1) (Nat.le_of_lt h))
      have t := O.mul_le_mul_right _ _ _ cross (O.mul_nonneg _ _ iy iy1)
      have e1 : K.mul (K.mul (x (k + 1)) (y k)) (K.mul (F.inv (y k)) (F.inv (y (k + 1)))) = g (k + 1) := by
        show _ = K.mul (x (k + 1)) (F.inv (y (k + 1)))
        rw [show K.mul (K.mul (x (k + 1)) (y k)) (K.mul (F.inv (y k)) (F.inv (y (k + 1))))
          = K.mul (K.mul (x (k + 1)) (F.inv (y (k + 1)))) (K.mul (y k) (F.inv (y k))) by lib_ring,
          F.mul_inv _ (hyne k (Nat.le_of_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt h))))]; lib_ring
      have e2 : K.mul (K.mul (x k) (y (k + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1)))) = g k := by
        show _ = K.mul (x k) (F.inv (y k))
        rw [show K.mul (K.mul (x k) (y (k + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1))))
          = K.mul (K.mul (x k) (F.inv (y k))) (K.mul (y (k + 1)) (F.inv (y (k + 1)))) by lib_ring,
          F.mul_inv _ (hyne (k + 1) (Nat.le_of_lt h))]; lib_ring
      rw [e1, e2] at t; exact t
  have os := one_step_gen K O F X hXg (M + 2) (m + 1) (by rw [hX]; exact Nat.le_add_left _ _) y g hy hg hg0
  have cg_eq : ∀ t, sumN K (fun k => K.mul (K.mul (y k) (CgWindow.Psi K X k t)) (g k)) (M + 3) = cgc K X X t := fun t => by
    rw [HalfRatio.sum_congr K _ (fun k => K.mul (x k) (CgWindow.Psi K X k t)) (M + 3) (fun k hk => by
      show K.mul (K.mul (y k) _) (g k) = _
      rw [show K.mul (K.mul (y k) (CgWindow.Psi K X k t)) (g k) = K.mul (K.mul (y k) (g k)) (CgWindow.Psi K X k t) by lib_ring,
        gy k (Nat.le_of_lt_succ hk)]),
      OneStep.cgc_sum K X X (M + 1) t hX]
    show K.add (sumN K (fun k => K.mul (co K X k) (CgWindow.Psi K X k t)) (M + 2)) (K.mul (co K X (M + 2)) (CgWindow.Psi K X (M + 2) t))
      = sumN K (fun j => K.mul (co K X j) (CgWindow.Psi K X j t)) (M + 2)
    rw [HeatLR.co_out K X (M + 2) (by rw [hX]; exact Nat.le_refl _)]; lib_ring
  have ch : ∀ t, sumN K (fun k => K.mul (y k) (CgWindow.Psi K X k (t + 1))) (M + 3) = K.add (cgc K X X t) (cgc K X X (t + 2)) := fun t => by
    have := chi1_sum K X x (M + 1) t (hz _ (Nat.le_refl _)) (hz _ (Nat.le_succ _))
    rw [this, sumN_ext K _ (fun j => K.add (K.mul (x j) (CgWindow.Psi K X j t)) (K.mul (x j) (CgWindow.Psi K X j (t + 2)))) (fun j => by lib_ring),
      sumN_add, OneStep.cgc_sum K X X (M + 1) t hX, OneStep.cgc_sum K X X (M + 1) (t + 2) hX]
  rw [cg_eq, cg_eq, ch m, ch (m + 1), show m + 1 + 1 = m + 2 from rfl, show m + 1 + 2 = m + 3 from rfl] at os
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ os (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

/-- **bond moving keeps v-monotonicity** (base condition c_1² ≤ (c_0 + c_2) c_0) -/
theorem prod_base (F : OField.OF K O) (X : List R) (hXg : Good K O X) (M : Nat) (hX : X.length = M + 2) :
    O.le (K.mul (cgc K X X 1) (cgc K X X 1)) (K.mul (K.add (cgc K X X 0) (cgc K X X 2)) (cgc K X X 0)) := by
  have ⟨hb, hpos, hv, h0⟩ := hXg
  let x := co K X
  let y := chi1f K x
  have hz : ∀ j, M + 2 ≤ j → x j = K.zero := fun j h => HeatLR.co_out K X j (by rw [hX]; exact h)
  have hxp : ∀ j, j < M + 2 → x j ≠ K.zero := fun j h => hpos j (by rw [hX]; exact h)
  have hy : ∀ k, O.le K.zero (y k) := fun k => by
    cases k with
    | zero => exact hb 1
    | succ k => have := O.add_le_add _ _ _ _ (hb k) (hb (k + 2)); rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  have hyne : ∀ k, k ≤ M + 2 → y k ≠ K.zero := fun k hk => by
    cases k with
    | zero => exact hxp 1 (Nat.succ_lt_succ (Nat.zero_lt_succ _))
    | succ k =>
      intro e
      have lo := HeatV.le_add_nn K O (x k) (x (k + 2)) (hb (k + 2))
      have : y (k + 1) = K.add (x k) (x (k + 2)) := rfl
      rw [← this, e] at lo
      exact hxp k (Nat.lt_of_lt_of_le (Nat.lt_succ_self k) hk) (O.le_antisymm _ _ lo (hb k))
  let g := fun k => K.mul (x k) (F.inv (y k))
  have hg0 : ∀ k, O.le K.zero (g k) := fun k => by
    cases Nat.lt_or_ge k (M + 3) with
    | inl h => exact O.mul_nonneg _ _ (hb k) (F.inv_pos _ (hy k) (hyne k (Nat.le_of_lt_succ h)))
    | inr h => show O.le K.zero (K.mul (x k) _); rw [hz k (Nat.le_trans (Nat.le_succ _) h)]; rw [K.mul_comm, DetChain.mul_zero]; exact O.le_refl _
  have gy : ∀ k, k ≤ M + 2 → K.mul (y k) (g k) = x k := fun k hk => by
    show K.mul (y k) (K.mul (x k) (F.inv (y k))) = x k
    rw [show K.mul (y k) (K.mul (x k) (F.inv (y k))) = K.mul (x k) (K.mul (y k) (F.inv (y k))) by lib_ring, F.mul_inv _ (hyne k hk)]; lib_ring
  have hg : ∀ k, O.le (g (k + 1)) (g k) := fun k => by
    cases Nat.lt_or_ge (k + 1) (M + 2) with
    | inr h => show O.le (K.mul (x (k + 1)) _) _; rw [hz (k + 1) h, K.mul_comm, DetChain.mul_zero]; exact hg0 k
    | inl h =>
      -- cross form x_{k+1} y_k ≤ x_k y_{k+1}
      have cross : O.le (K.mul (x (k + 1)) (y k)) (K.mul (x k) (y (k + 1))) := by
        cases k with
        | zero => show O.le (K.mul (x 1) (x 1)) (K.mul (x 0) (K.add (x 0) (x 2))); rw [K.mul_comm (x 0)]; exact h0
        | succ j =>
          have := hv j
          unfold HalfRatio.VV at this
          show O.le (K.mul (x (j + 2)) (K.add (x j) (x (j + 2)))) (K.mul (x (j + 1)) (K.add (x (j + 1)) (x (j + 3))))
          rw [K.mul_comm (x (j + 2)), K.mul_comm (x (j + 1))]; exact this
      have iy := F.inv_pos _ (hy k) (hyne k (Nat.le_of_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt h))))
      have iy1 := F.inv_pos _ (hy (k + 1)) (hyne (k + 1) (Nat.le_of_lt h))
      have t := O.mul_le_mul_right _ _ _ cross (O.mul_nonneg _ _ iy iy1)
      have e1 : K.mul (K.mul (x (k + 1)) (y k)) (K.mul (F.inv (y k)) (F.inv (y (k + 1)))) = g (k + 1) := by
        show _ = K.mul (x (k + 1)) (F.inv (y (k + 1)))
        rw [show K.mul (K.mul (x (k + 1)) (y k)) (K.mul (F.inv (y k)) (F.inv (y (k + 1))))
          = K.mul (K.mul (x (k + 1)) (F.inv (y (k + 1)))) (K.mul (y k) (F.inv (y k))) by lib_ring,
          F.mul_inv _ (hyne k (Nat.le_of_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt h))))]; lib_ring
      have e2 : K.mul (K.mul (x k) (y (k + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1)))) = g k := by
        show _ = K.mul (x k) (F.inv (y k))
        rw [show K.mul (K.mul (x k) (y (k + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1))))
          = K.mul (K.mul (x k) (F.inv (y k))) (K.mul (y (k + 1)) (F.inv (y (k + 1)))) by lib_ring,
          F.mul_inv _ (hyne (k + 1) (Nat.le_of_lt h))]; lib_ring
      rw [e1, e2] at t; exact t
  have os := one_step_gen K O F X hXg (M + 2) 0 (by rw [hX]; exact Nat.le_add_left _ _) y g hy hg hg0
  have cg_eq : ∀ t, sumN K (fun k => K.mul (K.mul (y k) (CgWindow.Psi K X k t)) (g k)) (M + 3) = cgc K X X t := fun t => by
    rw [HalfRatio.sum_congr K _ (fun k => K.mul (x k) (CgWindow.Psi K X k t)) (M + 3) (fun k hk => by
      show K.mul (K.mul (y k) _) (g k) = _
      rw [show K.mul (K.mul (y k) (CgWindow.Psi K X k t)) (g k) = K.mul (K.mul (y k) (g k)) (CgWindow.Psi K X k t) by lib_ring,
        gy k (Nat.le_of_lt_succ hk)]),
      OneStep.cgc_sum K X X (M + 1) t hX]
    show K.add (sumN K (fun k => K.mul (co K X k) (CgWindow.Psi K X k t)) (M + 2)) (K.mul (co K X (M + 2)) (CgWindow.Psi K X (M + 2) t))
      = sumN K (fun j => K.mul (co K X j) (CgWindow.Psi K X j t)) (M + 2)
    rw [HeatLR.co_out K X (M + 2) (by rw [hX]; exact Nat.le_refl _)]; lib_ring
  have ch1 : sumN K (fun k => K.mul (y k) (CgWindow.Psi K X k (0 + 1))) (M + 3) = K.add (cgc K X X 0) (cgc K X X 2) := by
    have := chi1_sum K X x (M + 1) 0 (hz _ (Nat.le_refl _)) (hz _ (Nat.le_succ _))
    rw [this, sumN_ext K _ (fun j => K.add (K.mul (x j) (CgWindow.Psi K X j 0)) (K.mul (x j) (CgWindow.Psi K X j (0 + 2)))) (fun j => by lib_ring),
      sumN_add, OneStep.cgc_sum K X X (M + 1) 0 hX, OneStep.cgc_sum K X X (M + 1) (0 + 2) hX]
  -- Ψ_0(k) = x_k and Ψ_1(j) = (χ₁x)_j
  have psi0 : ∀ k, CgWindow.Psi K X k 0 = x k := fun k => by
    have := CgWindow.Psi_hi K X 0 k; rw [Nat.zero_add] at this; rw [this]
    show K.add K.zero (co K X (k + 2 * 0)) = co K X k; rw [Nat.mul_zero, Nat.add_zero]; lib_ring
  have psi1 : ∀ j, CgWindow.Psi K X j 1 = y j := fun j => by
    cases j with
    | zero =>
      have := CgWindow.Psi_lo K X 0 1; rw [Nat.zero_add] at this; rw [this]
      show K.add K.zero (co K X (1 + 2 * 0)) = co K X 1; rw [Nat.mul_zero, Nat.add_zero]; lib_ring
    | succ j =>
      have := CgWindow.Psi_hi K X 1 j; rw [show 1 + j = j + 1 by lib_nat] at this; rw [this, Window.P_succ, Window.P_succ]
      show K.add (K.add K.zero (co K X (j + 2 * 0))) (co K X (j + 2 * 1)) = K.add (co K X j) (co K X (j + 2))
      rw [Nat.mul_zero, Nat.add_zero, Nat.mul_one]; lib_ring
  have c1 : sumN K (fun k => K.mul (y k) (CgWindow.Psi K X k 0)) (M + 3) = cgc K X X 1 := by
    rw [OneStep.cgc_sum K X X (M + 1) 1 hX, sumN_ext K _ (fun k => K.mul (x k) (y k)) (fun k => by rw [psi0]; lib_ring),
      sumN_ext K (fun j => K.mul (co K X j) (CgWindow.Psi K X j 1)) (fun k => K.mul (x k) (y k)) (fun k => by rw [psi1])]
    show K.add (sumN K (fun k => K.mul (co K X k) (y k)) (M + 2)) (K.mul (co K X (M + 2)) (y (M + 2))) = sumN K (fun k => K.mul (co K X k) (y k)) (M + 2)
    rw [HeatLR.co_out K X (M + 2) (by rw [hX]; exact Nat.le_refl _)]; lib_ring
  rw [cg_eq, cg_eq, ch1, c1, show (0 : Nat) + 1 = 1 from rfl] at os
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ os (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

theorem term_le_sum (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) : ∀ N j, j < N → O.le (f j) (sumN K f N)
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | N + 1, j, h => by
    show O.le (f j) (K.add (sumN K f N) (f N))
    cases Nat.lt_or_ge j N with
    | inl hl => exact O.le_trans _ _ _ (term_le_sum f hf N j hl) (HeatV.le_add_nn K O _ _ (hf N))
    | inr hg =>
      have e : j = N := Nat.le_antisymm (Nat.le_of_lt_succ h) hg
      rw [e]; have := O.add_le_add _ _ _ _ (OneFactor.sum_nn K O f hf N) (O.le_refl (f N))
      rw [show K.add K.zero (f N) = f N by lib_ring] at this; exact this

/-- the exact (untruncated) product as a list: [c_0, …, c_{2M+2}] for X of length M + 2 -/
def prodL (X : List R) (M : Nat) : List R := ListRow.listF (fun t => cgc K X X t) 0 (2 * M + 3)

theorem cgc_far (X : List R) (M : Nat) (hX : X.length = M + 2) (n : Nat) (hn : 2 * M + 3 ≤ n) : cgc K X X n = K.zero := by
  rw [OneStep.cgc_sum K X X (M + 1) n hX]
  have : ∀ j, j < M + 2 → CgWindow.Psi K X j n = K.zero := fun j hj => by
    have ⟨e, he⟩ := L1Zero.ex_add j n (Nat.le_trans (Nat.le_of_lt (Nat.lt_of_lt_of_le hj (by
      rw [show 2 * M + 3 = (M + 2) + (M + 1) by lib_nat]; exact Nat.le_add_right _ _))) hn)
    subst he
    rw [CgWindow.Psi_lo]
    apply P_zero_far K X e
    rw [hX]
    have h1 : j + (M + 2) ≤ j + e := Nat.le_trans (by
      rw [show 2 * M + 3 = (M + 1) + (M + 2) by lib_nat]
      exact Nat.add_le_add_right (Nat.le_of_lt_succ hj) _) hn
    exact cancel_le j _ _ h1
  rw [HalfRatio.sum_congr K _ (fun _ => K.zero) (M + 2) (fun j hj => by rw [this j hj]; lib_ring)]
  exact CgWindow.sum_co_nil K (fun _ => 0) (M + 2) |> fun _ => by
    have : ∀ N, sumN K (fun _ => (K.zero : R)) N = K.zero := by
      intro N; induction N with
      | zero => rfl
      | succ N ih => show K.add _ K.zero = K.zero; rw [ih]; lib_ring
    exact this _

theorem co_prodL (X : List R) (M : Nat) (hX : X.length = M + 2) (j : Nat) : co K (prodL K X M) j = cgc K X X j := by
  cases Nat.lt_or_ge j (2 * M + 3) with
  | inl h => unfold prodL; rw [ListRow.co_listF K _ _ 0 j h, Nat.zero_add]
  | inr h =>
    rw [cgc_far K X M hX j h]
    exact HeatLR.co_out K _ j (by unfold prodL; rw [OneStep.len_listF]; exact h)

/-- **the bond-moving product of a v-good list is v-good** -/
theorem Good_prod (X : List R) (hXg : Good K O X) (M : Nat) (hX : X.length = M + 2) (F : OField.OF K O) :
    Good K O (prodL K X M) := by
  have ⟨hb, hpos, _, _⟩ := hXg
  have nn : ∀ t, O.le K.zero (cgc K X X t) := fun t => by
    rw [OneStep.cgc_sum K X X (M + 1) t hX]
    exact OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (hb j) (OneStep.Psi_nn' K O X hb j t)) _
  refine ⟨fun j => by rw [co_prodL K X M hX]; exact nn j, ?_, fun m => ?_, ?_⟩
  · intro j hj
    rw [co_prodL K X M hX]
    have hj' : j < 2 * M + 3 := by unfold prodL at hj; rw [OneStep.len_listF] at hj; exact hj
    cases Nat.lt_or_ge j (M + 2) with
    | inl hl =>
      have lo := cgc_ge K O X X (M + 1) hX hb hb j
      exact ne_of_ge K O _ _ (O.mul_nonneg _ _ (hb 0) (hb j)) lo (fun z =>
        hpos j (by rw [hX]; exact hl) (O.pos_mul_eq_zero _ _ (hb 0) (hpos 0 (by rw [hX]; exact Nat.zero_lt_succ _)) z))
    | inr hg =>
      have ⟨d, hd⟩ := L1Zero.ex_add (M + 1) j (Nat.le_trans (Nat.le_succ _) hg)
      have hdl : d < M + 2 := by
        have : M + 1 + d < M + 1 + (M + 2) := by rw [← hd, show M + 1 + (M + 2) = 2 * M + 3 by lib_nat]; exact hj'
        exact Nat.lt_of_add_lt_add_left this
      have t1 := term_le_sum K O (fun k => K.mul (co K X k) (CgWindow.Psi K X k j))
        (fun k => O.mul_nonneg _ _ (hb k) (OneStep.Psi_nn' K O X hb k j)) (M + 2) (M + 1) (Nat.lt_succ_self _)
      rw [← OneStep.cgc_sum K X X (M + 1) j hX] at t1
      have t2 := (Psi_ge K O X hb (M + 1) j).1 (Nat.le_trans (Nat.le_succ _) hg) d hd
      have t3 := O.mul_le_mul_right _ _ _ t2 (hb (M + 1))
      have lo : O.le (K.mul (co K X d) (co K X (M + 1))) (cgc K X X j) := by
        refine O.le_trans _ _ _ t3 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) t1); lib_ring
      exact ne_of_ge K O _ _ (O.mul_nonneg _ _ (hb d) (hb (M + 1))) lo (fun z =>
        hpos d (by rw [hX]; exact hdl) (O.pos_mul_eq_zero _ _ (hb (M + 1)) (hpos (M + 1) (by rw [hX]; exact Nat.lt_succ_self _))
          (by rw [K.mul_comm]; exact z)))
  · unfold HalfRatio.VV
    rw [co_prodL K X M hX, co_prodL K X M hX, co_prodL K X M hX, co_prodL K X M hX]
    exact prod_VV K O F X hXg M hX m
  · rw [co_prodL K X M hX, co_prodL K X M hX, co_prodL K X M hX]
    exact prod_base K O F X hXg M hX

end L1Full

#print axioms L1Full.pairwise
#print axioms L1Full.P_ge_first
#print axioms L1Full.P_zero_far
#print axioms L1Full.one_step_eq
#print axioms L1Full.cg_comm
#print axioms L1Full.cgc_comm
#print axioms L1Full.l1
#print axioms L1Full.l1_in
#print axioms L1Full.Psi_rec
#print axioms L1Full.Psi_rec0
#print axioms L1Full.chi1_sum
#print axioms L1Full.one_step_gen
#print axioms L1Full.prod_VV
#print axioms L1Full.prod_base
#print axioms L1Full.cgc_far
#print axioms L1Full.Good_prod
#print axioms L1Full.cancel_add
#print axioms L1Full.cancel_le
#print axioms L1Full.Psi_ge
#print axioms L1Full.ne_of_ge
#print axioms L1Full.dist_symm
#print axioms L1Full.cancel_add_r
#print axioms L1Full.cgc_ge
#print axioms L1Full.term_le_sum
#print axioms L1Full.co_prodL

#print axioms: 25 theorems, no axioms.

90.83 Bond moving keeps \(v\)-monotonicity

The bond-moving product of a \(v\)-monotone list is \(v\)-monotone.

\[ (c_m+c_{m+2})\,c_{m+2}\le (c_{m+1}+c_{m+3})\,c_{m+1},\qquad c_1^2\le (c_0+c_2)\,c_0,\qquad c=X\cdot X \]

ym/L1Full.lean

/-
  ym/L1Full — T45, G44-L1 assembly for equal-length lists. Pairwise row comparisons from consecutive ones (zero rows
  form a prefix), and the one-factor step X·B ≼_LR Y·B for any v-monotone list B, every level n.
  Lean 4 core only, no axioms.
-/
import ym.OneStep
import ym.CgSplit

namespace L1Full

open YMChain LinkIntegral SU2Char QBinom HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- pairwise from consecutive, when the zero rows form a prefix [0, s) -/
theorem pairwise (F : OField.OF K O) (P Q : Nat → R) (hP : ∀ m, O.le K.zero (P m)) (hQ : ∀ m, O.le K.zero (Q m))
    (s M : Nat) (hz : ∀ m, m < s → P m = K.zero ∧ Q m = K.zero) (hne : ∀ m, s ≤ m → m < M → P m ≠ K.zero)
    (hc : ∀ m, O.le (K.mul (Q m) (P (m + 1))) (K.mul (Q (m + 1)) (P m))) :
    ∀ i k, i + k ≤ M → O.le (K.mul (Q i) (P (i + k))) (K.mul (Q (i + k)) (P i))
  | i, 0, _ => by rw [Nat.add_zero, K.mul_comm]; exact O.le_refl _
  | i, k + 1, hk => by
    cases Nat.lt_or_ge i s with
    | inl hi =>
      rw [(hz i hi).1, (hz i hi).2, K.mul_comm K.zero, DetChain.mul_zero, DetChain.mul_zero]; exact O.le_refl _
    | inr hi =>
      have ih := pairwise F P Q hP hQ s M hz hne hc i k (Nat.le_of_succ_le hk)
      have c := hc (i + k)
      show O.le (K.mul (Q i) (P (i + k + 1))) (K.mul (Q (i + k + 1)) (P i))
      apply GridQ.cancel K O F (P (i + k)) _ _ (hP _) (hne _ (Nat.le_trans hi (Nat.le_add_right _ _)) hk)
      have s1 := O.mul_le_mul_right _ _ _ ih (hP (i + k + 1))
      have s2 := O.mul_le_mul_right _ _ _ c (hP i)
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
        (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ ?_)))) <;> lib_ring

theorem P_ge_first (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) (c : Nat) : ∀ l, O.le (f c) (Window.P K f c (l + 1))
  | 0 => by show O.le (f c) (K.add K.zero (f (c + 2 * 0))); rw [show K.add K.zero (f (c + 2 * 0)) = f c by rw [Nat.mul_zero, Nat.add_zero]; lib_ring]; exact O.le_refl _
  | l + 1 => by rw [Window.P_succ]; exact O.le_trans _ _ _ (P_ge_first f hf c l) (HeatV.le_add_nn K O _ _ (hf _))

theorem P_zero_far (b : List R) (c : Nat) (hc : b.length ≤ c) : ∀ l, Window.P K (co K b) c l = K.zero
  | 0 => rfl
  | l + 1 => by
    rw [Window.P_succ, P_zero_far b c hc l, HeatLR.co_out K b _ (Nat.le_trans hc (Nat.le_add_right _ _))]; lib_ring

theorem cancel_add : ∀ m a b : Nat, m + a = m + b → a = b
  | 0, a, b, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | m + 1, a, b, h => cancel_add m a b (Nat.succ.inj (by show m + a + 1 = m + b + 1; rw [show m + a + 1 = m + 1 + a by lib_nat, show m + b + 1 = m + 1 + b by lib_nat]; exact h))

theorem cancel_le : ∀ m a b : Nat, m + a ≤ m + b → a ≤ b
  | 0, a, b, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | m + 1, a, b, h => cancel_le m a b (Nat.le_of_succ_le_succ (by show m + a + 1 ≤ m + b + 1; rw [show m + a + 1 = m + 1 + a by lib_nat, show m + b + 1 = m + 1 + b by lib_nat]; exact h))

/-- row sums are ≥ the first window entry, and vanish when the window starts past the list -/
theorem Psi_ge (b : List R) (hb : ∀ j, O.le K.zero (co K b j)) (m n : Nat) :
    (m ≤ n → ∀ e, n = m + e → O.le (co K b e) (CgWindow.Psi K b m n)) ∧
    (n ≤ m → ∀ d, m = n + d → O.le (co K b d) (CgWindow.Psi K b m n)) := by
  constructor
  · intro _ e he; subst he; rw [CgWindow.Psi_lo]; exact P_ge_first K O _ hb e m
  · intro _ d hd; subst hd; rw [CgWindow.Psi_hi]; exact P_ge_first K O _ hb d n

theorem ne_of_ge (x y : R) (hx : O.le K.zero x) (hxy : O.le x y) (hne : x ≠ K.zero) : y ≠ K.zero := by
  intro e; rw [e] at hxy; exact hne (O.le_antisymm _ _ hxy hx)

/-- **one bond-moving factor, equal lengths**: X·B ≼_LR Y·B at every level n, for B any v-monotone list
  at least as long as X, Y -/
theorem one_step_eq (F : OField.OF K O) (B : List R) (hb : ∀ j, O.le K.zero (co K B j))
    (hpos : ∀ j, j < B.length → co K B j ≠ K.zero) (hv : ∀ m, HalfRatio.VV K O (co K B) m)
    (h0 : O.le (K.mul (co K B 1) (co K B 1)) (K.mul (K.add (co K B 0) (co K B 2)) (co K B 0)))
    (M : Nat) (hM : M + 1 ≤ B.length) (Xl Yl : List R) (hX : Xl.length = M + 1) (hYl : Yl.length = M + 1)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K Yl k) (g k)) (hy : ∀ k, O.le K.zero (co K Yl k)) (n : Nat) :
    O.le (K.mul (cgc K Xl B (n + 1)) (cgc K Yl B n)) (K.mul (cgc K Xl B n) (cgc K Yl B (n + 1))) := by
  let Pm := fun m => CgWindow.Psi K B m n
  let Qm := fun m => CgWindow.Psi K B m (n + 1)
  have hP : ∀ m, O.le K.zero (Pm m) := fun m => OneStep.Psi_nn' K O B hb m n
  have hQ : ∀ m, O.le K.zero (Qm m) := fun m => OneStep.Psi_nn' K O B hb m (n + 1)
  have hc : ∀ m, O.le (K.mul (Qm m) (Pm (m + 1))) (K.mul (Qm (m + 1)) (Pm m)) := fun m => by
    have d := OneStep.det_nn_t K O F B hb hpos hv h0 m n
    have d' : O.le K.zero (K.add (K.mul (Qm (m + 1)) (Pm m)) (K.neg (K.mul (Qm m) (Pm (m + 1))))) := d
    have := O.add_le_add _ _ _ _ d' (O.le_refl (K.mul (Qm m) (Pm (m + 1))))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  -- zero prefix
  have nonz : ∀ m, m ≤ M → (∀ e, n = m + e → e < B.length) → Pm m ≠ K.zero := by
    intro m hm hst
    cases Nat.lt_or_ge n m with
    | inl hl =>
      have ⟨d, hd⟩ := L1Zero.ex_add n m (Nat.le_of_lt hl)
      have g1 := (Psi_ge K O B hb m n).2 (Nat.le_of_lt hl) d hd
      have hdm : d ≤ m := by rw [hd]; exact Nat.le_add_left d n
      exact ne_of_ge K O _ _ (hb d) g1 (hpos d (Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_trans hdm hm)) hM))
    | inr hg =>
      have ⟨e, he⟩ := L1Zero.ex_add m n hg
      have g1 := (Psi_ge K O B hb m n).1 hg e he
      exact ne_of_ge K O _ _ (hb e) g1 (hpos e (hst e he))
  have zer : ∀ m e, B.length ≤ e → CgWindow.Psi K B m (m + e) = K.zero := by
    intro m e h
    rw [CgWindow.Psi_lo]; exact P_zero_far K B e h (m + 1)
  have nz : ∃ s, (∀ m, m < s → Pm m = K.zero ∧ Qm m = K.zero) ∧ (∀ m, s ≤ m → m ≤ M → Pm m ≠ K.zero) := by
    cases Nat.lt_or_ge n B.length with
    | inl hl =>
      refine ⟨0, fun m h => absurd h (Nat.not_lt_zero _), fun m _ hm => nonz m hm (fun e he => ?_)⟩
      exact Nat.lt_of_le_of_lt (by rw [he]; exact Nat.le_add_left e m) hl
    | inr hg =>
      have ⟨t, ht⟩ := L1Zero.ex_add B.length n hg
      refine ⟨t + 1, fun m hm => ?_, fun m hs hm => nonz m hm (fun e he => ?_)⟩
      · have ⟨u, hu⟩ := L1Zero.ex_add m t (Nat.le_of_lt_succ hm)
        have e1 : n = m + (B.length + u) := by rw [ht, hu]; lib_nat
        have e2 : n + 1 = m + (B.length + u + 1) := by rw [e1]; lib_nat
        refine ⟨?_, ?_⟩
        · show CgWindow.Psi K B m n = K.zero
          rw [e1]; exact zer m _ (Nat.le_add_right _ _)
        · show CgWindow.Psi K B m (n + 1) = K.zero
          rw [e2]; exact zer m _ (Nat.le_trans (Nat.le_add_right _ u) (Nat.le_succ _))
      · have ⟨v, hv'⟩ := L1Zero.ex_add (t + 1) m hs
        have q : t + (v + e + 1) = t + B.length := by
          rw [show t + (v + e + 1) = (t + 1 + v) + e by lib_nat, ← hv', ← he, ht]; lib_nat
        have q2 := cancel_add t _ _ q
        rw [← q2]; exact Nat.lt_succ_of_le (Nat.le_add_left e v)
  have ⟨s, hz, hne⟩ := nz
  have pw := pairwise K O F Pm Qm hP hQ s M hz (fun m hs hm => hne m hs (Nat.le_of_lt hm)) hc
  have of := OneFactor.one_factor_pw K O (fun k => K.mul (co K Yl k) (Qm k)) (fun k => K.mul (co K Yl k) (Pm k)) g (M + 1)
    (fun i j hij hj => by
      have ⟨k, e⟩ := L1Zero.ex_add i j (Nat.le_of_lt hij)
      have t := pw i k (by rw [← e]; exact Nat.le_of_lt_succ hj)
      rw [← e] at t
      have u := O.mul_le_mul_right _ _ _ t (O.mul_nonneg _ _ (hy i) (hy j))
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ u (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring)
    hg hg0
  rw [OneStep.cgc_sum K Xl B M (n + 1) hX, OneStep.cgc_sum K Yl B M n hYl, OneStep.cgc_sum K Xl B M n hX,
    OneStep.cgc_sum K Yl B M (n + 1) hYl]
  have ex : ∀ t, sumN K (fun j => K.mul (co K Xl j) (CgWindow.Psi K B j t)) (M + 1)
      = sumN K (fun k => K.mul (K.mul (co K Yl k) (CgWindow.Psi K B k t)) (g k)) (M + 1) :=
    fun t => sumN_ext K _ _ (fun k => by rw [hxy]; lib_ring) _
  rw [ex, ex]
  exact of

theorem dist_symm : ∀ j k, CgSplit.dist j k = CgSplit.dist k j
  | 0, 0 => rfl
  | 0, k + 1 => rfl
  | j + 1, 0 => rfl
  | j + 1, k + 1 => dist_symm j k

theorem cancel_add_r : ∀ c a b : Nat, a + c = b + c → a = b
  | c, a, b, h => cancel_add c a b (by rw [Nat.add_comm c a, Nat.add_comm c b]; exact h)

theorem cg_comm (j k n : Nat) : Fusion.cg j k n = Fusion.cg k j n := by
  have h1 := CgSplit.cg_split j k n
  have h2 := CgSplit.cg_split k j n
  rw [dist_symm k j, Nat.add_comm k j, ← h1] at h2
  exact (cancel_add_r _ _ _ h2).symm

theorem cgc_comm (a b : List R) (n : Nat) : cgc K a b n = cgc K b a n := by
  show dot K 0 a (fun j => dot K 0 b (fun k => cN K (Fusion.cg j k n))) = dot K 0 b (fun j => dot K 0 a (fun k => cN K (Fusion.cg j k n)))
  rw [dot_swap K (fun j k => cN K (Fusion.cg j k n))]
  exact dot_ext K _ _ (fun k => dot_ext K _ _ (fun j => by rw [cg_comm]) 0 a) 0 b

/-- a list is "v-good": nonnegative, nonzero within its length, truncated coefficient function v-monotone, base condition -/
def Good (b : List R) : Prop :=
  (∀ j, O.le K.zero (co K b j)) ∧ (∀ j, j < b.length → co K b j ≠ K.zero) ∧ (∀ m, HalfRatio.VV K O (co K b) m) ∧
    O.le (K.mul (co K b 1) (co K b 1)) (K.mul (K.add (co K b 0) (co K b 2)) (co K b 0))

/-- **L1 (bond moving preserves the LR comparison)**: X = Y·g with g nonincreasing, X and Y v-good of equal length:
  X·X ≼_LR Y·Y at level n (wherever (X·Y)_n ≠ 0) -/
theorem l1 (F : OField.OF K O) (Xl Yl : List R) (hXg : Good K O Xl) (hYg : Good K O Yl) (M : Nat)
    (hX : Xl.length = M + 1) (hYl : Yl.length = M + 1)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K Yl k) (g k)) (n : Nat) (hBn : cgc K Xl Yl n ≠ K.zero) :
    O.le (K.mul (cgc K Xl Xl (n + 1)) (cgc K Yl Yl n)) (K.mul (cgc K Xl Xl n) (cgc K Yl Yl (n + 1))) := by
  have ⟨xb, xp, xv, x0⟩ := hXg
  have ⟨yb, yp, yv, y0⟩ := hYg
  -- step 1: fixed factor X
  have s1 := one_step_eq K O F Xl xb xp xv x0 M (by rw [hX]; exact Nat.le_refl _) Xl Yl hX hYl g hg hg0 hxy yb n
  -- step 2: fixed factor Y
  have s2 := one_step_eq K O F Yl yb yp yv y0 M (by rw [hYl]; exact Nat.le_refl _) Xl Yl hX hYl g hg hg0 hxy yb n
  rw [cgc_comm K Yl Xl, cgc_comm K Yl Xl] at s1
  have nnB : ∀ t, O.le K.zero (cgc K Xl Yl t) := fun t => by
    rw [OneStep.cgc_sum K Xl Yl M t hX]
    exact OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (xb j) (OneStep.Psi_nn' K O Yl yb j t)) _
  have nnX : ∀ t, O.le K.zero (cgc K Xl Xl t) := fun t => by
    rw [OneStep.cgc_sum K Xl Xl M t hX]
    exact OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (xb j) (OneStep.Psi_nn' K O Xl xb j t)) _
  have nnY : ∀ t, O.le K.zero (cgc K Yl Yl t) := fun t => by
    rw [OneStep.cgc_sum K Yl Yl M t hYl]
    exact OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (yb j) (OneStep.Psi_nn' K O Yl yb j t)) _
  apply GridQ.cancel K O F (cgc K Xl Yl n) _ _ (nnB n) hBn
  have t1 := O.mul_le_mul_right _ _ _ s1 (nnY n)
  have t2 := O.mul_le_mul_right _ _ _ s2 (nnX n)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ t1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
    (O.le_trans _ _ _ t2 (TripleE.le_of_eq' K O _ _ ?_)))) <;> lib_ring

/-- (X·Y)_n ≥ X_0 Y_n, so it is nonzero for n within the list length -/
theorem cgc_ge (Xl Yl : List R) (M : Nat) (hX : Xl.length = M + 1) (hx : ∀ j, O.le K.zero (co K Xl j))
    (hy : ∀ j, O.le K.zero (co K Yl j)) (n : Nat) : O.le (K.mul (co K Xl 0) (co K Yl n)) (cgc K Xl Yl n) := by
  rw [OneStep.cgc_sum K Xl Yl M n hX, sumN_shift]
  have e : CgWindow.Psi K Yl 0 (0 + n) = Window.P K (co K Yl) n 1 := CgWindow.Psi_lo K Yl 0 n
  rw [Nat.zero_add] at e
  rw [e]
  have h1 : Window.P K (co K Yl) n 1 = co K Yl n := by show K.add K.zero (co K Yl (n + 2 * 0)) = _; rw [Nat.mul_zero, Nat.add_zero]; lib_ring
  rw [h1]
  exact HeatV.le_add_nn K O _ _ (OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (hx _) (OneStep.Psi_nn' K O Yl hy _ n)) M)

/-- **L1 at every level inside the list length** -/
theorem l1_in (F : OField.OF K O) (Xl Yl : List R) (hXg : Good K O Xl) (hYg : Good K O Yl) (M : Nat)
    (hX : Xl.length = M + 1) (hYl : Yl.length = M + 1)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K Yl k) (g k)) (n : Nat) (hn : n ≤ M) :
    O.le (K.mul (cgc K Xl Xl (n + 1)) (cgc K Yl Yl n)) (K.mul (cgc K Xl Xl n) (cgc K Yl Yl (n + 1))) := by
  have ⟨xb, xp, _, _⟩ := hXg
  have ⟨yb, yp, _, _⟩ := hYg
  have lo := cgc_ge K O Xl Yl M hX xb yb n
  have pos : K.mul (co K Xl 0) (co K Yl n) ≠ K.zero := by
    intro z
    have x0 : co K Xl 0 ≠ K.zero := xp 0 (by rw [hX]; exact Nat.zero_lt_succ _)
    exact yp n (by rw [hYl]; exact Nat.lt_succ_of_le hn) (O.pos_mul_eq_zero _ _ (xb 0) x0 z)
  exact l1 K O F Xl Yl hXg hYg M hX hYl g hg hg0 hxy n (ne_of_ge K O _ _ (O.mul_nonneg _ _ (xb 0) (yb n)) lo pos)

/-- **CG row recursion** (χ₁ acting on either factor): Ψ_{m+1}(j+2) + Ψ_{m+1}(j) = Ψ_m(j+1) + Ψ_{m+2}(j+1) -/
theorem Psi_rec (b : List R) (j m : Nat) :
    K.add (CgWindow.Psi K b (j + 2) (m + 1)) (CgWindow.Psi K b j (m + 1))
      = K.add (CgWindow.Psi K b (j + 1) m) (CgWindow.Psi K b (j + 1) (m + 2)) := by
  cases Nat.lt_or_ge j m with
  | inl hl =>
    have ⟨c, hc⟩ := L1Zero.ex_add (j + 1) m hl
    subst hc
    have e1 : CgWindow.Psi K b (j + 2) (j + 1 + c + 1) = Window.P K (co K b) c (j + 3) := by
      rw [show j + 1 + c + 1 = (j + 2) + c by lib_nat, CgWindow.Psi_lo]
    have e2 : CgWindow.Psi K b j (j + 1 + c + 1) = Window.P K (co K b) (c + 2) (j + 1) := by
      rw [show j + 1 + c + 1 = j + (c + 2) by lib_nat, CgWindow.Psi_lo]
    have e3 : CgWindow.Psi K b (j + 1) (j + 1 + c) = Window.P K (co K b) c (j + 2) := by
      rw [CgWindow.Psi_lo]
    have e4 : CgWindow.Psi K b (j + 1) (j + 1 + c + 2) = Window.P K (co K b) (c + 2) (j + 2) := by
      rw [show j + 1 + c + 2 = (j + 1) + (c + 2) by lib_nat, CgWindow.Psi_lo]
    rw [e1, e2, e3, e4, show j + 3 = (j + 2) + 1 from rfl, Window.P_shift K _ c (j + 2), Window.P_shift K _ c (j + 1)]
    lib_ring
  | inr hg =>
    cases Nat.lt_or_ge m j with
    | inl hl =>
      have ⟨d, hd⟩ := L1Zero.ex_add (m + 1) j hl
      subst hd
      have e1 : CgWindow.Psi K b (m + 1 + d + 2) (m + 1) = Window.P K (co K b) (d + 2) (m + 2) := by
        rw [show m + 1 + d + 2 = (m + 1) + (d + 2) by lib_nat, CgWindow.Psi_hi]
      have e2 : CgWindow.Psi K b (m + 1 + d) (m + 1) = Window.P K (co K b) d (m + 2) := by
        rw [CgWindow.Psi_hi]
      have e3 : CgWindow.Psi K b (m + 1 + d + 1) m = Window.P K (co K b) (d + 2) (m + 1) := by
        rw [show m + 1 + d + 1 = m + (d + 2) by lib_nat, CgWindow.Psi_hi]
      have e4 : CgWindow.Psi K b (m + 1 + d + 1) (m + 2) = Window.P K (co K b) d (m + 3) := by
        rw [show m + 1 + d + 1 = (m + 2) + d by lib_nat, CgWindow.Psi_hi]
      rw [e1, e2, e3, e4, show m + 3 = (m + 2) + 1 from rfl, Window.P_shift K _ d (m + 2), Window.P_shift K _ d (m + 1)]
      lib_ring
    | inr hg2 =>
      have e : j = m := Nat.le_antisymm hg2 hg
      subst e
      have e1 : CgWindow.Psi K b (j + 2) (j + 1) = Window.P K (co K b) 1 (j + 2) := by
        rw [show j + 2 = (j + 1) + 1 from rfl, CgWindow.Psi_hi]
      have e2 : CgWindow.Psi K b j (j + 1) = Window.P K (co K b) 1 (j + 1) := CgWindow.Psi_lo K b j 1
      have e3 : CgWindow.Psi K b (j + 1) j = Window.P K (co K b) 1 (j + 1) := CgWindow.Psi_hi K b j 1
      have e4 : CgWindow.Psi K b (j + 1) (j + 2) = Window.P K (co K b) 1 (j + 2) := CgWindow.Psi_lo K b (j + 1) 1
      rw [e1, e2, e3, e4]; lib_ring

/-- χ₁ times a coefficient function: (χ₁x)_0 = x_1, (χ₁x)_{k+1} = x_k + x_{k+2} -/
def chi1f (x : Nat → R) : Nat → R
  | 0 => x 1
  | k + 1 => K.add (x k) (x (k + 2))

theorem Psi_rec0 (b : List R) (m : Nat) :
    CgWindow.Psi K b 1 (m + 1) = K.add (CgWindow.Psi K b 0 m) (CgWindow.Psi K b 0 (m + 2)) := by
  have e1 : CgWindow.Psi K b 1 (m + 1) = Window.P K (co K b) m 2 := by
    have := CgWindow.Psi_lo K b 1 m; rw [Nat.add_comm 1 m] at this; exact this
  have e2 : CgWindow.Psi K b 0 m = Window.P K (co K b) m 1 := by
    have := CgWindow.Psi_lo K b 0 m; rw [Nat.zero_add] at this; exact this
  have e3 : CgWindow.Psi K b 0 (m + 2) = Window.P K (co K b) (m + 2) 1 := by
    have := CgWindow.Psi_lo K b 0 (m + 2); rw [Nat.zero_add] at this; exact this
  have p1 : ∀ c, Window.P K (co K b) c 1 = co K b c := fun c => by
    show K.add K.zero (co K b (c + 2 * 0)) = _; rw [Nat.mul_zero, Nat.add_zero]; lib_ring
  rw [e1, e2, e3, Window.P_shift K _ m 1, p1, p1]

/-- **χ₁ moves between the factors** (sum form) -/
theorem chi1_sum (b : List R) (x : Nat → R) (M m : Nat) (hz1 : x (M + 1) = K.zero) (hz2 : x (M + 2) = K.zero) :
    sumN K (fun k => K.mul (chi1f K x k) (CgWindow.Psi K b k (m + 1))) (M + 2)
      = sumN K (fun j => K.mul (x j) (K.add (CgWindow.Psi K b j m) (CgWindow.Psi K b j (m + 2)))) (M + 1) := by
  -- Ψ' k = Ψ_{m+1}(k)
  have hrec : ∀ j, K.add (CgWindow.Psi K b (j + 1) m) (CgWindow.Psi K b (j + 1) (m + 2))
      = K.add (CgWindow.Psi K b (j + 2) (m + 1)) (CgWindow.Psi K b j (m + 1)) := fun j => (Psi_rec K b j m).symm
  rw [sumN_shift K _ (M + 1), sumN_shift K (fun j => K.mul (x j) (K.add (CgWindow.Psi K b j m) (CgWindow.Psi K b j (m + 2)))) M]
  show K.add (K.mul (x 1) (CgWindow.Psi K b 0 (m + 1)))
      (sumN K (fun k => K.mul (K.add (x k) (x (k + 2))) (CgWindow.Psi K b (k + 1) (m + 1))) (M + 1)) = _
  rw [sumN_ext K _ (fun k => K.add (K.mul (x k) (CgWindow.Psi K b (k + 1) (m + 1))) (K.mul (x (k + 2)) (CgWindow.Psi K b (k + 1) (m + 1))))
      (fun k => by lib_ring) (M + 1), sumN_add, sumN_shift K (fun k => K.mul (x k) (CgWindow.Psi K b (k + 1) (m + 1))) M]
  rw [sumN_ext K (fun k => K.mul (x (k + 1)) (K.add (CgWindow.Psi K b (k + 1) m) (CgWindow.Psi K b (k + 1) (m + 2))))
      (fun k => K.add (K.mul (x (k + 1)) (CgWindow.Psi K b (k + 2) (m + 1))) (K.mul (x (k + 1)) (CgWindow.Psi K b k (m + 1))))
      (fun k => by rw [hrec]; lib_ring) M, sumN_add, ← Psi_rec0]
  -- remaining: x_1 Ψ'(0) + Σ_{k<M+1} x_{k+2} Ψ'(k+1) = Σ_{j<M} x_{j+1} Ψ'(j)  (up to the common terms)
  have key : K.add (K.mul (x 1) (CgWindow.Psi K b 0 (m + 1))) (sumN K (fun k => K.mul (x (k + 2)) (CgWindow.Psi K b (k + 1) (m + 1))) (M + 1))
      = sumN K (fun k => K.mul (x (k + 1)) (CgWindow.Psi K b k (m + 1))) M := by
    cases M with
    | zero =>
      show K.add (K.mul (x 1) _) (K.add K.zero (K.mul (x (0 + 2)) _)) = K.zero
      rw [show (0 : Nat) + 2 = 2 from rfl, show x 1 = x (0 + 1) from rfl, hz1, hz2]; lib_ring
    | succ M' =>
      rw [sumN_shift K (fun k => K.mul (x (k + 1)) (CgWindow.Psi K b k (m + 1))) M']
      show K.add _ (K.add (K.add (sumN K (fun k => K.mul (x (k + 2)) (CgWindow.Psi K b (k + 1) (m + 1))) M')
          (K.mul (x (M' + 2)) (CgWindow.Psi K b (M' + 1) (m + 1)))) (K.mul (x (M' + 1 + 2)) (CgWindow.Psi K b (M' + 1 + 1) (m + 1))))
        = K.add (K.mul (x 1) (CgWindow.Psi K b 0 (m + 1))) (sumN K (fun k => K.mul (x (k + 2)) (CgWindow.Psi K b (k + 1) (m + 1))) M')
      rw [show M' + 2 = M' + 1 + 1 from rfl, hz1, show M' + 1 + 2 = M' + 1 + 2 from rfl, hz2]
      lib_ring
  rw [← key]
  have eS : sumN K (fun k => K.mul (x (k + 1)) (CgWindow.Psi K b (k + 1 + 1) (m + 1))) M
      = sumN K (fun k => K.mul (x (k + 1)) (CgWindow.Psi K b (k + 2) (m + 1))) M := rfl
  rw [eS, show (0 : Nat) + 1 = 1 from rfl]
  lib_ring

/-- **general one-factor step** (weights y over M+1 rows, fixed v-good factor B, M ≤ n + |B|) -/
theorem one_step_gen (F : OField.OF K O) (B : List R) (hBg : Good K O B) (M n : Nat) (hM : M ≤ n + B.length)
    (y g : Nat → R) (hy : ∀ k, O.le K.zero (y k)) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m)) :
    O.le (K.mul (sumN K (fun k => K.mul (K.mul (y k) (CgWindow.Psi K B k (n + 1))) (g k)) (M + 1))
        (sumN K (fun k => K.mul (y k) (CgWindow.Psi K B k n)) (M + 1)))
      (K.mul (sumN K (fun k => K.mul (K.mul (y k) (CgWindow.Psi K B k n)) (g k)) (M + 1))
        (sumN K (fun k => K.mul (y k) (CgWindow.Psi K B k (n + 1))) (M + 1))) := by
  have ⟨hb, hpos, hv, h0⟩ := hBg
  let Pm := fun m => CgWindow.Psi K B m n
  let Qm := fun m => CgWindow.Psi K B m (n + 1)
  have hP : ∀ m, O.le K.zero (Pm m) := fun m => OneStep.Psi_nn' K O B hb m n
  have hQ : ∀ m, O.le K.zero (Qm m) := fun m => OneStep.Psi_nn' K O B hb m (n + 1)
  have hc : ∀ m, O.le (K.mul (Qm m) (Pm (m + 1))) (K.mul (Qm (m + 1)) (Pm m)) := fun m => by
    have d := OneStep.det_nn_t K O F B hb hpos hv h0 m n
    have d' : O.le K.zero (K.add (K.mul (Qm (m + 1)) (Pm m)) (K.neg (K.mul (Qm m) (Pm (m + 1))))) := d
    have := O.add_le_add _ _ _ _ d' (O.le_refl (K.mul (Qm m) (Pm (m + 1))))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  have nonz : ∀ m, m < M → (∀ e, n = m + e → e < B.length) → Pm m ≠ K.zero := by
    intro m hm hst
    cases Nat.lt_or_ge n m with
    | inl hl =>
      have ⟨d, hd⟩ := L1Zero.ex_add n m (Nat.le_of_lt hl)
      have g1 := (Psi_ge K O B hb m n).2 (Nat.le_of_lt hl) d hd
      have hdl : d < B.length := by
        have : n + d < n + B.length := by rw [← hd]; exact Nat.lt_of_lt_of_le hm hM
        exact Nat.lt_of_add_lt_add_left this
      exact ne_of_ge K O _ _ (hb d) g1 (hpos d hdl)
    | inr hg' =>
      have ⟨e, he⟩ := L1Zero.ex_add m n hg'
      have g1 := (Psi_ge K O B hb m n).1 hg' e he
      exact ne_of_ge K O _ _ (hb e) g1 (hpos e (hst e he))
  have zer : ∀ m e, B.length ≤ e → CgWindow.Psi K B m (m + e) = K.zero := by
    intro m e h
    rw [CgWindow.Psi_lo]; exact P_zero_far K B e h (m + 1)
  have nz : ∃ s, (∀ m, m < s → Pm m = K.zero ∧ Qm m = K.zero) ∧ (∀ m, s ≤ m → m < M → Pm m ≠ K.zero) := by
    cases Nat.lt_or_ge n B.length with
    | inl hl =>
      refine ⟨0, fun m h => absurd h (Nat.not_lt_zero _), fun m _ hm => nonz m hm (fun e he => ?_)⟩
      exact Nat.lt_of_le_of_lt (by rw [he]; exact Nat.le_add_left e m) hl
    | inr hg' =>
      have ⟨t, ht⟩ := L1Zero.ex_add B.length n hg'
      refine ⟨t + 1, fun m hm => ?_, fun m hs hm => nonz m hm (fun e he => ?_)⟩
      · have ⟨u, hu⟩ := L1Zero.ex_add m t (Nat.le_of_lt_succ hm)
        have e1 : n = m + (B.length + u) := by rw [ht, hu]; lib_nat
        have e2 : n + 1 = m + (B.length + u + 1) := by rw [e1]; lib_nat
        refine ⟨?_, ?_⟩
        · show CgWindow.Psi K B m n = K.zero
          rw [e1]; exact zer m _ (Nat.le_add_right _ _)
        · show CgWindow.Psi K B m (n + 1) = K.zero
          rw [e2]; exact zer m _ (Nat.le_trans (Nat.le_add_right _ u) (Nat.le_succ _))
      · have ⟨v, hv'⟩ := L1Zero.ex_add (t + 1) m hs
        have q : t + (v + e + 1) = t + B.length := by
          rw [show t + (v + e + 1) = (t + 1 + v) + e by lib_nat, ← hv', ← he, ht]; lib_nat
        have q2 := cancel_add t _ _ q
        rw [← q2]; exact Nat.lt_succ_of_le (Nat.le_add_left e v)
  have ⟨s, hz, hne⟩ := nz
  have pw := pairwise K O F Pm Qm hP hQ s M hz hne hc
  exact OneFactor.one_factor_pw K O (fun k => K.mul (y k) (Qm k)) (fun k => K.mul (y k) (Pm k)) g (M + 1)
    (fun i j hij hj => by
      have ⟨k, e⟩ := L1Zero.ex_add i j (Nat.le_of_lt hij)
      have t := pw i k (by rw [← e]; exact Nat.le_of_lt_succ hj)
      rw [← e] at t
      have u := O.mul_le_mul_right _ _ _ t (O.mul_nonneg _ _ (hy i) (hy j))
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ u (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring)
    hg hg0

/-- **bond moving keeps v-monotonicity** (levels m ≥ 0 of VV for c = X·X, i.e. c ≼ χ₁c at n = m + 1) -/
theorem prod_VV (F : OField.OF K O) (X : List R) (hXg : Good K O X) (M : Nat) (hX : X.length = M + 2) (m : Nat) :
    O.le (K.mul (K.add (cgc K X X m) (cgc K X X (m + 2))) (cgc K X X (m + 2)))
      (K.mul (K.add (cgc K X X (m + 1)) (cgc K X X (m + 3))) (cgc K X X (m + 1))) := by
  have ⟨hb, hpos, hv, h0⟩ := hXg
  let x := co K X
  let y := chi1f K x
  have hz : ∀ j, M + 2 ≤ j → x j = K.zero := fun j h => HeatLR.co_out K X j (by rw [hX]; exact h)
  have hxp : ∀ j, j < M + 2 → x j ≠ K.zero := fun j h => hpos j (by rw [hX]; exact h)
  have hy : ∀ k, O.le K.zero (y k) := fun k => by
    cases k with
    | zero => exact hb 1
    | succ k => have := O.add_le_add _ _ _ _ (hb k) (hb (k + 2)); rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  have hyne : ∀ k, k ≤ M + 2 → y k ≠ K.zero := fun k hk => by
    cases k with
    | zero => exact hxp 1 (Nat.succ_lt_succ (Nat.zero_lt_succ _))
    | succ k =>
      intro e
      have lo := HeatV.le_add_nn K O (x k) (x (k + 2)) (hb (k + 2))
      have : y (k + 1) = K.add (x k) (x (k + 2)) := rfl
      rw [← this, e] at lo
      exact hxp k (Nat.lt_of_lt_of_le (Nat.lt_succ_self k) hk) (O.le_antisymm _ _ lo (hb k))
  let g := fun k => K.mul (x k) (F.inv (y k))
  have hg0 : ∀ k, O.le K.zero (g k) := fun k => by
    cases Nat.lt_or_ge k (M + 3) with
    | inl h => exact O.mul_nonneg _ _ (hb k) (F.inv_pos _ (hy k) (hyne k (Nat.le_of_lt_succ h)))
    | inr h => show O.le K.zero (K.mul (x k) _); rw [hz k (Nat.le_trans (Nat.le_succ _) h)]; rw [K.mul_comm, DetChain.mul_zero]; exact O.le_refl _
  have gy : ∀ k, k ≤ M + 2 → K.mul (y k) (g k) = x k := fun k hk => by
    show K.mul (y k) (K.mul (x k) (F.inv (y k))) = x k
    rw [show K.mul (y k) (K.mul (x k) (F.inv (y k))) = K.mul (x k) (K.mul (y k) (F.inv (y k))) by lib_ring, F.mul_inv _ (hyne k hk)]; lib_ring
  have hg : ∀ k, O.le (g (k + 1)) (g k) := fun k => by
    cases Nat.lt_or_ge (k + 1) (M + 2) with
    | inr h => show O.le (K.mul (x (k + 1)) _) _; rw [hz (k + 1) h, K.mul_comm, DetChain.mul_zero]; exact hg0 k
    | inl h =>
      -- cross form x_{k+1} y_k ≤ x_k y_{k+1}
      have cross : O.le (K.mul (x (k + 1)) (y k)) (K.mul (x k) (y (k + 1))) := by
        cases k with
        | zero => show O.le (K.mul (x 1) (x 1)) (K.mul (x 0) (K.add (x 0) (x 2))); rw [K.mul_comm (x 0)]; exact h0
        | succ j =>
          have := hv j
          unfold HalfRatio.VV at this
          show O.le (K.mul (x (j + 2)) (K.add (x j) (x (j + 2)))) (K.mul (x (j + 1)) (K.add (x (j + 1)) (x (j + 3))))
          rw [K.mul_comm (x (j + 2)), K.mul_comm (x (j + 1))]; exact this
      have iy := F.inv_pos _ (hy k) (hyne k (Nat.le_of_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt h))))
      have iy1 := F.inv_pos _ (hy (k + 1)) (hyne (k + 1) (Nat.le_of_lt h))
      have t := O.mul_le_mul_right _ _ _ cross (O.mul_nonneg _ _ iy iy1)
      have e1 : K.mul (K.mul (x (k + 1)) (y k)) (K.mul (F.inv (y k)) (F.inv (y (k + 1)))) = g (k + 1) := by
        show _ = K.mul (x (k + 1)) (F.inv (y (k + 1)))
        rw [show K.mul (K.mul (x (k + 1)) (y k)) (K.mul (F.inv (y k)) (F.inv (y (k + 1))))
          = K.mul (K.mul (x (k + 1)) (F.inv (y (k + 1)))) (K.mul (y k) (F.inv (y k))) by lib_ring,
          F.mul_inv _ (hyne k (Nat.le_of_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt h))))]; lib_ring
      have e2 : K.mul (K.mul (x k) (y (k + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1)))) = g k := by
        show _ = K.mul (x k) (F.inv (y k))
        rw [show K.mul (K.mul (x k) (y (k + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1))))
          = K.mul (K.mul (x k) (F.inv (y k))) (K.mul (y (k + 1)) (F.inv (y (k + 1)))) by lib_ring,
          F.mul_inv _ (hyne (k + 1) (Nat.le_of_lt h))]; lib_ring
      rw [e1, e2] at t; exact t
  have os := one_step_gen K O F X hXg (M + 2) (m + 1) (by rw [hX]; exact Nat.le_add_left _ _) y g hy hg hg0
  have cg_eq : ∀ t, sumN K (fun k => K.mul (K.mul (y k) (CgWindow.Psi K X k t)) (g k)) (M + 3) = cgc K X X t := fun t => by
    rw [HalfRatio.sum_congr K _ (fun k => K.mul (x k) (CgWindow.Psi K X k t)) (M + 3) (fun k hk => by
      show K.mul (K.mul (y k) _) (g k) = _
      rw [show K.mul (K.mul (y k) (CgWindow.Psi K X k t)) (g k) = K.mul (K.mul (y k) (g k)) (CgWindow.Psi K X k t) by lib_ring,
        gy k (Nat.le_of_lt_succ hk)]),
      OneStep.cgc_sum K X X (M + 1) t hX]
    show K.add (sumN K (fun k => K.mul (co K X k) (CgWindow.Psi K X k t)) (M + 2)) (K.mul (co K X (M + 2)) (CgWindow.Psi K X (M + 2) t))
      = sumN K (fun j => K.mul (co K X j) (CgWindow.Psi K X j t)) (M + 2)
    rw [HeatLR.co_out K X (M + 2) (by rw [hX]; exact Nat.le_refl _)]; lib_ring
  have ch : ∀ t, sumN K (fun k => K.mul (y k) (CgWindow.Psi K X k (t + 1))) (M + 3) = K.add (cgc K X X t) (cgc K X X (t + 2)) := fun t => by
    have := chi1_sum K X x (M + 1) t (hz _ (Nat.le_refl _)) (hz _ (Nat.le_succ _))
    rw [this, sumN_ext K _ (fun j => K.add (K.mul (x j) (CgWindow.Psi K X j t)) (K.mul (x j) (CgWindow.Psi K X j (t + 2)))) (fun j => by lib_ring),
      sumN_add, OneStep.cgc_sum K X X (M + 1) t hX, OneStep.cgc_sum K X X (M + 1) (t + 2) hX]
  rw [cg_eq, cg_eq, ch m, ch (m + 1), show m + 1 + 1 = m + 2 from rfl, show m + 1 + 2 = m + 3 from rfl] at os
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ os (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

/-- **bond moving keeps v-monotonicity** (base condition c_1² ≤ (c_0 + c_2) c_0) -/
theorem prod_base (F : OField.OF K O) (X : List R) (hXg : Good K O X) (M : Nat) (hX : X.length = M + 2) :
    O.le (K.mul (cgc K X X 1) (cgc K X X 1)) (K.mul (K.add (cgc K X X 0) (cgc K X X 2)) (cgc K X X 0)) := by
  have ⟨hb, hpos, hv, h0⟩ := hXg
  let x := co K X
  let y := chi1f K x
  have hz : ∀ j, M + 2 ≤ j → x j = K.zero := fun j h => HeatLR.co_out K X j (by rw [hX]; exact h)
  have hxp : ∀ j, j < M + 2 → x j ≠ K.zero := fun j h => hpos j (by rw [hX]; exact h)
  have hy : ∀ k, O.le K.zero (y k) := fun k => by
    cases k with
    | zero => exact hb 1
    | succ k => have := O.add_le_add _ _ _ _ (hb k) (hb (k + 2)); rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  have hyne : ∀ k, k ≤ M + 2 → y k ≠ K.zero := fun k hk => by
    cases k with
    | zero => exact hxp 1 (Nat.succ_lt_succ (Nat.zero_lt_succ _))
    | succ k =>
      intro e
      have lo := HeatV.le_add_nn K O (x k) (x (k + 2)) (hb (k + 2))
      have : y (k + 1) = K.add (x k) (x (k + 2)) := rfl
      rw [← this, e] at lo
      exact hxp k (Nat.lt_of_lt_of_le (Nat.lt_succ_self k) hk) (O.le_antisymm _ _ lo (hb k))
  let g := fun k => K.mul (x k) (F.inv (y k))
  have hg0 : ∀ k, O.le K.zero (g k) := fun k => by
    cases Nat.lt_or_ge k (M + 3) with
    | inl h => exact O.mul_nonneg _ _ (hb k) (F.inv_pos _ (hy k) (hyne k (Nat.le_of_lt_succ h)))
    | inr h => show O.le K.zero (K.mul (x k) _); rw [hz k (Nat.le_trans (Nat.le_succ _) h)]; rw [K.mul_comm, DetChain.mul_zero]; exact O.le_refl _
  have gy : ∀ k, k ≤ M + 2 → K.mul (y k) (g k) = x k := fun k hk => by
    show K.mul (y k) (K.mul (x k) (F.inv (y k))) = x k
    rw [show K.mul (y k) (K.mul (x k) (F.inv (y k))) = K.mul (x k) (K.mul (y k) (F.inv (y k))) by lib_ring, F.mul_inv _ (hyne k hk)]; lib_ring
  have hg : ∀ k, O.le (g (k + 1)) (g k) := fun k => by
    cases Nat.lt_or_ge (k + 1) (M + 2) with
    | inr h => show O.le (K.mul (x (k + 1)) _) _; rw [hz (k + 1) h, K.mul_comm, DetChain.mul_zero]; exact hg0 k
    | inl h =>
      -- cross form x_{k+1} y_k ≤ x_k y_{k+1}
      have cross : O.le (K.mul (x (k + 1)) (y k)) (K.mul (x k) (y (k + 1))) := by
        cases k with
        | zero => show O.le (K.mul (x 1) (x 1)) (K.mul (x 0) (K.add (x 0) (x 2))); rw [K.mul_comm (x 0)]; exact h0
        | succ j =>
          have := hv j
          unfold HalfRatio.VV at this
          show O.le (K.mul (x (j + 2)) (K.add (x j) (x (j + 2)))) (K.mul (x (j + 1)) (K.add (x (j + 1)) (x (j + 3))))
          rw [K.mul_comm (x (j + 2)), K.mul_comm (x (j + 1))]; exact this
      have iy := F.inv_pos _ (hy k) (hyne k (Nat.le_of_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt h))))
      have iy1 := F.inv_pos _ (hy (k + 1)) (hyne (k + 1) (Nat.le_of_lt h))
      have t := O.mul_le_mul_right _ _ _ cross (O.mul_nonneg _ _ iy iy1)
      have e1 : K.mul (K.mul (x (k + 1)) (y k)) (K.mul (F.inv (y k)) (F.inv (y (k + 1)))) = g (k + 1) := by
        show _ = K.mul (x (k + 1)) (F.inv (y (k + 1)))
        rw [show K.mul (K.mul (x (k + 1)) (y k)) (K.mul (F.inv (y k)) (F.inv (y (k + 1))))
          = K.mul (K.mul (x (k + 1)) (F.inv (y (k + 1)))) (K.mul (y k) (F.inv (y k))) by lib_ring,
          F.mul_inv _ (hyne k (Nat.le_of_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt h))))]; lib_ring
      have e2 : K.mul (K.mul (x k) (y (k + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1)))) = g k := by
        show _ = K.mul (x k) (F.inv (y k))
        rw [show K.mul (K.mul (x k) (y (k + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1))))
          = K.mul (K.mul (x k) (F.inv (y k))) (K.mul (y (k + 1)) (F.inv (y (k + 1)))) by lib_ring,
          F.mul_inv _ (hyne (k + 1) (Nat.le_of_lt h))]; lib_ring
      rw [e1, e2] at t; exact t
  have os := one_step_gen K O F X hXg (M + 2) 0 (by rw [hX]; exact Nat.le_add_left _ _) y g hy hg hg0
  have cg_eq : ∀ t, sumN K (fun k => K.mul (K.mul (y k) (CgWindow.Psi K X k t)) (g k)) (M + 3) = cgc K X X t := fun t => by
    rw [HalfRatio.sum_congr K _ (fun k => K.mul (x k) (CgWindow.Psi K X k t)) (M + 3) (fun k hk => by
      show K.mul (K.mul (y k) _) (g k) = _
      rw [show K.mul (K.mul (y k) (CgWindow.Psi K X k t)) (g k) = K.mul (K.mul (y k) (g k)) (CgWindow.Psi K X k t) by lib_ring,
        gy k (Nat.le_of_lt_succ hk)]),
      OneStep.cgc_sum K X X (M + 1) t hX]
    show K.add (sumN K (fun k => K.mul (co K X k) (CgWindow.Psi K X k t)) (M + 2)) (K.mul (co K X (M + 2)) (CgWindow.Psi K X (M + 2) t))
      = sumN K (fun j => K.mul (co K X j) (CgWindow.Psi K X j t)) (M + 2)
    rw [HeatLR.co_out K X (M + 2) (by rw [hX]; exact Nat.le_refl _)]; lib_ring
  have ch1 : sumN K (fun k => K.mul (y k) (CgWindow.Psi K X k (0 + 1))) (M + 3) = K.add (cgc K X X 0) (cgc K X X 2) := by
    have := chi1_sum K X x (M + 1) 0 (hz _ (Nat.le_refl _)) (hz _ (Nat.le_succ _))
    rw [this, sumN_ext K _ (fun j => K.add (K.mul (x j) (CgWindow.Psi K X j 0)) (K.mul (x j) (CgWindow.Psi K X j (0 + 2)))) (fun j => by lib_ring),
      sumN_add, OneStep.cgc_sum K X X (M + 1) 0 hX, OneStep.cgc_sum K X X (M + 1) (0 + 2) hX]
  -- Ψ_0(k) = x_k and Ψ_1(j) = (χ₁x)_j
  have psi0 : ∀ k, CgWindow.Psi K X k 0 = x k := fun k => by
    have := CgWindow.Psi_hi K X 0 k; rw [Nat.zero_add] at this; rw [this]
    show K.add K.zero (co K X (k + 2 * 0)) = co K X k; rw [Nat.mul_zero, Nat.add_zero]; lib_ring
  have psi1 : ∀ j, CgWindow.Psi K X j 1 = y j := fun j => by
    cases j with
    | zero =>
      have := CgWindow.Psi_lo K X 0 1; rw [Nat.zero_add] at this; rw [this]
      show K.add K.zero (co K X (1 + 2 * 0)) = co K X 1; rw [Nat.mul_zero, Nat.add_zero]; lib_ring
    | succ j =>
      have := CgWindow.Psi_hi K X 1 j; rw [show 1 + j = j + 1 by lib_nat] at this; rw [this, Window.P_succ, Window.P_succ]
      show K.add (K.add K.zero (co K X (j + 2 * 0))) (co K X (j + 2 * 1)) = K.add (co K X j) (co K X (j + 2))
      rw [Nat.mul_zero, Nat.add_zero, Nat.mul_one]; lib_ring
  have c1 : sumN K (fun k => K.mul (y k) (CgWindow.Psi K X k 0)) (M + 3) = cgc K X X 1 := by
    rw [OneStep.cgc_sum K X X (M + 1) 1 hX, sumN_ext K _ (fun k => K.mul (x k) (y k)) (fun k => by rw [psi0]; lib_ring),
      sumN_ext K (fun j => K.mul (co K X j) (CgWindow.Psi K X j 1)) (fun k => K.mul (x k) (y k)) (fun k => by rw [psi1])]
    show K.add (sumN K (fun k => K.mul (co K X k) (y k)) (M + 2)) (K.mul (co K X (M + 2)) (y (M + 2))) = sumN K (fun k => K.mul (co K X k) (y k)) (M + 2)
    rw [HeatLR.co_out K X (M + 2) (by rw [hX]; exact Nat.le_refl _)]; lib_ring
  rw [cg_eq, cg_eq, ch1, c1, show (0 : Nat) + 1 = 1 from rfl] at os
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ os (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

theorem term_le_sum (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) : ∀ N j, j < N → O.le (f j) (sumN K f N)
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | N + 1, j, h => by
    show O.le (f j) (K.add (sumN K f N) (f N))
    cases Nat.lt_or_ge j N with
    | inl hl => exact O.le_trans _ _ _ (term_le_sum f hf N j hl) (HeatV.le_add_nn K O _ _ (hf N))
    | inr hg =>
      have e : j = N := Nat.le_antisymm (Nat.le_of_lt_succ h) hg
      rw [e]; have := O.add_le_add _ _ _ _ (OneFactor.sum_nn K O f hf N) (O.le_refl (f N))
      rw [show K.add K.zero (f N) = f N by lib_ring] at this; exact this

/-- the exact (untruncated) product as a list: [c_0, …, c_{2M+2}] for X of length M + 2 -/
def prodL (X : List R) (M : Nat) : List R := ListRow.listF (fun t => cgc K X X t) 0 (2 * M + 3)

theorem cgc_far (X : List R) (M : Nat) (hX : X.length = M + 2) (n : Nat) (hn : 2 * M + 3 ≤ n) : cgc K X X n = K.zero := by
  rw [OneStep.cgc_sum K X X (M + 1) n hX]
  have : ∀ j, j < M + 2 → CgWindow.Psi K X j n = K.zero := fun j hj => by
    have ⟨e, he⟩ := L1Zero.ex_add j n (Nat.le_trans (Nat.le_of_lt (Nat.lt_of_lt_of_le hj (by
      rw [show 2 * M + 3 = (M + 2) + (M + 1) by lib_nat]; exact Nat.le_add_right _ _))) hn)
    subst he
    rw [CgWindow.Psi_lo]
    apply P_zero_far K X e
    rw [hX]
    have h1 : j + (M + 2) ≤ j + e := Nat.le_trans (by
      rw [show 2 * M + 3 = (M + 1) + (M + 2) by lib_nat]
      exact Nat.add_le_add_right (Nat.le_of_lt_succ hj) _) hn
    exact cancel_le j _ _ h1
  rw [HalfRatio.sum_congr K _ (fun _ => K.zero) (M + 2) (fun j hj => by rw [this j hj]; lib_ring)]
  exact CgWindow.sum_co_nil K (fun _ => 0) (M + 2) |> fun _ => by
    have : ∀ N, sumN K (fun _ => (K.zero : R)) N = K.zero := by
      intro N; induction N with
      | zero => rfl
      | succ N ih => show K.add _ K.zero = K.zero; rw [ih]; lib_ring
    exact this _

theorem co_prodL (X : List R) (M : Nat) (hX : X.length = M + 2) (j : Nat) : co K (prodL K X M) j = cgc K X X j := by
  cases Nat.lt_or_ge j (2 * M + 3) with
  | inl h => unfold prodL; rw [ListRow.co_listF K _ _ 0 j h, Nat.zero_add]
  | inr h =>
    rw [cgc_far K X M hX j h]
    exact HeatLR.co_out K _ j (by unfold prodL; rw [OneStep.len_listF]; exact h)

/-- **the bond-moving product of a v-good list is v-good** -/
theorem Good_prod (X : List R) (hXg : Good K O X) (M : Nat) (hX : X.length = M + 2) (F : OField.OF K O) :
    Good K O (prodL K X M) := by
  have ⟨hb, hpos, _, _⟩ := hXg
  have nn : ∀ t, O.le K.zero (cgc K X X t) := fun t => by
    rw [OneStep.cgc_sum K X X (M + 1) t hX]
    exact OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (hb j) (OneStep.Psi_nn' K O X hb j t)) _
  refine ⟨fun j => by rw [co_prodL K X M hX]; exact nn j, ?_, fun m => ?_, ?_⟩
  · intro j hj
    rw [co_prodL K X M hX]
    have hj' : j < 2 * M + 3 := by unfold prodL at hj; rw [OneStep.len_listF] at hj; exact hj
    cases Nat.lt_or_ge j (M + 2) with
    | inl hl =>
      have lo := cgc_ge K O X X (M + 1) hX hb hb j
      exact ne_of_ge K O _ _ (O.mul_nonneg _ _ (hb 0) (hb j)) lo (fun z =>
        hpos j (by rw [hX]; exact hl) (O.pos_mul_eq_zero _ _ (hb 0) (hpos 0 (by rw [hX]; exact Nat.zero_lt_succ _)) z))
    | inr hg =>
      have ⟨d, hd⟩ := L1Zero.ex_add (M + 1) j (Nat.le_trans (Nat.le_succ _) hg)
      have hdl : d < M + 2 := by
        have : M + 1 + d < M + 1 + (M + 2) := by rw [← hd, show M + 1 + (M + 2) = 2 * M + 3 by lib_nat]; exact hj'
        exact Nat.lt_of_add_lt_add_left this
      have t1 := term_le_sum K O (fun k => K.mul (co K X k) (CgWindow.Psi K X k j))
        (fun k => O.mul_nonneg _ _ (hb k) (OneStep.Psi_nn' K O X hb k j)) (M + 2) (M + 1) (Nat.lt_succ_self _)
      rw [← OneStep.cgc_sum K X X (M + 1) j hX] at t1
      have t2 := (Psi_ge K O X hb (M + 1) j).1 (Nat.le_trans (Nat.le_succ _) hg) d hd
      have t3 := O.mul_le_mul_right _ _ _ t2 (hb (M + 1))
      have lo : O.le (K.mul (co K X d) (co K X (M + 1))) (cgc K X X j) := by
        refine O.le_trans _ _ _ t3 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) t1); lib_ring
      exact ne_of_ge K O _ _ (O.mul_nonneg _ _ (hb d) (hb (M + 1))) lo (fun z =>
        hpos d (by rw [hX]; exact hdl) (O.pos_mul_eq_zero _ _ (hb (M + 1)) (hpos (M + 1) (by rw [hX]; exact Nat.lt_succ_self _))
          (by rw [K.mul_comm]; exact z)))
  · unfold HalfRatio.VV
    rw [co_prodL K X M hX, co_prodL K X M hX, co_prodL K X M hX, co_prodL K X M hX]
    exact prod_VV K O F X hXg M hX m
  · rw [co_prodL K X M hX, co_prodL K X M hX, co_prodL K X M hX]
    exact prod_base K O F X hXg M hX

end L1Full

#print axioms L1Full.pairwise
#print axioms L1Full.P_ge_first
#print axioms L1Full.P_zero_far
#print axioms L1Full.one_step_eq
#print axioms L1Full.cg_comm
#print axioms L1Full.cgc_comm
#print axioms L1Full.l1
#print axioms L1Full.l1_in
#print axioms L1Full.Psi_rec
#print axioms L1Full.Psi_rec0
#print axioms L1Full.chi1_sum
#print axioms L1Full.one_step_gen
#print axioms L1Full.prod_VV
#print axioms L1Full.prod_base
#print axioms L1Full.cgc_far
#print axioms L1Full.Good_prod
#print axioms L1Full.cancel_add
#print axioms L1Full.cancel_le
#print axioms L1Full.Psi_ge
#print axioms L1Full.ne_of_ge
#print axioms L1Full.dist_symm
#print axioms L1Full.cancel_add_r
#print axioms L1Full.cgc_ge
#print axioms L1Full.term_le_sum
#print axioms L1Full.co_prodL

#print axioms: 25 theorems, no axioms.

90.84 Decimation keeps \(v\)-monotonicity

For \(a\) nonincreasing and log-concave, \(v\)-monotonicity of \((n+1)a_n\) passes to \((n+1)a_n^4\).

\[ F(2)=(\tfrac 1y+x)F(1)+\alpha '(y-\tfrac 1y)(y-x)+\beta '(\tfrac 1y-\tfrac 1z)(x-\tfrac 1z)\ \ge 0 \]

ym/DecVV.lean

/-
  ym/DecVV — T45, G44 (decimation keeps v-monotonicity): squaring step. With x ≤ y, P ≤ x, r ≤ P (P = 1/y, r = 1/z
  in the t-variables t_n = a_n/a_{n+1}), and coefficients with α′ − α − β + β′ = 0,
    α′y − αx − βP + β′r ≥ 0   ⇒   α′y² − αx² − βP² + β′r² ≥ 0,
  because Goal = (P + x)·H1 + α′(y − P)(y − x) + β′(P − r)(x − r) (a Descartes-type quadratic multiplier).
  Lean 4 core only, no axioms.
-/
import ym.L1Full

namespace DecVV

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem sq_step (x y P r a b a' b' : R) (hxy : O.le x y) (hPx : O.le P x) (hrP : O.le r P) (hP0 : O.le K.zero P)
    (hr0 : O.le K.zero r) (ha' : O.le K.zero a') (hb' : O.le K.zero b')
    (h0 : K.add (K.add a' (K.neg a)) (K.add (K.neg b) b') = K.zero)
    (h1 : O.le K.zero (K.add (K.add (K.mul a' y) (K.neg (K.mul a x))) (K.add (K.neg (K.mul b P)) (K.mul b' r)))) :
    O.le K.zero (K.add (K.add (K.mul a' (K.mul y y)) (K.neg (K.mul a (K.mul x x)))) (K.add (K.neg (K.mul b (K.mul P P))) (K.mul b' (K.mul r r)))) := by
  have hyP : O.le K.zero (K.add y (K.neg P)) := HeatV.sub_nn K O _ _ (O.le_trans _ _ _ hPx hxy)
  have hyx : O.le K.zero (K.add y (K.neg x)) := HeatV.sub_nn K O _ _ hxy
  have hPr : O.le K.zero (K.add P (K.neg r)) := HeatV.sub_nn K O _ _ hrP
  have hxr : O.le K.zero (K.add x (K.neg r)) := HeatV.sub_nn K O _ _ (O.le_trans _ _ _ hrP hPx)
  have hx0 : O.le K.zero x := O.le_trans _ _ _ hP0 hPx
  have t1 := O.mul_nonneg _ _ (O.add_le_add _ _ _ _ hP0 hx0 |> fun h => by rw [show K.add K.zero K.zero = K.zero by lib_ring] at h; exact h) h1
  have t2 := O.mul_nonneg _ _ ha' (O.mul_nonneg _ _ hyP hyx)
  have t3 := O.mul_nonneg _ _ hb' (O.mul_nonneg _ _ hPr hxr)
  have s := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ t1 t2) t3
  rw [show K.add (K.add K.zero K.zero) K.zero = K.zero by lib_ring] at s
  refine O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_)
  -- use h0 : a' = a + b − b'
  have ea : a' = K.add (K.add a b) (K.neg b') := by
    have e2 : a' = K.add (K.add (K.add a' (K.neg a)) (K.add (K.neg b) b')) (K.add (K.add a b) (K.neg b')) := by lib_ring
    rw [h0] at e2; rw [e2]; lib_ring
  rw [ea]; lib_ring

/-- **cleared squaring step** for Y_n = (n+1) a_n: with A_i = a_{m+i}, M = m + 1 (as a ring element),
  VV_m of (n+1)a_n, a nonincreasing (A2 ≤ A1) and log-concave (A0A2 ≤ A1², A1A3 ≤ A2², A1A3 ≤ A0A2) ⇒ VV_m of (n+1)a_n² -/
theorem vv_sq (M A0 A1 A2 A3 : R) (hM : O.le K.zero M) (h1 : O.le K.zero K.one)
    (h0 : O.le K.zero A0) (h2 : O.le K.zero A2) (hA21 : O.le A2 A1) (h21 : O.le K.zero A2)
    (lc1 : O.le (K.mul A0 A2) (K.mul A1 A1)) (lc2 : O.le (K.mul A1 A3) (K.mul A2 A2)) (lc3 : O.le (K.mul A1 A3) (K.mul A0 A2))
    (vv : O.le (K.mul (K.add (K.mul M A0) (K.mul (K.add M (K.add K.one K.one)) A2)) (K.mul (K.add M (K.add K.one K.one)) A2))
      (K.mul (K.add (K.mul (K.add M K.one) A1) (K.mul (K.add M (K.add K.one (K.add K.one K.one))) A3)) (K.mul (K.add M K.one) A1))) :
    O.le (K.mul (K.add (K.mul M (K.mul A0 A0)) (K.mul (K.add M (K.add K.one K.one)) (K.mul A2 A2))) (K.mul (K.add M (K.add K.one K.one)) (K.mul A2 A2)))
      (K.mul (K.add (K.mul (K.add M K.one) (K.mul A1 A1)) (K.mul (K.add M (K.add K.one (K.add K.one K.one))) (K.mul A3 A3))) (K.mul (K.add M K.one) (K.mul A1 A1))) := by
  have H := HeatV.sub_nn K O _ _ vv
  have m1 : O.le K.zero (K.add M K.one) := by have := O.add_le_add _ _ _ _ hM h1; rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  have m3 : O.le K.zero (K.add M (K.add K.one (K.add K.one K.one))) := by
    have := O.add_le_add _ _ _ _ hM (O.add_le_add _ _ _ _ h1 (O.add_le_add _ _ _ _ h1 h1))
    rw [show K.add K.zero (K.add K.zero (K.add K.zero K.zero)) = K.zero by lib_ring] at this; exact this
  have hA1 : O.le K.zero A1 := O.le_trans _ _ _ h2 hA21
  have sA : O.le K.zero (K.add (K.mul A1 A1) (K.neg (K.mul A2 A2))) :=
    HeatV.sub_nn K O _ _ (O.le_trans _ _ _ (O.mul_le_mul_right _ _ _ hA21 h2) (by rw [K.mul_comm A1 A2]; exact O.mul_le_mul_right _ _ _ hA21 hA1))
  have t1 := O.mul_nonneg _ _ (O.add_le_add _ _ _ _ (O.mul_nonneg _ _ h2 h2) (O.mul_nonneg _ _ h0 h2) |> fun h => by rw [show K.add K.zero K.zero = K.zero by lib_ring] at h; exact h) H
  have t2 := O.mul_nonneg _ _ (O.mul_nonneg _ _ m1 m1) (O.mul_nonneg _ _ sA (HeatV.sub_nn K O _ _ lc1))
  have t3 := O.mul_nonneg _ _ (O.mul_nonneg _ _ m1 m3) (O.mul_nonneg _ _ (HeatV.sub_nn K O _ _ lc2) (HeatV.sub_nn K O _ _ lc3))
  have s := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ t1 t2) t3
  rw [show K.add (K.add K.zero K.zero) K.zero = K.zero by lib_ring] at s
  have key : O.le K.zero (K.add (K.mul (K.add (K.mul (K.add M K.one) (K.mul A1 A1)) (K.mul (K.add M (K.add K.one (K.add K.one K.one))) (K.mul A3 A3))) (K.mul (K.add M K.one) (K.mul A1 A1)))
      (K.neg (K.mul (K.add (K.mul M (K.mul A0 A0)) (K.mul (K.add M (K.add K.one K.one)) (K.mul A2 A2))) (K.mul (K.add M (K.add K.one K.one)) (K.mul A2 A2))))) := by
    refine O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_); lib_ring
  have := O.add_le_add _ _ _ _ key (O.le_refl (K.mul (K.add (K.mul M (K.mul A0 A0)) (K.mul (K.add M (K.add K.one K.one)) (K.mul A2 A2))) (K.mul (K.add M (K.add K.one K.one)) (K.mul A2 A2))))
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

theorem sq_mono (u v : R) (hu : O.le K.zero u) (huv : O.le u v) : O.le (K.mul u u) (K.mul v v) := by
  have hv : O.le K.zero v := O.le_trans _ _ _ hu huv
  have s1 := O.mul_le_mul_right _ _ _ huv hu
  have s2 := O.mul_le_mul_right _ _ _ huv hv
  rw [K.mul_comm v u] at s1
  exact O.le_trans _ _ _ s1 s2

/-- **base condition under squaring**: 4a₁² ≤ (a₀ + 3a₂)a₀ ⇒ 4a₁⁴ ≤ (a₀² + 3a₂²)a₀² -/
theorem base_sq (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (a0 a1 a2 : R) (h0 : O.le K.zero a0) (ha1 : O.le K.zero a1)
    (h2 : O.le K.zero a2)
    (hb : O.le (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul a1 a1))
      (K.mul (K.add a0 (K.mul (K.add K.one (K.add K.one K.one)) a2)) a0)) :
    O.le (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (K.mul a1 a1) (K.mul a1 a1)))
      (K.mul (K.add (K.mul a0 a0) (K.mul (K.add K.one (K.add K.one K.one)) (K.mul a2 a2))) (K.mul a0 a0)) := by
  have four : O.le K.zero (K.add (K.add K.one K.one) (K.add K.one K.one)) := by
    have := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ h1 h1) (O.add_le_add _ _ _ _ h1 h1)
    rw [show K.add (K.add K.zero K.zero) (K.add K.zero K.zero) = K.zero by lib_ring] at this; exact this
  have four_ne : K.add (K.add K.one K.one) (K.add K.one K.one) ≠ K.zero := by
    intro e
    have : O.le K.one (K.add (K.add K.one K.one) (K.add K.one K.one)) := by
      have := O.add_le_add _ _ _ _ (O.le_refl K.one) (O.add_le_add _ _ _ _ h1 (O.add_le_add _ _ _ _ h1 h1))
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
    rw [e] at this; exact h10 (O.le_antisymm _ _ this h1)
  have hl : O.le K.zero (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul a1 a1)) :=
    O.mul_nonneg _ _ four (O.mul_nonneg _ _ ha1 ha1)
  have sq := sq_mono K O _ _ hl hb
  have three : O.le K.zero (K.add (K.add K.one K.one) K.one) := by
    have := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ h1 h1) h1
    rw [show K.add (K.add K.zero K.zero) K.zero = K.zero by lib_ring] at this; exact this
  have dd : O.le K.zero (K.mul (K.add a0 (K.neg a2)) (K.add a0 (K.neg a2))) := by
    cases O.le_total a2 a0 with
    | inl h => have hn := HeatV.sub_nn K O _ _ h; exact O.mul_nonneg _ _ hn hn
    | inr h =>
      have hn := HeatV.sub_nn K O _ _ h
      have := O.mul_nonneg _ _ hn hn
      rw [show K.mul (K.add a2 (K.neg a0)) (K.add a2 (K.neg a0)) = K.mul (K.add a0 (K.neg a2)) (K.add a0 (K.neg a2)) by lib_ring] at this; exact this
  have d2 := O.mul_nonneg _ _ (O.mul_nonneg _ _ three dd) (O.mul_nonneg _ _ h0 h0)
  have t := O.add_le_add _ _ _ _ sq d2
  apply O.pos_cancel _ _ _ four four_ne
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ t (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · lib_ring

/-- vv_sq with the third log-concavity condition derived: A1A3 ≤ A2² ≤ A0A2 when A2 ≤ A0 -/
theorem vv_sq' (M A0 A1 A2 A3 : R) (hM : O.le K.zero M) (h1 : O.le K.zero K.one)
    (h0 : O.le K.zero A0) (h2 : O.le K.zero A2) (hA21 : O.le A2 A1) (hA20 : O.le A2 A0)
    (lc1 : O.le (K.mul A0 A2) (K.mul A1 A1)) (lc2 : O.le (K.mul A1 A3) (K.mul A2 A2))
    (vv : O.le (K.mul (K.add (K.mul M A0) (K.mul (K.add M (K.add K.one K.one)) A2)) (K.mul (K.add M (K.add K.one K.one)) A2))
      (K.mul (K.add (K.mul (K.add M K.one) A1) (K.mul (K.add M (K.add K.one (K.add K.one K.one))) A3)) (K.mul (K.add M K.one) A1))) :
    O.le (K.mul (K.add (K.mul M (K.mul A0 A0)) (K.mul (K.add M (K.add K.one K.one)) (K.mul A2 A2))) (K.mul (K.add M (K.add K.one K.one)) (K.mul A2 A2)))
      (K.mul (K.add (K.mul (K.add M K.one) (K.mul A1 A1)) (K.mul (K.add M (K.add K.one (K.add K.one K.one))) (K.mul A3 A3))) (K.mul (K.add M K.one) (K.mul A1 A1))) := by
  have lc3 : O.le (K.mul A1 A3) (K.mul A0 A2) :=
    O.le_trans _ _ _ lc2 (by rw [K.mul_comm A0 A2]; exact (by
      have := O.mul_le_mul_right _ _ _ hA20 h2; rw [K.mul_comm A0 A2] at this; rw [K.mul_comm A2 A0]
      exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (K.mul_comm _ _)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (K.mul_comm _ _)))))
  exact vv_sq K O M A0 A1 A2 A3 hM h1 h0 h2 hA21 h2 lc1 lc2 lc3 vv

/-- Y_n = (n+1) a_n -/
def Yd (a : Nat → R) (n : Nat) : R := K.mul (cN K (n + 1)) (a n)

theorem cN_shift (m k : Nat) : cN K (m + 1 + k) = K.add (cN K (m + 1)) (cN K k) := cN_add K (m + 1) k

/-- **squaring keeps v-monotonicity** at every m (sequence form) -/
theorem sq_VV (h1 : O.le K.zero K.one) (a : Nat → R) (ha : ∀ n, O.le K.zero (a n))
    (hdec : ∀ n, O.le (a (n + 1)) (a n)) (hlc : ∀ n, O.le (K.mul (a n) (a (n + 2))) (K.mul (a (n + 1)) (a (n + 1))))
    (hv : ∀ m, HalfRatio.VV K O (Yd K a) m) (m : Nat) : HalfRatio.VV K O (Yd K (fun n => K.mul (a n) (a n))) m := by
  have v := hv m
  unfold HalfRatio.VV Yd at *
  have n2 : cN K 2 = K.add K.one K.one := by rw [show (2 : Nat) = 1 + 1 from rfl, cN_add K 1 1, SU2CharExp.cN_one]
  have n3 : cN K 3 = K.add K.one (K.add K.one K.one) := by rw [show (3 : Nat) = 2 + 1 from rfl, cN_add K 2 1, n2, SU2CharExp.cN_one]; lib_ring
  have c2 : cN K (m + 2 + 1) = K.add (cN K (m + 1)) (K.add K.one K.one) := by
    rw [show m + 2 + 1 = m + 1 + 2 by lib_nat, cN_shift K m 2, n2]
  have c1 : cN K (m + 1 + 1) = K.add (cN K (m + 1)) K.one := by rw [cN_add K (m + 1) 1, SU2CharExp.cN_one]
  have c3 : cN K (m + 3 + 1) = K.add (cN K (m + 1)) (K.add K.one (K.add K.one K.one)) := by
    rw [show m + 3 + 1 = m + 1 + 3 by lib_nat, cN_shift K m 3, n3]
  rw [c1, c2, c3] at v ⊢
  have hA20 : O.le (a (m + 2)) (a m) := O.le_trans _ _ _ (hdec (m + 1)) (hdec m)
  have l1 := hlc m
  have l2 := hlc (m + 1)
  rw [show m + 1 + 2 = m + 3 from rfl, show m + 1 + 1 = m + 2 from rfl] at l2
  exact vv_sq' K O (cN K (m + 1)) (a m) (a (m + 1)) (a (m + 2)) (a (m + 3)) (SU2CharExp.cN_nn K O h1 _) h1
    (ha m) (ha (m + 2)) (hdec (m + 1)) hA20 l1 l2 v

/-- the base condition Y₁² ≤ (Y₀ + Y₂)Y₀ for Y = (n+1)a survives squaring -/
theorem sq_base (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (a : Nat → R) (ha : ∀ n, O.le K.zero (a n))
    (hb : O.le (K.mul (Yd K a 1) (Yd K a 1)) (K.mul (K.add (Yd K a 0) (Yd K a 2)) (Yd K a 0))) :
    O.le (K.mul (Yd K (fun n => K.mul (a n) (a n)) 1) (Yd K (fun n => K.mul (a n) (a n)) 1))
      (K.mul (K.add (Yd K (fun n => K.mul (a n) (a n)) 0) (Yd K (fun n => K.mul (a n) (a n)) 2)) (Yd K (fun n => K.mul (a n) (a n)) 0)) := by
  have n1 : cN K 1 = K.one := SU2CharExp.cN_one K
  have n2 : cN K 2 = K.add K.one K.one := by rw [show (2 : Nat) = 1 + 1 from rfl, cN_add K 1 1, n1]
  have n3 : cN K 3 = K.add K.one (K.add K.one K.one) := by rw [show (3 : Nat) = 2 + 1 from rfl, cN_add K 2 1, n2, n1]; lib_ring
  unfold Yd at *
  rw [show (0 : Nat) + 1 = 1 from rfl, show (1 : Nat) + 1 = 2 from rfl, show (2 : Nat) + 1 = 3 from rfl, n1, n2, n3] at hb ⊢
  have hb' : O.le (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (a 1) (a 1)))
      (K.mul (K.add (a 0) (K.mul (K.add K.one (K.add K.one K.one)) (a 2))) (a 0)) := by
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ hb (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  have t := base_sq K O h1 h10 (a 0) (a 1) (a 2) (ha 0) (ha 1) (ha 2) hb'
  show O.le (K.mul (K.mul (K.add K.one K.one) (K.mul (a 1) (a 1))) (K.mul (K.add K.one K.one) (K.mul (a 1) (a 1))))
    (K.mul (K.add (K.mul K.one (K.mul (a 0) (a 0))) (K.mul (K.add K.one (K.add K.one K.one)) (K.mul (a 2) (a 2)))) (K.mul K.one (K.mul (a 0) (a 0))))
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ t (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

/-- **decimation keeps the flow class**: (n+1)a v-monotone + base, a ≥ 0 nonincreasing log-concave ⇒ the same for
  (n+1)a⁴ (= Y⁴/(n+1)³, the decimated weight) -/
theorem dec_good (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (a : Nat → R) (ha : ∀ n, O.le K.zero (a n))
    (hdec : ∀ n, O.le (a (n + 1)) (a n)) (hlc : ∀ n, O.le (K.mul (a n) (a (n + 2))) (K.mul (a (n + 1)) (a (n + 1))))
    (hv : ∀ m, HalfRatio.VV K O (Yd K a) m)
    (hb : O.le (K.mul (Yd K a 1) (Yd K a 1)) (K.mul (K.add (Yd K a 0) (Yd K a 2)) (Yd K a 0))) :
    let a4 := fun n => K.mul (K.mul (a n) (a n)) (K.mul (a n) (a n))
    (∀ m, HalfRatio.VV K O (Yd K a4) m) ∧
      O.le (K.mul (Yd K a4 1) (Yd K a4 1)) (K.mul (K.add (Yd K a4 0) (Yd K a4 2)) (Yd K a4 0)) := by
  let a2 := fun n => K.mul (a n) (a n)
  have ha2 : ∀ n, O.le K.zero (a2 n) := fun n => O.mul_nonneg _ _ (ha n) (ha n)
  have hdec2 : ∀ n, O.le (a2 (n + 1)) (a2 n) := fun n => sq_mono K O _ _ (ha _) (hdec n)
  have hlc2 : ∀ n, O.le (K.mul (a2 n) (a2 (n + 2))) (K.mul (a2 (n + 1)) (a2 (n + 1))) := fun n => by
    have := sq_mono K O _ _ (O.mul_nonneg _ _ (ha n) (ha (n + 2))) (hlc n)
    show O.le (K.mul (K.mul (a n) (a n)) (K.mul (a (n + 2)) (a (n + 2)))) (K.mul (K.mul (a (n + 1)) (a (n + 1))) (K.mul (a (n + 1)) (a (n + 1))))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  have hv2 : ∀ m, HalfRatio.VV K O (Yd K a2) m := sq_VV K O h1 a ha hdec hlc hv
  have hb2 := sq_base K O h1 h10 a ha hb
  exact ⟨sq_VV K O h1 a2 ha2 hdec2 hlc2 hv2, sq_base K O h1 h10 a2 ha2 hb2⟩

end DecVV

#print axioms DecVV.sq_step
#print axioms DecVV.vv_sq
#print axioms DecVV.base_sq
#print axioms DecVV.vv_sq'
#print axioms DecVV.sq_VV
#print axioms DecVV.sq_base
#print axioms DecVV.dec_good
#print axioms DecVV.sq_mono
#print axioms DecVV.cN_shift

#print axioms: 9 theorems, no axioms.

90.85 Bond moving keeps \(a=c/(n+1)\) nonincreasing

The bond-moving product of a \(v\)-monotone list with \(a\) nonincreasing has \(a\) nonincreasing.

\[ \sum _k (k+1)\,\Psi _t(k)=(t+1)\sum _j (j+1)X_j \]

ym/DimRow.lean

/-
  ym/DimRow — T45, G44 (A, nonincreasing half): the CG row sums of the dimension list d_k = k + 1 are
  Ψ^d_t(j) = (j+1)(t+1), so Σ_k (k+1) Ψ^X_t(k) = (t+1) Σ_j (j+1) X_j. Lean 4 core only, no axioms.
-/
import ym.DecVV

namespace DimRow

open YMChain LinkIntegral SU2Char QBinom HeatPair

variable {R : Type} (K : DetChain.CRing R)

theorem arith (c : Nat) : ∀ l, sumN K (fun i => cN K (c + 2 * i + 1)) l = cN K (l * (c + l))
  | 0 => by show K.zero = cN K (0 * (c + 0)); rw [Nat.zero_mul]; rfl
  | l + 1 => by
    show K.add (sumN K (fun i => cN K (c + 2 * i + 1)) l) (cN K (c + 2 * l + 1)) = _
    rw [arith c l, ← cN_add, show l * (c + l) + (c + 2 * l + 1) = (l + 1) * (c + (l + 1)) by lib_nat]

theorem P_congr2 (f g : Nat → R) (c : Nat) : ∀ l, (∀ j, j + 2 ≤ c + 2 * l → f j = g j) → Window.P K f c l = Window.P K g c l
  | 0, _ => rfl
  | l + 1, h => by
    rw [Window.P_succ, Window.P_succ, P_congr2 f g c l (fun j hj => h j (Nat.le_trans hj (by
      rw [show c + 2 * (l + 1) = c + 2 * l + 2 by lib_nat]; exact Nat.le_add_right _ _))),
      h (c + 2 * l) (by rw [show c + 2 * (l + 1) = c + 2 * l + 2 by lib_nat]; exact Nat.le_refl _)]

/-- the dimension list cut at N -/
def dims (N : Nat) : List R := ListRow.listF (fun k => cN K (k + 1)) 0 N

theorem P_dims (N c l : Nat) (h : c + 2 * l ≤ N + 1) :
    Window.P K (co K (dims K N)) c l = cN K (l * (c + l)) := by
  rw [P_congr2 K _ (fun k => cN K (k + 1)) c l (fun j hj => by
    unfold dims; rw [ListRow.co_listF K _ N 0 j (Nat.lt_of_succ_le (Nat.le_of_succ_le_succ (Nat.le_trans hj h))), Nat.zero_add])]
  unfold Window.P
  rw [sumN_ext K _ (fun i => cN K (c + 2 * i + 1)) (fun i => rfl)]
  exact arith K c l

/-- **Ψ^d_t(j) = (j+1)(t+1)** for a long enough dimension list -/
theorem Psi_dims (N j t : Nat) (h : j + t + 1 ≤ N) : CgWindow.Psi K (dims K N) j t = cN K ((j + 1) * (t + 1)) := by
  cases Nat.lt_or_ge j t with
  | inl hl =>
    have ⟨e, he⟩ := L1Zero.ex_add j t (Nat.le_of_lt hl)
    subst he
    rw [CgWindow.Psi_lo, P_dims K N e (j + 1) (by
      rw [show e + 2 * (j + 1) = j + (j + e) + 1 + 1 by lib_nat]; exact Nat.succ_le_succ h)]
    rw [show (j + 1) * (e + (j + 1)) = (j + 1) * (j + e + 1) by lib_nat]
  | inr hg =>
    have ⟨d, hd⟩ := L1Zero.ex_add t j hg
    subst hd
    rw [CgWindow.Psi_hi, P_dims K N d (t + 1) (by
      rw [show d + 2 * (t + 1) = t + d + t + 1 + 1 by lib_nat]; exact Nat.succ_le_succ h)]
    rw [show (t + 1) * (d + (t + 1)) = (t + d + 1) * (t + 1) by lib_nat]

/-- **dimension sum**: Σ_{k<N} (k+1) Ψ^X_t(k) = (t+1) Σ_j (j+1) X_j  (N ≥ |X| + t) -/
theorem dimsum (X : List R) (M t N : Nat) (hX : X.length = M + 2) (hN : M + 1 + t + 1 ≤ N) :
    sumN K (fun k => K.mul (cN K (k + 1)) (CgWindow.Psi K X k t)) N
      = K.mul (cN K (t + 1)) (sumN K (fun j => K.mul (co K X j) (cN K (j + 1))) (M + 2)) := by
  have e1 : ∀ k, K.mul (cN K (k + 1)) (CgWindow.Psi K X k t)
      = sumN K (fun j => K.mul (co K X j) (K.mul (cN K (k + 1)) (cN K (Fusion.cg j k t)))) (M + 2) := fun k => by
    show K.mul (cN K (k + 1)) (dot K 0 X (fun j => cN K (Fusion.cg k j t))) = _
    rw [TripleA.dot_sumN, hX, ← sumN_mul]
    exact sumN_ext K _ _ (fun j => by rw [Nat.zero_add, L1Full.cg_comm]; lib_ring) _
  rw [sumN_ext K _ _ e1 N, ← AbelLR.sum_swap]
  have e2 : ∀ j, j < M + 2 → sumN K (fun k => K.mul (co K X j) (K.mul (cN K (k + 1)) (cN K (Fusion.cg j k t)))) N
      = K.mul (cN K (t + 1)) (K.mul (co K X j) (cN K (j + 1))) := fun j hj => by
    rw [sumN_mul]
    have ps := Psi_dims K N j t (Nat.le_trans (Nat.add_le_add_right (Nat.add_le_add_right (Nat.le_of_lt_succ hj) t) 1) hN)
    have : CgWindow.Psi K (dims K N) j t = sumN K (fun k => K.mul (cN K (k + 1)) (cN K (Fusion.cg j k t))) N := by
      show dot K 0 (dims K N) (fun k => cN K (Fusion.cg j k t)) = _
      rw [TripleA.dot_sumN, show (dims K N).length = N from OneStep.len_listF _ N 0]
      exact HalfRatio.sum_congr K _ _ N (fun k hk => by
        rw [Nat.zero_add]; unfold dims; rw [ListRow.co_listF K _ N 0 k hk, Nat.zero_add])
    rw [← this, ps, SU2CharExp.cN_mul]; lib_ring
  rw [HalfRatio.sum_congr K _ _ (M + 2) e2, sumN_mul]

variable (O : ORing K)

theorem cN_pos (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (k : Nat) : cN K (k + 1) ≠ K.zero := by
  intro e
  have lo : O.le K.one (cN K (k + 1)) := by
    rw [cN_add, SU2CharExp.cN_one]
    have := O.add_le_add _ _ _ _ (SU2CharExp.cN_nn K O h1 k) (O.le_refl K.one)
    rw [show K.add K.zero K.one = K.one by lib_ring] at this; exact this
  rw [e] at lo; exact h10 (O.le_antisymm _ _ lo h1)

/-- **the product keeps a = c/(n+1) nonincreasing**: X v-good with X_{k+1}(k+1) ≤ X_k(k+2) ⇒ same for c = X·X -/
theorem prod_anoninc (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X : List R)
    (hXg : L1Full.Good K O X) (M : Nat) (hX : X.length = M + 2)
    (ha : ∀ k, O.le (K.mul (co K X (k + 1)) (cN K (k + 1))) (K.mul (co K X k) (cN K (k + 2)))) (n : Nat) :
    O.le (K.mul (MigdalCone.cgc K X X (n + 1)) (cN K (n + 1))) (K.mul (MigdalCone.cgc K X X n) (cN K (n + 2))) := by
  have ⟨hb, hpos, _, _⟩ := hXg
  let y := fun k => cN K (k + 1)
  have hy : ∀ k, O.le K.zero (y k) := fun k => SU2CharExp.cN_nn K O h1 _
  have hyne : ∀ k, y k ≠ K.zero := fun k => cN_pos K O h1 h10 k
  let g := fun k => K.mul (co K X k) (F.inv (y k))
  have hg0 : ∀ k, O.le K.zero (g k) := fun k => O.mul_nonneg _ _ (hb k) (F.inv_pos _ (hy k) (hyne k))
  have gy : ∀ k, K.mul (y k) (g k) = co K X k := fun k => by
    show K.mul (y k) (K.mul (co K X k) (F.inv (y k))) = _
    rw [show K.mul (y k) (K.mul (co K X k) (F.inv (y k))) = K.mul (co K X k) (K.mul (y k) (F.inv (y k))) by lib_ring, F.mul_inv _ (hyne k)]; lib_ring
  have hg : ∀ k, O.le (g (k + 1)) (g k) := fun k => by
    have t := O.mul_le_mul_right _ _ _ (ha k) (O.mul_nonneg _ _ (F.inv_pos _ (hy k) (hyne k)) (F.inv_pos _ (hy (k + 1)) (hyne (k + 1))))
    have e1 : K.mul (K.mul (co K X (k + 1)) (cN K (k + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1)))) = g (k + 1) := by
      show _ = K.mul (co K X (k + 1)) (F.inv (y (k + 1)))
      rw [show K.mul (K.mul (co K X (k + 1)) (cN K (k + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1))))
        = K.mul (K.mul (co K X (k + 1)) (F.inv (y (k + 1)))) (K.mul (y k) (F.inv (y k))) by show _ = K.mul _ (K.mul (cN K (k + 1)) _); lib_ring,
        F.mul_inv _ (hyne k)]; lib_ring
    rw [show k + 2 = k + 1 + 1 from rfl] at t
    have e2 : K.mul (K.mul (co K X k) (cN K (k + 1 + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1)))) = g k := by
      show _ = K.mul (co K X k) (F.inv (y k))
      rw [show K.mul (K.mul (co K X k) (cN K (k + 1 + 1))) (K.mul (F.inv (y k)) (F.inv (y (k + 1))))
        = K.mul (K.mul (co K X k) (F.inv (y k))) (K.mul (y (k + 1)) (F.inv (y (k + 1)))) by show _ = K.mul _ (K.mul (cN K (k + 1 + 1)) _); lib_ring,
        F.mul_inv _ (hyne (k + 1))]; lib_ring
    rw [e1, e2] at t; exact t
  have os := L1Full.one_step_gen K O F X hXg (n + M + 2) n (by rw [hX]; exact Nat.le_of_eq (by lib_nat)) y g hy hg hg0
  -- Σ (y Ψ_t) g = c_t
  have cg_eq : ∀ t, sumN K (fun k => K.mul (K.mul (y k) (CgWindow.Psi K X k t)) (g k)) (n + M + 2 + 1) = MigdalCone.cgc K X X t := fun t => by
    rw [OneStep.cgc_sum K X X (M + 1) t hX, show n + M + 2 + 1 = (M + 1 + 1) + (n + 1) by lib_nat, OneFactor.sum_split]
    rw [HalfRatio.sum_congr K _ (fun j => K.mul (co K X j) (CgWindow.Psi K X j t)) (M + 1 + 1) (fun k _ => by
      show K.mul (K.mul (y k) _) (g k) = _
      rw [show K.mul (K.mul (y k) (CgWindow.Psi K X k t)) (g k) = K.mul (K.mul (y k) (g k)) (CgWindow.Psi K X k t) by lib_ring, gy])]
    have tail : sumN K (fun j => K.mul (K.mul (y (M + 1 + 1 + j)) (CgWindow.Psi K X (M + 1 + 1 + j) t)) (g (M + 1 + 1 + j))) (n + 1) = K.zero := by
      rw [HalfRatio.sum_congr K _ (fun _ => K.zero) (n + 1) (fun j _ => by
        rw [show K.mul (K.mul (y (M + 1 + 1 + j)) (CgWindow.Psi K X (M + 1 + 1 + j) t)) (g (M + 1 + 1 + j))
          = K.mul (K.mul (y (M + 1 + 1 + j)) (g (M + 1 + 1 + j))) (CgWindow.Psi K X (M + 1 + 1 + j) t) by lib_ring, gy,
          HeatLR.co_out K X _ (by rw [hX]; exact Nat.le_add_right _ _)]; lib_ring)]
      have : ∀ N, sumN K (fun _ => (K.zero : R)) N = K.zero := by
        intro N; induction N with
        | zero => rfl
        | succ N ih => show K.add _ K.zero = K.zero; rw [ih]; lib_ring
      exact this _
    rw [tail]; lib_ring
  have dn := dimsum K X M n (n + M + 2 + 1) hX (by rw [show n + M + 2 + 1 = M + 1 + n + 1 + 1 by lib_nat]; exact Nat.le_succ _)
  have dn1 := dimsum K X M (n + 1) (n + M + 2 + 1) hX (by rw [show n + M + 2 + 1 = M + 1 + (n + 1) + 1 by lib_nat]; exact Nat.le_refl _)
  have ey : ∀ t, sumN K (fun k => K.mul (y k) (CgWindow.Psi K X k t)) (n + M + 2 + 1) = sumN K (fun k => K.mul (cN K (k + 1)) (CgWindow.Psi K X k t)) (n + M + 2 + 1) := fun t => rfl
  rw [cg_eq, cg_eq, ey, ey, dn, dn1] at os
  -- cancel E = Σ X_j (j+1) > 0
  have hE : O.le K.zero (sumN K (fun j => K.mul (co K X j) (cN K (j + 1))) (M + 2)) :=
    OneFactor.sum_nn K O _ (fun j => O.mul_nonneg _ _ (hb j) (hy j)) _
  have hEne : sumN K (fun j => K.mul (co K X j) (cN K (j + 1))) (M + 2) ≠ K.zero := by
    have lo := L1Full.term_le_sum K O (fun j => K.mul (co K X j) (cN K (j + 1))) (fun j => O.mul_nonneg _ _ (hb j) (hy j)) (M + 2) 0 (Nat.zero_lt_succ _)
    exact L1Full.ne_of_ge K O _ _ (O.mul_nonneg _ _ (hb 0) (hy 0)) lo (fun z =>
      hpos 0 (by rw [hX]; exact Nat.zero_lt_succ _) (O.pos_mul_eq_zero _ _ (hy 0) (hyne 0) (by rw [K.mul_comm]; exact z)))
  rw [show n + 2 = n + 1 + 1 from rfl]
  apply GridQ.cancel K O F _ _ _ hE hEne
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ os (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

/-- **the decimated list in the (n+1)a⁴ form**: co(decM Y)_n = (n+1) a_n⁴ with a_n = Y_n/(n+1) -/
theorem decM_Yd (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Y : List R) (n : Nat) :
    co K (MigdalCone.decM K O F 0 Y) n
      = K.mul (cN K (n + 1)) (K.mul (K.mul (K.mul (co K Y n) (F.inv (cN K (n + 1)))) (K.mul (co K Y n) (F.inv (cN K (n + 1)))))
          (K.mul (K.mul (co K Y n) (F.inv (cN K (n + 1)))) (K.mul (co K Y n) (F.inv (cN K (n + 1)))))) := by
  rw [DecLR.co_decM, Nat.zero_add]
  have e := F.mul_inv _ (cN_pos K O h1 h10 n)
  show K.mul (K.mul (K.mul (K.mul (K.mul K.one (co K Y n)) (co K Y n)) (co K Y n)) (co K Y n))
      (K.mul (K.mul (K.mul K.one (F.inv (cN K (n + 1)))) (F.inv (cN K (n + 1)))) (F.inv (cN K (n + 1)))) = _
  generalize F.inv (cN K (n + 1)) = i at e ⊢
  generalize cN K (n + 1) = d at e ⊢
  have r : K.mul d (K.mul (K.mul (K.mul (co K Y n) i) (K.mul (co K Y n) i)) (K.mul (K.mul (co K Y n) i) (K.mul (co K Y n) i)))
      = K.mul (K.mul d i) (K.mul (K.mul (K.mul (K.mul (co K Y n) (co K Y n)) (co K Y n)) (co K Y n)) (K.mul (K.mul i i) i)) := by lib_ring
  rw [r, e]; lib_ring

/-- **decimation keeps the flow class (list form)**: with a_n = Y_n/(n+1) ≥ 0 nonincreasing and log-concave, if the
  coefficients of Y are v-monotone with the base condition then so are those of decM Y -/
theorem decM_good (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Y : List R)
    (ha : ∀ n, O.le K.zero (K.mul (co K Y n) (F.inv (cN K (n + 1)))))
    (hdec : ∀ n, O.le (K.mul (co K Y (n + 1)) (F.inv (cN K (n + 1 + 1)))) (K.mul (co K Y n) (F.inv (cN K (n + 1)))))
    (hlc : ∀ n, O.le (K.mul (K.mul (co K Y n) (F.inv (cN K (n + 1)))) (K.mul (co K Y (n + 2)) (F.inv (cN K (n + 2 + 1)))))
      (K.mul (K.mul (co K Y (n + 1)) (F.inv (cN K (n + 1 + 1)))) (K.mul (co K Y (n + 1)) (F.inv (cN K (n + 1 + 1))))))
    (hv : ∀ m, HalfRatio.VV K O (co K Y) m)
    (hb : O.le (K.mul (co K Y 1) (co K Y 1)) (K.mul (K.add (co K Y 0) (co K Y 2)) (co K Y 0))) :
    (∀ m, HalfRatio.VV K O (co K (MigdalCone.decM K O F 0 Y)) m) ∧
      O.le (K.mul (co K (MigdalCone.decM K O F 0 Y) 1) (co K (MigdalCone.decM K O F 0 Y) 1))
        (K.mul (K.add (co K (MigdalCone.decM K O F 0 Y) 0) (co K (MigdalCone.decM K O F 0 Y) 2)) (co K (MigdalCone.decM K O F 0 Y) 0)) := by
  let a := fun n => K.mul (co K Y n) (F.inv (cN K (n + 1)))
  have ya : ∀ n, DecVV.Yd K a n = co K Y n := fun n => by
    show K.mul (cN K (n + 1)) (K.mul (co K Y n) (F.inv (cN K (n + 1)))) = co K Y n
    rw [show K.mul (cN K (n + 1)) (K.mul (co K Y n) (F.inv (cN K (n + 1)))) = K.mul (co K Y n) (K.mul (cN K (n + 1)) (F.inv (cN K (n + 1)))) by lib_ring,
      F.mul_inv _ (cN_pos K O h1 h10 n)]; lib_ring
  have hv' : ∀ m, HalfRatio.VV K O (DecVV.Yd K a) m := fun m => by
    unfold HalfRatio.VV; rw [ya, ya, ya, ya]; exact hv m
  have hb' : O.le (K.mul (DecVV.Yd K a 1) (DecVV.Yd K a 1)) (K.mul (K.add (DecVV.Yd K a 0) (DecVV.Yd K a 2)) (DecVV.Yd K a 0)) := by
    rw [ya, ya, ya]; exact hb
  have dg := DecVV.dec_good K O h1 h10 a ha hdec hlc hv' hb'
  have eq : ∀ n, co K (MigdalCone.decM K O F 0 Y) n = DecVV.Yd K (fun n => K.mul (K.mul (a n) (a n)) (K.mul (a n) (a n))) n :=
    fun n => decM_Yd K O F h1 h10 Y n
  refine ⟨fun m => ?_, ?_⟩
  · unfold HalfRatio.VV; rw [eq, eq, eq, eq]; exact dg.1 m
  · rw [eq, eq, eq]; exact dg.2

end DimRow

#print axioms DimRow.arith
#print axioms DimRow.Psi_dims
#print axioms DimRow.dimsum
#print axioms DimRow.prod_anoninc
#print axioms DimRow.decM_Yd
#print axioms DimRow.decM_good
#print axioms DimRow.P_congr2
#print axioms DimRow.P_dims
#print axioms DimRow.cN_pos

#print axioms: 9 theorems, no axioms.

90.86 First log-concavity step of the bond-moving product

For a \(v\)-monotone list \(X\) the product \(c=X\cdot X\) satisfies \(a_1^2\ge a_0a_2\) for \(a_n=c_n/(n+1)\).

\[ 3c_1^2-4c_0c_2=4\sum _{w\ge 1}\sum _x\bigl (S_w(x)^2-D_w(x)^2\bigr )\ \ge 0 \]

ym/LC0.lean

/-
  ym/LC0 — T45, G44 (A) at n = 0: the bond-moving product c = X·X of a v-good list satisfies 3c₁² ≥ 4c₀c₂,
  i.e. a = c/(n+1) is log-concave at its first interior index (a₁² ≥ a₀a₂). Proof: two Lagrange identities and the
  telescoping of same-width atoms (Lagrange.core0_sums) applied to Y = (0, X₀, X₁, …). Lean 4 core only, no axioms.
-/
import ym.Lagrange

namespace LC0

open YMChain LinkIntegral SU2Char QBinom HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem sum_zero_tail (f : Nat → R) (M : Nat) (hf : ∀ i, M ≤ i → f i = K.zero) : ∀ T, sumN K f (M + T) = sumN K f M
  | 0 => rfl
  | T + 1 => by
    show K.add (sumN K f (M + T)) (f (M + T)) = sumN K f M
    rw [sum_zero_tail f M hf T, hf (M + T) (Nat.le_add_right _ _)]; lib_ring

theorem sum_shift0 (f : Nat → R) (h0 : f 0 = K.zero) (N : Nat) : sumN K f (N + 1) = sumN K (fun i => f (i + 1)) N := by
  rw [sumN_shift, h0]; lib_ring

theorem chi_pair (x : Nat → R) : ∀ N,
    sumN K (fun j => K.mul (x j) (L1Full.chi1f K x j)) (N + 1)
      = K.add (K.mul (K.add K.one K.one) (sumN K (fun j => K.mul (x j) (x (j + 1))) (N + 1))) (K.neg (K.mul (x N) (x (N + 1))))
  | 0 => by
    show K.add K.zero (K.mul (x 0) (x 1)) = K.add (K.mul (K.add K.one K.one) (K.add K.zero (K.mul (x 0) (x 1)))) (K.neg (K.mul (x 0) (x 1)))
    lib_ring
  | N + 1 => by
    show K.add (sumN K (fun j => K.mul (x j) (L1Full.chi1f K x j)) (N + 1)) (K.mul (x (N + 1)) (K.add (x N) (x (N + 2))))
      = K.add (K.mul (K.add K.one K.one) (K.add (sumN K (fun j => K.mul (x j) (x (j + 1))) (N + 1)) (K.mul (x (N + 1)) (x (N + 2)))))
          (K.neg (K.mul (x (N + 1)) (x (N + 2))))
    rw [chi_pair x N]; lib_ring

theorem psi0 (X : List R) (k : Nat) : CgWindow.Psi K X k 0 = co K X k := by
  have := CgWindow.Psi_hi K X 0 k; rw [Nat.zero_add] at this; rw [this]
  show K.add K.zero (co K X (k + 2 * 0)) = co K X k; rw [Nat.mul_zero, Nat.add_zero]; lib_ring

theorem psi1 (X : List R) (j : Nat) : CgWindow.Psi K X j 1 = L1Full.chi1f K (co K X) j := by
  cases j with
  | zero =>
    have := CgWindow.Psi_lo K X 0 1; rw [Nat.zero_add] at this; rw [this]
    show K.add K.zero (co K X (1 + 2 * 0)) = co K X 1; rw [Nat.mul_zero, Nat.add_zero]; lib_ring
  | succ j =>
    have := CgWindow.Psi_hi K X 1 j; rw [show 1 + j = j + 1 by lib_nat] at this; rw [this, Window.P_succ, Window.P_succ]
    show K.add (K.add K.zero (co K X (j + 2 * 0))) (co K X (j + 2 * 1)) = K.add (co K X j) (co K X (j + 2))
    rw [Nat.mul_zero, Nat.add_zero, Nat.mul_one]; lib_ring

/-- **a log-concave at the first index for products**: 4 c₀ c₂ ≤ 3 c₁² -/
theorem prod_lc0 (F : OField.OF K O) (X : List R) (hXg : L1Full.Good K O X) (M : Nat) (hX : X.length = M + 2) :
    O.le (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (cgc K X X 0) (cgc K X X 2)))
      (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (cgc K X X 1) (cgc K X X 1))) := by
  have ⟨hb, hpos, hv, h0⟩ := hXg
  let x := co K X
  let Y := CgWindow.Zs K x
  have hz : ∀ t, M + 3 ≤ t → Y t = K.zero := fun t ht => by
    cases t with
    | zero => exact absurd ht (Nat.not_succ_le_zero _)
    | succ t => exact HeatLR.co_out K X t (by rw [hX]; exact Nat.le_of_succ_le_succ ht)
  have hY : ∀ j, O.le K.zero (Y j) := fun j => by cases j with
    | zero => exact O.le_refl _
    | succ j => exact hb j
  have hD : ∀ a k, O.le K.zero (Window.Dl K Y a (a + k)) := fun a k =>
    Window.dl_nn_t K O F Y hY (M + 3)
      (fun j hj => hpos j (by rw [hX]; exact Nat.lt_of_succ_lt_succ hj))
      hz (CgWindow.VV_Zs K O x hv h0) a k
  have core := Lagrange.core0_sums K O Y (M + 3) (M + 3) hz (Nat.le_refl _) hD
  have xz : ∀ i, M + 2 ≤ i → x i = K.zero := fun i hi => HeatLR.co_out K X i (by rw [hX]; exact hi)
  -- c's as sums
  have c0e : cgc K X X 0 = sumN K (fun j => K.mul (x j) (x j)) (M + 2) := by
    rw [OneStep.cgc_sum K X X (M + 1) 0 hX]; exact sumN_ext K _ _ (fun j => by rw [psi0]) _
  have c1e : cgc K X X 1 = sumN K (fun j => K.mul (x j) (L1Full.chi1f K x j)) (M + 2) := by
    rw [OneStep.cgc_sum K X X (M + 1) 1 hX]; exact sumN_ext K _ _ (fun j => by rw [psi1]) _
  have c02 : K.add (cgc K X X 0) (cgc K X X 2) = sumN K (fun k => K.mul (L1Full.chi1f K x k) (L1Full.chi1f K x k)) (M + 3) := by
    have ch := L1Full.chi1_sum K X x (M + 1) 0 (xz _ (Nat.le_refl _)) (xz _ (Nat.le_succ _))
    rw [OneStep.cgc_sum K X X (M + 1) 0 hX, OneStep.cgc_sum K X X (M + 1) 2 hX, ← sumN_add]
    rw [show M + 3 = M + 1 + 2 from rfl, sumN_ext K (fun k => K.mul (L1Full.chi1f K x k) (L1Full.chi1f K x k))
      (fun k => K.mul (L1Full.chi1f K x k) (CgWindow.Psi K X k (0 + 1))) (fun k => by rw [Nat.zero_add, psi1]), ch]
    exact sumN_ext K _ _ (fun j => by rw [show (0 : Nat) + 2 = 2 from rfl]; show _ = K.mul (co K X j) _; lib_ring) _
  -- sums in core
  have z1 : ∀ (f : Nat → R), (∀ i, M + 2 ≤ i → f i = K.zero) → sumN K f (M + 3) = sumN K f (M + 2) :=
    fun f hf => sum_zero_tail K f (M + 2) hf 1
  have eX2 : sumN K (fun i => K.mul (Y (i + 1)) (Y (i + 1))) (M + 3) = cgc K X X 0 := by
    rw [c0e]; exact z1 (fun i => K.mul (x i) (x i)) (fun i hi => by show K.mul (x i) (x i) = K.zero; rw [xz i hi]; lib_ring)
  have eY2 : sumN K (fun i => K.mul (Y i) (Y i)) (M + 3) = cgc K X X 0 := by
    rw [show M + 3 = (M + 2) + 1 from rfl, sum_shift0 K _ (by show K.mul K.zero K.zero = K.zero; lib_ring), c0e]; rfl
  have zchi : ∀ i, K.add (Y i) (Y (i + 2)) = L1Full.chi1f K x i := fun i => by
    cases i with
    | zero => show K.add K.zero (x 1) = x 1; lib_ring
    | succ i => rfl
  have eZ2 : sumN K (fun i => K.mul (K.add (Y i) (Y (i + 2))) (K.add (Y i) (Y (i + 2)))) (M + 3)
      = K.add (cgc K X X 0) (cgc K X X 2) := by
    rw [c02]; exact sumN_ext K _ _ (fun i => by rw [zchi]) _
  have eXZ : sumN K (fun i => K.mul (Y (i + 1)) (K.add (Y i) (Y (i + 2)))) (M + 3) = cgc K X X 1 := by
    rw [c1e, sumN_ext K _ (fun i => K.mul (x i) (L1Full.chi1f K x i)) (fun i => by rw [zchi]; rfl) (M + 3)]
    exact z1 _ (fun i hi => by show K.mul (x i) _ = K.zero; rw [xz i hi]; lib_ring)
  have eYX : sumN K (fun i => K.mul (Y i) (Y (i + 1))) (M + 3) = sumN K (fun j => K.mul (x j) (x (j + 1))) (M + 2) := by
    rw [show M + 3 = (M + 2) + 1 from rfl, sum_shift0 K _ (by show K.mul K.zero (x 0) = K.zero; lib_ring)]; rfl
  have c1P : cgc K X X 1 = K.mul (K.add K.one K.one) (sumN K (fun j => K.mul (x j) (x (j + 1))) (M + 2)) := by
    rw [c1e, show M + 2 = (M + 1) + 1 from rfl, chi_pair, xz (M + 1 + 1) (Nat.le_refl _)]; lib_ring
  rw [eX2, eY2, eZ2, eXZ, eYX] at core
  rw [c1P]
  rw [c1P] at core
  generalize sumN K (fun j => K.mul (x j) (x (j + 1))) (M + 2) = P at core ⊢
  generalize cgc K X X 0 = a at core ⊢
  generalize cgc K X X 2 = b at core ⊢
  have d := HeatV.sub_nn K O _ _ core
  have h1 : O.le K.zero K.one := by
    cases O.le_total K.zero K.one with
    | inl h => exact h
    | inr h =>
      have hn := HeatV.sub_nn K O _ _ h
      have := O.mul_nonneg _ _ hn hn
      rw [show K.mul (K.add K.zero (K.neg K.one)) (K.add K.zero (K.neg K.one)) = K.one by lib_ring] at this; exact this
  have four : O.le K.zero (K.add (K.add K.one K.one) (K.add K.one K.one)) := by
    have := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ h1 h1) (O.add_le_add _ _ _ _ h1 h1)
    rw [show K.add (K.add K.zero K.zero) (K.add K.zero K.zero) = K.zero by lib_ring] at this; exact this
  have t := O.mul_nonneg _ _ four d
  have := O.add_le_add _ _ _ _ t (O.le_refl (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul a b)))
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

end LC0
#print axioms LC0.prod_lc0
#print axioms LC0.sum_zero_tail
#print axioms LC0.sum_shift0
#print axioms LC0.chi_pair
#print axioms LC0.psi0
#print axioms LC0.psi1

#print axioms: 6 theorems, no axioms.

90.87 Second log-concavity step of the bond-moving product

For a \(v\)-monotone list \(X\) of any length the product \(c=X\cdot X\) satisfies \(a_2^2\ge a_1a_3\).

\[ 8c_2^2-9c_1c_3=8\,CB_1+4\,CB_2+4\,CB_3+8\,CB_4,\qquad CB_k=\textstyle \sum _{i<j}M_{ij}M'_{ij}\ge 0 \]

ym/A1.lean

/-
  ym/A1 — T45, G50: (A) at n = 1 for every length. For a v-monotone list X (any length ≥ 2) the bond-moving product
  c = X·X satisfies 9 c₁ c₃ ≤ 8 c₂², i.e. a₂² ≥ a₁ a₃ for a_n = c_n/(n+1).
  Proof: with S⁻¹ = CgWindow.Zs, χ₁x = c1v, ψ₂x = p2v (reflected CG rows),
    8c₂² − 9c₁c₃ = 8·CB(S⁻³x, S⁻²x; S⁻¹x, x) + 4·CB(S⁻³x, S⁻¹χ₁x; S⁻¹x, x) + 4·CB(S⁻²x, S⁻²χ₁x; x, S⁻¹x)
                   + 8·CB(x, S⁻¹x; S⁻¹χ₁x, S⁻¹ψ₂x)
  (CB = 2×2 Cauchy–Binet value, CauchyBinet2), an identity in R₀..R₃, x₀, x₁ (A1Ip, A1C); every integrand is a
  product of two minors of the same sign, each a nonnegative combination of window atoms D = Window.Dl (Zs x).
  Lean 4 core only, no axioms.
-/
import ym.A1C
import ym.CauchyBinet2
import ym.RingZ

namespace A1

open YMChain QBinom CgWindow HeatPair A1Ip MigdalCone

variable {R : Type} (K : DetChain.CRing R)

section ident

variable (x : Nat → R) (L : Nat) (hz : ∀ t, L ≤ t → x t = K.zero)

/-- CB(u,v;u',v') = ⟨u,u'⟩⟨v,v'⟩ − ⟨u,v'⟩⟨v,u'⟩ over [0, L+4) -/
def CB (u v u' v' : Nat → R) : R :=
  K.add (K.mul (ip K u u' (L + 4)) (ip K v v' (L + 4))) (K.neg (K.mul (ip K u v' (L + 4)) (ip K v u' (L + 4))))

include hz in
theorem ident :
    K.add (K.mul (LibRing.castN K 8)
        (K.mul (K.add (K.add (Rk K x L 2) (Rk K x L 2)) (K.add (Rk K x L 0) (K.neg (K.mul (x 0) (x 0)))))
          (K.add (K.add (Rk K x L 2) (Rk K x L 2)) (K.add (Rk K x L 0) (K.neg (K.mul (x 0) (x 0)))))))
      (K.neg (K.mul (LibRing.castN K 9) (K.mul (K.add (Rk K x L 1) (Rk K x L 1))
        (K.add (K.add (Rk K x L 3) (Rk K x L 3)) (K.add (K.add (Rk K x L 1) (Rk K x L 1)) (K.neg (K.add (K.mul (x 0) (x 1)) (K.mul (x 0) (x 1)))))))))
    = K.add (K.add (K.mul (LibRing.castN K 8) (CB K L (Zs K (Zs K (Zs K x))) (Zs K (Zs K x)) (Zs K x) x))
        (K.mul (LibRing.castN K 4) (CB K L (Zs K (Zs K (Zs K x))) (Zs K (c1v K x)) (Zs K x) x)))
      (K.add (K.mul (LibRing.castN K 4) (CB K L (Zs K (Zs K x)) (Zs K (Zs K (c1v K x))) x (Zs K x)))
        (K.mul (LibRing.castN K 8) (CB K L x (Zs K x) (Zs K (c1v K x)) (Zs K (p2v K x))))) := by
  unfold CB
  rw [e1 K x L hz, e2 K x L hz, e3 K x L hz, e4 K x L hz, e5 K x L hz, e6 K x L hz]
  rw [e8 K x L hz, e7 K x L hz, ip_comm K x (Zs K (c1v K x)), e5 K x L hz, e11 K x L hz, e10 K x L hz, e9 K x L hz]
  ring_z

end ident

section signs

variable (O : ORing K) (x : Nat → R)
  (hY : ∀ j, O.le K.zero (Zs K x j))
  (hT : ∀ a w, O.le K.zero (K.add (K.mul (Zs K x (a + 1)) (Zs K x (a + w))) (K.neg (K.mul (Zs K x a) (Zs K x (a + w + 1))))))
  (hD : ∀ a k, O.le K.zero (Window.Dl K (Zs K x) a (a + k)))

theorem nn_neg (a : R) (h : O.le K.zero (K.neg a)) : O.le a K.zero := by
  have := O.add_le_add _ _ _ _ h (O.le_refl a)
  rw [show K.add (K.neg a) a = K.zero by lib_ring, show K.add K.zero a = a by lib_ring] at this; exact this

theorem prod_np (a b : R) (ha : O.le a K.zero) (hb : O.le b K.zero) : O.le K.zero (K.mul a b) := by
  have h1 : O.le K.zero (K.neg a) := by
    have := O.add_le_add _ _ _ _ ha (O.le_refl (K.neg a)); rw [show K.add a (K.neg a) = K.zero by lib_ring, show K.add K.zero (K.neg a) = K.neg a by lib_ring] at this; exact this
  have h2 : O.le K.zero (K.neg b) := by
    have := O.add_le_add _ _ _ _ hb (O.le_refl (K.neg b)); rw [show K.add b (K.neg b) = K.zero by lib_ring, show K.add K.zero (K.neg b) = K.neg b by lib_ring] at this; exact this
  have := O.mul_nonneg _ _ h1 h2
  rw [show K.mul (K.neg a) (K.neg b) = K.mul a b by lib_ring] at this; exact this

theorem neg_nn (a : R) (h : O.le K.zero a) : O.le (K.neg a) K.zero := by
  apply nn_neg K O; rw [show K.neg (K.neg a) = a by lib_ring]; exact h

include hT in
/-- (s1) minor(S⁻¹x, x)(i, i+w+1) = −T(i, w+1) ≤ 0 -/
theorem s1 (i w : Nat) : O.le (Lagrange.minor K (Zs K x) x i (i + w + 1)) K.zero := by
  apply nn_neg K O
  have := hT i (w + 1)
  unfold Lagrange.minor
  rw [show K.neg (K.add (K.mul (Zs K x i) (x (i + w + 1))) (K.neg (K.mul (Zs K x (i + w + 1)) (x i))))
      = K.add (K.mul (Zs K x (i + 1)) (Zs K x (i + (w + 1)))) (K.neg (K.mul (Zs K x i) (Zs K x (i + (w + 1) + 1)))) by
    show _ = K.add (K.mul (x i) (Zs K x (i + w + 1))) (K.neg (K.mul (Zs K x i) (x (i + w + 1)))); lib_ring]
  exact this

include hT in
/-- (s2) minor(S⁻³x, S⁻²x)(i, i+w+1) ≤ 0 -/
theorem s2 (i w : Nat) : O.le (Lagrange.minor K (Zs K (Zs K (Zs K x))) (Zs K (Zs K x)) i (i + w + 1)) K.zero := by
  cases i with
  | zero =>
    show O.le (K.add (K.mul K.zero (Zs K (Zs K x) (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K (Zs K x)) (0 + w + 1)) K.zero))) K.zero
    rw [show K.add (K.mul K.zero (Zs K (Zs K x) (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K (Zs K x)) (0 + w + 1)) K.zero)) = K.zero by lib_ring]
    exact O.le_refl _
  | succ i =>
    cases i with
    | zero =>
      show O.le (K.add (K.mul K.zero (Zs K (Zs K x) (1 + w + 1))) (K.neg (K.mul (Zs K (Zs K (Zs K x)) (1 + w + 1)) K.zero))) K.zero
      rw [show K.add (K.mul K.zero (Zs K (Zs K x) (1 + w + 1))) (K.neg (K.mul (Zs K (Zs K (Zs K x)) (1 + w + 1)) K.zero)) = K.zero by lib_ring]
      exact O.le_refl _
    | succ a =>
      apply nn_neg K O
      have := hT a (w + 1)
      unfold Lagrange.minor
      rw [show a + 1 + 1 + w + 1 = a + w + 1 + 1 + 1 by lib_nat]
      show O.le K.zero (K.neg (K.add (K.mul (Zs K x a) (Zs K x (a + w + 1 + 1))) (K.neg (K.mul (Zs K x (a + w + 1)) (Zs K x (a + 1))))))
      rw [show a + (w + 1) = a + w + 1 by lib_nat] at this
      rw [show K.neg (K.add (K.mul (Zs K x a) (Zs K x (a + w + 1 + 1))) (K.neg (K.mul (Zs K x (a + w + 1)) (Zs K x (a + 1)))))
          = K.add (K.mul (Zs K x (a + 1)) (Zs K x (a + w + 1))) (K.neg (K.mul (Zs K x a) (Zs K x (a + w + 1 + 1)))) by lib_ring]
      exact this

include hT in
theorem Tn (a W n0 n1 n2 n3 : Nat) (e0 : a = n0) (e1 : a + 1 = n1) (e2 : a + W = n2) (e3 : a + W + 1 = n3) :
    O.le K.zero (K.add (K.mul (Zs K x n1) (Zs K x n2)) (K.neg (K.mul (Zs K x n0) (Zs K x n3)))) := by
  rw [← e0, ← e1, ← e2, ← e3]; exact hT a W

theorem le_of_neg_sum (m t : R) (ht : O.le K.zero t) (h : m = K.neg t) : O.le m K.zero := by
  rw [h]; exact neg_nn K O t ht

theorem nn_add (a b : R) (ha : O.le K.zero a) (hb : O.le K.zero b) : O.le K.zero (K.add a b) := by
  have := O.add_le_add _ _ _ _ ha hb; rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this

include hY in
theorem zs_nn (j : Nat) : O.le K.zero (Zs K (Zs K x) j) := by
  cases j with
  | zero => exact O.le_refl _
  | succ j => exact hY j

include hY in
theorem zzs_nn (j : Nat) : O.le K.zero (Zs K (Zs K (Zs K x)) j) := by
  cases j with
  | zero => exact O.le_refl _
  | succ j => exact zs_nn K O x hY j

include hT hY in
/-- (s3) minor(S⁻³x, S⁻¹χ₁x)(i, i+w+1) ≤ 0 -/
theorem s3 (i w : Nat) : O.le (Lagrange.minor K (Zs K (Zs K (Zs K x))) (Zs K (c1v K x)) i (i + w + 1)) K.zero := by
  cases i with
  | zero =>
    show O.le (K.add (K.mul K.zero (Zs K (c1v K x) (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K (Zs K x)) (0 + w + 1)) K.zero))) K.zero
    rw [show K.add (K.mul K.zero (Zs K (c1v K x) (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K (Zs K x)) (0 + w + 1)) K.zero)) = K.zero by lib_ring]
    exact O.le_refl _
  | succ i =>
    cases i with
    | zero =>
      show O.le (K.add (K.mul K.zero (Zs K (c1v K x) (1 + w + 1))) (K.neg (K.mul (Zs K (Zs K (Zs K x)) (1 + w + 1)) (K.add (Zs K x 0) (Zs K x 2))))) K.zero
      apply le_of_neg_sum K O _ (K.mul (Zs K (Zs K (Zs K x)) (1 + w + 1)) (K.add (Zs K x 0) (Zs K x 2)))
        (O.mul_nonneg _ _ (zzs_nn K O x hY _) (nn_add K O _ _ (hY 0) (hY 2)))
      lib_ring
    | succ a =>
      unfold Lagrange.minor
      rw [show a + 1 + 1 + w + 1 = a + w + 2 + 1 by lib_nat]
      show O.le (K.add (K.mul (Zs K x a) (K.add (Zs K x (a + w + 2)) (Zs K x (a + w + 4))))
        (K.neg (K.mul (Zs K x (a + w + 1)) (K.add (Zs K x (a + 1)) (Zs K x (a + 3)))))) K.zero
      cases w with
      | zero =>
        have t1 := Tn K O x hT a 1 a (a + 1) (a + 0 + 1) (a + 0 + 2) rfl rfl (by lib_nat) (by lib_nat)
        have t2 := Tn K O x hT a 3 a (a + 1) (a + 3) (a + 0 + 4) rfl rfl rfl (by lib_nat)
        apply le_of_neg_sum K O _ _ (nn_add K O _ _ t1 t2)
        rw [show a + 0 + 1 = a + 1 by lib_nat]; lib_ring
      | succ w =>
        cases w with
        | zero =>
          have t1 := Tn K O x hT a 2 a (a + 1) (a + (0 + 1) + 1) (a + (0 + 1) + 2) rfl rfl (by lib_nat) (by lib_nat)
          have t2 := Tn K O x hT a 4 a (a + 1) (a + 3 + 1) (a + (0 + 1) + 4) rfl rfl (by lib_nat) (by lib_nat)
          have t3 := Tn K O x hT (a + 1) 2 (a + 1) (a + 1 + 1) (a + 3) (a + (0 + 1) + 3) rfl rfl (by lib_nat) (by lib_nat)
          apply le_of_neg_sum K O _ _ (nn_add K O _ _ (nn_add K O _ _ t1 t2) t3)
          rw [show a + (0 + 1) + 1 = a + 2 by lib_nat, show a + (0 + 1) + 2 = a + 3 by lib_nat, show a + (0 + 1) + 4 = a + 5 by lib_nat,
            show a + 3 + 1 = a + 4 by lib_nat, show a + 1 + 1 = a + 2 by lib_nat, show a + (0 + 1) + 3 = a + 4 by lib_nat]
          lib_ring
        | succ w =>
          have t1 := Tn K O x hT a (w + 3) a (a + 1) (a + (w + 1 + 1) + 1) (a + (w + 1 + 1) + 2) rfl rfl (by lib_nat) (by lib_nat)
          have t2 := Tn K O x hT a (w + 5) a (a + 1) (a + (w + 1 + 1) + 3) (a + (w + 1 + 1) + 4) rfl rfl (by lib_nat) (by lib_nat)
          have t3 := Tn K O x hT (a + 1) (w + 3) (a + 1) (a + 2) (a + (w + 1 + 1) + 2) (a + (w + 1 + 1) + 3) rfl rfl (by lib_nat) (by lib_nat)
          have t4 := Tn K O x hT (a + 2) (w + 1) (a + 2) (a + 3) (a + (w + 1 + 1) + 1) (a + (w + 1 + 1) + 2) rfl rfl (by lib_nat) (by lib_nat)
          apply le_of_neg_sum K O _ _ (nn_add K O _ _ (nn_add K O _ _ (nn_add K O _ _ t1 t2) t3) t4)
          lib_ring

include hD in
theorem Dn (p k n0 n1 n2 m0 m1 m2 : Nat) (e0 : p = n0) (e1 : p + 1 = n1) (e2 : p + 2 = n2)
    (f0 : p + k = m0) (f1 : p + k + 1 = m1) (f2 : p + k + 2 = m2) :
    O.le K.zero (K.add (K.mul (K.add (Zs K x m0) (Zs K x m2)) (Zs K x n1)) (K.neg (K.mul (K.add (Zs K x n0) (Zs K x n2)) (Zs K x m1)))) := by
  rw [← e0, ← e1, ← e2, ← f0, ← f1, ← f2]; exact hD p k

include hD in
/-- (s4) minor(S⁻²x, S⁻²χ₁x)(i, i+w+1) = D ≥ 0 -/
theorem s4 (i w : Nat) : O.le K.zero (Lagrange.minor K (Zs K (Zs K x)) (Zs K (Zs K (c1v K x))) i (i + w + 1)) := by
  cases i with
  | zero =>
    show O.le K.zero (K.add (K.mul K.zero (Zs K (Zs K (c1v K x)) (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K x) (0 + w + 1)) K.zero)))
    rw [show K.add (K.mul K.zero (Zs K (Zs K (c1v K x)) (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K x) (0 + w + 1)) K.zero)) = K.zero by lib_ring]
    exact O.le_refl _
  | succ i =>
    cases i with
    | zero =>
      show O.le K.zero (K.add (K.mul K.zero (Zs K (Zs K (c1v K x)) (1 + w + 1))) (K.neg (K.mul (Zs K (Zs K x) (1 + w + 1)) K.zero)))
      rw [show K.add (K.mul K.zero (Zs K (Zs K (c1v K x)) (1 + w + 1))) (K.neg (K.mul (Zs K (Zs K x) (1 + w + 1)) K.zero)) = K.zero by lib_ring]
      exact O.le_refl _
    | succ a =>
      unfold Lagrange.minor
      rw [show a + 1 + 1 + w + 1 = a + w + 1 + 1 + 1 by lib_nat]
      show O.le K.zero (K.add (K.mul (Zs K x (a + 1)) (K.add (Zs K x (a + w + 1)) (Zs K x (a + w + 1 + 2))))
        (K.neg (K.mul (Zs K x (a + w + 1 + 1)) (K.add (Zs K x a) (Zs K x (a + 2))))))
      have d := Dn K O x hD a (w + 1) a (a + 1) (a + 2) (a + w + 1) (a + w + 1 + 1) (a + w + 1 + 2) rfl rfl rfl (by lib_nat) (by lib_nat) (by lib_nat)
      rw [show K.add (K.mul (Zs K x (a + 1)) (K.add (Zs K x (a + w + 1)) (Zs K x (a + w + 1 + 2))))
          (K.neg (K.mul (Zs K x (a + w + 1 + 1)) (K.add (Zs K x a) (Zs K x (a + 2)))))
        = K.add (K.mul (K.add (Zs K x (a + w + 1)) (Zs K x (a + w + 1 + 2))) (Zs K x (a + 1)))
          (K.neg (K.mul (K.add (Zs K x a) (Zs K x (a + 2))) (Zs K x (a + w + 1 + 1)))) by lib_ring]
      exact d

theorem nn_of_eq (a b : R) (hb : O.le K.zero b) (h : b = a) : O.le K.zero a := h ▸ hb

include hD in
/-- (s5) minor(S⁻¹χ₁x, S⁻¹ψ₂x)(i, i+w+1) ≥ 0: a sum of five window atoms -/
theorem s5 (i w : Nat) : O.le K.zero (Lagrange.minor K (Zs K (c1v K x)) (Zs K (p2v K x)) i (i + w + 1)) := by
  cases i with
  | zero =>
    show O.le K.zero (K.add (K.mul K.zero (Zs K (p2v K x) (0 + w + 1))) (K.neg (K.mul (Zs K (c1v K x) (0 + w + 1)) K.zero)))
    exact nn_of_eq K O _ _ (O.le_refl K.zero) (by lib_ring)
  | succ a =>
    unfold Lagrange.minor
    cases a with
    | zero =>
      rw [show 0 + 1 + w + 1 = w + 1 + 1 by lib_nat]
      cases w with
      | zero =>
        show O.le K.zero (K.add (K.mul (K.add K.zero (Zs K x 2)) (K.add (K.add K.zero (Zs K x 2)) (Zs K x 4)))
          (K.neg (K.mul (K.add (Zs K x 1) (Zs K x 3)) (Zs K x 3))))
        have d := Dn K O x hD 1 1 1 2 3 2 3 4 rfl rfl rfl rfl rfl rfl
        exact nn_of_eq K O _ _ d (by lib_ring)
      | succ w =>
        rw [show w + 1 + 1 + 1 = w + 2 + 1 by lib_nat]
        show O.le K.zero (K.add (K.mul (K.add K.zero (Zs K x 2)) (K.add (K.add (Zs K x (w + 1)) (Zs K x (w + 3))) (Zs K x (w + 5))))
          (K.neg (K.mul (K.add (Zs K x (w + 2)) (Zs K x (w + 4))) (Zs K x 3))))
        have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 2)) (Zs K x (w + 4))) (Zs K x 1)) (K.neg (K.mul (K.add K.zero (Zs K x 2)) (Zs K x (w + 3))))) :=
          Dn K O x hD 0 (w + 2) 0 1 2 (w + 2) (w + 3) (w + 4) rfl rfl rfl (by lib_nat) (by lib_nat) (by lib_nat)
        have d2 := Dn K O x hD 1 w 1 2 3 (w + 1) (w + 2) (w + 3) rfl rfl rfl (by lib_nat) (by lib_nat) (by lib_nat)
        have d3 := Dn K O x hD 1 (w + 2) 1 2 3 (w + 3) (w + 4) (w + 5) rfl rfl rfl (by lib_nat) (by lib_nat) (by lib_nat)
        exact nn_of_eq K O _ _ (nn_add K O _ _ (nn_add K O _ _ d1 d2) d3) (by lib_ring)
    | succ a =>
      rw [show a + 1 + 1 + w + 1 = a + w + 2 + 1 by lib_nat]
      show O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3)))
          (K.add (K.add (Zs K x (a + w + 1)) (Zs K x (a + w + 3))) (Zs K x (a + w + 5))))
        (K.neg (K.mul (K.add (Zs K x (a + w + 2)) (Zs K x (a + w + 4))) (K.add (K.add (Zs K x a) (Zs K x (a + 2))) (Zs K x (a + 4))))))
      have d1 := Dn K O x hD a (w + 1) a (a + 1) (a + 2) (a + w + 1) (a + w + 2) (a + w + 3) rfl rfl rfl (by lib_nat) (by lib_nat) (by lib_nat)
      have d2 := Dn K O x hD a (w + 3) a (a + 1) (a + 2) (a + w + 3) (a + w + 4) (a + w + 5) rfl rfl rfl (by lib_nat) (by lib_nat) (by lib_nat)
      have d5 := Dn K O x hD (a + 2) (w + 1) (a + 2) (a + 3) (a + 4) (a + w + 3) (a + w + 4) (a + w + 5) rfl rfl rfl (by lib_nat) (by lib_nat) (by lib_nat)
      cases w with
      | zero =>
        show O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3)))
            (K.add (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))))
          (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (K.add (K.add (Zs K x a) (Zs K x (a + 2))) (Zs K x (a + 4))))))
        have e1 := Dn K O x hD a 1 a (a + 1) (a + 2) (a + 1) (a + 2) (a + 3) rfl rfl rfl rfl rfl rfl
        have e2 := Dn K O x hD a 3 a (a + 1) (a + 2) (a + 3) (a + 4) (a + 5) rfl rfl rfl rfl rfl rfl
        have e5 := Dn K O x hD (a + 2) 1 (a + 2) (a + 3) (a + 4) (a + 3) (a + 4) (a + 5) rfl rfl rfl (by lib_nat) (by lib_nat) (by lib_nat)
        exact nn_of_eq K O _ _ (nn_add K O _ _ (nn_add K O _ _ e1 e2) e5) (by lib_ring)
      | succ w =>
        have d3 := Dn K O x hD (a + 1) (w + 2) (a + 1) (a + 2) (a + 3) (a + (w + 1) + 2) (a + (w + 1) + 3) (a + (w + 1) + 4) rfl (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
        have d4 := Dn K O x hD (a + 2) w (a + 2) (a + 3) (a + 4) (a + (w + 1) + 1) (a + (w + 1) + 2) (a + (w + 1) + 3) rfl rfl rfl (by lib_nat) (by lib_nat) (by lib_nat)
        exact nn_of_eq K O _ _ (nn_add K O _ _ (nn_add K O _ _ (nn_add K O _ _ (nn_add K O _ _ d1 d2) d3) d4) d5) (by lib_ring)

end signs

section final

variable (O : ORing K)

theorem le_of_sub_nn (a b : R) (h : O.le K.zero (K.add a (K.neg b))) : O.le b a := by
  have := O.add_le_add _ _ _ _ h (O.le_refl b)
  rw [show K.add K.zero b = b by lib_ring, show K.add (K.add a (K.neg b)) b = a by lib_ring] at this; exact this

theorem pos_mul (c v : R) (hc : O.le K.zero c) (hv : O.le K.zero v) : O.le K.zero (K.mul c v) := O.mul_nonneg _ _ hc hv

/-- integrand helper: i < j gives j = i + w + 1 -/
theorem split_lt (P : Nat → Nat → Prop) (h : ∀ i w, P i (i + w + 1)) : ∀ i j, i < j → P i j := by
  intro i j hij
  have ⟨w, hw⟩ := Nat.le.dest hij
  rw [← hw, show i + 1 + w = i + w + 1 by lib_nat]; exact h i w

/-- **(A) at n = 1 for every length**: a v-good X gives 9 c₁ c₃ ≤ 8 c₂² for c = X·X -/
theorem prod_lc1 (F : OField.OF K O) (h1 : O.le K.zero K.one) (X : List R) (hXg : L1Full.Good K O X) (M : Nat) (hX : X.length = M + 2) :
    O.le (K.mul (LibRing.castN K 9) (K.mul (cgc K X X 1) (cgc K X X 3)))
      (K.mul (LibRing.castN K 8) (K.mul (cgc K X X 2) (cgc K X X 2))) := by
  have ⟨hb, hpos, hv, h0⟩ := hXg
  let x := co K X
  have hzY : ∀ t, M + 3 ≤ t → Zs K x t = K.zero := fun t ht => by
    cases t with
    | zero => exact absurd ht (Nat.not_succ_le_zero _)
    | succ t => exact HeatLR.co_out K X t (by rw [hX]; exact Nat.le_of_succ_le_succ ht)
  have hY : ∀ j, O.le K.zero (Zs K x j) := fun j => by cases j with
    | zero => exact O.le_refl _
    | succ j => exact hb j
  have hD : ∀ a k, O.le K.zero (Window.Dl K (Zs K x) a (a + k)) := fun a k =>
    Window.dl_nn_t K O F (Zs K x) hY (M + 3)
      (fun j hj => hpos j (by rw [hX]; exact Nat.lt_of_succ_lt_succ hj))
      hzY (CgWindow.VV_Zs K O x hv h0) a k
  have hT : ∀ a w, O.le K.zero (K.add (K.mul (Zs K x (a + 1)) (Zs K x (a + w))) (K.neg (K.mul (Zs K x a) (Zs K x (a + w + 1))))) := by
    intro a w
    rw [← Lagrange.tail_minor K (Zs K x) (M + 3) hzY a w (M + 3) (by lib_nat_le)]
    exact CauchyBinet2.sum_nn K O _ (fun i => by rw [show a + w + i = a + i + w by lib_nat]; exact hD (a + i) w) _
  have hxz : ∀ t, M + 1 + 1 ≤ t → x t = K.zero := A1C.hzX K X (M + 1) hX
  have m10 : ∀ i j, i < j → O.le K.zero (Lagrange.minor K x (Zs K x) i j) := split_lt _ (fun i w => by
    have := s1 K O x hT i w
    have e : Lagrange.minor K x (Zs K x) i (i + w + 1) = K.neg (Lagrange.minor K (Zs K x) x i (i + w + 1)) := by
      unfold Lagrange.minor; lib_ring
    rw [e]
    have := O.add_le_add _ _ _ _ this (O.le_refl (K.neg (Lagrange.minor K (Zs K x) x i (i + w + 1))))
    rw [show K.add (Lagrange.minor K (Zs K x) x i (i + w + 1)) (K.neg (Lagrange.minor K (Zs K x) x i (i + w + 1))) = K.zero by lib_ring,
      show K.add K.zero (K.neg (Lagrange.minor K (Zs K x) x i (i + w + 1))) = K.neg (Lagrange.minor K (Zs K x) x i (i + w + 1)) by lib_ring] at this
    exact this)
  have cb1 := CauchyBinet2.cb_nonneg K O (Zs K (Zs K (Zs K x))) (Zs K (Zs K x)) (Zs K x) x (M + 1 + 1 + 4)
    (split_lt _ (fun i w => prod_np K O _ _ (s2 K O x hT i w) (s1 K O x hT i w)))
  have cb2 := CauchyBinet2.cb_nonneg K O (Zs K (Zs K (Zs K x))) (Zs K (c1v K x)) (Zs K x) x (M + 1 + 1 + 4)
    (split_lt _ (fun i w => prod_np K O _ _ (s3 K O x hY hT i w) (s1 K O x hT i w)))
  have cb3 := CauchyBinet2.cb_nonneg K O (Zs K (Zs K x)) (Zs K (Zs K (c1v K x))) x (Zs K x) (M + 1 + 1 + 4)
    (split_lt _ (fun i w => O.mul_nonneg _ _ (s4 K O x hD i w) (m10 i (i + w + 1) (LibNatLe.le_of_eq_add (show i + w + 1 = i + 1 + w by lib_nat)))))
  have cb4 := CauchyBinet2.cb_nonneg K O x (Zs K x) (Zs K (c1v K x)) (Zs K (p2v K x)) (M + 1 + 1 + 4)
    (split_lt _ (fun i w => O.mul_nonneg _ _ (m10 i (i + w + 1) (LibNatLe.le_of_eq_add (show i + w + 1 = i + 1 + w by lib_nat))) (s5 K O x hD i w)))
  have id := ident K x (M + 1 + 1) hxz
  have tot : O.le K.zero (K.add (K.add (K.mul (LibRing.castN K 8) (CB K (M + 1 + 1) (Zs K (Zs K (Zs K x))) (Zs K (Zs K x)) (Zs K x) x))
        (K.mul (LibRing.castN K 4) (CB K (M + 1 + 1) (Zs K (Zs K (Zs K x))) (Zs K (c1v K x)) (Zs K x) x)))
      (K.add (K.mul (LibRing.castN K 4) (CB K (M + 1 + 1) (Zs K (Zs K x)) (Zs K (Zs K (c1v K x))) x (Zs K x)))
        (K.mul (LibRing.castN K 8) (CB K (M + 1 + 1) x (Zs K x) (Zs K (c1v K x)) (Zs K (p2v K x)))))) :=
    nn_add K O _ _ (nn_add K O _ _ (pos_mul K O _ _ (SU2CharExp.cN_nn K O h1 8) cb1) (pos_mul K O _ _ (SU2CharExp.cN_nn K O h1 4) cb2))
      (nn_add K O _ _ (pos_mul K O _ _ (SU2CharExp.cN_nn K O h1 4) cb3) (pos_mul K O _ _ (SU2CharExp.cN_nn K O h1 8) cb4))
  rw [← id] at tot
  apply le_of_sub_nn K O
  rw [A1C.c1eq K X (M + 1) hX, A1C.c2eq K X (M + 1) hX, A1C.c3eq K X (M + 1) hX]
  exact tot

end final

end A1

#print axioms A1.ident
#print axioms A1.s1
#print axioms A1.s2
#print axioms A1.s3
#print axioms A1.s4
#print axioms A1.s5
#print axioms A1.prod_lc1
#print axioms A1.nn_neg
#print axioms A1.prod_np
#print axioms A1.neg_nn
#print axioms A1.Tn
#print axioms A1.le_of_neg_sum
#print axioms A1.nn_add
#print axioms A1.zs_nn
#print axioms A1.zzs_nn
#print axioms A1.Dn
#print axioms A1.nn_of_eq
#print axioms A1.le_of_sub_nn
#print axioms A1.pos_mul
#print axioms A1.split_lt

#print axioms: 20 theorems, no axioms.

90.88 Third log-concavity step of the bond-moving product

For a \(v\)-monotone list \(X\) of any length the product \(c=X\cdot X\) satisfies \(a_3^2\ge a_2a_4\).

\[ 15c_3^2-16c_2c_4=\textstyle \sum _{k=1}^{7}\gamma _k\,CB_k,\qquad \gamma _k>0,\ CB_k=\sum _{i<j}M_{ij}M'_{ij}\ge 0 \]

ym/A2.lean

/-
  ym/A2 — T45, G53: (A) at n = 2 for every length: for a v-monotone list X the product c = X·X satisfies
  16 c₂ c₄ ≤ 15 c₃², i.e. a₃² ≥ a₂ a₄. Identity (over [0, L+6), S = left shift, S⁻¹ = Zs):
    15c₃² − 16c₂c₄ = 8·CB(S⁻³x,S⁻²x; x,Sx) + 8·CB(S⁻³x,S⁻¹χ₁x; x,Sx) + 4·CB(S⁻³x,ψ₂; x,Sx)
      + 12·CB(S⁻²x,S⁻¹x; S⁻²ψ₃,S⁻²ψ₂) + 8·CB(S⁻²x,S⁻²χ₁x; Sx,x) + 8·CB(x,S⁻¹x; S⁻²χ₁x,S⁻²ψ₂) + 4·Lag(Sx,S²x).
  Lean 4 core only, no axioms.
-/
import ym.A2Ip
import ym.A1

namespace A2

open YMChain QBinom CgWindow A1Ip A2Ip MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R)

section ident

variable (x : Nat → R) (L : Nat) (hz : ∀ t, L ≤ t → x t = K.zero)

def CB (u v u' v' : Nat → R) : R :=
  K.add (K.mul (ip K u u' (L + 6)) (ip K v v' (L + 6))) (K.neg (K.mul (ip K u v' (L + 6)) (ip K v u' (L + 6))))

theorem B1 (k : Nat) : Bs K x k 1 = K.mul (x 0) (x k) := by
  show K.add K.zero (K.mul (x 0) (x (0 + k))) = _; rw [Nat.zero_add]; lib_ring
theorem B2 (k : Nat) : Bs K x k 2 = K.add (K.mul (x 0) (x k)) (K.mul (x 1) (x (1 + k))) := by
  show K.add (K.add K.zero (K.mul (x 0) (x (0 + k)))) (K.mul (x 1) (x (1 + k))) = _; rw [Nat.zero_add]; lib_ring
theorem B3 (k : Nat) : Bs K x k 3 = K.add (K.add (K.mul (x 0) (x k)) (K.mul (x 1) (x (1 + k)))) (K.mul (x 2) (x (2 + k))) := by
  show K.add (K.add (K.add K.zero (K.mul (x 0) (x (0 + k)))) (K.mul (x 1) (x (1 + k)))) (K.mul (x 2) (x (2 + k))) = _; rw [Nat.zero_add]; lib_ring
theorem B4 (k : Nat) : Bs K x k 4 = K.add (K.add (K.add (K.mul (x 0) (x k)) (K.mul (x 1) (x (1 + k)))) (K.mul (x 2) (x (2 + k)))) (K.mul (x 3) (x (3 + k))) := by
  show K.add (K.add (K.add (K.add K.zero (K.mul (x 0) (x (0 + k)))) (K.mul (x 1) (x (1 + k)))) (K.mul (x 2) (x (2 + k)))) (K.mul (x 3) (x (3 + k))) = _
  rw [Nat.zero_add]; lib_ring

theorem Bs11 : Bs K x 1 1 = (K.mul (x 0) (x 1)) := by
  show (K.add K.zero (K.mul (x 0) (x 1))) = _; lib_ring
theorem Bs12 : Bs K x 1 2 = (K.add (K.mul (x 0) (x 1)) (K.mul (x 1) (x 2))) := by
  show (K.add (K.add K.zero (K.mul (x 0) (x 1))) (K.mul (x 1) (x 2))) = _; lib_ring
theorem Bs01 : Bs K x 0 1 = (K.mul (x 0) (x 0)) := by
  show (K.add K.zero (K.mul (x 0) (x 0))) = _; lib_ring
theorem Bs21 : Bs K x 2 1 = (K.mul (x 0) (x 2)) := by
  show (K.add K.zero (K.mul (x 0) (x 2))) = _; lib_ring
theorem Bs22 : Bs K x 2 2 = (K.add (K.mul (x 0) (x 2)) (K.mul (x 1) (x 3))) := by
  show (K.add (K.add K.zero (K.mul (x 0) (x 2))) (K.mul (x 1) (x 3))) = _; lib_ring
theorem Bs04 : Bs K x 0 4 = (K.add (K.add (K.add (K.mul (x 0) (x 0)) (K.mul (x 1) (x 1))) (K.mul (x 2) (x 2))) (K.mul (x 3) (x 3))) := by
  show (K.add (K.add (K.add (K.add K.zero (K.mul (x 0) (x 0))) (K.mul (x 1) (x 1))) (K.mul (x 2) (x 2))) (K.mul (x 3) (x 3))) = _; lib_ring
theorem Bs24 : Bs K x 2 4 = (K.add (K.add (K.add (K.mul (x 0) (x 2)) (K.mul (x 1) (x 3))) (K.mul (x 2) (x 4))) (K.mul (x 3) (x 5))) := by
  show (K.add (K.add (K.add (K.add K.zero (K.mul (x 0) (x 2))) (K.mul (x 1) (x 3))) (K.mul (x 2) (x 4))) (K.mul (x 3) (x 5))) = _; lib_ring
theorem Bs44 : Bs K x 4 4 = (K.add (K.add (K.add (K.mul (x 0) (x 4)) (K.mul (x 1) (x 5))) (K.mul (x 2) (x 6))) (K.mul (x 3) (x 7))) := by
  show (K.add (K.add (K.add (K.add K.zero (K.mul (x 0) (x 4))) (K.mul (x 1) (x 5))) (K.mul (x 2) (x 6))) (K.mul (x 3) (x 7))) = _; lib_ring
theorem Bs02 : Bs K x 0 2 = (K.add (K.mul (x 0) (x 0)) (K.mul (x 1) (x 1))) := by
  show (K.add (K.add K.zero (K.mul (x 0) (x 0))) (K.mul (x 1) (x 1))) = _; lib_ring
theorem Bs03 : Bs K x 0 3 = (K.add (K.add (K.mul (x 0) (x 0)) (K.mul (x 1) (x 1))) (K.mul (x 2) (x 2))) := by
  show (K.add (K.add (K.add K.zero (K.mul (x 0) (x 0))) (K.mul (x 1) (x 1))) (K.mul (x 2) (x 2))) = _; lib_ring

include hz in
theorem ident2 :
    K.add (K.mul (LibRing.castN K 15) (K.mul (K.add (K.add (Rk K x L 3) (Rk K x L 3)) (K.add (K.add (Rk K x L 1) (Rk K x L 1)) (K.neg (K.add (K.mul (x 0) (x 1)) (K.mul (x 0) (x 1)))))) (K.add (K.add (Rk K x L 3) (Rk K x L 3)) (K.add (K.add (Rk K x L 1) (Rk K x L 1)) (K.neg (K.add (K.mul (x 0) (x 1)) (K.mul (x 0) (x 1)))))))) (K.neg (K.mul (LibRing.castN K 16) (K.mul (K.add (K.add (Rk K x L 2) (Rk   -- (+89 chars; full line in the Zenodo source)
    = K.add (K.add (K.add (K.mul (LibRing.castN K 8) (CB K L (Zs K (Zs K (Zs K x))) (Zs K (Zs K x)) x (fun i => x (i + 1))))
          (K.mul (LibRing.castN K 8) (CB K L (Zs K (Zs K (Zs K x))) (Zs K (c1v K x)) x (fun i => x (i + 1)))))
        (K.add (K.mul (LibRing.castN K 4) (CB K L (Zs K (Zs K (Zs K x))) (p2v K x) x (fun i => x (i + 1))))
          (K.mul (LibRing.castN K 12) (CB K L (Zs K (Zs K x)) (Zs K x) (Zs K (Zs K (A1C.p3v K x))) (Zs K (Zs K (p2v K x)))))))
      (K.add (K.add (K.mul (LibRing.castN K 8) (CB K L (Zs K (Zs K x)) (Zs K (Zs K (c1v K x))) (fun i => x (i + 1)) x))
          (K.mul (LibRing.castN K 8) (CB K L x (Zs K x) (Zs K (Zs K (c1v K x))) (Zs K (Zs K (p2v K x))))))
        (K.mul (LibRing.castN K 4) (CB K L (fun i => x (i + 1)) (fun i => x (i + 2)) (fun i => x (i + 1)) (fun i => x (i + 2))))) := by
  unfold CB
  rw [f1 K x L hz, f2 K x L hz, f3 K x L hz, f4 K x L hz, f5 K x L hz, f6 K x L hz, f7 K x L hz, f8 K x L hz,
    f9 K x L hz, f10 K x L hz, f11 K x L hz, f12 K x L hz, f13 K x L hz, f14 K x L hz,
    ip_comm K x (Zs K (Zs K (c1v K x))), f13 K x L hz, f16 K x L hz, f17 K x L hz,
    Rg K x L hz 0 1 6 (fun i => x (i + 1)) (fun i => x (i + 1)) (fun i => rfl) (fun i => rfl),
    Rg K x L hz 0 2 6 (fun i => x (i + 2)) (fun i => x (i + 2)) (fun i => rfl) (fun i => rfl),
    Rg K x L hz 1 1 6 (fun i => x (i + 1)) (fun i => x (i + 2)) (fun i => rfl) (fun i => rfl),
    ip_comm K (fun i => x (i + 2)) (fun i => x (i + 1)),
    Rg K x L hz 1 1 6 (fun i => x (i + 1)) (fun i => x (i + 2)) (fun i => rfl) (fun i => rfl)]
  rw [ip_p4 K x L hz]
  rw [Bs11, Bs12, Bs01, Bs21, Bs22, Bs04, Bs24, Bs44, Bs03, Bs02]
  ring_z

end ident

section signs2

variable (O : ORing K) (x : Nat → R)
  (hx : ∀ j, O.le K.zero (x j))
  (hT : ∀ a w, O.le K.zero (K.add (K.mul (Zs K x (a + 1)) (Zs K x (a + w))) (K.neg (K.mul (Zs K x a) (Zs K x (a + w + 1))))))
  (hD : ∀ a k, O.le K.zero (Window.Dl K (Zs K x) a (a + k)))

theorem sq_nn (a : R) : O.le K.zero (K.mul a a) := by
  cases O.le_total K.zero a with
  | inl h => exact O.mul_nonneg _ _ h h
  | inr h => exact A1.prod_np K O _ _ h h

theorem le_eq (a b c : R) (h : a = b) (hb : O.le b c) : O.le a c := h ▸ hb

include hT in
/-- m₁(p, p+w+1) = x_p x_{p+w+2} − x_{p+w+1} x_{p+1} ≤ 0 -/
theorem m1x (p w : Nat) : O.le (K.add (K.mul (x p) (x (p + w + 1 + 1))) (K.neg (K.mul (x (p + w + 1)) (x (p + 1))))) K.zero := by
  apply A1.nn_neg K O
  have h := hT (p + 1) (w + 1)
  rw [show p + 1 + (w + 1) = p + w + 1 + 1 by lib_nat] at h
  have e : K.add (K.mul (Zs K x (p + 1 + 1)) (Zs K x (p + w + 1 + 1))) (K.neg (K.mul (Zs K x (p + 1)) (Zs K x (p + w + 1 + 1 + 1))))
      = K.add (K.mul (x (p + 1)) (x (p + w + 1))) (K.neg (K.mul (x p) (x (p + w + 1 + 1)))) := rfl
  rw [e] at h
  exact A1.nn_of_eq K O _ _ h (by lib_ring)

include hT in
/-- m_k(p, p+w+1) = x_p x_{p+w+1+k} − x_{p+w+1} x_{p+k} ≤ 0 (ratios over lag k decrease) -/
theorem mk : ∀ k p w, O.le (K.add (K.mul (x p) (x (p + w + 1 + k))) (K.neg (K.mul (x (p + w + 1)) (x (p + k))))) K.zero
  | 0, p, w => by
    show O.le (K.add (K.mul (x p) (x (p + w + 1))) (K.neg (K.mul (x (p + w + 1)) (x p)))) K.zero
    rw [show K.add (K.mul (x p) (x (p + w + 1))) (K.neg (K.mul (x (p + w + 1)) (x p))) = K.zero by lib_ring]
    exact O.le_refl _
  | k + 1, p, w => by
    have a := m1x K O x hT p (w + k)
    have b : O.le (K.add (K.mul (x (p + 1)) (x (p + w + 1 + k))) (K.neg (K.mul (x (p + w + 1)) (x (p + 1 + k))))) K.zero := by
      cases w with
      | zero =>
        rw [show K.add (K.mul (x (p + 1)) (x (p + 0 + 1 + k))) (K.neg (K.mul (x (p + 0 + 1)) (x (p + 1 + k)))) = K.zero by
          rw [show p + 0 + 1 + k = p + 1 + k by lib_nat, show p + 0 + 1 = p + 1 by lib_nat]; lib_ring]
        exact O.le_refl _
      | succ w =>
        have := mk k (p + 1) w
        rw [show p + 1 + w + 1 + k = p + (w + 1) + 1 + k by lib_nat, show p + 1 + w + 1 = p + (w + 1) + 1 by lib_nat] at this
        exact this
    rw [show p + (w + k) + 1 + 1 = p + w + k + 2 by lib_nat, show p + (w + k) + 1 = p + w + k + 1 by lib_nat] at a
    rw [show p + w + 1 + k = p + w + k + 1 by lib_nat, show p + 1 + k = p + k + 1 by lib_nat] at b
    rw [show p + w + 1 + (k + 1) = p + w + k + 2 by lib_nat, show p + (k + 1) = p + k + 1 by lib_nat]
    have s := O.add_le_add _ _ _ _ a b
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at s
    exact le_eq K O _ _ _ (by lib_ring) s

include hT in
theorem u1 (i w : Nat) : O.le (Lagrange.minor K x (fun i => x (i + 1)) i (i + w + 1)) K.zero := m1x K O x hT i w

include hx in
theorem hY : ∀ j, O.le K.zero (Zs K x j)
  | 0 => O.le_refl _
  | j + 1 => hx j

include hx in
theorem p2_nn : ∀ j, O.le K.zero (p2v K x j)
  | 0 => hx 2
  | j + 1 => A1.nn_add K O _ _ (A1.nn_add K O _ _ (hY K O x hx j) (hx (j + 1))) (hx (j + 3))

include hx hT in
theorem u2 (i w : Nat) : O.le (Lagrange.minor K (Zs K (Zs K (Zs K x))) (p2v K x) i (i + w + 1)) K.zero := by
  unfold Lagrange.minor
  have low : ∀ i, O.le (K.add (K.mul K.zero (p2v K x (i + w + 1))) (K.neg (K.mul (Zs K (Zs K (Zs K x)) (i + w + 1)) (p2v K x i)))) K.zero :=
    fun i => A1.le_of_neg_sum K O _ _ (O.mul_nonneg _ _ (A1.zzs_nn K O x (hY K O x hx) (i + w + 1)) (p2_nn K O x hx i)) (by lib_ring)
  cases i with
  | zero => exact low 0
  | succ i =>
    cases i with
    | zero => exact low 1
    | succ i =>
      cases i with
      | zero => exact low 2
      | succ a =>
        rw [show a + 1 + 1 + 1 + w + 1 = a + w + 3 + 1 by lib_nat]
        show O.le (K.add (K.mul (x a) (K.add (K.add (x (a + w + 2)) (x (a + w + 4))) (x (a + w + 6))))
          (K.neg (K.mul (x (a + w + 1)) (K.add (K.add (x (a + 1)) (x (a + 3))) (x (a + 5)))))) K.zero
        have k1 := mk K O x hT 1 a w
        have k3 := mk K O x hT 3 a w
        have k5 := mk K O x hT 5 a w
        rw [show a + w + 1 + 1 = a + w + 2 by lib_nat] at k1
        rw [show a + w + 1 + 3 = a + w + 4 by lib_nat] at k3
        rw [show a + w + 1 + 5 = a + w + 6 by lib_nat] at k5
        have s := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ k1 k3) k5
        rw [show K.add (K.add K.zero K.zero) K.zero = K.zero by lib_ring] at s
        exact le_eq K O _ _ _ (by lib_ring) s

include hx hT in
theorem u3 (i w : Nat) : O.le (Lagrange.minor K (Zs K (Zs K x)) (Zs K x) i (i + w + 1)) K.zero := by
  unfold Lagrange.minor
  cases i with
  | zero =>
    show O.le (K.add (K.mul K.zero (Zs K x (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K x) (0 + w + 1)) K.zero))) K.zero
    rw [show K.add (K.mul K.zero (Zs K x (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K x) (0 + w + 1)) K.zero)) = K.zero by lib_ring]
    exact O.le_refl _
  | succ a =>
    rw [show a + 1 + w + 1 = a + w + 1 + 1 by lib_nat]
    cases a with
    | zero =>
      show O.le (K.add (K.mul K.zero (x (0 + w + 1))) (K.neg (K.mul (x (0 + w)) (x 0)))) K.zero
      exact A1.le_of_neg_sum K O _ _ (O.mul_nonneg _ _ (hx (0 + w)) (hx 0)) (by lib_ring)
    | succ a =>
      show O.le (K.add (K.mul (x a) (x (a + 1 + w + 1))) (K.neg (K.mul (x (a + 1 + w)) (x (a + 1))))) K.zero
      have h := m1x K O x hT a w
      rw [show a + w + 1 + 1 = a + 1 + w + 1 by lib_nat, show a + w + 1 = a + 1 + w by lib_nat] at h
      exact h

include hD in
theorem u4 (i w : Nat) : O.le K.zero (Lagrange.minor K (Zs K (Zs K (c1v K x))) (Zs K (Zs K (p2v K x))) i (i + w + 1)) := by
  cases i with
  | zero =>
    show O.le K.zero (K.add (K.mul K.zero (Zs K (Zs K (p2v K x)) (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K (c1v K x)) (0 + w + 1)) K.zero)))
    exact A1.nn_of_eq K O _ _ (O.le_refl K.zero) (by lib_ring)
  | succ a =>
    rw [show a + 1 + w + 1 = a + w + 1 + 1 by lib_nat]
    exact A1.s5 K O x hD a w

theorem u5 (f g : Nat → R) (i j : Nat) : O.le K.zero (K.mul (Lagrange.minor K f g i j) (Lagrange.minor K f g i j)) := sq_nn K O _

include hD in
theorem u6core (a w : Nat) : O.le K.zero (K.add (K.mul (p2v K x a) (A1C.p3v K x (a + w + 1))) (K.neg (K.mul (p2v K x (a + w + 1)) (A1C.p3v K x a)))) := by
  cases a with
  | zero =>
    cases w with
    | zero =>
      rw [show 0 + 0 + 1 = 1 by lib_nat]
      show O.le K.zero (K.add (K.mul (Zs K x (3)) (K.add (Zs K x (3)) (Zs K x (5)))) (K.neg (K.mul (K.add (K.add K.zero (Zs K x (2))) (Zs K x (4))) (Zs K x (4)))))
      have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (4))))) := A1.Dn K O x hD (2) (1) (2) (3) (4) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
      exact A1.nn_of_eq K O _ _ d0 (by lib_ring)
    | succ w =>
      cases w with
      | zero =>
        rw [show 0 + (0 + 1) + 1 = 2 by lib_nat]
        show O.le K.zero (K.add (K.mul (Zs K x (3)) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6)))) (K.neg (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (Zs K x (4)))))
        have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (4))))) := A1.Dn K O x hD (1) (2) (1) (2) (3) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
        have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (5))))) := A1.Dn K O x hD (2) (2) (2) (3) (4) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
        exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ d0 d1) (by lib_ring)
      | succ w =>
        cases w with
        | zero =>
          rw [show 0 + (0 + 1 + 1) + 1 = 3 by lib_nat]
          show O.le K.zero (K.add (K.mul (Zs K x (3)) (K.add (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (Zs K x (7)))) (K.neg (K.mul (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))) (Zs K x (4)))))
          have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (4))))) := A1.Dn K O x hD (0) (3) (0) (1) (2) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))))) := A1.Dn K O x hD (1) (3) (1) (2) (3) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (4))))) := A1.Dn K O x hD (2) (1) (2) (3) (4) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))) := A1.Dn K O x hD (2) (3) (2) (3) (4) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) (by lib_ring)
        | succ w =>
          rw [show 0 + (w + 1 + 1 + 1) + 1 = w + 4 by lib_nat]
          show O.le K.zero (K.add (K.mul (Zs K x (3)) (K.add (K.add (K.add (Zs K x (w + 2)) (Zs K x (w + 4))) (Zs K x (w + 6))) (Zs K x (w + 8)))) (K.neg (K.mul (K.add (K.add (Zs K x (w + 3)) (Zs K x (w + 5))) (Zs K x (w + 7))) (Zs K x (4)))))
          have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 5))))) := A1.Dn K O x hD (0) (w + 4) (0) (1) (2) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 3)) (Zs K x (w + 5))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 4))))) := A1.Dn K O x hD (1) (w + 2) (1) (2) (3) (w + 3) (w + 4) (w + 5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 6))))) := A1.Dn K O x hD (1) (w + 4) (1) (2) (3) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 2)) (Zs K x (w + 4))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 3))))) := A1.Dn K O x hD (2) (w) (2) (3) (4) (w + 2) (w + 3) (w + 4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 5))))) := A1.Dn K O x hD (2) (w + 2) (2) (3) (4) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 7))))) := A1.Dn K O x hD (2) (w + 4) (2) (3) (4) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) (by lib_ring)
  | succ a =>
    cases a with
    | zero =>
      cases w with
      | zero =>
        rw [show 0 + 1 + 0 + 1 = 2 by lib_nat]
        show O.le K.zero (K.add (K.mul (K.add (K.add K.zero (Zs K x (2))) (Zs K x (4))) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6)))) (K.neg (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (K.add (Zs K x (3)) (Zs K x (5))))))
        have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (3))))) := A1.Dn K O x hD (1) (1) (1) (2) (3) (2) (3) (4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
        have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))))) := A1.Dn K O x hD (1) (3) (1) (2) (3) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
        have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (5))))) := A1.Dn K O x hD (3) (1) (3) (4) (5) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
        exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) (by lib_ring)
      | succ w =>
        cases w with
        | zero =>
          rw [show 0 + 1 + (0 + 1) + 1 = 3 by lib_nat]
          show O.le K.zero (K.add (K.mul (K.add (K.add K.zero (Zs K x (2))) (Zs K x (4))) (K.add (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (Zs K x (7)))) (K.neg (K.mul (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))) (K.add (Zs K x (3)) (Zs K x (5))))))
          have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (3))))) := A1.Dn K O x hD (0) (2) (0) (1) (2) (2) (3) (4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (5))))) := A1.Dn K O x hD (0) (4) (0) (1) (2) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (4))))) := A1.Dn K O x hD (1) (2) (1) (2) (3) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (6))))) := A1.Dn K O x hD (1) (4) (1) (2) (3) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (5))))) := A1.Dn K O x hD (2) (2) (2) (3) (4) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (6))))) := A1.Dn K O x hD (3) (2) (3) (4) (5) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) (by lib_ring)
        | succ w =>
          cases w with
          | zero =>
            rw [show 0 + 1 + (0 + 1 + 1) + 1 = 4 by lib_nat]
            show O.le K.zero (K.add (K.mul (K.add (K.add K.zero (Zs K x (2))) (Zs K x (4))) (K.add (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))) (Zs K x (8)))) (K.neg (K.mul (K.add (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (7))) (K.add (Zs K x (3)) (Zs K x (5))))))
            have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (4))))) := A1.Dn K O x hD (0) (3) (0) (1) (2) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (6))))) := A1.Dn K O x hD (0) (5) (0) (1) (2) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (3))))) := A1.Dn K O x hD (1) (1) (1) (2) (3) (2) (3) (4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))))) := A1.Dn K O x hD (1) (3) (1) (2) (3) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))))) := A1.Dn K O x hD (1) (3) (1) (2) (3) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (7))))) := A1.Dn K O x hD (1) (5) (1) (2) (3) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d6 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))) := A1.Dn K O x hD (2) (3) (2) (3) (4) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d7 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (5))))) := A1.Dn K O x hD (3) (1) (3) (4) (5) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d8 : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (7))))) := A1.Dn K O x hD (3) (3) (3) (4) (5) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) (by lib_ring)
          | succ w =>
            rw [show 0 + 1 + (w + 1 + 1 + 1) + 1 = w + 5 by lib_nat]
            show O.le K.zero (K.add (K.mul (K.add (K.add K.zero (Zs K x (2))) (Zs K x (4))) (K.add (K.add (K.add (Zs K x (w + 3)) (Zs K x (w + 5))) (Zs K x (w + 7))) (Zs K x (w + 9)))) (K.neg (K.mul (K.add (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (w + 8))) (K.add (Zs K x (3)) (Zs K x (5))))))
            have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 5))))) := A1.Dn K O x hD (0) (w + 4) (0) (1) (2) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 7))))) := A1.Dn K O x hD (0) (w + 6) (0) (1) (2) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 3)) (Zs K x (w + 5))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 4))))) := A1.Dn K O x hD (1) (w + 2) (1) (2) (3) (w + 3) (w + 4) (w + 5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 6))))) := A1.Dn K O x hD (1) (w + 4) (1) (2) (3) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 6))))) := A1.Dn K O x hD (1) (w + 4) (1) (2) (3) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 7)) (Zs K x (w + 9))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 8))))) := A1.Dn K O x hD (1) (w + 6) (1) (2) (3) (w + 7) (w + 8) (w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d6 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 5))))) := A1.Dn K O x hD (2) (w + 2) (2) (3) (4) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d7 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 7))))) := A1.Dn K O x hD (2) (w + 4) (2) (3) (4) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d8 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 3)) (Zs K x (w + 5))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (w + 4))))) := A1.Dn K O x hD (3) (w) (3) (4) (5) (w + 3) (w + 4) (w + 5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d9 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (w + 6))))) := A1.Dn K O x hD (3) (w + 2) (3) (4) (5) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d10 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 7)) (Zs K x (w + 9))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (w + 8))))) := A1.Dn K O x hD (3) (w + 4) (3) (4) (5) (w + 7) (w + 8) (w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) d9) d10) (by lib_ring)
    | succ a =>
      cases a with
      | zero =>
        cases w with
        | zero =>
          rw [show 0 + 1 + 1 + 0 + 1 = 3 by lib_nat]
          show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (K.add (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (Zs K x (7)))) (K.neg (K.mul (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
          have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (2))))) := A1.Dn K O x hD (0) (1) (0) (1) (2) (1) (2) (3) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (4))))) := A1.Dn K O x hD (0) (3) (0) (1) (2) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (6))))) := A1.Dn K O x hD (0) (5) (0) (1) (2) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (4))))) := A1.Dn K O x hD (2) (1) (2) (3) (4) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))) := A1.Dn K O x hD (2) (3) (2) (3) (4) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (6))))) := A1.Dn K O x hD (4) (1) (4) (5) (6) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) (by lib_ring)
        | succ w =>
          cases w with
          | zero =>
            rw [show 0 + 1 + 1 + (0 + 1) + 1 = 4 by lib_nat]
            show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (K.add (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))) (Zs K x (8)))) (K.neg (K.mul (K.add (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (7))) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
            have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (3))))) := A1.Dn K O x hD (0) (2) (0) (1) (2) (2) (3) (4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (5))))) := A1.Dn K O x hD (0) (4) (0) (1) (2) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (7))))) := A1.Dn K O x hD (0) (6) (0) (1) (2) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (4))))) := A1.Dn K O x hD (1) (2) (1) (2) (3) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (6))))) := A1.Dn K O x hD (1) (4) (1) (2) (3) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (5))))) := A1.Dn K O x hD (2) (2) (2) (3) (4) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d6 : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (7))))) := A1.Dn K O x hD (2) (4) (2) (3) (4) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d7 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (6))))) := A1.Dn K O x hD (3) (2) (3) (4) (5) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d8 : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (7))))) := A1.Dn K O x hD (4) (2) (4) (5) (6) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) (by lib_ring)
          | succ w =>
            cases w with
            | zero =>
              rw [show 0 + 1 + 1 + (0 + 1 + 1) + 1 = 5 by lib_nat]
              show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (K.add (K.add (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (7))) (Zs K x (9)))) (K.neg (K.mul (K.add (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (8))) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
              have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (4))))) := A1.Dn K O x hD (0) (3) (0) (1) (2) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (6))))) := A1.Dn K O x hD (0) (5) (0) (1) (2) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (7)) (Zs K x (9))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (8))))) := A1.Dn K O x hD (0) (7) (0) (1) (2) (7) (8) (9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))))) := A1.Dn K O x hD (1) (3) (1) (2) (3) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (7))))) := A1.Dn K O x hD (1) (5) (1) (2) (3) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (4))))) := A1.Dn K O x hD (2) (1) (2) (3) (4) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d6 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))) := A1.Dn K O x hD (2) (3) (2) (3) (4) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d7 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))) := A1.Dn K O x hD (2) (3) (2) (3) (4) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d8 : O.le K.zero (K.add (K.mul (K.add (Zs K x (7)) (Zs K x (9))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (8))))) := A1.Dn K O x hD (2) (5) (2) (3) (4) (7) (8) (9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d9 : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (7))))) := A1.Dn K O x hD (3) (3) (3) (4) (5) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d10 : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (6))))) := A1.Dn K O x hD (4) (1) (4) (5) (6) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d11 : O.le K.zero (K.add (K.mul (K.add (Zs K x (7)) (Zs K x (9))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (8))))) := A1.Dn K O x hD (4) (3) (4) (5) (6) (7) (8) (9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) d9) d10) d11) (by lib_ring)
            | succ w =>
              rw [show 0 + 1 + 1 + (w + 1 + 1 + 1) + 1 = w + 6 by lib_nat]
              show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (K.add (K.add (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (w + 8))) (Zs K x (w + 10)))) (K.neg (K.mul (K.add (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (w + 9))) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
              have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 5))))) := A1.Dn K O x hD (0) (w + 4) (0) (1) (2) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 7))))) := A1.Dn K O x hD (0) (w + 6) (0) (1) (2) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 8)) (Zs K x (w + 10))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 9))))) := A1.Dn K O x hD (0) (w + 8) (0) (1) (2) (w + 8) (w + 9) (w + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 6))))) := A1.Dn K O x hD (1) (w + 4) (1) (2) (3) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 7)) (Zs K x (w + 9))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 8))))) := A1.Dn K O x hD (1) (w + 6) (1) (2) (3) (w + 7) (w + 8) (w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 5))))) := A1.Dn K O x hD (2) (w + 2) (2) (3) (4) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d6 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 7))))) := A1.Dn K O x hD (2) (w + 4) (2) (3) (4) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d7 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 7))))) := A1.Dn K O x hD (2) (w + 4) (2) (3) (4) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d8 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 8)) (Zs K x (w + 10))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 9))))) := A1.Dn K O x hD (2) (w + 6) (2) (3) (4) (w + 8) (w + 9) (w + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d9 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (w + 6))))) := A1.Dn K O x hD (3) (w + 2) (3) (4) (5) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d10 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 7)) (Zs K x (w + 9))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (w + 8))))) := A1.Dn K O x hD (3) (w + 4) (3) (4) (5) (w + 7) (w + 8) (w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d11 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (w + 5))))) := A1.Dn K O x hD (4) (w) (4) (5) (6) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d12 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (w + 7))))) := A1.Dn K O x hD (4) (w + 2) (4) (5) (6) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d13 : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 8)) (Zs K x (w + 10))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (w + 9))))) := A1.Dn K O x hD (4) (w + 4) (4) (5) (6) (w + 8) (w + 9) (w + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) d9) d10) d11) d12) d13) (by lib_ring)
      | succ a =>
        cases w with
        | zero =>
          rw [show a + 1 + 1 + 1 + 0 + 1 = a + 4 by lib_nat]
          show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))) (K.add (K.add (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))) (Zs K x (a + 8)))) (K.neg (K.mul (K.add (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 7))) (K.add (K.add (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))) (Zs K x (a + 7))))))
          have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 3))))) := A1.Dn K O x hD (a + 1) (1) (a + 1) (a + 2) (a + 3) (a + 2) (a + 3) (a + 4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))))) := A1.Dn K O x hD (a + 1) (3) (a + 1) (a + 2) (a + 3) (a + 4) (a + 5) (a + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 7))))) := A1.Dn K O x hD (a + 1) (5) (a + 1) (a + 2) (a + 3) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 5))))) := A1.Dn K O x hD (a + 3) (1) (a + 3) (a + 4) (a + 5) (a + 4) (a + 5) (a + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 7))))) := A1.Dn K O x hD (a + 3) (3) (a + 3) (a + 4) (a + 5) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 7))))) := A1.Dn K O x hD (a + 5) (1) (a + 5) (a + 6) (a + 7) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
          exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) (by lib_ring)
        | succ w =>
          cases w with
          | zero =>
            rw [show a + 1 + 1 + 1 + (0 + 1) + 1 = a + 5 by lib_nat]
            show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))) (K.add (K.add (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 7))) (Zs K x (a + 9)))) (K.neg (K.mul (K.add (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 8))) (K.add (K.add (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))) (Zs K x (a + 7))))))
            have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 4))))) := A1.Dn K O x hD (a + 1) (2) (a + 1) (a + 2) (a + 3) (a + 3) (a + 4) (a + 5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 6))))) := A1.Dn K O x hD (a + 1) (4) (a + 1) (a + 2) (a + 3) (a + 5) (a + 6) (a + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 7)) (Zs K x (a + 9))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 8))))) := A1.Dn K O x hD (a + 1) (6) (a + 1) (a + 2) (a + 3) (a + 7) (a + 8) (a + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 5))))) := A1.Dn K O x hD (a + 2) (2) (a + 2) (a + 3) (a + 4) (a + 4) (a + 5) (a + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 7))))) := A1.Dn K O x hD (a + 2) (4) (a + 2) (a + 3) (a + 4) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 6))))) := A1.Dn K O x hD (a + 3) (2) (a + 3) (a + 4) (a + 5) (a + 5) (a + 6) (a + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d6 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 7)) (Zs K x (a + 9))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 8))))) := A1.Dn K O x hD (a + 3) (4) (a + 3) (a + 4) (a + 5) (a + 7) (a + 8) (a + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d7 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 5))) (K.neg (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 7))))) := A1.Dn K O x hD (a + 4) (2) (a + 4) (a + 5) (a + 6) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            have d8 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 7)) (Zs K x (a + 9))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 8))))) := A1.Dn K O x hD (a + 5) (2) (a + 5) (a + 6) (a + 7) (a + 7) (a + 8) (a + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
            exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) (by lib_ring)
          | succ w =>
            cases w with
            | zero =>
              rw [show a + 1 + 1 + 1 + (0 + 1 + 1) + 1 = a + 6 by lib_nat]
              show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))) (K.add (K.add (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 8))) (Zs K x (a + 10)))) (K.neg (K.mul (K.add (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 9))) (K.add (K.add (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))) (Zs K x (a + 7))))))
              have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))))) := A1.Dn K O x hD (a + 1) (3) (a + 1) (a + 2) (a + 3) (a + 4) (a + 5) (a + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 7))))) := A1.Dn K O x hD (a + 1) (5) (a + 1) (a + 2) (a + 3) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 8)) (Zs K x (a + 10))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 9))))) := A1.Dn K O x hD (a + 1) (7) (a + 1) (a + 2) (a + 3) (a + 8) (a + 9) (a + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))))) := A1.Dn K O x hD (a + 2) (3) (a + 2) (a + 3) (a + 4) (a + 5) (a + 6) (a + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 7)) (Zs K x (a + 9))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 8))))) := A1.Dn K O x hD (a + 2) (5) (a + 2) (a + 3) (a + 4) (a + 7) (a + 8) (a + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 5))))) := A1.Dn K O x hD (a + 3) (1) (a + 3) (a + 4) (a + 5) (a + 4) (a + 5) (a + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d6 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 7))))) := A1.Dn K O x hD (a + 3) (3) (a + 3) (a + 4) (a + 5) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d7 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 7))))) := A1.Dn K O x hD (a + 3) (3) (a + 3) (a + 4) (a + 5) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d8 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 8)) (Zs K x (a + 10))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 9))))) := A1.Dn K O x hD (a + 3) (5) (a + 3) (a + 4) (a + 5) (a + 8) (a + 9) (a + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d9 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 7)) (Zs K x (a + 9))) (Zs K x (a + 5))) (K.neg (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 8))))) := A1.Dn K O x hD (a + 4) (3) (a + 4) (a + 5) (a + 6) (a + 7) (a + 8) (a + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d10 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 7))))) := A1.Dn K O x hD (a + 5) (1) (a + 5) (a + 6) (a + 7) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d11 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 8)) (Zs K x (a + 10))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 9))))) := A1.Dn K O x hD (a + 5) (3) (a + 5) (a + 6) (a + 7) (a + 8) (a + 9) (a + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) d9) d10) d11) (by lib_ring)
            | succ w =>
              rw [show a + 1 + 1 + 1 + (w + 1 + 1 + 1) + 1 = a + w + 7 by lib_nat]
              show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))) (K.add (K.add (K.add (Zs K x (a + w + 5)) (Zs K x (a + w + 7))) (Zs K x (a + w + 9))) (Zs K x (a + w + 11)))) (K.neg (K.mul (K.add (K.add (Zs K x (a + w + 6)) (Zs K x (a + w + 8))) (Zs K x (a + w + 10))) (K.add (K.add (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))) (Zs K x (a  -- (+10 chars; full line in the Zenodo source)
              have d0 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 5)) (Zs K x (a + w + 7))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + w + 6))))) := A1.Dn K O x hD (a + 1) (w + 4) (a + 1) (a + 2) (a + 3) (a + w + 5) (a + w + 6) (a + w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d1 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 7)) (Zs K x (a + w + 9))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + w + 8))))) := A1.Dn K O x hD (a + 1) (w + 6) (a + 1) (a + 2) (a + 3) (a + w + 7) (a + w + 8) (a + w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d2 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 9)) (Zs K x (a + w + 11))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + w + 10))))) := A1.Dn K O x hD (a + 1) (w + 8) (a + 1) (a + 2) (a + 3) (a + w + 9) (a + w + 10) (a + w + 11) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d3 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 6)) (Zs K x (a + w + 8))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + w + 7))))) := A1.Dn K O x hD (a + 2) (w + 4) (a + 2) (a + 3) (a + 4) (a + w + 6) (a + w + 7) (a + w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d4 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 8)) (Zs K x (a + w + 10))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + w + 9))))) := A1.Dn K O x hD (a + 2) (w + 6) (a + 2) (a + 3) (a + 4) (a + w + 8) (a + w + 9) (a + w + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d5 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 5)) (Zs K x (a + w + 7))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + w + 6))))) := A1.Dn K O x hD (a + 3) (w + 2) (a + 3) (a + 4) (a + 5) (a + w + 5) (a + w + 6) (a + w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d6 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 7)) (Zs K x (a + w + 9))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + w + 8))))) := A1.Dn K O x hD (a + 3) (w + 4) (a + 3) (a + 4) (a + 5) (a + w + 7) (a + w + 8) (a + w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d7 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 7)) (Zs K x (a + w + 9))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + w + 8))))) := A1.Dn K O x hD (a + 3) (w + 4) (a + 3) (a + 4) (a + 5) (a + w + 7) (a + w + 8) (a + w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d8 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 9)) (Zs K x (a + w + 11))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + w + 10))))) := A1.Dn K O x hD (a + 3) (w + 6) (a + 3) (a + 4) (a + 5) (a + w + 9) (a + w + 10) (a + w + 11) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d9 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 6)) (Zs K x (a + w + 8))) (Zs K x (a + 5))) (K.neg (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + w + 7))))) := A1.Dn K O x hD (a + 4) (w + 2) (a + 4) (a + 5) (a + 6) (a + w + 6) (a + w + 7) (a + w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d10 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 8)) (Zs K x (a + w + 10))) (Zs K x (a + 5))) (K.neg (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + w + 9))))) := A1.Dn K O x hD (a + 4) (w + 4) (a + 4) (a + 5) (a + 6) (a + w + 8) (a + w + 9) (a + w + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d11 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 5)) (Zs K x (a + w + 7))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + w + 6))))) := A1.Dn K O x hD (a + 5) (w) (a + 5) (a + 6) (a + 7) (a + w + 5) (a + w + 6) (a + w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d12 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 7)) (Zs K x (a + w + 9))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + w + 8))))) := A1.Dn K O x hD (a + 5) (w + 2) (a + 5) (a + 6) (a + 7) (a + w + 7) (a + w + 8) (a + w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              have d13 : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 9)) (Zs K x (a + w + 11))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + w + 10))))) := A1.Dn K O x hD (a + 5) (w + 4) (a + 5) (a + 6) (a + 7) (a + w + 9) (a + w + 10) (a + w + 11) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat)
              exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) d9) d10) d11) d12) d13) (by lib_ring)

include hD in
/-- (u6) minor(S⁻²ψ₂, S⁻²ψ₃)(i, i+w+1) ≥ 0 -/
theorem u6 (i w : Nat) : O.le K.zero (Lagrange.minor K (Zs K (Zs K (p2v K x))) (Zs K (Zs K (A1C.p3v K x))) i (i + w + 1)) := by
  unfold Lagrange.minor
  cases i with
  | zero =>
    show O.le K.zero (K.add (K.mul K.zero (Zs K (Zs K (A1C.p3v K x)) (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K (p2v K x)) (0 + w + 1)) K.zero)))
    exact A1.nn_of_eq K O _ _ (O.le_refl K.zero) (by lib_ring)
  | succ i =>
    cases i with
    | zero =>
      show O.le K.zero (K.add (K.mul K.zero (Zs K (Zs K (A1C.p3v K x)) (0 + 1 + w + 1))) (K.neg (K.mul (Zs K (Zs K (p2v K x)) (0 + 1 + w + 1)) K.zero)))
      exact A1.nn_of_eq K O _ _ (O.le_refl K.zero) (by lib_ring)
    | succ a =>
      rw [show a + 1 + 1 + w + 1 = a + w + 1 + 2 by lib_nat]
      exact u6core K O x hD a w

end signs2

section final2

variable (O : ORing K)

theorem minor_swap (f g : Nat → R) (i j : Nat) : Lagrange.minor K f g i j = K.neg (Lagrange.minor K g f i j) := by
  unfold Lagrange.minor; lib_ring

theorem nonpos_of_nn_neg (a : R) (h : O.le K.zero a) : O.le (K.neg a) K.zero := A1.neg_nn K O a h

theorem nn_of_nonpos_neg (a : R) (h : O.le a K.zero) : O.le K.zero (K.neg a) := by
  have := O.add_le_add _ _ _ _ h (O.le_refl (K.neg a))
  rw [show K.add a (K.neg a) = K.zero by lib_ring, show K.add K.zero (K.neg a) = K.neg a by lib_ring] at this; exact this

/-- **(A) at n = 2 for every length**: a v-good X gives 16 c₂ c₄ ≤ 15 c₃² for c = X·X -/
theorem prod_lc2 (F : OField.OF K O) (h1 : O.le K.zero K.one) (X : List R) (hXg : L1Full.Good K O X) (M : Nat) (hX : X.length = M + 2) :
    O.le (K.mul (LibRing.castN K 16) (K.mul (cgc K X X 2) (cgc K X X 4)))
      (K.mul (LibRing.castN K 15) (K.mul (cgc K X X 3) (cgc K X X 3))) := by
  have ⟨hb, hpos, hv, h0⟩ := hXg
  let x := co K X
  have hzY : ∀ t, M + 3 ≤ t → Zs K x t = K.zero := fun t ht => by
    cases t with
    | zero => exact absurd ht (Nat.not_succ_le_zero _)
    | succ t => exact HeatLR.co_out K X t (by rw [hX]; exact Nat.le_of_succ_le_succ ht)
  have hY : ∀ j, O.le K.zero (Zs K x j) := fun j => by cases j with
    | zero => exact O.le_refl _
    | succ j => exact hb j
  have hD : ∀ a k, O.le K.zero (Window.Dl K (Zs K x) a (a + k)) := fun a k =>
    Window.dl_nn_t K O F (Zs K x) hY (M + 3)
      (fun j hj => hpos j (by rw [hX]; exact Nat.lt_of_succ_lt_succ hj))
      hzY (CgWindow.VV_Zs K O x hv h0) a k
  have hT : ∀ a w, O.le K.zero (K.add (K.mul (Zs K x (a + 1)) (Zs K x (a + w))) (K.neg (K.mul (Zs K x a) (Zs K x (a + w + 1))))) := by
    intro a w
    rw [← Lagrange.tail_minor K (Zs K x) (M + 3) hzY a w (M + 3) (by lib_nat_le)]
    exact CauchyBinet2.sum_nn K O _ (fun i => by rw [show a + w + i = a + i + w by lib_nat]; exact hD (a + i) w) _
  have hxz : ∀ t, M + 1 + 1 ≤ t → x t = K.zero := A1C.hzX K X (M + 1) hX
  let N := M + 1 + 1 + 6
  have cb1 := CauchyBinet2.cb_nonneg K O (Zs K (Zs K (Zs K x))) (Zs K (Zs K x)) x (fun i => x (i + 1)) N
    (A1.split_lt _ (fun i w => A1.prod_np K O _ _ (A1.s2 K O x hT i w) (u1 K O x hT i w)))
  have cb2 := CauchyBinet2.cb_nonneg K O (Zs K (Zs K (Zs K x))) (Zs K (c1v K x)) x (fun i => x (i + 1)) N
    (A1.split_lt _ (fun i w => A1.prod_np K O _ _ (A1.s3 K O x hY hT i w) (u1 K O x hT i w)))
  have cb3 := CauchyBinet2.cb_nonneg K O (Zs K (Zs K (Zs K x))) (p2v K x) x (fun i => x (i + 1)) N
    (A1.split_lt _ (fun i w => A1.prod_np K O _ _ (u2 K O x hb hT i w) (u1 K O x hT i w)))
  have cb4 := CauchyBinet2.cb_nonneg K O (Zs K (Zs K x)) (Zs K x) (Zs K (Zs K (A1C.p3v K x))) (Zs K (Zs K (p2v K x))) N
    (A1.split_lt _ (fun i w => A1.prod_np K O _ _ (u3 K O x hb hT i w)
      (by rw [minor_swap K]; exact nonpos_of_nn_neg K O _ (u6 K O x hD i w))))
  have cb5 := CauchyBinet2.cb_nonneg K O (Zs K (Zs K x)) (Zs K (Zs K (c1v K x))) (fun i => x (i + 1)) x N
    (A1.split_lt _ (fun i w => O.mul_nonneg _ _ (A1.s4 K O x hD i w)
      (by rw [minor_swap K]; exact nn_of_nonpos_neg K O _ (u1 K O x hT i w))))
  have cb6 := CauchyBinet2.cb_nonneg K O x (Zs K x) (Zs K (Zs K (c1v K x))) (Zs K (Zs K (p2v K x))) N
    (A1.split_lt _ (fun i w => O.mul_nonneg _ _
      (by rw [minor_swap K]; exact nn_of_nonpos_neg K O _ (A1.s1 K O x hT i w)) (u4 K O x hD i w)))
  have cb7 := CauchyBinet2.cb_nonneg K O (fun i => x (i + 1)) (fun i => x (i + 2)) (fun i => x (i + 1)) (fun i => x (i + 2)) N
    (A1.split_lt _ (fun i w => u5 K O _ _ i (i + w + 1)))
  have id := ident2 K x (M + 1 + 1) hxz
  have c8 := SU2CharExp.cN_nn K O h1 8
  have c4 := SU2CharExp.cN_nn K O h1 4
  have c12 := SU2CharExp.cN_nn K O h1 12
  have tot := A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.pos_mul K O _ _ c8 cb1) (A1.pos_mul K O _ _ c8 cb2))
      (A1.nn_add K O _ _ (A1.pos_mul K O _ _ c4 cb3) (A1.pos_mul K O _ _ c12 cb4)))
    (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.pos_mul K O _ _ c8 cb5) (A1.pos_mul K O _ _ c8 cb6)) (A1.pos_mul K O _ _ c4 cb7))
  have tot' : O.le K.zero _ := id ▸ tot
  apply A1.le_of_sub_nn K O
  rw [A1C.c2eq K X (M + 1) hX, A1C.c3eq K X (M + 1) hX, A1C.cgc_ip K X (M + 1) 4 hX (p4v K (co K X)) (psi4 K X) 6]
  exact tot'

end final2

end A2
#print axioms A2.ident2
#print axioms A2.m1x
#print axioms A2.mk
#print axioms A2.u1
#print axioms A2.u2
#print axioms A2.u3
#print axioms A2.u4
#print axioms A2.u5
#print axioms A2.u6core
#print axioms A2.u6
#print axioms A2.prod_lc2
#print axioms A2.B1
#print axioms A2.B2
#print axioms A2.B3
#print axioms A2.B4
#print axioms A2.Bs11
#print axioms A2.Bs12
#print axioms A2.Bs01
#print axioms A2.Bs21
#print axioms A2.Bs22
#print axioms A2.Bs04
#print axioms A2.Bs24
#print axioms A2.Bs44
#print axioms A2.Bs02
#print axioms A2.Bs03
#print axioms A2.sq_nn
#print axioms A2.le_eq
#print axioms A2.hY
#print axioms A2.p2_nn
#print axioms A2.minor_swap
#print axioms A2.nonpos_of_nn_neg
#print axioms A2.nn_of_nonpos_neg

#print axioms: 32 theorems, no axioms.

90.89 Autocorrelations of \(v\)-monotone lists are log-concave

For a \(v\)-monotone list the autocorrelation \(R_k=\sum _i x_ix_{i+k}\) is log-concave.

\[ R_{k+1}^2-R_kR_{k+2}=\textstyle \sum _{i<j}M_{ij}(S^{-k-1}x,S^{-k}x)\,M_{ij}(x,Sx)\ \ge 0 \]

ym/RLc.lean

/-
  ym/RLc — T45, G54 by-product: for a v-good list the autocorrelation R_k = Σ_i x_i x_{i+k} is log-concave:
    R_k R_{k+2} ≤ R_{k+1}²   (every k),
  as one Cauchy–Binet value CB(S^{-(k+1)}x, S^{-k}x; x, Sx) whose integrand is a product of two m₁-minors (both ≤ 0).
  Lean 4 core only, no axioms.
-/
import ym.A2

namespace RLc

open YMChain QBinom CgWindow A1Ip A2Ip MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem minor_zz (f g : Nat → R) (i j : Nat) : Lagrange.minor K (Zs K f) (Zs K g) (i + 1) (j + 1) = Lagrange.minor K f g i j := rfl

/-- minor(S^{-(k+1)}x, S^{-k}x)(i, i+w+1) ≤ 0 -/
theorem mzz (x : Nat → R)
    (hT : ∀ a w, O.le K.zero (K.add (K.mul (Zs K x (a + 1)) (Zs K x (a + w))) (K.neg (K.mul (Zs K x a) (Zs K x (a + w + 1)))))) :
    ∀ k i w, O.le (Lagrange.minor K (Zsn K (k + 1) x) (Zsn K k x) i (i + w + 1)) K.zero
  | 0, i, w => A1.s1 K O x hT i w
  | k + 1, i, w => by
    cases i with
    | zero =>
      show O.le (K.add (K.mul K.zero (Zsn K (k + 1) x (0 + w + 1))) (K.neg (K.mul (Zsn K (k + 1 + 1) x (0 + w + 1)) K.zero))) K.zero
      rw [show K.add (K.mul K.zero (Zsn K (k + 1) x (0 + w + 1))) (K.neg (K.mul (Zsn K (k + 1 + 1) x (0 + w + 1)) K.zero)) = K.zero by lib_ring]
      exact O.le_refl _
    | succ i =>
      rw [show i + 1 + w + 1 = i + w + 1 + 1 by lib_nat]
      exact mzz x hT k i w

/-- **autocorrelations of a v-good list are log-concave** -/
theorem r_lc (F : OField.OF K O) (X : List R) (hXg : L1Full.Good K O X) (M : Nat) (hX : X.length = M + 2) (k : Nat) :
    O.le (K.mul (Rk K (co K X) (M + 1 + 1) k) (Rk K (co K X) (M + 1 + 1) (k + 2)))
      (K.mul (Rk K (co K X) (M + 1 + 1) (k + 1)) (Rk K (co K X) (M + 1 + 1) (k + 1))) := by
  have ⟨hb, hpos, hv, h0⟩ := hXg
  let x := co K X
  have hzY : ∀ t, M + 3 ≤ t → Zs K x t = K.zero := fun t ht => by
    cases t with
    | zero => exact absurd ht (Nat.not_succ_le_zero _)
    | succ t => exact HeatLR.co_out K X t (by rw [hX]; exact Nat.le_of_succ_le_succ ht)
  have hY : ∀ j, O.le K.zero (Zs K x j) := fun j => by cases j with
    | zero => exact O.le_refl _
    | succ j => exact hb j
  have hD : ∀ a k, O.le K.zero (Window.Dl K (Zs K x) a (a + k)) := fun a k =>
    Window.dl_nn_t K O F (Zs K x) hY (M + 3)
      (fun j hj => hpos j (by rw [hX]; exact Nat.lt_of_succ_lt_succ hj))
      hzY (CgWindow.VV_Zs K O x hv h0) a k
  have hT : ∀ a w, O.le K.zero (K.add (K.mul (Zs K x (a + 1)) (Zs K x (a + w))) (K.neg (K.mul (Zs K x a) (Zs K x (a + w + 1))))) := by
    intro a w
    rw [← Lagrange.tail_minor K (Zs K x) (M + 3) hzY a w (M + 3) (by lib_nat_le)]
    exact CauchyBinet2.sum_nn K O _ (fun i => by rw [show a + w + i = a + i + w by lib_nat]; exact hD (a + i) w) _
  have hxz : ∀ t, M + 1 + 1 ≤ t → x t = K.zero := A1C.hzX K X (M + 1) hX
  have cb := CauchyBinet2.cb_nonneg K O (Zsn K (k + 1) x) (Zsn K k x) x (fun i => x (i + 1)) (M + 1 + 1 + (k + 1) + 0)
    (A1.split_lt _ (fun i w => A1.prod_np K O _ _ (mzz K O x hT k i w) (A2.u1 K O x hT i w)))
  have e1 : CauchyBinet2.ip K (Zsn K (k + 1) x) x (M + 1 + 1 + (k + 1) + 0) = Rk K x (M + 1 + 1) (k + 1) := by
    have := ipZS K x (M + 1 + 1) hxz (k + 1) 0 0
    rw [Nat.add_zero] at this; exact this
  have e2 : CauchyBinet2.ip K (Zsn K k x) (fun i => x (i + 1)) (M + 1 + 1 + (k + 1) + 0) = Rk K x (M + 1 + 1) (k + 1) := by
    have := ipZS K x (M + 1 + 1) hxz k 1 1
    rw [show M + 1 + 1 + k + 1 = M + 1 + 1 + (k + 1) + 0 by lib_nat] at this; exact this
  have e3 : CauchyBinet2.ip K (Zsn K (k + 1) x) (fun i => x (i + 1)) (M + 1 + 1 + (k + 1) + 0) = Rk K x (M + 1 + 1) (k + 2) := by
    have := ipZS K x (M + 1 + 1) hxz (k + 1) 1 0
    rw [show k + 1 + 1 = k + 2 by lib_nat] at this; exact this
  have e4 : CauchyBinet2.ip K (Zsn K k x) x (M + 1 + 1 + (k + 1) + 0) = Rk K x (M + 1 + 1) k := by
    have := ipZS K x (M + 1 + 1) hxz k 0 1
    rw [show M + 1 + 1 + k + 1 = M + 1 + 1 + (k + 1) + 0 by lib_nat, Nat.add_zero] at this; exact this
  rw [e1, e2, e3, e4] at cb
  show O.le (K.mul (Rk K x (M + 1 + 1) k) (Rk K x (M + 1 + 1) (k + 2))) (K.mul (Rk K x (M + 1 + 1) (k + 1)) (Rk K x (M + 1 + 1) (k + 1)))
  apply A1.le_of_sub_nn K O
  exact A1.nn_of_eq K O _ _ cb (by lib_ring)

end RLc

#print axioms RLc.minor_zz
#print axioms RLc.mzz
#print axioms RLc.r_lc

#print axioms: 3 theorems, no axioms.

90.90 Log-concavity of short products

For \(v\)-monotone lists of length at most \(9\) the product \(X\cdot X\) has \(a_n=c_n/(n+1)\) log-concave at every \(n\).

\[ (n+1)(n+3)c_{n+1}^2-(n+2)^2c_nc_{n+2}=\textstyle \sum \lambda \,D(u,v)\,D(u',v'),\quad \lambda \in \mathbb N \]

ym/LCL5.lean

/-
  ym/LCL5 — T45, G44 (A) for v-good lists of length 5, every index: for c = X·X (X v-good, |X| = 5) and every n ≥ 1,
    (n+2)² c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}²,   i.e. a_{n+1}² ≥ a_n a_{n+2} for a_n = c_n/(n+1)
  (n = 0 is LC0.prod_lc0 for all lengths; for n ≥ 8 both sides vanish). Each case is an integer certificate
    (n+1)(n+3) c_{n+1}² − (n+2)² c_n c_{n+2} = Σ (nonnegative integer) · D(u,v) D(u′,v′)
  with the window minors D(u,v) = (Y_{v−1}+Y_{v+1})Y_u − (Y_{u−1}+Y_{u+1})Y_v ≥ 0 (Window.dl_nn_t), found by NNLS
  (work/cert/A/dd_n*_L5.txt, generator work/cert/A/lean/gen_lcL5.py). Lean 4 core only, no axioms.
-/
import lib.RingTac
import ym.LC0

namespace LCL5

open YMChain SU2Char HeatPair MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem add_nn (a b : R) (ha : O.le K.zero a) (hb : O.le K.zero b) : O.le K.zero (K.add a b) := by
  have h := O.add_le_add _ _ _ _ ha hb
  rw [show K.add K.zero K.zero = K.zero by lib_ring] at h
  exact h

/-- window minor D(u,v) = (Y_{v−1}+Y_{v+1})Y_u − (Y_{u−1}+Y_{u+1})Y_v -/
def Dd (Y : Nat → R) (u v : Nat) : R :=
  LibRing.sub K (K.mul (K.add (CgWindow.Zs K Y v) (Y (v + 1))) (Y u)) (K.mul (K.add (CgWindow.Zs K Y u) (Y (u + 1))) (Y v))

theorem cgc0 (y0 y1 y2 y3 y4 : R) : cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 0 = (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 0) (co K [y0, y1, y2, y3, y4] 0)) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 1))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 3) (co K [y0, y1, y2, y3, y4] 3))) (K.mul (c  -- (+65 chars; full line in the Zenodo source)
  show (K.add (K.mul y0 (K.add (K.mul y0 (LibRing.castN K 1)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.add (K.mul y4 (LibRing.castN K 0)) K.zero)))))) (K.add (K.mul y1 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 1)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.a  -- (+822 chars; full line in the Zenodo source)
  lib_ring

theorem cgc1 (y0 y1 y2 y3 y4 : R) : cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 1 = (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 0) (co K [y0, y1, y2, y3, y4] 1)) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 0))) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1, y2, y  -- (+293 chars; full line in the Zenodo source)
  show (K.add (K.mul y0 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 1)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.add (K.mul y4 (LibRing.castN K 0)) K.zero)))))) (K.add (K.mul y1 (K.add (K.mul y0 (LibRing.castN K 1)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 1)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.a  -- (+888 chars; full line in the Zenodo source)
  lib_ring

theorem cgc2 (y0 y1 y2 y3 y4 : R) : cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 2 = (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 0) (co K [y0, y1, y2, y3, y4] 2)) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 1))) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 3))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K   -- (+445 chars; full line in the Zenodo source)
  show (K.add (K.mul y0 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 1)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.add (K.mul y4 (LibRing.castN K 0)) K.zero)))))) (K.add (K.mul y1 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 1)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 1)) (K.a  -- (+932 chars; full line in the Zenodo source)
  lib_ring

theorem cgc3 (y0 y1 y2 y3 y4 : R) : cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 3 = (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 0) (co K [y0, y1, y2, y3, y4] 3)) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 4))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K   -- (+445 chars; full line in the Zenodo source)
  show (K.add (K.mul y0 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 1)) (K.add (K.mul y4 (LibRing.castN K 0)) K.zero)))))) (K.add (K.mul y1 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 1)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.a  -- (+932 chars; full line in the Zenodo source)
  lib_ring

theorem cgc4 (y0 y1 y2 y3 y4 : R) : cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 4 = (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 0) (co K [y0, y1, y2, y3, y4] 4)) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 3))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1  -- (+369 chars; full line in the Zenodo source)
  show (K.add (K.mul y0 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.add (K.mul y4 (LibRing.castN K 1)) K.zero)))))) (K.add (K.mul y1 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 1)) (K.a  -- (+910 chars; full line in the Zenodo source)
  lib_ring

theorem cgc5 (y0 y1 y2 y3 y4 : R) : cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 5 = (K.add (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 4)) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1, y2, y3, y4] 3))) (K.mul (co K [y0, y1, y2, y3, y4] 3) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 3) (co K [y0, y1, y2, y3, y4] 4))) (K  -- (+141 chars; full line in the Zenodo source)
  show (K.add (K.mul y0 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.add (K.mul y4 (LibRing.castN K 0)) K.zero)))))) (K.add (K.mul y1 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.a  -- (+844 chars; full line in the Zenodo source)
  lib_ring

theorem cgc6 (y0 y1 y2 y3 y4 : R) : cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 6 = (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1, y2, y3, y4] 4)) (K.mul (co K [y0, y1, y2, y3, y4] 3) (co K [y0, y1, y2, y3, y4] 3))) (K.mul (co K [y0, y1, y2, y3, y4] 4) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 4) (co K [y0, y1, y2, y3, y4] 4))) := by
  show (K.add (K.mul y0 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.add (K.mul y4 (LibRing.castN K 0)) K.zero)))))) (K.add (K.mul y1 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.a  -- (+800 chars; full line in the Zenodo source)
  lib_ring

theorem cgc7 (y0 y1 y2 y3 y4 : R) : cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 7 = (K.add (K.mul (co K [y0, y1, y2, y3, y4] 3) (co K [y0, y1, y2, y3, y4] 4)) (K.mul (co K [y0, y1, y2, y3, y4] 4) (co K [y0, y1, y2, y3, y4] 3))) := by
  show (K.add (K.mul y0 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.add (K.mul y4 (LibRing.castN K 0)) K.zero)))))) (K.add (K.mul y1 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.a  -- (+756 chars; full line in the Zenodo source)
  lib_ring

theorem cgc8 (y0 y1 y2 y3 y4 : R) : cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 8 = (K.mul (co K [y0, y1, y2, y3, y4] 4) (co K [y0, y1, y2, y3, y4] 4)) := by
  show (K.add (K.mul y0 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.add (K.mul y4 (LibRing.castN K 0)) K.zero)))))) (K.add (K.mul y1 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.a  -- (+734 chars; full line in the Zenodo source)
  lib_ring

theorem cgc9 (y0 y1 y2 y3 y4 : R) : cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 9 = K.zero := by
  show (K.add (K.mul y0 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.add (K.mul y4 (LibRing.castN K 0)) K.zero)))))) (K.add (K.mul y1 (K.add (K.mul y0 (LibRing.castN K 0)) (K.add (K.mul y1 (LibRing.castN K 0)) (K.add (K.mul y2 (LibRing.castN K 0)) (K.add (K.mul y3 (LibRing.castN K 0)) (K.a  -- (+727 chars; full line in the Zenodo source)
  lib_ring

def T1 (Y : Nat → R) : R := (LibRing.sub K (K.mul (LibRing.castN K 8) (K.mul (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (Y 0) (Y 2)) (K.mul (Y 1) (Y 1))) (K.mul (Y 1) (Y 3))) (K.mul (Y 2) (Y 0))) (K.mul (Y 2) (Y 2))) (K.mul (Y 2) (Y 4))) (K.mul (Y 3) (Y 1))) (K.mul (Y 3) (Y 3))) (K.mul (Y 4) (Y 2))) (K.mul (Y 4) (Y 4))) (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K  -- (+747 chars; full line in the Zenodo source)

set_option maxRecDepth 200000 in
theorem cert1 (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero) : T1 K Y = (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (LibRing.castN K 36) (K.mul (LibRing.sub K (K.mul (K.add (Y 0) (Y 2)) (Y 0)) (K.mul (K.add K.zero (Y 1)) (Y 1))) (LibR  -- (+5858 chars; full line in the Zenodo source)
  rw [show T1 K Y = (LibRing.sub K (K.mul (LibRing.castN K 8) (K.mul (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (Y 0) (Y 2)) (K.mul (Y 1) (Y 1))) (K.mul (Y 1) (Y 3))) (K.mul (Y 2) (Y 0))) (K.mul (Y 2) (Y 2))) (K.mul (Y 2) (Y 4))) (K.mul (Y 3) (Y 1))) (K.mul (Y 3) (Y 3))) (K.mul (Y 4) (Y 2))) (K.mul (Y 4) (Y 4))) (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.  -- (+757 chars; full line in the Zenodo source)
  lib_ring

theorem lc1 (h1 : O.le K.zero K.one) (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero)
    (hD : ∀ u k, O.le K.zero (Dd K Y u (u + k))) : O.le K.zero (T1 K Y) := by
  rw [cert1 K Y h5 h6]
  exact (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn   -- (+2541 chars; full line in the Zenodo source)

def T2 (Y : Nat → R) : R := (LibRing.sub K (K.mul (LibRing.castN K 15) (K.mul (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (Y 0) (Y 3)) (K.mul (Y 1) (Y 2))) (K.mul (Y 1) (Y 4))) (K.mul (Y 2) (Y 1))) (K.mul (Y 2) (Y 3))) (K.mul (Y 3) (Y 0))) (K.mul (Y 3) (Y 2))) (K.mul (Y 3) (Y 4))) (K.mul (Y 4) (Y 1))) (K.mul (Y 4) (Y 3))) (K.add (K.add (K.add (K.add (K.add (K.add (K.add (  -- (+777 chars; full line in the Zenodo source)

set_option maxRecDepth 200000 in
theorem cert2 (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero) : T2 K Y = (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (LibRing.castN K 48) (K.mul (LibRing.sub K (K.mul (K.add (Y 0) (Y 2)) (Y 0)) (K.mul (K.add K.zero (Y 1)) (Y 1))) (LibRing.sub K (K.mul (K.a  -- (+5226 chars; full line in the Zenodo source)
  rw [show T2 K Y = (LibRing.sub K (K.mul (LibRing.castN K 15) (K.mul (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (Y 0) (Y 3)) (K.mul (Y 1) (Y 2))) (K.mul (Y 1) (Y 4))) (K.mul (Y 2) (Y 1))) (K.mul (Y 2) (Y 3))) (K.mul (Y 3) (Y 0))) (K.mul (Y 3) (Y 2))) (K.mul (Y 3) (Y 4))) (K.mul (Y 4) (Y 1))) (K.mul (Y 4) (Y 3))) (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K  -- (+787 chars; full line in the Zenodo source)
  lib_ring

theorem lc2 (h1 : O.le K.zero K.one) (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero)
    (hD : ∀ u k, O.le K.zero (Dd K Y u (u + k))) : O.le K.zero (T2 K Y) := by
  rw [cert2 K Y h5 h6]
  exact (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn   -- (+2244 chars; full line in the Zenodo source)

def T3 (Y : Nat → R) : R := (LibRing.sub K (K.mul (LibRing.castN K 24) (K.mul (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (Y 0) (Y 4)) (K.mul (Y 1) (Y 3))) (K.mul (Y 2) (Y 2))) (K.mul (Y 2) (Y 4))) (K.mul (Y 3) (Y 1))) (K.mul (Y 3) (Y 3))) (K.mul (Y 4) (Y 0))) (K.mul (Y 4) (Y 2))) (K.mul (Y 4) (Y 4))) (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (Y 0) (Y 4)) (K  -- (+637 chars; full line in the Zenodo source)

set_option maxRecDepth 200000 in
theorem cert3 (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero) : T3 K Y = (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (LibRing.castN K 88) (K.mul (LibRing.sub K (K.mul (K.add (Y 0) (Y 2)) (Y 0)) (K.mul (K.add K.zero (Y 1)) (Y 1))) (LibRing.sub K (K.mul (K.add (Y 4) (Y 6)) (Y 4)) (K.mul (K.ad  -- (+4160 chars; full line in the Zenodo source)
  rw [show T3 K Y = (LibRing.sub K (K.mul (LibRing.castN K 24) (K.mul (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (Y 0) (Y 4)) (K.mul (Y 1) (Y 3))) (K.mul (Y 2) (Y 2))) (K.mul (Y 2) (Y 4))) (K.mul (Y 3) (Y 1))) (K.mul (Y 3) (Y 3))) (K.mul (Y 4) (Y 0))) (K.mul (Y 4) (Y 2))) (K.mul (Y 4) (Y 4))) (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (Y 0) (Y 4)) (K.mul (Y   -- (+647 chars; full line in the Zenodo source)
  lib_ring

theorem lc3 (h1 : O.le K.zero K.one) (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero)
    (hD : ∀ u k, O.le K.zero (Dd K Y u (u + k))) : O.le K.zero (T3 K Y) := by
  rw [cert3 K Y h5 h6]
  exact (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 88) (O.mul_nonneg _ _ (hD 0 1  -- (+1735 chars; full line in the Zenodo source)

def T4 (Y : Nat → R) : R := (LibRing.sub K (K.mul (LibRing.castN K 35) (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul (Y 1) (Y 4)) (K.mul (Y 2) (Y 3))) (K.mul (Y 3) (Y 2))) (K.mul (Y 3) (Y 4))) (K.mul (Y 4) (Y 1))) (K.mul (Y 4) (Y 3))) (K.add (K.add (K.add (K.add (K.add (K.mul (Y 1) (Y 4)) (K.mul (Y 2) (Y 3))) (K.mul (Y 3) (Y 2))) (K.mul (Y 3) (Y 4))) (K.mul (Y 4) (Y 1))) (K.mul (Y 4) (Y 3)))))   -- (+385 chars; full line in the Zenodo source)

set_option maxRecDepth 200000 in
theorem cert4 (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero) : T4 K Y = (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (LibRing.castN K 20) (K.mul (LibRing.sub K (K.mul (K.add (Y 4) (Y 6)) (Y 0)) (K.mul (K.add K.zero (Y 1)) (Y 5))) (LibRing.sub K (K.mul (K.add (Y 4) (Y 6)) (Y 2)) (K.mul (K.add (Y 1) (Y 3)) (Y 5))))) (K.mul (LibRing.castN K 164) (K  -- (+2449 chars; full line in the Zenodo source)
  rw [show T4 K Y = (LibRing.sub K (K.mul (LibRing.castN K 35) (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul (Y 1) (Y 4)) (K.mul (Y 2) (Y 3))) (K.mul (Y 3) (Y 2))) (K.mul (Y 3) (Y 4))) (K.mul (Y 4) (Y 1))) (K.mul (Y 4) (Y 3))) (K.add (K.add (K.add (K.add (K.add (K.mul (Y 1) (Y 4)) (K.mul (Y 2) (Y 3))) (K.mul (Y 3) (Y 2))) (K.mul (Y 3) (Y 4))) (K.mul (Y 4) (Y 1))) (K.mul (Y 4) (Y 3))))) (K.mul (  -- (+395 chars; full line in the Zenodo source)
  lib_ring

theorem lc4 (h1 : O.le K.zero K.one) (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero)
    (hD : ∀ u k, O.le K.zero (Dd K Y u (u + k))) : O.le K.zero (T4 K Y) := by
  rw [cert4 K Y h5 h6]
  exact (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 20) (O.mul_nonneg _ _ (hD 0 5) (hD 2 3))) (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 164) (O.mul_nonneg _ _ (hD 1 1) (hD 4 1)))) (O.mul_nonneg _ _ (SU2CharEx  -- (+919 chars; full line in the Zenodo source)

def T5 (Y : Nat → R) : R := (LibRing.sub K (K.mul (LibRing.castN K 48) (K.mul (K.add (K.add (K.add (K.mul (Y 2) (Y 4)) (K.mul (Y 3) (Y 3))) (K.mul (Y 4) (Y 2))) (K.mul (Y 4) (Y 4))) (K.add (K.add (K.add (K.mul (Y 2) (Y 4)) (K.mul (Y 3) (Y 3))) (K.mul (Y 4) (Y 2))) (K.mul (Y 4) (Y 4))))) (K.mul (LibRing.castN K 49) (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul (Y 1) (Y 4)) (K.mul (Y 2) (Y 3))) (  -- (+133 chars; full line in the Zenodo source)

set_option maxRecDepth 200000 in
theorem cert5 (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero) : T5 K Y = (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (LibRing.castN K 196) (K.mul (LibRing.sub K (K.mul (K.add (Y 2) (Y 4)) (Y 2)) (K.mul (K.add (Y 1) (Y 3)) (Y 3))) (LibRing.sub K (K.mul (K.add (Y 4) (Y 6)) (Y 4)) (K.mul (K.add (Y 3) (Y 5)) (Y 5))))) (K.mul (LibRing.castN K 40) (K.mul (LibRing.sub K (K.mul (K.add (Y 4) (Y   -- (+1167 chars; full line in the Zenodo source)
  rw [show T5 K Y = (LibRing.sub K (K.mul (LibRing.castN K 48) (K.mul (K.add (K.add (K.add (K.mul (Y 2) (Y 4)) (K.mul (Y 3) (Y 3))) (K.mul (Y 4) (Y 2))) (K.mul (Y 4) (Y 4))) (K.add (K.add (K.add (K.mul (Y 2) (Y 4)) (K.mul (Y 3) (Y 3))) (K.mul (Y 4) (Y 2))) (K.mul (Y 4) (Y 4))))) (K.mul (LibRing.castN K 49) (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul (Y 1) (Y 4)) (K.mul (Y 2) (Y 3))) (K.mul (Y  -- (+143 chars; full line in the Zenodo source)
  lib_ring

theorem lc5 (h1 : O.le K.zero K.one) (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero)
    (hD : ∀ u k, O.le K.zero (Dd K Y u (u + k))) : O.le K.zero (T5 K Y) := by
  rw [cert5 K Y h5 h6]
  exact (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (add_nn K O _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 196) (O.mul_nonneg _ _ (hD 2 1) (hD 4 1))) (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 40) (O.mul_nonneg _ _ (hD 2 3) (hD 2 3)))) (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 92) (O.mul_nonneg _ _ (hD 2 3) (hD 3 1)))) (O.mul_nonneg _ _ (SU2CharExp.cN_nn K   -- (+304 chars; full line in the Zenodo source)

def T6 (Y : Nat → R) : R := (LibRing.sub K (K.mul (LibRing.castN K 63) (K.mul (K.add (K.mul (Y 3) (Y 4)) (K.mul (Y 4) (Y 3))) (K.add (K.mul (Y 3) (Y 4)) (K.mul (Y 4) (Y 3))))) (K.mul (LibRing.castN K 64) (K.mul (K.add (K.add (K.add (K.mul (Y 2) (Y 4)) (K.mul (Y 3) (Y 3))) (K.mul (Y 4) (Y 2))) (K.mul (Y 4) (Y 4))) (K.mul (Y 4) (Y 4)))))

set_option maxRecDepth 200000 in
theorem cert6 (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero) : T6 K Y = (K.add (K.add (K.mul (LibRing.castN K 60) (K.mul (LibRing.sub K (K.mul (K.add (Y 4) (Y 6)) (Y 2)) (K.mul (K.add (Y 1) (Y 3)) (Y 5))) (LibRing.sub K (K.mul (K.add (Y 4) (Y 6)) (Y 4)) (K.mul (K.add (Y 3) (Y 5)) (Y 5))))) (K.mul (LibRing.castN K 188) (K.mul (LibRing.sub K (K.mul (K.add (Y 3) (Y 5)) (Y 3)) (K.mul (K.add (Y   -- (+317 chars; full line in the Zenodo source)
  rw [show T6 K Y = (LibRing.sub K (K.mul (LibRing.castN K 63) (K.mul (K.add (K.mul (Y 3) (Y 4)) (K.mul (Y 4) (Y 3))) (K.add (K.mul (Y 3) (Y 4)) (K.mul (Y 4) (Y 3))))) (K.mul (LibRing.castN K 64) (K.mul (K.add (K.add (K.add (K.mul (Y 2) (Y 4)) (K.mul (Y 3) (Y 3))) (K.mul (Y 4) (Y 2))) (K.mul (Y 4) (Y 4))) (K.mul (Y 4) (Y 4))))) from rfl, h5, h6]
  lib_ring

theorem lc6 (h1 : O.le K.zero K.one) (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero)
    (hD : ∀ u k, O.le K.zero (Dd K Y u (u + k))) : O.le K.zero (T6 K Y) := by
  rw [cert6 K Y h5 h6]
  exact (add_nn K O _ _ (add_nn K O _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 60) (O.mul_nonneg _ _ (hD 2 3) (hD 4 1))) (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 188) (O.mul_nonneg _ _ (hD 3 1) (hD 4 1)))) (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 124) (O.mul_nonneg _ _ (hD 4 1) (hD 4 1))))

def T7 (Y : Nat → R) : R := (LibRing.sub K (K.mul (LibRing.castN K 80) (K.mul (K.mul (Y 4) (Y 4)) (K.mul (Y 4) (Y 4)))) (K.mul (LibRing.castN K 81) (K.mul (K.add (K.mul (Y 3) (Y 4)) (K.mul (Y 4) (Y 3))) K.zero)))

set_option maxRecDepth 200000 in
theorem cert7 (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero) : T7 K Y = (K.mul (LibRing.castN K 80) (K.mul (LibRing.sub K (K.mul (K.add (Y 4) (Y 6)) (Y 4)) (K.mul (K.add (Y 3) (Y 5)) (Y 5))) (LibRing.sub K (K.mul (K.add (Y 4) (Y 6)) (Y 4)) (K.mul (K.add (Y 3) (Y 5)) (Y 5))))) := by
  rw [show T7 K Y = (LibRing.sub K (K.mul (LibRing.castN K 80) (K.mul (K.mul (Y 4) (Y 4)) (K.mul (Y 4) (Y 4)))) (K.mul (LibRing.castN K 81) (K.mul (K.add (K.mul (Y 3) (Y 4)) (K.mul (Y 4) (Y 3))) K.zero))) from rfl, h5, h6]
  lib_ring

theorem lc7 (h1 : O.le K.zero K.one) (Y : Nat → R) (h5 : Y 5 = K.zero) (h6 : Y 6 = K.zero)
    (hD : ∀ u k, O.le K.zero (Dd K Y u (u + k))) : O.le K.zero (T7 K Y) := by
  rw [cert7 K Y h5 h6]
  exact (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 80) (O.mul_nonneg _ _ (hD 4 1) (hD 4 1)))

theorem one_nn (h : O.le K.zero K.one ∨ O.le K.one K.zero) : O.le K.zero K.one := by
  cases h with
  | inl h => exact h
  | inr h =>
    have hn := HeatV.sub_nn K O _ _ h
    have := O.mul_nonneg _ _ hn hn
    rw [show K.mul (K.add K.zero (K.neg K.one)) (K.add K.zero (K.neg K.one)) = K.one by lib_ring] at this; exact this

/-- the window minors of a v-good list of length 5 are nonnegative -/
theorem minors (F : OField.OF K O) (y0 y1 y2 y3 y4 : R) (hXg : L1Full.Good K O [y0, y1, y2, y3, y4]) :
    ∀ u k, O.le K.zero (Dd K (co K [y0, y1, y2, y3, y4]) u (u + k)) := by
  have ⟨hb, hpos, hv, h0⟩ := hXg
  have hz : ∀ t, 6 ≤ t → CgWindow.Zs K (co K [y0, y1, y2, y3, y4]) t = K.zero := fun t ht => by
    cases t with
    | zero => exact absurd ht (Nat.not_succ_le_zero _)
    | succ t => exact HeatLR.co_out K [y0, y1, y2, y3, y4] t (Nat.le_of_succ_le_succ ht)
  have hY : ∀ j, O.le K.zero (CgWindow.Zs K (co K [y0, y1, y2, y3, y4]) j) := fun j => by cases j with
    | zero => exact O.le_refl _
    | succ j => exact hb j
  exact fun a k => Window.dl_nn_t K O F (CgWindow.Zs K (co K [y0, y1, y2, y3, y4])) hY 6
    (fun j hj => hpos j (Nat.lt_of_succ_lt_succ hj)) hz (CgWindow.VV_Zs K O _ hv h0) a k

theorem lit1 (F : OField.OF K O) (y0 y1 y2 y3 y4 : R) (hXg : L1Full.Good K O [y0, y1, y2, y3, y4]) :
    O.le (K.mul (LibRing.castN K 9) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 1) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 3))) (K.mul (LibRing.castN K 8) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 2) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 2))) := by
  have h1 := one_nn K O (O.le_total K.zero K.one)
  have t := lc1 K O h1 (co K [y0, y1, y2, y3, y4]) rfl rfl (minors K O F y0 y1 y2 y3 y4 hXg)
  rw [cgc1, cgc2, cgc3]
  have s := O.add_le_add _ _ _ _ t (O.le_refl (K.mul (LibRing.castN K 9) (K.mul ((K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 0) (co K [y0, y1, y2, y3, y4] 1)) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 0))) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1, y2, y3, y4]   -- (+1038 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_))
  · show _ = K.add K.zero _; lib_ring
  · show K.add (LibRing.sub K _ _) _ = _; lib_ring

/-- **(A) for v-good lists of length 5 at index n = 1**: 9 c_1 c_3 ≤ 8 c_2² -/
theorem prod_lc1 (F : OField.OF K O) (X : List R) (hXg : L1Full.Good K O X) (hX : X.length = 5) :
    O.le (K.mul (LibRing.castN K 9) (K.mul (cgc K X X 1) (cgc K X X 3))) (K.mul (LibRing.castN K 8) (K.mul (cgc K X X 2) (cgc K X X 2))) := by
  cases X with
  | nil => exact Nat.noConfusion hX
  | cons y0 X => cases X with
    | nil => exact Nat.noConfusion (Nat.succ.inj hX)
    | cons y1 X => cases X with
      | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj hX))
      | cons y2 X => cases X with
        | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))
        | cons y3 X => cases X with
          | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX))))
          | cons y4 X => cases X with
            | nil => exact lit1 K O F y0 y1 y2 y3 y4 hXg
            | cons y5 X => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))))

theorem lit2 (F : OField.OF K O) (y0 y1 y2 y3 y4 : R) (hXg : L1Full.Good K O [y0, y1, y2, y3, y4]) :
    O.le (K.mul (LibRing.castN K 16) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 2) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 4))) (K.mul (LibRing.castN K 15) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 3) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 3))) := by
  have h1 := one_nn K O (O.le_total K.zero K.one)
  have t := lc2 K O h1 (co K [y0, y1, y2, y3, y4]) rfl rfl (minors K O F y0 y1 y2 y3 y4 hXg)
  rw [cgc2, cgc3, cgc4]
  have s := O.add_le_add _ _ _ _ t (O.le_refl (K.mul (LibRing.castN K 16) (K.mul ((K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 0) (co K [y0, y1, y2, y3, y4] 2)) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 1))) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 3))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y  -- (+1115 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_))
  · show _ = K.add K.zero _; lib_ring
  · show K.add (LibRing.sub K _ _) _ = _; lib_ring

/-- **(A) for v-good lists of length 5 at index n = 2**: 16 c_2 c_4 ≤ 15 c_3² -/
theorem prod_lc2 (F : OField.OF K O) (X : List R) (hXg : L1Full.Good K O X) (hX : X.length = 5) :
    O.le (K.mul (LibRing.castN K 16) (K.mul (cgc K X X 2) (cgc K X X 4))) (K.mul (LibRing.castN K 15) (K.mul (cgc K X X 3) (cgc K X X 3))) := by
  cases X with
  | nil => exact Nat.noConfusion hX
  | cons y0 X => cases X with
    | nil => exact Nat.noConfusion (Nat.succ.inj hX)
    | cons y1 X => cases X with
      | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj hX))
      | cons y2 X => cases X with
        | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))
        | cons y3 X => cases X with
          | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX))))
          | cons y4 X => cases X with
            | nil => exact lit2 K O F y0 y1 y2 y3 y4 hXg
            | cons y5 X => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))))

theorem lit3 (F : OField.OF K O) (y0 y1 y2 y3 y4 : R) (hXg : L1Full.Good K O [y0, y1, y2, y3, y4]) :
    O.le (K.mul (LibRing.castN K 25) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 3) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 5))) (K.mul (LibRing.castN K 24) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 4) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 4))) := by
  have h1 := one_nn K O (O.le_total K.zero K.one)
  have t := lc3 K O h1 (co K [y0, y1, y2, y3, y4]) rfl rfl (minors K O F y0 y1 y2 y3 y4 hXg)
  rw [cgc3, cgc4, cgc5]
  have s := O.add_le_add _ _ _ _ t (O.le_refl (K.mul (LibRing.castN K 25) (K.mul ((K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 0) (co K [y0, y1, y2, y3, y4] 3)) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 4))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y  -- (+887 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_))
  · show _ = K.add K.zero _; lib_ring
  · show K.add (LibRing.sub K _ _) _ = _; lib_ring

/-- **(A) for v-good lists of length 5 at index n = 3**: 25 c_3 c_5 ≤ 24 c_4² -/
theorem prod_lc3 (F : OField.OF K O) (X : List R) (hXg : L1Full.Good K O X) (hX : X.length = 5) :
    O.le (K.mul (LibRing.castN K 25) (K.mul (cgc K X X 3) (cgc K X X 5))) (K.mul (LibRing.castN K 24) (K.mul (cgc K X X 4) (cgc K X X 4))) := by
  cases X with
  | nil => exact Nat.noConfusion hX
  | cons y0 X => cases X with
    | nil => exact Nat.noConfusion (Nat.succ.inj hX)
    | cons y1 X => cases X with
      | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj hX))
      | cons y2 X => cases X with
        | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))
        | cons y3 X => cases X with
          | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX))))
          | cons y4 X => cases X with
            | nil => exact lit3 K O F y0 y1 y2 y3 y4 hXg
            | cons y5 X => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))))

theorem lit4 (F : OField.OF K O) (y0 y1 y2 y3 y4 : R) (hXg : L1Full.Good K O [y0, y1, y2, y3, y4]) :
    O.le (K.mul (LibRing.castN K 36) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 4) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 6))) (K.mul (LibRing.castN K 35) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 5) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 5))) := by
  have h1 := one_nn K O (O.le_total K.zero K.one)
  have t := lc4 K O h1 (co K [y0, y1, y2, y3, y4]) rfl rfl (minors K O F y0 y1 y2 y3 y4 hXg)
  rw [cgc4, cgc5, cgc6]
  have s := O.add_le_add _ _ _ _ t (O.le_refl (K.mul (LibRing.castN K 36) (K.mul ((K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 0) (co K [y0, y1, y2, y3, y4] 4)) (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 3))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1, y2,   -- (+659 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_))
  · show _ = K.add K.zero _; lib_ring
  · show K.add (LibRing.sub K _ _) _ = _; lib_ring

/-- **(A) for v-good lists of length 5 at index n = 4**: 36 c_4 c_6 ≤ 35 c_5² -/
theorem prod_lc4 (F : OField.OF K O) (X : List R) (hXg : L1Full.Good K O X) (hX : X.length = 5) :
    O.le (K.mul (LibRing.castN K 36) (K.mul (cgc K X X 4) (cgc K X X 6))) (K.mul (LibRing.castN K 35) (K.mul (cgc K X X 5) (cgc K X X 5))) := by
  cases X with
  | nil => exact Nat.noConfusion hX
  | cons y0 X => cases X with
    | nil => exact Nat.noConfusion (Nat.succ.inj hX)
    | cons y1 X => cases X with
      | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj hX))
      | cons y2 X => cases X with
        | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))
        | cons y3 X => cases X with
          | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX))))
          | cons y4 X => cases X with
            | nil => exact lit4 K O F y0 y1 y2 y3 y4 hXg
            | cons y5 X => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))))

theorem lit5 (F : OField.OF K O) (y0 y1 y2 y3 y4 : R) (hXg : L1Full.Good K O [y0, y1, y2, y3, y4]) :
    O.le (K.mul (LibRing.castN K 49) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 5) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 7))) (K.mul (LibRing.castN K 48) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 6) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 6))) := by
  have h1 := one_nn K O (O.le_total K.zero K.one)
  have t := lc5 K O h1 (co K [y0, y1, y2, y3, y4]) rfl rfl (minors K O F y0 y1 y2 y3 y4 hXg)
  rw [cgc5, cgc6, cgc7]
  have s := O.add_le_add _ _ _ _ t (O.le_refl (K.mul (LibRing.castN K 49) (K.mul ((K.add (K.add (K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 1) (co K [y0, y1, y2, y3, y4] 4)) (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1, y2, y3, y4] 3))) (K.mul (co K [y0, y1, y2, y3, y4] 3) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 3) (co K [y0, y1, y2, y3, y4] 4))) (K.mul (  -- (+279 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_))
  · show _ = K.add K.zero _; lib_ring
  · show K.add (LibRing.sub K _ _) _ = _; lib_ring

/-- **(A) for v-good lists of length 5 at index n = 5**: 49 c_5 c_7 ≤ 48 c_6² -/
theorem prod_lc5 (F : OField.OF K O) (X : List R) (hXg : L1Full.Good K O X) (hX : X.length = 5) :
    O.le (K.mul (LibRing.castN K 49) (K.mul (cgc K X X 5) (cgc K X X 7))) (K.mul (LibRing.castN K 48) (K.mul (cgc K X X 6) (cgc K X X 6))) := by
  cases X with
  | nil => exact Nat.noConfusion hX
  | cons y0 X => cases X with
    | nil => exact Nat.noConfusion (Nat.succ.inj hX)
    | cons y1 X => cases X with
      | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj hX))
      | cons y2 X => cases X with
        | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))
        | cons y3 X => cases X with
          | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX))))
          | cons y4 X => cases X with
            | nil => exact lit5 K O F y0 y1 y2 y3 y4 hXg
            | cons y5 X => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))))

theorem lit6 (F : OField.OF K O) (y0 y1 y2 y3 y4 : R) (hXg : L1Full.Good K O [y0, y1, y2, y3, y4]) :
    O.le (K.mul (LibRing.castN K 64) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 6) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 8))) (K.mul (LibRing.castN K 63) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 7) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 7))) := by
  have h1 := one_nn K O (O.le_total K.zero K.one)
  have t := lc6 K O h1 (co K [y0, y1, y2, y3, y4]) rfl rfl (minors K O F y0 y1 y2 y3 y4 hXg)
  rw [cgc6, cgc7, cgc8]
  have s := O.add_le_add _ _ _ _ t (O.le_refl (K.mul (LibRing.castN K 64) (K.mul ((K.add (K.add (K.add (K.mul (co K [y0, y1, y2, y3, y4] 2) (co K [y0, y1, y2, y3, y4] 4)) (K.mul (co K [y0, y1, y2, y3, y4] 3) (co K [y0, y1, y2, y3, y4] 3))) (K.mul (co K [y0, y1, y2, y3, y4] 4) (co K [y0, y1, y2, y3, y4] 2))) (K.mul (co K [y0, y1, y2, y3, y4] 4) (co K [y0, y1, y2, y3, y4] 4)))) ((K.mul (co K [y0, y1  -- (+51 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_))
  · show _ = K.add K.zero _; lib_ring
  · show K.add (LibRing.sub K _ _) _ = _; lib_ring

/-- **(A) for v-good lists of length 5 at index n = 6**: 64 c_6 c_8 ≤ 63 c_7² -/
theorem prod_lc6 (F : OField.OF K O) (X : List R) (hXg : L1Full.Good K O X) (hX : X.length = 5) :
    O.le (K.mul (LibRing.castN K 64) (K.mul (cgc K X X 6) (cgc K X X 8))) (K.mul (LibRing.castN K 63) (K.mul (cgc K X X 7) (cgc K X X 7))) := by
  cases X with
  | nil => exact Nat.noConfusion hX
  | cons y0 X => cases X with
    | nil => exact Nat.noConfusion (Nat.succ.inj hX)
    | cons y1 X => cases X with
      | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj hX))
      | cons y2 X => cases X with
        | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))
        | cons y3 X => cases X with
          | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX))))
          | cons y4 X => cases X with
            | nil => exact lit6 K O F y0 y1 y2 y3 y4 hXg
            | cons y5 X => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))))

theorem lit7 (F : OField.OF K O) (y0 y1 y2 y3 y4 : R) (hXg : L1Full.Good K O [y0, y1, y2, y3, y4]) :
    O.le (K.mul (LibRing.castN K 81) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 7) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 9))) (K.mul (LibRing.castN K 80) (K.mul (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 8) (cgc K [y0, y1, y2, y3, y4] [y0, y1, y2, y3, y4] 8))) := by
  have h1 := one_nn K O (O.le_total K.zero K.one)
  have t := lc7 K O h1 (co K [y0, y1, y2, y3, y4]) rfl rfl (minors K O F y0 y1 y2 y3 y4 hXg)
  rw [cgc7, cgc8, cgc9]
  have s := O.add_le_add _ _ _ _ t (O.le_refl (K.mul (LibRing.castN K 81) (K.mul ((K.add (K.mul (co K [y0, y1, y2, y3, y4] 3) (co K [y0, y1, y2, y3, y4] 4)) (K.mul (co K [y0, y1, y2, y3, y4] 4) (co K [y0, y1, y2, y3, y4] 3)))) (K.zero))))
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_))
  · show _ = K.add K.zero _; lib_ring
  · show K.add (LibRing.sub K _ _) _ = _; lib_ring

/-- **(A) for v-good lists of length 5 at index n = 7**: 81 c_7 c_9 ≤ 80 c_8² -/
theorem prod_lc7 (F : OField.OF K O) (X : List R) (hXg : L1Full.Good K O X) (hX : X.length = 5) :
    O.le (K.mul (LibRing.castN K 81) (K.mul (cgc K X X 7) (cgc K X X 9))) (K.mul (LibRing.castN K 80) (K.mul (cgc K X X 8) (cgc K X X 8))) := by
  cases X with
  | nil => exact Nat.noConfusion hX
  | cons y0 X => cases X with
    | nil => exact Nat.noConfusion (Nat.succ.inj hX)
    | cons y1 X => cases X with
      | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj hX))
      | cons y2 X => cases X with
        | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))
        | cons y3 X => cases X with
          | nil => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX))))
          | cons y4 X => cases X with
            | nil => exact lit7 K O F y0 y1 y2 y3 y4 hXg
            | cons y5 X => exact Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj hX)))))

end LCL5

#print axioms LCL5.add_nn
#print axioms LCL5.cgc0
#print axioms LCL5.cgc1
#print axioms LCL5.cgc2
#print axioms LCL5.cgc3
#print axioms LCL5.cgc4
#print axioms LCL5.cgc5
#print axioms LCL5.cgc6
#print axioms LCL5.cgc7
#print axioms LCL5.cgc8
#print axioms LCL5.cgc9
#print axioms LCL5.cert1
#print axioms LCL5.lc1
#print axioms LCL5.cert2
#print axioms LCL5.lc2
#print axioms LCL5.cert3
#print axioms LCL5.lc3
#print axioms LCL5.cert4
#print axioms LCL5.lc4
#print axioms LCL5.cert5
#print axioms LCL5.lc5
#print axioms LCL5.cert6
#print axioms LCL5.lc6
#print axioms LCL5.cert7
#print axioms LCL5.lc7
#print axioms LCL5.one_nn
#print axioms LCL5.minors
#print axioms LCL5.lit1
#print axioms LCL5.prod_lc1
#print axioms LCL5.lit2
#print axioms LCL5.prod_lc2
#print axioms LCL5.lit3
#print axioms LCL5.prod_lc3
#print axioms LCL5.lit4
#print axioms LCL5.prod_lc4
#print axioms LCL5.lit5
#print axioms LCL5.prod_lc5
#print axioms LCL5.lit6
#print axioms LCL5.prod_lc6
#print axioms LCL5.lit7
#print axioms LCL5.prod_lc7

#print axioms: 41 theorems, no axioms.

90.91 Log-concavity of products needs \(v\)-monotonicity

Log-concave nonincreasing \(a\) alone does not give a log-concave product: \(X=[1,2,3]\) is a counterexample.

\[ c=X\cdot X=(14,16,19,\dots ),\qquad 4c_0c_2=1064>768=3c_1^2 \]

ym/HoggarFalse.lean

/-
  ym/HoggarFalse — T45, G44 (A): the "SU(2) Hoggar" form of (A) is false. For a class function with coefficients
  c_n = (n+1) a_n, log-concavity and monotonicity of a alone do NOT make the bond-moving product log-concave in a;
  the v-good hypothesis of LC0 / LCL* is essential.
    (i)  X = [1,2,3] (a = (1,1,1), log-concave, nonincreasing): c = X·X has 4 c₀ c₂ > 3 c₁² (1064 > 768),
         i.e. a₁² < a₀ a₂ at the first index (compare LC0.prod_lc0, which needs v-good).
    (ii) X = [5,8,9] (a = (5,4,3)), Y = [20,34] (a = (20,17)): c = X·Y has 9 c₁ c₃ > 8 c₂², i.e. a₂² < a₁ a₃.
  The natural-number values are checked by the kernel (decide); cgc_cast transfers them to any ring.
  Lean 4 core only, no axioms.
-/
import ym.CertSound

namespace HoggarFalse

open YMChain SU2Char MigdalCone

variable {R : Type} (K : DetChain.CRing R)

theorem dot_congr (f g : Nat → R) (h : ∀ j, f j = g j) : ∀ (m : Nat) (l : List R), dot K m l f = dot K m l g
  | _, [] => rfl
  | m, c :: l => by
    show K.add (K.mul c (f m)) (dot K (m + 1) l f) = K.add (K.mul c (g m)) (dot K (m + 1) l g)
    rw [h m, dot_congr f g h (m + 1) l]

theorem dot_cast (g : Nat → Nat) : ∀ (m : Nat) (l : List Nat),
    dot K m (l.map (cN K)) (fun j => cN K (g j)) = cN K (CertSound.dotN m l g)
  | _, [] => rfl
  | m, c :: l => by
    show K.add (K.mul (cN K c) (cN K (g m))) (dot K (m + 1) (l.map (cN K)) (fun j => cN K (g j)))
      = cN K (c * g m + CertSound.dotN (m + 1) l g)
    rw [dot_cast g (m + 1) l, cN_add, SU2CharExp.cN_mul]

/-- integer lists: the bond-moving product is the cast of the natural-number product -/
theorem cgc_cast (la lb : List Nat) (n : Nat) :
    cgc K (la.map (cN K)) (lb.map (cN K)) n = cN K (CertSound.cgcN la lb n) := by
  show dot K 0 (la.map (cN K)) (fun j => dot K 0 (lb.map (cN K)) (fun k => cN K (Fusion.cg j k n))) = _
  rw [dot_congr K _ (fun j => cN K (CertSound.dotN 0 lb (fun k => Fusion.cg j k n)))
    (fun j => dot_cast K (fun k => Fusion.cg j k n) 0 lb) 0]
  exact dot_cast K _ 0 la

/-- (i) values of X·X for X = [1,2,3] -/
theorem xx_vals : CertSound.cgcN [1, 2, 3] [1, 2, 3] 0 = 14 ∧ CertSound.cgcN [1, 2, 3] [1, 2, 3] 1 = 16 ∧
    CertSound.cgcN [1, 2, 3] [1, 2, 3] 2 = 19 := by decide

/-- (i) a = (1,1,1) is log-concave and nonincreasing, but the product fails log-concavity at n = 0 -/
theorem xx_fails : 3 * (CertSound.cgcN [1, 2, 3] [1, 2, 3] 1 * CertSound.cgcN [1, 2, 3] [1, 2, 3] 1)
    < 4 * (CertSound.cgcN [1, 2, 3] [1, 2, 3] 0 * CertSound.cgcN [1, 2, 3] [1, 2, 3] 2) := by decide

/-- (ii) a = (5,4,3), b = (20,17) (log-concave, nonincreasing): a(X·Y) fails log-concavity at index 2 -/
theorem xy_input : 4 * 4 ≥ 5 * 3 ∧ 5 ≥ 4 ∧ 4 ≥ 3 ∧ 20 ≥ 17 := by decide

theorem xy_fails : 8 * (CertSound.cgcN [5, 8, 9] [20, 34] 2 * CertSound.cgcN [5, 8, 9] [20, 34] 2)
    < 9 * (CertSound.cgcN [5, 8, 9] [20, 34] 1 * CertSound.cgcN [5, 8, 9] [20, 34] 3) := by decide

end HoggarFalse

#print axioms HoggarFalse.dot_congr
#print axioms HoggarFalse.dot_cast
#print axioms HoggarFalse.cgc_cast
#print axioms HoggarFalse.xx_vals
#print axioms HoggarFalse.xx_fails
#print axioms HoggarFalse.xy_input
#print axioms HoggarFalse.xy_fails

#print axioms: 7 theorems, no axioms.

90.92 Turán coefficients of the Wilson weight

Every power-series coefficient of \(a_n^2-a_{n-1}a_{n+1}\) for the Wilson weight is nonnegative.

\[ m!\,(m+2\mu )!\,[t^{2\mu +2m}]\bigl (I_\mu ^2-I_{\mu -1}I_{\mu +1}\bigr )=\tbinom {2m+2\mu }{m+\mu }-\tbinom {2m+2\mu }{m+\mu -1}\ge 0 \]

ym/TuranCoef.lean

/-
  ym/TuranCoef — T45, G44-L0 piece: the Turán inequality for the SU(2) Wilson weight, coefficient by coefficient.
  The Wilson weight has a_n = c_n/(n+1) = Σ_k t^{n+2k}/(k!(n+k+1)!) = I_{n+1}(2t)/t (SU2CharExp, ballot numbers).
  For μ = ν + 1 the coefficient of t^{2μ+2m} in I_μ² − I_{μ−1}I_{μ+1}, multiplied by m!(m+2μ)!, is
      Σ_j C(m,j)C(m+2μ, m+μ−j) − Σ_j C(m,j)C(m+2μ, m+μ−1−j) = C(2m+2μ, m+μ) − C(2m+2μ, m+μ−1) ≥ 0
  (Vandermonde, then the central binomial coefficient is the largest). Hence every power-series coefficient of the
  Turánian is ≥ 0, i.e. a_n² ≥ a_{n−1}a_{n+1} for the full (untruncated) series at every t ≥ 0.
    absorb     C(n,k+1)(k+1) + C(n,k)k = C(n,k)n
    central    C(2M+2, M) ≤ C(2M+2, M+1)
    turan_coef conv (C m) (C (m+2ν+2)) (m+ν) ≤ conv (C m) (C (m+2ν+2)) (m+ν+1)
  Lean 4 core only, no axioms.
-/
import lib.Vandermonde
import lib.NatLe

namespace TuranCoef

open LibBinom LibVandermonde

theorem absorb : ∀ n k, C n (k + 1) * (k + 1) + C n k * k = C n k * n
  | 0, 0 => rfl
  | 0, k + 1 => by show (0 : Nat) * (k + 1 + 1) + 0 * (k + 1) = 0 * 0; lib_nat
  | n + 1, 0 => by
    have h' : C n (0 + 1) * (0 + 1) + C n 0 * 0 = C n 0 * n := absorb n 0
    rw [C_zero_right] at h'
    have e : C n 1 = n := by
      have h2 : C n 1 * 1 + 1 * 0 = 1 * n := h'
      rw [Nat.mul_one, Nat.mul_zero, Nat.add_zero, Nat.one_mul] at h2; exact h2
    show (C n 0 + C n 1) * 1 + C (n + 1) 0 * 0 = C (n + 1) 0 * (n + 1)
    rw [C_zero_right, C_zero_right, e]; lib_nat
  | n + 1, k + 1 => by
    have h1 := absorb n (k + 1)
    have h0 := absorb n k
    show (C n (k + 1) + C n (k + 1 + 1)) * (k + 1 + 1) + (C n k + C n (k + 1)) * (k + 1) = (C n k + C n (k + 1)) * (n + 1)
    rw [show (C n k + C n (k + 1)) * (n + 1) = C n k * n + C n (k + 1) * n + C n k + C n (k + 1) by lib_nat, ← h0, ← h1]
    lib_nat

theorem add_cancel (a b : Nat) : ∀ c, a + c = b + c → a = b
  | 0, h => h
  | c + 1, h => add_cancel a b c (Nat.succ.inj h)

theorem add_le_cancel (a b : Nat) : ∀ c, a + c ≤ b + c → a ≤ b
  | 0, h => h
  | c + 1, h => add_le_cancel a b c (Nat.le_of_succ_le_succ h)

theorem mul_le_cancel (s : Nat) : ∀ a b, a * (s + 1) ≤ b * (s + 1) → a ≤ b
  | 0, _, _ => Nat.zero_le _
  | a + 1, 0, h => by
    rw [show (a + 1) * (s + 1) = ((a + 1) * s + a) + 1 by lib_nat, show (0 : Nat) * (s + 1) = 0 by lib_nat] at h
    exact absurd h (Nat.not_succ_le_zero _)
  | a + 1, b + 1, h => by
    rw [show (a + 1) * (s + 1) = a * (s + 1) + (s + 1) by lib_nat,
      show (b + 1) * (s + 1) = b * (s + 1) + (s + 1) by lib_nat] at h
    exact Nat.succ_le_succ (mul_le_cancel s a b (add_le_cancel _ _ _ h))

theorem central (M : Nat) : C (2 * M + 2) M ≤ C (2 * M + 2) (M + 1) := by
  have h := absorb (2 * M + 2) M
  have e : C (2 * M + 2) (M + 1) * (M + 1) = C (2 * M + 2) M * (M + 2) := by
    apply add_cancel _ _ (C (2 * M + 2) M * M)
    rw [h]; lib_nat
  have le : C (2 * M + 2) M * (M + 1) ≤ C (2 * M + 2) (M + 1) * (M + 1) := by
    rw [e]; exact LibNatLe.le_of_eq_add (show C (2 * M + 2) M * (M + 2) = C (2 * M + 2) M * (M + 1) + C (2 * M + 2) M by lib_nat)
  exact mul_le_cancel M _ _ le

/-- **Turán, coefficientwise**: the m-th Turánian coefficient of I_{ν+1} is ≥ 0 -/
theorem turan_coef (m ν : Nat) :
    conv (C m) (C (m + 2 * ν + 2)) (m + ν) ≤ conv (C m) (C (m + 2 * ν + 2)) (m + ν + 1) := by
  rw [vandermonde, vandermonde, show m + (m + 2 * ν + 2) = 2 * (m + ν) + 2 by lib_nat]
  exact central (m + ν)

end TuranCoef

#print axioms TuranCoef.absorb
#print axioms TuranCoef.add_cancel
#print axioms TuranCoef.add_le_cancel
#print axioms TuranCoef.mul_le_cancel
#print axioms TuranCoef.central
#print axioms TuranCoef.turan_coef

#print axioms: 6 theorems, no axioms.

90.93 Turán coefficients on every truncation window

On every symmetric window the Turán coefficient inequality of the Wilson weight still holds.

\[ \sum _{j=a}^{m-a}\tbinom {m}{j}\tbinom {N}{\nu +j}\le \sum _{j=a}^{m-a}\tbinom {m}{j}\tbinom {N}{\nu +1+j},\qquad N=m+2\nu +2 \]

ym/TuranWin.lean

/-
  ym/TuranWin — T45, G51 (Wilson weight log-concave at every truncation), binomial part.
  For N = r + s:  (r+1)C(N,r+1) = s·C(N,r)  and  (s+1)C(N,r−1) = r·C(N,r)  (absorb), hence
      (r+1)(s+1)·(C(N,r−1) + C(N,r+1)) = C(N,r)·(r(r+1) + s(s+1)),
  so the end pair 2C(N,r) − C(N,r−1) − C(N,r+1) of a symmetric window has the sign of r + s + 2 − (r − s)²
  (end_pos, end_neg); and C(N,k) ≤ C(N,k+1) below the centre (mono_half). Lean 4 core only, no axioms.
-/
import ym.TuranCoef

namespace TuranWin

open LibBinom TuranCoef

/-- (r+1) C(r+s, r+1) = s C(r+s, r) -/
theorem up (r s : Nat) : C (r + s) (r + 1) * (r + 1) = C (r + s) r * s := by
  have h := absorb (r + s) r
  apply add_cancel _ _ (C (r + s) r * r)
  rw [h]; lib_nat

/-- (s+1) C(r+1+s, r) = (r+1) C(r+1+s, r+1) -/
theorem down (r s : Nat) : C (r + 1 + s) r * (s + 1) = C (r + 1 + s) (r + 1) * (r + 1) := by
  have h := absorb (r + 1 + s) r
  apply add_cancel _ _ (C (r + 1 + s) r * r)
  rw [show C (r + 1 + s) r * (s + 1) + C (r + 1 + s) r * r = C (r + 1 + s) r * (r + 1 + s) by lib_nat, ← h]

/-- below the centre the binomial coefficients increase: C(k+1+k+e, k) ≤ C(k+1+k+e, k+1) -/
theorem mono_half (k e : Nat) : C (k + (k + 1 + e)) k ≤ C (k + (k + 1 + e)) (k + 1) := by
  have h := up k (k + 1 + e)
  have le : C (k + (k + 1 + e)) k * (k + 1) ≤ C (k + (k + 1 + e)) (k + 1) * (k + 1) := by
    rw [h]; exact LibNatLe.le_of_eq_add (show C (k + (k + 1 + e)) k * (k + 1 + e) = C (k + (k + 1 + e)) k * (k + 1) + C (k + (k + 1 + e)) k * e by lib_nat)
  exact mul_le_cancel k _ _ le

/-- the end-pair identity, N = (r+1) + (s+1), centre value c = C(N, r+1):
    (r+2)(s+2)(C(N,r) + C(N,r+2)) = c((r+1)(r+2) + (s+1)(s+2)) -/
theorem pair_id (r s : Nat) :
    (r + 2) * (s + 2) * (C (r + 1 + (s + 1)) r + C (r + 1 + (s + 1)) (r + 1 + 1))
      = C (r + 1 + (s + 1)) (r + 1) * ((r + 1) * (r + 2) + (s + 1) * (s + 2)) := by
  have hu := up (r + 1) (s + 1)
  have hd := down r (s + 1)
  have e : (r + 2) * (s + 2) * (C (r + 1 + (s + 1)) r + C (r + 1 + (s + 1)) (r + 1 + 1))
      = (r + 2) * (C (r + 1 + (s + 1)) r * (s + 1 + 1)) + (s + 2) * (C (r + 1 + (s + 1)) (r + 1 + 1) * (r + 1 + 1)) := by
    lib_nat
  rw [e, hd, hu]; lib_nat

/-- end pair ≥ 0 when (s−r)² ≤ r+s+4 (s = r + g): C(N,r) + C(N,r+2) ≤ 2 C(N,r+1), N = (r+1)+(s+1) -/
theorem end_pos (r g : Nat) (h : g * g ≤ r + (r + g) + 4) :
    C (r + 1 + (r + g + 1)) r + C (r + 1 + (r + g + 1)) (r + 1 + 1) ≤ 2 * C (r + 1 + (r + g + 1)) (r + 1) := by
  have hp := pair_id r (r + g)
  have ⟨t, ht⟩ := Nat.le.dest h
  let c := C (r + 1 + (r + g + 1)) (r + 1)
  let Q1 := (r + 1) * (r + 2) + (r + g + 1) * (r + g + 2)
  have hQ : 2 * ((r + 2) * (r + g + 2)) = Q1 + t := by
    apply add_cancel _ _ (g * g)
    rw [show Q1 + t + g * g = Q1 + (g * g + t) by lib_nat, ht]
    show 2 * ((r + 2) * (r + g + 2)) + g * g = (r + 1) * (r + 2) + (r + g + 1) * (r + g + 2) + (r + (r + g) + 4)
    lib_nat
  have le : (C (r + 1 + (r + g + 1)) r + C (r + 1 + (r + g + 1)) (r + 1 + 1)) * ((r + 1) * (r + g + 2) + (r + g + 1) + 1)
      ≤ 2 * C (r + 1 + (r + g + 1)) (r + 1) * ((r + 1) * (r + g + 2) + (r + g + 1) + 1) := by
    rw [show (C (r + 1 + (r + g + 1)) r + C (r + 1 + (r + g + 1)) (r + 1 + 1)) * ((r + 1) * (r + g + 2) + (r + g + 1) + 1)
        = (r + 2) * (r + g + 2) * (C (r + 1 + (r + g + 1)) r + C (r + 1 + (r + g + 1)) (r + 1 + 1)) by lib_nat, hp,
      show 2 * C (r + 1 + (r + g + 1)) (r + 1) * ((r + 1) * (r + g + 2) + (r + g + 1) + 1)
        = C (r + 1 + (r + g + 1)) (r + 1) * (2 * ((r + 2) * (r + g + 2))) by lib_nat, hQ]
    exact LibNatLe.le_of_eq_add (show c * (Q1 + t) = c * Q1 + c * t by lib_nat)
  exact mul_le_cancel _ _ _ le

theorem C_self : ∀ n, C n n = 1
  | 0 => rfl
  | n + 1 => by
    show C n n + C n (n + 1) = 1
    rw [C_self n, show n + 1 = n + 1 + 0 from rfl, C_gt n 0]

theorem succ_ne0 (x : Nat) (h : x + 1 = 0) : False := Nat.noConfusion h

/-- symmetry, by induction on n: a + b = n ⇒ C(n,a) = C(n,b) -/
theorem symmN (n : Nat) : ∀ a b, a + b = n → C n a = C n b := by
  induction n with
  | zero =>
    intro a b h
    cases a with
    | zero =>
      cases b with
      | zero => rfl
      | succ b => exact (succ_ne0 _ h).elim
    | succ a => exact (succ_ne0 (a + b) (by rw [show a + b + 1 = a + 1 + b by lib_nat]; exact h)).elim
  | succ n ih =>
    intro a b h
    cases a with
    | zero =>
      have hb : b = n + 1 := (Nat.zero_add b).symm.trans h
      rw [hb, C_zero_right, C_self]
    | succ a =>
      cases b with
      | zero =>
        have ha : a + 1 = n + 1 := h
        rw [ha, C_zero_right, C_self]
      | succ b =>
        have h' : a + 1 + b = n := Nat.succ.inj h
        have h1 := ih a (b + 1) (by rw [← h']; lib_nat)
        have h2 := ih (a + 1) b h'
        show C n a + C n (a + 1) = C n b + C n (b + 1)
        rw [h1, h2]; lib_nat

/-- symmetry: C(a+b, a) = C(a+b, b) -/
theorem symm (a b : Nat) : C (a + b) a = C (a + b) b := symmN (a + b) a b rfl

/-- window sum Σ_{i<len} h(a+i) -/
def ws (h : Nat → Nat) : Nat → Nat → Nat
  | _, 0 => 0
  | a, len + 1 => h a + ws h (a + 1) len

theorem ws_ends (h : Nat → Nat) : ∀ len a, ws h a (len + 2) = h a + ws h (a + 1) len + h (a + len + 1)
  | 0, a => by show h a + (h (a + 1) + 0) = h a + 0 + h (a + 0 + 1); rw [Nat.add_zero a]; lib_nat
  | len + 1, a => by
    show h a + ws h (a + 1) (len + 2) = h a + (h (a + 1) + ws h (a + 1 + 1) len) + h (a + (len + 1) + 1)
    rw [ws_ends h len (a + 1), show a + 1 + len + 1 = a + (len + 1) + 1 by lib_nat]; lib_nat

theorem ws_congr (f g : Nat → Nat) : ∀ len a, (∀ i, f (a + i) = g (a + i)) → ws f a len = ws g a len
  | 0, _, _ => rfl
  | len + 1, a, h => by
    show f a + ws f (a + 1) len = g a + ws g (a + 1) len
    rw [show f a = g a from h 0, ws_congr f g len (a + 1) (fun i => by rw [show a + 1 + i = a + (i + 1) by lib_nat]; exact h (i + 1))]

theorem ws_add (f g : Nat → Nat) : ∀ len a, ws (fun j => f j + g j) a len = ws f a len + ws g a len
  | 0, _ => rfl
  | len + 1, a => by
    show f a + g a + ws (fun j => f j + g j) (a + 1) len = f a + ws f (a + 1) len + (g a + ws g (a + 1) len)
    rw [ws_add f g len (a + 1)]; lib_nat

theorem ws_snoc (h : Nat → Nat) : ∀ len a, ws h a (len + 1) = ws h a len + h (a + len)
  | 0, a => by show h a + 0 = 0 + h a; lib_nat
  | len + 1, a => by
    show h a + ws h (a + 1) (len + 1) = h a + ws h (a + 1) len + h (a + (len + 1))
    rw [ws_snoc h len (a + 1), show a + 1 + len = a + (len + 1) by lib_nat]; lib_nat

theorem ws_shift (h : Nat → Nat) : ∀ len a, ws h (a + 1) len = ws (fun i => h (i + 1)) a len
  | 0, _ => rfl
  | len + 1, a => by
    show h (a + 1) + ws h (a + 1 + 1) len = h (a + 1) + ws (fun i => h (i + 1)) (a + 1) len
    rw [ws_shift h len (a + 1)]

theorem ws_ext (f g : Nat → Nat) (h : ∀ j, f j = g j) : ∀ len a, ws f a len = ws g a len
  | 0, _ => rfl
  | len + 1, a => by
    show f a + ws f (a + 1) len = g a + ws g (a + 1) len
    rw [h a, ws_ext f g h len (a + 1)]

/-- Vandermonde in window form: Σ_{j ≤ m} C(m,j) C(M, t+j) = C(m+M, m+t) -/
theorem vand (M : Nat) : ∀ m t, ws (fun j => C m j * C M (t + j)) 0 (m + 1) = C (m + M) (m + t)
  | 0, t => by
    show C 0 0 * C M t + 0 = C (0 + M) (0 + t)
    rw [Nat.zero_add M, Nat.zero_add t]; show 1 * C M t + 0 = C M t; lib_nat
  | m + 1, t => by
    have A := vand M m (t + 1)
    have B := vand M m t
    have key : C m 0 * C M t + ws (fun i => C m (i + 1) * C M (t + (i + 1))) 0 (m + 1) = C (m + M) (m + t) := by
      have e1 := ws_snoc (fun j => C m j * C M (t + j)) (m + 1) 0
      have e0 : ws (fun j => C m j * C M (t + j)) 0 (m + 1 + 1)
          = C m 0 * C M (t + 0) + ws (fun j => C m j * C M (t + j)) (0 + 1) (m + 1) := rfl
      rw [e0, ws_shift, B, show 0 + (m + 1) = m + 1 + 0 by lib_nat, C_gt m 0] at e1
      have e2 : C m 0 * C M t + ws (fun i => C m (i + 1) * C M (t + (i + 1))) 0 (m + 1)
          = C (m + M) (m + t) + 0 * C M (t + (m + 1 + 0)) := e1
      rw [e2]; lib_nat
    show C (m + 1) 0 * C M (t + 0) + ws (fun j => C (m + 1) j * C M (t + j)) (0 + 1) (m + 1) = C (m + 1 + M) (m + 1 + t)
    rw [ws_shift, ws_ext (fun i => C (m + 1) (i + 1) * C M (t + (i + 1)))
      (fun i => C m i * C M (t + 1 + i) + C m (i + 1) * C M (t + (i + 1)))
      (fun i => by
        show (C m i + C m (i + 1)) * C M (t + (i + 1)) = C m i * C M (t + 1 + i) + C m (i + 1) * C M (t + (i + 1))
        rw [show t + (i + 1) = t + 1 + i by lib_nat]; lib_nat), ws_add, A]
    have k2 : C (m + 1) 0 * C M (t + 0) + ws (fun i => C m (i + 1) * C M (t + (i + 1))) 0 (m + 1) = C (m + M) (m + t) := by
      rw [C_zero_right]; rw [C_zero_right] at key; exact key
    rw [show C (m + 1) 0 * C M (t + 0) + (C (m + M) (m + (t + 1)) + ws (fun i => C m (i + 1) * C M (t + (i + 1))) 0 (m + 1))
        = C (m + M) (m + (t + 1)) + (C (m + 1) 0 * C M (t + 0) + ws (fun i => C m (i + 1) * C M (t + (i + 1))) 0 (m + 1)) by lib_nat,
      k2, show m + 1 + M = (m + M) + 1 by lib_nat, show m + 1 + t = (m + t) + 1 by lib_nat, pascal,
      show m + (t + 1) = (m + t) + 1 by lib_nat]
    lib_nat

theorem symm' (X Y Z : Nat) (h : Z = X + Y) : C Z X = C Z Y := by rw [h]; exact symm X Y

theorem mul_le_left (c : Nat) {x y : Nat} (h : x ≤ y) : c * x ≤ c * y := by
  have ⟨k, hk⟩ := Nat.le.dest h
  exact LibNatLe.le_of_eq_add (show c * y = c * x + c * k by rw [← hk]; lib_nat)

theorem sq_mono (x : Nat) : x * x ≤ (x + 2) * (x + 2) :=
  LibNatLe.le_of_eq_add (show (x + 2) * (x + 2) = x * x + (4 * x + 4) by lib_nat)

/-- end pair ≤ 0 when (s−r)² ≥ r+s+4 -/
theorem end_neg (r g : Nat) (h : r + (r + g) + 4 ≤ g * g) :
    2 * C (r + 1 + (r + g + 1)) (r + 1) ≤ C (r + 1 + (r + g + 1)) r + C (r + 1 + (r + g + 1)) (r + 1 + 1) := by
  have hp := pair_id r (r + g)
  have ⟨t, ht⟩ := Nat.le.dest h
  let c := C (r + 1 + (r + g + 1)) (r + 1)
  let Q1 := (r + 1) * (r + 2) + (r + g + 1) * (r + g + 2)
  have hQ : 2 * ((r + 2) * (r + g + 2)) + t = Q1 := by
    apply add_cancel _ _ (r + (r + g) + 4)
    rw [show 2 * ((r + 2) * (r + g + 2)) + t + (r + (r + g) + 4) = 2 * ((r + 2) * (r + g + 2)) + (r + (r + g) + 4 + t) by lib_nat, ht]
    show 2 * ((r + 2) * (r + g + 2)) + g * g = (r + 1) * (r + 2) + (r + g + 1) * (r + g + 2) + (r + (r + g) + 4)
    lib_nat
  have le : 2 * C (r + 1 + (r + g + 1)) (r + 1) * ((r + 1) * (r + g + 2) + (r + g + 1) + 1)
      ≤ (C (r + 1 + (r + g + 1)) r + C (r + 1 + (r + g + 1)) (r + 1 + 1)) * ((r + 1) * (r + g + 2) + (r + g + 1) + 1) := by
    rw [show (C (r + 1 + (r + g + 1)) r + C (r + 1 + (r + g + 1)) (r + 1 + 1)) * ((r + 1) * (r + g + 2) + (r + g + 1) + 1)
        = (r + 2) * (r + g + 2) * (C (r + 1 + (r + g + 1)) r + C (r + 1 + (r + g + 1)) (r + 1 + 1)) by lib_nat, hp,
      show 2 * C (r + 1 + (r + g + 1)) (r + 1) * ((r + 1) * (r + g + 2) + (r + g + 1) + 1)
        = C (r + 1 + (r + g + 1)) (r + 1) * (2 * ((r + 2) * (r + g + 2))) by lib_nat,
      show (r + 1) * (r + 2) + (r + g + 1) * (r + g + 2) = 2 * ((r + 2) * (r + g + 2)) + t from hQ.symm]
    exact LibNatLe.le_of_eq_add (show c * (2 * ((r + 2) * (r + g + 2)) + t) = c * (2 * ((r + 2) * (r + g + 2))) + c * t by lib_nat)
  exact mul_le_cancel _ _ _ le

/-- the two Turánian kernels at m: G_j = C(m,j)C(N,ν+j), F_j = C(m,j)C(N,ν+1+j), N = m+2ν+2 -/
def Gw (m ν j : Nat) : Nat := C m j * C (m + 2 * ν + 2) (ν + j)
def Fw (m ν j : Nat) : Nat := C m j * C (m + 2 * ν + 2) (ν + 1 + j)

theorem full (m ν : Nat) : ws (Gw m ν) 0 (m + 1) ≤ ws (Fw m ν) 0 (m + 1) := by
  have hG := vand (m + 2 * ν + 2) m ν
  have hF := vand (m + 2 * ν + 2) m (ν + 1)
  have eG : ws (Gw m ν) 0 (m + 1) = ws (fun j => C m j * C (m + 2 * ν + 2) (ν + j)) 0 (m + 1) := rfl
  have eF : ws (Fw m ν) 0 (m + 1) = ws (fun j => C m j * C (m + 2 * ν + 2) (ν + 1 + j)) 0 (m + 1) := rfl
  rw [eG, eF, hG, hF, show m + (m + 2 * ν + 2) = 2 * (m + ν) + 2 by lib_nat, show m + (ν + 1) = m + ν + 1 by lib_nat]
  exact central (m + ν)

/-- the end pair of the window [a, a+e+2] in m = a + (a+e+2) -/
theorem ends (m ν a e : Nat) (hm : m = a + (a + e + 2)) :
    Gw m ν a + Gw m ν (a + e + 2) = C m a * (C (m + 2 * ν + 2) (ν + a) + C (m + 2 * ν + 2) (ν + a + 2)) ∧
    Fw m ν a + Fw m ν (a + e + 2) = C m a * (2 * C (m + 2 * ν + 2) (ν + a + 1)) := by
  have s1 : C m (a + e + 2) = C m a := (symm' a (a + e + 2) m hm).symm
  have s2 : C (m + 2 * ν + 2) (ν + (a + e + 2)) = C (m + 2 * ν + 2) (ν + a + 2) :=
    symm' _ _ _ (by rw [hm]; lib_nat)
  have s3 : C (m + 2 * ν + 2) (ν + 1 + (a + e + 2)) = C (m + 2 * ν + 2) (ν + a + 1) :=
    symm' _ _ _ (by rw [hm]; lib_nat)
  constructor
  · show C m a * C (m + 2 * ν + 2) (ν + a) + C m (a + e + 2) * C (m + 2 * ν + 2) (ν + (a + e + 2)) = _
    rw [s1, s2]; lib_nat
  · show C m a * C (m + 2 * ν + 2) (ν + 1 + a) + C m (a + e + 2) * C (m + 2 * ν + 2) (ν + 1 + (a + e + 2)) = _
    rw [s1, s3, show ν + 1 + a = ν + a + 1 by lib_nat]; lib_nat

theorem pair_pos (m ν a e : Nat) (hm : m = a + (a + e + 2)) (h : (e + 2) * (e + 2) ≤ m + 2 * ν + 4) :
    Gw m ν a + Gw m ν (a + e + 2) ≤ Fw m ν a + Fw m ν (a + e + 2) := by
  rw [(ends m ν a e hm).1, (ends m ν a e hm).2]
  apply mul_le_left
  have := end_pos (ν + a) (e + 2) (by rw [hm] at h; rw [show ν + a + (ν + a + (e + 2)) + 4 = a + (a + e + 2) + 2 * ν + 4 by lib_nat]; exact h)
  rw [show ν + a + 1 + (ν + a + (e + 2) + 1) = m + 2 * ν + 2 by rw [hm]; lib_nat, show ν + a + 1 + 1 = ν + a + 2 by lib_nat] at this
  exact this

theorem pair_neg (m ν a e : Nat) (hm : m = a + (a + e + 2)) (h : m + 2 * ν + 4 ≤ (e + 2) * (e + 2)) :
    Fw m ν a + Fw m ν (a + e + 2) ≤ Gw m ν a + Gw m ν (a + e + 2) := by
  rw [(ends m ν a e hm).1, (ends m ν a e hm).2]
  apply mul_le_left
  have := end_neg (ν + a) (e + 2) (by rw [hm] at h; rw [show ν + a + (ν + a + (e + 2)) + 4 = a + (a + e + 2) + 2 * ν + 4 by lib_nat]; exact h)
  rw [show ν + a + 1 + (ν + a + (e + 2) + 1) = m + 2 * ν + 2 by rw [hm]; lib_nat, show ν + a + 1 + 1 = ν + a + 2 by lib_nat] at this
  exact this

theorem center0 (m ν a : Nat) (hm : m = a + a) : ws (Gw m ν) a 1 ≤ ws (Fw m ν) a 1 := by
  show C m a * C (m + 2 * ν + 2) (ν + a) + 0 ≤ C m a * C (m + 2 * ν + 2) (ν + 1 + a) + 0
  rw [Nat.add_zero, Nat.add_zero]
  apply mul_le_left
  have := central (ν + a)
  rw [show 2 * (ν + a) + 2 = m + 2 * ν + 2 by rw [hm]; lib_nat, show ν + a + 1 = ν + 1 + a by lib_nat] at this
  exact this

theorem center1 (m ν a : Nat) (hm : m = a + (a + 1)) : ws (Gw m ν) a 2 ≤ ws (Fw m ν) a 2 := by
  have s1 : C m (a + 1) = C m a := (symm' a (a + 1) m hm).symm
  have s2 : C (m + 2 * ν + 2) (ν + 1 + (a + 1)) = C (m + 2 * ν + 2) (ν + 1 + a) := symm' _ _ _ (by rw [hm]; lib_nat)
  show C m a * C (m + 2 * ν + 2) (ν + a) + (C m (a + 1) * C (m + 2 * ν + 2) (ν + (a + 1)) + 0)
    ≤ C m a * C (m + 2 * ν + 2) (ν + 1 + a) + (C m (a + 1) * C (m + 2 * ν + 2) (ν + 1 + (a + 1)) + 0)
  rw [s1, s2, show ν + (a + 1) = ν + 1 + a by lib_nat]
  have mh := mono_half (ν + a) 2
  rw [show ν + a + (ν + a + 1 + 2) = m + 2 * ν + 2 by rw [hm]; lib_nat, show ν + a + 1 = ν + 1 + a by lib_nat] at mh
  have ⟨k, hk⟩ := Nat.le.dest (mul_le_left (C m a) mh)
  exact LibNatLe.le_of_eq_add (show C m a * C (m + 2 * ν + 2) (ν + 1 + a) + (C m a * C (m + 2 * ν + 2) (ν + 1 + a) + 0)
    = C m a * C (m + 2 * ν + 2) (ν + a) + (C m a * C (m + 2 * ν + 2) (ν + 1 + a) + 0) + k by rw [← hk]; lib_nat)

theorem add_le_add' {a b c d : Nat} (h1 : a ≤ b) (h2 : c ≤ d) : a + c ≤ b + d := by
  have ⟨k1, e1⟩ := Nat.le.dest h1
  have ⟨k2, e2⟩ := Nat.le.dest h2
  exact LibNatLe.le_of_eq_add (show b + d = a + c + (k1 + k2) by rw [← e1, ← e2]; lib_nat)

theorem parity : ∀ e, ∃ k, e = 0 + 2 * k ∨ e = 1 + 2 * k
  | 0 => ⟨0, Or.inl rfl⟩
  | e + 1 => by
    have ⟨k, hk⟩ := parity e
    cases hk with
    | inl h => exact ⟨k, Or.inr (by rw [h]; lib_nat)⟩
    | inr h => exact ⟨k + 1, Or.inl (by rw [h]; lib_nat)⟩

/-- windows inside the positive region, built from the centre outwards -/
theorem chainA (m ν e0 : Nat) (he0 : e0 = 0 ∨ e0 = 1) : ∀ k a, a + (a + (e0 + 2 * k)) = m →
    (e0 + 2 * k) * (e0 + 2 * k) ≤ m + 2 * ν + 4 → ws (Gw m ν) a (e0 + 2 * k + 1) ≤ ws (Fw m ν) a (e0 + 2 * k + 1)
  | 0, a, hm, _ => by
    cases he0 with
    | inl h0 =>
      subst h0
      exact center0 m ν a (by rw [← hm]; try lib_nat)
    | inr h1 =>
      subst h1
      exact center1 m ν a (by rw [← hm]; try lib_nat)
  | k + 1, a, hm, hc => by
    have ih := chainA m ν e0 he0 k (a + 1) (by rw [← hm]; lib_nat)
      (Nat.le_trans (sq_mono (e0 + 2 * k)) (by rw [show e0 + 2 * k + 2 = e0 + 2 * (k + 1) by lib_nat]; exact hc))
    have hp := pair_pos m ν a (e0 + 2 * k) (by rw [← hm]; lib_nat)
      (by rw [show e0 + 2 * k + 2 = e0 + 2 * (k + 1) by lib_nat]; exact hc)
    rw [show e0 + 2 * (k + 1) + 1 = (e0 + 2 * k + 1) + 2 by lib_nat, ws_ends, ws_ends,
      show a + (e0 + 2 * k + 1) + 1 = a + (e0 + 2 * k) + 2 by lib_nat]
    have := add_le_add' hp ih
    exact (show Gw m ν a + ws (Gw m ν) (a + 1) (e0 + 2 * k + 1) + Gw m ν (a + (e0 + 2 * k) + 2)
        = Gw m ν a + Gw m ν (a + (e0 + 2 * k) + 2) + ws (Gw m ν) (a + 1) (e0 + 2 * k + 1) by lib_nat) ▸
      (show Fw m ν a + ws (Fw m ν) (a + 1) (e0 + 2 * k + 1) + Fw m ν (a + (e0 + 2 * k) + 2)
        = Fw m ν a + Fw m ν (a + (e0 + 2 * k) + 2) + ws (Fw m ν) (a + 1) (e0 + 2 * k + 1) by lib_nat) ▸ this

/-- windows in the negative region, peeled from the full window inwards -/
theorem chainB (m ν : Nat) : ∀ k e, k + (k + e) = m → m + 2 * ν + 4 ≤ (e + 2) * (e + 2) →
    ws (Gw m ν) k (e + 1) ≤ ws (Fw m ν) k (e + 1)
  | 0, e, hm, _ => by
    rw [show e = m by rw [← hm]; lib_nat]; exact full m ν
  | k + 1, e, hm, hc => by
    have ih := chainB m ν k (e + 2) (by rw [← hm]; lib_nat) (Nat.le_trans hc (sq_mono (e + 2)))
    have hn := pair_neg m ν k e (by rw [← hm]; lib_nat) hc
    rw [show e + 2 + 1 = (e + 1) + 2 by lib_nat, ws_ends, ws_ends, show k + (e + 1) + 1 = k + e + 2 by lib_nat] at ih
    have h2 : Gw m ν k + Gw m ν (k + e + 2) + ws (Gw m ν) (k + 1) (e + 1)
        ≤ Gw m ν k + Gw m ν (k + e + 2) + ws (Fw m ν) (k + 1) (e + 1) := by
      have h3 : Gw m ν k + Gw m ν (k + e + 2) + ws (Gw m ν) (k + 1) (e + 1)
          ≤ Fw m ν k + Fw m ν (k + e + 2) + ws (Fw m ν) (k + 1) (e + 1) :=
        (show Gw m ν k + ws (Gw m ν) (k + 1) (e + 1) + Gw m ν (k + e + 2)
          = Gw m ν k + Gw m ν (k + e + 2) + ws (Gw m ν) (k + 1) (e + 1) by lib_nat) ▸
        (show Fw m ν k + ws (Fw m ν) (k + 1) (e + 1) + Fw m ν (k + e + 2)
          = Fw m ν k + Fw m ν (k + e + 2) + ws (Fw m ν) (k + 1) (e + 1) by lib_nat) ▸ ih
      exact Nat.le_trans h3 (add_le_add' hn (Nat.le_refl _))
    have h4 := add_le_cancel (ws (Gw m ν) (k + 1) (e + 1)) (ws (Fw m ν) (k + 1) (e + 1)) (Gw m ν k + Gw m ν (k + e + 2))
      (by rw [Nat.add_comm (ws (Gw m ν) (k + 1) (e + 1)), Nat.add_comm (ws (Fw m ν) (k + 1) (e + 1))]; exact h2)
    exact h4

/-- **Turán on every symmetric window**: Σ_{j=a}^{m−a} C(m,j)C(N,ν+j) ≤ Σ_{j=a}^{m−a} C(m,j)C(N,ν+1+j), N = m+2ν+2 -/
theorem win (m ν a e : Nat) (hm : a + (a + e) = m) : ws (Gw m ν) a (e + 1) ≤ ws (Fw m ν) a (e + 1) := by
  cases Nat.le_total ((e + 2) * (e + 2)) (m + 2 * ν + 4) with
  | inl h =>
    have ⟨k, hk⟩ := parity e
    cases hk with
    | inl h0 =>
      rw [h0] at hm h ⊢
      exact chainA m ν 0 (Or.inl rfl) k a hm (Nat.le_trans (sq_mono _) h)
    | inr h1 =>
      rw [h1] at hm h ⊢
      exact chainA m ν 1 (Or.inr rfl) k a hm (Nat.le_trans (sq_mono _) h)
  | inr h => exact chainB m ν a e hm h

end TuranWin

#print axioms TuranWin.up
#print axioms TuranWin.down
#print axioms TuranWin.mono_half
#print axioms TuranWin.pair_id
#print axioms TuranWin.end_pos
#print axioms TuranWin.end_neg
#print axioms TuranWin.C_self
#print axioms TuranWin.symmN
#print axioms TuranWin.symm
#print axioms TuranWin.ws_ends
#print axioms TuranWin.vand
#print axioms TuranWin.full
#print axioms TuranWin.ends
#print axioms TuranWin.pair_pos
#print axioms TuranWin.pair_neg
#print axioms TuranWin.center0
#print axioms TuranWin.center1
#print axioms TuranWin.parity
#print axioms TuranWin.chainA
#print axioms TuranWin.chainB
#print axioms TuranWin.win
#print axioms TuranWin.succ_ne0
#print axioms TuranWin.ws_congr
#print axioms TuranWin.ws_add
#print axioms TuranWin.ws_snoc
#print axioms TuranWin.ws_shift
#print axioms TuranWin.ws_ext
#print axioms TuranWin.symm'
#print axioms TuranWin.mul_le_left
#print axioms TuranWin.sq_mono
#print axioms TuranWin.add_le_add'

#print axioms: 31 theorems, no axioms.

90.94 The truncated Wilson weight has log-concave \(a_n\)

For every \(t\ge 0\), every \(n\) and every truncation \(K\) the Wilson character coefficients satisfy the Turán inequality.

\[ (n+2)^2\,c_n c_{n+2}\le (n+1)(n+3)\,c_{n+1}^2,\qquad c_m=\textstyle \sum _{k<K}\frac {(m+1)\,t^{m+2k}}{k!\,(m+k+1)!} \]

ym/WilsonTuranF.lean

/-
  ym/WilsonTuranF — T45, G51: the truncated SU(2) Wilson weight has log-concave a_n = c_n/(n+1), for every t ≥ 0 and
  every truncation Kk (field part). With α(m,k) = 1/(k!(m+k+1)!) and A_m = Σ_{k<Kk} α(m,k) t^{m+2k} (so that
  SU2CharExp.cS t m Kk = (m+1) A_m, WilsonBallot), the Cauchy-product coefficients of A_{n+1}² and A_n A_{n+2} in y = t²,
  multiplied by T!(T+2ν+2)! (ν = n+1), are the truncated Turán kernels of WilsonTuran; anti_turan compares them.
  Lean 4 core only, no axioms.
-/
import ym.WilsonTuran
import ym.WilsonBallot
import ym.LC0
import ym.GridQ
import ym.SU2Wilson2D
import ym.SecondRing

namespace WilsonTuranF

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R)

/-- ch is LibBinom.C -/
theorem ch_C : ∀ n k, ch n k = LibBinom.C n k
  | 0, 0 => rfl
  | 0, _ + 1 => rfl
  | n + 1, 0 => rfl
  | n + 1, k + 1 => by
    show ch n k + ch n (k + 1) = LibBinom.C n k + LibBinom.C n (k + 1)
    rw [ch_C n k, ch_C n (k + 1)]

/-- Nat anti-diagonal sum cast -/
theorem cast_anti : ∀ T (g : Nat → Nat → Nat), cN K (WilsonTuran.antiN g T) = anti K (fun j k => cN K (g j k)) T
  | 0, g => rfl
  | T + 1, g => by
    show cN K (g 0 (T + 1) + WilsonTuran.antiN (fun j k => g (j + 1) k) T) = K.add (cN K (g 0 (T + 1))) (anti K (fun j k => cN K (g (j + 1) k)) T)
    rw [cN_add, cast_anti T]

theorem anti_smul (c : R) : ∀ T (f : Nat → Nat → R), K.mul c (anti K f T) = anti K (fun j k => K.mul c (f j k)) T
  | 0, f => rfl
  | T + 1, f => by
    show K.mul c (K.add (f 0 (T + 1)) (anti K (fun j k => f (j + 1) k) T)) = K.add (K.mul c (f 0 (T + 1))) (anti K (fun j k => K.mul c (f (j + 1) k)) T)
    rw [show anti K (fun j k => K.mul c (f (j + 1) k)) T = K.mul c (anti K (fun j k => f (j + 1) k) T) from
      (anti_smul c T (fun j k => f (j + 1) k)).symm]; lib_ring

/-- anti-diagonal sums only see the diagonal -/
theorem anti_diag : ∀ T (f g : Nat → Nat → R), (∀ j k, j + k = T → f j k = g j k) → anti K f T = anti K g T
  | 0, f, g, h => h 0 0 rfl
  | T + 1, f, g, h => by
    show K.add (f 0 (T + 1)) (anti K (fun j k => f (j + 1) k) T) = K.add (g 0 (T + 1)) (anti K (fun j k => g (j + 1) k) T)
    rw [h 0 (T + 1) (by lib_nat), anti_diag T _ _ (fun j k hjk => h (j + 1) k (by rw [← hjk]; lib_nat))]

theorem anti_zero_diag : ∀ T (f : Nat → Nat → R), (∀ j k, j + k = T → f j k = K.zero) → anti K f T = K.zero := by
  intro T f h
  rw [anti_diag K T f (fun _ _ => K.zero) h]
  exact anti_zero K _ (fun _ _ => rfl) T

/-- a list whose coefficients vanish from N on evaluates as Σ_{T<N} co T · y^T -/
theorem ev_co (y : R) : ∀ (l : List R) (N : Nat), (∀ T, N ≤ T → co K l T = K.zero) →
    ev K l y = sumN K (fun T => K.mul (co K l T) (pwR K y T)) N
  | [], N, _ => by
    show K.zero = _
    induction N with
    | zero => rfl
    | succ N ih =>
      show K.zero = K.add (sumN K (fun T => K.mul (co K [] T) (pwR K y T)) N) (K.mul K.zero (pwR K y N))
      rw [← ih (fun _ _ => rfl)]; lib_ring
  | c :: p, 0, h => by
    have hc : c = K.zero := h 0 (Nat.le_refl 0)
    have hp := ev_co y p 0 (fun T _ => h (T + 1) (Nat.zero_le _))
    show K.add c (K.mul y (ev K p y)) = K.zero
    rw [hc, hp]; show K.add K.zero (K.mul y K.zero) = K.zero; lib_ring
  | c :: p, N + 1, h => by
    have hp := ev_co y p N (fun T hT => h (T + 1) (Nat.succ_le_succ hT))
    show K.add c (K.mul y (ev K p y)) = _
    rw [sumN_shift, hp, ← sumN_mul]
    show K.add c _ = K.add (K.mul c K.one) _
    rw [show K.mul c K.one = c by lib_ring]
    congr 1
    exact sumN_ext K _ _ (fun T => by show K.mul y (K.mul (co K p T) (pwR K y T)) = K.mul (co K p T) (K.mul (pwR K y T) y); lib_ring) N

variable (O : ORing K) (F : OField.OF K O)

/-- α(m,k) = 1/(k!(m+k+1)!) -/
def α (m k : Nat) : R := F.inv (cN K (fact k * fact (m + k + 1)))

def pL (m Kk : Nat) : List R := ListRow.listF (α K O F m) 0 Kk

theorem co_pL_in (m Kk j : Nat) (hj : j < Kk) : co K (pL K O F m Kk) j = α K O F m j := by
  unfold pL; rw [ListRow.co_listF K _ Kk 0 j hj, Nat.zero_add]

theorem co_pL_out (m Kk j : Nat) (hj : Kk ≤ j) : co K (pL K O F m Kk) j = K.zero :=
  HeatLR.co_out K _ j (by unfold pL; rw [OneStep.len_listF]; exact hj)

theorem fpos (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (a b : Nat) :
    O.le K.zero (cN K (fact a * fact b)) ∧ cN K (fact a * fact b) ≠ K.zero :=
  pos_of_ge_one K O h1 h10 _ (Nat.mul_le_mul (fact_pos a) (fact_pos b))

/-- the coefficient identity: X·Y·A·B = T!·N! with A, B the two denominators, cast and divided -/
theorem coef (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X Y a1 a2 b1 b2 P : Nat)
    (h : P = (X * Y) * ((fact a1 * fact a2) * (fact b1 * fact b2))) :
    K.mul (cN K P) (K.mul (F.inv (cN K (fact a1 * fact a2))) (F.inv (cN K (fact b1 * fact b2)))) = cN K (X * Y) := by
  have hA := fpos K O h1 h10 a1 a2
  have hB := fpos K O h1 h10 b1 b2
  rw [h, cN_mul K (X * Y), cN_mul K (fact a1 * fact a2), show K.mul (K.mul (cN K (X * Y)) (K.mul (cN K (fact a1 * fact a2)) (cN K (fact b1 * fact b2))))
      (K.mul (F.inv (cN K (fact a1 * fact a2))) (F.inv (cN K (fact b1 * fact b2))))
      = K.mul (cN K (X * Y)) (K.mul (K.mul (cN K (fact a1 * fact a2)) (F.inv (cN K (fact a1 * fact a2))))
          (K.mul (cN K (fact b1 * fact b2)) (F.inv (cN K (fact b1 * fact b2))))) by lib_ring,
    F.mul_inv _ hA.2, F.mul_inv _ hB.2]
  lib_ring

/-- Nat: T!·N! = C(T,j)·C(N,ν+j)·(j!(ν+j)!)·(k!(ν+k+2)!) with T = j+k, N = T+2ν+2 (G kernel, ν = n+1) -/
theorem natG (n j k : Nat) :
    fact (j + k) * fact (j + k + 2 * (n + 1) + 2)
      = (LibBinom.C (j + k) j * LibBinom.C (j + k + 2 * (n + 1) + 2) (n + 1 + j))
        * ((fact j * fact (n + j + 1)) * (fact k * fact (n + 2 + k + 1))) := by
  have e1 := ch_fact (k + j) k j rfl
  have e2 := ch_fact ((n + 2 + k + 1) + (n + 1 + j)) (n + 2 + k + 1) (n + 1 + j) rfl
  rw [ch_C] at e1 e2
  rw [show j + k = k + j by lib_nat, show k + j + 2 * (n + 1) + 2 = (n + 2 + k + 1) + (n + 1 + j) by lib_nat,
    ← e1, ← e2, show n + j + 1 = n + 1 + j by lib_nat]
  lib_nat

/-- Nat, F kernel: T!·N! = C(T,j)·C(N,ν+1+j)·(j!(ν+1+j)!)·(k!(ν+1+k)!) -/
theorem natF (n j k : Nat) :
    fact (j + k) * fact (j + k + 2 * (n + 1) + 2)
      = (LibBinom.C (j + k) j * LibBinom.C (j + k + 2 * (n + 1) + 2) (n + 1 + 1 + j))
        * ((fact j * fact (n + 1 + j + 1)) * (fact k * fact (n + 1 + k + 1))) := by
  have e1 := ch_fact (k + j) k j rfl
  have e2 := ch_fact ((n + 1 + k + 1) + (n + 1 + 1 + j)) (n + 1 + k + 1) (n + 1 + 1 + j) rfl
  rw [ch_C] at e1 e2
  rw [show j + k = k + j by lib_nat, show k + j + 2 * (n + 1) + 2 = (n + 1 + k + 1) + (n + 1 + 1 + j) by lib_nat,
    ← e1, ← e2, show n + 1 + j + 1 = n + 1 + 1 + j by lib_nat]
  lib_nat

/-- the Cauchy-product coefficient of A_n·A_{n+2}, times T!N!, is the truncated G kernel sum -/
theorem coG (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n Kk T : Nat) :
    K.mul (cN K (fact T * fact (T + 2 * (n + 1) + 2))) (co K (mulL K (pL K O F n Kk) (pL K O F (n + 2) Kk)) T)
      = cN K (WilsonTuran.antiN (WilsonTuran.gG Kk (n + 1)) T) := by
  rw [co_mulL, anti_smul, cast_anti]
  apply anti_diag; intro j k hjk
  cases Nat.lt_or_ge j Kk with
  | inr hj => rw [co_pL_out K O F n Kk j hj, WilsonTuran.gG_out1 Kk (n + 1) j k hj]; show _ = K.zero; lib_ring
  | inl hj =>
    cases Nat.lt_or_ge k Kk with
    | inr hk => rw [co_pL_out K O F (n + 2) Kk k hk, WilsonTuran.gG_out2 Kk (n + 1) j k hk]; show _ = K.zero; lib_ring
    | inl hk =>
      rw [co_pL_in K O F n Kk j hj, co_pL_in K O F (n + 2) Kk k hk, WilsonTuran.gG_in Kk (n + 1) j k hj hk, ← hjk]
      exact coef K O F h1 h10 _ _ j (n + j + 1) k (n + 2 + k + 1) _ (natG n j k)

theorem coF (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n Kk T : Nat) :
    K.mul (cN K (fact T * fact (T + 2 * (n + 1) + 2))) (co K (mulL K (pL K O F (n + 1) Kk) (pL K O F (n + 1) Kk)) T)
      = cN K (WilsonTuran.antiN (WilsonTuran.gF Kk (n + 1)) T) := by
  rw [co_mulL, anti_smul, cast_anti]
  apply anti_diag; intro j k hjk
  cases Nat.lt_or_ge j Kk with
  | inr hj => rw [co_pL_out K O F (n + 1) Kk j hj, WilsonTuran.gF_out1 Kk (n + 1) j k hj]; show _ = K.zero; lib_ring
  | inl hj =>
    cases Nat.lt_or_ge k Kk with
    | inr hk => rw [co_pL_out K O F (n + 1) Kk k hk, WilsonTuran.gF_out2 Kk (n + 1) j k hk]; show _ = K.zero; lib_ring
    | inl hk =>
      rw [co_pL_in K O F (n + 1) Kk j hj, co_pL_in K O F (n + 1) Kk k hk, WilsonTuran.gF_in Kk (n + 1) j k hj hk, ← hjk]
      exact coef K O F h1 h10 _ _ j (n + 1 + j + 1) k (n + 1 + k + 1) _ (natF n j k)

/-- **coefficientwise Turán for the truncated Wilson series** -/
theorem co_cmp (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n Kk T : Nat) :
    O.le (co K (mulL K (pL K O F n Kk) (pL K O F (n + 2) Kk)) T) (co K (mulL K (pL K O F (n + 1) Kk) (pL K O F (n + 1) Kk)) T) := by
  have hc := fpos K O h1 h10 T (T + 2 * (n + 1) + 2)
  have le := cN_mono K O h1 _ _ (WilsonTuran.anti_turan Kk (n + 1) T)
  rw [← coG K O F h1 h10 n Kk T, ← coF K O F h1 h10 n Kk T] at le
  exact GridQ.cancel K O F _ _ _ hc.1 hc.2 le

/-- the product coefficients vanish from 2Kk on -/
theorem co_hi (m1 m2 Kk : Nat) : ∀ T, 2 * Kk ≤ T → co K (mulL K (pL K O F m1 Kk) (pL K O F m2 Kk)) T = K.zero := by
  intro T hT
  rw [co_mulL]
  apply anti_zero_diag; intro j k hjk
  cases Nat.lt_or_ge j Kk with
  | inr hj => rw [co_pL_out K O F m1 Kk j hj]; lib_ring
  | inl hj =>
    cases Nat.lt_or_ge k Kk with
    | inr hk => rw [co_pL_out K O F m2 Kk k hk]; lib_ring
    | inl hk =>
      have a1 : j + 1 + (k + 1) ≤ Kk + Kk := TuranWin.add_le_add' hj hk
      have a2 : j + k + 1 + 1 ≤ j + k := by
        rw [show j + 1 + (k + 1) = j + k + 1 + 1 by lib_nat, show Kk + Kk = 2 * Kk by lib_nat] at a1
        exact Nat.le_trans a1 (by rw [hjk]; exact hT)
      exact absurd (Nat.le_trans (Nat.le_succ _) a2) (Nat.not_succ_le_self _)

/-- for y ≥ 0: (Σ α(n,·) y^·)(Σ α(n+2,·) y^·) ≤ (Σ α(n+1,·) y^·)² -/
theorem ev_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n Kk : Nat) (y : R) (hy : O.le K.zero y) :
    O.le (K.mul (ev K (pL K O F n Kk) y) (ev K (pL K O F (n + 2) Kk) y))
      (K.mul (ev K (pL K O F (n + 1) Kk) y) (ev K (pL K O F (n + 1) Kk) y)) := by
  rw [← SU2Wilson2D.ev_mulL, ← SU2Wilson2D.ev_mulL,
    ev_co K y _ (2 * Kk) (co_hi K O F n (n + 2) Kk), ev_co K y _ (2 * Kk) (co_hi K O F (n + 1) (n + 1) Kk)]
  apply Lagrange.sum_le_lt K O
  intro T _
  exact O.mul_le_mul_right _ _ _ (co_cmp K O F h1 h10 n Kk T) (AreaLower.pw_nn K O y hy h1 T)

/-- A_m = t^m Σ_k α(m,k) (t²)^k -/
def A (t : R) (m Kk : Nat) : R := K.mul (pwR K t m) (ev K (pL K O F m Kk) (K.mul t t))

theorem A_turan (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n Kk : Nat) :
    O.le (K.mul (A K O F t n Kk) (A K O F t (n + 2) Kk)) (K.mul (A K O F t (n + 1) Kk) (A K O F t (n + 1) Kk)) := by
  have hp : K.mul (pwR K t n) (pwR K t (n + 2)) = K.mul (pwR K t (n + 1)) (pwR K t (n + 1)) := by
    rw [← GKS.pwR_add, ← GKS.pwR_add, show n + (n + 2) = n + 1 + (n + 1) by lib_nat]
  have le := ev_le K O F h1 h10 n Kk (K.mul t t) (O.mul_nonneg _ _ ht ht)
  have pp := O.mul_nonneg _ _ (AreaLower.pw_nn K O t ht h1 n) (AreaLower.pw_nn K O t ht h1 (n + 2))
  have m := O.mul_le_mul_right _ _ _ le pp
  unfold A
  rw [show K.mul (K.mul (pwR K t n) (ev K (pL K O F n Kk) (K.mul t t))) (K.mul (pwR K t (n + 2)) (ev K (pL K O F (n + 2) Kk) (K.mul t t)))
      = K.mul (K.mul (ev K (pL K O F n Kk) (K.mul t t)) (ev K (pL K O F (n + 2) Kk) (K.mul t t))) (K.mul (pwR K t n) (pwR K t (n + 2))) by lib_ring,
    show K.mul (K.mul (pwR K t (n + 1)) (ev K (pL K O F (n + 1) Kk) (K.mul t t))) (K.mul (pwR K t (n + 1)) (ev K (pL K O F (n + 1) Kk) (K.mul t t)))
      = K.mul (K.mul (ev K (pL K O F (n + 1) Kk) (K.mul t t)) (ev K (pL K O F (n + 1) Kk) (K.mul t t))) (K.mul (pwR K t n) (pwR K t (n + 2))) by rw [hp]; lib_ring]
  exact m

/-- the k-th Wilson term is (m+1)·t^m·α(m,k)·(t²)^k (ballot) -/
theorem term_eq (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (m k : Nat) :
    term K O F t m k = K.mul (cN K (m + 1)) (K.mul (pwR K t m) (K.mul (α K O F m k) (pwR K (K.mul t t) k))) := by
  have hb := WilsonBallot.ballot m k
  have hA := fpos K O h1 h10 k (m + k + 1)
  have hF := pos_of_ge_one K O h1 h10 _ (fact_pos (m + 2 * k))
  have e1 : K.mul (cN K (SU2Haar.P (m + 2 * k) 0 m)) (cN K (fact k * fact (m + k + 1)))
      = K.mul (cN K (m + 1)) (cN K (fact (m + 2 * k))) := by
    rw [← cN_mul, ← cN_mul, hb]
  have eP : cN K (SU2Haar.P (m + 2 * k) 0 m) = K.mul (K.mul (cN K (m + 1)) (cN K (fact (m + 2 * k)))) (α K O F m k) := by
    unfold α
    rw [← e1, show K.mul (K.mul (cN K (SU2Haar.P (m + 2 * k) 0 m)) (cN K (fact k * fact (m + k + 1)))) (F.inv (cN K (fact k * fact (m + k + 1))))
        = K.mul (cN K (SU2Haar.P (m + 2 * k) 0 m)) (K.mul (cN K (fact k * fact (m + k + 1))) (F.inv (cN K (fact k * fact (m + k + 1))))) by lib_ring,
      F.mul_inv _ hA.2]
    lib_ring
  have hpw : pwR K t (m + 2 * k) = K.mul (pwR K t m) (pwR K (K.mul t t) k) := by
    rw [GKS.pwR_add, GKS2.pwR_mul, show 2 * k = k + k by lib_nat, GKS.pwR_add]
  show K.mul (K.mul (pwR K t (m + 2 * k)) (F.inv (cN K (fact (m + 2 * k))))) (cN K (SU2Haar.P (m + 2 * k) 0 m)) = _
  rw [eP, hpw, show K.mul (K.mul (K.mul (pwR K t m) (pwR K (K.mul t t) k)) (F.inv (cN K (fact (m + 2 * k)))))
      (K.mul (K.mul (cN K (m + 1)) (cN K (fact (m + 2 * k)))) (α K O F m k))
      = K.mul (K.mul (cN K (fact (m + 2 * k))) (F.inv (cN K (fact (m + 2 * k)))))
          (K.mul (cN K (m + 1)) (K.mul (pwR K t m) (K.mul (α K O F m k) (pwR K (K.mul t t) k)))) by lib_ring,
    F.mul_inv _ hF.2]
  lib_ring

theorem cS_sum (t : R) (m : Nat) : ∀ Kk, cS K O F t m Kk = sumN K (fun k => term K O F t m k) Kk
  | 0 => rfl
  | Kk + 1 => by
    show K.add (cS K O F t m Kk) (term K O F t m Kk) = K.add (sumN K (fun k => term K O F t m k) Kk) (term K O F t m Kk)
    rw [cS_sum t m Kk]

theorem ev_pL (m Kk : Nat) (y : R) : ev K (pL K O F m Kk) y = sumN K (fun k => K.mul (α K O F m k) (pwR K y k)) Kk := by
  rw [ev_co K y _ Kk (fun T hT => co_pL_out K O F m Kk T hT)]
  exact SecondRing.sumN_ext_lt K _ _ Kk (fun k hk => by rw [co_pL_in K O F m Kk k hk])

/-- c_m = (m+1)·A_m -/
theorem cS_eq (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (m Kk : Nat) :
    cS K O F t m Kk = K.mul (cN K (m + 1)) (A K O F t m Kk) := by
  unfold A
  rw [cS_sum, ev_pL, ← sumN_mul, ← sumN_mul]
  exact sumN_ext K _ _ (fun k => term_eq K O F h1 h10 t m k) Kk

/-- **The truncated Wilson weight has log-concave a_n = c_n/(n+1)**: for t ≥ 0, every n and every truncation Kk,
    (n+2)² c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}²,  c_m = SU2CharExp.cS t m Kk. -/
theorem wilson_lc (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht : O.le K.zero t) (n Kk : Nat) :
    O.le (K.mul (cN K ((n + 2) * (n + 2))) (K.mul (cS K O F t n Kk) (cS K O F t (n + 2) Kk)))
      (K.mul (cN K ((n + 1) * (n + 3))) (K.mul (cS K O F t (n + 1) Kk) (cS K O F t (n + 1) Kk))) := by
  rw [cS_eq K O F h1 h10 t n Kk, cS_eq K O F h1 h10 t (n + 2) Kk, cS_eq K O F h1 h10 t (n + 1) Kk,
    show n + 2 + 1 = n + 3 by lib_nat, show n + 1 + 1 = n + 2 by lib_nat, cN_mul, cN_mul]
  have le := A_turan K O F h1 h10 t ht n Kk
  have M0 : O.le K.zero (K.mul (K.mul (cN K (n + 1)) (cN K (n + 3))) (K.mul (cN K (n + 2)) (cN K (n + 2)))) :=
    O.mul_nonneg _ _ (O.mul_nonneg _ _ (cN_nn K O h1 _) (cN_nn K O h1 _)) (O.mul_nonneg _ _ (cN_nn K O h1 _) (cN_nn K O h1 _))
  have m := O.mul_le_mul_right _ _ _ le M0
  rw [show K.mul (K.mul (cN K (n + 2)) (cN K (n + 2))) (K.mul (K.mul (cN K (n + 1)) (A K O F t n Kk)) (K.mul (cN K (n + 3)) (A K O F t (n + 2) Kk)))
      = K.mul (K.mul (A K O F t n Kk) (A K O F t (n + 2) Kk)) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 3))) (K.mul (cN K (n + 2)) (cN K (n + 2)))) by lib_ring,
    show K.mul (K.mul (cN K (n + 1)) (cN K (n + 3))) (K.mul (K.mul (cN K (n + 2)) (A K O F t (n + 1) Kk)) (K.mul (cN K (n + 2)) (A K O F t (n + 1) Kk)))
      = K.mul (K.mul (A K O F t (n + 1) Kk) (A K O F t (n + 1) Kk)) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 3))) (K.mul (cN K (n + 2)) (cN K (n + 2)))) by lib_ring]
  exact m

end WilsonTuranF

#print axioms WilsonTuranF.ch_C
#print axioms WilsonTuranF.cast_anti
#print axioms WilsonTuranF.anti_smul
#print axioms WilsonTuranF.anti_diag
#print axioms WilsonTuranF.anti_zero_diag
#print axioms WilsonTuranF.ev_co
#print axioms WilsonTuranF.coef
#print axioms WilsonTuranF.natG
#print axioms WilsonTuranF.natF
#print axioms WilsonTuranF.coG
#print axioms WilsonTuranF.coF
#print axioms WilsonTuranF.co_cmp
#print axioms WilsonTuranF.co_hi
#print axioms WilsonTuranF.ev_le
#print axioms WilsonTuranF.A_turan
#print axioms WilsonTuranF.term_eq
#print axioms WilsonTuranF.cS_sum
#print axioms WilsonTuranF.ev_pL
#print axioms WilsonTuranF.cS_eq
#print axioms WilsonTuranF.wilson_lc
#print axioms WilsonTuranF.co_pL_in
#print axioms WilsonTuranF.co_pL_out
#print axioms WilsonTuranF.fpos

#print axioms: 23 theorems, no axioms.

90.95 Bond log-concavity at \(n=3\)

The bond-moving product of any v-monotone list has \(a_4^2\ge a_3a_5\).

\[ 75\,c_3c_5\le 72\,c_4^2,\qquad c_m=\textstyle \sum _{j,k}x_jx_k\,[\text {CG}(j,k,m)] \]

ym/GenA3.lean

ym/GenA3.lean (1.0 MiB, 9,150 lines, on its own page)

#print axioms: 2 theorems, no axioms.

90.96 Bond log-concavity for \(n\le 3\)

One statement collecting the cases \(n=0,1,2,3\).

\[ (n+2)^2\,c_nc_{n+2}\le (n+1)(n+3)\,c_{n+1}^2\quad (n\le 3) \]

ym/BondLC3.lean

/-
  ym/BondLC3 — T45, G54: the bond-moving product of a v-monotone list satisfies
  (n+2)^2 c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}^2 for every n ≤ 3 (uniform statement collecting LC0, A1, A2, GenA3).
-/
import ym.GenA3

namespace BondLC3

open YMChain QBinom CgWindow MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem scale_cancel (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (d : Nat) (a b : R)
    (h : O.le (K.mul (LibRing.castN K (d + 1)) a) (K.mul (LibRing.castN K (d + 1)) b)) : O.le a b := by
  have p := SU2CharExp.pos_of_ge_one K O h1 h10 (d + 1) (Nat.succ_le_succ (Nat.zero_le d))
  apply O.pos_cancel _ _ _ p.1 p.2
  have e1 : K.mul a (SU2Char.cN K (d + 1)) = K.mul (LibRing.castN K (d + 1)) a := by
    show K.mul a (LibRing.castN K (d + 1)) = K.mul (LibRing.castN K (d + 1)) a
    lib_ring
  have e2 : K.mul b (SU2Char.cN K (d + 1)) = K.mul (LibRing.castN K (d + 1)) b := by
    show K.mul b (LibRing.castN K (d + 1)) = K.mul (LibRing.castN K (d + 1)) b
    lib_ring
  rw [e1, e2]
  exact h

theorem bond_lc_le3 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (X : List R) (hXg : L1Full.Good K O X) (M : Nat) (hX : X.length = M + 2) (n : Nat) (hn : n ≤ 3) :
    O.le (K.mul (LibRing.castN K ((n + 2) * (n + 2))) (K.mul (cgc K X X n) (cgc K X X (n + 2))))
      (K.mul (LibRing.castN K ((n + 1) * (n + 3))) (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1)))) := by
  cases n with
  | zero =>
    have h := LC0.prod_lc0 K O F X hXg M hX
    have e1 : K.mul (LibRing.castN K ((0 + 2) * (0 + 2))) (K.mul (cgc K X X 0) (cgc K X X (0 + 2)))
        = K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (cgc K X X 0) (cgc K X X 2)) := by
      show K.mul (LibRing.castN K 4) (K.mul (cgc K X X 0) (cgc K X X 2)) = _
      lib_ring
    have e2 : K.mul (LibRing.castN K ((0 + 1) * (0 + 3))) (K.mul (cgc K X X (0 + 1)) (cgc K X X (0 + 1)))
        = K.mul (K.add (K.add K.one K.one) K.one) (K.mul (cgc K X X 1) (cgc K X X 1)) := by
      show K.mul (LibRing.castN K 3) (K.mul (cgc K X X 1) (cgc K X X 1)) = _
      lib_ring
    rw [e1, e2]
    exact h
  | succ n =>
    cases n with
    | zero => exact A1.prod_lc1 K O F h1 X hXg M hX
    | succ n =>
      cases n with
      | zero => exact A2.prod_lc2 K O F h1 X hXg M hX
      | succ n =>
        cases n with
        | zero =>
          have h := GenA3.prod_lc K O F h1 h10 X hXg M hX
          apply scale_cancel K O h1 h10 2
          have e1 : K.mul (LibRing.castN K (2 + 1)) (K.mul (LibRing.castN K ((0 + 1 + 1 + 1 + 2) * (0 + 1 + 1 + 1 + 2)))
              (K.mul (cgc K X X (0 + 1 + 1 + 1)) (cgc K X X (0 + 1 + 1 + 1 + 2))))
              = K.mul (LibRing.castN K 75) (K.mul (cgc K X X 3) (cgc K X X 5)) := by
            show K.mul (LibRing.castN K 3) (K.mul (LibRing.castN K 25) (K.mul (cgc K X X 3) (cgc K X X 5))) = _
            lib_ring
          have e2 : K.mul (LibRing.castN K (2 + 1)) (K.mul (LibRing.castN K ((0 + 1 + 1 + 1 + 1) * (0 + 1 + 1 + 1 + 3)))
              (K.mul (cgc K X X (0 + 1 + 1 + 1 + 1)) (cgc K X X (0 + 1 + 1 + 1 + 1))))
              = K.mul (LibRing.castN K 72) (K.mul (cgc K X X 4) (cgc K X X 4)) := by
            show K.mul (LibRing.castN K 3) (K.mul (LibRing.castN K 24) (K.mul (cgc K X X 4) (cgc K X X 4))) = _
            lib_ring
          rw [e1, e2]
          exact h
        | succ n => exact absurd hn (by
            intro h'
            exact Nat.not_succ_le_zero n (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ h'))))

end BondLC3

#print axioms BondLC3.scale_cancel
#print axioms BondLC3.bond_lc_le3

#print axioms: 2 theorems, no axioms.

90.97 Bond log-concavity for large \(n\)

When \(n\) is large compared with the length, the top coefficient vanishes and (A) is immediate.

\[ 2\,\mathrm {len}(X)\le n+3\ \Rightarrow \ c_{n+2}=0,\quad (n+2)^2c_nc_{n+2}\le (n+1)(n+3)c_{n+1}^2 \]

ym/BondLCBig.lean

/-
  ym/BondLCBig — T45, G54: (A) holds trivially once n is large compared with the length:
  if 2·len(X) ≤ n + 3 then c_{n+2} = 0, so (n+2)² c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}².
-/
import ym.A2

namespace BondLCBig

open YMChain SU2Char MigdalCone

variable {R : Type} (K : DetChain.CRing R)

theorem dot_zero_of : ∀ (a : List R) (m : Nat) (f : Nat → R),
    (∀ j, m ≤ j → j < m + a.length → f j = K.zero) → dot K m a f = K.zero
  | [], _, _, _ => rfl
  | c :: a, m, f, h => by
    show K.add (K.mul c (f m)) (dot K (m + 1) a f) = K.zero
    have h0 : f m = K.zero := h m (Nat.le_refl m) (by
      show m < m + (a.length + 1)
      exact Nat.lt_of_lt_of_le (Nat.lt_succ_self m) (Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le _)) m))
    have h1 : dot K (m + 1) a f = K.zero := dot_zero_of a (m + 1) f (fun j hj hj2 =>
      h j (Nat.le_trans (Nat.le_succ m) hj) (by
        have e : m + 1 + a.length = m + (a.length + 1) := by lib_nat
        show j < m + (a.length + 1)
        rw [← e]; exact hj2))
    rw [h0, h1]; lib_ring

theorem sum_lt (j k A B t : Nat) (hj : j < A) (hk : k < B) (h : A + B ≤ t + 1) : j + k < t := by
  have g := Nat.add_le_add (Nat.succ_le_of_lt hj) (Nat.succ_le_of_lt hk)
  have e : j + 1 + (k + 1) = (j + k + 1) + 1 := by lib_nat
  rw [e] at g
  exact Nat.le_of_succ_le_succ (Nat.le_trans g h)

theorem cgc_big (a b : List R) (t : Nat) (h : a.length + b.length ≤ t + 1) : cgc K a b t = K.zero := by
  unfold cgc
  apply dot_zero_of K a 0
  intro j _ hj
  apply dot_zero_of K b 0
  intro k _ hk
  have hj' : j < a.length := by rw [Nat.zero_add] at hj; exact hj
  have hk' : k < b.length := by rw [Nat.zero_add] at hk; exact hk
  rw [Fusion.cg_top j k t (sum_lt j k _ _ t hj' hk' h)]
  rfl

theorem bond_lc_big (O : ORing K) (h1 : O.le K.zero K.one) (X : List R) (n : Nat)
    (h : X.length + X.length ≤ n + 2 + 1) :
    O.le (K.mul (LibRing.castN K ((n + 2) * (n + 2))) (K.mul (cgc K X X n) (cgc K X X (n + 2))))
      (K.mul (LibRing.castN K ((n + 1) * (n + 3))) (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1)))) := by
  rw [cgc_big K X X (n + 2) h]
  have z : K.mul (LibRing.castN K ((n + 2) * (n + 2))) (K.mul (cgc K X X n) K.zero) = K.zero := by lib_ring
  rw [z]
  exact O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (A2.sq_nn K O _)

end BondLCBig

#print axioms BondLCBig.dot_zero_of
#print axioms BondLCBig.sum_lt
#print axioms BondLCBig.cgc_big
#print axioms BondLCBig.bond_lc_big

#print axioms: 4 theorems, no axioms.

90.98 Bond log-concavity at \(n=4\)

The bond-moving product of any v-monotone list has \(a_5^2\ge a_4a_6\), and the uniform statement holds for \(n\le 4\).

\[ 36\,c_4c_6\le 35\,c_5^2 \]

ym/GenA4.lean

ym/GenA4.lean (3.0 MiB, 20,868 lines, on its own page)

ym/BondLC4.lean

/-
  ym/BondLC4 — T45, G54: the bond-moving product of a v-monotone list satisfies
  (n+2)^2 c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}^2 for every n ≤ 4 (BondLC3 together with GenA4).
-/
import ym.BondLC3
import ym.GenA4

namespace BondLC4

open YMChain QBinom CgWindow MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem bond_lc_le4 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (X : List R) (hXg : L1Full.Good K O X) (M : Nat) (hX : X.length = M + 2) (n : Nat) (hn : n ≤ 4) :
    O.le (K.mul (LibRing.castN K ((n + 2) * (n + 2))) (K.mul (cgc K X X n) (cgc K X X (n + 2))))
      (K.mul (LibRing.castN K ((n + 1) * (n + 3))) (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1)))) := by
  cases Nat.lt_or_ge n 4 with
  | inl h => exact BondLC3.bond_lc_le3 K O F h1 h10 X hXg M hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 4 := Nat.le_antisymm hn h
    subst e
    exact GenA4.prod_lc K O F h1 h10 X hXg M hX

end BondLC4

#print axioms BondLC4.bond_lc_le4

#print axioms: 3 theorems, no axioms.

90.99 Bond log-concavity at \(n=5\)

The bond-moving product of any v-monotone list has \(a_6^2\ge a_5a_7\), and the uniform statement holds for \(n\le 5\).

\[ 49\,c_5c_7\le 48\,c_6^2 \]

ym/GenA5.lean

ym/GenA5.lean (6.7 MiB, 53,174 lines, 2 pages, on its own page)

ym/BondLC5.lean

/-
  ym/BondLC5 — T45, G54: the bond-moving product of a v-monotone list satisfies
  (n+2)^2 c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}^2 for every n ≤ 5 (BondLC4 together with GenA5).
-/
import ym.BondLC4
import ym.GenA5

namespace BondLC5

open YMChain QBinom CgWindow MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem bond_lc_le5 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (X : List R) (hXg : L1Full.Good K O X) (M : Nat) (hX : X.length = M + 2) (n : Nat) (hn : n ≤ 5) :
    O.le (K.mul (LibRing.castN K ((n + 2) * (n + 2))) (K.mul (cgc K X X n) (cgc K X X (n + 2))))
      (K.mul (LibRing.castN K ((n + 1) * (n + 3))) (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1)))) := by
  cases Nat.lt_or_ge n 5 with
  | inl h => exact BondLC4.bond_lc_le4 K O F h1 h10 X hXg M hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 5 := Nat.le_antisymm hn h
    subst e
    have g := GenA5.prod_lc K O F h1 h10 X hXg M hX
    apply BondLC3.scale_cancel K O h1 h10 1
    have e1 : K.mul (LibRing.castN K (1 + 1)) (K.mul (LibRing.castN K ((5 + 2) * (5 + 2)))
        (K.mul (cgc K X X 5) (cgc K X X (5 + 2))))
        = K.mul (LibRing.castN K 98) (K.mul (cgc K X X 5) (cgc K X X 7)) := by
      show K.mul (LibRing.castN K 2) (K.mul (LibRing.castN K 49) (K.mul (cgc K X X 5) (cgc K X X 7))) = _
      lib_ring
    have e2 : K.mul (LibRing.castN K (1 + 1)) (K.mul (LibRing.castN K ((5 + 1) * (5 + 3)))
        (K.mul (cgc K X X (5 + 1)) (cgc K X X (5 + 1))))
        = K.mul (LibRing.castN K 96) (K.mul (cgc K X X 6) (cgc K X X 6)) := by
      show K.mul (LibRing.castN K 2) (K.mul (LibRing.castN K 48) (K.mul (cgc K X X 6) (cgc K X X 6))) = _
      lib_ring
    rw [e1, e2]
    exact g

end BondLC5

#print axioms BondLC5.bond_lc_le5

#print axioms: 3 theorems, no axioms.

90.100 Bond log-concavity at \(n=6\)

For a v-good list \(X\) of any length, the bond-moving coefficients satisfy (A) at \(n=6\), hence for every \(n\le 6\).

\[ 64\,c_6(X\cdot X)\,c_8(X\cdot X)\le 63\,c_7(X\cdot X)^2 \]

ym/BondLC6.lean

/-
  ym/BondLC6 — T45, G54: the bond-moving product of a v-monotone list satisfies
  (n+2)^2 c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}^2 for every n ≤ 6 (BondLC5 together with GenA6).
-/
import ym.BondLC5
import ym.GenA6

namespace BondLC6

open YMChain QBinom CgWindow MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem bond_lc_le6 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (X : List R) (hXg : L1Full.Good K O X) (M : Nat) (hX : X.length = M + 2) (n : Nat) (hn : n ≤ 6) :
    O.le (K.mul (LibRing.castN K ((n + 2) * (n + 2))) (K.mul (cgc K X X n) (cgc K X X (n + 2))))
      (K.mul (LibRing.castN K ((n + 1) * (n + 3))) (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1)))) := by
  cases Nat.lt_or_ge n 6 with
  | inl h => exact BondLC5.bond_lc_le5 K O F h1 h10 X hXg M hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 6 := Nat.le_antisymm hn h
    subst e
    exact GenA6.prod_lc K O F h1 h10 X hXg M hX

end BondLC6

#print axioms BondLC6.bond_lc_le6

#print axioms: 3 theorems, no axioms.

90.101 Bond log-concavity at \(n=7\)

For a v-good list \(X\) of any length, the bond-moving coefficients satisfy (A) at \(n=7\), hence for every \(n\le 7\).

\[ 81\,c_7(X\cdot X)\,c_9(X\cdot X)\le 80\,c_8(X\cdot X)^2 \]

ym/BondLC7.lean

/-
  ym/BondLC7 — T45, G54: the bond-moving product of a v-monotone list satisfies
  (n+2)^2 c_n c_{n+2} ≤ (n+1)(n+3) c_{n+1}^2 for every n ≤ 7 (BondLC6 together with GenA7, scale 15).
-/
import ym.BondLC6
import ym.GenA7

namespace BondLC7

open YMChain QBinom CgWindow MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem bond_lc_le7 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (X : List R) (hXg : L1Full.Good K O X) (M : Nat) (hX : X.length = M + 2) (n : Nat) (hn : n ≤ 7) :
    O.le (K.mul (LibRing.castN K ((n + 2) * (n + 2))) (K.mul (cgc K X X n) (cgc K X X (n + 2))))
      (K.mul (LibRing.castN K ((n + 1) * (n + 3))) (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1)))) := by
  cases Nat.lt_or_ge n 7 with
  | inl h => exact BondLC6.bond_lc_le6 K O F h1 h10 X hXg M hX n (Nat.le_of_lt_succ h)
  | inr h =>
    have e : n = 7 := Nat.le_antisymm hn h
    subst e
    have g := GenA7.prod_lc K O F h1 h10 X hXg M hX
    apply BondLC3.scale_cancel K O h1 h10 14
    have e1 : K.mul (LibRing.castN K (14 + 1)) (K.mul (LibRing.castN K ((7 + 2) * (7 + 2)))
        (K.mul (cgc K X X 7) (cgc K X X (7 + 2))))
        = K.mul (LibRing.castN K 1215) (K.mul (cgc K X X 7) (cgc K X X 9)) := by
      show K.mul (LibRing.castN K 15) (K.mul (LibRing.castN K 81) (K.mul (cgc K X X 7) (cgc K X X 9))) = _
      lib_ring
    have e2 : K.mul (LibRing.castN K (14 + 1)) (K.mul (LibRing.castN K ((7 + 1) * (7 + 3)))
        (K.mul (cgc K X X (7 + 1)) (cgc K X X (7 + 1))))
        = K.mul (LibRing.castN K 1200) (K.mul (cgc K X X 8) (cgc K X X 8)) := by
      show K.mul (LibRing.castN K 15) (K.mul (LibRing.castN K 80) (K.mul (cgc K X X 8) (cgc K X X 8))) = _
      lib_ring
    rw [e1, e2]
    exact g

end BondLC7

#print axioms BondLC7.bond_lc_le7

#print axioms: 4 theorems, no axioms.

90.102 Bond log-concavity on the top diagonals

For every v-good list of length \(L=m+12\) and \(k\le 5\), (A) holds at \(n=2L-3-k\) with a certificate independent of \(L\).

\[ (n+2)^2\,c_n\,c_{n+2}\le (n+1)(n+3)\,c_{n+1}^2,\qquad n=21-k+2m \]

ym/TopShift.lean

/-
  ym/TopShift — T45, G62/G66: the top coefficients of a bond-moving product do not see the bottom of the list.
  For lists P, T with T.length ≤ t + 1,
    cgc (P ++ T) (P ++ T) (t + |P| + |P|) = cgc T T t,
  because cg (j + p) (s + p) (t + 2p) = cg j s t whenever |j − s| ≤ t (cg_shift_p) and the cross terms with
  an index in P lie strictly below the diagonal (Fusion.cg_top). Lean 4 core only, no axioms.
-/
import ym.BondLCBig
import ym.CgDim

namespace TopShift

open YMChain SU2Haar SU2Char Fusion MigdalCone

/-- the Clebsch–Gordan count drops by the top diagonal when the target index goes up by two -/
theorem cg_step : ∀ j s t, j ≤ s + t → s ≤ j + t → cg j s (t + 2) + dl (j + s) t = cg j s t
  | 0, s, t, _, h2 => by
    show dl s (t + 2) + dl (0 + s) t = dl s t
    have hs : s ≤ t := by rw [Nat.zero_add] at h2; exact h2
    rw [SU2Char.dl_lt s (t + 2) (Nat.lt_of_le_of_lt hs (Nat.lt_succ_of_lt (Nat.lt_succ_self t))), Nat.zero_add,
      Nat.zero_add]
  | j + 1, 0, t, h1, _ => by
    show dl (j + 1) (t + 2) + dl (j + 1 + 0) t = dl (j + 1) t
    have hs : j + 1 ≤ t := by rw [Nat.zero_add] at h1; exact h1
    rw [SU2Char.dl_lt (j + 1) (t + 2) (Nat.lt_of_le_of_lt hs (Nat.lt_succ_of_lt (Nat.lt_succ_self t))),
      Nat.zero_add, Nat.add_zero]
  | j + 1, s + 1, t, h1, h2 => by
    show cg j s (t + 2) + dl (s + j + 2) (t + 2) + dl (j + 1 + (s + 1)) t = cg j s t + dl (s + j + 2) t
    have h1' : j ≤ s + t := Nat.le_of_succ_le_succ (Nat.le_trans h1 (Nat.le_of_eq (by lib_nat)))
    have h2' : s ≤ j + t := Nat.le_of_succ_le_succ (Nat.le_trans h2 (Nat.le_of_eq (by lib_nat)))
    have e1 : dl (s + j + 2) (t + 2) = dl (j + s) t := by
      show dl (s + j) t = dl (j + s) t
      rw [Nat.add_comm s j]
    have e2 : j + 1 + (s + 1) = s + j + 2 := by lib_nat
    rw [e1, e2, cg_step j s t h1' h2']

/-- **shift by one on both legs and two on the target** -/
theorem cg_shift (j s t : Nat) (h1 : j ≤ s + t) (h2 : s ≤ j + t) : cg (j + 1) (s + 1) (t + 2) = cg j s t := by
  show cg j s (t + 2) + dl (s + j + 2) (t + 2) = cg j s t
  have e1 : dl (s + j + 2) (t + 2) = dl (j + s) t := by
    show dl (s + j) t = dl (j + s) t
    rw [Nat.add_comm s j]
  rw [e1]
  exact cg_step j s t h1 h2

/-- **shift by p on both legs and 2p on the target** -/
theorem cg_shift_p (j s t : Nat) (h1 : j ≤ s + t) (h2 : s ≤ j + t) :
    ∀ p, cg (j + p) (s + p) (t + p + p) = cg j s t
  | 0 => rfl
  | p + 1 => by
    have a1 : j + p ≤ s + p + (t + p + p) :=
      Nat.le_trans (Nat.add_le_add_right h1 p)
        (Nat.le_trans (Nat.le_add_right (s + t + p) (p + p)) (Nat.le_of_eq (by lib_nat)))
    have a2 : s + p ≤ j + p + (t + p + p) :=
      Nat.le_trans (Nat.add_le_add_right h2 p)
        (Nat.le_trans (Nat.le_add_right (j + t + p) (p + p)) (Nat.le_of_eq (by lib_nat)))
    have e : t + (p + 1) + (p + 1) = t + p + p + 2 := by lib_nat
    rw [e, show j + (p + 1) = j + p + 1 from rfl, show s + (p + 1) = s + p + 1 from rfl,
      cg_shift (j + p) (s + p) (t + p + p) a1 a2, cg_shift_p j s t h1 h2 p]

variable {R : Type} (K : DetChain.CRing R)

theorem dot_append (f : Nat → R) : ∀ (P T : List R) (m : Nat),
    dot K m (P ++ T) f = K.add (dot K m P f) (dot K (m + P.length) T f)
  | [], T, m => by
    show dot K m T f = K.add K.zero (dot K (m + 0) T f)
    rw [Nat.add_zero, K.zero_add]
  | c :: P, T, m => by
    show K.add (K.mul c (f m)) (dot K (m + 1) (P ++ T) f)
      = K.add (K.add (K.mul c (f m)) (dot K (m + 1) P f)) (dot K (m + (P.length + 1)) T f)
    rw [dot_append f P T (m + 1), show m + 1 + P.length = m + (P.length + 1) by lib_nat]
    lib_ring

theorem dot_off (f : Nat → R) : ∀ (T : List R) (m p : Nat), dot K (m + p) T f = dot K m T (fun i => f (i + p))
  | [], _, _ => rfl
  | c :: T, m, p => by
    show K.add (K.mul c (f (m + p))) (dot K (m + p + 1) T f) = K.add (K.mul c (f (m + p))) (dot K (m + 1) T _)
    rw [show m + p + 1 = m + 1 + p by lib_nat, dot_off f T (m + 1) p]

theorem len_app : ∀ (P T : List R), (P ++ T).length = P.length + T.length
  | [], T => (Nat.zero_add _).symm
  | _ :: P, T => by
    show (P ++ T).length + 1 = P.length + 1 + T.length
    rw [len_app P T]; lib_nat

/-- **the top coefficients only see the top segment** -/
theorem cgc_top (P T : List R) (t : Nat) (hT : T.length ≤ t + 1) :
    cgc K (P ++ T) (P ++ T) (t + P.length + P.length) = cgc K T T t := by
  unfold cgc
  have low : ∀ j k, j < P.length → k < P.length + T.length → j + k < t + P.length + P.length := by
    intro j k hj hk
    have g : j + 1 + (k + 1) ≤ P.length + (P.length + T.length) := Nat.add_le_add hj hk
    have g2 : P.length + (P.length + T.length) ≤ (t + P.length + P.length) + 1 :=
      Nat.le_trans (Nat.add_le_add_left (Nat.add_le_add_left hT _) _) (Nat.le_of_eq (by lib_nat))
    have e : j + 1 + (k + 1) = (j + k + 1) + 1 := by lib_nat
    rw [e] at g
    exact Nat.le_of_succ_le_succ (Nat.le_trans g g2)
  rw [dot_append K _ P T 0]
  have z1 : dot K 0 P (fun j => dot K 0 (P ++ T) (fun k => cN K (cg j k (t + P.length + P.length)))) = K.zero := by
    apply BondLCBig.dot_zero_of K P 0
    intro j _ hj
    apply BondLCBig.dot_zero_of K (P ++ T) 0
    intro k _ hk
    rw [Nat.zero_add] at hj hk
    rw [len_app P T] at hk
    show cN K (cg j k (t + P.length + P.length)) = K.zero
    rw [Fusion.cg_top j k _ (low j k hj hk)]
    rfl
  rw [z1, K.zero_add, dot_off K _ T 0 P.length]
  apply CgDim.dot_ext_lt K _ _ 0 T
  intro a _ ha
  rw [Nat.zero_add] at ha
  show dot K 0 (P ++ T) (fun k => cN K (cg (a + P.length) k (t + P.length + P.length))) = _
  rw [dot_append K _ P T 0]
  have z2 : dot K 0 P (fun k => cN K (cg (a + P.length) k (t + P.length + P.length))) = K.zero := by
    apply BondLCBig.dot_zero_of K P 0
    intro k _ hk
    rw [Nat.zero_add] at hk
    have hj : a + P.length < P.length + T.length := by
      rw [Nat.add_comm a]; exact Nat.add_lt_add_left ha _
    have g := low k (a + P.length) hk hj
    show cN K (cg (a + P.length) k (t + P.length + P.length)) = K.zero
    rw [Fusion.cg_top (a + P.length) k _ (by rw [Nat.add_comm (a + P.length) k]; exact g)]
    rfl
  rw [z2, K.zero_add, dot_off K _ T 0 P.length]
  apply CgDim.dot_ext_lt K _ _ 0 T
  intro b _ hb
  rw [Nat.zero_add] at hb
  have hat : a ≤ t := Nat.le_of_lt_succ (Nat.lt_of_lt_of_le ha hT)
  have hbt : b ≤ t := Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hb hT)
  show cN K (cg (a + P.length) (b + P.length) (t + P.length + P.length)) = cN K (cg a b t)
  rw [cg_shift_p a b t (Nat.le_trans hat (Nat.le_add_left t b)) (Nat.le_trans hbt (Nat.le_add_left t a)) P.length]

/-- the coefficient function of an appended list, read above the prefix -/
theorem co_append : ∀ (P T : List R) (i : Nat), HeatPair.co K (P ++ T) (i + P.length) = HeatPair.co K T i
  | [], _, _ => rfl
  | _ :: P, T, i => by
    show HeatPair.co K (P ++ T) (i + P.length) = _
    exact co_append P T i

end TopShift

#print axioms TopShift.cg_step
#print axioms TopShift.cg_shift
#print axioms TopShift.cg_shift_p
#print axioms TopShift.dot_append
#print axioms TopShift.dot_off
#print axioms TopShift.len_app
#print axioms TopShift.cgc_top
#print axioms TopShift.co_append

ym/TopLC2.lean

/-
  ym/TopLC2 — T45, G66: (A) on the top diagonal n = 2L − 3 − 2 for every v-good list of length L ≥ 12:
    (n+2)² c_n c_(n+2) ≤ (n+1)(n+3) c_(n+1)²,  n = 19 + 2m,  L = m + 12.
  Certificate (7 terms): (n+1)(n+3)C₁² − (n+2)²C₀C₂ = Σ (a₀ + a₁m + a₂m²)·D·D on the top eight entries,
  a_i ≥ 0 integers (generated by work/cert/A/top/gen_top.py from the length 12–14 certificates). Lean 4 core only, no axioms.
-/
import ym.TopLC

namespace TopLC2

open YMChain SU2Char HeatPair MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

set_option maxRecDepth 200000 in
set_option maxHeartbeats 0 in
theorem cert (q t0 t1 t2 t3 t4 t5 t6 t7 : R) : LibRing.sub K (K.mul (K.mul (K.add (K.add (LibRing.castN K 20) q) q) (K.add (K.add (LibRing.castN K 22) q) q)) (K.mul (K.add (K.add (K.add (K.mul t5 t7) (K.mul t6 t6)) (K.mul t7 t5)) (K.mul t7 t7)) (K.add (K.add (K.add (K.mul t5 t7) (K.mul t6 t6)) (K.mul t7 t5)) (K.mul t7 t7)))) (K.mul (K.mul (K.add (K.add (LibRing.castN K 21) q) q) (K.add (K.add (Lib  -- (+2150 chars; full line in the Zenodo source)
  ring_z

set_option maxRecDepth 200000 in
theorem core (h1 : O.le K.zero K.one) (q : R) (hq : O.le K.zero q) (t0 t1 t2 t3 t4 t5 t6 t7 : R) (hd0 : O.le K.zero (LibRing.sub K (K.mul (K.add t5 t7) t5) (K.mul (K.add t4 t6) t6))) (hd1 : O.le K.zero (LibRing.sub K (K.mul (K.add t6 K.zero) t5) (K.mul (K.add t4 t6) t7))) (hd2 : O.le K.zero (LibRing.sub K (K.mul (K.add t7 K.zero) t5) (K.mul (K.add t4 t6) K.zero))) (hd3 : O.le K.zero (LibRing.sub K  -- (+250 chars; full line in the Zenodo source)
    O.le (K.mul (K.mul (K.add (K.add (LibRing.castN K 21) q) q) (K.add (K.add (LibRing.castN K 21) q) q)) (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul t4 t7) (K.mul t5 t6)) (K.mul t6 t5)) (K.mul t6 t7)) (K.mul t7 t4)) (K.mul t7 t6)) (K.add (K.mul t6 t7) (K.mul t7 t6)))) (K.mul (K.mul (K.add (K.add (LibRing.castN K 20) q) q) (K.add (K.add (LibRing.castN K 22) q) q)) (K.mul (K.add (K.add (K.add   -- (+146 chars; full line in the Zenodo source)
  have t : O.le K.zero (LibRing.sub K (K.mul (K.mul (K.add (K.add (LibRing.castN K 20) q) q) (K.add (K.add (LibRing.castN K 22) q) q)) (K.mul (K.add (K.add (K.add (K.mul t5 t7) (K.mul t6 t6)) (K.mul t7 t5)) (K.mul t7 t7)) (K.add (K.add (K.add (K.mul t5 t7) (K.mul t6 t6)) (K.mul t7 t5)) (K.mul t7 t7)))) (K.mul (K.mul (K.add (K.add (LibRing.castN K 21) q) q) (K.add (K.add (LibRing.castN K 21) q) q))  -- (+176 chars; full line in the Zenodo source)
    rw [cert K q t0 t1 t2 t3 t4 t5 t6 t7]
    exact (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ (O.mul_nonneg _ _ (TopLC.add_nn K O _ _ (SU2CharExp.cN_nn K O h1 888) (TopLC.add_nn K O _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 168) hq) (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 8) (O.mul_nonneg _ _ hq hq)))) (O.mul_nonneg _ _ hd0 hd5)) (O.mul_nonne  -- (+1461 chars; full line in the Zenodo source)
  exact TopLC.le_of_sub_nn K O _ _ t

theorem top_app (F : OField.OF K O) (P : List R) (t0 t1 t2 t3 t4 t5 t6 t7 : R) (hXg : L1Full.Good K O (P ++ [t0, t1, t2, t3, t4, t5, t6, t7])) (m : Nat)
    (hP : P.length = m + 4) :
    O.le (K.mul (LibRing.castN K ((21 + m + m) * (21 + m + m))) (K.mul (cgc K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (19 + m + m)) (cgc K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (21 + m + m))))
      (K.mul (LibRing.castN K ((20 + m + m) * (22 + m + m))) (K.mul (cgc K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (20 + m + m)) (cgc K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (20 + m + m)))) := by
  have h1 := TopLC.one_nn K O (O.le_total K.zero K.one)
  have hq : O.le K.zero (LibRing.castN K m) := SU2CharExp.cN_nn K O h1 m
  have w2 : LibRing.castN K ((21 + m + m) * (21 + m + m)) = (K.mul (K.add (K.add (LibRing.castN K 21) (LibRing.castN K m)) (LibRing.castN K m)) (K.add (K.add (LibRing.castN K 21) (LibRing.castN K m)) (LibRing.castN K m))) := by
    rw [RingZ.castN_mul, RingZ.castN_add, RingZ.castN_add]
  have w13 : LibRing.castN K ((20 + m + m) * (22 + m + m)) = (K.mul (K.add (K.add (LibRing.castN K 20) (LibRing.castN K m)) (LibRing.castN K m)) (K.add (K.add (LibRing.castN K 22) (LibRing.castN K m)) (LibRing.castN K m))) := by
    rw [RingZ.castN_mul, RingZ.castN_add, RingZ.castN_add, RingZ.castN_add, RingZ.castN_add]
  have e0 : 19 + m + m = 11 + P.length + P.length := by rw [hP]; lib_nat
  have e1 : 20 + m + m = 12 + P.length + P.length := by rw [hP]; lib_nat
  have e2 : 21 + m + m = 13 + P.length + P.length := by rw [hP]; lib_nat
  rw [w2, w13, e0, e1, e2, TopShift.cgc_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 11 (Nat.le_of_ble_eq_true rfl), TopShift.cgc_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 12 (Nat.le_of_ble_eq_true rfl),
    TopShift.cgc_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 13 (Nat.le_of_ble_eq_true rfl), TopLC.cgcT11 K t0 t1 t2 t3 t4 t5 t6 t7, TopLC.cgcT12 K t0 t1 t2 t3 t4 t5 t6 t7, TopLC.cgcT13 K t0 t1 t2 t3 t4 t5 t6 t7]
  have hD := TopLC.minors_gen K O F (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) hXg
  have hd0 : O.le K.zero (LibRing.sub K (K.mul (K.add t5 t7) t5) (K.mul (K.add t4 t6) t6)) := by
    have h := hD (P.length + 5) 1
    have e : TopLC.Dd K (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7])) (P.length + 5) (P.length + 5 + 1) = (LibRing.sub K (K.mul (K.add t5 t7) t5) (K.mul (K.add t4 t6) t6)) := by
      show (LibRing.sub K (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 5)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 7))) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 5))) (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 4)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 6))) (co K (P ++ [t0, t1, t2, t3, t4,   -- (+34 chars; full line in the Zenodo source)
      rw [TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 4, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 5, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 6, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 7]
      all_goals rfl
    rw [e] at h; exact h
  have hd1 : O.le K.zero (LibRing.sub K (K.mul (K.add t6 K.zero) t5) (K.mul (K.add t4 t6) t7)) := by
    have h := hD (P.length + 5) 2
    have e : TopLC.Dd K (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7])) (P.length + 5) (P.length + 5 + 2) = (LibRing.sub K (K.mul (K.add t6 K.zero) t5) (K.mul (K.add t4 t6) t7)) := by
      show (LibRing.sub K (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 6)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 8))) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 5))) (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 4)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 6))) (co K (P ++ [t0, t1, t2, t3, t4,   -- (+34 chars; full line in the Zenodo source)
      rw [TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 4, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 5, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 6, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 7, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 8]
      all_goals rfl
    rw [e] at h; exact h
  have hd2 : O.le K.zero (LibRing.sub K (K.mul (K.add t7 K.zero) t5) (K.mul (K.add t4 t6) K.zero)) := by
    have h := hD (P.length + 5) 3
    have e : TopLC.Dd K (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7])) (P.length + 5) (P.length + 5 + 3) = (LibRing.sub K (K.mul (K.add t7 K.zero) t5) (K.mul (K.add t4 t6) K.zero)) := by
      show (LibRing.sub K (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 7)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 9))) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 5))) (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 4)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 6))) (co K (P ++ [t0, t1, t2, t3, t4,   -- (+34 chars; full line in the Zenodo source)
      rw [TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 4, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 5, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 6, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 7, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 8, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 9]
      all_goals rfl
    rw [e] at h; exact h
  have hd3 : O.le K.zero (LibRing.sub K (K.mul (K.add t6 K.zero) t6) (K.mul (K.add t5 t7) t7)) := by
    have h := hD (P.length + 6) 1
    have e : TopLC.Dd K (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7])) (P.length + 6) (P.length + 6 + 1) = (LibRing.sub K (K.mul (K.add t6 K.zero) t6) (K.mul (K.add t5 t7) t7)) := by
      show (LibRing.sub K (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 6)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 8))) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 6))) (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 5)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 7))) (co K (P ++ [t0, t1, t2, t3, t4,   -- (+34 chars; full line in the Zenodo source)
      rw [TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 5, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 6, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 7, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 8]
      all_goals rfl
    rw [e] at h; exact h
  have hd4 : O.le K.zero (LibRing.sub K (K.mul (K.add t7 K.zero) t6) (K.mul (K.add t5 t7) K.zero)) := by
    have h := hD (P.length + 6) 2
    have e : TopLC.Dd K (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7])) (P.length + 6) (P.length + 6 + 2) = (LibRing.sub K (K.mul (K.add t7 K.zero) t6) (K.mul (K.add t5 t7) K.zero)) := by
      show (LibRing.sub K (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 7)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 9))) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 6))) (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 5)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 7))) (co K (P ++ [t0, t1, t2, t3, t4,   -- (+34 chars; full line in the Zenodo source)
      rw [TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 5, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 6, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 7, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 8, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 9]
      all_goals rfl
    rw [e] at h; exact h
  have hd5 : O.le K.zero (LibRing.sub K (K.mul (K.add t7 K.zero) t7) (K.mul (K.add t6 K.zero) K.zero)) := by
    have h := hD (P.length + 7) 1
    have e : TopLC.Dd K (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7])) (P.length + 7) (P.length + 7 + 1) = (LibRing.sub K (K.mul (K.add t7 K.zero) t7) (K.mul (K.add t6 K.zero) K.zero)) := by
      show (LibRing.sub K (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 7)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 9))) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 7))) (K.mul (K.add (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 6)) (co K (P ++ [t0, t1, t2, t3, t4, t5, t6, t7]) (P.length + 8))) (co K (P ++ [t0, t1, t2, t3, t4,   -- (+34 chars; full line in the Zenodo source)
      rw [TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 6, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 7, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 8, TopLC.co_top K P [t0, t1, t2, t3, t4, t5, t6, t7] 9]
      all_goals rfl
    rw [e] at h; exact h
  exact core K O h1 (LibRing.castN K m) hq t0 t1 t2 t3 t4 t5 t6 t7 hd0 hd1 hd2 hd3 hd4 hd5

/-- **(A) on the top diagonal n = 2L − 3 − 2, every v-good list of length L = m + 12** -/
theorem top (F : OField.OF K O) (X : List R) (hXg : L1Full.Good K O X) (m : Nat) (hX : X.length = m + 12) :
    O.le (K.mul (LibRing.castN K ((21 + m + m) * (21 + m + m))) (K.mul (cgc K X X (19 + m + m)) (cgc K X X (21 + m + m))))
      (K.mul (LibRing.castN K ((20 + m + m) * (22 + m + m))) (K.mul (cgc K X X (20 + m + m)) (cgc K X X (20 + m + m)))) := by
  have hX' : X.length = (m + 4) + 8 := Eq.trans hX (by lib_nat)
  cases TopLC.split8 (m + 4) X hX' with
  | intro P hP => cases hP with
    | intro T hT =>
      have ⟨hXe, hPl, hTl⟩ := hT
      subst hXe
      cases T with
      | nil => exact absurd hTl (fun e => Nat.noConfusion e)
      | cons t0 T => cases T with
        | nil => exact absurd hTl (fun e => Nat.noConfusion (Nat.succ.inj (e)))
        | cons t1 T => cases T with
          | nil => exact absurd hTl (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (e))))
          | cons t2 T => cases T with
            | nil => exact absurd hTl (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (e)))))
            | cons t3 T => cases T with
              | nil => exact absurd hTl (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (e))))))
              | cons t4 T => cases T with
                | nil => exact absurd hTl (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (e)))))))
                | cons t5 T => cases T with
                  | nil => exact absurd hTl (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (e))))))))
                  | cons t6 T => cases T with
                    | nil => exact absurd hTl (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (e)))))))))
                    | cons t7 T => cases T with
                      | nil => exact top_app K O F P t0 t1 t2 t3 t4 t5 t6 t7 hXg m hPl
                      | cons _ T => exact absurd hTl (fun e => Nat.noConfusion (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (Nat.succ.inj (e))))))))))

end TopLC2

#print axioms TopLC2.cert
#print axioms TopLC2.core
#print axioms TopLC2.top_app
#print axioms TopLC2.top

#print axioms: 46 theorems, no axioms.

90.103 Propagation of (A) up the diagonals

(A) for the tail \(Y\) at \(t\) gives (A) for \(x_0::Y\) at \(t+2\) when \(|Y|\le t+1\); hence (A) for all \(n\ge |X|-1\) with \(n\ge 2|X|-16\).

\[ (t+1)(t+3)\,E_{t+2}(x_0::Y)=(t+1)(t+3)\,D_t(Y)+(4t+12)\bigl (D_t(Y)+c_tc_{t+2}\bigr ) \]

ym/DiagUp.lean

/-
  ym/DiagUp — T45, G66: (A) propagates up the diagonals.
  If X = x0 :: Y and Y.length ≤ t + 1, then c_{t+2+i}(X) = c_{t+i}(Y) (TopShift.cgc_top), so with
  D = (t+1)(t+3)c'_{t+1}² − (t+2)²c'_t c'_{t+2} ≥ 0 for Y and β = c'_t c'_{t+2} ≥ 0,
    (t+1)(t+3) · [(t+3)(t+5)c'_{t+1}² − (t+4)²β] = (t+1)(t+3)·D + (4t+12)(D + β) ≥ 0,
  i.e. (A) for Y at t gives (A) for X at t + 2. Iterating: (A) at n ≥ |X| − 1 follows from (A) on the boundary
  diagonal n = |Z| − 1 of the tails Z of X. Lean 4 core only, no axioms.
-/
import ym.TopShift
import ym.HeatL1All
import ym.AInd
import ym.BondLC3
import ym.TopLC

namespace DiagUp

open YMChain SU2Char HeatPair MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- the statement (A) at index n -/
def Stmt (X : List R) (n : Nat) : Prop :=
  O.le (K.mul (LibRing.castN K ((n + 2) * (n + 2))) (K.mul (cgc K X X n) (cgc K X X (n + 2))))
    (K.mul (LibRing.castN K ((n + 1) * (n + 3))) (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1))))

/-- the ring identity behind the propagation step -/
theorem combine (c e w13 w2 w13' w2' A B : R)
    (hA : K.mul c w13' = K.add (K.mul c w13) (K.mul e w13))
    (hB : K.add (K.mul c w2') e = K.add (K.mul c w2) (K.mul e w2)) :
    LibRing.sub K (K.mul c (K.mul w13' A)) (K.mul c (K.mul w2' B))
      = K.add (K.mul c (LibRing.sub K (K.mul w13 A) (K.mul w2 B)))
          (K.mul e (K.add (LibRing.sub K (K.mul w13 A) (K.mul w2 B)) B)) := by
  have e1 : LibRing.sub K (K.mul c (K.mul w13' A)) (K.mul c (K.mul w2' B))
      = LibRing.sub K (K.mul (K.mul c w13') A) (K.mul (K.mul c w2') B) := by lib_ring
  have hB' : K.mul c w2' = LibRing.sub K (K.add (K.mul c w2) (K.mul e w2)) e := by
    rw [← hB]; lib_ring
  rw [e1, hA, hB']
  lib_ring

theorem step (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x0 : R) (Y : List R)
    (hg : L1Full.Good K O (x0 :: Y)) (L : Nat) (hY : Y.length = L + 1) (t : Nat) (hT : Y.length ≤ t + 1)
    (hA : Stmt K O Y t) : Stmt K O (x0 :: Y) (t + 2) := by
  have c0 : cgc K (x0 :: Y) (x0 :: Y) (t + 2) = cgc K Y Y t := TopShift.cgc_top K [x0] Y t hT
  have c1 : cgc K (x0 :: Y) (x0 :: Y) (t + 2 + 1) = cgc K Y Y (t + 1) :=
    TopShift.cgc_top K [x0] Y (t + 1) (Nat.le_succ_of_le hT)
  have c2 : cgc K (x0 :: Y) (x0 :: Y) (t + 2 + 2) = cgc K Y Y (t + 2) :=
    TopShift.cgc_top K [x0] Y (t + 2) (Nat.le_succ_of_le (Nat.le_succ_of_le hT))
  show O.le (K.mul (LibRing.castN K ((t + 2 + 2) * (t + 2 + 2))) (K.mul (cgc K (x0 :: Y) (x0 :: Y) (t + 2))
      (cgc K (x0 :: Y) (x0 :: Y) (t + 2 + 2))))
    (K.mul (LibRing.castN K ((t + 2 + 1) * (t + 2 + 3))) (K.mul (cgc K (x0 :: Y) (x0 :: Y) (t + 2 + 1))
      (cgc K (x0 :: Y) (x0 :: Y) (t + 2 + 1))))
  rw [c0, c1, c2]
  have hYg := AInd.good_tail K O x0 Y hg
  have hc : ∀ s, O.le K.zero (cgc K Y Y s) := fun s =>
    HeatL1All.cgc_nn K O Y Y L hY hYg.1 hYg.1 s
  have hB : O.le K.zero (K.mul (cgc K Y Y t) (cgc K Y Y (t + 2))) := O.mul_nonneg _ _ (hc t) (hc (t + 2))
  have hD : O.le K.zero (LibRing.sub K (K.mul (LibRing.castN K ((t + 1) * (t + 3))) (K.mul (cgc K Y Y (t + 1)) (cgc K Y Y (t + 1))))
      (K.mul (LibRing.castN K ((t + 2) * (t + 2))) (K.mul (cgc K Y Y t) (cgc K Y Y (t + 2))))) :=
    HeatV.sub_nn K O _ _ hA
  apply BondLC3.scale_cancel K O h1 h10 (t * t + 4 * t + 2)
  apply TopLC.le_of_sub_nn K O
  have nA : (t * t + 4 * t + 2 + 1) * ((t + 2 + 1) * (t + 2 + 3))
      = (t * t + 4 * t + 2 + 1) * ((t + 1) * (t + 3)) + (4 * t + 12) * ((t + 1) * (t + 3)) := by lib_nat
  have nB : (t * t + 4 * t + 2 + 1) * ((t + 2 + 2) * (t + 2 + 2)) + (4 * t + 12)
      = (t * t + 4 * t + 2 + 1) * ((t + 2) * (t + 2)) + (4 * t + 12) * ((t + 2) * (t + 2)) := by lib_nat
  have hAe : K.mul (LibRing.castN K (t * t + 4 * t + 2 + 1)) (LibRing.castN K ((t + 2 + 1) * (t + 2 + 3)))
      = K.add (K.mul (LibRing.castN K (t * t + 4 * t + 2 + 1)) (LibRing.castN K ((t + 1) * (t + 3))))
          (K.mul (LibRing.castN K (4 * t + 12)) (LibRing.castN K ((t + 1) * (t + 3)))) := by
    rw [← RingZ.castN_mul K (t * t + 4 * t + 2 + 1) ((t + 2 + 1) * (t + 2 + 3)),
      ← RingZ.castN_mul K (t * t + 4 * t + 2 + 1) ((t + 1) * (t + 3)),
      ← RingZ.castN_mul K (4 * t + 12) ((t + 1) * (t + 3)),
      ← RingZ.castN_add K ((t * t + 4 * t + 2 + 1) * ((t + 1) * (t + 3))) ((4 * t + 12) * ((t + 1) * (t + 3))), nA]
  have hBe : K.add (K.mul (LibRing.castN K (t * t + 4 * t + 2 + 1)) (LibRing.castN K ((t + 2 + 2) * (t + 2 + 2))))
        (LibRing.castN K (4 * t + 12))
      = K.add (K.mul (LibRing.castN K (t * t + 4 * t + 2 + 1)) (LibRing.castN K ((t + 2) * (t + 2))))
          (K.mul (LibRing.castN K (4 * t + 12)) (LibRing.castN K ((t + 2) * (t + 2)))) := by
    rw [← RingZ.castN_mul K (t * t + 4 * t + 2 + 1) ((t + 2 + 2) * (t + 2 + 2)),
      ← RingZ.castN_add K ((t * t + 4 * t + 2 + 1) * ((t + 2 + 2) * (t + 2 + 2))) (4 * t + 12),
      ← RingZ.castN_mul K (t * t + 4 * t + 2 + 1) ((t + 2) * (t + 2)),
      ← RingZ.castN_mul K (4 * t + 12) ((t + 2) * (t + 2)),
      ← RingZ.castN_add K ((t * t + 4 * t + 2 + 1) * ((t + 2) * (t + 2))) ((4 * t + 12) * ((t + 2) * (t + 2))), nB]
  rw [combine K _ _ _ _ _ _ _ _ hAe hBe]
  have hc0 : O.le K.zero (LibRing.castN K (t * t + 4 * t + 2 + 1)) := SU2CharExp.cN_nn K O h1 _
  have he : O.le K.zero (LibRing.castN K (4 * t + 12)) := SU2CharExp.cN_nn K O h1 _
  exact TopLC.add_nn K O _ _ (O.mul_nonneg _ _ hc0 hD) (O.mul_nonneg _ _ he (TopLC.add_nn K O _ _ hD hB))

/-- **propagation up the diagonals**: if (A) holds on the boundary diagonal n = |Z| − 1 for every v-good Z with
  |Z| ≤ M + 1, then (A) holds for every v-good X at every n ≥ |X| − 1 with 2|X| − n ≤ M + 2 -/
theorem diag_up (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (M : Nat)
    (base : ∀ (Z : List R) (L : Nat), L1Full.Good K O Z → Z.length = L + 1 → L ≤ M → Stmt K O Z L) :
    ∀ (e : Nat) (X : List R) (L : Nat), L1Full.Good K O X → X.length = L + 1 → L ≤ M + e → Stmt K O X (L + e)
  | 0, X, L, hg, hX, hL => base X L hg hX hL
  | e + 1, [], L, _, hX, _ => absurd hX (fun h => Nat.noConfusion h)
  | e + 1, x0 :: Y, 0, _, hX, _ => by
    have hY : Y.length = 0 := Nat.succ.inj hX
    have g := BondLCBig.bond_lc_big K O h1 (x0 :: Y) (0 + (e + 1))
      (by show Y.length + 1 + (Y.length + 1) ≤ 0 + (e + 1) + 2 + 1
          rw [hY]
          exact Nat.le_trans (Nat.le_succ 2) (Nat.le_trans (Nat.le_add_left 3 (0 + (e + 1))) (Nat.le_of_eq (by lib_nat))))
    exact g
  | e + 1, x0 :: Y, L + 1, hg, hX, hL => by
    have hY : Y.length = L + 1 := Nat.succ.inj hX
    have hL' : L ≤ M + e := Nat.le_of_succ_le_succ (Nat.le_trans hL (Nat.le_of_eq (by lib_nat)))
    have ih := diag_up F h1 h10 M base e Y L (AInd.good_tail K O x0 Y hg) hY hL'
    have hT : Y.length ≤ L + e + 1 := by rw [hY]; exact Nat.succ_le_succ (Nat.le_add_right L e)
    have g := step K O F h1 h10 x0 Y hg L hY (L + e) hT ih
    rw [show L + 1 + (e + 1) = L + e + 2 by lib_nat]
    exact g

end DiagUp

#print axioms DiagUp.combine
#print axioms DiagUp.step
#print axioms DiagUp.diag_up

#print axioms: 5 theorems, no axioms.

90.104 Bond-moving coefficients as sums of tail Cauchy squares

The coefficients of \(X\cdot X\) are the sums over tails of the Cauchy squares.

\[ c_n(x_0::Y)=c_n(Y)+\bigl ((x_0::Y)\star (x_0::Y)\bigr )_n \]

ym/TailSq.lean

/-
  ym/TailSq — T45, G68: the bond-moving coefficients are sums of Cauchy squares of the tails.
  cg (j+1) (s+1) n = cg j s n + dl (j+s+2) n gives
    c_n(x0 :: Y) = c_n(Y) + ((x0 :: Y) ⋆ (x0 :: Y))_n,
  hence c_n(X) = Σ_i (T_i ⋆ T_i)_n over the tails T_i of X (the rows of the Hankel matrix x_{i+a}).
  Lean 4 core only, no axioms.
-/
import ym.TopShift
import ym.RingZ

namespace TailSq

open YMChain SU2Haar SU2Char Fusion MigdalCone

variable {R : Type} (K : DetChain.CRing R)

/-- Cauchy square of a list at index n -/
def sqc (X : List R) (n : Nat) : R := dot K 0 X (fun j => dot K 0 X (fun s => cN K (dl (j + s) n)))

theorem dot_fadd (f g : Nat → R) : ∀ (p : List R) (m : Nat),
    dot K m p (fun i => K.add (f i) (g i)) = K.add (dot K m p f) (dot K m p g)
  | [], _ => by show K.zero = K.add K.zero K.zero; lib_ring
  | c :: p, m => by
    show K.add (K.mul c (K.add (f m) (g m))) (dot K (m + 1) p (fun i => K.add (f i) (g i)))
      = K.add (K.add (K.mul c (f m)) (dot K (m + 1) p f)) (K.add (K.mul c (g m)) (dot K (m + 1) p g))
    rw [dot_fadd f g p (m + 1)]; lib_ring

theorem dot_one (f : Nat → R) (p : List R) : dot K 1 p f = dot K 0 p (fun i => f (i + 1)) := by
  have h := TopShift.dot_off K f p 0 1
  rw [Nat.zero_add] at h
  exact h

/-- expansion of a double sum over x0 :: Y -/
theorem expand (x0 : R) (Y : List R) (F : Nat → Nat → R) :
    dot K 0 (x0 :: Y) (fun j => dot K 0 (x0 :: Y) (fun s => F j s))
      = K.add (K.mul x0 (K.add (K.mul x0 (F 0 0)) (dot K 0 Y (fun s => F 0 (s + 1)))))
          (dot K 0 Y (fun j => K.add (K.mul x0 (F (j + 1) 0)) (dot K 0 Y (fun s => F (j + 1) (s + 1))))) := by
  show K.add (K.mul x0 (K.add (K.mul x0 (F 0 0)) (dot K 1 Y (fun s => F 0 s))))
      (dot K 1 Y (fun j => K.add (K.mul x0 (F j 0)) (dot K 1 Y (fun s => F j s)))) = _
  rw [dot_one K (fun s => F 0 s) Y, dot_one K (fun j => K.add (K.mul x0 (F j 0)) (dot K 1 Y (fun s => F j s))) Y]
  apply congrArg (K.add _)
  apply SU2Char.dot_ext K
  intro j
  show K.add (K.mul x0 (F (j + 1) 0)) (dot K 1 Y (fun s => F (j + 1) s)) = _
  rw [dot_one K (fun s => F (j + 1) s) Y]

/-- **one tail step**: c_n(x0 :: Y) = c_n(Y) + ((x0 :: Y) ⋆ (x0 :: Y))_n -/
theorem cgc_cons (x0 : R) (Y : List R) (n : Nat) :
    cgc K (x0 :: Y) (x0 :: Y) n = K.add (cgc K Y Y n) (sqc K (x0 :: Y) n) := by
  unfold cgc sqc
  rw [expand K x0 Y (fun j s => cN K (cg j s n)), expand K x0 Y (fun j s => cN K (dl (j + s) n))]
  have i1 : dot K 0 Y (fun s => cN K (cg 0 (s + 1) n)) = dot K 0 Y (fun s => cN K (dl (0 + (s + 1)) n)) := by
    apply SU2Char.dot_ext K; intro s; rw [Nat.zero_add]; rfl
  have i2 : ∀ j, dot K 0 Y (fun s => cN K (cg (j + 1) (s + 1) n))
      = K.add (dot K 0 Y (fun s => cN K (cg j s n))) (dot K 0 Y (fun s => cN K (dl (j + 1 + (s + 1)) n))) := by
    intro j
    rw [← dot_fadd K]
    apply SU2Char.dot_ext K; intro s
    show cN K (cg j s n + dl (s + j + 2) n) = K.add (cN K (cg j s n)) (cN K (dl (j + 1 + (s + 1)) n))
    rw [show j + 1 + (s + 1) = s + j + 2 by lib_nat]
    exact RingZ.castN_add K _ _
  rw [i1]
  have i3 : dot K 0 Y (fun j => K.add (K.mul x0 (cN K (cg (j + 1) 0 n))) (dot K 0 Y (fun s => cN K (cg (j + 1) (s + 1) n))))
      = K.add (dot K 0 Y (fun j => dot K 0 Y (fun s => cN K (cg j s n))))
          (dot K 0 Y (fun j => K.add (K.mul x0 (cN K (dl (j + 1 + 0) n))) (dot K 0 Y (fun s => cN K (dl (j + 1 + (s + 1)) n))))) := by
    rw [← dot_fadd K]
    apply SU2Char.dot_ext K; intro j
    rw [i2 j]
    show K.add (K.mul x0 (cN K (dl (j + 1) n))) (K.add _ _) = K.add _ (K.add (K.mul x0 (cN K (dl (j + 1) n))) _)
    lib_ring
  rw [i3]
  show K.add (K.mul x0 (K.add (K.mul x0 (cN K (dl 0 n))) _)) _ = K.add _ (K.add (K.mul x0 (K.add (K.mul x0 (cN K (dl 0 n))) _)) _)
  lib_ring

end TailSq

#print axioms TailSq.dot_fadd
#print axioms TailSq.dot_one
#print axioms TailSq.expand
#print axioms TailSq.cgc_cons

#print axioms: 4 theorems, no axioms.

90.105 v-good lists are log-concave

Every v-good list satisfies \(x_kx_{k+2}\le x_{k+1}^2\), by downward induction from the top.

\[ x_k\,x_{k+2}\le x_{k+1}^2\qquad (k\ge 0) \]

ym/VGoodLC.lean

/-
  ym/VGoodLC — T45, G69: v-good lists are log-concave.
  Downward induction from the top: VV at k, (x_k + x_{k+2})x_{k+2} ≤ (x_{k+1} + x_{k+3})x_{k+1}, together with
  x_{k+1}x_{k+3} ≤ x_{k+2}² gives x_k x_{k+2} ≤ x_{k+1}² (no division); above the list all entries vanish.
  Lean 4 core only, no axioms.
-/
import ym.TopLC

namespace VGoodLC

open YMChain SU2Char HeatPair MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- additive cancellation in an ordered ring -/
theorem cancel_add (a b c : R) (h : O.le (K.add a c) (K.add b c)) : O.le a b := by
  have s := O.add_le_add _ _ _ _ h (O.le_refl (K.neg c))
  rw [show K.add (K.add a c) (K.neg c) = a by lib_ring, show K.add (K.add b c) (K.neg c) = b by lib_ring] at s
  exact s

theorem lc_from_top (X : List R) (hg : L1Full.Good K O X) :
    ∀ (d k : Nat), X.length ≤ k + d →
      O.le (K.mul (co K X k) (co K X (k + 2))) (K.mul (co K X (k + 1)) (co K X (k + 1)))
  | 0, k, h => by
    have z : co K X (k + 2) = K.zero := HeatLR.co_out K X (k + 2) (Nat.le_trans h (Nat.le_add_right k 2))
    rw [z, show K.mul (co K X k) K.zero = K.zero by lib_ring]
    exact O.mul_nonneg _ _ (hg.1 (k + 1)) (hg.1 (k + 1))
  | d + 1, k, h => by
    have ih : O.le (K.mul (co K X (k + 1)) (co K X (k + 3))) (K.mul (co K X (k + 2)) (co K X (k + 2))) :=
      lc_from_top X hg d (k + 1) (Nat.le_trans h (Nat.le_of_eq (by lib_nat)))
    have vv : O.le (K.mul (K.add (co K X k) (co K X (k + 2))) (co K X (k + 2)))
        (K.mul (K.add (co K X (k + 1)) (co K X (k + 3))) (co K X (k + 1))) := hg.2.2.1 k
    -- x_k x_{k+2} + x_{k+2}^2 ≤ x_{k+1}^2 + x_{k+1} x_{k+3} ≤ x_{k+1}^2 + x_{k+2}^2
    have s := O.add_le_add _ _ _ _ vv ih
    apply cancel_add K O _ _ (K.add (K.mul (co K X (k + 2)) (co K X (k + 2))) (K.mul (co K X (k + 1)) (co K X (k + 3))))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_))
    · show _ = K.add (K.mul (K.add (co K X k) (co K X (k + 2))) (co K X (k + 2))) (K.mul (co K X (k + 1)) (co K X (k + 3)))
      lib_ring
    · show K.add (K.mul (K.add (co K X (k + 1)) (co K X (k + 3))) (co K X (k + 1))) (K.mul (co K X (k + 2)) (co K X (k + 2))) = _
      lib_ring

/-- **v-good lists are log-concave**: x_k x_{k+2} ≤ x_{k+1}² for every k -/
theorem good_lc (X : List R) (hg : L1Full.Good K O X) (k : Nat) :
    O.le (K.mul (co K X k) (co K X (k + 2))) (K.mul (co K X (k + 1)) (co K X (k + 1))) :=
  lc_from_top K O X hg X.length k (Nat.le_add_left _ _)

end VGoodLC

#print axioms VGoodLC.cancel_add
#print axioms VGoodLC.lc_from_top
#print axioms VGoodLC.good_lc

#print axioms: 3 theorems, no axioms.

90.106 Pair minors of v-good lists

Every pair minor \(D(u,v)=x_uy_v-x_vy_u\) (\(y=\chi _1x\)) of a v-good list is nonnegative.

\[ D(u,v)\ge 0\qquad (0\le u<v\le |X|) \]

ym/VGoodPair.lean

/-
  ym/VGoodPair — T45, G69: every pair minor of a v-good list is nonnegative.
  With y = chi_1 x (y_0 = x_1, y_{u+1} = x_u + x_{u+2}) and D(u,v) = x_u y_v - x_v y_u:
    adjacent D(u,u+1) >= 0 is exactly the base condition (u = 0) or VV (u >= 1);
    the Pluecker identity x_v D(u,v+1) = x_u D(v,v+1) + x_{v+1} D(u,v) and x_v > 0 (v < length)
  give D(u,v) >= 0 for all u < v <= length. These are the atoms of the CB-M certificates.
  Lean 4 core only, no axioms.
-/
import ym.VGoodLC

namespace VGoodPair

open YMChain SU2Char HeatPair MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- y = chi_1 x -/
def yv (X : List R) : Nat → R
  | 0 => co K X 1
  | u + 1 => K.add (co K X u) (co K X (u + 2))

/-- the pair minor D(u,v) = x_u y_v - x_v y_u -/
def dp (X : List R) (u v : Nat) : R :=
  K.add (K.mul (co K X u) (yv K X v)) (K.neg (K.mul (co K X v) (yv K X u)))

theorem adj_nn (X : List R) (hg : L1Full.Good K O X) : ∀ u, O.le K.zero (dp K X u (u + 1))
  | 0 => by
    have h := HeatV.sub_nn K O _ _ hg.2.2.2
    refine O.le_trans _ _ _ h (TripleE.le_of_eq' K O _ _ ?_)
    show _ = K.add (K.mul (co K X 0) (K.add (co K X 0) (co K X 2))) (K.neg (K.mul (co K X 1) (co K X 1)))
    lib_ring
  | m + 1 => by
    have h := HeatV.sub_nn K O _ _ (hg.2.2.1 m)
    refine O.le_trans _ _ _ h (TripleE.le_of_eq' K O _ _ ?_)
    show _ = K.add (K.mul (co K X (m + 1)) (K.add (co K X (m + 1)) (co K X (m + 3))))
      (K.neg (K.mul (co K X (m + 2)) (K.add (co K X m) (co K X (m + 2)))))
    lib_ring

/-- Pluecker: x_v D(u,v+1) = x_u D(v,v+1) + x_{v+1} D(u,v) -/
theorem pluecker (X : List R) (u v : Nat) :
    K.mul (dp K X u (v + 1)) (co K X v)
      = K.add (K.mul (co K X u) (dp K X v (v + 1))) (K.mul (co K X (v + 1)) (dp K X u v)) := by
  unfold dp
  lib_ring

/-- **pair minors of v-good lists**: D(u, u+k+1) >= 0 whenever u + k + 1 <= length -/
theorem pair_nn (X : List R) (hg : L1Full.Good K O X) :
    ∀ (k u : Nat), u + k + 1 ≤ X.length → O.le K.zero (dp K X u (u + k + 1))
  | 0, u, _ => adj_nn K O X hg u
  | k + 1, u, h => by
    have hv : u + k + 1 < X.length := h
    have ih := pair_nn X hg k u (Nat.le_of_lt hv)
    have ad := adj_nn K O X hg (u + k + 1)
    have xv0 := hg.1 (u + k + 1)
    have xvne := hg.2.1 (u + k + 1) hv
    have rhs : O.le K.zero (K.add (K.mul (co K X u) (dp K X (u + k + 1) (u + k + 1 + 1)))
        (K.mul (co K X (u + k + 1 + 1)) (dp K X u (u + k + 1)))) :=
      TopLC.add_nn K O _ _ (O.mul_nonneg _ _ (hg.1 u) ad) (O.mul_nonneg _ _ (hg.1 _) ih)
    rw [← pluecker K X u (u + k + 1)] at rhs
    apply O.pos_cancel _ _ _ xv0 xvne
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) rhs
    lib_ring

end VGoodPair

#print axioms VGoodPair.adj_nn
#print axioms VGoodPair.pluecker
#print axioms VGoodPair.pair_nn

#print axioms: 3 theorems, no axioms.

90.107 Tail minors of v-good lists

Every tail minor \(T(w,w')=x_wx_{w'-1}-x_{w-1}x_{w'}\) of a v-good list is nonnegative.

\[ T(w,w')\ge 0\ (w<w'),\qquad D(u,v)=T(u,v)-T(u+1,v+1) \]

ym/VGoodTail.lean

/-
  ym/VGoodTail — T45, G69: tail minors of a v-good list are nonnegative.
  With x_{-1} = 0 and T(w,w') = x_w x_{w'-1} - x_{w-1} x_{w'}:
    D(u,v) = T(u,v) - T(u+1,v+1)        (y_v = x_{v-1} + x_{v+1}),
    D(u,v) >= 0 for all u < v           (VGoodPair.pair_nn below the length, x_v = 0 above it),
    T(w,w') >= 0 for all w < w'         (T(w,w') = D(w,w') + T(w+1,w'+1), and T = 0 above the list).
  D and T are the two kinds of atoms of the closed-form CB-M certificates. Lean 4 core only, no axioms.
-/
import ym.VGoodPair

namespace VGoodTail

open YMChain SU2Char HeatPair MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- x_{w-1}, with x_{-1} = 0 -/
def cm (X : List R) : Nat → R
  | 0 => K.zero
  | w + 1 => co K X w

/-- the tail minor T(w,w') = x_w x_{w'-1} - x_{w-1} x_{w'} -/
def tt (X : List R) (w w' : Nat) : R :=
  K.add (K.mul (co K X w) (cm K X w')) (K.neg (K.mul (cm K X w) (co K X w')))

theorem yv_eq (X : List R) : ∀ v, VGoodPair.yv K X v = K.add (cm K X v) (co K X (v + 1))
  | 0 => by
    show co K X 1 = K.add K.zero (co K X 1)
    lib_ring
  | _ + 1 => rfl

/-- D(u,v) = T(u,v) - T(u+1,v+1) -/
theorem dp_eq (X : List R) (u v : Nat) :
    VGoodPair.dp K X u v = K.add (tt K X u v) (K.neg (tt K X (u + 1) (v + 1))) := by
  unfold VGoodPair.dp
  rw [yv_eq K X v, yv_eq K X u]
  show _ = K.add (K.add (K.mul (co K X u) (cm K X v)) (K.neg (K.mul (cm K X u) (co K X v))))
    (K.neg (K.add (K.mul (co K X (u + 1)) (co K X v)) (K.neg (K.mul (co K X u) (co K X (v + 1))))))
  lib_ring

theorem yv_nn (X : List R) (hg : L1Full.Good K O X) : ∀ v, O.le K.zero (VGoodPair.yv K X v)
  | 0 => hg.1 1
  | v + 1 => TopLC.add_nn K O _ _ (hg.1 v) (hg.1 (v + 2))

/-- D(u, u+k+1) >= 0 for every u, k -/
theorem pair_all (X : List R) (hg : L1Full.Good K O X) (u k : Nat) :
    O.le K.zero (VGoodPair.dp K X u (u + k + 1)) := by
  cases Nat.lt_or_ge X.length (u + k + 1) with
  | inl h =>
    have z : co K X (u + k + 1) = K.zero := HeatLR.co_out K X _ (Nat.le_of_lt h)
    unfold VGoodPair.dp
    rw [z]
    refine O.le_trans _ _ _ (O.mul_nonneg _ _ (hg.1 u) (yv_nn K O X hg (u + k + 1))) (TripleE.le_of_eq' K O _ _ ?_)
    lib_ring
  | inr h => exact VGoodPair.pair_nn K O X hg k u h

/-- T(w, w+k+1) >= 0, by induction on the distance m to the top -/
theorem tail_nn_aux (X : List R) (hg : L1Full.Good K O X) (k : Nat) :
    ∀ (m w : Nat), X.length + 1 ≤ w + m → O.le K.zero (tt K X w (w + k + 1))
  | 0, 0, h => absurd h (Nat.not_succ_le_zero _)
  | 0, w0 + 1, h => by
    have z1 : co K X (w0 + 1) = K.zero := HeatLR.co_out K X _ (Nat.le_trans (Nat.le_succ _) h)
    have z2 : co K X w0 = K.zero := HeatLR.co_out K X _ (Nat.le_of_succ_le_succ h)
    show O.le K.zero (K.add (K.mul (co K X (w0 + 1)) (cm K X (w0 + 1 + k + 1)))
      (K.neg (K.mul (co K X w0) (co K X (w0 + 1 + k + 1)))))
    rw [z1, z2]
    exact TripleE.le_of_eq' K O _ _ (by lib_ring)
  | m + 1, w, h => by
    have h' : X.length + 1 ≤ w + 1 + m := Nat.le_trans h (Nat.le_of_eq (by lib_nat))
    have ih := tail_nn_aux X hg k m (w + 1) h'
    rw [show w + 1 + k + 1 = w + k + 1 + 1 by lib_nat] at ih
    have dnn := pair_all K O X hg w k
    have e := dp_eq K X w (w + k + 1)
    refine O.le_trans _ _ _ (TopLC.add_nn K O _ _ dnn ih) (TripleE.le_of_eq' K O _ _ ?_)
    rw [e]
    lib_ring

/-- **tail minors of v-good lists**: T(w, w+k+1) >= 0 -/
theorem tail_nn (X : List R) (hg : L1Full.Good K O X) (w k : Nat) : O.le K.zero (tt K X w (w + k + 1)) :=
  tail_nn_aux K O X hg k (X.length + 1) w (Nat.le_add_left _ _)

end VGoodTail

#print axioms VGoodTail.yv_eq
#print axioms VGoodTail.dp_eq
#print axioms VGoodTail.pair_all
#print axioms VGoodTail.tail_nn_aux
#print axioms VGoodTail.tail_nn

#print axioms: 5 theorems, no axioms.

90.108 Christoffel–Darboux telescoping of the box sums

The nested box sums of the CB-M certificate telescope to one \(2\times 2\) determinant of window sums.

\[ \mathrm {box}(t,m+1)=2\Psi (t,m+1)-2\Psi (t+1,m),\quad \Psi (t,m)=W_{t+2,m}W_{t,m+1}-W_{t+1,m}W_{t+1,m+1} \]

ym/RhoCD.lean

/-
  ym/RhoCD — T45, G69b3a: the Christoffel–Darboux telescoping of the box sums.
  For any sequence rho and the step-2 windows Wn(lo,k) = sum_{i<k} rho(lo + 2i), the box quantity
    box(t,k) = 2 U0^2 + 2 U0 U_{-2} - 2 U_{-1} U_1 - 2 U_{-1}^2,   U_s = Wn(t+2+s, k)
  (the factorised form of sum_{a,b in B} F(a,b), B = {t+2, t+4, ..., t+2k}) telescopes:
    box(t, m+1) = 2 Psi(t, m+1) - 2 Psi(t+1, m),   Psi(t,m) = Wn(t+2,m) Wn(t,m+1) - Wn(t+1,m) Wn(t+1,m+1),
  hence the sum of the P nested boxes is 2 Psi(0,P) (Psi(t,0) = 0).
  Lean 4 core only, no axioms.
-/
import ym.VGoodTail

namespace RhoCD

open YMChain

variable {R : Type} (K : DetChain.CRing R)

/-- step-2 window sum Wn(lo,k) = rho(lo) + rho(lo+2) + ... (k terms) -/
def Wn (ρ : Nat → R) (lo : Nat) : Nat → R
  | 0 => K.zero
  | k + 1 => K.add (Wn ρ lo k) (ρ (lo + k + k))

theorem wn_first (ρ : Nat → R) : ∀ (lo k : Nat), Wn K ρ lo (k + 1) = K.add (ρ lo) (Wn K ρ (lo + 2) k)
  | lo, 0 => by
    show K.add K.zero (ρ lo) = K.add (ρ lo) K.zero
    lib_ring
  | lo, k + 1 => by
    show K.add (Wn K ρ lo (k + 1)) (ρ (lo + (k + 1) + (k + 1)))
      = K.add (ρ lo) (K.add (Wn K ρ (lo + 2) k) (ρ (lo + 2 + k + k)))
    rw [wn_first ρ lo k, show lo + (k + 1) + (k + 1) = lo + 2 + k + k by lib_nat]
    lib_ring

/-- shifting the start by 2: Wn(lo+2,k) = Wn(lo,k) + rho(lo+2k) - rho(lo) -/
theorem wn_shift (ρ : Nat → R) (lo k : Nat) :
    Wn K ρ (lo + 2) k = K.add (K.add (Wn K ρ lo k) (ρ (lo + k + k))) (K.neg (ρ lo)) := by
  have h1 := wn_first K ρ lo k
  have h2 : Wn K ρ lo (k + 1) = K.add (Wn K ρ lo k) (ρ (lo + k + k)) := rfl
  rw [h2] at h1
  have : Wn K ρ (lo + 2) k = K.add (K.add (ρ lo) (Wn K ρ (lo + 2) k)) (K.neg (ρ lo)) := by lib_ring
  rw [this, ← h1]

/-- the factorised box sum over B = {t+2, ..., t+2k} -/
def box (ρ : Nat → R) (t k : Nat) : R :=
  K.add (K.add (K.mul (K.add (Wn K ρ (t + 2) k) (Wn K ρ (t + 2) k)) (Wn K ρ (t + 2) k))
      (K.mul (K.add (Wn K ρ (t + 2) k) (Wn K ρ (t + 2) k)) (Wn K ρ t k)))
    (K.neg (K.add (K.mul (K.add (Wn K ρ (t + 1) k) (Wn K ρ (t + 1) k)) (Wn K ρ (t + 3) k))
      (K.mul (K.add (Wn K ρ (t + 1) k) (Wn K ρ (t + 1) k)) (Wn K ρ (t + 1) k))))

/-- the Christoffel–Darboux potential -/
def psi (ρ : Nat → R) (t m : Nat) : R :=
  K.add (K.mul (Wn K ρ (t + 2) m) (Wn K ρ t (m + 1))) (K.neg (K.mul (Wn K ρ (t + 1) m) (Wn K ρ (t + 1) (m + 1))))

theorem psi_zero (ρ : Nat → R) (t : Nat) : psi K ρ t 0 = K.zero := by
  show K.add (K.mul K.zero (Wn K ρ t 1)) (K.neg (K.mul K.zero (Wn K ρ (t + 1) 1))) = K.zero
  lib_ring

/-- **one telescoping step**: box(t, m+1) = 2 Psi(t, m+1) - 2 Psi(t+1, m) -/
theorem cd_step (ρ : Nat → R) (t m : Nat) :
    box K ρ t (m + 1) = K.add (K.add (psi K ρ t (m + 1)) (psi K ρ t (m + 1)))
      (K.neg (K.add (psi K ρ (t + 1) m) (psi K ρ (t + 1) m))) := by
  -- all windows in terms of E = Wn(t,m), O = Wn(t+1,m) and six boundary values
  have w8 : Wn K ρ (t + 2) m = K.add (K.add (Wn K ρ t m) (ρ (t + m + m))) (K.neg (ρ t)) := wn_shift K ρ t m
  have w7 : Wn K ρ (t + 3) m = K.add (K.add (Wn K ρ (t + 1) m) (ρ (t + 1 + m + m))) (K.neg (ρ (t + 1))) :=
    wn_shift K ρ (t + 1) m
  have w1 : Wn K ρ (t + 2) (m + 1) = K.add (Wn K ρ (t + 2) m) (ρ (t + m + m + 2)) := by
    show K.add (Wn K ρ (t + 2) m) (ρ (t + 2 + m + m)) = _
    rw [show t + 2 + m + m = t + m + m + 2 by lib_nat]
  have w4 : Wn K ρ (t + 3) (m + 1) = K.add (Wn K ρ (t + 3) m) (ρ (t + 1 + m + m + 2)) := by
    show K.add (Wn K ρ (t + 3) m) (ρ (t + 3 + m + m)) = _
    rw [show t + 3 + m + m = t + 1 + m + m + 2 by lib_nat]
  have w2 : Wn K ρ t (m + 1) = K.add (Wn K ρ t m) (ρ (t + m + m)) := rfl
  have w3 : Wn K ρ (t + 1) (m + 1) = K.add (Wn K ρ (t + 1) m) (ρ (t + 1 + m + m)) := rfl
  have w5 : Wn K ρ t (m + 1 + 1) = K.add (K.add (Wn K ρ t m) (ρ (t + m + m))) (ρ (t + m + m + 2)) := by
    show K.add (K.add (Wn K ρ t m) (ρ (t + m + m))) (ρ (t + (m + 1) + (m + 1))) = _
    rw [show t + (m + 1) + (m + 1) = t + m + m + 2 by lib_nat]
  have w6 : Wn K ρ (t + 1) (m + 1 + 1)
      = K.add (K.add (Wn K ρ (t + 1) m) (ρ (t + 1 + m + m))) (ρ (t + 1 + m + m + 2)) := by
    show K.add (K.add (Wn K ρ (t + 1) m) (ρ (t + 1 + m + m))) (ρ (t + 1 + (m + 1) + (m + 1))) = _
    rw [show t + 1 + (m + 1) + (m + 1) = t + 1 + m + m + 2 by lib_nat]
  have w9 : Wn K ρ (t + 1 + 2) m = Wn K ρ (t + 3) m := rfl
  have w10 : Wn K ρ (t + 1 + 1) m = Wn K ρ (t + 2) m := rfl
  have w11 : Wn K ρ (t + 1 + 1) (m + 1) = Wn K ρ (t + 2) (m + 1) := rfl
  unfold box psi
  rw [w9, w10, w11, w1, w4, w5, w6, w2, w3, w8, w7]
  lib_ring

/-- **nested boxes**: sum_{j=1}^{P} box(P-j, j) = 2 Psi(0, P), written as a recursion over the number of boxes:
    the boxes of sizes 1..m with starts s+m-1, ..., s sum to 2 Psi(s, m) -/
def boxes (ρ : Nat → R) : Nat → Nat → R
  | _, 0 => K.zero
  | s, m + 1 => K.add (box K ρ s (m + 1)) (boxes ρ (s + 1) m)

theorem boxes_eq (ρ : Nat → R) : ∀ (m s : Nat), boxes K ρ s m = K.add (psi K ρ s m) (psi K ρ s m)
  | 0, s => by
    show K.zero = K.add (psi K ρ s 0) (psi K ρ s 0)
    rw [psi_zero]; lib_ring
  | m + 1, s => by
    show K.add (box K ρ s (m + 1)) (boxes K ρ (s + 1) m) = _
    rw [boxes_eq ρ m (s + 1), cd_step]
    lib_ring

end RhoCD

#print axioms RhoCD.wn_first
#print axioms RhoCD.wn_shift
#print axioms RhoCD.psi_zero
#print axioms RhoCD.cd_step
#print axioms RhoCD.boxes_eq

#print axioms: 5 theorems, no axioms.

90.109 CG step by the reflected correlation

The Clebsch–Gordan sums step by two through a correlation minus a convolution.

\[ c_{n+2}+\sum _{j+k=n}x_jx_k=c_n+\sum _{|j-k|=n+2}x_jx_k \]

ym/RhoC.lean

/-
  ym/RhoC — T45, G69b3b: the CG coefficients step by the reflected correlation.
  Pointwise:  cg(j,k,n+2) + [j+k = n] = cg(j,k,n) + [|j-k| = n+2]   (from CgSplit.cg_split and lp_split),
  hence for every list X
    c_{n+2}(X) + s_n(X) = c_n(X) + d_{n+2}(X),
  s_n = sum_{j+k=n} x_j x_k (convolution), d_m = sum_{|j-k|=m} x_j x_k (= 2 r_m).
  With the odd reflection x~_{-k-2} = -x_k the right side difference d_{n+2} - s_n is the reflected
  correlation rho(n+2), so c_n = (1/2) sum_{|m| <= n, m = n mod 2} rho(m). Lean 4 core only, no axioms.
-/
import ym.RhoCD
import ym.CgSplit

namespace RhoC

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit

variable {R : Type} (K : DetChain.CRing R)

/-- the bilinear sum sum_{j,k} w(j,k) x_j x_k -/
def bil (X : List R) (w : Nat → Nat → Nat) : R := dot K 0 X (fun j => dot K 0 X (fun k => cN K (w j k)))

theorem cgc_bil (X : List R) (n : Nat) : cgc K X X n = bil K X (fun j k => cg j k n) := rfl

theorem lin (a c e f g h d : Nat) (h1 : c + d = e + f) (h2 : a + g = f) (h3 : d = h + g) : a + e = c + h := by
  have s : a + e + g = c + h + g := by
    calc a + e + g = e + (a + g) := by lib_nat
      _ = e + f := by rw [h2]
      _ = c + d := h1.symm
      _ = c + (h + g) := by rw [h3]
      _ = c + h + g := by lib_nat
  exact L1Full.cancel_add_r g _ _ s

/-- **pointwise step**: cg(j,k,n+2) + [j+k = n] = cg(j,k,n) + [|j-k| = n+2] -/
theorem cg_step (j k n : Nat) : cg j k (n + 2) + dl n (j + k) = cg j k n + dl (n + 2) (dist j k) := by
  have s0 := cg_split j k n
  have s2 := cg_split j k (n + 2)
  have p0 := lp_split n (j + k)
  have p2 := lp_split (n + 2) (dist j k)
  rw [p0] at s0
  exact lin (cg j k (n + 2)) (cg j k n) (dl n (j + k)) (lp (n + 2) (j + k)) (lp (n + 2 + 2) (dist j k))
    (dl (n + 2) (dist j k)) (lp (n + 2) (dist j k)) s0 s2 p2

theorem bil_add (X : List R) (v w : Nat → Nat → Nat) :
    K.add (bil K X v) (bil K X w) = bil K X (fun j k => v j k + w j k) := by
  unfold bil
  rw [← dot_fadd K]
  apply dot_ext K
  intro j
  dsimp only
  rw [← dot_fadd K]
  apply dot_ext K
  intro k
  exact (cN_add K (v j k) (w j k)).symm

/-- **c_{n+2} + s_n = c_n + d_{n+2}** -/
theorem cgc_step (X : List R) (n : Nat) :
    K.add (cgc K X X (n + 2)) (bil K X (fun j k => dl n (j + k)))
      = K.add (cgc K X X n) (bil K X (fun j k => dl (n + 2) (dist j k))) := by
  rw [cgc_bil, cgc_bil, bil_add, bil_add]
  unfold bil
  apply dot_ext K
  intro j
  apply dot_ext K
  intro k
  dsimp only
  rw [cg_step j k n]

end RhoC

#print axioms RhoC.lin
#print axioms RhoC.cg_step
#print axioms RhoC.bil_add
#print axioms RhoC.cgc_step

#print axioms: 4 theorems, no axioms.

90.110 CB-M at \(N=2\)

CB-M holds at \(N=2\) for every v-good list of every length, by Cauchy–Binet.

\[ c_2^2-c_1c_3-M_2^2=4\sum _{i<j}D(i,j)\,T(i+2,j+2)\ge 0 \]

ym/CBM2.lean

/-
  ym/CBM2 — T45, G69b3: CB-M at N = 2 for every v-good list, by Cauchy–Binet (prototype of the general case).
    c_2^2 - c_1 c_3 - M_2^2 = 4 sum_{i<j} D(i,j) T(i+2,j+2)
                            = 4 ( <x,H><y,H^-> - <x,H^-><y,H> ),   H_i = x_{i+2}, H^-_i = x_{i+1}, y = chi_1 x,
  with c_0 = r_0, c_1 = 2 r_1, c_2 + s_0 = c_0 + 2 r_2, c_3 + s_1 = c_1 + 2 r_3 (RhoC, Corr), M_2 = r_0 - x_0^2;
  D >= 0 (VGoodTail.pair_all), T >= 0 (VGoodTail.tail_nn), CauchyBinet2.cb_nonneg.
  Lean 4 core only, no axioms.
-/
import ym.Corr
import ym.CauchyBinet2
import ym.ReduceCBM

namespace CBM2

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom

variable {R : Type} (K : DetChain.CRing R)

theorem sum_zero : ∀ N, sumN K (fun _ => K.zero) N = K.zero
  | 0 => rfl
  | N + 1 => by
    show K.add (sumN K (fun _ => K.zero) N) K.zero = K.zero
    rw [sum_zero N]; lib_ring

/-- the list correlation as a finite sum -/
theorem rr_sum (m : Nat) : ∀ (X : List R) (N : Nat), X.length ≤ N →
    Corr.rr K m X = sumN K (fun i => K.mul (co K X i) (co K X (i + m))) N
  | [], N, _ => by
    rw [sumN_ext K _ (fun _ => K.zero) (fun i => by show K.mul K.zero (co K [] (i + m)) = K.zero; lib_ring) N, sum_zero]
    rfl
  | _ :: _, 0, h => absurd h (Nat.not_succ_le_zero _)
  | x0 :: Y, N + 1, h => by
    have ih := rr_sum m Y N (Nat.le_of_succ_le_succ h)
    rw [sumN_shift K]
    have e : sumN K (fun k => K.mul (co K (x0 :: Y) (k + 1)) (co K (x0 :: Y) (k + 1 + m))) N
        = sumN K (fun i => K.mul (co K Y i) (co K Y (i + m))) N := by
      apply sumN_ext K
      intro k
      rw [show k + 1 + m = k + m + 1 by lib_nat]
      rfl
    have e0 : co K (x0 :: Y) (0 + m) = co K (x0 :: Y) m := by rw [Nat.zero_add]
    show K.add (Corr.rr K m Y) (K.mul x0 (co K (x0 :: Y) m))
      = K.add (K.mul x0 (co K (x0 :: Y) (0 + m))) (sumN K (fun k => K.mul (co K (x0 :: Y) (k + 1)) (co K (x0 :: Y) (k + 1 + m))) N)
    rw [e, e0, ← ih]
    lib_ring

theorem bil_ext (X : List R) (v w : Nat → Nat → Nat) (h : ∀ j k, v j k = w j k) :
    RhoC.bil K X v = RhoC.bil K X w := by
  unfold RhoC.bil
  apply dot_ext K; intro j
  apply dot_ext K; intro k
  rw [h j k]

theorem eq_of_add (a b c : R) (h : K.add a b = c) : a = K.add c (K.neg b) := by
  rw [← h]; lib_ring

/-- s_0(x0 :: Y) = x0^2 -/
theorem conv0 (x0 : R) (Y : List R) :
    RhoC.bil K (x0 :: Y) (fun j k => dl 0 (j + k)) = K.mul x0 x0 := by
  have ex := TailSq.expand K x0 Y (fun j s => cN K (dl 0 (j + s)))
  have z1 : dot K 0 Y (fun s => cN K (dl 0 (0 + (s + 1)))) = K.zero := dot_fzero K 0 Y
  have z2 : dot K 0 Y (fun j => K.add (K.mul x0 (cN K (dl 0 (j + 1 + 0))))
      (dot K 0 Y (fun s => cN K (dl 0 (j + 1 + (s + 1)))))) = K.zero := by
    have : dot K 0 Y (fun j => K.add (K.mul x0 (cN K (dl 0 (j + 1 + 0))))
        (dot K 0 Y (fun s => cN K (dl 0 (j + 1 + (s + 1)))))) = dot K 0 Y (fun _ => K.zero) := by
      apply dot_ext K; intro j
      show K.add (K.mul x0 K.zero) (dot K 0 Y (fun _ => K.zero)) = K.zero
      rw [dot_fzero K 0 Y]; lib_ring
    rw [this, dot_fzero K 0 Y]
  show dot K 0 (x0 :: Y) (fun j => dot K 0 (x0 :: Y) (fun s => cN K (dl 0 (j + s)))) = _
  rw [ex, z1, z2]
  show K.add (K.mul x0 (K.add (K.mul x0 (K.add K.one K.zero)) K.zero)) K.zero = K.mul x0 x0
  lib_ring

/-- s_1(x0 :: Y) = 2 x0 x1 -/
theorem conv1 (x0 : R) (Y : List R) :
    RhoC.bil K (x0 :: Y) (fun j k => dl 1 (j + k)) = K.add (K.mul x0 (co K Y 0)) (K.mul x0 (co K Y 0)) := by
  have ex := TailSq.expand K x0 Y (fun j s => cN K (dl 1 (j + s)))
  have z1 : dot K 0 Y (fun s => cN K (dl 1 (0 + (s + 1)))) = co K Y 0 := by
    rw [← HeatPair.dot_dl K Y 0]
    apply dot_ext K; intro s
    rw [Nat.zero_add]; rfl
  have z2 : dot K 0 Y (fun j => K.add (K.mul x0 (cN K (dl 1 (j + 1 + 0))))
      (dot K 0 Y (fun s => cN K (dl 1 (j + 1 + (s + 1)))))) = K.mul x0 (co K Y 0) := by
    have : dot K 0 Y (fun j => K.add (K.mul x0 (cN K (dl 1 (j + 1 + 0))))
        (dot K 0 Y (fun s => cN K (dl 1 (j + 1 + (s + 1)))))) = dot K 0 Y (fun j => K.mul x0 (cN K (dl 0 j))) := by
      apply dot_ext K; intro j
      have zz : dot K 0 Y (fun s => cN K (dl 1 (j + 1 + (s + 1)))) = K.zero := by
        rw [← dot_fzero K 0 Y]
        apply dot_ext K; intro s
        rw [show j + 1 + (s + 1) = j + s + 1 + 1 by lib_nat]; rfl
      rw [zz]
      show K.add (K.mul x0 (cN K (dl 0 j))) K.zero = K.mul x0 (cN K (dl 0 j))
      lib_ring
    rw [this, dot_fsmul K (fun j => cN K (dl 0 j)) x0 0 Y, HeatPair.dot_dl K Y 0]
  show dot K 0 (x0 :: Y) (fun j => dot K 0 (x0 :: Y) (fun s => cN K (dl 1 (j + s)))) = _
  rw [ex, z1, z2]
  show K.add (K.mul x0 (K.add (K.mul x0 K.zero) (co K Y 0))) (K.mul x0 (co K Y 0)) = _
  lib_ring

/-- M_2 + x0^2 = r_0 -/
theorem mt_r0 : ∀ (X : List R), K.add (ReduceCBM.Mt K 1 0 X) (K.mul (co K X 0) (co K X 0)) = Corr.rr K 0 X
  | [] => by show K.add K.zero (K.mul K.zero K.zero) = K.zero; lib_ring
  | x0 :: Y => by
    have ih := mt_r0 Y
    show K.add (K.add (ReduceCBM.Mt K 1 0 Y) (K.mul (co K Y 0) (co K Y 0))) (K.mul x0 x0)
      = K.add (Corr.rr K 0 Y) (K.mul x0 x0)
    rw [ih]

theorem lt_dest : ∀ (i j : Nat), i < j → ∃ k, j = i + k + 1
  | _, 0, h => absurd h (Nat.not_lt_zero _)
  | 0, j + 1, _ => ⟨j, by lib_nat⟩
  | i + 1, j + 1, h => by
    obtain ⟨k, hk⟩ := lt_dest i j (Nat.lt_of_succ_lt_succ h)
    exact ⟨k, by rw [hk]; lib_nat⟩

variable (O : ORing K)

theorem atom_nn (X : List R) (hg : L1Full.Good K O X) (i k : Nat) :
    O.le K.zero (K.mul (Lagrange.minor K (co K X) (VGoodPair.yv K X) i (i + k + 1))
      (Lagrange.minor K (fun i => co K X (i + 2)) (fun i => co K X (i + 1)) i (i + k + 1))) := by
  have d := VGoodTail.pair_all K O X hg i k
  have t := VGoodTail.tail_nn K O X hg (i + 2) k
  have e1 : Lagrange.minor K (co K X) (VGoodPair.yv K X) i (i + k + 1) = VGoodPair.dp K X i (i + k + 1) := rfl
  have e2 : Lagrange.minor K (fun i => co K X (i + 2)) (fun i => co K X (i + 1)) i (i + k + 1)
      = VGoodTail.tt K X (i + 2) (i + 2 + k + 1) := by
    show K.add (K.mul (co K X (i + 2)) (co K X (i + k + 1 + 1))) (K.neg (K.mul (co K X (i + k + 1 + 2)) (co K X (i + 1))))
      = K.add (K.mul (co K X (i + 2)) (co K X (i + 2 + k))) (K.neg (K.mul (co K X (i + 1)) (co K X (i + 2 + k + 1))))
    rw [show i + k + 1 + 1 = i + 2 + k by lib_nat, show i + k + 1 + 2 = i + 2 + k + 1 by lib_nat]
    lib_ring
  rw [e1, e2]
  exact O.mul_nonneg _ _ d t

/-- **CB-M at N = 2**: M_2^2 <= c_2^2 - c_1 c_3 for v-good lists -/
theorem cbm2 (X : List R) (hg : L1Full.Good K O X) :
    O.le (K.mul (ReduceCBM.Mt K 1 0 X) (ReduceCBM.Mt K 1 0 X))
      (LibRing.sub K (K.mul (cgc K X X 2) (cgc K X X 2)) (K.mul (cgc K X X 1) (cgc K X X 3))) := by
  cases X with
  | nil =>
    show O.le (K.mul K.zero K.zero) (LibRing.sub K (K.mul K.zero K.zero) (K.mul K.zero K.zero))
    exact TripleE.le_of_eq' K O _ _ (by unfold LibRing.sub; lib_ring)
  | cons x0 Y =>
    have hcb := CauchyBinet2.cb_nonneg K O (co K (x0 :: Y)) (VGoodPair.yv K (x0 :: Y))
      (fun i => co K (x0 :: Y) (i + 2)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1) (fun i j hij => by
        obtain ⟨k, hk⟩ := lt_dest i j hij
        subst hk
        exact atom_nn K O (x0 :: Y) hg i k)
    have hl : (x0 :: Y).length ≤ Y.length + 3 := Nat.succ_le_succ (Nat.le_add_right _ 2)
    have hl4 : (x0 :: Y).length ≤ Y.length + 3 + 1 := Nat.le_succ_of_le hl
    have hY : Y.length ≤ Y.length + 3 + 1 := Nat.le_add_right _ 4
    -- the four inner products
    have hip1 : CauchyBinet2.ip K (co K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 2)) (Y.length + 3 + 1)
        = Corr.rr K 2 (x0 :: Y) := (rr_sum K 2 (x0 :: Y) _ hl4).symm
    have hip2 : CauchyBinet2.ip K (co K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1)
        = Corr.rr K 1 (x0 :: Y) := (rr_sum K 1 (x0 :: Y) _ hl4).symm
    have hip3 : CauchyBinet2.ip K (VGoodPair.yv K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1)
        = K.add (Corr.rr K 2 (x0 :: Y)) (Corr.rr K 0 Y) := by
      unfold CauchyBinet2.ip
      rw [sumN_ext K _ (fun i => K.add (K.mul (VGoodTail.cm K (x0 :: Y) i) (co K (x0 :: Y) (i + 1)))
          (K.mul (co K (x0 :: Y) (i + 1)) (co K (x0 :: Y) (i + 1))))
          (fun i => by
            show K.mul (VGoodPair.yv K (x0 :: Y) i) (co K (x0 :: Y) (i + 1)) = _
            rw [VGoodTail.yv_eq]; lib_ring), sumN_add K, sumN_shift K]
      have a : sumN K (fun k => K.mul (VGoodTail.cm K (x0 :: Y) (k + 1)) (co K (x0 :: Y) (k + 1 + 1))) (Y.length + 3)
          = Corr.rr K 2 (x0 :: Y) := (rr_sum K 2 (x0 :: Y) _ hl).symm
      have b : sumN K (fun i => K.mul (co K (x0 :: Y) (i + 1)) (co K (x0 :: Y) (i + 1))) (Y.length + 3 + 1)
          = Corr.rr K 0 Y := (rr_sum K 0 Y _ hY).symm
      rw [a, b]
      show K.add (K.add (K.mul K.zero (co K (x0 :: Y) (0 + 1))) (Corr.rr K 2 (x0 :: Y))) (Corr.rr K 0 Y) = _
      lib_ring
    have hip4 : CauchyBinet2.ip K (VGoodPair.yv K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 2)) (Y.length + 3 + 1)
        = K.add (Corr.rr K 3 (x0 :: Y)) (Corr.rr K 1 Y) := by
      unfold CauchyBinet2.ip
      rw [sumN_ext K _ (fun i => K.add (K.mul (VGoodTail.cm K (x0 :: Y) i) (co K (x0 :: Y) (i + 2)))
          (K.mul (co K (x0 :: Y) (i + 1)) (co K (x0 :: Y) (i + 2))))
          (fun i => by
            show K.mul (VGoodPair.yv K (x0 :: Y) i) (co K (x0 :: Y) (i + 2)) = _
            rw [VGoodTail.yv_eq]; lib_ring), sumN_add K, sumN_shift K]
      have a : sumN K (fun k => K.mul (VGoodTail.cm K (x0 :: Y) (k + 1)) (co K (x0 :: Y) (k + 1 + 2))) (Y.length + 3)
          = Corr.rr K 3 (x0 :: Y) := (rr_sum K 3 (x0 :: Y) _ hl).symm
      have b : sumN K (fun i => K.mul (co K (x0 :: Y) (i + 1)) (co K (x0 :: Y) (i + 2))) (Y.length + 3 + 1)
          = Corr.rr K 1 Y := (rr_sum K 1 Y _ hY).symm
      rw [a, b]
      show K.add (K.add (K.mul K.zero (co K (x0 :: Y) (0 + 2))) (Corr.rr K 3 (x0 :: Y))) (Corr.rr K 1 Y) = _
      lib_ring
    -- the CG sums through correlations
    have hc0 : cgc K (x0 :: Y) (x0 :: Y) 0 = Corr.rr K 0 (x0 :: Y) :=
      (bil_ext K (x0 :: Y) _ _ (fun j k => Corr.cg0 j k)).trans (Corr.dist0_bil K (x0 :: Y))
    have hc1 : cgc K (x0 :: Y) (x0 :: Y) 1 = K.add (Corr.rr K 1 (x0 :: Y)) (Corr.rr K 1 (x0 :: Y)) :=
      (bil_ext K (x0 :: Y) _ _ (fun j k => Corr.cg1 j k)).trans (Corr.dist_bil K 0 (x0 :: Y))
    have hd2 : RhoC.bil K (x0 :: Y) (fun j k => dl (0 + 2) (dist j k))
        = K.add (Corr.rr K 2 (x0 :: Y)) (Corr.rr K 2 (x0 :: Y)) := Corr.dist_bil K 1 (x0 :: Y)
    have hd3 : RhoC.bil K (x0 :: Y) (fun j k => dl (1 + 2) (dist j k))
        = K.add (Corr.rr K 3 (x0 :: Y)) (Corr.rr K 3 (x0 :: Y)) := Corr.dist_bil K 2 (x0 :: Y)
    have st0 : K.add (cgc K (x0 :: Y) (x0 :: Y) 2) (RhoC.bil K (x0 :: Y) (fun j k => dl 0 (j + k)))
        = K.add (cgc K (x0 :: Y) (x0 :: Y) 0) (RhoC.bil K (x0 :: Y) (fun j k => dl (0 + 2) (dist j k))) :=
      RhoC.cgc_step K (x0 :: Y) 0
    have st1 : K.add (cgc K (x0 :: Y) (x0 :: Y) 3) (RhoC.bil K (x0 :: Y) (fun j k => dl 1 (j + k)))
        = K.add (cgc K (x0 :: Y) (x0 :: Y) 1) (RhoC.bil K (x0 :: Y) (fun j k => dl (1 + 2) (dist j k))) :=
      RhoC.cgc_step K (x0 :: Y) 1
    rw [conv0, hd2, hc0] at st0
    rw [conv1, hd3, hc1] at st1
    have e2 := eq_of_add K _ _ _ st0
    have e3 := eq_of_add K _ _ _ st1
    have hm : K.add (ReduceCBM.Mt K 1 0 (x0 :: Y)) (K.mul x0 x0) = Corr.rr K 0 (x0 :: Y) := mt_r0 K (x0 :: Y)
    have eM := eq_of_add K _ _ _ hm
    have hr0 : Corr.rr K 0 (x0 :: Y) = K.add (Corr.rr K 0 Y) (K.mul x0 x0) := rfl
    have hr1 : Corr.rr K 1 (x0 :: Y) = K.add (Corr.rr K 1 Y) (K.mul x0 (co K Y 0)) := rfl
    have key : LibRing.sub K (K.mul (cgc K (x0 :: Y) (x0 :: Y) 2) (cgc K (x0 :: Y) (x0 :: Y) 2))
        (K.mul (cgc K (x0 :: Y) (x0 :: Y) 1) (cgc K (x0 :: Y) (x0 :: Y) 3))
        = K.add (K.mul (ReduceCBM.Mt K 1 0 (x0 :: Y)) (ReduceCBM.Mt K 1 0 (x0 :: Y)))
          (K.add (K.add (K.add (K.mul (CauchyBinet2.ip K (co K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 2)) (Y.length + 3 + 1))
              (CauchyBinet2.ip K (VGoodPair.yv K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1)))
              (K.neg (K.mul (CauchyBinet2.ip K (co K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1))
                (CauchyBinet2.ip K (VGoodPair.yv K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 2)) (Y.length + 3 + 1)))))
            (K.add (K.mul (CauchyBinet2.ip K (co K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 2)) (Y.length + 3 + 1))
              (CauchyBinet2.ip K (VGoodPair.yv K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1)))
              (K.neg (K.mul (CauchyBinet2.ip K (co K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1))
                (CauchyBinet2.ip K (VGoodPair.yv K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 2)) (Y.length + 3 + 1))))))
          (K.add (K.add (K.mul (CauchyBinet2.ip K (co K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 2)) (Y.length + 3 + 1))
              (CauchyBinet2.ip K (VGoodPair.yv K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1)))
              (K.neg (K.mul (CauchyBinet2.ip K (co K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1))
                (CauchyBinet2.ip K (VGoodPair.yv K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 2)) (Y.length + 3 + 1)))))
            (K.add (K.mul (CauchyBinet2.ip K (co K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 2)) (Y.length + 3 + 1))
              (CauchyBinet2.ip K (VGoodPair.yv K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1)))
              (K.neg (K.mul (CauchyBinet2.ip K (co K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 1)) (Y.length + 3 + 1))
                (CauchyBinet2.ip K (VGoodPair.yv K (x0 :: Y)) (fun i => co K (x0 :: Y) (i + 2)) (Y.length + 3 + 1))))))) := by
      rw [e2, e3, hc1, eM, hip1, hip2, hip3, hip4, hr0, hr1]
      unfold LibRing.sub
      show _ = _
      lib_ring
    rw [key]
    have c2 := TopLC.add_nn K O _ _ hcb hcb
    have c4 := TopLC.add_nn K O _ _ c2 c2
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.add_le_add _ _ _ _ (O.le_refl _) c4)
    lib_ring

end CBM2

#print axioms CBM2.sum_zero
#print axioms CBM2.rr_sum
#print axioms CBM2.conv0
#print axioms CBM2.conv1
#print axioms CBM2.mt_r0
#print axioms CBM2.lt_dest
#print axioms CBM2.atom_nn
#print axioms CBM2.cbm2

#print axioms: 8 theorems, no axioms.

90.111 Reduction of (A) to one inequality

For a v-good list, (A) at index \(n=N-1\) follows from \(c_N^2-c_{N-1}c_{N+1}\ge M_N^2\), where \(M_N\) is the sum of the middle products of the tails.

\[ (w^2-1)c_N^2-w^2c_{N-1}c_{N+1}=w^2\bigl (c_N^2-c_{N-1}c_{N+1}-M_N^2\bigr )+\bigl (w^2M_N^2-c_N^2\bigr ),\quad w=N+1 \]

ym/ReduceCBM.lean

/-
  ym/ReduceCBM — T45, G69: (A) at every index follows from the single inequality CB-M.
  With M_N(X) = Σ_i x_{i+P} x_{i+P+e} (N = 2P + e, e ≤ 1; the middle products of the tails):
    (i)  c_N ≤ (N+1) M_N           (TailSq.cgc_cons + MidSq.mid, induction over the tails),
    (ii) if c_N² − c_{N−1}c_{N+1} ≥ M_N² (CB-M), then with w = N + 1,
         (w² − 1)c_N² − w² c_{N−1}c_{N+1} = w²(c_N² − c_{N−1}c_{N+1} − M_N²) + (w²M_N² − c_N²) ≥ 0,
  which is (A) at n = N − 1. Lean 4 core only, no axioms.
-/
import ym.MidSq
import ym.TopLC
import ym.HeatL1All

namespace ReduceCBM

open YMChain SU2Haar SU2Char Fusion MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- the sum of the middle products of the tails -/
def Mt (P e : Nat) : List R → R
  | [] => K.zero
  | x0 :: Y => K.add (Mt P e Y) (K.mul (co K (x0 :: Y) P) (co K (x0 :: Y) (P + e)))

theorem Mt_nn (P e : Nat) : ∀ (X : List R), L1Full.Good K O X → O.le K.zero (Mt K P e X)
  | [], _ => O.le_refl _
  | x0 :: Y, hg => TopLC.add_nn K O _ _ (Mt_nn P e Y (AInd.good_tail K O x0 Y hg)) (O.mul_nonneg _ _ (hg.1 _) (hg.1 _))

/-- **c_N ≤ (N+1) M_N** for v-good lists -/
theorem cgc_le (P e : Nat) (he : e ≤ 1) : ∀ (X : List R), L1Full.Good K O X →
    O.le (cgc K X X (P + P + e)) (K.mul (LibRing.castN K (P + P + e + 1)) (Mt K P e X))
  | [], _ => by
    show O.le K.zero (K.mul _ K.zero)
    rw [show K.mul (LibRing.castN K (P + P + e + 1)) K.zero = K.zero by lib_ring]; exact O.le_refl _
  | x0 :: Y, hg => by
    rw [TailSq.cgc_cons K x0 Y (P + P + e)]
    have ih := cgc_le P e he Y (AInd.good_tail K O x0 Y hg)
    have m := MidSq.mid K O e he P (x0 :: Y) hg
    have s := O.add_le_add _ _ _ _ ih m
    refine O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ ?_)
    show _ = K.mul (LibRing.castN K (P + P + e + 1)) (K.add (Mt K P e Y) (K.mul (co K (x0 :: Y) P) (co K (x0 :: Y) (P + e))))
    lib_ring

/-- **(A) at n from CB-M at N = n + 1 = 2P + e** -/
theorem a_of_cbm (X : List R) (hg : L1Full.Good K O X) (n P e : Nat) (he : e ≤ 1) (hN : n + 1 = P + P + e)
    (hcb : O.le (K.mul (Mt K P e X) (Mt K P e X))
      (LibRing.sub K (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1))) (K.mul (cgc K X X n) (cgc K X X (n + 2))))) :
    O.le (K.mul (LibRing.castN K ((n + 2) * (n + 2))) (K.mul (cgc K X X n) (cgc K X X (n + 2))))
      (K.mul (LibRing.castN K ((n + 1) * (n + 3))) (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1)))) := by
  have h1 := TopLC.one_nn K O (O.le_total K.zero K.one)
  have hX := hg
  have hc : ∀ s, O.le K.zero (cgc K X X s) := by
    cases X with
    | nil => intro s; exact O.le_refl _
    | cons x Y => exact fun s => HeatL1All.cgc_nn K O (x :: Y) (x :: Y) Y.length rfl hX.1 hX.1 s
  -- a ≤ w M with w = castN (n + 2)
  have hle : O.le (cgc K X X (n + 1)) (K.mul (LibRing.castN K (n + 2)) (Mt K P e X)) := by
    have g := cgc_le K O P e he X hg
    rw [← hN] at g
    exact g
  have hM := Mt_nn K O P e X hg
  have hw : O.le K.zero (LibRing.castN K (n + 2)) := SU2CharExp.cN_nn K O h1 _
  -- a·a ≤ (wM)·(wM)
  have sq1 := O.mul_le_mul_right _ _ (cgc K X X (n + 1)) hle (hc (n + 1))
  have sq2 := O.mul_le_mul_right _ _ (K.mul (LibRing.castN K (n + 2)) (Mt K P e X)) hle (O.mul_nonneg _ _ hw hM)
  have hsq : O.le (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1)))
      (K.mul (K.mul (LibRing.castN K (n + 2)) (Mt K P e X)) (K.mul (LibRing.castN K (n + 2)) (Mt K P e X))) := by
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ sq1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
      (O.le_trans _ _ _ sq2 (TripleE.le_of_eq' K O _ _ ?_))))
    · lib_ring
    · lib_ring
    · lib_ring
  have d1 := HeatV.sub_nn K O _ _ hcb
  have d2 := HeatV.sub_nn K O _ _ hsq
  have hww : O.le K.zero (K.mul (LibRing.castN K (n + 2)) (LibRing.castN K (n + 2))) := O.mul_nonneg _ _ hw hw
  have nat1 : (n + 1) * (n + 3) + 1 = (n + 2) * (n + 2) := by lib_nat
  have hW : K.add (LibRing.castN K ((n + 1) * (n + 3))) (LibRing.castN K 1)
      = K.mul (LibRing.castN K (n + 2)) (LibRing.castN K (n + 2)) := by
    rw [← RingZ.castN_add K ((n + 1) * (n + 3)) 1, nat1, RingZ.castN_mul K (n + 2) (n + 2)]
  have hW2 : LibRing.castN K ((n + 2) * (n + 2)) = K.mul (LibRing.castN K (n + 2)) (LibRing.castN K (n + 2)) :=
    RingZ.castN_mul K (n + 2) (n + 2)
  have hW1 : LibRing.castN K ((n + 1) * (n + 3))
      = LibRing.sub K (K.mul (LibRing.castN K (n + 2)) (LibRing.castN K (n + 2))) K.one := by
    rw [← hW]; show _ = LibRing.sub K (K.add _ (K.add K.one K.zero)) K.one; lib_ring
  rw [hW2, hW1]
  apply TopLC.le_of_sub_nn K O
  have t := TopLC.add_nn K O _ _ (O.mul_nonneg _ _ hww d1) d2
  refine O.le_trans _ _ _ t (TripleE.le_of_eq' K O _ _ ?_)
  lib_ring

end ReduceCBM

#print axioms ReduceCBM.Mt_nn
#print axioms ReduceCBM.cgc_le
#print axioms ReduceCBM.a_of_cbm

#print axioms: 11 theorems (MidSq, ReduceCBM), no axioms.

90.112 Block-spin reflection positivity for the flowed weight

The flowed plaquette weight is an explicit Gram kernel with nonnegative coefficients, so the block step keeps reflection positivity.

\[ \ker (LW)(x,y)=s+s\sum _{n\ge 1}cf_n\chi _n(2x\cdot y),\qquad \sum _{x,y}G(Bx)G(By)\,\ker (LW)(x,y)\ge 0 \]

ym/RPGram.lean

/-
  ym/RPGram — T45, G42: one Gram list for the whole Migdal-flow plaquette weight. For s ≥ 0 and cf ≥ 0 the list
  LW = [(s, 1)] ++ Σ_n (s·cf_n) · LK_n has nonnegative coefficients and its kernel is s + s·Σ_{n≥1} cf_n χ_n(2x·y) on unit
  quaternions; hence YMChain.block_rp applies: the block-spin step preserves reflection positivity for flowed weights.
  Lean 4 core only, no axioms.
-/
import ym.RPFlow
import ym.WilsonGram

namespace RPGram

open DetChain YMChain CxPair RPDot RPChi WilsonGram

variable {R : Type} (K : CRing R) (O : ORing K) (F : OField.OF K O)

/-- Gram list for dot m cf χ -/
def LD (m : Nat) : List R → List (R × (SU R → R))
  | [] => []
  | c :: cf => scaleG K c (LK K O F m (LibSum.rng (m + 1))) ++ LD (m + 1) cf

theorem ker_LD (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x y : SU R) (hx : snrm K x = K.one) (hy : snrm K y = K.one) :
    ∀ (m : Nat) (cf : List R), ker K (LD K O F m cf) x y = SU2Char.dot K m cf (fun n => chiK K n x y)
  | _, [] => rfl
  | m, c :: cf => by
    show ker K (scaleG K c (LK K O F m (LibSum.rng (m + 1))) ++ LD K O F (m + 1) cf) x y = K.add (K.mul c (chiK K m x y)) _
    rw [ker_app, ker_scale, ker_LD h1 h10 x y hx hy (m + 1) cf, ker_LK]
    have g := GramChi.gram_chi K O F h1 h10 x y hx hy m
    unfold chiK; rw [g]; rfl

theorem allNN_LD (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) :
    ∀ (m : Nat) (cf : List R), (∀ c, memL c cf → O.le K.zero c) → AllNN K (Pos K O) (LD K O F m cf)
  | _, [], _ => trivial
  | m, c :: cf, hc => allNN_app' K (Pos K O) _ _
      (allNN_scale K (Pos K O) c (hc c (Or.inl rfl)) _ (allNN_LK K O F h1 h10 m _ (fun k hk => LibBinom.mem_rng hk)))
      (allNN_LD h1 h10 (m + 1) cf (fun c' h' => hc c' (Or.inr h')))

/-- Gram list of the plaquette weight s + s·dot 1 cf χ -/
def LW (s : R) (cf : List R) : List (R × (SU R → R)) := scaleG K s (oneG K ++ LD K O F 1 cf)

theorem ker_LW (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (s : R) (cf : List R) (x y : SU R)
    (hx : snrm K x = K.one) (hy : snrm K y = K.one) :
    ker K (LW K O F s cf) x y = K.add s (K.mul s (SU2Char.dot K 1 cf (fun n => chiK K n x y))) := by
  unfold LW
  rw [ker_scale, ker_app, ker_one, ker_LD K O F h1 h10 x y hx hy]; lib_ring

theorem allNN_LW (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (s : R) (hs : O.le K.zero s) (cf : List R)
    (hc : ∀ c, memL c cf → O.le K.zero c) : AllNN K (Pos K O) (LW K O F s cf) :=
  allNN_scale K (Pos K O) s hs _ (allNN_app' K (Pos K O) _ _ (allNN_one K (Pos K O)) (allNN_LD K O F h1 h10 1 cf hc))

/-- **block-spin step preserves reflection positivity for the flowed weight** (cone ⇒ cf ≥ 0) -/
theorem block_rp_flow {Y : Type} (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (r : R) (cf : List R)
    (hcf : ConeBound.Bd K O r 0 (K.one :: cf)) (s : R) (hs : O.le K.zero s) (Bl : SU R → Y) (Gf : Y → R)
    (xs : List (SU R)) (hu : ∀ x, memL x xs → snrm K x = K.one) :
    O.le K.zero (sumK K (fun x => sumK K (fun y => K.mul (K.mul (Gf (Bl x)) (Gf (Bl y)))
      (K.add s (K.mul s (SU2Char.dot K 1 cf (fun n => chiK K n x y))))) xs) xs) := by
  have hc : ∀ c, memL c cf → O.le K.zero c := fun c h => ConeBound.Bd_nn K O r 0 _ hcf c (Or.inr h)
  have b := block_rp K (Pos K O) Bl Gf xs (LW K O F s cf) (allNN_LW K O F h1 h10 s hs cf hc)
  rw [ChangeVar2D.sumK_ext_mem K _ _ xs (fun x hx => ChangeVar2D.sumK_ext_mem K _ _ xs (fun y hy => by
    show K.mul _ (ker K _ x y) = _; rw [ker_LW K O F h1 h10 s cf x y (hu x hx) (hu y hy)]))] at b
  exact b

end RPGram

#print axioms RPGram.ker_LW
#print axioms RPGram.block_rp_flow

#print axioms: 2 theorems, no axioms.

90.113 Second ring for the heat list at small \(Q\)

For the heat list with \(16Q^6\le 1\) and \(108Q^{10}+384Q^4\le 64\), the bond-moving coefficients satisfy the second-ring inequality for every truncation.

\[ 2c_0\Big (\sum _{j<M}c_jc_{j+1}\Big )^2\le c_1\Big (\sum _{j<M}c_j^2\Big )^2,\qquad c_n=\sum _{j,k<L}\mathrm {cg}(j,k,n)H_jH_k,\ H_j=(j+1)Q^{2j(j+2)} \]

ym/SecondRingHeat.lean

/-
  ym/SecondRingHeat — T45, G45 (second ring, small Q, every truncation): for the heat list
  H_j = (j+1) Q^{2j(j+2)} with 0 ≤ Q ≤ 1, 16 Q⁶ ≤ 1 and 108 Q¹⁰ + 384 Q⁴ ≤ 64 (both hold for Q ≤ 0.63),
  the bond-moving coefficients c_n = C_n(L+2) satisfy, for every M,
    2 c₀ A² ≤ c₁ B²,   A = Σ_{j<M} c_j c_{j+1},  B = Σ_{j<M} c_j².
  Lean 4 core only, no axioms.
-/
import ym.SecondRingAll
import ym.SecondRingAlg2

namespace SecondRingHeat

open YMChain LinkIntegral SU2Char QBinom SecondRingC1

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- 16 Q⁶ ≤ 1 gives 8 H_{j+1} ≤ H_j -/
theorem hH_ratio8 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h16 : O.le (K.mul (cN K 16) (pwR K Q 6)) K.one) (j : Nat) :
    O.le (K.mul (cN K 8) (hH K Q (j + 1))) (hH K Q j) := by
  unfold hH
  have eE : (j + 1) * (j + 1 + 2) + (j + 1) * (j + 1 + 2) = (j * (j + 2) + j * (j + 2)) + (6 + 4 * j) := by lib_nat
  rw [eE, GKS.pwR_add, show j + 1 + 1 = j + 2 from rfl]
  generalize hP : pwR K Q (j * (j + 2) + j * (j + 2)) = P
  have hP0 : O.le K.zero P := hP ▸ AreaLower.pw_nn K O Q hQ0 h1 _
  have hr := SmallQ.pw_anti K O h1 Q hQ0 hQ1 6 (4 * j)
  have nj2 := SU2CharExp.cN_nn K O h1 (j + 2)
  have hcp : O.le K.zero (K.mul (cN K 8) (K.mul (cN K (j + 2)) P)) :=
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 8) (O.mul_nonneg _ _ nj2 hP0)
  have s1 := O.mul_le_mul_right _ _ _ hr hcp
  have s2 := O.mul_le_mul_right _ _ (K.mul (cN K (j + 2)) P) h16 (O.mul_nonneg _ _ nj2 hP0)
  have m := SU2CharExp.cN_mono K O h1 (j + 2) (2 * (j + 1)) (by
    show j + 2 ≤ 2 * (j + 1)
    rw [show 2 * (j + 1) = (j + 2) + j by lib_nat]; exact Nat.le_add_right _ _)
  rw [SU2CharExp.cN_mul] at m
  have s3 := O.mul_le_mul_right _ _ P m hP0
  have s23 := O.le_trans _ _ _ s2 (by rw [show K.mul K.one (K.mul (cN K (j + 2)) P) = K.mul (cN K (j + 2)) P by lib_ring]; exact s3)
  have s12 := O.mul_le_mul_right _ _ (cN K 2) s1 (SU2CharExp.cN_nn K O h1 2)
  have fin := O.le_trans _ _ _ s12 (by
    rw [show K.mul (K.mul (pwR K Q 6) (K.mul (cN K 8) (K.mul (cN K (j + 2)) P))) (cN K 2)
        = K.mul (K.mul (cN K 16) (pwR K Q 6)) (K.mul (cN K (j + 2)) P) by unfold cN; lib_ring]; exact s23)
  rw [show K.mul (K.mul (pwR K Q (6 + 4 * j)) (K.mul (cN K 8) (K.mul (cN K (j + 2)) P))) (cN K 2)
      = K.mul (K.mul (cN K 8) (K.mul (cN K (j + 2)) (K.mul P (pwR K Q (6 + 4 * j))))) (cN K 2) by lib_ring,
    show K.mul (K.mul (cN K 2) (cN K (j + 1))) P = K.mul (K.mul (cN K (j + 1)) P) (cN K 2) by lib_ring] at fin
  exact O.pos_cancel _ _ _ (SU2CharExp.cN_nn K O h1 2) (GridQ.cN_pos K O h1 h10 1) fin

/-- c₀ ≥ H₀² = 1 -/
theorem c0_ge (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (L : Nat) :
    O.le K.one (C K (hH K Q) (L + 1) 0) := by
  rw [c0_eq, sumN_shift]
  have := O.add_le_add _ _ _ _ (O.le_refl (K.mul (hH K Q 0) (hH K Q 0)))
    (sumN_nn K O (fun j => K.mul (hH K Q (j + 1)) (hH K Q (j + 1)))
      (fun j => O.mul_nonneg _ _ (hH_nn K O h1 Q hQ0 _) (hH_nn K O h1 Q hQ0 _)) L)
  rw [show K.add (K.mul (hH K Q 0) (hH K Q 0)) K.zero = K.one by
    unfold hH; rw [show pwR K Q (0 * (0 + 2) + 0 * (0 + 2)) = K.one from rfl]; unfold cN; lib_ring] at this
  exact this

/-- **G45: second ring for the heat list, every truncation L + 2 and every M** -/
theorem heat_second_ring (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (hQ1 : O.le Q K.one) (h16 : O.le (K.mul (cN K 16) (pwR K Q 6)) K.one)
    (hq : O.le (K.add (K.mul (cN K 108) (pwR K Q 10)) (K.mul (cN K 384) (pwR K Q 4))) (cN K 64)) (L M : Nat) :
    O.le (K.mul (cN K 2) (K.mul (C K (hH K Q) (L + 2) 0)
        (K.mul (SecondRingAlg2.A K (C K (hH K Q) (L + 2)) M) (SecondRingAlg2.A K (C K (hH K Q) (L + 2)) M))))
      (K.mul (C K (hH K Q) (L + 2) 1)
        (K.mul (SecondRingAlg2.B K (C K (hH K Q) (L + 2)) M) (SecondRingAlg2.B K (C K (hH K Q) (L + 2)) M))) := by
  have hHn := hH_nn K O h1 Q hQ0
  have hr8 := hH_ratio8 K O h1 h10 Q hQ0 hQ1 h16
  -- 128 Q⁶ ≤ 9
  have h128 : O.le (K.mul (cN K 128) (pwR K Q 6)) (cN K 9) := by
    have t := O.mul_le_mul_right _ _ (cN K 8) h16 (SU2CharExp.cN_nn K O h1 8)
    rw [show K.mul (K.mul (cN K 16) (pwR K Q 6)) (cN K 8) = K.mul (cN K 128) (pwR K Q 6) by unfold cN; lib_ring,
      K.one_mul] at t
    exact O.le_trans _ _ _ t (SU2CharExp.cN_mono K O h1 8 9 (Nat.le_succ 8))
  have c0n : C K (hH K Q) (L + 2) 0 ≠ K.zero := fun e => by
    have := c0_ge K O h1 Q hQ0 (L + 1)
    rw [e] at this
    exact h10 (O.le_antisymm _ _ this h1)
  exact SecondRingAlg2.alg_full K O h1 h10 (C K (hH K Q) (L + 2))
    (fun n => SecondRingA1.C_nn K O h1 (hH K Q) hHn (L + 2) n) c0n
    (fun j => SecondRingA1.heat_a1 K O h1 h10 Q hQ0 hQ1 h128 hq L j)
    (SecondRingAll.shift_all K O h1 h10 (hH K Q) hHn hr8 1 (L + 2))
    (SecondRingAll.shift_all K O h1 h10 (hH K Q) hHn hr8 0 (L + 2)) M

end SecondRingHeat

#print axioms SecondRingHeat.hH_ratio8
#print axioms SecondRingHeat.c0_ge
#print axioms SecondRingHeat.heat_second_ring

#print axioms: 3 theorems, no axioms.

90.114 One heat step does not increase \(u\) at small \(Q\)

For the heat list with \(4Q^3\le 1\) and \(108Q^{10}+384Q^4\le 64\), product, second product and decimation keep the fundamental ratio below \(u(H)=Q^6\), for every truncation.

\[ u\big (\mathrm {dec}(X\cdot X)\big )=\frac {Z_1}{2Z_0}\le Q^6=u(H),\qquad X=(c_n)_{n<N},\ c_n=\sum _{j,k<L}\mathrm {cg}(j,k,n)H_jH_k \]

ym/SecondRingStep.lean

/-
  ym/SecondRingStep — T45, G48b (one heat step at list level). With X the list of the first N bond-moving
  coefficients c_n = C_n(L+2) of the heat list, Y the list of the first K+2 coefficients of X·X (cgc), and
  Z = decM 0 Y the decimated list, the fundamental ratio does not increase:
    u(Z) = Z₁/(2Z₀) ≤ Q⁶ = u(H),   i.e.  Z₁ ≤ 2Q⁶ Z₀,
  under 0 ≤ Q ≤ 1, 4Q³ ≤ 1, 108Q¹⁰ + 384Q⁴ ≤ 64 (Q ≤ 0.63). Lean 4 core only, no axioms.
-/
import ym.SecondRingChain
import ym.DecU

namespace SecondRingStep

open YMChain LinkIntegral SU2Char QBinom SecondRingC1 HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

def cl (f : Nat → R) : Nat → Nat → List R
  | _, 0 => []
  | m, N + 1 => f m :: cl f (m + 1) N

theorem len_cl (f : Nat → R) : ∀ N m, (cl f m N).length = N
  | 0, _ => rfl
  | N + 1, m => by show (cl f (m + 1) N).length + 1 = N + 1; rw [len_cl f N (m + 1)]

theorem co_cl (f : Nat → R) (hf : ∀ n, O.le K.zero (f n)) : ∀ N m j,
    O.le K.zero (co K (cl f m N) j) ∧ O.le (co K (cl f m N) j) (f (m + j))
  | 0, m, j => ⟨O.le_refl _, hf (m + j)⟩
  | N + 1, m, 0 => ⟨hf m, O.le_refl _⟩
  | N + 1, m, j + 1 => by
    have := co_cl f hf N (m + 1) j
    rw [show m + (j + 1) = m + 1 + j by lib_nat]; exact this

theorem co_cl_lt (f : Nat → R) : ∀ N m j, j < N → co K (cl f m N) j = f (m + j)
  | 0, _, _, h => absurd h (Nat.not_lt_zero _)
  | N + 1, m, 0, _ => rfl
  | N + 1, m, j + 1, h => by
    show co K (cl f (m + 1) N) j = _
    rw [co_cl_lt f N (m + 1) j (Nat.lt_of_succ_lt_succ h), show m + 1 + j = m + (j + 1) by lib_nat]

/-- the Clebsch–Gordan product of a list with itself is the double sum C over its entries -/
theorem cgc_eq (X : List R) (n : Nat) : MigdalCone.cgc K X X n = C K (co K X) X.length n := by
  unfold MigdalCone.cgc C
  rw [TripleA.dot_sumN]
  apply SecondRing.sumN_ext_lt K _ _ X.length
  intro j _
  rw [TripleA.dot_sumN, ← sumN_mul]
  apply SecondRing.sumN_ext_lt K _ _ X.length
  intro k _
  rw [Nat.zero_add, Nat.zero_add]; lib_ring

theorem sq_nn (a : R) : O.le K.zero (K.mul a a) := by
  cases O.le_total K.zero a with
  | inl h => exact O.mul_nonneg _ _ h h
  | inr h =>
    have hn : O.le K.zero (K.neg a) := by
      have := O.add_le_add _ _ _ _ h (O.le_refl (K.neg a))
      rw [show K.add a (K.neg a) = K.zero by lib_ring, show K.add K.zero (K.neg a) = K.neg a by lib_ring] at this; exact this
    have := O.mul_nonneg _ _ hn hn
    rw [show K.mul (K.neg a) (K.neg a) = K.mul a a by lib_ring] at this; exact this

/-- **G48b: one heat step does not increase u** -/
theorem heat_step (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h4 : O.le (K.mul (cN K 4) (pwR K Q 3)) K.one)
    (hq : O.le (K.add (K.mul (cN K 108) (pwR K Q 10)) (K.mul (cN K 384) (pwR K Q 4))) (cN K 64)) (L N K' : Nat) :
    O.le (co K (MigdalCone.decM K O F 0 (cl (fun n => MigdalCone.cgc K (cl (C K (hH K Q) (L + 2)) 0 N)
              (cl (C K (hH K Q) (L + 2)) 0 N) n) 0 (K' + 2))) 1)
      (K.mul (K.mul (cN K 2) (pwR K Q 6)) (co K (MigdalCone.decM K O F 0 (cl (fun n => MigdalCone.cgc K
              (cl (C K (hH K Q) (L + 2)) 0 N) (cl (C K (hH K Q) (L + 2)) 0 N) n) 0 (K' + 2))) 0)) := by
  generalize hc : C K (hH K Q) (L + 2) = c
  have hHn := hH_nn K O h1 Q hQ0
  have cn : ∀ n, O.le K.zero (c n) := fun n => hc ▸ SecondRingA1.C_nn K O h1 (hH K Q) hHn (L + 2) n
  generalize hX : cl c 0 N = X
  have hlen : X.length = N := hX ▸ len_cl c N 0
  have cX := fun j => hX ▸ co_cl K O c cn N 0 j
  have cXn : ∀ j, O.le K.zero (co K X j) := fun j => (cX j).1
  -- y₀ = B_N, y₁ ≤ 2 A_N
  have y0 : MigdalCone.cgc K X X 0 = SecondRingAlg2.B K c N := by
    rw [cgc_eq, c0_eq, hlen]
    unfold SecondRingAlg2.B
    apply SecondRing.sumN_ext_lt K _ _ N
    intro j hj
    rw [← hX, co_cl_lt K c N 0 j hj, Nat.zero_add]
  have y1 : O.le (MigdalCone.cgc K X X 1) (K.add (SecondRingAlg2.A K c N) (SecondRingAlg2.A K c N)) := by
    rw [cgc_eq, hlen]
    refine O.le_trans _ _ _ (c1_le K O h1 (co K X) cXn N) ?_
    have m : O.le (sumN K (fun j => K.mul (co K X j) (co K X (j + 1))) N) (SecondRingAlg2.A K c N) := by
      unfold SecondRingAlg2.A
      apply sumN_mono K O; intro j
      have a := O.mul_le_mul_right _ _ (co K X (j + 1)) (by have := (cX j).2; rw [Nat.zero_add] at this; exact this) (cXn _)
      have b := O.mul_le_mul_right _ _ (c j) (by have := (cX (j + 1)).2; rw [Nat.zero_add] at this; exact this) (cn j)
      rw [K.mul_comm (co K X (j + 1)) (c j), K.mul_comm (c (j + 1)) (c j)] at b
      exact O.le_trans _ _ _ a b
    exact O.add_le_add _ _ _ _ m m
  have y1n : O.le K.zero (MigdalCone.cgc K X X 1) := by rw [cgc_eq]; exact SecondRingA1.C_nn K O h1 _ cXn _ 1
  have ch := SecondRingChain.heat_chain K O h1 h10 Q hQ0 hQ1 h4 hq L N
  rw [hc] at ch
  generalize SecondRingAlg2.A K c N = a at y1 ch
  generalize SecondRingAlg2.B K c N = b at y0 ch
  generalize hz : MigdalCone.cgc K X X 1 = z at y1 y1n
  -- z⁴ ≤ 16 a⁴ ≤ 16 Q⁶ b⁴
  have z2 := SecondRingAlg.sq_mono K O _ _ y1n y1
  have z4 := SecondRingAlg.sq_mono K O _ _ (O.mul_nonneg _ _ y1n y1n) z2
  rw [show K.mul (K.mul (K.add a a) (K.add a a)) (K.mul (K.add a a) (K.add a a))
      = K.mul (cN K 16) (K.mul (K.mul a a) (K.mul a a)) by unfold cN; lib_ring] at z4
  have z16 := O.le_trans _ _ _ z4 (by
    have := O.mul_le_mul_right _ _ (cN K 16) ch (SU2CharExp.cN_nn K O h1 16)
    rw [K.mul_comm, K.mul_comm (K.mul (pwR K Q 6) _)] at this; exact this)
  -- decimated entries
  have e1 : co K (MigdalCone.decM K O F 0 (cl (fun n => MigdalCone.cgc K X X n) 0 (K' + 2))) 1
      = K.mul (pwR K (MigdalCone.cgc K X X 1) 4) (pwR K (F.inv (cN K 2)) 3) := rfl
  have e0 : co K (MigdalCone.decM K O F 0 (cl (fun n => MigdalCone.cgc K X X n) 0 (K' + 2))) 0
      = K.mul (pwR K (MigdalCone.cgc K X X 0) 4) (pwR K (F.inv (cN K 1)) 3) := rfl
  rw [e1, e0, y0]
  generalize hi1 : F.inv (cN K 1) = i1
  generalize hi2 : F.inv (cN K 2) = i2
  have c1ne : cN K 1 ≠ K.zero := GridQ.cN_pos K O h1 h10 0
  have c2ne : cN K 2 ≠ K.zero := GridQ.cN_pos K O h1 h10 1
  have m1 := F.mul_inv _ c1ne
  have m2 := F.mul_inv _ c2ne
  rw [hi1] at m1; rw [hi2] at m2
  have i1e : i1 = K.one := by
    rw [show cN K 1 = K.one by unfold cN; lib_ring, K.one_mul] at m1; exact m1
  have i2n : O.le K.zero i2 := hi2 ▸ F.inv_pos _ (SU2CharExp.cN_nn K O h1 2) c2ne
  have p3n : O.le K.zero (pwR K i2 3) := AreaLower.pw_nn K O i2 i2n h1 3
  have p8 : K.mul (cN K 8) (pwR K i2 3) = K.one := by
    show K.mul (cN K 8) (K.mul (K.mul (K.mul K.one i2) i2) i2) = K.one
    rw [show K.mul (cN K 8) (K.mul (K.mul (K.mul K.one i2) i2) i2)
      = K.mul (K.mul (K.mul (cN K 2) i2) (K.mul (cN K 2) i2)) (K.mul (cN K 2) i2) by unfold cN; lib_ring, m2]
    lib_ring
  rw [i1e, hz]
  have pz : pwR K z 4 = K.mul (K.mul z z) (K.mul z z) := by
    show K.mul (K.mul (K.mul (K.mul K.one z) z) z) z = _; lib_ring
  have pb : pwR K b 4 = K.mul (K.mul b b) (K.mul b b) := by
    show K.mul (K.mul (K.mul (K.mul K.one b) b) b) b = _; lib_ring
  rw [pz, pb]
  have t := O.mul_le_mul_right _ _ (pwR K i2 3) z16 p3n
  refine O.le_trans _ _ _ t ?_
  rw [show K.mul (K.mul (cN K 16) (K.mul (pwR K Q 6) (K.mul (K.mul b b) (K.mul b b)))) (pwR K i2 3)
      = K.mul (K.mul (K.mul (cN K 2) (pwR K Q 6)) (K.mul (K.mul b b) (K.mul b b))) (K.mul (cN K 8) (pwR K i2 3)) by
        unfold cN; lib_ring, p8,
    show pwR K K.one 3 = K.one by show K.mul (K.mul (K.mul K.one K.one) K.one) K.one = K.one; lib_ring]
  rw [show K.mul (K.mul (K.mul (cN K 2) (pwR K Q 6)) (K.mul (K.mul b b) (K.mul b b))) K.one
      = K.mul (K.mul (cN K 2) (pwR K Q 6)) (K.mul (K.mul (K.mul b b) (K.mul b b)) K.one) by lib_ring]
  exact O.le_refl _

end SecondRingStep

#print axioms SecondRingStep.len_cl
#print axioms SecondRingStep.co_cl
#print axioms SecondRingStep.co_cl_lt
#print axioms SecondRingStep.cgc_eq
#print axioms SecondRingStep.sq_nn
#print axioms SecondRingStep.heat_step

#print axioms: 6 theorems, no axioms.

90.115 Lower LR comparison for the heat product at small \(Q\)

For \(0\le Q\le 3/5\) and \(q\ge 0\) with \(q^3\le 2Q^6\), \(5q^2\le 8Q^4\), the heat product coefficients grow at least like a heat list with parameter \(q\), for every truncation.

\[ (n+2)\,q^{2n+3}\,C_n(L)\le (n+1)\,C_{n+1}(L)\qquad (1\le n,\ n+2<2L) \]

ym/HeatAssemble.lean

/-
  ym/HeatAssemble — T45, G44a (assembly, part 1): truncation-exact upper bound
    C_n(L) ≤ (1+3Q⁶) Σ_{j,k<L} δ(k, |n−j|) H_j H_k,
  and the pointwise split δ(k, |n−j|) ≤ δ(j+k, n) + D(j,k) (D: the pairs (k'+1+n, k'+1), j > n).
  Lean 4 core only, no axioms.
-/
import ym.HeatNat
import ym.HeatMatch
import ym.AbelLR
import ym.HeatStepQ6

namespace HeatAssemble

open YMChain LinkIntegral SU2Char QBinom SecondRingC1 CgSplit

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem inner2 (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h6 : O.le (K.mul (cN K 6) (pwR K Q 10)) K.one) (j n L : Nat) :
    O.le (sumN K (fun k => K.mul (cN K (Fusion.cg j k n)) (hH K Q k)) L)
      (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) (sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j n))) (hH K Q k)) L)) := by
  have hHn := hH_nn K O h1 Q hQ0
  have f0 : O.le K.zero (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) := by
    have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 3) (AreaLower.pw_nn K O Q hQ0 h1 6))
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  cases Nat.lt_or_ge (dist j n) L with
  | inl h =>
    rw [dsum_eq K (hH K Q) (dist j n) L h]
    exact HeatUpper.inner K O h1 Q hQ0 hQ1 h6 j n L
  | inr h =>
    rw [SecondRing.sumN_ext_lt K _ (fun _ => K.zero) L (fun k hk => by
      rw [HeatUpper.cg_below j k n (Nat.lt_of_lt_of_le hk h)]; unfold cN; lib_ring), SecondRingShift.sumN_zero]
    exact O.mul_nonneg _ _ f0 (sumN_nn K O _ (fun k => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (hHn k)) L)

/-- C_n(L) ≤ (1+3Q⁶) Σ_{j,k<L} δ(k,|n−j|) H_j H_k -/
theorem C_upper2 (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h6 : O.le (K.mul (cN K 6) (pwR K Q 10)) K.one) (L n : Nat) :
    O.le (C K (hH K Q) L n) (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 6)))
      (sumN K (fun j => sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j n))) (K.mul (hH K Q j) (hH K Q k))) L) L)) := by
  have hHn := hH_nn K O h1 Q hQ0
  unfold C
  rw [← sumN_mul]
  apply SecondRingC1.sumN_mono K O; intro j
  rw [SecondRing.sumN_ext_lt K _ (fun k => K.mul (hH K Q j) (K.mul (cN K (Fusion.cg j k n)) (hH K Q k))) L
    (fun k _ => by lib_ring), sumN_mul,
    SecondRing.sumN_ext_lt K (fun k => K.mul (cN K (SU2Haar.dl k (dist j n))) (K.mul (hH K Q j) (hH K Q k)))
      (fun k => K.mul (hH K Q j) (K.mul (cN K (SU2Haar.dl k (dist j n))) (hH K Q k))) L (fun k _ => by lib_ring), sumN_mul]
  have := O.mul_le_mul_right _ _ (hH K Q j) (inner2 K O h1 Q hQ0 hQ1 h6 j n L) (hHn j)
  rw [K.mul_comm (sumN K _ L) (hH K Q j)] at this
  rw [show K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) (K.mul (hH K Q j)
      (sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j n))) (hH K Q k)) L))
    = K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) (sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j n))) (hH K Q k)) L)) (hH K Q j) by lib_ring]
  exact this

/-- the tail indicator: D(j, k'+1) = δ(k'+1+n, j), D(j,0) = 0 -/
def D (n : Nat) (j : Nat) : Nat → Nat
  | 0 => 0
  | k + 1 => SU2Haar.dl (k + 1 + n) j

theorem dist_self' : ∀ j, dist j j = 0
  | 0 => rfl
  | j + 1 => dist_self' j

/-- δ(k, |n−j|) ≤ δ(j+k, n) + D(j,k) -/
theorem dl_split (j k n : Nat) : SU2Haar.dl k (dist j n) ≤ SU2Haar.dl (j + k) n + D n j k := by
  cases Nat.decEq k (dist j n) with
  | isFalse h => rw [Fusion.dl_ne k (dist j n) h]; exact Nat.zero_le _
  | isTrue h =>
    have e1 : SU2Haar.dl k (dist j n) = 1 := by rw [h]; exact SU2Char.dl_self _
    rw [e1]
    cases HeatUpper.dist_cases j n with
    | inl e =>
      rw [show j + k = n by rw [h]; exact e, SU2Char.dl_self]; exact Nat.le_add_right 1 _
    | inr e =>
      cases k with
      | zero =>
        rw [show j + 0 = n by rw [← e, ← h]; rfl, SU2Char.dl_self]; exact Nat.le_add_right 1 _
      | succ k =>
        show 1 ≤ SU2Haar.dl (j + (k + 1)) n + SU2Haar.dl (k + 1 + n) j
        rw [show k + 1 + n = j by rw [h, Nat.add_comm]; exact e, SU2Char.dl_self]
        exact Nat.le_add_left 1 _

section pair
variable (Q q : R)

def f6 : R := K.add K.one (K.mul (cN K 3) (pwR K Q 6))
def Lam (n : Nat) : R := K.mul (cN K (n + 2)) (K.mul (pwR K q (3 + n * 2)) (f6 K Q))
def g (n : Nat) (j k : Nat) : R := K.mul (cN K (SU2Haar.dl (j + k) n)) (K.mul (hH K Q j) (hH K Q k))


/-- heat_m re-indexed by n: 4 Λ(n) H_m ≤ k (n+1) H_{m+1} times any nonnegative factor -/
theorem hm (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (hQ0 : O.le K.zero Q)
    (h5 : O.le (K.mul (cN K 5) Q) (cN K 3)) (hq0 : O.le K.zero q)
    (hq3 : O.le (pwR K q 3) (K.mul (cN K 2) (pwR K Q 6)))
    (hq2 : O.le (K.mul (cN K 5) (K.mul q q)) (K.mul (cN K 8) (pwR K Q 4))) (m t k n : Nat) (hn : 2 * m + t = n)
    (hN : 64 * 3 ^ 0 * 9 ^ (2 * m + t + t) * (8 ^ (2 * m + t) * ((2 * m + t + 2) * (m + 1)))
      ≤ k * (7 * 5 ^ 0 * 25 ^ (2 * m + t + t)) * (5 ^ (2 * m + t) * ((2 * m + t + 1) * (m + 2))))
    (z : R) (hz : O.le K.zero z) :
    O.le (K.mul (cN K 4) (K.mul (Lam K Q q n) (K.mul (hH K Q m) z)))
      (K.mul (cN K k) (K.mul (cN K (n + 1)) (K.mul (hH K Q (m + 1)) z))) := by
  have t := O.mul_le_mul_right _ _ z (HeatClose.heat_m K O h1 h10 Q q hQ0 h5 hq0 hq3 hq2 m t k hN) hz
  rw [hn] at t
  unfold Lam f6
  rw [show K.mul (K.mul (cN K 4) (K.mul (cN K (n + 2)) (K.mul (pwR K q (3 + n * 2))
        (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) (hH K Q m))))) z
      = K.mul (cN K 4) (K.mul (K.mul (cN K (n + 2)) (K.mul (pwR K q (3 + n * 2)) (K.add K.one (K.mul (cN K 3) (pwR K Q 6)))))
          (K.mul (hH K Q m) z)) by lib_ring,
    show K.mul (K.mul (cN K k) (K.mul (cN K (n + 1)) (hH K Q (m + 1)))) z
      = K.mul (cN K k) (K.mul (cN K (n + 1)) (K.mul (hH K Q (m + 1)) z)) by lib_ring] at t
  exact t
theorem ex_add (a b : Nat) (h : a ≤ b) : ∃ s, b = a + s := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih => obtain ⟨s, hs⟩ := ih; exact ⟨s + 1, by rw [hs]; rfl⟩

theorem g_eq (n j k : Nat) (h : j + k = n) : g K Q n j k = K.mul (hH K Q j) (hH K Q k) := by
  unfold g; rw [h, SU2Char.dl_self]; unfold cN; lib_ring

theorem g_ne (n j k : Nat) (h : j + k ≠ n) : g K Q n j k = K.zero := by
  unfold g; rw [Fusion.dl_ne _ _ h]; unfold cN; lib_ring

/-- **pair inequality** for the target (a+1, b) -/
theorem pair (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (hQ0 : O.le K.zero Q)
    (h5 : O.le (K.mul (cN K 5) Q) (cN K 3)) (hq0 : O.le K.zero q)
    (hq3 : O.le (pwR K q 3) (K.mul (cN K 2) (pwR K Q 6)))
    (hq2 : O.le (K.mul (cN K 5) (K.mul q q)) (K.mul (cN K 8) (pwR K Q 4))) (n : Nat) (hn1 : 1 ≤ n) (a b : Nat) :
    O.le (K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add (HeatMatch.A K (g K Q n) (a + 1) b) (HeatMatch.B K (g K Q n) (a + 1) b))))
      (K.mul (cN K 4) (K.mul (cN K (n + 1)) (g K Q (n + 1) (a + 1) b))) := by
  have hHn := hH_nn K O h1 Q hQ0
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have gnn : ∀ m j k, O.le K.zero (g K Q m j k) := fun m j k =>
    O.mul_nonneg _ _ (nn _) (O.mul_nonneg _ _ (hHn j) (hHn k))
  have hm' := hm K O Q q h1 h10 hQ0 h5 hq0 hq3 hq2
  have rhs0 : O.le K.zero (K.mul (cN K 4) (K.mul (cN K (n + 1)) (g K Q (n + 1) (a + 1) b))) :=
    O.mul_nonneg _ _ (nn 4) (O.mul_nonneg _ _ (nn _) (gnn _ _ _))
  have lhs0 : ∀ x y : R, x = K.zero → y = K.zero →
      K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add x y)) = K.zero := by
    intro x y hx hy; rw [hx, hy]; lib_ring
  have eA : HeatMatch.A K (g K Q n) (a + 1) b = if a ≤ b then g K Q n a b else K.zero := rfl
  cases Nat.decEq (a + b) n with
  | isFalse hne =>
    rw [lhs0 _ _ (by
        rw [eA]; cases Nat.decLe a b with
        | isTrue h => rw [if_pos h, g_ne K Q n a b hne]
        | isFalse h => rw [if_neg h]) (by
        cases b with
        | zero => rfl
        | succ b =>
          show (if b < a + 1 then g K Q n (a + 1) b else K.zero) = K.zero
          cases Nat.decLt b (a + 1) with
          | isTrue h => rw [if_pos h, g_ne K Q n (a + 1) b (by rw [show a + 1 + b = a + (b + 1) by lib_nat]; exact hne)]
          | isFalse h => rw [if_neg h])]
    exact rhs0
  | isTrue he =>
    rw [g_eq K Q (n + 1) (a + 1) b (by rw [← he]; lib_nat)]
    have rhs1 : O.le K.zero (K.mul (cN K 4) (K.mul (cN K (n + 1)) (K.mul (hH K Q (a + 1)) (hH K Q b)))) :=
      O.mul_nonneg _ _ (nn 4) (O.mul_nonneg _ _ (nn _) (O.mul_nonneg _ _ (hHn _) (hHn _)))
    -- k(n+1)H H ≤ 4(n+1)H H for k ≤ 4
    have up : ∀ (k : Nat), k ≤ 4 → ∀ z : R, O.le K.zero z → O.le (K.mul (cN K k) z) (K.mul (cN K 4) z) :=
      fun k hk z hz => O.mul_le_mul_right _ _ z (SU2CharExp.cN_mono K O h1 k 4 hk) hz
    cases Nat.lt_or_ge b a with
    | inl hba =>
      -- (iv): A = 0; b = b'+1 with b' + 2 ≤ a + 1
      rw [eA, if_neg (Nat.not_le_of_gt hba)]
      cases b with
      | zero =>
        rw [lhs0 K.zero (HeatMatch.B K (g K Q n) (a + 1) 0) rfl rfl]; exact rhs1
      | succ b' =>
        show O.le (K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add K.zero
            (if b' < a + 1 then g K Q n (a + 1) b' else K.zero)))) _
        rw [if_pos (Nat.lt_succ_of_lt (Nat.lt_of_succ_lt hba)), g_eq K Q n (a + 1) b' (by rw [← he]; lib_nat)]
        obtain ⟨s, rfl⟩ := ex_add (b' + 2) a hba
        have t := hm' b' (s + 1 + 2) 1 n (by rw [← he]; lib_nat) (HeatNat.fam2 (s + 1) b') (hH K Q (b' + 2 + s + 1)) (hHn _)
        have t2 := O.le_trans _ _ _ t (up 1 (by decide) _ (O.mul_nonneg _ _ (nn _) (O.mul_nonneg _ _ (hHn _) (hHn _))))
        rw [show K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add K.zero (K.mul (hH K Q (b' + 2 + s + 1)) (hH K Q b'))))
            = K.mul (cN K 4) (K.mul (Lam K Q q n) (K.mul (hH K Q b') (hH K Q (b' + 2 + s + 1)))) by lib_ring,
          show K.mul (cN K 4) (K.mul (cN K (n + 1)) (K.mul (hH K Q (b' + 2 + s + 1)) (hH K Q (b' + 1))))
            = K.mul (cN K 4) (K.mul (cN K (n + 1)) (K.mul (hH K Q (b' + 1)) (hH K Q (b' + 2 + s + 1)))) by lib_ring]
        exact t2
    | inr hab =>
      rw [eA, if_pos hab, g_eq K Q n a b he]
      obtain ⟨s, rfl⟩ := ex_add a b hab
      cases s with
      | zero =>
        cases a with
        | zero => exact absurd (show 1 ≤ 0 by rw [← he] at hn1; exact hn1) (by decide)
        | succ c =>
          show O.le (K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add (K.mul (hH K Q (c + 1)) (hH K Q (c + 1)))
              (if c < c + 1 + 1 then g K Q n (c + 1 + 1) c else K.zero)))) _
          rw [if_pos (Nat.lt_succ_of_lt (Nat.lt_succ_self c)), g_eq K Q n (c + 1 + 1) c (by rw [← he]; lib_nat)]
          have t1 := hm' (c + 1) 0 3 n (by rw [← he]; lib_nat) (HeatNat.fam0 c) (hH K Q (c + 1)) (hHn _)
          have t2 := hm' c (0 + 2) 1 n (by rw [← he]; lib_nat) (HeatNat.fam2 0 c) (hH K Q (c + 1 + 1)) (hHn _)
          have t := O.add_le_add _ _ _ _ t1 t2
          rw [show K.add (K.mul (cN K 4) (K.mul (Lam K Q q n) (K.mul (hH K Q (c + 1)) (hH K Q (c + 1)))))
                (K.mul (cN K 4) (K.mul (Lam K Q q n) (K.mul (hH K Q c) (hH K Q (c + 1 + 1)))))
              = K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add (K.mul (hH K Q (c + 1)) (hH K Q (c + 1)))
                  (K.mul (hH K Q (c + 1 + 1)) (hH K Q c)))) by lib_ring,
            show K.add (K.mul (cN K 3) (K.mul (cN K (n + 1)) (K.mul (hH K Q (c + 1 + 1)) (hH K Q (c + 1)))))
                (K.mul (cN K 1) (K.mul (cN K (n + 1)) (K.mul (hH K Q (c + 1)) (hH K Q (c + 1 + 1)))))
              = K.mul (cN K 4) (K.mul (cN K (n + 1)) (K.mul (hH K Q (c + 1 + 1)) (hH K Q (c + 1)))) by
                unfold cN; lib_ring] at t
          exact t
      | succ s =>
        cases s with
        | zero =>
          show O.le (K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add (K.mul (hH K Q a) (hH K Q (a + 1)))
              (if a < a + 1 then g K Q n (a + 1) a else K.zero)))) _
          rw [if_pos (Nat.lt_succ_self a), g_eq K Q n (a + 1) a (by rw [← he]; lib_nat)]
          have t1 := hm' a 1 2 n (by rw [← he]; lib_nat) (HeatNat.fam1 a) (hH K Q (a + 1)) (hHn _)
          have t := O.add_le_add _ _ _ _ t1 t1
          rw [show K.add (K.mul (cN K 4) (K.mul (Lam K Q q n) (K.mul (hH K Q a) (hH K Q (a + 1)))))
                (K.mul (cN K 4) (K.mul (Lam K Q q n) (K.mul (hH K Q a) (hH K Q (a + 1)))))
              = K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add (K.mul (hH K Q a) (hH K Q (a + 1)))
                  (K.mul (hH K Q (a + 1)) (hH K Q a)))) by lib_ring,
            show K.add (K.mul (cN K 2) (K.mul (cN K (n + 1)) (K.mul (hH K Q (a + 1)) (hH K Q (a + 1)))))
                (K.mul (cN K 2) (K.mul (cN K (n + 1)) (K.mul (hH K Q (a + 1)) (hH K Q (a + 1)))))
              = K.mul (cN K 4) (K.mul (cN K (n + 1)) (K.mul (hH K Q (a + 1)) (hH K Q (a + 1)))) by
                unfold cN; lib_ring] at t
          exact t
        | succ s' =>
          show O.le (K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add (K.mul (hH K Q a) (hH K Q (a + (s' + 1 + 1))))
              (if a + s' + 1 < a + 1 then g K Q n (a + 1) (a + s' + 1) else K.zero)))) _
          rw [if_neg (Nat.not_lt_of_ge (Nat.succ_le_succ (Nat.le_add_right a s')))]
          have t1 := hm' a (s' + 2) 1 n (by rw [← he]; lib_nat) (HeatNat.fam2 s' a) (hH K Q (a + (s' + 1 + 1))) (hHn _)
          have t2 := O.le_trans _ _ _ t1 (up 1 (by decide) _ (O.mul_nonneg _ _ (nn _) (O.mul_nonneg _ _ (hHn _) (hHn _))))
          rw [show K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add (K.mul (hH K Q a) (hH K Q (a + (s' + 1 + 1)))) K.zero))
            = K.mul (cN K 4) (K.mul (Lam K Q q n) (K.mul (hH K Q a) (hH K Q (a + (s' + 1 + 1))))) by lib_ring]
          exact t2

/-- corner: A(L, k) = 0 for k < L when n + 2 < 2L -/
theorem corner (n L : Nat) (hL : n + 2 < L + L) (k : Nat) (hk : k < L) :
    HeatMatch.A K (g K Q n) L k = K.zero := by
  cases L with
  | zero => exact absurd hk (Nat.not_lt_zero _)
  | succ L =>
    show (if L ≤ k then g K Q n L k else K.zero) = K.zero
    cases Nat.decLe L k with
    | isFalse h => rw [if_neg h]
    | isTrue h =>
      rw [if_pos h]
      have e : k = L := Nat.le_antisymm (Nat.le_of_lt_succ hk) h
      subst e
      exact g_ne K Q n k k (fun hh => by
        rw [← hh] at hL
        exact absurd hL (by rw [show k + k + 2 = k + 1 + (k + 1) by lib_nat]; exact Nat.lt_irrefl _))

/-- H is nonincreasing (0 ≤ Q ≤ 1, 2Q⁶ ≤ 1) -/
theorem hH_dec (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h2 : O.le (K.mul (cN K 2) (pwR K Q 6)) K.one) : ∀ i d, O.le (hH K Q (i + d)) (hH K Q i)
  | _, 0 => O.le_refl _
  | i, d + 1 => by
    have r := HeatLower.hH_ratio6 K O h1 Q hQ0 hQ1 (i + d)
    have s := O.mul_le_mul_right _ _ (hH K Q (i + d)) h2 (hH_nn K O h1 Q hQ0 _)
    rw [K.one_mul] at s
    exact O.le_trans _ _ _ r (O.le_trans _ _ _ s (hH_dec h1 hQ0 hQ1 h2 i d))

def TT (n L : Nat) : R := sumN K (fun j => sumN K (fun k => K.mul (cN K (D n j k)) (K.mul (hH K Q j) (hH K Q k))) L) L
def R0 (n L : Nat) : R := sumN K (fun b => g K Q (n + 1) 0 b) L

theorem hH0 : hH K Q 0 = K.one := by unfold hH; show K.mul (cN K 1) K.one = K.one; unfold cN; lib_ring

theorem R0_eq (n L : Nat) : R0 K Q n L = sumN K (fun b => K.mul (cN K (SU2Haar.dl b (n + 1))) (hH K Q b)) L := by
  unfold R0 g
  apply SecondRing.sumN_ext_lt K _ _ L; intro b _
  rw [Nat.zero_add, hH0]; lib_ring

/-- one column of the tail: Σ_j D(j,k) H_j H_k ≤ H_k · R0 -/
theorem tt_col (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h2 : O.le (K.mul (cN K 2) (pwR K Q 6)) K.one) (n L k : Nat) :
    O.le (sumN K (fun j => K.mul (cN K (D n j k)) (K.mul (hH K Q j) (hH K Q k))) L) (K.mul (hH K Q k) (R0 K Q n L)) := by
  have hHn := hH_nn K O h1 Q hQ0
  have R0n : O.le K.zero (R0 K Q n L) := by
    rw [R0_eq]; exact sumN_nn K O _ (fun b => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (hHn b)) L
  cases k with
  | zero =>
    rw [SecondRing.sumN_ext_lt K _ (fun _ => K.zero) L (fun j _ => by show K.mul (cN K 0) _ = _; unfold cN; lib_ring),
      SecondRingShift.sumN_zero]
    exact O.mul_nonneg _ _ (hHn 0) R0n
  | succ k =>
    rw [SecondRing.sumN_ext_lt K _ (fun j => K.mul (hH K Q (k + 1)) (K.mul (cN K (SU2Haar.dl j (k + 1 + n))) (hH K Q j))) L
      (fun j _ => by show K.mul (cN K (SU2Haar.dl (k + 1 + n) j)) _ = _; rw [SU2Haar.dl_symm]; lib_ring), sumN_mul]
    have inner : O.le (sumN K (fun j => K.mul (cN K (SU2Haar.dl j (k + 1 + n))) (hH K Q j)) L) (R0 K Q n L) := by
      cases Nat.lt_or_ge (k + 1 + n) L with
      | inl h =>
        rw [dsum_eq K (hH K Q) _ L h, R0_eq, dsum_eq K (hH K Q) (n + 1) L
          (Nat.lt_of_le_of_lt (by rw [show k + 1 + n = n + 1 + k by lib_nat]; exact Nat.le_add_right _ _) h),
          show k + 1 + n = n + 1 + k by lib_nat]
        exact hH_dec K O Q h1 hQ0 hQ1 h2 (n + 1) k
      | inr h => rw [dsum_zero K (hH K Q) _ L h]; exact R0n
    have := O.mul_le_mul_right _ _ (hH K Q (k + 1)) inner (hHn _)
    rw [K.mul_comm _ (hH K Q (k + 1)), K.mul_comm (R0 K Q n L)] at this
    exact this

theorem Lam_nn (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (hq0 : O.le K.zero q) (n : Nat) : O.le K.zero (Lam K Q q n) := by
  unfold Lam f6
  refine O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (O.mul_nonneg _ _ (AreaLower.pw_nn K O q hq0 h1 _) ?_)
  have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 3) (AreaLower.pw_nn K O Q hQ0 h1 6))
  rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this

/-- **tail**: 4 Λ(n) TT ≤ 4 (n+1) R0 (n ≥ 1) -/
theorem tt_bound (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h5 : O.le (K.mul (cN K 5) Q) (cN K 3)) (hq0 : O.le K.zero q)
    (hq3 : O.le (pwR K q 3) (K.mul (cN K 2) (pwR K Q 6)))
    (hq2 : O.le (K.mul (cN K 5) (K.mul q q)) (K.mul (cN K 8) (pwR K Q 4)))
    (h2 : O.le (K.mul (cN K 2) (pwR K Q 6)) K.one) (h6 : O.le (K.mul (cN K 6) (pwR K Q 10)) K.one)
    (n : Nat) (hn1 : 1 ≤ n) (L : Nat) :
    O.le (K.mul (cN K 4) (K.mul (Lam K Q q n) (TT K Q n L))) (K.mul (cN K 4) (K.mul (cN K (n + 1)) (R0 K Q n L))) := by
  have hHn := hH_nn K O h1 Q hQ0
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have R0n : O.le K.zero (R0 K Q n L) := by
    rw [R0_eq]; exact sumN_nn K O _ (fun b => O.mul_nonneg _ _ (nn _) (hHn b)) L
  -- TT ≤ (Σ_k sh H₊ k) R0 ≤ (1+3Q¹⁰) H₁ R0
  have col : ∀ k, O.le (sumN K (fun j => K.mul (cN K (D n j k)) (K.mul (hH K Q j) (hH K Q k))) L)
      (K.mul (SecondRingShift.sh K (fun k' => hH K Q (k' + 1)) k) (R0 K Q n L)) := by
    intro k
    cases k with
    | zero =>
      rw [SecondRing.sumN_ext_lt K _ (fun _ => K.zero) L (fun j _ => by show K.mul (cN K 0) _ = _; unfold cN; lib_ring),
        SecondRingShift.sumN_zero]
      show O.le K.zero (K.mul K.zero (R0 K Q n L)); rw [show K.mul K.zero (R0 K Q n L) = K.zero by lib_ring]
      exact O.le_refl _
    | succ k => exact tt_col K O Q h1 hQ0 hQ1 h2 n L (k + 1)
  have t1 : O.le (TT K Q n L) (K.mul (sumN K (SecondRingShift.sh K (fun k' => hH K Q (k' + 1))) L) (R0 K Q n L)) := by
    unfold TT
    rw [AbelLR.sum_swap K (fun j k => K.mul (cN K (D n j k)) (K.mul (hH K Q j) (hH K Q k))) L L,
      K.mul_comm, ← sumN_mul]
    apply sumN_mono K O; intro k
    rw [K.mul_comm (R0 K Q n L)]; exact col k
  have t2 := SecondRingShift.sh_sum_le K O (fun k' => hH K Q (k' + 1)) (fun k' => hHn _) L
  have t3 := HeatTail.tail K O h1 Q hQ0 hQ1 h6 L 1
  rw [HeatUpper.tl_eq] at t3
  rw [SecondRing.sumN_ext_lt K (fun i => hH K Q (1 + i)) (fun k' => hH K Q (k' + 1)) L (fun i _ => by rw [Nat.add_comm])] at t3
  have tb : O.le (TT K Q n L) (K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (hH K Q 1)) (R0 K Q n L)) :=
    O.le_trans _ _ _ t1 (O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ t2 t3) R0n)
  -- 4Λ ≤ 2(n+1) H₁
  have l4 : O.le (K.mul (cN K 4) (Lam K Q q n)) (K.mul (cN K 2) (K.mul (cN K (n + 1)) (hH K Q 1))) := by
    obtain ⟨n', rfl⟩ := ex_add 1 n hn1
    cases n' with
    | zero =>
      have t := hm K O Q q h1 h10 hQ0 h5 hq0 hq3 hq2 0 1 2 (1 + 0) rfl (HeatNat.fam1 0) K.one h1
      rw [hH0] at t
      rw [show K.mul (cN K 4) (Lam K Q q (1 + 0)) = K.mul (cN K 4) (K.mul (Lam K Q q (1 + 0)) (K.mul K.one K.one)) by lib_ring,
        show K.mul (cN K 2) (K.mul (cN K (1 + 0 + 1)) (hH K Q 1)) = K.mul (cN K 2) (K.mul (cN K (1 + 0 + 1)) (K.mul (hH K Q (0 + 1)) K.one)) by
          rw [show (0 + 1 : Nat) = 1 from rfl]; lib_ring]
      exact t
    | succ t' =>
      have t := hm K O Q q h1 h10 hQ0 h5 hq0 hq3 hq2 0 (t' + 2) 1 (1 + (t' + 1)) (by lib_nat) (HeatNat.fam2 t' 0) K.one h1
      rw [hH0] at t
      refine O.le_trans _ _ _ (by
        rw [show K.mul (cN K 4) (Lam K Q q (1 + (t' + 1))) = K.mul (cN K 4) (K.mul (Lam K Q q (1 + (t' + 1))) (K.mul K.one K.one)) by lib_ring]
        exact t) ?_
      have := O.mul_le_mul_right _ _ (K.mul (cN K (1 + (t' + 1) + 1)) (K.mul (hH K Q (0 + 1)) K.one))
        (SU2CharExp.cN_mono K O h1 1 2 (by decide)) (O.mul_nonneg _ _ (nn _) (O.mul_nonneg _ _ (hHn _) h1))
      rw [show K.mul (cN K 2) (K.mul (cN K (1 + (t' + 1) + 1)) (K.mul (hH K Q (0 + 1)) K.one))
        = K.mul (cN K 2) (K.mul (cN K (1 + (t' + 1) + 1)) (hH K Q 1)) by rw [show (0 + 1 : Nat) = 1 from rfl]; lib_ring] at this
      exact this
  have H1e : hH K Q 1 = K.mul (cN K 2) (pwR K Q 6) := rfl
  have H1le : O.le (hH K Q 1) K.one := by rw [H1e]; exact h2
  have f10le : O.le (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (cN K 2) := by
    have a := O.mul_le_mul_right _ _ (pwR K Q 10) (SU2CharExp.cN_mono K O h1 3 6 (by decide)) (AreaLower.pw_nn K O Q hQ0 h1 10)
    have b := O.add_le_add _ _ _ _ (O.le_refl K.one) (O.le_trans _ _ _ a h6)
    rw [show K.add K.one K.one = cN K 2 by unfold cN; lib_ring] at b; exact b
  have f10n : O.le K.zero (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) := by
    have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (nn 3) (AreaLower.pw_nn K O Q hQ0 h1 10))
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  -- f10 H₁ H₁ ≤ 2
  have hh : O.le (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (K.mul (hH K Q 1) (hH K Q 1))) (cN K 2) := by
    have a := SecondRingAlg.sq_mono K O _ _ (hHn 1) H1le
    rw [K.one_mul] at a
    have b := O.mul_le_mul_right _ _ (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) a f10n
    rw [K.one_mul, K.mul_comm] at b
    exact O.le_trans _ _ _ b f10le
  have Ln := Lam_nn K O Q q h1 hQ0 hq0 n
  have s1 := O.mul_le_mul_right _ _ (K.mul (cN K 4) (Lam K Q q n)) tb (O.mul_nonneg _ _ (nn 4) Ln)
  have s2 := O.mul_le_mul_right _ _ (K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (hH K Q 1)) (R0 K Q n L)) l4
    (O.mul_nonneg _ _ (O.mul_nonneg _ _ f10n (hHn 1)) R0n)
  have s3 := O.mul_le_mul_right _ _ (K.mul (cN K 2) (K.mul (cN K (n + 1)) (R0 K Q n L))) hh
    (O.mul_nonneg _ _ (nn 2) (O.mul_nonneg _ _ (nn _) R0n))
  rw [show K.mul (TT K Q n L) (K.mul (cN K 4) (Lam K Q q n)) = K.mul (cN K 4) (K.mul (Lam K Q q n) (TT K Q n L)) by lib_ring,
    show K.mul (K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (hH K Q 1)) (R0 K Q n L)) (K.mul (cN K 4) (Lam K Q q n))
      = K.mul (K.mul (cN K 4) (Lam K Q q n)) (K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (hH K Q 1)) (R0 K Q n L)) by lib_ring] at s1
  refine O.le_trans _ _ _ s1 (O.le_trans _ _ _ s2 ?_)
  rw [show K.mul (K.mul (cN K 2) (K.mul (cN K (n + 1)) (hH K Q 1))) (K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (hH K Q 1)) (R0 K Q n L))
      = K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (K.mul (hH K Q 1) (hH K Q 1))) (K.mul (cN K 2) (K.mul (cN K (n + 1)) (R0 K Q n L))) by lib_ring]
  rw [show K.mul (cN K 2) (K.mul (cN K 2) (K.mul (cN K (n + 1)) (R0 K Q n L))) = K.mul (cN K 4) (K.mul (cN K (n + 1)) (R0 K Q n L)) by
    unfold cN; lib_ring] at s3
  exact s3

/-- **G44a (small Q)**: (n+2) q^{2n+3} C_n(L) ≤ (n+1) C_{n+1}(L) for 1 ≤ n, n + 2 < 2L, 0 ≤ Q ≤ 3/5,
    0 ≤ q, q³ ≤ 2Q⁶, 5q² ≤ 8Q⁴ — the ratios c_n / ((n+1) q^{n(n+2)}) increase. -/
theorem heat_monotone (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (hQ0 : O.le K.zero Q)
    (h5 : O.le (K.mul (cN K 5) Q) (cN K 3)) (hq0 : O.le K.zero q)
    (hq3 : O.le (pwR K q 3) (K.mul (cN K 2) (pwR K Q 6)))
    (hq2 : O.le (K.mul (cN K 5) (K.mul q q)) (K.mul (cN K 8) (pwR K Q 4)))
    (n : Nat) (hn1 : 1 ≤ n) (L : Nat) (hL : n + 2 < L + L) :
    O.le (K.mul (cN K (n + 2)) (K.mul (pwR K q (3 + n * 2)) (C K (hH K Q) L n)))
      (K.mul (cN K (n + 1)) (C K (hH K Q) L (n + 1))) := by
  have hHn := hH_nn K O h1 Q hQ0
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  obtain ⟨hQ1, c3, _⟩ := HeatStepQ6.conds K O h1 h10 Q hQ0 h5
  have q3n := AreaLower.pw_nn K O Q hQ0 h1 3
  have h16 : O.le (K.mul (cN K 16) (pwR K Q 6)) K.one := by
    have sq := SecondRingAlg.sq_mono K O _ _ (O.mul_nonneg _ _ (nn 4) q3n) c3
    rw [show K.mul (K.mul (cN K 4) (pwR K Q 3)) (K.mul (cN K 4) (pwR K Q 3)) = K.mul (cN K 16) (K.mul (pwR K Q 3) (pwR K Q 3)) by
      unfold cN; lib_ring, ← GKS.pwR_add, K.one_mul] at sq
    exact sq
  have q6n := AreaLower.pw_nn K O Q hQ0 h1 6
  have up6 : ∀ c, c ≤ 16 → O.le (K.mul (cN K c) (pwR K Q 6)) K.one := fun c hc =>
    O.le_trans _ _ _ (O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 c 16 hc) q6n) h16
  have h2 := up6 2 (by decide)
  have h6 : O.le (K.mul (cN K 6) (pwR K Q 10)) K.one := by
    have a := O.mul_le_mul_right _ _ (cN K 6) (SmallQ.pw_anti K O h1 Q hQ0 hQ1 6 4) (nn 6)
    rw [K.mul_comm, K.mul_comm (pwR K Q 6)] at a
    exact O.le_trans _ _ _ a (up6 6 (by decide))
  obtain ⟨L', rfl⟩ : ∃ L', L = L' + 1 := by
    cases L with
    | zero => exact absurd hL (Nat.not_lt_zero _)
    | succ L' => exact ⟨L', rfl⟩
  -- (1) C_n ≤ f6 U'
  have u1 := C_upper2 K O h1 Q hQ0 hQ1 h6 (L' + 1) n
  -- (2) U' ≤ S_n + TT
  have u2 : O.le (sumN K (fun j => sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j n))) (K.mul (hH K Q j) (hH K Q k))) (L' + 1)) (L' + 1))
      (K.add (sumN K (fun j => sumN K (fun k => g K Q n j k) (L' + 1)) (L' + 1)) (TT K Q n (L' + 1))) := by
    unfold TT
    rw [← sumN_add]
    apply sumN_mono K O; intro j
    rw [← sumN_add]
    apply sumN_mono K O; intro k
    have := O.mul_le_mul_right _ _ (K.mul (hH K Q j) (hH K Q k)) (SU2CharExp.cN_mono K O h1 _ _ (dl_split j k n))
      (O.mul_nonneg _ _ (hHn j) (hHn k))
    rw [cN_add] at this
    unfold g
    rw [show K.mul (K.add (cN K (SU2Haar.dl (j + k) n)) (cN K (D n j k))) (K.mul (hH K Q j) (hH K Q k))
      = K.add (K.mul (cN K (SU2Haar.dl (j + k) n)) (K.mul (hH K Q j) (hH K Q k))) (K.mul (cN K (D n j k)) (K.mul (hH K Q j) (hH K Q k))) by lib_ring] at this
    exact this
  -- (3) matching
  have u3 := HeatMatch.match_eq K (g K Q n) (L' + 1) (corner K Q n (L' + 1) hL)
  -- (4) rows
  have rows : O.le (K.mul (cN K 4) (K.mul (Lam K Q q n)
        (sumN K (fun a => sumN K (fun b => K.add (HeatMatch.A K (g K Q n) a b) (HeatMatch.B K (g K Q n) a b)) (L' + 1)) (L' + 1))))
      (K.mul (cN K 4) (K.mul (cN K (n + 1)) (sumN K (fun a => sumN K (fun b => g K Q (n + 1) (a + 1) b) (L' + 1)) L'))) := by
    rw [sumN_shift]
    have r0 : sumN K (fun b => K.add (HeatMatch.A K (g K Q n) 0 b) (HeatMatch.B K (g K Q n) 0 b)) (L' + 1) = K.zero := by
      rw [SecondRing.sumN_ext_lt K _ (fun _ => K.zero) (L' + 1) (fun b _ => by
        cases b with
        | zero => show K.add K.zero K.zero = K.zero; lib_ring
        | succ b =>
          show K.add K.zero (if b < 0 then g K Q n 0 b else K.zero) = K.zero
          rw [if_neg (Nat.not_lt_zero b)]; lib_ring), SecondRingShift.sumN_zero]
    rw [r0, show K.add K.zero (sumN K (fun k => sumN K (fun b => K.add (HeatMatch.A K (g K Q n) (k + 1) b) (HeatMatch.B K (g K Q n) (k + 1) b)) (L' + 1)) L')
      = sumN K (fun k => sumN K (fun b => K.add (HeatMatch.A K (g K Q n) (k + 1) b) (HeatMatch.B K (g K Q n) (k + 1) b)) (L' + 1)) L' by lib_ring]
    rw [← sumN_mul, ← sumN_mul, ← sumN_mul, ← sumN_mul]
    apply sumN_mono K O; intro a
    rw [← sumN_mul, ← sumN_mul, ← sumN_mul, ← sumN_mul]
    apply sumN_mono K O; intro b
    have := pair K O Q q h1 h10 hQ0 h5 hq0 hq3 hq2 n hn1 a b
    rw [show K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add (HeatMatch.A K (g K Q n) (a + 1) b) (HeatMatch.B K (g K Q n) (a + 1) b)))
      = K.mul (cN K 4) (K.mul (Lam K Q q n) (K.add (HeatMatch.A K (g K Q n) (a + 1) b) (HeatMatch.B K (g K Q n) (a + 1) b))) from rfl] at this
    exact this
  -- (5) tail and (6) lower bound
  have tt := tt_bound K O Q q h1 h10 hQ0 hQ1 h5 hq0 hq3 hq2 h2 h6 n hn1 (L' + 1)
  have sc := HeatUpper.S_le_C K O h1 (hH K Q) hHn (L' + 1) (n + 1)
  have sEq : HeatUpper.S K (hH K Q) (L' + 1) (n + 1) = K.add (R0 K Q n (L' + 1))
      (sumN K (fun a => sumN K (fun b => g K Q (n + 1) (a + 1) b) (L' + 1)) L') := by
    show sumN K (fun j => sumN K (fun k => g K Q (n + 1) j k) (L' + 1)) (L' + 1) = _
    rw [sumN_shift]; rfl
  rw [u3] at u2
  generalize sumN K (fun a => sumN K (fun b => K.add (HeatMatch.A K (g K Q n) a b) (HeatMatch.B K (g K Q n) a b)) (L' + 1)) (L' + 1) = SA at u2 rows
  generalize sumN K (fun a => sumN K (fun b => g K Q (n + 1) (a + 1) b) (L' + 1)) L' = SR at rows sEq
  generalize TT K Q n (L' + 1) = T at u2 tt
  generalize R0 K Q n (L' + 1) = r at tt sEq
  rw [sEq] at sc
  have Ln := Lam_nn K O Q q h1 hQ0 hq0 n
  -- 4ΛU' ≤ 4Λ(SA + T) ≤ 4(n+1)(r + SR)
  have v1 := O.mul_le_mul_right _ _ (K.mul (cN K 4) (Lam K Q q n)) u2 (O.mul_nonneg _ _ (nn 4) Ln)
  have v2 := O.add_le_add _ _ _ _ rows tt
  have v3 := O.le_trans _ _ _ v1 (by
    rw [show K.mul (K.add SA T) (K.mul (cN K 4) (Lam K Q q n))
      = K.add (K.mul (cN K 4) (K.mul (Lam K Q q n) SA)) (K.mul (cN K 4) (K.mul (Lam K Q q n) T)) by lib_ring]
    exact v2)
  have v4 := O.le_trans _ _ _ v3 (by
    rw [show K.add (K.mul (cN K 4) (K.mul (cN K (n + 1)) SR)) (K.mul (cN K 4) (K.mul (cN K (n + 1)) r))
      = K.mul (K.mul (cN K 4) (cN K (n + 1))) (K.add r SR) by lib_ring]
    exact O.mul_le_mul_right _ _ _ sc (O.mul_nonneg _ _ (nn 4) (nn _)) |> fun h => by
      rw [K.mul_comm (K.add r SR), K.mul_comm (C K (hH K Q) (L' + 1) (n + 1))] at h; exact h)
  -- C_n side
  have w := O.mul_le_mul_right _ _ (K.mul (cN K 4) (K.mul (cN K (n + 2)) (pwR K q (3 + n * 2)))) u1
    (O.mul_nonneg _ _ (nn 4) (O.mul_nonneg _ _ (nn _) (AreaLower.pw_nn K O q hq0 h1 _)))
  have w2 := O.le_trans _ _ _ w (by
    unfold Lam f6 at v4
    rw [show K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) (sumN K (fun j => sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j n))) (K.mul (hH K Q j) (hH K Q k))) (L' + 1)) (L' + 1)))
          (K.mul (cN K 4) (K.mul (cN K (n + 2)) (pwR K q (3 + n * 2))))
        = K.mul (sumN K (fun j => sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j n))) (K.mul (hH K Q j) (hH K Q k))) (L' + 1)) (L' + 1))
            (K.mul (cN K 4) (K.mul (cN K (n + 2)) (K.mul (pwR K q (3 + n * 2)) (K.add K.one (K.mul (cN K 3) (pwR K Q 6)))))) by lib_ring]
    exact v4)
  rw [show K.mul (C K (hH K Q) (L' + 1) n) (K.mul (cN K 4) (K.mul (cN K (n + 2)) (pwR K q (3 + n * 2))))
      = K.mul (K.mul (cN K (n + 2)) (K.mul (pwR K q (3 + n * 2)) (C K (hH K Q) (L' + 1) n))) (cN K 4) by lib_ring,
    show K.mul (K.mul (cN K 4) (cN K (n + 1))) (C K (hH K Q) (L' + 1) (n + 1))
      = K.mul (K.mul (cN K (n + 1)) (C K (hH K Q) (L' + 1) (n + 1))) (cN K 4) by lib_ring] at w2
  exact O.pos_cancel _ _ _ (nn 4) (GridQ.cN_pos K O h1 h10 3) w2

end pair

end HeatAssemble

#print axioms HeatAssemble.inner2
#print axioms HeatAssemble.C_upper2
#print axioms HeatAssemble.dist_self'
#print axioms HeatAssemble.dl_split
#print axioms HeatAssemble.hm
#print axioms HeatAssemble.ex_add
#print axioms HeatAssemble.pair
#print axioms HeatAssemble.corner
#print axioms HeatAssemble.hH_dec
#print axioms HeatAssemble.hH0
#print axioms HeatAssemble.R0_eq
#print axioms HeatAssemble.tt_col
#print axioms HeatAssemble.Lam_nn
#print axioms HeatAssemble.tt_bound
#print axioms HeatAssemble.heat_monotone

#print axioms: 15 theorems, no axioms.

90.116 Upper ratio bound for the heat product at small \(Q\)

For \(0\le Q\le 1\) with \(16Q^6\le 1\) the heat product coefficients decay at least like \(Q^{2n+4}\) per step, for every truncation; for \(6Q\le 1\) this is the upper LR comparison with the heat list \(K_Q\).

\[ C_{n+1}(L)\le 6\,Q^{2n+4}\,C_n(L),\qquad (n+1)\,C_{n+1}(L)\le (n+2)\,Q^{2n+3}\,C_n(L)\ \ (6Q\le 1) \]

ym/HeatUpperLR.lean

/-
  ym/HeatUpperLR — T45, G56 (upper ratio bound for the heat bond-moving product, small Q):
  for 0 ≤ Q ≤ 1 with 16Q⁶ ≤ 1, every truncation L and every n,
    C_{n+1}(L) ≤ 6 Q^{2n+4} C_n(L),
  and hence, for 6Q ≤ 1, (n+1) C_{n+1}(L) ≤ (n+2) Q^{2n+3} C_n(L)  (H·H ≼_LR K_Q, upper side).
  Method: every pair (j,k) with j + k = n+1 is charged to the pair obtained by lowering its larger index
  (ratio H_{a+1} ≤ 2Q^{4a+6}H_a with 4a+6 ≥ 2n+4); the distance tail is bounded by H₁·H_{n+2}.
  Lean 4 core only, no axioms.
-/
import ym.HeatAssemble

namespace HeatUpperLR

open YMChain LinkIntegral SU2Char QBinom SecondRingC1 CgSplit

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (Q : R)

/-- left-lowered pair weight: gl(a+1,k) = g_n(a,k) -/
def gl (n : Nat) : Nat → Nat → R
  | 0, _ => K.zero
  | a + 1, k => HeatAssemble.g K Q n a k

/-- right-lowered pair weight: gr(j,b+1) = g_n(j,b) -/
def gr (n j : Nat) : Nat → R
  | 0 => K.zero
  | b + 1 => HeatAssemble.g K Q n j b

theorem mul_le_l (c a b : R) (hc : O.le K.zero c) (h : O.le a b) : O.le (K.mul c a) (K.mul c b) := by
  have := O.mul_le_mul_right _ _ c h hc
  rw [K.mul_comm a c, K.mul_comm b c] at this; exact this

theorem g_nn (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (n j k : Nat) : O.le K.zero (HeatAssemble.g K Q n j k) := by
  unfold HeatAssemble.g
  exact O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (O.mul_nonneg _ _ (hH_nn K O h1 Q hQ0 j) (hH_nn K O h1 Q hQ0 k))

theorem gl_nn (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (n : Nat) : ∀ j k, O.le K.zero (gl K Q n j k)
  | 0, _ => O.le_refl _
  | a + 1, k => g_nn K O Q h1 hQ0 n a k

theorem gr_nn (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (n j : Nat) : ∀ k, O.le K.zero (gr K Q n j k)
  | 0 => O.le_refl _
  | b + 1 => g_nn K O Q h1 hQ0 n j b

theorem idx (a k s : Nat) (hs : a + 1 = k + s) : 4 * a + 6 = (2 * (a + k) + 4) + 2 * s := by
  have e : 4 * a + 6 = 2 * (a + 1) + (2 * a + 4) := by lib_nat
  rw [hs] at e
  rw [e]; lib_nat

/-- H_{a+1} ≤ 2 Q^e H_a whenever 4a + 6 = e + d -/
theorem ratio_up (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (a e d : Nat)
    (he : 4 * a + 6 = e + d) :
    O.le (hH K Q (a + 1)) (K.mul (K.mul (cN K 2) (pwR K Q e)) (hH K Q a)) := by
  have r := HeatTail.ratio_m K O h1 Q hQ0 a
  rw [he] at r
  have p := SmallQ.pw_anti K O h1 Q hQ0 hQ1 e d
  have p2 := O.mul_le_mul_right _ _ (K.mul (cN K 2) (hH K Q a)) p
    (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 2) (hH_nn K O h1 Q hQ0 a))
  rw [show K.mul (pwR K Q (e + d)) (K.mul (cN K 2) (hH K Q a)) = K.mul (K.mul (cN K 2) (pwR K Q (e + d))) (hH K Q a) by lib_ring,
    show K.mul (pwR K Q e) (K.mul (cN K 2) (hH K Q a)) = K.mul (K.mul (cN K 2) (pwR K Q e)) (hH K Q a) by lib_ring] at p2
  exact O.le_trans _ _ _ r p2

/-- pointwise charge: g_{n+1}(j,k) ≤ 2Q^{2n+4} (gl(j,k) + gr(j,k)) -/
theorem pt (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (n j k : Nat) :
    O.le (HeatAssemble.g K Q (n + 1) j k)
      (K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4))) (K.add (gl K Q n j k) (gr K Q n j k))) := by
  have hHn := hH_nn K O h1 Q hQ0
  have c0 : O.le K.zero (K.mul (cN K 2) (pwR K Q (2 * n + 4))) :=
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 2) (AreaLower.pw_nn K O Q hQ0 h1 _)
  have gln := gl_nn K O Q h1 hQ0 n j k
  have grn := gr_nn K O Q h1 hQ0 n j k
  cases Nat.decEq (j + k) (n + 1) with
  | isFalse h =>
    rw [HeatAssemble.g_ne K Q (n + 1) j k h]
    have := O.add_le_add _ _ _ _ gln grn
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this
    exact O.mul_nonneg _ _ c0 this
  | isTrue h =>
    rw [HeatAssemble.g_eq K Q (n + 1) j k h]
    cases Nat.lt_or_ge j k with
    | inr hkj =>
      cases j with
      | zero =>
        have t : 0 + k ≤ 0 + 0 := Nat.add_le_add (Nat.le_refl 0) hkj
        rw [h] at t
        exact absurd t (Nat.not_succ_le_zero n)
      | succ a =>
        have hn : a + k = n := Nat.succ.inj (show a + k + 1 = n + 1 by rw [show a + k + 1 = a + 1 + k by lib_nat]; exact h)
        obtain ⟨s, hs⟩ := HeatAssemble.ex_add k (a + 1) hkj
        have e := idx a k s hs
        rw [hn] at e
        have r := ratio_up K O Q h1 hQ0 hQ1 a (2 * n + 4) (2 * s) e
        have m := O.mul_le_mul_right _ _ (hH K Q k) r (hHn k)
        have m2 : O.le (K.mul (hH K Q (a + 1)) (hH K Q k))
            (K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4))) (K.mul (hH K Q a) (hH K Q k))) := by
          rw [show K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4))) (K.mul (hH K Q a) (hH K Q k))
            = K.mul (K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4))) (hH K Q a)) (hH K Q k) by lib_ring]
          exact m
        have gle : gl K Q n (a + 1) k = K.mul (hH K Q a) (hH K Q k) := HeatAssemble.g_eq K Q n a k hn
        have u : O.le (K.mul (hH K Q a) (hH K Q k)) (K.add (gl K Q n (a + 1) k) (gr K Q n (a + 1) k)) := by
          rw [gle]
          have := O.add_le_add _ _ _ _ (O.le_refl (K.mul (hH K Q a) (hH K Q k))) grn
          rw [show K.add (K.mul (hH K Q a) (hH K Q k)) K.zero = K.mul (hH K Q a) (hH K Q k) by lib_ring] at this
          exact this
        exact O.le_trans _ _ _ m2 (mul_le_l K O _ _ _ c0 u)
    | inl hjk =>
      cases k with
      | zero => exact absurd hjk (Nat.not_lt_zero j)
      | succ b =>
        have hn : j + b = n := Nat.succ.inj (by show j + b + 1 = n + 1; exact h)
        obtain ⟨s, hs⟩ := HeatAssemble.ex_add j (b + 1) (Nat.le_of_lt hjk)
        have e := idx b j s hs
        rw [Nat.add_comm b j, hn] at e
        have r := ratio_up K O Q h1 hQ0 hQ1 b (2 * n + 4) (2 * s) e
        have m := mul_le_l K O (hH K Q j) _ _ (hHn j) r
        have m2 : O.le (K.mul (hH K Q j) (hH K Q (b + 1)))
            (K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4))) (K.mul (hH K Q j) (hH K Q b))) := by
          rw [show K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4))) (K.mul (hH K Q j) (hH K Q b))
            = K.mul (hH K Q j) (K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4))) (hH K Q b)) by lib_ring]
          exact m
        have gre : gr K Q n j (b + 1) = K.mul (hH K Q j) (hH K Q b) := HeatAssemble.g_eq K Q n j b hn
        have u : O.le (K.mul (hH K Q j) (hH K Q b)) (K.add (gl K Q n j (b + 1)) (gr K Q n j (b + 1))) := by
          rw [gre]
          have := O.add_le_add _ _ _ _ gln (O.le_refl (K.mul (hH K Q j) (hH K Q b)))
          rw [show K.add K.zero (K.mul (hH K Q j) (hH K Q b)) = K.mul (hH K Q j) (hH K Q b) by lib_ring] at this
          exact this
        exact O.le_trans _ _ _ m2 (mul_le_l K O _ _ _ c0 u)

theorem S_eq (n L : Nat) :
    HeatUpper.S K (hH K Q) L n = sumN K (fun j => sumN K (fun k => HeatAssemble.g K Q n j k) L) L := rfl

theorem sum_gl (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (n : Nat) : ∀ L,
    O.le (sumN K (fun j => sumN K (fun k => gl K Q n j k) L) L) (HeatUpper.S K (hH K Q) L n)
  | 0 => O.le_refl _
  | L + 1 => by
    rw [S_eq, sumN_shift K (fun j => sumN K (fun k => gl K Q n j k) (L + 1)) L]
    have z : sumN K (fun k => gl K Q n 0 k) (L + 1) = K.zero := by
      rw [SecondRing.sumN_ext_lt K (fun k => gl K Q n 0 k) (fun _ => K.zero) (L + 1) (fun k _ => rfl), SecondRingShift.sumN_zero]
    have e2 : sumN K (fun a => sumN K (fun k => gl K Q n (a + 1) k) (L + 1)) L
        = sumN K (fun a => sumN K (fun k => HeatAssemble.g K Q n a k) (L + 1)) L := rfl
    rw [z, e2, show K.add K.zero (sumN K (fun a => sumN K (fun k => HeatAssemble.g K Q n a k) (L + 1)) L)
      = sumN K (fun a => sumN K (fun k => HeatAssemble.g K Q n a k) (L + 1)) L by lib_ring]
    exact sumN_succ_ge K O (fun a => sumN K (fun k => HeatAssemble.g K Q n a k) (L + 1))
      (fun a => sumN_nn K O _ (g_nn K O Q h1 hQ0 n a) (L + 1)) L

theorem sum_gr (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (n : Nat) : ∀ L,
    O.le (sumN K (fun j => sumN K (fun k => gr K Q n j k) L) L) (HeatUpper.S K (hH K Q) L n)
  | 0 => O.le_refl _
  | L + 1 => by
    rw [S_eq]
    apply SecondRingC1.sumN_mono K O; intro j
    show O.le (sumN K (fun k => gr K Q n j k) (L + 1)) (sumN K (fun k => HeatAssemble.g K Q n j k) (L + 1))
    rw [sumN_shift K (fun k => gr K Q n j k) L]
    have e2 : sumN K (fun b => gr K Q n j (b + 1)) L = sumN K (fun b => HeatAssemble.g K Q n j b) L := rfl
    have z : gr K Q n j 0 = K.zero := rfl
    rw [e2, z, show K.add K.zero (sumN K (fun b => HeatAssemble.g K Q n j b) L) = sumN K (fun b => HeatAssemble.g K Q n j b) L by
      lib_ring]
    exact sumN_succ_ge K O _ (g_nn K O Q h1 hQ0 n j) L

/-- main part: Σ_{j+k=n+1} H_j H_k ≤ 2Q^{2n+4} (S_n + S_n) -/
theorem main (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (n L : Nat) :
    O.le (sumN K (fun j => sumN K (fun k => HeatAssemble.g K Q (n + 1) j k) L) L)
      (K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4))) (K.add (HeatUpper.S K (hH K Q) L n) (HeatUpper.S K (hH K Q) L n))) := by
  have c0 : O.le K.zero (K.mul (cN K 2) (pwR K Q (2 * n + 4))) :=
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 2) (AreaLower.pw_nn K O Q hQ0 h1 _)
  have s1 : O.le (sumN K (fun j => sumN K (fun k => HeatAssemble.g K Q (n + 1) j k) L) L)
      (sumN K (fun j => sumN K (fun k => K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4)))
        (K.add (gl K Q n j k) (gr K Q n j k))) L) L) :=
    SecondRingC1.sumN_mono K O _ _ (fun j => SecondRingC1.sumN_mono K O _ _ (fun k => pt K O Q h1 hQ0 hQ1 n j k) L) L
  rw [SecondRing.sumN_ext_lt K (fun j => sumN K (fun k => K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4)))
        (K.add (gl K Q n j k) (gr K Q n j k))) L)
      (fun j => K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4)))
        (K.add (sumN K (fun k => gl K Q n j k) L) (sumN K (fun k => gr K Q n j k) L))) L
      (fun j _ => by rw [sumN_mul, sumN_add]), sumN_mul, sumN_add] at s1
  have t := O.add_le_add _ _ _ _ (sum_gl K O Q h1 hQ0 n L) (sum_gr K O Q h1 hQ0 n L)
  exact O.le_trans _ _ _ s1 (mul_le_l K O _ _ _ c0 t)

/-- distance tail: TT_n ≤ (1+3Q¹⁰) H₁ R0_n (any n) -/
theorem tail_bd (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h2 : O.le (K.mul (cN K 2) (pwR K Q 6)) K.one) (h6 : O.le (K.mul (cN K 6) (pwR K Q 10)) K.one) (n L : Nat) :
    O.le (HeatAssemble.TT K Q n L)
      (K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (hH K Q 1)) (HeatAssemble.R0 K Q n L)) := by
  have hHn := hH_nn K O h1 Q hQ0
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have R0n : O.le K.zero (HeatAssemble.R0 K Q n L) := by
    rw [HeatAssemble.R0_eq]; exact sumN_nn K O _ (fun b => O.mul_nonneg _ _ (nn _) (hHn b)) L
  have col : ∀ k, O.le (sumN K (fun j => K.mul (cN K (HeatAssemble.D n j k)) (K.mul (hH K Q j) (hH K Q k))) L)
      (K.mul (SecondRingShift.sh K (fun k' => hH K Q (k' + 1)) k) (HeatAssemble.R0 K Q n L)) := by
    intro k
    cases k with
    | zero =>
      rw [SecondRing.sumN_ext_lt K _ (fun _ => K.zero) L (fun j _ => by show K.mul (cN K 0) _ = _; unfold cN; lib_ring),
        SecondRingShift.sumN_zero]
      show O.le K.zero (K.mul K.zero (HeatAssemble.R0 K Q n L))
      rw [show K.mul K.zero (HeatAssemble.R0 K Q n L) = K.zero by lib_ring]
      exact O.le_refl _
    | succ k => exact HeatAssemble.tt_col K O Q h1 hQ0 hQ1 h2 n L (k + 1)
  have t1 : O.le (HeatAssemble.TT K Q n L)
      (K.mul (sumN K (SecondRingShift.sh K (fun k' => hH K Q (k' + 1))) L) (HeatAssemble.R0 K Q n L)) := by
    unfold HeatAssemble.TT
    rw [AbelLR.sum_swap K (fun j k => K.mul (cN K (HeatAssemble.D n j k)) (K.mul (hH K Q j) (hH K Q k))) L L,
      K.mul_comm, ← sumN_mul]
    apply SecondRingC1.sumN_mono K O; intro k
    rw [K.mul_comm (HeatAssemble.R0 K Q n L)]; exact col k
  have t2 := SecondRingShift.sh_sum_le K O (fun k' => hH K Q (k' + 1)) (fun k' => hHn _) L
  have t3 := HeatTail.tail K O h1 Q hQ0 hQ1 h6 L 1
  rw [HeatUpper.tl_eq] at t3
  rw [SecondRing.sumN_ext_lt K (fun i => hH K Q (1 + i)) (fun k' => hH K Q (k' + 1)) L (fun i _ => by rw [Nat.add_comm])] at t3
  exact O.le_trans _ _ _ t1 (O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ t2 t3) R0n)

/-- R0_{n+1} = H_{n+2} (or 0) ≤ 2Q^{2n+4} C_n -/
theorem R0_bd (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h2 : O.le (K.mul (cN K 2) (pwR K Q 6)) K.one) (n L : Nat) :
    O.le (HeatAssemble.R0 K Q (n + 1) L) (K.mul (K.mul (cN K 2) (pwR K Q (2 * n + 4))) (C K (hH K Q) L n)) := by
  have hHn := hH_nn K O h1 Q hQ0
  have c0 : O.le K.zero (K.mul (cN K 2) (pwR K Q (2 * n + 4))) :=
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 2) (AreaLower.pw_nn K O Q hQ0 h1 _)
  rw [HeatAssemble.R0_eq]
  cases Nat.lt_or_ge (n + 1 + 1) L with
  | inl h =>
    rw [dsum_eq K (hH K Q) (n + 1 + 1) L h]
    have d1 := HeatAssemble.hH_dec K O Q h1 hQ0 hQ1 h2 (n + 1) 1
    have r := ratio_up K O Q h1 hQ0 hQ1 n (2 * n + 4) (2 * n + 2) (by lib_nat)
    have ct := HeatLower.C_ge_term K O h1 (hH K Q) hHn L n 0
      (Nat.lt_of_le_of_lt (Nat.le_add_right n 2) h) (Nat.lt_of_le_of_lt (Nat.zero_le _) h)
    rw [HeatAssemble.hH0, show K.mul (hH K Q n) K.one = hH K Q n by lib_ring, Nat.add_zero] at ct
    exact O.le_trans _ _ _ d1 (O.le_trans _ _ _ r (mul_le_l K O _ _ _ c0 ct))
  | inr h =>
    rw [dsum_zero K (hH K Q) (n + 1 + 1) L h]
    exact O.mul_nonneg _ _ c0 (SecondRingA1.C_nn K O h1 (hH K Q) hHn L n)

/-- numerics: (1+3y)(4 + 2f·(2y)) ≤ 6 for 0 ≤ y, 16y ≤ 1, 0 ≤ f ≤ 2 -/
theorem num6 (h1 : O.le K.zero K.one) (y f : R) (hy : O.le K.zero y) (h16 : O.le (K.mul (cN K 16) y) K.one)
    (hf : O.le f (cN K 2)) :
    O.le (K.mul (K.add K.one (K.mul (cN K 3) y)) (K.add (cN K 4) (K.mul (cN K 2) (K.mul f (K.mul (cN K 2) y))))) (cN K 6) := by
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have y4 : O.le K.zero (K.mul (cN K 4) y) := O.mul_nonneg _ _ (nn 4) hy
  have a := O.mul_le_mul_right _ _ _ hf y4
  rw [show K.mul f (K.mul (cN K 4) y) = K.mul (cN K 2) (K.mul f (K.mul (cN K 2) y)) by unfold cN; lib_ring,
    show K.mul (cN K 2) (K.mul (cN K 4) y) = K.mul (cN K 8) y by unfold cN; lib_ring] at a
  have b := O.add_le_add _ _ _ _ (O.le_refl (cN K 4)) a
  have f0 : O.le K.zero (K.add K.one (K.mul (cN K 3) y)) := by
    have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (nn 3) hy)
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  have c := mul_le_l K O _ _ _ f0 b
  rw [show K.mul (K.add K.one (K.mul (cN K 3) y)) (K.add (cN K 4) (K.mul (cN K 8) y))
    = K.add (cN K 4) (K.add (K.mul (cN K 20) y) (K.mul (cN K 24) (K.mul y y))) by unfold cN; lib_ring] at c
  -- 24 y² ≤ 2 y
  have e1 := mul_le_l K O y _ _ hy h16
  rw [show K.mul y (K.mul (cN K 16) y) = K.mul (cN K 16) (K.mul y y) by lib_ring, show K.mul y K.one = y by lib_ring] at e1
  have e2 := mul_le_l K O (cN K 2) _ _ (nn 2) e1
  rw [show K.mul (cN K 2) (K.mul (cN K 16) (K.mul y y)) = K.mul (cN K 32) (K.mul y y) by unfold cN; lib_ring] at e2
  have e3 := O.mul_le_mul_right _ _ (K.mul y y) (SU2CharExp.cN_mono K O h1 24 32 (by decide)) (O.mul_nonneg _ _ hy hy)
  have e4 := O.le_trans _ _ _ e3 e2
  -- 22 y ≤ 2
  have g1 := mul_le_l K O (cN K 2) _ _ (nn 2) h16
  rw [show K.mul (cN K 2) (K.mul (cN K 16) y) = K.mul (cN K 32) y by unfold cN; lib_ring,
    show K.mul (cN K 2) K.one = cN K 2 by unfold cN; lib_ring] at g1
  have g2 := O.mul_le_mul_right _ _ y (SU2CharExp.cN_mono K O h1 22 32 (by decide)) hy
  have g3 := O.le_trans _ _ _ g2 g1
  have s := O.add_le_add _ _ _ _ (O.le_refl (K.mul (cN K 20) y)) e4
  rw [show K.add (K.mul (cN K 20) y) (K.mul (cN K 2) y) = K.mul (cN K 22) y by unfold cN; lib_ring] at s
  have s2 := O.add_le_add _ _ _ _ (O.le_refl (cN K 4)) (O.le_trans _ _ _ s g3)
  rw [show K.add (cN K 4) (cN K 2) = cN K 6 by unfold cN; lib_ring] at s2
  exact O.le_trans _ _ _ c s2

/-- **G56 (ratio form)**: C_{n+1}(L) ≤ 6 Q^{2n+4} C_n(L) for 0 ≤ Q ≤ 1, 16Q⁶ ≤ 1, every n and L -/
theorem upper_ratio (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h16 : O.le (K.mul (cN K 16) (pwR K Q 6)) K.one) (n L : Nat) :
    O.le (C K (hH K Q) L (n + 1)) (K.mul (K.mul (cN K 6) (pwR K Q (2 * n + 4))) (C K (hH K Q) L n)) := by
  have hHn := hH_nn K O h1 Q hQ0
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have q6n := AreaLower.pw_nn K O Q hQ0 h1 6
  have up6 : ∀ c, c ≤ 16 → O.le (K.mul (cN K c) (pwR K Q 6)) K.one := fun c hc =>
    O.le_trans _ _ _ (O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 c 16 hc) q6n) h16
  have h2 := up6 2 (by decide)
  have h6 : O.le (K.mul (cN K 6) (pwR K Q 10)) K.one := by
    have a := O.mul_le_mul_right _ _ (cN K 6) (SmallQ.pw_anti K O h1 Q hQ0 hQ1 6 4) (nn 6)
    rw [K.mul_comm, K.mul_comm (pwR K Q 6)] at a
    exact O.le_trans _ _ _ a (up6 6 (by decide))
  have f10le : O.le (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (cN K 2) := by
    have a := O.mul_le_mul_right _ _ (pwR K Q 10) (SU2CharExp.cN_mono K O h1 3 6 (by decide)) (AreaLower.pw_nn K O Q hQ0 h1 10)
    have b := O.add_le_add _ _ _ _ (O.le_refl K.one) (O.le_trans _ _ _ a h6)
    rw [show K.add K.one K.one = cN K 2 by unfold cN; lib_ring] at b; exact b
  have f10n : O.le K.zero (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) := by
    have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (nn 3) (AreaLower.pw_nn K O Q hQ0 h1 10))
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  have f6n : O.le K.zero (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) := by
    have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (nn 3) q6n)
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  -- (1) C_{n+1} ≤ f6 U
  have u1 := HeatAssemble.C_upper2 K O h1 Q hQ0 hQ1 h6 L (n + 1)
  -- (2) U ≤ Σ g_{n+1} + TT_{n+1}
  have u2 : O.le (sumN K (fun j => sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j (n + 1)))) (K.mul (hH K Q j) (hH K Q k))) L) L)
      (K.add (sumN K (fun j => sumN K (fun k => HeatAssemble.g K Q (n + 1) j k) L) L) (HeatAssemble.TT K Q (n + 1) L)) := by
    unfold HeatAssemble.TT
    rw [← sumN_add]
    apply SecondRingC1.sumN_mono K O; intro j
    rw [← sumN_add]
    apply SecondRingC1.sumN_mono K O; intro k
    have := O.mul_le_mul_right _ _ (K.mul (hH K Q j) (hH K Q k))
      (SU2CharExp.cN_mono K O h1 _ _ (HeatAssemble.dl_split j k (n + 1))) (O.mul_nonneg _ _ (hHn j) (hHn k))
    rw [cN_add] at this
    unfold HeatAssemble.g
    rw [show K.mul (K.add (cN K (SU2Haar.dl (j + k) (n + 1))) (cN K (HeatAssemble.D (n + 1) j k))) (K.mul (hH K Q j) (hH K Q k))
      = K.add (K.mul (cN K (SU2Haar.dl (j + k) (n + 1))) (K.mul (hH K Q j) (hH K Q k)))
          (K.mul (cN K (HeatAssemble.D (n + 1) j k)) (K.mul (hH K Q j) (hH K Q k))) by lib_ring] at this
    exact this
  have m := main K O Q h1 hQ0 hQ1 n L
  have sc := HeatUpper.S_le_C K O h1 (hH K Q) hHn L n
  have tb := tail_bd K O Q h1 hQ0 hQ1 h2 h6 (n + 1) L
  have rb := R0_bd K O Q h1 hQ0 hQ1 h2 n L
  have Xn := SecondRingA1.C_nn K O h1 (hH K Q) hHn L n
  generalize sumN K (fun j => sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j (n + 1)))) (K.mul (hH K Q j) (hH K Q k))) L) L = U at u1 u2
  generalize sumN K (fun j => sumN K (fun k => HeatAssemble.g K Q (n + 1) j k) L) L = M at u2 m
  generalize HeatAssemble.TT K Q (n + 1) L = T at u2 tb
  generalize HeatAssemble.R0 K Q (n + 1) L = r at tb rb
  generalize HeatUpper.S K (hH K Q) L n = S at m sc
  generalize C K (hH K Q) L (n + 1) = C1 at u1 ⊢
  generalize C K (hH K Q) L n = X at sc rb Xn ⊢
  generalize hP : pwR K Q (2 * n + 4) = P at m rb ⊢
  have P0 : O.le K.zero P := hP ▸ AreaLower.pw_nn K O Q hQ0 h1 _
  have c0 : O.le K.zero (K.mul (cN K 2) P) := O.mul_nonneg _ _ (nn 2) P0
  -- M ≤ 2P (X + X)
  have m2 := O.le_trans _ _ _ m (mul_le_l K O _ _ _ c0 (O.add_le_add _ _ _ _ sc sc))
  -- T ≤ f10 H₁ (2P X)
  have t2 := O.le_trans _ _ _ tb (mul_le_l K O _ _ _ (O.mul_nonneg _ _ f10n (hHn 1)) rb)
  have v := O.le_trans _ _ _ u2 (O.add_le_add _ _ _ _ m2 t2)
  have w := O.le_trans _ _ _ u1 (mul_le_l K O _ _ _ f6n v)
  have H1e : hH K Q 1 = K.mul (cN K 2) (pwR K Q 6) := rfl
  rw [H1e] at w
  have nm := num6 K O h1 (pwR K Q 6) (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) q6n h16 f10le
  have PX : O.le K.zero (K.mul P X) := O.mul_nonneg _ _ P0 Xn
  have fin := O.mul_le_mul_right _ _ (K.mul P X) nm PX
  refine O.le_trans _ _ _ w (O.le_trans _ _ _ (by
    rw [show K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) (K.add (K.mul (K.mul (cN K 2) P) (K.add X X))
        (K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (K.mul (cN K 2) (pwR K Q 6))) (K.mul (K.mul (cN K 2) P) X)))
      = K.mul (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) (K.add (cN K 4) (K.mul (cN K 2)
          (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (K.mul (cN K 2) (pwR K Q 6)))))) (K.mul P X) by unfold cN; lib_ring]
    exact fin) ?_)
  rw [show K.mul (cN K 6) (K.mul P X) = K.mul (K.mul (cN K 6) P) X by lib_ring]
  exact O.le_refl _

/-- **G56 (LR form, 6Q ≤ 1)**: (n+1) C_{n+1}(L) ≤ (n+2) Q^{2n+3} C_n(L) -/
theorem upper_lr (h1 : O.le K.zero K.one) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (h16 : O.le (K.mul (cN K 16) (pwR K Q 6)) K.one) (h6Q : O.le (K.mul (cN K 6) (pwR K Q 1)) K.one) (n L : Nat) :
    O.le (K.mul (cN K (n + 1)) (C K (hH K Q) L (n + 1)))
      (K.mul (cN K (n + 2)) (K.mul (pwR K Q (2 * n + 3)) (C K (hH K Q) L n))) := by
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have hHn := hH_nn K O h1 Q hQ0
  have Xn := SecondRingA1.C_nn K O h1 (hH K Q) hHn L n
  have u := upper_ratio K O Q h1 hQ0 hQ1 h16 n L
  rw [show 2 * n + 4 = 2 * n + 3 + 1 from rfl, GKS.pwR_add] at u
  have PX := O.mul_nonneg _ _ (AreaLower.pw_nn K O Q hQ0 h1 (2 * n + 3)) Xn
  have a := O.mul_le_mul_right _ _ (K.mul (pwR K Q (2 * n + 3)) (C K (hH K Q) L n)) h6Q PX
  rw [K.one_mul, show K.mul (K.mul (cN K 6) (pwR K Q 1)) (K.mul (pwR K Q (2 * n + 3)) (C K (hH K Q) L n))
    = K.mul (K.mul (cN K 6) (K.mul (pwR K Q (2 * n + 3)) (pwR K Q 1))) (C K (hH K Q) L n) by lib_ring] at a
  have b := O.le_trans _ _ _ u a
  have c := mul_le_l K O (cN K (n + 1)) _ _ (nn _) b
  have d := O.mul_le_mul_right _ _ (K.mul (pwR K Q (2 * n + 3)) (C K (hH K Q) L n))
    (SU2CharExp.cN_mono K O h1 (n + 1) (n + 2) (Nat.le_succ _)) PX
  exact O.le_trans _ _ _ c d

end HeatUpperLR

#print axioms HeatUpperLR.mul_le_l
#print axioms HeatUpperLR.g_nn
#print axioms HeatUpperLR.gl_nn
#print axioms HeatUpperLR.gr_nn
#print axioms HeatUpperLR.idx
#print axioms HeatUpperLR.ratio_up
#print axioms HeatUpperLR.pt
#print axioms HeatUpperLR.S_eq
#print axioms HeatUpperLR.sum_gl
#print axioms HeatUpperLR.sum_gr
#print axioms HeatUpperLR.main
#print axioms HeatUpperLR.tail_bd
#print axioms HeatUpperLR.R0_bd
#print axioms HeatUpperLR.num6
#print axioms HeatUpperLR.upper_ratio
#print axioms HeatUpperLR.upper_lr

#print axioms: 16 theorems, no axioms.

90.117 Upper LR comparison for the heat product at \(Q\le 1/2\)

For \(0\le Q\) with \(2Q\le 1\) the heat product is below the heat list \(K_Q\) in likelihood-ratio order, for every truncation.

\[ (n+1)\,C_{n+1}(L)\le (n+2)\,Q^{2n+3}\,C_n(L)\qquad (\text {all } n,\ L) \]

ym/HeatUpperLR2.lean

/-
  ym/HeatUpperLR2 — T45, G56 for 0 ≤ Q ≤ 1/2 (upper LR comparison of the heat product with K_Q):
  for 0 ≤ Q with 2Q ≤ 1, every truncation L and every n,
    (n+1) C_{n+1}(L) ≤ (n+2) Q^{2n+3} C_n(L).
  Sharper charging than HeatUpperLR: a pair (a+1, k) with a+1 ≥ k is charged to (a, k) with the exact ratio
  (a+1) H_{a+1} = (a+2) Q^{4a+6} H_a and (n+1)(a+2) ≤ (n+3)(a+1); a pair (j, b+1) with j ≤ b is charged to (j, b)
  with H_{b+1} ≤ 2 Q^{2n+6} H_b.  Lean 4 core only, no axioms.
-/
import ym.HeatUpperLR
import ym.HeatStepQ58

namespace HeatUpperLR2

open YMChain LinkIntegral SU2Char QBinom SecondRingC1 CgSplit

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (Q : R)

/-- exact heat ratio: (a+1) H_{a+1} = (a+2) Q^{4a+6} H_a -/
theorem ratio_ex (a : Nat) :
    K.mul (cN K (a + 1)) (hH K Q (a + 1)) = K.mul (K.mul (cN K (a + 2)) (pwR K Q (4 * a + 6))) (hH K Q a) := by
  unfold hH
  have eE : (a + 1) * (a + 1 + 2) + (a + 1) * (a + 1 + 2) = (a * (a + 2) + a * (a + 2)) + (4 * a + 6) := by lib_nat
  rw [eE, GKS.pwR_add K Q (a * (a + 2) + a * (a + 2)) (4 * a + 6), show a + 1 + 1 = a + 2 from rfl]
  lib_ring

theorem nat_ineq (a k : Nat) (hk : k ≤ a + 1) : (a + k + 1) * (a + 2) ≤ (a + k + 3) * (a + 1) := by
  have e1 : (a + k + 1) * (a + 2) = (a + k + 1) * (a + 1) + (k + (a + 1)) := by lib_nat
  have e2 : (a + k + 3) * (a + 1) = (a + k + 1) * (a + 1) + ((a + 1) + (a + 1)) := by lib_nat
  rw [e1, e2]
  exact Nat.add_le_add (Nat.le_refl _) (Nat.add_le_add hk (Nat.le_refl _))

theorem idx2 (b j s : Nat) (hs : b = j + s) : 4 * b + 6 = (2 * (j + b) + 6) + 2 * s := by
  subst hs; lib_nat

theorem le_eq (a b : R) (h : a = b) : O.le a b := h ▸ O.le_refl a

theorem le_add_r (x B : R) (hB : O.le K.zero B) : O.le x (K.add x B) := by
  have := O.add_le_add _ _ _ _ (O.le_refl x) hB
  rw [show K.add x K.zero = x by lib_ring] at this; exact this

theorem le_add_l (A x : R) (hA : O.le K.zero A) : O.le x (K.add A x) := by
  have := O.add_le_add _ _ _ _ hA (O.le_refl x)
  rw [show K.add K.zero x = x by lib_ring] at this; exact this

/-- pointwise charge: (n+1) g_{n+1}(j,k) ≤ Q^{2n+4} ((n+3) gl + (n+1)·2Q²·gr) -/
theorem pt2 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (n j k : Nat) :
    O.le (K.mul (cN K (n + 1)) (HeatAssemble.g K Q (n + 1) j k))
      (K.mul (pwR K Q (2 * n + 4)) (K.add (K.mul (cN K (n + 3)) (HeatUpperLR.gl K Q n j k))
        (K.mul (K.mul (cN K (n + 1)) (K.mul (cN K 2) (pwR K Q 2))) (HeatUpperLR.gr K Q n j k)))) := by
  have hHn := hH_nn K O h1 Q hQ0
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have P0 := AreaLower.pw_nn K O Q hQ0 h1 (2 * n + 4)
  have x20 := AreaLower.pw_nn K O Q hQ0 h1 2
  have gln := HeatUpperLR.gl_nn K O Q h1 hQ0 n j k
  have grn := HeatUpperLR.gr_nn K O Q h1 hQ0 n j k
  have A0 : O.le K.zero (K.mul (cN K (n + 3)) (HeatUpperLR.gl K Q n j k)) := O.mul_nonneg _ _ (nn _) gln
  have B0 : O.le K.zero (K.mul (K.mul (cN K (n + 1)) (K.mul (cN K 2) (pwR K Q 2))) (HeatUpperLR.gr K Q n j k)) :=
    O.mul_nonneg _ _ (O.mul_nonneg _ _ (nn _) (O.mul_nonneg _ _ (nn 2) x20)) grn
  cases Nat.decEq (j + k) (n + 1) with
  | isFalse h =>
    rw [HeatAssemble.g_ne K Q (n + 1) j k h, show K.mul (cN K (n + 1)) K.zero = K.zero by lib_ring]
    have := O.add_le_add _ _ _ _ A0 B0
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this
    exact O.mul_nonneg _ _ P0 this
  | isTrue h =>
    rw [HeatAssemble.g_eq K Q (n + 1) j k h]
    cases Nat.lt_or_ge j k with
    | inr hkj =>
      cases j with
      | zero =>
        have t : 0 + k ≤ 0 + 0 := Nat.add_le_add (Nat.le_refl 0) hkj
        rw [h] at t
        exact absurd t (Nat.not_succ_le_zero n)
      | succ a =>
        have hn : a + k = n := Nat.succ.inj (show a + k + 1 = n + 1 by rw [show a + k + 1 = a + 1 + k by lib_nat]; exact h)
        obtain ⟨s, hs⟩ := HeatAssemble.ex_add k (a + 1) hkj
        have e := HeatUpperLR.idx a k s hs
        rw [hn] at e
        have pe : O.le (pwR K Q (4 * a + 6)) (pwR K Q (2 * n + 4)) := by
          rw [e]; exact SmallQ.pw_anti K O h1 Q hQ0 hQ1 _ _
        have ni : (n + 1) * (a + 2) ≤ (n + 3) * (a + 1) := by rw [← hn]; exact nat_ineq a k hkj
        have cmono := SU2CharExp.cN_mono K O h1 _ _ ni
        rw [SU2CharExp.cN_mul, SU2CharExp.cN_mul] at cmono
        have Z0 := O.mul_nonneg _ _ (hHn a) (hHn k)
        have st1 := HeatUpperLR.mul_le_l K O (K.mul (cN K (n + 1)) (cN K (a + 2))) _ _ (O.mul_nonneg _ _ (nn _) (nn _))
          (O.mul_le_mul_right _ _ (K.mul (hH K Q a) (hH K Q k)) pe Z0)
        have st2 := O.mul_le_mul_right _ _ (K.mul (pwR K Q (2 * n + 4)) (K.mul (hH K Q a) (hH K Q k))) cmono
          (O.mul_nonneg _ _ P0 Z0)
        have st := O.le_trans _ _ _ st1 st2
        have lhs_eq : K.mul (K.mul (cN K (n + 1)) (K.mul (hH K Q (a + 1)) (hH K Q k))) (cN K (a + 1))
            = K.mul (K.mul (cN K (n + 1)) (cN K (a + 2))) (K.mul (pwR K Q (4 * a + 6)) (K.mul (hH K Q a) (hH K Q k))) := by
          rw [show K.mul (K.mul (cN K (n + 1)) (K.mul (hH K Q (a + 1)) (hH K Q k))) (cN K (a + 1))
            = K.mul (K.mul (cN K (n + 1)) (hH K Q k)) (K.mul (cN K (a + 1)) (hH K Q (a + 1))) by lib_ring, ratio_ex K Q a]
          lib_ring
        rw [← lhs_eq, show K.mul (K.mul (cN K (n + 3)) (cN K (a + 1))) (K.mul (pwR K Q (2 * n + 4)) (K.mul (hH K Q a) (hH K Q k)))
          = K.mul (K.mul (pwR K Q (2 * n + 4)) (K.mul (cN K (n + 3)) (K.mul (hH K Q a) (hH K Q k)))) (cN K (a + 1)) by lib_ring] at st
        have fin := O.pos_cancel _ _ _ (nn _) (GridQ.cN_pos K O h1 h10 a) st
        have gle : HeatUpperLR.gl K Q n (a + 1) k = K.mul (hH K Q a) (hH K Q k) := HeatAssemble.g_eq K Q n a k hn
        rw [gle]
        exact O.le_trans _ _ _ fin (HeatUpperLR.mul_le_l K O _ _ _ P0 (le_add_r K O _ _ B0))
    | inl hjk =>
      cases k with
      | zero => exact absurd hjk (Nat.not_lt_zero j)
      | succ b =>
        have hn : j + b = n := Nat.succ.inj (show j + b + 1 = n + 1 from h)
        obtain ⟨s, hs⟩ := HeatAssemble.ex_add j b (Nat.le_of_lt_succ hjk)
        have e := idx2 b j s hs
        rw [hn] at e
        have r := HeatUpperLR.ratio_up K O Q h1 hQ0 hQ1 b (2 * n + 6) (2 * s) e
        rw [show 2 * n + 6 = 2 * n + 4 + 2 from rfl, GKS.pwR_add K Q (2 * n + 4) 2] at r
        have m := HeatUpperLR.mul_le_l K O (K.mul (cN K (n + 1)) (hH K Q j)) _ _ (O.mul_nonneg _ _ (nn _) (hHn j)) r
        have gre : HeatUpperLR.gr K Q n j (b + 1) = K.mul (hH K Q j) (hH K Q b) := HeatAssemble.g_eq K Q n j b hn
        rw [gre]
        have m2 : O.le (K.mul (cN K (n + 1)) (K.mul (hH K Q j) (hH K Q (b + 1))))
            (K.mul (pwR K Q (2 * n + 4)) (K.mul (K.mul (cN K (n + 1)) (K.mul (cN K 2) (pwR K Q 2))) (K.mul (hH K Q j) (hH K Q b)))) := by
          rw [show K.mul (cN K (n + 1)) (K.mul (hH K Q j) (hH K Q (b + 1))) = K.mul (K.mul (cN K (n + 1)) (hH K Q j)) (hH K Q (b + 1)) by lib_ring,
            show K.mul (pwR K Q (2 * n + 4)) (K.mul (K.mul (cN K (n + 1)) (K.mul (cN K 2) (pwR K Q 2))) (K.mul (hH K Q j) (hH K Q b)))
              = K.mul (K.mul (cN K (n + 1)) (hH K Q j)) (K.mul (K.mul (cN K 2) (K.mul (pwR K Q (2 * n + 4)) (pwR K Q 2))) (hH K Q b)) by lib_ring]
          exact m
        exact O.le_trans _ _ _ m2 (HeatUpperLR.mul_le_l K O _ _ _ P0 (le_add_l K O _ _ A0))

/-- summed charge: (n+1) M_{n+1} ≤ Q^{2n+4} ((n+3) S_n + (n+1)·2Q²·S_n) -/
theorem main2 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (n L : Nat) :
    O.le (K.mul (cN K (n + 1)) (sumN K (fun j => sumN K (fun k => HeatAssemble.g K Q (n + 1) j k) L) L))
      (K.mul (pwR K Q (2 * n + 4)) (K.add (K.mul (cN K (n + 3)) (HeatUpper.S K (hH K Q) L n))
        (K.mul (K.mul (cN K (n + 1)) (K.mul (cN K 2) (pwR K Q 2))) (HeatUpper.S K (hH K Q) L n)))) := by
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have P0 := AreaLower.pw_nn K O Q hQ0 h1 (2 * n + 4)
  have c10 : O.le K.zero (K.mul (cN K (n + 1)) (K.mul (cN K 2) (pwR K Q 2))) :=
    O.mul_nonneg _ _ (nn _) (O.mul_nonneg _ _ (nn 2) (AreaLower.pw_nn K O Q hQ0 h1 2))
  rw [← sumN_mul, SecondRing.sumN_ext_lt K (fun j => K.mul (cN K (n + 1)) (sumN K (fun k => HeatAssemble.g K Q (n + 1) j k) L))
    (fun j => sumN K (fun k => K.mul (cN K (n + 1)) (HeatAssemble.g K Q (n + 1) j k)) L) L (fun j _ => by rw [sumN_mul])]
  have s1 := SecondRingC1.sumN_mono K O _ _ (fun j => SecondRingC1.sumN_mono K O _ _ (fun k => pt2 K O Q h1 h10 hQ0 hQ1 n j k) L) L
  rw [SecondRing.sumN_ext_lt K (fun j => sumN K (fun k => K.mul (pwR K Q (2 * n + 4)) (K.add (K.mul (cN K (n + 3)) (HeatUpperLR.gl K Q n j k))
        (K.mul (K.mul (cN K (n + 1)) (K.mul (cN K 2) (pwR K Q 2))) (HeatUpperLR.gr K Q n j k)))) L)
      (fun j => K.mul (pwR K Q (2 * n + 4)) (K.add (K.mul (cN K (n + 3)) (sumN K (fun k => HeatUpperLR.gl K Q n j k) L))
        (K.mul (K.mul (cN K (n + 1)) (K.mul (cN K 2) (pwR K Q 2))) (sumN K (fun k => HeatUpperLR.gr K Q n j k) L)))) L
      (fun j _ => by rw [sumN_mul, sumN_add, sumN_mul, sumN_mul]), sumN_mul, sumN_add, sumN_mul, sumN_mul] at s1
  refine O.le_trans _ _ _ s1 (HeatUpperLR.mul_le_l K O _ _ _ P0 (O.add_le_add _ _ _ _ ?_ ?_))
  · exact HeatUpperLR.mul_le_l K O _ _ _ (nn _) (HeatUpperLR.sum_gl K O Q h1 hQ0 n L)
  · exact HeatUpperLR.mul_le_l K O _ _ _ c10 (HeatUpperLR.sum_gr K O Q h1 hQ0 n L)

/-- numerics: for 2x ≤ 1, 4x₂ ≤ 1, 16y ≤ x₂, 64y ≤ 1, 0 ≤ f ≤ 2, N ≥ 0:
    (1+3y)·x·[(N+3) + (N+1)(2x₂) + (N+1)(f·2y·2)] ≤ N + 2 -/
theorem numG (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (N x x2 y f : R) (hN : O.le K.zero N)
    (hx : O.le K.zero x) (hy : O.le K.zero y) (hf : O.le f (cN K 2))
    (h2x : O.le (K.mul (cN K 2) x) K.one) (h4 : O.le (K.mul (cN K 4) x2) K.one)
    (h16 : O.le (K.mul (cN K 16) y) x2) (h64 : O.le (K.mul (cN K 64) y) K.one) :
    O.le (K.mul (K.add K.one (K.mul (cN K 3) y)) (K.mul x (K.add (K.add N (cN K 3))
      (K.add (K.mul (K.add N K.one) (K.mul (cN K 2) x2)) (K.mul (K.add N K.one) (K.mul (K.mul f (K.mul (cN K 2) y)) (cN K 2)))))))
      (K.add N (cN K 2)) := by
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have N1 : O.le K.zero (K.add N K.one) := by
    have := O.add_le_add _ _ _ _ hN h1; rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  -- 16 W ≤ 10
  have a1 := O.mul_le_mul_right _ _ (K.mul (cN K 4) y) hf (O.mul_nonneg _ _ (nn 4) hy)
  have b1 := HeatUpperLR.mul_le_l K O (cN K 16) _ _ (nn 16) a1
  rw [show K.mul (cN K 16) (K.mul (cN K 2) (K.mul (cN K 4) y)) = K.mul (cN K 8) (K.mul (cN K 16) y) by unfold cN; lib_ring] at b1
  have b2 := HeatUpperLR.mul_le_l K O (cN K 8) _ _ (nn 8) h16
  have b3 := HeatUpperLR.mul_le_l K O (cN K 10) _ _ (nn 10) h4
  rw [show K.mul (cN K 10) (K.mul (cN K 4) x2) = K.add (K.mul (cN K 32) x2) (K.mul (cN K 8) x2) by unfold cN; lib_ring,
    show K.mul (cN K 10) K.one = cN K 10 by lib_ring] at b3
  have hW := O.le_trans _ _ _ (O.add_le_add _ _ _ _ (O.le_refl (K.mul (cN K 32) x2)) (O.le_trans _ _ _ b1 b2)) b3
  rw [show K.add (K.mul (cN K 32) x2) (K.mul (cN K 16) (K.mul f (K.mul (cN K 4) y)))
    = K.mul (cN K 16) (K.add (K.mul (cN K 2) x2) (K.mul (K.mul f (K.mul (cN K 2) y)) (cN K 2))) by unfold cN; lib_ring] at hW
  -- 16 Bk ≤ 26N + 58
  have hB := O.add_le_add _ _ _ _ (O.le_refl (K.mul (cN K 16) (K.add N (cN K 3)))) (HeatUpperLR.mul_le_l K O _ _ _ N1 hW)
  rw [show K.add (K.mul (cN K 16) (K.add N (cN K 3))) (K.mul (K.add N K.one) (cN K 10)) = K.add (K.mul (cN K 26) N) (cN K 58) by
    unfold cN; lib_ring] at hB
  have Rn : O.le K.zero (K.add (K.mul (cN K 26) N) (cN K 58)) := by
    have := O.add_le_add _ _ _ _ (O.mul_nonneg _ _ (nn 26) hN) (nn 58)
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  -- (2x)(16Bk) ≤ 26N + 58
  have c1 := HeatUpperLR.mul_le_l K O (K.mul (cN K 2) x) _ _ (O.mul_nonneg _ _ (nn 2) hx) hB
  have c2 := O.mul_le_mul_right _ _ _ h2x Rn
  rw [K.one_mul] at c2
  have c3 := O.le_trans _ _ _ c1 c2
  -- 16 f6 ≤ 17
  have d1 := O.le_trans _ _ _ (O.mul_le_mul_right _ _ y (SU2CharExp.cN_mono K O h1 48 64 (by decide)) hy) h64
  have d2 := O.add_le_add _ _ _ _ (O.le_refl (cN K 16)) d1
  rw [show K.add (cN K 16) (K.mul (cN K 48) y) = K.mul (cN K 16) (K.add K.one (K.mul (cN K 3) y)) by unfold cN; lib_ring,
    show K.add (cN K 16) K.one = cN K 17 by unfold cN; lib_ring] at d2
  have f60 : O.le K.zero (K.mul (cN K 16) (K.add K.one (K.mul (cN K 3) y))) := by
    have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (nn 3) hy)
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact O.mul_nonneg _ _ (nn 16) this
  have e1 := HeatUpperLR.mul_le_l K O _ _ _ f60 c3
  have e2 := O.mul_le_mul_right _ _ _ d2 Rn
  have e3 := O.le_trans _ _ _ e1 e2
  -- 17(26N + 58) ≤ 512 (N + 2)
  rw [show K.mul (cN K 17) (K.add (K.mul (cN K 26) N) (cN K 58))
    = K.add (K.mul (K.mul (cN K 17) (cN K 26)) N) (K.mul (cN K 17) (cN K 58)) by lib_ring,
    HeatStepQ58.cm K 17 26, HeatStepQ58.cm K 17 58] at e3
  have f1 := O.add_le_add _ _ _ _ (O.mul_le_mul_right _ _ N (SU2CharExp.cN_mono K O h1 (17 * 26) 512 (by decide)) hN)
    (SU2CharExp.cN_mono K O h1 (17 * 58) (512 * 2) (by decide))
  have g := O.le_trans _ _ _ e3 f1
  rw [show K.mul (K.mul (cN K 16) (K.add K.one (K.mul (cN K 3) y))) (K.mul (K.mul (cN K 2) x) (K.add (K.mul (cN K 16) (K.add N (cN K 3)))
      (K.mul (K.add N K.one) (K.mul (cN K 16) (K.add (K.mul (cN K 2) x2) (K.mul (K.mul f (K.mul (cN K 2) y)) (cN K 2)))))))
    = K.mul (K.mul (K.add K.one (K.mul (cN K 3) y)) (K.mul x (K.add (K.add N (cN K 3))
      (K.add (K.mul (K.add N K.one) (K.mul (cN K 2) x2)) (K.mul (K.add N K.one) (K.mul (K.mul f (K.mul (cN K 2) y)) (cN K 2)))))))
      (K.mul (K.mul (cN K 16) (cN K 2)) (cN K 16)) by lib_ring,
    HeatStepQ58.cm K 16 2, HeatStepQ58.cm K (16 * 2) 16,
    show K.add (K.mul (cN K 512) N) (cN K (512 * 2)) = K.mul (K.add N (cN K 2)) (cN K 512) by
      rw [← HeatStepQ58.cm K 512 2]; lib_ring] at g
  exact O.pos_cancel _ _ _ (nn _) (GridQ.cN_pos K O h1 h10 511) g

/-- **G56 for 0 ≤ Q ≤ 1/2**: (n+1) C_{n+1}(L) ≤ (n+2) Q^{2n+3} C_n(L) for every n and every truncation L -/
theorem upper_lr2 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (hQ0 : O.le K.zero Q)
    (h2Q : O.le (K.mul (cN K 2) Q) K.one) (n L : Nat) :
    O.le (K.mul (cN K (n + 1)) (C K (hH K Q) L (n + 1)))
      (K.mul (cN K (n + 2)) (K.mul (pwR K Q (2 * n + 3)) (C K (hH K Q) L n))) := by
  have hHn := hH_nn K O h1 Q hQ0
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have hQ1 : O.le Q K.one := by
    have a := O.mul_le_mul_right _ _ Q (SU2CharExp.cN_mono K O h1 1 2 (by decide)) hQ0
    rw [show K.mul (cN K 1) Q = Q by unfold cN; lib_ring] at a
    exact O.le_trans _ _ _ a h2Q
  -- 4Q² ≤ 1, 16Q⁴ ≤ 1, 16Q⁶ ≤ Q², 64Q⁶ ≤ 1
  have s2 := SecondRingAlg.sq_mono K O _ _ (O.mul_nonneg _ _ (nn 2) hQ0) h2Q
  rw [show K.mul (K.mul (cN K 2) Q) (K.mul (cN K 2) Q) = K.mul (cN K 4) (pwR K Q 2) by
    show _ = K.mul (cN K 4) (K.mul (K.mul K.one Q) Q); unfold cN; lib_ring, K.one_mul] at s2
  have x20 := AreaLower.pw_nn K O Q hQ0 h1 2
  have s4 := SecondRingAlg.sq_mono K O _ _ (O.mul_nonneg _ _ (nn 4) x20) s2
  rw [show K.mul (K.mul (cN K 4) (pwR K Q 2)) (K.mul (cN K 4) (pwR K Q 2)) = K.mul (cN K 16) (pwR K Q 4) by
    rw [show (4 : Nat) = 2 + 2 from rfl, GKS.pwR_add K Q 2 2]; unfold cN; lib_ring, K.one_mul] at s4
  have y16 := HeatUpperLR.mul_le_l K O (pwR K Q 2) _ _ x20 s4
  rw [show K.mul (pwR K Q 2) (K.mul (cN K 16) (pwR K Q 4)) = K.mul (cN K 16) (pwR K Q 6) by
    rw [show (6 : Nat) = 4 + 2 from rfl, GKS.pwR_add K Q 4 2]; lib_ring, show K.mul (pwR K Q 2) K.one = pwR K Q 2 by lib_ring] at y16
  have y64 := O.le_trans _ _ _ (HeatUpperLR.mul_le_l K O (cN K 4) _ _ (nn 4) y16) s2
  rw [show K.mul (cN K 4) (K.mul (cN K 16) (pwR K Q 6)) = K.mul (cN K 64) (pwR K Q 6) by unfold cN; lib_ring] at y64
  have q6n := AreaLower.pw_nn K O Q hQ0 h1 6
  have h16 : O.le (K.mul (cN K 16) (pwR K Q 6)) K.one :=
    O.le_trans _ _ _ (O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 16 64 (by decide)) q6n) y64
  have up6 : ∀ c, c ≤ 16 → O.le (K.mul (cN K c) (pwR K Q 6)) K.one := fun c hc =>
    O.le_trans _ _ _ (O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 c 16 hc) q6n) h16
  have h2 := up6 2 (by decide)
  have h6 : O.le (K.mul (cN K 6) (pwR K Q 10)) K.one := by
    have a := O.mul_le_mul_right _ _ (cN K 6) (SmallQ.pw_anti K O h1 Q hQ0 hQ1 6 4) (nn 6)
    rw [K.mul_comm, K.mul_comm (pwR K Q 6)] at a
    exact O.le_trans _ _ _ a (up6 6 (by decide))
  have f10le : O.le (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) (cN K 2) := by
    have a := O.mul_le_mul_right _ _ (pwR K Q 10) (SU2CharExp.cN_mono K O h1 3 6 (by decide)) (AreaLower.pw_nn K O Q hQ0 h1 10)
    have b := O.add_le_add _ _ _ _ (O.le_refl K.one) (O.le_trans _ _ _ a h6)
    rw [show K.add K.one K.one = cN K 2 by unfold cN; lib_ring] at b; exact b
  have f10n : O.le K.zero (K.add K.one (K.mul (cN K 3) (pwR K Q 10))) := by
    have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (nn 3) (AreaLower.pw_nn K O Q hQ0 h1 10))
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  have f6n : O.le K.zero (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) := by
    have := O.add_le_add _ _ _ _ h1 (O.mul_nonneg _ _ (nn 3) q6n)
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  -- C_{n+1} ≤ f6 U, U ≤ M + T
  have u1 := HeatAssemble.C_upper2 K O h1 Q hQ0 hQ1 h6 L (n + 1)
  have u2 : O.le (sumN K (fun j => sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j (n + 1)))) (K.mul (hH K Q j) (hH K Q k))) L) L)
      (K.add (sumN K (fun j => sumN K (fun k => HeatAssemble.g K Q (n + 1) j k) L) L) (HeatAssemble.TT K Q (n + 1) L)) := by
    unfold HeatAssemble.TT
    rw [← sumN_add]
    apply SecondRingC1.sumN_mono K O; intro j
    rw [← sumN_add]
    apply SecondRingC1.sumN_mono K O; intro k
    have := O.mul_le_mul_right _ _ (K.mul (hH K Q j) (hH K Q k))
      (SU2CharExp.cN_mono K O h1 _ _ (HeatAssemble.dl_split j k (n + 1))) (O.mul_nonneg _ _ (hHn j) (hHn k))
    rw [cN_add] at this
    unfold HeatAssemble.g
    rw [show K.mul (K.add (cN K (SU2Haar.dl (j + k) (n + 1))) (cN K (HeatAssemble.D (n + 1) j k))) (K.mul (hH K Q j) (hH K Q k))
      = K.add (K.mul (cN K (SU2Haar.dl (j + k) (n + 1))) (K.mul (hH K Q j) (hH K Q k)))
          (K.mul (cN K (HeatAssemble.D (n + 1) j k)) (K.mul (hH K Q j) (hH K Q k))) by lib_ring] at this
    exact this
  have m := main2 K O Q h1 h10 hQ0 hQ1 n L
  have sc := HeatUpper.S_le_C K O h1 (hH K Q) hHn L n
  have tb := HeatUpperLR.tail_bd K O Q h1 hQ0 hQ1 h2 h6 (n + 1) L
  have rb := HeatUpperLR.R0_bd K O Q h1 hQ0 hQ1 h2 n L
  have Xn := SecondRingA1.C_nn K O h1 (hH K Q) hHn L n
  generalize sumN K (fun j => sumN K (fun k => K.mul (cN K (SU2Haar.dl k (dist j (n + 1)))) (K.mul (hH K Q j) (hH K Q k))) L) L = U at u1 u2
  generalize sumN K (fun j => sumN K (fun k => HeatAssemble.g K Q (n + 1) j k) L) L = M at u2 m
  generalize HeatAssemble.TT K Q (n + 1) L = T at u2 tb
  generalize HeatAssemble.R0 K Q (n + 1) L = r at tb rb
  generalize HeatUpper.S K (hH K Q) L n = S at m sc
  generalize C K (hH K Q) L (n + 1) = C1 at u1 ⊢
  generalize C K (hH K Q) L n = X at sc rb Xn ⊢
  have P0 := AreaLower.pw_nn K O Q hQ0 h1 (2 * n + 4)
  have c1n := nn (n + 1)
  -- (n+1) M ≤ P((n+3) X + (n+1)2Q² X)
  have m2 := O.le_trans _ _ _ m (HeatUpperLR.mul_le_l K O _ _ _ P0 (O.add_le_add _ _ _ _
    (HeatUpperLR.mul_le_l K O _ _ _ (nn _) sc)
    (HeatUpperLR.mul_le_l K O _ _ _ (O.mul_nonneg _ _ (nn _) (O.mul_nonneg _ _ (nn 2) x20)) sc)))
  -- (n+1) T ≤ (n+1) f10 H₁ (2 P X)
  have t2 := HeatUpperLR.mul_le_l K O (cN K (n + 1)) _ _ c1n
    (O.le_trans _ _ _ tb (HeatUpperLR.mul_le_l K O _ _ _ (O.mul_nonneg _ _ f10n (hHn 1)) rb))
  have v := HeatUpperLR.mul_le_l K O (cN K (n + 1)) _ _ c1n u2
  rw [show K.mul (cN K (n + 1)) (K.add M T) = K.add (K.mul (cN K (n + 1)) M) (K.mul (cN K (n + 1)) T) by lib_ring] at v
  have v2 := O.le_trans _ _ _ v (O.add_le_add _ _ _ _ m2 t2)
  have w0 := HeatUpperLR.mul_le_l K O (cN K (n + 1)) _ _ c1n u1
  rw [show K.mul (cN K (n + 1)) (K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) U)
    = K.mul (K.add K.one (K.mul (cN K 3) (pwR K Q 6))) (K.mul (cN K (n + 1)) U) by lib_ring] at w0
  have w := O.le_trans _ _ _ w0 (HeatUpperLR.mul_le_l K O _ _ _ f6n v2)
  -- numerics
  have nm := numG K O h1 h10 (cN K n) Q (pwR K Q 2) (pwR K Q 6) (K.add K.one (K.mul (cN K 3) (pwR K Q 10)))
    (nn n) hQ0 q6n f10le h2Q s2 y16 y64
  have QX := O.mul_nonneg _ _ (AreaLower.pw_nn K O Q hQ0 h1 (2 * n + 3)) Xn
  have fin := O.mul_le_mul_right _ _ (K.mul (pwR K Q (2 * n + 3)) X) nm QX
  have H1e : hH K Q 1 = K.mul (cN K 2) (pwR K Q 6) := rfl
  have c1e : cN K 1 = K.one := by unfold cN; lib_ring
  rw [H1e, show 2 * n + 4 = 2 * n + 3 + 1 from rfl, GKS.pwR_add K Q (2 * n + 3) 1,
    show pwR K Q 1 = Q by show K.mul K.one Q = Q; lib_ring, cN_add K n 3, cN_add K n 1, c1e] at w
  rw [cN_add K n 2, cN_add K n 1, c1e]
  exact O.le_trans _ _ _ w (O.le_trans _ _ _ (le_eq K O _ _ (by lib_ring)) fin)

end HeatUpperLR2

#print axioms HeatUpperLR2.ratio_ex
#print axioms HeatUpperLR2.nat_ineq
#print axioms HeatUpperLR2.idx2
#print axioms HeatUpperLR2.le_eq
#print axioms HeatUpperLR2.le_add_r
#print axioms HeatUpperLR2.le_add_l
#print axioms HeatUpperLR2.pt2
#print axioms HeatUpperLR2.main2
#print axioms HeatUpperLR2.numG
#print axioms HeatUpperLR2.upper_lr2

#print axioms: 10 theorems, no axioms.

90.118 One bond-moving product below a heat list

A v-good weight below the heat list \(K_{Q^2}\) in likelihood-ratio order stays below \(K_Q\) after one bond-moving product, for \(0<Q\le 1/2\) and every truncation.

\[ X=K_{Q^2}\,g\ (g\downarrow )\ \Longrightarrow \ (X\!\cdot \! X)_{n+1}K_Q(n)\le (X\!\cdot \! X)_n K_Q(n+1)\quad (\text {all } n) \]

ym/HeatFirstStep.lean

/-
  ym/HeatFirstStep — T45, G60 piece (a2): one bond-moving product of a weight below a heat list.
  If X is v-good of length M+2 and X = K_{Q²}·g (g nonincreasing, K_{Q²} the truncated heat list with q = Q²),
  then for 0 < Q, 2Q ≤ 1 and every n,
    X·X ≼_LR K_Q   (LRat (cgc X X) K_Q n,  K_Q(n) = (n+1) Q^{n(n+2)}).
  Chain: X·X ≼ K_{Q²}·K_{Q²} (HeatL1All.heat_first), the list product equals the heat product C(hH Q) (heat_C),
  C(hH Q) ≼ K_Q (HeatLRUpperK.lr_upper), joined by a local transitivity through the positive middle C.
  Lean 4 core only, no axioms.
-/
import ym.HeatL1All
import ym.HeatLRUpperK

namespace HeatFirstStep

open YMChain LinkIntegral SU2Char QBinom HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- local transitivity of the LR order through a middle sequence positive at n and n+1 -/
theorem lr_at (a b c : Nat → R) (n : Nat) (hb0 : O.le K.zero (b n)) (hb1 : O.le K.zero (b (n + 1)))
    (hbn0 : b n ≠ K.zero) (hbn1 : b (n + 1) ≠ K.zero) (ha : O.le K.zero (a n)) (hc : O.le K.zero (c n))
    (h1 : DecLR.LRat K O a b n) (h2 : DecLR.LRat K O b c n) : DecLR.LRat K O a c n := by
  unfold DecLR.LRat at *
  apply O.pos_cancel _ _ (K.mul (b n) (b (n + 1))) (O.mul_nonneg _ _ hb0 hb1)
    (fun e => hbn1 (O.pos_mul_eq_zero _ _ hb0 hbn0 e))
  have s1 := O.mul_le_mul_right _ _ _ h1 (O.mul_nonneg _ _ hb1 hc)
  have s2 := O.mul_le_mul_right _ _ _ h2 (O.mul_nonneg _ _ ha hb1)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
    (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ ?_))))
  · lib_ring
  · lib_ring
  · lib_ring

theorem C_ext (H H' : Nat → R) (L n : Nat) (h : ∀ j, j < L → H j = H' j) :
    SecondRingC1.C K H L n = SecondRingC1.C K H' L n := by
  unfold SecondRingC1.C
  apply SecondRing.sumN_ext_lt K _ _ L; intro j hj
  apply SecondRing.sumN_ext_lt K _ _ L; intro k hk
  rw [h j hj, h k hk]

/-- the list product of the heat list with q = Q² is the heat product C(hH Q) -/
theorem heat_C (Q : R) (L n : Nat) :
    cgc K (HeatDec.heat K (pwR K Q 2) 0 L) (HeatDec.heat K (pwR K Q 2) 0 L) n = SecondRingC1.C K (SecondRingC1.hH K Q) L n := by
  rw [SecondRingStep.cgc_eq, SecondRing.len_heat]
  apply C_ext; intro j hj
  rw [HeatGood.coY K (pwR K Q 2) L j hj]
  unfold HalfRatio.Yh SecondRingC1.hH
  rw [Migdal.pw_pw, show 2 * (j * (j + 2)) = j * (j + 2) + j * (j + 2) by lib_nat]

/-- positivity of the heat product inside its support -/
theorem C_ne (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q) (hQne : Q ≠ K.zero)
    (M n : Nat) (hn : n ≤ 2 * M + 2) : SecondRingC1.C K (SecondRingC1.hH K Q) (M + 2) n ≠ K.zero := by
  have hHn := SecondRingC1.hH_nn K O h1 Q hQ0
  have Hne : ∀ j, SecondRingC1.hH K Q j ≠ K.zero := fun j e => by
    unfold SecondRingC1.hH at e
    exact HeatGood.pw_ne K O h1 Q hQ0 hQne h10 _ (O.pos_mul_eq_zero _ _ (SU2CharExp.cN_nn K O h1 _) (GridQ.cN_pos K O h1 h10 j) e)
  have key : ∀ a b, a < M + 2 → b < M + 2 → a + b = n → SecondRingC1.C K (SecondRingC1.hH K Q) (M + 2) n ≠ K.zero := by
    intro a b ha hb hab
    have t := HeatLower.C_ge_term K O h1 (SecondRingC1.hH K Q) hHn (M + 2) a b ha hb
    rw [hab] at t
    exact L1Full.ne_of_ge K O _ _ (O.mul_nonneg _ _ (hHn a) (hHn b)) t
      (fun e => Hne b (O.pos_mul_eq_zero _ _ (hHn a) (Hne a) e))
  cases Nat.lt_or_ge n (M + 2) with
  | inl h => exact key n 0 h (Nat.zero_lt_succ _) (Nat.add_zero n)
  | inr h =>
    obtain ⟨e, he⟩ := HeatAssemble.ex_add (M + 1) n (Nat.le_of_succ_le h)
    have heM : e < M + 2 := by
      have t : M + 1 + e < M + 1 + (M + 2) := by
        rw [← he, show M + 1 + (M + 2) = 2 * M + 3 by lib_nat]; exact Nat.lt_succ_of_le hn
      exact Nat.lt_of_add_lt_add_left t
    exact key (M + 1) e (Nat.lt_succ_self _) heM he.symm

/-- **G60 (a)**: one product of a weight below K_{Q²} lies below K_Q, at every level -/
theorem first_step (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (hQne : Q ≠ K.zero) (h2Q : O.le (K.mul (cN K 2) Q) K.one)
    (Xl : List R) (hXg : L1Full.Good K O Xl) (M : Nat) (hX : Xl.length = M + 2)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K (HeatDec.heat K (pwR K Q 2) 0 (M + 2)) k) (g k)) (n : Nat) :
    DecLR.LRat K O (cgc K Xl Xl) (HeatLRUpperK.kQ K Q) n := by
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have ⟨xb, _, _, _⟩ := hXg
  -- conditions on q = Q²
  have hQ1 : O.le Q K.one := by
    have a := O.mul_le_mul_right _ _ Q (SU2CharExp.cN_mono K O h1 1 2 (by decide)) hQ0
    rw [show K.mul (cN K 1) Q = Q by unfold cN; lib_ring] at a
    exact O.le_trans _ _ _ a h2Q
  have q0 := AreaLower.pw_nn K O Q hQ0 h1 2
  have q1 : O.le (pwR K Q 2) K.one := Tail.pw_le_one K O h1 Q hQ0 hQ1 2
  have qne := HeatGood.pw_ne K O h1 Q hQ0 hQne h10 2
  have s2 := SecondRingAlg.sq_mono K O _ _ (O.mul_nonneg _ _ (nn 2) hQ0) h2Q
  rw [show K.mul (K.mul (cN K 2) Q) (K.mul (cN K 2) Q) = K.mul (cN K 4) (pwR K Q 2) by
    show _ = K.mul (cN K 4) (K.mul (K.mul K.one Q) Q); unfold cN; lib_ring, K.one_mul] at s2
  have s4 := SecondRingAlg.sq_mono K O _ _ (O.mul_nonneg _ _ (nn 4) q0) s2
  rw [show K.mul (K.mul (cN K 4) (pwR K Q 2)) (K.mul (cN K 4) (pwR K Q 2)) = K.mul (cN K 16) (pwR K (pwR K Q 2) 2) by
    show _ = K.mul (cN K 16) (K.mul (K.mul K.one (pwR K Q 2)) (pwR K Q 2)); unfold cN; lib_ring, K.one_mul] at s4
  have hq2 : O.le (K.mul (cN K 2) (pwR K (pwR K Q 2) 2)) K.one :=
    O.le_trans _ _ _ (O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 2 16 (by decide))
      (AreaLower.pw_nn K O _ q0 h1 2)) s4
  have Cnn := fun t => SecondRingA1.C_nn K O h1 (SecondRingC1.hH K Q) (SecondRingC1.hH_nn K O h1 Q hQ0) (M + 2) t
  have xxn := fun t => HeatL1All.cgc_nn K O Xl Xl (M + 1) hX xb xb t
  cases Nat.lt_or_ge (n + 1) (2 * M + 3) with
  | inl h =>
    have h1' := HeatL1All.heat_first K O F h1 h10 (pwR K Q 2) q0 q1 qne hq2 Xl hXg M hX g hg hg0 hxy n
    rw [heat_C, heat_C] at h1'
    exact lr_at K O (cgc K Xl Xl) (SecondRingC1.C K (SecondRingC1.hH K Q) (M + 2)) (HeatLRUpperK.kQ K Q) n
      (Cnn n) (Cnn (n + 1))
      (C_ne K O h1 h10 Q hQ0 hQne M n (Nat.le_of_succ_le (Nat.le_of_lt_succ h)))
      (C_ne K O h1 h10 Q hQ0 hQne M (n + 1) (Nat.le_of_lt_succ h))
      (xxn n) (HeatLRUpperK.kQ_nn K O Q h1 hQ0 n) h1'
      (HeatLRUpperK.lr_upper K O Q h1 h10 hQ0 h2Q (M + 2) n)
  | inr h =>
    unfold DecLR.LRat
    rw [L1Full.cgc_far K Xl M hX (n + 1) h, show K.mul K.zero (HeatLRUpperK.kQ K Q n) = K.zero by lib_ring]
    exact O.mul_nonneg _ _ (xxn n) (HeatLRUpperK.kQ_nn K O Q h1 hQ0 (n + 1))

end HeatFirstStep

#print axioms HeatFirstStep.lr_at
#print axioms HeatFirstStep.C_ext
#print axioms HeatFirstStep.heat_C
#print axioms HeatFirstStep.C_ne
#print axioms HeatFirstStep.first_step

#print axioms: 5 theorems, no axioms.

90.119 One Migdal–Kadanoff step keeps the class below \(K_{P^4}\)

For \(0<P\le 1/2\), a v-good weight below the heat list \(K_{P^4}\) stays below \(K_{P^4}\) after two bond-moving products and one decimation.

\[ X=K_{P^4}\,g\ (g\downarrow )\ \Longrightarrow \ \mathrm {dec}\big ((X\!\cdot \!X)\!\cdot \!(X\!\cdot \!X)\big )\preceq _{LR} K_{P^4} \]

ym/HeatMKClass.lean

/-
  ym/HeatMKClass — T45, G60 piece (d): one Migdal–Kadanoff step (two bond-moving products, then decimation)
  keeps a weight below the heat list K_{P⁴}: for 0 < P, 2P ≤ 1, if X is v-good of length M+2 with
  X = K_{P⁴}·g (g nonincreasing), then Z = decM((X·X)·(X·X)) satisfies Z ≼_LR K_{P⁴} at every level
  (against the heat list of the same length 4M+5). Lean 4 core only, no axioms.
-/
import ym.HeatSecondStep

namespace HeatMKClass

open YMChain LinkIntegral SU2Char QBinom HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem w_nn (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n : Nat) :
    O.le K.zero (DecLR.w K O F n) := by
  unfold DecLR.w
  exact AreaLower.pw_nn K O _ (F.inv_pos _ (SU2CharExp.cN_nn K O h1 _) (GridQ.cN_pos K O h1 h10 n)) h1 3

/-- **G60 (d)**: one MK step keeps the class "below K_{P⁴}" (0 < P ≤ 1/2) -/
theorem mk_class (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (P : R) (hP0 : O.le K.zero P)
    (hPne : P ≠ K.zero) (h2P : O.le (K.mul (cN K 2) P) K.one)
    (Xl : List R) (hXg : L1Full.Good K O Xl) (M : Nat) (hX : Xl.length = M + 2)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) k) (g k)) (n : Nat) :
    O.le (K.mul (co K (decM K O F 0 (L1Full.prodL K (L1Full.prodL K Xl M) (2 * M + 1))) (n + 1))
        (co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (4 * M + 5)) n))
      (K.mul (co K (decM K O F 0 (L1Full.prodL K (L1Full.prodL K Xl M) (2 * M + 1))) n)
        (co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (4 * M + 5)) (n + 1))) := by
  have ⟨xb, _, _, _⟩ := hXg
  have hYl := HeatSecondStep.len_prodL K (L1Full.prodL K Xl M) (2 * M + 1)
  have hY1 := HeatSecondStep.len_prodL K Xl M
  have coA := L1Full.co_prodL K (L1Full.prodL K Xl M) (2 * M + 1) hY1
  have hYg := L1Full.Good_prod K O Xl hXg M hX F
  have ⟨yb, _, _, _⟩ := hYg
  have Ann : ∀ t, O.le K.zero (co K (L1Full.prodL K (L1Full.prodL K Xl M) (2 * M + 1)) t) := fun t => by
    rw [coA t]; exact HeatL1All.cgc_nn K O _ _ (2 * M + 2) hY1 yb yb t
  have tp := HeatSecondStep.two_products K O F h1 h10 P hP0 hPne h2P Xl hXg M hX g hg hg0 hxy
  have L5 : 2 * (2 * M + 1) + 3 = 4 * M + 5 := by lib_nat
  have h : ∀ m, O.le (K.mul (co K (L1Full.prodL K (L1Full.prodL K Xl M) (2 * M + 1)) (m + 1)) (co K (HeatDec.heat K P 0 (4 * M + 5)) m))
      (K.mul (co K (L1Full.prodL K (L1Full.prodL K Xl M) (2 * M + 1)) m) (co K (HeatDec.heat K P 0 (4 * M + 5)) (m + 1))) := by
    intro m
    cases Nat.lt_or_ge (m + 1) (4 * M + 5) with
    | inl hm =>
      have t := tp m
      unfold DecLR.LRat at t
      rw [coA (m + 1), coA m, HeatGood.coY K P (4 * M + 5) m (Nat.lt_of_succ_lt hm), HeatGood.coY K P (4 * M + 5) (m + 1) hm]
      exact t
    | inr hm =>
      rw [HeatLR.co_out K _ (m + 1) (by rw [hYl, L5]; exact hm),
        show K.mul K.zero (co K (HeatDec.heat K P 0 (4 * M + 5)) m) = K.zero by lib_ring]
      exact O.mul_nonneg _ _ (Ann m) (HeatGood.co_nn K O h1 P hP0 (4 * M + 5) (m + 1))
  have d := DecLR.dec_LR_heat K O F h1 h10 P hP0 _ (4 * M + 5) Ann (w_nn K O F h1 h10) h n
  rw [Migdal.pw_pw K P 2 2] at ⊢
  exact d

end HeatMKClass

#print axioms HeatMKClass.w_nn
#print axioms HeatMKClass.mk_class

#print axioms: 2 theorems, no axioms.

90.120 Liggett-type log-concavity of a Cauchy square, \(n\le 50\)

For an adjacent-log-concave \(A\ge 0\) and \(X_k=(k+1)A_k\), the Cauchy square \(S=X\star X\) is log-concave after division by \(\binom {n+3}{3}\) at every level \(n\le 50\), for every length.

\[ \tbinom {n+2}{3}\tbinom {n+4}{3}\,S_n^2\ \ge \ \tbinom {n+3}{3}^2\,S_{n-1}S_{n+1}\qquad (1\le n\le 50) \]

ym/G61Lv5.lean

/-
  ym/G61Lv5 — T45, G61a at level n = 5 (all lengths, generated by work/cert/A/g61/gen61.py from an exact LP
  certificate): for X_k = (k+1)A_k with A ≥ 0 and the log-concavity minors e_{j,k} = A_{j+1}A_k − A_jA_{k+1} ≥ 0,
  and S = X⋆X, the Liggett-type inequality W_{n}² S_{n−1}S_{n+1} ≤ W_{n−1}W_{n+1} S_n² (W_m = C(m+3,3)),
  scaled by 1. Lean 4 core only, no axioms.
-/
import ym.TripleE
import ym.RingZ
import ym.HeatV
import ym.G61Minor

namespace G61Lv5

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem add_nn (x y : R) (hx : O.le K.zero x) (hy : O.le K.zero y) : O.le K.zero (K.add x y) := by
  have := O.add_le_add _ _ _ _ hx hy
  rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this

set_option maxRecDepth 100000 in
theorem level (h1 : O.le K.zero K.one) (a0 a1 a2 a3 a4 a5 a6 : R) (p0 : O.le K.zero a0) (p1 : O.le K.zero a1) (p2 : O.le K.zero a2) (p3 : O.le K.zero a3) (p4 : O.le K.zero a4) (p5 : O.le K.zero a5) (p6 : O.le K.zero a6)
    (e0_1 : O.le (K.mul a0 a2) (K.mul a1 a1)) (e0_2 : O.le (K.mul a0 a3) (K.mul a1 a2)) (e0_3 : O.le (K.mul a0 a4) (K.mul a1 a3)) (e0_4 : O.le (K.mul a0 a5) (K.mul a1 a4)) (e0_5 : O.le (K.mul a0 a6) (K.mul a1 a5)) (e1_2 : O.le (K.mul a1 a3) (K.mul a2 a2)) (e1_3 : O.le (K.mul a1 a4) (K.mul a2 a3)) (e1_4 : O.le (K.mul a1 a5) (K.mul a2 a4)) (e1_5 : O.le (K.mul a1 a6) (K.mul a2 a5)) (e2_3 : O.le (K.mu  -- (+235 chars; full line in the Zenodo source)
    O.le (K.mul (cN K 3136) (K.mul (K.add (K.add (K.add (K.add (K.mul (K.mul (cN K 1) a0) (K.mul (cN K 5) a4)) (K.mul (K.mul (cN K 2) a1) (K.mul (cN K 4) a3))) (K.mul (K.mul (cN K 3) a2) (K.mul (cN K 3) a2))) (K.mul (K.mul (cN K 4) a3) (K.mul (cN K 2) a1))) (K.mul (K.mul (cN K 5) a4) (K.mul (cN K 1) a0))) (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (cN K 1) a0) (K.mul (cN K 7) a6)) (K.  -- (+292 chars; full line in the Zenodo source)
      (K.mul (cN K 2940) (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (cN K 1) a0) (K.mul (cN K 6) a5)) (K.mul (K.mul (cN K 2) a1) (K.mul (cN K 5) a4))) (K.mul (K.mul (cN K 3) a2) (K.mul (cN K 4) a3))) (K.mul (K.mul (cN K 4) a3) (K.mul (cN K 3) a2))) (K.mul (K.mul (cN K 5) a4) (K.mul (cN K 2) a1))) (K.mul (K.mul (cN K 6) a5) (K.mul (cN K 1) a0))) (K.add (K.add (K.add (K.add (K.add (K.mu  -- (+295 chars; full line in the Zenodo source)
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have d0_1 := HeatV.sub_nn K O _ _ e0_1
  have d0_2 := HeatV.sub_nn K O _ _ e0_2
  have d0_3 := HeatV.sub_nn K O _ _ e0_3
  have d0_5 := HeatV.sub_nn K O _ _ e0_5
  have d1_2 := HeatV.sub_nn K O _ _ e1_2
  have d1_3 := HeatV.sub_nn K O _ _ e1_3
  have d1_4 := HeatV.sub_nn K O _ _ e1_4
  have d1_5 := HeatV.sub_nn K O _ _ e1_5
  have d2_3 := HeatV.sub_nn K O _ _ e2_3
  have d2_4 := HeatV.sub_nn K O _ _ e2_4
  have d2_5 := HeatV.sub_nn K O _ _ e2_5
  have d3_4 := HeatV.sub_nn K O _ _ e3_4
  have d3_5 := HeatV.sub_nn K O _ _ e3_5
  have d4_5 := HeatV.sub_nn K O _ _ e4_5
  have hs : O.le K.zero (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (cN K 159936) (K.mul (K.mul a3 a4) (K.add (K.mul a1 a2) (K.neg (K.mul a0 a3))))) (K.mul (cN K 43904) (K.mul (K.mul a3 a3) (K.add (K.mul a1 a3) (K.neg (K.mul a0 a4)))))) (K.mul (cN K 15680) (K.mul (K.mul a0 a4) (K.add (K.mul a1 a5) (K.neg (K.mul a0 a6)))))) (K.mul (cN K 395136)   -- (+2325 chars; full line in the Zenodo source)
  have u := HeatV.le_add_nn K O (K.mul (cN K 3136) (K.mul (K.add (K.add (K.add (K.add (K.mul (K.mul (cN K 1) a0) (K.mul (cN K 5) a4)) (K.mul (K.mul (cN K 2) a1) (K.mul (cN K 4) a3))) (K.mul (K.mul (cN K 3) a2) (K.mul (cN K 3) a2))) (K.mul (K.mul (cN K 4) a3) (K.mul (cN K 2) a1))) (K.mul (K.mul (cN K 5) a4) (K.mul (cN K 1) a0))) (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (cN K 1) a0) (  -- (+320 chars; full line in the Zenodo source)
  refine O.le_trans _ _ _ u (TripleE.le_of_eq' K O _ _ ?_)
  unfold cN; ring_z


/-- the same for a sequence: nonnegative, nonzero on 0..6, adjacent-log-concave -/
theorem level_fun (h1 : O.le K.zero K.one) (A : Nat → R) (hnn : ∀ i, O.le K.zero (A i))
    (hne : ∀ i, i ≤ 6 → A i ≠ K.zero)
    (hlc : ∀ k, O.le (K.mul (A k) (A (k + 2))) (K.mul (A (k + 1)) (A (k + 1)))) :
    O.le (K.mul (cN K 3136) (K.mul (K.add (K.add (K.add (K.add (K.mul (K.mul (cN K 1) (A 0)) (K.mul (cN K 5) (A 4))) (K.mul (K.mul (cN K 2) (A 1)) (K.mul (cN K 4) (A 3)))) (K.mul (K.mul (cN K 3) (A 2)) (K.mul (cN K 3) (A 2)))) (K.mul (K.mul (cN K 4) (A 3)) (K.mul (cN K 2) (A 1)))) (K.mul (K.mul (cN K 5) (A 4)) (K.mul (cN K 1) (A 0)))) (K.add (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (cN K 1  -- (+364 chars; full line in the Zenodo source)
      (K.mul (cN K 2940) (K.mul (K.add (K.add (K.add (K.add (K.add (K.mul (K.mul (cN K 1) (A 0)) (K.mul (cN K 6) (A 5))) (K.mul (K.mul (cN K 2) (A 1)) (K.mul (cN K 5) (A 4)))) (K.mul (K.mul (cN K 3) (A 2)) (K.mul (cN K 4) (A 3)))) (K.mul (K.mul (cN K 4) (A 3)) (K.mul (cN K 3) (A 2)))) (K.mul (K.mul (cN K 5) (A 4)) (K.mul (cN K 2) (A 1)))) (K.mul (K.mul (cN K 6) (A 5)) (K.mul (cN K 1) (A 0)))) (K.a  -- (+364 chars; full line in the Zenodo source)
  level K O h1 (A 0) (A 1) (A 2) (A 3) (A 4) (A 5) (A 6) (hnn 0) (hnn 1) (hnn 2) (hnn 3) (hnn 4) (hnn 5) (hnn 6)
    (G61Minor.minor K O A 6 hnn hne hlc 0 0 (by decide)) (G61Minor.minor K O A 6 hnn hne hlc 1 0 (by decide)) (G61Minor.minor K O A 6 hnn hne hlc 2 0 (by decide)) (G61Minor.minor K O A 6 hnn hne hlc 3 0 (by decide)) (G61Minor.minor K O A 6 hnn hne hlc 4 0 (by decide)) (G61Minor.minor K O A 6 hnn hne hlc 0 1 (by decide)) (G61Minor.minor K O A 6 hnn hne hlc 1 1 (by decide)) (G61Minor.minor K O A 6 h  -- (+398 chars; full line in the Zenodo source)

end G61Lv5

#print axioms G61Lv5.add_nn
#print axioms G61Lv5.level
#print axioms G61Lv5.level_fun

#print axioms: 249 theorems (G61Base, G61N2, G61Minor, G61Lv2–G61Lv50, G61Upto), no axioms.

90.121 Top-index peeling for the Liggett-type inequality

For every level, an explicit nonnegative sum of gapped minors removes \(A_{n+2}\) from the deficit; the remainder involves only \(A_0,\dots ,A_{n+1}\).

\[ D_{n+1}-P_n\ge 0\ \Rightarrow \ W_{n+1}^2S_nS_{n+2}\le W_nW_{n+2}S_{n+1}^2,\qquad P_n=\sum _{i+d=n}2(n+3)W_{n+1}^2(i+1)(d+1)\,e_{i,n+1}A_0A_d\ge 0 \]

ym/G61Peel.lean

/-
  ym/G61Peel — T45, G61a: uniform peeling of the top index, for every level.
  At level N = n+1 the deficit D_N = W_n W_{n+2} S_{n+1}² − W_{n+1}² S_n S_{n+2} (G61Upto.Stmt, W_m = C(m+3,3)) carries
  A_{n+2} only through −W_{n+1}²·S_n·2(n+3)A_0A_{n+2}; the explicit sum
    P_n = Σ_{i+d=n} 2(n+3) W_{n+1}² (i+1)(d+1) · (A_{i+1}A_{i+d+1} − A_iA_{i+d+2}) · A_0 A_d
  of gapped log-concavity minors (nonnegative by G61Minor) removes it:
    `peel_reduce`  D_N − P_n ≥ 0 implies the level-N inequality;
    `peeled_indep` D_N − P_n is the same for two sequences agreeing on 0..n+1 (it does not involve A_{n+2}).
  Lean 4 core only, no axioms.
-/
import ym.G61Upto
import lib.NatSub

namespace G61Peel

open YMChain LinkIntegral SU2Char G61Upto

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- the minor-times-monomial term with index i and gap d, scaled by κ -/
def term (A : Nat → R) (κ : R) (i d : Nat) : R :=
  K.mul κ (K.mul (K.mul (cN K (i + 1)) (cN K (d + 1)))
    (K.mul (K.add (K.mul (A (i + 1)) (A (i + d + 1))) (K.neg (K.mul (A i) (A (i + d + 2)))))
      (K.mul (A 0) (A d))))

/-- Σ_{k=0}^{d} term (i+k) (d−k), written without subtraction -/
def pAux (A : Nat → R) (κ : R) : Nat → Nat → R
  | i, 0 => term K A κ i 0
  | i, d + 1 => K.add (term K A κ i (d + 1)) (pAux A κ (i + 1) d)

/-- the peeling constant 2(n+3)W_{n+1}² -/
def kap (n : Nat) : R := K.mul (K.mul (cN K 2) (cN K (n + 3))) (K.mul (cN K (wt (n + 1))) (cN K (wt (n + 1))))

/-- the peeling sum at level n+1 -/
def peel (A : Nat → R) (n : Nat) : R := pAux K A (kap K n) 0 n

/-- the peeled deficit D_{n+1} − P_n -/
def peeled (A : Nat → R) (n : Nat) : R :=
  K.add (K.add (K.mul (cN K (wt n * wt (n + 2))) (K.mul (sq K A (n + 1)) (sq K A (n + 1))))
    (K.neg (K.mul (cN K (wt (n + 1) * wt (n + 1))) (K.mul (sq K A n) (sq K A (n + 2))))))
    (K.neg (peel K A n))

theorem add_nn (x y : R) (hx : O.le K.zero x) (hy : O.le K.zero y) : O.le K.zero (K.add x y) := by
  have := O.add_le_add _ _ _ _ hx hy
  rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this

theorem kap_nn (h1 : O.le K.zero K.one) (n : Nat) : O.le K.zero (kap K n) :=
  O.mul_nonneg _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (SU2CharExp.cN_nn K O h1 _))
    (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (SU2CharExp.cN_nn K O h1 _))

theorem term_nn (h1 : O.le K.zero K.one) (A : Nat → R) (hnn : ∀ i, O.le K.zero (A i)) (N : Nat)
    (hne : ∀ i, i ≤ N → A i ≠ K.zero)
    (hlc : ∀ k, O.le (K.mul (A k) (A (k + 2))) (K.mul (A (k + 1)) (A (k + 1))))
    (κ : R) (hκ : O.le K.zero κ) (i d : Nat) (h : i + d + 1 ≤ N) : O.le K.zero (term K A κ i d) := by
  have m := G61Minor.minor K O A N hnn hne hlc d i h
  exact O.mul_nonneg _ _ hκ (O.mul_nonneg _ _
    (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (SU2CharExp.cN_nn K O h1 _))
    (O.mul_nonneg _ _ (HeatV.sub_nn K O _ _ m) (O.mul_nonneg _ _ (hnn 0) (hnn d))))

theorem pAux_nn (h1 : O.le K.zero K.one) (A : Nat → R) (hnn : ∀ i, O.le K.zero (A i)) (N : Nat)
    (hne : ∀ i, i ≤ N → A i ≠ K.zero)
    (hlc : ∀ k, O.le (K.mul (A k) (A (k + 2))) (K.mul (A (k + 1)) (A (k + 1)))) (κ : R) (hκ : O.le K.zero κ) :
    ∀ d i, i + d + 1 ≤ N → O.le K.zero (pAux K A κ i d)
  | 0, i, h => term_nn K O h1 A hnn N hne hlc κ hκ i 0 h
  | d + 1, i, h => by
    have h' : (i + 1) + d + 1 ≤ N := by
      have e : (i + 1) + d + 1 = i + (d + 1) + 1 := by lib_nat
      rw [e]; exact h
    exact add_nn K O _ _ (term_nn K O h1 A hnn N hne hlc κ hκ i (d + 1) h)
      (pAux_nn h1 A hnn N hne hlc κ hκ d (i + 1) h')

/-- the peeling sum is nonnegative for adjacent-log-concave A ≥ 0, nonzero on 0..n+1 -/
theorem peel_nn (h1 : O.le K.zero K.one) (A : Nat → R) (hnn : ∀ i, O.le K.zero (A i)) (n : Nat)
    (hne : ∀ i, i ≤ n + 1 → A i ≠ K.zero)
    (hlc : ∀ k, O.le (K.mul (A k) (A (k + 2))) (K.mul (A (k + 1)) (A (k + 1)))) :
    O.le K.zero (peel K A n) :=
  pAux_nn K O h1 A hnn (n + 1) hne hlc _ (kap_nn K O h1 n) n 0 (by
    have e : 0 + n + 1 = n + 1 := by lib_nat
    rw [e]; exact Nat.le_refl _)

/-- **reduction**: if the peeled deficit is nonnegative then the level-(n+1) inequality holds -/
theorem peel_reduce (h1 : O.le K.zero K.one) (A : Nat → R) (hnn : ∀ i, O.le K.zero (A i)) (n : Nat)
    (hne : ∀ i, i ≤ n + 1 → A i ≠ K.zero)
    (hlc : ∀ k, O.le (K.mul (A k) (A (k + 2))) (K.mul (A (k + 1)) (A (k + 1))))
    (hD : O.le K.zero (peeled K A n)) : Stmt K O A n := by
  have hP := peel_nn K O h1 A hnn n hne hlc
  have s := O.add_le_add _ _ _ _ hD hP
  have s2 := O.add_le_add _ _ _ _ s
    (O.le_refl (K.mul (cN K (wt (n + 1) * wt (n + 1))) (K.mul (sq K A n) (sq K A (n + 2)))))
  unfold Stmt
  unfold peeled at s2
  exact (show K.add (K.add K.zero K.zero) (K.mul (cN K (wt (n + 1) * wt (n + 1))) (K.mul (sq K A n) (sq K A (n + 2))))
      = K.mul (cN K (wt (n + 1) * wt (n + 1))) (K.mul (sq K A n) (sq K A (n + 2))) by lib_ring) ▸
    (show K.add (K.add (K.add (K.add (K.mul (cN K (wt n * wt (n + 2))) (K.mul (sq K A (n + 1)) (sq K A (n + 1))))
        (K.neg (K.mul (cN K (wt (n + 1) * wt (n + 1))) (K.mul (sq K A n) (sq K A (n + 2))))))
        (K.neg (peel K A n))) (peel K A n)) (K.mul (cN K (wt (n + 1) * wt (n + 1))) (K.mul (sq K A n) (sq K A (n + 2))))
      = K.mul (cN K (wt n * wt (n + 2))) (K.mul (sq K A (n + 1)) (sq K A (n + 1))) by lib_ring) ▸ s2

/-! ### independence of A_{n+2} -/

theorem sumS_congr (f g : Nat → R) : ∀ m, (∀ j, j ≤ m → f j = g j) → sumS K f m = sumS K g m
  | 0, h => h 0 (Nat.le_refl 0)
  | m + 1, h => by
    show K.add (sumS K f m) (f (m + 1)) = K.add (sumS K g m) (g (m + 1))
    rw [sumS_congr f g m (fun j hj => h j (Nat.le_succ_of_le hj)), h (m + 1) (Nat.le_refl _)]

/-- sums agreeing except at index 0 -/
theorem sumS_diff (f g : Nat → R) : ∀ m, (∀ j, 1 ≤ j → j ≤ m → f j = g j) →
    sumS K f m = K.add (sumS K g m) (K.add (f 0) (K.neg (g 0)))
  | 0, _ => by show f 0 = K.add (g 0) (K.add (f 0) (K.neg (g 0))); lib_ring
  | m + 1, h => by
    show K.add (sumS K f m) (f (m + 1)) = K.add (K.add (sumS K g m) (g (m + 1))) (K.add (f 0) (K.neg (g 0)))
    rw [sumS_diff f g m (fun j h1 hj => h j h1 (Nat.le_succ_of_le hj)), h (m + 1) (Nat.succ_le_succ (Nat.zero_le m)) (Nat.le_refl _)]
    lib_ring

theorem sub_le' (a b : Nat) : a - b ≤ a := by
  cases Nat.le_total b a with
  | inl hba => exact Nat.le.intro (LibNatSub.sub_add hba)
  | inr hab => rw [LibNatSub.sub_eq_zero hab]; exact Nat.zero_le a

theorem sq_agree (A B : Nat → R) (M : Nat) (hAB : ∀ j, j ≤ M → A j = B j) (m : Nat) (hm : m ≤ M) :
    sq K A m = sq K B m := by
  unfold G61Upto.sq
  apply sumS_congr
  intro j hj
  show K.mul (K.mul (cN K (j + 1)) (A j)) (K.mul (cN K (m - j + 1)) (A (m - j)))
    = K.mul (K.mul (cN K (j + 1)) (B j)) (K.mul (cN K (m - j + 1)) (B (m - j)))
  rw [hAB j (Nat.le_trans hj hm), hAB (m - j) (Nat.le_trans (sub_le' m j) hm)]

theorem self_sub (a : Nat) : a - a = 0 := by
  have := LibNatSub.add_sub_r 0 a
  rw [Nat.zero_add] at this; exact this

/-- the top sum changes only through the two corner terms -/
theorem sq_top (A B : Nat → R) (n : Nat) (hAB : ∀ j, j ≤ n + 1 → A j = B j) :
    sq K A (n + 2) = K.add (sq K B (n + 2))
      (K.mul (K.mul (cN K 2) (cN K (n + 3))) (K.mul (B 0) (K.add (A (n + 2)) (K.neg (B (n + 2)))))) := by
  have key := sumS_diff K (fun j => K.mul (K.mul (cN K (j + 1)) (A j)) (K.mul (cN K (n + 2 - j + 1)) (A (n + 2 - j))))
    (fun j => K.mul (K.mul (cN K (j + 1)) (B j)) (K.mul (cN K (n + 2 - j + 1)) (B (n + 2 - j)))) (n + 1)
    (fun j h1 hj => by
      cases j with
      | zero => exact absurd h1 (Nat.not_succ_le_zero 0)
      | succ j' =>
        have e : n + 2 - (j' + 1) = n + 1 - j' := Nat.succ_sub_succ (n + 1) j'
        show K.mul (K.mul (cN K (j' + 1 + 1)) (A (j' + 1))) (K.mul (cN K (n + 2 - (j' + 1) + 1)) (A (n + 2 - (j' + 1))))
          = K.mul (K.mul (cN K (j' + 1 + 1)) (B (j' + 1))) (K.mul (cN K (n + 2 - (j' + 1) + 1)) (B (n + 2 - (j' + 1))))
        rw [e, hAB (j' + 1) hj, hAB (n + 1 - j') (sub_le' (n + 1) j')])
  have e0 : n + 2 - (n + 2) = 0 := self_sub (n + 2)
  have e01 : n + 2 - (n + 2) + 1 = 1 := by rw [e0]
  have e2 : cN K 2 = K.add (cN K 1) (cN K 1) := cN_add K 1 1
  unfold G61Upto.sq
  show K.add (sumS K (fun j => K.mul (K.mul (cN K (j + 1)) (A j)) (K.mul (cN K (n + 2 - j + 1)) (A (n + 2 - j)))) (n + 1))
      (K.mul (K.mul (cN K (n + 3)) (A (n + 2))) (K.mul (cN K (n + 2 - (n + 2) + 1)) (A (n + 2 - (n + 2)))))
    = K.add (K.add (sumS K (fun j => K.mul (K.mul (cN K (j + 1)) (B j)) (K.mul (cN K (n + 2 - j + 1)) (B (n + 2 - j)))) (n + 1))
      (K.mul (K.mul (cN K (n + 3)) (B (n + 2))) (K.mul (cN K (n + 2 - (n + 2) + 1)) (B (n + 2 - (n + 2))))))
      (K.mul (K.mul (cN K 2) (cN K (n + 3))) (K.mul (B 0) (K.add (A (n + 2)) (K.neg (B (n + 2))))))
  rw [key]
  show K.add (K.add (sumS K _ (n + 1)) (K.add (K.mul (K.mul (cN K 1) (A 0)) (K.mul (cN K (n + 3)) (A (n + 2))))
      (K.neg (K.mul (K.mul (cN K 1) (B 0)) (K.mul (cN K (n + 3)) (B (n + 2)))))))
      (K.mul (K.mul (cN K (n + 3)) (A (n + 2))) (K.mul (cN K (n + 2 - (n + 2) + 1)) (A (n + 2 - (n + 2))))) = _
  rw [e01, e0, hAB 0 (Nat.zero_le _), e2]
  lib_ring

/-- the weight sum Σ_{k} (i+k+1)(d−k+1) B_{i+k} B_{d−k}, written without subtraction -/
def aux (g : Nat → Nat → R) : Nat → Nat → R
  | i, 0 => g i 0
  | i, d + 1 => K.add (g i (d + 1)) (aux g (i + 1) d)

theorem sumS_shift (h : Nat → R) : ∀ d, sumS K h (d + 1) = K.add (h 0) (sumS K (fun k => h (k + 1)) d)
  | 0 => rfl
  | d + 1 => by
    show K.add (sumS K h (d + 1)) (h (d + 1 + 1)) = K.add (h 0) (K.add (sumS K (fun k => h (k + 1)) d) (h (d + 1 + 1)))
    rw [sumS_shift h d]; lib_ring

theorem aux_rev (g : Nat → Nat → R) : ∀ d i, aux K g i d = sumS K (fun k => g (k + i) (d - k)) d
  | 0, i => by
    show g i 0 = g (0 + i) (0 - 0)
    rw [Nat.zero_add]
  | d + 1, i => by
    show K.add (g i (d + 1)) (aux K g (i + 1) d) = _
    rw [aux_rev g d (i + 1), sumS_shift K _ d]
    show _ = K.add (g (0 + i) (d + 1 - 0)) _
    rw [Nat.zero_add]
    have c := sumS_congr K (fun k => g (k + (i + 1)) (d - k)) (fun k => g (k + 1 + i) (d + 1 - (k + 1))) d
      (fun k _ => by
        show g (k + (i + 1)) (d - k) = g (k + 1 + i) (d + 1 - (k + 1))
        have e1 : k + 1 + i = k + (i + 1) := by lib_nat
        rw [e1, Nat.succ_sub_succ d k])
    rw [c]; rfl

theorem term_diff (A B : Nat → R) (n : Nat) (hAB : ∀ j, j ≤ n + 1 → A j = B j) (κ : R) (i d : Nat)
    (hid : i + d = n) :
    term K A κ i d = K.add (term K B κ i d)
      (K.neg (K.mul (K.mul κ (K.mul (B 0) (K.add (A (n + 2)) (K.neg (B (n + 2))))))
        (K.mul (K.mul (cN K (i + 1)) (B i)) (K.mul (cN K (d + 1)) (B d))))) := by
  have e : i + d + 2 = n + 2 := by rw [hid]
  have hid1 : i + d + 1 ≤ n + 1 := Nat.le_of_eq (by rw [hid])
  have hi1 : i + 1 ≤ n + 1 := Nat.succ_le_succ (hid ▸ Nat.le_add_right i d)
  have hi : i ≤ n + 1 := Nat.le_trans (Nat.le_succ i) hi1
  have hd : d ≤ n + 1 := Nat.le_trans (hid ▸ Nat.le_add_left d i) (Nat.le_succ n)
  unfold term
  rw [e, hAB (i + 1) hi1, hAB (i + d + 1) hid1, hAB i hi, hAB 0 (Nat.zero_le _), hAB d hd]
  lib_ring

theorem pAux_diff (A B : Nat → R) (n : Nat) (hAB : ∀ j, j ≤ n + 1 → A j = B j) (κ : R) :
    ∀ d i, i + d = n → pAux K A κ i d = K.add (pAux K B κ i d)
      (K.neg (K.mul (K.mul κ (K.mul (B 0) (K.add (A (n + 2)) (K.neg (B (n + 2))))))
        (aux K (fun k e => K.mul (K.mul (cN K (k + 1)) (B k)) (K.mul (cN K (e + 1)) (B e))) i d)))
  | 0, i, hid => term_diff K A B n hAB κ i 0 hid
  | d + 1, i, hid => by
    have hid' : i + 1 + d = n := by rw [← hid]; lib_nat
    show K.add (term K A κ i (d + 1)) (pAux K A κ (i + 1) d) = K.add (K.add (term K B κ i (d + 1)) (pAux K B κ (i + 1) d))
      (K.neg (K.mul (K.mul κ (K.mul (B 0) (K.add (A (n + 2)) (K.neg (B (n + 2))))))
        (K.add (K.mul (K.mul (cN K (i + 1)) (B i)) (K.mul (cN K (d + 1 + 1)) (B (d + 1))))
          (aux K (fun k e => K.mul (K.mul (cN K (k + 1)) (B k)) (K.mul (cN K (e + 1)) (B e))) (i + 1) d))))
    rw [term_diff K A B n hAB κ i (d + 1) hid, pAux_diff A B n hAB κ d (i + 1) hid']
    lib_ring

/-- **independence**: the peeled deficit D_{n+1} − P_n does not involve A_{n+2} (nor anything above n+1) -/
theorem peeled_indep (A B : Nat → R) (n : Nat) (hAB : ∀ j, j ≤ n + 1 → A j = B j) :
    peeled K A n = peeled K B n := by
  have r : aux K (fun k e => K.mul (K.mul (cN K (k + 1)) (B k)) (K.mul (cN K (e + 1)) (B e))) 0 n = sq K B n :=
    (aux_rev K _ n 0).trans rfl
  unfold peeled peel
  rw [sq_agree K A B (n + 1) hAB n (Nat.le_succ n), sq_agree K A B (n + 1) hAB (n + 1) (Nat.le_refl _),
    sq_top K A B n hAB, pAux_diff K A B n hAB (kap K n) n 0 (Nat.zero_add n), r,
    SU2CharExp.cN_mul K (wt (n + 1)) (wt (n + 1))]
  unfold kap
  lib_ring

end G61Peel

#print axioms G61Peel.add_nn
#print axioms G61Peel.kap_nn
#print axioms G61Peel.term_nn
#print axioms G61Peel.pAux_nn
#print axioms G61Peel.peel_nn
#print axioms G61Peel.peel_reduce
#print axioms G61Peel.sumS_congr
#print axioms G61Peel.sumS_diff
#print axioms G61Peel.sub_le'
#print axioms G61Peel.sq_agree
#print axioms G61Peel.self_sub
#print axioms G61Peel.sq_top
#print axioms G61Peel.sumS_shift
#print axioms G61Peel.aux_rev
#print axioms G61Peel.term_diff
#print axioms G61Peel.pAux_diff
#print axioms G61Peel.peeled_indep

#print axioms: 17 theorems, no axioms.

90.122 Heat weights at \(Q\le 0.63\) enter the strong-coupling cone

A uniform ratio bound \(x_{k+1}\le \sigma x_k\) becomes \((4\sigma )^4\) after one 4D Migdal step, so heat weights with \(2q^3\le 1/8\) reach Cone(\(2^{-8}\)) in four steps and stay there.

\[ 2q^3\le 2^{-3}\ \Longrightarrow \ \mathrm {mk}^{k+4}(1,\mathrm {heat}_q)\in \mathrm {Cone}(2^{-8})\quad (k\ge 0) \]

ym/RatioStep.lean

/-
  ym/RatioStep — T45, G57 (uniform ratio class under one 4D Migdal step): for a normalised weight 1 :: a whose
  entries satisfy x_{k+1} ≤ σ x_k (x ≥ 0), the normalised product has ratio 2σ, and decimation raises a ratio τ to τ⁴;
  so one step MigdalCone.mk maps ratio σ to (4σ)⁴ = 256σ⁴. Lean 4 core only, no axioms.
-/
import ym.RatioClass
import ym.SecondRingStep
import ym.Lambert
import ym.UFlowAB

namespace RatioStep

open YMChain LinkIntegral SU2Char HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

def RC (σ : R) (l : List R) : Prop := (∀ k, O.le K.zero (co K l k)) ∧ ∀ k, O.le (co K l (k + 1)) (K.mul σ (co K l k))

theorem mem_nn : ∀ (l : List R), (∀ k, O.le K.zero (co K l k)) → ∀ x, memL x l → O.le K.zero x
  | [], _, _, h => False.elim h
  | y :: l, h, x, hx => by
    cases hx with
    | inl e => rw [e]; exact h 0
    | inr hx => exact mem_nn l (fun k => h (k + 1)) x hx

theorem co_lmap (f : Nat → R) : ∀ L m n, n < L → co K (lmap f m L) n = f (m + n)
  | 0, _, _, h => absurd h (Nat.not_lt_zero _)
  | L + 1, m, 0, _ => rfl
  | L + 1, m, n + 1, h => by
    show co K (lmap f (m + 1) L) n = _
    rw [co_lmap f L (m + 1) n (Nat.lt_of_succ_lt_succ h), show m + 1 + n = m + (n + 1) by lib_nat]

theorem co_lmap_ge (f : Nat → R) : ∀ L m n, L ≤ n → co K (lmap f m L) n = K.zero
  | 0, _, _, _ => rfl
  | L + 1, m, 0, h => absurd h (Nat.not_succ_le_zero _)
  | L + 1, m, n + 1, h => co_lmap_ge f L (m + 1) n (Nat.le_of_succ_le_succ h)

/-- **product**: ratio σ for 1 :: a gives ratio 2σ for the normalised product -/
theorem nprod_ratio (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (σ : R) (hσ : O.le K.zero σ) (a : List R)
    (ha : RC K O σ (K.one :: a)) (L : Nat) : RC K O (K.add σ σ) (nprod K O F (K.one :: a) (K.one :: a) L) := by
  have hann := mem_nn K O _ ha.1
  have c0 := c0_ge_one K O h1 a a (fun x hx => hann x (Or.inr hx)) (fun x hx => hann x (Or.inr hx))
  have c0n : O.le K.zero (cgc K (K.one :: a) (K.one :: a) 0) := O.le_trans _ _ _ h1 c0
  have c0ne : cgc K (K.one :: a) (K.one :: a) 0 ≠ K.zero := fun e => by rw [e] at c0; exact h10 (O.le_antisymm _ _ c0 h1)
  have i0 := F.inv_pos _ c0n c0ne
  have Cn : ∀ n, O.le K.zero (cgc K (K.one :: a) (K.one :: a) n) := fun n => by
    rw [SecondRingStep.cgc_eq]; exact SecondRingA1.C_nn K O h1 _ ha.1 _ n
  have nn : ∀ n, O.le K.zero (co K (nprod K O F (K.one :: a) (K.one :: a) L) n) := fun n => by
    unfold nprod
    cases Nat.lt_or_ge n L with
    | inl h => rw [co_lmap K _ L 0 n h]; exact O.mul_nonneg _ _ (Cn _) i0
    | inr h => rw [co_lmap_ge K _ L 0 n h]; exact O.le_refl _
  refine ⟨nn, fun k => ?_⟩
  cases Nat.lt_or_ge (k + 1) L with
  | inr h =>
    unfold nprod; rw [co_lmap_ge K _ L 0 (k + 1) h]
    exact O.mul_nonneg _ _ (by
      have := O.add_le_add _ _ _ _ hσ hσ; rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this) (nn k)
  | inl h =>
    unfold nprod
    rw [co_lmap K _ L 0 (k + 1) h, co_lmap K _ L 0 k (Nat.lt_of_succ_lt h), Nat.zero_add, Nat.zero_add]
    have s := RatioClass.shift_sigma K O h1 (co K (K.one :: a)) ha.1 σ hσ ha.2 k (K.one :: a).length
    rw [← SecondRingStep.cgc_eq, ← SecondRingStep.cgc_eq] at s
    have := O.mul_le_mul_right _ _ _ s i0
    rw [show K.mul (K.mul (K.add σ σ) (cgc K (K.one :: a) (K.one :: a) k)) (F.inv (cgc K (K.one :: a) (K.one :: a) 0))
      = K.mul (K.add σ σ) (K.mul (cgc K (K.one :: a) (K.one :: a) k) (F.inv (cgc K (K.one :: a) (K.one :: a) 0))) by lib_ring] at this
    exact this

theorem co_decM : ∀ (l : List R) (m n : Nat),
    co K (decM K O F m l) n = K.mul (pwR K (co K l n) 4) (pwR K (F.inv (cN K (m + n + 1))) 3)
  | [], _, _ => by
    show K.zero = K.mul (pwR K K.zero 4) _
    show K.zero = K.mul (K.mul (K.mul (K.mul (K.mul K.one K.zero) K.zero) K.zero) K.zero) _; lib_ring
  | _ :: _, _, 0 => rfl
  | _ :: l, m, n + 1 => by
    show co K (decM K O F (m + 1) l) n = _
    rw [co_decM l (m + 1) n, show m + 1 + n + 1 = m + (n + 1) + 1 by lib_nat]; rfl

/-- **decimation**: ratio τ becomes τ⁴ -/
theorem decM_ratio (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (τ : R) (hτ : O.le K.zero τ) (l : List R)
    (hl : RC K O τ l) : RC K O (pwR K τ 4) (decM K O F 0 l) := by
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have ipos : ∀ k, O.le K.zero (F.inv (cN K (k + 1))) := fun k => F.inv_pos _ (nn _) (GridQ.cN_pos K O h1 h10 k)
  refine ⟨fun k => ?_, fun k => ?_⟩
  · rw [co_decM]
    exact O.mul_nonneg _ _ (AreaLower.pw_nn K O _ (hl.1 k) h1 4) (AreaLower.pw_nn K O _ (by rw [Nat.zero_add]; exact ipos k) h1 3)
  · rw [co_decM, co_decM, Nat.zero_add, Nat.zero_add]
    have a := SU2Strong4D.pw_mono K O h1 _ _ (hl.1 (k + 1)) (hl.2 k) 4
    rw [GKS2.pwR_mul] at a
    have b := SU2Strong4D.pw_mono K O h1 _ _ (ipos (k + 1))
      (Lambert.inv_anti K O F _ _ (nn _) (GridQ.cN_pos K O h1 h10 k) (GridQ.cN_pos K O h1 h10 (k + 1))
        (SU2CharExp.cN_mono K O h1 (k + 1) (k + 1 + 1) (Nat.le_succ _))) 3
    have s1 := O.mul_le_mul_right _ _ _ a (AreaLower.pw_nn K O _ (ipos (k + 1)) h1 3)
    have s2 := O.mul_le_mul_right _ _ (K.mul (pwR K τ 4) (pwR K (co K l k) 4)) b
      (O.mul_nonneg _ _ (AreaLower.pw_nn K O τ hτ h1 4) (AreaLower.pw_nn K O _ (hl.1 k) h1 4))
    have t := O.le_trans _ _ _ s1 (by rw [K.mul_comm (K.mul (pwR K τ 4) (pwR K (co K l k) 4))]; exact s2)
    rw [show K.mul (pwR K (F.inv (cN K (k + 1))) 3) (K.mul (pwR K τ 4) (pwR K (co K l k) 4))
      = K.mul (pwR K τ 4) (K.mul (pwR K (co K l k) 4) (pwR K (F.inv (cN K (k + 1))) 3)) by lib_ring] at t
    exact t

/-- **one 4D Migdal step**: ratio σ for 1 :: a gives ratio (4σ)⁴ for mk L a -/
theorem mk_ratio (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (σ : R) (hσ : O.le K.zero σ) (a : List R)
    (ha : RC K O σ (K.one :: a)) (L : Nat) :
    RC K O (pwR K (K.add (K.add σ σ) (K.add σ σ)) 4) (mk K O F L a) := by
  have hann : ∀ x, memL x a → O.le K.zero x := fun x hx => mem_nn K O _ ha.1 x (Or.inr hx)
  have P := nprod_ratio K O F h1 h10 σ hσ a ha (L + 1)
  rw [nprod_head K O F h1 h10 a a hann hann L] at P
  have h2 : O.le K.zero (K.add σ σ) := by
    have := O.add_le_add _ _ _ _ hσ hσ; rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  have P2 := nprod_ratio K O F h1 h10 _ h2 _ P (L + 1)
  have h4 : O.le K.zero (K.add (K.add σ σ) (K.add σ σ)) := by
    have := O.add_le_add _ _ _ _ h2 h2; rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this
  have D := decM_ratio K O F h1 h10 _ h4 _ P2
  unfold mk
  rw [nprod_head K O F h1 h10 a a hann hann L]
  exact D

theorem RC_mono (σ σ' : R) (hs : O.le σ σ') (l : List R) (h : RC K O σ l) : RC K O σ' l :=
  ⟨h.1, fun k => O.le_trans _ _ _ (h.2 k) (O.mul_le_mul_right _ _ _ hs (h.1 k))⟩

/-- σ ≤ h^{e+2} ⇒ (4σ)⁴ ≤ h^{4e}, with h + h = 1 -/
theorem four_pow (h1 : O.le K.zero K.one) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h) (σ : R)
    (hσ0 : O.le K.zero σ) (e : Nat) (hσ : O.le σ (pwR K h (e + 2))) :
    O.le (pwR K (K.add (K.add σ σ) (K.add σ σ)) 4) (pwR K h (4 * e)) := by
  have a : O.le (K.add (K.add σ σ) (K.add σ σ)) (pwR K h e) := by
    have t := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ hσ hσ) (O.add_le_add _ _ _ _ hσ hσ)
    have e2 : pwR K h (e + 2) = K.mul (pwR K h e) (K.mul h h) := by
      rw [GKS.pwR_add]; show K.mul _ (K.mul (K.mul K.one h) h) = _; lib_ring
    rw [e2, show K.add (K.add (K.mul (pwR K h e) (K.mul h h)) (K.mul (pwR K h e) (K.mul h h)))
        (K.add (K.mul (pwR K h e) (K.mul h h)) (K.mul (pwR K h e) (K.mul h h)))
      = K.mul (pwR K h e) (K.mul (K.add h h) (K.add h h)) by lib_ring, hh, K.one_mul,
      show K.mul (pwR K h e) K.one = pwR K h e by lib_ring] at t
    exact t
  have s4 : O.le K.zero (K.add (K.add σ σ) (K.add σ σ)) := by
    have := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ hσ0 hσ0) (O.add_le_add _ _ _ _ hσ0 hσ0)
    rw [show K.add (K.add K.zero K.zero) (K.add K.zero K.zero) = K.zero by lib_ring] at this; exact this
  have := SU2Strong4D.pw_mono K O h1 _ _ s4 a 4
  rw [Migdal.pw_pw, Nat.mul_comm] at this
  exact this

/-- iterates keep a ratio bound -/
theorem iter_ratio (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (L : Nat) :
    ∀ (k : Nat) (a : List R) (σ : Nat → R), (∀ i, O.le K.zero (σ i)) →
      (∀ i, O.le (pwR K (K.add (K.add (σ i) (σ i)) (K.add (σ i) (σ i))) 4) (σ (i + 1))) →
      RC K O (σ 0) (K.one :: a) → RC K O (σ k) (K.one :: mkIt K O F L a k)
  | 0, _, _, _, _, ha => ha
  | k + 1, a, σ, hσ, hstep, ha => by
    have ih := iter_ratio h1 h10 L k a σ hσ hstep ha
    have m := mk_ratio K O F h1 h10 (σ k) (hσ k) _ ih L
    have hb : ∀ x, memL x (mkIt K O F L a k) → O.le K.zero x := fun x hx => mem_nn K O _ ih.1 x (Or.inr hx)
    rw [mk_head_nn K O F h1 h10 _ hb L] at m
    exact RC_mono K O _ _ (hstep k) _ m

theorem bd_co (r : R) : ∀ (l : List R) (m : Nat),
    (∀ n, O.le K.zero (co K l n) ∧ O.le (co K l n) (K.mul (cN K (m + n + 1)) (pwR K r (4 * (m + n))))) → ConeBound.Bd K O r m l
  | [], _, _ => trivial
  | _ :: l, m, h => ⟨h 0, bd_co r l (m + 1) (fun n => by
      have := h (n + 1); rw [show m + (n + 1) = m + 1 + n by lib_nat] at this; exact this)⟩

theorem co_pow (σ : R) (hσ : O.le K.zero σ) (a : List R) (ha : RC K O σ (K.one :: a)) :
    ∀ n, O.le (co K (K.one :: a) n) (pwR K σ n)
  | 0 => O.le_refl _
  | n + 1 => by
    have a1 := ha.2 n
    have a2 := O.mul_le_mul_right _ _ σ (co_pow σ hσ a ha n) hσ
    rw [K.mul_comm] at a2
    exact O.le_trans _ _ _ a1 a2

/-- **into the cone**: ratio σ ≤ r⁴ gives Bd r -/
theorem RC_bd (h1 : O.le K.zero K.one) (σ r : R) (hσ : O.le K.zero σ) (hr : O.le K.zero r) (hsr : O.le σ (pwR K r 4))
    (a : List R) (ha : RC K O σ (K.one :: a)) : ConeBound.Bd K O r 0 (K.one :: a) := by
  apply bd_co K O r _ 0
  intro n
  refine ⟨ha.1 n, ?_⟩
  have p1 := co_pow K O σ hσ a ha n
  have p2 := SU2Strong4D.pw_mono K O h1 _ _ hσ hsr n
  rw [Migdal.pw_pw] at p2
  have p3 := O.mul_le_mul_right _ _ (pwR K r (4 * n)) (SU2CharExp.cN_mono K O h1 1 (0 + n + 1)
    (by rw [Nat.zero_add]; exact Nat.succ_le_succ (Nat.zero_le n))) (AreaLower.pw_nn K O r hr h1 _)
  rw [SU2CharExp.cN_one, K.one_mul] at p3
  rw [Nat.zero_add]
  rw [Nat.zero_add] at p3
  exact O.le_trans _ _ _ p1 (O.le_trans _ _ _ p2 p3)

/-- the heat weight 1 :: (heat q 1 L) has ratio 2q³ -/
theorem heat_RC (h1 : O.le K.zero K.one) (q : R) (hq0 : O.le K.zero q) (hq1 : O.le q K.one) (L : Nat) :
    RC K O (K.mul (cN K 2) (pwR K q 3)) (K.one :: HeatDec.heat K q 1 L) := by
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have hn : ∀ k, O.le K.zero (co K (HeatDec.heat K q 1 L) k) := fun k => by
    cases Nat.lt_or_ge k L with
    | inl h => rw [SecondRing.co_heat K q L 1 k h]; exact O.mul_nonneg _ _ (nn _) (AreaLower.pw_nn K O q hq0 h1 _)
    | inr h => rw [UFlowAB.co_ge K _ k (by rw [SecondRing.len_heat]; exact h)]; exact O.le_refl _
  have σn : O.le K.zero (K.mul (cN K 2) (pwR K q 3)) := O.mul_nonneg _ _ (nn 2) (AreaLower.pw_nn K O q hq0 h1 3)
  refine ⟨fun k => ?_, fun k => ?_⟩
  · cases k with
    | zero => exact h1
    | succ k => exact hn k
  · cases k with
    | zero =>
      show O.le (co K (HeatDec.heat K q 1 L) 0) (K.mul (K.mul (cN K 2) (pwR K q 3)) K.one)
      cases Nat.lt_or_ge 0 L with
      | inl h =>
        rw [SecondRing.co_heat K q L 1 0 h]
        rw [show K.mul (K.mul (cN K 2) (pwR K q 3)) K.one = K.mul (cN K (1 + 0 + 1)) (pwR K q ((1 + 0) * (1 + 0 + 2))) by
          show _ = K.mul (cN K 2) (pwR K q 3); lib_ring]
        exact O.le_refl _
      | inr h =>
        rw [UFlowAB.co_ge K _ 0 (by rw [SecondRing.len_heat]; exact h)]
        exact O.mul_nonneg _ _ σn h1
    | succ k =>
      show O.le (co K (HeatDec.heat K q 1 L) (k + 1)) (K.mul (K.mul (cN K 2) (pwR K q 3)) (co K (HeatDec.heat K q 1 L) k))
      cases Nat.lt_or_ge (k + 1) L with
      | inr h =>
        rw [UFlowAB.co_ge K _ (k + 1) (by rw [SecondRing.len_heat]; exact h)]
        exact O.mul_nonneg _ _ σn (hn k)
      | inl h =>
        rw [SecondRing.co_heat K q L 1 (k + 1) h, SecondRing.co_heat K q L 1 k (Nat.lt_of_succ_lt h),
          show (1 + (k + 1)) * (1 + (k + 1) + 2) = (1 + k) * (1 + k + 2) + (3 + 2 * (k + 1)) by lib_nat,
          GKS.pwR_add]
        have pa := SmallQ.pw_anti K O h1 q hq0 hq1 3 (2 * (k + 1))
        have m := SU2CharExp.cN_mono K O h1 (1 + (k + 1) + 1) (2 * (1 + k + 1)) (by
          rw [show 2 * (1 + k + 1) = (1 + (k + 1) + 1) + (k + 1) by lib_nat]; exact Nat.le_add_right _ _)
        rw [SU2CharExp.cN_mul] at m
        generalize hP : pwR K q ((1 + k) * (1 + k + 2)) = P
        have P0 : O.le K.zero P := hP ▸ AreaLower.pw_nn K O q hq0 h1 _
        have s1 := O.mul_le_mul_right _ _ (K.mul (cN K (1 + (k + 1) + 1)) P) pa (O.mul_nonneg _ _ (nn _) P0)
        have s2 := O.mul_le_mul_right _ _ (K.mul P (pwR K q 3)) m (O.mul_nonneg _ _ P0 (AreaLower.pw_nn K O q hq0 h1 3))
        rw [show K.mul (pwR K q (3 + 2 * (k + 1))) (K.mul (cN K (1 + (k + 1) + 1)) P)
            = K.mul (cN K (1 + (k + 1) + 1)) (K.mul P (pwR K q (3 + 2 * (k + 1)))) by lib_ring,
          show K.mul (pwR K q 3) (K.mul (cN K (1 + (k + 1) + 1)) P) = K.mul (cN K (1 + (k + 1) + 1)) (K.mul P (pwR K q 3)) by lib_ring] at s1
        rw [show K.mul (K.mul (cN K 2) (cN K (1 + k + 1))) (K.mul P (pwR K q 3))
          = K.mul (K.mul (cN K 2) (pwR K q 3)) (K.mul (cN K (1 + k + 1)) P) by lib_ring] at s2
        exact O.le_trans _ _ _ s1 s2

theorem mkIt_add (L : Nat) (a : List R) (j : Nat) : ∀ k, mkIt K O F L a (k + j) = mkIt K O F L (mkIt K O F L a j) k
  | 0 => by rw [Nat.zero_add]; rfl
  | k + 1 => by
    rw [show k + 1 + j = (k + j) + 1 by lib_nat]
    show (mk K O F L (mkIt K O F L a (k + j))).tail = (mk K O F L (mkIt K O F L (mkIt K O F L a j) k)).tail
    rw [mkIt_add L a j k]

def sig (q h : R) : Nat → R
  | 0 => K.mul (cN K 2) (pwR K q 3)
  | 1 => pwR K h 4
  | 2 => pwR K h 8
  | 3 => pwR K h 24
  | _ + 4 => pwR K h 88

/-- **G57: heat weights with 16q³ ≤ 1 (Q ≤ 0.63) enter the strong-coupling cone Cone(2⁻⁸) after four 4D Migdal
    steps and stay there forever.** -/
theorem heat_cone (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (q : R) (hq0 : O.le K.zero q) (hq1 : O.le q K.one) (hq : O.le (K.mul (cN K 2) (pwR K q 3)) (pwR K h 3)) (L L' : Nat) :
    ∀ k, ConeBound.Bd K O (pwR K h 8) 0 (K.one :: mkIt K O F L (HeatDec.heat K q 1 L') (k + 4)) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [show K.add h K.zero = h by lib_ring, hh] at this; exact this
  have pn := AreaLower.pw_nn K O h h0 h1
  have sn : ∀ i, O.le K.zero (sig K q h i) := fun i => by
    match i with
    | 0 => exact O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 2) (AreaLower.pw_nn K O q hq0 h1 3)
    | 1 => exact pn 4
    | 2 => exact pn 8
    | 3 => exact pn 24
    | _ + 4 => exact pn 88
  have st : ∀ i, O.le (pwR K (K.add (K.add (sig K q h i) (sig K q h i)) (K.add (sig K q h i) (sig K q h i))) 4) (sig K q h (i + 1)) := by
    intro i
    match i with
    | 0 => exact four_pow K O h1 h hh h0 _ (sn 0) 1 hq
    | 1 => exact four_pow K O h1 h hh h0 _ (sn 1) 2 (O.le_refl _)
    | 2 => exact four_pow K O h1 h hh h0 _ (sn 2) 6 (O.le_refl _)
    | 3 => exact four_pow K O h1 h hh h0 _ (sn 3) 22 (O.le_refl _)
    | i + 4 => exact O.le_trans _ _ _ (four_pow K O h1 h hh h0 _ (sn (i + 4)) 86 (O.le_refl _)) (by show O.le (pwR K h (4 * 86)) (pwR K h 88); rw [show 4 * 86 = 88 + 256 from rfl]; exact SmallQ.pw_anti K O h1 h h0 hle 88 256)
  have r4 := iter_ratio K O F h1 h10 L 4 (HeatDec.heat K q 1 L') (sig K q h) sn st (heat_RC K O h1 q hq0 hq1 L')
  have bd4 := RC_bd K O h1 (sig K q h 4) (pwR K h 8) (sn 4) (pn 8) (by
    show O.le (pwR K h 88) (pwR K (pwR K h 8) 4)
    rw [Migdal.pw_pw]; exact SmallQ.pw_anti K O h1 h h0 hle 32 56) _ r4
  intro k
  rw [mkIt_add K O F L _ 4 k]
  exact mk_forever K O F h1 h10 h hh h0 _ bd4 L k

def sigG (σ0 h : R) : Nat → R
  | 0 => σ0
  | 1 => pwR K h 4
  | 2 => pwR K h 8
  | 3 => pwR K h 24
  | _ + 4 => pwR K h 88

/-- **any** normalised weight 1 :: a with ratio σ₀ ≤ 1/8 enters Cone(2⁻⁸) after four steps and stays there -/
theorem rc_cone (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (σ0 : R) (hs0 : O.le K.zero σ0) (hs : O.le σ0 (pwR K h 3)) (a : List R) (ha : RC K O σ0 (K.one :: a)) (L : Nat) :
    ∀ k, ConeBound.Bd K O (pwR K h 8) 0 (K.one :: mkIt K O F L a (k + 4)) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [show K.add h K.zero = h by lib_ring, hh] at this; exact this
  have pn := AreaLower.pw_nn K O h h0 h1
  have sn : ∀ i, O.le K.zero (sigG K σ0 h i) := fun i => by
    match i with
    | 0 => exact hs0
    | 1 => exact pn 4
    | 2 => exact pn 8
    | 3 => exact pn 24
    | _ + 4 => exact pn 88
  have st : ∀ i, O.le (pwR K (K.add (K.add (sigG K σ0 h i) (sigG K σ0 h i)) (K.add (sigG K σ0 h i) (sigG K σ0 h i))) 4) (sigG K σ0 h (i + 1)) := by
    intro i
    match i with
    | 0 => exact four_pow K O h1 h hh h0 _ (sn 0) 1 hs
    | 1 => exact four_pow K O h1 h hh h0 _ (sn 1) 2 (O.le_refl _)
    | 2 => exact four_pow K O h1 h hh h0 _ (sn 2) 6 (O.le_refl _)
    | 3 => exact four_pow K O h1 h hh h0 _ (sn 3) 22 (O.le_refl _)
    | i + 4 => exact O.le_trans _ _ _ (four_pow K O h1 h hh h0 _ (sn (i + 4)) 86 (O.le_refl _)) (by show O.le (pwR K h (4 * 86)) (pwR K h 88); rw [show 4 * 86 = 88 + 256 from rfl]; exact SmallQ.pw_anti K O h1 h h0 hle 88 256)
  have r4 := iter_ratio K O F h1 h10 L 4 a (sigG K σ0 h) sn st ha
  have bd4 := RC_bd K O h1 (sigG K σ0 h 4) (pwR K h 8) (sn 4) (pn 8) (by
    show O.le (pwR K h 88) (pwR K (pwR K h 8) 4)
    rw [Migdal.pw_pw]; exact SmallQ.pw_anti K O h1 h h0 hle 32 56) _ r4
  intro k
  rw [mkIt_add K O F L _ 4 k]
  exact mk_forever K O F h1 h10 h hh h0 _ bd4 L k

end RatioStep

#print axioms RatioStep.mem_nn
#print axioms RatioStep.co_lmap
#print axioms RatioStep.co_lmap_ge
#print axioms RatioStep.nprod_ratio
#print axioms RatioStep.co_decM
#print axioms RatioStep.decM_ratio
#print axioms RatioStep.mk_ratio
#print axioms RatioStep.RC_mono
#print axioms RatioStep.four_pow
#print axioms RatioStep.iter_ratio
#print axioms RatioStep.bd_co
#print axioms RatioStep.co_pow
#print axioms RatioStep.RC_bd
#print axioms RatioStep.heat_RC
#print axioms RatioStep.mkIt_add
#print axioms RatioStep.heat_cone
#print axioms RatioStep.rc_cone

#print axioms: 17 theorems, no axioms.

90.123 Bond-moving log-concavity at every index

For every v-good list \(x\) of every length, the self-product coefficients satisfy (A) at every index.

\[ (n+2)^2\,c_n c_{n+2}\le (n+1)(n+3)\,c_{n+1}^2\qquad (n\ge 0) \]

ym/CBMEven.lean

/-
  ym/CBMEven — T45, G69b3f: the CB-M identity for every even N = 2Q+2 and every list, as a telescoping sum.
  With the staircase sums G(k,n) = sum_{s<n} Mt(s, k + 2(n-1-s))  (G(k,0) = 0, G(k,n+1) = G(k+2,n) + Mt(n,k)):
    c_{2P} = G(2,P) + G(0,P+1),  c_{2P+1} = 2 G(1,P+1)                         (c_even, c_odd)
    c_N^2 - c_{N-1} c_{N+1} - M_N^2 = 4 Phi(0,P),   Phi(g,n) = G(g+2,n)G(g,n+1) - G(g+1,n)G(g+1,n+1).
  The bottom-truncated windows Hw(k,n)(u) = sum_{s<n, s<=u} x_{u+k+2(n-1-s)} have
    <x, Hw(k,n)> = G(k,n),  <y, Hw(k+1,n+1)> = G(k+2,n) + G(k,n+2)   (y = chi_1 x),
  so the Cauchy–Binet value of the box (g,n+1) is Phi(g,n+1) - Phi(g+1,n) and the boxes telescope:
    c_N^2 - c_{N-1} c_{N+1} - M_N^2 = 4 sum_{j<i} D(i,j) Kern(i,j)   (even_id, even_dbl).
  Hence CB-M at N = 2Q+2 follows from Kern >= 0 (cbm_even_of_kern). Lean 4 core only, no axioms.
-/
import ym.CBLib

namespace CBMEven

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom

variable {R : Type} (K : DetChain.CRing R)

/-! ### staircase sums -/

/-- sgf cdp k n = sum_{s<n} cdp s (k + 2(n-1-s)) -/
def sgf (φ : Nat → Nat → R) : Nat → Nat → R
  | _, 0 => K.zero
  | k, n + 1 => K.add (sgf φ (k + 2) n) (φ n k)

theorem gf_ext (φ ψ : Nat → Nat → R) (h : ∀ s e, φ s e = ψ s e) : ∀ (n k : Nat), sgf K φ k n = sgf K ψ k n
  | 0, _ => rfl
  | n + 1, k => by
    show K.add (sgf K φ (k + 2) n) (φ n k) = K.add (sgf K ψ (k + 2) n) (ψ n k)
    rw [gf_ext φ ψ h n (k + 2), h n k]

theorem gf_add (φ ψ : Nat → Nat → R) : ∀ (n k : Nat),
    sgf K (fun s e => K.add (φ s e) (ψ s e)) k n = K.add (sgf K φ k n) (sgf K ψ k n)
  | 0, _ => by show K.zero = K.add K.zero K.zero; lib_ring
  | n + 1, k => by
    show K.add (sgf K (fun s e => K.add (φ s e) (ψ s e)) (k + 2) n) (K.add (φ n k) (ψ n k))
      = K.add (K.add (sgf K φ (k + 2) n) (φ n k)) (K.add (sgf K ψ (k + 2) n) (ψ n k))
    rw [gf_add φ ψ n (k + 2)]
    lib_ring

/-- peel the s = 0 term -/
theorem gf_peel (φ : Nat → Nat → R) : ∀ (n k : Nat),
    sgf K φ k (n + 1) = K.add (φ 0 (k + n + n)) (sgf K (fun s e => φ (s + 1) e) k n)
  | 0, k => by
    show K.add K.zero (φ 0 k) = K.add (φ 0 k) K.zero
    lib_ring
  | n + 1, k => by
    show K.add (sgf K φ (k + 2) (n + 1)) (φ (n + 1) k)
      = K.add (φ 0 (k + (n + 1) + (n + 1))) (K.add (sgf K (fun s e => φ (s + 1) e) (k + 2) n) (φ (n + 1) k))
    rw [gf_peel φ n (k + 2), show k + 2 + n + n = k + (n + 1) + (n + 1) by lib_nat]
    lib_ring

/-- the product staircase of a sequence -/
def sff (f : Nat → R) (k n : Nat) : R := sgf K (fun s e => K.mul (f s) (f (s + e))) k n

theorem ff_succ (f : Nat → R) (k n : Nat) :
    sgf K (fun s e => K.mul (f (s + 1)) (f (s + 1 + e))) k n = sff K (fun i => f (i + 1)) k n := by
  unfold sff
  apply gf_ext K
  intro s e
  rw [show s + 1 + e = s + e + 1 by lib_nat]

theorem cs_odd : ∀ (P : Nat) (f : Nat → R), CBLib.cs K f (P + P + 1) = K.add (sff K f 1 (P + 1)) (sff K f 1 (P + 1))
  | 0, f => by
    show K.add (K.mul (f 0) (f 1)) (K.mul (f 0) (f 1))
      = K.add (K.add K.zero (K.mul (f 0) (f 1))) (K.add K.zero (K.mul (f 0) (f 1)))
    lib_ring
  | P + 1, f => by
    rw [show P + 1 + (P + 1) + 1 = P + P + 1 + 2 by lib_nat]
    show K.add (K.add (K.mul (f 0) (f (P + P + 1 + 2))) (K.mul (f 0) (f (P + P + 1 + 2))))
        (CBLib.cs K (fun i => f (i + 1)) (P + P + 1)) = _
    rw [cs_odd P (fun i => f (i + 1))]
    have e : sff K f 1 (P + 1 + 1) = K.add (K.mul (f 0) (f (P + P + 1 + 2))) (sff K (fun i => f (i + 1)) 1 (P + 1)) := by
      show sgf K (fun s e => K.mul (f s) (f (s + e))) 1 (P + 1 + 1) = _
      rw [gf_peel K _ (P + 1) 1, ff_succ K f 1 (P + 1), show 0 + (1 + (P + 1) + (P + 1)) = P + P + 1 + 2 by lib_nat]
    rw [e]
    lib_ring

theorem cs_even : ∀ (P : Nat) (f : Nat → R), CBLib.cs K f (P + P) = K.add (sff K f 2 P) (sff K f 0 (P + 1))
  | 0, f => by
    show K.mul (f 0) (f 0) = K.add K.zero (K.add K.zero (K.mul (f 0) (f 0)))
    lib_ring
  | P + 1, f => by
    rw [show P + 1 + (P + 1) = P + P + 2 by lib_nat]
    show K.add (K.add (K.mul (f 0) (f (P + P + 2))) (K.mul (f 0) (f (P + P + 2))))
        (CBLib.cs K (fun i => f (i + 1)) (P + P)) = _
    rw [cs_even P (fun i => f (i + 1))]
    have e2 : sff K f 2 (P + 1) = K.add (K.mul (f 0) (f (P + P + 2))) (sff K (fun i => f (i + 1)) 2 P) := by
      show sgf K (fun s e => K.mul (f s) (f (s + e))) 2 (P + 1) = _
      rw [gf_peel K _ P 2, ff_succ K f 2 P, show 0 + (2 + P + P) = P + P + 2 by lib_nat]
    have e0 : sff K f 0 (P + 1 + 1) = K.add (K.mul (f 0) (f (P + P + 2))) (sff K (fun i => f (i + 1)) 0 (P + 1)) := by
      show sgf K (fun s e => K.mul (f s) (f (s + e))) 0 (P + 1 + 1) = _
      rw [gf_peel K _ (P + 1) 0, ff_succ K f 0 (P + 1), show 0 + (0 + (P + 1) + (P + 1)) = P + P + 2 by lib_nat]
    rw [e2, e0]
    lib_ring

/-! ### the even and odd coefficients as staircases -/

section
variable (X : List R)

/-- G(k,n) = sum_{s<n} Mt(s, k + 2(n-1-s)) -/
def sg (k n : Nat) : R := sgf K (fun s e => ReduceCBM.Mt K s e X) k n

/-- one more row: G(k,n+1) = r_{k+2n} + G(k,n) - F(k,n) -/
theorem g_step (k n : Nat) :
    sg K X k (n + 1) = K.add (K.add (ReduceCBM.Mt K 0 (k + n + n) X) (sg K X k n)) (K.neg (sff K (co K X) k n)) := by
  unfold sg
  rw [gf_peel K _ n k]
  have h : sgf K (fun s e => ReduceCBM.Mt K s e X) k n
      = sgf K (fun s e => K.add (K.mul (co K X s) (co K X (s + e))) (ReduceCBM.Mt K (s + 1) e X)) k n :=
    gf_ext K _ _ (fun s e => CBLib.mt_step K s e X) n k
  rw [h, gf_add]
  unfold sff
  lib_ring

theorem c_even : ∀ (P : Nat), cgc K X X (P + P) = K.add (sg K X 2 P) (sg K X 0 (P + 1))
  | 0 => by
    have hc0 : cgc K X X 0 = Corr.rr K 0 X :=
      (CBM2.bil_ext K X _ _ (fun j k => Corr.cg0 j k)).trans (Corr.dist0_bil K X)
    show cgc K X X 0 = _
    rw [hc0, CBLib.rr_mt K 0 X]
    show _ = K.add K.zero (K.add K.zero (ReduceCBM.Mt K 0 0 X))
    lib_ring
  | P + 1 => by
    have st : K.add (cgc K X X (P + P + 2)) (RhoC.bil K X (fun j k => dl (P + P) (j + k)))
        = K.add (cgc K X X (P + P)) (RhoC.bil K X (fun j k => dl (P + P + 2) (dist j k))) := RhoC.cgc_step K X (P + P)
    have dd : RhoC.bil K X (fun j k => dl (P + P + 2) (dist j k))
        = K.add (Corr.rr K (P + P + 2) X) (Corr.rr K (P + P + 2) X) := Corr.dist_bil K (P + P + 1) X
    have cc : RhoC.bil K X (fun j k => dl (P + P) (j + k)) = CBLib.cs K (co K X) (P + P) := CBLib.conv_cs K (P + P) X
    rw [dd, cc, cs_even K P, c_even P, CBLib.rr_mt K (P + P + 2) X] at st
    have e := CBM2.eq_of_add K _ _ _ st
    rw [show P + 1 + (P + 1) = P + P + 2 by lib_nat, e, g_step K X 2 P, g_step K X 0 (P + 1),
      show 2 + P + P = P + P + 2 by lib_nat, show 0 + (P + 1) + (P + 1) = P + P + 2 by lib_nat]
    lib_ring

theorem c_odd : ∀ (P : Nat), cgc K X X (P + P + 1) = K.add (sg K X 1 (P + 1)) (sg K X 1 (P + 1))
  | 0 => by
    have hc1 : cgc K X X 1 = K.add (Corr.rr K 1 X) (Corr.rr K 1 X) :=
      (CBM2.bil_ext K X _ _ (fun j k => Corr.cg1 j k)).trans (Corr.dist_bil K 0 X)
    show cgc K X X 1 = _
    rw [hc1, CBLib.rr_mt K 1 X]
    show _ = K.add (K.add K.zero (ReduceCBM.Mt K 0 1 X)) (K.add K.zero (ReduceCBM.Mt K 0 1 X))
    lib_ring
  | P + 1 => by
    have st : K.add (cgc K X X (P + P + 1 + 2)) (RhoC.bil K X (fun j k => dl (P + P + 1) (j + k)))
        = K.add (cgc K X X (P + P + 1)) (RhoC.bil K X (fun j k => dl (P + P + 1 + 2) (dist j k))) :=
      RhoC.cgc_step K X (P + P + 1)
    have dd : RhoC.bil K X (fun j k => dl (P + P + 1 + 2) (dist j k))
        = K.add (Corr.rr K (P + P + 1 + 2) X) (Corr.rr K (P + P + 1 + 2) X) := Corr.dist_bil K (P + P + 2) X
    have cc : RhoC.bil K X (fun j k => dl (P + P + 1) (j + k)) = CBLib.cs K (co K X) (P + P + 1) :=
      CBLib.conv_cs K (P + P + 1) X
    rw [dd, cc, cs_odd K P, c_odd P, CBLib.rr_mt K (P + P + 1 + 2) X] at st
    have e := CBM2.eq_of_add K _ _ _ st
    rw [show P + 1 + (P + 1) + 1 = P + P + 1 + 2 by lib_nat, e, g_step K X 1 (P + 1),
      show 1 + (P + 1) + (P + 1) = P + P + 1 + 2 by lib_nat]
    lib_ring

/-! ### bottom-truncated windows -/

/-- wtm n k u = [n <= u] x_{u+k} -/
def wtm : Nat → Nat → Nat → R
  | 0, k, u => co K X (u + k)
  | _ + 1, _, 0 => K.zero
  | n + 1, k, u + 1 => wtm n (k + 1) u

theorem tm_hi : ∀ (n i k : Nat), wtm K X n k (i + n) = co K X (i + n + k)
  | 0, _, _ => rfl
  | n + 1, i, k => by
    show wtm K X n (k + 1) (i + n) = _
    rw [tm_hi n i (k + 1), show i + n + (k + 1) = i + (n + 1) + k by lib_nat]

/-- the lower part of the window inner product vanishes -/
theorem tm_low : ∀ (n : Nat) (f : Nat → R) (k : Nat), sumN K (fun u => K.mul (f u) (wtm K X n k u)) n = K.zero
  | 0, _, _ => rfl
  | n + 1, f, k => by
    rw [sumN_shift K]
    show K.add (K.mul (f 0) K.zero) (sumN K (fun u => K.mul (f (u + 1)) (wtm K X n (k + 1) u)) n) = K.zero
    rw [tm_low n (fun u => f (u + 1)) (k + 1)]
    lib_ring

theorem ip_tm (f : Nat → R) (n k m : Nat) :
    CauchyBinet2.ip K f (wtm K X n k) (m + n) = sumN K (fun i => K.mul (f (i + n)) (co K X (i + n + k))) m := by
  unfold CauchyBinet2.ip
  rw [CBLib.sumN_peel K _ n m, tm_low K X n f k]
  rw [sumN_ext K (fun i => K.mul (f (i + n)) (wtm K X n k (i + n))) (fun i => K.mul (f (i + n)) (co K X (i + n + k)))
    (fun i => by rw [tm_hi K X n i k]) m]
  lib_ring

theorem le_dest (a b : Nat) (h : a ≤ b) : ∃ d, b = a + d := by
  obtain ⟨d, hd⟩ := CBM2.lt_dest a (b + 1) (Nat.succ_le_succ h)
  exact ⟨d, Nat.succ.inj hd⟩

theorem sum_mt (s e m : Nat) (hm : X.length ≤ m) (g : Nat → R)
    (hg : ∀ i, g i = K.mul (co K X (i + s)) (co K X (i + s + e))) : sumN K g m = ReduceCBM.Mt K s e X := by
  rw [CBLib.mt_sum K s e X m hm]
  exact sumN_ext K _ _ hg m

theorem ip_tm_x (n k NN : Nat) (h : X.length + n ≤ NN) :
    CauchyBinet2.ip K (co K X) (wtm K X n k) NN = ReduceCBM.Mt K n k X := by
  obtain ⟨d, hd⟩ := le_dest _ _ h
  rw [hd, show X.length + n + d = X.length + d + n by lib_nat, ip_tm]
  exact sum_mt K X n k _ (Nat.le_add_right _ _) _ (fun _ => rfl)

theorem ip_tm_y0 (k NN : Nat) (h : X.length + 1 ≤ NN) :
    CauchyBinet2.ip K (VGoodPair.yv K X) (wtm K X 0 (k + 1)) NN
      = K.add (ReduceCBM.Mt K 0 (k + 2) X) (ReduceCBM.Mt K 1 k X) := by
  obtain ⟨d, hd⟩ := le_dest _ _ h
  rw [hd, show X.length + 1 + d = X.length + d + 1 + 0 by lib_nat, ip_tm]
  rw [sumN_ext K _ (fun i => K.add (K.mul (VGoodTail.cm K X i) (co K X (i + 0 + (k + 1))))
      (K.mul (co K X (i + 1)) (co K X (i + 1 + k))))
    (fun i => by
      try dsimp only
      rw [VGoodTail.yv_eq, show i + 0 + (k + 1) = i + 1 + k by lib_nat, Nat.add_zero]
      lib_ring), sumN_add K]
  have a1 : sumN K (fun i => K.mul (co K X (i + 1)) (co K X (i + 1 + k))) (X.length + d + 1)
      = ReduceCBM.Mt K 1 k X := sum_mt K X 1 k _ (Nat.le_succ_of_le (Nat.le_add_right _ _)) _ (fun _ => rfl)
  have a0 : sumN K (fun i => K.mul (VGoodTail.cm K X i) (co K X (i + 0 + (k + 1)))) (X.length + d + 1)
      = ReduceCBM.Mt K 0 (k + 2) X := by
    rw [sumN_shift K]
    show K.add (K.mul K.zero _) (sumN K (fun i => K.mul (co K X i) (co K X (i + 1 + 0 + (k + 1)))) (X.length + d)) = _
    have s0 := sum_mt K X 0 (k + 2) (X.length + d) (Nat.le_add_right _ _)
      (fun i => K.mul (co K X i) (co K X (i + 1 + 0 + (k + 1)))) (fun i => by
        show K.mul (co K X i) (co K X (i + 1 + 0 + (k + 1))) = K.mul (co K X (i + 0)) (co K X (i + 0 + (k + 2)))
        rw [show i + 1 + 0 + (k + 1) = i + 0 + (k + 2) by lib_nat]
        rfl)
    rw [s0]
    lib_ring
  rw [a0, a1]

theorem ip_tm_y (n k NN : Nat) (h : X.length + n + 1 ≤ NN) :
    CauchyBinet2.ip K (VGoodPair.yv K X) (wtm K X (n + 1) (k + 1)) NN
      = K.add (ReduceCBM.Mt K n (k + 2) X) (ReduceCBM.Mt K (n + 2) k X) := by
  obtain ⟨d, hd⟩ := le_dest _ _ h
  rw [hd, show X.length + n + 1 + d = X.length + d + (n + 1) by lib_nat, ip_tm]
  rw [sumN_ext K _ (fun i => K.add (K.mul (co K X (i + n)) (co K X (i + n + (k + 2))))
      (K.mul (co K X (i + (n + 2))) (co K X (i + (n + 2) + k))))
    (fun i => by
      try dsimp only
      rw [VGoodTail.yv_eq, show i + (n + 1) + (k + 1) = i + n + (k + 2) by lib_nat,
        show i + (n + 1) + 1 = i + (n + 2) by lib_nat, show i + n + (k + 2) = i + (n + 2) + k by lib_nat]
      show K.mul (K.add (co K X (i + n)) (co K X (i + (n + 2)))) (co K X (i + (n + 2) + k)) = _
      lib_ring), sumN_add K]
  rw [sum_mt K X n (k + 2) _ (Nat.le_add_right _ _) _ (fun _ => rfl),
    sum_mt K X (n + 2) k _ (Nat.le_add_right _ _) _ (fun _ => rfl)]

/-- the bottom-truncated window Hw(k,n) -/
def whw : Nat → Nat → Nat → R
  | _, 0 => fun _ => K.zero
  | k, n + 1 => fun u => K.add (whw (k + 2) n u) (wtm K X n k u)

theorem ip_add (f g h : Nat → R) : ∀ (NN : Nat),
    CauchyBinet2.ip K f (fun u => K.add (g u) (h u)) NN
      = K.add (CauchyBinet2.ip K f g NN) (CauchyBinet2.ip K f h NN)
  | 0 => by show K.zero = K.add K.zero K.zero; lib_ring
  | NN + 1 => by
    show K.add (CauchyBinet2.ip K f (fun u => K.add (g u) (h u)) NN) (K.mul (f NN) (K.add (g NN) (h NN)))
      = K.add (K.add (CauchyBinet2.ip K f g NN) (K.mul (f NN) (g NN))) (K.add (CauchyBinet2.ip K f h NN) (K.mul (f NN) (h NN)))
    rw [ip_add f g h NN]
    lib_ring

theorem ip_zero (f : Nat → R) : ∀ (NN : Nat), CauchyBinet2.ip K f (fun _ => K.zero) NN = K.zero
  | 0 => rfl
  | NN + 1 => by
    show K.add (CauchyBinet2.ip K f (fun _ => K.zero) NN) (K.mul (f NN) K.zero) = K.zero
    rw [ip_zero f NN]
    lib_ring

theorem hw_x : ∀ (n k NN : Nat), X.length + n ≤ NN → CauchyBinet2.ip K (co K X) (whw K X k n) NN = sg K X k n
  | 0, _, NN, _ => ip_zero K _ NN
  | n + 1, k, NN, h => by
    have e3 : CauchyBinet2.ip K (co K X) (whw K X k (n + 1)) NN
        = K.add (CauchyBinet2.ip K (co K X) (whw K X (k + 2) n) NN) (CauchyBinet2.ip K (co K X) (wtm K X n k) NN) :=
      ip_add K (co K X) (whw K X (k + 2) n) (wtm K X n k) NN
    rw [e3, hw_x n (k + 2) NN (Nat.le_of_succ_le h), ip_tm_x K X n k NN (Nat.le_of_succ_le h)]
    rfl

theorem hw_y : ∀ (n k NN : Nat), X.length + n + 1 ≤ NN →
    CauchyBinet2.ip K (VGoodPair.yv K X) (whw K X (k + 1) (n + 1)) NN = K.add (sg K X (k + 2) n) (sg K X k (n + 2))
  | 0, k, NN, h => by
    have e3 : CauchyBinet2.ip K (VGoodPair.yv K X) (whw K X (k + 1) 1) NN
        = K.add (CauchyBinet2.ip K (VGoodPair.yv K X) (fun _ => K.zero) NN)
            (CauchyBinet2.ip K (VGoodPair.yv K X) (wtm K X 0 (k + 1)) NN) :=
      ip_add K (VGoodPair.yv K X) (fun _ => K.zero) (wtm K X 0 (k + 1)) NN
    rw [e3, ip_zero, ip_tm_y0 K X k NN h]
    show _ = K.add K.zero (K.add (K.add K.zero (ReduceCBM.Mt K 0 (k + 2) X)) (ReduceCBM.Mt K 1 k X))
    lib_ring
  | n + 1, k, NN, h => by
    have e3 : CauchyBinet2.ip K (VGoodPair.yv K X) (whw K X (k + 1) (n + 2)) NN
        = K.add (CauchyBinet2.ip K (VGoodPair.yv K X) (whw K X (k + 2 + 1) (n + 1)) NN)
            (CauchyBinet2.ip K (VGoodPair.yv K X) (wtm K X (n + 1) (k + 1)) NN) :=
      ip_add K (VGoodPair.yv K X) (whw K X (k + 2 + 1) (n + 1)) (wtm K X (n + 1) (k + 1)) NN
    rw [e3, hw_y n (k + 2) NN (Nat.le_of_succ_le h), ip_tm_y K X n k NN (Nat.le_of_succ_le h)]
    show _ = K.add (K.add (sg K X (k + 2 + 2) n) (ReduceCBM.Mt K n (k + 2) X))
      (K.add (sg K X (k + 2) (n + 2)) (ReduceCBM.Mt K (n + 2) k X))
    lib_ring

/-! ### the boxes and their telescoping -/

/-- the Christoffel–Darboux potential Phi(g,n) -/
def cdp (g n : Nat) : R :=
  K.add (K.mul (sg K X (g + 2) n) (sg K X g (n + 1))) (K.neg (K.mul (sg K X (g + 1) n) (sg K X (g + 1) (n + 1))))

/-- the Cauchy–Binet value of the box (g,n) -/
def cbox (NN g n : Nat) : R :=
  K.add (K.mul (CauchyBinet2.ip K (co K X) (whw K X (g + 2) n) NN) (CauchyBinet2.ip K (VGoodPair.yv K X) (whw K X (g + 1) n) NN))
    (K.neg (K.mul (CauchyBinet2.ip K (co K X) (whw K X (g + 1) n) NN) (CauchyBinet2.ip K (VGoodPair.yv K X) (whw K X (g + 2) n) NN)))

def cbsum (NN : Nat) : Nat → Nat → R
  | _, 0 => K.zero
  | g, n + 1 => K.add (cbox K X NN g (n + 1)) (cbsum NN (g + 1) n)

theorem cbx_eq (NN g n : Nat) (h : X.length + n + 1 ≤ NN) :
    cbox K X NN g (n + 1) = K.add (cdp K X g (n + 1)) (K.neg (cdp K X (g + 1) n)) := by
  unfold cbox
  have x2 : CauchyBinet2.ip K (co K X) (whw K X (g + 2) (n + 1)) NN = sg K X (g + 2) (n + 1) := hw_x K X (n + 1) (g + 2) NN h
  have x1 : CauchyBinet2.ip K (co K X) (whw K X (g + 1) (n + 1)) NN = sg K X (g + 1) (n + 1) := hw_x K X (n + 1) (g + 1) NN h
  have y2 : CauchyBinet2.ip K (VGoodPair.yv K X) (whw K X (g + 2) (n + 1)) NN = K.add (sg K X (g + 3) n) (sg K X (g + 1) (n + 2)) :=
    hw_y K X n (g + 1) NN h
  have y1 : CauchyBinet2.ip K (VGoodPair.yv K X) (whw K X (g + 1) (n + 1)) NN = K.add (sg K X (g + 2) n) (sg K X g (n + 2)) :=
    hw_y K X n g NN h
  rw [x2, x1, y2, y1]
  show _ = K.add (K.add (K.mul (sg K X (g + 2) (n + 1)) (sg K X g (n + 2)))
      (K.neg (K.mul (sg K X (g + 1) (n + 1)) (sg K X (g + 1) (n + 2)))))
    (K.neg (K.add (K.mul (sg K X (g + 3) n) (sg K X (g + 1) (n + 1)))
      (K.neg (K.mul (sg K X (g + 2) n) (sg K X (g + 2) (n + 1))))))
  lib_ring

theorem cbs_eq : ∀ (n g NN : Nat), X.length + n ≤ NN → cbsum K X NN g n = cdp K X g n
  | 0, g, _, _ => by
    show K.zero = K.add (K.mul K.zero (sg K X g 1)) (K.neg (K.mul K.zero (sg K X (g + 1) 1)))
    lib_ring
  | n + 1, g, NN, h => by
    show K.add (cbox K X NN g (n + 1)) (cbsum K X NN (g + 1) n) = _
    rw [cbx_eq K X NN g n h, cbs_eq n (g + 1) NN (Nat.le_of_succ_le h)]
    lib_ring

/-- **the even CB-M identity**: c_N^2 - c_{N-1} c_{N+1} = M_N^2 + 4 cbs, N = 2Q+2, every list -/
theorem even_id (Q : Nat) :
    LibRing.sub K (K.mul (cgc K X X (Q + 1 + (Q + 1))) (cgc K X X (Q + 1 + (Q + 1))))
        (K.mul (cgc K X X (Q + Q + 1)) (cgc K X X (Q + 1 + (Q + 1) + 1)))
      = K.add (K.mul (ReduceCBM.Mt K (Q + 1) 0 X) (ReduceCBM.Mt K (Q + 1) 0 X))
          (K.add (K.add (cbsum K X (X.length + Q + 1) 0 (Q + 1)) (cbsum K X (X.length + Q + 1) 0 (Q + 1)))
            (K.add (cbsum K X (X.length + Q + 1) 0 (Q + 1)) (cbsum K X (X.length + Q + 1) 0 (Q + 1)))) := by
  rw [cbs_eq K X (Q + 1) 0 (X.length + Q + 1) (Nat.le_refl _), c_even K X (Q + 1), c_odd K X Q, c_odd K X (Q + 1)]
  have p : cdp K X 0 (Q + 1) = K.add (K.mul (sg K X 2 (Q + 1)) (K.add (sg K X 2 (Q + 1)) (ReduceCBM.Mt K (Q + 1) 0 X)))
      (K.neg (K.mul (sg K X 1 (Q + 1)) (sg K X 1 (Q + 1 + 1)))) := rfl
  have g0 : sg K X 0 (Q + 1 + 1) = K.add (sg K X 2 (Q + 1)) (ReduceCBM.Mt K (Q + 1) 0 X) := rfl
  rw [p, g0]
  unfold LibRing.sub
  lib_ring

/-! ### the double-sum (kernel) form -/

def bkern : Nat → Nat → Nat → Nat → R
  | _, 0 => fun _ _ => K.zero
  | g, n + 1 => fun i j => K.add (Lagrange.minor K (whw K X (g + 2) (n + 1)) (whw K X (g + 1) (n + 1)) i j) (bkern (g + 1) n i j)

theorem cbs_dbl (NN : Nat) : ∀ (n g : Nat), cbsum K X NN g n
    = sumN K (fun j => sumN K (fun i => K.mul (Lagrange.minor K (co K X) (VGoodPair.yv K X) i j) (bkern K X g n i j)) j) NN
  | 0, _ => by
    show K.zero = _
    rw [sumN_ext K _ (fun _ => K.zero) (fun j => by
      show sumN K (fun i => K.mul (Lagrange.minor K (co K X) (VGoodPair.yv K X) i j) K.zero) j = K.zero
      rw [sumN_ext K _ (fun _ => K.zero) (fun i => by lib_ring) j, CBM2.sum_zero]) NN, CBM2.sum_zero]
  | n + 1, g => by
    show K.add (cbox K X NN g (n + 1)) (cbsum K X NN (g + 1) n) = _
    rw [cbs_dbl NN n (g + 1)]
    unfold cbox
    rw [CauchyBinet2.cauchy_binet K (co K X) (VGoodPair.yv K X) (whw K X (g + 2) (n + 1)) (whw K X (g + 1) (n + 1)) NN]
    unfold CauchyBinet2.mm
    rw [← sumN_add K]
    apply sumN_ext K
    intro j
    try dsimp only
    rw [← sumN_add K]
    apply sumN_ext K
    intro i
    try dsimp only
    show _ = K.mul (Lagrange.minor K (co K X) (VGoodPair.yv K X) i j)
      (K.add (Lagrange.minor K (whw K X (g + 2) (n + 1)) (whw K X (g + 1) (n + 1)) i j) (bkern K X (g + 1) n i j))
    lib_ring

end

variable (O : ORing K)

/-- **CB-M at N = 2Q+2 for every v-good list, given the summed kernel is nonnegative** -/
theorem cbm_even_of_kern (X : List R) (hg : L1Full.Good K O X) (Q : Nat)
    (hk : ∀ i m, O.le K.zero (bkern K X 0 (Q + 1) i (i + m + 1))) :
    O.le (K.mul (ReduceCBM.Mt K (Q + 1) 0 X) (ReduceCBM.Mt K (Q + 1) 0 X))
      (LibRing.sub K (K.mul (cgc K X X (Q + 1 + (Q + 1))) (cgc K X X (Q + 1 + (Q + 1))))
        (K.mul (cgc K X X (Q + Q + 1)) (cgc K X X (Q + 1 + (Q + 1) + 1)))) := by
  rw [even_id K X Q]
  have pa : O.le K.zero (cbsum K X (X.length + Q + 1) 0 (Q + 1)) := by
    rw [cbs_dbl K X]
    apply CauchyBinet2.sum_nn K O
    intro j
    have t := Lagrange.sum_le_lt K O (fun _ => K.zero)
      (fun i => K.mul (Lagrange.minor K (co K X) (VGoodPair.yv K X) i j) (bkern K X 0 (Q + 1) i j)) j
      (fun i hi => by
        obtain ⟨m, hm⟩ := CBM2.lt_dest i j hi
        subst hm
        exact O.mul_nonneg _ _ (VGoodTail.pair_all K O X hg i m) (hk i m))
    rw [CBM2.sum_zero] at t
    exact t
  have s2 := TopLC.add_nn K O _ _ pa pa
  have s4 := TopLC.add_nn K O _ _ s2 s2
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.add_le_add _ _ _ _ (O.le_refl _) s4)
  lib_ring

end CBMEven

#print axioms CBMEven.gf_ext
#print axioms CBMEven.gf_add
#print axioms CBMEven.gf_peel
#print axioms CBMEven.ff_succ
#print axioms CBMEven.cs_odd
#print axioms CBMEven.cs_even
#print axioms CBMEven.g_step
#print axioms CBMEven.c_even
#print axioms CBMEven.c_odd
#print axioms CBMEven.tm_hi
#print axioms CBMEven.tm_low
#print axioms CBMEven.ip_tm
#print axioms CBMEven.le_dest
#print axioms CBMEven.sum_mt
#print axioms CBMEven.ip_tm_x
#print axioms CBMEven.ip_tm_y0
#print axioms CBMEven.ip_tm_y
#print axioms CBMEven.ip_add
#print axioms CBMEven.ip_zero
#print axioms CBMEven.hw_x
#print axioms CBMEven.hw_y
#print axioms CBMEven.cbx_eq
#print axioms CBMEven.cbs_eq
#print axioms CBMEven.even_id
#print axioms CBMEven.cbs_dbl
#print axioms CBMEven.cbm_even_of_kern

ym/CBMKern.lean

/-
  ym/CBMKern — T45, G69b3g: the box-chain kernel of CBMEven is nonnegative, hence CB-M at every even N
  and (A) at every odd n, for every v-good list of every length.
  The chain kernel Kc(g,n) = bkern g n satisfies, pointwise in (u,v),
    Kc(g,n+1) - Kc(g+2,n) = dk(g,n),   dk(g,n+1) = inc(g,n) + dk(g+1,n)            (dk_step)
  and dk has a closed form in three regions (u < v):
    (a) n <= u:            dk = x_{v+g+1} W(u+g+2, n+1) - x_{u+g+1} W(v+g+2, n+1)     (dk_a)
    (b) n = u+1+f <= v:    dk = x_{v+g+1} W(u+g+2f+4, u+1) - x_{u+g+f+2} W(v+g+f+3, u+1)
    (c) v <= u+f:          dk = 0                                                    (dk_bc)
  where W(i,m) = x_i + x_{i+2} + ... (m terms). Each nonzero form is a sum of
    F_k(P,Q) = x_{P+k} x_Q - x_P x_{Q+k} = sum_{r} T(P+1+r, Q+k-r) >= 0   (P <= Q, fk_nn),
  so dk >= 0 and Kc >= 0 (bk_nn). With CBMEven.cbm_even_of_kern and ReduceCBM.a_of_cbm:
  cbm_even (CB-M at N = 2Q+2) and a_odd ((A) at n = 2Q+1). Lean 4 core only, no axioms.
-/
import ym.CBMEven
import ym.RhoCD

namespace CBMKern

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom

variable {R : Type} (K : DetChain.CRing R)

section
variable (X : List R)

/-- W(i,m) = x_i + x_{i+2} + ... (m terms) -/
abbrev W (i m : Nat) : R := RhoCD.Wn K (co K X) i m

/-! ### window evaluations -/

theorem tm_zero : ∀ (u d k : Nat), CBMEven.wtm K X (u + d + 1) k u = K.zero
  | 0, _, _ => rfl
  | u + 1, d, k => by
    rw [show u + 1 + d + 1 = u + d + 1 + 1 by lib_nat]
    exact tm_zero u d (k + 1)

theorem tm_ge (n k u : Nat) (h : n ≤ u) : CBMEven.wtm K X n k u = co K X (u + k) := by
  obtain ⟨d, hd⟩ := CBMEven.le_dest n u h
  subst hd
  rw [show n + d = d + n by lib_nat, CBMEven.tm_hi]

/-- untruncated window -/
theorem whw_hi : ∀ (n k u : Nat), n ≤ u + 1 → CBMEven.whw K X k n u = W K X (u + k) n
  | 0, _, _, _ => rfl
  | n + 1, k, u, h => by
    have h1 : n ≤ u := Nat.le_of_succ_le_succ h
    show K.add (CBMEven.whw K X (k + 2) n u) (CBMEven.wtm K X n k u) = _
    show _ = RhoCD.Wn K (co K X) (u + k) (n + 1)
    rw [whw_hi n (k + 2) u (Nat.le_succ_of_le h1), tm_ge K X n k u h1, RhoCD.wn_first]
    show K.add (RhoCD.Wn K (co K X) (u + k + 2) n) (co K X (u + k)) = K.add (co K X (u + k)) (RhoCD.Wn K (co K X) (u + k + 2) n)
    lib_ring

/-- truncation: the boxes below the point vanish -/
theorem whw_tr : ∀ (f k u : Nat), CBMEven.whw K X k (u + 1 + f) u = CBMEven.whw K X (k + f + f) (u + 1) u
  | 0, _, _ => rfl
  | f + 1, k, u => by
    show K.add (CBMEven.whw K X (k + 2) (u + 1 + f) u) (CBMEven.wtm K X (u + 1 + f) k u) = _
    rw [whw_tr f (k + 2) u, show u + 1 + f = u + f + 1 by lib_nat, tm_zero K X u f k,
      show k + 2 + f + f = k + (f + 1) + (f + 1) by lib_nat]
    lib_ring

/-! ### the positive building blocks -/

/-- F_k(P, P+m) = x_{P+k} x_{P+m} - x_P x_{P+m+k} -/
def fk (k P m : Nat) : R :=
  K.add (K.mul (co K X (P + k)) (co K X (P + m))) (K.neg (K.mul (co K X P) (co K X (P + m + k))))

/-- sum_{t<M} F_{k0+2t}(P, P+m) -/
def fs (M P m k0 : Nat) : R :=
  K.add (K.mul (co K X (P + m)) (W K X (P + k0) M)) (K.neg (K.mul (co K X P) (W K X (P + m + k0) M)))

theorem fs_step (M P m k0 : Nat) : fs K X (M + 1) P m k0 = K.add (fs K X M P m k0) (fk K X (k0 + M + M) P m) := by
  unfold fs fk
  show K.add (K.mul (co K X (P + m)) (K.add (W K X (P + k0) M) (co K X (P + k0 + M + M))))
      (K.neg (K.mul (co K X P) (K.add (W K X (P + m + k0) M) (co K X (P + m + k0 + M + M))))) = _
  rw [show P + (k0 + M + M) = P + k0 + M + M by lib_nat, show P + m + (k0 + M + M) = P + m + k0 + M + M by lib_nat]
  lib_ring

/-! ### the chain-kernel difference -/

def dk (g n u v : Nat) : R :=
  K.add (CBMEven.bkern K X g (n + 1) u v) (K.neg (CBMEven.bkern K X (g + 2) n u v))

def inc (g n u v : Nat) : R :=
  K.add (Lagrange.minor K (CBMEven.whw K X (g + 2) (n + 2)) (CBMEven.wtm K X (n + 1) (g + 1)) u v)
    (K.neg (Lagrange.minor K (CBMEven.whw K X (g + 3) (n + 1)) (CBMEven.wtm K X (n + 1) (g + 2)) u v))

theorem dk_step (g n u v : Nat) : dk K X g (n + 1) u v = K.add (inc K X g n u v) (dk K X (g + 1) n u v) := by
  have b1 : CBMEven.bkern K X g (n + 2) u v
      = K.add (Lagrange.minor K (CBMEven.whw K X (g + 2) (n + 2)) (CBMEven.whw K X (g + 1) (n + 2)) u v)
          (CBMEven.bkern K X (g + 1) (n + 1) u v) := rfl
  have b2 : CBMEven.bkern K X (g + 2) (n + 1) u v
      = K.add (Lagrange.minor K (CBMEven.whw K X (g + 4) (n + 1)) (CBMEven.whw K X (g + 3) (n + 1)) u v)
          (CBMEven.bkern K X (g + 3) n u v) := rfl
  have b3 : dk K X (g + 1) n u v
      = K.add (CBMEven.bkern K X (g + 1) (n + 1) u v) (K.neg (CBMEven.bkern K X (g + 3) n u v)) := rfl
  have w1 : ∀ w, CBMEven.whw K X (g + 2) (n + 2) w
      = K.add (CBMEven.whw K X (g + 4) (n + 1) w) (CBMEven.wtm K X (n + 1) (g + 2) w) := fun _ => rfl
  have w2 : ∀ w, CBMEven.whw K X (g + 1) (n + 2) w
      = K.add (CBMEven.whw K X (g + 3) (n + 1) w) (CBMEven.wtm K X (n + 1) (g + 1) w) := fun _ => rfl
  show K.add (CBMEven.bkern K X g (n + 2) u v) (K.neg (CBMEven.bkern K X (g + 2) (n + 1) u v)) = _
  rw [b1, b2, b3]
  unfold inc Lagrange.minor
  rw [w1 u, w1 v, w2 u, w2 v]
  lib_ring

/-- region (a): n <= u <= v -/
theorem dk_a : ∀ (n g u v : Nat), n ≤ u → u ≤ v →
    dk K X g n u v = K.add (K.mul (co K X (v + g + 1)) (W K X (u + g + 2) (n + 1)))
      (K.neg (K.mul (co K X (u + g + 1)) (W K X (v + g + 2) (n + 1))))
  | 0, g, u, v, _, _ => by
    show K.add (K.add (K.add (K.mul (K.add K.zero (co K X (u + g + 2))) (K.add K.zero (co K X (v + g + 1))))
        (K.neg (K.mul (K.add K.zero (co K X (v + g + 2))) (K.add K.zero (co K X (u + g + 1)))))) K.zero) (K.neg K.zero)
      = K.add (K.mul (co K X (v + g + 1)) (K.add K.zero (co K X (u + g + 2))))
          (K.neg (K.mul (co K X (u + g + 1)) (K.add K.zero (co K X (v + g + 2)))))
    lib_ring
  | n + 1, g, u, v, hu, huv => by
    have hv : n + 1 ≤ v := Nat.le_trans hu huv
    rw [dk_step, dk_a n (g + 1) u v (Nat.le_of_succ_le hu) huv]
    unfold inc Lagrange.minor
    rw [whw_hi K X (n + 2) (g + 2) u (Nat.succ_le_succ hu), whw_hi K X (n + 2) (g + 2) v (Nat.succ_le_succ hv),
      whw_hi K X (n + 1) (g + 3) u (Nat.le_succ_of_le hu), whw_hi K X (n + 1) (g + 3) v (Nat.le_succ_of_le hv),
      tm_ge K X (n + 1) (g + 1) u hu, tm_ge K X (n + 1) (g + 1) v hv,
      tm_ge K X (n + 1) (g + 2) u hu, tm_ge K X (n + 1) (g + 2) v hv]
    show K.add (K.add (K.add (K.mul (W K X (u + g + 2) (n + 2)) (co K X (v + g + 1)))
          (K.neg (K.mul (W K X (v + g + 2) (n + 2)) (co K X (u + g + 1)))))
        (K.neg (K.add (K.mul (W K X (u + g + 3) (n + 1)) (co K X (v + g + 2)))
          (K.neg (K.mul (W K X (v + g + 3) (n + 1)) (co K X (u + g + 2)))))))
      (K.add (K.mul (co K X (v + g + 2)) (W K X (u + g + 3) (n + 1)))
        (K.neg (K.mul (co K X (u + g + 2)) (W K X (v + g + 3) (n + 1)))))
      = K.add (K.mul (co K X (v + g + 1)) (W K X (u + g + 2) (n + 2)))
          (K.neg (K.mul (co K X (u + g + 1)) (W K X (v + g + 2) (n + 2))))
    lib_ring

/-- regions (b) and (c): u < n = u+1+f -/
theorem dk_bc : ∀ (f g u v : Nat), u < v →
    (u + 1 + f ≤ v → dk K X g (u + 1 + f) u v
      = K.add (K.mul (co K X (v + g + 1)) (W K X (u + g + f + f + 4) (u + 1)))
          (K.neg (K.mul (co K X (u + g + f + 2)) (W K X (v + g + f + 3) (u + 1))))) ∧
    (v ≤ u + f → dk K X g (u + 1 + f) u v = K.zero)
  | 0, g, u, v, huv => by
    constructor
    · intro _
      show dk K X g (u + 1) u v = _
      rw [dk_step, dk_a K X u (g + 1) u v (Nat.le_refl u) (Nat.le_of_lt huv)]
      unfold inc Lagrange.minor
      have b1 : CBMEven.whw K X (g + 2) (u + 2) u = W K X (u + g + 4) (u + 1) :=
        (whw_tr K X 1 (g + 2) u).trans (whw_hi K X (u + 1) (g + 2 + 1 + 1) u (Nat.le_refl _))
      rw [b1, whw_hi K X (u + 2) (g + 2) v (Nat.succ_le_succ huv),
        whw_hi K X (u + 1) (g + 3) u (Nat.le_refl _), whw_hi K X (u + 1) (g + 3) v (Nat.succ_le_succ (Nat.le_of_lt huv)),
        tm_ge K X (u + 1) (g + 1) v huv, tm_ge K X (u + 1) (g + 2) v huv,
        show CBMEven.wtm K X (u + 1) (g + 1) u = K.zero from tm_zero K X u 0 (g + 1),
        show CBMEven.wtm K X (u + 1) (g + 2) u = K.zero from tm_zero K X u 0 (g + 2)]
      show K.add (K.add (K.add (K.mul (W K X (u + g + 4) (u + 1)) (co K X (v + g + 1)))
            (K.neg (K.mul (W K X (v + g + 2) (u + 2)) K.zero)))
          (K.neg (K.add (K.mul (W K X (u + g + 3) (u + 1)) (co K X (v + g + 2)))
            (K.neg (K.mul (W K X (v + g + 3) (u + 1)) K.zero)))))
        (K.add (K.mul (co K X (v + g + 2)) (W K X (u + g + 3) (u + 1)))
          (K.neg (K.mul (co K X (u + g + 2)) (W K X (v + g + 3) (u + 1)))))
        = K.add (K.mul (co K X (v + g + 1)) (W K X (u + g + 4) (u + 1)))
            (K.neg (K.mul (co K X (u + g + 2)) (W K X (v + g + 3) (u + 1))))
      lib_ring
    · intro h
      exact absurd (Nat.lt_of_lt_of_le huv h) (Nat.lt_irrefl u)
  | f + 1, g, u, v, huv => by
    have ih := dk_bc f (g + 1) u v huv
    have st : dk K X g (u + 1 + (f + 1)) u v = K.add (inc K X g (u + 1 + f) u v) (dk K X (g + 1) (u + 1 + f) u v) :=
      dk_step K X g (u + 1 + f) u v
    have b1 : CBMEven.whw K X (g + 2) (u + 1 + f + 2) u = W K X (u + g + f + f + 6) (u + 1) := by
      rw [show u + g + f + f + 6 = u + (g + 2 + (f + 2) + (f + 2)) by lib_nat]
      exact (whw_tr K X (f + 2) (g + 2) u).trans (whw_hi K X (u + 1) _ u (Nat.le_refl _))
    have b3 : CBMEven.whw K X (g + 3) (u + 1 + f + 1) u = W K X (u + g + f + f + 5) (u + 1) := by
      rw [show u + g + f + f + 5 = u + (g + 3 + (f + 1) + (f + 1)) by lib_nat]
      exact (whw_tr K X (f + 1) (g + 3) u).trans (whw_hi K X (u + 1) _ u (Nat.le_refl _))
    have z1 : CBMEven.wtm K X (u + 1 + f + 1) (g + 1) u = K.zero := by
      rw [show u + 1 + f + 1 = u + (f + 1) + 1 by lib_nat]; exact tm_zero K X u (f + 1) (g + 1)
    have z2 : CBMEven.wtm K X (u + 1 + f + 1) (g + 2) u = K.zero := by
      rw [show u + 1 + f + 1 = u + (f + 1) + 1 by lib_nat]; exact tm_zero K X u (f + 1) (g + 2)
    constructor
    · intro h
      have h' : u + 1 + f + 1 ≤ v := h
      have ihc : dk K X (g + 1) (u + 1 + f) u v
          = K.add (K.mul (co K X (v + g + 2)) (W K X (u + g + f + f + 5) (u + 1)))
              (K.neg (K.mul (co K X (u + g + f + 3)) (W K X (v + g + f + 4) (u + 1)))) := by
        rw [show u + g + f + f + 5 = u + (g + 1) + f + f + 4 by lib_nat,
          show u + g + f + 3 = u + (g + 1) + f + 2 by lib_nat, show v + g + f + 4 = v + (g + 1) + f + 3 by lib_nat]
        exact ih.1 (Nat.le_of_succ_le h')
      rw [st, ihc]
      unfold inc Lagrange.minor
      rw [b1, b3, z1, z2, whw_hi K X (u + 1 + f + 2) (g + 2) v (Nat.succ_le_succ h'),
        whw_hi K X (u + 1 + f + 1) (g + 3) v (Nat.le_succ_of_le h'),
        tm_ge K X (u + 1 + f + 1) (g + 1) v h', tm_ge K X (u + 1 + f + 1) (g + 2) v h',
        show u + g + (f + 1) + (f + 1) + 4 = u + g + f + f + 6 by lib_nat,
        show u + g + (f + 1) + 2 = u + g + f + 3 by lib_nat, show v + g + (f + 1) + 3 = v + g + f + 4 by lib_nat]
      show K.add (K.add (K.add (K.mul (W K X (u + g + f + f + 6) (u + 1)) (co K X (v + g + 1)))
            (K.neg (K.mul (W K X (v + (g + 2)) (u + 1 + f + 2)) K.zero)))
          (K.neg (K.add (K.mul (W K X (u + g + f + f + 5) (u + 1)) (co K X (v + g + 2)))
            (K.neg (K.mul (W K X (v + (g + 3)) (u + 1 + f + 1)) K.zero)))))
        (K.add (K.mul (co K X (v + g + 2)) (W K X (u + g + f + f + 5) (u + 1)))
          (K.neg (K.mul (co K X (u + g + f + 3)) (W K X (v + g + f + 4) (u + 1)))))
        = K.add (K.mul (co K X (v + g + 1)) (W K X (u + g + f + f + 6) (u + 1)))
            (K.neg (K.mul (co K X (u + g + f + 3)) (W K X (v + g + f + 4) (u + 1))))
      lib_ring
    · intro h
      obtain ⟨d, hd⟩ := CBMEven.le_dest v (u + (f + 1)) h
      have hz : u + 1 + f + 1 = v + d + 1 := by rw [← hd]; lib_nat
      have t1 : CBMEven.wtm K X (u + 1 + f + 1) (g + 1) v = K.zero := by rw [hz]; exact tm_zero K X v d (g + 1)
      have t2 : CBMEven.wtm K X (u + 1 + f + 1) (g + 2) v = K.zero := by rw [hz]; exact tm_zero K X v d (g + 2)
      rw [st]
      unfold inc Lagrange.minor
      rw [z1, z2, t1, t2]
      cases Nat.lt_or_ge v (u + 1 + f) with
      | inl hlt =>
        have hlt' : v < u + f + 1 := by rw [show u + f + 1 = u + 1 + f by lib_nat]; exact hlt
        rw [ih.2 (Nat.le_of_lt_succ hlt')]
        lib_ring
      | inr hge =>
        have e1 := ih.1 hge
        obtain ⟨d', hd'⟩ := CBMEven.le_dest _ _ hge
        cases d' with
        | zero =>
          have hv : v = u + 1 + f := hd'
          rw [e1, hv, show u + 1 + f + (g + 1) + 1 = u + (g + 1) + f + 2 by lib_nat,
            show u + 1 + f + (g + 1) + f + 3 = u + (g + 1) + f + f + 4 by lib_nat]
          lib_ring
        | succ d'' =>
          have c1 := h
          rw [hd'] at c1
          have c2 : u + (f + 1) < u + 1 + f + (d'' + 1) := by
            rw [show u + 1 + f + (d'' + 1) = u + (f + 1) + d'' + 1 by lib_nat]
            exact Nat.lt_succ_of_le (Nat.le_add_right _ _)
          exact absurd (Nat.lt_of_lt_of_le c2 c1) (Nat.lt_irrefl _)

end

/-! ### positivity -/

variable (O : ORing K)

theorem fk_nn (X : List R) (hg : L1Full.Good K O X) : ∀ (k P m : Nat), O.le K.zero (fk K X k P m)
  | 0, P, m => by
    refine TripleE.le_of_eq' K O _ _ ?_
    show K.zero = K.add (K.mul (co K X P) (co K X (P + m))) (K.neg (K.mul (co K X P) (co K X (P + m))))
    lib_ring
  | k + 1, P, 0 => by
    refine TripleE.le_of_eq' K O _ _ ?_
    show K.zero = K.add (K.mul (co K X (P + (k + 1))) (co K X P)) (K.neg (K.mul (co K X P) (co K X (P + (k + 1)))))
    lib_ring
  | k + 1, P, m + 1 => by
    have e : fk K X (k + 1) P (m + 1) = K.add (VGoodTail.tt K X (P + 1) (P + 1 + (m + k) + 1)) (fk K X k (P + 1) m) := by
      unfold fk VGoodTail.tt
      show _ = K.add (K.add (K.mul (co K X (P + 1)) (co K X (P + 1 + (m + k))))
          (K.neg (K.mul (co K X P) (co K X (P + 1 + (m + k) + 1))))) _
      rw [show P + (k + 1) = P + 1 + k by lib_nat, show P + (m + 1) = P + 1 + m by lib_nat,
        show P + 1 + m + (k + 1) = P + 1 + (m + k) + 1 by lib_nat, show P + 1 + m + k = P + 1 + (m + k) by lib_nat]
      lib_ring
    rw [e]
    exact TopLC.add_nn K O _ _ (VGoodTail.tail_nn K O X hg (P + 1) (m + k)) (fk_nn X hg k (P + 1) m)

theorem fs_nn (X : List R) (hg : L1Full.Good K O X) : ∀ (M P m k0 : Nat), O.le K.zero (fs K X M P m k0)
  | 0, P, m, k0 => by
    refine TripleE.le_of_eq' K O _ _ ?_
    show K.zero = K.add (K.mul (co K X (P + m)) K.zero) (K.neg (K.mul (co K X P) K.zero))
    lib_ring
  | M + 1, P, m, k0 => by
    rw [fs_step]
    exact TopLC.add_nn K O _ _ (fs_nn X hg M P m k0) (fk_nn K O X hg (k0 + M + M) P m)

/-- flexible form of fs_nn -/
theorem fs_nn2 (X : List R) (hg : L1Full.Good K O X) (M P m k0 i1 i2 i3 : Nat)
    (h1 : i1 = P + m) (h2 : i2 = P + k0) (h3 : i3 = P + m + k0) :
    O.le K.zero (K.add (K.mul (co K X i1) (W K X i2 M)) (K.neg (K.mul (co K X P) (W K X i3 M)))) := by
  rw [h1, h2, h3]
  exact fs_nn K O X hg M P m k0

theorem dk_nn (X : List R) (hg : L1Full.Good K O X) (n g u m : Nat) : O.le K.zero (dk K X g n u (u + m + 1)) := by
  have huv : u < u + m + 1 := Nat.lt_succ_of_le (Nat.le_add_right _ _)
  cases Nat.lt_or_ge u n with
  | inr hu =>
    rw [dk_a K X n g u (u + m + 1) hu (Nat.le_of_lt huv)]
    exact fs_nn2 K O X hg (n + 1) (u + g + 1) (m + 1) 1 _ _ _ (by lib_nat) (by lib_nat) (by lib_nat)
  | inl hlt =>
    obtain ⟨f, hf⟩ := CBM2.lt_dest u n hlt
    rw [hf, show u + f + 1 = u + 1 + f by lib_nat]
    have hb := dk_bc K X f g u (u + m + 1) huv
    cases Nat.lt_or_ge (u + m + 1) (u + 1 + f) with
    | inl hv =>
      have hv' : u + m + 1 < u + f + 1 := by rw [show u + f + 1 = u + 1 + f by lib_nat]; exact hv
      rw [hb.2 (Nat.le_of_lt_succ hv')]
      exact O.le_refl _
    | inr hv =>
      rw [hb.1 hv]
      obtain ⟨d, hd⟩ := CBMEven.le_dest _ _ hv
      rw [hd]
      exact fs_nn2 K O X hg (u + 1) (u + g + f + 2) d (f + 2) _ _ _ (by lib_nat) (by lib_nat) (by lib_nat)

/-- **the box-chain kernel is nonnegative** -/
theorem bk_nn (X : List R) (hg : L1Full.Good K O X) :
    ∀ (n g u m : Nat), O.le K.zero (CBMEven.bkern K X g n u (u + m + 1))
  | 0, _, _, _ => O.le_refl _
  | n + 1, g, u, m => by
    have e : CBMEven.bkern K X g (n + 1) u (u + m + 1)
        = K.add (dk K X g n u (u + m + 1)) (CBMEven.bkern K X (g + 2) n u (u + m + 1)) := by
      unfold dk; lib_ring
    rw [e]
    exact TopLC.add_nn K O _ _ (dk_nn K O X hg n g u m) (bk_nn X hg n (g + 2) u m)

/-- **CB-M at every even N = 2Q+2, every v-good list of every length** -/
theorem cbm_even (X : List R) (hg : L1Full.Good K O X) (Q : Nat) :
    O.le (K.mul (ReduceCBM.Mt K (Q + 1) 0 X) (ReduceCBM.Mt K (Q + 1) 0 X))
      (LibRing.sub K (K.mul (cgc K X X (Q + 1 + (Q + 1))) (cgc K X X (Q + 1 + (Q + 1))))
        (K.mul (cgc K X X (Q + Q + 1)) (cgc K X X (Q + 1 + (Q + 1) + 1)))) :=
  CBMEven.cbm_even_of_kern K O X hg Q (fun i m => bk_nn K O X hg (Q + 1) 0 i m)

/-- **(A) at every odd index n = 2Q+1**, every v-good list of every length -/
theorem a_odd (X : List R) (hg : L1Full.Good K O X) (Q : Nat) :
    O.le (K.mul (LibRing.castN K ((Q + Q + 1 + 2) * (Q + Q + 1 + 2))) (K.mul (cgc K X X (Q + Q + 1)) (cgc K X X (Q + Q + 1 + 2))))
      (K.mul (LibRing.castN K ((Q + Q + 1 + 1) * (Q + Q + 1 + 3))) (K.mul (cgc K X X (Q + Q + 1 + 1)) (cgc K X X (Q + Q + 1 + 1)))) := by
  have h := cbm_even K O X hg Q
  rw [show Q + 1 + (Q + 1) = Q + Q + 1 + 1 by lib_nat] at h
  exact ReduceCBM.a_of_cbm K O X hg (Q + Q + 1) (Q + 1) 0 (by decide) (by lib_nat) h

end CBMKern

#print axioms CBMKern.tm_zero
#print axioms CBMKern.tm_ge
#print axioms CBMKern.whw_hi
#print axioms CBMKern.whw_tr
#print axioms CBMKern.fs_step
#print axioms CBMKern.dk_step
#print axioms CBMKern.dk_a
#print axioms CBMKern.dk_bc
#print axioms CBMKern.fk_nn
#print axioms CBMKern.fs_nn
#print axioms CBMKern.fs_nn2
#print axioms CBMKern.dk_nn
#print axioms CBMKern.bk_nn
#print axioms CBMKern.cbm_even
#print axioms CBMKern.a_odd

ym/CBMOdd.lean

/-
  ym/CBMOdd — T45, G69b3 (odd N): CB-M at every odd N = 2Q+3 and (A) at every even n = 2Q+2,
  for every v-good list of every length.
    c_N^2 - c_{N-1} c_{N+1} - M_N^2 = 2 Phi(1,P) + 2 [Phi(0,P) + box(-1,P+1)] + (Mt(P,0) Mt(P+1,0) - Mt(P,1)^2),
  P = Q+1 (odd_id; a ring identity in the staircase atoms G and the tail correlations Mt), where
  box(-1,P+1) is the Cauchy–Binet value of the windows Hw(1,P+1), Hw(0,P+1). The chain kernel starting at
  g = -1 is dk0 + bkern(1,·) with dk0 given in closed form by the regions of CBMKern at g = 0 (dk0_a, dk0_bc),
  hence nonnegative; the last term is Lagrange's identity. Lean 4 core only, no axioms.
-/
import ym.CBMKern

namespace CBMOdd

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom

variable {R : Type} (K : DetChain.CRing R)

section
variable (X : List R)

abbrev W (i m : Nat) : R := RhoCD.Wn K (co K X) i m

/-- <y, tm(n+1, 0)> = Mt(n,1) + Mt(n+1,1) -/
theorem ip_tm_y00 (n NN : Nat) (h : X.length + n + 1 ≤ NN) :
    CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.wtm K X (n + 1) 0) NN
      = K.add (ReduceCBM.Mt K n 1 X) (ReduceCBM.Mt K (n + 1) 1 X) := by
  obtain ⟨d, hd⟩ := CBMEven.le_dest _ _ h
  rw [hd, show X.length + n + 1 + d = X.length + d + (n + 1) by lib_nat, CBMEven.ip_tm]
  rw [sumN_ext K _ (fun i => K.add (K.mul (co K X (i + n)) (co K X (i + n + 1)))
      (K.mul (co K X (i + (n + 1))) (co K X (i + (n + 1) + 1))))
    (fun i => by
      try dsimp only
      rw [VGoodTail.yv_eq]
      show K.mul (K.add (co K X (i + n)) (co K X (i + (n + 1) + 1))) (co K X (i + (n + 1) + 0)) = _
      show _ = K.add (K.mul (co K X (i + n)) (co K X (i + (n + 1) + 0))) (K.mul (co K X (i + (n + 1) + 0)) (co K X (i + (n + 1) + 1)))
      lib_ring), sumN_add K]
  rw [CBMEven.sum_mt K X n 1 _ (Nat.le_add_right _ _) _ (fun _ => rfl),
    CBMEven.sum_mt K X (n + 1) 1 _ (Nat.le_add_right _ _) _ (fun _ => rfl)]

/-- the Cauchy–Binet value of the box (-1, n) -/
def cbox0 (NN n : Nat) : R :=
  K.add (K.mul (CauchyBinet2.ip K (co K X) (CBMEven.whw K X 1 n) NN) (CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.whw K X 0 n) NN))
    (K.neg (K.mul (CauchyBinet2.ip K (co K X) (CBMEven.whw K X 0 n) NN) (CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.whw K X 1 n) NN)))

/-- **the odd identity**, N = 2Q+3 -/
theorem odd_id (Q : Nat) :
    LibRing.sub K (K.mul (cgc K X X (Q + 1 + (Q + 1) + 1)) (cgc K X X (Q + 1 + (Q + 1) + 1)))
        (K.mul (cgc K X X (Q + 1 + (Q + 1))) (cgc K X X (Q + 2 + (Q + 2))))
      = K.add (K.mul (ReduceCBM.Mt K (Q + 1) 1 X) (ReduceCBM.Mt K (Q + 1) 1 X))
          (K.add (K.add (CBMEven.cbsum K X (X.length + Q + 2) 1 (Q + 1)) (CBMEven.cbsum K X (X.length + Q + 2) 1 (Q + 1)))
            (K.add (K.add (K.add (CBMEven.cbsum K X (X.length + Q + 2) 0 (Q + 1)) (cbox0 K X (X.length + Q + 2) (Q + 2)))
                (K.add (CBMEven.cbsum K X (X.length + Q + 2) 0 (Q + 1)) (cbox0 K X (X.length + Q + 2) (Q + 2))))
              (K.add (K.mul (ReduceCBM.Mt K (Q + 1) 0 X) (ReduceCBM.Mt K (Q + 2) 0 X))
                (K.neg (K.mul (ReduceCBM.Mt K (Q + 1) 1 X) (ReduceCBM.Mt K (Q + 1) 1 X)))))) := by
  have hN : X.length + (Q + 1) ≤ X.length + Q + 2 := Nat.le_succ _
  have hN2 : X.length + (Q + 2) ≤ X.length + Q + 2 := Nat.le_refl _
  have c1 : cgc K X X (Q + 1 + (Q + 1) + 1) = K.add (CBMEven.sg K X 1 (Q + 2)) (CBMEven.sg K X 1 (Q + 2)) :=
    CBMEven.c_odd K X (Q + 1)
  have c0 : cgc K X X (Q + 1 + (Q + 1)) = K.add (CBMEven.sg K X 2 (Q + 1)) (CBMEven.sg K X 0 (Q + 2)) :=
    CBMEven.c_even K X (Q + 1)
  have c2 : cgc K X X (Q + 2 + (Q + 2)) = K.add (CBMEven.sg K X 2 (Q + 2)) (CBMEven.sg K X 0 (Q + 3)) :=
    CBMEven.c_even K X (Q + 2)
  rw [CBMEven.cbs_eq K X (Q + 1) 1 _ hN, CBMEven.cbs_eq K X (Q + 1) 0 _ hN, c1, c0, c2]
  unfold cbox0
  have x1 : CauchyBinet2.ip K (co K X) (CBMEven.whw K X 1 (Q + 2)) (X.length + Q + 2) = CBMEven.sg K X 1 (Q + 2) :=
    CBMEven.hw_x K X (Q + 2) 1 _ hN2
  have x0 : CauchyBinet2.ip K (co K X) (CBMEven.whw K X 0 (Q + 2)) (X.length + Q + 2) = CBMEven.sg K X 0 (Q + 2) :=
    CBMEven.hw_x K X (Q + 2) 0 _ hN2
  have y1 : CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.whw K X 1 (Q + 2)) (X.length + Q + 2)
      = K.add (CBMEven.sg K X 2 (Q + 1)) (CBMEven.sg K X 0 (Q + 3)) := CBMEven.hw_y K X (Q + 1) 0 _ hN2
  have y0 : CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.whw K X 0 (Q + 2)) (X.length + Q + 2)
      = K.add (K.add (CBMEven.sg K X 3 Q) (CBMEven.sg K X 1 (Q + 2)))
          (K.add (ReduceCBM.Mt K Q 1 X) (ReduceCBM.Mt K (Q + 1) 1 X)) := by
    have e3 : CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.whw K X 0 (Q + 2)) (X.length + Q + 2)
        = K.add (CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.whw K X 2 (Q + 1)) (X.length + Q + 2))
            (CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.wtm K X (Q + 1) 0) (X.length + Q + 2)) :=
      CBMEven.ip_add K (VGoodPair.yv K X) (CBMEven.whw K X 2 (Q + 1)) (CBMEven.wtm K X (Q + 1) 0) _
    have hy2 : CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.whw K X 2 (Q + 1)) (X.length + Q + 2)
        = K.add (CBMEven.sg K X 3 Q) (CBMEven.sg K X 1 (Q + 2)) := CBMEven.hw_y K X Q 1 _ (Nat.le_succ _)
    rw [e3, hy2, ip_tm_y00 K X Q _ (Nat.le_succ _)]
  rw [x1, x0, y1, y0]
  -- staircase recursions (definitional)
  have g02 : CBMEven.sg K X 0 (Q + 2) = K.add (CBMEven.sg K X 2 (Q + 1)) (ReduceCBM.Mt K (Q + 1) 0 X) := rfl
  have g12 : CBMEven.sg K X 1 (Q + 2) = K.add (CBMEven.sg K X 3 (Q + 1)) (ReduceCBM.Mt K (Q + 1) 1 X) := rfl
  have g22 : CBMEven.sg K X 2 (Q + 2) = K.add (CBMEven.sg K X 4 (Q + 1)) (ReduceCBM.Mt K (Q + 1) 2 X) := rfl
  have g03 : CBMEven.sg K X 0 (Q + 3) = K.add (CBMEven.sg K X 2 (Q + 2)) (ReduceCBM.Mt K (Q + 2) 0 X) := rfl
  have g11 : CBMEven.sg K X 1 (Q + 1) = K.add (CBMEven.sg K X 3 Q) (ReduceCBM.Mt K Q 1 X) := rfl
  have p1 : CBMEven.cdp K X 1 (Q + 1) = K.add (K.mul (CBMEven.sg K X 3 (Q + 1)) (CBMEven.sg K X 1 (Q + 2)))
      (K.neg (K.mul (CBMEven.sg K X 2 (Q + 1)) (CBMEven.sg K X 2 (Q + 2)))) := rfl
  have p0 : CBMEven.cdp K X 0 (Q + 1) = K.add (K.mul (CBMEven.sg K X 2 (Q + 1)) (CBMEven.sg K X 0 (Q + 2)))
      (K.neg (K.mul (CBMEven.sg K X 1 (Q + 1)) (CBMEven.sg K X 1 (Q + 2)))) := rfl
  rw [p1, p0]
  rw [g03, g02, g12, g22, g11]
  unfold LibRing.sub
  lib_ring

/-! ### the chain kernel from g = -1 -/

/-- the kernel of the chain starting at the box (-1, n+1) -/
def k0 (n u v : Nat) : R :=
  K.add (Lagrange.minor K (CBMEven.whw K X 1 (n + 1)) (CBMEven.whw K X 0 (n + 1)) u v) (CBMEven.bkern K X 0 n u v)

def dk0 (n u v : Nat) : R := K.add (k0 K X n u v) (K.neg (CBMEven.bkern K X 1 n u v))

def inc0 (n u v : Nat) : R :=
  K.add (Lagrange.minor K (CBMEven.whw K X 1 (n + 2)) (CBMEven.wtm K X (n + 1) 0) u v)
    (K.neg (Lagrange.minor K (CBMEven.whw K X 2 (n + 1)) (CBMEven.wtm K X (n + 1) 1) u v))

theorem dk0_step (n u v : Nat) : dk0 K X (n + 1) u v = K.add (inc0 K X n u v) (CBMKern.dk K X 0 n u v) := by
  have b2 : CBMEven.bkern K X 1 (n + 1) u v
      = K.add (Lagrange.minor K (CBMEven.whw K X 3 (n + 1)) (CBMEven.whw K X 2 (n + 1)) u v)
          (CBMEven.bkern K X 2 n u v) := rfl
  have b3 : CBMKern.dk K X 0 n u v
      = K.add (CBMEven.bkern K X 0 (n + 1) u v) (K.neg (CBMEven.bkern K X 2 n u v)) := rfl
  have w1 : ∀ w, CBMEven.whw K X 1 (n + 2) w
      = K.add (CBMEven.whw K X 3 (n + 1) w) (CBMEven.wtm K X (n + 1) 1 w) := fun _ => rfl
  have w0 : ∀ w, CBMEven.whw K X 0 (n + 2) w
      = K.add (CBMEven.whw K X 2 (n + 1) w) (CBMEven.wtm K X (n + 1) 0 w) := fun _ => rfl
  show K.add (K.add (Lagrange.minor K (CBMEven.whw K X 1 (n + 2)) (CBMEven.whw K X 0 (n + 2)) u v)
      (CBMEven.bkern K X 0 (n + 1) u v)) (K.neg (CBMEven.bkern K X 1 (n + 1) u v)) = _
  rw [b2, b3]
  unfold inc0 Lagrange.minor
  rw [w1 u, w1 v, w0 u, w0 v]
  lib_ring

theorem dk0_a : ∀ (n u v : Nat), n ≤ u → u ≤ v →
    dk0 K X n u v = K.add (K.mul (co K X v) (W K X (u + 1) (n + 1))) (K.neg (K.mul (co K X u) (W K X (v + 1) (n + 1))))
  | 0, u, v, _, _ => by
    show K.add (K.add (K.add (K.mul (K.add K.zero (co K X (u + 1))) (K.add K.zero (co K X v)))
        (K.neg (K.mul (K.add K.zero (co K X (v + 1))) (K.add K.zero (co K X u))))) K.zero) (K.neg K.zero)
      = K.add (K.mul (co K X v) (K.add K.zero (co K X (u + 1)))) (K.neg (K.mul (co K X u) (K.add K.zero (co K X (v + 1)))))
    lib_ring
  | n + 1, u, v, hu, huv => by
    have hv : n + 1 ≤ v := Nat.le_trans hu huv
    rw [dk0_step, CBMKern.dk_a K X n 0 u v (Nat.le_of_succ_le hu) huv]
    unfold inc0 Lagrange.minor
    rw [CBMKern.whw_hi K X (n + 2) 1 u (Nat.succ_le_succ hu), CBMKern.whw_hi K X (n + 2) 1 v (Nat.succ_le_succ hv),
      CBMKern.whw_hi K X (n + 1) 2 u (Nat.le_succ_of_le hu), CBMKern.whw_hi K X (n + 1) 2 v (Nat.le_succ_of_le hv),
      CBMKern.tm_ge K X (n + 1) 0 u hu, CBMKern.tm_ge K X (n + 1) 0 v hv,
      CBMKern.tm_ge K X (n + 1) 1 u hu, CBMKern.tm_ge K X (n + 1) 1 v hv]
    show K.add (K.add (K.add (K.mul (W K X (u + 1) (n + 2)) (co K X v))
          (K.neg (K.mul (W K X (v + 1) (n + 2)) (co K X u))))
        (K.neg (K.add (K.mul (W K X (u + 2) (n + 1)) (co K X (v + 1)))
          (K.neg (K.mul (W K X (v + 2) (n + 1)) (co K X (u + 1)))))))
      (K.add (K.mul (co K X (v + 1)) (W K X (u + 2) (n + 1)))
        (K.neg (K.mul (co K X (u + 1)) (W K X (v + 2) (n + 1)))))
      = K.add (K.mul (co K X v) (W K X (u + 1) (n + 2))) (K.neg (K.mul (co K X u) (W K X (v + 1) (n + 2))))
    lib_ring

theorem dk0_bc (f u v : Nat) (huv : u < v) :
    (u + 1 + f ≤ v → dk0 K X (u + 1 + f) u v
      = K.add (K.mul (co K X v) (W K X (u + f + f + 3) (u + 1)))
          (K.neg (K.mul (co K X (u + f + 1)) (W K X (v + f + 2) (u + 1))))) ∧
    (v ≤ u + f → dk0 K X (u + 1 + f) u v = K.zero) := by
  cases f with
  | zero =>
    constructor
    · intro _
      show dk0 K X (u + 1) u v = _
      rw [dk0_step, CBMKern.dk_a K X u 0 u v (Nat.le_refl u) (Nat.le_of_lt huv)]
      unfold inc0 Lagrange.minor
      have b1 : CBMEven.whw K X 1 (u + 2) u = W K X (u + 3) (u + 1) :=
        (CBMKern.whw_tr K X 1 1 u).trans (CBMKern.whw_hi K X (u + 1) (1 + 1 + 1) u (Nat.le_refl _))
      rw [b1, CBMKern.whw_hi K X (u + 2) 1 v (Nat.succ_le_succ huv),
        CBMKern.whw_hi K X (u + 1) 2 u (Nat.le_refl _), CBMKern.whw_hi K X (u + 1) 2 v (Nat.succ_le_succ (Nat.le_of_lt huv)),
        CBMKern.tm_ge K X (u + 1) 0 v huv, CBMKern.tm_ge K X (u + 1) 1 v huv,
        show CBMEven.wtm K X (u + 1) 0 u = K.zero from CBMKern.tm_zero K X u 0 0,
        show CBMEven.wtm K X (u + 1) 1 u = K.zero from CBMKern.tm_zero K X u 0 1]
      show K.add (K.add (K.add (K.mul (W K X (u + 3) (u + 1)) (co K X v))
            (K.neg (K.mul (W K X (v + 1) (u + 2)) K.zero)))
          (K.neg (K.add (K.mul (W K X (u + 2) (u + 1)) (co K X (v + 1)))
            (K.neg (K.mul (W K X (v + 2) (u + 1)) K.zero)))))
        (K.add (K.mul (co K X (v + 1)) (W K X (u + 2) (u + 1)))
          (K.neg (K.mul (co K X (u + 1)) (W K X (v + 2) (u + 1)))))
        = K.add (K.mul (co K X v) (W K X (u + 3) (u + 1))) (K.neg (K.mul (co K X (u + 1)) (W K X (v + 2) (u + 1))))
      lib_ring
    · intro h
      exact absurd (Nat.lt_of_lt_of_le huv h) (Nat.lt_irrefl u)
  | succ f =>
    have hb := CBMKern.dk_bc K X f 0 u v huv
    have st : dk0 K X (u + 1 + (f + 1)) u v = K.add (inc0 K X (u + 1 + f) u v) (CBMKern.dk K X 0 (u + 1 + f) u v) :=
      dk0_step K X (u + 1 + f) u v
    have b1 : CBMEven.whw K X 1 (u + 1 + f + 2) u = W K X (u + f + f + 5) (u + 1) := by
      rw [show u + f + f + 5 = u + (1 + (f + 2) + (f + 2)) by lib_nat]
      exact (CBMKern.whw_tr K X (f + 2) 1 u).trans (CBMKern.whw_hi K X (u + 1) _ u (Nat.le_refl _))
    have b3 : CBMEven.whw K X 2 (u + 1 + f + 1) u = W K X (u + f + f + 4) (u + 1) := by
      rw [show u + f + f + 4 = u + (2 + (f + 1) + (f + 1)) by lib_nat]
      exact (CBMKern.whw_tr K X (f + 1) 2 u).trans (CBMKern.whw_hi K X (u + 1) _ u (Nat.le_refl _))
    have z1 : CBMEven.wtm K X (u + 1 + f + 1) 0 u = K.zero := by
      rw [show u + 1 + f + 1 = u + (f + 1) + 1 by lib_nat]; exact CBMKern.tm_zero K X u (f + 1) 0
    have z2 : CBMEven.wtm K X (u + 1 + f + 1) 1 u = K.zero := by
      rw [show u + 1 + f + 1 = u + (f + 1) + 1 by lib_nat]; exact CBMKern.tm_zero K X u (f + 1) 1
    constructor
    · intro h
      have h' : u + 1 + f + 1 ≤ v := h
      have ihc : CBMKern.dk K X 0 (u + 1 + f) u v
          = K.add (K.mul (co K X (v + 1)) (W K X (u + f + f + 4) (u + 1)))
              (K.neg (K.mul (co K X (u + f + 2)) (W K X (v + f + 3) (u + 1)))) := hb.1 (Nat.le_of_succ_le h')
      rw [st, ihc]
      unfold inc0 Lagrange.minor
      rw [b1, b3, z1, z2, CBMKern.whw_hi K X (u + 1 + f + 2) 1 v (Nat.succ_le_succ h'),
        CBMKern.whw_hi K X (u + 1 + f + 1) 2 v (Nat.le_succ_of_le h'),
        CBMKern.tm_ge K X (u + 1 + f + 1) 0 v h', CBMKern.tm_ge K X (u + 1 + f + 1) 1 v h',
        show u + (f + 1) + (f + 1) + 3 = u + f + f + 5 by lib_nat,
        show u + (f + 1) + 1 = u + f + 2 by lib_nat, show v + (f + 1) + 2 = v + f + 3 by lib_nat]
      show K.add (K.add (K.add (K.mul (W K X (u + f + f + 5) (u + 1)) (co K X v))
            (K.neg (K.mul (W K X (v + 1) (u + 1 + f + 2)) K.zero)))
          (K.neg (K.add (K.mul (W K X (u + f + f + 4) (u + 1)) (co K X (v + 1)))
            (K.neg (K.mul (W K X (v + 2) (u + 1 + f + 1)) K.zero)))))
        (K.add (K.mul (co K X (v + 1)) (W K X (u + f + f + 4) (u + 1)))
          (K.neg (K.mul (co K X (u + f + 2)) (W K X (v + f + 3) (u + 1)))))
        = K.add (K.mul (co K X v) (W K X (u + f + f + 5) (u + 1))) (K.neg (K.mul (co K X (u + f + 2)) (W K X (v + f + 3) (u + 1))))
      lib_ring
    · intro h
      obtain ⟨d, hd⟩ := CBMEven.le_dest v (u + (f + 1)) h
      have hz : u + 1 + f + 1 = v + d + 1 := by rw [← hd]; lib_nat
      have t1 : CBMEven.wtm K X (u + 1 + f + 1) 0 v = K.zero := by rw [hz]; exact CBMKern.tm_zero K X v d 0
      have t2 : CBMEven.wtm K X (u + 1 + f + 1) 1 v = K.zero := by rw [hz]; exact CBMKern.tm_zero K X v d 1
      rw [st]
      unfold inc0 Lagrange.minor
      rw [z1, z2, t1, t2]
      cases Nat.lt_or_ge v (u + 1 + f) with
      | inl hlt =>
        have hlt' : v < u + f + 1 := by rw [show u + f + 1 = u + 1 + f by lib_nat]; exact hlt
        rw [hb.2 (Nat.le_of_lt_succ hlt')]
        lib_ring
      | inr hge =>
        have e1 : CBMKern.dk K X 0 (u + 1 + f) u v
            = K.add (K.mul (co K X (v + 1)) (W K X (u + f + f + 4) (u + 1)))
                (K.neg (K.mul (co K X (u + f + 2)) (W K X (v + f + 3) (u + 1)))) := hb.1 hge
        obtain ⟨d', hd'⟩ := CBMEven.le_dest _ _ hge
        cases d' with
        | zero =>
          have hv : v = u + 1 + f := hd'
          rw [e1, hv, show u + 1 + f + 1 = u + f + 2 by lib_nat, show u + 1 + f + f + 3 = u + f + f + 4 by lib_nat]
          lib_ring
        | succ d'' =>
          have c1 := h
          rw [hd'] at c1
          have c2 : u + (f + 1) < u + 1 + f + (d'' + 1) := by
            rw [show u + 1 + f + (d'' + 1) = u + (f + 1) + d'' + 1 by lib_nat]
            exact Nat.lt_succ_of_le (Nat.le_add_right _ _)
          exact absurd (Nat.lt_of_lt_of_le c2 c1) (Nat.lt_irrefl _)

/-- the chain from (-1, n+1) as a double sum -/
theorem chain0_dbl (NN n : Nat) :
    K.add (CBMEven.cbsum K X NN 0 n) (cbox0 K X NN (n + 1))
      = sumN K (fun j => sumN K (fun i => K.mul (Lagrange.minor K (co K X) (VGoodPair.yv K X) i j) (k0 K X n i j)) j) NN := by
  rw [CBMEven.cbs_dbl K X NN n 0]
  unfold cbox0
  rw [CauchyBinet2.cauchy_binet K (co K X) (VGoodPair.yv K X) (CBMEven.whw K X 1 (n + 1)) (CBMEven.whw K X 0 (n + 1)) NN]
  unfold CauchyBinet2.mm
  rw [← sumN_add K]
  apply sumN_ext K
  intro j
  try dsimp only
  rw [← sumN_add K]
  apply sumN_ext K
  intro i
  try dsimp only
  show _ = K.mul (Lagrange.minor K (co K X) (VGoodPair.yv K X) i j)
    (K.add (Lagrange.minor K (CBMEven.whw K X 1 (n + 1)) (CBMEven.whw K X 0 (n + 1)) i j) (CBMEven.bkern K X 0 n i j))
  lib_ring

end

/-! ### positivity -/

variable (O : ORing K)

theorem dk0_nn (X : List R) (hg : L1Full.Good K O X) (n u m : Nat) : O.le K.zero (dk0 K X n u (u + m + 1)) := by
  have huv : u < u + m + 1 := Nat.lt_succ_of_le (Nat.le_add_right _ _)
  cases Nat.lt_or_ge u n with
  | inr hu =>
    rw [dk0_a K X n u (u + m + 1) hu (Nat.le_of_lt huv)]
    exact CBMKern.fs_nn2 K O X hg (n + 1) u (m + 1) 1 _ _ _ (by lib_nat) (by lib_nat) (by lib_nat)
  | inl hlt =>
    obtain ⟨f, hf⟩ := CBM2.lt_dest u n hlt
    rw [hf, show u + f + 1 = u + 1 + f by lib_nat]
    have hb := dk0_bc K X f u (u + m + 1) huv
    cases Nat.lt_or_ge (u + m + 1) (u + 1 + f) with
    | inl hv =>
      have hv' : u + m + 1 < u + f + 1 := by rw [show u + f + 1 = u + 1 + f by lib_nat]; exact hv
      rw [hb.2 (Nat.le_of_lt_succ hv')]
      exact O.le_refl _
    | inr hv =>
      rw [hb.1 hv]
      obtain ⟨d, hd⟩ := CBMEven.le_dest _ _ hv
      rw [hd]
      exact CBMKern.fs_nn2 K O X hg (u + 1) (u + f + 1) d (f + 2) _ _ _ (by lib_nat) (by lib_nat) (by lib_nat)

theorem k0_nn (X : List R) (hg : L1Full.Good K O X) (n u m : Nat) : O.le K.zero (k0 K X n u (u + m + 1)) := by
  have e : k0 K X n u (u + m + 1) = K.add (dk0 K X n u (u + m + 1)) (CBMEven.bkern K X 1 n u (u + m + 1)) := by
    unfold dk0; lib_ring
  rw [e]
  exact TopLC.add_nn K O _ _ (dk0_nn K O X hg n u m) (CBMKern.bk_nn K O X hg n 1 u m)

/-- a double sum against D with a kernel nonnegative above the diagonal is nonnegative -/
theorem dbl_nn (X : List R) (hg : L1Full.Good K O X) (kern : Nat → Nat → R)
    (hk : ∀ i m, O.le K.zero (kern i (i + m + 1))) (NN : Nat) :
    O.le K.zero (sumN K (fun j => sumN K (fun i => K.mul (Lagrange.minor K (co K X) (VGoodPair.yv K X) i j) (kern i j)) j) NN) := by
  apply CauchyBinet2.sum_nn K O
  intro j
  have t := Lagrange.sum_le_lt K O (fun _ => K.zero)
    (fun i => K.mul (Lagrange.minor K (co K X) (VGoodPair.yv K X) i j) (kern i j)) j
    (fun i hi => by
      obtain ⟨m, hm⟩ := CBM2.lt_dest i j hi
      subst hm
      exact O.mul_nonneg _ _ (VGoodTail.pair_all K O X hg i m) (hk i m))
  rw [CBM2.sum_zero] at t
  exact t

theorem lag_nn (X : List R) (Q : Nat) :
    O.le K.zero (K.add (K.mul (ReduceCBM.Mt K (Q + 1) 0 X) (ReduceCBM.Mt K (Q + 2) 0 X))
      (K.neg (K.mul (ReduceCBM.Mt K (Q + 1) 1 X) (ReduceCBM.Mt K (Q + 1) 1 X)))) := by
  rw [CBLib.mt_sum K (Q + 1) 0 X X.length (Nat.le_refl _), CBLib.mt_sum K (Q + 2) 0 X X.length (Nat.le_refl _),
    CBLib.mt_sum K (Q + 1) 1 X X.length (Nat.le_refl _)]
  have L := Lagrange.lagrange K (fun i => co K X (i + (Q + 1))) (fun i => co K X (i + (Q + 2))) X.length
  have hm : O.le K.zero (Lagrange.msq K (fun i => co K X (i + (Q + 1))) (fun i => co K X (i + (Q + 2))) X.length) :=
    CauchyBinet2.sum_nn K O _ (fun j => CauchyBinet2.sum_nn K O _ (fun i => CBLib.sq_nn K O _) j) _
  exact O.le_trans _ _ _ hm (TripleE.le_of_eq' K O _ _ L.symm)

/-- **CB-M at every odd N = 2Q+3**, every v-good list of every length -/
theorem cbm_odd (X : List R) (hg : L1Full.Good K O X) (Q : Nat) :
    O.le (K.mul (ReduceCBM.Mt K (Q + 1) 1 X) (ReduceCBM.Mt K (Q + 1) 1 X))
      (LibRing.sub K (K.mul (cgc K X X (Q + 1 + (Q + 1) + 1)) (cgc K X X (Q + 1 + (Q + 1) + 1)))
        (K.mul (cgc K X X (Q + 1 + (Q + 1))) (cgc K X X (Q + 2 + (Q + 2))))) := by
  rw [odd_id K X Q]
  have p1 : O.le K.zero (CBMEven.cbsum K X (X.length + Q + 2) 1 (Q + 1)) := by
    rw [CBMEven.cbs_dbl K X]
    exact dbl_nn K O X hg _ (fun i m => CBMKern.bk_nn K O X hg (Q + 1) 1 i m) _
  have p0' : O.le K.zero (K.add (CBMEven.cbsum K X (X.length + Q + 2) 0 (Q + 1)) (cbox0 K X (X.length + Q + 2) (Q + 1 + 1))) := by
    rw [chain0_dbl K X]
    exact dbl_nn K O X hg _ (fun i m => k0_nn K O X hg (Q + 1) i m) _
  have p0 : O.le K.zero (K.add (CBMEven.cbsum K X (X.length + Q + 2) 0 (Q + 1)) (cbox0 K X (X.length + Q + 2) (Q + 2))) := p0'
  have pl := lag_nn K O X Q
  have s := TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ p1 p1) (TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ p0 p0) pl)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.add_le_add _ _ _ _ (O.le_refl _) s)
  lib_ring

/-- **(A) at every even index n = 2Q+2**, every v-good list of every length -/
theorem a_even (X : List R) (hg : L1Full.Good K O X) (Q : Nat) :
    O.le (K.mul (LibRing.castN K ((Q + Q + 2 + 2) * (Q + Q + 2 + 2))) (K.mul (cgc K X X (Q + Q + 2)) (cgc K X X (Q + Q + 2 + 2))))
      (K.mul (LibRing.castN K ((Q + Q + 2 + 1) * (Q + Q + 2 + 3))) (K.mul (cgc K X X (Q + Q + 2 + 1)) (cgc K X X (Q + Q + 2 + 1)))) := by
  have h := cbm_odd K O X hg Q
  rw [show Q + 1 + (Q + 1) = Q + Q + 2 by lib_nat, show Q + 2 + (Q + 2) = Q + Q + 2 + 2 by lib_nat] at h
  exact ReduceCBM.a_of_cbm K O X hg (Q + Q + 2) (Q + 1) 1 (by decide) (by lib_nat) h

end CBMOdd

#print axioms CBMOdd.ip_tm_y00
#print axioms CBMOdd.odd_id
#print axioms CBMOdd.dk0_step
#print axioms CBMOdd.dk0_a
#print axioms CBMOdd.dk0_bc
#print axioms CBMOdd.chain0_dbl
#print axioms CBMOdd.dk0_nn
#print axioms CBMOdd.k0_nn
#print axioms CBMOdd.dbl_nn
#print axioms CBMOdd.lag_nn
#print axioms CBMOdd.cbm_odd
#print axioms CBMOdd.a_even

ym/AAll.lean

/-
  ym/AAll — T45, G69: (A) at every index n for every v-good list of every length:
    (n+2)^2 c_n c_{n+2} <= (n+1)(n+3) c_{n+1}^2.
  n = 2Q+1: CBMKern.a_odd; n = 2Q+2: CBMOdd.a_even; n = 0: CB-M at N = 1,
    c_1^2 - c_0 c_2 - Mt(0,1)^2 = 2 box(-1,1) + (Mt(0,0) Mt(1,0) - Mt(0,1)^2)
  (the chain from g = -1 with one box, kernel CBMOdd.k0 0 >= 0, and Lagrange). Lean 4 core only, no axioms.
-/
import ym.CBMOdd

namespace AAll

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom

variable {R : Type} (K : DetChain.CRing R)

section
variable (X : List R)

/-- <y, x> = 2 Mt(0,1) -/
theorem ip_y00 (NN : Nat) (h : X.length + 1 ≤ NN) :
    CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.wtm K X 0 0) NN
      = K.add (ReduceCBM.Mt K 0 1 X) (ReduceCBM.Mt K 0 1 X) := by
  obtain ⟨d, hd⟩ := CBMEven.le_dest _ _ h
  rw [hd, show X.length + 1 + d = X.length + d + 1 + 0 by lib_nat, CBMEven.ip_tm]
  rw [sumN_ext K _ (fun i => K.add (K.mul (VGoodTail.cm K X i) (co K X (i + 0 + 0)))
      (K.mul (co K X (i + 0)) (co K X (i + 0 + 1))))
    (fun i => by
      try dsimp only
      rw [VGoodTail.yv_eq]
      show K.mul (K.add (VGoodTail.cm K X i) (co K X (i + 1))) (co K X i)
        = K.add (K.mul (VGoodTail.cm K X i) (co K X i)) (K.mul (co K X i) (co K X (i + 1)))
      lib_ring), sumN_add K]
  have a1 : sumN K (fun i => K.mul (co K X (i + 0)) (co K X (i + 0 + 1))) (X.length + d + 1) = ReduceCBM.Mt K 0 1 X :=
    CBMEven.sum_mt K X 0 1 _ (Nat.le_succ_of_le (Nat.le_add_right _ _)) _ (fun _ => rfl)
  have a0 : sumN K (fun i => K.mul (VGoodTail.cm K X i) (co K X (i + 0 + 0))) (X.length + d + 1) = ReduceCBM.Mt K 0 1 X := by
    rw [sumN_shift K]
    show K.add (K.mul K.zero _) (sumN K (fun i => K.mul (co K X i) (co K X (i + 1 + 0 + 0))) (X.length + d)) = _
    have s0 := CBMEven.sum_mt K X 0 1 (X.length + d) (Nat.le_add_right _ _)
      (fun i => K.mul (co K X i) (co K X (i + 1 + 0 + 0))) (fun _ => rfl)
    rw [s0]
    lib_ring
  rw [a0, a1]

/-- CB-M at N = 1 as an identity -/
theorem one_id :
    LibRing.sub K (K.mul (cgc K X X 1) (cgc K X X 1)) (K.mul (cgc K X X 0) (cgc K X X 2))
      = K.add (K.mul (ReduceCBM.Mt K 0 1 X) (ReduceCBM.Mt K 0 1 X))
          (K.add (K.add (CBMOdd.cbox0 K X (X.length + 1) 1) (CBMOdd.cbox0 K X (X.length + 1) 1))
            (K.add (K.mul (ReduceCBM.Mt K 0 0 X) (ReduceCBM.Mt K 1 0 X))
              (K.neg (K.mul (ReduceCBM.Mt K 0 1 X) (ReduceCBM.Mt K 0 1 X))))) := by
  have c1 : cgc K X X 1 = K.add (CBMEven.sg K X 1 1) (CBMEven.sg K X 1 1) := CBMEven.c_odd K X 0
  have c0 : cgc K X X 0 = K.add (CBMEven.sg K X 2 0) (CBMEven.sg K X 0 1) := CBMEven.c_even K X 0
  have c2 : cgc K X X 2 = K.add (CBMEven.sg K X 2 1) (CBMEven.sg K X 0 2) := CBMEven.c_even K X 1
  rw [c1, c0, c2]
  unfold CBMOdd.cbox0
  have hN : X.length + 1 ≤ X.length + 1 := Nat.le_refl _
  have x1 : CauchyBinet2.ip K (co K X) (CBMEven.whw K X 1 1) (X.length + 1) = CBMEven.sg K X 1 1 :=
    CBMEven.hw_x K X 1 1 _ hN
  have x0 : CauchyBinet2.ip K (co K X) (CBMEven.whw K X 0 1) (X.length + 1) = CBMEven.sg K X 0 1 :=
    CBMEven.hw_x K X 1 0 _ hN
  have y1 : CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.whw K X 1 1) (X.length + 1)
      = K.add (CBMEven.sg K X 2 0) (CBMEven.sg K X 0 2) := CBMEven.hw_y K X 0 0 _ hN
  have y0 : CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.whw K X 0 1) (X.length + 1)
      = K.add (ReduceCBM.Mt K 0 1 X) (ReduceCBM.Mt K 0 1 X) := by
    have e3 : CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.whw K X 0 1) (X.length + 1)
        = K.add (CauchyBinet2.ip K (VGoodPair.yv K X) (fun _ => K.zero) (X.length + 1))
            (CauchyBinet2.ip K (VGoodPair.yv K X) (CBMEven.wtm K X 0 0) (X.length + 1)) :=
      CBMEven.ip_add K (VGoodPair.yv K X) (fun _ => K.zero) (CBMEven.wtm K X 0 0) _
    rw [e3, CBMEven.ip_zero, ip_y00 K X _ hN]
    lib_ring
  rw [x1, x0, y1, y0]
  have g11 : CBMEven.sg K X 1 1 = K.add K.zero (ReduceCBM.Mt K 0 1 X) := rfl
  have g20 : CBMEven.sg K X 2 0 = K.zero := rfl
  have g01 : CBMEven.sg K X 0 1 = K.add K.zero (ReduceCBM.Mt K 0 0 X) := rfl
  have g21 : CBMEven.sg K X 2 1 = K.add K.zero (ReduceCBM.Mt K 0 2 X) := rfl
  have g02 : CBMEven.sg K X 0 2 = K.add (K.add K.zero (ReduceCBM.Mt K 0 2 X)) (ReduceCBM.Mt K 1 0 X) := rfl
  rw [g11, g20, g01, g21, g02]
  unfold LibRing.sub
  lib_ring

end

variable (O : ORing K)

theorem lag0 (X : List R) :
    O.le K.zero (K.add (K.mul (ReduceCBM.Mt K 0 0 X) (ReduceCBM.Mt K 1 0 X))
      (K.neg (K.mul (ReduceCBM.Mt K 0 1 X) (ReduceCBM.Mt K 0 1 X)))) := by
  rw [CBLib.mt_sum K 0 0 X X.length (Nat.le_refl _), CBLib.mt_sum K 1 0 X X.length (Nat.le_refl _),
    CBLib.mt_sum K 0 1 X X.length (Nat.le_refl _)]
  have L := Lagrange.lagrange K (fun i => co K X (i + 0)) (fun i => co K X (i + 1)) X.length
  have hm : O.le K.zero (Lagrange.msq K (fun i => co K X (i + 0)) (fun i => co K X (i + 1)) X.length) :=
    CauchyBinet2.sum_nn K O _ (fun j => CauchyBinet2.sum_nn K O _ (fun i => CBLib.sq_nn K O _) j) _
  exact O.le_trans _ _ _ hm (TripleE.le_of_eq' K O _ _ L.symm)

theorem cbm_one (X : List R) (hg : L1Full.Good K O X) :
    O.le (K.mul (ReduceCBM.Mt K 0 1 X) (ReduceCBM.Mt K 0 1 X))
      (LibRing.sub K (K.mul (cgc K X X 1) (cgc K X X 1)) (K.mul (cgc K X X 0) (cgc K X X 2))) := by
  rw [one_id K X]
  have pb' : O.le K.zero (K.add (CBMEven.cbsum K X (X.length + 1) 0 0) (CBMOdd.cbox0 K X (X.length + 1) (0 + 1))) := by
    rw [CBMOdd.chain0_dbl K X]
    exact CBMOdd.dbl_nn K O X hg _ (fun i m => CBMOdd.k0_nn K O X hg 0 i m) _
  have pb : O.le K.zero (CBMOdd.cbox0 K X (X.length + 1) 1) := by
    refine O.le_trans _ _ _ pb' (TripleE.le_of_eq' K O _ _ ?_)
    show K.add K.zero (CBMOdd.cbox0 K X (X.length + 1) 1) = _
    lib_ring
  have s := TopLC.add_nn K O _ _ (TopLC.add_nn K O _ _ pb pb) (lag0 K O X)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.add_le_add _ _ _ _ (O.le_refl _) s)
  lib_ring

theorem par : ∀ n : Nat, n = 0 ∨ (∃ Q, n = Q + Q + 1) ∨ (∃ Q, n = Q + Q + 2)
  | 0 => Or.inl rfl
  | 1 => Or.inr (Or.inl ⟨0, rfl⟩)
  | n + 2 => by
    cases par n with
    | inl h => exact Or.inr (Or.inr ⟨0, by subst h; rfl⟩)
    | inr h =>
      cases h with
      | inl h => obtain ⟨Q, hQ⟩ := h; exact Or.inr (Or.inl ⟨Q + 1, by subst hQ; lib_nat⟩)
      | inr h => obtain ⟨Q, hQ⟩ := h; exact Or.inr (Or.inr ⟨Q + 1, by subst hQ; lib_nat⟩)

/-- **(A) at every index n for every v-good list of every length** -/
theorem a_all (X : List R) (hg : L1Full.Good K O X) (n : Nat) :
    O.le (K.mul (LibRing.castN K ((n + 2) * (n + 2))) (K.mul (cgc K X X n) (cgc K X X (n + 2))))
      (K.mul (LibRing.castN K ((n + 1) * (n + 3))) (K.mul (cgc K X X (n + 1)) (cgc K X X (n + 1)))) := by
  cases par n with
  | inl h =>
    subst h
    exact ReduceCBM.a_of_cbm K O X hg 0 0 1 (by decide) rfl (cbm_one K O X hg)
  | inr h =>
    cases h with
    | inl h => obtain ⟨Q, hQ⟩ := h; subst hQ; exact CBMKern.a_odd K O X hg Q
    | inr h => obtain ⟨Q, hQ⟩ := h; subst hQ; exact CBMOdd.a_even K O X hg Q

end AAll

#print axioms AAll.ip_y00
#print axioms AAll.one_id
#print axioms AAll.lag0
#print axioms AAll.cbm_one
#print axioms AAll.par
#print axioms AAll.a_all

#print axioms: 59 theorems, no axioms.

90.124 The Migdal–Kadanoff flow stays below the heat kernel

At every step the 4D Migdal–Kadanoff flow keeps v-goodness, a nonincreasing normalized profile, and the LR bound by the heat list.

\[ X_0=K_{P^4}g_0\ (g_0\downarrow ,\ 2P\le 1)\ \Longrightarrow \ X_k\ \text {v-good},\ X_k/(n{+}1)\downarrow ,\ X_k=K_{P^4}g_k\ (g_k\downarrow )\quad (k\ge 0) \]

ym/FlowStep.lean

/-
  ym/FlowStep — T45, G44 flow class: one 4D Migdal–Kadanoff step (two bond-moving products, then decimation)
  keeps the class "v-good and a_n = X_n/(n+1) nonincreasing", for every list of every length.
  Ingredients: L1Full.Good_prod (products keep v-goodness), DimRow.prod_anoninc (products keep a nonincreasing),
  AAll.a_all ((A) at every index: the product's a is log-concave), DimRow.decM_good (decimation keeps VV and
  the base condition given a >= 0 nonincreasing and log-concave), and co(decM Y)_n = (n+1) a_n^4.
  Lean 4 core only, no axioms.
-/
import ym.AAll
import ym.DimRow
import ym.HeatSecondStep

namespace FlowStep

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem len_decM : ∀ (m : Nat) (a : List R), (decM K O F m a).length = a.length
  | _, [] => rfl
  | m, _ :: a => by
    show (decM K O F (m + 1) a).length + 1 = a.length + 1
    rw [len_decM (m + 1) a]

theorem mul_ne (F' : OField.OF K O) (h10 : K.one ≠ K.zero) (a b : R) (ha : a ≠ K.zero) (hb : b ≠ K.zero) : K.mul a b ≠ K.zero := fun h => by
  have e : a = K.mul (K.mul a b) (F'.inv b) := by
    rw [K.mul_assoc, F'.mul_inv b hb]; lib_ring
  rw [h] at e
  exact ha (e.trans (by lib_ring))

/-- the class: a_n = X_n/(n+1) nonincreasing (cross-multiplied) -/
def ANoninc (X : List R) : Prop := ∀ k, O.le (K.mul (co K X (k + 1)) (cN K (k + 1))) (K.mul (co K X k) (cN K (k + 2)))

/-- the two products Y = (X·X)·(X·X) -/
def Ysq (X : List R) (M : Nat) : List R := L1Full.prodL K (L1Full.prodL K X M) (2 * M + 1)

/-- the MK step: two products, then decimation -/
def mk (X : List R) (M : Nat) : List R := decM K O F 0 (Ysq K X M)

theorem fourth (h1 : O.le K.zero K.one) (u v c1 c2 : R) (hc1 : O.le K.zero c1) (hc2 : O.le K.zero c2)
    (h : O.le (K.mul (K.mul u u) (K.mul u u)) (K.mul (K.mul v v) (K.mul v v))) :
    O.le (K.mul (K.mul c2 (K.mul (K.mul u u) (K.mul u u))) c1) (K.mul (K.mul c1 (K.mul (K.mul v v) (K.mul v v))) c2) := by
  have w := O.mul_le_mul_right _ _ (K.mul c1 c2) h (O.mul_nonneg _ _ hc1 hc2)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

theorem len_mk (X : List R) (M : Nat) : (mk K O F X M).length = 4 * M + 5 := by
  unfold mk Ysq
  rw [len_decM, HeatSecondStep.len_prodL]
  lib_nat

/-- **one MK step keeps the flow class** -/
theorem flow_step (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X : List R) (hXg : L1Full.Good K O X)
    (M : Nat) (hX : X.length = M + 2) (ha : ANoninc K O X) :
    L1Full.Good K O (mk K O F X M) ∧ ANoninc K O (mk K O F X M) := by
  -- the two products
  have hZl : (L1Full.prodL K X M).length = 2 * M + 1 + 2 := HeatSecondStep.len_prodL K X M
  have hZg := L1Full.Good_prod K O X hXg M hX F
  have hYg : L1Full.Good K O (Ysq K X M) := L1Full.Good_prod K O (L1Full.prodL K X M) hZg (2 * M + 1) hZl F
  have coZ := L1Full.co_prodL K X M hX
  have coY : ∀ t, co K (Ysq K X M) t = cgc K (L1Full.prodL K X M) (L1Full.prodL K X M) t :=
    L1Full.co_prodL K (L1Full.prodL K X M) (2 * M + 1) hZl
  -- a of Z nonincreasing
  have haZ : ANoninc K O (L1Full.prodL K X M) := fun k => by
    rw [coZ, coZ]; exact DimRow.prod_anoninc K O F h1 h10 X hXg M hX ha k
  have hY0 := hYg.1
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have ipos : ∀ k, O.le K.zero (F.inv (cN K (k + 1))) := fun k => F.inv_pos _ (nn _) (DimRow.cN_pos K O h1 h10 k)
  have mi : ∀ k, K.mul (cN K (k + 1)) (F.inv (cN K (k + 1))) = K.one := fun k => F.mul_inv _ (DimRow.cN_pos K O h1 h10 k)
  -- the decimation hypotheses for Y
  have hA : ∀ n, O.le K.zero (K.mul (co K (Ysq K X M) n) (F.inv (cN K (n + 1)))) := fun n => O.mul_nonneg _ _ (hY0 n) (ipos n)
  have hdec : ∀ n, O.le (K.mul (co K (Ysq K X M) (n + 1)) (F.inv (cN K (n + 1 + 1)))) (K.mul (co K (Ysq K X M) n) (F.inv (cN K (n + 1)))) := by
    intro n
    have t : O.le (K.mul (co K (Ysq K X M) (n + 1)) (cN K (n + 1))) (K.mul (co K (Ysq K X M) n) (cN K (n + 1 + 1))) := by
      rw [coY, coY]; exact DimRow.prod_anoninc K O F h1 h10 _ hZg (2 * M + 1) hZl haZ n
    have w := O.mul_le_mul_right _ _ (K.mul (F.inv (cN K (n + 1))) (F.inv (cN K (n + 1 + 1)))) t
      (O.mul_nonneg _ _ (ipos n) (ipos (n + 1)))
    have e1 : K.mul (K.mul (co K (Ysq K X M) (n + 1)) (cN K (n + 1))) (K.mul (F.inv (cN K (n + 1))) (F.inv (cN K (n + 1 + 1))))
        = K.mul (K.mul (co K (Ysq K X M) (n + 1)) (F.inv (cN K (n + 1 + 1)))) (K.mul (cN K (n + 1)) (F.inv (cN K (n + 1)))) := by lib_ring
    have e2 : K.mul (K.mul (co K (Ysq K X M) n) (cN K (n + 1 + 1))) (K.mul (F.inv (cN K (n + 1))) (F.inv (cN K (n + 1 + 1))))
        = K.mul (K.mul (co K (Ysq K X M) n) (F.inv (cN K (n + 1)))) (K.mul (cN K (n + 1 + 1)) (F.inv (cN K (n + 1 + 1)))) := by lib_ring
    rw [e1, e2, mi n, mi (n + 1)] at w
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  have hlc : ∀ n, O.le (K.mul (K.mul (co K (Ysq K X M) n) (F.inv (cN K (n + 1)))) (K.mul (co K (Ysq K X M) (n + 2)) (F.inv (cN K (n + 2 + 1)))))
      (K.mul (K.mul (co K (Ysq K X M) (n + 1)) (F.inv (cN K (n + 1 + 1)))) (K.mul (co K (Ysq K X M) (n + 1)) (F.inv (cN K (n + 1 + 1))))) := by
    intro n
    have t := AAll.a_all K O _ hZg n
    rw [← coY, ← coY, ← coY, RingZ.castN_mul K (n + 2) (n + 2), RingZ.castN_mul K (n + 1) (n + 3)] at t
    have t' : O.le (K.mul (K.mul (cN K (n + 1 + 1)) (cN K (n + 1 + 1))) (K.mul (co K (Ysq K X M) n) (co K (Ysq K X M) (n + 2))))
        (K.mul (K.mul (cN K (n + 1)) (cN K (n + 2 + 1))) (K.mul (co K (Ysq K X M) (n + 1)) (co K (Ysq K X M) (n + 1)))) := t
    have hI : O.le K.zero (K.mul (K.mul (F.inv (cN K (n + 1))) (F.inv (cN K (n + 2 + 1)))) (K.mul (F.inv (cN K (n + 1 + 1))) (F.inv (cN K (n + 1 + 1))))) := O.mul_nonneg _ _ (O.mul_nonneg _ _ (ipos n) (ipos (n + 2))) (O.mul_nonneg _ _ (ipos (n + 1)) (ipos (n + 1)))
    have w := O.mul_le_mul_right _ _ _ t' hI
    have e1 : K.mul (K.mul (K.mul (cN K (n + 1 + 1)) (cN K (n + 1 + 1))) (K.mul (co K (Ysq K X M) n) (co K (Ysq K X M) (n + 2)))) (K.mul (K.mul (F.inv (cN K (n + 1))) (F.inv (cN K (n + 2 + 1)))) (K.mul (F.inv (cN K (n + 1 + 1))) (F.inv (cN K (n + 1 + 1)))))
        = K.mul (K.mul (K.mul (co K (Ysq K X M) n) (F.inv (cN K (n + 1)))) (K.mul (co K (Ysq K X M) (n + 2)) (F.inv (cN K (n + 2 + 1)))))
            (K.mul (K.mul (cN K (n + 1 + 1)) (F.inv (cN K (n + 1 + 1)))) (K.mul (cN K (n + 1 + 1)) (F.inv (cN K (n + 1 + 1))))) := by
      lib_ring
    have e2 : K.mul (K.mul (K.mul (cN K (n + 1)) (cN K (n + 2 + 1))) (K.mul (co K (Ysq K X M) (n + 1)) (co K (Ysq K X M) (n + 1)))) (K.mul (K.mul (F.inv (cN K (n + 1))) (F.inv (cN K (n + 2 + 1)))) (K.mul (F.inv (cN K (n + 1 + 1))) (F.inv (cN K (n + 1 + 1)))))
        = K.mul (K.mul (K.mul (co K (Ysq K X M) (n + 1)) (F.inv (cN K (n + 1 + 1)))) (K.mul (co K (Ysq K X M) (n + 1)) (F.inv (cN K (n + 1 + 1)))))
            (K.mul (K.mul (cN K (n + 1)) (F.inv (cN K (n + 1)))) (K.mul (cN K (n + 2 + 1)) (F.inv (cN K (n + 2 + 1))))) := by
      lib_ring
    rw [e1, e2, mi n, mi (n + 1), mi (n + 2)] at w
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  have dg := DimRow.decM_good K O F h1 h10 (Ysq K X M) hA hdec hlc hYg.2.2.1 hYg.2.2.2
  have eq := fun n => DimRow.decM_Yd K O F h1 h10 (Ysq K X M) n
  refine ⟨⟨fun j => ?_, fun j hj => ?_, dg.1, dg.2⟩, fun k => ?_⟩
  · show O.le K.zero (co K (decM K O F 0 (Ysq K X M)) j)
    rw [eq j]; exact O.mul_nonneg _ _ (nn _) (O.mul_nonneg _ _ (CBLib.sq_nn K O _) (CBLib.sq_nn K O _))
  · show co K (decM K O F 0 (Ysq K X M)) j ≠ K.zero
    have hjY : j < (Ysq K X M).length := by
      have := hj; unfold mk at this; rw [len_decM] at this; exact this
    have hy : co K (Ysq K X M) j ≠ K.zero := hYg.2.1 j hjY
    have han : K.mul (co K (Ysq K X M) j) (F.inv (cN K (j + 1))) ≠ K.zero :=
      mul_ne K O F h10 _ _ hy (OField.inv_ne K O F _ (DimRow.cN_pos K O h1 h10 j) h10)
    rw [eq j]
    exact mul_ne K O F h10 _ _ (DimRow.cN_pos K O h1 h10 j)
      (mul_ne K O F h10 _ _ (mul_ne K O F h10 _ _ han han) (mul_ne K O F h10 _ _ han han))
  · show O.le (K.mul (co K (decM K O F 0 (Ysq K X M)) (k + 1)) (cN K (k + 1))) (K.mul (co K (decM K O F 0 (Ysq K X M)) k) (cN K (k + 2)))
    rw [eq (k + 1), eq k]
    have s1 := DecVV.sq_mono K O _ _ (hA (k + 1)) (hdec k)
    have s2 := DecVV.sq_mono K O _ _ (O.mul_nonneg _ _ (hA (k + 1)) (hA (k + 1))) s1
    exact fourth K O h1 _ _ (cN K (k + 1)) (cN K (k + 2)) (nn _) (nn _) s2

end FlowStep

#print axioms FlowStep.len_decM
#print axioms FlowStep.mul_ne
#print axioms FlowStep.fourth
#print axioms FlowStep.len_mk
#print axioms FlowStep.flow_step

ym/FlowIter.lean

/-
  ym/FlowIter — T45, G44/G60: the 4D Migdal–Kadanoff flow keeps, at every step k, the class
    "v-good, a_n = X_n/(n+1) nonincreasing, and X_k = K_q·g_k with g_k >= 0 nonincreasing"  (q = P^4, 0 < 2P <= 1),
  i.e. X_k stays below the heat list K_q in LR order at every step, for every starting length.
  Step: FlowStep.flow_step (class) and HeatMKClass.mk_class (LR comparison), turned back into the ratio form
  g = X / K_q (ratio_form). Lean 4 core only, no axioms.
-/
import ym.FlowStep
import ym.HeatMKClass

namespace FlowIter

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)
include F

/-- divide a cross-multiplied inequality by two positive elements -/
theorem div_le (A B c1 c2 : R) (hc1 : O.le K.zero c1) (hc2 : O.le K.zero c2) (n1 : c1 ≠ K.zero) (n2 : c2 ≠ K.zero)
    (h : O.le (K.mul A c1) (K.mul B c2)) : O.le (K.mul A (F.inv c2)) (K.mul B (F.inv c1)) := by
  have w := O.mul_le_mul_right _ _ (K.mul (F.inv c1) (F.inv c2)) h (O.mul_nonneg _ _ (F.inv_pos _ hc1 n1) (F.inv_pos _ hc2 n2))
  have e1 : K.mul (K.mul A c1) (K.mul (F.inv c1) (F.inv c2)) = K.mul (K.mul A (F.inv c2)) (K.mul c1 (F.inv c1)) := by lib_ring
  have e2 : K.mul (K.mul B c2) (K.mul (F.inv c1) (F.inv c2)) = K.mul (K.mul B (F.inv c1)) (K.mul c2 (F.inv c2)) := by lib_ring
  rw [e1, e2, F.mul_inv c1 n1, F.mul_inv c2 n2] at w
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

/-- the heat list has nonzero coefficients inside its length -/
theorem heat_ne (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (q : R) (hq0 : O.le K.zero q) (hqne : q ≠ K.zero)
    (L j : Nat) (h : j < L) : co K (HeatDec.heat K q 0 L) j ≠ K.zero := by
  rw [HeatGood.coY K q L j h]
  exact FlowStep.mul_ne K O F h10 _ _ (DimRow.cN_pos K O h1 h10 j) (HeatGood.pw_ne K O h1 q hq0 hqne h10 _)

/-- **LR comparison back to the ratio form**: W below the heat list of its own length gives W = K_q·g -/
theorem ratio_form (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (q : R) (hq0 : O.le K.zero q) (hqne : q ≠ K.zero)
    (W : List R) (L : Nat) (hL : W.length = L) (hW0 : ∀ k, O.le K.zero (co K W k))
    (hlr : ∀ n, O.le (K.mul (co K W (n + 1)) (co K (HeatDec.heat K q 0 L) n))
      (K.mul (co K W n) (co K (HeatDec.heat K q 0 L) (n + 1)))) :
    ∃ g : Nat → R, (∀ m, O.le (g (m + 1)) (g m)) ∧ (∀ m, O.le K.zero (g m)) ∧
      (∀ k, co K W k = K.mul (co K (HeatDec.heat K q 0 L) k) (g k)) := by
  have Hnn : ∀ k, O.le K.zero (co K (HeatDec.heat K q 0 L) k) := fun k => HeatGood.co_nn K O h1 q hq0 L k
  have Wout : ∀ k, L ≤ k → co K W k = K.zero := fun k hk => HeatLR.co_out K W k (by rw [hL]; exact hk)
  refine ⟨fun k => K.mul (co K W k) (F.inv ((co K (HeatDec.heat K q 0 L) k))), fun m => ?_, fun m => ?_, fun k => ?_⟩
  · cases Nat.lt_or_ge (m + 1) L with
    | inl hm =>
      exact div_le K O F _ _ _ _ (Hnn m) (Hnn (m + 1)) (heat_ne K O F h1 h10 q hq0 hqne L m (Nat.lt_of_succ_lt hm))
        (heat_ne K O F h1 h10 q hq0 hqne L (m + 1) hm) (hlr m)
    | inr hm =>
      show O.le (K.mul (co K W (m + 1)) (F.inv ((co K (HeatDec.heat K q 0 L) (m + 1))))) (K.mul (co K W m) (F.inv ((co K (HeatDec.heat K q 0 L) m))))
      rw [Wout (m + 1) hm, show K.mul K.zero (F.inv ((co K (HeatDec.heat K q 0 L) (m + 1)))) = K.zero by lib_ring]
      cases Nat.lt_or_ge m L with
      | inl h => exact O.mul_nonneg _ _ (hW0 m) (F.inv_pos _ (Hnn m) (heat_ne K O F h1 h10 q hq0 hqne L m h))
      | inr h => rw [Wout m h, show K.mul K.zero (F.inv ((co K (HeatDec.heat K q 0 L) m))) = K.zero by lib_ring]; exact O.le_refl _
  · show O.le K.zero (K.mul (co K W m) (F.inv ((co K (HeatDec.heat K q 0 L) m))))
    cases Nat.lt_or_ge m L with
    | inl h => exact O.mul_nonneg _ _ (hW0 m) (F.inv_pos _ (Hnn m) (heat_ne K O F h1 h10 q hq0 hqne L m h))
    | inr h => rw [Wout m h, show K.mul K.zero (F.inv ((co K (HeatDec.heat K q 0 L) m))) = K.zero by lib_ring]; exact O.le_refl _
  · show co K W k = K.mul ((co K (HeatDec.heat K q 0 L) k)) (K.mul (co K W k) (F.inv ((co K (HeatDec.heat K q 0 L) k))))
    cases Nat.lt_or_ge k L with
    | inl h =>
      have e := F.mul_inv _ (heat_ne K O F h1 h10 q hq0 hqne L k h)
      have r : K.mul ((co K (HeatDec.heat K q 0 L) k)) (K.mul (co K W k) (F.inv ((co K (HeatDec.heat K q 0 L) k)))) = K.mul (co K W k) (K.mul ((co K (HeatDec.heat K q 0 L) k)) (F.inv ((co K (HeatDec.heat K q 0 L) k)))) := by lib_ring
      rw [r]
      show co K W k = K.mul (co K W k) (K.mul (co K (HeatDec.heat K q 0 L) k) (F.inv (co K (HeatDec.heat K q 0 L) k)))
      rw [e]; lib_ring
    | inr h => rw [Wout k h]; lib_ring

/-- the flow: lengths M_{k+1} = 4 M_k + 3 (so |X_{k+1}| = 4 M_k + 5) -/
def itM (M : Nat) : Nat → Nat
  | 0 => M
  | k + 1 => 4 * itM M k + 3

def itX (X : List R) (M : Nat) : Nat → List R
  | 0 => X
  | k + 1 => FlowStep.mk K O F (itX X M k) (itM M k)

/-- the invariant class at step k -/
def Cls (q : R) (X : List R) (M : Nat) : Prop :=
  L1Full.Good K O X ∧ FlowStep.ANoninc K O X ∧ X.length = M + 2 ∧
    ∃ g : Nat → R, (∀ m, O.le (g (m + 1)) (g m)) ∧ (∀ m, O.le K.zero (g m)) ∧
      (∀ k, co K X k = K.mul (co K (HeatDec.heat K q 0 (M + 2)) k) (g k))

/-- **the MK flow stays in the class at every step** (q = P⁴, 0 < P, 2P ≤ 1) -/
theorem flow_iter (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (P : R) (hP0 : O.le K.zero P)
    (hPne : P ≠ K.zero) (h2P : O.le (K.mul (cN K 2) P) K.one) (X : List R) (M : Nat)
    (h0 : Cls K O (pwR K (pwR K P 2) 2) X M) :
    ∀ k, Cls K O (pwR K (pwR K P 2) 2) (itX K O F X M k) (itM M k)
  | 0 => h0
  | k + 1 => by
    have ⟨hg, ha, hl, g, gdec, gnn, gxy⟩ := flow_iter h1 h10 P hP0 hPne h2P X M h0 k
    have fs := FlowStep.flow_step K O F h1 h10 _ hg _ hl ha
    have hq0 : O.le K.zero (pwR K (pwR K P 2) 2) := AreaLower.pw_nn K O _ (AreaLower.pw_nn K O P hP0 h1 2) h1 2
    have hqne : pwR K (pwR K P 2) 2 ≠ K.zero :=
      HeatGood.pw_ne K O h1 _ (AreaLower.pw_nn K O P hP0 h1 2) (HeatGood.pw_ne K O h1 P hP0 hPne h10 2) h10 2
    have hlen : (itX K O F X M (k + 1)).length = 4 * itM M k + 5 := FlowStep.len_mk K O F _ _
    have lr := fun n => HeatMKClass.mk_class K O F h1 h10 P hP0 hPne h2P _ hg _ hl g gdec gnn gxy n
    obtain ⟨g', gd', gn', gx'⟩ := ratio_form K O F h1 h10 _ hq0 hqne (itX K O F X M (k + 1)) (4 * itM M k + 5) hlen
      fs.1.1 lr
    exact ⟨fs.1, fs.2, hlen, g', gd', gn', gx'⟩

end FlowIter

#print axioms FlowIter.div_le
#print axioms FlowIter.heat_ne
#print axioms FlowIter.ratio_form
#print axioms FlowIter.flow_iter

ym/FlowHeat.lean

/-
  ym/FlowHeat — T45, G44: starting from the heat-kernel weight itself, the 4D Migdal–Kadanoff flow stays in the
  class of FlowIter at every step: for q = P^4 with 0 < P, 2P <= 1, every length M+2 and every k,
  mk^k(K_q) is v-good, has a_n = X_n/(n+1) nonincreasing and X_k = K_q·g_k with g_k >= 0 nonincreasing.
  (The heat list is v-good by HeatGood.heat_good and its a_n = q^{n(n+2)} is nonincreasing for q <= 1.)
  Lean 4 core only, no axioms.
-/
import ym.FlowIter
import ym.GKS

namespace FlowHeat

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem pw1 : ∀ k, pwR K K.one k = K.one
  | 0 => rfl
  | k + 1 => by
    show K.mul (pwR K K.one k) K.one = K.one
    rw [pw1 k]; lib_ring

theorem pw_le1 (h1 : O.le K.zero K.one) (q : R) (hq0 : O.le K.zero q) (hq1 : O.le q K.one) (k : Nat) :
    O.le (pwR K q k) K.one := by
  have t := SU2Strong4D.pw_mono K O h1 q K.one hq0 hq1 k
  rw [pw1] at t
  exact t

theorem sq_le (a : R) (ha0 : O.le K.zero a) (ha1 : O.le a K.one) : O.le (pwR K a 2) a := by
  have t := O.mul_le_mul_right _ _ a ha1 ha0
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ t (TripleE.le_of_eq' K O _ _ ?_))
  · show K.mul (K.mul K.one a) a = K.mul a a; lib_ring
  · lib_ring

/-- the heat list has a_n = q^{n(n+2)} nonincreasing -/
theorem heat_anoninc (h1 : O.le K.zero K.one) (q : R) (hq0 : O.le K.zero q) (hq1 : O.le q K.one) (L : Nat) :
    FlowStep.ANoninc K O (HeatDec.heat K q 0 L) := by
  intro k
  have nn := fun j => SU2CharExp.cN_nn K O h1 j
  cases Nat.lt_or_ge (k + 1) L with
  | inl h =>
    rw [HeatGood.coY K q L (k + 1) h, HeatGood.coY K q L k (Nat.lt_of_succ_lt h)]
    show O.le (K.mul (K.mul (cN K (k + 1 + 1)) (pwR K q ((k + 1) * (k + 1 + 2)))) (cN K (k + 1)))
      (K.mul (K.mul (cN K (k + 1)) (pwR K q (k * (k + 2)))) (cN K (k + 2)))
    rw [show (k + 1) * (k + 1 + 2) = k * (k + 2) + (k + k + 3) by lib_nat, GKS.pwR_add]
    have b := pw_le1 K O h1 q hq0 hq1 (k + k + 3)
    have c0 : O.le K.zero (K.mul (K.mul (cN K (k + 1)) (cN K (k + 2))) (pwR K q (k * (k + 2)))) :=
      O.mul_nonneg _ _ (O.mul_nonneg _ _ (nn _) (nn _)) (AreaLower.pw_nn K O q hq0 h1 _)
    have t := O.mul_le_mul_right _ _ _ b c0
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ t (TripleE.le_of_eq' K O _ _ ?_))
    · show _ = K.mul (pwR K q (k + k + 3)) (K.mul (K.mul (cN K (k + 1)) (cN K (k + 1 + 1))) (pwR K q (k * (k + 2))))
      lib_ring
    · show K.mul K.one (K.mul (K.mul (cN K (k + 1)) (cN K (k + 1 + 1))) (pwR K q (k * (k + 2))))
        = K.mul (K.mul (cN K (k + 1)) (pwR K q (k * (k + 2)))) (cN K (k + 1 + 1))
      lib_ring
  | inr h =>
    rw [HeatGood.co_zero K q L (k + 1) h, show K.mul K.zero (cN K (k + 1)) = K.zero by lib_ring]
    exact O.mul_nonneg _ _ (HeatGood.co_nn K O h1 q hq0 L k) (nn _)

/-- **the MK flow from the heat-kernel weight stays in the class at every step** -/
theorem flow_heat (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (P : R) (hP0 : O.le K.zero P)
    (hPne : P ≠ K.zero) (h2P : O.le (K.mul (cN K 2) P) K.one) (M k : Nat) :
    FlowIter.Cls K O (pwR K (pwR K P 2) 2)
      (FlowIter.itX K O F (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) M k) (FlowIter.itM M k) := by
  have nn := fun j => SU2CharExp.cN_nn K O h1 j
  have hP1 : O.le P K.one := by
    have a := O.mul_le_mul_right _ _ P (SU2CharExp.cN_mono K O h1 1 2 (by decide)) hP0
    rw [show K.mul (cN K 1) P = P by unfold cN; lib_ring] at a
    exact O.le_trans _ _ _ a h2P
  have P20 := AreaLower.pw_nn K O P hP0 h1 2
  have hP2 : O.le (pwR K P 2) P := sq_le K O P hP0 hP1
  have q0 : O.le K.zero (pwR K (pwR K P 2) 2) := AreaLower.pw_nn K O _ P20 h1 2
  have hq2 : O.le (pwR K (pwR K P 2) 2) (pwR K P 2) := sq_le K O _ P20 (O.le_trans _ _ _ hP2 hP1)
  have hqP : O.le (pwR K (pwR K P 2) 2) P := O.le_trans _ _ _ hq2 hP2
  have hq1 : O.le (pwR K (pwR K P 2) 2) K.one := O.le_trans _ _ _ hqP hP1
  have qne : pwR K (pwR K P 2) 2 ≠ K.zero :=
    HeatGood.pw_ne K O h1 _ P20 (HeatGood.pw_ne K O h1 P hP0 hPne h10 2) h10 2
  have hqq : O.le (pwR K (pwR K (pwR K P 2) 2) 2) P :=
    O.le_trans _ _ _ (sq_le K O _ q0 hq1) hqP
  have h2 : O.le (K.mul (cN K 2) (pwR K (pwR K (pwR K P 2) 2) 2)) K.one := by
    have a := O.mul_le_mul_right _ _ (cN K 2) hqq (nn 2)
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ a (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) h2P))
    · lib_ring
    · lib_ring
  have h0 : FlowIter.Cls K O (pwR K (pwR K P 2) 2) (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) M :=
    ⟨HeatGood.heat_good K O h1 h10 _ q0 hq1 qne h2 (M + 2), heat_anoninc K O h1 _ q0 hq1 (M + 2),
      SecondRing.len_heat K _ (M + 2) 0, fun _ => K.one, fun _ => O.le_refl _, fun _ => h1, fun j => by
        show co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) j = K.mul (co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) j) K.one
        lib_ring⟩
  exact FlowIter.flow_iter K O F h1 h10 P hP0 hPne h2P _ M h0 k

end FlowHeat

#print axioms FlowHeat.pw1
#print axioms FlowHeat.pw_le1
#print axioms FlowHeat.sq_le
#print axioms FlowHeat.heat_anoninc
#print axioms FlowHeat.flow_heat

#print axioms: 14 theorems, no axioms.

90.125 Comparison of two Migdal–Kadanoff flows

Two v-good flows ordered in the likelihood-ratio sense stay ordered at every step, at any coupling.

\[ X_0=M_0\,g_0\ (g_0\downarrow )\ \Longrightarrow \ X_k=M_k\,g_k\ (g_k\downarrow )\quad (k\ge 0) \]

ym/FlowCmp.lean

/-
  ym/FlowCmp — T45, G71/G72: comparison of two Migdal–Kadanoff flows at any coupling.
  If X and M are v-good lists of the same length with a nonincreasing and X = M·g (g >= 0 nonincreasing,
  i.e. X ≼_LR M), then after one MK step (two bond-moving products, then decimation) again
  mk(X) = mk(M)·g' with g' >= 0 nonincreasing (mk_cmp), and so X_k ≼_LR M_k at every step k (cmp_iter).
  Ingredients: HeatL1All.l1_all (L1 for v-good pairs at every level), the ratio form against a v-good reference
  (ratio_formB), DecLR.dec_LR, FlowStep.flow_step (both flows stay v-good). Lean 4 core only, no axioms.
-/
import ym.FlowIter

namespace FlowCmp

open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)
include F

/-- X = B·g with g >= 0 nonincreasing -/
def Below (W B : List R) : Prop :=
  ∃ g : Nat → R, (∀ m, O.le (g (m + 1)) (g m)) ∧ (∀ m, O.le K.zero (g m)) ∧ (∀ k, co K W k = K.mul (co K B k) (g k))

/-- **LR comparison back to the ratio form** against a v-good reference of the same length -/
theorem ratio_formB (W B : List R) (L : Nat) (hW : W.length = L) (hB : B.length = L) (hBg : L1Full.Good K O B)
    (hW0 : ∀ k, O.le K.zero (co K W k))
    (hlr : ∀ n, O.le (K.mul (co K W (n + 1)) (co K B n)) (K.mul (co K W n) (co K B (n + 1)))) :
    Below K O W B := by
  have Bnn := hBg.1
  have Bne : ∀ k, k < L → co K B k ≠ K.zero := fun k hk => hBg.2.1 k (by rw [hB]; exact hk)
  have Wout : ∀ k, L ≤ k → co K W k = K.zero := fun k hk => HeatLR.co_out K W k (by rw [hW]; exact hk)
  refine ⟨fun k => K.mul (co K W k) (F.inv (co K B k)), fun m => ?_, fun m => ?_, fun k => ?_⟩
  · cases Nat.lt_or_ge (m + 1) L with
    | inl hm =>
      exact FlowIter.div_le K O F _ _ _ _ (Bnn m) (Bnn (m + 1)) (Bne m (Nat.lt_of_succ_lt hm)) (Bne (m + 1) hm) (hlr m)
    | inr hm =>
      show O.le (K.mul (co K W (m + 1)) (F.inv (co K B (m + 1)))) (K.mul (co K W m) (F.inv (co K B m)))
      rw [Wout (m + 1) hm, show K.mul K.zero (F.inv (co K B (m + 1))) = K.zero by lib_ring]
      cases Nat.lt_or_ge m L with
      | inl h => exact O.mul_nonneg _ _ (hW0 m) (F.inv_pos _ (Bnn m) (Bne m h))
      | inr h => rw [Wout m h, show K.mul K.zero (F.inv (co K B m)) = K.zero by lib_ring]; exact O.le_refl _
  · show O.le K.zero (K.mul (co K W m) (F.inv (co K B m)))
    cases Nat.lt_or_ge m L with
    | inl h => exact O.mul_nonneg _ _ (hW0 m) (F.inv_pos _ (Bnn m) (Bne m h))
    | inr h => rw [Wout m h, show K.mul K.zero (F.inv (co K B m)) = K.zero by lib_ring]; exact O.le_refl _
  · show co K W k = K.mul (co K B k) (K.mul (co K W k) (F.inv (co K B k)))
    cases Nat.lt_or_ge k L with
    | inl h =>
      have e := F.mul_inv _ (Bne k h)
      have r : K.mul (co K B k) (K.mul (co K W k) (F.inv (co K B k))) = K.mul (co K W k) (K.mul (co K B k) (F.inv (co K B k))) := by
        lib_ring
      rw [r, e]; lib_ring
    | inr h => rw [Wout k h]; lib_ring

/-- **one MK step keeps X ≼_LR M** (any coupling) -/
theorem mk_cmp (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X Mh : List R) (hXg : L1Full.Good K O X)
    (hMg : L1Full.Good K O Mh) (M : Nat) (hX : X.length = M + 2) (hM : Mh.length = M + 2)
    (haM : FlowStep.ANoninc K O Mh) (hb : Below K O X Mh) : Below K O (FlowStep.mk K O F X M) (FlowStep.mk K O F Mh M) := by
  obtain ⟨g, gd, gn, gx⟩ := hb
  -- first product
  have hZX := HeatSecondStep.len_prodL K X M
  have hZM := HeatSecondStep.len_prodL K Mh M
  have ZXg := L1Full.Good_prod K O X hXg M hX F
  have ZMg := L1Full.Good_prod K O Mh hMg M hM F
  have c1 : ∀ n, O.le (K.mul (co K (L1Full.prodL K X M) (n + 1)) (co K (L1Full.prodL K Mh M) n))
      (K.mul (co K (L1Full.prodL K X M) n) (co K (L1Full.prodL K Mh M) (n + 1))) := fun n => by
    rw [L1Full.co_prodL K X M hX, L1Full.co_prodL K X M hX, L1Full.co_prodL K Mh M hM, L1Full.co_prodL K Mh M hM]
    exact HeatL1All.l1_all K O F X Mh hXg hMg M hX hM g gd gn gx n
  obtain ⟨g1, g1d, g1n, g1x⟩ := ratio_formB K O F _ _ (2 * M + 3) hZX hZM ZMg ZXg.1 c1
  -- second product
  have hZX' : (L1Full.prodL K X M).length = 2 * M + 1 + 2 := hZX
  have hZM' : (L1Full.prodL K Mh M).length = 2 * M + 1 + 2 := hZM
  have c2 : ∀ n, O.le (K.mul (co K (FlowStep.Ysq K X M) (n + 1)) (co K (FlowStep.Ysq K Mh M) n))
      (K.mul (co K (FlowStep.Ysq K X M) n) (co K (FlowStep.Ysq K Mh M) (n + 1))) := fun n => by
    have eX : ∀ t, co K (FlowStep.Ysq K X M) t = cgc K (L1Full.prodL K X M) (L1Full.prodL K X M) t :=
      L1Full.co_prodL K (L1Full.prodL K X M) (2 * M + 1) hZX'
    have eM : ∀ t, co K (FlowStep.Ysq K Mh M) t = cgc K (L1Full.prodL K Mh M) (L1Full.prodL K Mh M) t :=
      L1Full.co_prodL K (L1Full.prodL K Mh M) (2 * M + 1) hZM'
    rw [eX, eX, eM, eM]
    exact HeatL1All.l1_all K O F _ _ ZXg ZMg (2 * M + 1) hZX' hZM' g1 g1d g1n g1x n
  have YXg : L1Full.Good K O (FlowStep.Ysq K X M) := L1Full.Good_prod K O _ ZXg (2 * M + 1) hZX' F
  have YMg : L1Full.Good K O (FlowStep.Ysq K Mh M) := L1Full.Good_prod K O _ ZMg (2 * M + 1) hZM' F
  -- decimation
  have c3 := DecLR.dec_LR K O F h1 _ _ 0 YXg.1 YMg.1 (HeatMKClass.w_nn K O F h1 h10) c2
  -- back to the ratio form against mk(M), v-good by FlowStep
  have hl1 := FlowStep.len_mk K O F X M
  have hl2 := FlowStep.len_mk K O F Mh M
  have mkMpos : ∀ k, O.le K.zero (co K (FlowStep.mk K O F X M) k) := fun k => by
    show O.le K.zero (co K (decM K O F 0 (FlowStep.Ysq K X M)) k)
    rw [DimRow.decM_Yd K O F h1 h10]
    exact O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (O.mul_nonneg _ _ (CBLib.sq_nn K O _) (CBLib.sq_nn K O _))
  have mkMg := (FlowStep.flow_step K O F h1 h10 Mh hMg M hM haM).1
  exact ratio_formB K O F _ _ (4 * M + 5) hl1 hl2 mkMg mkMpos c3

/-- both flows in the class and X_k ≼_LR M_k -/
def Pair (X Mh : List R) (M : Nat) : Prop :=
  L1Full.Good K O X ∧ FlowStep.ANoninc K O X ∧ X.length = M + 2 ∧
  L1Full.Good K O Mh ∧ FlowStep.ANoninc K O Mh ∧ Mh.length = M + 2 ∧ Below K O X Mh

/-- **X_k ≼_LR M_k at every step of the two MK flows** -/
theorem cmp_iter (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X Mh : List R) (M : Nat)
    (h0 : Pair K O X Mh M) :
    ∀ k, Pair K O (FlowIter.itX K O F X M k) (FlowIter.itX K O F Mh M k) (FlowIter.itM M k)
  | 0 => h0
  | k + 1 => by
    have ⟨xg, xa, xl, mg, ma, ml, b⟩ := cmp_iter h1 h10 X Mh M h0 k
    have fx := FlowStep.flow_step K O F h1 h10 _ xg _ xl xa
    have fm := FlowStep.flow_step K O F h1 h10 _ mg _ ml ma
    exact ⟨fx.1, fx.2, FlowStep.len_mk K O F _ _, fm.1, fm.2, FlowStep.len_mk K O F _ _,
      mk_cmp K O F h1 h10 _ _ xg mg _ xl ml ma b⟩

end FlowCmp

#print axioms FlowCmp.ratio_formB
#print axioms FlowCmp.mk_cmp
#print axioms FlowCmp.cmp_iter

#print axioms: 3 theorems, no axioms.

90.126 Migdal–Kadanoff flow gap and reflection positivity for \(\beta \le 32\)

For every \(\beta \in [0,32]\) and every truncation \(160+m\), the flowed weight is eventually reflection positive with a volume-uniform gap on \(\mathbb Z^4\).

\[ \forall \beta \le 32\ \exists k_0\ \forall j:\ \text {RP}\wedge \text {Cauchy}\wedge \text {clustering } 2^{-(7+16j)} \text { after } k_0+j \text { steps} \]

ym/Cover32T8.lean

/-
  ym/Cover32T8 — T45, G25: every β ∈ [0, 32] and every truncation Kk' = 160 + m: the 4D Migdal flow (L = 10) from the
  Wilson weight enters Cone(2⁻⁸) and stays there forever, uniformly in m (final check q = 8).
    t ≤ 8        Cover16T8.cover16T8  (m' = 100 + m)
    8 ≤ t ≤ 16   TMCoverPD8.cover_mid (degree-36 Taylor models, base Kk = 160, tail 2⁻⁵⁰⁰, S = 2¹⁸⁰⁰)
  Lean 4 core only, no axioms. (Built on Kamiak: TMCoverPD8 depends on the generated TSE* certificates.)
-/
import ym.Cover16T8
import ym.TMCoverPD8
import ym.TruncCert160

set_option exponentiation.threshold 4096
set_option maxRecDepth 100000

namespace Cover32T8

open YMChain SU2Char MigdalCone LinkIntegral

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **Every β ∈ [0, 32], uniformly in the truncation 160 + m.** -/
theorem cover32T8 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 16)) (m : Nat) :
    ∃ k, ∀ j, ConeBound.Bd K O (pwR K h 8) 0
      (K.one :: MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (160 + m) 11).tail (k + j)) := by
  have em : 60 + (100 + m) = 160 + m := by lib_nat
  cases O.le_total t (cN K 8) with
  | inl hle =>
    have := Cover16T8.cover16T8 K O F h1 h10 h hh h0 t ht0 hle (100 + m)
    rw [em] at this; exact this
  | inr hge =>
    have tl := fun n => TruncCert160.tail_hyp K O F h1 h10 h hh h0 t ht0 ht m n
    exact TMCoverPD8.cover_mid K O F h1 h10 h hh h0 t
      (CoverBridge2.ge_c K O h1 (2 ^ 1800) 8 t hge) (CoverBridge2.le_c K O h1 (2 ^ 1800) 16 t ht)
      (pwR K h 500) (Cover08T8.pad_eq K h hh 1300 500) (160 + m) (fun n => (tl n).1) (fun n => (tl n).2)

end Cover32T8

#print axioms Cover32T8.cover32T8

ym/GapBothBoxFlow32JR.lean

/-
  ym/GapBothBoxFlow32JR — T45, G26: Z⁴, every β ∈ [0,32], every truncation 160 + m: there is k₀ such that for every j
  the flowed weight after k₀ + j Migdal steps is reflection positive AND has the box-order Cauchy estimate and clustering
  with decay 2^{-(7+16j)}. Lean 4 core only, no axioms. (Built on Kamiak after Cover32T8.)
-/
import ym.Cover32T8
import ym.FlowAfterJ
import ym.RPFlow

namespace GapBothBoxFlow32JR

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

theorem both_rp_flow32J (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht : O.le t (cN K 16)) (mm : Nat) :
    ∃ k0, ∀ j,
      FlowAfterJ.BoxBoth K O F H h j (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (160 + mm) 11).tail (k0 + j)) ∧
      ∀ (Fx : CxPair.SU R → R) (xs : List (CxPair.SU R)), (∀ x, memL x xs → CxPair.snrm K x = K.one) →
        ∀ s, O.le K.zero s → O.le K.zero (RPDot.Q K Fx xs (fun x y => K.add s (K.mul s
          (SU2Char.dot K 1 (MigdalCone.mkIt K O F 10 (WilsonCone.wl K O F t (160 + mm) 11).tail (k0 + j))
            (fun n => RPDot.chiK K n x y))))) := by
  obtain ⟨k0, hk0⟩ := Cover32T8.cover32T8 K O F h1 h10 h hh h0 t ht0 ht mm
  exact ⟨k0, fun j => ⟨FlowAfterJ.after_k K O F H DH h1 h10 h hh h0 10 _ k0 (hk0 0) j,
    fun Fx xs hu s hs => RPFlow.rp_of_cone K O F h1 h10 _ _ (hk0 j) Fx xs hu s hs⟩⟩

end GapBothBoxFlow32JR

#print axioms GapBothBoxFlow32JR.both_rp_flow32J

#print axioms: 2 theorems, no axioms.

90.127 The Wilson weight is v-good for \(\beta \le 2\)

For \(0<t\le 1\) the normalised truncated Wilson weight is v-good and its Migdal–Kadanoff flow stays v-good with \(a_n\) nonincreasing at every step.

\[ (c_m+c_{m+2})\,c_{m+2}\le c_{m+1}^2,\quad c_1^2\le (c_0+c_2)\,c_0,\quad (n+1)\,c_{n+1}\le (n+2)\,c_n \]

ym/WilsonGood.lean

/-
  ym/WilsonGood — T45, G70 for β ≤ 2: the normalised truncated SU(2) Wilson weight wl(t, Kk, L) is v-good
  (L1Full.Good: nonnegative, nonzero inside its length, VV at every index, base condition) for 0 < t ≤ 1,
  every truncation Kk and every length L.
  With A_m = c_m/(m+1): the Turán inequality A_m A_{m+2} <= A_{m+1}² (WilsonTuranF.A_turan) and the ratio bound
  (m+2) A_{m+1} <= t A_m (A_ratio) give (c_m + c_{m+2}) c_{m+2} <= c_{m+1}² because (m+1)(m+3) + t² <= (m+2)²,
  and c_1 <= t c_0 <= c_0 gives the base condition. Lean 4 core only, no axioms.
-/
import ym.WilsonANon
import ym.WilsonCone
import ym.RatioStep

namespace WilsonGood

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- (m+2) A_{m+1} ≤ t A_m for t ≥ 0 -/
theorem A_ratio (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (m Kk : Nat) :
    O.le (K.mul (cN K (m + 2)) (WilsonTuranF.A K O F t (m + 1) Kk)) (K.mul t (WilsonTuranF.A K O F t m Kk)) := by
  unfold WilsonTuranF.A
  rw [WilsonTuranF.ev_pL, WilsonTuranF.ev_pL]
  have tt0 : O.le K.zero (K.mul t t) := O.mul_nonneg _ _ ht0 ht0
  have hs := SecondRingC1.sumN_mono K O
    (fun k => K.mul (cN K (m + 2)) (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k)))
    (fun k => K.mul (WilsonTuranF.α K O F m k) (pwR K (K.mul t t) k)) (fun k => by
      have a0 : O.le K.zero (WilsonTuranF.α K O F (m + 1) k) := by
        unfold WilsonTuranF.α
        have hD := WilsonTuranF.fpos K O h1 h10 k (m + 1 + k + 1)
        exact F.inv_pos _ hD.1 hD.2
      have c2 : O.le (cN K (m + 2)) (cN K (m + k + 2)) := cN_mono K O h1 _ _ (by
        rw [show m + k + 2 = m + 2 + k by lib_nat]; exact Nat.le_add_right _ _)
      have w := O.mul_le_mul_right _ _ (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k)) c2
        (O.mul_nonneg _ _ a0 (AreaLower.pw_nn K O _ tt0 h1 k))
      refine O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)
      rw [← WilsonANon.alpha_step K O F h1 h10 m k]
      lib_ring) Kk
  have tm := AreaLower.pw_nn K O t ht0 h1 m
  have w := O.mul_le_mul_right _ _ (K.mul t (pwR K t m)) hs (O.mul_nonneg _ _ ht0 tm)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
  · show K.mul (cN K (m + 2)) (K.mul (K.mul (pwR K t m) t) _) = _
    rw [sumN_mul]
    lib_ring
  · lib_ring

theorem sq_le (h1 : O.le K.zero K.one) (u v : R) (hu : O.le K.zero u) (huv : O.le u v) : O.le (K.mul u u) (K.mul v v) :=
  DecVV.sq_mono K O u v hu huv

/-- **(c_m + c_{m+2}) c_{m+2} ≤ c_{m+1}²** for 0 ≤ t ≤ 1 -/
theorem vv_strong (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one)
    (m Kk : Nat) :
    O.le (K.mul (K.add (cS K O F t m Kk) (cS K O F t (m + 2) Kk)) (cS K O F t (m + 2) Kk))
      (K.mul (cS K O F t (m + 1) Kk) (cS K O F t (m + 1) Kk)) := by
  have nn := fun j => cN_nn K O h1 j
  have Ann : ∀ j, O.le K.zero (WilsonTuranF.A K O F t j Kk) := fun j => by
    have := cN_nn K O h1 (j + 1)
    unfold WilsonTuranF.A
    rw [WilsonTuranF.ev_pL]
    refine O.mul_nonneg _ _ (AreaLower.pw_nn K O t ht0 h1 j) (OneFactor.sum_nn K O _ (fun k => ?_) Kk)
    have hD := WilsonTuranF.fpos K O h1 h10 k (j + k + 1)
    exact O.mul_nonneg _ _ (F.inv_pos _ hD.1 hD.2) (AreaLower.pw_nn K O _ (O.mul_nonneg _ _ ht0 ht0) h1 k)
  rw [WilsonTuranF.cS_eq K O F h1 h10 t m Kk, WilsonTuranF.cS_eq K O F h1 h10 t (m + 2) Kk,
    WilsonTuranF.cS_eq K O F h1 h10 t (m + 1) Kk]
  generalize ea : WilsonTuranF.A K O F t m Kk = a
  generalize eb : WilsonTuranF.A K O F t (m + 1) Kk = b
  generalize ed : WilsonTuranF.A K O F t (m + 2) Kk = d
  have T : O.le (K.mul a d) (K.mul b b) := by
    rw [← ea, ← eb, ← ed]; exact WilsonTuranF.A_turan K O F h1 h10 t ht0 m Kk
  have Rt : O.le (K.mul (cN K (m + 3)) d) (K.mul t b) := by
    rw [← eb, ← ed]; exact A_ratio K O F h1 h10 t ht0 (m + 1) Kk
  have a0 : O.le K.zero a := ea ▸ Ann m
  have b0 : O.le K.zero b := eb ▸ Ann (m + 1)
  have d0 : O.le K.zero d := ed ▸ Ann (m + 2)
  -- (cN3 d)² ≤ (t b)² ≤ b²
  have s1 := sq_le K O h1 _ _ (O.mul_nonneg _ _ (nn _) d0) Rt
  have tb : O.le (K.mul t b) b := by
    have := O.mul_le_mul_right _ _ b ht1 b0; rw [K.one_mul] at this; exact this
  have s2 := sq_le K O h1 _ _ (O.mul_nonneg _ _ ht0 b0) tb
  have S := O.le_trans _ _ _ s1 s2
  -- cN1 cN3 a d ≤ cN1 cN3 b²
  have T2 := O.mul_le_mul_right _ _ (K.mul (cN K (m + 1)) (cN K (m + 3))) T (O.mul_nonneg _ _ (nn _) (nn _))
  have sum := O.add_le_add _ _ _ _ T2 S
  have nat : (m + 1) * (m + 3) + 1 = (m + 2) * (m + 2) := by lib_nat
  have cn : K.add (K.mul (cN K (m + 1)) (cN K (m + 3))) K.one = K.mul (cN K (m + 2)) (cN K (m + 2)) := by
    rw [← cN_mul, ← cN_mul, ← nat, cN_add]; unfold cN; lib_ring
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ sum (TripleE.le_of_eq' K O _ _ ?_))
  · show K.mul (K.add (K.mul (cN K (m + 1)) a) (K.mul (cN K (m + 3)) d)) (K.mul (cN K (m + 3)) d) = _
    lib_ring
  · show _ = K.mul (K.mul (cN K (m + 2)) b) (K.mul (cN K (m + 2)) b)
    rw [show K.mul (K.mul (cN K (m + 2)) b) (K.mul (cN K (m + 2)) b) = K.mul (K.mul (cN K (m + 2)) (cN K (m + 2))) (K.mul b b) by lib_ring,
      ← cn]
    lib_ring

/-- c_1 ≤ t c_0 -/
theorem c1_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (Kk : Nat) :
    O.le (cS K O F t 1 Kk) (K.mul t (cS K O F t 0 Kk)) := by
  rw [WilsonTuranF.cS_eq K O F h1 h10 t 1 Kk, WilsonTuranF.cS_eq K O F h1 h10 t 0 Kk]
  have r := A_ratio K O F h1 h10 t ht0 0 Kk
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ r (TripleE.le_of_eq' K O _ _ ?_))
  · show K.mul (cN K 2) (WilsonTuranF.A K O F t 1 Kk) = _; rfl
  · show _ = K.mul t (K.mul (cN K 1) (WilsonTuranF.A K O F t 0 Kk))
    rw [show cN K 1 = K.one by unfold cN; lib_ring]; lib_ring

/-- c_n ≠ 0 for t ≠ 0 and at least one term -/
theorem c_ne (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero)
    (n Kk : Nat) : cS K O F t n (Kk + 1) ≠ K.zero := by
  rw [WilsonTuranF.cS_sum, sumN_shift K]
  have t0 : term K O F t n 0 ≠ K.zero := by
    rw [WilsonTuranF.term_eq K O F h1 h10 t n 0]
    have hD := WilsonTuranF.fpos K O h1 h10 0 (n + 0 + 1)
    have hp : pwR K t n ≠ K.zero := HeatGood.pw_ne K O h1 t ht0 htne h10 n
    have ha : WilsonTuranF.α K O F n 0 ≠ K.zero := OField.inv_ne K O F _ hD.2 h10
    have h0 : pwR K (K.mul t t) 0 = K.one := rfl
    rw [h0]
    exact FlowStep.mul_ne K O F h10 _ _ (DimRow.cN_pos K O h1 h10 n)
      (FlowStep.mul_ne K O F h10 _ _ hp (FlowStep.mul_ne K O F h10 _ _ ha h10))
  have rest : O.le K.zero (sumN K (fun k => term K O F t n (k + 1)) Kk) :=
    OneFactor.sum_nn K O _ (fun k => term_nn K O F h1 h10 t ht0 n (k + 1)) Kk
  have T0 := term_nn K O F h1 h10 t ht0 n 0
  exact L1Full.ne_of_ge K O _ _ T0 (by
    have := O.add_le_add _ _ _ _ (O.le_refl (term K O F t n 0)) rest
    rw [show K.add (term K O F t n 0) K.zero = term K O F t n 0 by lib_ring] at this
    exact this) t0

theorem len_lmap (f : Nat → R) : ∀ m L, (MigdalCone.lmap f m L).length = L
  | _, 0 => rfl
  | m, L + 1 => by
    show (MigdalCone.lmap f (m + 1) L).length + 1 = L + 1
    rw [len_lmap f (m + 1) L]

theorem cS_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (n Kk : Nat) :
    O.le K.zero (cS K O F t n Kk) := by
  rw [WilsonTuranF.cS_sum]
  exact OneFactor.sum_nn K O _ (fun k => term_nn K O F h1 h10 t ht0 n k) Kk

theorem le_add_self (x y : R) (hy : O.le K.zero y) : O.le x (K.add x y) := by
  have := O.add_le_add _ _ _ _ (O.le_refl x) hy
  rw [show K.add x K.zero = x by lib_ring] at this; exact this

/-- **the normalised truncated Wilson weight is v-good for 0 < t ≤ 1 (β ≤ 2)**, every truncation and length -/
theorem wl_good (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one)
    (htne : t ≠ K.zero) (Kk L : Nat) : L1Full.Good K O (WilsonCone.wl K O F t Kk L) := by
  have c0 := cS_nn K O F h1 h10 t ht0 0 (Kk + 1)
  have c0ne := c_ne K O F h1 h10 t ht0 htne 0 Kk
  have i0 := F.inv_pos _ c0 c0ne
  have co_in : ∀ n, n < L → co K (WilsonCone.wl K O F t Kk L) n
      = K.mul (cS K O F t n (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1))) := fun n hn => by
    unfold WilsonCone.wl
    rw [RatioStep.co_lmap K _ L 0 n hn, Nat.zero_add]
  have co_out : ∀ n, L ≤ n → co K (WilsonCone.wl K O F t Kk L) n = K.zero := fun n hn => by
    unfold WilsonCone.wl
    exact RatioStep.co_lmap_ge K _ L 0 n hn
  have gnn : ∀ n, O.le K.zero (K.mul (cS K O F t n (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))) := fun n =>
    O.mul_nonneg _ _ (cS_nn K O F h1 h10 t ht0 n (Kk + 1)) i0
  have Znn : ∀ n, O.le K.zero (co K (WilsonCone.wl K O F t Kk L) n) := fun n => by
    cases Nat.lt_or_ge n L with
    | inl h => rw [co_in n h]; exact gnn n
    | inr h => rw [co_out n h]; exact O.le_refl _
  refine ⟨Znn, fun j hj => ?_, fun m => ?_, ?_⟩
  · have hjL : j < L := by
      have := hj; unfold WilsonCone.wl at this; rw [len_lmap] at this; exact this
    rw [co_in j hjL]
    exact FlowStep.mul_ne K O F h10 _ _ (c_ne K O F h1 h10 t ht0 htne j Kk) (OField.inv_ne K O F _ c0ne h10)
  · unfold HalfRatio.VV
    cases Nat.lt_or_ge (m + 2) L with
    | inr h =>
      rw [co_out (m + 2) h, show K.mul (K.add (co K (WilsonCone.wl K O F t Kk L) m) K.zero) K.zero = K.zero by lib_ring]
      exact O.mul_nonneg _ _ (TopLC.add_nn K O _ _ (Znn _) (Znn _)) (Znn _)
    | inl h =>
      rw [co_in m (Nat.lt_of_le_of_lt (Nat.le_add_right m 2) h),
        co_in (m + 2) h, co_in (m + 1) (Nat.lt_of_le_of_lt (Nat.le_add_right (m + 1) 1) h)]
      have v := vv_strong K O F h1 h10 t ht0 ht1 m (Kk + 1)
      have w := O.mul_le_mul_right _ _ (K.mul (F.inv (cS K O F t 0 (Kk + 1))) (F.inv (cS K O F t 0 (Kk + 1)))) v
        (O.mul_nonneg _ _ i0 i0)
      have z3 := Znn (m + 3)
      have g1 := gnn (m + 1)
      have ext : O.le (K.mul (K.mul (cS K O F t (m + 1) (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1))))
          (K.mul (cS K O F t (m + 1) (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))))
          (K.mul (K.add (K.mul (cS K O F t (m + 1) (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))) (co K (WilsonCone.wl K O F t Kk L) (m + 3)))
            (K.mul (cS K O F t (m + 1) (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1))))) := by
        exact O.mul_le_mul_right _ _ (K.mul (cS K O F t (m + 1) (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1))))
          (le_add_self K O _ _ z3) g1
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) ext))
      · lib_ring
      · lib_ring
  · cases Nat.lt_or_ge 1 L with
    | inr h =>
      rw [co_out 1 h, show K.mul K.zero K.zero = K.zero by lib_ring]
      exact O.mul_nonneg _ _ (TopLC.add_nn K O _ _ (Znn _) (Znn _)) (Znn _)
    | inl h =>
      rw [co_in 1 h, co_in 0 (Nat.lt_of_succ_lt h)]
      have a := c1_le K O F h1 h10 t ht0 (Kk + 1)
      have b : O.le (K.mul t (cS K O F t 0 (Kk + 1))) (cS K O F t 0 (Kk + 1)) := by
        have := O.mul_le_mul_right _ _ (cS K O F t 0 (Kk + 1)) ht1 c0; rw [K.one_mul] at this; exact this
      have c := O.mul_le_mul_right _ _ (F.inv (cS K O F t 0 (Kk + 1))) (O.le_trans _ _ _ a b) i0
      have s := DecVV.sq_mono K O _ _ (gnn 1) c
      have z2 := Znn 2
      refine O.le_trans _ _ _ s ?_
      exact O.mul_le_mul_right _ _ (K.mul (cS K O F t 0 (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1))))
        (le_add_self K O _ _ z2) (gnn 0)

end WilsonGood

#print axioms WilsonGood.A_ratio
#print axioms WilsonGood.sq_le
#print axioms WilsonGood.vv_strong
#print axioms WilsonGood.c1_le
#print axioms WilsonGood.c_ne
#print axioms WilsonGood.len_lmap
#print axioms WilsonGood.cS_nn
#print axioms WilsonGood.le_add_self
#print axioms WilsonGood.wl_good

ym/WilsonFlow.lean

/-
  ym/WilsonFlow — T45, G70b: for 0 < t ≤ 1 (β ≤ 2) the MK flow started from the normalised truncated SU(2)
  Wilson weight wl(t, Kk, M+2) stays in the flow class (v-good and a_n = X_n/(n+1) nonincreasing) at every step k,
  for every truncation Kk and every starting length.
  Ingredients: WilsonGood.wl_good (start is v-good), WilsonANon.wilson_anoninc (start has a nonincreasing),
  FlowStep.flow_step (one MK step keeps the class), FlowStep.len_mk. Lean 4 core only, no axioms.
-/
import ym.WilsonGood
import ym.FlowIter

namespace WilsonFlow

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- the normalised Wilson start has a nonincreasing (t ≤ 2) -/
theorem wl_anoninc (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht : O.le t (cN K 2)) (htne : t ≠ K.zero) (Kk L : Nat) : FlowStep.ANoninc K O (WilsonCone.wl K O F t Kk L) := by
  have c0 := WilsonGood.cS_nn K O F h1 h10 t ht0 0 (Kk + 1)
  have i0 := F.inv_pos _ c0 (WilsonGood.c_ne K O F h1 h10 t ht0 htne 0 Kk)
  intro k
  cases Nat.lt_or_ge (k + 1) L with
  | inr h =>
    unfold WilsonCone.wl
    rw [RatioStep.co_lmap_ge K _ L 0 (k + 1) h, show K.mul K.zero (cN K (k + 1)) = K.zero by lib_ring]
    refine O.mul_nonneg _ _ ?_ (cN_nn K O h1 (k + 2))
    cases Nat.lt_or_ge k L with
    | inl h' =>
      rw [RatioStep.co_lmap K _ L 0 k h', Nat.zero_add]
      exact O.mul_nonneg _ _ (WilsonGood.cS_nn K O F h1 h10 t ht0 k (Kk + 1)) i0
    | inr h' => rw [RatioStep.co_lmap_ge K _ L 0 k h']; exact O.le_refl _
  | inl h =>
    unfold WilsonCone.wl
    rw [RatioStep.co_lmap K _ L 0 (k + 1) h, RatioStep.co_lmap K _ L 0 k (Nat.lt_of_succ_lt h),
      Nat.zero_add, Nat.zero_add]
    have a := WilsonANon.wilson_anoninc K O F h1 h10 t ht0 ht (Kk + 1) k
    have w := O.mul_le_mul_right _ _ (F.inv (cS K O F t 0 (Kk + 1))) a i0
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
    · lib_ring
    · lib_ring

theorem one_le_two (h1 : O.le K.zero K.one) : O.le K.one (cN K 2) := by
  have := cN_mono K O h1 1 2 (Nat.le_succ 1)
  rw [show cN K 1 = K.one by unfold cN; lib_ring] at this
  exact this

/-- **the Wilson MK flow stays in the class at every step** (0 < t ≤ 1, every truncation Kk, every length M + 2) -/
theorem wilson_flow (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht1 : O.le t K.one) (htne : t ≠ K.zero) (Kk M : Nat) :
    ∀ k, L1Full.Good K O (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M k) ∧
      FlowStep.ANoninc K O (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M k) ∧
      (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M k).length = FlowIter.itM M k + 2
  | 0 => ⟨WilsonGood.wl_good K O F h1 h10 t ht0 ht1 htne Kk (M + 2),
      wl_anoninc K O F h1 h10 t ht0 (O.le_trans _ _ _ ht1 (one_le_two K O h1)) htne Kk (M + 2), by
        show (WilsonCone.wl K O F t Kk (M + 2)).length = M + 2
        unfold WilsonCone.wl; exact WilsonGood.len_lmap _ 0 (M + 2)⟩
  | k + 1 => by
    have ⟨hg, ha, hl⟩ := wilson_flow h1 h10 t ht0 ht1 htne Kk M k
    have fs := FlowStep.flow_step K O F h1 h10 _ hg _ hl ha
    refine ⟨fs.1, fs.2, ?_⟩
    show (FlowStep.mk K O F _ (FlowIter.itM M k)).length = 4 * FlowIter.itM M k + 3 + 2
    exact FlowStep.len_mk K O F _ _

end WilsonFlow

#print axioms WilsonFlow.wl_anoninc
#print axioms WilsonFlow.one_le_two
#print axioms WilsonFlow.wilson_flow

#print axioms: 12 theorems, no axioms.

90.128 Small-\(\beta \) Wilson Migdal–Kadanoff flow: cone entry and gap

For \(\beta =2t\le 1/4\) the full-length Wilson flow enters the strong-coupling cone after 4 steps and its effective weight clusters exponentially.

\[ t\le h^3\ \Longrightarrow \ X_{j+4}\in \mathrm {RC}(h^{88}),\ \ X_{j+4}/X_{j+4}(0)\in \mathrm {Cone}(h^8)\quad (h=\tfrac 12) \]

ym/WilsonRC.lean

/-
  ym/WilsonRC — T45, G74 (small-β leg): the normalised truncated SU(2) Wilson weight has the ratio class
  RC t (x_{n+1} ≤ t x_n), and for 0 < t ≤ 1 with (4t)⁴ ≤ t (e.g. t ≤ 1/8) the whole Wilson MK flow
  (FlowStep.mk iterates) keeps v-goodness, a nonincreasing and RC t at every step, every truncation and length.
  Ingredients: WilsonGood.A_ratio ((n+2)A_{n+1} ≤ tA_n), WilsonFlow.wilson_flow, FlowRatio.flow_ratio.
  Lean 4 core only, no axioms.
-/
import ym.WilsonFlow
import ym.FlowRatio

namespace WilsonRC

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- c_{n+1} ≤ t c_n for the truncated Wilson coefficients (t ≥ 0) -/
theorem cn_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (n Kk : Nat) :
    O.le (cS K O F t (n + 1) Kk) (K.mul t (cS K O F t n Kk)) := by
  rw [WilsonTuranF.cS_eq K O F h1 h10 t (n + 1) Kk, WilsonTuranF.cS_eq K O F h1 h10 t n Kk]
  have An : O.le K.zero (WilsonTuranF.A K O F t n Kk) := by
    unfold WilsonTuranF.A
    rw [WilsonTuranF.ev_pL]
    refine O.mul_nonneg _ _ (AreaLower.pw_nn K O t ht0 h1 n) (OneFactor.sum_nn K O _ (fun k => ?_) Kk)
    have hD := WilsonTuranF.fpos K O h1 h10 k (n + k + 1)
    exact O.mul_nonneg _ _ (F.inv_pos _ hD.1 hD.2) (AreaLower.pw_nn K O _ (O.mul_nonneg _ _ ht0 ht0) h1 k)
  have r := WilsonGood.A_ratio K O F h1 h10 t ht0 n Kk
  have one : O.le K.one (cN K (n + 1)) := by
    have := cN_mono K O h1 1 (n + 1) (Nat.succ_le_succ (Nat.zero_le n))
    rw [show cN K 1 = K.one by unfold cN; lib_ring] at this
    exact this
  have w := O.mul_le_mul_right _ _ (K.mul t (WilsonTuranF.A K O F t n Kk)) one (O.mul_nonneg _ _ ht0 An)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ r (O.le_trans _ _ _
    (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))))
  · show K.mul (cN K (n + 2)) (WilsonTuranF.A K O F t (n + 1) Kk) = _; rfl
  · lib_ring
  · lib_ring

/-- the normalised Wilson start has the ratio class RC t -/
theorem wl_RC (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht1 : O.le t K.one) (htne : t ≠ K.zero) (Kk L : Nat) : RatioStep.RC K O t (WilsonCone.wl K O F t Kk L) := by
  have g := WilsonGood.wl_good K O F h1 h10 t ht0 ht1 htne Kk L
  have c0 := WilsonGood.cS_nn K O F h1 h10 t ht0 0 (Kk + 1)
  have i0 := F.inv_pos _ c0 (WilsonGood.c_ne K O F h1 h10 t ht0 htne 0 Kk)
  refine ⟨g.1, fun k => ?_⟩
  cases Nat.lt_or_ge (k + 1) L with
  | inr h =>
    have e : co K (WilsonCone.wl K O F t Kk L) (k + 1) = K.zero := by
      unfold WilsonCone.wl; exact RatioStep.co_lmap_ge K _ L 0 (k + 1) h
    rw [e]
    exact O.mul_nonneg _ _ ht0 (g.1 k)
  | inl h =>
    unfold WilsonCone.wl
    rw [RatioStep.co_lmap K _ L 0 (k + 1) h, RatioStep.co_lmap K _ L 0 k (Nat.lt_of_succ_lt h),
      Nat.zero_add, Nat.zero_add]
    have a := cn_le K O F h1 h10 t ht0 k (Kk + 1)
    have w := O.mul_le_mul_right _ _ (F.inv (cS K O F t 0 (Kk + 1))) a i0
    refine O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)
    lib_ring

/-- **the Wilson MK flow keeps v-goodness, a nonincreasing and RC t at every step** for 0 < t ≤ 1, (4t)⁴ ≤ t -/
theorem wilson_rc (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht1 : O.le t K.one) (htne : t ≠ K.zero) (h4 : O.le (pwR K (K.add (K.add t t) (K.add t t)) 4) t) (Kk M : Nat) :
    ∀ k, L1Full.Good K O (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M k) ∧
      FlowStep.ANoninc K O (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M k) ∧
      (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M k).length = FlowIter.itM M k + 2 ∧
      RatioStep.RC K O t (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M k) := by
  have s := WilsonFlow.wilson_flow K O F h1 h10 t ht0 ht1 htne Kk M 0
  exact FlowRatio.flow_ratio K O F h1 h10 _ M s.1 s.2.1 s.2.2 (fun _ => t) (fun _ => ht0) (fun _ => h4)
    (wl_RC K O F h1 h10 t ht0 ht1 htne Kk (M + 2))

end WilsonRC

#print axioms WilsonRC.cn_le
#print axioms WilsonRC.wl_RC
#print axioms WilsonRC.wilson_rc

ym/WilsonCone4.lean

/-
  ym/WilsonCone4 — T45, G74 (small-β leg): for 0 < t ≤ 1 with t ≤ h³ (h + h = 1, so t ≤ 1/8, β ≤ 1/4) the
  unnormalised Wilson MK flow X_k (FlowStep.mk iterates from wl(t, Kk, M+2)) is, from step 4 on and at every
  later step, in the ratio class RC h⁸⁸, and its normalisation X_k / X_k(0) lies in the strong-coupling cone
  ConeBound.Bd h⁸ — for every truncation Kk and every starting length; v-goodness and a nonincreasing hold at
  every step as well. Ingredients: WilsonRC.wl_RC, WilsonFlow.wilson_flow, FlowRatio.flow_ratio with the
  RatioStep.sigG schedule, RatioStep.RC_bd. Lean 4 core only, no axioms.
-/
import ym.WilsonRC

namespace WilsonCone4

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- RC is invariant under nonnegative scaling -/
theorem RC_scale (σ c : R) (hc : O.le K.zero c) (l : List R) (h : RatioStep.RC K O σ l) :
    RatioStep.RC K O σ (Projective.scaleL K c l) := by
  refine ⟨fun k => ?_, fun k => ?_⟩
  · rw [FlowRatio.co_scale K c l k]; exact O.mul_nonneg _ _ hc (h.1 k)
  · rw [FlowRatio.co_scale K c l (k + 1), FlowRatio.co_scale K c l k]
    have w := O.mul_le_mul_right _ _ c (h.2 k) hc
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
    · lib_ring
    · lib_ring

/-- a list in RC σ with σ ≤ r⁴, normalised by its head, lies in the cone Bd r -/
theorem norm_bd (h1 : O.le K.zero K.one) (σ r : R) (hσ : O.le K.zero σ) (hr : O.le K.zero r)
    (hsr : O.le σ (pwR K r 4)) (x0 : R) (Y : List R) (hx0 : x0 ≠ K.zero) (h : RatioStep.RC K O σ (x0 :: Y)) :
    ConeBound.Bd K O r 0 (Projective.scaleL K (F.inv x0) (x0 :: Y)) := by
  have hx : O.le K.zero x0 := h.1 0
  have hs := RC_scale K O σ (F.inv x0) (F.inv_pos _ hx hx0) _ h
  have e1 : K.mul (F.inv x0) x0 = K.one := by rw [K.mul_comm]; exact F.mul_inv x0 hx0
  show ConeBound.Bd K O r 0 (K.mul (F.inv x0) x0 :: List.map (K.mul (F.inv x0)) Y)
  change RatioStep.RC K O σ (K.mul (F.inv x0) x0 :: List.map (K.mul (F.inv x0)) Y) at hs
  rw [e1] at hs ⊢
  exact RatioStep.RC_bd K O h1 σ r hσ hr hsr _ hs

/-- **the Wilson MK flow enters the strong-coupling cone after 4 steps and stays** (t ≤ h³, t ≤ 1, t ≠ 0) -/
theorem wilson_cone4 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one) (htne : t ≠ K.zero)
    (hs : O.le t (pwR K h 3)) (Kk M : Nat) (k : Nat) :
    L1Full.Good K O (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (k + 4)) ∧
      RatioStep.RC K O (pwR K h 88) (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (k + 4)) ∧
      ConeBound.Bd K O (pwR K h 8) 0 (Projective.scaleL K
        (F.inv (co K (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (k + 4)) 0))
        (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (k + 4))) := by
  have hle : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [show K.add h K.zero = h by lib_ring, hh] at this; exact this
  have pn := AreaLower.pw_nn K O h h0 h1
  have sn : ∀ i, O.le K.zero (RatioStep.sigG K t h i) := fun i => by
    match i with
    | 0 => exact ht0
    | 1 => exact pn 4
    | 2 => exact pn 8
    | 3 => exact pn 24
    | _ + 4 => exact pn 88
  have st : ∀ i, O.le (pwR K (K.add (K.add (RatioStep.sigG K t h i) (RatioStep.sigG K t h i))
      (K.add (RatioStep.sigG K t h i) (RatioStep.sigG K t h i))) 4) (RatioStep.sigG K t h (i + 1)) := by
    intro i
    match i with
    | 0 => exact RatioStep.four_pow K O h1 h hh h0 _ (sn 0) 1 hs
    | 1 => exact RatioStep.four_pow K O h1 h hh h0 _ (sn 1) 2 (O.le_refl _)
    | 2 => exact RatioStep.four_pow K O h1 h hh h0 _ (sn 2) 6 (O.le_refl _)
    | 3 => exact RatioStep.four_pow K O h1 h hh h0 _ (sn 3) 22 (O.le_refl _)
    | i + 4 => exact O.le_trans _ _ _ (RatioStep.four_pow K O h1 h hh h0 _ (sn (i + 4)) 86 (O.le_refl _)) (by show O.le (pwR K h (4 * 86)) (pwR K h 88); rw [show 4 * 86 = 88 + 256 from rfl]; exact SmallQ.pw_anti K O h1 h h0 hle 88 256)
  have s0 := WilsonFlow.wilson_flow K O F h1 h10 t ht0 ht1 htne Kk M 0
  have fr := FlowRatio.flow_ratio K O F h1 h10 (WilsonCone.wl K O F t Kk (M + 2)) M s0.1 s0.2.1 s0.2.2
    (RatioStep.sigG K t h) sn st (WilsonRC.wl_RC K O F h1 h10 t ht0 ht1 htne Kk (M + 2)) (k + 4)
  have g : L1Full.Good K O (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (k + 4)) := fr.1
  have hl : (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (k + 4)).length = FlowIter.itM M (k + 4) + 2 :=
    fr.2.2.1
  have rc : RatioStep.RC K O (pwR K h 88) (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (k + 4)) := fr.2.2.2
  refine ⟨g, rc, ?_⟩
  have hb : O.le (pwR K h 88) (pwR K (pwR K h 8) 4) := by
    rw [Migdal.pw_pw]; exact SmallQ.pw_anti K O h1 h h0 hle 32 56
  clear fr s0
  revert g hl rc
  generalize FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (k + 4) = X
  intro g hl rc
  cases X with
  | nil => exact Nat.noConfusion hl
  | cons x0 Y =>
    have hx0 : x0 ≠ K.zero := g.2.1 0 (by rw [hl]; exact Nat.zero_lt_succ _)
    exact norm_bd K O F h1 _ _ (pn 88) (pn 8) hb x0 Y hx0 rc

end WilsonCone4

#print axioms WilsonCone4.RC_scale
#print axioms WilsonCone4.norm_bd
#print axioms WilsonCone4.wilson_cone4

ym/WilsonGapFlow.lean

/-
  ym/WilsonGapFlow — T45, G74b (small-β leg): for the SU(2) Wilson weight at 0 < t ≤ h³ (β = 2t ≤ 1/4), every
  truncation Kk and starting length, the effective MK weight after j + 4 steps (FlowStep.mk iterates, normalised
  by its head) lies in Cone(2⁻⁸), so the lattice theory with that plaquette weight has exponential clustering in
  every finite volume (GapCone8.gap_of_cone8). Lean 4 core only, no axioms.
-/
import ym.WilsonCone4
import ym.GapCone8

namespace WilsonGapFlow

open YMChain LinkIntegral GKS LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist OField Volume
  WilsonCauchy

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- the effective weight after j + 4 steps, normalised by its head: its tail -/
def effTail (t : R) (Kk M j : Nat) : List R :=
  (Projective.scaleL K (F.inv (HeatPair.co K (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (j + 4)) 0))
    (FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (j + 4))).tail

/-- **the normalised effective weight 1 :: effTail lies in Cone(h⁸)** -/
theorem eff_cone (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one) (htne : t ≠ K.zero)
    (hs : O.le t (pwR K h 3)) (Kk M j : Nat) :
    ConeBound.Bd K O (pwR K h 8) 0 (K.one :: effTail K O F t Kk M j) := by
  have c4 := WilsonCone4.wilson_cone4 K O F h1 h10 h hh h0 t ht0 ht1 htne hs Kk M j
  have hl := (WilsonFlow.wilson_flow K O F h1 h10 t ht0 ht1 htne Kk M (j + 4)).2.2
  have ⟨g, _, bd⟩ := c4
  unfold effTail
  revert g hl bd
  generalize FlowIter.itX K O F (WilsonCone.wl K O F t Kk (M + 2)) M (j + 4) = X
  intro hl g bd
  cases X with
  | nil => exact Nat.noConfusion hl
  | cons x0 Y =>
    have hx0 : x0 ≠ K.zero := g.2.1 0 (by rw [hl]; exact Nat.zero_lt_succ _)
    have e1 : K.mul (F.inv x0) x0 = K.one := by rw [K.mul_comm]; exact F.mul_inv x0 hx0
    change ConeBound.Bd K O (pwR K h 8) 0 (K.mul (F.inv x0) x0 :: List.map (K.mul (F.inv x0)) Y) at bd
    rw [e1] at bd
    exact bd

/-- **small-β MK approximation has a gap**: for β = 2t ≤ 2h³, the plaquette weight s + s·Σ cf_n χ_n built from
  the effective MK weight after j + 4 steps clusters exponentially in every finite volume (d with 8d ≤ 2^{k+ad}) -/
theorem wilson_gap_flow (d k ad : Nat) (hka : k + ad + 7 ≤ 29) (hd8 : 4 * (2 * d) ≤ 2 ^ (k + ad))
    (hd16 : 2 * (4 * (2 * d) + 1) ≤ 2 ^ (k + ad + 2))
    (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (ht1 : O.le t K.one) (htne : t ≠ K.zero) (hs : O.le t (pwR K h 3)) (Kk M j : Nat)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid d p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (M' : List Bool) (hM : M'.length = Pn.length) :
    let cf := effTail K O F t Kk M j
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 cf (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h (k + ad + 7)))
    let κ := K.add K.one (pwR K h (k + ad + 4))
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w M' (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w M' (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w M' Up) (ZxH K O H w M' Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h (k + ad + 1))) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w M' (fun _ => K.one)) (ZxH K O H w M' (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w M' (fun _ => K.one)) (ZxH K O H w M' (fun _ => K.one)))) :=
  GapCone8.gap_of_cone8 K O F H d k ad hka hd8 hd16 DH h1 h10 h hh h0 LL hLL hL Pn hnd hv p0 q0 hpq
    (effTail K O F t Kk M j) (eff_cone K O F h1 h10 h hh h0 t ht0 ht1 htne hs Kk M j)
    tr Up Uq K1 Rr htd hUp hUq hta hUpa hUqa htr hK1 hUpb hUqb hdist M' hM

end WilsonGapFlow

#print axioms WilsonGapFlow.eff_cone
#print axioms WilsonGapFlow.wilson_gap_flow

#print axioms: 8 theorems, no axioms.

90.129 Coefficientwise VV for the Wilson series

Where the truncation window is complete, every \(y=t^2\) coefficient of the Wilson VV inequality is nonnegative (Vandermonde to central binomials).

\[ (m+1)(m+3)[A_mA_{m+2}]_{T+1}+(m+3)^2[A_{m+2}^2]_T\le (m+2)^2[A_{m+1}^2]_{T+1}+(m+2)(m+4)[A_{m+1}A_{m+3}]_T \]

ym/WilsonVV.lean

/-
  ym/WilsonVV — T45, G76a: the y-coefficients (y = t²) of the VV inequality for the SU(2) Wilson series are
  nonnegative wherever the truncation window is complete.
  With A_m = t^m Σ_k α(m,k) y^k, α(m,k) = 1/(k!(m+k+1)!), and c_m = (m+1)A_m, VV
      (c_m + c_{m+2}) c_{m+2} ≤ (c_{m+1} + c_{m+3}) c_{m+1}
  reads, coefficient of y^{T+1} (after dividing by t^{2m+2}):
      (m+1)(m+3)[A_mA_{m+2}]_{T+1} + (m+3)²[A_{m+2}²]_T ≤ (m+2)²[A_{m+1}²]_{T+1} + (m+2)(m+4)[A_{m+1}A_{m+3}]_T.
  By Vandermonde all four products are central binomials over B = 2T + 2m + 6: g = C(B, c−1), f = C(B, c)
  (c = T + m + 3), with g(c+1) = f c, and the difference is (2m+5)(m+2)(m+3)·f/(c+1) > 0.
  Lean 4 core only, no axioms.
-/
import ym.WilsonTuranF

namespace WilsonVV

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp LibBinom TuranCoef TuranWin WilsonTuran

/-- complete window: the G kernel anti-diagonal sum is a single binomial -/
theorem antiG_full (K ν T : Nat) (hT : T < K) : antiN (gG K ν) T = C (T + (T + 2 * ν + 2)) (T + ν) := by
  rw [antiN_aw]
  have hjk_lt : ∀ j k, j + k = T → j < K ∧ k < K := fun j k hjk =>
    ⟨Nat.lt_of_le_of_lt (LibNatLe.le_of_eq_add hjk.symm) hT,
     Nat.lt_of_le_of_lt (LibNatLe.le_of_eq_add (show T = k + j by rw [← hjk]; lib_nat)) hT⟩
  rw [aw_ws (gG K ν) (Gw T ν) T (T + 1) 0 0 (fun j k hjk _ _ => by
        rw [gG_in K ν j k (hjk_lt j k hjk).1 (hjk_lt j k hjk).2, hjk]; rfl) (by lib_nat)]
  exact vand (T + 2 * ν + 2) T ν

/-- complete window: the F kernel anti-diagonal sum is a single binomial -/
theorem antiF_full (K ν T : Nat) (hT : T < K) : antiN (gF K ν) T = C (T + (T + 2 * ν + 2)) (T + (ν + 1)) := by
  rw [antiN_aw]
  have hjk_lt : ∀ j k, j + k = T → j < K ∧ k < K := fun j k hjk =>
    ⟨Nat.lt_of_le_of_lt (LibNatLe.le_of_eq_add hjk.symm) hT,
     Nat.lt_of_le_of_lt (LibNatLe.le_of_eq_add (show T = k + j by rw [← hjk]; lib_nat)) hT⟩
  rw [aw_ws (gF K ν) (Fw T ν) T (T + 1) 0 0 (fun j k hjk _ _ => by
        rw [gF_in K ν j k (hjk_lt j k hjk).1 (hjk_lt j k hjk).2, hjk]; rfl) (by lib_nat)]
  exact vand (T + 2 * ν + 2) T (ν + 1)

/-- the Nat inequality behind the VV coefficient, given g(c+1) = f c -/
theorem nat_key (m T g f : Nat) (hu : g * (T + m + 4) = f * (T + m + 3)) :
    (m + 1) * (m + 3) * (T + 2 * m + 6) * g + (m + 3) * (m + 3) * (T + 1) * f
      ≤ (m + 2) * (m + 2) * (T + 2 * m + 6) * f + (m + 2) * (m + 4) * (T + 1) * g := by
  apply mul_le_cancel (T + m + 3)
  have eL : ((m + 1) * (m + 3) * (T + 2 * m + 6) * g + (m + 3) * (m + 3) * (T + 1) * f) * (T + m + 3 + 1)
      = f * ((m + 1) * (m + 3) * (T + 2 * m + 6) * (T + m + 3) + (m + 3) * (m + 3) * (T + 1) * (T + m + 4)) := by
    rw [show ((m + 1) * (m + 3) * (T + 2 * m + 6) * g + (m + 3) * (m + 3) * (T + 1) * f) * (T + m + 3 + 1)
        = (m + 1) * (m + 3) * (T + 2 * m + 6) * (g * (T + m + 4)) + (m + 3) * (m + 3) * (T + 1) * (T + m + 4) * f
        by lib_nat, hu]
    lib_nat
  have eR : ((m + 2) * (m + 2) * (T + 2 * m + 6) * f + (m + 2) * (m + 4) * (T + 1) * g) * (T + m + 3 + 1)
      = f * ((m + 1) * (m + 3) * (T + 2 * m + 6) * (T + m + 3) + (m + 3) * (m + 3) * (T + 1) * (T + m + 4))
        + f * ((2 * m + 5) * (m + 2) * (m + 3)) := by
    rw [show ((m + 2) * (m + 2) * (T + 2 * m + 6) * f + (m + 2) * (m + 4) * (T + 1) * g) * (T + m + 3 + 1)
        = (m + 2) * (m + 2) * (T + 2 * m + 6) * (T + m + 4) * f + (m + 2) * (m + 4) * (T + 1) * (g * (T + m + 4))
        by lib_nat, hu]
    lib_nat
  rw [eL, eR]
  exact Nat.le_add_right _ _

/-- g(c+1) = f c for the central pair over B = 2T + 2m + 6 -/
theorem hu_pair (m T : Nat) :
    C ((T + m + 2) + (T + m + 4)) (T + m + 2) * (T + m + 4)
      = C ((T + m + 2) + (T + m + 4)) (T + m + 3) * (T + m + 3) := (TuranWin.up (T + m + 2) (T + m + 4)).symm

/-- **the VV y-coefficient inequality in Nat form** (complete windows, T + 1 < Kk) -/
theorem vv_nat (m Kk T : Nat) (hT : T + 1 < Kk) :
    (m + 1) * (m + 3) * (T + 2 * m + 6) * antiN (gG Kk (m + 1)) (T + 1)
      + (m + 3) * (m + 3) * (T + 1) * antiN (gF Kk (m + 2)) T
      ≤ (m + 2) * (m + 2) * (T + 2 * m + 6) * antiN (gF Kk (m + 1)) (T + 1)
        + (m + 2) * (m + 4) * (T + 1) * antiN (gG Kk (m + 2)) T := by
  rw [antiG_full Kk (m + 1) (T + 1) hT, antiF_full Kk (m + 2) T (Nat.lt_of_succ_lt hT),
    antiF_full Kk (m + 1) (T + 1) hT, antiG_full Kk (m + 2) T (Nat.lt_of_succ_lt hT)]
  rw [show T + 1 + (T + 1 + 2 * (m + 1) + 2) = (T + m + 2) + (T + m + 4) by lib_nat,
    show T + (T + 2 * (m + 2) + 2) = (T + m + 2) + (T + m + 4) by lib_nat,
    show T + 1 + (m + 1) = T + m + 2 by lib_nat, show T + (m + 2 + 1) = T + m + 3 by lib_nat,
    show T + 1 + (m + 1 + 1) = T + m + 3 by lib_nat, show T + (m + 2) = T + m + 2 by lib_nat]
  exact nat_key m T _ _ (hu_pair m T)

end WilsonVV

#print axioms WilsonVV.antiG_full
#print axioms WilsonVV.antiF_full
#print axioms WilsonVV.nat_key
#print axioms WilsonVV.hu_pair
#print axioms WilsonVV.vv_nat

ym/WilsonVVR.lean

/-
  ym/WilsonVVR — T45, G76a (field form): for complete truncation windows (T + 1 < Kk) the y^{T+1}-coefficient of
  the Wilson VV inequality is nonnegative:
      (m+1)(m+3)[A_mA_{m+2}]_{T+1} + (m+3)²[A_{m+2}²]_T ≤ (m+2)²[A_{m+1}²]_{T+1} + (m+2)(m+4)[A_{m+1}A_{m+3}]_T,
  obtained from WilsonVV.vv_nat by multiplying with (T+1)!·(T+2m+6)! (WilsonTuranF.coG / coF).
  Lean 4 core only, no axioms.
-/
import ym.WilsonVV

namespace WilsonVVR

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **the VV coefficient inequality for the truncated Wilson series, complete windows** -/
theorem vv_co (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (m Kk T : Nat) (hT : T + 1 < Kk) :
    O.le (K.add (K.mul (cN K ((m + 1) * (m + 3)))
          (co K (mulL K (WilsonTuranF.pL K O F m Kk) (WilsonTuranF.pL K O F (m + 2) Kk)) (T + 1)))
        (K.mul (cN K ((m + 3) * (m + 3)))
          (co K (mulL K (WilsonTuranF.pL K O F (m + 2) Kk) (WilsonTuranF.pL K O F (m + 2) Kk)) T)))
      (K.add (K.mul (cN K ((m + 2) * (m + 2)))
          (co K (mulL K (WilsonTuranF.pL K O F (m + 1) Kk) (WilsonTuranF.pL K O F (m + 1) Kk)) (T + 1)))
        (K.mul (cN K ((m + 2) * (m + 4)))
          (co K (mulL K (WilsonTuranF.pL K O F (m + 1) Kk) (WilsonTuranF.pL K O F (m + 3) Kk)) T))) := by
  generalize ea : co K (mulL K (WilsonTuranF.pL K O F m Kk) (WilsonTuranF.pL K O F (m + 2) Kk)) (T + 1) = a
  generalize eb : co K (mulL K (WilsonTuranF.pL K O F (m + 2) Kk) (WilsonTuranF.pL K O F (m + 2) Kk)) T = b
  generalize ec : co K (mulL K (WilsonTuranF.pL K O F (m + 1) Kk) (WilsonTuranF.pL K O F (m + 1) Kk)) (T + 1) = c
  generalize ed : co K (mulL K (WilsonTuranF.pL K O F (m + 1) Kk) (WilsonTuranF.pL K O F (m + 3) Kk)) T = d
  have eA : K.mul (cN K (fact (T + 1) * fact (T + 2 * m + 5))) a = cN K (WilsonTuran.antiN (WilsonTuran.gG Kk (m + 1)) (T + 1)) := by
    have := WilsonTuranF.coG K O F h1 h10 m Kk (T + 1)
    rw [show T + 1 + 2 * (m + 1) + 2 = T + 2 * m + 5 by lib_nat, ea] at this; exact this
  have eC : K.mul (cN K (fact (T + 1) * fact (T + 2 * m + 5))) c = cN K (WilsonTuran.antiN (WilsonTuran.gF Kk (m + 1)) (T + 1)) := by
    have := WilsonTuranF.coF K O F h1 h10 m Kk (T + 1)
    rw [show T + 1 + 2 * (m + 1) + 2 = T + 2 * m + 5 by lib_nat, ec] at this; exact this
  have eB : K.mul (cN K (fact T * fact (T + 2 * m + 6))) b = cN K (WilsonTuran.antiN (WilsonTuran.gF Kk (m + 2)) T) := by
    have := WilsonTuranF.coF K O F h1 h10 (m + 1) Kk T
    rw [show T + 2 * (m + 1 + 1) + 2 = T + 2 * m + 6 by lib_nat] at this
    rw [← eb]; exact this
  have eD : K.mul (cN K (fact T * fact (T + 2 * m + 6))) d = cN K (WilsonTuran.antiN (WilsonTuran.gG Kk (m + 2)) T) := by
    have := WilsonTuranF.coG K O F h1 h10 (m + 1) Kk T
    rw [show T + 2 * (m + 1 + 1) + 2 = T + 2 * m + 6 by lib_nat] at this
    rw [← ed]; exact this
  have hD1 : cN K (fact (T + 1) * fact (T + 2 * m + 6))
      = K.mul (cN K (fact (T + 1) * fact (T + 2 * m + 5))) (cN K (T + 2 * m + 6)) := by
    rw [← cN_mul K]
    show cN K (fact (T + 1) * (fact (T + 2 * m + 5) * (T + 2 * m + 5 + 1))) = _
    rw [show fact (T + 1) * (fact (T + 2 * m + 5) * (T + 2 * m + 5 + 1))
      = fact (T + 1) * fact (T + 2 * m + 5) * (T + 2 * m + 6) by lib_nat]
  have hD2 : cN K (fact (T + 1) * fact (T + 2 * m + 6))
      = K.mul (cN K (fact T * fact (T + 2 * m + 6))) (cN K (T + 1)) := by
    rw [← cN_mul K]
    show cN K (fact T * (T + 1) * fact (T + 2 * m + 6)) = _
    rw [show fact T * (T + 1) * fact (T + 2 * m + 6) = fact T * fact (T + 2 * m + 6) * (T + 1) by lib_nat]
  have hp := WilsonTuranF.fpos K O h1 h10 (T + 1) (T + 2 * m + 6)
  refine GridQ.cancel K O F _ _ _ hp.1 hp.2 ?_
  have nat := WilsonVV.vv_nat m Kk T hT
  have le := cN_mono K O h1 _ _ nat
  have L : K.mul (cN K (fact (T + 1) * fact (T + 2 * m + 6)))
      (K.add (K.mul (cN K ((m + 1) * (m + 3))) a) (K.mul (cN K ((m + 3) * (m + 3))) b))
      = cN K ((m + 1) * (m + 3) * (T + 2 * m + 6) * WilsonTuran.antiN (WilsonTuran.gG Kk (m + 1)) (T + 1)
          + (m + 3) * (m + 3) * (T + 1) * WilsonTuran.antiN (WilsonTuran.gF Kk (m + 2)) T) := by
    rw [cN_add K, cN_mul K ((m + 1) * (m + 3) * (T + 2 * m + 6)), cN_mul K ((m + 3) * (m + 3) * (T + 1)),
      cN_mul K ((m + 1) * (m + 3)) (T + 2 * m + 6), cN_mul K ((m + 3) * (m + 3)) (T + 1), ← eA, ← eB]
    have e : K.mul (cN K (fact (T + 1) * fact (T + 2 * m + 6)))
        (K.add (K.mul (cN K ((m + 1) * (m + 3))) a) (K.mul (cN K ((m + 3) * (m + 3))) b))
        = K.add (K.mul (K.mul (cN K (fact (T + 1) * fact (T + 2 * m + 5))) (cN K (T + 2 * m + 6)))
            (K.mul (cN K ((m + 1) * (m + 3))) a))
          (K.mul (K.mul (cN K (fact T * fact (T + 2 * m + 6))) (cN K (T + 1))) (K.mul (cN K ((m + 3) * (m + 3))) b)) := by
      rw [← hD1, ← hD2]; lib_ring
    rw [e]; lib_ring
  have Rr : K.mul (cN K (fact (T + 1) * fact (T + 2 * m + 6)))
      (K.add (K.mul (cN K ((m + 2) * (m + 2))) c) (K.mul (cN K ((m + 2) * (m + 4))) d))
      = cN K ((m + 2) * (m + 2) * (T + 2 * m + 6) * WilsonTuran.antiN (WilsonTuran.gF Kk (m + 1)) (T + 1)
          + (m + 2) * (m + 4) * (T + 1) * WilsonTuran.antiN (WilsonTuran.gG Kk (m + 2)) T) := by
    rw [cN_add K, cN_mul K ((m + 2) * (m + 2) * (T + 2 * m + 6)), cN_mul K ((m + 2) * (m + 4) * (T + 1)),
      cN_mul K ((m + 2) * (m + 2)) (T + 2 * m + 6), cN_mul K ((m + 2) * (m + 4)) (T + 1), ← eC, ← eD]
    have e : K.mul (cN K (fact (T + 1) * fact (T + 2 * m + 6)))
        (K.add (K.mul (cN K ((m + 2) * (m + 2))) c) (K.mul (cN K ((m + 2) * (m + 4))) d))
        = K.add (K.mul (K.mul (cN K (fact (T + 1) * fact (T + 2 * m + 5))) (cN K (T + 2 * m + 6)))
            (K.mul (cN K ((m + 2) * (m + 2))) c))
          (K.mul (K.mul (cN K (fact T * fact (T + 2 * m + 6))) (cN K (T + 1))) (K.mul (cN K ((m + 2) * (m + 4))) d)) := by
      rw [← hD1, ← hD2]; lib_ring
    rw [e]; lib_ring
  rw [L, Rr]; exact le

end WilsonVVR

#print axioms WilsonVVR.vv_co

#print axioms: 6 theorems, no axioms.

90.130 Sharp Wilson coefficient ratio

Every truncated Wilson coefficient satisfies the sharp one-step ratio bound.

\[ (m+1)\,c_{m+1}(t)\le t\,c_m(t)\qquad (t\ge 0,\ \text {every truncation}) \]

ym/WilsonHeatStart.lean

/-
  ym/WilsonHeatStart — T45, G78d′ piece: the termwise Bessel ratio bound for the truncated SU(2) Wilson weight,
  valid for every t ≥ 0 and every truncation Kk:  (m+2)·A_{m+1} ≤ t·A_m, i.e. (m+1)·c_{m+1} ≤ t·c_m
  (c_m = (m+1)·A_m). Termwise: α(m+1,k)·(m+k+2) = α(m,k) and m+2 ≤ m+k+2. Used for the large characters of the
  comparison of the Wilson start with a heat list. Lean 4 core only, no axioms.
-/
import ym.WilsonANon

namespace WilsonHeatStart

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **termwise ratio bound**: (m+2)·A_{m+1} ≤ t·A_m for every t ≥ 0 -/
theorem A_ratio (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (m Kk : Nat) :
    O.le (K.mul (cN K (m + 2)) (WilsonTuranF.A K O F t (m + 1) Kk)) (K.mul t (WilsonTuranF.A K O F t m Kk)) := by
  unfold WilsonTuranF.A
  rw [WilsonTuranF.ev_pL, WilsonTuranF.ev_pL]
  have tt0 : O.le K.zero (K.mul t t) := O.mul_nonneg _ _ ht0 ht0
  have hs := SecondRingC1.sumN_mono K O
    (fun k => K.mul (cN K (m + 2)) (K.mul t (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k))))
    (fun k => K.mul t (K.mul (WilsonTuranF.α K O F m k) (pwR K (K.mul t t) k))) (fun k => by
      have a0 : O.le K.zero (WilsonTuranF.α K O F (m + 1) k) := by
        unfold WilsonTuranF.α
        have hD := WilsonTuranF.fpos K O h1 h10 k (m + 1 + k + 1)
        exact F.inv_pos _ hD.1 hD.2
      have c : O.le (cN K (m + 2)) (cN K (m + k + 2)) :=
        cN_mono K O h1 (m + 2) (m + k + 2) (Nat.succ_le_succ (Nat.succ_le_succ (Nat.le_add_right m k)))
      have w := O.mul_le_mul_right _ _ (K.mul t (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k))) c
        (O.mul_nonneg _ _ ht0 (O.mul_nonneg _ _ a0 (AreaLower.pw_nn K O _ tt0 h1 k)))
      refine O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)
      rw [← WilsonANon.alpha_step K O F h1 h10 m k]
      lib_ring) Kk
  have tm := AreaLower.pw_nn K O t ht0 h1 m
  have w := O.mul_le_mul_right _ _ (pwR K t m) hs tm
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
  · show K.mul (cN K (m + 2)) (K.mul (K.mul (pwR K t m) t) _) = _
    rw [sumN_mul, sumN_mul]
    lib_ring
  · rw [sumN_mul]
    lib_ring

/-- **the Wilson coefficients**: (m+1)·c_{m+1} ≤ t·c_m for every t ≥ 0 and every truncation -/
theorem cS_ratio (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (m Kk : Nat) :
    O.le (K.mul (cN K (m + 1)) (cS K O F t (m + 1) Kk)) (K.mul t (cS K O F t m Kk)) := by
  rw [WilsonTuranF.cS_eq K O F h1 h10 t (m + 1) Kk, WilsonTuranF.cS_eq K O F h1 h10 t m Kk]
  have a := A_ratio K O F h1 h10 t ht0 m Kk
  have w := O.mul_le_mul_right _ _ (cN K (m + 1)) a (cN_nn K O h1 (m + 1))
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
  · rw [show m + 1 + 1 = m + 2 from rfl]
    lib_ring
  · lib_ring

/-- **LR step from the termwise ratio**: (n+1)c′ ≤ t·c and t·K₀ ≤ (n+1)·K₁ (c, K₀ ≥ 0) give c′·K₀ ≤ c·K₁ -/
theorem lr_from_ratio (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (n : Nat) (t c c' K0 K1 : R)
    (hc : O.le K.zero c) (hK0 : O.le K.zero K0)
    (hr : O.le (K.mul (cN K (n + 1)) c') (K.mul t c)) (hq : O.le (K.mul t K0) (K.mul (cN K (n + 1)) K1)) :
    O.le (K.mul c' K0) (K.mul c K1) := by
  have p := pos_of_ge_one K O h1 h10 (n + 1) (Nat.le_add_left 1 n)
  refine O.pos_cancel _ _ (cN K (n + 1)) p.1 p.2 ?_
  have a := O.mul_le_mul_right _ _ K0 hr hK0
  have b := O.mul_le_mul_right _ _ c hq hc
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ a (O.le_trans _ _ _
    (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ b (TripleE.le_of_eq' K O _ _ ?_))))
  · lib_ring
  · lib_ring
  · lib_ring

end WilsonHeatStart

#print axioms WilsonHeatStart.A_ratio
#print axioms WilsonHeatStart.cS_ratio
#print axioms WilsonHeatStart.lr_from_ratio

#print axioms: 3 theorems, no axioms.

90.131 Perron gap for the flowed weight

The transfer matrix built from the flowed weight has all entries at least \(1/2\), so the Perron gap is strict.

\[ T_{ij}\ge \tfrac 12\ \Rightarrow \ \lambda _1>|\lambda _2| \]

ym/WeightPerron.lean

/-
  ym/WeightPerron — T45, G79 (a): the flowed SU(2) weight makes every transfer-matrix entry strictly positive, so the
  strict Perron gap of YMChain.perron_gap applies. If T i j = 1 + Σ_{n≥1} a_n^{(j′)} χ^{ij}_n with the flowed
  activities a^{(j′)} = mkIt L a j′ (1 :: a ∈ Cone(2⁻⁸)) and character values |χ^{ij}_n| ≤ n + 1, then
  T i j ≥ 1 − 2^{-(16j′+29)} ≥ 1/2 > 0 for all i, j (WeightPosJ.weight_pos_j), and every eigenvalue μ ≠ λ of T
  on a finite index set has |μ| < λ. Lean 4 core only, no axioms.
-/
import ym.WeightPosJ
import ym.TripleE

namespace WeightPerron

open YMChain LinkIntegral SU2Char ConeBound MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- 0 ≤ h ≤ 1 ⇒ h^{m+1} ≤ h -/
theorem pw_le_self (h1 : O.le K.zero K.one) (h : R) (h0 : O.le K.zero h) (hl : O.le h K.one) :
    ∀ m, O.le (pwR K h (m + 1)) h
  | 0 => TripleE.le_of_eq' K O _ _ (by show K.mul K.one h = h; lib_ring)
  | m + 1 => by
    show O.le (K.mul (pwR K h (m + 1)) h) h
    have a := O.mul_le_mul_right _ _ h (pw_le_self h1 h h0 hl m) h0
    have b := O.mul_le_mul_right _ _ h hl h0
    exact O.le_trans _ _ _ a (O.le_trans _ _ _ b (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **every entry of the flowed transfer matrix is ≥ h (= 1/2), hence Pos** -/
theorem entry_pos (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (a : List R) (ha : Bd K O (pwR K h 8) 0 (K.one :: a)) (L j : Nat) (χ : Nat → R)
    (hχ : ∀ n, O.le (K.neg (cN K (n + 1))) (χ n) ∧ O.le (χ n) (cN K (n + 1))) :
    Pos K O (K.add K.one (SU2Char.dot K 1 (mkIt K O F L a j) χ)) := by
  have hl : O.le h K.one := by
    have := O.add_le_add _ _ _ _ (O.le_refl h) h0; rw [DetChain.add_zero', hh] at this; exact this
  have w := WeightPosJ.weight_pos_j K O F h1 h10 h hh h0 a ha L j χ hχ
  have p := pw_le_self K O h1 h h0 hl (16 * j + 28)
  -- 1 − h^{16j+29} ≥ 1 − h = h
  have s : O.le h (K.add K.one (K.neg (pwR K h (16 * j + 29)))) := by
    have q := O.add_le_add _ _ _ _ (O.le_refl (K.add K.one (K.neg (pwR K h (16 * j + 29))))) p
    have e1 : K.add (K.add K.one (K.neg (pwR K h (16 * j + 29)))) (pwR K h (16 * j + 28 + 1))
        = K.one := by rw [show 16 * j + 28 + 1 = 16 * j + 29 by lib_nat]; lib_ring
    rw [e1] at q
    have r := O.add_le_add _ _ _ _ q (O.le_refl (K.neg h))
    have eh : h = K.add K.one (K.neg h) := by rw [← hh]; lib_ring
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ eh) (O.le_trans _ _ _ r (TripleE.le_of_eq' K O _ _ (by lib_ring)))
  have ge := O.le_trans _ _ _ s w.1
  have hne : h ≠ K.zero := fun e => h10 (by rw [← hh, e]; lib_ring)
  refine ⟨w.2.2, fun e => hne (O.le_antisymm _ _ (by rw [e] at ge; exact ge) h0)⟩

/-- **strict Perron gap for the flowed transfer matrix** (finite index set a₀ :: rest) -/
theorem flowed_gap (A : AbsLaws K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one)
    (h0 : O.le K.zero h) (a : List R) (ha : Bd K O (pwR K h 8) 0 (K.one :: a)) (L j : Nat)
    (χ : Nat → Nat → Nat → R) (hχ : ∀ i k n, O.le (K.neg (cN K (n + 1))) (χ i k n) ∧ O.le (χ i k n) (cN K (n + 1)))
    (u w : Nat → R) (lam mu : R) (a0 : Nat) (rest : List Nat) (hu : ∀ i, Pos K O (u i))
    (heu : ∀ i, sumK K (fun k => K.mul (K.add K.one (SU2Char.dot K 1 (mkIt K O F L a j) (χ i k))) (u k)) (a0 :: rest) = K.mul lam (u i))
    (hew : ∀ i, sumK K (fun k => K.mul (K.add K.one (SU2Char.dot K 1 (mkIt K O F L a j) (χ i k))) (w k)) (a0 :: rest) = K.mul mu (w i))
    (i0 : Nat) (hi0 : memL i0 (a0 :: rest)) (hw0 : w i0 ≠ K.zero) (hne : mu ≠ lam) :
    O.le (A.abs mu) lam ∧ A.abs mu ≠ lam :=
  perron_gap K O A (fun i k => K.add K.one (SU2Char.dot K 1 (mkIt K O F L a j) (χ i k))) u w lam mu a0 rest
    (fun i k => entry_pos K O F h1 h10 h hh h0 a ha L j (χ i k) (hχ i k)) hu heu hew i0 hi0 hw0 hne

end WeightPerron

#print axioms WeightPerron.pw_le_self
#print axioms WeightPerron.entry_pos
#print axioms WeightPerron.flowed_gap

#print axioms: 3 theorems, no axioms.

90.132 One MK rung from a kernel check

One MK step of a heat list cast from naturals lies below the heat list at ’\( on the first +1\) characters.

\[ \mathrm {mk}(H)_{n+1}\,K'_n\le \mathrm {mk}(H)_n\,K'_{n+1}\qquad (n<N,\ \texttt {rungsOK}=\texttt {true}) \]

ym/HeatLadder.lean

/-
  ym/HeatLadder — T45, G78b (core): one rung of the heat ladder is a natural-number check.
  For a natural-number list H (the heat list at a rational q, common denominator dropped — LR order ignores a
  global factor) of length M + 2, let Z = (H·H) and Q_n = (Z·Z)_n computed in ℕ (CertSound.cgcN). Then the
  full-length MK step X ↦ FlowStep.mk X M applied to the cast list H has coefficients
      co (mk H) n = cN(Q_n)⁴ / (n+1)³,
  and the LR cross inequality against a natural-number reference list K2 at level n follows from
      Q_{n+1}⁴ (n+1)³ K2_n ≤ Q_n⁴ (n+2)³ K2_{n+1}   (in ℕ, checked by the kernel).
  Lean 4 core only, no axioms.
-/
import ym.FlowStep
import ym.HoggarFalse
import ym.RatioStep
import ym.GridQ
import ym.SecondRing
import lib.NatSub
import ym.HeatStepMK

namespace HeatLadder

open YMChain LinkIntegral SU2Char MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- natural-number listF -/
def listN (f : Nat → Nat) : Nat → Nat → List Nat
  | _, 0 => []
  | m, N + 1 => f m :: listN f (m + 1) N

theorem len_listN (f : Nat → Nat) : ∀ m N, (listN f m N).length = N
  | _, 0 => rfl
  | m, N + 1 => by show (listN f (m + 1) N).length + 1 = N + 1; rw [len_listN f (m + 1) N]

theorem listF_cast (f : Nat → Nat) : ∀ m N, ListRow.listF (fun t => cN K (f t)) m N = (listN f m N).map (cN K)
  | _, 0 => rfl
  | m, N + 1 => by
    show cN K (f m) :: ListRow.listF (fun t => cN K (f t)) (m + 1) N = cN K (f m) :: (listN f (m + 1) N).map (cN K)
    rw [listF_cast f (m + 1) N]

theorem listF_ext (f g : Nat → R) (h : ∀ t, f t = g t) : ∀ m N, ListRow.listF f m N = ListRow.listF g m N
  | _, 0 => rfl
  | m, N + 1 => by
    show f m :: ListRow.listF f (m + 1) N = g m :: ListRow.listF g (m + 1) N
    rw [h m, listF_ext f g h (m + 1) N]

/-- the first product of a cast list is a cast list -/
def ZN (H : List Nat) (M : Nat) : List Nat := listN (fun t => CertSound.cgcN H H t) 0 (2 * M + 3)

theorem prodL_cast (H : List Nat) (M : Nat) : L1Full.prodL K (H.map (cN K)) M = (ZN H M).map (cN K) := by
  unfold L1Full.prodL ZN
  rw [listF_ext _ (fun t => cN K (CertSound.cgcN H H t)) (fun t => HoggarFalse.cgc_cast K H H t)]
  exact listF_cast K _ 0 _

/-- coefficients of the second product -/
theorem co_Ysq (H : List Nat) (M : Nat) (hH : H.length = M + 2) (n : Nat) :
    co K (FlowStep.Ysq K (H.map (cN K)) M) n = cN K (CertSound.cgcN (ZN H M) (ZN H M) n) := by
  have hZl : (L1Full.prodL K (H.map (cN K)) M).length = 2 * M + 1 + 2 := HeatSecondStep.len_prodL K _ M
  unfold FlowStep.Ysq
  rw [L1Full.co_prodL K _ (2 * M + 1) hZl, prodL_cast K H M]
  exact HoggarFalse.cgc_cast K _ _ n

/-- coefficients of one MK step of a cast list -/
theorem co_mk (H : List Nat) (M : Nat) (hH : H.length = M + 2) (n : Nat) :
    co K (FlowStep.mk K O F (H.map (cN K)) M) n
      = K.mul (pwR K (cN K (CertSound.cgcN (ZN H M) (ZN H M) n)) 4) (pwR K (F.inv (cN K (0 + n + 1))) 3) := by
  unfold FlowStep.mk
  rw [RatioStep.co_decM K O F _ 0 n, co_Ysq K H M hH n]

/-- coefficient of a natural-number list -/
def coN : List Nat → Nat → Nat
  | [], _ => 0
  | x :: _, 0 => x
  | _ :: l, k + 1 => coN l k

theorem pw3 (x : R) : pwR K x 3 = K.mul x (K.mul x x) := by
  show K.mul (K.mul (K.mul K.one x) x) x = _; lib_ring

theorem pw4 (x : R) : pwR K x 4 = K.mul (K.mul x x) (K.mul x x) := by
  show K.mul (K.mul (K.mul (K.mul K.one x) x) x) x = _; lib_ring

theorem pw_inv (x : R) (hx : K.mul x (F.inv x) = K.one) : ∀ k, K.mul (pwR K x k) (pwR K (F.inv x) k) = K.one
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | k + 1 => by
    show K.mul (K.mul (pwR K x k) x) (K.mul (pwR K (F.inv x) k) (F.inv x)) = K.one
    rw [show K.mul (K.mul (pwR K x k) x) (K.mul (pwR K (F.inv x) k) (F.inv x))
      = K.mul (K.mul (pwR K x k) (pwR K (F.inv x) k)) (K.mul x (F.inv x)) by lib_ring, pw_inv x hx k, hx]
    lib_ring

/-- **one rung**: the natural-number cross inequality gives the LR cross inequality at level n -/
theorem rung (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (H K2 : List Nat) (M : Nat) (hH : H.length = M + 2)
    (n : Nat)
    (hN : CertSound.cgcN (ZN H M) (ZN H M) (n + 1) ^ 4 * (n + 1) ^ 3 * coN K2 n
        ≤ CertSound.cgcN (ZN H M) (ZN H M) n ^ 4 * (n + 2) ^ 3 * coN K2 (n + 1)) :
    O.le (K.mul (co K (FlowStep.mk K O F (H.map (cN K)) M) (n + 1)) (cN K (coN K2 n)))
      (K.mul (co K (FlowStep.mk K O F (H.map (cN K)) M) n) (cN K (coN K2 (n + 1)))) := by
  rw [co_mk K O F H M hH (n + 1), co_mk K O F H M hH n,
    show 0 + (n + 1) + 1 = n + 2 by lib_nat, show 0 + n + 1 = n + 1 by lib_nat]
  have p1 := SU2CharExp.pos_of_ge_one K O h1 h10 (n + 1) (Nat.le_add_left 1 n)
  have p2 := SU2CharExp.pos_of_ge_one K O h1 h10 (n + 2) (Nat.le_add_left 1 (n + 1))
  have i1 := F.mul_inv _ p1.2
  have i2 := F.mul_inv _ p2.2
  have hP : 1 ≤ (n + 1) ^ 3 * (n + 2) ^ 3 := by lib_nat_le
  have pP := SU2CharExp.pos_of_ge_one K O h1 h10 _ hP
  refine GridQ.cancel K O F (cN K ((n + 1) ^ 3 * (n + 2) ^ 3)) _ _ pP.1 pP.2 ?_
  have le := SU2CharExp.cN_mono K O h1 _ _ hN
  rw [SU2CharExp.cN_mul K, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K,
    CertSound.cN_pow K, CertSound.cN_pow K, CertSound.cN_pow K, CertSound.cN_pow K] at le
  rw [SU2CharExp.cN_mul K, CertSound.cN_pow K, CertSound.cN_pow K]
  generalize cN K (CertSound.cgcN (ZN H M) (ZN H M) (n + 1)) = A at le ⊢
  generalize cN K (CertSound.cgcN (ZN H M) (ZN H M) n) = B at le ⊢
  generalize cN K (coN K2 n) = k0 at le ⊢
  generalize cN K (coN K2 (n + 1)) = k1 at le ⊢
  generalize cN K (n + 1) = c1 at le i1 ⊢
  generalize cN K (n + 2) = c2 at le i2 ⊢
  have q1 := pw_inv K O F c1 i1 3
  have q2 := pw_inv K O F c2 i2 3
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ le (TripleE.le_of_eq' K O _ _ ?_))
  · rw [show K.mul (K.mul (pwR K c1 3) (pwR K c2 3)) (K.mul (K.mul (pwR K A 4) (pwR K (F.inv c2) 3)) k0)
        = K.mul (K.mul (K.mul (pwR K A 4) (pwR K c1 3)) k0) (K.mul (pwR K c2 3) (pwR K (F.inv c2) 3)) by lib_ring, q2]
    lib_ring
  · rw [show K.mul (K.mul (pwR K c1 3) (pwR K c2 3)) (K.mul (K.mul (pwR K B 4) (pwR K (F.inv c1) 3)) k1)
        = K.mul (K.mul (K.mul (pwR K B 4) (pwR K c2 3)) k1) (K.mul (pwR K c1 3) (pwR K (F.inv c1) 3)) by lib_ring, q1]
    lib_ring

/-- the natural-number check of one rung on all levels n < N -/
def rungsOK (H K2 : List Nat) (M : Nat) : Nat → Bool
  | 0 => true
  | N + 1 => rungsOK H K2 M N &&
      Nat.ble (CertSound.cgcN (ZN H M) (ZN H M) (N + 1) ^ 4 * (N + 1) ^ 3 * coN K2 N)
        (CertSound.cgcN (ZN H M) (ZN H M) N ^ 4 * (N + 2) ^ 3 * coN K2 (N + 1))

theorem rungsOK_le (H K2 : List Nat) (M : Nat) : ∀ N, rungsOK H K2 M N = true → ∀ n, n < N →
    CertSound.cgcN (ZN H M) (ZN H M) (n + 1) ^ 4 * (n + 1) ^ 3 * coN K2 n
      ≤ CertSound.cgcN (ZN H M) (ZN H M) n ^ 4 * (n + 2) ^ 3 * coN K2 (n + 1)
  | 0, _, n, hn => absurd hn (Nat.not_lt_zero n)
  | N + 1, h, n, hn => by
    have h' : rungsOK H K2 M N = true ∧ Nat.ble (CertSound.cgcN (ZN H M) (ZN H M) (N + 1) ^ 4 * (N + 1) ^ 3 * coN K2 N)
        (CertSound.cgcN (ZN H M) (ZN H M) N ^ 4 * (N + 2) ^ 3 * coN K2 (N + 1)) = true := by
      change (rungsOK H K2 M N && _) = true at h
      cases hr : rungsOK H K2 M N with
      | false => rw [hr] at h; exact absurd h (by intro e; cases e)
      | true =>
        rw [hr] at h
        exact ⟨rfl, h⟩
    cases Nat.lt_or_ge n N with
    | inl hl => exact rungsOK_le H K2 M N h'.1 n hl
    | inr hg =>
      have e : n = N := Nat.le_antisymm (Nat.le_of_lt_succ hn) hg
      rw [e]
      exact Nat.le_of_ble_eq_true h'.2

/-- **all rungs**: the kernel check gives the LR cross inequality at every level n < N -/
theorem rungs (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (H K2 : List Nat) (M N : Nat) (hH : H.length = M + 2)
    (hc : rungsOK H K2 M N = true) (n : Nat) (hn : n < N) :
    O.le (K.mul (co K (FlowStep.mk K O F (H.map (cN K)) M) (n + 1)) (cN K (coN K2 n)))
      (K.mul (co K (FlowStep.mk K O F (H.map (cN K)) M) n) (cN K (coN K2 (n + 1)))) :=
  rung K O F h1 h10 H K2 M hH n (rungsOK_le H K2 M N hc n hn)

/-- the heat list at q = a/b as natural numbers with common denominator b^E -/
def heatN (a b E : Nat) (L : Nat) : List Nat := listN (fun n => (n + 1) * a ^ (n * (n + 2)) * b ^ (E - n * (n + 2))) 0 L

theorem coN_listN (f : Nat → Nat) : ∀ N m j, j < N → coN (listN f m N) j = f (m + j)
  | 0, _, _, h => absurd h (Nat.not_lt_zero _)
  | N + 1, m, 0, _ => rfl
  | N + 1, m, j + 1, h => by
    show coN (listN f (m + 1) N) j = f (m + (j + 1))
    rw [coN_listN f N (m + 1) j (Nat.lt_of_succ_lt_succ h), show m + 1 + j = m + (j + 1) by lib_nat]

/-- **the real heat list is the cast natural-number list up to the factor b^E**: for q·b = a, n < L, n(n+2) ≤ E -/
theorem heat_scale (q : R) (a b E L n : Nat) (hq : K.mul q (cN K b) = cN K a) (hn : n < L) (hE : n * (n + 2) ≤ E) :
    K.mul (cN K (b ^ E)) (co K (HeatDec.heat K q 0 L) n) = cN K (coN (heatN a b E L) n) := by
  unfold heatN
  rw [coN_listN _ L 0 n hn, SecondRing.co_heat K q L 0 n hn, Nat.zero_add]
  have eE : pwR K (cN K b) E = K.mul (pwR K (cN K b) (E - n * (n + 2))) (pwR K (cN K b) (n * (n + 2))) := by
    rw [← GKS.pwR_add, LibNatSub.sub_add hE]
  rw [CertSound.cN_pow K b E, eE, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K, CertSound.cN_pow K a (n * (n + 2)),
    CertSound.cN_pow K b (E - n * (n + 2)), ← hq, HeatDec.pw_mul_pw K]
  lib_ring

theorem one_le_pow (b : Nat) (hb : 1 ≤ b) : ∀ k, 1 ≤ b ^ k
  | 0 => Nat.le_refl 1
  | k + 1 => by
    show 1 ≤ b ^ k * b
    exact Nat.mul_le_mul (one_le_pow b hb k) hb

/-- **one rung, against the real heat list**: if the kernel check holds for H and K2 = heatN a′ b′ E′ L′, then
  mk(H) is below heat q′ (q′b′ = a′) on the first N + 1 characters (cross-ratio form) -/
theorem rung_heat (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (H : List Nat) (M N : Nat) (hH : H.length = M + 2)
    (q' : R) (a' b' E' L' : Nat) (hb : 1 ≤ b') (hq : K.mul q' (cN K b') = cN K a') (hNL : N < L')
    (hE : N * (N + 2) ≤ E') (hc : rungsOK H (heatN a' b' E' L') M N = true) :
    HeatStepMK.BelowL K O (FlowStep.mk K O F (H.map (cN K)) M) (HeatDec.heat K q' 0 L') N := by
  intro n hn
  have r := rungs K O F h1 h10 H (heatN a' b' E' L') M N hH hc n hn
  have hEn : n * (n + 2) ≤ E' := Nat.le_trans (Nat.mul_le_mul (Nat.le_of_lt hn) (Nat.succ_le_succ (Nat.succ_le_succ (Nat.le_of_lt hn)))) hE
  have hEn1 : (n + 1) * (n + 1 + 2) ≤ E' := Nat.le_trans (Nat.mul_le_mul hn (Nat.succ_le_succ (Nat.succ_le_succ hn))) hE
  have s0 := heat_scale K q' a' b' E' L' n hq (Nat.lt_trans hn hNL) hEn
  have s1 := heat_scale K q' a' b' E' L' (n + 1) hq (Nat.lt_of_le_of_lt hn hNL) hEn1
  rw [← s0, ← s1] at r
  have pb := SU2CharExp.pos_of_ge_one K O h1 h10 (b' ^ E') (one_le_pow b' hb E')
  refine O.pos_cancel _ _ (cN K (b' ^ E')) pb.1 pb.2 ?_
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ r (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · lib_ring

/-- **one rung with margin u = un/ud**: un·Q_{n+1}⁴(n+1)³K2_n ≤ ud·Q_n⁴(n+2)³K2_{n+1} in ℕ gives
  u·(mk_{n+1}·K2_n) ≤ mk_n·K2_{n+1} -/
theorem rungU (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (H K2 : List Nat) (M : Nat) (hH : H.length = M + 2)
    (n un ud : Nat) (hud : 1 ≤ ud)
    (hN : un * (CertSound.cgcN (ZN H M) (ZN H M) (n + 1) ^ 4 * (n + 1) ^ 3 * coN K2 n)
        ≤ ud * (CertSound.cgcN (ZN H M) (ZN H M) n ^ 4 * (n + 2) ^ 3 * coN K2 (n + 1))) :
    O.le (K.mul (K.mul (cN K un) (F.inv (cN K ud)))
        (K.mul (co K (FlowStep.mk K O F (H.map (cN K)) M) (n + 1)) (cN K (coN K2 n))))
      (K.mul (co K (FlowStep.mk K O F (H.map (cN K)) M) n) (cN K (coN K2 (n + 1)))) := by
  rw [co_mk K O F H M hH (n + 1), co_mk K O F H M hH n,
    show 0 + (n + 1) + 1 = n + 2 by lib_nat, show 0 + n + 1 = n + 1 by lib_nat]
  have p1 := SU2CharExp.pos_of_ge_one K O h1 h10 (n + 1) (Nat.le_add_left 1 n)
  have p2 := SU2CharExp.pos_of_ge_one K O h1 h10 (n + 2) (Nat.le_add_left 1 (n + 1))
  have pd := SU2CharExp.pos_of_ge_one K O h1 h10 ud hud
  have i1 := F.mul_inv _ p1.2
  have i2 := F.mul_inv _ p2.2
  have id := F.mul_inv _ pd.2
  have hP0 : 1 ≤ (n + 1) ^ 3 * (n + 2) ^ 3 := by lib_nat_le
  have hP : 1 ≤ ud * ((n + 1) ^ 3 * (n + 2) ^ 3) :=
    Nat.le_trans hP0 (Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.mul_le_mul_right ((n + 1) ^ 3 * (n + 2) ^ 3) hud))
  have pP := SU2CharExp.pos_of_ge_one K O h1 h10 _ hP
  refine GridQ.cancel K O F (cN K (ud * ((n + 1) ^ 3 * (n + 2) ^ 3))) _ _ pP.1 pP.2 ?_
  have le := SU2CharExp.cN_mono K O h1 _ _ hN
  rw [SU2CharExp.cN_mul K un, SU2CharExp.cN_mul K ud, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K,
    SU2CharExp.cN_mul K, CertSound.cN_pow K, CertSound.cN_pow K, CertSound.cN_pow K, CertSound.cN_pow K] at le
  rw [SU2CharExp.cN_mul K ud, SU2CharExp.cN_mul K, CertSound.cN_pow K, CertSound.cN_pow K]
  generalize cN K (CertSound.cgcN (ZN H M) (ZN H M) (n + 1)) = A at le ⊢
  generalize cN K (CertSound.cgcN (ZN H M) (ZN H M) n) = B at le ⊢
  generalize cN K (coN K2 n) = k0 at le ⊢
  generalize cN K (coN K2 (n + 1)) = k1 at le ⊢
  generalize cN K (n + 1) = c1 at le i1 ⊢
  generalize cN K (n + 2) = c2 at le i2 ⊢
  generalize cN K ud = d at le id ⊢
  generalize cN K un = e at le ⊢
  have q1 := pw_inv K O F c1 i1 3
  have q2 := pw_inv K O F c2 i2 3
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ le (TripleE.le_of_eq' K O _ _ ?_))
  · rw [show K.mul (K.mul d (K.mul (pwR K c1 3) (pwR K c2 3)))
        (K.mul (K.mul e (F.inv d)) (K.mul (K.mul (pwR K A 4) (pwR K (F.inv c2) 3)) k0))
        = K.mul (K.mul e (K.mul (K.mul (pwR K A 4) (pwR K c1 3)) k0))
          (K.mul (K.mul d (F.inv d)) (K.mul (pwR K c2 3) (pwR K (F.inv c2) 3))) by lib_ring, id, q2]
    lib_ring
  · rw [show K.mul (K.mul d (K.mul (pwR K c1 3) (pwR K c2 3))) (K.mul (K.mul (pwR K B 4) (pwR K (F.inv c1) 3)) k1)
        = K.mul (K.mul d (K.mul (K.mul (pwR K B 4) (pwR K c2 3)) k1)) (K.mul (pwR K c1 3) (pwR K (F.inv c1) 3)) by lib_ring, q1]
    lib_ring

/-- the natural-number check of one rung with margin un/ud on all levels n < N -/
def rungsOKU (H K2 : List Nat) (M un ud : Nat) : Nat → Bool
  | 0 => true
  | N + 1 => rungsOKU H K2 M un ud N &&
      Nat.ble (un * (CertSound.cgcN (ZN H M) (ZN H M) (N + 1) ^ 4 * (N + 1) ^ 3 * coN K2 N))
        (ud * (CertSound.cgcN (ZN H M) (ZN H M) N ^ 4 * (N + 2) ^ 3 * coN K2 (N + 1)))

theorem rungsOKU_le (H K2 : List Nat) (M un ud : Nat) : ∀ N, rungsOKU H K2 M un ud N = true → ∀ n, n < N →
    un * (CertSound.cgcN (ZN H M) (ZN H M) (n + 1) ^ 4 * (n + 1) ^ 3 * coN K2 n)
      ≤ ud * (CertSound.cgcN (ZN H M) (ZN H M) n ^ 4 * (n + 2) ^ 3 * coN K2 (n + 1))
  | 0, _, n, hn => absurd hn (Nat.not_lt_zero n)
  | N + 1, h, n, hn => by
    have h' : rungsOKU H K2 M un ud N = true ∧ Nat.ble (un * (CertSound.cgcN (ZN H M) (ZN H M) (N + 1) ^ 4 * (N + 1) ^ 3 * coN K2 N))
        (ud * (CertSound.cgcN (ZN H M) (ZN H M) N ^ 4 * (N + 2) ^ 3 * coN K2 (N + 1))) = true := by
      change (rungsOKU H K2 M un ud N && _) = true at h
      cases hr : rungsOKU H K2 M un ud N with
      | false => rw [hr] at h; exact absurd h (by intro e; cases e)
      | true =>
        rw [hr] at h
        exact ⟨rfl, h⟩
    cases Nat.lt_or_ge n N with
    | inl hl => exact rungsOKU_le H K2 M un ud N h'.1 n hl
    | inr hg =>
      have e : n = N := Nat.le_antisymm (Nat.le_of_lt_succ hn) hg
      rw [e]
      exact Nat.le_of_ble_eq_true h'.2

end HeatLadder

#print axioms HeatLadder.len_listN
#print axioms HeatLadder.listF_cast
#print axioms HeatLadder.listF_ext
#print axioms HeatLadder.prodL_cast
#print axioms HeatLadder.co_Ysq
#print axioms HeatLadder.co_mk
#print axioms HeatLadder.pw3
#print axioms HeatLadder.pw4
#print axioms HeatLadder.pw_inv
#print axioms HeatLadder.rung
#print axioms HeatLadder.rungsOK_le
#print axioms HeatLadder.rungs
#print axioms HeatLadder.coN_listN
#print axioms HeatLadder.heat_scale
#print axioms HeatLadder.one_le_pow
#print axioms HeatLadder.rung_heat
#print axioms HeatLadder.rungU
#print axioms HeatLadder.rungsOKU_le

#print axioms: 18 theorems, no axioms.

90.133 Wilson start below a heat list

The Wilson start lies below a heat list at one large character and on each 569JNRXghklcell, from natural-number checks.

\[ c_{n+1}(t)\,h_n\le c_n(t)\,h_{n+1}\qquad (n<N,\ t\in [t_0,t_1]) \]

ym/WilsonHeatLR.lean

/-
  ym/WilsonHeatLR — T45, G78d′ piece: at a character n where the natural-number check
  T·K_n ≤ (n+1)·K_{n+1} holds for the heat list K = heatN a b E L (q = a/b), the truncated Wilson weight at any
  0 ≤ t ≤ T satisfies the LR cross inequality c_{n+1}·heat_n ≤ c_n·heat_{n+1} against the real heat list at q.
  Ingredients: WilsonHeatStart.cS_ratio, lr_from_ratio, HeatLadder.heat_scale. Lean 4 core only, no axioms.
-/
import ym.WilsonHeatStart
import ym.HeatLadder
import ym.CertWilson
import ym.WilsonGood

namespace WilsonHeatLR

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **Wilson start below the heat list at one large character** -/
theorem wl_heat_big (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t q : R) (ht0 : O.le K.zero t) (T : Nat)
    (htT : O.le t (cN K T)) (a b E L n Kk : Nat) (hb : 1 ≤ b) (hq : K.mul q (cN K b) = cN K a) (hnL : n + 1 < L)
    (hE : (n + 1) * (n + 1 + 2) ≤ E)
    (hN : T * HeatLadder.coN (HeatLadder.heatN a b E L) n ≤ (n + 1) * HeatLadder.coN (HeatLadder.heatN a b E L) (n + 1)) :
    O.le (K.mul (cS K O F t (n + 1) Kk) (co K (HeatDec.heat K q 0 L) n))
      (K.mul (cS K O F t n Kk) (co K (HeatDec.heat K q 0 L) (n + 1))) := by
  have hE0 : n * (n + 2) ≤ E :=
    Nat.le_trans (Nat.mul_le_mul (Nat.le_succ n) (Nat.succ_le_succ (Nat.succ_le_succ (Nat.le_succ n)))) hE
  have s0 := HeatLadder.heat_scale K q a b E L n hq (Nat.lt_trans (Nat.lt_succ_self n) hnL) hE0
  have s1 := HeatLadder.heat_scale K q a b E L (n + 1) hq hnL hE
  generalize HeatLadder.coN (HeatLadder.heatN a b E L) n = k0 at hN s0
  generalize HeatLadder.coN (HeatLadder.heatN a b E L) (n + 1) = k1 at hN s1
  have hc := WilsonGood.cS_nn K O F h1 h10 t ht0 n Kk
  have k0n := cN_nn K O h1 k0
  have hr := WilsonHeatStart.cS_ratio K O F h1 h10 t ht0 n Kk
  have hq' : O.le (K.mul t (cN K k0)) (K.mul (cN K (n + 1)) (cN K k1)) := by
    have a1 := O.mul_le_mul_right _ _ (cN K k0) htT k0n
    have a2 := cN_mono K O h1 _ _ hN
    rw [cN_mul K, cN_mul K] at a2
    exact O.le_trans _ _ _ a1 a2
  have lr := WilsonHeatStart.lr_from_ratio K O h1 h10 n t _ _ _ _ hc k0n hr hq'
  have pE := pos_of_ge_one K O h1 h10 (b ^ E) (HeatLadder.one_le_pow b hb E)
  refine O.pos_cancel _ _ (cN K (b ^ E)) pE.1 pE.2 ?_
  rw [← s0, ← s1] at lr
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ lr (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · lib_ring

/-- **one t-cell**: the endpoint inequality c_{n+1}(t₊)·K_n ≤ c_n(t₋)·K_{n+1} gives the LR cross inequality at
  every t ∈ [t₋, t₊] (c monotone in t, K ≥ 0) -/
theorem wl_cell (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (tl th t K0 K1 : R) (htl : O.le K.zero tl)
    (h1t : O.le tl t) (h2t : O.le t th) (hK0 : O.le K.zero K0) (hK1 : O.le K.zero K1) (n Kk : Nat)
    (he : O.le (K.mul (cS K O F th (n + 1) Kk) K0) (K.mul (cS K O F tl n Kk) K1)) :
    O.le (K.mul (cS K O F t (n + 1) Kk) K0) (K.mul (cS K O F t n Kk) K1) := by
  have t0 : O.le K.zero t := O.le_trans _ _ _ htl h1t
  have a := O.mul_le_mul_right _ _ K0 (CertWilson.cS_mono K O F h1 h10 t th t0 h2t (n + 1) Kk) hK0
  have b := O.mul_le_mul_right _ _ K1 (CertWilson.cS_mono K O F h1 h10 tl t htl h1t n Kk) hK1
  exact O.le_trans _ _ _ a (O.le_trans _ _ _ he b)

/-- **one t-cell, natural-number form**: t ∈ [a1/b1, a2/b2], heat list Nat values k0, k1 (any common scale):
  Wn(a2,b2,n+1)·k0·Dn(b1,n) ≤ Wn(a1,b1,n)·k1·Dn(b2,n+1) gives c_{n+1}(t)·k0 ≤ c_n(t)·k1 -/
theorem wl_cell_nat (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t tl th : R) (htl : O.le K.zero tl)
    (h1t : O.le tl t) (h2t : O.le t th) (a1 b1 a2 b2 n Kk k0 k1 : Nat) (hb1 : 1 ≤ b1) (hb2 : 1 ≤ b2)
    (e1 : K.mul tl (cN K b1) = cN K a1) (e2 : K.mul th (cN K b2) = cN K a2)
    (hN : CertWilson.Wn a2 b2 (n + 1) Kk * k0 * CertWilson.Dn b1 n Kk ≤ CertWilson.Wn a1 b1 n Kk * k1 * CertWilson.Dn b2 (n + 1) Kk) :
    O.le (K.mul (cS K O F t (n + 1) Kk) (cN K k0)) (K.mul (cS K O F t n Kk) (cN K k1)) := by
  refine wl_cell K O F h1 h10 tl th t _ _ htl h1t h2t (cN_nn K O h1 k0) (cN_nn K O h1 k1) n Kk ?_
  have x1 := CertWilson.cS_exact K O F h1 h10 tl a1 b1 n e1 Kk
  have x2 := CertWilson.cS_exact K O F h1 h10 th a2 b2 (n + 1) e2 Kk
  have d1 : 1 ≤ CertWilson.Dn b1 n Kk := Nat.mul_le_mul (CertWilson.pow_ge b1 _ hb1) (CertWilson.fact_ge _)
  have d2 : 1 ≤ CertWilson.Dn b2 (n + 1) Kk := Nat.mul_le_mul (CertWilson.pow_ge b2 _ hb2) (CertWilson.fact_ge _)
  have p1 := pos_of_ge_one K O h1 h10 _ d1
  have p2 := pos_of_ge_one K O h1 h10 _ d2
  refine O.pos_cancel _ _ _ p1.1 p1.2 ?_
  refine O.pos_cancel _ _ _ p2.1 p2.2 ?_
  have le := cN_mono K O h1 _ _ hN
  rw [cN_mul K, cN_mul K, cN_mul K, cN_mul K, ← x1, ← x2] at le
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ le (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · lib_ring

/-- the natural-number check of one t-cell [a1/b1, a2/b2] on all characters n < N (heat Nat list H) -/
def cellOK (a1 b1 a2 b2 Kk : Nat) (H : List Nat) : Nat → Bool
  | 0 => true
  | N + 1 => cellOK a1 b1 a2 b2 Kk H N &&
      Nat.ble (CertWilson.Wn a2 b2 (N + 1) Kk * HeatLadder.coN H N * CertWilson.Dn b1 N Kk)
        (CertWilson.Wn a1 b1 N Kk * HeatLadder.coN H (N + 1) * CertWilson.Dn b2 (N + 1) Kk)

theorem cellOK_le (a1 b1 a2 b2 Kk : Nat) (H : List Nat) : ∀ N, cellOK a1 b1 a2 b2 Kk H N = true → ∀ n, n < N →
    CertWilson.Wn a2 b2 (n + 1) Kk * HeatLadder.coN H n * CertWilson.Dn b1 n Kk
      ≤ CertWilson.Wn a1 b1 n Kk * HeatLadder.coN H (n + 1) * CertWilson.Dn b2 (n + 1) Kk
  | 0, _, n, hn => absurd hn (Nat.not_lt_zero n)
  | N + 1, h, n, hn => by
    have h' : cellOK a1 b1 a2 b2 Kk H N = true ∧ Nat.ble (CertWilson.Wn a2 b2 (N + 1) Kk * HeatLadder.coN H N * CertWilson.Dn b1 N Kk)
        (CertWilson.Wn a1 b1 N Kk * HeatLadder.coN H (N + 1) * CertWilson.Dn b2 (N + 1) Kk) = true := by
      change (cellOK a1 b1 a2 b2 Kk H N && _) = true at h
      cases hr : cellOK a1 b1 a2 b2 Kk H N with
      | false => rw [hr] at h; exact absurd h (by intro e; cases e)
      | true => rw [hr] at h; exact ⟨rfl, h⟩
    cases Nat.lt_or_ge n N with
    | inl hl => exact cellOK_le a1 b1 a2 b2 Kk H N h'.1 n hl
    | inr hg =>
      have e : n = N := Nat.le_antisymm (Nat.le_of_lt_succ hn) hg
      rw [e]
      exact Nat.le_of_ble_eq_true h'.2

/-- **one ratio of the normalised Wilson start**: if t ≤ T and T ≤ (n+1)·σ (as naturals over a common σ = a/b:
  T·b ≤ (n+1)·a), then wl_{n+1} ≤ σ·wl_n (every truncation, every length) -/
theorem wl_ratio_geo (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t σ : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero)
    (T a b n : Nat) (hb : 1 ≤ b) (htT : O.le t (cN K T)) (hσ : K.mul σ (cN K b) = cN K a) (hN : T * b ≤ (n + 1) * a)
    (Kk L : Nat) :
    O.le (co K (WilsonCone.wl K O F t Kk L) (n + 1)) (K.mul σ (co K (WilsonCone.wl K O F t Kk L) n)) := by
  have c0 := WilsonGood.cS_nn K O F h1 h10 t ht0 0 (Kk + 1)
  have i0 := F.inv_pos _ c0 (WilsonGood.c_ne K O F h1 h10 t ht0 htne 0 Kk)
  -- (n+1)·c_{n+1} ≤ t·c_n ≤ T·c_n ≤ (n+1)σ·c_n, cancel n+1
  have r := WilsonHeatStart.cS_ratio K O F h1 h10 t ht0 n (Kk + 1)
  have cn := WilsonGood.cS_nn K O F h1 h10 t ht0 n (Kk + 1)
  have pb := pos_of_ge_one K O h1 h10 b hb
  have pn := pos_of_ge_one K O h1 h10 (n + 1) (Nat.le_add_left 1 n)
  have key : O.le (cS K O F t (n + 1) (Kk + 1)) (K.mul σ (cS K O F t n (Kk + 1))) := by
    refine O.pos_cancel _ _ _ pn.1 pn.2 ?_
    refine O.pos_cancel _ _ _ pb.1 pb.2 ?_
    have a1 := O.mul_le_mul_right _ _ (cN K b) r pb.1
    have a2 := O.mul_le_mul_right _ _ (K.mul (cS K O F t n (Kk + 1)) (cN K b)) htT (O.mul_nonneg _ _ cn pb.1)
    have a3 := cN_mono K O h1 _ _ hN
    rw [cN_mul K, cN_mul K] at a3
    have a4 := O.mul_le_mul_right _ _ (cS K O F t n (Kk + 1)) a3 cn
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ a1 (O.le_trans _ _ _
      (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ a2 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
        (O.le_trans _ _ _ a4 (TripleE.le_of_eq' K O _ _ ?_))))))
    · lib_ring
    · lib_ring
    · lib_ring
    · rw [← hσ]; lib_ring
  unfold WilsonCone.wl
  cases Nat.lt_or_ge (n + 1) L with
  | inl h =>
    rw [RatioStep.co_lmap K _ L 0 (n + 1) h, RatioStep.co_lmap K _ L 0 n (Nat.lt_of_succ_lt h), Nat.zero_add, Nat.zero_add]
    have w := O.mul_le_mul_right _ _ _ key i0
    exact O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring))
  | inr h =>
    rw [RatioStep.co_lmap_ge K _ L 0 (n + 1) h]
    have nn : O.le K.zero (co K (MigdalCone.lmap (fun n => K.mul (cS K O F t n (Kk + 1)) (F.inv (cS K O F t 0 (Kk + 1)))) 0 L) n) := by
      cases Nat.lt_or_ge n L with
      | inl h' => rw [RatioStep.co_lmap K _ L 0 n h', Nat.zero_add]; exact O.mul_nonneg _ _ cn i0
      | inr h' => rw [RatioStep.co_lmap_ge K _ L 0 n h']; exact O.le_refl _
    have σ0 : O.le K.zero σ := by
      refine O.pos_cancel _ _ _ pb.1 pb.2 ?_
      rw [hσ]
      exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (cN_nn K O h1 a)
    exact O.mul_nonneg _ _ σ0 nn

end WilsonHeatLR

#print axioms WilsonHeatLR.wl_heat_big
#print axioms WilsonHeatLR.wl_cell
#print axioms WilsonHeatLR.wl_cell_nat
#print axioms WilsonHeatLR.cellOK_le
#print axioms WilsonHeatLR.wl_ratio_geo

#print axioms: 5 theorems, no axioms.

90.134 Bond moving with a variable ratio

With an eventually nonincreasing ratio sequence the bond-moving product keeps a halved-index ratio bound.

\[ C_{m+1}\le 2\rho _{\lfloor m/2\rfloor }\,C_m \]

ym/RatioVar.lean

/-
  ym/RatioVar — T45, G78f (b2): the bond-moving ratio bound with a variable, eventually nonincreasing ratio.
  If H ≥ 0, H_{k+1} ≤ ρ_k H_k for all k, and ρ_k ≤ ρ_c for k ≥ c where 2c ≤ m, then C_{m+1} ≤ 2ρ_c C_m for every
  truncation (each pair of C_{m+1} raises from a pair of C_m by its larger index, which is ≥ c whenever the
  Clebsch–Gordan coefficient is nonzero). Variant of RatioClass.shift_sigma. Lean 4 core only, no axioms.
-/
import ym.RatioClass
import ym.SecondRingStep
import ym.RatioStep
import ym.FlowStep
import ym.HeatL1All
import ym.HeatSecondStep

namespace RatioVar

open YMChain LinkIntegral SU2Char QBinom SecondRingC1 SecondRingShift RatioClass HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- one raising step with the variable ratio: for j ≤ k + 1, T(m+1, j, k+1) ≤ ρ_c·T(m, j, k) -/
theorem step_v (h1 : O.le K.zero K.one) (H : Nat → R) (hH : ∀ j, O.le K.zero (H j)) (ρv : Nat → R)
    (hρv : ∀ k, O.le K.zero (ρv k)) (hr : ∀ k, O.le (H (k + 1)) (K.mul (ρv k) (H k))) (c m : Nat) (hc : c + c ≤ m)
    (hmono : ∀ k, c ≤ k → O.le (ρv k) (ρv c)) (j k : Nat) (hjk : j ≤ k + 1) :
    O.le (SecondRingShift.T K H (m + 1) j (k + 1)) (K.mul (ρv c) (SecondRingShift.T K H m j k)) := by
  have Tn := T_nn K O h1 H hH m j k
  cases Nat.lt_or_ge k c with
  | inl hkc =>
    have z : Fusion.cg j (k + 1) (m + 1) = 0 := by
      apply Fusion.cg_top
      have a : j + (k + 1) ≤ (k + 1) + (k + 1) := Nat.add_le_add_right hjk (k + 1)
      have b : (k + 1) + (k + 1) ≤ c + c := Nat.add_le_add hkc hkc
      exact Nat.lt_succ_of_le (Nat.le_trans a (Nat.le_trans b hc))
    unfold SecondRingShift.T at Tn ⊢
    rw [z]
    have e : K.mul (cN K 0) (K.mul (H j) (H (k + 1))) = K.zero := by unfold cN; lib_ring
    rw [e]
    exact O.mul_nonneg _ _ (hρv c) Tn
  | inr hkc =>
    have hc' : Fusion.cg j (k + 1) (m + 1) = Fusion.cg j k m := CgShift.cg_shift j k m hjk
    have r' : O.le (H (k + 1)) (K.mul (ρv c) (H k)) :=
      O.le_trans _ _ _ (hr k) (O.mul_le_mul_right _ _ (H k) (hmono k hkc) (hH k))
    unfold SecondRingShift.T; rw [hc']
    have nc := SU2CharExp.cN_nn K O h1 (Fusion.cg j k m)
    have := O.mul_le_mul_right _ _ (K.mul (cN K (Fusion.cg j k m)) (H j)) r' (O.mul_nonneg _ _ nc (hH j))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_))
    · lib_ring
    · lib_ring

/-- termwise matching with the variable ratio -/
theorem tw_v (h1 : O.le K.zero K.one) (H : Nat → R) (hH : ∀ j, O.le K.zero (H j)) (ρv : Nat → R)
    (hρv : ∀ k, O.le K.zero (ρv k)) (hr : ∀ k, O.le (H (k + 1)) (K.mul (ρv k) (H k))) (c m : Nat) (hc : c + c ≤ m)
    (hmono : ∀ k, c ≤ k → O.le (ρv k) (ρv c)) (j k : Nat) :
    O.le (SecondRingShift.T K H (m + 1) j k) (K.mul (ρv c) (K.add (sh K (fun k' => SecondRingShift.T K H m j k') k)
      (sh K (fun j' => SecondRingShift.T K H m j' k) j))) := by
  have hρ := hρv c
  have hA := sh_nn K O (fun k' => SecondRingShift.T K H m j k') (T_nn K O h1 H hH m j) k
  have hB := sh_nn K O (fun j' => SecondRingShift.T K H m j' k) (fun j' => T_nn K O h1 H hH m j' k) j
  have hR : O.le K.zero (K.mul (ρv c) (K.add (sh K (fun k' => SecondRingShift.T K H m j k') k) (sh K (fun j' => SecondRingShift.T K H m j' k) j))) := by
    have := O.add_le_add _ _ _ _ hA hB
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this
    exact O.mul_nonneg _ _ hρ this
  have z0 : SecondRingShift.T K H (m + 1) 0 0 = K.zero := SecondRingAll.T_zero0 K H m
  cases Nat.le_total j k with
  | inl hjk =>
    cases k with
    | zero =>
      have e : j = 0 := Nat.le_antisymm hjk (Nat.zero_le _)
      subst e; rw [z0]; exact hR
    | succ k =>
      have step := step_v K O h1 H hH ρv hρv hr c m hc hmono j k hjk
      refine O.le_trans _ _ _ step ?_
      have := O.mul_le_mul_right _ _ (ρv c) (O.add_le_add _ _ _ _ (O.le_refl (sh K (fun k' => SecondRingShift.T K H m j k') (k + 1))) hB) hρ
      rw [show K.add (sh K (fun k' => SecondRingShift.T K H m j k') (k + 1)) K.zero = SecondRingShift.T K H m j k by
        show K.add (SecondRingShift.T K H m j k) K.zero = _; lib_ring] at this
      rw [K.mul_comm (ρv c) (SecondRingShift.T K H m j k), K.mul_comm (ρv c)]; exact this
  | inr hkj =>
    cases j with
    | zero =>
      have e : k = 0 := Nat.le_antisymm hkj (Nat.zero_le _)
      subst e; rw [z0]; exact hR
    | succ j =>
      -- symmetric: swap the roles via T(n, a, b) = T(n, b, a)
      have sw : ∀ n a b, SecondRingShift.T K H n a b = SecondRingShift.T K H n b a := fun n a b => by
        unfold SecondRingShift.T; rw [L1Full.cg_comm]; lib_ring
      have step0 := step_v K O h1 H hH ρv hρv hr c m hc hmono k j hkj
      have step : O.le (SecondRingShift.T K H (m + 1) (j + 1) k) (K.mul (ρv c) (SecondRingShift.T K H m j k)) := by
        rw [sw (m + 1) (j + 1) k, sw m j k]; exact step0
      refine O.le_trans _ _ _ step ?_
      have := O.mul_le_mul_right _ _ (ρv c) (O.add_le_add _ _ _ _ hA (O.le_refl (sh K (fun j' => SecondRingShift.T K H m j' k) (j + 1)))) hρ
      rw [show K.add K.zero (sh K (fun j' => SecondRingShift.T K H m j' k) (j + 1)) = SecondRingShift.T K H m j k by
        show K.add K.zero (SecondRingShift.T K H m j k) = _; lib_ring] at this
      rw [K.mul_comm (ρv c) (SecondRingShift.T K H m j k), K.mul_comm (ρv c)]; exact this

/-- **C_{m+1} ≤ 2ρ_c C_m with a variable ratio** (every truncation) -/
theorem shift_var (h1 : O.le K.zero K.one) (H : Nat → R) (hH : ∀ j, O.le K.zero (H j)) (ρv : Nat → R)
    (hρv : ∀ k, O.le K.zero (ρv k)) (hr : ∀ k, O.le (H (k + 1)) (K.mul (ρv k) (H k))) (c m L : Nat) (hc : c + c ≤ m)
    (hmono : ∀ k, c ≤ k → O.le (ρv k) (ρv c)) (ρ : R) (eρ : ρ = ρv c) :
    O.le (C K H L (m + 1)) (K.mul (K.add ρ ρ) (C K H L m)) := by
  have hρ : O.le K.zero ρ := by rw [eρ]; exact hρv c
  rw [SecondRingShift.C_eq, SecondRingShift.C_eq]
  have tw : ∀ j k, O.le (SecondRingShift.T K H (m + 1) j k) (K.mul ρ (K.add (sh K (fun k' => SecondRingShift.T K H m j k') k)
      (sh K (fun j' => SecondRingShift.T K H m j' k) j))) := fun j k => by
    rw [eρ]; exact tw_v K O h1 H hH ρv hρv hr c m hc hmono j k
  have s1 := sumN_mono K O _ _ (fun j => sumN_mono K O _ _ (fun k => tw j k) L) L
  refine O.le_trans _ _ _ s1 ?_
  -- split the double sum of ρ(A + B)
  have eS : sumN K (fun j => sumN K (fun k => K.mul ρ (K.add (sh K (fun k' => SecondRingShift.T K H m j k') k)
      (sh K (fun j' => SecondRingShift.T K H m j' k) j))) L) L
      = K.mul ρ (K.add (sumN K (fun j => sumN K (sh K (fun k' => SecondRingShift.T K H m j k')) L) L)
          (sumN K (sh K (fun j' => sumN K (fun k => SecondRingShift.T K H m j' k) L)) L)) := by
    rw [← sumN_add, ← sumN_mul]
    apply SecondRing.sumN_ext_lt K _ _ L
    intro j _
    cases j with
    | zero =>
      show sumN K (fun k => K.mul ρ (K.add (sh K (fun k' => SecondRingShift.T K H m 0 k') k) K.zero)) L
        = K.mul ρ (K.add (sumN K (sh K (fun k' => SecondRingShift.T K H m 0 k')) L) K.zero)
      rw [show K.add (sumN K (sh K (fun k' => SecondRingShift.T K H m 0 k')) L) K.zero = sumN K (sh K (fun k' => SecondRingShift.T K H m 0 k')) L by lib_ring,
        ← sumN_mul]
      apply SecondRing.sumN_ext_lt K _ _ L; intro k _; lib_ring
    | succ j =>
      show _ = K.mul ρ (K.add (sumN K (sh K (fun k' => SecondRingShift.T K H m (j + 1) k')) L) (sumN K (fun k => SecondRingShift.T K H m j k) L))
      rw [← sumN_add, ← sumN_mul]; rfl
  rw [eS, show K.mul (K.add ρ ρ) (sumN K (fun j => sumN K (fun k => SecondRingShift.T K H m j k) L) L)
      = K.mul ρ (K.add (sumN K (fun j => sumN K (fun k => SecondRingShift.T K H m j k) L) L)
          (sumN K (fun j => sumN K (fun k => SecondRingShift.T K H m j k) L) L)) by lib_ring]
  rw [K.mul_comm ρ, K.mul_comm ρ]
  apply O.mul_le_mul_right _ _ _ _ hρ
  apply O.add_le_add
  · exact sumN_mono K O _ _ (fun j => sh_sum_le K O _ (T_nn K O h1 H hH m j) L) L
  · exact sh_sum_le K O _ (fun j => sumN_nn K O _ (T_nn K O h1 H hH m j) L) L

/-- **list form**: for a nonnegative list X with X_{k+1} ≤ ρ_k X_k (ρ eventually nonincreasing from c, 2c ≤ m),
  (X·X)_{m+1} ≤ 2ρ_c (X·X)_m -/
theorem cgc_var (h1 : O.le K.zero K.one) (X : List R) (hX : ∀ k, O.le K.zero (co K X k)) (ρv : Nat → R)
    (hρv : ∀ k, O.le K.zero (ρv k)) (hr : ∀ k, O.le (co K X (k + 1)) (K.mul (ρv k) (co K X k))) (c m : Nat)
    (hc : c + c ≤ m) (hmono : ∀ k, c ≤ k → O.le (ρv k) (ρv c)) :
    O.le (MigdalCone.cgc K X X (m + 1)) (K.mul (K.add (ρv c) (ρv c)) (MigdalCone.cgc K X X m)) := by
  rw [SecondRingStep.cgc_eq K X (m + 1), SecondRingStep.cgc_eq K X m]
  exact shift_var K O h1 (co K X) hX ρv hρv hr c m X.length hc hmono (ρv c) rfl

/-- **decimation at one character**: 0 ≤ Y_{n+1} ≤ ρ·Y_n gives (decM Y)_{n+1} ≤ ρ⁴·(decM Y)_n -/
theorem decM_pt (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (l : List R) (ρ : R)
    (hρ : O.le K.zero ρ) (k : Nat) (hn1 : O.le K.zero (co K l (k + 1))) (hn : O.le K.zero (co K l k))
    (hr : O.le (co K l (k + 1)) (K.mul ρ (co K l k))) :
    O.le (co K (decM K O F 0 l) (k + 1)) (K.mul (pwR K ρ 4) (co K (decM K O F 0 l) k)) := by
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have ipos : ∀ k, O.le K.zero (F.inv (cN K (k + 1))) := fun k => F.inv_pos _ (nn _) (GridQ.cN_pos K O h1 h10 k)
  rw [RatioStep.co_decM, RatioStep.co_decM, Nat.zero_add, Nat.zero_add]
  have a := SU2Strong4D.pw_mono K O h1 _ _ hn1 hr 4
  rw [GKS2.pwR_mul] at a
  have b := SU2Strong4D.pw_mono K O h1 _ _ (ipos (k + 1))
    (Lambert.inv_anti K O F _ _ (nn _) (GridQ.cN_pos K O h1 h10 k) (GridQ.cN_pos K O h1 h10 (k + 1))
      (SU2CharExp.cN_mono K O h1 (k + 1) (k + 1 + 1) (Nat.le_succ _))) 3
  have s1 := O.mul_le_mul_right _ _ _ a (AreaLower.pw_nn K O _ (ipos (k + 1)) h1 3)
  have s2 := O.mul_le_mul_right _ _ (K.mul (pwR K ρ 4) (pwR K (co K l k) 4)) b
    (O.mul_nonneg _ _ (AreaLower.pw_nn K O ρ hρ h1 4) (AreaLower.pw_nn K O _ hn h1 4))
  have t := O.le_trans _ _ _ s1 (by rw [K.mul_comm (K.mul (pwR K ρ 4) (pwR K (co K l k) 4))]; exact s2)
  rw [show K.mul (pwR K (F.inv (cN K (k + 1))) 3) (K.mul (pwR K ρ 4) (pwR K (co K l k) 4))
    = K.mul (pwR K ρ 4) (K.mul (pwR K (co K l k) 4) (pwR K (F.inv (cN K (k + 1))) 3)) by lib_ring] at t
  exact t

/-- floor of n/2 by recursion (no Nat.div lemmas) -/
def half : Nat → Nat
  | 0 => 0
  | 1 => 0
  | n + 2 => half n + 1

theorem half_le : ∀ n, half n + half n ≤ n
  | 0 => Nat.le_refl 0
  | 1 => Nat.zero_le 1
  | n + 2 => by
    show half n + 1 + (half n + 1) ≤ n + 2
    have := half_le n
    have e : half n + 1 + (half n + 1) = half n + half n + 2 := by lib_nat
    rw [e]; exact Nat.add_le_add_right this 2

theorem half_mono : ∀ m n, m ≤ n → half m ≤ half n
  | 0, _, _ => Nat.zero_le _
  | 1, n, _ => Nat.zero_le _
  | m + 2, 0, h => absurd h (Nat.not_succ_le_zero _)
  | m + 2, 1, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)
  | m + 2, n + 2, h => Nat.succ_le_succ (half_mono m n (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ h)))

/-- the ratio sequence of the product: ρ′_m = 2ρ_{⌊m/2⌋}; it is nonincreasing wherever ρ is -/
def rho2 (ρv : Nat → R) (m : Nat) : R := K.add (ρv (half m)) (ρv (half m))

/-- **product ratio sequence**: for a nonnegative list X with X_{k+1} ≤ ρ_k X_k and ρ nonincreasing,
  (X·X)_{m+1} ≤ ρ′_m (X·X)_m for every m -/
theorem cgc_rho2 (h1 : O.le K.zero K.one) (X : List R) (hX : ∀ k, O.le K.zero (co K X k)) (ρv : Nat → R)
    (hρv : ∀ k, O.le K.zero (ρv k)) (hr : ∀ k, O.le (co K X (k + 1)) (K.mul (ρv k) (co K X k)))
    (hdec : ∀ a b, a ≤ b → O.le (ρv b) (ρv a)) (m : Nat) :
    O.le (MigdalCone.cgc K X X (m + 1)) (K.mul (rho2 K ρv m) (MigdalCone.cgc K X X m)) :=
  cgc_var K O h1 X hX ρv hρv hr (half m) m (half_le m) (fun k hk => hdec _ _ hk)

theorem rho2_nn (ρv : Nat → R) (hρv : ∀ k, O.le K.zero (ρv k)) (m : Nat) : O.le K.zero (rho2 K ρv m) := by
  have := O.add_le_add _ _ _ _ (hρv (half m)) (hρv (half m))
  rw [show K.add K.zero K.zero = K.zero by lib_ring] at this; exact this

theorem rho2_dec (ρv : Nat → R) (hdec : ∀ a b, a ≤ b → O.le (ρv b) (ρv a)) (a b : Nat) (h : a ≤ b) :
    O.le (rho2 K ρv b) (rho2 K ρv a) :=
  O.add_le_add _ _ _ _ (hdec _ _ (half_mono a b h)) (hdec _ _ (half_mono a b h))

/-- the product list inherits a nonincreasing ratio sequence ρ′ = rho2 ρ -/
theorem prodL_rho2 (h1 : O.le K.zero K.one) (X : List R) (M : Nat) (hX : X.length = M + 2)
    (hXn : ∀ k, O.le K.zero (co K X k)) (ρv : Nat → R) (hρv : ∀ k, O.le K.zero (ρv k))
    (hr : ∀ k, O.le (co K X (k + 1)) (K.mul (ρv k) (co K X k))) (hdec : ∀ a b, a ≤ b → O.le (ρv b) (ρv a)) :
    (∀ k, O.le K.zero (co K (L1Full.prodL K X M) k)) ∧
      ∀ m, O.le (co K (L1Full.prodL K X M) (m + 1)) (K.mul (rho2 K ρv m) (co K (L1Full.prodL K X M) m)) := by
  refine ⟨fun k => ?_, fun m => ?_⟩
  · rw [L1Full.co_prodL K X M hX]; exact HeatL1All.cgc_nn K O X X (M + 1) hX hXn hXn k
  · rw [L1Full.co_prodL K X M hX, L1Full.co_prodL K X M hX]
    exact cgc_rho2 K O h1 X hXn ρv hρv hr hdec m

/-- **the tail ratio after one MK step**: (mk X)_{n+1} ≤ (ρ″_n)⁴ (mk X)_n with ρ″ = rho2 (rho2 ρ) -/
theorem mk_rho (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X : List R) (M : Nat)
    (hX : X.length = M + 2) (hXn : ∀ k, O.le K.zero (co K X k)) (ρv : Nat → R) (hρv : ∀ k, O.le K.zero (ρv k))
    (hr : ∀ k, O.le (co K X (k + 1)) (K.mul (ρv k) (co K X k))) (hdec : ∀ a b, a ≤ b → O.le (ρv b) (ρv a)) (n : Nat) :
    O.le (co K (FlowStep.mk K O F X M) (n + 1)) (K.mul (pwR K (rho2 K (rho2 K ρv) n) 4) (co K (FlowStep.mk K O F X M) n)) := by
  have Y := prodL_rho2 K O h1 X M hX hXn ρv hρv hr hdec
  have lY : (L1Full.prodL K X M).length = 2 * M + 1 + 2 := HeatSecondStep.len_prodL K X M
  have Z := prodL_rho2 K O h1 _ (2 * M + 1) lY Y.1 (rho2 K ρv) (rho2_nn K O ρv hρv) Y.2 (rho2_dec K O ρv hdec)
  unfold FlowStep.mk FlowStep.Ysq
  exact decM_pt K O F h1 h10 _ _ (rho2_nn K O _ (rho2_nn K O ρv hρv) n) n (Z.1 (n + 1)) (Z.1 n) (Z.2 n)

end RatioVar

#print axioms RatioVar.step_v
#print axioms RatioVar.tw_v
#print axioms RatioVar.shift_var
#print axioms RatioVar.cgc_var
#print axioms RatioVar.decM_pt
#print axioms RatioVar.half_le
#print axioms RatioVar.half_mono
#print axioms RatioVar.cgc_rho2
#print axioms RatioVar.rho2_nn
#print axioms RatioVar.rho2_dec
#print axioms RatioVar.prodL_rho2
#print axioms RatioVar.mk_rho

#print axioms: 12 theorems, no axioms.

90.135 Wilson weight valid up to \(\beta =64\)

The truncated Wilson weight is v-good for \(\beta =2t\le 8,16,64\).

\[ (c_m+c_{m+2})\,c_{m+2}\le (c_{m+1}+c_{m+3})\,c_{m+1}\qquad (t^2\le 1024,\ \text {truncation }320) \]

ym/WilsonTailV.lean

/-
  ym/WilsonTailV — T45, G76b3: the Wilson VV polynomial inequality for every truncation Kk = K' + 2, given the
  explicit tail condition  Σ_{i ≤ K'+2} 2^{2s} y^s / ((s−1)!)² ≤ 1  (s = i + K' + 2):
      (m+1)(m+3)·Q₀₂ + (m+3)²·y·Q₂₂ ≤ (m+2)²·Q₁₁ + (m+2)(m+4)·y·Q₁₃,   Q_ab = ev(pL_a·pL_b)(y),
  for all m and y ≥ 0. Head: [Φ]₀ = δ = (m+2)/((m+2)!)² exactly; middle: WilsonVVR.vv_co; tail: each
  coefficient s ≥ Kk is ≤ δ·2^{2s}/((s−1)!)² by WilsonTailB, absorbed by WilsonTailS.sum_cmp.
  Lean 4 core only, no axioms.
-/
import ym.WilsonTailB
import ym.WilsonTailS
import ym.WilsonVVR
import ym.Projective
import ym.TopLC
import ym.Lagrange

namespace WilsonTailV

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp LibBinom

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- a/A ≤ b/B from a·B ≤ b·A -/
theorem frac_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (a A b B : Nat) (hA : 1 ≤ A) (hB : 1 ≤ B)
    (h : a * B ≤ b * A) : O.le (K.mul (cN K a) (F.inv (cN K A))) (K.mul (cN K b) (F.inv (cN K B))) := by
  have pA := pos_of_ge_one K O h1 h10 A hA
  have pB := pos_of_ge_one K O h1 h10 B hB
  have P0 := O.mul_nonneg _ _ pA.1 pB.1
  have Pne : K.mul (cN K A) (cN K B) ≠ K.zero := by
    rw [← cN_mul K]; exact (pos_of_ge_one K O h1 h10 _ (Nat.mul_le_mul hA hB)).2
  refine GridQ.cancel K O F _ _ _ P0 Pne ?_
  have iA := F.mul_inv _ pA.2
  have iB := F.mul_inv _ pB.2
  have le := cN_mono K O h1 _ _ h
  rw [cN_mul K, cN_mul K] at le
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ le (TripleE.le_of_eq' K O _ _ ?_))
  · rw [show K.mul (K.mul (cN K A) (cN K B)) (K.mul (cN K a) (F.inv (cN K A)))
        = K.mul (K.mul (cN K A) (F.inv (cN K A))) (K.mul (cN K a) (cN K B)) by lib_ring, iA]
    lib_ring
  · rw [show K.mul (K.mul (cN K A) (cN K B)) (K.mul (cN K b) (F.inv (cN K B)))
        = K.mul (K.mul (cN K B) (F.inv (cN K B))) (K.mul (cN K b) (cN K A)) by lib_ring, iB]
    lib_ring

/-- a/A + d/B ≤ c/Cc from (a·B + d·A)·Cc ≤ c·(A·B) -/
theorem frac_add_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (a A d B c Cc : Nat) (hA : 1 ≤ A) (hB : 1 ≤ B)
    (hC : 1 ≤ Cc) (h : (a * B + d * A) * Cc ≤ c * (A * B)) :
    O.le (K.add (K.mul (cN K a) (F.inv (cN K A))) (K.mul (cN K d) (F.inv (cN K B))))
      (K.mul (cN K c) (F.inv (cN K Cc))) := by
  have pA := pos_of_ge_one K O h1 h10 A hA
  have pB := pos_of_ge_one K O h1 h10 B hB
  have pC := pos_of_ge_one K O h1 h10 Cc hC
  have P0 := O.mul_nonneg _ _ (O.mul_nonneg _ _ pA.1 pB.1) pC.1
  have Pne : K.mul (K.mul (cN K A) (cN K B)) (cN K Cc) ≠ K.zero := by
    rw [← cN_mul K, ← cN_mul K]
    exact (pos_of_ge_one K O h1 h10 _ (Nat.mul_le_mul (Nat.mul_le_mul hA hB) hC)).2
  refine GridQ.cancel K O F _ _ _ P0 Pne ?_
  have iA := F.mul_inv _ pA.2
  have iB := F.mul_inv _ pB.2
  have iC := F.mul_inv _ pC.2
  have le := cN_mono K O h1 _ _ h
  rw [cN_mul K, cN_add K, cN_mul K, cN_mul K, cN_mul K c, cN_mul K A] at le
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ le (TripleE.le_of_eq' K O _ _ ?_))
  · rw [show K.mul (K.mul (K.mul (cN K A) (cN K B)) (cN K Cc))
          (K.add (K.mul (cN K a) (F.inv (cN K A))) (K.mul (cN K d) (F.inv (cN K B))))
        = K.add (K.mul (K.mul (cN K A) (F.inv (cN K A))) (K.mul (K.mul (cN K a) (cN K B)) (cN K Cc)))
            (K.mul (K.mul (cN K B) (F.inv (cN K B))) (K.mul (K.mul (cN K d) (cN K A)) (cN K Cc))) by lib_ring, iA, iB]
    lib_ring
  · rw [show K.mul (K.mul (K.mul (cN K A) (cN K B)) (cN K Cc)) (K.mul (cN K c) (F.inv (cN K Cc)))
        = K.mul (K.mul (cN K Cc) (F.inv (cN K Cc))) (K.mul (cN K c) (K.mul (cN K A) (cN K B))) by lib_ring, iC]
    lib_ring

/-- shifted coefficients -/
def sh (f : Nat → R) : Nat → R
  | 0 => K.zero
  | s + 1 => f s

theorem shift_sum (f : Nat → R) (y : R) : ∀ N,
    K.mul y (sumN K (fun T => K.mul (f T) (pwR K y T)) N) = sumN K (fun s => K.mul (sh K f s) (pwR K y s)) (N + 1)
  | 0 => by
    show K.mul y K.zero = K.add K.zero (K.mul K.zero K.one)
    lib_ring
  | N + 1 => by
    show K.mul y (K.add (sumN K (fun T => K.mul (f T) (pwR K y T)) N) (K.mul (f N) (pwR K y N)))
      = K.add (sumN K (fun s => K.mul (sh K f s) (pwR K y s)) (N + 1)) (K.mul (f N) (K.mul (pwR K y N) y))
    rw [← shift_sum f y N]
    lib_ring

/-- coefficients of products of truncated Wilson series are nonnegative -/
theorem co_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (a b Kk T : Nat) :
    O.le K.zero (co K (mulL K (WilsonTuranF.pL K O F a Kk) (WilsonTuranF.pL K O F b Kk)) T) := by
  have pnn : ∀ c j, O.le K.zero (co K (WilsonTuranF.pL K O F c Kk) j) := fun c j => by
    cases Nat.lt_or_ge j Kk with
    | inl hj =>
      rw [WilsonTuranF.co_pL_in K O F c Kk j hj]
      unfold WilsonTuranF.α
      have hD := WilsonTuranF.fpos K O h1 h10 j (c + j + 1)
      exact F.inv_pos _ hD.1 hD.2
    | inr hj => rw [WilsonTuranF.co_pL_out K O F c Kk j hj]; exact O.le_refl _
  rw [co_mulL]
  have z := WilsonTuranF.anti_zero_diag K T (fun _ _ => K.zero) (fun _ _ _ => rfl)
  rw [← z]
  exact WilsonTailB.anti_mono K O T _ _ (fun j k _ => O.mul_nonneg _ _ (pnn a j) (pnn b k))

theorem co_zero (a b Kk : Nat) (hK : 1 ≤ Kk) :
    co K (mulL K (WilsonTuranF.pL K O F a Kk) (WilsonTuranF.pL K O F b Kk)) 0
      = K.mul (WilsonTuranF.α K O F a 0) (WilsonTuranF.α K O F b 0) := by
  rw [co_mulL]
  show K.mul (co K (WilsonTuranF.pL K O F a Kk) 0) (co K (WilsonTuranF.pL K O F b Kk) 0) = _
  rw [WilsonTuranF.co_pL_in K O F a Kk 0 hK, WilsonTuranF.co_pL_in K O F b Kk 0 hK]

/-- the exact head: (m+1)(m+3)α(m,0)α(m+2,0) + (m+2)/((m+2)!)² ≤ (m+2)²α(m+1,0)² -/
theorem head0 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (m : Nat) :
    O.le (K.add (K.mul (cN K ((m + 1) * (m + 3))) (K.mul (WilsonTuranF.α K O F m 0) (WilsonTuranF.α K O F (m + 2) 0)))
        (K.mul (cN K (m + 2)) (F.inv (cN K (fact (m + 2) * fact (m + 2))))))
      (K.mul (cN K ((m + 2) * (m + 2))) (K.mul (WilsonTuranF.α K O F (m + 1) 0) (WilsonTuranF.α K O F (m + 1) 0))) := by
  unfold WilsonTuranF.α
  have p0 := WilsonTuranF.fpos K O h1 h10 0 (m + 0 + 1)
  have p2 := WilsonTuranF.fpos K O h1 h10 0 (m + 2 + 0 + 1)
  have p1 := WilsonTuranF.fpos K O h1 h10 0 (m + 1 + 0 + 1)
  rw [← Projective.inv_mul K O F _ _ p0.2 p2.2, ← Projective.inv_mul K O F _ _ p1.2 p1.2, ← cN_mul K, ← cN_mul K]
  have f1 : 1 ≤ fact 0 * fact (m + 0 + 1) * (fact 0 * fact (m + 2 + 0 + 1)) :=
    Nat.mul_le_mul (Nat.mul_le_mul (fact_pos _) (fact_pos _)) (Nat.mul_le_mul (fact_pos _) (fact_pos _))
  have f2 : 1 ≤ fact (m + 2) * fact (m + 2) := Nat.mul_le_mul (fact_pos _) (fact_pos _)
  have f3 : 1 ≤ fact 0 * fact (m + 1 + 0 + 1) * (fact 0 * fact (m + 1 + 0 + 1)) :=
    Nat.mul_le_mul (Nat.mul_le_mul (fact_pos _) (fact_pos _)) (Nat.mul_le_mul (fact_pos _) (fact_pos _))
  refine O.le_trans _ _ _ (frac_add_le K O F h1 h10 _ _ _ _ ((m + 2) * (m + 2)) _ f1 f2 f3 ?_) (O.le_refl _)
  apply Nat.le_of_eq
  rw [show fact (m + 2 + 0 + 1) = fact (m + 1) * (m + 2) * (m + 3) from rfl,
    show fact (m + 1 + 0 + 1) = fact (m + 1) * (m + 2) from rfl, show fact (m + 0 + 1) = fact (m + 1) from rfl,
    show fact 0 = 1 from rfl]
  lib_nat

/-- the left identity of nat_tail with independent atoms (a, b, c, d) = (m+1, m+2, m+3, r+2) -/
theorem eL_atoms (a b c d Q Fm Gr : Nat) :
    (a * c * (Q * 2 * 2) * (Gr * Gr * (Fm * b * c * (Fm * b * c)))
      + c * c * Q * (Gr * d * (Gr * d) * (b * (Fm * (Fm * b * c)))))
      * (Fm * b * (Fm * b) * (Gr * Gr))
      = (Q * (Fm * Fm * Fm * Fm) * (Gr * Gr * Gr * Gr) * (b * b * b * b) * (c * c * c)) * (4 * a + d * d) := by lib_nat

/-- the right identity of nat_tail with independent atoms -/
theorem eR_atoms (b c d Q Fm Gr : Nat) :
    b * (Q * 2 * 2) * (Gr * d * (Gr * d) * (b * (Fm * (Fm * b * c))) * (Gr * Gr * (Fm * b * c * (Fm * b * c))))
      = (Q * (Fm * Fm * Fm * Fm) * (Gr * Gr * Gr * Gr) * (b * b * b * b) * (c * c * c)) * (4 * b * (d * d)) := by lib_nat

/-- the Nat comparison behind one tail coefficient (s = r + 2) -/
theorem nat_tail (m r C1 C2 : Nat) (hC1 : C1 ≤ 2 ^ ((r + 2) + (r + 2))) (hC2 : C2 ≤ 2 ^ ((r + 1) + (r + 1))) :
    ((m + 1) * (m + 3) * C1 * (fact (r + 1) * fact (r + 1) * (fact (m + 3) * fact (m + 3)))
      + (m + 3) * (m + 3) * C2 * (fact (r + 2) * fact (r + 2) * ((m + 2) * (fact (m + 1) * fact (m + 3)))))
      * (fact (m + 2) * fact (m + 2) * (fact (r + 1) * fact (r + 1)))
    ≤ (m + 2) * 2 ^ ((r + 2) + (r + 2))
      * (fact (r + 2) * fact (r + 2) * ((m + 2) * (fact (m + 1) * fact (m + 3)))
        * (fact (r + 1) * fact (r + 1) * (fact (m + 3) * fact (m + 3)))) := by
  have hP : 2 ^ ((r + 2) + (r + 2)) = 2 ^ ((r + 1) + (r + 1)) * 2 * 2 := by
    rw [show (r + 2) + (r + 2) = (r + 1) + (r + 1) + 2 by lib_nat]; rfl
  rw [hP] at hC1 ⊢
  rw [show fact (r + 2) = fact (r + 1) * (r + 2) from rfl, show fact (m + 3) = fact (m + 1) * (m + 2) * (m + 3) from rfl,
    show fact (m + 2) = fact (m + 1) * (m + 2) from rfl]
  generalize 2 ^ ((r + 1) + (r + 1)) = Q at hC1 hC2 ⊢
  generalize fact (m + 1) = Fm
  generalize fact (r + 1) = Gr
  have W1 := Nat.mul_le_mul_left ((m + 1) * (m + 3)) hC1
  have W2 := Nat.mul_le_mul_left ((m + 3) * (m + 3)) hC2
  refine Nat.le_trans (Nat.mul_le_mul_right _ (Nat.add_le_add
    (Nat.mul_le_mul_right (Gr * Gr * (Fm * (m + 2) * (m + 3) * (Fm * (m + 2) * (m + 3)))) W1)
    (Nat.mul_le_mul_right (Gr * (r + 2) * (Gr * (r + 2)) * ((m + 2) * (Fm * (Fm * (m + 2) * (m + 3))))) W2))) ?_
  have eL := eL_atoms (m + 1) (m + 2) (m + 3) (r + 2) Q Fm Gr
  have eR := eR_atoms (m + 2) (m + 3) (r + 2) Q Fm Gr
  refine Nat.le_trans (Nat.le_of_eq eL) (Nat.le_trans ?_ (Nat.le_of_eq eR.symm))
  exact Nat.mul_le_mul_left _ (by lib_nat_le)

/-- c·x ≤ c·y -/
theorem lmul (c x y : R) (hc : O.le K.zero c) (h : O.le x y) : O.le (K.mul c x) (K.mul c y) := by
  have := O.mul_le_mul_right _ _ c h hc
  rw [K.mul_comm x, K.mul_comm y] at this; exact this

/-- **one tail coefficient** (s = r + 2): L_s ≤ δ·2^{2s}/((s−1)!)²·y^s, δ = (m+2)/((m+2)!)² -/
theorem tail_term (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (m Kk r : Nat) (y : R) (hy : O.le K.zero y) :
    O.le (K.add (K.mul (cN K ((m + 1) * (m + 3)))
          (K.mul (co K (mulL K (WilsonTuranF.pL K O F m Kk) (WilsonTuranF.pL K O F (m + 2) Kk)) (r + 2)) (pwR K y (r + 2))))
        (K.mul (cN K ((m + 3) * (m + 3)))
          (K.mul (co K (mulL K (WilsonTuranF.pL K O F (m + 2) Kk) (WilsonTuranF.pL K O F (m + 2) Kk)) (r + 1)) (pwR K y (r + 2)))))
      (K.mul (K.mul (cN K (m + 2)) (F.inv (cN K (fact (m + 2) * fact (m + 2)))))
        (K.mul (K.mul (cN K (2 ^ ((r + 2) + (r + 2)))) (F.inv (cN K (fact (r + 1) * fact (r + 1))))) (pwR K y (r + 2)))) := by
  have b02 := WilsonTailB.co02_le K O F h1 h10 m Kk (r + 2) (Nat.succ_le_succ (Nat.zero_le _))
  have b22 := WilsonTailB.co22_le K O F h1 h10 m Kk (r + 1)
  have py := AreaLower.pw_nn K O y hy h1 (r + 2)
  have c1 := cN_nn K O h1 ((m + 1) * (m + 3))
  have c2 := cN_nn K O h1 ((m + 3) * (m + 3))
  have X1 : 1 ≤ fact (r + 2) * fact (r + 2) * ((m + 2) * (fact (m + 1) * fact (m + 3))) :=
    Nat.mul_le_mul (Nat.mul_le_mul (fact_pos _) (fact_pos _))
      (Nat.mul_le_mul (Nat.succ_le_succ (Nat.zero_le _)) (Nat.mul_le_mul (fact_pos _) (fact_pos _)))
  have X2 : 1 ≤ fact (r + 1) * fact (r + 1) * (fact (m + 3) * fact (m + 3)) :=
    Nat.mul_le_mul (Nat.mul_le_mul (fact_pos _) (fact_pos _)) (Nat.mul_le_mul (fact_pos _) (fact_pos _))
  have DG : 1 ≤ fact (m + 2) * fact (m + 2) * (fact (r + 1) * fact (r + 1)) :=
    Nat.mul_le_mul (Nat.mul_le_mul (fact_pos _) (fact_pos _)) (Nat.mul_le_mul (fact_pos _) (fact_pos _))
  have fr := frac_add_le K O F h1 h10 _ _ _ _ ((m + 2) * 2 ^ ((r + 2) + (r + 2))) _ X1 X2 DG
    (nat_tail m r _ _ (WilsonTailB.C_le_pow ((r + 2) + (r + 2)) (r + 2)) (WilsonTailB.C_le_pow ((r + 1) + (r + 1)) (r + 1)))
  have s1 := O.add_le_add _ _ _ _ (lmul K O _ _ _ c1 b02) (lmul K O _ _ _ c2 b22)
  have e1 : K.add (K.mul (cN K ((m + 1) * (m + 3))) (K.mul (cN K (C ((r + 2) + (r + 2)) (r + 2)))
        (F.inv (cN K (fact (r + 2) * fact (r + 2) * ((m + 2) * (fact (m + 1) * fact (m + 3))))))))
      (K.mul (cN K ((m + 3) * (m + 3))) (K.mul (cN K (C ((r + 1) + (r + 1)) (r + 1)))
        (F.inv (cN K (fact (r + 1) * fact (r + 1) * (fact (m + 3) * fact (m + 3)))))))
      = K.add (K.mul (cN K ((m + 1) * (m + 3) * C ((r + 2) + (r + 2)) (r + 2)))
          (F.inv (cN K (fact (r + 2) * fact (r + 2) * ((m + 2) * (fact (m + 1) * fact (m + 3)))))))
        (K.mul (cN K ((m + 3) * (m + 3) * C ((r + 1) + (r + 1)) (r + 1)))
          (F.inv (cN K (fact (r + 1) * fact (r + 1) * (fact (m + 3) * fact (m + 3)))))) := by
    rw [cN_mul K ((m + 1) * (m + 3)), cN_mul K ((m + 3) * (m + 3))]
    lib_ring
  have s2 := O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ s1 (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ e1) fr)) py
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · have pD := WilsonTuranF.fpos K O h1 h10 (m + 2) (m + 2)
    have pG := WilsonTuranF.fpos K O h1 h10 (r + 1) (r + 1)
    rw [cN_mul K (m + 2), cN_mul K (fact (m + 2) * fact (m + 2)), Projective.inv_mul K O F _ _ pD.2 pG.2]
    lib_ring

/-- **Wilson VV polynomial inequality at truncation Kk = K' + 2, given the explicit tail condition** -/
theorem vv_poly (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (m K' : Nat) (y : R) (hy : O.le K.zero y)
    (htail : O.le (sumN K (fun i => K.mul (K.mul (cN K (2 ^ ((i + K' + 2) + (i + K' + 2))))
        (F.inv (cN K (fact (i + K' + 1) * fact (i + K' + 1))))) (pwR K y (i + K' + 2))) (K' + 3)) K.one) :
    O.le (K.add (K.mul (cN K ((m + 1) * (m + 3)))
          (ev K (mulL K (WilsonTuranF.pL K O F m (K' + 2)) (WilsonTuranF.pL K O F (m + 2) (K' + 2))) y))
        (K.mul (cN K ((m + 3) * (m + 3)))
          (K.mul y (ev K (mulL K (WilsonTuranF.pL K O F (m + 2) (K' + 2)) (WilsonTuranF.pL K O F (m + 2) (K' + 2))) y))))
      (K.add (K.mul (cN K ((m + 2) * (m + 2)))
          (ev K (mulL K (WilsonTuranF.pL K O F (m + 1) (K' + 2)) (WilsonTuranF.pL K O F (m + 1) (K' + 2))) y))
        (K.mul (cN K ((m + 2) * (m + 4)))
          (K.mul y (ev K (mulL K (WilsonTuranF.pL K O F (m + 1) (K' + 2)) (WilsonTuranF.pL K O F (m + 3) (K' + 2))) y)))) := by
  generalize eq02 : co K (mulL K (WilsonTuranF.pL K O F m (K' + 2)) (WilsonTuranF.pL K O F (m + 2) (K' + 2))) = q02
  have ev02 : ev K (mulL K (WilsonTuranF.pL K O F m (K' + 2)) (WilsonTuranF.pL K O F (m + 2) (K' + 2))) y
      = sumN K (fun T => K.mul (q02 T) (pwR K y T)) (2 * (K' + 2) + 1) := by
    rw [← eq02]; exact WilsonTuranF.ev_co K y _ _ (fun T hT => WilsonTuranF.co_hi K O F _ _ _ T (Nat.le_of_succ_le hT))
  have ev11 : ev K (mulL K (WilsonTuranF.pL K O F (m + 1) (K' + 2)) (WilsonTuranF.pL K O F (m + 1) (K' + 2))) y
      = sumN K (fun T => K.mul (co K (mulL K (WilsonTuranF.pL K O F (m + 1) (K' + 2)) (WilsonTuranF.pL K O F (m + 1) (K' + 2))) T)
          (pwR K y T)) (2 * (K' + 2) + 1) :=
    WilsonTuranF.ev_co K y _ _ (fun T hT => WilsonTuranF.co_hi K O F _ _ _ T (Nat.le_of_succ_le hT))
  have ev22 : K.mul y (ev K (mulL K (WilsonTuranF.pL K O F (m + 2) (K' + 2)) (WilsonTuranF.pL K O F (m + 2) (K' + 2))) y)
      = sumN K (fun s => K.mul (sh K (co K (mulL K (WilsonTuranF.pL K O F (m + 2) (K' + 2))
          (WilsonTuranF.pL K O F (m + 2) (K' + 2)))) s) (pwR K y s)) (2 * (K' + 2) + 1) := by
    rw [WilsonTuranF.ev_co K y _ (2 * (K' + 2)) (WilsonTuranF.co_hi K O F _ _ _)]
    exact shift_sum K _ y _
  have ev13 : K.mul y (ev K (mulL K (WilsonTuranF.pL K O F (m + 1) (K' + 2)) (WilsonTuranF.pL K O F (m + 3) (K' + 2))) y)
      = sumN K (fun s => K.mul (sh K (co K (mulL K (WilsonTuranF.pL K O F (m + 1) (K' + 2))
          (WilsonTuranF.pL K O F (m + 3) (K' + 2)))) s) (pwR K y s)) (2 * (K' + 2) + 1) := by
    rw [WilsonTuranF.ev_co K y _ (2 * (K' + 2)) (WilsonTuranF.co_hi K O F _ _ _)]
    exact shift_sum K _ y _
  rw [ev02, ev11, ev22, ev13, ← sumN_mul K, ← sumN_mul K, ← sumN_mul K, ← sumN_mul K, ← sumN_add K, ← sumN_add K,
    show 2 * (K' + 2) + 1 = (K' + 3) + (K' + 2) by lib_nat]
  have hδ : O.le K.zero (K.mul (cN K (m + 2)) (F.inv (cN K (fact (m + 2) * fact (m + 2))))) := by
    have p := WilsonTuranF.fpos K O h1 h10 (m + 2) (m + 2)
    exact O.mul_nonneg _ _ (cN_nn K O h1 _) (F.inv_pos _ p.1 p.2)
  have hK : 1 ≤ K' + 2 := Nat.succ_le_succ (Nat.zero_le _)
  have cnn := fun a b T => co_nn K O F h1 h10 a b (K' + 2) T
  have py := fun s => AreaLower.pw_nn K O y hy h1 s
  refine WilsonTailS.sum_cmp K O _ _ (K.mul (cN K (m + 2)) (F.inv (cN K (fact (m + 2) * fact (m + 2))))) (K' + 2) (K' + 3) hK ?_ ?_ ?_ ?_
  · -- head
    show O.le (K.add (K.add (K.mul (cN K ((m + 1) * (m + 3))) (K.mul (q02 0) K.one))
        (K.mul (cN K ((m + 3) * (m + 3))) (K.mul K.zero K.one))) _)
      (K.add (K.mul (cN K ((m + 2) * (m + 2)))
        (K.mul (co K (mulL K (WilsonTuranF.pL K O F (m + 1) (K' + 2)) (WilsonTuranF.pL K O F (m + 1) (K' + 2))) 0) K.one))
        (K.mul (cN K ((m + 2) * (m + 4))) (K.mul K.zero K.one)))
    rw [← eq02, co_zero K O F _ _ _ hK, co_zero K O F _ _ _ hK]
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ (head0 K O F h1 h10 m) (TripleE.le_of_eq' K O _ _ ?_))
    · lib_ring
    · lib_ring
  · -- middle
    intro s hs1 hs
    cases s with
    | zero => exact absurd hs1 (Nat.not_succ_le_zero 0)
    | succ T =>
      have v := WilsonVVR.vv_co K O F h1 h10 m (K' + 2) T hs
      rw [eq02] at v
      have w := O.mul_le_mul_right _ _ _ v (py (T + 1))
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
      · show K.add (K.mul (cN K ((m + 1) * (m + 3))) (K.mul (q02 (T + 1)) (pwR K y (T + 1))))
            (K.mul (cN K ((m + 3) * (m + 3))) (K.mul (co K (mulL K (WilsonTuranF.pL K O F (m + 2) (K' + 2))
              (WilsonTuranF.pL K O F (m + 2) (K' + 2))) T) (pwR K y (T + 1)))) = _
        lib_ring
      · show _ = K.add (K.mul (cN K ((m + 2) * (m + 2))) (K.mul (co K (mulL K (WilsonTuranF.pL K O F (m + 1) (K' + 2))
              (WilsonTuranF.pL K O F (m + 1) (K' + 2))) (T + 1)) (pwR K y (T + 1))))
            (K.mul (cN K ((m + 2) * (m + 4))) (K.mul (co K (mulL K (WilsonTuranF.pL K O F (m + 1) (K' + 2))
              (WilsonTuranF.pL K O F (m + 3) (K' + 2))) T) (pwR K y (T + 1))))
        lib_ring
  · -- R ≥ 0
    intro s
    have sh13 : O.le K.zero (sh K (co K (mulL K (WilsonTuranF.pL K O F (m + 1) (K' + 2))
        (WilsonTuranF.pL K O F (m + 3) (K' + 2)))) s) := by
      cases s with
      | zero => exact O.le_refl _
      | succ T => exact cnn _ _ T
    exact TopLC.add_nn K O _ _ (O.mul_nonneg _ _ (cN_nn K O h1 _) (O.mul_nonneg _ _ (cnn _ _ s) (py s)))
      (O.mul_nonneg _ _ (cN_nn K O h1 _) (O.mul_nonneg _ _ sh13 (py s)))
  · -- tail
    refine O.le_trans _ _ _ (Lagrange.sum_le_lt K O _ (fun i => K.mul (K.mul (cN K (m + 2)) (F.inv (cN K (fact (m + 2) * fact (m + 2)))))
        (K.mul (K.mul (cN K (2 ^ ((i + K' + 2) + (i + K' + 2)))) (F.inv (cN K (fact (i + K' + 1) * fact (i + K' + 1)))))
          (pwR K y (i + K' + 2)))) (K' + 3) (fun i _ => ?_)) ?_
    · have tt := tail_term K O F h1 h10 m (K' + 2) (i + K') y hy
      rw [eq02] at tt
      exact tt
    · rw [sumN_mul K]
      have := lmul K O _ _ _ hδ htail
      rw [show K.mul (K.mul (cN K (m + 2)) (F.inv (cN K (fact (m + 2) * fact (m + 2))))) K.one
        = K.mul (cN K (m + 2)) (F.inv (cN K (fact (m + 2) * fact (m + 2)))) by lib_ring] at this
      exact this

end WilsonTailV

#print axioms WilsonTailV.frac_le
#print axioms WilsonTailV.frac_add_le
#print axioms WilsonTailV.shift_sum
#print axioms WilsonTailV.co_nn
#print axioms WilsonTailV.co_zero
#print axioms WilsonTailV.head0
#print axioms WilsonTailV.eL_atoms
#print axioms WilsonTailV.eR_atoms
#print axioms WilsonTailV.nat_tail
#print axioms WilsonTailV.lmul
#print axioms WilsonTailV.tail_term
#print axioms WilsonTailV.vv_poly

ym/WilsonTailN.lean

/-
  ym/WilsonTailN — T45, G76b4: the tail condition of WilsonTailV.vv_poly from two Nat inequalities.
  With S = K' + 2, 0 ≤ y ≤ Y, 8Y ≤ S² and 2·2^{2S}·Y^S ≤ ((S−1)!)²:
      Σ_{i<n} 2^{2s} y^s / ((s−1)!)² ≤ 1   (s = i + S),
  because consecutive terms of the majorant u_i = 2^{2s}Y^s/((s−1)!)² halve (2u_{i+1} ≤ u_i ⇔ 8Y ≤ s²),
  so Σ_{i<n} u_i + 2u_n ≤ 2u_0 ≤ 1. Lean 4 core only, no axioms.
-/
import ym.WilsonTailV
import ym.SU2Strong4D
import ym.CertSound

namespace WilsonTailN

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- the majorant term -/
def u (Y K' i : Nat) : R :=
  K.mul (cN K (2 ^ ((i + K' + 2) + (i + K' + 2)) * Y ^ (i + K' + 2)))
    (F.inv (cN K (fact (i + K' + 1) * fact (i + K' + 1))))

theorem u_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Y K' i : Nat) : O.le K.zero (u K O F Y K' i) := by
  have p := WilsonTuranF.fpos K O h1 h10 (i + K' + 1) (i + K' + 1)
  exact O.mul_nonneg _ _ (cN_nn K O h1 _) (F.inv_pos _ p.1 p.2)

/-- consecutive majorant terms halve -/
theorem u_half (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Y K' i : Nat) (hS : 8 * Y ≤ (K' + 2) * (K' + 2)) :
    O.le (K.add (u K O F Y K' (i + 1)) (u K O F Y K' (i + 1))) (u K O F Y K' i) := by
  unfold u
  have e : K.add (K.mul (cN K (2 ^ ((i + 1 + K' + 2) + (i + 1 + K' + 2)) * Y ^ (i + 1 + K' + 2)))
        (F.inv (cN K (fact (i + 1 + K' + 1) * fact (i + 1 + K' + 1)))))
      (K.mul (cN K (2 ^ ((i + 1 + K' + 2) + (i + 1 + K' + 2)) * Y ^ (i + 1 + K' + 2)))
        (F.inv (cN K (fact (i + 1 + K' + 1) * fact (i + 1 + K' + 1)))))
      = K.mul (cN K (2 * (2 ^ ((i + 1 + K' + 2) + (i + 1 + K' + 2)) * Y ^ (i + 1 + K' + 2))))
        (F.inv (cN K (fact (i + 1 + K' + 1) * fact (i + 1 + K' + 1)))) := by
    rw [cN_mul K 2, show cN K 2 = K.add K.one K.one by unfold cN; lib_ring]; lib_ring
  rw [e]
  refine WilsonTailV.frac_le K O F h1 h10 _ _ _ _ (Nat.mul_le_mul (fact_pos _) (fact_pos _))
    (Nat.mul_le_mul (fact_pos _) (fact_pos _)) ?_
  rw [show (i + 1 + K' + 2) + (i + 1 + K' + 2) = (i + K' + 2) + (i + K' + 2) + 2 by lib_nat,
    show i + 1 + K' + 2 = (i + K' + 2) + 1 by lib_nat, show i + 1 + K' + 1 = (i + K' + 1) + 1 by lib_nat]
  show 2 * (2 ^ ((i + K' + 2) + (i + K' + 2)) * 2 * 2 * (Y ^ (i + K' + 2) * Y))
      * (fact (i + K' + 1) * fact (i + K' + 1))
    ≤ 2 ^ ((i + K' + 2) + (i + K' + 2)) * Y ^ (i + K' + 2)
      * (fact (i + K' + 1) * (i + K' + 1 + 1) * (fact (i + K' + 1) * (i + K' + 1 + 1)))
  generalize 2 ^ ((i + K' + 2) + (i + K' + 2)) = P
  generalize Y ^ (i + K' + 2) = Z
  generalize fact (i + K' + 1) = G
  have hs : 8 * Y ≤ (i + K' + 1 + 1) * (i + K' + 1 + 1) := by
    refine Nat.le_trans hS (Nat.mul_le_mul ?_ ?_) <;>
      exact Nat.succ_le_succ (Nat.succ_le_succ (Nat.le_add_left K' i))
  have w := Nat.mul_le_mul_left (P * Z * (G * G)) hs
  refine Nat.le_trans (Nat.le_of_eq ?_) (Nat.le_trans w (Nat.le_of_eq ?_))
  · lib_nat
  · lib_nat

/-- Σ_{i<n} u_{i} + 2u_n ≤ 2u_0 (shifted start j) -/
theorem u_sum (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Y K' : Nat) (hS : 8 * Y ≤ (K' + 2) * (K' + 2)) :
    ∀ n, O.le (K.add (sumN K (fun i => u K O F Y K' i) n) (K.add (u K O F Y K' n) (u K O F Y K' n)))
      (K.add (u K O F Y K' 0) (u K O F Y K' 0))
  | 0 => by
    show O.le (K.add K.zero _) _
    rw [K.zero_add]; exact O.le_refl _
  | n + 1 => by
    have ih := u_sum h1 h10 Y K' hS n
    have hh := u_half K O F h1 h10 Y K' n hS
    have a := O.add_le_add _ _ _ _ (O.le_refl (K.add (sumN K (fun i => u K O F Y K' i) n) (u K O F Y K' n))) hh
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ a (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) ih))
    · show K.add (K.add (sumN K (fun i => u K O F Y K' i) n) (u K O F Y K' n)) _ = _
      rfl
    · lib_ring

/-- **the tail condition of vv_poly** -/
theorem tail_ok (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Y K' n : Nat) (y : R) (hy : O.le K.zero y)
    (hyY : O.le y (cN K Y)) (hS : 8 * Y ≤ (K' + 2) * (K' + 2))
    (h0 : 2 * (2 ^ ((K' + 2) + (K' + 2)) * Y ^ (K' + 2)) ≤ fact (K' + 1) * fact (K' + 1)) :
    O.le (sumN K (fun i => K.mul (K.mul (cN K (2 ^ ((i + K' + 2) + (i + K' + 2))))
        (F.inv (cN K (fact (i + K' + 1) * fact (i + K' + 1))))) (pwR K y (i + K' + 2))) n) K.one := by
  have term : ∀ i, O.le (K.mul (K.mul (cN K (2 ^ ((i + K' + 2) + (i + K' + 2))))
        (F.inv (cN K (fact (i + K' + 1) * fact (i + K' + 1))))) (pwR K y (i + K' + 2))) (u K O F Y K' i) := fun i => by
    unfold u
    have p := WilsonTuranF.fpos K O h1 h10 (i + K' + 1) (i + K' + 1)
    have pm := SU2Strong4D.pw_mono K O h1 y (cN K Y) hy hyY (i + K' + 2)
    rw [← CertSound.cN_pow K Y] at pm
    have w := WilsonTailV.lmul K O _ _ _ (O.mul_nonneg _ _ (cN_nn K O h1 (2 ^ ((i + K' + 2) + (i + K' + 2))))
      (F.inv_pos _ p.1 p.2)) pm
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
    · lib_ring
    · rw [cN_mul K (2 ^ ((i + K' + 2) + (i + K' + 2))) (Y ^ (i + K' + 2))]; lib_ring
  have s1 := Lagrange.sum_le_lt K O _ _ n (fun i _ => term i)
  have s2 := u_sum K O F h1 h10 Y K' hS n
  have un := u_nn K O F h1 h10 Y K' n
  have s3 : O.le (sumN K (fun i => u K O F Y K' i) n) (K.add (u K O F Y K' 0) (u K O F Y K' 0)) := by
    refine O.le_trans _ _ _ ?_ s2
    have := O.add_le_add _ _ _ _ (O.le_refl (sumN K (fun i => u K O F Y K' i) n)) (TopLC.add_nn K O _ _ un un)
    rw [show K.add (sumN K (fun i => u K O F Y K' i) n) K.zero = sumN K (fun i => u K O F Y K' i) n by lib_ring] at this
    exact this
  have s4 : O.le (K.add (u K O F Y K' 0) (u K O F Y K' 0)) K.one := by
    unfold u
    have p := WilsonTuranF.fpos K O h1 h10 (0 + K' + 1) (0 + K' + 1)
    have f := WilsonTailV.frac_le K O F h1 h10 (2 * (2 ^ ((0 + K' + 2) + (0 + K' + 2)) * Y ^ (0 + K' + 2)))
      (fact (0 + K' + 1) * fact (0 + K' + 1)) 1 1 (Nat.mul_le_mul (fact_pos _) (fact_pos _)) (Nat.le_refl 1)
      (by rw [show 0 + K' + 2 = K' + 2 by lib_nat, show 0 + K' + 1 = K' + 1 by lib_nat]
          refine Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans h0 (Nat.le_of_eq (by lib_nat))))
    have i1 : F.inv K.one = K.one := by
      have := F.mul_inv K.one h10
      rw [K.one_mul] at this; exact this
    rw [show cN K 1 = K.one by unfold cN; lib_ring, i1, cN_mul K 2, show cN K 2 = K.add K.one K.one by unfold cN; lib_ring] at f
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ f (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  exact O.le_trans _ _ _ s1 (O.le_trans _ _ _ s3 s4)

end WilsonTailN

#print axioms WilsonTailN.u_nn
#print axioms WilsonTailN.u_half
#print axioms WilsonTailN.u_sum
#print axioms WilsonTailN.tail_ok

ym/WilsonGoodT.lean

/-
  ym/WilsonGoodT — T45, G76e: the truncated Wilson coefficients c_m = cS(t, m, Kk) satisfy, for t² ≤ Y,
    (general VV)  (c_m + c_{m+2}) c_{m+2} ≤ (c_{m+1} + c_{m+3}) c_{m+1}   (tail condition at Kk = K' + 2),
    (base)        c_1² ≤ (c_0 + c_2) c_0                                    (same tail condition),
    (strong VV)   (c_m + c_{m+2}) c_{m+2} ≤ c_{m+1}²                         (2Y ≤ m + 4, 4Y ≤ m + 2),
  obtained from the y-polynomial forms WilsonTailV.vv_poly, WilsonBaseT.base_poly, WilsonStrong.strong_vv by
  c_m = (m+1)·t^m·ev(pL_m)(t²) (WilsonTuranF.cS_eq). Lean 4 core only, no axioms.
-/
import ym.WilsonBaseT
import ym.WilsonStrong
import ym.WilsonGood
import ym.FlowStep

namespace WilsonGoodT

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **general VV for the truncated Wilson coefficients** -/
theorem cs_vv (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (m K' : Nat)
    (htail : O.le (sumN K (fun i => K.mul (K.mul (cN K (2 ^ ((i + K' + 2) + (i + K' + 2))))
        (F.inv (cN K (fact (i + K' + 1) * fact (i + K' + 1))))) (pwR K (K.mul t t) (i + K' + 2))) (K' + 3)) K.one) :
    O.le (K.mul (K.add (cS K O F t m (K' + 2)) (cS K O F t (m + 2) (K' + 2))) (cS K O F t (m + 2) (K' + 2)))
      (K.mul (K.add (cS K O F t (m + 1) (K' + 2)) (cS K O F t (m + 3) (K' + 2))) (cS K O F t (m + 1) (K' + 2))) := by
  have v := WilsonTailV.vv_poly K O F h1 h10 m K' (K.mul t t) (O.mul_nonneg _ _ ht0 ht0) htail
  rw [SU2Wilson2D.ev_mulL, SU2Wilson2D.ev_mulL, SU2Wilson2D.ev_mulL, SU2Wilson2D.ev_mulL,
    cN_mul K (m + 1), cN_mul K (m + 3), cN_mul K (m + 2) (m + 2), cN_mul K (m + 2) (m + 4)] at v
  rw [WilsonTuranF.cS_eq K O F h1 h10 t m, WilsonTuranF.cS_eq K O F h1 h10 t (m + 2),
    WilsonTuranF.cS_eq K O F h1 h10 t (m + 1), WilsonTuranF.cS_eq K O F h1 h10 t (m + 3)]
  unfold WilsonTuranF.A
  show O.le (K.mul (K.add (K.mul (cN K (m + 1)) (K.mul (pwR K t m) (ev K (WilsonTuranF.pL K O F m (K' + 2)) (K.mul t t))))
        (K.mul (cN K (m + 3)) (K.mul (K.mul (K.mul (pwR K t m) t) t) (ev K (WilsonTuranF.pL K O F (m + 2) (K' + 2)) (K.mul t t)))))
      (K.mul (cN K (m + 3)) (K.mul (K.mul (K.mul (pwR K t m) t) t) (ev K (WilsonTuranF.pL K O F (m + 2) (K' + 2)) (K.mul t t)))))
    (K.mul (K.add (K.mul (cN K (m + 2)) (K.mul (K.mul (pwR K t m) t) (ev K (WilsonTuranF.pL K O F (m + 1) (K' + 2)) (K.mul t t))))
        (K.mul (cN K (m + 4)) (K.mul (K.mul (K.mul (K.mul (pwR K t m) t) t) t) (ev K (WilsonTuranF.pL K O F (m + 3) (K' + 2)) (K.mul t t)))))
      (K.mul (cN K (m + 2)) (K.mul (K.mul (pwR K t m) t) (ev K (WilsonTuranF.pL K O F (m + 1) (K' + 2)) (K.mul t t)))))
  have pn := AreaLower.pw_nn K O t ht0 h1 m
  generalize pwR K t m = p at *
  generalize ev K (WilsonTuranF.pL K O F m (K' + 2)) (K.mul t t) = e0 at *
  generalize ev K (WilsonTuranF.pL K O F (m + 1) (K' + 2)) (K.mul t t) = e1 at *
  generalize ev K (WilsonTuranF.pL K O F (m + 2) (K' + 2)) (K.mul t t) = e2 at *
  generalize ev K (WilsonTuranF.pL K O F (m + 3) (K' + 2)) (K.mul t t) = e3 at *
  have w := WilsonTailV.lmul K O (K.mul (K.mul p p) (K.mul t t)) _ _ (O.mul_nonneg _ _ (O.mul_nonneg _ _ pn pn)
    (O.mul_nonneg _ _ ht0 ht0)) v
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · lib_ring

/-- **base condition for the truncated Wilson coefficients** -/
theorem cs_base (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (K' : Nat)
    (htail : O.le (sumN K (fun i => K.mul (K.mul (cN K (2 ^ ((i + K' + 2) + (i + K' + 2))))
        (F.inv (cN K (fact (i + K' + 1) * fact (i + K' + 1))))) (pwR K (K.mul t t) (i + K' + 2))) (K' + 3)) K.one) :
    O.le (K.mul (cS K O F t 1 (K' + 2)) (cS K O F t 1 (K' + 2)))
      (K.mul (K.add (cS K O F t 0 (K' + 2)) (cS K O F t 2 (K' + 2))) (cS K O F t 0 (K' + 2))) := by
  have v := WilsonBaseT.base_poly K O F h1 h10 K' (K.mul t t) (O.mul_nonneg _ _ ht0 ht0) htail
  rw [SU2Wilson2D.ev_mulL, SU2Wilson2D.ev_mulL, SU2Wilson2D.ev_mulL] at v
  rw [WilsonTuranF.cS_eq K O F h1 h10 t 1, WilsonTuranF.cS_eq K O F h1 h10 t 0, WilsonTuranF.cS_eq K O F h1 h10 t 2]
  unfold WilsonTuranF.A
  show O.le (K.mul (K.mul (cN K 2) (K.mul (K.mul K.one t) (ev K (WilsonTuranF.pL K O F 1 (K' + 2)) (K.mul t t))))
        (K.mul (cN K 2) (K.mul (K.mul K.one t) (ev K (WilsonTuranF.pL K O F 1 (K' + 2)) (K.mul t t)))))
    (K.mul (K.add (K.mul (cN K 1) (K.mul K.one (ev K (WilsonTuranF.pL K O F 0 (K' + 2)) (K.mul t t))))
        (K.mul (cN K 3) (K.mul (K.mul (K.mul K.one t) t) (ev K (WilsonTuranF.pL K O F 2 (K' + 2)) (K.mul t t)))))
      (K.mul (cN K 1) (K.mul K.one (ev K (WilsonTuranF.pL K O F 0 (K' + 2)) (K.mul t t)))))
  rw [show cN K 1 = K.one by unfold cN; lib_ring]
  have c4 : cN K 4 = K.mul (cN K 2) (cN K 2) := by rw [← cN_mul K]
  rw [c4] at v
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ v (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · lib_ring

/-- **strong VV for the truncated Wilson coefficients at large index** -/
theorem cs_strong (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (Y m Kk : Nat)
    (hK : 1 ≤ Kk) (hyY : O.le (K.mul t t) (cN K Y)) (hY2 : 2 * Y ≤ m + 4) (hY4 : 4 * Y ≤ m + 2) :
    O.le (K.mul (K.add (cS K O F t m Kk) (cS K O F t (m + 2) Kk)) (cS K O F t (m + 2) Kk))
      (K.mul (cS K O F t (m + 1) Kk) (cS K O F t (m + 1) Kk)) := by
  have v := WilsonStrong.strong_vv K O F h1 h10 Y m Kk hK (K.mul t t) (O.mul_nonneg _ _ ht0 ht0) hyY hY2 hY4
  rw [SU2Wilson2D.ev_mulL, SU2Wilson2D.ev_mulL, SU2Wilson2D.ev_mulL,
    cN_mul K (m + 1), cN_mul K (m + 3), cN_mul K (m + 2) (m + 2)] at v
  rw [WilsonTuranF.cS_eq K O F h1 h10 t m, WilsonTuranF.cS_eq K O F h1 h10 t (m + 2),
    WilsonTuranF.cS_eq K O F h1 h10 t (m + 1)]
  unfold WilsonTuranF.A
  show O.le (K.mul (K.add (K.mul (cN K (m + 1)) (K.mul (pwR K t m) (ev K (WilsonTuranF.pL K O F m Kk) (K.mul t t))))
        (K.mul (cN K (m + 3)) (K.mul (K.mul (K.mul (pwR K t m) t) t) (ev K (WilsonTuranF.pL K O F (m + 2) Kk) (K.mul t t)))))
      (K.mul (cN K (m + 3)) (K.mul (K.mul (K.mul (pwR K t m) t) t) (ev K (WilsonTuranF.pL K O F (m + 2) Kk) (K.mul t t)))))
    (K.mul (K.mul (cN K (m + 2)) (K.mul (K.mul (pwR K t m) t) (ev K (WilsonTuranF.pL K O F (m + 1) Kk) (K.mul t t))))
      (K.mul (cN K (m + 2)) (K.mul (K.mul (pwR K t m) t) (ev K (WilsonTuranF.pL K O F (m + 1) Kk) (K.mul t t)))))
  have pn := AreaLower.pw_nn K O t ht0 h1 m
  generalize pwR K t m = p at *
  generalize ev K (WilsonTuranF.pL K O F m Kk) (K.mul t t) = e0 at *
  generalize ev K (WilsonTuranF.pL K O F (m + 1) Kk) (K.mul t t) = e1 at *
  generalize ev K (WilsonTuranF.pL K O F (m + 2) Kk) (K.mul t t) = e2 at *
  have w := WilsonTailV.lmul K O (K.mul (K.mul p p) (K.mul t t)) _ _ (O.mul_nonneg _ _ (O.mul_nonneg _ _ pn pn)
    (O.mul_nonneg _ _ ht0 ht0)) v
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · lib_ring

/-- scaling an inequality of products by i·i ≥ 0 -/
theorem scale2 (a b c d i : R) (hi : O.le K.zero i) (h : O.le (K.mul a b) (K.mul c d)) :
    O.le (K.mul (K.mul a i) (K.mul b i)) (K.mul (K.mul c i) (K.mul d i)) := by
  have w := O.mul_le_mul_right _ _ (K.mul i i) h (O.mul_nonneg _ _ hi hi)
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

/-- **the normalised truncated Wilson weight wl(t, K'+1, L) is v-good for t² ≤ Y**, given the tail condition at
  truncation K' + 2 and a length L with 3 ≤ L, 2Y ≤ L + 1, 4Y + 1 ≤ L -/
theorem wl_good_big (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero)
    (Y K' L : Nat) (hyY : O.le (K.mul t t) (cN K Y))
    (htail : O.le (sumN K (fun i => K.mul (K.mul (cN K (2 ^ ((i + K' + 2) + (i + K' + 2))))
        (F.inv (cN K (fact (i + K' + 1) * fact (i + K' + 1))))) (pwR K (K.mul t t) (i + K' + 2))) (K' + 3)) K.one)
    (hL3 : 3 ≤ L) (hY2 : 2 * Y ≤ L + 1) (hY4 : 4 * Y + 1 ≤ L) :
    L1Full.Good K O (WilsonCone.wl K O F t (K' + 1) L) := by
  have c0 := WilsonGood.cS_nn K O F h1 h10 t ht0 0 (K' + 2)
  have c0ne := WilsonGood.c_ne K O F h1 h10 t ht0 htne 0 (K' + 1)
  have i0 := F.inv_pos _ c0 c0ne
  have co_in : ∀ n, n < L → co K (WilsonCone.wl K O F t (K' + 1) L) n
      = K.mul (cS K O F t n (K' + 2)) (F.inv (cS K O F t 0 (K' + 2))) := fun n hn => by
    unfold WilsonCone.wl
    rw [RatioStep.co_lmap K _ L 0 n hn, Nat.zero_add]
  have co_out : ∀ n, L ≤ n → co K (WilsonCone.wl K O F t (K' + 1) L) n = K.zero := fun n hn => by
    unfold WilsonCone.wl
    exact RatioStep.co_lmap_ge K _ L 0 n hn
  have gnn : ∀ n, O.le K.zero (K.mul (cS K O F t n (K' + 2)) (F.inv (cS K O F t 0 (K' + 2)))) := fun n =>
    O.mul_nonneg _ _ (WilsonGood.cS_nn K O F h1 h10 t ht0 n (K' + 2)) i0
  have Znn : ∀ n, O.le K.zero (co K (WilsonCone.wl K O F t (K' + 1) L) n) := fun n => by
    cases Nat.lt_or_ge n L with
    | inl h => rw [co_in n h]; exact gnn n
    | inr h => rw [co_out n h]; exact O.le_refl _
  refine ⟨Znn, fun j hj => ?_, fun m => ?_, ?_⟩
  · have hjL : j < L := by
      have := hj; unfold WilsonCone.wl at this; rw [WilsonGood.len_lmap] at this; exact this
    rw [co_in j hjL]
    exact FlowStep.mul_ne K O F h10 _ _ (WilsonGood.c_ne K O F h1 h10 t ht0 htne j (K' + 1)) (OField.inv_ne K O F _ c0ne h10)
  · unfold HalfRatio.VV
    cases Nat.lt_or_ge (m + 2) L with
    | inr h =>
      rw [co_out (m + 2) h, show K.mul (K.add (co K (WilsonCone.wl K O F t (K' + 1) L) m) K.zero) K.zero = K.zero by lib_ring]
      exact O.mul_nonneg _ _ (TopLC.add_nn K O _ _ (Znn _) (Znn _)) (Znn _)
    | inl h =>
      have h1L : m + 1 < L := Nat.lt_of_le_of_lt (Nat.le_succ (m + 1)) h
      have h0L : m < L := Nat.lt_of_le_of_lt (Nat.le_succ m) h1L
      cases Nat.lt_or_ge (m + 3) L with
      | inl h3 =>
        rw [co_in m h0L, co_in (m + 1) h1L, co_in (m + 2) h, co_in (m + 3) h3]
        have v := WilsonGoodT.cs_vv K O F h1 h10 t ht0 m K' htail
        have w := scale2 K O _ _ _ _ _ i0 v
        refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
      | inr h3 =>
        rw [co_in m h0L, co_in (m + 1) h1L, co_in (m + 2) h, co_out (m + 3) h3]
        have hm2 : 2 * Y ≤ m + 4 := Nat.le_trans hY2 (Nat.succ_le_succ h3)
        have hm4 : 4 * Y ≤ m + 2 := by
          have := Nat.le_trans hY4 h3
          exact Nat.le_of_succ_le_succ this
        have v := WilsonGoodT.cs_strong K O F h1 h10 t ht0 Y m (K' + 2) (Nat.succ_le_succ (Nat.zero_le _)) hyY hm2 hm4
        have w := scale2 K O _ _ _ _ _ i0 v
        refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  · have h2 : 2 < L := hL3
    rw [co_in 1 (Nat.lt_trans (Nat.lt_succ_self 1) h2), co_in 0 (Nat.lt_trans (Nat.zero_lt_succ 1) h2), co_in 2 h2]
    have v := cs_base K O F h1 h10 t ht0 K' htail
    have w := scale2 K O _ _ _ _ _ i0 v
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

end WilsonGoodT

#print axioms WilsonGoodT.cs_vv
#print axioms WilsonGoodT.cs_base
#print axioms WilsonGoodT.cs_strong
#print axioms WilsonGoodT.scale2
#print axioms WilsonGoodT.wl_good_big

ym/WilsonGood4.lean

/-
  ym/WilsonGood4 — T45, G70 for β ≤ 8, 16, 64: the normalised truncated SU(2) Wilson weight wl(t, 28, L) is v-good for
  every 0 < t with t² ≤ 16 (β = 2t ≤ 8) and every length L ≥ 65. The tail condition of WilsonGoodT.wl_good_big at
  K' = 27 (truncation 29) is discharged by WilsonTailN.tail_ok with the two numeric checks
  8·16 ≤ 29² and 2·2^{58}·16^{29} ≤ (28!)². Lean 4 core only, no axioms.
-/
import ym.WilsonGoodT

namespace WilsonGood4

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem num1 : 8 * 16 ≤ (27 + 2) * (27 + 2) := by decide

theorem num2 : 2 * (2 ^ ((27 + 2) + (27 + 2)) * 16 ^ (27 + 2)) ≤ fact (27 + 1) * fact (27 + 1) := by decide

/-- **the truncated Wilson weight is v-good for β ≤ 8** (truncation 29, length L ≥ 65) -/
theorem wl_good8 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero)
    (ht : O.le (K.mul t t) (cN K 16)) (L : Nat) (hL : 65 ≤ L) :
    L1Full.Good K O (WilsonCone.wl K O F t 28 L) := by
  have tail := WilsonTailN.tail_ok K O F h1 h10 16 27 (27 + 3) (K.mul t t) (O.mul_nonneg _ _ ht0 ht0) ht num1 num2
  exact WilsonGoodT.wl_good_big K O F h1 h10 t ht0 htne 16 27 L ht tail
    (Nat.le_trans (by decide) hL) (Nat.le_trans (by decide) (Nat.le_succ_of_le hL)) hL

theorem num3 : 8 * 64 ≤ (78 + 2) * (78 + 2) := by decide

theorem num4 : 2 * (2 ^ ((78 + 2) + (78 + 2)) * 64 ^ (78 + 2)) ≤ fact (78 + 1) * fact (78 + 1) := by decide

/-- **the truncated Wilson weight is v-good for β ≤ 16** (truncation 80, length L ≥ 257) -/
theorem wl_good16 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero)
    (ht : O.le (K.mul t t) (cN K 64)) (L : Nat) (hL : 257 ≤ L) :
    L1Full.Good K O (WilsonCone.wl K O F t 79 L) := by
  have tail := WilsonTailN.tail_ok K O F h1 h10 64 78 (78 + 3) (K.mul t t) (O.mul_nonneg _ _ ht0 ht0) ht num3 num4
  exact WilsonGoodT.wl_good_big K O F h1 h10 t ht0 htne 64 78 L ht tail
    (Nat.le_trans (by decide) hL) (Nat.le_trans (by decide) (Nat.le_succ_of_le hL)) hL

theorem num5 : 8 * 1024 ≤ (318 + 2) * (318 + 2) := by decide

set_option maxRecDepth 100000 in
theorem num6 : 2 * (2 ^ ((318 + 2) + (318 + 2)) * 1024 ^ (318 + 2)) ≤ fact (318 + 1) * fact (318 + 1) := by decide

/-- **the truncated Wilson weight is v-good for β ≤ 64** (truncation 320, length L ≥ 4097) -/
theorem wl_good64 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero)
    (ht : O.le (K.mul t t) (cN K 1024)) (L : Nat) (hL : 4097 ≤ L) :
    L1Full.Good K O (WilsonCone.wl K O F t 319 L) := by
  have tail := WilsonTailN.tail_ok K O F h1 h10 1024 318 (318 + 3) (K.mul t t) (O.mul_nonneg _ _ ht0 ht0) ht num5 num6
  exact WilsonGoodT.wl_good_big K O F h1 h10 t ht0 htne 1024 318 L ht tail
    (Nat.le_trans (by decide) hL) (Nat.le_trans (by decide) (Nat.le_succ_of_le hL)) hL

end WilsonGood4

#print axioms WilsonGood4.num1
#print axioms WilsonGood4.num2
#print axioms WilsonGood4.wl_good8
#print axioms WilsonGood4.wl_good16
#print axioms WilsonGood4.wl_good64
#print axioms WilsonGood4.num3
#print axioms WilsonGood4.num4
#print axioms WilsonGood4.num5
#print axioms WilsonGood4.num6

#print axioms: 30 theorems, no axioms.

90.136 Wilson MK flow up to \(\beta =64\)

Every MK flow iterate of the truncated Wilson weight stays v-good with nonincreasing \(a\).

\[ \mathrm {mk}^k(w_t)\ \text {v-good},\quad a\ \text {nonincreasing}\qquad (t\le 32,\ k\ge 0) \]

ym/WilsonANonT.lean

/-
  ym/WilsonANonT — T45, G76f: the truncated Wilson weight has a_n = c_n/(n+1) nonincreasing, A_{n+1} ≤ A_n,
  for every t ≥ 0 with 2t ≤ Kk + 1 (truncation Kk + 1 terms ... k < Kk + 1), every n.
  With P_k = α(m,k)y^k (y = t²) and N_k = t·α(m+1,k)y^k = P_k·t/(m+k+2):
    2N_k ≤ P_k + P_{k+1}   (AM-GM 2t(k+1) ≤ (k+1)² + t², and α(m,k+1)(k+1) = α(m+1,k)),
    2N_K ≤ P_K             (last index K = Kk, from 2t ≤ K + 1 ≤ m + K + 2),
  and the telescoping invariant 2Σ_{k<j}N_k + P_j ≤ 2Σ_{k≤j}P_k gives Σ N ≤ Σ P. Lean 4 core only, no axioms.
-/
import ym.WilsonANon
import ym.CBLib
import ym.GridQ
import ym.TopLC

namespace WilsonANonT

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- α(m, k+1)·(k+1) = α(m+1, k) -/
theorem alpha_succ (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (m k : Nat) :
    K.mul (WilsonTuranF.α K O F m (k + 1)) (cN K (k + 1)) = WilsonTuranF.α K O F (m + 1) k := by
  unfold WilsonTuranF.α
  have hD := WilsonTuranF.fpos K O h1 h10 k (m + 1 + k + 1)
  have hc := pos_of_ge_one K O h1 h10 (k + 1) (Nat.le_add_left 1 k)
  have e : cN K (fact (k + 1) * fact (m + (k + 1) + 1)) = K.mul (cN K (fact k * fact (m + 1 + k + 1))) (cN K (k + 1)) := by
    rw [show m + (k + 1) + 1 = m + 1 + k + 1 by lib_nat, ← cN_mul K]
    show cN K (fact k * (k + 1) * fact (m + 1 + k + 1)) = _
    rw [show fact k * (k + 1) * fact (m + 1 + k + 1) = fact k * fact (m + 1 + k + 1) * (k + 1) by lib_nat]
  rw [e]
  have hDc : K.mul (cN K (fact k * fact (m + 1 + k + 1))) (cN K (k + 1)) ≠ K.zero :=
    FlowStep.mul_ne K O F h10 _ _ hD.2 hc.2
  generalize cN K (fact k * fact (m + 1 + k + 1)) = D at hD hDc ⊢
  generalize cN K (k + 1) = c at hc hDc ⊢
  have i1 := F.mul_inv _ hDc
  have i2 := F.mul_inv _ hD.2
  have s : K.mul (F.inv (K.mul D c)) c
      = K.mul (K.mul (F.inv (K.mul D c)) c) (K.mul D (F.inv D)) := by rw [i2]; lib_ring
  rw [s, show K.mul (K.mul (F.inv (K.mul D c)) c) (K.mul D (F.inv D))
      = K.mul (K.mul (K.mul D c) (F.inv (K.mul D c))) (F.inv D) by lib_ring, i1]
  lib_ring

/-- AM-GM: 2t·c ≤ c² + t² -/
theorem amgm (t c : R) : O.le (K.mul (K.add t t) c) (K.add (K.mul c c) (K.mul t t)) := by
  have s := CBLib.sq_nn K O (K.add t (K.neg c))
  have a := O.add_le_add _ _ _ _ s (O.le_refl (K.mul (K.add t t) c))
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ a (TripleE.le_of_eq' K O _ _ ?_))
  · lib_ring
  · lib_ring

/-- 2N_k ≤ P_k + P_{k+1} -/
theorem pair (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (m k : Nat) :
    O.le (K.add (K.mul t (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k)))
          (K.mul t (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k))))
      (K.add (K.mul (WilsonTuranF.α K O F m k) (pwR K (K.mul t t) k))
        (K.mul (WilsonTuranF.α K O F m (k + 1)) (pwR K (K.mul t t) (k + 1)))) := by
  have hc := pos_of_ge_one K O h1 h10 (k + 1) (Nat.le_add_left 1 k)
  refine GridQ.cancel K O F (cN K (k + 1)) _ _ hc.1 hc.2 ?_
  have a0 : O.le K.zero (WilsonTuranF.α K O F (m + 1) k) := by
    unfold WilsonTuranF.α
    have hD := WilsonTuranF.fpos K O h1 h10 k (m + 1 + k + 1)
    exact F.inv_pos _ hD.1 hD.2
  have aY := O.mul_nonneg _ _ a0 (AreaLower.pw_nn K O _ (O.mul_nonneg _ _ ht0 ht0) h1 k)
  have am := amgm K O t (cN K (k + 1))
  have w := O.mul_le_mul_right _ _ _ am aY
  have cd := cN_mono K O h1 (k + 1) (m + k + 2) (by lib_nat_le)
  have w2 := O.mul_le_mul_right _ _ (K.mul (cN K (k + 1)) (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k))) cd
    (O.mul_nonneg _ _ (cN_nn K O h1 _) aY)
  have st := WilsonANon.alpha_step K O F h1 h10 m k
  have su := alpha_succ K O F h1 h10 m k
  have w3 := O.add_le_add _ _ _ _ w2 (O.le_refl (K.mul (K.mul t t) (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k))))
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
    (O.le_trans _ _ _ w3 (TripleE.le_of_eq' K O _ _ ?_))))
  · lib_ring
  · lib_ring
  · rw [← st, show pwR K (K.mul t t) (k + 1) = K.mul (pwR K (K.mul t t) k) (K.mul t t) from rfl]
    rw [show K.mul (cN K (k + 1)) (K.add (K.mul (K.mul (WilsonTuranF.α K O F (m + 1) k) (cN K (m + k + 2))) (pwR K (K.mul t t) k))
        (K.mul (WilsonTuranF.α K O F m (k + 1)) (K.mul (pwR K (K.mul t t) k) (K.mul t t))))
      = K.add (K.mul (cN K (k + 1)) (K.mul (K.mul (WilsonTuranF.α K O F (m + 1) k) (cN K (m + k + 2))) (pwR K (K.mul t t) k)))
        (K.mul (K.mul (WilsonTuranF.α K O F m (k + 1)) (cN K (k + 1))) (K.mul (pwR K (K.mul t t) k) (K.mul t t))) by lib_ring, su]
    lib_ring

/-- the last index: 2N_k ≤ P_k when 2t ≤ k + 2 -/
theorem last (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (m k : Nat)
    (ht : O.le (K.add t t) (cN K (k + 2))) :
    O.le (K.add (K.mul t (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k)))
          (K.mul t (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k))))
      (K.mul (WilsonTuranF.α K O F m k) (pwR K (K.mul t t) k)) := by
  have a0 : O.le K.zero (WilsonTuranF.α K O F (m + 1) k) := by
    unfold WilsonTuranF.α
    have hD := WilsonTuranF.fpos K O h1 h10 k (m + 1 + k + 1)
    exact F.inv_pos _ hD.1 hD.2
  have aY := O.mul_nonneg _ _ a0 (AreaLower.pw_nn K O _ (O.mul_nonneg _ _ ht0 ht0) h1 k)
  have d := O.le_trans _ _ _ ht (cN_mono K O h1 (k + 2) (m + k + 2) (by lib_nat_le))
  have w := O.mul_le_mul_right _ _ _ d aY
  rw [← WilsonANon.alpha_step K O F h1 h10 m k]
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring

/-- the telescoping invariant for abstract sequences: 2Σ_{k<j}N_k + P_j ≤ 2Σ_{k<j+1}P_k -/
theorem inv_abs (Nf Pf : Nat → R) (hpair : ∀ k, O.le (K.add (Nf k) (Nf k)) (K.add (Pf k) (Pf (k + 1))))
    (hP0 : O.le K.zero (Pf 0)) : ∀ j,
    O.le (K.add (K.add (sumN K Nf j) (sumN K Nf j)) (Pf j)) (K.add (sumN K Pf (j + 1)) (sumN K Pf (j + 1)))
  | 0 => by
    show O.le (K.add (K.add K.zero K.zero) (Pf 0)) (K.add (K.add K.zero (Pf 0)) (K.add K.zero (Pf 0)))
    have := O.add_le_add _ _ _ _ (O.le_refl (Pf 0)) hP0
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ ?_)) <;> lib_ring
  | j + 1 => by
    have ih := inv_abs Nf Pf hpair hP0 j
    have a1 := O.add_le_add _ _ _ _ (O.le_refl (K.add (K.add (sumN K Nf j) (sumN K Nf j)) (Pf (j + 1)))) (hpair j)
    have a2 := O.add_le_add _ _ _ _ ih (O.le_refl (K.add (Pf (j + 1)) (Pf (j + 1))))
    show O.le (K.add (K.add (K.add (sumN K Nf j) (Nf j)) (K.add (sumN K Nf j) (Nf j))) (Pf (j + 1)))
      (K.add (K.add (sumN K Pf (j + 1)) (Pf (j + 1))) (K.add (sumN K Pf (j + 1)) (Pf (j + 1))))
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ a1 (O.le_trans _ _ _
      (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ a2 (TripleE.le_of_eq' K O _ _ ?_)))) <;> lib_ring

/-- Σ_{k<K'+1} N ≤ Σ_{k<K'+1} P from the pair bounds and the last bound -/
theorem sum_abs (F' : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Nf Pf : Nat → R) (K' : Nat)
    (hpair : ∀ k, O.le (K.add (Nf k) (Nf k)) (K.add (Pf k) (Pf (k + 1)))) (hP0 : O.le K.zero (Pf 0))
    (hlast : O.le (K.add (Nf K') (Nf K')) (Pf K')) : O.le (sumN K Nf (K' + 1)) (sumN K Pf (K' + 1)) := by
  have iv := inv_abs K O Nf Pf hpair hP0 K'
  have a := O.add_le_add _ _ _ _ (O.le_refl (K.add (sumN K Nf K') (sumN K Nf K'))) hlast
  have two : O.le (K.mul (K.add K.one K.one) (sumN K Nf (K' + 1))) (K.mul (K.add K.one K.one) (sumN K Pf (K' + 1))) := by
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ a (O.le_trans _ _ _ iv (TripleE.le_of_eq' K O _ _ ?_)))
    · show K.mul (K.add K.one K.one) (K.add (sumN K Nf K') (Nf K')) = _
      lib_ring
    · lib_ring
  have p2 : O.le K.zero (K.add K.one K.one) := TopLC.add_nn K O _ _ h1 h1
  have n2 : K.add K.one K.one ≠ K.zero := by
    have := (pos_of_ge_one K O h1 h10 2 (Nat.le_succ 1)).2
    rw [show cN K 2 = K.add K.one K.one by unfold cN; lib_ring] at this
    exact this
  exact GridQ.cancel K O F' _ _ _ p2 n2 two

/-- **A_{m+1} ≤ A_m for 0 ≤ t, 2t ≤ Kk + 2, truncation Kk + 1** -/
theorem A_anti_big (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (m Kk : Nat)
    (ht : O.le (K.add t t) (cN K (Kk + 2))) :
    O.le (WilsonTuranF.A K O F t (m + 1) (Kk + 1)) (WilsonTuranF.A K O F t m (Kk + 1)) := by
  unfold WilsonTuranF.A
  rw [WilsonTuranF.ev_pL, WilsonTuranF.ev_pL]
  have p0 : O.le K.zero (K.mul (WilsonTuranF.α K O F m 0) (pwR K (K.mul t t) 0)) := by
    unfold WilsonTuranF.α
    have hD := WilsonTuranF.fpos K O h1 h10 0 (m + 0 + 1)
    exact O.mul_nonneg _ _ (F.inv_pos _ hD.1 hD.2) (AreaLower.pw_nn K O _ (O.mul_nonneg _ _ ht0 ht0) h1 0)
  have half := sum_abs K O F h1 h10
    (fun k => K.mul t (K.mul (WilsonTuranF.α K O F (m + 1) k) (pwR K (K.mul t t) k)))
    (fun k => K.mul (WilsonTuranF.α K O F m k) (pwR K (K.mul t t) k)) Kk
    (fun k => pair K O F h1 h10 t ht0 m k) p0 (last K O F h1 h10 t ht0 m Kk ht)
  have tm := AreaLower.pw_nn K O t ht0 h1 m
  have w := O.mul_le_mul_right _ _ (pwR K t m) half tm
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
  · show K.mul (K.mul (pwR K t m) t) _ = _
    rw [sumN_mul]
    lib_ring
  · lib_ring

/-- **a_n nonincreasing for the truncated Wilson weight, 2t ≤ Kk + 2** -/
theorem wilson_anoninc_big (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (Kk n : Nat) (ht : O.le (K.add t t) (cN K (Kk + 2))) :
    O.le (K.mul (cS K O F t (n + 1) (Kk + 1)) (cN K (n + 1))) (K.mul (cS K O F t n (Kk + 1)) (cN K (n + 2))) := by
  rw [WilsonTuranF.cS_eq K O F h1 h10 t (n + 1) (Kk + 1), WilsonTuranF.cS_eq K O F h1 h10 t n (Kk + 1)]
  have a := A_anti_big K O F h1 h10 t ht0 n Kk ht
  have c := O.mul_nonneg _ _ (cN_nn K O h1 (n + 1)) (cN_nn K O h1 (n + 2))
  have w := O.mul_le_mul_right _ _ _ a c
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
  · show K.mul (K.mul (cN K (n + 2)) (WilsonTuranF.A K O F t (n + 1) (Kk + 1))) (cN K (n + 1)) = _
    lib_ring
  · lib_ring

end WilsonANonT

#print axioms WilsonANonT.alpha_succ
#print axioms WilsonANonT.amgm
#print axioms WilsonANonT.pair
#print axioms WilsonANonT.last
#print axioms WilsonANonT.inv_abs
#print axioms WilsonANonT.sum_abs
#print axioms WilsonANonT.A_anti_big
#print axioms WilsonANonT.wilson_anoninc_big

ym/WilsonFlowBig.lean

/-
  ym/WilsonFlowBig — T45, G70 for β ≤ 8 (flow class): for 0 < t ≤ 4 the MK flow started from the normalised
  truncated Wilson weight wl(t, 28, M + 2) (truncation 29, M + 2 ≥ 65) stays v-good with a_n nonincreasing at
  every step. Ingredients: WilsonGood4.wl_good8, WilsonANonT.wilson_anoninc_big (2t ≤ 30), FlowStep.flow_step.
  Lean 4 core only, no axioms.
-/
import ym.WilsonGood4
import ym.WilsonANonT
import ym.WilsonFlow

namespace WilsonFlowBig

open YMChain LinkIntegral SU2Char QBinom HeatPair SU2CharExp

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- the normalised Wilson start has a nonincreasing, 2t ≤ Kk + 2 -/
theorem wl_anoninc_big (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (htne : t ≠ K.zero) (Kk L : Nat) (ht : O.le (K.add t t) (cN K (Kk + 2))) :
    FlowStep.ANoninc K O (WilsonCone.wl K O F t Kk L) := by
  have c0 := WilsonGood.cS_nn K O F h1 h10 t ht0 0 (Kk + 1)
  have i0 := F.inv_pos _ c0 (WilsonGood.c_ne K O F h1 h10 t ht0 htne 0 Kk)
  intro k
  cases Nat.lt_or_ge (k + 1) L with
  | inr h =>
    unfold WilsonCone.wl
    rw [RatioStep.co_lmap_ge K _ L 0 (k + 1) h, show K.mul K.zero (cN K (k + 1)) = K.zero by lib_ring]
    refine O.mul_nonneg _ _ ?_ (cN_nn K O h1 (k + 2))
    cases Nat.lt_or_ge k L with
    | inl h' =>
      rw [RatioStep.co_lmap K _ L 0 k h', Nat.zero_add]
      exact O.mul_nonneg _ _ (WilsonGood.cS_nn K O F h1 h10 t ht0 k (Kk + 1)) i0
    | inr h' => rw [RatioStep.co_lmap_ge K _ L 0 k h']; exact O.le_refl _
  | inl h =>
    unfold WilsonCone.wl
    rw [RatioStep.co_lmap K _ L 0 (k + 1) h, RatioStep.co_lmap K _ L 0 k (Nat.lt_of_succ_lt h),
      Nat.zero_add, Nat.zero_add]
    have a := WilsonANonT.wilson_anoninc_big K O F h1 h10 t ht0 Kk k ht
    have w := O.mul_le_mul_right _ _ (F.inv (cS K O F t 0 (Kk + 1))) a i0
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ ?_))
    · lib_ring
    · lib_ring

/-- t ≤ 4 ⇒ t² ≤ 16 and 2t ≤ 30 -/
theorem t4_bounds (h1 : O.le K.zero K.one) (t : R) (ht0 : O.le K.zero t) (ht4 : O.le t (cN K 4)) :
    O.le (K.mul t t) (cN K 16) ∧ O.le (K.add t t) (cN K (28 + 2)) := by
  have c4 := cN_nn K O h1 4
  have a := O.mul_le_mul_right _ _ t ht4 ht0
  have b := O.mul_le_mul_right _ _ (cN K 4) ht4 c4
  have e1 : K.mul (cN K 4) t = K.mul t (cN K 4) := by lib_ring
  have e2 : K.mul (cN K 4) (cN K 4) = cN K 16 := by rw [← cN_mul K]
  refine ⟨O.le_trans _ _ _ a (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ e1) (O.le_trans _ _ _ b
    (TripleE.le_of_eq' K O _ _ e2))), ?_⟩
  have s := O.add_le_add _ _ _ _ ht4 ht4
  have e3 : K.add (cN K 4) (cN K 4) = cN K (4 + 4) := by rw [← cN_add K]
  exact O.le_trans _ _ _ s (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ e3) (cN_mono K O h1 (4 + 4) (28 + 2) (by decide)))

/-- **the Wilson MK flow stays in the class at every step for 0 < t ≤ 4 (β ≤ 8)**, start length M + 2 ≥ 65 -/
theorem wilson_flow8 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht4 : O.le t (cN K 4)) (htne : t ≠ K.zero) (M : Nat) (hM : 63 ≤ M) :
    ∀ k, L1Full.Good K O (FlowIter.itX K O F (WilsonCone.wl K O F t 28 (M + 2)) M k) ∧
      FlowStep.ANoninc K O (FlowIter.itX K O F (WilsonCone.wl K O F t 28 (M + 2)) M k) ∧
      (FlowIter.itX K O F (WilsonCone.wl K O F t 28 (M + 2)) M k).length = FlowIter.itM M k + 2
  | 0 => by
    have b := t4_bounds K O h1 t ht0 ht4
    exact ⟨WilsonGood4.wl_good8 K O F h1 h10 t ht0 htne b.1 (M + 2) (Nat.succ_le_succ (Nat.succ_le_succ hM)),
      wl_anoninc_big K O F h1 h10 t ht0 htne 28 (M + 2) b.2, by
        show (WilsonCone.wl K O F t 28 (M + 2)).length = M + 2
        unfold WilsonCone.wl; exact WilsonGood.len_lmap _ 0 (M + 2)⟩
  | k + 1 => by
    have ⟨hg, ha, hl⟩ := wilson_flow8 h1 h10 t ht0 ht4 htne M hM k
    have fs := FlowStep.flow_step K O F h1 h10 _ hg _ hl ha
    refine ⟨fs.1, fs.2, ?_⟩
    show (FlowStep.mk K O F _ (FlowIter.itM M k)).length = 4 * FlowIter.itM M k + 3 + 2
    exact FlowStep.len_mk K O F _ _

/-- t ≤ T ⇒ t² ≤ T² and 2t ≤ 2T (T a natural number) -/
theorem tN_bounds (h1 : O.le K.zero K.one) (T : Nat) (t : R) (ht0 : O.le K.zero t) (htT : O.le t (cN K T)) :
    O.le (K.mul t t) (cN K (T * T)) ∧ O.le (K.add t t) (cN K (T + T)) := by
  have cT := cN_nn K O h1 T
  have a := O.mul_le_mul_right _ _ t htT ht0
  have b := O.mul_le_mul_right _ _ (cN K T) htT cT
  have e1 : K.mul (cN K T) t = K.mul t (cN K T) := by lib_ring
  have e2 : K.mul (cN K T) (cN K T) = cN K (T * T) := by rw [← cN_mul K]
  refine ⟨O.le_trans _ _ _ a (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ e1) (O.le_trans _ _ _ b
    (TripleE.le_of_eq' K O _ _ e2))), ?_⟩
  have s := O.add_le_add _ _ _ _ htT htT
  exact O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ (by rw [← cN_add K]))

/-- **β ≤ 16**: the Wilson MK flow (truncation 80, start length ≥ 257) stays in the class at every step -/
theorem wilson_flow16 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht8 : O.le t (cN K 8)) (htne : t ≠ K.zero) (M : Nat) (hM : 255 ≤ M) :
    ∀ k, L1Full.Good K O (FlowIter.itX K O F (WilsonCone.wl K O F t 79 (M + 2)) M k) ∧
      FlowStep.ANoninc K O (FlowIter.itX K O F (WilsonCone.wl K O F t 79 (M + 2)) M k) ∧
      (FlowIter.itX K O F (WilsonCone.wl K O F t 79 (M + 2)) M k).length = FlowIter.itM M k + 2
  | 0 => by
    have b := tN_bounds K O h1 8 t ht0 ht8
    exact ⟨WilsonGood4.wl_good16 K O F h1 h10 t ht0 htne b.1 (M + 2) (Nat.succ_le_succ (Nat.succ_le_succ hM)),
      wl_anoninc_big K O F h1 h10 t ht0 htne 79 (M + 2) (O.le_trans _ _ _ b.2 (cN_mono K O h1 _ _ (by decide))), by
        show (WilsonCone.wl K O F t 79 (M + 2)).length = M + 2
        unfold WilsonCone.wl; exact WilsonGood.len_lmap _ 0 (M + 2)⟩
  | k + 1 => by
    have ⟨hg, ha, hl⟩ := wilson_flow16 h1 h10 t ht0 ht8 htne M hM k
    have fs := FlowStep.flow_step K O F h1 h10 _ hg _ hl ha
    refine ⟨fs.1, fs.2, ?_⟩
    show (FlowStep.mk K O F _ (FlowIter.itM M k)).length = 4 * FlowIter.itM M k + 3 + 2
    exact FlowStep.len_mk K O F _ _

/-- **β ≤ 64**: the Wilson MK flow (truncation 320, start length ≥ 4097) stays in the class at every step -/
theorem wilson_flow64 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht32 : O.le t (cN K 32)) (htne : t ≠ K.zero) (M : Nat) (hM : 4095 ≤ M) :
    ∀ k, L1Full.Good K O (FlowIter.itX K O F (WilsonCone.wl K O F t 319 (M + 2)) M k) ∧
      FlowStep.ANoninc K O (FlowIter.itX K O F (WilsonCone.wl K O F t 319 (M + 2)) M k) ∧
      (FlowIter.itX K O F (WilsonCone.wl K O F t 319 (M + 2)) M k).length = FlowIter.itM M k + 2
  | 0 => by
    have b := tN_bounds K O h1 32 t ht0 ht32
    exact ⟨WilsonGood4.wl_good64 K O F h1 h10 t ht0 htne b.1 (M + 2) (Nat.succ_le_succ (Nat.succ_le_succ hM)),
      wl_anoninc_big K O F h1 h10 t ht0 htne 319 (M + 2) (O.le_trans _ _ _ b.2 (cN_mono K O h1 _ _ (by decide))), by
        show (WilsonCone.wl K O F t 319 (M + 2)).length = M + 2
        unfold WilsonCone.wl; exact WilsonGood.len_lmap _ 0 (M + 2)⟩
  | k + 1 => by
    have ⟨hg, ha, hl⟩ := wilson_flow64 h1 h10 t ht0 ht32 htne M hM k
    have fs := FlowStep.flow_step K O F h1 h10 _ hg _ hl ha
    refine ⟨fs.1, fs.2, ?_⟩
    show (FlowStep.mk K O F _ (FlowIter.itM M k)).length = 4 * FlowIter.itM M k + 3 + 2
    exact FlowStep.len_mk K O F _ _

end WilsonFlowBig

#print axioms WilsonFlowBig.wl_anoninc_big
#print axioms WilsonFlowBig.t4_bounds
#print axioms WilsonFlowBig.wilson_flow8
#print axioms WilsonFlowBig.tN_bounds
#print axioms WilsonFlowBig.wilson_flow16
#print axioms WilsonFlowBig.wilson_flow64

#print axioms: 14 theorems, no axioms.

90.137 Shooting comparison of ratio sequences

A ratio sequence with the VV inequality stays below one with the VV equality.

\[ z_{n+1}(1+z_nz_{n+1})\le z_n(1+z_{n+1}z_{n+2}) \]

ym/TailShoot.lean

/-
  ym/TailShoot — T45, G78g: comparison of ratio sequences by forward shooting. For ratios a = y_m, b = y_{m+1},
  c = y_{m+2} of a v-good list the VV condition reads b(1 + ab) ≤ a(1 + bc); a reference built by VV equality has
  b′(1 + a′b′) = a′(1 + b′c′). If the differences do not decrease at (m, m+1) — b − b′ ≥ a − a′ and b − b′ ≥ 0 — and
  the ratios decrease (b ≤ a, b′ ≤ a′), then c − c′ ≥ b − b′. Iterated from the first index where y exceeds the
  reference, this forces y > reference at the end, where both vanish. Lean 4 core only, no axioms.
-/
import ym.GeoTail
import lib.NatLe

namespace TailShoot

open YMChain LinkIntegral SU2Char HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- **one shooting step** -/
theorem shoot_step (a a' b b' c c' : R) (ha : O.le K.zero a) (ha' : O.le K.zero a') (hb : O.le K.zero b)
    (hb' : O.le K.zero b') (hpos : K.mul (K.mul a b) (K.mul a' b') ≠ K.zero)
    (hY : O.le (K.mul b (K.add K.one (K.mul a b))) (K.mul a (K.add K.one (K.mul b c))))
    (hZ : K.mul b' (K.add K.one (K.mul a' b')) = K.mul a' (K.add K.one (K.mul b' c')))
    (hba : O.le b a) (hba' : O.le b' a')
    (hd : O.le (K.add a (K.neg a')) (K.add b (K.neg b'))) (hd0 : O.le K.zero (K.add b (K.neg b'))) :
    O.le (K.add b (K.neg b')) (K.add c (K.neg c')) := by
  have P0 : O.le K.zero (K.mul (K.mul a b) (K.mul a' b')) :=
    O.mul_nonneg _ _ (O.mul_nonneg _ _ ha hb) (O.mul_nonneg _ _ ha' hb')
  refine O.pos_cancel _ _ _ P0 hpos ?_
  -- key: a a′ (b − b′) ≥ b b′ (a − a′)
  have aa0 : O.le K.zero (K.mul a a') := O.mul_nonneg _ _ ha ha'
  have bb0 : O.le K.zero (K.mul b b') := O.mul_nonneg _ _ hb hb'
  have bbaa : O.le (K.mul b b') (K.mul a a') := by
    have x := O.mul_le_mul_right _ _ b' hba hb'
    have y := O.mul_le_mul_right _ _ a hba' ha
    exact O.le_trans _ _ _ x (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ y
      (TripleE.le_of_eq' K O _ _ (by lib_ring))))
  have key : O.le (K.mul (K.mul b b') (K.add a (K.neg a'))) (K.mul (K.mul a a') (K.add b (K.neg b'))) := by
    cases O.le_total (K.add a (K.neg a')) K.zero with
    | inl hneg =>
      have l : O.le (K.mul (K.mul b b') (K.add a (K.neg a'))) K.zero := by
        have := O.mul_le_mul_right _ _ (K.mul b b') hneg bb0
        exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring)))
      exact O.le_trans _ _ _ l (O.mul_nonneg _ _ aa0 hd0)
    | inr hpos0 =>
      have x := O.mul_le_mul_right _ _ (K.add a (K.neg a')) bbaa hpos0
      have y := O.mul_le_mul_right _ _ (K.mul a a') hd aa0
      exact O.le_trans _ _ _ x (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ y
        (TripleE.le_of_eq' K O _ _ (by lib_ring))))
  -- (A) VV for y times a′b′, plus the key; then subtract the reference identity (B) times ab
  have yA := O.mul_le_mul_right _ _ (K.mul a' b') hY (O.mul_nonneg _ _ ha' hb')
  have s := O.add_le_add _ _ _ _ yA key
  have B : K.mul (K.mul b' (K.add K.one (K.mul a' b'))) (K.mul a b)
      = K.mul (K.mul a' (K.add K.one (K.mul b' c'))) (K.mul a b) := by rw [hZ]
  have s2 := O.add_le_add _ _ _ _ s (O.le_refl (K.neg (K.mul (K.mul b' (K.add K.one (K.mul a' b'))) (K.mul a b))))
  have e2 : K.add (K.add (K.mul (K.mul a (K.add K.one (K.mul b c))) (K.mul a' b')) (K.mul (K.mul a a') (K.add b (K.neg b'))))
      (K.neg (K.mul (K.mul b' (K.add K.one (K.mul a' b'))) (K.mul a b)))
      = K.add (K.add (K.mul (K.mul a (K.add K.one (K.mul b c))) (K.mul a' b')) (K.mul (K.mul a a') (K.add b (K.neg b'))))
      (K.neg (K.mul (K.mul a' (K.add K.one (K.mul b' c'))) (K.mul a b))) := by rw [B]
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s2 (O.le_trans _ _ _
    (TripleE.le_of_eq' K O _ _ e2) (TripleE.le_of_eq' K O _ _ ?_)))
  · lib_ring
  · lib_ring

/-- once the difference starts to grow it keeps growing up to E -/
theorem run (d : Nat → R) (E : Nat)
    (hstep : ∀ m, m + 2 ≤ E → O.le (d m) (d (m + 1)) → O.le K.zero (d (m + 1)) → O.le (d (m + 1)) (d (m + 2)))
    (m : Nat) (h0 : O.le (d m) (d (m + 1))) (h1 : O.le K.zero (d (m + 1))) :
    ∀ j, m + 1 + j ≤ E → O.le (d (m + 1)) (d (m + 1 + j)) ∧ O.le (d (m + j)) (d (m + 1 + j))
  | 0, _ => ⟨O.le_refl _, h0⟩
  | j + 1, hj => by
    have ih := run d E hstep m h0 h1 j (Nat.le_trans (Nat.le_succ _) hj)
    have nn : O.le K.zero (d (m + 1 + j)) := O.le_trans _ _ _ h1 ih.1
    have st := hstep (m + j) (by
      have : m + j + 2 = m + 1 + (j + 1) := by lib_nat
      rw [this]; exact hj) (by
      have e : m + j + 1 = m + 1 + j := by lib_nat
      rw [e]; exact ih.2) (by
      have e : m + j + 1 = m + 1 + j := by lib_nat
      rw [e]; exact nn)
    have e1 : m + j + 1 = m + 1 + j := by lib_nat
    have e2 : m + j + 2 = m + 1 + (j + 1) := by lib_nat
    rw [e1, e2] at st
    exact ⟨O.le_trans _ _ _ ih.1 st, by rw [show m + (j + 1) = m + 1 + j by lib_nat]; exact st⟩

/-- **the comparison**: d_N ≤ 0, d_E ≤ 0 and the shooting step give d_n ≤ 0 for every N ≤ n ≤ E -/
theorem tail_le (d : Nat → R) (N E : Nat)
    (hstep : ∀ m, m + 2 ≤ E → O.le (d m) (d (m + 1)) → O.le K.zero (d (m + 1)) → O.le (d (m + 1)) (d (m + 2)))
    (hN : O.le (d N) K.zero) (hE : O.le (d E) K.zero) : ∀ i, N + i ≤ E → O.le (d (N + i)) K.zero
  | 0, _ => hN
  | i + 1, hi => by
    have ih := tail_le d N E hstep hN hE i (Nat.le_trans (Nat.le_succ _) hi)
    cases O.le_total (d (N + i + 1)) K.zero with
    | inl h => exact h
    | inr h =>
      have h0 : O.le (d (N + i)) (d (N + i + 1)) := O.le_trans _ _ _ ih h
      obtain ⟨j, hj⟩ := Nat.le.dest hi
      have r := run K O d E hstep (N + i) h0 h (j) (by rw [← hj]; exact Nat.le_refl _)
      have eE : N + i + 1 + j = E := hj
      rw [eE] at r
      exact O.le_trans _ _ _ r.1 hE

/-- **the VV-equality reference has decreasing ratios backward**: if b(1 + ab) = a(1 + bc), a, b ≥ 0 and c ≤ b,
  then b ≤ a -/
theorem ref_dec (a b c : R) (ha : O.le K.zero a) (hb : O.le K.zero b) (hcb : O.le c b)
    (hZ : K.mul b (K.add K.one (K.mul a b)) = K.mul a (K.add K.one (K.mul b c))) : O.le b a := by
  -- b = a + ab(c − b) ≤ a since ab ≥ 0 and c − b ≤ 0
  have e : b = K.add a (K.mul (K.mul a b) (K.add c (K.neg b))) := by
    have : K.add b (K.mul (K.mul a b) b) = K.add a (K.mul (K.mul a b) c) := by
      refine Eq.trans ?_ (Eq.trans hZ ?_) <;> lib_ring
    calc b = K.add (K.add b (K.mul (K.mul a b) b)) (K.neg (K.mul (K.mul a b) b)) := by lib_ring
      _ = K.add (K.add a (K.mul (K.mul a b) c)) (K.neg (K.mul (K.mul a b) b)) := by rw [this]
      _ = K.add a (K.mul (K.mul a b) (K.add c (K.neg b))) := by lib_ring
  have ab0 : O.le K.zero (K.mul a b) := O.mul_nonneg _ _ ha hb
  have neg : O.le (K.add c (K.neg b)) K.zero := by
    have := O.add_le_add _ _ _ _ hcb (O.le_refl (K.neg b))
    exact O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring))
  have t := O.mul_le_mul_right _ _ (K.mul a b) neg ab0
  have u := O.add_le_add _ _ _ _ (O.le_refl a) t
  rw [e]
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ u (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **list VV ⇒ ratio VV**: with a = y₁/y₀, b = y₂/y₁, c = y₃/y₂ (y₀, y₁, y₂ > 0), (y₀ + y₂)y₂ ≤ (y₁ + y₃)y₁ gives
  b(1 + ab) ≤ a(1 + bc) -/
theorem ratio_vv (F : OField.OF K O) (y0 y1 y2 y3 : R) (p0 : O.le K.zero y0) (n0 : y0 ≠ K.zero)
    (p1 : O.le K.zero y1) (n1 : y1 ≠ K.zero) (n2 : y2 ≠ K.zero)
    (hvv : O.le (K.mul (K.add y0 y2) y2) (K.mul (K.add y1 y3) y1)) :
    O.le (K.mul (K.mul y2 (F.inv y1)) (K.add K.one (K.mul (K.mul y1 (F.inv y0)) (K.mul y2 (F.inv y1)))))
      (K.mul (K.mul y1 (F.inv y0)) (K.add K.one (K.mul (K.mul y2 (F.inv y1)) (K.mul y3 (F.inv y2))))) := by
  have P : O.le K.zero (K.mul y0 y1) := O.mul_nonneg _ _ p0 p1
  have Pn : K.mul y0 y1 ≠ K.zero := fun e => n1 (O.pos_mul_eq_zero _ _ p0 n0 e)
  refine O.pos_cancel _ _ _ P Pn ?_
  have e1 : K.mul (K.mul (K.mul y2 (F.inv y1)) (K.add K.one (K.mul (K.mul y1 (F.inv y0)) (K.mul y2 (F.inv y1))))) (K.mul y0 y1)
      = K.add (K.mul (K.mul y2 y0) (K.mul y1 (F.inv y1)))
          (K.mul (K.mul y2 y2) (K.mul (K.mul (K.mul y1 (F.inv y1)) (K.mul y1 (F.inv y1))) (K.mul y0 (F.inv y0)))) := by lib_ring
  have e2 : K.mul (K.mul (K.mul y1 (F.inv y0)) (K.add K.one (K.mul (K.mul y2 (F.inv y1)) (K.mul y3 (F.inv y2))))) (K.mul y0 y1)
      = K.add (K.mul (K.mul y1 y1) (K.mul y0 (F.inv y0)))
          (K.mul (K.mul y1 y3) (K.mul (K.mul (K.mul y0 (F.inv y0)) (K.mul y1 (F.inv y1))) (K.mul y2 (F.inv y2)))) := by lib_ring
  rw [e1, e2, F.mul_inv y0 n0, F.mul_inv y1 n1, F.mul_inv y2 n2]
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ hvv (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **ratio comparison ⇒ cross inequality**: Y₁/Y₀ ≤ M₁/M₀ (Y₀, M₀ > 0) gives Y₁M₀ ≤ Y₀M₁ -/
theorem cross_of_ratio (F : OField.OF K O) (Y0 Y1 M0 M1 : R) (pY : O.le K.zero Y0) (nY : Y0 ≠ K.zero)
    (pM : O.le K.zero M0) (nM : M0 ≠ K.zero)
    (h : O.le (K.mul Y1 (F.inv Y0)) (K.mul M1 (F.inv M0))) : O.le (K.mul Y1 M0) (K.mul Y0 M1) := by
  have P : O.le K.zero (K.mul Y0 M0) := O.mul_nonneg _ _ pY pM
  have w := O.mul_le_mul_right _ _ (K.mul Y0 M0) h P
  have e1 : K.mul (K.mul Y1 (F.inv Y0)) (K.mul Y0 M0) = K.mul (K.mul Y1 M0) (K.mul Y0 (F.inv Y0)) := by lib_ring
  have e2 : K.mul (K.mul M1 (F.inv M0)) (K.mul Y0 M0) = K.mul (K.mul Y0 M1) (K.mul M0 (F.inv M0)) := by lib_ring
  rw [e1, e2, F.mul_inv Y0 nY, F.mul_inv M0 nM] at w
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **one backward step of the potential bound** for the VV-equality reference: with b(1 + ab) = a(1 + bc),
  0 ≤ c ≤ b ≤ ρ, Φ = b + κ(b − c) ≤ R ≤ ρ, ρ²(1 + κ) = κ, ρ² ≤ κ and 1 − b(b − c) > 0, the previous ratio satisfies
  a ≤ ρ and a + κ(a − b) ≤ R -/
theorem pot_step (h1 : O.le K.zero K.one) (a b c ρ κ R : R) (hb : O.le K.zero b) (hcb : O.le c b)
    (hbρ : O.le b ρ) (hρ : O.le K.zero ρ) (hκ : O.le K.zero κ) (eκ : K.mul (K.mul ρ ρ) (K.add K.one κ) = κ)
    (hρκ : O.le (K.mul ρ ρ) κ) (hΦ : O.le (K.add b (K.mul κ (K.add b (K.neg c)))) R) (hR : O.le R ρ)
    (hpos : O.le K.zero (K.add K.one (K.neg (K.mul b (K.add b (K.neg c))))))
    (hne : K.add K.one (K.neg (K.mul b (K.add b (K.neg c)))) ≠ K.zero)
    (hZ : K.mul b (K.add K.one (K.mul a b)) = K.mul a (K.add K.one (K.mul b c))) :
    O.le a ρ ∧ O.le (K.add a (K.mul κ (K.add a (K.neg b)))) R := by
  have eA : K.mul a (K.add K.one (K.neg (K.mul b (K.add b (K.neg c))))) = b := by
    have h : K.add b (K.mul (K.mul a b) b) = K.add a (K.mul (K.mul a b) c) := by
      refine Eq.trans ?_ (Eq.trans hZ ?_) <;> lib_ring
    calc K.mul a (K.add K.one (K.neg (K.mul b (K.add b (K.neg c)))))
        = K.add (K.add a (K.mul (K.mul a b) c)) (K.neg (K.mul (K.mul a b) b)) := by lib_ring
      _ = K.add (K.add b (K.mul (K.mul a b) b)) (K.neg (K.mul (K.mul a b) b)) := by rw [h]
      _ = b := by lib_ring
  have bc0 : O.le K.zero (K.add b (K.neg c)) := by
    have := O.add_le_add _ _ _ _ hcb (O.le_refl (K.neg c))
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this
  have ρb : O.le (K.mul ρ b) κ := by
    have := O.mul_le_mul_right _ _ ρ hbρ hρ
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this hρκ)
  have t1 : O.le (K.add b (K.mul (K.mul ρ b) (K.add b (K.neg c)))) ρ := by
    have := O.add_le_add _ _ _ _ (O.le_refl b) (O.mul_le_mul_right _ _ _ ρb bc0)
    exact O.le_trans _ _ _ this (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ hΦ hR))
  have aρ : O.le a ρ := by
    refine O.pos_cancel _ _ _ hpos hne ?_
    rw [eA]
    have := O.add_le_add _ _ _ _ t1 (O.le_refl (K.neg (K.mul (K.mul ρ b) (K.add b (K.neg c)))))
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring)))
  refine ⟨aρ, ?_⟩
  have eab : K.add a (K.neg b) = K.mul (K.mul a b) (K.add b (K.neg c)) := by
    calc K.add a (K.neg b) = K.add a (K.neg (K.mul a (K.add K.one (K.neg (K.mul b (K.add b (K.neg c))))))) := by rw [eA]
      _ = K.mul (K.mul a b) (K.add b (K.neg c)) := by lib_ring
  have abρ : O.le (K.mul a b) (K.mul ρ ρ) := by
    have x := O.mul_le_mul_right _ _ b aρ hb
    have y := O.mul_le_mul_right _ _ ρ hbρ hρ
    exact O.le_trans _ _ _ x (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ y
      (TripleE.le_of_eq' K O _ _ (by lib_ring))))
  have k1 : O.le K.zero (K.add K.one κ) := by
    have := O.add_le_add _ _ _ _ h1 hκ
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this
  have u := O.mul_le_mul_right _ _ (K.add b (K.neg c)) abρ bc0
  have v := O.mul_le_mul_right _ _ (K.add K.one κ) u k1
  have w := O.add_le_add _ _ _ _ (O.le_refl b) v
  have e1 : K.add a (K.mul κ (K.add a (K.neg b)))
      = K.add b (K.mul (K.mul (K.mul a b) (K.add b (K.neg c))) (K.add K.one κ)) := by
    calc K.add a (K.mul κ (K.add a (K.neg b))) = K.add b (K.mul (K.add a (K.neg b)) (K.add K.one κ)) := by lib_ring
      _ = _ := by rw [eab]
  have e2 : K.add b (K.mul (K.mul (K.mul ρ ρ) (K.add b (K.neg c))) (K.add K.one κ))
      = K.add b (K.mul κ (K.add b (K.neg c))) := by
    calc K.add b (K.mul (K.mul (K.mul ρ ρ) (K.add b (K.neg c))) (K.add K.one κ))
        = K.add b (K.mul (K.mul (K.mul ρ ρ) (K.add K.one κ)) (K.add b (K.neg c))) := by lib_ring
      _ = _ := by rw [eκ]
  rw [e1]
  exact O.le_trans _ _ _ w (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ e2) hΦ)

/-- **every ratio of the VV-equality reference is ≤ ρ** (backward induction from the end E: z_E = 0, z_{E−1} = r):
  for m + i + 1 = E, z_m ≤ ρ, z_{m+1} ≤ z_m and z_m + κ(z_m − z_{m+1}) ≤ r(1 + κ) -/
theorem pot_run (h1 : O.le K.zero K.one) (z : Nat → R) (E : Nat) (r ρ κ : R) (hz0 : ∀ m, O.le K.zero (z m))
    (hE : z E = K.zero) (hE1 : ∀ m, m + 1 = E → z m = r) (hr : O.le K.zero r)
    (hZ : ∀ m, m + 2 ≤ E → K.mul (z (m + 1)) (K.add K.one (K.mul (z m) (z (m + 1))))
      = K.mul (z m) (K.add K.one (K.mul (z (m + 1)) (z (m + 2)))))
    (hρ : O.le K.zero ρ) (hκ : O.le K.zero κ) (eκ : K.mul (K.mul ρ ρ) (K.add K.one κ) = κ) (hρκ : O.le (K.mul ρ ρ) κ)
    (hR : O.le (K.mul r (K.add K.one κ)) ρ)
    (h1ρ : O.le K.zero (K.add K.one (K.neg (K.mul ρ ρ)))) (h1ρn : K.add K.one (K.neg (K.mul ρ ρ)) ≠ K.zero) :
    ∀ i m, m + i + 1 = E → O.le (z m) ρ ∧ O.le (z (m + 1)) (z m) ∧
      O.le (K.add (z m) (K.mul κ (K.add (z m) (K.neg (z (m + 1)))))) (K.mul r (K.add K.one κ))
  | 0, m, hm => by
    have hm0 : m + 1 = E := hm
    have e1 : z m = r := hE1 m hm0
    have e2 : z (m + 1) = K.zero := by rw [hm0]; exact hE
    rw [e1, e2]
    have k1 : O.le K.one (K.add K.one κ) := by
      have := O.add_le_add _ _ _ _ (O.le_refl K.one) hκ
      exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this
    have rR : O.le r (K.mul r (K.add K.one κ)) := by
      have := O.mul_le_mul_right _ _ r k1 hr
      exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring)))
    exact ⟨O.le_trans _ _ _ rR hR, hr, TripleE.le_of_eq' K O _ _ (by lib_ring)⟩
  | i + 1, m, hm => by
    have hm' : m + 1 + i + 1 = E := by rw [← hm]; lib_nat
    have ih := pot_run h1 z E r ρ κ hz0 hE hE1 hr hZ hρ hκ eκ hρκ hR h1ρ h1ρn i (m + 1) hm'
    have hm2 : m + 2 ≤ E := by rw [← hm]; lib_nat_le
    have Z := hZ m hm2
    have b0 := hz0 (m + 1)
    have c0 := hz0 (m + 2)
    -- b(b − c) ≤ ρ²
    have x : O.le (K.add (z (m + 1)) (K.neg (z (m + 2)))) (z (m + 1)) := by
      have := O.add_le_add _ _ _ _ (O.le_refl (K.add (z (m + 1)) (K.neg (z (m + 2))))) c0
      exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring)))
    have bb : O.le (K.mul (z (m + 1)) (K.add (z (m + 1)) (K.neg (z (m + 2))))) (K.mul ρ ρ) := by
      have y := O.mul_le_mul_right _ _ (z (m + 1)) x b0
      have w := O.mul_le_mul_right _ _ (z (m + 1)) ih.1 b0
      have v := O.mul_le_mul_right _ _ ρ ih.1 hρ
      exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ y (O.le_trans _ _ _ w
        (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ v (TripleE.le_of_eq' K O _ _ (by lib_ring))))))
    have pge : O.le (K.add K.one (K.neg (K.mul ρ ρ)))
        (K.add K.one (K.neg (K.mul (z (m + 1)) (K.add (z (m + 1)) (K.neg (z (m + 2))))))) := by
      have := O.add_le_add _ _ _ _ bb (O.le_refl (K.add (K.add K.one (K.neg (K.mul ρ ρ)))
        (K.neg (K.mul (z (m + 1)) (K.add (z (m + 1)) (K.neg (z (m + 2))))))))
      exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring)))
    have hpos := O.le_trans _ _ _ h1ρ pge
    have hne : K.add K.one (K.neg (K.mul (z (m + 1)) (K.add (z (m + 1)) (K.neg (z (m + 2)))))) ≠ K.zero := fun e => by
      rw [e] at pge
      exact h1ρn (O.le_antisymm _ _ pge h1ρ)
    have ps := pot_step K O h1 (z m) (z (m + 1)) (z (m + 2)) ρ κ (K.mul r (K.add K.one κ)) b0 ih.2.1 ih.1 hρ hκ eκ hρκ
      ih.2.2 hR hpos hne Z
    exact ⟨ps.1, ref_dec K O (z m) (z (m + 1)) (z (m + 2)) (hz0 m) b0 ih.2.1 Z, ps.2⟩

/-- **tail comparison of ratio sequences**: y (VV inequality) and z (VV equality), both nonnegative and
  nonincreasing, positive products before the end; y_N ≤ z_N and y_E ≤ z_E give y_n ≤ z_n on N ≤ n ≤ E -/
theorem tail_cmp (y z : Nat → R) (N E : Nat) (hy0 : ∀ m, O.le K.zero (y m)) (hz0 : ∀ m, O.le K.zero (z m))
    (hyd : ∀ m, O.le (y (m + 1)) (y m)) (hzd : ∀ m, m + 1 ≤ E → O.le (z (m + 1)) (z m))
    (hP : ∀ m, m + 2 ≤ E → K.mul (K.mul (y m) (y (m + 1))) (K.mul (z m) (z (m + 1))) ≠ K.zero)
    (hY : ∀ m, m + 2 ≤ E → O.le (K.mul (y (m + 1)) (K.add K.one (K.mul (y m) (y (m + 1)))))
      (K.mul (y m) (K.add K.one (K.mul (y (m + 1)) (y (m + 2))))))
    (hZ : ∀ m, m + 2 ≤ E → K.mul (z (m + 1)) (K.add K.one (K.mul (z m) (z (m + 1))))
      = K.mul (z m) (K.add K.one (K.mul (z (m + 1)) (z (m + 2)))))
    (hN : O.le (y N) (z N)) (hE : O.le (y E) (z E)) :
    ∀ i, N + i ≤ E → O.le (y (N + i)) (z (N + i)) := by
  have dle : ∀ a b, O.le a b → O.le (K.add a (K.neg b)) K.zero := fun a b h => by
    have := O.add_le_add _ _ _ _ h (O.le_refl (K.neg b))
    exact O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring))
  have back : ∀ a b, O.le (K.add a (K.neg b)) K.zero → O.le a b := fun a b h => by
    have := O.add_le_add _ _ _ _ h (O.le_refl b)
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring)))
  have st : ∀ m, m + 2 ≤ E → O.le (K.add (y m) (K.neg (z m))) (K.add (y (m + 1)) (K.neg (z (m + 1)))) →
      O.le K.zero (K.add (y (m + 1)) (K.neg (z (m + 1)))) →
      O.le (K.add (y (m + 1)) (K.neg (z (m + 1)))) (K.add (y (m + 2)) (K.neg (z (m + 2)))) := fun m hm h0 h1 =>
    shoot_step K O (y m) (z m) (y (m + 1)) (z (m + 1)) (y (m + 2)) (z (m + 2)) (hy0 _) (hz0 _) (hy0 _) (hz0 _)
      (hP m hm) (hY m hm) (hZ m hm) (hyd m) (hzd m (Nat.le_trans (Nat.le_succ _) hm)) h0 h1
  intro i hi
  exact back _ _ (tail_le K O (fun m => K.add (y m) (K.neg (z m))) N E st (dle _ _ hN) (dle _ _ hE) i hi)

end TailShoot

#print axioms TailShoot.shoot_step
#print axioms TailShoot.run
#print axioms TailShoot.tail_le
#print axioms TailShoot.ref_dec
#print axioms TailShoot.ratio_vv
#print axioms TailShoot.cross_of_ratio
#print axioms TailShoot.pot_step
#print axioms TailShoot.pot_run
#print axioms TailShoot.tail_cmp

#print axioms: 9 theorems, no axioms.

90.138 Truncated MK flow brackets the full flow

Projection and padding identities with coefficientwise monotonicity bracket the full MK flow by truncated ones.

\[ \mathrm {mk}(\pi _N X)\le \mathrm {mk}(X)\le \mathrm {mk}(\pi _N X)+\varepsilon _N \]

ym/FlowBridge.lean

/-
  ym/FlowBridge — T45, G74c (part 1): the truncated Migdal–Kadanoff map of the TM covers (ProjMK.Wm, truncation
  L + 1) and the full-length map of the v-good flow (FlowStep.mk) agree: for X of length M + 2 and
  L + 1 = 4M + 5 + p,
      Wm L X = padZ (FlowStep.mk X M) p        (padZ = append p zeros).
  Zero padding changes neither weighted sums (dot), Clebsch–Gordan products (cgc), nor decimation beyond the
  padded zeros. Lean 4 core only, no axioms.
-/
import ym.ProjMK
import ym.FlowStep
import ym.ConeBound
import ym.FlowRatio
import ym.HeatL1All
import ym.FlowIter
import ym.SU2Strong4D
import ym.TuranCoef

namespace FlowBridge

open YMChain LinkIntegral SU2Char MigdalCone Projective HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- append p zeros -/
def padZ (a : List R) : Nat → List R
  | 0 => a
  | p + 1 => padZ a p ++ [K.zero]

theorem dot_snoc0 (f : Nat → R) : ∀ (m : Nat) (a : List R), dot K m (a ++ [K.zero]) f = dot K m a f
  | m, [] => by show K.add (K.mul K.zero (f m)) K.zero = K.zero; lib_ring
  | m, c :: a => by
    show K.add (K.mul c (f m)) (dot K (m + 1) (a ++ [K.zero]) f) = K.add (K.mul c (f m)) (dot K (m + 1) a f)
    rw [dot_snoc0 f (m + 1) a]

theorem dot_pad (f : Nat → R) (m : Nat) (a : List R) : ∀ p, dot K m (padZ K a p) f = dot K m a f
  | 0 => rfl
  | p + 1 => by
    show dot K m (padZ K a p ++ [K.zero]) f = _
    rw [dot_snoc0, dot_pad f m a p]

theorem dot_congr (f g : Nat → R) (h : ∀ j, f j = g j) : ∀ (m : Nat) (l : List R), dot K m l f = dot K m l g
  | _, [] => rfl
  | m, c :: l => by
    show K.add (K.mul c (f m)) (dot K (m + 1) l f) = K.add (K.mul c (g m)) (dot K (m + 1) l g)
    rw [h m, dot_congr f g h (m + 1) l]

theorem cgc_pad (a b : List R) (p q : Nat) (n : Nat) : cgc K (padZ K a p) (padZ K b q) n = cgc K a b n := by
  show dot K 0 (padZ K a p) (fun j => dot K 0 (padZ K b q) (fun k => cN K (Fusion.cg j k n))) = _
  rw [dot_pad]
  exact dot_congr K _ _ (fun j => dot_pad K _ 0 b q) 0 a

theorem lmap_snoc (f : Nat → R) : ∀ (m L : Nat), lmap f m (L + 1) = lmap f m L ++ [f (m + L)]
  | m, 0 => by show [f m] = [] ++ [f (m + 0)]; rfl
  | m, L + 1 => by
    show f m :: lmap f (m + 1) (L + 1) = (f m :: lmap f (m + 1) L) ++ [f (m + (L + 1))]
    rw [lmap_snoc f (m + 1) L, show m + 1 + L = m + (L + 1) by lib_nat]
    rfl

theorem listF_lmap (f : Nat → R) : ∀ (m N : Nat), ListRow.listF f m N = lmap f m N
  | _, 0 => rfl
  | m, N + 1 => by
    show f m :: ListRow.listF f (m + 1) N = f m :: lmap f (m + 1) N
    rw [listF_lmap f (m + 1) N]

/-- lmap past the support is zero padding -/
theorem lmap_pad (f : Nat → R) (N : Nat) (hf : ∀ n, N ≤ n → f n = K.zero) :
    ∀ p, lmap f 0 (N + p) = padZ K (lmap f 0 N) p
  | 0 => rfl
  | p + 1 => by
    show lmap f 0 (N + p + 1) = padZ K (lmap f 0 N) p ++ [K.zero]
    rw [lmap_snoc, lmap_pad f N hf p, hf (0 + (N + p)) (by rw [Nat.zero_add]; exact Nat.le_add_right N p)]

theorem decM_snoc0 (h1 : O.le K.zero K.one) : ∀ (m : Nat) (a : List R),
    decM K O F m (a ++ [K.zero]) = decM K O F m a ++ [K.zero]
  | m, [] => by
    show [K.mul (pwR K K.zero 4) (pwR K (F.inv (cN K (m + 1))) 3)] = [K.zero]
    rw [show pwR K K.zero 4 = K.zero by show K.mul (K.mul (K.mul (K.mul K.one K.zero) K.zero) K.zero) K.zero = K.zero; lib_ring]
    rw [show K.mul K.zero (pwR K (F.inv (cN K (m + 1))) 3) = K.zero by lib_ring]
  | m, c :: a => by
    show _ :: decM K O F (m + 1) (a ++ [K.zero]) = _ :: (decM K O F (m + 1) a ++ [K.zero])
    rw [decM_snoc0 h1 (m + 1) a]

theorem decM_pad (h1 : O.le K.zero K.one) (m : Nat) (a : List R) : ∀ p, decM K O F m (padZ K a p) = padZ K (decM K O F m a) p
  | 0 => rfl
  | p + 1 => by
    show decM K O F m (padZ K a p ++ [K.zero]) = padZ K (decM K O F m a) p ++ [K.zero]
    rw [decM_snoc0 K O F h1, decM_pad h1 m a p]

/-- **Wm L X = FlowStep.mk X M padded with zeros**, X of length M + 2, L + 1 = 4M + 5 + p -/
theorem Wm_mk (h1 : O.le K.zero K.one) (X : List R) (M p L : Nat) (hX : X.length = M + 2) (hL : L + 1 = 4 * M + 5 + p) :
    ProjMK.Wm K O F L X = padZ K (FlowStep.mk K O F X M) p := by
  have hZl : (L1Full.prodL K X M).length = 2 * M + 1 + 2 := HeatSecondStep.len_prodL K X M
  -- first product: cgP X X (L+1) = padZ (prodL X M) (2M + 2 + p)
  have e1 : ProjMK.cgP K X X (L + 1) = padZ K (L1Full.prodL K X M) (2 * M + 2 + p) := by
    unfold ProjMK.cgP L1Full.prodL
    rw [listF_lmap, hL, show 4 * M + 5 + p = (2 * M + 3) + (2 * M + 2 + p) by lib_nat]
    exact lmap_pad K _ _ (fun n hn => L1Full.cgc_far K X M hX n hn) _
  -- second product
  have e2 : ProjMK.cgP K (ProjMK.cgP K X X (L + 1)) (ProjMK.cgP K X X (L + 1)) (L + 1)
      = padZ K (FlowStep.Ysq K X M) p := by
    rw [e1]
    show ProjMK.cgP K (padZ K (L1Full.prodL K X M) (2 * M + 2 + p)) (padZ K (L1Full.prodL K X M) (2 * M + 2 + p)) (L + 1)
      = padZ K (L1Full.prodL K (L1Full.prodL K X M) (2 * M + 1)) p
    generalize L1Full.prodL K X M = Z at hZl ⊢
    unfold ProjMK.cgP L1Full.prodL
    rw [listF_lmap, hL]
    have e : lmap (fun n => cgc K (padZ K Z (2 * M + 2 + p)) (padZ K Z (2 * M + 2 + p)) n) 0 (4 * M + 5 + p)
        = lmap (fun n => cgc K Z Z n) 0 (4 * M + 5 + p) :=
      Projective.lmap_ext _ _ (fun n => cgc_pad K Z Z _ _ n) 0 _
    rw [e, show 4 * M + 5 + p = 2 * (2 * M + 1) + 3 + p by lib_nat]
    exact lmap_pad K _ _ (fun n hn => L1Full.cgc_far K Z (2 * M + 1) hZl n hn) p
  unfold ProjMK.Wm FlowStep.mk
  rw [e2, decM_pad K O F h1]

theorem co_snoc0 : ∀ (a : List R) (n : Nat), co K (a ++ [K.zero]) n = co K a n
  | [], 0 => rfl
  | [], _ + 1 => rfl
  | _ :: _, 0 => rfl
  | _ :: a, n + 1 => co_snoc0 a n

/-- zero padding does not change coefficients -/
theorem co_pad (a : List R) (n : Nat) : ∀ p, co K (padZ K a p) n = co K a n
  | 0 => rfl
  | p + 1 => by
    show co K (padZ K a p ++ [K.zero]) n = _
    rw [co_snoc0, co_pad a n p]

/-- the cone bound survives removing trailing zeros -/
theorem bd_snoc0 (r : R) : ∀ (m : Nat) (a : List R), ConeBound.Bd K O r m (a ++ [K.zero]) → ConeBound.Bd K O r m a
  | _, [], _ => trivial
  | m, c :: a, h => ⟨h.1, bd_snoc0 r (m + 1) a h.2⟩

theorem bd_unpad (r : R) (m : Nat) (a : List R) : ∀ p, ConeBound.Bd K O r m (padZ K a p) → ConeBound.Bd K O r m a
  | 0, h => h
  | p + 1, h => bd_unpad r m a p (bd_snoc0 K O r m _ h)

/-- Wm does not see trailing zeros of its input -/
theorem Wm_pad (X : List R) (L p : Nat) : ProjMK.Wm K O F L (padZ K X p) = ProjMK.Wm K O F L X := by
  have e : ProjMK.cgP K (padZ K X p) (padZ K X p) (L + 1) = ProjMK.cgP K X X (L + 1) :=
    Projective.lmap_ext _ _ (fun n => cgc_pad K X X p p n) 0 (L + 1)
  unfold ProjMK.Wm
  rw [e]

/-- **one Migdal step of the TM flow is a rescaled, zero-padded FlowStep.mk step**: if 1 :: a = c·padZ X p with
  |X| = M + 2 and L + 1 = 4M + 5 + p', then mk L a = (q⁴c¹⁶)·padZ (FlowStep.mk X M) p' (q⁻¹ the normaliser) -/
theorem step_rel (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (L M p p' : Nat) (a X : List R) (c : R)
    (hX : X.length = M + 2) (hrel : K.one :: a = scaleL K c (padZ K X p)) (hL : L + 1 = 4 * M + 5 + p')
    (hP : cgc K (K.one :: a) (K.one :: a) 0 ≠ K.zero)
    (hQ : cgc K (ProjMK.cgP K (K.one :: a) (K.one :: a) (L + 1)) (ProjMK.cgP K (K.one :: a) (K.one :: a) (L + 1)) 0 ≠ K.zero) :
    mk K O F L a = scaleL K (K.mul (pwR K (F.inv (cgc K (ProjMK.cgP K (K.one :: a) (K.one :: a) (L + 1))
        (ProjMK.cgP K (K.one :: a) (K.one :: a) (L + 1)) 0)) 4) (pwR K c 16)) (padZ K (FlowStep.mk K O F X M) p') := by
  rw [ProjMK.mk_eq K O F h10 L a hP hQ]
  generalize pwR K (F.inv (cgc K (ProjMK.cgP K (K.one :: a) (K.one :: a) (L + 1))
    (ProjMK.cgP K (K.one :: a) (K.one :: a) (L + 1)) 0)) 4 = q
  rw [hrel, ProjMK.Wm_scale, Wm_pad, Wm_mk K O F h1 X M p' L hX hL, ProjMK.scale_scaleL]

theorem scale_snoc0 (c : R) : ∀ a : List R, scaleL K c (a ++ [K.zero]) = scaleL K c a ++ [K.zero]
  | [] => by show [K.mul c K.zero] = [K.zero]; rw [show K.mul c K.zero = K.zero by lib_ring]
  | x :: a => by
    show K.mul c x :: scaleL K c (a ++ [K.zero]) = K.mul c x :: (scaleL K c a ++ [K.zero])
    rw [scale_snoc0 c a]

theorem scale_pad (c : R) (a : List R) : ∀ p, scaleL K c (padZ K a p) = padZ K (scaleL K c a) p
  | 0 => rfl
  | p + 1 => by
    show scaleL K c (padZ K a p ++ [K.zero]) = padZ K (scaleL K c a) p ++ [K.zero]
    rw [scale_snoc0, scale_pad c a p]

/-- x₀y₀ ≤ c₀ for nonnegative lists (generalises MigdalCone.c0_ge_one) -/
theorem c0_ge_mul (h1 : O.le K.zero K.one) (x0 y0 : R) (hx : O.le K.zero x0) (hy : O.le K.zero y0) (a b : List R)
    (ha : ∀ x, memL x a → O.le K.zero x) (hb : ∀ x, memL x b → O.le K.zero x) :
    O.le (K.mul x0 y0) (cgc K (x0 :: a) (y0 :: b) 0) := by
  have cgnn : ∀ j k, O.le K.zero (cN K (Fusion.cg j k 0)) := fun j k => SU2CharExp.cN_nn K O h1 _
  have inner : ∀ j, O.le K.zero (dot K 0 (y0 :: b) (fun k => cN K (Fusion.cg j k 0))) := fun j =>
    ConeBound.dot_nn K O _ (cgnn j) 0 _ (fun x hx' => by cases hx' with | inl e => rw [e]; exact hy | inr h => exact hb x h)
  have o := MigdalCone.dot_head_le K O _ inner x0 a 0 ha
  have i0 := MigdalCone.dot_head_le K O _ (cgnn 0) y0 b 0 hb
  have e : cN K (Fusion.cg 0 0 0) = K.one := SU2CharExp.cN_one K
  rw [e, show K.mul y0 K.one = y0 by rw [K.mul_comm, K.one_mul]] at i0
  have m := O.mul_le_mul_right _ _ x0 i0 hx
  rw [K.mul_comm y0 x0, K.mul_comm (dot K 0 (y0 :: b) (fun k => cN K (Fusion.cg 0 k 0))) x0] at m
  exact O.le_trans _ _ _ m o

theorem ne_of_ge_one (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx : O.le K.one x) : x ≠ K.zero := by
  intro e
  rw [e] at hx
  exact h10 (O.le_antisymm _ _ hx h1)

/-- **the TM flow is the rescaled, zero-padded v-good flow at every step that fits the truncation** -/
theorem flow_rel (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (L M p0 : Nat) (a X0 : List R)
    (hrel0 : K.one :: a = padZ K X0 p0) (hL0 : L + 1 = M + 2 + p0)
    (hnn : ∀ k j, O.le K.zero (co K (FlowIter.itX K O F X0 M k) j))
    (hlen : ∀ k, (FlowIter.itX K O F X0 M k).length = FlowIter.itM M k + 2) :
    ∀ k p, L + 1 = FlowIter.itM M k + 2 + p → ∃ c : R, O.le K.zero c ∧
      K.one :: mkIt K O F L a k = scaleL K c (padZ K (FlowIter.itX K O F X0 M k) p)
  | 0, p, hp => by
    have hpp : p = p0 := TuranCoef.add_cancel p p0 (M + 2) (by
      rw [show p + (M + 2) = M + 2 + p by lib_nat, show p0 + (M + 2) = M + 2 + p0 by lib_nat, ← hL0]; exact hp.symm)
    refine ⟨K.one, h1, ?_⟩
    rw [hpp, FlowRatio.scale_one]
    exact hrel0
  | k + 1, p, hp => by
    have hpk : L + 1 = FlowIter.itM M k + 2 + (3 * FlowIter.itM M k + 3 + p) := by
      rw [hp]; show 4 * FlowIter.itM M k + 3 + 2 + p = _; lib_nat
    have ⟨c, hc, hrel⟩ := flow_rel h1 h10 L M p0 a X0 hrel0 hL0 hnn hlen k _ hpk
    -- nonnegativity of the TM list
    have hco : ∀ j, O.le K.zero (co K (K.one :: mkIt K O F L a k) j) := fun j => by
      rw [hrel, FlowRatio.co_scale, co_pad]
      exact O.mul_nonneg _ _ hc (hnn k j)
    have hb : ∀ x, memL x (mkIt K O F L a k) → O.le K.zero x := fun x hx =>
      RatioStep.mem_nn K O _ hco x (Or.inr hx)
    have hP1 := MigdalCone.c0_ge_one K O h1 (mkIt K O F L a k) (mkIt K O F L a k) hb hb
    have hP := ne_of_ge_one K O h1 h10 _ hP1
    -- the second normaliser
    have vco : ∀ j, O.le K.zero (co K (K.one :: mkIt K O F L a k) j) := hco
    have c0nn := HeatL1All.cgc_nn K O (K.one :: mkIt K O F L a k) (K.one :: mkIt K O F L a k) (mkIt K O F L a k).length rfl vco vco
    have hrest : ∀ x, memL x (lmap (fun n => cgc K (K.one :: mkIt K O F L a k) (K.one :: mkIt K O F L a k) n) 1 L) →
        O.le K.zero x := fun x hx => by
      refine RatioStep.mem_nn K O _ (fun j => ?_) x hx
      cases Nat.lt_or_ge j L with
      | inl h => rw [RatioStep.co_lmap K _ L 1 j h]; exact c0nn _
      | inr h => rw [RatioStep.co_lmap_ge K _ L 1 j h]; exact O.le_refl _
    have hQ1 := c0_ge_mul K O h1 _ _ (c0nn 0) (c0nn 0) _ _ hrest hrest
    have sq : O.le K.one (K.mul (cgc K (K.one :: mkIt K O F L a k) (K.one :: mkIt K O F L a k) 0)
        (cgc K (K.one :: mkIt K O F L a k) (K.one :: mkIt K O F L a k) 0)) := by
      have m := O.mul_le_mul_right _ _ _ hP1 (c0nn 0)
      rw [K.one_mul] at m
      exact O.le_trans _ _ _ hP1 m
    have hQ : cgc K (ProjMK.cgP K (K.one :: mkIt K O F L a k) (K.one :: mkIt K O F L a k) (L + 1))
        (ProjMK.cgP K (K.one :: mkIt K O F L a k) (K.one :: mkIt K O F L a k) (L + 1)) 0 ≠ K.zero :=
      ne_of_ge_one K O h1 h10 _ (O.le_trans _ _ _ sq hQ1)
    have st := step_rel K O F h1 h10 L (FlowIter.itM M k) (3 * FlowIter.itM M k + 3 + p) p (mkIt K O F L a k)
      (FlowIter.itX K O F X0 M k) c (hlen k) hrel (by rw [hp]; show 4 * FlowIter.itM M k + 3 + 2 + p = _; lib_nat) hP hQ
    have qnn : O.le K.zero (pwR K (F.inv (cgc K (ProjMK.cgP K (K.one :: mkIt K O F L a k) (K.one :: mkIt K O F L a k) (L + 1))
        (ProjMK.cgP K (K.one :: mkIt K O F L a k) (K.one :: mkIt K O F L a k) (L + 1)) 0)) 4) :=
      AreaLower.pw_nn K O _ (F.inv_pos _ (O.le_trans _ _ _ h1 (O.le_trans _ _ _ sq hQ1)) hQ) h1 4
    refine ⟨_, O.mul_nonneg _ _ qnn (AreaLower.pw_nn K O c hc h1 16), ?_⟩
    have hd := ProjMK.mk_head1 K O F h10 L (mkIt K O F L a k) hP hQ
    show K.one :: (mk K O F L (mkIt K O F L a k)).tail = _
    rw [← hd, st]
    rfl

/-- **cone transfer**: if the TM flow 1 :: mkIt L a k lies in the cone Bd r, then so does the v-good flow X_k
  rescaled by the same constant (which normalises its head to 1) -/
theorem cone_transfer (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (L M p0 : Nat) (a X0 : List R) (r : R)
    (hrel0 : K.one :: a = padZ K X0 p0) (hL0 : L + 1 = M + 2 + p0)
    (hnn : ∀ k j, O.le K.zero (co K (FlowIter.itX K O F X0 M k) j))
    (hlen : ∀ k, (FlowIter.itX K O F X0 M k).length = FlowIter.itM M k + 2)
    (k p : Nat) (hp : L + 1 = FlowIter.itM M k + 2 + p)
    (hbd : ConeBound.Bd K O r 0 (K.one :: mkIt K O F L a k)) :
    ∃ c : R, O.le K.zero c ∧ ConeBound.Bd K O r 0 (scaleL K c (FlowIter.itX K O F X0 M k)) := by
  have ⟨c, hc, hrel⟩ := flow_rel K O F h1 h10 L M p0 a X0 hrel0 hL0 hnn hlen k p hp
  refine ⟨c, hc, ?_⟩
  rw [hrel, scale_pad] at hbd
  exact bd_unpad K O r 0 _ p hbd

/-- truncation is multiplication by the nonincreasing 0/1 weight g = [n < N]: co (lmap (co X) 0 N) n = co X n · g n -/
theorem trunc_weight (X : List R) (N n : Nat) :
    co K (lmap (fun j => co K X j) 0 N) n = K.mul (co K X n) (if n < N then K.one else K.zero) := by
  cases Nat.lt_or_ge n N with
  | inl h =>
    rw [RatioStep.co_lmap K _ N 0 n h, Nat.zero_add]
    rw [if_pos h]
    lib_ring
  | inr h =>
    rw [RatioStep.co_lmap_ge K _ N 0 n h]
    have hn : ¬ n < N := fun h' => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h' h)
    rw [if_neg hn]
    lib_ring

/-- dot of a nonnegative list against nonnegative weights is nonnegative -/
theorem dot_nn0 (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) :
    ∀ (b' : List R) (m : Nat), (∀ k, O.le K.zero (co K b' k)) → O.le K.zero (dot K m b' f)
  | [], _, _ => O.le_refl _
  | y :: b', m, h' => by
    show O.le K.zero (K.add (K.mul y (f m)) (dot K (m + 1) b' f))
    have a := dot_nn0 f hf b' (m + 1) (fun k => h' (k + 1))
    have := O.add_le_add _ _ _ _ (O.mul_nonneg _ _ (h' 0) (hf m)) a
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this
    exact this

/-- dot of a nonpositive list against nonnegative weights is nonpositive -/
theorem dot_np0 (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) :
    ∀ (b : List R) (m : Nat), (∀ k, O.le (co K b k) K.zero) → O.le (dot K m b f) K.zero
  | [], _, _ => O.le_refl _
  | x :: b, m, h => by
    show O.le (K.add (K.mul x (f m)) (dot K (m + 1) b f)) K.zero
    have a := dot_np0 f hf b (m + 1) (fun k => h (k + 1))
    have h0 : O.le x K.zero := h 0
    have x0 : O.le (K.mul x (f m)) (K.mul K.zero (f m)) := O.mul_le_mul_right _ _ (f m) h0 (hf m)
    rw [show K.mul K.zero (f m) = K.zero by lib_ring] at x0
    have := O.add_le_add _ _ _ _ x0 a
    rw [show K.add K.zero K.zero = K.zero by lib_ring] at this
    exact this

/-- dot is monotone in the coefficients (any lengths), for nonnegative weights -/
theorem dot_mono (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) :
    ∀ (b b' : List R) (m : Nat), (∀ k, O.le (co K b k) (co K b' k)) → (∀ k, O.le K.zero (co K b' k)) →
      O.le (dot K m b f) (dot K m b' f)
  | [], b', m, _, h' => dot_nn0 K O f hf b' m h'
  | x :: b, b', m, h, h' => by
    cases b' with
    | nil => exact dot_np0 K O f hf (x :: b) m h
    | cons y b' =>
      show O.le (K.add (K.mul x (f m)) (dot K (m + 1) b f)) (K.add (K.mul y (f m)) (dot K (m + 1) b' f))
      exact O.add_le_add _ _ _ _ (O.mul_le_mul_right _ _ _ (h 0) (hf m))
        (dot_mono f hf b b' (m + 1) (fun k => h (k + 1)) (fun k => h' (k + 1)))

/-- dot is monotone in the weight function, for nonnegative lists -/
theorem dot_fmono (f g : Nat → R) (hfg : ∀ j, O.le (f j) (g j)) :
    ∀ (m : Nat) (a : List R), (∀ k, O.le K.zero (co K a k)) → O.le (dot K m a f) (dot K m a g)
  | _, [], _ => O.le_refl _
  | m, x :: a, h => by
    show O.le (K.add (K.mul x (f m)) (dot K (m + 1) a f)) (K.add (K.mul x (g m)) (dot K (m + 1) a g))
    have w := O.mul_le_mul_right _ _ x (hfg m) (h 0)
    rw [K.mul_comm (f m), K.mul_comm (g m)] at w
    exact O.add_le_add _ _ _ _ w (dot_fmono f g hfg (m + 1) a (fun k => h (k + 1)))

/-- **cgc is monotone in both coefficient lists** (nonnegative coefficients, any lengths) -/
theorem cgc_mono (h1 : O.le K.zero K.one) (a a' b b' : List R) (n : Nat)
    (ha : ∀ k, O.le K.zero (co K a k)) (haa : ∀ k, O.le (co K a k) (co K a' k))
    (hb : ∀ k, O.le K.zero (co K b k)) (hbb : ∀ k, O.le (co K b k) (co K b' k)) :
    O.le (cgc K a b n) (cgc K a' b' n) := by
  have ha' : ∀ k, O.le K.zero (co K a' k) := fun k => O.le_trans _ _ _ (ha k) (haa k)
  have hb' : ∀ k, O.le K.zero (co K b' k) := fun k => O.le_trans _ _ _ (hb k) (hbb k)
  have gnn : ∀ j k, O.le K.zero (cN K (Fusion.cg j k n)) := fun j k => SU2CharExp.cN_nn K O h1 _
  have inner : ∀ j, O.le (dot K 0 b (fun k => cN K (Fusion.cg j k n))) (dot K 0 b' (fun k => cN K (Fusion.cg j k n))) :=
    fun j => dot_mono K O _ (gnn j) b b' 0 hbb hb'
  have innn : ∀ j, O.le K.zero (dot K 0 b' (fun k => cN K (Fusion.cg j k n))) := fun j =>
    ConeBound.dot_nn K O _ (gnn j) 0 b' (RatioStep.mem_nn K O b' hb')
  show O.le (dot K 0 a (fun j => dot K 0 b (fun k => cN K (Fusion.cg j k n))))
    (dot K 0 a' (fun j => dot K 0 b' (fun k => cN K (Fusion.cg j k n))))
  exact O.le_trans _ _ _ (dot_fmono K O _ _ inner 0 a ha) (dot_mono K O _ innn a a' 0 haa ha')

/-- truncating a nonnegative list lowers every coefficient -/
theorem trunc_le (h1 : O.le K.zero K.one) (X : List R) (hX : ∀ k, O.le K.zero (co K X k)) (N n : Nat) :
    O.le (co K (lmap (fun j => co K X j) 0 N) n) (co K X n) ∧ O.le K.zero (co K (lmap (fun j => co K X j) 0 N) n) := by
  rw [trunc_weight]
  cases Nat.lt_or_ge n N with
  | inl h =>
    rw [if_pos h, show K.mul (co K X n) K.one = co K X n by lib_ring]
    exact ⟨O.le_refl _, hX n⟩
  | inr h =>
    have hn : ¬ n < N := fun h' => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h' h)
    rw [if_neg hn, show K.mul (co K X n) K.zero = K.zero by lib_ring]
    exact ⟨hX n, O.le_refl _⟩

/-- **the projected product is coefficientwise below the full product** -/
theorem proj_le (h1 : O.le K.zero K.one) (X Y : List R) (hX : ∀ k, O.le K.zero (co K X k))
    (hY : ∀ k, O.le K.zero (co K Y k)) (N n : Nat) :
    O.le (cgc K (lmap (fun j => co K X j) 0 N) (lmap (fun j => co K Y j) 0 N) n) (cgc K X Y n) :=
  cgc_mono K O h1 _ _ _ _ n (fun k => (trunc_le K O h1 X hX N k).2) (fun k => (trunc_le K O h1 X hX N k).1)
    (fun k => (trunc_le K O h1 Y hY N k).2) (fun k => (trunc_le K O h1 Y hY N k).1)

/-- decimation is coefficientwise monotone on nonnegative lists -/
theorem decM_mono (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (a b : List R) (m n : Nat)
    (ha : O.le K.zero (co K a n)) (hab : O.le (co K a n) (co K b n)) :
    O.le (co K (decM K O F m a) n) (co K (decM K O F m b) n) := by
  rw [RatioStep.co_decM K O F a m n, RatioStep.co_decM K O F b m n]
  have c := SU2CharExp.pos_of_ge_one K O h1 h10 (m + n + 1) (Nat.le_add_left 1 (m + n))
  have i := AreaLower.pw_nn K O _ (F.inv_pos _ c.1 c.2) h1 3
  have p4 := SU2Strong4D.pw_mono K O h1 _ _ ha hab 4
  exact O.mul_le_mul_right _ _ _ p4 i

/-- cgc is at most the product of the coefficient sums (cg ≤ 1) -/
theorem cgc_le_tot (h1 : O.le K.zero K.one) (a b : List R) (n : Nat)
    (ha : ∀ k, O.le K.zero (co K a k)) (hb : ∀ k, O.le K.zero (co K b k)) :
    O.le (cgc K a b n) (dot K 0 a (fun _ => dot K 0 b (fun _ => K.one))) := by
  have c1 : ∀ j k, O.le (cN K (Fusion.cg j k n)) K.one := fun j k => by
    have := SU2CharExp.cN_mono K O h1 _ _ (Fusion.cg_le_one j k n)
    rw [SU2CharExp.cN_one K] at this; exact this
  have inner : ∀ j, O.le (dot K 0 b (fun k => cN K (Fusion.cg j k n))) (dot K 0 b (fun _ => K.one)) :=
    fun j => dot_fmono K O _ _ (c1 j) 0 b hb
  show O.le (dot K 0 a (fun j => dot K 0 b (fun k => cN K (Fusion.cg j k n)))) _
  exact dot_fmono K O _ _ inner 0 a ha

theorem dot_ht (f : Nat → R) (m : Nat) : ∀ l : List R, dot K m l f = K.add (K.mul (co K l 0) (f m)) (dot K (m + 1) l.tail f)
  | [] => by show K.zero = K.add (K.mul K.zero (f m)) K.zero; lib_ring
  | _ :: _ => rfl

theorem co_tail : ∀ (l : List R) (k : Nat), co K l.tail k = co K l (k + 1)
  | [], _ => rfl
  | _ :: _, _ => rfl

theorem len_tail (n : Nat) : ∀ l : List R, l.length ≤ n + 1 → l.tail.length ≤ n
  | [], _ => Nat.zero_le _
  | _ :: _, h => Nat.le_of_succ_le_succ h

theorem nil_of_len0 : ∀ l : List R, l.length ≤ 0 → l = []
  | [], _ => rfl
  | _ :: _, h => absurd h (Nat.not_succ_le_zero _)

/-- **dot is additive in the coefficients** (any lengths; fuel n bounds the lengths) -/
theorem dot_split (f : Nat → R) : ∀ (n : Nat) (a a1 a2 : List R) (m : Nat), a.length ≤ n → a1.length ≤ n → a2.length ≤ n →
    (∀ k, co K a k = K.add (co K a1 k) (co K a2 k)) → dot K m a f = K.add (dot K m a1 f) (dot K m a2 f)
  | 0, a, a1, a2, m, h, h1', h2', _ => by
    rw [nil_of_len0 a h, nil_of_len0 a1 h1', nil_of_len0 a2 h2']
    show K.zero = K.add K.zero K.zero; lib_ring
  | n + 1, a, a1, a2, m, h, h1', h2', hc => by
    rw [dot_ht K f m a, dot_ht K f m a1, dot_ht K f m a2, hc 0,
      dot_split f n a.tail a1.tail a2.tail (m + 1) (len_tail n a h) (len_tail n a1 h1') (len_tail n a2 h2')
        (fun k => by rw [co_tail, co_tail, co_tail]; exact hc (k + 1))]
    lib_ring

/-- cgc is additive in its first list (lengths ≤ n) -/
theorem cgc_split (n : Nat) (a a1 a2 b : List R) (t : Nat) (h : a.length ≤ n) (h1' : a1.length ≤ n) (h2' : a2.length ≤ n)
    (hc : ∀ k, co K a k = K.add (co K a1 k) (co K a2 k)) :
    cgc K a b t = K.add (cgc K a1 b t) (cgc K a2 b t) :=
  dot_split K _ n a a1 a2 0 h h1' h2' hc

/-- the projection error of one product: cgc a b − cgc (Pa) (Pb) ≤ cgc (Ta) b + cgc (Pa) (Tb) form,
  stated as cgc a b ≤ cgc Pa Pb + cgc Ta b + cgc Pa Tb when a = Pa + Ta, b = Pb + Tb coefficientwise -/
theorem cgc_proj_err (n : Nat) (a pa ta b pb tb : List R) (t : Nat)
    (ha : a.length ≤ n) (hpa : pa.length ≤ n) (hta : ta.length ≤ n)
    (hb : b.length ≤ n) (hpb : pb.length ≤ n) (htb : tb.length ≤ n)
    (hsa : ∀ k, co K a k = K.add (co K pa k) (co K ta k)) (hsb : ∀ k, co K b k = K.add (co K pb k) (co K tb k)) :
    cgc K a b t = K.add (cgc K pa pb t) (K.add (cgc K ta b t) (cgc K pa tb t)) := by
  rw [cgc_split K n a pa ta b t ha hpa hta hsa, L1Full.cgc_comm K pa b, L1Full.cgc_comm K pa tb,
    cgc_split K n b pb tb pa t hb hpb htb hsb, L1Full.cgc_comm K pb pa]
  lib_ring

/-- **one-product projection error bound**: cgc a b ≤ cgc Pa Pb + (ΣTa)(Σb) + (ΣPa)(ΣTb) -/
theorem proj_err_bound (h1 : O.le K.zero K.one) (n : Nat) (a pa ta b pb tb : List R) (t : Nat)
    (ha : a.length ≤ n) (hpa : pa.length ≤ n) (hta : ta.length ≤ n)
    (hb : b.length ≤ n) (hpb : pb.length ≤ n) (htb : tb.length ≤ n)
    (hsa : ∀ k, co K a k = K.add (co K pa k) (co K ta k)) (hsb : ∀ k, co K b k = K.add (co K pb k) (co K tb k))
    (npa : ∀ k, O.le K.zero (co K pa k)) (nta : ∀ k, O.le K.zero (co K ta k))
    (nb : ∀ k, O.le K.zero (co K b k)) (ntb : ∀ k, O.le K.zero (co K tb k)) :
    O.le (cgc K a b t) (K.add (cgc K pa pb t) (K.add (dot K 0 ta (fun _ => dot K 0 b (fun _ => K.one)))
      (dot K 0 pa (fun _ => dot K 0 tb (fun _ => K.one))))) := by
  rw [cgc_proj_err K n a pa ta b pb tb t ha hpa hta hb hpb htb hsa hsb]
  exact O.add_le_add _ _ _ _ (O.le_refl _) (O.add_le_add _ _ _ _ (cgc_le_tot K O h1 ta b t nta nb)
    (cgc_le_tot K O h1 pa tb t npa ntb))

end FlowBridge

#print axioms FlowBridge.dot_snoc0
#print axioms FlowBridge.dot_pad
#print axioms FlowBridge.cgc_pad
#print axioms FlowBridge.lmap_snoc
#print axioms FlowBridge.listF_lmap
#print axioms FlowBridge.lmap_pad
#print axioms FlowBridge.decM_snoc0
#print axioms FlowBridge.decM_pad
#print axioms FlowBridge.Wm_mk
#print axioms FlowBridge.co_snoc0
#print axioms FlowBridge.co_pad
#print axioms FlowBridge.bd_snoc0
#print axioms FlowBridge.bd_unpad
#print axioms FlowBridge.Wm_pad
#print axioms FlowBridge.step_rel
#print axioms FlowBridge.scale_snoc0
#print axioms FlowBridge.scale_pad
#print axioms FlowBridge.c0_ge_mul
#print axioms FlowBridge.ne_of_ge_one
#print axioms FlowBridge.flow_rel
#print axioms FlowBridge.cone_transfer
#print axioms FlowBridge.trunc_weight
#print axioms FlowBridge.dot_nn0
#print axioms FlowBridge.dot_np0
#print axioms FlowBridge.dot_mono
#print axioms FlowBridge.dot_fmono
#print axioms FlowBridge.cgc_mono
#print axioms FlowBridge.trunc_le
#print axioms FlowBridge.proj_le
#print axioms FlowBridge.decM_mono
#print axioms FlowBridge.cgc_le_tot
#print axioms FlowBridge.dot_ht
#print axioms FlowBridge.co_tail
#print axioms FlowBridge.dot_split
#print axioms FlowBridge.cgc_split
#print axioms FlowBridge.cgc_proj_err
#print axioms FlowBridge.len_tail
#print axioms FlowBridge.nil_of_len0
#print axioms FlowBridge.proj_err_bound

#print axioms: 39 theorems, no axioms.

90.139 Route (B) reference lists

A heat head followed by the backward VV-equality tail is v-good for every length.

\[ z_{n+1}(1+z_nz_{n+1})=z_n(1+z_{n+1}z_{n+2}),\quad z_E=0 \]

ym/RefB.lean

/-
  ym/RefB — T45, G78g route (B): the VV-equality reference ratios, built backward from the end. pairB r k holds the
  ratios (z_{E−1−k}, z_{E−k}) counted from the end E (z_E = 0, z_{E−1} = r); each step solves the VV equality
  b(1 + ab) = a(1 + bc) for a: a = b/(1 + b(c − b)). Lean 4 core only, no axioms.
-/
import ym.TailShoot
import lib.NatSub
import ym.WilsonGood

namespace RefB

open YMChain LinkIntegral SU2Char HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- ratios counted from the end: (z_{E−1−k}, z_{E−k}) -/
def pairB (r : R) : Nat → R × R
  | 0 => (r, K.zero)
  | k + 1 => ((K.mul (pairB r k).1 (F.inv (K.add K.one (K.mul (pairB r k).1 (K.add (pairB r k).2 (K.neg (pairB r k).1)))))),
      (pairB r k).1)

/-- the denominator of one backward step -/
def den (r : R) (k : Nat) : R := K.add K.one (K.mul (pairB K O F r k).1 (K.add (pairB K O F r k).2 (K.neg (pairB K O F r k).1)))

/-- **the VV equality holds at every backward step** (denominator nonzero): with a = z_{E−2−k}, b = z_{E−1−k},
  c = z_{E−k}: b(1 + ab) = a(1 + bc) -/
theorem pairB_vv (r : R) (k : Nat) (hd : den K O F r k ≠ K.zero) :
    K.mul (pairB K O F r k).1 (K.add K.one (K.mul (pairB K O F r (k + 1)).1 (pairB K O F r k).1))
      = K.mul (pairB K O F r (k + 1)).1 (K.add K.one (K.mul (pairB K O F r k).1 (pairB K O F r k).2)) := by
  have i := F.mul_inv _ hd
  unfold den at i
  show K.mul (pairB K O F r k).1 (K.add K.one (K.mul (K.mul (pairB K O F r k).1 (F.inv (K.add K.one
      (K.mul (pairB K O F r k).1 (K.add (pairB K O F r k).2 (K.neg (pairB K O F r k).1)))))) (pairB K O F r k).1))
    = K.mul (K.mul (pairB K O F r k).1 (F.inv (K.add K.one (K.mul (pairB K O F r k).1 (K.add (pairB K O F r k).2
      (K.neg (pairB K O F r k).1)))))) (K.add K.one (K.mul (pairB K O F r k).1 (pairB K O F r k).2))
  generalize (pairB K O F r k).1 = b at i ⊢
  generalize (pairB K O F r k).2 = c at i ⊢
  generalize F.inv (K.add K.one (K.mul b (K.add c (K.neg b)))) = v at i ⊢
  -- b(1 + b v b) = b v (1 + b c) ⇔ b + b³v = bv + b²cv; using (1 + b(c − b))v = 1
  have e : K.mul b (K.add K.one (K.mul (K.mul b v) b))
      = K.add (K.mul (K.mul b v) (K.add K.one (K.mul b c)))
          (K.mul b (K.add K.one (K.neg (K.mul (K.add K.one (K.mul b (K.add c (K.neg b)))) v)))) := by lib_ring
  rw [e, i]
  lib_ring

/-- the second component is the previous first component -/
theorem pairB_snd (r : R) (k : Nat) : (pairB K O F r (k + 1)).2 = (pairB K O F r k).1 := rfl

/-- the reference ratio at index m for a list ending at E (ratio z_m = M_{m+1}/M_m, zero from E on) -/
def zB (r : R) (E m : Nat) : R := if m < E then (pairB K O F r (E - 1 - m)).1 else K.zero

/-- the reference list values: head h on n ≤ N0, then M_{n+1} = z_n·M_n (length E + 1) -/
def valB (h : Nat → R) (N0 : Nat) (r : R) (E : Nat) : Nat → R
  | 0 => h 0
  | n + 1 => if n + 1 ≤ N0 then h (n + 1) else K.mul (zB K O F r E n) (valB h N0 r E n)

theorem valB_tail (h : Nat → R) (N0 : Nat) (r : R) (E n : Nat) (hn : N0 ≤ n) :
    valB K O F h N0 r E (n + 1) = K.mul (zB K O F r E n) (valB K O F h N0 r E n) := by
  show (if n + 1 ≤ N0 then h (n + 1) else K.mul (zB K O F r E n) (valB K O F h N0 r E n)) = _
  have hn' : ¬ n + 1 ≤ N0 := fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (Nat.lt_of_succ_le e) hn)
  rw [if_neg hn']

theorem zB_ge (r : R) (E m : Nat) (h : E ≤ m) : zB K O F r E m = K.zero := by
  unfold zB
  have hn : ¬ m < E := fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le e h)
  rw [if_neg hn]

/-- (m + 2 + j) − 1 − m = j + 1 and (m + 2 + j) − 1 − (m + 1) = j -/
theorem idx1 (m j : Nat) : m + 2 + j - 1 - m = j + 1 := by
  rw [LibNatSub.sub_sub', show m + 2 + j = (1 + m) + (j + 1) by lib_nat, LibNatSub.add_sub_l]

theorem idx2 (m j : Nat) : m + 2 + j - 1 - (m + 1) = j := by
  rw [LibNatSub.sub_sub', show m + 2 + j = (1 + (m + 1)) + j by lib_nat, LibNatSub.add_sub_l]

/-- **the reference ratios satisfy the VV equality** at every m with m + 2 ≤ E (denominators nonzero) -/
theorem zB_vv (r : R) (E m : Nat) (hm : m + 2 ≤ E)
    (hd : ∀ k, den K O F r k ≠ K.zero) :
    K.mul (zB K O F r E (m + 1)) (K.add K.one (K.mul (zB K O F r E m) (zB K O F r E (m + 1))))
      = K.mul (zB K O F r E m) (K.add K.one (K.mul (zB K O F r E (m + 1)) (zB K O F r E (m + 2)))) := by
  obtain ⟨j, hj⟩ := LibNatSub.le_ex hm
  subst hj
  have l0 : m < m + 2 + j := Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_succ m)) (Nat.le_add_right _ _)
  have l1 : m + 1 < m + 2 + j := Nat.lt_of_lt_of_le (Nat.lt_succ_self (m + 1)) (Nat.le_add_right _ _)
  have z0 : zB K O F r (m + 2 + j) m = (pairB K O F r (j + 1)).1 := by
    unfold zB; rw [if_pos l0, idx1]
  have z1 : zB K O F r (m + 2 + j) (m + 1) = (pairB K O F r j).1 := by
    unfold zB; rw [if_pos l1, idx2]
  have z2 : zB K O F r (m + 2 + j) (m + 2) = (pairB K O F r j).2 := by
    cases j with
    | zero => rw [zB_ge K O F r _ _ (Nat.le_refl _)]; rfl
    | succ j' =>
      have l2 : m + 2 < m + 2 + (j' + 1) :=
        Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.add_le_add_left (Nat.succ_le_succ (Nat.zero_le j')) (m + 2))
      unfold zB; rw [if_pos l2]
      have e : m + 2 + (j' + 1) - 1 - (m + 2) = j' := by
        rw [LibNatSub.sub_sub', show m + 2 + (j' + 1) = (1 + (m + 2)) + j' by lib_nat, LibNatSub.add_sub_l]
      rw [e]; rfl
  rw [z0, z1, z2]
  exact pairB_vv K O F r j (hd j)

/-- the last reference ratio is r -/
theorem zB_last (r : R) (j : Nat) : zB K O F r (j + 1) j = r := by
  unfold zB
  rw [if_pos (Nat.lt_succ_self j), LibNatSub.add_sub_r j 1, LibNatSub.sub_eq_zero (Nat.le_refl j)]
  rfl

/-- **ratio VV equality ⇒ list VV** (with equality): y₁ = a·y₀, y₂ = b·y₁, y₃ = c·y₂ and b(1 + ab) = a(1 + bc)
  give (y₀ + y₂)y₂ ≤ (y₁ + y₃)y₁ -/
theorem vv_of_ratio (y0 y1 y2 y3 a b c : R) (e1 : y1 = K.mul a y0) (e2 : y2 = K.mul b y1) (e3 : y3 = K.mul c y2)
    (hZ : K.mul b (K.add K.one (K.mul a b)) = K.mul a (K.add K.one (K.mul b c))) :
    O.le (K.mul (K.add y0 y2) y2) (K.mul (K.add y1 y3) y1) := by
  rw [e3, e2, e1]
  have w : K.mul (K.mul b (K.add K.one (K.mul a b))) (K.mul a (K.mul y0 y0))
      = K.mul (K.mul a (K.add K.one (K.mul b c))) (K.mul a (K.mul y0 y0)) := by rw [hZ]
  exact TripleE.le_of_eq' K O _ _ (Eq.trans (by lib_ring) (Eq.trans w (by lib_ring)))

/-- **the reference ratios are positive** when r > 0 and every denominator is positive -/
theorem pairB_pos (h10 : K.one ≠ K.zero) (r : R) (hr : O.le K.zero r) (hrn : r ≠ K.zero)
    (hd : ∀ k, O.le K.zero (den K O F r k)) (hdn : ∀ k, den K O F r k ≠ K.zero) :
    ∀ k, O.le K.zero (pairB K O F r k).1 ∧ (pairB K O F r k).1 ≠ K.zero
  | 0 => ⟨hr, hrn⟩
  | k + 1 => by
    have ih := pairB_pos h10 r hr hrn hd hdn k
    have ip := F.inv_pos _ (hd k) (hdn k)
    have inz : F.inv (den K O F r k) ≠ K.zero := fun e => h10 (by
      have := F.mul_inv _ (hdn k); rw [e] at this; rw [← this]; lib_ring)
    show O.le K.zero (K.mul (pairB K O F r k).1 (F.inv (den K O F r k))) ∧ K.mul (pairB K O F r k).1 (F.inv (den K O F r k)) ≠ K.zero
    exact ⟨O.mul_nonneg _ _ ih.1 ip, fun e => inz (O.pos_mul_eq_zero _ _ ih.1 ih.2 e)⟩

/-- the reference head: valB n = h n for n ≤ N0 -/
theorem valB_head (h : Nat → R) (N0 : Nat) (r : R) (E : Nat) : ∀ n, n ≤ N0 → valB K O F h N0 r E n = h n
  | 0, _ => rfl
  | n + 1, hn => by
    show (if n + 1 ≤ N0 then h (n + 1) else K.mul (zB K O F r E n) (valB K O F h N0 r E n)) = h (n + 1)
    rw [if_pos hn]

/-- **the reference values are positive** wherever the head is positive and the ratios before the end are positive -/
theorem valB_pos (h : Nat → R) (N0 : Nat) (r : R) (E : Nat)
    (hh : ∀ n, n ≤ N0 → O.le K.zero (h n) ∧ h n ≠ K.zero)
    (hz : ∀ n, N0 ≤ n → n < E → O.le K.zero (zB K O F r E n) ∧ zB K O F r E n ≠ K.zero) :
    ∀ n, n ≤ E → O.le K.zero (valB K O F h N0 r E n) ∧ valB K O F h N0 r E n ≠ K.zero
  | 0, _ => hh 0 (Nat.zero_le _)
  | n + 1, hn => by
    cases Nat.lt_or_ge n N0 with
    | inl hl =>
      rw [valB_head K O F h N0 r E (n + 1) hl]
      exact hh (n + 1) hl
    | inr hg =>
      rw [valB_tail K O F h N0 r E n hg]
      have ih := valB_pos h N0 r E hh hz n (Nat.le_trans (Nat.le_succ n) hn)
      have zz := hz n hg (Nat.lt_of_succ_le hn)
      exact ⟨O.mul_nonneg _ _ zz.1 ih.1, fun e => ih.2 (O.pos_mul_eq_zero _ _ zz.1 zz.2 e)⟩

/-- the reference as a list of length E + 1 -/
def refL (h : Nat → R) (N0 : Nat) (r : R) (E : Nat) : List R := MigdalCone.lmap (valB K O F h N0 r E) 0 (E + 1)

theorem refL_len (h : Nat → R) (N0 : Nat) (r : R) (E : Nat) : (refL K O F h N0 r E).length = E + 1 :=
  WilsonGood.len_lmap _ 0 (E + 1)

theorem co_refL (h : Nat → R) (N0 : Nat) (r : R) (E n : Nat) (hn : n ≤ E) :
    co K (refL K O F h N0 r E) n = valB K O F h N0 r E n := by
  unfold refL
  rw [RatioStep.co_lmap K _ (E + 1) 0 n (Nat.lt_succ_of_le hn), Nat.zero_add]

theorem co_refL_out (h : Nat → R) (N0 : Nat) (r : R) (E n : Nat) (hn : E < n) : co K (refL K O F h N0 r E) n = K.zero := by
  unfold refL
  exact RatioStep.co_lmap_ge K _ (E + 1) 0 n hn

/-- **the reference tail is v-good**: VV at every m with N0 ≤ m and m + 2 ≤ E (denominators nonzero) -/
theorem refL_vv_tail (h : Nat → R) (N0 : Nat) (r : R) (E m : Nat) (hm0 : N0 ≤ m) (hmE : m + 2 ≤ E)
    (hd : ∀ k, den K O F r k ≠ K.zero) : HalfRatio.VV K O (co K (refL K O F h N0 r E)) m := by
  unfold HalfRatio.VV
  have l0 : m ≤ E := Nat.le_trans (Nat.le_add_right m 2) hmE
  have l1 : m + 1 ≤ E := Nat.le_trans (Nat.le_succ_of_le (Nat.le_refl _)) hmE
  rw [co_refL K O F h N0 r E m l0, co_refL K O F h N0 r E (m + 1) l1, co_refL K O F h N0 r E (m + 2) hmE]
  have e1 := valB_tail K O F h N0 r E m hm0
  have e2 := valB_tail K O F h N0 r E (m + 1) (Nat.le_succ_of_le hm0)
  have hZ := zB_vv K O F r E m hmE hd
  have e3 : co K (refL K O F h N0 r E) (m + 3) = K.mul (zB K O F r E (m + 2)) (valB K O F h N0 r E (m + 2)) := by
    cases Nat.lt_or_ge (m + 2) E with
    | inl hlt =>
      rw [co_refL K O F h N0 r E (m + 3) hlt]
      exact valB_tail K O F h N0 r E (m + 2) (Nat.le_trans hm0 (Nat.le_add_right m 2))
    | inr hge =>
      have eE : m + 2 = E := Nat.le_antisymm hmE hge
      rw [co_refL_out K O F h N0 r E (m + 3) (by rw [← eE]; exact Nat.lt_succ_self _), zB_ge K O F r E (m + 2) hge]
      lib_ring
  rw [e3]
  exact vv_of_ratio K O _ _ _ _ _ _ _ e1 e2 rfl hZ

/-- **VV at the end** (m + 2 > E, so Y_{m+2} = 0): holds for any nonnegative list -/
theorem refL_vv_end (h : Nat → R) (N0 : Nat) (r : R) (E m : Nat) (hm : E < m + 2)
    (hnn : ∀ n, O.le K.zero (co K (refL K O F h N0 r E) n)) : HalfRatio.VV K O (co K (refL K O F h N0 r E)) m := by
  unfold HalfRatio.VV
  rw [co_refL_out K O F h N0 r E (m + 2) hm]
  have a := O.add_le_add _ _ _ _ (hnn (m + 1)) (hnn (m + 3))
  have b := O.mul_nonneg _ _ (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) a) (hnn (m + 1))
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) b


/-- **junction VV in ratio form**: 0 ≤ b ≤ ρ, r ≤ c, a·r ≤ 1, a ≥ 0 and the endpoint check ρ(1 + aρ) ≤ a(1 + ρr)
  give b(1 + ab) ≤ a(1 + bc) -/
theorem junction_vv (h1 : O.le K.zero K.one) (a b c r ρ : R) (ha : O.le K.zero a) (hb : O.le K.zero b)
    (hbρ : O.le b ρ) (hr : O.le K.zero r) (hrc : O.le r c) (har : O.le (K.mul a r) K.one)
    (hend : O.le (K.mul ρ (K.add K.one (K.mul a ρ))) (K.mul a (K.add K.one (K.mul ρ r)))) :
    O.le (K.mul b (K.add K.one (K.mul a b))) (K.mul a (K.add K.one (K.mul b c))) := by
  -- (ρ − b)(1 + a(ρ + b) − ar) ≥ 0
  have d0 : O.le K.zero (K.add ρ (K.neg b)) := by
    have := O.add_le_add _ _ _ _ hbρ (O.le_refl (K.neg b))
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring)))
  have ρ0 : O.le K.zero ρ := O.le_trans _ _ _ hb hbρ
  have f0 : O.le K.zero (K.add (K.add K.one (K.neg (K.mul a r))) (K.mul a (K.add ρ b))) := by
    have x : O.le K.zero (K.add K.one (K.neg (K.mul a r))) := by
      have := O.add_le_add _ _ _ _ har (O.le_refl (K.neg (K.mul a r)))
      exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring)))
    have y : O.le K.zero (K.mul a (K.add ρ b)) := by
      have := O.add_le_add _ _ _ _ ρ0 hb
      exact O.mul_nonneg _ _ ha (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this)
    have := O.add_le_add _ _ _ _ x y
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this
  have P := O.mul_nonneg _ _ d0 f0
  -- RHS with c ≥ r
  have cr : O.le (K.mul a (K.add K.one (K.mul b r))) (K.mul a (K.add K.one (K.mul b c))) := by
    have u := O.mul_le_mul_right _ _ (K.mul a b) hrc (O.mul_nonneg _ _ ha hb)
    have v := O.add_le_add _ _ _ _ (O.le_refl a) u
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ v (TripleE.le_of_eq' K O _ _ (by lib_ring)))
  -- b(1+ab) + P = ρ(1+aρ) − a(1+ρr) + a(1+br) ≤ a(1+br)
  have s := O.add_le_add _ _ _ _ hend (O.le_refl (K.add (K.mul a (K.add K.one (K.mul b r))) (K.neg (K.mul a (K.add K.one (K.mul ρ r))))))
  have t := O.add_le_add _ _ _ _ (O.le_refl (K.mul b (K.add K.one (K.mul a b)))) P
  refine O.le_trans _ _ _ (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) t) (O.le_trans _ _ _
    (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ s (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) cr)))

/-- ratio VV inequality ⇒ list VV: y₁ = a·y₀, y₂ = b·y₁, y₃ = c·y₂, a, y₀ ≥ 0 and b(1 + ab) ≤ a(1 + bc) -/
theorem vv_of_ratio_le (y0 y1 y2 y3 a b c : R) (ha : O.le K.zero a) (hy0 : O.le K.zero y0) (e1 : y1 = K.mul a y0)
    (e2 : y2 = K.mul b y1) (e3 : y3 = K.mul c y2)
    (hY : O.le (K.mul b (K.add K.one (K.mul a b))) (K.mul a (K.add K.one (K.mul b c)))) :
    O.le (K.mul (K.add y0 y2) y2) (K.mul (K.add y1 y3) y1) := by
  rw [e3, e2, e1]
  have w := O.mul_le_mul_right _ _ (K.mul a (K.mul y0 y0)) hY (O.mul_nonneg _ _ ha (O.mul_nonneg _ _ hy0 hy0))
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **head VV from the natural-number check**: head h n = cN(coN H n); vvOK H Mx = true gives VV of the reference at
  every m < Mx with m + 3 ≤ N0 (and N0 ≤ E) -/
theorem refL_vv_head (h1 : O.le K.zero K.one) (H : List Nat) (N0 : Nat) (r : R) (E Mx : Nat) (hNE : N0 ≤ E)
    (hok : GeoTail.vvOK H Mx = true) (m : Nat) (hm : m < Mx) (hm3 : m + 3 ≤ N0) :
    HalfRatio.VV K O (co K (refL K O F (fun n => cN K (HeatLadder.coN H n)) N0 r E)) m := by
  have v := GeoTail.vvOK_sound K O h1 H Mx hok m hm
  unfold HalfRatio.VV at v ⊢
  have e : ∀ n, n ≤ N0 → co K (refL K O F (fun n => cN K (HeatLadder.coN H n)) N0 r E) n = co K (H.map (cN K)) n :=
    fun n hn => by
      rw [co_refL K O F _ N0 r E n (Nat.le_trans hn hNE), valB_head K O F _ N0 r E n hn, GeoTail.co_mapN K H n]
  have l2 : m + 2 ≤ N0 := Nat.le_trans (Nat.le_succ _) hm3
  have l1 : m + 1 ≤ N0 := Nat.le_trans (Nat.le_succ _) l2
  have l0 : m ≤ N0 := Nat.le_trans (Nat.le_succ _) l1
  rw [e m l0, e (m + 1) l1, e (m + 2) l2, e (m + 3) hm3]
  exact v

/-- **junction VV in list form** (at the first tail index): Y₂ = b·Y₁, Y₃ = c·Y₂ with 0 ≤ b ≤ ρ, r ≤ c, r·Y₁ ≤ Y₀,
  and the endpoint check (Y₀ + ρY₁)ρ ≤ Y₁(1 + ρr) give (Y₀ + Y₂)Y₂ ≤ (Y₁ + Y₃)Y₁ -/
theorem junction_list (Y0 Y1 Y2 Y3 b c r ρ : R) (hY0 : O.le K.zero Y0) (hY1 : O.le K.zero Y1) (hb : O.le K.zero b)
    (hbρ : O.le b ρ) (hr : O.le K.zero r) (hrc : O.le r c) (hrY : O.le (K.mul r Y1) Y0)
    (e2 : Y2 = K.mul b Y1) (e3 : Y3 = K.mul c Y2)
    (hend : O.le (K.mul (K.add Y0 (K.mul ρ Y1)) ρ) (K.mul Y1 (K.add K.one (K.mul ρ r)))) :
    O.le (K.mul (K.add Y0 Y2) Y2) (K.mul (K.add Y1 Y3) Y1) := by
  rw [e3, e2]
  have ρ0 : O.le K.zero ρ := O.le_trans _ _ _ hb hbρ
  -- g(ρ) − g(b) = (ρ − b)(Y₀ − rY₁ + (ρ + b)Y₁) ≥ 0, g(x) = (Y₀ + xY₁)x − Y₁(1 + xr)
  have d0 : O.le K.zero (K.add ρ (K.neg b)) := by
    have := O.add_le_add _ _ _ _ hbρ (O.le_refl (K.neg b))
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring)))
  have f0 : O.le K.zero (K.add (K.add Y0 (K.neg (K.mul r Y1))) (K.mul (K.add ρ b) Y1)) := by
    have x : O.le K.zero (K.add Y0 (K.neg (K.mul r Y1))) := by
      have := O.add_le_add _ _ _ _ hrY (O.le_refl (K.neg (K.mul r Y1)))
      exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ this (TripleE.le_of_eq' K O _ _ (by lib_ring)))
    have y : O.le K.zero (K.mul (K.add ρ b) Y1) := by
      have := O.add_le_add _ _ _ _ ρ0 hb
      exact O.mul_nonneg _ _ (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this) hY1
    have := O.add_le_add _ _ _ _ x y
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this
  have P := O.mul_nonneg _ _ d0 f0
  -- (Y₀ + bY₁)b + P = (Y₀ + ρY₁)ρ − Y₁(1 + ρr) + Y₁(1 + br) ≤ Y₁(1 + br) ≤ Y₁(1 + bc)
  have s := O.add_le_add _ _ _ _ hend (O.le_refl (K.add (K.mul Y1 (K.add K.one (K.mul b r))) (K.neg (K.mul Y1 (K.add K.one (K.mul ρ r))))))
  have t := O.add_le_add _ _ _ _ (O.le_refl (K.mul (K.add Y0 (K.mul b Y1)) b)) P
  have cr : O.le (K.mul Y1 (K.add K.one (K.mul b r))) (K.mul Y1 (K.add K.one (K.mul b c))) := by
    have u := O.mul_le_mul_right _ _ (K.mul b Y1) hrc (O.mul_nonneg _ _ hb hY1)
    have v := O.add_le_add _ _ _ _ (O.le_refl Y1) u
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ v (TripleE.le_of_eq' K O _ _ (by lib_ring)))
  have core : O.le (K.mul (K.add Y0 (K.mul b Y1)) b) (K.mul Y1 (K.add K.one (K.mul b c))) :=
    O.le_trans _ _ _ (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) t) (O.le_trans _ _ _
      (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ s (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) cr)))
  have w := O.mul_le_mul_right _ _ Y1 core hY1
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **the reference list is v-good** (N0 = P + 2): head VV from the natural-number check (m < P), the two junction
  positions m = P, P + 1 as hypotheses, the tail and the end from the VV-equality construction -/
theorem refL_good (h1 : O.le K.zero K.one) (H : List Nat) (P : Nat) (r : R) (E : Nat) (hNE : P + 2 ≤ E)
    (hok : GeoTail.vvOK H P = true) (hd : ∀ k, den K O F r k ≠ K.zero)
    (hpos : ∀ n, n ≤ E → O.le K.zero (valB K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E n) ∧
      valB K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E n ≠ K.zero)
    (hJ1 : HalfRatio.VV K O (co K (refL K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E)) P)
    (hJ2 : HalfRatio.VV K O (co K (refL K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E)) (P + 1))
    (hbase : O.le (K.mul (co K (refL K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E) 1)
        (co K (refL K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E) 1))
      (K.mul (K.add (co K (refL K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E) 0)
        (co K (refL K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E) 2))
        (co K (refL K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E) 0))) :
    L1Full.Good K O (refL K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E) := by
  have nn : ∀ n, O.le K.zero (co K (refL K O F (fun n => cN K (HeatLadder.coN H n)) (P + 2) r E) n) := fun n => by
    cases Nat.lt_or_ge E n with
    | inl h => rw [co_refL_out K O F _ _ r E n h]; exact O.le_refl _
    | inr h => rw [co_refL K O F _ _ r E n h]; exact (hpos n h).1
  refine ⟨nn, fun j hj => ?_, fun m => ?_, hbase⟩
  · rw [refL_len] at hj
    rw [co_refL K O F _ _ r E j (Nat.le_of_lt_succ hj)]
    exact (hpos j (Nat.le_of_lt_succ hj)).2
  · cases Nat.lt_or_ge m P with
    | inl hmP =>
      exact refL_vv_head K O F h1 H (P + 2) r E P (Nat.le_trans (Nat.le_refl _) hNE) hok m hmP
        (Nat.succ_le_succ (Nat.succ_le_succ hmP))
    | inr hmP =>
      cases Nat.lt_or_ge m (P + 2) with
      | inl hm2 =>
        cases Nat.lt_or_ge m (P + 1) with
        | inl hm1 =>
          have e : m = P := Nat.le_antisymm (Nat.le_of_lt_succ hm1) hmP
          rw [e]; exact hJ1
        | inr hm1 =>
          have e : m = P + 1 := Nat.le_antisymm (Nat.le_of_lt_succ hm2) hm1
          rw [e]; exact hJ2
      | inr hm2 =>
        cases Nat.lt_or_ge E (m + 2) with
        | inl hE => exact refL_vv_end K O F _ _ r E m hE nn
        | inr hE => exact refL_vv_tail K O F _ (P + 2) r E m hm2 hE hd

/-- **junction VV at the last head position**: Y₃ = c·Y₂ with r ≤ c, Y₁, Y₂ ≥ 0 and the check
  (Y₀ + Y₂)Y₂ ≤ (Y₁ + rY₂)Y₁ give (Y₀ + Y₂)Y₂ ≤ (Y₁ + Y₃)Y₁ -/
theorem junction1_list (Y0 Y1 Y2 Y3 c r : R) (hY1 : O.le K.zero Y1) (hY2 : O.le K.zero Y2) (hrc : O.le r c)
    (e3 : Y3 = K.mul c Y2) (hchk : O.le (K.mul (K.add Y0 Y2) Y2) (K.mul (K.add Y1 (K.mul r Y2)) Y1)) :
    O.le (K.mul (K.add Y0 Y2) Y2) (K.mul (K.add Y1 Y3) Y1) := by
  rw [e3]
  have u := O.mul_le_mul_right _ _ Y2 hrc hY2
  have v := O.add_le_add _ _ _ _ (O.le_refl Y1) u
  have w := O.mul_le_mul_right _ _ Y1 v hY1
  exact O.le_trans _ _ _ hchk w

/-- **lower geometric envelope**: if every tail ratio is ≥ σ (and the values are ≥ 0), the reference lies above the
  geometric-tail list with ratio σ on [0, E] -/
theorem valB_ge_geo (h : Nat → R) (N0 : Nat) (r σ : R) (E : Nat) (hσ0 : O.le K.zero σ)
    (hge : ∀ m, N0 ≤ m → m < E → O.le σ (zB K O F r E m))
    (hnn : ∀ n, n ≤ E → O.le K.zero (valB K O F h N0 r E n)) :
    ∀ n, n ≤ E → O.le (GeoTail.geoF K h N0 σ n) (valB K O F h N0 r E n)
  | 0, _ => O.le_refl _
  | n + 1, hn => by
    cases Nat.lt_or_ge n N0 with
    | inl hl =>
      rw [valB_head K O F h N0 r E (n + 1) hl]
      show O.le (if n + 1 ≤ N0 then h (n + 1) else K.mul σ (GeoTail.geoF K h N0 σ n)) (h (n + 1))
      rw [if_pos (show n + 1 ≤ N0 from hl)]; exact O.le_refl _
    | inr hg =>
      rw [valB_tail K O F h N0 r E n hg, GeoTail.geoF_step K h N0 σ n hg]
      have ih := valB_ge_geo h N0 r σ E hσ0 hge hnn n (Nat.le_trans (Nat.le_succ n) hn)
      have z := hge n hg (Nat.lt_of_succ_le hn)
      have v0 := hnn n (Nat.le_trans (Nat.le_succ n) hn)
      have a := O.mul_le_mul_right _ _ σ ih hσ0
      have b := O.mul_le_mul_right _ _ (valB K O F h N0 r E n) z v0
      exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ a
        (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) b))

/-- **upper geometric envelope**: if every tail ratio is ≤ σ and ≥ 0, the reference lies below the geometric-tail
  list with ratio σ on [0, E] (head values ≥ 0) -/
theorem valB_le_geo (h : Nat → R) (N0 : Nat) (r σ : R) (E : Nat)
    (hle : ∀ m, N0 ≤ m → m < E → O.le (zB K O F r E m) σ ∧ O.le K.zero (zB K O F r E m))
    (hnn : ∀ n, n ≤ E → O.le K.zero (valB K O F h N0 r E n)) :
    ∀ n, n ≤ E → O.le (valB K O F h N0 r E n) (GeoTail.geoF K h N0 σ n)
  | 0, _ => O.le_refl _
  | n + 1, hn => by
    cases Nat.lt_or_ge n N0 with
    | inl hl =>
      rw [valB_head K O F h N0 r E (n + 1) hl]
      show O.le (h (n + 1)) (if n + 1 ≤ N0 then h (n + 1) else K.mul σ (GeoTail.geoF K h N0 σ n))
      rw [if_pos (show n + 1 ≤ N0 from hl)]; exact O.le_refl _
    | inr hg =>
      rw [valB_tail K O F h N0 r E n hg, GeoTail.geoF_step K h N0 σ n hg]
      have ih := valB_le_geo h N0 r σ E hle hnn n (Nat.le_trans (Nat.le_succ n) hn)
      have z := hle n hg (Nat.lt_of_succ_le hn)
      have v0 := hnn n (Nat.le_trans (Nat.le_succ n) hn)
      have σ0 : O.le K.zero σ := O.le_trans _ _ _ z.2 z.1
      have a := O.mul_le_mul_right _ _ (valB K O F h N0 r E n) z.1 v0
      have b := O.mul_le_mul_right _ _ σ ih σ0
      exact O.le_trans _ _ _ a (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ b
        (TripleE.le_of_eq' K O _ _ (by lib_ring))))

end RefB

#print axioms RefB.pairB_vv
#print axioms RefB.pairB_snd
#print axioms RefB.valB_tail
#print axioms RefB.zB_ge
#print axioms RefB.idx1
#print axioms RefB.idx2
#print axioms RefB.zB_vv
#print axioms RefB.zB_last
#print axioms RefB.vv_of_ratio
#print axioms RefB.pairB_pos
#print axioms RefB.valB_head
#print axioms RefB.valB_pos
#print axioms RefB.refL_len
#print axioms RefB.co_refL
#print axioms RefB.co_refL_out
#print axioms RefB.refL_vv_tail
#print axioms RefB.refL_vv_end
#print axioms RefB.junction_vv
#print axioms RefB.vv_of_ratio_le
#print axioms RefB.refL_vv_head
#print axioms RefB.junction_list
#print axioms RefB.refL_good
#print axioms RefB.junction1_list
#print axioms RefB.valB_ge_geo
#print axioms RefB.valB_le_geo

#print axioms: 25 theorems, no axioms.

90.140 One rung and the ladder

Head cross inequalities and tail shooting carry \(X\preceq M\) to \(\mathrm {mk}(X)\preceq M'\); the rungs iterate.

\[ X\preceq \mathrm {Ref}_k\ \Rightarrow \ \mathrm {mk}(X)\preceq \mathrm {Ref}_{k+1} \]

ym/RatioSeq.lean

/-
  ym/RatioSeq — T45, G78g route (B): the ratio sequence y_n = Y_{n+1}/Y_n of a v-good list Y of length L.
  It is nonnegative, nonincreasing (log-concavity, VGoodLC.good_lc; zero after the end), and satisfies the ratio
  form of VV, b(1 + ab) ≤ a(1 + bc), for every m with m + 2 < L (TailShoot.ratio_vv). These are the hypotheses of
  TailShoot.tail_cmp on the flow side. Lean 4 core only, no axioms.
-/
import ym.TailShoot

namespace RatioSeq

open YMChain LinkIntegral SU2Char HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- the ratio sequence -/
def yr (Y : List R) (n : Nat) : R := K.mul (co K Y (n + 1)) (F.inv (co K Y n))

theorem yr_nn (Y : List R) (hg : L1Full.Good K O Y) (n : Nat) : O.le K.zero (yr K O F Y n) := by
  unfold yr
  cases Nat.lt_or_ge n Y.length with
  | inl h => exact O.mul_nonneg _ _ (hg.1 _) (F.inv_pos _ (hg.1 n) (hg.2.1 n h))
  | inr h =>
    rw [HeatLR.co_out K Y (n + 1) (Nat.le_succ_of_le h)]
    exact TripleE.le_of_eq' K O _ _ (by lib_ring)

/-- **the ratios decrease** -/
theorem yr_dec (Y : List R) (hg : L1Full.Good K O Y) (m : Nat) : O.le (yr K O F Y (m + 1)) (yr K O F Y m) := by
  unfold yr
  cases Nat.lt_or_ge (m + 1) Y.length with
  | inr h =>
    rw [HeatLR.co_out K Y (m + 1 + 1) (Nat.le_succ_of_le h)]
    refine O.le_trans _ K.zero _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) ?_
    cases Nat.lt_or_ge m Y.length with
    | inl h' => exact O.mul_nonneg _ _ (hg.1 _) (F.inv_pos _ (hg.1 m) (hg.2.1 m h'))
    | inr h' => rw [HeatLR.co_out K Y (m + 1) (Nat.le_succ_of_le h')]; exact TripleE.le_of_eq' K O _ _ (by lib_ring)
  | inl h =>
    have h' : m < Y.length := Nat.lt_of_succ_lt h
    have p0 := hg.1 m
    have n0 := hg.2.1 m h'
    have p1 := hg.1 (m + 1)
    have n1 := hg.2.1 (m + 1) h
    have lc : O.le (K.mul (co K Y m) (co K Y (m + 1 + 1))) (K.mul (co K Y (m + 1)) (co K Y (m + 1))) :=
      VGoodLC.good_lc K O Y hg m
    -- Y_{m+2}/Y_{m+1} ≤ Y_{m+1}/Y_m: multiply by Y_m Y_{m+1} > 0
    have P : O.le K.zero (K.mul (co K Y m) (co K Y (m + 1))) := O.mul_nonneg _ _ p0 p1
    have Pn : K.mul (co K Y m) (co K Y (m + 1)) ≠ K.zero := fun e => n1 (O.pos_mul_eq_zero _ _ p0 n0 e)
    refine O.pos_cancel _ _ _ P Pn ?_
    have e1 : K.mul (K.mul (co K Y (m + 1 + 1)) (F.inv (co K Y (m + 1)))) (K.mul (co K Y m) (co K Y (m + 1)))
        = K.mul (K.mul (co K Y m) (co K Y (m + 1 + 1))) (K.mul (co K Y (m + 1)) (F.inv (co K Y (m + 1)))) := by lib_ring
    have e2 : K.mul (K.mul (co K Y (m + 1)) (F.inv (co K Y m))) (K.mul (co K Y m) (co K Y (m + 1)))
        = K.mul (K.mul (co K Y (m + 1)) (co K Y (m + 1))) (K.mul (co K Y m) (F.inv (co K Y m))) := by lib_ring
    rw [e1, e2, F.mul_inv _ n1, F.mul_inv _ n0]
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ lc (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **ratio VV** for m + 2 < L -/
theorem yr_vv (Y : List R) (hg : L1Full.Good K O Y) (m : Nat) (hm : m + 2 < Y.length) :
    O.le (K.mul (yr K O F Y (m + 1)) (K.add K.one (K.mul (yr K O F Y m) (yr K O F Y (m + 1)))))
      (K.mul (yr K O F Y m) (K.add K.one (K.mul (yr K O F Y (m + 1)) (yr K O F Y (m + 2))))) := by
  have h1 : m + 1 < Y.length := Nat.lt_trans (Nat.lt_succ_self _) hm
  have h0 : m < Y.length := Nat.lt_trans (Nat.lt_succ_self _) h1
  have vv := hg.2.2.1 m
  unfold HalfRatio.VV at vv
  unfold yr
  exact TailShoot.ratio_vv K O F (co K Y m) (co K Y (m + 1)) (co K Y (m + 2)) (co K Y (m + 3)) (hg.1 m) (hg.2.1 m h0)
    (hg.1 (m + 1)) (hg.2.1 (m + 1) h1) (hg.2.1 (m + 2) hm) vv
/-- **a coefficient bound gives a ratio bound**: Y_{n+1} ≤ σ·Y_n with Y_n > 0 gives yr Y n ≤ σ -/
theorem yr_le_of (Y : List R) (n : Nat) (σ : R) (p : O.le K.zero (co K Y n)) (nz : co K Y n ≠ K.zero)
    (h : O.le (co K Y (n + 1)) (K.mul σ (co K Y n))) : O.le (yr K O F Y n) σ := by
  unfold yr
  have ip := F.inv_pos _ p nz
  have w := O.mul_le_mul_right _ _ (F.inv (co K Y n)) h ip
  have e : K.mul (K.mul σ (co K Y n)) (F.inv (co K Y n)) = σ := by
    rw [show K.mul (K.mul σ (co K Y n)) (F.inv (co K Y n)) = K.mul σ (K.mul (co K Y n) (F.inv (co K Y n))) by lib_ring,
      F.mul_inv _ nz]
    lib_ring
  rw [e] at w
  exact w

/-- **ratio bound from two-sided bounds**: lo ≤ Y_n, Y_{n+1} ≤ up, up ≤ σ·lo, σ ≥ 0, Y_n > 0 give yr Y n ≤ σ
  (the junction hypothesis hN of RungB.rung_B / below_of_ref from the certified head bounds) -/
theorem yr_le_of_bounds (Y : List R) (n : Nat) (σ lo up : R) (p : O.le K.zero (co K Y n)) (nz : co K Y n ≠ K.zero)
    (hσ : O.le K.zero σ) (hlo : O.le lo (co K Y n)) (hup : O.le (co K Y (n + 1)) up)
    (hchk : O.le up (K.mul σ lo)) : O.le (yr K O F Y n) σ := by
  refine yr_le_of K O F Y n σ p nz (O.le_trans _ _ _ hup (O.le_trans _ _ _ hchk ?_))
  have w := O.mul_le_mul_right _ _ σ hlo hσ
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring)))

end RatioSeq

#print axioms RatioSeq.yr_nn
#print axioms RatioSeq.yr_dec
#print axioms RatioSeq.yr_vv
#print axioms RatioSeq.yr_le_of
#print axioms RatioSeq.yr_le_of_bounds

ym/RungB.lean

/-
  ym/RungB — T45, G78g route (B): one rung of the ladder against a reference whose tail ratios z satisfy the
  VV equality. Tail part: the ratio sequence of the v-good list Y = mk Mr (RatioSeq) is compared with z by forward
  shooting (TailShoot.tail_cmp, indices shifted by N, end N + T). Lean 4 core only, no axioms.
-/
import ym.RatioSeq
import ym.HeatStepMK
import ym.FlowIter
import ym.FlowRC
import ym.FlowRatio

namespace RungB

open YMChain LinkIntegral SU2Char HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- ratios of a v-good list are nonzero before the end -/
theorem yr_ne (h10 : K.one ≠ K.zero) (Y : List R) (hg : L1Full.Good K O Y) (n : Nat) (hn : n + 1 < Y.length) :
    RatioSeq.yr K O F Y n ≠ K.zero := fun e => by
  unfold RatioSeq.yr at e
  have nz := hg.2.1 n (Nat.lt_trans (Nat.lt_succ_self n) hn)
  have ip := F.inv_pos _ (hg.1 n) nz
  have inz : F.inv (co K Y n) ≠ K.zero := fun e' => h10 (by
    have := F.mul_inv _ nz
    rw [e'] at this
    rw [← this]; lib_ring)
  rw [K.mul_comm] at e
  exact hg.2.1 (n + 1) hn (O.pos_mul_eq_zero _ _ ip inz e)

theorem mul_ne (a b : R) (ha : O.le K.zero a) (hane : a ≠ K.zero) (hbne : b ≠ K.zero) : K.mul a b ≠ K.zero :=
  fun e => hbne (O.pos_mul_eq_zero _ _ ha hane e)

/-- **tail comparison for a v-good list against a VV-equality ratio sequence** (from N to N + T) -/
theorem tail_vs_ref (h10 : K.one ≠ K.zero) (Y : List R) (hg : L1Full.Good K O Y) (N T : Nat)
    (hL : Y.length = N + T + 1) (z : Nat → R)
    (hz0 : ∀ k, O.le K.zero (z (N + k))) (hzne : ∀ k, k < T → z (N + k) ≠ K.zero)
    (hzd : ∀ k, k + 1 ≤ T → O.le (z (N + (k + 1))) (z (N + k)))
    (hZ : ∀ k, k + 2 ≤ T → K.mul (z (N + (k + 1))) (K.add K.one (K.mul (z (N + k)) (z (N + (k + 1)))))
      = K.mul (z (N + k)) (K.add K.one (K.mul (z (N + (k + 1))) (z (N + (k + 2))))))
    (hzE : z (N + T) = K.zero) (hN : O.le (RatioSeq.yr K O F Y N) (z N)) :
    ∀ i, i ≤ T → O.le (RatioSeq.yr K O F Y (N + i)) (z (N + i)) := by
  have lt1 : ∀ k, k < T → N + k + 1 < Y.length := fun k hk => by
    rw [hL]; exact Nat.succ_lt_succ (Nat.add_lt_add_left hk N)
  have r := TailShoot.tail_cmp K O (fun k => RatioSeq.yr K O F Y (N + k)) (fun k => z (N + k)) 0 T
    (fun k => RatioSeq.yr_nn K O F Y hg (N + k)) hz0 (fun k => RatioSeq.yr_dec K O F Y hg (N + k)) hzd
    (fun k hk => by
      have k0 : k < T := Nat.lt_of_lt_of_le (Nat.lt_succ_of_lt (Nat.lt_succ_self k)) hk
      have k1 : k + 1 < T := hk
      have y0 := yr_ne K O F h10 Y hg (N + k) (lt1 k k0)
      have y1 := yr_ne K O F h10 Y hg (N + (k + 1)) (lt1 (k + 1) k1)
      exact mul_ne K O _ _ (O.mul_nonneg _ _ (RatioSeq.yr_nn K O F Y hg _) (RatioSeq.yr_nn K O F Y hg _))
        (mul_ne K O _ _ (RatioSeq.yr_nn K O F Y hg _) y0 y1)
        (mul_ne K O _ _ (hz0 k) (hzne k k0) (hzne (k + 1) k1)))
    (fun k hk => RatioSeq.yr_vv K O F Y hg (N + k) (by rw [hL]; exact Nat.succ_lt_succ (Nat.add_lt_add_left hk N)))
    hZ hN (by
      show O.le (RatioSeq.yr K O F Y (N + T)) (z (N + T))
      rw [hzE]; unfold RatioSeq.yr
      rw [HeatLR.co_out K Y (N + T + 1) (by rw [hL]; exact Nat.le_refl _)]
      exact TripleE.le_of_eq' K O _ _ (by lib_ring))
  intro i hi
  have := r i (by rw [Nat.zero_add]; exact hi)
  rw [Nat.zero_add] at this
  exact this

/-- **ratio bound ⇒ cross inequality** against a reference with M′_{n+1} = z·M′_n (Y v-good) -/
theorem cross_tail (Y M' : List R) (hg : L1Full.Good K O Y) (n : Nat) (z : R) (hM0 : O.le K.zero (co K M' n))
    (hM : co K M' (n + 1) = K.mul z (co K M' n)) (hle : O.le (RatioSeq.yr K O F Y n) z) :
    O.le (K.mul (co K Y (n + 1)) (co K M' n)) (K.mul (co K Y n) (co K M' (n + 1))) := by
  rw [hM]
  cases Nat.lt_or_ge n Y.length with
  | inr h =>
    rw [HeatLR.co_out K Y (n + 1) (Nat.le_succ_of_le h), HeatLR.co_out K Y n h]
    exact TripleE.le_of_eq' K O _ _ (by lib_ring)
  | inl h =>
    have nz := hg.2.1 n h
    have e : co K Y (n + 1) = K.mul (RatioSeq.yr K O F Y n) (co K Y n) := by
      unfold RatioSeq.yr
      calc co K Y (n + 1) = K.mul (co K Y (n + 1)) (K.mul (co K Y n) (F.inv (co K Y n))) := by
            rw [F.mul_inv _ nz]; lib_ring
        _ = _ := by lib_ring
    rw [e]
    have w := O.mul_le_mul_right _ _ (K.mul (co K Y n) (co K M' n)) hle (O.mul_nonneg _ _ (hg.1 n) hM0)
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **one rung of route (B)**: X ≼ Mr (Mr v-good, a nonincreasing) and a v-good reference M′ (same length as mk Mr)
  with head cross inequalities n < N and tail ratios z (VV equality, z_{N+T} = 0) dominating the flow ratio at N
  give mk X ≼ M′ -/
theorem rung_B (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X Mr M' : List R) (M : Nat)
    (hXg : L1Full.Good K O X) (hMg : L1Full.Good K O Mr) (haM : FlowStep.ANoninc K O Mr)
    (hX : X.length = M + 2) (hM : Mr.length = M + 2) (hb : FlowCmp.Below K O X Mr)
    (hM'g : L1Full.Good K O M') (N T : Nat) (hNT : 4 * M + 5 = N + T + 1) (hM'l : M'.length = 4 * M + 5)
    (hhead : ∀ n, n < N → O.le (K.mul (co K (FlowStep.mk K O F Mr M) (n + 1)) (co K M' n))
      (K.mul (co K (FlowStep.mk K O F Mr M) n) (co K M' (n + 1))))
    (z : Nat → R) (hz : ∀ n, N ≤ n → co K M' (n + 1) = K.mul (z n) (co K M' n))
    (hz0 : ∀ k, O.le K.zero (z (N + k))) (hzne : ∀ k, k < T → z (N + k) ≠ K.zero)
    (hzd : ∀ k, k + 1 ≤ T → O.le (z (N + (k + 1))) (z (N + k)))
    (hZ : ∀ k, k + 2 ≤ T → K.mul (z (N + (k + 1))) (K.add K.one (K.mul (z (N + k)) (z (N + (k + 1)))))
      = K.mul (z (N + k)) (K.add K.one (K.mul (z (N + (k + 1))) (z (N + (k + 2))))))
    (hzE : z (N + T) = K.zero) (hN : O.le (RatioSeq.yr K O F (FlowStep.mk K O F Mr M) N) (z N)) :
    FlowCmp.Below K O (FlowStep.mk K O F X M) M' := by
  have fs := FlowStep.flow_step K O F h1 h10 Mr hMg M hM haM
  have hl : (FlowStep.mk K O F Mr M).length = N + T + 1 := by rw [FlowStep.len_mk]; exact hNT
  have tl := tail_vs_ref K O F h10 (FlowStep.mk K O F Mr M) fs.1 N T hl z hz0 hzne hzd hZ hzE hN
  have lr : ∀ n, O.le (K.mul (co K (FlowStep.mk K O F Mr M) (n + 1)) (co K M' n))
      (K.mul (co K (FlowStep.mk K O F Mr M) n) (co K M' (n + 1))) := fun n => by
    cases Nat.lt_or_ge n N with
    | inl h => exact hhead n h
    | inr h =>
      obtain ⟨i, hi⟩ := Nat.le.dest h
      cases Nat.lt_or_ge T i with
      | inl hTi =>
        -- beyond the end: Y_{n+1} = 0
        have hout : (FlowStep.mk K O F Mr M).length ≤ n + 1 := by
          rw [hl, ← hi]; exact Nat.succ_le_succ (Nat.le_of_lt (Nat.add_lt_add_left hTi N))
        rw [HeatLR.co_out K _ (n + 1) hout]
        exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring))
          (O.mul_nonneg _ _ (fs.1.1 n) (hM'g.1 (n + 1)))
      | inr hiT =>
        rw [← hi]
        exact cross_tail K O F _ M' fs.1 (N + i) (z (N + i)) (hM'g.1 _) (hz (N + i) (Nat.le_add_right N i)) (tl i hiT)
  have b2 := FlowCmp.ratio_formB K O F _ M' (4 * M + 5) (FlowStep.len_mk K O F Mr M) hM'l hM'g fs.1.1 lr
  exact HeatStepMK.below_trans K O _ _ _ (FlowCmp.mk_cmp K O F h1 h10 X Mr hXg hMg M hX hM haM hb) b2

/-- **the ladder**: if every rung carries "below Ref k" to "below Ref (k+1)" after one MK step (rung_B), then every
  flow iterate lies below its reference -/
theorem ladder_B (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X : List R) (M : Nat)
    (hXg : L1Full.Good K O X) (hXa : FlowStep.ANoninc K O X) (hX : X.length = M + 2) (Ref : Nat → List R)
    (hrung : ∀ k (Y : List R), L1Full.Good K O Y → Y.length = FlowIter.itM M k + 2 → FlowCmp.Below K O Y (Ref k) →
      FlowCmp.Below K O (FlowStep.mk K O F Y (FlowIter.itM M k)) (Ref (k + 1)))
    (hb0 : FlowCmp.Below K O X (Ref 0)) :
    ∀ k, FlowCmp.Below K O (FlowIter.itX K O F X M k) (Ref k) ∧ L1Full.Good K O (FlowIter.itX K O F X M k) ∧
      FlowStep.ANoninc K O (FlowIter.itX K O F X M k) ∧ (FlowIter.itX K O F X M k).length = FlowIter.itM M k + 2
  | 0 => ⟨hb0, hXg, hXa, hX⟩
  | k + 1 => by
    have ⟨hb, hg, ha, hl⟩ := ladder_B h1 h10 X M hXg hXa hX Ref hrung hb0 k
    have fs := FlowStep.flow_step K O F h1 h10 _ hg _ hl ha
    exact ⟨hrung k _ hg hl hb, fs.1, fs.2, FlowStep.len_mk K O F _ _⟩

/-- **the end of the ladder**: at the last rung K the reference has ratio class σ₀; from there the flow stays in the
  ratio class along any majorant σ_{j+1} ≥ (4σ_j)⁴ (FlowRC.below_RC, FlowRatio.flow_ratio) -/
theorem ladder_end (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X : List R) (M : Nat)
    (hXg : L1Full.Good K O X) (hXa : FlowStep.ANoninc K O X) (hX : X.length = M + 2) (Ref : Nat → List R)
    (hrung : ∀ k (Y : List R), L1Full.Good K O Y → Y.length = FlowIter.itM M k + 2 → FlowCmp.Below K O Y (Ref k) →
      FlowCmp.Below K O (FlowStep.mk K O F Y (FlowIter.itM M k)) (Ref (k + 1)))
    (hb0 : FlowCmp.Below K O X (Ref 0)) (Kend : Nat) (σ : Nat → R) (hσ : ∀ i, O.le K.zero (σ i))
    (hstep : ∀ i, O.le (pwR K (K.add (K.add (σ i) (σ i)) (K.add (σ i) (σ i))) 4) (σ (i + 1)))
    (hR : RatioStep.RC K O (σ 0) (Ref Kend)) :
    ∀ j, RatioStep.RC K O (σ j) (FlowIter.itX K O F (FlowIter.itX K O F X M Kend) (FlowIter.itM M Kend) j) := by
  have L := ladder_B K O F h1 h10 X M hXg hXa hX Ref hrung hb0 Kend
  have r0 := FlowRC.below_RC K O (σ 0) (hσ 0) _ _ L.1 hR
  intro j
  exact (FlowRatio.flow_ratio K O F h1 h10 _ _ L.2.1 L.2.2.1 L.2.2.2 σ hσ hstep r0 j).2.2.2

/-- **a v-good list below a VV-equality reference** (same length N + T + 1): head cross inequalities for n < N,
  tail ratios z (VV equality, z_{N+T} = 0) dominating the list ratio at N give Y ≼ M′ (used for the Wilson start and
  inside every rung) -/
theorem below_of_ref (h10 : K.one ≠ K.zero) (Y M' : List R) (hg : L1Full.Good K O Y) (hM'g : L1Full.Good K O M')
    (N T : Nat) (hYl : Y.length = N + T + 1) (hM'l : M'.length = N + T + 1)
    (hhead : ∀ n, n < N → O.le (K.mul (co K Y (n + 1)) (co K M' n)) (K.mul (co K Y n) (co K M' (n + 1))))
    (z : Nat → R) (hz : ∀ n, N ≤ n → co K M' (n + 1) = K.mul (z n) (co K M' n))
    (hz0 : ∀ k, O.le K.zero (z (N + k))) (hzne : ∀ k, k < T → z (N + k) ≠ K.zero)
    (hzd : ∀ k, k + 1 ≤ T → O.le (z (N + (k + 1))) (z (N + k)))
    (hZ : ∀ k, k + 2 ≤ T → K.mul (z (N + (k + 1))) (K.add K.one (K.mul (z (N + k)) (z (N + (k + 1)))))
      = K.mul (z (N + k)) (K.add K.one (K.mul (z (N + (k + 1))) (z (N + (k + 2))))))
    (hzE : z (N + T) = K.zero) (hN : O.le (RatioSeq.yr K O F Y N) (z N)) :
    FlowCmp.Below K O Y M' := by
  have tl := tail_vs_ref K O F h10 Y hg N T hYl z hz0 hzne hzd hZ hzE hN
  have lr : ∀ n, O.le (K.mul (co K Y (n + 1)) (co K M' n)) (K.mul (co K Y n) (co K M' (n + 1))) := fun n => by
    cases Nat.lt_or_ge n N with
    | inl h => exact hhead n h
    | inr h =>
      obtain ⟨i, hi⟩ := Nat.le.dest h
      cases Nat.lt_or_ge T i with
      | inl hTi =>
        have hout : Y.length ≤ n + 1 := by
          rw [hYl, ← hi]; exact Nat.succ_le_succ (Nat.le_of_lt (Nat.add_lt_add_left hTi N))
        rw [HeatLR.co_out K _ (n + 1) hout]
        exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.mul_nonneg _ _ (hg.1 n) (hM'g.1 (n + 1)))
      | inr hiT =>
        rw [← hi]
        exact cross_tail K O F _ M' hg (N + i) (z (N + i)) (hM'g.1 _) (hz (N + i) (Nat.le_add_right N i)) (tl i hiT)
  exact FlowCmp.ratio_formB K O F _ M' (N + T + 1) hYl hM'l hM'g hg.1 lr

end RungB

#print axioms RungB.yr_ne
#print axioms RungB.mul_ne
#print axioms RungB.tail_vs_ref
#print axioms RungB.cross_tail
#print axioms RungB.rung_B
#print axioms RungB.ladder_B
#print axioms RungB.ladder_end
#print axioms RungB.below_of_ref

#print axioms: 13 theorems, no axioms.

90.141 Wilson start of the ladder

For \(\beta \le 8\) the Wilson weight lies below a VV-equality reference.

\[ w_t\preceq M'\qquad (t^2\le 16,\ t\le (N+1)z_N) \]

ym/WilsonStartB.lean

/-
  ym/WilsonStartB — T45, G78g route (B): the start of the ladder. The truncated Wilson weight wl t 28 L
  (β = 2t ≤ 8, v-good by WilsonGood4.wl_good8) lies below a VV-equality reference M′ of the same length as soon as
  the head cross inequalities hold for n < N and the reference tail ratio z_N dominates T/(N+1) ≥ t/(N+1)
  (WilsonHeatLR.wl_ratio_geo, RatioSeq.yr_le_of). Lean 4 core only, no axioms.
-/
import ym.RungB
import ym.WilsonHeatLR
import ym.WilsonGood4

namespace WilsonStartB

open YMChain LinkIntegral SU2Char HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **the junction from the Wilson ratio bound**: (n+1)·σ ≥ T ≥ t (σ·b = a, T·b ≤ (N+1)·a) gives yr(wl) N ≤ σ -/
theorem wl_yr_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t σ : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero)
    (T a b N : Nat) (hb : 1 ≤ b) (htT : O.le t (cN K T)) (hσ : K.mul σ (cN K b) = cN K a) (hN : T * b ≤ (N + 1) * a)
    (Kk L : Nat) (hg : L1Full.Good K O (WilsonCone.wl K O F t Kk L)) (hNL : N < L) :
    O.le (RatioSeq.yr K O F (WilsonCone.wl K O F t Kk L) N) σ := by
  have hl : N < (WilsonCone.wl K O F t Kk L).length := by
    unfold WilsonCone.wl; rw [WilsonGood.len_lmap]; exact hNL
  exact RatioSeq.yr_le_of K O F _ N σ (hg.1 N) (hg.2.1 N hl)
    (WilsonHeatLR.wl_ratio_geo K O F h1 h10 t σ ht0 htne T a b N hb htT hσ hN Kk L)

/-- **the start of the ladder** (β = 2t ≤ 8, L = N + T′ + 1 ≥ 65): head cross inequalities for n < N, a
  VV-equality tail z with z_N = σ = a/b, (N+1)·a ≥ T·b, t ≤ T give wl t 28 L ≼ M′ -/
theorem wl_below_ref (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero)
    (ht : O.le (K.mul t t) (cN K 16)) (M' : List R) (hM'g : L1Full.Good K O M') (N T' : Nat) (hL : 65 ≤ N + T' + 1)
    (hM'l : M'.length = N + T' + 1)
    (hhead : ∀ n, n < N → O.le (K.mul (co K (WilsonCone.wl K O F t 28 (N + T' + 1)) (n + 1)) (co K M' n))
      (K.mul (co K (WilsonCone.wl K O F t 28 (N + T' + 1)) n) (co K M' (n + 1))))
    (z : Nat → R) (hz : ∀ n, N ≤ n → co K M' (n + 1) = K.mul (z n) (co K M' n))
    (hz0 : ∀ k, O.le K.zero (z (N + k))) (hzne : ∀ k, k < T' → z (N + k) ≠ K.zero)
    (hzd : ∀ k, k + 1 ≤ T' → O.le (z (N + (k + 1))) (z (N + k)))
    (hZ : ∀ k, k + 2 ≤ T' → K.mul (z (N + (k + 1))) (K.add K.one (K.mul (z (N + k)) (z (N + (k + 1)))))
      = K.mul (z (N + k)) (K.add K.one (K.mul (z (N + (k + 1))) (z (N + (k + 2))))))
    (hzE : z (N + T') = K.zero) (T a b : Nat) (hb : 1 ≤ b) (htT : O.le t (cN K T))
    (hσ : K.mul (z N) (cN K b) = cN K a) (hN : T * b ≤ (N + 1) * a) :
    FlowCmp.Below K O (WilsonCone.wl K O F t 28 (N + T' + 1)) M' := by
  have hg := WilsonGood4.wl_good8 K O F h1 h10 t ht0 htne ht (N + T' + 1) hL
  have hlen : (WilsonCone.wl K O F t 28 (N + T' + 1)).length = N + T' + 1 := by
    unfold WilsonCone.wl; rw [WilsonGood.len_lmap]
  have hNL : N < N + T' + 1 := Nat.lt_succ_of_le (Nat.le_add_right N T')
  exact RungB.below_of_ref K O F h10 _ M' hg hM'g N T' hlen hM'l hhead z hz hz0 hzne hzd hZ hzE
    (wl_yr_le K O F h1 h10 t (z N) ht0 htne T a b N hb htT hσ hN 28 (N + T' + 1) hg hNL)

end WilsonStartB

#print axioms WilsonStartB.wl_yr_le
#print axioms WilsonStartB.wl_below_ref

#print axioms: 2 theorems, no axioms.

90.142 Weighted mass

The weighted mass is multiplicative under the Clebsch–Gordan product and bounds geometric tails.

\[ W(X\cdot Y)=W(X)\,W(Y),\qquad (1-\rho )\,W(\text {tail})\le (N+1)X_N \]

ym/WMass.lean

/-
  ym/WMass — T45, G78g route (B): the weighted mass W(X) = Σ_k (k+1) X_k (the class function at the identity).
  One coefficient is bounded by it, (n+1)X_n ≤ W(X) for X ≥ 0, and it is multiplicative under the bond-moving
  product (CgDim.cgc_dim). Used for length-independent error bounds. Lean 4 core only, no axioms.
-/
import ym.CgDim
import ym.HeadBound
import ym.TripleA

namespace WMass

open YMChain LinkIntegral SU2Char HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- the weighted mass -/
def W (X : List R) : R := dot K 0 X (fun k => cN K (k + 1))

/-- dot with nonnegative entries and weights dominates one term: f(m+n)·X_n ≤ dot m X f -/
theorem dot_term (h1 : O.le K.zero K.one) (f : Nat → R) (hf : ∀ j, O.le K.zero (f j)) :
    ∀ (m : Nat) (X : List R), (∀ k, O.le K.zero (co K X k)) → ∀ n,
      O.le (K.mul (co K X n) (f (m + n))) (dot K m X f)
  | m, [], _, n => by
    show O.le (K.mul K.zero (f (m + n))) K.zero
    exact TripleE.le_of_eq' K O _ _ (by lib_ring)
  | m, x :: X, hX, 0 => by
    show O.le (K.mul x (f (m + 0))) (K.add (K.mul x (f m)) (dot K (m + 1) X f))
    have r := ConeBound.dot_nn K O f hf (m + 1) X (RatioStep.mem_nn K O X (fun k => hX (k + 1)))
    have := O.add_le_add _ _ _ _ (O.le_refl (K.mul x (f m))) r
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by rw [Nat.add_zero]; lib_ring)) this
  | m, x :: X, hX, n + 1 => by
    show O.le (K.mul (co K X n) (f (m + (n + 1)))) (K.add (K.mul x (f m)) (dot K (m + 1) X f))
    have ih := dot_term h1 f hf (m + 1) X (fun k => hX (k + 1)) n
    have x0 := O.mul_nonneg _ _ (hX 0) (hf m)
    have := O.add_le_add _ _ _ _ x0 ih
    rw [show m + (n + 1) = m + 1 + n by lib_nat]
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this

/-- **one coefficient is bounded by the weighted mass**: (n+1)X_n ≤ W(X) for X ≥ 0 -/
theorem W_term (h1 : O.le K.zero K.one) (X : List R) (hX : ∀ k, O.le K.zero (co K X k)) (n : Nat) :
    O.le (K.mul (co K X n) (cN K (n + 1))) (W K X) := by
  have := dot_term K O h1 (fun k => cN K (k + 1)) (fun k => SU2CharExp.cN_nn K O h1 _) 0 X hX n
  rw [Nat.zero_add] at this
  exact this

/-- Σ_{t<N} g(m+t)·(m+t+1) two ways: over the listed values with the weight, or over dimL with g -/
theorem dot_listF_dimL (g : Nat → R) : ∀ N m, dot K m (ListRow.listF g m N) (fun k => cN K (k + 1))
    = dot K m (CgDim.dimL K m N) g
  | 0, m => rfl
  | N + 1, m => by
    show K.add (K.mul (g m) (cN K (m + 1))) (dot K (m + 1) (ListRow.listF g (m + 1) N) (fun k => cN K (k + 1)))
      = K.add (K.mul (cN K (m + 1)) (g m)) (dot K (m + 1) (CgDim.dimL K (m + 1) N) g)
    rw [dot_listF_dimL g N (m + 1)]
    lib_ring

/-- dimL can be extended by entries where g vanishes -/
theorem dot_dimL_snoc (g : Nat → R) : ∀ N m, g (m + N) = K.zero →
    dot K m (CgDim.dimL K m (N + 1)) g = dot K m (CgDim.dimL K m N) g
  | 0, m, h => by
    show K.add (K.mul (cN K (m + 1)) (g m)) K.zero = K.zero
    rw [show g m = K.zero from h]; lib_ring
  | N + 1, m, h => by
    show K.add (K.mul (cN K (m + 1)) (g m)) (dot K (m + 1) (CgDim.dimL K (m + 1) (N + 1)) g)
      = K.add (K.mul (cN K (m + 1)) (g m)) (dot K (m + 1) (CgDim.dimL K (m + 1) N) g)
    rw [dot_dimL_snoc g N (m + 1) (by rw [show m + 1 + N = m + (N + 1) by lib_nat]; exact h)]

/-- **W is multiplicative under the product list**: W(prodL X M) = W(X)² (X of length M + 2) -/
theorem W_prodL (X : List R) (M : Nat) (hX : X.length = M + 2) :
    W K (L1Full.prodL K X M) = K.mul (W K X) (W K X) := by
  unfold W L1Full.prodL
  rw [dot_listF_dimL K _ (2 * M + 3) 0,
    ← dot_dimL_snoc K _ (2 * M + 3) 0 (L1Full.cgc_far K X M hX (0 + (2 * M + 3)) (by rw [Nat.zero_add]; exact Nat.le_refl _))]
  exact CgDim.cgc_dim K X X (2 * M + 3 + 1) (by rw [hX]; exact Nat.le_of_eq (by lib_nat))

theorem co_dimL : ∀ (L m j : Nat), j < L → co K (CgDim.dimL K m L) j = cN K (m + j + 1)
  | 0, _, j, h => absurd h (Nat.not_lt_zero j)
  | L + 1, m, 0, _ => rfl
  | L + 1, m, j + 1, h => by
    show co K (CgDim.dimL K (m + 1) L) j = _
    rw [co_dimL L (m + 1) j (Nat.lt_of_succ_lt_succ h), show m + 1 + j + 1 = m + (j + 1) + 1 by lib_nat]

theorem co_dimL_out : ∀ (L m j : Nat), L ≤ j → co K (CgDim.dimL K m L) j = K.zero
  | 0, _, _, _ => rfl
  | L + 1, m, 0, h => absurd h (Nat.not_succ_le_zero L)
  | L + 1, m, j + 1, h => co_dimL_out L (m + 1) j (Nat.le_of_succ_le_succ h)

/-- cgc of nonnegative lists is nonnegative (any lengths) -/
theorem cgc_nn0 (h1 : O.le K.zero K.one) (a b : List R) (ha : ∀ k, O.le K.zero (co K a k))
    (hb : ∀ k, O.le K.zero (co K b k)) (t : Nat) : O.le K.zero (cgc K a b t) := by
  show O.le K.zero (dot K 0 a (fun j => dot K 0 b (fun k => cN K (Fusion.cg j k t))))
  exact ConeBound.dot_nn K O _ (fun j => ConeBound.dot_nn K O _ (fun k => SU2CharExp.cN_nn K O h1 _) 0 b
    (RatioStep.mem_nn K O b hb)) 0 a (RatioStep.mem_nn K O a ha)

/-- **one product coefficient is bounded by the masses**: (n+1)·cgc(a, b)_n ≤ W(a)·W(b) for a, b ≥ 0 -/
theorem cgc_term (h1 : O.le K.zero K.one) (a b : List R) (ha : ∀ k, O.le K.zero (co K a k))
    (hb : ∀ k, O.le K.zero (co K b k)) (n : Nat) :
    O.le (K.mul (cgc K a b n) (cN K (n + 1))) (K.mul (W K a) (W K b)) := by
  have hN : a.length + b.length ≤ a.length + b.length + n + 1 := Nat.le_trans (Nat.le_add_right _ n) (Nat.le_succ _)
  have dm := CgDim.cgc_dim K a b (a.length + b.length + n + 1) hN
  have dnn : ∀ k, O.le K.zero (co K (CgDim.dimL K 0 (a.length + b.length + n + 1)) k) := fun k => by
    cases Nat.lt_or_ge k (a.length + b.length + n + 1) with
    | inl h => rw [co_dimL K _ 0 k h]; exact SU2CharExp.cN_nn K O h1 _
    | inr h => rw [co_dimL_out K _ 0 k h]; exact O.le_refl _
  have t := dot_term K O h1 (cgc K a b) (cgc_nn0 K O h1 a b ha hb) 0 _ dnn n
  have hn : n < a.length + b.length + n + 1 := Nat.lt_succ_of_le (Nat.le_add_left n _)
  rw [co_dimL K _ 0 n hn, Nat.zero_add] at t
  unfold W
  rw [← dm]
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) t

/-- **ratio-decaying sums**: a_{m+1} ≤ ρ·a_m gives (1 − ρ)·Σ_{m<T} a_m + a_T ≤ a_0 (for every T) -/
theorem sum_ratio_le (a : Nat → R) (ρ : R) (hr : ∀ m, O.le (a (m + 1)) (K.mul ρ (a m))) :
    ∀ T, O.le (K.add (K.mul (K.add K.one (K.neg ρ)) (QBinom.sumN K a T)) (a T)) (a 0)
  | 0 => TripleE.le_of_eq' K O _ _ (by show K.add (K.mul (K.add K.one (K.neg ρ)) K.zero) (a 0) = a 0; lib_ring)
  | T + 1 => by
    have ih := sum_ratio_le a ρ hr T
    have s := O.add_le_add _ _ _ _ (O.le_refl (K.add (K.mul (K.add K.one (K.neg ρ)) (QBinom.sumN K a T))
      (K.add (a T) (K.neg (K.mul ρ (a T)))))) (hr T)
    show O.le (K.add (K.mul (K.add K.one (K.neg ρ)) (K.add (QBinom.sumN K a T) (a T))) (a (T + 1))) (a 0)
    exact O.le_trans _ _ _ (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) s)
      (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) ih)


/-- **mass of the tail list**: W(tlL G N (N + T)) = Σ_{i<T} G_{N+i}·(N + i + 1) -/
theorem W_tail (G : List R) (N T : Nat) :
    W K (HeatTrunc.tlL K G N (N + T)) = QBinom.sumN K (fun i => K.mul (co K G (N + i)) (cN K (N + i + 1))) T := by
  unfold W
  rw [TripleA.dot_sumN K _ _ 0, HeatTrunc.len_tlL K G N (N + T), TripleA.sumN_split K _ N T]
  have z : QBinom.sumN K (fun j => K.mul (co K (HeatTrunc.tlL K G N (N + T)) j) (cN K (0 + j + 1))) N = K.zero := by
    rw [SecondRing.sumN_ext_lt K _ (fun _ => K.zero) N (fun j hj => by
      unfold HeatTrunc.tlL
      rw [RatioStep.co_lmap K _ (N + T) 0 j (Nat.lt_of_lt_of_le hj (Nat.le_add_right N T)), Nat.zero_add, if_pos hj]
      lib_ring)]
    exact SecondRingShift.sumN_zero K N
  rw [z]
  have e : QBinom.sumN K (fun k => K.mul (co K (HeatTrunc.tlL K G N (N + T)) (N + k)) (cN K (0 + (N + k) + 1))) T
      = QBinom.sumN K (fun i => K.mul (co K G (N + i)) (cN K (N + i + 1))) T := by
    apply SecondRing.sumN_ext_lt K _ _ T
    intro i hi
    have hn : ¬ N + i < N := fun e => Nat.lt_irrefl _ (Nat.lt_of_le_of_lt (Nat.le_add_right N i) e)
    unfold HeatTrunc.tlL
    rw [RatioStep.co_lmap K _ (N + T) 0 (N + i) (Nat.add_lt_add_left hi N), Nat.zero_add, if_neg hn]
    lib_ring
  rw [e]
  lib_ring

/-- **length-independent tail mass bound**: if the weighted tail terms a_i = G_{N+i}(N+i+1) decay with ratio ρ,
  then (1 − ρ)·W(tlL G N (N+T)) ≤ a_0 (all a_i ≥ 0) -/
theorem W_tail_le (G : List R) (N T : Nat) (ρ : R)
    (hn : ∀ i, O.le K.zero (K.mul (co K G (N + i)) (cN K (N + i + 1))))
    (hr : ∀ i, O.le (K.mul (co K G (N + (i + 1))) (cN K (N + (i + 1) + 1))) (K.mul ρ (K.mul (co K G (N + i)) (cN K (N + i + 1))))) :
    O.le (K.mul (K.add K.one (K.neg ρ)) (W K (HeatTrunc.tlL K G N (N + T)))) (K.mul (co K G (N + 0)) (cN K (N + 0 + 1))) := by
  rw [W_tail K G N T]
  have s := sum_ratio_le K O (fun i => K.mul (co K G (N + i)) (cN K (N + i + 1))) ρ hr T
  have u := O.add_le_add _ _ _ _
    (O.le_refl (K.mul (K.add K.one (K.neg ρ)) (QBinom.sumN K (fun i => K.mul (co K G (N + i)) (cN K (N + i + 1))) T))) (hn T)
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ u s)

end WMass

#print axioms WMass.dot_term
#print axioms WMass.W_term
#print axioms WMass.dot_listF_dimL
#print axioms WMass.dot_dimL_snoc
#print axioms WMass.W_prodL
#print axioms WMass.co_dimL
#print axioms WMass.co_dimL_out
#print axioms WMass.cgc_nn0
#print axioms WMass.cgc_term
#print axioms WMass.sum_ratio_le
#print axioms WMass.W_tail
#print axioms WMass.W_tail_le

#print axioms: 12 theorems, no axioms.

90.143 The Wilson ladder theorem

Wilson start, all rungs and an end reference in the ratio class give the flow in the ratio class.

\[ w_t\preceq \mathrm {Ref}_0,\ \text {rungs},\ \mathrm {RC}(\sigma _0,\mathrm {Ref}_K)\ \Rightarrow \ \mathrm {RC}(\sigma _j,\mathrm {mk}^{K+j}(w_t)) \]

ym/LadderEnd.lean

/-
  ym/LadderEnd — T45, G78g route (B): the whole ladder for the Wilson start. For β = 2t ≤ 8 (t ≤ 4) the truncated
  Wilson weight (truncation 29, length M + 2 ≥ 65) is v-good with nonincreasing a (WilsonFlowBig.wilson_flow8); if it
  lies below the first reference and every rung carries "below Ref k" to "below Ref (k+1)", then after the last rung
  Kend the MK flow stays in the ratio class RC(σ_j) for every later step (RungB.ladder_end). Lean 4 core only, no axioms.
-/
import ym.RungB
import ym.WilsonFlowBig

namespace LadderEnd

open YMChain LinkIntegral SU2Char HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **the Wilson ladder**: Wilson start (β ≤ 8) below Ref 0, all rungs, and the end reference in RC(σ₀) with
  (4σ_j)⁴ ≤ σ_{j+1} give the flow in the ratio class after Kend steps -/
theorem wilson_ladder (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (t : R) (ht0 : O.le K.zero t)
    (ht4 : O.le t (cN K 4)) (htne : t ≠ K.zero) (M : Nat) (hM : 63 ≤ M) (Ref : Nat → List R)
    (hrung : ∀ k (Y : List R), L1Full.Good K O Y → Y.length = FlowIter.itM M k + 2 → FlowCmp.Below K O Y (Ref k) →
      FlowCmp.Below K O (FlowStep.mk K O F Y (FlowIter.itM M k)) (Ref (k + 1)))
    (hb0 : FlowCmp.Below K O (WilsonCone.wl K O F t 28 (M + 2)) (Ref 0)) (Kend : Nat) (σ : Nat → R)
    (hσ : ∀ i, O.le K.zero (σ i))
    (hstep : ∀ i, O.le (pwR K (K.add (K.add (σ i) (σ i)) (K.add (σ i) (σ i))) 4) (σ (i + 1)))
    (hR : RatioStep.RC K O (σ 0) (Ref Kend)) :
    ∀ j, RatioStep.RC K O (σ j)
      (FlowIter.itX K O F (FlowIter.itX K O F (WilsonCone.wl K O F t 28 (M + 2)) M Kend) (FlowIter.itM M Kend) j) := by
  have w0 := WilsonFlowBig.wilson_flow8 K O F h1 h10 t ht0 ht4 htne M hM 0
  exact RungB.ladder_end K O F h1 h10 _ M w0.1 w0.2.1 w0.2.2 Ref hrung hb0 Kend σ hσ hstep hR

end LadderEnd

#print axioms LadderEnd.wilson_ladder

#print axioms: 1 theorem, no axioms.

90.144 One rung from one kernel check

Natural-number head bounds, a mass error and one kernel check give the cross inequalities of a rung.

\[ (Q^{\mathrm {up}}_{n+1}+e)^4(n+1)^3k_n\le (Q^{\mathrm {lo}}_n)^4(n+2)^3k_{n+1}\ (n<N)\ \Rightarrow \ \mathrm {mk}(X)\preceq M' \]

ym/HeatTrunc.lean

/-
  ym/HeatTrunc — T45, G78e pieces: comparing one MK step of a list with one MK step of its truncation to the
  first N characters. Monotonicity (mk_mono, mk_trL_le), the split X = truncation + tail (split_trL), the
  one-product truncation error (prod_trunc_err), and the passage of multiplicative slack through powers and the
  decimation (pw_slack, decM_slack). Lean 4 core only, no axioms.
-/
import ym.FlowBridge
import ym.HeatSecondStep
import ym.HeatStepMK
import ym.WilsonGood

namespace HeatTrunc

open YMChain LinkIntegral SU2Char MigdalCone HeatPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- the product list is nonnegative and monotone in X (same length) -/
theorem prodL_mono (h1 : O.le K.zero K.one) (X Y : List R) (M : Nat) (hX : X.length = M + 2) (hY : Y.length = M + 2)
    (hXn : ∀ k, O.le K.zero (co K X k)) (hXY : ∀ k, O.le (co K X k) (co K Y k)) (k : Nat) :
    O.le K.zero (co K (L1Full.prodL K X M) k) ∧ O.le (co K (L1Full.prodL K X M) k) (co K (L1Full.prodL K Y M) k) := by
  rw [L1Full.co_prodL K X M hX, L1Full.co_prodL K Y M hY]
  exact ⟨HeatL1All.cgc_nn K O X X (M + 1) hX hXn hXn k, FlowBridge.cgc_mono K O h1 X Y X Y k hXn hXY hXn hXY⟩

/-- **one MK step is coefficientwise monotone** on nonnegative lists of the same length -/
theorem mk_mono (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X Y : List R) (M : Nat) (hX : X.length = M + 2)
    (hY : Y.length = M + 2) (hXn : ∀ k, O.le K.zero (co K X k)) (hXY : ∀ k, O.le (co K X k) (co K Y k)) (n : Nat) :
    O.le K.zero (co K (FlowStep.mk K O F X M) n) ∧ O.le (co K (FlowStep.mk K O F X M) n) (co K (FlowStep.mk K O F Y M) n) := by
  have lX : (L1Full.prodL K X M).length = 2 * M + 1 + 2 := HeatSecondStep.len_prodL K X M
  have lY : (L1Full.prodL K Y M).length = 2 * M + 1 + 2 := HeatSecondStep.len_prodL K Y M
  have p := prodL_mono K O h1 X Y M hX hY hXn hXY
  have q := prodL_mono K O h1 _ _ (2 * M + 1) lX lY (fun k => (p k).1) (fun k => (p k).2) n
  unfold FlowStep.mk FlowStep.Ysq
  refine ⟨?_, FlowBridge.decM_mono K O F h1 h10 _ _ 0 n q.1 q.2⟩
  rw [RatioStep.co_decM K O F _ 0 n]
  have c := SU2CharExp.pos_of_ge_one K O h1 h10 (0 + n + 1) (Nat.le_add_left 1 (0 + n))
  exact O.mul_nonneg _ _ (AreaLower.pw_nn K O _ q.1 h1 4) (AreaLower.pw_nn K O _ (F.inv_pos _ c.1 c.2) h1 3)

/-- **additive error to multiplicative slack**: W ≤ P + E and E ≤ v·P give W ≤ (1 + v)·P -/
theorem add_err_mul (W P E v : R) (hW : O.le W (K.add P E)) (hE : O.le E (K.mul v P)) :
    O.le W (K.mul (K.add K.one v) P) := by
  have s := O.add_le_add _ _ _ _ (O.le_refl P) hE
  exact O.le_trans _ _ _ hW (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- the truncation of X to its first N characters, kept at length L (zeros after N) -/
def trL (X : List R) (N L : Nat) : List R := lmap (fun j => K.mul (co K X j) (if j < N then K.one else K.zero)) 0 L

theorem len_trL (X : List R) (N L : Nat) : (trL K X N L).length = L := WilsonGood.len_lmap _ 0 L

/-- the truncation is nonnegative and below X -/
theorem trL_le (X : List R) (hX : ∀ k, O.le K.zero (co K X k)) (N L n : Nat) :
    O.le K.zero (co K (trL K X N L) n) ∧ O.le (co K (trL K X N L) n) (co K X n) := by
  unfold trL
  cases Nat.lt_or_ge n L with
  | inr h =>
    rw [RatioStep.co_lmap_ge K _ L 0 n h]
    exact ⟨O.le_refl _, hX n⟩
  | inl h =>
    rw [RatioStep.co_lmap K _ L 0 n h, Nat.zero_add]
    cases Nat.lt_or_ge n N with
    | inl h' =>
      rw [if_pos h', show K.mul (co K X n) K.one = co K X n by lib_ring]
      exact ⟨hX n, O.le_refl _⟩
    | inr h' =>
      have hn : ¬ n < N := fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le e h')
      rw [if_neg hn, show K.mul (co K X n) K.zero = K.zero by lib_ring]
      exact ⟨O.le_refl _, hX n⟩

/-- below N the truncation agrees with X -/
theorem trL_eq (X : List R) (N L n : Nat) (hN : n < N) (hL : n < L) : co K (trL K X N L) n = co K X n := by
  unfold trL
  rw [RatioStep.co_lmap K _ L 0 n hL, Nat.zero_add, if_pos hN]
  lib_ring

/-- **the MK step of the truncation is below the MK step of X** (same length M + 2) -/
theorem mk_trL_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X : List R) (M N : Nat) (hX : X.length = M + 2)
    (hXn : ∀ k, O.le K.zero (co K X k)) (n : Nat) :
    O.le K.zero (co K (FlowStep.mk K O F (trL K X N (M + 2)) M) n) ∧
      O.le (co K (FlowStep.mk K O F (trL K X N (M + 2)) M) n) (co K (FlowStep.mk K O F X M) n) :=
  mk_mono K O F h1 h10 _ X M (len_trL K X N (M + 2)) hX (fun k => (trL_le K O X hXn N (M + 2) k).1)
    (fun k => (trL_le K O X hXn N (M + 2) k).2) n

/-- the tail of X after its first N characters, at length L -/
def tlL (X : List R) (N L : Nat) : List R := lmap (fun j => K.mul (co K X j) (if j < N then K.zero else K.one)) 0 L

theorem len_tlL (X : List R) (N L : Nat) : (tlL K X N L).length = L := WilsonGood.len_lmap _ 0 L

theorem tlL_nn (X : List R) (hX : ∀ k, O.le K.zero (co K X k)) (N L n : Nat) : O.le K.zero (co K (tlL K X N L) n) := by
  unfold tlL
  cases Nat.lt_or_ge n L with
  | inr h => rw [RatioStep.co_lmap_ge K _ L 0 n h]; exact O.le_refl _
  | inl h =>
    rw [RatioStep.co_lmap K _ L 0 n h, Nat.zero_add]
    cases Nat.lt_or_ge n N with
    | inl h' => rw [if_pos h', show K.mul (co K X n) K.zero = K.zero by lib_ring]; exact O.le_refl _
    | inr h' =>
      have hn : ¬ n < N := fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le e h')
      rw [if_neg hn, show K.mul (co K X n) K.one = co K X n by lib_ring]; exact hX n

/-- X = truncation + tail, coefficientwise (X of length L) -/
theorem split_trL (X : List R) (N L : Nat) (hX : X.length = L) (k : Nat) :
    co K X k = K.add (co K (trL K X N L) k) (co K (tlL K X N L) k) := by
  unfold trL tlL
  cases Nat.lt_or_ge k L with
  | inr h =>
    rw [RatioStep.co_lmap_ge K _ L 0 k h, RatioStep.co_lmap_ge K _ L 0 k h,
      HeatLR.co_out K X k (Nat.le_trans (Nat.le_of_eq hX) h)]
    lib_ring
  | inl h =>
    rw [RatioStep.co_lmap K _ L 0 k h, RatioStep.co_lmap K _ L 0 k h, Nat.zero_add]
    cases Nat.lt_or_ge k N with
    | inl h' => rw [if_pos h', if_pos h']; lib_ring
    | inr h' =>
      have hn : ¬ k < N := fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le e h')
      rw [if_neg hn, if_neg hn]; lib_ring

/-- **one product, truncation error**: (X·X)_t ≤ (PX·PX)_t + (ΣTX)(ΣX) + (ΣPX)(ΣTX) -/
theorem prod_trunc_err (h1 : O.le K.zero K.one) (X : List R) (M N : Nat) (hX : X.length = M + 2)
    (hXn : ∀ k, O.le K.zero (co K X k)) (t : Nat) :
    O.le (cgc K X X t) (K.add (cgc K (trL K X N (M + 2)) (trL K X N (M + 2)) t)
      (K.add (dot K 0 (tlL K X N (M + 2)) (fun _ => dot K 0 X (fun _ => K.one)))
        (dot K 0 (trL K X N (M + 2)) (fun _ => dot K 0 (tlL K X N (M + 2)) (fun _ => K.one))))) :=
  FlowBridge.proj_err_bound K O h1 (M + 2) X _ _ X _ _ t (Nat.le_of_eq hX) (Nat.le_of_eq (len_trL K X N (M + 2)))
    (Nat.le_of_eq (len_tlL K X N (M + 2))) (Nat.le_of_eq hX) (Nat.le_of_eq (len_trL K X N (M + 2)))
    (Nat.le_of_eq (len_tlL K X N (M + 2))) (split_trL K X N (M + 2) hX) (split_trL K X N (M + 2) hX)
    (fun k => (trL_le K O X hXn N (M + 2) k).1) (tlL_nn K O X hXn N (M + 2)) hXn (tlL_nn K O X hXn N (M + 2))

/-- multiplicative slack passes through powers: 0 ≤ a ≤ u·b ⇒ aᵏ ≤ uᵏ·bᵏ -/
theorem pw_slack (h1 : O.le K.zero K.one) (a b u : R) (ha : O.le K.zero a) (hab : O.le a (K.mul u b)) (k : Nat) :
    O.le (pwR K a k) (K.mul (pwR K u k) (pwR K b k)) := by
  rw [← HeatDec.pw_mul_pw K u b k]
  exact SU2Strong4D.pw_mono K O h1 _ _ ha hab k

/-- **slack through one decimation**: if 0 ≤ Y_n ≤ u·Z_n then (decM Y)_n ≤ u⁴·(decM Z)_n -/
theorem decM_slack (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Y Z : List R) (m n : Nat) (u : R)
    (hY : O.le K.zero (co K Y n)) (hYZ : O.le (co K Y n) (K.mul u (co K Z n))) :
    O.le (co K (decM K O F m Y) n) (K.mul (pwR K u 4) (co K (decM K O F m Z) n)) := by
  rw [RatioStep.co_decM K O F Y m n, RatioStep.co_decM K O F Z m n]
  have c := SU2CharExp.pos_of_ge_one K O h1 h10 (m + n + 1) (Nat.le_add_left 1 (m + n))
  have i := AreaLower.pw_nn K O _ (F.inv_pos _ c.1 c.2) h1 3
  have p := O.mul_le_mul_right _ _ _ (pw_slack K O h1 _ _ u hY hYZ 4) i
  exact O.le_trans _ _ _ p (TripleE.le_of_eq' K O _ _ (by lib_ring))

/-- a list whose coefficients are at most e has coefficient sum at most (length)·e -/
theorem dot_one_le (e : R) : ∀ (l : List R) (m : Nat), (∀ k, O.le (co K l k) e) →
    O.le (dot K m l (fun _ => K.one)) (K.mul (cN K l.length) e)
  | [], m, _ => by
    show O.le K.zero (K.mul K.zero e)
    exact TripleE.le_of_eq' K O _ _ (by lib_ring)
  | x :: a, m, h => by
    show O.le (K.add (K.mul x K.one) (dot K (m + 1) a (fun _ => K.one))) (K.mul (K.add K.one (cN K a.length)) e)
    have ih := dot_one_le e a (m + 1) (fun k => h (k + 1))
    have h0 : O.le x e := h 0
    have s := O.add_le_add _ _ _ _ h0 ih
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- a dot product against a constant weight -/
theorem dot_const (c : R) : ∀ (m : Nat) (l : List R), dot K m l (fun _ => c) = K.mul (dot K m l (fun _ => K.one)) c
  | m, [] => by show K.zero = K.mul K.zero c; lib_ring
  | m, x :: a => by
    show K.add (K.mul x c) (dot K (m + 1) a (fun _ => c)) = K.mul (K.add (K.mul x K.one) (dot K (m + 1) a (fun _ => K.one))) c
    rw [dot_const c (m + 1) a]
    lib_ring

/-- a ≤ b ⇒ 0 ≤ b − a, and b ≤ a + e ⇒ b − a ≤ e -/
theorem sub_nn (a b : R) (h : O.le a b) : O.le K.zero (K.add b (K.neg a)) := by
  have s := O.add_le_add _ _ _ _ h (O.le_refl (K.neg a))
  rw [K.add_neg] at s
  exact s

theorem sub_le (a b e : R) (h : O.le b (K.add a e)) : O.le (K.add b (K.neg a)) e := by
  have s := O.add_le_add _ _ _ _ h (O.le_refl (K.neg a))
  exact O.le_trans _ _ _ s (TripleE.le_of_eq' K O _ _ (by lib_ring))

/-- the difference list Y − P at length L -/
def dfL (Y P : List R) (L : Nat) : List R := lmap (fun j => K.add (co K Y j) (K.neg (co K P j))) 0 L

theorem len_dfL (Y P : List R) (L : Nat) : (dfL K Y P L).length = L := WilsonGood.len_lmap _ 0 L

/-- **one product with an additive coefficient error**: if 0 ≤ P ≤ Y ≤ P + e coefficientwise (lengths L), then
  (Y·Y)_t ≤ (P·P)_t + ΣD·ΣY + ΣP·ΣD with ΣD ≤ L·e -/
theorem prod_add_err (h1 : O.le K.zero K.one) (Y P : List R) (L : Nat) (hY : Y.length = L) (hP : P.length = L) (e : R)
    (hPn : ∀ k, O.le K.zero (co K P k)) (hPY : ∀ k, O.le (co K P k) (co K Y k))
    (hYe : ∀ k, O.le (co K Y k) (K.add (co K P k) e)) (t : Nat) :
    O.le (cgc K Y Y t) (K.add (cgc K P P t) (K.mul (K.mul (cN K L) e)
      (K.add (dot K 0 Y (fun _ => K.one)) (dot K 0 P (fun _ => K.one))))) := by
  have hYn : ∀ k, O.le K.zero (co K Y k) := fun k => O.le_trans _ _ _ (hPn k) (hPY k)
  have e0 : O.le K.zero e := by
    have w := hYe L
    rw [HeatLR.co_out K Y L (Nat.le_of_eq hY), HeatLR.co_out K P L (Nat.le_of_eq hP)] at w
    exact O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring))
  have dD : ∀ k, O.le K.zero (co K (dfL K Y P L) k) ∧ O.le (co K (dfL K Y P L) k) e := fun k => by
    unfold dfL
    cases Nat.lt_or_ge k L with
    | inr h => rw [RatioStep.co_lmap_ge K _ L 0 k h]; exact ⟨O.le_refl _, e0⟩
    | inl h =>
      rw [RatioStep.co_lmap K _ L 0 k h, Nat.zero_add]
      exact ⟨sub_nn K O _ _ (hPY k), sub_le K O _ _ e (hYe k)⟩
  have sp : ∀ k, co K Y k = K.add (co K P k) (co K (dfL K Y P L) k) := fun k => by
    unfold dfL
    cases Nat.lt_or_ge k L with
    | inr h =>
      rw [RatioStep.co_lmap_ge K _ L 0 k h, HeatLR.co_out K Y k (Nat.le_trans (Nat.le_of_eq hY) h),
        HeatLR.co_out K P k (Nat.le_trans (Nat.le_of_eq hP) h)]
      lib_ring
    | inl h => rw [RatioStep.co_lmap K _ L 0 k h, Nat.zero_add]; lib_ring
  have lD := len_dfL K Y P L
  have b := FlowBridge.proj_err_bound K O h1 L Y P (dfL K Y P L) Y P (dfL K Y P L) t (Nat.le_of_eq hY) (Nat.le_of_eq hP)
    (Nat.le_of_eq lD) (Nat.le_of_eq hY) (Nat.le_of_eq hP) (Nat.le_of_eq lD) sp sp hPn (fun k => (dD k).1) hYn
    (fun k => (dD k).1)
  rw [dot_const K (dot K 0 Y (fun _ => K.one)) 0 (dfL K Y P L), dot_const K (dot K 0 (dfL K Y P L) (fun _ => K.one)) 0 P] at b
  have sD := dot_one_le K O e (dfL K Y P L) 0 (fun k => (dD k).2)
  rw [lD] at sD
  have nY := ConeBound.dot_nn K O _ (fun _ => h1) 0 Y (RatioStep.mem_nn K O Y hYn)
  have nP := ConeBound.dot_nn K O _ (fun _ => h1) 0 P (RatioStep.mem_nn K O P hPn)
  have t1 := O.mul_le_mul_right _ _ _ sD nY
  have t2 := O.mul_le_mul_right _ _ _ sD nP
  have s := O.add_le_add _ _ _ _ (O.le_refl (cgc K P P t)) (O.add_le_add _ _ _ _ t1 (TripleE.le_of_eq' K O _ _
    (K.mul_comm (dot K 0 P (fun _ => K.one)) (dot K 0 (dfL K Y P L) (fun _ => K.one)))))
  have s2 := O.add_le_add _ _ _ _ (O.le_refl (cgc K P P t)) (O.add_le_add _ _ _ _ (O.le_refl (K.mul (K.mul (cN K L) e)
    (dot K 0 Y (fun _ => K.one)))) t2)
  exact O.le_trans _ _ _ b (O.le_trans _ _ _ s (O.le_trans _ _ _ s2 (TripleE.le_of_eq' K O _ _ (by lib_ring))))

include F in
/-- **BelowL on the first N + 1 characters gives full Below for the truncation**: if X (length L, nonnegative)
  satisfies the cross inequality against B for n < N and B > 0 on [0, N], then trL X (N + 1) L ≼ B (weight
  g_k = X_k/B_k for k ≤ N, 0 after) -/
theorem trL_below (X B : List R) (N L : Nat) (hX : X.length = L) (hXn : ∀ k, O.le K.zero (co K X k))
    (hBp : ∀ k, k ≤ N → O.le K.zero (co K B k)) (hBne : ∀ k, k ≤ N → co K B k ≠ K.zero)
    (h : HeatStepMK.BelowL K O X B N) : FlowCmp.Below K O (trL K X (N + 1) L) B := by
  have gnn : ∀ k, k ≤ N → O.le K.zero (K.mul (co K X k) (F.inv (co K B k))) := fun k hk =>
    O.mul_nonneg _ _ (hXn k) (F.inv_pos _ (hBp k hk) (hBne k hk))
  refine ⟨fun k => if k < N + 1 then K.mul (co K X k) (F.inv (co K B k)) else K.zero, fun m => ?_, fun m => ?_, fun k => ?_⟩
  · cases Nat.lt_or_ge (m + 1) (N + 1) with
    | inl hm =>
      have hm' : m < N + 1 := Nat.lt_trans (Nat.lt_succ_self m) hm
      show O.le (if m + 1 < N + 1 then _ else _) (if m < N + 1 then _ else _)
      rw [if_pos hm, if_pos hm']
      have hmN : m < N := Nat.lt_of_succ_lt_succ hm
      have b0 := hBp m (Nat.le_of_lt hmN)
      have b1 := hBp (m + 1) (Nat.le_of_lt_succ hm)
      have n0 := hBne m (Nat.le_of_lt hmN)
      have n1 := hBne (m + 1) (Nat.le_of_lt_succ hm)
      have i0 := F.mul_inv _ n0
      have i1 := F.mul_inv _ n1
      have bb : O.le K.zero (K.mul (co K B m) (co K B (m + 1))) := O.mul_nonneg _ _ b0 b1
      have bbn : K.mul (co K B m) (co K B (m + 1)) ≠ K.zero := fun e => n1 (O.pos_mul_eq_zero _ _ b0 n0 e)
      refine O.pos_cancel _ _ _ bb bbn ?_
      have c := h m hmN
      refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ c (TripleE.le_of_eq' K O _ _ ?_))
      · rw [show K.mul (K.mul (co K X (m + 1)) (F.inv (co K B (m + 1)))) (K.mul (co K B m) (co K B (m + 1)))
            = K.mul (K.mul (co K X (m + 1)) (co K B m)) (K.mul (co K B (m + 1)) (F.inv (co K B (m + 1)))) by lib_ring, i1]
        lib_ring
      · rw [show K.mul (K.mul (co K X m) (F.inv (co K B m))) (K.mul (co K B m) (co K B (m + 1)))
            = K.mul (K.mul (co K X m) (co K B (m + 1))) (K.mul (co K B m) (F.inv (co K B m))) by lib_ring, i0]
        lib_ring
    | inr hm =>
      have hn : ¬ m + 1 < N + 1 := fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le e hm)
      show O.le (if m + 1 < N + 1 then _ else _) (if m < N + 1 then _ else _)
      rw [if_neg hn]
      cases Nat.lt_or_ge m (N + 1) with
      | inl hl => rw [if_pos hl]; exact gnn m (Nat.le_of_lt_succ hl)
      | inr hg =>
        have hn' : ¬ m < N + 1 := fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le e hg)
        rw [if_neg hn']; exact O.le_refl _
  · show O.le K.zero (if m < N + 1 then _ else _)
    cases Nat.lt_or_ge m (N + 1) with
    | inl hl => rw [if_pos hl]; exact gnn m (Nat.le_of_lt_succ hl)
    | inr hg =>
      have hn' : ¬ m < N + 1 := fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le e hg)
      rw [if_neg hn']; exact O.le_refl _
  · show co K (trL K X (N + 1) L) k = K.mul (co K B k) (if k < N + 1 then _ else _)
    unfold trL
    cases Nat.lt_or_ge k (N + 1) with
    | inl hk =>
      rw [if_pos hk]
      have i := F.mul_inv _ (hBne k (Nat.le_of_lt_succ hk))
      cases Nat.lt_or_ge k L with
      | inl hL =>
        rw [RatioStep.co_lmap K _ L 0 k hL, Nat.zero_add, if_pos hk,
          show K.mul (co K B k) (K.mul (co K X k) (F.inv (co K B k))) = K.mul (co K X k) (K.mul (co K B k) (F.inv (co K B k))) by lib_ring, i]
      | inr hL =>
        rw [RatioStep.co_lmap_ge K _ L 0 k hL, HeatLR.co_out K X k (Nat.le_trans (Nat.le_of_eq hX) hL)]
        lib_ring
    | inr hk =>
      have hn' : ¬ k < N + 1 := fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le e hk)
      rw [if_neg hn']
      cases Nat.lt_or_ge k L with
      | inl hL => rw [RatioStep.co_lmap K _ L 0 k hL, Nat.zero_add, if_neg hn']; lib_ring
      | inr hL => rw [RatioStep.co_lmap_ge K _ L 0 k hL]; lib_ring

end HeatTrunc

#print axioms HeatTrunc.prodL_mono
#print axioms HeatTrunc.mk_mono
#print axioms HeatTrunc.add_err_mul
#print axioms HeatTrunc.len_trL
#print axioms HeatTrunc.trL_le
#print axioms HeatTrunc.trL_eq
#print axioms HeatTrunc.mk_trL_le
#print axioms HeatTrunc.len_tlL
#print axioms HeatTrunc.tlL_nn
#print axioms HeatTrunc.split_trL
#print axioms HeatTrunc.prod_trunc_err
#print axioms HeatTrunc.pw_slack
#print axioms HeatTrunc.decM_slack
#print axioms HeatTrunc.dot_one_le
#print axioms HeatTrunc.dot_const
#print axioms HeatTrunc.sub_nn
#print axioms HeatTrunc.sub_le
#print axioms HeatTrunc.len_dfL
#print axioms HeatTrunc.prod_add_err
#print axioms HeatTrunc.trL_below

ym/HeadBound.lean

/-
  ym/HeadBound — T45, G78g route (B): length-independent head bounds for one MK step of a reference. The truncation
  trL X N L (length L, zeros after N) has the same coefficients as the finite list of the first N coefficients, so
  by MkExt.mk_ext its MK step is that of a fixed finite list. Lean 4 core only, no axioms.
-/
import ym.HeatTrunc
import ym.MkExt

namespace HeadBound

open YMChain LinkIntegral SU2Char HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- the truncation and the finite head list have the same coefficients (N ≤ L) -/
theorem co_trL_lmap (X : List R) (N L : Nat) (hNL : N ≤ L) (k : Nat) :
    co K (HeatTrunc.trL K X N L) k = co K (lmap (fun j => co K X j) 0 N) k := by
  unfold HeatTrunc.trL
  cases Nat.lt_or_ge k N with
  | inl h =>
    rw [RatioStep.co_lmap K _ L 0 k (Nat.lt_of_lt_of_le h hNL), RatioStep.co_lmap K _ N 0 k h, Nat.zero_add, if_pos h]
    lib_ring
  | inr h =>
    rw [RatioStep.co_lmap_ge K _ N 0 k h]
    cases Nat.lt_or_ge k L with
    | inl hL =>
      have hn : ¬ k < N := fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le e h)
      rw [RatioStep.co_lmap K _ L 0 k hL, Nat.zero_add, if_neg hn]; lib_ring
    | inr hL => rw [RatioStep.co_lmap_ge K _ L 0 k hL]

/-- **lower bound of one MK step by a finite list**: for X ≥ 0 of length M + 2 and N ≤ M + 2,
  mk(first N coefficients, as a list of length N = M0 + 2) ≤ mk X coefficientwise -/
theorem mk_head_le (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X : List R) (M M0 : Nat) (hX : X.length = M + 2)
    (hXn : ∀ k, O.le K.zero (co K X k)) (hN : M0 + 2 ≤ M + 2) (n : Nat) :
    O.le (co K (FlowStep.mk K O F (lmap (fun j => co K X j) 0 (M0 + 2)) M0) n) (co K (FlowStep.mk K O F X M) n) := by
  have e := MkExt.mk_ext K O F (HeatTrunc.trL K X (M0 + 2) (M + 2)) (lmap (fun j => co K X j) 0 (M0 + 2)) M M0
    (HeatTrunc.len_trL K X (M0 + 2) (M + 2)) (WilsonGood.len_lmap _ 0 (M0 + 2)) (co_trL_lmap K X (M0 + 2) (M + 2) hN) n
  rw [← e]
  exact (HeatTrunc.mk_trL_le K O F h1 h10 X M (M0 + 2) hX hXn n).2
/-- **head cross inequality from bounds**: lo ≤ Y_n, Y_{n+1} ≤ up (M′ ≥ 0) and up·M′_n ≤ lo·M′_{n+1} give
  Y_{n+1}M′_n ≤ Y_n M′_{n+1} -/
theorem cross_of_bounds (y0 y1 m0 m1 lo up : R) (hm0 : O.le K.zero m0) (hm1 : O.le K.zero m1)
    (hlo : O.le lo y0) (hup : O.le y1 up) (hchk : O.le (K.mul up m0) (K.mul lo m1)) :
    O.le (K.mul y1 m0) (K.mul y0 m1) :=
  O.le_trans _ _ _ (O.mul_le_mul_right _ _ m0 hup hm0) (O.le_trans _ _ _ hchk (O.mul_le_mul_right _ _ m1 hlo hm1))

/-- **lower chain**: G ≤ Rf coefficientwise (G ≥ 0, both of length M + 2) and N0 + 2 ≤ M + 2 give
  mk(first N0 + 2 coefficients of G) ≤ mk(Rf) coefficientwise -/
theorem lower_chain (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (G Rf : List R) (M M0 : Nat)
    (hG : G.length = M + 2) (hR : Rf.length = M + 2) (hGn : ∀ k, O.le K.zero (co K G k))
    (hGR : ∀ k, O.le (co K G k) (co K Rf k)) (hN : M0 + 2 ≤ M + 2) (n : Nat) :
    O.le (co K (FlowStep.mk K O F (lmap (fun j => co K G j) 0 (M0 + 2)) M0) n) (co K (FlowStep.mk K O F Rf M) n) :=
  O.le_trans _ _ _ (mk_head_le K O F h1 h10 G M M0 hG hGn hN n) (HeatTrunc.mk_mono K O F h1 h10 G Rf M hG hR hGn hGR n).2

end HeadBound

#print axioms HeadBound.co_trL_lmap
#print axioms HeadBound.mk_head_le
#print axioms HeadBound.cross_of_bounds
#print axioms HeadBound.lower_chain

ym/HeadUpper.lean

/-
  ym/HeadUpper — T45, G78g route (B): length-independent upper bound for the coefficients of the two products of
  one MK step. If X = P + T coefficientwise (all ≥ 0, length M + 2) then
    (n+1)·Ysq(X)_n ≤ (n+1)·Ysq(P)_n + W(T)(W(X) + W(P))·(W(X)² + W(P)²),
  with the weighted mass W (WMass). Lean 4 core only, no axioms.
-/
import ym.WMass

namespace HeadUpper

open YMChain LinkIntegral SU2Char HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- dimL dominated by one more entry (g ≥ 0) -/
theorem dot_dimL_le_succ (h1 : O.le K.zero K.one) (g : Nat → R) (hg : ∀ t, O.le K.zero (g t)) :
    ∀ N m, O.le (dot K m (CgDim.dimL K m N) g) (dot K m (CgDim.dimL K m (N + 1)) g)
  | 0, m => by
    show O.le K.zero (K.add (K.mul (cN K (m + 1)) (g m)) K.zero)
    refine O.le_trans _ (K.mul (cN K (m + 1)) (g m)) _ (O.mul_nonneg (cN K (m + 1)) (g m) (SU2CharExp.cN_nn K O h1 (m + 1)) (hg m)) ?_
    exact TripleE.le_of_eq' K O _ _ (by lib_ring)
  | N + 1, m => by
    show O.le (K.add (K.mul (cN K (m + 1)) (g m)) (dot K (m + 1) (CgDim.dimL K (m + 1) N) g))
      (K.add (K.mul (cN K (m + 1)) (g m)) (dot K (m + 1) (CgDim.dimL K (m + 1) (N + 1)) g))
    exact O.add_le_add _ _ _ _ (O.le_refl _) (dot_dimL_le_succ h1 g hg N (m + 1))

/-- W of a product-coefficient list of length N ≤ the product of masses once N + 1 ≥ |a| + |b| -/
theorem W_cgc_le (h1 : O.le K.zero K.one) (a b : List R) (ha : ∀ k, O.le K.zero (co K a k))
    (hb : ∀ k, O.le K.zero (co K b k)) (N : Nat) (hN : a.length + b.length ≤ N + 1) :
    O.le (dot K 0 (CgDim.dimL K 0 N) (cgc K a b)) (K.mul (WMass.W K a) (WMass.W K b)) := by
  have d := CgDim.cgc_dim K a b (N + 1) hN
  have m := dot_dimL_le_succ K O h1 (cgc K a b) (WMass.cgc_nn0 K O h1 a b ha hb) N 0
  unfold WMass.W
  rw [← d]
  exact m

theorem W_nn (h1 : O.le K.zero K.one) (X : List R) (hX : ∀ k, O.le K.zero (co K X k)) : O.le K.zero (WMass.W K X) :=
  ConeBound.dot_nn K O _ (fun k => SU2CharExp.cN_nn K O h1 _) 0 X (RatioStep.mem_nn K O X hX)

/-- **upper bound through the two products**: X = P + T (≥ 0, length M + 2) gives
  (n+1)·Ysq(X)_n ≤ (n+1)·Ysq(P)_n + W(T)(W(X) + W(P))·(W(X)² + W(P)²) -/
theorem upper_Z (h1 : O.le K.zero K.one) (X P T : List R) (M : Nat) (hX : X.length = M + 2) (hP : P.length = M + 2)
    (hT : T.length = M + 2) (hs : ∀ k, co K X k = K.add (co K P k) (co K T k))
    (hPn : ∀ k, O.le K.zero (co K P k)) (hTn : ∀ k, O.le K.zero (co K T k)) (n : Nat) :
    O.le (K.mul (co K (FlowStep.Ysq K X M) n) (cN K (n + 1)))
      (K.add (K.mul (co K (FlowStep.Ysq K P M) n) (cN K (n + 1)))
        (K.mul (K.mul (WMass.W K T) (K.add (WMass.W K X) (WMass.W K P)))
          (K.add (K.mul (WMass.W K X) (WMass.W K X)) (K.mul (WMass.W K P) (WMass.W K P))))) := by
  have hXn : ∀ k, O.le K.zero (co K X k) := fun k => by
    rw [hs k]
    have := O.add_le_add _ _ _ _ (hPn k) (hTn k)
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this
  -- first product: Y = P1 + D1
  have lY : (L1Full.prodL K X M).length = 2 * M + 1 + 2 := HeatSecondStep.len_prodL K X M
  have lP1 : (L1Full.prodL K P M).length = 2 * M + 1 + 2 := HeatSecondStep.len_prodL K P M
  have lD1 : (ListRow.listF (fun t => K.add (cgc K T X t) (cgc K P T t)) 0 (2 * M + 3)).length = 2 * M + 1 + 2 :=
    OneStep.len_listF _ (2 * M + 3) 0
  have sp1 : ∀ t, co K (L1Full.prodL K X M) t = K.add (co K (L1Full.prodL K P M) t)
      (co K (ListRow.listF (fun t => K.add (cgc K T X t) (cgc K P T t)) 0 (2 * M + 3)) t) := fun t => by
    rw [L1Full.co_prodL K X M hX, L1Full.co_prodL K P M hP]
    cases Nat.lt_or_ge t (2 * M + 3) with
    | inl h =>
      rw [ListRow.co_listF K _ (2 * M + 3) 0 t h, Nat.zero_add]
      exact FlowBridge.cgc_proj_err K (M + 2) X P T X P T t (Nat.le_of_eq hX) (Nat.le_of_eq hP) (Nat.le_of_eq hT)
        (Nat.le_of_eq hX) (Nat.le_of_eq hP) (Nat.le_of_eq hT) hs hs
    | inr h =>
      rw [L1Full.cgc_far K X M hX t h, L1Full.cgc_far K P M hP t h, HeatLR.co_out K _ t (by rw [lD1]; exact h)]
      lib_ring
  have nY : ∀ t, O.le K.zero (co K (L1Full.prodL K X M) t) := fun t => by
    rw [L1Full.co_prodL K X M hX]; exact WMass.cgc_nn0 K O h1 X X hXn hXn t
  have nP1 : ∀ t, O.le K.zero (co K (L1Full.prodL K P M) t) := fun t => by
    rw [L1Full.co_prodL K P M hP]; exact WMass.cgc_nn0 K O h1 P P hPn hPn t
  have nD1 : ∀ t, O.le K.zero (co K (ListRow.listF (fun t => K.add (cgc K T X t) (cgc K P T t)) 0 (2 * M + 3)) t) :=
    fun t => by
      cases Nat.lt_or_ge t (2 * M + 3) with
      | inl h =>
        rw [ListRow.co_listF K _ (2 * M + 3) 0 t h, Nat.zero_add]
        have := O.add_le_add _ _ _ _ (WMass.cgc_nn0 K O h1 T X hTn hXn t) (WMass.cgc_nn0 K O h1 P T hPn hTn t)
        exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this
      | inr h => rw [HeatLR.co_out K _ t (by rw [lD1]; exact h)]; exact O.le_refl _
  -- second product
  have eZ : co K (FlowStep.Ysq K X M) n = K.add (cgc K (L1Full.prodL K P M) (L1Full.prodL K P M) n)
      (K.add (cgc K (ListRow.listF (fun t => K.add (cgc K T X t) (cgc K P T t)) 0 (2 * M + 3)) (L1Full.prodL K X M) n)
        (cgc K (L1Full.prodL K P M) (ListRow.listF (fun t => K.add (cgc K T X t) (cgc K P T t)) 0 (2 * M + 3)) n)) := by
    unfold FlowStep.Ysq
    rw [L1Full.co_prodL K _ (2 * M + 1) lY]
    exact FlowBridge.cgc_proj_err K (2 * M + 3) _ _ _ _ _ _ n (Nat.le_of_eq lY) (Nat.le_of_eq lP1) (Nat.le_of_eq lD1)
      (Nat.le_of_eq lY) (Nat.le_of_eq lP1) (Nat.le_of_eq lD1) sp1 sp1
  have eP : co K (FlowStep.Ysq K P M) n = cgc K (L1Full.prodL K P M) (L1Full.prodL K P M) n := by
    unfold FlowStep.Ysq; exact L1Full.co_prodL K _ (2 * M + 1) lP1 n
  have t1 := WMass.cgc_term K O h1 _ _ nD1 nY n
  have t2 := WMass.cgc_term K O h1 _ _ nP1 nD1 n
  -- masses
  have wY := WMass.W_prodL K X M hX
  have wP := WMass.W_prodL K P M hP
  have wD : O.le (WMass.W K (ListRow.listF (fun t => K.add (cgc K T X t) (cgc K P T t)) 0 (2 * M + 3)))
      (K.mul (WMass.W K T) (K.add (WMass.W K X) (WMass.W K P))) := by
    unfold WMass.W
    rw [WMass.dot_listF_dimL K _ (2 * M + 3) 0, SU2Char.dot_fadd K (cgc K T X) (cgc K P T) 0]
    have a := W_cgc_le K O h1 T X hTn hXn (2 * M + 3) (by rw [hT, hX]; exact Nat.le_of_eq (by lib_nat))
    have b := W_cgc_le K O h1 P T hPn hTn (2 * M + 3) (by rw [hP, hT]; exact Nat.le_of_eq (by lib_nat))
    unfold WMass.W at a b
    exact O.le_trans _ _ _ (O.add_le_add _ _ _ _ a b) (TripleE.le_of_eq' K O _ _ (by lib_ring))
  have wYn := W_nn K O h1 _ nY
  have wPn := W_nn K O h1 _ nP1
  have s1 := O.add_le_add _ _ _ _ t1 t2
  have wD' := O.mul_le_mul_right _ _ (K.add (WMass.W K (L1Full.prodL K X M)) (WMass.W K (L1Full.prodL K P M))) wD
    (by have := O.add_le_add _ _ _ _ wYn wPn; exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) this)
  rw [eZ, eP]
  rw [wY, wP] at wD' s1
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _
    (O.add_le_add _ _ _ _ (O.le_refl (K.mul (cgc K (L1Full.prodL K P M) (L1Full.prodL K P M) n) (cN K (n + 1)))) s1)
    (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_)
      (O.add_le_add _ _ _ _ (O.le_refl (K.mul (cgc K (L1Full.prodL K P M) (L1Full.prodL K P M) n) (cN K (n + 1)))) wD')))
  · lib_ring
  · lib_ring

/-- **bounds through the decimation**: lo ≤ Ysq(X)_n ≤ up (0 ≤ lo) give lo⁴·c ≤ mk(X)_n ≤ up⁴·c with
  c = (1/(n+1))³ -/
theorem mk_bounds (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X : List R) (M n : Nat)
    (lo up : R) (hlo0 : O.le K.zero lo) (hlo : O.le lo (co K (FlowStep.Ysq K X M) n))
    (hup : O.le (co K (FlowStep.Ysq K X M) n) up) :
    O.le (K.mul (pwR K lo 4) (pwR K (F.inv (cN K (0 + n + 1))) 3)) (co K (FlowStep.mk K O F X M) n) ∧
      O.le (co K (FlowStep.mk K O F X M) n) (K.mul (pwR K up 4) (pwR K (F.inv (cN K (0 + n + 1))) 3)) := by
  unfold FlowStep.mk
  rw [RatioStep.co_decM K O F _ 0 n]
  have c := SU2CharExp.pos_of_ge_one K O h1 h10 (0 + n + 1) (Nat.le_add_left 1 (0 + n))
  have i := AreaLower.pw_nn K O _ (F.inv_pos _ c.1 c.2) h1 3
  have y0 := O.le_trans _ _ _ hlo0 hlo
  exact ⟨O.mul_le_mul_right _ _ _ (SU2Strong4D.pw_mono K O h1 _ _ hlo0 hlo 4) i,
    O.mul_le_mul_right _ _ _ (SU2Strong4D.pw_mono K O h1 _ _ y0 hup 4) i⟩

end HeadUpper

#print axioms HeadUpper.dot_dimL_le_succ
#print axioms HeadUpper.W_cgc_le
#print axioms HeadUpper.W_nn
#print axioms HeadUpper.upper_Z
#print axioms HeadUpper.mk_bounds

ym/RungCheck.lean

/-
  ym/RungCheck — T45, G78g route (B): the natural-number check of the head cross inequalities of one rung, with
  separate lower (Hlo) and upper (Hup, plus the mass error e) lists:
    ((Hup·Hup)·(Hup·Hup))_{n+1} + e)⁴ (n+1)³ K₂(n) ≤ ((Hlo·Hlo)·(Hlo·Hlo))_n⁴ (n+2)³ K₂(n+1)   for n < N.
  Lean 4 core only, no axioms.
-/
import ym.HeadUpper
import ym.HeatLadder

namespace RungCheck

open YMChain LinkIntegral SU2Char HeatPair MigdalCone

/-- the Bool check on n < N -/
def rungsLU (Hlo Hup K2 : List Nat) (Mlo Mup e : Nat) : Nat → Bool
  | 0 => true
  | N + 1 => rungsLU Hlo Hup K2 Mlo Mup e N &&
      Nat.ble ((CertSound.cgcN (HeatLadder.ZN Hup Mup) (HeatLadder.ZN Hup Mup) (N + 1) + e) ^ 4 * (N + 1) ^ 3 *
          HeatLadder.coN K2 N)
        (CertSound.cgcN (HeatLadder.ZN Hlo Mlo) (HeatLadder.ZN Hlo Mlo) N ^ 4 * (N + 2) ^ 3 * HeatLadder.coN K2 (N + 1))

theorem rungsLU_le (Hlo Hup K2 : List Nat) (Mlo Mup e : Nat) : ∀ N, rungsLU Hlo Hup K2 Mlo Mup e N = true →
    ∀ n, n < N →
      (CertSound.cgcN (HeatLadder.ZN Hup Mup) (HeatLadder.ZN Hup Mup) (n + 1) + e) ^ 4 * (n + 1) ^ 3 * HeatLadder.coN K2 n
        ≤ CertSound.cgcN (HeatLadder.ZN Hlo Mlo) (HeatLadder.ZN Hlo Mlo) n ^ 4 * (n + 2) ^ 3 * HeatLadder.coN K2 (n + 1)
  | 0, _, n, hn => absurd hn (Nat.not_lt_zero n)
  | N + 1, h, n, hn => by
    have h' : rungsLU Hlo Hup K2 Mlo Mup e N = true ∧ Nat.ble
        ((CertSound.cgcN (HeatLadder.ZN Hup Mup) (HeatLadder.ZN Hup Mup) (N + 1) + e) ^ 4 * (N + 1) ^ 3 * HeatLadder.coN K2 N)
        (CertSound.cgcN (HeatLadder.ZN Hlo Mlo) (HeatLadder.ZN Hlo Mlo) N ^ 4 * (N + 2) ^ 3 * HeatLadder.coN K2 (N + 1)) = true := by
      change (rungsLU Hlo Hup K2 Mlo Mup e N && _) = true at h
      cases hr : rungsLU Hlo Hup K2 Mlo Mup e N with
      | false => rw [hr] at h; exact absurd h (by intro e; cases e)
      | true => rw [hr] at h; exact ⟨rfl, h⟩
    cases Nat.lt_or_ge n N with
    | inl hl => exact rungsLU_le Hlo Hup K2 Mlo Mup e N h'.1 n hl
    | inr hg =>
      have e' : n = N := Nat.le_antisymm (Nat.le_of_lt_succ hn) hg
      rw [e']
      exact Nat.le_of_ble_eq_true h'.2

/-- **upper chain**: for G′ ≥ 0 of length M + 2 and the exact part of M0 + 2 characters,
  (n+1)·Ysq(G′)_n ≤ (n+1)·Ysq(first M0 + 2 coefficients of G′)_n + W(T)(W(G′) + W(P))(W(G′)² + W(P)²) with
  P, T the truncation and the tail -/
theorem upper_chain {R : Type} (K : DetChain.CRing R) (O : ORing K) (h1 : O.le K.zero K.one) (G : List R)
    (M M0 : Nat) (hG : G.length = M + 2) (hGn : ∀ k, O.le K.zero (co K G k)) (hN : M0 + 2 ≤ M + 2) (n : Nat) :
    O.le (K.mul (co K (FlowStep.Ysq K G M) n) (cN K (n + 1)))
      (K.add (K.mul (co K (FlowStep.Ysq K (lmap (fun j => co K G j) 0 (M0 + 2)) M0) n) (cN K (n + 1)))
        (K.mul (K.mul (WMass.W K (HeatTrunc.tlL K G (M0 + 2) (M + 2)))
            (K.add (WMass.W K G) (WMass.W K (HeatTrunc.trL K G (M0 + 2) (M + 2)))))
          (K.add (K.mul (WMass.W K G) (WMass.W K G))
            (K.mul (WMass.W K (HeatTrunc.trL K G (M0 + 2) (M + 2))) (WMass.W K (HeatTrunc.trL K G (M0 + 2) (M + 2))))))) := by
  have u := HeadUpper.upper_Z K O h1 G (HeatTrunc.trL K G (M0 + 2) (M + 2)) (HeatTrunc.tlL K G (M0 + 2) (M + 2)) M hG
    (HeatTrunc.len_trL K G (M0 + 2) (M + 2)) (HeatTrunc.len_tlL K G (M0 + 2) (M + 2))
    (HeatTrunc.split_trL K G (M0 + 2) (M + 2) hG) (fun k => (HeatTrunc.trL_le K O G hGn (M0 + 2) (M + 2) k).1)
    (HeatTrunc.tlL_nn K O G hGn (M0 + 2) (M + 2)) n
  rw [MkExt.ysq_ext K (HeatTrunc.trL K G (M0 + 2) (M + 2)) (lmap (fun j => co K G j) 0 (M0 + 2)) M M0
    (HeatTrunc.len_trL K G (M0 + 2) (M + 2)) (WilsonGood.len_lmap _ 0 (M0 + 2))
    (HeadBound.co_trL_lmap K G (M0 + 2) (M + 2) hN) n] at u
  exact u

/-- **one head cross inequality from two-sided bounds of the second product**: if
  0 ≤ Ysq(Y)_{n+1} ≤ A + e and B ≤ Ysq(Y)_n and the natural-number check (A + e)⁴ (n+1)³ k₀ ≤ B⁴ (n+2)³ k₁ holds,
  then mk(Y)_{n+1}·k₀ ≤ mk(Y)_n·k₁ -/
theorem cross_LU {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) (h1 : O.le K.zero K.one)
    (h10 : K.one ≠ K.zero) (Y : List R) (M n A B e k0 k1 : Nat)
    (hs0 : O.le K.zero (co K (FlowStep.Ysq K Y M) (n + 1)))
    (hup : O.le (co K (FlowStep.Ysq K Y M) (n + 1)) (cN K (A + e)))
    (hlo : O.le (cN K B) (co K (FlowStep.Ysq K Y M) n))
    (hN : (A + e) ^ 4 * (n + 1) ^ 3 * k0 ≤ B ^ 4 * (n + 2) ^ 3 * k1) :
    O.le (K.mul (co K (FlowStep.mk K O F Y M) (n + 1)) (cN K k0))
      (K.mul (co K (FlowStep.mk K O F Y M) n) (cN K k1)) := by
  have hB0 : O.le K.zero (cN K B) := SU2CharExp.cN_mono K O h1 0 B (Nat.zero_le B)
  have k0n : O.le K.zero (cN K k0) := SU2CharExp.cN_mono K O h1 0 k0 (Nat.zero_le k0)
  have k1n : O.le K.zero (cN K k1) := SU2CharExp.cN_mono K O h1 0 k1 (Nat.zero_le k1)
  have bu := (HeadUpper.mk_bounds K O F h1 h10 Y M (n + 1) K.zero (cN K (A + e)) (O.le_refl _) hs0 hup).2
  have bl := (HeadUpper.mk_bounds K O F h1 h10 Y M n (cN K B) (co K (FlowStep.Ysq K Y M) n) hB0 hlo (O.le_refl _)).1
  rw [show 0 + (n + 1) + 1 = n + 2 by lib_nat] at bu
  rw [show 0 + n + 1 = n + 1 by lib_nat] at bl
  have s1 := O.mul_le_mul_right _ _ (cN K k0) bu k0n
  have s3 := O.mul_le_mul_right _ _ (cN K k1) bl k1n
  refine O.le_trans _ _ _ s1 (O.le_trans _ _ _ ?_ s3)
  -- the middle: U⁴ (n+2)⁻³ k₀ ≤ B⁴ (n+1)⁻³ k₁
  have p1 := SU2CharExp.pos_of_ge_one K O h1 h10 (n + 1) (Nat.le_add_left 1 n)
  have p2 := SU2CharExp.pos_of_ge_one K O h1 h10 (n + 2) (Nat.le_add_left 1 (n + 1))
  have i1 := F.mul_inv _ p1.2
  have i2 := F.mul_inv _ p2.2
  have hP : 1 ≤ (n + 1) ^ 3 * (n + 2) ^ 3 := by lib_nat_le
  have pP := SU2CharExp.pos_of_ge_one K O h1 h10 _ hP
  refine GridQ.cancel K O F (cN K ((n + 1) ^ 3 * (n + 2) ^ 3)) _ _ pP.1 pP.2 ?_
  have le := SU2CharExp.cN_mono K O h1 _ _ hN
  rw [SU2CharExp.cN_mul K, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K,
    CertSound.cN_pow K, CertSound.cN_pow K, CertSound.cN_pow K, CertSound.cN_pow K] at le
  rw [SU2CharExp.cN_mul K, CertSound.cN_pow K, CertSound.cN_pow K]
  generalize cN K (A + e) = U at le ⊢
  generalize cN K B = Bc at le ⊢
  generalize cN K k0 = c0 at le ⊢
  generalize cN K k1 = c1' at le ⊢
  generalize cN K (n + 1) = c1 at le i1 ⊢
  generalize cN K (n + 2) = c2 at le i2 ⊢
  have q1 := HeatLadder.pw_inv K O F c1 i1 3
  have q2 := HeatLadder.pw_inv K O F c2 i2 3
  refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ le (TripleE.le_of_eq' K O _ _ ?_))
  · rw [show K.mul (K.mul (pwR K c1 3) (pwR K c2 3)) (K.mul (K.mul (pwR K U 4) (pwR K (F.inv c2) 3)) c0)
        = K.mul (K.mul (K.mul (pwR K U 4) (pwR K c1 3)) c0) (K.mul (pwR K c2 3) (pwR K (F.inv c2) 3)) by lib_ring, q2]
    lib_ring
  · rw [show K.mul (K.mul (pwR K c1 3) (pwR K c2 3)) (K.mul (K.mul (pwR K Bc 4) (pwR K (F.inv c1) 3)) c1')
        = K.mul (K.mul (K.mul (pwR K Bc 4) (pwR K c2 3)) c1') (K.mul (pwR K c1 3) (pwR K (F.inv c1) 3)) by lib_ring, q1]
    lib_ring

/-- the second product is coefficientwise monotone on nonnegative lists of the same length -/
theorem ysq_mono {R : Type} (K : DetChain.CRing R) (O : ORing K) (h1 : O.le K.zero K.one) (X Y : List R) (M : Nat)
    (hX : X.length = M + 2) (hY : Y.length = M + 2) (hXn : ∀ k, O.le K.zero (co K X k))
    (hXY : ∀ k, O.le (co K X k) (co K Y k)) (n : Nat) :
    O.le K.zero (co K (FlowStep.Ysq K X M) n) ∧ O.le (co K (FlowStep.Ysq K X M) n) (co K (FlowStep.Ysq K Y M) n) := by
  have lX : (L1Full.prodL K X M).length = 2 * M + 1 + 2 := HeatSecondStep.len_prodL K X M
  have lY : (L1Full.prodL K Y M).length = 2 * M + 1 + 2 := HeatSecondStep.len_prodL K Y M
  have p := HeatTrunc.prodL_mono K O h1 X Y M hX hY hXn hXY
  unfold FlowStep.Ysq
  exact HeatTrunc.prodL_mono K O h1 _ _ (2 * M + 1) lX lY (fun k => (p k).1) (fun k => (p k).2) n

/-- **lower bound of the second product by the head**: Ysq(first M0 + 2 coefficients of X) ≤ Ysq(X) -/
theorem ysq_head_le {R : Type} (K : DetChain.CRing R) (O : ORing K) (h1 : O.le K.zero K.one) (X : List R) (M M0 : Nat)
    (hX : X.length = M + 2) (hXn : ∀ k, O.le K.zero (co K X k)) (hN : M0 + 2 ≤ M + 2) (n : Nat) :
    O.le (co K (FlowStep.Ysq K (lmap (fun j => co K X j) 0 (M0 + 2)) M0) n) (co K (FlowStep.Ysq K X M) n) := by
  rw [← MkExt.ysq_ext K (HeatTrunc.trL K X (M0 + 2) (M + 2)) (lmap (fun j => co K X j) 0 (M0 + 2)) M M0
    (HeatTrunc.len_trL K X (M0 + 2) (M + 2)) (WilsonGood.len_lmap _ 0 (M0 + 2))
    (HeadBound.co_trL_lmap K X (M0 + 2) (M + 2) hN) n]
  exact (ysq_mono K O h1 _ X M (HeatTrunc.len_trL K X (M0 + 2) (M + 2)) hX
    (fun k => (HeatTrunc.trL_le K O X hXn (M0 + 2) (M + 2) k).1)
    (fun k => (HeatTrunc.trL_le K O X hXn (M0 + 2) (M + 2) k).2) n).2

/-- **scaling the next reference**: a cross inequality against k₀, k₁ transfers to M′ with M′_n = s·k₀,
  M′_{n+1} = s·k₁, s ≥ 0 -/
theorem cross_scale {R : Type} (K : DetChain.CRing R) (O : ORing K) (M' : List R) (n : Nat) (s y0 y1 k0 k1 : R)
    (hs : O.le K.zero s) (h0 : co K M' n = K.mul s k0) (h1' : co K M' (n + 1) = K.mul s k1)
    (h : O.le (K.mul y1 k0) (K.mul y0 k1)) :
    O.le (K.mul y1 (co K M' n)) (K.mul y0 (co K M' (n + 1))) := by
  rw [h0, h1']
  have w := O.mul_le_mul_right _ _ s h hs
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring)))

theorem len_map {R : Type} (K : DetChain.CRing R) : ∀ (H : List Nat), (H.map (cN K)).length = H.length
  | [] => rfl
  | _ :: t => by show (t.map (cN K)).length + 1 = t.length + 1; rw [len_map K t]

/-- **the certified lower bound of the second product**: a natural-number head Hlo (length M0 + 2) below the first
  M0 + 2 coefficients of Y ≥ 0 gives ((Hlo·Hlo)·(Hlo·Hlo))_n ≤ Ysq(Y)_n -/
theorem ysq_lo {R : Type} (K : DetChain.CRing R) (O : ORing K) (h1 : O.le K.zero K.one) (Y : List R) (Hlo : List Nat)
    (M M0 : Nat) (hY : Y.length = M + 2) (hYn : ∀ k, O.le K.zero (co K Y k)) (hH : Hlo.length = M0 + 2)
    (hN : M0 + 2 ≤ M + 2) (hHn : ∀ k, O.le K.zero (co K (Hlo.map (cN K)) k))
    (hle : ∀ k, O.le (co K (Hlo.map (cN K)) k) (co K (lmap (fun j => co K Y j) 0 (M0 + 2)) k)) (n : Nat) :
    O.le (cN K (CertSound.cgcN (HeatLadder.ZN Hlo M0) (HeatLadder.ZN Hlo M0) n)) (co K (FlowStep.Ysq K Y M) n) := by
  rw [← HeatLadder.co_Ysq K Hlo M0 hH n]
  have hl : (Hlo.map (cN K)).length = M0 + 2 := by rw [len_map K Hlo]; exact hH
  exact O.le_trans _ _ _
    (ysq_mono K O h1 _ _ M0 hl (WilsonGood.len_lmap _ 0 (M0 + 2)) hHn hle n).2
    (ysq_head_le K O h1 Y M M0 hY hYn hN n)

/-- **from the upper chain to a plain upper bound**: y·c ≤ P·c + E with c ≥ 1, P ≤ A, E ≤ ε, ε ≥ 0 gives y ≤ A + ε -/
theorem up_of_chain {R : Type} (K : DetChain.CRing R) (O : ORing K) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (y P E A ε c : R) (hc : O.le K.one c) (hε : O.le K.zero ε) (hP : O.le P A) (hE : O.le E ε)
    (h : O.le (K.mul y c) (K.add (K.mul P c) E)) : O.le y (K.add A ε) := by
  have c0 : O.le K.zero c := O.le_trans _ _ _ h1 hc
  have cne : c ≠ K.zero := fun e => h10 (O.le_antisymm _ _ (by rw [← e]; exact hc) h1)
  refine O.pos_cancel _ _ c c0 cne (O.le_trans _ _ _ h ?_)
  -- P·c + E ≤ A·c + ε ≤ (A + ε)·c = A·c + ε·c, ε ≤ ε·c
  have s1 := O.add_le_add _ _ _ _ (O.mul_le_mul_right _ _ c hP c0) hE
  have s2 : O.le ε (K.mul ε c) := O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring))
    (O.le_trans _ _ _ (O.mul_le_mul_right _ _ ε hc hε) (TripleE.le_of_eq' K O _ _ (by lib_ring)))
  refine O.le_trans _ _ _ s1 (O.le_trans _ _ _ (O.add_le_add _ _ _ _ (O.le_refl _) s2) (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **one certified head cross inequality of a rung**: Y ≥ 0 of length M + 2 whose first M0 + 2 coefficients lie
  between the natural-number heads Hlo ≤ · ≤ Hup, the mass error of the upper chain at level n + 1 below e, and the
  natural-number check (Q_up(n+1) + e)⁴ (n+1)³ k₀ ≤ Q_lo(n)⁴ (n+2)³ k₁ give mk(Y)_{n+1}·k₀ ≤ mk(Y)_n·k₁ -/
theorem head_cross {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) (h1 : O.le K.zero K.one)
    (h10 : K.one ≠ K.zero) (Y : List R) (Hlo Hup : List Nat) (M M0 n e k0 k1 : Nat) (hY : Y.length = M + 2)
    (hYn : ∀ k, O.le K.zero (co K Y k)) (hlo : Hlo.length = M0 + 2) (hupl : Hup.length = M0 + 2) (hN : M0 + 2 ≤ M + 2)
    (hHn : ∀ k, O.le K.zero (co K (Hlo.map (cN K)) k))
    (hle : ∀ k, O.le (co K (Hlo.map (cN K)) k) (co K (lmap (fun j => co K Y j) 0 (M0 + 2)) k))
    (hge : ∀ k, O.le (co K (lmap (fun j => co K Y j) 0 (M0 + 2)) k) (co K (Hup.map (cN K)) k))
    (hW : O.le (K.mul (K.mul (WMass.W K (HeatTrunc.tlL K Y (M0 + 2) (M + 2)))
            (K.add (WMass.W K Y) (WMass.W K (HeatTrunc.trL K Y (M0 + 2) (M + 2)))))
          (K.add (K.mul (WMass.W K Y) (WMass.W K Y))
            (K.mul (WMass.W K (HeatTrunc.trL K Y (M0 + 2) (M + 2))) (WMass.W K (HeatTrunc.trL K Y (M0 + 2) (M + 2))))))
        (cN K e))
    (hNat : (CertSound.cgcN (HeatLadder.ZN Hup M0) (HeatLadder.ZN Hup M0) (n + 1) + e) ^ 4 * (n + 1) ^ 3 * k0
        ≤ CertSound.cgcN (HeatLadder.ZN Hlo M0) (HeatLadder.ZN Hlo M0) n ^ 4 * (n + 2) ^ 3 * k1) :
    O.le (K.mul (co K (FlowStep.mk K O F Y M) (n + 1)) (cN K k0))
      (K.mul (co K (FlowStep.mk K O F Y M) n) (cN K k1)) := by
  have hl : (Hup.map (cN K)).length = M0 + 2 := by rw [len_map K Hup]; exact hupl
  have hs0 := (ysq_mono K O h1 Y Y M hY hY hYn (fun k => O.le_refl _) (n + 1)).1
  have hlo' := ysq_lo K O h1 Y Hlo M M0 hY hYn hlo hN hHn hle n
  -- the head part at level n + 1 is below the Hup value
  have hhead : O.le (co K (FlowStep.Ysq K (lmap (fun j => co K Y j) 0 (M0 + 2)) M0) (n + 1))
      (cN K (CertSound.cgcN (HeatLadder.ZN Hup M0) (HeatLadder.ZN Hup M0) (n + 1))) := by
    rw [← HeatLadder.co_Ysq K Hup M0 hupl (n + 1)]
    exact (ysq_mono K O h1 _ _ M0 (WilsonGood.len_lmap _ 0 (M0 + 2)) hl
      (fun k => O.le_trans _ _ _ (hHn k) (hle k)) hge (n + 1)).2
  have uc := upper_chain K O h1 Y M M0 hY hYn hN (n + 1)
  have c1 : O.le K.one (cN K (n + 1 + 1)) := by
    rw [← SU2CharExp.cN_one K]; exact SU2CharExp.cN_mono K O h1 1 (n + 1 + 1) (Nat.le_add_left 1 (n + 1))
  have e0 : O.le K.zero (cN K e) := SU2CharExp.cN_mono K O h1 0 e (Nat.zero_le e)
  have up := up_of_chain K O h1 h10 _ _ _ _ _ _ c1 e0 hhead hW uc
  rw [← SU2Char.cN_add K] at up
  exact cross_LU K O F h1 h10 Y M n _ _ e k0 k1 hs0 up hlo' hNat

/-- **all head cross inequalities of a rung from one kernel check** (rungsLU, levels n < N) -/
theorem heads_cross {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) (h1 : O.le K.zero K.one)
    (h10 : K.one ≠ K.zero) (Y : List R) (Hlo Hup K2 : List Nat) (M M0 N e : Nat) (hY : Y.length = M + 2)
    (hYn : ∀ k, O.le K.zero (co K Y k)) (hlo : Hlo.length = M0 + 2) (hupl : Hup.length = M0 + 2) (hN : M0 + 2 ≤ M + 2)
    (hHn : ∀ k, O.le K.zero (co K (Hlo.map (cN K)) k))
    (hle : ∀ k, O.le (co K (Hlo.map (cN K)) k) (co K (lmap (fun j => co K Y j) 0 (M0 + 2)) k))
    (hge : ∀ k, O.le (co K (lmap (fun j => co K Y j) 0 (M0 + 2)) k) (co K (Hup.map (cN K)) k))
    (hW : O.le (K.mul (K.mul (WMass.W K (HeatTrunc.tlL K Y (M0 + 2) (M + 2)))
            (K.add (WMass.W K Y) (WMass.W K (HeatTrunc.trL K Y (M0 + 2) (M + 2)))))
          (K.add (K.mul (WMass.W K Y) (WMass.W K Y))
            (K.mul (WMass.W K (HeatTrunc.trL K Y (M0 + 2) (M + 2))) (WMass.W K (HeatTrunc.trL K Y (M0 + 2) (M + 2))))))
        (cN K e))
    (hc : rungsLU Hlo Hup K2 M0 M0 e N = true) (n : Nat) (hn : n < N) :
    O.le (K.mul (co K (FlowStep.mk K O F Y M) (n + 1)) (cN K (HeatLadder.coN K2 n)))
      (K.mul (co K (FlowStep.mk K O F Y M) n) (cN K (HeatLadder.coN K2 (n + 1)))) :=
  head_cross K O F h1 h10 Y Hlo Hup M M0 n e _ _ hY hYn hlo hupl hN hHn hle hge hW
    (rungsLU_le Hlo Hup K2 M0 M0 e N hc n hn)

theorem mul_both {R : Type} (K : DetChain.CRing R) (O : ORing K) (a b c d : R) (hb : O.le K.zero b)
    (hc : O.le K.zero c) (h1 : O.le a b) (h2 : O.le c d) : O.le (K.mul a c) (K.mul b d) :=
  O.le_trans _ _ _ (O.mul_le_mul_right _ _ c h1 hc)
    (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring))
      (O.le_trans _ _ _ (O.mul_le_mul_right _ _ b h2 hb) (TripleE.le_of_eq' K O _ _ (by lib_ring))))

/-- **monotone mass error**: 0 ≤ t ≤ τ, 0 ≤ a ≤ P, 0 ≤ x ≤ X give t(x + a)(x² + a²) ≤ τ(X + P)(X² + P²) -/
theorem err_mono {R : Type} (K : DetChain.CRing R) (O : ORing K) (t a x τ P X : R) (ht : O.le K.zero t)
    (ha : O.le K.zero a) (hx : O.le K.zero x) (htτ : O.le t τ) (haP : O.le a P) (hxX : O.le x X) :
    O.le (K.mul (K.mul t (K.add x a)) (K.add (K.mul x x) (K.mul a a)))
      (K.mul (K.mul τ (K.add X P)) (K.add (K.mul X X) (K.mul P P))) := by
  have P0 := O.le_trans _ _ _ ha haP
  have X0 := O.le_trans _ _ _ hx hxX
  have τ0 := O.le_trans _ _ _ ht htτ
  have s1 := O.add_le_add _ _ _ _ hxX haP
  have s2 := O.add_le_add _ _ _ _ (mul_both K O _ _ _ _ X0 hx hxX hxX) (mul_both K O _ _ _ _ P0 ha haP haP)
  have xa0 : O.le K.zero (K.add x a) :=
    O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.add_le_add _ _ _ _ hx ha)
  have q1 := mul_both K O t τ (K.add x a) (K.add X P) τ0 xa0 htτ s1
  have n2 : O.le K.zero (K.add (K.mul x x) (K.mul a a)) :=
    O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.add_le_add _ _ _ _ (O.mul_nonneg _ _ hx hx) (O.mul_nonneg _ _ ha ha))
  exact mul_both K O _ _ _ _ (O.mul_nonneg _ _ τ0 (O.le_trans _ _ _ xa0 s1)) n2 q1 s2

/-- **the mass splits** at the truncation point: W(X) = W(first N) + W(tail) -/
theorem W_split {R : Type} (K : DetChain.CRing R) (X : List R) (N L : Nat) (hX : X.length = L) :
    WMass.W K X = K.add (WMass.W K (HeatTrunc.trL K X N L)) (WMass.W K (HeatTrunc.tlL K X N L)) := by
  unfold WMass.W
  rw [TripleA.dot_sumN K _ X 0, TripleA.dot_sumN K _ _ 0, TripleA.dot_sumN K _ _ 0, hX, HeatTrunc.len_trL K X N L,
    HeatTrunc.len_tlL K X N L, ← QBinom.sumN_add K]
  exact QBinom.sumN_ext K _ _ (fun j => by rw [HeatTrunc.split_trL K X N L hX j]; lib_ring) L

end RungCheck

#print axioms RungCheck.rungsLU_le
#print axioms RungCheck.upper_chain
#print axioms RungCheck.cross_LU
#print axioms RungCheck.ysq_mono
#print axioms RungCheck.ysq_head_le
#print axioms RungCheck.cross_scale
#print axioms RungCheck.len_map
#print axioms RungCheck.ysq_lo
#print axioms RungCheck.up_of_chain
#print axioms RungCheck.head_cross
#print axioms RungCheck.heads_cross
#print axioms RungCheck.mul_both
#print axioms RungCheck.err_mono
#print axioms RungCheck.W_split

ym/RungFull.lean

/-
  ym/RungFull — T45, G78g route (B): the head hypothesis of RungB.rung_B from one kernel check. The reference Mr
  (nonnegative, length M + 2) lies between natural-number heads Hlo ≤ · ≤ Hup on its first M0 + 2 characters, its
  mass error is below e, and the next reference M′ is a nonnegative multiple s of the natural-number list K2 on the
  first N + 1 characters; then rungsLU gives the cross inequalities mk(Mr)_{n+1}·M′_n ≤ mk(Mr)_n·M′_{n+1}, n < N.
  Lean 4 core only, no axioms.
-/
import ym.RungCheck
import ym.RungB

namespace RungFull

open YMChain LinkIntegral SU2Char HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

/-- **the head of one rung from one kernel check** -/
theorem hhead_of_cert (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Mr M' : List R) (Hlo Hup K2 : List Nat)
    (M M0 N e : Nat) (s : R) (hs : O.le K.zero s) (hMr : Mr.length = M + 2) (hMn : ∀ k, O.le K.zero (co K Mr k))
    (hlo : Hlo.length = M0 + 2) (hupl : Hup.length = M0 + 2) (hN : M0 + 2 ≤ M + 2)
    (hHn : ∀ k, O.le K.zero (co K (Hlo.map (cN K)) k))
    (hle : ∀ k, O.le (co K (Hlo.map (cN K)) k) (co K (lmap (fun j => co K Mr j) 0 (M0 + 2)) k))
    (hge : ∀ k, O.le (co K (lmap (fun j => co K Mr j) 0 (M0 + 2)) k) (co K (Hup.map (cN K)) k))
    (hW : O.le (K.mul (K.mul (WMass.W K (HeatTrunc.tlL K Mr (M0 + 2) (M + 2)))
            (K.add (WMass.W K Mr) (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2)))))
          (K.add (K.mul (WMass.W K Mr) (WMass.W K Mr))
            (K.mul (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2))) (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2))))))
        (cN K e))
    (hM' : ∀ n, n ≤ N → co K M' n = K.mul s (cN K (HeatLadder.coN K2 n)))
    (hc : RungCheck.rungsLU Hlo Hup K2 M0 M0 e N = true) :
    ∀ n, n < N → O.le (K.mul (co K (FlowStep.mk K O F Mr M) (n + 1)) (co K M' n))
      (K.mul (co K (FlowStep.mk K O F Mr M) n) (co K M' (n + 1))) := fun n hn =>
  RungCheck.cross_scale K O M' n s _ _ _ _ hs (hM' n (Nat.le_of_lt hn)) (hM' (n + 1) hn)
    (RungCheck.heads_cross K O F h1 h10 Mr Hlo Hup K2 M M0 N e hMr hMn hlo hupl hN hHn hle hge hW hc n hn)

/-- **the mass error from head and tail mass bounds**: W(head) ≤ P, W(tail) ≤ τ and τ(P + τ + P)((P + τ)² + P²) ≤ e
  give the hypothesis hW of hhead_of_cert -/
theorem hW_of_bounds (h1 : O.le K.zero K.one) (Mr : List R) (M M0 e : Nat) (hMr : Mr.length = M + 2)
    (hMn : ∀ k, O.le K.zero (co K Mr k)) (P τ : R)
    (hP : O.le (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2))) P)
    (hτ : O.le (WMass.W K (HeatTrunc.tlL K Mr (M0 + 2) (M + 2))) τ)
    (he : O.le (K.mul (K.mul τ (K.add (K.add P τ) P)) (K.add (K.mul (K.add P τ) (K.add P τ)) (K.mul P P))) (cN K e)) :
    O.le (K.mul (K.mul (WMass.W K (HeatTrunc.tlL K Mr (M0 + 2) (M + 2)))
            (K.add (WMass.W K Mr) (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2)))))
          (K.add (K.mul (WMass.W K Mr) (WMass.W K Mr))
            (K.mul (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2))) (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2))))))
        (cN K e) := by
  have t0 := HeadUpper.W_nn K O h1 _ (HeatTrunc.tlL_nn K O Mr hMn (M0 + 2) (M + 2))
  have a0 := HeadUpper.W_nn K O h1 _ (fun k => (HeatTrunc.trL_le K O Mr hMn (M0 + 2) (M + 2) k).1)
  have x0 := HeadUpper.W_nn K O h1 Mr hMn
  have hx : O.le (WMass.W K Mr) (K.add P τ) := by
    rw [RungCheck.W_split K Mr (M0 + 2) (M + 2) hMr]
    exact O.add_le_add _ _ _ _ hP hτ
  exact O.le_trans _ _ _ (RungCheck.err_mono K O _ _ _ _ _ _ t0 a0 x0 hτ hP hx) he

/-- **the head mass from the natural-number upper head**: W(first M0 + 2 coefficients of Mr) ≤ W(Hup) -/
theorem hP_of_head (h1 : O.le K.zero K.one) (Mr : List R) (Hup : List Nat) (M M0 : Nat) (hN : M0 + 2 ≤ M + 2)
    (hHn : ∀ k, O.le K.zero (co K (Hup.map (cN K)) k))
    (hge : ∀ k, O.le (co K (lmap (fun j => co K Mr j) 0 (M0 + 2)) k) (co K (Hup.map (cN K)) k)) :
    O.le (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2))) (WMass.W K (Hup.map (cN K))) := by
  unfold WMass.W
  exact FlowBridge.dot_mono K O (fun k => cN K (k + 1)) (fun k => SU2CharExp.cN_nn K O h1 _) _ _ 0
    (fun k => by rw [HeadBound.co_trL_lmap K Mr (M0 + 2) (M + 2) hN k]; exact hge k) hHn

/-- **the tail mass from a weighted ratio bound**: if (k+1)·Mr_k decays at ratio ρ < 1 from k = M0 + 2 on, then
  (1 − ρ)·W(tail) ≤ (M0 + 3)·Mr_{M0+2}; so W(tail) ≤ τ whenever (M0 + 3)·Mr_{M0+2} ≤ (1 − ρ)·τ -/
theorem hτ_of_ratio (Mr : List R) (M M0 T : Nat) (hT : M + 2 = M0 + 2 + T) (ρ τ : R)
    (hn : ∀ i, O.le K.zero (K.mul (co K Mr (M0 + 2 + i)) (cN K (M0 + 2 + i + 1))))
    (hr : ∀ i, O.le (K.mul (co K Mr (M0 + 2 + (i + 1))) (cN K (M0 + 2 + (i + 1) + 1)))
      (K.mul ρ (K.mul (co K Mr (M0 + 2 + i)) (cN K (M0 + 2 + i + 1)))))
    (hρ : O.le K.zero (K.add K.one (K.neg ρ))) (hρne : K.add K.one (K.neg ρ) ≠ K.zero)
    (hτ : O.le (K.mul (co K Mr (M0 + 2 + 0)) (cN K (M0 + 2 + 0 + 1))) (K.mul (K.add K.one (K.neg ρ)) τ)) :
    O.le (WMass.W K (HeatTrunc.tlL K Mr (M0 + 2) (M + 2))) τ := by
  rw [hT]
  have w := WMass.W_tail_le K O Mr (M0 + 2) T ρ hn hr
  refine O.pos_cancel _ _ (K.add K.one (K.neg ρ)) hρ hρne ?_
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring))
    (O.le_trans _ _ _ w (O.le_trans _ _ _ hτ (TripleE.le_of_eq' K O _ _ (by lib_ring))))

/-- **one certified rung**: X ≼ Mr (both v-good, Mr with nonincreasing a), the head of mk(Mr) against M′ from one
  kernel check (hhead_of_cert), the VV-equality tail z of M′ and the junction bound give mk(X) ≼ M′ -/
theorem rung_of_cert (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (X Mr M' : List R) (M : Nat)
    (hXg : L1Full.Good K O X) (hMg : L1Full.Good K O Mr) (haM : FlowStep.ANoninc K O Mr)
    (hX : X.length = M + 2) (hM : Mr.length = M + 2) (hb : FlowCmp.Below K O X Mr)
    (hM'g : L1Full.Good K O M') (N T : Nat) (hNT : 4 * M + 5 = N + T + 1) (hM'l : M'.length = 4 * M + 5)
    (Hlo Hup K2 : List Nat) (M0 e : Nat) (s : R) (hs : O.le K.zero s)
    (hlo : Hlo.length = M0 + 2) (hupl : Hup.length = M0 + 2) (hN0 : M0 + 2 ≤ M + 2)
    (hHn : ∀ k, O.le K.zero (co K (Hlo.map (cN K)) k))
    (hle : ∀ k, O.le (co K (Hlo.map (cN K)) k) (co K (lmap (fun j => co K Mr j) 0 (M0 + 2)) k))
    (hge : ∀ k, O.le (co K (lmap (fun j => co K Mr j) 0 (M0 + 2)) k) (co K (Hup.map (cN K)) k))
    (hW : O.le (K.mul (K.mul (WMass.W K (HeatTrunc.tlL K Mr (M0 + 2) (M + 2)))
            (K.add (WMass.W K Mr) (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2)))))
          (K.add (K.mul (WMass.W K Mr) (WMass.W K Mr))
            (K.mul (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2))) (WMass.W K (HeatTrunc.trL K Mr (M0 + 2) (M + 2))))))
        (cN K e))
    (hM' : ∀ n, n ≤ N → co K M' n = K.mul s (cN K (HeatLadder.coN K2 n)))
    (hc : RungCheck.rungsLU Hlo Hup K2 M0 M0 e N = true)
    (z : Nat → R) (hz : ∀ n, N ≤ n → co K M' (n + 1) = K.mul (z n) (co K M' n))
    (hz0 : ∀ k, O.le K.zero (z (N + k))) (hzne : ∀ k, k < T → z (N + k) ≠ K.zero)
    (hzd : ∀ k, k + 1 ≤ T → O.le (z (N + (k + 1))) (z (N + k)))
    (hZ : ∀ k, k + 2 ≤ T → K.mul (z (N + (k + 1))) (K.add K.one (K.mul (z (N + k)) (z (N + (k + 1)))))
      = K.mul (z (N + k)) (K.add K.one (K.mul (z (N + (k + 1))) (z (N + (k + 2))))))
    (hzE : z (N + T) = K.zero) (hJ : O.le (RatioSeq.yr K O F (FlowStep.mk K O F Mr M) N) (z N)) :
    FlowCmp.Below K O (FlowStep.mk K O F X M) M' :=
  RungB.rung_B K O F h1 h10 X Mr M' M hXg hMg haM hX hM hb hM'g N T hNT hM'l
    (hhead_of_cert K O F h1 h10 Mr M' Hlo Hup K2 M M0 N e s hs hM hMg.1 hlo hupl hN0 hHn hle hge hW hM' hc)
    z hz hz0 hzne hzd hZ hzE hJ

end RungFull

#print axioms RungFull.hhead_of_cert
#print axioms RungFull.hW_of_bounds
#print axioms RungFull.hP_of_head
#print axioms RungFull.hτ_of_ratio
#print axioms RungFull.rung_of_cert

ym/RefScale.lean

/-
  ym/RefScale — T45, G78g route (B): positive rescaling of references. The rung certificates work with integer lists
  on a common scale S, so the reference used in the ladder is S·refL. Rescaling by s > 0 keeps v-goodness and
  "a nonincreasing", and the comparison X ≼ B is unchanged when B is rescaled. Lean 4 core only, no axioms.
-/
import ym.MkExt
import ym.FlowCmp

namespace RefScale

open YMChain LinkIntegral SU2Char HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O)

theorem mul_ne' (a b : R) (ha : O.le K.zero a) (hane : a ≠ K.zero) (hbne : b ≠ K.zero) : K.mul a b ≠ K.zero :=
  fun e => hbne (O.pos_mul_eq_zero _ _ ha hane e)

/-- an inequality survives multiplication by s² (s ≥ 0) -/
theorem le_sq (s x y : R) (hs : O.le K.zero s) (h : O.le x y) : O.le (K.mul (K.mul s s) x) (K.mul (K.mul s s) y) := by
  have w := O.mul_le_mul_right _ _ (K.mul s s) h (O.mul_nonneg _ _ hs hs)
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **v-goodness survives positive rescaling** -/
theorem good_scale (s : R) (hs : O.le K.zero s) (hsne : s ≠ K.zero) (X : List R) (hg : L1Full.Good K O X) :
    L1Full.Good K O (Projective.scaleL K s X) := by
  refine ⟨fun j => ?_, fun j hj => ?_, fun m => ?_, ?_⟩
  · rw [FlowRatio.co_scale K s X j]; exact O.mul_nonneg _ _ hs (hg.1 j)
  · rw [MkExt.len_scale K s X] at hj
    rw [FlowRatio.co_scale K s X j]; exact mul_ne' K O _ _ hs hsne (hg.2.1 j hj)
  · have v := le_sq K O s _ _ hs (hg.2.2.1 m)
    unfold HalfRatio.VV at v ⊢
    rw [FlowRatio.co_scale K s X m, FlowRatio.co_scale K s X (m + 1), FlowRatio.co_scale K s X (m + 2),
      FlowRatio.co_scale K s X (m + 3)]
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ v (TripleE.le_of_eq' K O _ _ (by lib_ring)))
  · have v := le_sq K O s _ _ hs hg.2.2.2
    rw [FlowRatio.co_scale K s X 0, FlowRatio.co_scale K s X 1, FlowRatio.co_scale K s X 2]
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ v (TripleE.le_of_eq' K O _ _ (by lib_ring)))

/-- **"a nonincreasing" survives positive rescaling** -/
theorem anon_scale (s : R) (hs : O.le K.zero s) (X : List R) (ha : FlowStep.ANoninc K O X) :
    FlowStep.ANoninc K O (Projective.scaleL K s X) := fun k => by
  rw [FlowRatio.co_scale K s X (k + 1), FlowRatio.co_scale K s X k]
  have w := O.mul_le_mul_right _ _ s (ha k) hs
  exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring)))

include F in
/-- **rescaling the reference keeps the comparison**: W ≼ B and s > 0 give W ≼ s·B (weight g/s) -/
theorem below_scale (s : R) (hs : O.le K.zero s) (hsne : s ≠ K.zero) (W B : List R) (hb : FlowCmp.Below K O W B) :
    FlowCmp.Below K O W (Projective.scaleL K s B) := by
  obtain ⟨g, hdec, hnn, heq⟩ := hb
  have ip := F.inv_pos _ hs hsne
  have i := F.mul_inv _ hsne
  refine ⟨fun m => K.mul (g m) (F.inv s), fun m => O.mul_le_mul_right _ _ _ (hdec m) ip,
    fun m => O.mul_nonneg _ _ (hnn m) ip, fun k => ?_⟩
  rw [FlowRatio.co_scale K s B k, heq k,
    show K.mul (K.mul s (co K B k)) (K.mul (g k) (F.inv s)) = K.mul (K.mul (co K B k) (g k)) (K.mul s (F.inv s)) by lib_ring,
    i]
  lib_ring

/-- **rescaling the compared list keeps the comparison**: W ≼ B and s ≥ 0 give s·W ≼ B (weight s·g) -/
theorem below_scale_left (s : R) (hs : O.le K.zero s) (W B : List R) (hb : FlowCmp.Below K O W B) :
    FlowCmp.Below K O (Projective.scaleL K s W) B := by
  obtain ⟨g, hdec, hnn, heq⟩ := hb
  refine ⟨fun m => K.mul s (g m), fun m => ?_, fun m => O.mul_nonneg _ _ hs (hnn m), fun k => ?_⟩
  · show O.le (K.mul s (g (m + 1))) (K.mul s (g m))
    have w := O.mul_le_mul_right _ _ s (hdec m) hs
    exact O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ (by lib_ring)) (O.le_trans _ _ _ w (TripleE.le_of_eq' K O _ _ (by lib_ring)))
  · show co K (Projective.scaleL K s W) k = K.mul (co K B k) (K.mul s (g k))
    rw [FlowRatio.co_scale K s W k, heq k]; lib_ring

end RefScale

#print axioms RefScale.mul_ne'
#print axioms RefScale.le_sq
#print axioms RefScale.good_scale
#print axioms RefScale.anon_scale
#print axioms RefScale.below_scale
#print axioms RefScale.below_scale_left

#print axioms: 54 theorems, no axioms.

90.145 Gap of the full flow for \(\beta \le 8\) and \(\beta \le 16\)

After 103 (resp. 116) kernel-checked rungs the full Migdal–Kadanoff flow is in RC(1/7) and has a volume-uniform gap.

\[ \beta =2t\le 8\ (\text {resp. }16):\quad |\langle U_pU_q\rangle -\langle U_p\rangle \langle U_q\rangle |\le \tfrac 12 B\,2^{-R}\ \text {at every step}\ge 107 \]

ym/GapFlow8.lean

/-
  ym/GapFlow8 — T45, G78h: the volume-uniform gap of the Migdal–Kadanoff flow for β = 2t ≤ 8 (all characters,
  every list length M + 2 ≥ 72). After the certified ladder (LadderMain: 103 rungs) the flow is in RC(1/7); along the
  flow the ratio class sharpens (GapSig.sig: σ_{4+j} ≤ h²⁴ ≤ h¹⁶). The normalised flowed weight 1 :: a_j
  (a_j = flow coefficients 1, 2, … divided by coefficient 0) then lies in the cone Bd(h⁴) (RatioStep.RC_bd), and
  GapFlow.gap_of_cone gives the connected plaquette correlation bound at every step 103 + 4 + j.
  Lean 4 core only, no axioms.
-/
import ym.LadderMain
import ym.GapSig
import ym.GapFlow

namespace GapFlow8

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist
  SU2Char HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- the normalised tail: coefficients 1, 2, … of X divided by coefficient 0 -/
def nrm (X : List R) : List R := lmap (fun k => K.mul (co K X (k + 1)) (F.inv (co K X 0))) 0 X.length

theorem co_nrm (X : List R) (k : Nat) : co K (nrm K O F X) k = K.mul (co K X (k + 1)) (F.inv (co K X 0)) := by
  unfold nrm
  cases Nat.lt_or_ge k X.length with
  | inl hl => rw [RatioStep.co_lmap K _ X.length 0 k hl, Nat.zero_add]
  | inr hg =>
    rw [RatioStep.co_lmap_ge K _ X.length 0 k hg, HeatLR.co_out K X (k + 1) (Nat.le_trans hg (Nat.le_succ k))]
    lib_ring

/-- **normalisation keeps the ratio class** -/
theorem nrm_RC (h1 : O.le K.zero K.one) (σ : R) (hσ : O.le K.zero σ) (X : List R) (hX : RatioStep.RC K O σ X)
    (hne : co K X 0 ≠ K.zero) : RatioStep.RC K O σ (K.one :: nrm K O F X) := by
  have i0 := F.inv_pos _ (hX.1 0) hne
  have e0 := F.mul_inv _ hne
  refine ⟨fun k => ?_, fun k => ?_⟩
  · cases k with
    | zero => exact h1
    | succ k => show O.le K.zero (co K (nrm K O F X) k); rw [co_nrm]; exact O.mul_nonneg _ _ (hX.1 _) i0
  · cases k with
    | zero =>
      show O.le (co K (nrm K O F X) 0) (K.mul σ K.one)
      rw [co_nrm]
      have u := O.mul_le_mul_right _ _ _ (hX.2 0) i0
      refine O.le_trans _ _ _ u (TripleE.le_of_eq' K O _ _ ?_)
      calc K.mul (K.mul σ (co K X 0)) (F.inv (co K X 0)) = K.mul σ (K.mul (co K X 0) (F.inv (co K X 0))) := by lib_ring
        _ = K.mul σ K.one := by rw [e0]
    | succ k =>
      show O.le (co K (nrm K O F X) (k + 1)) (K.mul σ (co K (nrm K O F X) k))
      rw [co_nrm, co_nrm]
      have u := O.mul_le_mul_right _ _ _ (hX.2 (k + 1)) i0
      exact O.le_trans _ _ _ u (TripleE.le_of_eq' K O _ _ (by lib_ring))

/-- the flow after the ladder: step j after the 103 certified rungs -/
def X8 (t : R) (M j : Nat) : List R :=
  FlowIter.itX K O F (FlowIter.itX K O F (WilsonCone.wl K O F t 28 (M + 2)) M 103) (FlowIter.itM M 103) j

/-- **generic**: a v-good, a-noninc list in RC(1/7) flows into the cone Bd(h⁴) from step 4 on -/
theorem cone_gen (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (X : List R) (M : Nat) (hXg : L1Full.Good K O X) (hXa : FlowStep.ANoninc K O X) (hX : X.length = M + 2)
    (hR : RatioStep.RC K O (RefScalar.rat K O F 1 7) X) :
    ∀ j, ConeBound.Bd K O (pwR K h 4) 0 (K.one :: nrm K O F (FlowIter.itX K O F X M (4 + j))) := by
  have FR := FlowRatio.flow_ratio K O F h1 h10 X M hXg hXa hX (GapSig.sig K O F)
    (GapSig.sig_nn K O F h1 h10) (fun i => O.le_refl _) hR
  intro j
  have fr := FR (4 + j)
  have b16 : O.le (GapSig.sig K O F (4 + j)) (pwR K (pwR K h 4) 4) := by
    rw [Migdal.pw_pw]
    exact O.le_trans _ _ _ (GapSig.sig_small K O F h1 h10 h hh h0 j) (GapSig.pw_anti K O h1 h hh h0 16 8)
  have rc := RatioStep.RC_mono K O _ _ b16 _ fr.2.2.2
  have hlen : 0 < (FlowIter.itX K O F X M (4 + j)).length := by rw [fr.2.2.1]; exact Nat.zero_lt_succ _
  have hne : co K (FlowIter.itX K O F X M (4 + j)) 0 ≠ K.zero := fr.1.2.1 0 hlen
  have hr4 := AreaLower.pw_nn K O h h0 h1 4
  exact RatioStep.RC_bd K O h1 _ _ (AreaLower.pw_nn K O _ hr4 h1 4) hr4 (O.le_refl _) _
    (nrm_RC K O F h1 _ (AreaLower.pw_nn K O _ hr4 h1 4) _ rc hne)

/-- **the flowed weight is in the cone Bd(h⁴) from step 103 + 4 on** (β ≤ 8) -/
theorem cone8 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero) (ht4 : O.le t (cN K 4)) (M : Nat) (hM : 70 ≤ M) :
    ∀ j, ConeBound.Bd K O (pwR K h 4) 0 (K.one :: nrm K O F (X8 K O F t M (4 + j))) := by
  have hM63 : 63 ≤ M := Nat.le_trans (by decide) hM
  have w0 := WilsonFlowBig.wilson_flow8 K O F h1 h10 t ht0 ht4 htne M hM63 0
  have Lf := LadderFin.ladder_fin K O F h1 h10 _ M w0.1 w0.2.1 w0.2.2 (LadderMain.Ref K O F M) 103
    (LadderMain.rungs K O F h1 h10 M hM) (LadderMain.start K O F h1 h10 t ht0 htne ht4 M hM) 103 (Nat.le_refl _)
  have s0 := RefScalar.rat_nn K O F h1 h10 1 7 (by decide)
  have r0 := FlowRC.below_RC K O _ s0 _ _ Lf.1 (LadderMain.finish K O F h1 h10 M hM)
  exact cone_gen K O F h1 h10 h hh h0 _ _ Lf.2.1 Lf.2.2.1 Lf.2.2.2 r0

/-- **G78h: the volume-uniform gap of the flowed SU(2) plaquette weight for β = 2t ≤ 8** (MK model, every list
  length M + 2 ≥ 72, every step 107 + j): connected plaquette correlations ≤ ½·B·2⁻ᴿ, as in GapFlow.gap_of_cone -/
theorem gap8 (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero) (ht4 : O.le t (cN K 4)) (M : Nat) (hM : 70 ≤ M)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (Ml : List Bool) (hMl : Ml.length = Pn.length) (j : Nat) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (nrm K O F (X8 K O F t M (4 + j))) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w Ml (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w Ml (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w Ml Up) (ZxH K O H w Ml Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w Ml (fun _ => K.one)) (ZxH K O H w Ml (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w Ml (fun _ => K.one)) (ZxH K O H w Ml (fun _ => K.one)))) :=
  GapFlow.gap_of_cone K O F H DH h1 h10 h hh h0 (fun j => nrm K O F (X8 K O F t M j)) 4
    (cone8 K O F h1 h10 h hh h0 t ht0 htne ht4 M hM) LL hLL hL Pn hnd hv p0 q0 hpq tr Up Uq K1 Rr htd hUp hUq
    hta hUpa hUqa htr hK1 hUpb hUqb hdist Ml hMl j

end GapFlow8

#print axioms GapFlow8.co_nrm
#print axioms GapFlow8.nrm_RC
#print axioms GapFlow8.cone_gen
#print axioms GapFlow8.cone8
#print axioms GapFlow8.gap8

ym/GapFlow16.lean

/-
  ym/GapFlow16 — T45, G78i: the volume-uniform gap of the Migdal–Kadanoff flow for β = 2t ≤ 16 (all characters,
  every list length M + 2 ≥ 257): the certified ladder LadderMain16 (planC) puts the flow in RC(1/7); GapFlow8.cone_gen
  gives the cone Bd(h⁴) from 4 steps later on; GapFlow.gap_of_cone gives the gap. Lean 4 core only, no axioms.
-/
import ym.LadderMain16
import ym.GapFlow8

namespace GapFlow16

open YMChain GKS LinkIntegral LinkFactor MaskSum Closure Factor Seeds Count Indep PosRatio Expand Approx Poly Signed
  Dup DupAlg AreaG Join Reach HaarStr HaarPoly AreaH DupH DupAlgH GapG GapH InjCount ZdLinks ZdDegree ZdDist
  SU2Char HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K) (F : OField.OF K O) {G : Type} {L : Nat} (H : Haar K O G L)

/-- the number of rungs of the β ≤ 16 ladder -/
def K16 : Nat := 116

/-- the flow after the ladder -/
def X16 (t : R) (M j : Nat) : List R :=
  FlowIter.itX K O F (FlowIter.itX K O F (WilsonCone.wl K O F t 79 (M + 2)) M K16) (FlowIter.itM M K16) j

theorem cone16 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero) (ht8 : O.le t (cN K 8)) (M : Nat) (hM : 255 ≤ M) :
    ∀ j, ConeBound.Bd K O (pwR K h 4) 0 (K.one :: GapFlow8.nrm K O F (X16 K O F t M (4 + j))) := by
  have w0 := WilsonFlowBig.wilson_flow16 K O F h1 h10 t ht0 ht8 htne M hM 0
  have Lf := LadderFin.ladder_fin K O F h1 h10 _ M w0.1 w0.2.1 w0.2.2 (LadderMain16.Ref K O F M) K16
    (LadderMain16.rungs K O F h1 h10 M hM) (LadderMain16.start K O F h1 h10 t ht0 htne ht8 M hM) K16 (Nat.le_refl _)
  have s0 := RefScalar.rat_nn K O F h1 h10 1 7 (by decide)
  have r0 := FlowRC.below_RC K O _ s0 _ _ Lf.1 (LadderMain16.finish K O F h1 h10 M hM)
  exact GapFlow8.cone_gen K O F h1 h10 h hh h0 _ _ Lf.2.1 Lf.2.2.1 Lf.2.2.2 r0

/-- **G78i: the volume-uniform gap of the flowed SU(2) plaquette weight for β = 2t ≤ 16** (MK model, every list
  length M + 2 ≥ 257, every step K16 + 4 + j): connected plaquette correlations ≤ ½·B·2⁻ᴿ (GapFlow.gap_of_cone) -/
theorem gap16 (DH : DupHaar K O H) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero)
    (h : R) (hh : K.add h h = K.one) (h0 : O.le K.zero h)
    (t : R) (ht0 : O.le K.zero t) (htne : t ≠ K.zero) (ht8 : O.le t (cN K 8)) (M : Nat) (hM : 255 ≤ M)
    (LL : List Lk) (hLL : nodupL LL) (hL : LL.length = L) (Pn : List Pq) (hnd : nodupL Pn)
    (hv : ∀ p, memL p Pn → valid 4 p) (p0 q0 : Pq)
    (hpq : disjN (vec LL p0) (vec LL q0))
    (tr : Nat → List G → R) (Up Uq : List G → R) (K1 : R) (Rr : Nat)
    (htd : ∀ i, depOn (spP L (Pn.map (vec LL)) i) (tr i)) (hUp : depOn ((vec LL p0).map nzb) Up)
    (hUq : depOn ((vec LL q0).map nzb) Uq)
    (hta : ∀ i, H.adm (tr i)) (hUpa : H.adm Up) (hUqa : H.adm Uq)
    (htr : ∀ i x, O.le (K.neg (K.add K.one K.one)) (tr i x) ∧ O.le (tr i x) (K.add K.one K.one))
    (hK1 : O.le K.zero K1) (hUpb : ∀ x, O.le (K.neg K1) (Up x) ∧ O.le (Up x) K1)
    (hUqb : ∀ x, O.le (K.neg K1) (Uq x) ∧ O.le (Uq x) K1)
    (hdist : ¬ dcl (Rr + 1) p0.1 q0.1)
    (Ml : List Bool) (hMl : Ml.length = Pn.length) (j : Nat) :
    let f0 : Nat → List G → R := fun i x => SU2Char.dot K 1 (GapFlow8.nrm K O F (X16 K O F t M (4 + j))) (ChiBound.c K (tr i x))
    let s := F.inv (K.add K.one (pwR K h 13))
    let κ := K.add K.one (pwR K h 10)
    let w : Nat → List G → R := fun i x => K.add s (K.mul s (f0 i x))
    let Dd := K.add (K.mul (ZxH K O H w Ml (fun x => K.mul (Up x) (Uq x))) (ZxH K O H w Ml (fun _ => K.one)))
      (K.neg (K.mul (ZxH K O H w Ml Up) (ZxH K O H w Ml Uq)))
    let sc := cnt ((Pn.map (vec LL)).map (fun r => touch r (zadd (vec LL p0) (vec LL q0))))
    let B := K.mul (K.mul (K.mul (K.add K1 K1) (K.add K1 K1)) (pwR K (K.mul κ κ) sc))
      (K.mul (pwR K (K.add K.one (pwR K h 6)) sc) (pwR K h Rr))
    O.le (K.neg (K.mul B (K.mul (ZxH K O H w Ml (fun _ => K.one)) (ZxH K O H w Ml (fun _ => K.one))))) (K.add Dd Dd) ∧
      O.le (K.add Dd Dd) (K.mul B (K.mul (ZxH K O H w Ml (fun _ => K.one)) (ZxH K O H w Ml (fun _ => K.one)))) :=
  GapFlow.gap_of_cone K O F H DH h1 h10 h hh h0 (fun j => GapFlow8.nrm K O F (X16 K O F t M j)) 4
    (cone16 K O F h1 h10 h hh h0 t ht0 htne ht8 M hM) LL hLL hL Pn hnd hv p0 q0 hpq tr Up Uq K1 Rr htd hUp hUq
    hta hUpa hUqa htr hK1 hUpb hUqb hdist Ml hMl j

end GapFlow16

#print axioms GapFlow16.cone16
#print axioms GapFlow16.gap16

#print axioms: 7 theorems, no axioms.

90.146 Exact heat-product formula

The Clebsch–Gordan product of two truncated heat kernels is an exact Gaussian double sum.

\[ 4q^2\,c_n(H\cdot H)=\sum _{D<L}\mu _D\sum _{i<L-D}\mathrm {cg}(i,i+D,n)\,(M^2-D^2)\,Q^{M^2+D^2},\quad M=2i+D+2 \]

ym/HeatCg.lean

/-
  ym/HeatCg — T45, G78m route (f), part 6c(iv-b): the exact heat-product formula for the truncated heat list.
    4 q² c_n(H·H) = Σ_{D<L} μ_D Σ_{i<L−D} cg(i, i+D, n) · (M² − D²) Q^{M²+D²},   M = 2i + D + 2,  μ_0 = 1, μ_{D+1} = 2,
  with cg(i, i+D, n) = 0 for D > n and cg(i, i+D, n) = lp n (2i + D) for D ≤ n (the classical SU(2) heat formula:
  inner window |d| ≤ n, outer Gaussian tail M ≥ n + 2, M ≡ n).  Lean 4 core only, no axioms.
-/
import ym.CgCorrG
import ym.HeatR
import ym.L1Full

namespace HeatCg

open YMChain LinkIntegral SU2Char HeatPair ThetaDup2 CgSum CgCorr CgCorrG HeatR CgSplit MigdalCone

variable {R : Type} (K : DetChain.CRing R)

/-! ### the Clebsch–Gordan count on a diagonal -/

theorem lp_lt : ∀ m t, t < m → lp m t = 0
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | _ + 1, 0, _ => rfl
  | m + 1, t + 1, h => lp_lt m t (Nat.lt_of_succ_lt_succ h)

theorem lp_shift : ∀ n r, lp n (n + r) = ev r
  | 0, r => by rw [Nat.zero_add]; rfl
  | n + 1, r => by
    rw [show n + 1 + r = (n + r) + 1 by lib_nat]
    exact lp_shift n r

theorem ev_add2 : ∀ i r, ev (2 * i + r) = ev r
  | 0, r => by rw [show 2 * 0 + r = r by lib_nat]
  | i + 1, r => by
    rw [show 2 * (i + 1) + r = (2 * i + r) + 2 by lib_nat]
    exact ev_add2 i r

theorem cg_near (i D n : Nat) (h : D ≤ n) : Fusion.cg i (i + D) n = lp n (i + (i + D)) := by
  have e := cg_split i (i + D) n
  rw [dist_add, lp_lt (n + 2) D (Nat.lt_of_le_of_lt h (Nat.lt_succ_of_lt (Nat.lt_succ_self n))), Nat.add_zero] at e
  exact e

theorem cg_far (i D n : Nat) (h : n < D) : Fusion.cg i (i + D) n = 0 := by
  have e := cg_split i (i + D) n
  rw [dist_add] at e
  match LibNatSub.le_ex h with
  | ⟨r, hr⟩ =>
    have hl : lp n (i + (i + D)) = lp (n + 2) D := by
      rw [hr, show i + (i + (n + 1 + r)) = n + (2 * i + (1 + r)) by lib_nat, lp_shift, ev_add2]
      cases r with
      | zero => exact (lp_lt (n + 2) (n + 1) (Nat.lt_succ_self _)).symm
      | succ r' =>
        rw [show n + 1 + (r' + 1) = (n + 2) + r' by lib_nat, lp_shift, show 1 + (r' + 1) = r' + 2 by lib_nat]
        rfl
    rw [hl] at e
    exact L1Full.cancel_add_r _ _ 0 (by rw [Nat.zero_add]; exact e)

/-! ### heat list, diagonal by diagonal -/

theorem dotG_heat (Q : R) (Φ : Nat → Nat → R) (D m : Nat) :
    K.mul (cN K 4) (K.mul (dotG K (hl K Q (D + m)) (hl K Q (D + m)) Φ D) (pwR K (K.mul Q Q) 2))
      = QBinom.sumN K (fun i => K.mul (Φ i (i + D)) (wgt K Q (i + (i + D) + 2) D)) m := by
  have e1 : dotG K (hl K Q (D + m)) (hl K Q (D + m)) Φ D
      = QBinom.sumN K (fun i => K.mul (co K (hl K Q (D + m)) i)
          (K.mul (co K (hl K Q (D + m)) (i + D)) (Φ i (i + D)))) m := by
    show dot K 0 _ _ = _
    rw [dot0_B K _ _ (D + m) (by rw [len_hl]; exact Nat.le_refl _)]
    rw [show D + m = m + D from Nat.add_comm D m]
    exact sumN_stab K _ m (fun T hT => by
      show K.mul (co K (hl K Q (m + D)) T) (K.mul (co K (hl K Q (m + D)) (T + D)) (Φ T (T + D))) = K.zero
      rw [co_ge' K _ (T + D) (by rw [len_hl]; exact Nat.add_le_add_right hT D)]
      lib_ring) D
  rw [e1, ← ThetaDup5.sumN_mulc, ← QBinom.sumN_mul]
  apply ThetaDup3.sumN_ext_lt
  intro i hi
  show K.mul (cN K 4) (K.mul (K.mul (co K (hl K Q (D + m)) i)
    (K.mul (co K (hl K Q (D + m)) (i + D)) (Φ i (i + D)))) (pwR K (K.mul Q Q) 2)) = _
  rw [co_hl K Q (D + m) i (Nat.lt_of_lt_of_le hi (Nat.le_add_left m D)),
    co_hl K Q (D + m) (i + D) (by rw [Nat.add_comm i D]; exact Nat.add_lt_add_left hi D)]
  have hp := heat_pair4 K Q i (i + D)
  rw [dist_add] at hp
  rw [← hp]
  generalize Hh K Q i = a; generalize Hh K Q (i + D) = b; generalize Φ i (i + D) = f
  generalize cN K 4 = c; generalize pwR K (K.mul Q Q) 2 = p
  lib_ring

/-- the same, for any L ≥ D -/
theorem dotG_heatL (Q : R) (Φ : Nat → Nat → R) (L D : Nat) (h : D ≤ L) :
    K.mul (cN K 4) (K.mul (dotG K (hl K Q L) (hl K Q L) Φ D) (pwR K (K.mul Q Q) 2))
      = QBinom.sumN K (fun i => K.mul (Φ i (i + D)) (wgt K Q (i + (i + D) + 2) D)) (L - D) := by
  have e := dotG_heat K Q Φ D (L - D)
  rw [LibNatSub.add_sub_of_le h] at e
  exact e

/-- multiplicity: once on the diagonal, twice off it -/
def mulS : Nat → R → R
  | 0, S => S
  | _ + 1, S => K.add S S

def cgw (Q : R) (n D i : Nat) : R := K.mul (cN K (Fusion.cg i (i + D) n)) (wgt K Q (i + (i + D) + 2) D)

/-- **exact heat-product formula** -/
theorem cgc_heat (Q : R) (n L : Nat) :
    K.mul (cN K 4) (K.mul (cgc K (hl K Q L) (hl K Q L) n) (pwR K (K.mul Q Q) 2))
      = QBinom.sumN K (fun D => mulS K D (QBinom.sumN K (fun i => cgw K Q n D i) (L - D))) L := by
  have e0 : cgc K (hl K Q L) (hl K Q L) n = corrG K (hl K Q L) (hl K Q L) (fun j k => cN K (Fusion.cg j k n)) := rfl
  rw [e0, corr_sumG K _ _ _ L (by rw [len_hl]; exact Nat.le_refl L) (by rw [len_hl]; exact Nat.le_refl L),
    ← ThetaDup5.sumN_mulc, ← QBinom.sumN_mul]
  apply ThetaDup3.sumN_ext_lt
  intro D hD
  have hle : D ≤ L := Nat.le_of_lt hD
  cases D with
  | zero =>
    show K.mul (cN K 4) (K.mul (dotG K (hl K Q L) (hl K Q L) (fun j k => cN K (Fusion.cg j k n)) 0)
      (pwR K (K.mul Q Q) 2)) = _
    rw [dotG_heatL K Q _ L 0 hle]
    rfl
  | succ D' =>
    show K.mul (cN K 4) (K.mul (K.add (dotG K (hl K Q L) (hl K Q L) (fun j k => cN K (Fusion.cg j k n)) (D' + 1))
      (dotG K (hl K Q L) (hl K Q L) (tr (fun j k => cN K (Fusion.cg j k n))) (D' + 1))) (pwR K (K.mul Q Q) 2)) = _
    have s1 := dotG_heatL K Q (fun j k => cN K (Fusion.cg j k n)) L (D' + 1) hle
    have s2 := dotG_heatL K Q (tr (fun j k => cN K (Fusion.cg j k n))) L (D' + 1) hle
    have s3 : QBinom.sumN K (fun i => K.mul (tr (fun j k => cN K (Fusion.cg j k n)) i (i + (D' + 1)))
        (wgt K Q (i + (i + (D' + 1)) + 2) (D' + 1))) (L - (D' + 1))
        = QBinom.sumN K (fun i => cgw K Q n (D' + 1) i) (L - (D' + 1)) :=
      QBinom.sumN_ext K _ _ (fun i => by
        show K.mul (cN K (Fusion.cg (i + (D' + 1)) i n)) _ = K.mul (cN K (Fusion.cg i (i + (D' + 1)) n)) _
        rw [L1Full.cg_comm]) _
    rw [s3] at s2
    show _ = K.add (QBinom.sumN K (fun i => cgw K Q n (D' + 1) i) (L - (D' + 1)))
      (QBinom.sumN K (fun i => cgw K Q n (D' + 1) i) (L - (D' + 1)))
    have s1' : K.mul (cN K 4) (K.mul (dotG K (hl K Q L) (hl K Q L) (fun j k => cN K (Fusion.cg j k n)) (D' + 1))
        (pwR K (K.mul Q Q) 2)) = QBinom.sumN K (fun i => cgw K Q n (D' + 1) i) (L - (D' + 1)) := s1
    have hS : K.add (QBinom.sumN K (fun i => cgw K Q n (D' + 1) i) (L - (D' + 1)))
        (QBinom.sumN K (fun i => cgw K Q n (D' + 1) i) (L - (D' + 1)))
        = K.add (K.mul (cN K 4) (K.mul (dotG K (hl K Q L) (hl K Q L) (fun j k => cN K (Fusion.cg j k n)) (D' + 1))
            (pwR K (K.mul Q Q) 2)))
          (K.mul (cN K 4) (K.mul (dotG K (hl K Q L) (hl K Q L) (tr (fun j k => cN K (Fusion.cg j k n))) (D' + 1))
            (pwR K (K.mul Q Q) 2))) := by rw [s1', s2]
    rw [hS]
    generalize dotG K (hl K Q L) (hl K Q L) (fun j k => cN K (Fusion.cg j k n)) (D' + 1) = u
    generalize dotG K (hl K Q L) (hl K Q L) (tr (fun j k => cN K (Fusion.cg j k n))) (D' + 1) = v
    generalize cN K 4 = c; generalize pwR K (K.mul Q Q) 2 = p
    lib_ring

end HeatCg

#print axioms HeatCg.lp_lt
#print axioms HeatCg.lp_shift
#print axioms HeatCg.ev_add2
#print axioms HeatCg.cg_near
#print axioms HeatCg.cg_far
#print axioms HeatCg.dotG_heat
#print axioms HeatCg.dotG_heatL
#print axioms HeatCg.cgc_heat

#print axioms: 8 theorems, no axioms.

90.147 Heat-product coefficients: sandwich

The heat-product coefficient is an outer Gaussian tail times a parity window, bounded on both sides.

\[ (n+2)^2 B_nW_n\le 4q^2c_n+n^2B_nW_n,\qquad 4q^2c_n\le A_nW_n+X_n \]

ym/HeatLow.lean

/-
  ym/HeatLow — T45, G78m route (f), part 6d(e): an exact lower bound for the even heat-product coefficient in any
  ordered ring (0 ≤ Q):
    (2p+2)² · B · W₀ ≤ 4 q² c_{2p} + (2p)² · B · W₀,
  from 4q²c_{2p} = A·W₀ − B·W₂ + X with X ≥ 0, A ≥ (2p+2)² B, W₂ ≤ (2p)² W₀.  Since (2p+2)² − (2p)² = 8p + 4 > 0 and
  B, W₀ > 0, this gives c_{2p} ≥ (8p+4) B W₀ / 4q² > 0.  Lean 4 core only, no axioms.
-/
import ym.HeatFac2
import ym.TripleE

namespace HeatLow

open YMChain LinkIntegral SU2Char HeatPair ThetaDup2 CgSplit CgCorr HeatR HeatCg HeatAsm HeatFac HeatFac2 MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem sum_nn (f : Nat → R) (M : Nat) (h : ∀ m, m < M → O.le K.zero (f m)) : O.le K.zero (QBinom.sumN K f M) := by
  have := TripleE.sumN_mono_lt K O (fun _ => K.zero) f M h
  rw [sumN_zero] at this
  exact this

theorem cN_sub_nn (h1 : O.le K.zero K.one) (a b : Nat) (h : a ≤ b) :
    O.le K.zero (K.add (cN K b) (K.neg (cN K a))) := by
  match le_ex _ _ h with
  | ⟨k, hk⟩ =>
    rw [hk, cN_add, show K.add (K.add (cN K a) (cN K k)) (K.neg (cN K a)) = cN K k by
      generalize cN K a = x; generalize cN K k = y; lib_ring]
    exact SU2CharExp.cN_nn K O h1 k

theorem wgt_nn (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (M D : Nat) (h : D ≤ M) :
    O.le K.zero (wgt K Q M D) :=
  O.mul_nonneg _ _ (cN_sub_nn K O h1 _ _ (Nat.mul_le_mul h h)) (AreaLower.pw_nn K O Q hQ h1 _)

theorem mu_nn (h1 : O.le K.zero K.one) : ∀ D, O.le K.zero (mu K D)
  | 0 => h1
  | _ + 1 => by
    have := O.add_le_add _ _ _ _ h1 h1
    rw [K.zero_add] at this
    exact this

theorem Xtr_nn (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (p r : Nat) : O.le K.zero (Xtr K Q p r) :=
  sum_nn K O _ (p + 1) (fun c hc => O.mul_nonneg _ _ (mu_nn K O h1 _) (sum_nn K O _ _ (fun t _ =>
    wgt_nn K O h1 Q hQ _ _ (by
      show 2 * c ≤ 2 * p + 2 + 2 * (r + 1 + t)
      exact Nat.le_trans (Nat.mul_le_mul_left 2 (Nat.le_of_lt_succ hc))
        (Nat.le_trans (Nat.le_add_right (2 * p) 2) (Nat.le_add_right _ _))))))

theorem Aout_ge (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (p T : Nat) :
    O.le (K.mul (cN K ((2 * p + 2) * (2 * p + 2))) (Bout K Q p T)) (Aout K Q p T) := by
  show O.le (K.mul _ (QBinom.sumN K _ T)) _
  rw [← QBinom.sumN_mul]
  exact TripleE.sumN_mono_lt K O _ _ T (fun t _ => O.mul_le_mul_right _ _ _
    (SU2CharExp.cN_mono K O h1 _ _ (Nat.mul_le_mul (Nat.le_add_right (2 * p + 2) (2 * t))
      (Nat.le_add_right (2 * p + 2) (2 * t)))) (AreaLower.pw_nn K O Q hQ h1 _))

theorem W2_le (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (p : Nat) :
    O.le (W2 K Q p) (K.mul (cN K (2 * p * (2 * p))) (W0 K Q p)) := by
  show O.le (QBinom.sumN K _ (p + 1)) (K.mul _ (QBinom.sumN K _ (p + 1)))
  rw [← QBinom.sumN_mul]
  exact TripleE.sumN_mono_lt K O _ _ (p + 1) (fun c hc => by
    show O.le (K.mul (mu K (2 * c)) (K.mul (cN K (2 * c * (2 * c))) (pwR K Q (2 * c * (2 * c)))))
      (K.mul (cN K (2 * p * (2 * p))) (K.mul (mu K (2 * c)) (pwR K Q (2 * c * (2 * c)))))
    have hc' : 2 * c ≤ 2 * p := Nat.mul_le_mul_left 2 (Nat.le_of_lt_succ hc)
    have := O.mul_le_mul_right _ _ _ (SU2CharExp.cN_mono K O h1 _ _ (Nat.mul_le_mul hc' hc'))
      (O.mul_nonneg _ _ (mu_nn K O h1 (2 * c)) (AreaLower.pw_nn K O Q hQ h1 (2 * c * (2 * c))))
    rw [show K.mul (cN K (2 * c * (2 * c))) (K.mul (mu K (2 * c)) (pwR K Q (2 * c * (2 * c))))
        = K.mul (mu K (2 * c)) (K.mul (cN K (2 * c * (2 * c))) (pwR K Q (2 * c * (2 * c)))) by
      generalize cN K (2 * c * (2 * c)) = a; generalize mu K (2 * c) = b; generalize pwR K Q (2 * c * (2 * c)) = d
      lib_ring] at this
    exact this)

theorem W0_nn (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (p : Nat) : O.le K.zero (W0 K Q p) :=
  sum_nn K O _ _ (fun c _ => O.mul_nonneg _ _ (mu_nn K O h1 _) (AreaLower.pw_nn K O Q hQ h1 _))

theorem Bout_nn (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (p T : Nat) : O.le K.zero (Bout K Q p T) :=
  sum_nn K O _ _ (fun t _ => AreaLower.pw_nn K O Q hQ h1 _)

/-- **lower bound for the even heat-product coefficient** -/
theorem cgc_even_low (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (p r : Nat) :
    O.le (K.mul (cN K ((2 * p + 2) * (2 * p + 2))) (K.mul (Bout K Q p (r + 1)) (W0 K Q p)))
      (K.add (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * p + 1 + r)) (hl K Q (2 * p + 1 + r)) (2 * p))
          (pwR K (K.mul Q Q) 2)))
        (K.mul (Bout K Q p (r + 1)) (K.mul (cN K (2 * p * (2 * p))) (W0 K Q p)))) := by
  rw [cgc_even_fac]
  have hA := O.mul_le_mul_right _ _ _ (Aout_ge K O h1 Q hQ p (r + 1)) (W0_nn K O h1 Q hQ p)
  have hW := O.mul_le_mul_right _ _ _ (W2_le K O h1 Q hQ p) (Bout_nn K O h1 Q hQ p (r + 1))
  have hX := Xtr_nn K O h1 Q hQ p r
  have hD := O.add_le_add _ _ _ _ hW (O.le_refl (K.neg (K.mul (W2 K Q p) (Bout K Q p (r + 1)))))
  have hS := O.add_le_add _ _ _ _ hA (O.add_le_add _ _ _ _ hX hD)
  generalize Aout K Q p (r + 1) = A at hA hS ⊢
  generalize Bout K Q p (r + 1) = B at hA hW hD hS ⊢
  generalize W0 K Q p = W at hA hW hD hS ⊢
  generalize W2 K Q p = V at hW hD hS ⊢
  generalize Xtr K Q p r = X at hX hS ⊢
  generalize cN K ((2 * p + 2) * (2 * p + 2)) = m at hA hS ⊢
  generalize cN K (2 * p * (2 * p)) = n at hW hD hS ⊢
  rw [show K.add (K.mul (K.mul m B) W) (K.add K.zero (K.add (K.mul V B) (K.neg (K.mul V B)))) = K.mul m (K.mul B W) by
    lib_ring] at hS
  rw [show K.add (K.mul A W) (K.add X (K.add (K.mul (K.mul n W) B) (K.neg (K.mul V B))))
    = K.add (K.add (K.add (K.mul A W) (K.neg (K.mul B V))) X) (K.mul B (K.mul n W)) by lib_ring] at hS
  exact hS

end HeatLow

#print axioms HeatLow.sum_nn
#print axioms HeatLow.cN_sub_nn
#print axioms HeatLow.wgt_nn
#print axioms HeatLow.mu_nn
#print axioms HeatLow.Xtr_nn
#print axioms HeatLow.Aout_ge
#print axioms HeatLow.W2_le
#print axioms HeatLow.W0_nn
#print axioms HeatLow.Bout_nn
#print axioms HeatLow.cgc_even_low

#print axioms: 10 theorems, no axioms.

90.148 Heat-product coefficients: neighbour ratio

Consecutive heat-product coefficients satisfy a cross-multiplied ratio bound, in both parity directions.

\[ (4q^2c_{n+1})\,D_n\le (n+3)^2Q^{2n+5}(W_n+1)\,(4q^2c_n)+X_nD_n,\qquad D_n+k^2W_n+E_k\le (n+2)^2W_n \]

ym/HeatRatio.lean

/-
  ym/HeatRatio — T45, G78m route (f), part 6e(k): the neighbour ratio of the heat-product coefficients at one common
  truncation L = 2p+2+r (p = k+J, 0 ≤ Q ≤ 1), in cross-multiplied form: for any D ≥ 0 with D + (2k)²W₀ + E ≤ (2p+2)²W₀,
    (4q²c_{2p+1}) · D ≤ (2p+3)² Q^{4p+5} (W₀ + 1) · (4q²c_{2p}) + X′ · D,
  i.e. c_{2p+1}/c_{2p} ≤ (2p+3)² Q^{4p+5} (W₀+1)/D + X′/(4q²c_{2p}).  Odd side HeatUpA (A with r+1 terms), even side
  HeatLowA (A with r+2 terms), A monotone in the number of terms.  Lean 4 core only, no axioms.
-/
import ym.HeatUpA

namespace HeatRatio

open YMChain LinkIntegral SU2Char HeatPair CgSplit HeatR HeatFac2 HeatOddB MigdalCone HeatW2

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem Aout_mono (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (p T : Nat) :
    O.le (Aout K Q p T) (Aout K Q p (T + 1)) := by
  show O.le (Aout K Q p T) (K.add (Aout K Q p T) (K.mul (cN K (Mt p T * Mt p T)) (pwR K Q (Mt p T * Mt p T))))
  have h := O.add_le_add _ _ _ _ (O.le_refl (Aout K Q p T))
    (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 (Mt p T * Mt p T)) (AreaLower.pw_nn K O Q hQ h1 (Mt p T * Mt p T)))
  rw [DetChain.add_zero'] at h
  exact h

/-- the ordered-ring algebra of the ratio step -/
theorem alg (m co ce D A1 A2 Z W1 X n W E : R) (hm0 : O.le K.zero m) (hmne : m ≠ K.zero) (hD0 : O.le K.zero D)
    (hZ : O.le K.zero Z) (hW1 : O.le K.zero W1) (hA1 : O.le A1 A2) (hA20 : O.le K.zero A2)
    (ho : O.le (K.mul m co) (K.add (K.mul (K.mul Z A1) W1) (K.mul m X)))
    (he : O.le (K.mul A2 (K.mul m W)) (K.add (K.mul m ce) (K.add (K.mul A2 (K.mul n W)) (K.mul A2 E))))
    (hD : O.le (K.add D (K.add (K.mul n W) E)) (K.mul m W)) :
    O.le (K.mul co D) (K.add (K.mul (K.mul Z W1) ce) (K.mul X D)) := by
  have s1 := O.mul_le_mul_right _ _ _ hD hA20
  rw [show K.mul (K.mul m W) A2 = K.mul A2 (K.mul m W) by lib_ring] at s1
  have s2 := O.add_le_add _ _ _ _ (O.le_trans _ _ _ s1 he) (O.le_refl (K.neg (K.mul A2 (K.add (K.mul n W) E))))
  rw [show K.add (K.mul (K.add D (K.add (K.mul n W) E)) A2) (K.neg (K.mul A2 (K.add (K.mul n W) E))) = K.mul A2 D by lib_ring,
    show K.add (K.add (K.mul m ce) (K.add (K.mul A2 (K.mul n W)) (K.mul A2 E))) (K.neg (K.mul A2 (K.add (K.mul n W) E)))
      = K.mul m ce by lib_ring] at s2
  have s4 := O.mul_le_mul_right _ _ _ ho hD0
  have hZWD := O.mul_nonneg _ _ (O.mul_nonneg _ _ hZ hW1) hD0
  have s5 := O.mul_le_mul_right _ _ _ hA1 hZWD
  have s6 := O.mul_le_mul_right _ _ _ s2 (O.mul_nonneg _ _ hZ hW1)
  have s7 := O.add_le_add _ _ _ _ (O.le_trans _ _ _ s5 (by
      rw [show K.mul A2 (K.mul (K.mul Z W1) D) = K.mul (K.mul A2 D) (K.mul Z W1) by lib_ring]; exact s6))
    (O.le_refl (K.mul (K.mul m X) D))
  rw [show K.add (K.mul A1 (K.mul (K.mul Z W1) D)) (K.mul (K.mul m X) D)
      = K.mul (K.add (K.mul (K.mul Z A1) W1) (K.mul m X)) D by lib_ring,
    show K.add (K.mul (K.mul m ce) (K.mul Z W1)) (K.mul (K.mul m X) D)
      = K.mul (K.add (K.mul (K.mul Z W1) ce) (K.mul X D)) m by lib_ring] at s7
  have s8 := O.le_trans _ _ _ s4 s7
  rw [show K.mul (K.mul m co) D = K.mul (K.mul co D) m by lib_ring] at s8
  exact O.pos_cancel _ _ _ hm0 hmne s8

/-- **neighbour ratio, cross-multiplied** -/
theorem cgc_ratio (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (k J r : Nat) (D : R) (hD0 : O.le K.zero D)
    (hD : O.le (K.add D (K.add (K.mul (cN K (2 * k * (2 * k))) (W0 K Q (k + J)))
        (K.mul (two K) (K.mul (pwR K Q (2 * (k + 1) * (2 * (k + 1)))) (Tl K Q k J)))))
      (K.mul (cN K ((2 * (k + J) + 2) * (2 * (k + J) + 2))) (W0 K Q (k + J)))) :
    O.le (K.mul (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + r)) (hl K Q (2 * (k + J + 1) + r)) (2 * (k + J) + 1))
          (pwR K (K.mul Q Q) 2))) D)
      (K.add (K.mul (K.mul (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (pwR K Q (4 * (k + J) + 5)))
            (K.add (W0 K Q (k + J)) K.one))
          (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + r)) (hl K Q (2 * (k + J + 1) + r)) (2 * (k + J)))
            (pwR K (K.mul Q Q) 2))))
        (K.mul (Xo K Q (k + J) r) D)) := by
  have ho := HeatUpA.cgc_odd_upA K O h1 Q hQ hQ1 (k + J) r
  have he := HeatLowA.cgc_even_lowA K O h1 Q hQ hQ1 k J (r + 1)
  rw [show 2 * (k + J) + 1 + (r + 1) = 2 * (k + J + 1) + r by lib_nat] at he
  rw [show K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (K.mul (pwR K Q (4 * (k + J) + 5)) (Aout K Q (k + J) (r + 1)))
    = K.mul (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (pwR K Q (4 * (k + J) + 5))) (Aout K Q (k + J) (r + 1))
    by lib_ring] at ho
  have hmp := SU2CharExp.pos_of_ge_one K O h1 h10 ((2 * (k + J) + 2) * (2 * (k + J) + 2))
    (Nat.mul_le_mul (Nat.succ_le_succ (Nat.zero_le _)) (Nat.succ_le_succ (Nat.zero_le _)))
  have hZ := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 ((2 * (k + J) + 3) * (2 * (k + J) + 3)))
    (AreaLower.pw_nn K O Q hQ h1 (4 * (k + J) + 5))
  have hW1 := O.add_le_add _ _ _ _ (HeatLow.W0_nn K O h1 Q hQ (k + J)) h1
  rw [DetChain.add_zero'] at hW1
  have hA20 := O.le_trans _ _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (HeatLow.Bout_nn K O h1 Q hQ (k + J) (r + 1 + 1)))
    (HeatLow.Aout_ge K O h1 Q hQ (k + J) (r + 1 + 1))
  exact alg K O _ _ _ _ _ _ _ _ _ _ _ _ hmp.1 hmp.2 hD0 hZ hW1 (Aout_mono K O h1 Q hQ (k + J) (r + 1)) hA20 ho he hD

end HeatRatio

#print axioms HeatRatio.Aout_mono
#print axioms HeatRatio.alg
#print axioms HeatRatio.cgc_ratio

ym/HeatRatioK.lean

/-
  ym/HeatRatioK — T45, G78m route (f), part 6e(l): an explicit D for HeatRatio. If 2·4·Q^{8(k+1)} ≤ 1 then
    Tl(k, J) ≤ 2 (2k+2)²   (HeatW2T.Tl_geo),
  and D = (2p+2)²W₀ − (2k)²W₀ − 2 Q^{(2k+2)²} · 2(2k+2)² satisfies D + (2k)²W₀ + E ≤ (2p+2)²W₀, so (p = k+J, L = 2p+2+r)
    (4q²c_{2p+1}) · D ≤ (2p+3)² Q^{4p+5} (W₀ + 1) · (4q²c_{2p}) + X′ · D   whenever D ≥ 0.
  Lean 4 core only, no axioms.
-/
import ym.HeatRatio

namespace HeatRatioK

open YMChain LinkIntegral SU2Char HeatPair CgSplit HeatR HeatFac2 HeatOddB MigdalCone HeatW2

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem Tl_nn (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (k J : Nat) : O.le K.zero (Tl K Q k J) :=
  TripleE.sumN_nn K O _ (fun _ => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O Q hQ0 h1 _)) J

/-- **the tail sum is at most twice its first term once the ratio is ≤ 1/2** -/
theorem Tl_two (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (k J : Nat)
    (hρ : O.le (K.mul (two K) (K.mul (cN K 4) (pwR K Q (8 * (k + 1))))) K.one) :
    O.le (Tl K Q k J) (K.mul (two K) (cN K (2 * (k + 1) * (2 * (k + 1))))) := by
  have g := HeatW2T.Tl_geo K O h1 Q hQ0 k J
  have hT := Tl_nn K O h1 Q hQ0 k J
  have g2 := O.mul_le_mul_right _ _ _ g (two_nn K O h1)
  have hr := O.mul_le_mul_right _ _ _ hρ hT
  have tw : two K = K.add K.one K.one := rfl
  rw [tw] at g2 hr ⊢
  generalize Tl K Q k J = T at g g2 hr hT ⊢
  generalize K.mul (cN K 4) (pwR K Q (8 * (k + 1))) = ρ at g g2 hr
  generalize cN K (2 * (k + 1) * (2 * (k + 1))) = c at g g2 ⊢
  have s := O.add_le_add _ _ _ _ (O.add_le_add _ _ _ _ g2 hr)
    (O.le_refl (K.add (K.neg (K.mul (K.mul (K.add K.one K.one) ρ) T)) (K.neg T)))
  rw [show K.add (K.add (K.mul T (K.add K.one K.one)) (K.mul (K.mul (K.add K.one K.one) ρ) T))
        (K.add (K.neg (K.mul (K.mul (K.add K.one K.one) ρ) T)) (K.neg T)) = T by lib_ring,
    show K.add (K.add (K.mul (K.add c (K.mul ρ T)) (K.add K.one K.one)) (K.mul K.one T))
        (K.add (K.neg (K.mul (K.mul (K.add K.one K.one) ρ) T)) (K.neg T)) = K.mul (K.add K.one K.one) c by lib_ring] at s
  exact s

/-- the explicit D -/
def Dk (Q : R) (k J : Nat) : R :=
  K.add (K.mul (cN K ((2 * (k + J) + 2) * (2 * (k + J) + 2))) (W0 K Q (k + J)))
    (K.neg (K.add (K.mul (cN K (2 * k * (2 * k))) (W0 K Q (k + J)))
      (K.mul (two K) (K.mul (pwR K Q (2 * (k + 1) * (2 * (k + 1)))) (K.mul (two K) (cN K (2 * (k + 1) * (2 * (k + 1)))))))))

theorem Dk_ok (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (k J : Nat)
    (hρ : O.le (K.mul (two K) (K.mul (cN K 4) (pwR K Q (8 * (k + 1))))) K.one) :
    O.le (K.add (Dk K Q k J) (K.add (K.mul (cN K (2 * k * (2 * k))) (W0 K Q (k + J)))
        (K.mul (two K) (K.mul (pwR K Q (2 * (k + 1) * (2 * (k + 1)))) (Tl K Q k J)))))
      (K.mul (cN K ((2 * (k + J) + 2) * (2 * (k + J) + 2))) (W0 K Q (k + J))) := by
  have ht := Tl_two K O h1 Q hQ0 k J hρ
  have hq := O.mul_nonneg _ _ (two_nn K O h1) (AreaLower.pw_nn K O Q hQ0 h1 (2 * (k + 1) * (2 * (k + 1))))
  have s := O.mul_le_mul_right _ _ _ ht hq
  unfold Dk
  generalize Tl K Q k J = T at s ⊢
  generalize two K = t at s hq ⊢
  generalize pwR K Q (2 * (k + 1) * (2 * (k + 1))) = q at s hq ⊢
  generalize cN K (2 * (k + 1) * (2 * (k + 1))) = c at s ⊢
  generalize K.mul (cN K ((2 * (k + J) + 2) * (2 * (k + J) + 2))) (W0 K Q (k + J)) = MW
  generalize K.mul (cN K (2 * k * (2 * k))) (W0 K Q (k + J)) = NW
  have s2 := O.add_le_add _ _ _ _ s (O.le_refl (K.add MW (K.neg (K.mul t (K.mul q (K.mul t c))))))
  rw [show K.add (K.mul T (K.mul t q)) (K.add MW (K.neg (K.mul t (K.mul q (K.mul t c)))))
        = K.add (K.add MW (K.neg (K.add NW (K.mul t (K.mul q (K.mul t c)))))) (K.add NW (K.mul t (K.mul q T))) by lib_ring,
    show K.add (K.mul (K.mul t c) (K.mul t q)) (K.add MW (K.neg (K.mul t (K.mul q (K.mul t c))))) = MW by lib_ring] at s2
  exact s2

/-- **neighbour ratio with the explicit D** -/
theorem cgc_ratioK (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (k J r : Nat) (hρ : O.le (K.mul (two K) (K.mul (cN K 4) (pwR K Q (8 * (k + 1))))) K.one)
    (hD0 : O.le K.zero (Dk K Q k J)) :
    O.le (K.mul (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + r)) (hl K Q (2 * (k + J + 1) + r)) (2 * (k + J) + 1))
          (pwR K (K.mul Q Q) 2))) (Dk K Q k J))
      (K.add (K.mul (K.mul (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (pwR K Q (4 * (k + J) + 5)))
            (K.add (W0 K Q (k + J)) K.one))
          (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + r)) (hl K Q (2 * (k + J + 1) + r)) (2 * (k + J)))
            (pwR K (K.mul Q Q) 2))))
        (K.mul (Xo K Q (k + J) r) (Dk K Q k J))) :=
  HeatRatio.cgc_ratio K O h1 h10 Q hQ hQ1 k J r (Dk K Q k J) hD0 (Dk_ok K O h1 Q hQ k J hρ)

end HeatRatioK

#print axioms HeatRatioK.Tl_nn
#print axioms HeatRatioK.Tl_two
#print axioms HeatRatioK.Dk_ok
#print axioms HeatRatioK.cgc_ratioK

ym/HeatRatioO.lean

/-
  ym/HeatRatioO — T45, G78m route (f), part 6e(o): the odd → even neighbour ratio (p = k+J, 0 ≤ Q ≤ 1).
    odd lower through A′:  A′·m′·V₀ ≤ m′·4q²c_{2p+1} + A′·(2k+1)²·V₀ + A′·E′,   m′ = (2p+3)², E′ = 2Q^{(2k+3)²}To
    tails:                 (2p+3)²·A_{p+1}(T) ≤ (2p+4)²·Q^{4p+7}·A′_p(T)
    ratio (one truncation L = 2p+3+r): for D ≥ 0 with D + (2k+1)²V₀ + E′ ≤ m′V₀,
      (4q²c_{2p+2})·D ≤ (2p+4)² Q^{4p+7} (V₀+1)·(4q²c_{2p+1}) + X·D.
  Lean 4 core only, no axioms.
-/
import ym.HeatV2
import ym.HeatW0V0

namespace HeatRatioO

open YMChain LinkIntegral SU2Char HeatPair CgSplit HeatR HeatFac2 HeatOddB MigdalCone HeatW2 HeatV2

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- **odd coefficient, lower bound through A′** -/
theorem cgc_odd_lowA (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (hQ1 : O.le Q K.one) (k J r : Nat) :
    O.le (K.mul (Ao K Q (k + J) (r + 1)) (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (V0 K Q (k + J))))
      (K.add (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3)))
          (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + r)) (hl K Q (2 * (k + J + 1) + r)) (2 * (k + J) + 1))
            (pwR K (K.mul Q Q) 2))))
        (K.add (K.mul (Ao K Q (k + J) (r + 1)) (K.mul (cN K ((2 * k + 1) * (2 * k + 1))) (V0 K Q (k + J))))
          (K.mul (Ao K Q (k + J) (r + 1))
            (K.mul (two K) (K.mul (pwR K Q ((2 * k + 3) * (2 * k + 3))) (To K Q k J)))))) := by
  rw [cgc_odd_fac]
  have hB := Ao_ge K O h1 Q hQ (k + J) (r + 1)
  have hA0 := O.le_trans _ _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (Bo_nn K O h1 Q hQ (k + J) (r + 1))) hB
  have h1' := O.mul_le_mul_right _ _ _ hB (V2_nn K O h1 Q hQ (k + J))
  have h2' := O.mul_le_mul_right _ _ _ (V2_split K O h1 Q hQ hQ1 k J) hA0
  have h3' := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (Xo_nn K O h1 Q hQ (k + J) r)
  have S := O.add_le_add _ _ _ _ h1' (O.add_le_add _ _ _ _ h2' h3')
  generalize Ao K Q (k + J) (r + 1) = A at hB hA0 h1' h2' S ⊢
  generalize Bo K Q (k + J) (r + 1) = B at hB h1' S ⊢
  generalize V0 K Q (k + J) = W at h2' S ⊢
  generalize V2 K Q (k + J) = V at h1' h2' S ⊢
  generalize Xo K Q (k + J) r = X at h3' S ⊢
  generalize K.mul (two K) (K.mul (pwR K Q ((2 * k + 3) * (2 * k + 3))) (To K Q k J)) = E at h2' S ⊢
  generalize cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3)) = m at hB h1' h3' S ⊢
  generalize cN K ((2 * k + 1) * (2 * k + 1)) = n at h2' S ⊢
  have T := O.add_le_add _ _ _ _ S (O.le_refl
    (K.add (K.mul A (K.mul m W)) (K.neg (K.add (K.mul (K.mul m B) V) (K.add (K.mul V A) K.zero)))))
  rw [show K.add (K.add (K.mul (K.mul m B) V) (K.add (K.mul V A) K.zero))
        (K.add (K.mul A (K.mul m W)) (K.neg (K.add (K.mul (K.mul m B) V) (K.add (K.mul V A) K.zero))))
      = K.mul A (K.mul m W) by lib_ring,
    show K.add (K.add (K.mul A V) (K.add (K.mul (K.add (K.mul n W) E) A) (K.mul m X)))
        (K.add (K.mul A (K.mul m W)) (K.neg (K.add (K.mul (K.mul m B) V) (K.add (K.mul V A) K.zero))))
      = K.add (K.mul m (K.add (K.add (K.mul A W) (K.neg (K.mul B V))) X))
          (K.add (K.mul A (K.mul n W)) (K.mul A E)) by lib_ring] at T
  exact T

theorem nat_key (p t : Nat) :
    (2 * p + 3) * (2 * p + 3) * ((2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t))
      ≤ (2 * p + 4) * (2 * p + 4) * ((2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t)) := by
  have h : (2 * p + 3) * (2 * (p + 1) + 2 + 2 * t) ≤ (2 * p + 4) * (2 * p + 3 + 2 * t) := by
    rw [show (2 * p + 4) * (2 * p + 3 + 2 * t) = (2 * p + 3) * (2 * (p + 1) + 2 + 2 * t) + 2 * t by lib_nat]
    exact Nat.le_add_right _ _
  have h2 := Nat.mul_le_mul h h
  rw [show (2 * p + 3) * (2 * (p + 1) + 2 + 2 * t) * ((2 * p + 3) * (2 * (p + 1) + 2 + 2 * t))
      = (2 * p + 3) * (2 * p + 3) * ((2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t)) by lib_nat,
    show (2 * p + 4) * (2 * p + 3 + 2 * t) * ((2 * p + 4) * (2 * p + 3 + 2 * t))
      = (2 * p + 4) * (2 * p + 4) * ((2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t)) by lib_nat] at h2
  exact h2

theorem exp_key (p t : Nat) :
    4 * p + 7 + (2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t) ≤ (2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t) := by
  rw [show (2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t)
    = 4 * p + 7 + (2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t) + 4 * t by lib_nat]
  exact Nat.le_add_right _ _

/-- **next even tail against the odd tail** -/
theorem Aout_le (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (p T : Nat) :
    O.le (K.mul (cN K ((2 * p + 3) * (2 * p + 3))) (Aout K Q (p + 1) T))
      (K.mul (cN K ((2 * p + 4) * (2 * p + 4))) (K.mul (pwR K Q (4 * p + 7)) (Ao K Q p T))) := by
  show O.le (K.mul _ (QBinom.sumN K _ T)) (K.mul _ (K.mul _ (QBinom.sumN K _ T)))
  rw [← QBinom.sumN_mul, ← QBinom.sumN_mul, ← QBinom.sumN_mul]
  exact TripleE.sumN_mono_lt K O _ _ T (fun t _ => by
    show O.le (K.mul (cN K ((2 * p + 3) * (2 * p + 3))) (K.mul (cN K ((2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t)))
        (pwR K Q ((2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t)))))
      (K.mul (cN K ((2 * p + 4) * (2 * p + 4))) (K.mul (pwR K Q (4 * p + 7))
        (K.mul (cN K ((2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t))) (pwR K Q ((2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t))))))
    have hc := SU2CharExp.cN_mono K O h1 _ _ (nat_key p t)
    have hq := ConeBound.pw_anti K O h1 Q hQ0 hQ1 _ _ (exp_key p t)
    rw [GKS.pwR_add K Q (4 * p + 7)] at hq
    rw [SU2CharExp.cN_mul K ((2 * p + 3) * (2 * p + 3)) ((2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t)),
      SU2CharExp.cN_mul K ((2 * p + 4) * (2 * p + 4)) ((2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t))] at hc
    have hx0 := AreaLower.pw_nn K O Q hQ0 h1 ((2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t))
    have hc0 := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 ((2 * p + 4) * (2 * p + 4)))
      (SU2CharExp.cN_nn K O h1 ((2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t)))
    have s1 := O.mul_le_mul_right _ _ _ hc hx0
    have s2 := O.mul_le_mul_right _ _ _ hq hc0
    generalize cN K ((2 * p + 3) * (2 * p + 3)) = a at s1 s2 ⊢
    generalize cN K ((2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t)) = b at s1 s2 ⊢
    generalize cN K ((2 * p + 4) * (2 * p + 4)) = c at s1 s2 ⊢
    generalize cN K ((2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t)) = d at s1 s2 ⊢
    generalize pwR K Q ((2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t)) = x at s1 s2 ⊢
    generalize pwR K Q (4 * p + 7) = y at s1 s2 ⊢
    generalize pwR K Q ((2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t)) = z at s1 s2 ⊢
    have e1 : K.mul a (K.mul b x) = K.mul (K.mul a b) x := by lib_ring
    have e2 : K.mul c (K.mul y (K.mul d z)) = K.mul (K.mul y z) (K.mul c d) := by lib_ring
    rw [e1, e2]
    exact O.le_trans _ _ _ s1 (by
      have e3 : K.mul (K.mul c d) x = K.mul x (K.mul c d) := K.mul_comm _ _
      rw [e3]; exact s2))

theorem Ao_mono (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (p T : Nat) :
    O.le (Ao K Q p T) (Ao K Q p (T + 1)) := by
  show O.le (Ao K Q p T) (K.add (Ao K Q p T) (K.mul (cN K (Mo p T * Mo p T)) (pwR K Q (Mo p T * Mo p T))))
  have h := O.add_le_add _ _ _ _ (O.le_refl (Ao K Q p T))
    (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 (Mo p T * Mo p T)) (AreaLower.pw_nn K O Q hQ h1 (Mo p T * Mo p T)))
  rw [DetChain.add_zero'] at h
  exact h

/-- **odd → even neighbour ratio, cross-multiplied** -/
theorem cgc_ratio_o (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (k J r : Nat) (D : R) (hD0 : O.le K.zero D)
    (hD : O.le (K.add D (K.add (K.mul (cN K ((2 * k + 1) * (2 * k + 1))) (V0 K Q (k + J)))
        (K.mul (two K) (K.mul (pwR K Q ((2 * k + 3) * (2 * k + 3))) (To K Q k J)))))
      (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (V0 K Q (k + J)))) :
    O.le (K.mul (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + 1 + r)) (hl K Q (2 * (k + J + 1) + 1 + r))
          (2 * (k + J + 1))) (pwR K (K.mul Q Q) 2))) D)
      (K.add (K.mul (K.mul (K.mul (cN K ((2 * (k + J) + 4) * (2 * (k + J) + 4))) (pwR K Q (4 * (k + J) + 7)))
            (K.add (V0 K Q (k + J)) K.one))
          (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + 1 + r)) (hl K Q (2 * (k + J + 1) + 1 + r))
            (2 * (k + J) + 1)) (pwR K (K.mul Q Q) 2))))
        (K.mul (Xtr K Q (k + J + 1) r) D)) := by
  have hu := HeatUp.cgc_even_up K O h1 Q hQ (k + J + 1) r
  have hl' := cgc_odd_lowA K O h1 Q hQ hQ1 k J (r + 1)
  rw [show 2 * (k + J + 1) + (r + 1) = 2 * (k + J + 1) + 1 + r by lib_nat] at hl'
  have hA1 := O.le_trans _ _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (HeatLow.Bout_nn K O h1 Q hQ (k + J + 1) (r + 1)))
    (HeatLow.Aout_ge K O h1 Q hQ (k + J + 1) (r + 1))
  have hW := O.mul_le_mul_right _ _ _ (HeatW0V0.W0_succ_le K O h1 Q hQ hQ1 (k + J)) hA1
  rw [K.mul_comm (W0 K Q (k + J + 1)), K.mul_comm (K.add (V0 K Q (k + J)) K.one)] at hW
  have hmp := SU2CharExp.pos_of_ge_one K O h1 h10 ((2 * (k + J) + 3) * (2 * (k + J) + 3))
    (Nat.mul_le_mul (Nat.succ_le_succ (Nat.zero_le _)) (Nat.succ_le_succ (Nat.zero_le _)))
  have hW1 := O.add_le_add _ _ _ _ (HeatOddB.V0_nn K O h1 Q hQ (k + J)) h1
  rw [DetChain.add_zero'] at hW1
  have ht := O.mul_le_mul_right _ _ _ (Aout_le K O h1 Q hQ hQ1 (k + J) (r + 1)) hW1
  have hZ := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 ((2 * (k + J) + 4) * (2 * (k + J) + 4)))
    (AreaLower.pw_nn K O Q hQ h1 (4 * (k + J) + 7))
  have hA20 := O.le_trans _ _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (Bo_nn K O h1 Q hQ (k + J) (r + 1 + 1)))
    (Ao_ge K O h1 Q hQ (k + J) (r + 1 + 1))
  -- odd-side input of HeatRatio.alg: m·c_e ≤ (Z·A′)·(V₀+1) + m·X
  have ho : O.le (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3)))
        (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + 1 + r)) (hl K Q (2 * (k + J + 1) + 1 + r))
          (2 * (k + J + 1))) (pwR K (K.mul Q Q) 2))))
      (K.add (K.mul (K.mul (K.mul (cN K ((2 * (k + J) + 4) * (2 * (k + J) + 4))) (pwR K Q (4 * (k + J) + 7)))
          (Ao K Q (k + J) (r + 1))) (K.add (V0 K Q (k + J)) K.one))
        (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (Xtr K Q (k + J + 1) r))) := by
    have t1 := O.le_trans _ _ _ hu (O.add_le_add _ _ _ _ hW (O.le_refl (Xtr K Q (k + J + 1) r)))
    have hm := O.mul_le_mul_right _ _ _ t1 hmp.1
    generalize K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + 1 + r)) (hl K Q (2 * (k + J + 1) + 1 + r))
      (2 * (k + J + 1))) (pwR K (K.mul Q Q) 2)) = c at hm ⊢
    generalize Aout K Q (k + J + 1) (r + 1) = A1 at hm ht ⊢
    generalize Ao K Q (k + J) (r + 1) = A' at ht ⊢
    generalize K.add (V0 K Q (k + J)) K.one = W1 at hm ht ⊢
    generalize Xtr K Q (k + J + 1) r = X at hm ⊢
    generalize cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3)) = m at hm ht ⊢
    generalize cN K ((2 * (k + J) + 4) * (2 * (k + J) + 4)) = z at ht ⊢
    generalize pwR K Q (4 * (k + J) + 7) = y at ht ⊢
    rw [show K.mul (K.add (K.mul A1 W1) X) m = K.add (K.mul (K.mul m A1) W1) (K.mul m X) by lib_ring,
      show K.mul c m = K.mul m c by lib_ring] at hm
    rw [show K.mul (K.mul z (K.mul y A')) W1 = K.mul (K.mul (K.mul z y) A') W1 by lib_ring] at ht
    exact O.le_trans _ _ _ hm (O.add_le_add _ _ _ _ ht (O.le_refl _))
  exact HeatRatio.alg K O _ _ _ _ _ _ _ _ _ _ _ _ hmp.1 hmp.2 hD0 hZ hW1 (Ao_mono K O h1 Q hQ (k + J) (r + 1)) hA20 ho hl' hD

end HeatRatioO

#print axioms HeatRatioO.cgc_odd_lowA
#print axioms HeatRatioO.nat_key
#print axioms HeatRatioO.exp_key
#print axioms HeatRatioO.Aout_le
#print axioms HeatRatioO.Ao_mono
#print axioms HeatRatioO.cgc_ratio_o

ym/HeatRatioX.lean

/-
  ym/HeatRatioX — T45, G78m route (f), part 6e(s): the even → odd neighbour ratio with the truncation extra absorbed
  (p = k+J, L = 2p+2+r, p ≤ r+2, 0 ≤ Q ≤ 1, ρ = 4Q^{8p+12}): for D ≥ 0 with D + (2k)²W₀ + E ≤ (2p+2)²W₀,
    (4q²c_{2p+1}) · D ≤ ((2p+3)² Q^{4p+5} + (2p+2)² · 4ρ^{r+1}) (W₀ + 1) · (4q²c_{2p}).
  X′ ≤ Axo·V₀ (HeatXo), Axo ≤ 4·Ax (odd moment ≤ 4 × even moment below it), Ax ≤ ρ^{r+1} A_p(p) (HeatAx),
  A_p(p) ≤ A_p(r+2), V₀ ≤ W₀ + 1.  Lean 4 core only, no axioms.
-/
import ym.HeatAx

namespace HeatRatioX

open YMChain LinkIntegral SU2Char HeatPair CgSplit HeatR HeatFac2 HeatOddB MigdalCone HeatW2 HeatXtr HeatXo HeatAx

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem nat_sq (p s : Nat) : (2 * p + 3 + 2 * s) * (2 * p + 3 + 2 * s) ≤ 4 * (Mt p s * Mt p s) := by
  show (2 * p + 3 + 2 * s) * (2 * p + 3 + 2 * s) ≤ 4 * ((2 * p + 2 + 2 * s) * (2 * p + 2 + 2 * s))
  have h : 2 * p + 3 + 2 * s ≤ 2 * (2 * p + 2 + 2 * s) := by
    rw [show 2 * (2 * p + 2 + 2 * s) = (2 * p + 3 + 2 * s) + (2 * p + 1 + 2 * s) by lib_nat]
    exact Nat.le_add_right _ _
  have h2 := Nat.mul_le_mul h h
  rw [show 2 * (2 * p + 2 + 2 * s) * (2 * (2 * p + 2 + 2 * s)) = 4 * ((2 * p + 2 + 2 * s) * (2 * p + 2 + 2 * s)) by lib_nat] at h2
  exact h2

theorem nat_exp (p s : Nat) : Mt p s * Mt p s ≤ (2 * p + 3 + 2 * s) * (2 * p + 3 + 2 * s) := by
  show (2 * p + 2 + 2 * s) * (2 * p + 2 + 2 * s) ≤ (2 * p + 3 + 2 * s) * (2 * p + 3 + 2 * s)
  have h : 2 * p + 2 + 2 * s ≤ 2 * p + 3 + 2 * s := by
    rw [show 2 * p + 3 + 2 * s = (2 * p + 2 + 2 * s) + 1 by lib_nat]; exact Nat.le_add_right _ _
  exact Nat.mul_le_mul h h

/-- odd far moments ≤ 4 × even far moments -/
theorem Axo_le (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (p r : Nat) :
    O.le (Axo K Q p r) (K.mul (cN K 4) (Ax K Q p r)) := by
  show O.le (QBinom.sumN K _ p) (K.mul _ (QBinom.sumN K _ p))
  rw [← QBinom.sumN_mul]
  exact TripleE.sumN_mono_lt K O _ _ p (fun t _ => by
    show O.le (K.mul (cN K ((2 * p + 3 + 2 * (r + 1 + t)) * (2 * p + 3 + 2 * (r + 1 + t))))
        (pwR K Q ((2 * p + 3 + 2 * (r + 1 + t)) * (2 * p + 3 + 2 * (r + 1 + t)))))
      (K.mul (cN K 4) (K.mul (cN K (Mt p (r + 1 + t) * Mt p (r + 1 + t))) (pwR K Q (Mt p (r + 1 + t) * Mt p (r + 1 + t)))))
    have hc := SU2CharExp.cN_mono K O h1 _ _ (nat_sq p (r + 1 + t))
    rw [SU2CharExp.cN_mul K 4] at hc
    have hq := ConeBound.pw_anti K O h1 Q hQ0 hQ1 _ _ (nat_exp p (r + 1 + t))
    have s1 := O.mul_le_mul_right _ _ _ hc (AreaLower.pw_nn K O Q hQ0 h1 ((2 * p + 3 + 2 * (r + 1 + t)) * (2 * p + 3 + 2 * (r + 1 + t))))
    have s2 := O.mul_le_mul_right _ _ _ hq (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 4) (SU2CharExp.cN_nn K O h1 (Mt p (r + 1 + t) * Mt p (r + 1 + t))))
    generalize cN K ((2 * p + 3 + 2 * (r + 1 + t)) * (2 * p + 3 + 2 * (r + 1 + t))) = b at s1 ⊢
    generalize pwR K Q ((2 * p + 3 + 2 * (r + 1 + t)) * (2 * p + 3 + 2 * (r + 1 + t))) = x at s1 s2 ⊢
    generalize cN K 4 = f at s1 s2 ⊢
    generalize cN K (Mt p (r + 1 + t) * Mt p (r + 1 + t)) = c at s1 s2 ⊢
    generalize pwR K Q (Mt p (r + 1 + t) * Mt p (r + 1 + t)) = z at s2 ⊢
    rw [show K.mul (K.mul f c) x = K.mul x (K.mul f c) by lib_ring] at s1
    rw [show K.mul f (K.mul c z) = K.mul z (K.mul f c) by lib_ring]
    exact O.le_trans _ _ _ s1 s2)

/-- the algebra with the extra absorbed -/
theorem alg2 (m co ce D A1 A2 Z W1 X n W E κ : R) (hm0 : O.le K.zero m) (hmne : m ≠ K.zero) (hD0 : O.le K.zero D)
    (hZ : O.le K.zero Z) (hW1 : O.le K.zero W1) (hA1 : O.le A1 A2) (hA20 : O.le K.zero A2) (hκ : O.le K.zero κ)
    (hX : O.le X (K.mul (K.mul κ A2) W1))
    (ho : O.le (K.mul m co) (K.add (K.mul (K.mul Z A1) W1) (K.mul m X)))
    (he : O.le (K.mul A2 (K.mul m W)) (K.add (K.mul m ce) (K.add (K.mul A2 (K.mul n W)) (K.mul A2 E))))
    (hD : O.le (K.add D (K.add (K.mul n W) E)) (K.mul m W)) :
    O.le (K.mul co D) (K.mul (K.mul (K.add Z (K.mul m κ)) W1) ce) := by
  have s1 := O.mul_le_mul_right _ _ _ hD hA20
  rw [show K.mul (K.mul m W) A2 = K.mul A2 (K.mul m W) by lib_ring] at s1
  have s2 := O.add_le_add _ _ _ _ (O.le_trans _ _ _ s1 he) (O.le_refl (K.neg (K.mul A2 (K.add (K.mul n W) E))))
  rw [show K.add (K.mul (K.add D (K.add (K.mul n W) E)) A2) (K.neg (K.mul A2 (K.add (K.mul n W) E))) = K.mul A2 D by lib_ring,
    show K.add (K.add (K.mul m ce) (K.add (K.mul A2 (K.mul n W)) (K.mul A2 E))) (K.neg (K.mul A2 (K.add (K.mul n W) E)))
      = K.mul m ce by lib_ring] at s2
  -- m·co·D ≤ (Z A1 W1 + m X) D ≤ (Z A2 W1 + m κ A2 W1) D = (Z + mκ) W1 (A2 D) ≤ (Z + mκ) W1 m ce
  have s4 := O.mul_le_mul_right _ _ _ ho hD0
  have t1 := O.mul_le_mul_right _ _ _ hA1 (O.mul_nonneg _ _ (O.mul_nonneg _ _ hZ hW1) hD0)
  have t2 := O.mul_le_mul_right _ _ _ hX (O.mul_nonneg _ _ hm0 hD0)
  have t3 := O.add_le_add _ _ _ _ t1 t2
  rw [show K.add (K.mul A1 (K.mul (K.mul Z W1) D)) (K.mul X (K.mul m D))
      = K.mul (K.add (K.mul (K.mul Z A1) W1) (K.mul m X)) D by lib_ring,
    show K.add (K.mul A2 (K.mul (K.mul Z W1) D)) (K.mul (K.mul (K.mul κ A2) W1) (K.mul m D))
      = K.mul (K.mul A2 D) (K.mul (K.add Z (K.mul m κ)) W1) by lib_ring] at t3
  have hc : O.le K.zero (K.mul (K.add Z (K.mul m κ)) W1) := by
    have h := O.add_le_add _ _ _ _ hZ (O.mul_nonneg _ _ hm0 hκ)
    rw [DetChain.add_zero'] at h
    exact O.mul_nonneg _ _ h hW1
  have t4 := O.mul_le_mul_right _ _ _ s2 hc
  have s8 := O.le_trans _ _ _ s4 (O.le_trans _ _ _ t3 t4)
  rw [show K.mul (K.mul m co) D = K.mul (K.mul co D) m by lib_ring,
    show K.mul (K.mul m ce) (K.mul (K.add Z (K.mul m κ)) W1) = K.mul (K.mul (K.mul (K.add Z (K.mul m κ)) W1) ce) m by
      lib_ring] at s8
  exact O.pos_cancel _ _ _ hm0 hmne s8

/-- **even → odd ratio with the truncation extra absorbed** -/
theorem cgc_ratio_abs (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (k J r : Nat) (hpr : k + J ≤ r + 2) (D : R) (hD0 : O.le K.zero D)
    (hD : O.le (K.add D (K.add (K.mul (cN K (2 * k * (2 * k))) (W0 K Q (k + J)))
        (K.mul (two K) (K.mul (pwR K Q (2 * (k + 1) * (2 * (k + 1)))) (Tl K Q k J)))))
      (K.mul (cN K ((2 * (k + J) + 2) * (2 * (k + J) + 2))) (W0 K Q (k + J)))) :
    O.le (K.mul (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + r)) (hl K Q (2 * (k + J + 1) + r)) (2 * (k + J) + 1))
          (pwR K (K.mul Q Q) 2))) D)
      (K.mul (K.mul (K.add (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (pwR K Q (4 * (k + J) + 5)))
            (K.mul (cN K ((2 * (k + J) + 2) * (2 * (k + J) + 2))) (K.mul (cN K 4) (pwR K (rho K Q (k + J)) (r + 1)))))
          (K.add (W0 K Q (k + J)) K.one))
        (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + r)) (hl K Q (2 * (k + J + 1) + r)) (2 * (k + J)))
          (pwR K (K.mul Q Q) 2)))) := by
  have ho := HeatUpA.cgc_odd_upA K O h1 Q hQ hQ1 (k + J) r
  have he := HeatLowA.cgc_even_lowA K O h1 Q hQ hQ1 k J (r + 1)
  rw [show 2 * (k + J) + 1 + (r + 1) = 2 * (k + J + 1) + r by lib_nat] at he
  rw [show K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (K.mul (pwR K Q (4 * (k + J) + 5)) (Aout K Q (k + J) (r + 1)))
    = K.mul (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (pwR K Q (4 * (k + J) + 5))) (Aout K Q (k + J) (r + 1))
    by lib_ring] at ho
  have hmp := SU2CharExp.pos_of_ge_one K O h1 h10 ((2 * (k + J) + 2) * (2 * (k + J) + 2))
    (Nat.mul_le_mul (Nat.succ_le_succ (Nat.zero_le _)) (Nat.succ_le_succ (Nat.zero_le _)))
  have hZ := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 ((2 * (k + J) + 3) * (2 * (k + J) + 3)))
    (AreaLower.pw_nn K O Q hQ h1 (4 * (k + J) + 5))
  have hW1 := O.add_le_add _ _ _ _ (HeatLow.W0_nn K O h1 Q hQ (k + J)) h1
  rw [DetChain.add_zero'] at hW1
  have hA20 := O.le_trans _ _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (HeatLow.Bout_nn K O h1 Q hQ (k + J) (r + 1 + 1)))
    (HeatLow.Aout_ge K O h1 Q hQ (k + J) (r + 1 + 1))
  have hκ := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 4) (AreaLower.pw_nn K O _ (rho_nn K O h1 Q hQ (k + J)) h1 (r + 1))
  -- X′ ≤ (4 ρ^{r+1} A₂)(W₀+1)
  have hX : O.le (Xo K Q (k + J) r)
      (K.mul (K.mul (K.mul (cN K 4) (pwR K (rho K Q (k + J)) (r + 1))) (Aout K Q (k + J) (r + 1 + 1))) (K.add (W0 K Q (k + J)) K.one)) := by
    have x1 := Xo_le K O h1 Q hQ (k + J) r
    have hAxo0 : O.le K.zero (Axo K Q (k + J) r) :=
      TripleE.sumN_nn K O _ (fun _ => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O Q hQ h1 _)) _
    have x2 := O.mul_le_mul_right _ _ _ (HeatV0W0.V0_le K O h1 Q hQ hQ1 (k + J)) hAxo0
    have x3 := Axo_le K O h1 Q hQ hQ1 (k + J) r
    have x4 := Ax_le K O h1 Q hQ hQ1 (k + J) r
    have x5 := HeatXtr.sumN_len K O (fA K Q (k + J)) (fA_nn K O h1 Q hQ (k + J)) (k + J) (r + 1 + 1) hpr
    have hf : QBinom.sumN K (fA K Q (k + J)) (k + J) = Aout K Q (k + J) (k + J) := rfl
    have hf2 : QBinom.sumN K (fA K Q (k + J)) (r + 1 + 1) = Aout K Q (k + J) (r + 1 + 1) := rfl
    rw [hf, hf2] at x5
    have hpw := AreaLower.pw_nn K O _ (rho_nn K O h1 Q hQ (k + J)) h1 (r + 1)
    have x6 := O.le_trans _ _ _ x4 (by
      have := O.mul_le_mul_right _ _ _ x5 hpw
      rw [K.mul_comm (Aout K Q (k + J) (k + J)), K.mul_comm (Aout K Q (k + J) (r + 1 + 1))] at this
      exact this)
    have x7 := O.le_trans _ _ _ x3 (by
      have := O.mul_le_mul_right _ _ _ x6 (SU2CharExp.cN_nn K O h1 4)
      rw [K.mul_comm (Ax K Q (k + J) r)] at this
      exact this)
    have x8 := O.mul_le_mul_right _ _ _ x7 hW1
    rw [K.mul_comm (V0 K Q (k + J)), K.mul_comm (K.add (W0 K Q (k + J)) K.one) (Axo K Q (k + J) r)] at x2
    have x9 := O.le_trans _ _ _ x1 (O.le_trans _ _ _ (by
      rw [K.mul_comm (Axo K Q (k + J) r) (V0 K Q (k + J))] at x2
      rw [K.mul_comm (Axo K Q (k + J) r) (V0 K Q (k + J))]
      exact x2) x8)
    generalize Xo K Q (k + J) r = X at x9 ⊢
    generalize cN K 4 = f at x9 ⊢
    generalize pwR K (rho K Q (k + J)) (r + 1) = w at x9 ⊢
    generalize Aout K Q (k + J) (r + 1 + 1) = A at x9 ⊢
    generalize K.add (W0 K Q (k + J)) K.one = W1 at x9 ⊢
    rw [show K.mul (K.mul (K.mul w A) f) W1 = K.mul (K.mul (K.mul f w) A) W1 by lib_ring] at x9
    exact x9
  exact alg2 K O _ _ _ _ _ _ _ _ _ _ _ _ _ hmp.1 hmp.2 hD0 hZ hW1 (HeatRatio.Aout_mono K O h1 Q hQ (k + J) (r + 1)) hA20
    hκ hX ho he hD

end HeatRatioX

#print axioms HeatRatioX.nat_sq
#print axioms HeatRatioX.nat_exp
#print axioms HeatRatioX.Axo_le
#print axioms HeatRatioX.alg2
#print axioms HeatRatioX.cgc_ratio_abs

ym/HeatRatioXO.lean

/-
  ym/HeatRatioXO — T45, G78m route (f), part 6e(t): the odd → even neighbour ratio with the truncation extra absorbed
  (p = k+J, L = 2p+3+r, p+1 ≤ r+2, 0 ≤ Q ≤ 1, ρ′ = 4Q^{8p+20}): for D ≥ 0 with D + (2k+1)²V₀ + E′ ≤ (2p+3)²V₀,
    (4q²c_{2p+2}) · D ≤ ((2p+4)² Q^{4p+7} + (2p+3)² · 4ρ′^{r+1}) (V₀ + 1) · (4q²c_{2p+1}).
  X ≤ Ax_{p+1}·W₀(p+1) (HeatXtr), Ax_{p+1} ≤ ρ′^{r+1} A_{p+1}(p+1) (HeatAx), A_{p+1}(T) ≤ 4 A′_p(T) (even moment ≤ 4 × odd
  moment below it), A′_p(p+1) ≤ A′_p(r+2), W₀(p+1) ≤ V₀ + 1.  Lean 4 core only, no axioms.
-/
import ym.HeatRatioX

namespace HeatRatioXO

open YMChain LinkIntegral SU2Char HeatPair CgSplit HeatR HeatFac2 HeatOddB MigdalCone HeatW2 HeatV2 HeatXtr HeatAx
  HeatRatioO

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem nat_sq (p t : Nat) : Mt (p + 1) t * Mt (p + 1) t ≤ 4 * (Mo p t * Mo p t) := by
  show (2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t) ≤ 4 * ((2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t))
  have h : 2 * (p + 1) + 2 + 2 * t ≤ 2 * (2 * p + 3 + 2 * t) := by
    rw [show 2 * (2 * p + 3 + 2 * t) = (2 * (p + 1) + 2 + 2 * t) + (2 * p + 2 + 2 * t) by lib_nat]
    exact Nat.le_add_right _ _
  have h2 := Nat.mul_le_mul h h
  rw [show 2 * (2 * p + 3 + 2 * t) * (2 * (2 * p + 3 + 2 * t)) = 4 * ((2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t)) by lib_nat] at h2
  exact h2

theorem nat_exp (p t : Nat) : Mo p t * Mo p t ≤ Mt (p + 1) t * Mt (p + 1) t := by
  show (2 * p + 3 + 2 * t) * (2 * p + 3 + 2 * t) ≤ (2 * (p + 1) + 2 + 2 * t) * (2 * (p + 1) + 2 + 2 * t)
  have h : 2 * p + 3 + 2 * t ≤ 2 * (p + 1) + 2 + 2 * t := by
    rw [show 2 * (p + 1) + 2 + 2 * t = (2 * p + 3 + 2 * t) + 1 by lib_nat]; exact Nat.le_add_right _ _
  exact Nat.mul_le_mul h h

/-- next even moments ≤ 4 × odd moments -/
theorem Aout_le4 (h1 : O.le K.zero K.one) (Q : R) (hQ0 : O.le K.zero Q) (hQ1 : O.le Q K.one) (p T : Nat) :
    O.le (Aout K Q (p + 1) T) (K.mul (cN K 4) (Ao K Q p T)) := by
  show O.le (QBinom.sumN K _ T) (K.mul _ (QBinom.sumN K _ T))
  rw [← QBinom.sumN_mul]
  exact TripleE.sumN_mono_lt K O _ _ T (fun t _ => by
    show O.le (K.mul (cN K (Mt (p + 1) t * Mt (p + 1) t)) (pwR K Q (Mt (p + 1) t * Mt (p + 1) t)))
      (K.mul (cN K 4) (K.mul (cN K (Mo p t * Mo p t)) (pwR K Q (Mo p t * Mo p t))))
    have hc := SU2CharExp.cN_mono K O h1 _ _ (nat_sq p t)
    rw [SU2CharExp.cN_mul K 4] at hc
    have hq := ConeBound.pw_anti K O h1 Q hQ0 hQ1 _ _ (nat_exp p t)
    have s1 := O.mul_le_mul_right _ _ _ hc (AreaLower.pw_nn K O Q hQ0 h1 (Mt (p + 1) t * Mt (p + 1) t))
    have s2 := O.mul_le_mul_right _ _ _ hq (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 4) (SU2CharExp.cN_nn K O h1 (Mo p t * Mo p t)))
    generalize cN K (Mt (p + 1) t * Mt (p + 1) t) = b at s1 ⊢
    generalize pwR K Q (Mt (p + 1) t * Mt (p + 1) t) = x at s1 s2 ⊢
    generalize cN K 4 = f at s1 s2 ⊢
    generalize cN K (Mo p t * Mo p t) = c at s1 s2 ⊢
    generalize pwR K Q (Mo p t * Mo p t) = z at s2 ⊢
    rw [show K.mul (K.mul f c) x = K.mul x (K.mul f c) by lib_ring] at s1
    rw [show K.mul f (K.mul c z) = K.mul z (K.mul f c) by lib_ring]
    exact O.le_trans _ _ _ s1 s2)

/-- **odd → even ratio with the truncation extra absorbed** -/
theorem cgc_ratio_abs_o (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ : O.le K.zero Q) (hQ1 : O.le Q K.one)
    (k J r : Nat) (hpr : k + J + 1 ≤ r + 2) (D : R) (hD0 : O.le K.zero D)
    (hD : O.le (K.add D (K.add (K.mul (cN K ((2 * k + 1) * (2 * k + 1))) (V0 K Q (k + J)))
        (K.mul (two K) (K.mul (pwR K Q ((2 * k + 3) * (2 * k + 3))) (To K Q k J)))))
      (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (V0 K Q (k + J)))) :
    O.le (K.mul (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + 1 + r)) (hl K Q (2 * (k + J + 1) + 1 + r))
          (2 * (k + J + 1))) (pwR K (K.mul Q Q) 2))) D)
      (K.mul (K.mul (K.add (K.mul (cN K ((2 * (k + J) + 4) * (2 * (k + J) + 4))) (pwR K Q (4 * (k + J) + 7)))
            (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (K.mul (cN K 4) (pwR K (rho K Q (k + J + 1)) (r + 1)))))
          (K.add (V0 K Q (k + J)) K.one))
        (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + 1 + r)) (hl K Q (2 * (k + J + 1) + 1 + r))
          (2 * (k + J) + 1)) (pwR K (K.mul Q Q) 2)))) := by
  have hu := HeatUp.cgc_even_up K O h1 Q hQ (k + J + 1) r
  have hl' := cgc_odd_lowA K O h1 Q hQ hQ1 k J (r + 1)
  rw [show 2 * (k + J + 1) + (r + 1) = 2 * (k + J + 1) + 1 + r by lib_nat] at hl'
  have hA1 := O.le_trans _ _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (HeatLow.Bout_nn K O h1 Q hQ (k + J + 1) (r + 1)))
    (HeatLow.Aout_ge K O h1 Q hQ (k + J + 1) (r + 1))
  have hW := O.mul_le_mul_right _ _ _ (HeatW0V0.W0_succ_le K O h1 Q hQ hQ1 (k + J)) hA1
  rw [K.mul_comm (W0 K Q (k + J + 1)), K.mul_comm (K.add (V0 K Q (k + J)) K.one)] at hW
  have hmp := SU2CharExp.pos_of_ge_one K O h1 h10 ((2 * (k + J) + 3) * (2 * (k + J) + 3))
    (Nat.mul_le_mul (Nat.succ_le_succ (Nat.zero_le _)) (Nat.succ_le_succ (Nat.zero_le _)))
  have hW1 := O.add_le_add _ _ _ _ (HeatOddB.V0_nn K O h1 Q hQ (k + J)) h1
  rw [DetChain.add_zero'] at hW1
  have ht := O.mul_le_mul_right _ _ _ (Aout_le K O h1 Q hQ hQ1 (k + J) (r + 1)) hW1
  have hZ := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 ((2 * (k + J) + 4) * (2 * (k + J) + 4)))
    (AreaLower.pw_nn K O Q hQ h1 (4 * (k + J) + 7))
  have hA20 := O.le_trans _ _ _ (O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (Bo_nn K O h1 Q hQ (k + J) (r + 1 + 1)))
    (Ao_ge K O h1 Q hQ (k + J) (r + 1 + 1))
  -- odd-side input of HeatRatio.alg: m·c_e ≤ (Z·A′)·(V₀+1) + m·X
  have ho : O.le (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3)))
        (K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + 1 + r)) (hl K Q (2 * (k + J + 1) + 1 + r))
          (2 * (k + J + 1))) (pwR K (K.mul Q Q) 2))))
      (K.add (K.mul (K.mul (K.mul (cN K ((2 * (k + J) + 4) * (2 * (k + J) + 4))) (pwR K Q (4 * (k + J) + 7)))
          (Ao K Q (k + J) (r + 1))) (K.add (V0 K Q (k + J)) K.one))
        (K.mul (cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3))) (Xtr K Q (k + J + 1) r))) := by
    have t1 := O.le_trans _ _ _ hu (O.add_le_add _ _ _ _ hW (O.le_refl (Xtr K Q (k + J + 1) r)))
    have hm := O.mul_le_mul_right _ _ _ t1 hmp.1
    generalize K.mul (cN K 4) (K.mul (cgc K (hl K Q (2 * (k + J + 1) + 1 + r)) (hl K Q (2 * (k + J + 1) + 1 + r))
      (2 * (k + J + 1))) (pwR K (K.mul Q Q) 2)) = c at hm ⊢
    generalize Aout K Q (k + J + 1) (r + 1) = A1 at hm ht ⊢
    generalize Ao K Q (k + J) (r + 1) = A' at ht ⊢
    generalize K.add (V0 K Q (k + J)) K.one = W1 at hm ht ⊢
    generalize Xtr K Q (k + J + 1) r = X at hm ⊢
    generalize cN K ((2 * (k + J) + 3) * (2 * (k + J) + 3)) = m at hm ht ⊢
    generalize cN K ((2 * (k + J) + 4) * (2 * (k + J) + 4)) = z at ht ⊢
    generalize pwR K Q (4 * (k + J) + 7) = y at ht ⊢
    rw [show K.mul (K.add (K.mul A1 W1) X) m = K.add (K.mul (K.mul m A1) W1) (K.mul m X) by lib_ring,
      show K.mul c m = K.mul m c by lib_ring] at hm
    rw [show K.mul (K.mul z (K.mul y A')) W1 = K.mul (K.mul (K.mul z y) A') W1 by lib_ring] at ht
    exact O.le_trans _ _ _ hm (O.add_le_add _ _ _ _ ht (O.le_refl _))
  have hκ := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 4) (AreaLower.pw_nn K O _ (rho_nn K O h1 Q hQ (k + J + 1)) h1 (r + 1))
  -- X ≤ (4 ρ′^{r+1} A′₂)(V₀+1)
  have hX : O.le (Xtr K Q (k + J + 1) r)
      (K.mul (K.mul (K.mul (cN K 4) (pwR K (rho K Q (k + J + 1)) (r + 1))) (Ao K Q (k + J) (r + 1 + 1))) (K.add (V0 K Q (k + J)) K.one)) := by
    have x1 := Xtr_le K O h1 Q hQ (k + J + 1) r
    have hAx0 : O.le K.zero (Ax K Q (k + J + 1) r) :=
      TripleE.sumN_nn K O _ (fun _ => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O Q hQ h1 _)) _
    have x2 := O.mul_le_mul_right _ _ _ (HeatW0V0.W0_succ_le K O h1 Q hQ hQ1 (k + J)) hAx0
    have x4 := Ax_le K O h1 Q hQ hQ1 (k + J + 1) r
    have x3 := Aout_le4 K O h1 Q hQ hQ1 (k + J) (k + J + 1)
    have hAo : ∀ t, O.le K.zero (K.mul (cN K (Mo (k + J) t * Mo (k + J) t)) (pwR K Q (Mo (k + J) t * Mo (k + J) t))) :=
      fun t => O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (AreaLower.pw_nn K O Q hQ h1 _)
    have x5 := HeatXtr.sumN_len K O _ hAo (k + J + 1) (r + 1 + 1) hpr
    have hf : QBinom.sumN K (fun t => K.mul (cN K (Mo (k + J) t * Mo (k + J) t)) (pwR K Q (Mo (k + J) t * Mo (k + J) t))) (k + J + 1)
      = Ao K Q (k + J) (k + J + 1) := rfl
    have hf2 : QBinom.sumN K (fun t => K.mul (cN K (Mo (k + J) t * Mo (k + J) t)) (pwR K Q (Mo (k + J) t * Mo (k + J) t))) (r + 1 + 1)
      = Ao K Q (k + J) (r + 1 + 1) := rfl
    rw [hf, hf2] at x5
    have hpw := AreaLower.pw_nn K O _ (rho_nn K O h1 Q hQ (k + J + 1)) h1 (r + 1)
    have x6 := O.le_trans _ _ _ x3 (by
      have := O.mul_le_mul_right _ _ _ x5 (SU2CharExp.cN_nn K O h1 4)
      rw [K.mul_comm (Ao K Q (k + J) (k + J + 1)), K.mul_comm (Ao K Q (k + J) (r + 1 + 1))] at this
      exact this)
    have x7 := O.le_trans _ _ _ x4 (by
      have := O.mul_le_mul_right _ _ _ x6 hpw
      rw [K.mul_comm (Aout K Q (k + J + 1) (k + J + 1))] at this
      exact this)
    have hW1 := O.add_le_add _ _ _ _ (HeatOddB.V0_nn K O h1 Q hQ (k + J)) h1
    rw [DetChain.add_zero'] at hW1
    have x8 := O.mul_le_mul_right _ _ _ x7 hW1
    rw [K.mul_comm (W0 K Q (k + J + 1)), K.mul_comm (K.add (V0 K Q (k + J)) K.one) (Ax K Q (k + J + 1) r)] at x2
    have x9 := O.le_trans _ _ _ x1 (O.le_trans _ _ _ x2 x8)
    generalize Xtr K Q (k + J + 1) r = X at x9 ⊢
    generalize cN K 4 = f at x9 ⊢
    generalize pwR K (rho K Q (k + J + 1)) (r + 1) = w at x9 ⊢
    generalize Ao K Q (k + J) (r + 1 + 1) = A at x9 ⊢
    generalize K.add (V0 K Q (k + J)) K.one = W1 at x9 ⊢
    rw [show K.mul (K.mul (K.mul f A) w) W1 = K.mul (K.mul (K.mul f w) A) W1 by lib_ring] at x9
    exact x9
  exact HeatRatioX.alg2 K O _ _ _ _ _ _ _ _ _ _ _ _ _ hmp.1 hmp.2 hD0 hZ hW1 (Ao_mono K O h1 Q hQ (k + J) (r + 1)) hA20
    hκ hX ho hl' hD

end HeatRatioXO

#print axioms HeatRatioXO.nat_sq
#print axioms HeatRatioXO.nat_exp
#print axioms HeatRatioXO.Aout_le4
#print axioms HeatRatioXO.cgc_ratio_abs_o

#print axioms: 22 theorems, no axioms.

90.149 Heat-product LR inequality for \(Q\le 0.7\)

Every truncation of the heat product satisfies the one-step LR inequality for \(10Q\le 7\), and the MK flow class persists.

\[ (n+1)\,C_{n+1}(L)\le (n+2)\,Q^{2n+3}\,C_n(L)\quad (0<Q,\ 10Q\le 7,\ \text {all } n,L);\qquad X_k\preceq _{LR}K_{P^4}\ (10P\le 7) \]

ym/PairN.lean

/-
  ym/PairN — T45, G78m 6f-A′-2a (n ≥ 1): the σ-grouped pairs of the n-th monotonicity sum are nonnegative
  (i) for every σ when 10x ≤ 7, and (ii) for the m-th pair (σ = n + 2 + 2m) as soon as 5x^{4m+1} ≤ 2 — both
  uniformly in n.
  The pair
    pair_n(σ) = (n+2) Σ_{|δ|≤n} (σ²−δ²) x^{σ²+δ²+2n+3} − (n+1) Σ_{|δ′|≤n+1} ((σ+1)²−δ′²) x^{(σ+1)²+δ′²}
  is written in matched form (each negative term next to the positive term one step inward), c = k + m + 1:
    n = 2k     : pairE k m x = headE k m x + Σ_{(t′,r) ∈ pts k} funE m x (t′,r)
    n = 2k + 1 : pairO k m x = headO k m x + Σ_{(t′,r) ∈ pts k} funO m x (t′,r)
  (equality with the σ–δ form checked exactly over ℚ for k ≤ 4, m ≤ 3; the Lean identity is a later step).
    tm3 A B e d x       = A x^e − B x^{e+d+3}
    tm15 A B1 B2 e d x  = A x^e − (B1 x^{e+d+1} + B2 x^{e+d+5})
    domT    d + 3 = d′ + G, 5x^G ≤ 2, 2B ≤ 5A                        ⇒ 0 ≤ tm3
    domT15  d + 1 = d₁ + G, d + 5 = d₅ + G, 5x^G ≤ 2, 2(B1 + B2) ≤ 5A ⇒ 0 ≤ tm15
    domS15  10x ≤ 7, 10x⁵ ≤ 2, 7B1 + 2B2 ≤ 10A                     ⇒ 0 ≤ tm15
    natE, natFE, natFO, natOT, natOS   the coefficient inequalities (small cores by lib_nat_le, then scaled)
    pairE_small, pairO_small   0 ≤ x, 10x ≤ 7 ⇒ 0 ≤ pair (all k, m)
    pairE_tail, pairO_tail     0 ≤ x ≤ 1, 5x^{4m+1} ≤ 2 ⇒ 0 ≤ pair (all k)
  Lean 4 core only, no axioms.
-/
import ym.N0CellD
import ym.GKS2
import ym.SU2Strong4D
import BridgeChain
import lib.NatLe

namespace PairN

open YMChain LinkIntegral SU2Char

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- A x^e − B x^{e+d+3} -/
def tm3 (A B e d : Nat) (x : R) : R :=
  K.add (K.mul (cN K A) (pwR K x e)) (K.neg (K.mul (cN K B) (pwR K x (e + (d + 3)))))

/-- A x^e − (B1 x^{e+d+1} + B2 x^{e+d+5}) -/
def tm15 (A B1 B2 e d : Nat) (x : R) : R :=
  K.add (K.mul (cN K A) (pwR K x e))
    (K.neg (K.add (K.mul (cN K B1) (pwR K x (e + (d + 1)))) (K.mul (cN K B2) (pwR K x (e + (d + 5))))))

/-- inner index list: (t′, r) with t′ + 1 + r = k -/
def pts : Nat → List (Nat × Nat)
  | 0 => []
  | k + 1 => (k, 0) :: (pts k).map (fun p => (p.1, p.2 + 1))

/-- even n = 2k, centre term (δ = 0 against δ′ = ±1), c = k + m + 1 -/
def headE (k m : Nat) (x : R) : R :=
  tm3 K ((2 * k + 2) * (4 * (k + m + 1) * (k + m + 1))) ((2 * k + 1) * (8 * (k + m + 1) * (k + m + 1 + 1)))
    (4 * (k + m + 1) * (k + m + 1) + 4 * k + 3) (4 * m) x

/-- even n, the twin terms δ = ±t (t = t′ + 1) against δ′ = ±(t + 1) -/
def funE (m : Nat) (x : R) (p : Nat × Nat) : R :=
  tm3 K (8 * (2 * (p.1 + 1 + p.2) + 2) * (p.2 + 1 + m) * (2 * (p.1 + 1) + p.2 + m + 1))
    (8 * (2 * (p.1 + 1 + p.2) + 1) * (p.2 + 1 + m) * (2 * (p.1 + 1) + p.2 + m + 1 + 1))
    (4 * (p.1 + 1 + p.2 + m + 1) * (p.1 + 1 + p.2 + m + 1) + 4 * (p.1 + 1) * (p.1 + 1) + 4 * (p.1 + 1 + p.2) + 3)
    (4 * m + 4 * (p.1 + 1)) x

/-- odd n = 2k + 1, the twin centre terms against δ′ = 0 and δ′ = ±2, c = k + m + 1 -/
def headO (k m : Nat) (x : R) : R :=
  tm15 K ((2 * k + 3) * (8 * (k + m + 1) * (k + m + 1 + 1))) ((2 * k + 2) * (4 * (k + m + 1 + 1) * (k + m + 1 + 1)))
    ((2 * k + 2) * (8 * (k + m + 1) * (k + m + 1 + 2)))
    (4 * (k + m + 1) * (k + m + 1) + 4 * (k + m + 1) + 4 * k + 7) (4 * m) x

/-- odd n, the twin terms (t = t′ + 1) against δ′ = ±(2t + 2) -/
def funO (m : Nat) (x : R) (p : Nat × Nat) : R :=
  tm3 K (8 * (2 * (p.1 + 1 + p.2) + 3) * (p.2 + 1 + m) * (2 * (p.1 + 1) + p.2 + m + 1 + 1))
    (8 * (2 * (p.1 + 1 + p.2) + 2) * (p.2 + 1 + m) * (2 * (p.1 + 1) + p.2 + m + 1 + 1 + 1))
    (4 * (p.1 + 1 + p.2 + m + 1) * (p.1 + 1 + p.2 + m + 1) + 4 * (p.1 + 1 + p.2 + m + 1) + 2
      + 4 * (p.1 + 1) * (p.1 + 1) + 4 * (p.1 + 1) + 4 * (p.1 + 1 + p.2) + 5)
    (4 * m + 4 * (p.1 + 1) + 2) x

def pairE (k m : Nat) (x : R) : R := K.add (headE K k m x) (sumK K (funE K m x) (pts k))

def pairO (k m : Nat) (x : R) : R := K.add (headO K k m x) (sumK K (funO K m x) (pts k))

/-! ## coefficient inequalities (cores kept small for lib_nat_le) -/

theorem natE (k m : Nat) :
    2 * ((2 * k + 1) * (8 * (k + m + 1) * (k + m + 1 + 1))) ≤ 5 * ((2 * k + 2) * (4 * (k + m + 1) * (k + m + 1))) := by
  have core : 4 * ((2 * k + 1) * (k + m + 1 + 1)) ≤ 5 * ((2 * k + 2) * (k + m + 1)) := by lib_nat_le
  generalize k + m + 1 = c at core ⊢
  have s := Nat.mul_le_mul_left (4 * c) core
  exact Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans s (Nat.le_of_eq (by lib_nat)))

theorem natFE (a y s : Nat) :
    2 * (8 * (2 * a + 1) * s * (y + 1 + 1)) ≤ 5 * (8 * (2 * a + 2) * s * (y + 1)) := by
  have core : 2 * ((2 * a + 1) * (y + 1 + 1)) ≤ 5 * ((2 * a + 2) * (y + 1)) := by lib_nat_le
  have t := Nat.mul_le_mul_left (8 * s) core
  exact Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans t (Nat.le_of_eq (by lib_nat)))

theorem natFO (a y s : Nat) :
    2 * (8 * (2 * a + 2) * s * (y + 1 + 1)) ≤ 5 * (8 * (2 * a + 3) * s * (y + 1)) := by
  have core : 2 * ((2 * a + 2) * (y + 1 + 1)) ≤ 5 * ((2 * a + 3) * (y + 1)) := by lib_nat_le
  have t := Nat.mul_le_mul_left (8 * s) core
  exact Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans t (Nat.le_of_eq (by lib_nat)))

theorem natOT (k m : Nat) :
    2 * ((2 * k + 2) * (4 * (k + m + 1 + 1) * (k + m + 1 + 1)) + (2 * k + 2) * (8 * (k + m + 1) * (k + m + 1 + 2)))
      ≤ 5 * ((2 * k + 3) * (8 * (k + m + 1) * (k + m + 1 + 1))) := by
  generalize k + m = c'
  have core : (c' + 1 + 1) * (c' + 1 + 1) + 2 * ((c' + 1) * (c' + 1 + 2)) ≤ 5 * ((c' + 1) * (c' + 1 + 1)) := by
    lib_nat_le
  generalize c' + 1 = c at core ⊢
  have t := Nat.mul_le_mul_left 8 (Nat.mul_le_mul (show 2 * k + 2 ≤ 2 * k + 3 by lib_nat_le) core)
  exact Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans t (Nat.le_of_eq (by lib_nat)))

theorem natOS (k m : Nat) :
    7 * ((2 * k + 2) * (4 * (k + m + 1 + 1) * (k + m + 1 + 1))) + 2 * ((2 * k + 2) * (8 * (k + m + 1) * (k + m + 1 + 2)))
      ≤ 10 * ((2 * k + 3) * (8 * (k + m + 1) * (k + m + 1 + 1))) := by
  generalize k + m = c'
  have core : 7 * ((c' + 1 + 1) * (c' + 1 + 1)) + 4 * ((c' + 1) * (c' + 1 + 2)) ≤ 20 * ((c' + 1) * (c' + 1 + 1)) := by
    lib_nat_le
  generalize c' + 1 = c at core ⊢
  have t := Nat.mul_le_mul_left 4 (Nat.mul_le_mul (show 2 * k + 2 ≤ 2 * k + 3 by lib_nat_le) core)
  exact Nat.le_trans (Nat.le_of_eq (by lib_nat)) (Nat.le_trans t (Nat.le_of_eq (by lib_nat)))

/-! ## ring-side domination -/

theorem pw3_cN (a : Nat) : pwR K (cN K a) 3 = cN K (a * a * a) := by
  show K.mul (K.mul (K.mul K.one (cN K a)) (cN K a)) (cN K a) = _
  rw [K.one_mul, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K]

theorem pw5_cN (a : Nat) : pwR K (cN K a) 5 = cN K (a * a * a * a * a) := by
  show K.mul (K.mul (K.mul (K.mul (K.mul K.one (cN K a)) (cN K a)) (cN K a)) (cN K a)) (cN K a) = _
  rw [K.one_mul, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K]

theorem domT (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one)
    (A B e d G d' : Nat) (hG : d + 3 = d' + G) (hy : O.le (K.mul (cN K 5) (pwR K x G)) (cN K 2))
    (hN : 2 * B ≤ 5 * A) : O.le K.zero (tm3 K A B e d x) := by
  unfold tm3
  rw [hG]
  apply AltSum.le_sub K O
  have pG := ThetaDup5.pw_nn K O x hx0 h1 G
  have pe := ThetaDup5.pw_nn K O x hx0 h1 e
  have pd1 := HeatClass.pw_le_one K O x hx0 hx1 d'
  have cB := SU2CharExp.cN_nn K O h1 B
  have q : O.le (K.mul (pwR K x d') (pwR K x G)) (pwR K x G) := by
    have := O.mul_le_mul_right _ _ _ pd1 pG
    rw [K.one_mul] at this
    exact this
  have bq := N0CellC.mul_le_l K O _ _ _ cB q
  have c5 := N0CellD.one_le_cN K O h1 5 (by decide)
  have bA : O.le (K.mul (cN K B) (pwR K x G)) (cN K A) := by
    apply O.pos_cancel _ _ (cN K 5) (O.le_trans _ _ _ h1 c5) (N0CellD.ne_of_one_le K O h1 h10 _ c5)
    have s1 := N0CellC.mul_le_l K O _ _ _ cB hy
    have s2 := SU2CharExp.cN_mono K O h1 _ _ hN
    rw [SU2CharExp.cN_mul K, SU2CharExp.cN_mul K] at s2
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ s1
      (O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ s2 (N0Close.le_eq K O _ _ ?_))))
    · generalize cN K B = b; generalize cN K 5 = c; generalize pwR K x G = p; lib_ring
    · generalize cN K B = b; generalize cN K 2 = c; lib_ring
    · generalize cN K A = a; generalize cN K 5 = c; lib_ring
  have all := O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ bq bA) pe
  refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) all
  rw [GKS.pwR_add K x e (d' + G), GKS.pwR_add K x d' G]
  generalize cN K B = b; generalize pwR K x e = u; generalize pwR K x d' = v; generalize pwR K x G = w; lib_ring

theorem domT15 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one)
    (A B1 B2 e d G d1 d5 : Nat) (hG1 : d + 1 = d1 + G) (hG5 : d + 5 = d5 + G)
    (hy : O.le (K.mul (cN K 5) (pwR K x G)) (cN K 2)) (hN : 2 * (B1 + B2) ≤ 5 * A) :
    O.le K.zero (tm15 K A B1 B2 e d x) := by
  unfold tm15
  rw [hG1, hG5]
  apply AltSum.le_sub K O
  have pG := ThetaDup5.pw_nn K O x hx0 h1 G
  have pe := ThetaDup5.pw_nn K O x hx0 h1 e
  have q1 : O.le (K.mul (pwR K x d1) (pwR K x G)) (pwR K x G) := by
    have := O.mul_le_mul_right _ _ _ (HeatClass.pw_le_one K O x hx0 hx1 d1) pG
    rw [K.one_mul] at this
    exact this
  have q5 : O.le (K.mul (pwR K x d5) (pwR K x G)) (pwR K x G) := by
    have := O.mul_le_mul_right _ _ _ (HeatClass.pw_le_one K O x hx0 hx1 d5) pG
    rw [K.one_mul] at this
    exact this
  have c1 := SU2CharExp.cN_nn K O h1 B1
  have c2 := SU2CharExp.cN_nn K O h1 B2
  have c12 : O.le K.zero (K.add (cN K B1) (cN K B2)) := by
    have := O.add_le_add _ _ _ _ c1 c2
    rw [K.zero_add] at this
    exact this
  have bq := O.add_le_add _ _ _ _ (N0CellC.mul_le_l K O _ _ _ c1 q1) (N0CellC.mul_le_l K O _ _ _ c2 q5)
  have c5 := N0CellD.one_le_cN K O h1 5 (by decide)
  have bA : O.le (K.add (K.mul (cN K B1) (pwR K x G)) (K.mul (cN K B2) (pwR K x G))) (cN K A) := by
    apply O.pos_cancel _ _ (cN K 5) (O.le_trans _ _ _ h1 c5) (N0CellD.ne_of_one_le K O h1 h10 _ c5)
    have s1 := N0CellC.mul_le_l K O _ _ _ c12 hy
    have s2 := SU2CharExp.cN_mono K O h1 _ _ hN
    rw [SU2CharExp.cN_mul K, SU2CharExp.cN_mul K, SU2Char.cN_add K] at s2
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ s1
      (O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ s2 (N0Close.le_eq K O _ _ ?_))))
    · generalize cN K B1 = b1; generalize cN K B2 = b2; generalize cN K 5 = c; generalize pwR K x G = p; lib_ring
    · generalize cN K B1 = b1; generalize cN K B2 = b2; generalize cN K 2 = c; lib_ring
    · generalize cN K A = a; generalize cN K 5 = c; lib_ring
  have all := O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ bq bA) pe
  refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) all
  rw [GKS.pwR_add K x e (d1 + G), GKS.pwR_add K x e (d5 + G), GKS.pwR_add K x d1 G, GKS.pwR_add K x d5 G]
  generalize cN K B1 = b1; generalize cN K B2 = b2; generalize pwR K x e = u; generalize pwR K x d1 = v1
  generalize pwR K x d5 = v5; generalize pwR K x G = w; lib_ring

theorem domS15 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x) (hx1 : O.le x K.one)
    (hx : O.le (K.mul (cN K 10) x) (cN K 7)) (h5 : O.le (K.mul (cN K 10) (pwR K x 5)) (cN K 2))
    (A B1 B2 e d : Nat) (hN : 7 * B1 + 2 * B2 ≤ 10 * A) :
    O.le K.zero (tm15 K A B1 B2 e d x) := by
  unfold tm15
  apply AltSum.le_sub K O
  have p5 := ThetaDup5.pw_nn K O x hx0 h1 5
  have pe := ThetaDup5.pw_nn K O x hx0 h1 e
  have pd1 := HeatClass.pw_le_one K O x hx0 hx1 d
  have c1 := SU2CharExp.cN_nn K O h1 B1
  have c2 := SU2CharExp.cN_nn K O h1 B2
  have q1 : O.le (K.mul (pwR K x d) x) x := by
    have := O.mul_le_mul_right _ _ _ pd1 hx0
    rw [K.one_mul] at this
    exact this
  have q5 : O.le (K.mul (pwR K x d) (pwR K x 5)) (pwR K x 5) := by
    have := O.mul_le_mul_right _ _ _ pd1 p5
    rw [K.one_mul] at this
    exact this
  have bq := O.add_le_add _ _ _ _ (N0CellC.mul_le_l K O _ _ _ c1 q1) (N0CellC.mul_le_l K O _ _ _ c2 q5)
  have c10 := N0CellD.one_le_cN K O h1 10 (by decide)
  have bA : O.le (K.add (K.mul (cN K B1) x) (K.mul (cN K B2) (pwR K x 5))) (cN K A) := by
    apply O.pos_cancel _ _ (cN K 10) (O.le_trans _ _ _ h1 c10) (N0CellD.ne_of_one_le K O h1 h10 _ c10)
    have s1 := O.add_le_add _ _ _ _ (N0CellC.mul_le_l K O _ _ _ c1 hx) (N0CellC.mul_le_l K O _ _ _ c2 h5)
    have s2 := SU2CharExp.cN_mono K O h1 _ _ hN
    rw [SU2Char.cN_add K, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K, SU2CharExp.cN_mul K] at s2
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ s1
      (O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ s2 (N0Close.le_eq K O _ _ ?_))))
    · generalize cN K B1 = b1; generalize cN K B2 = b2; generalize cN K 10 = c; generalize pwR K x 5 = p; lib_ring
    · generalize cN K B1 = b1; generalize cN K B2 = b2; generalize cN K 7 = u1; generalize cN K 2 = u2; lib_ring
    · generalize cN K A = a; generalize cN K 10 = c; lib_ring
  have all := O.mul_le_mul_right _ _ _ (O.le_trans _ _ _ bq bA) pe
  refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) all
  have e1 : pwR K x 1 = x := K.one_mul x
  rw [GKS.pwR_add K x e (d + 1), GKS.pwR_add K x e (d + 5), GKS.pwR_add K x d 1, GKS.pwR_add K x d 5, e1]
  generalize cN K B1 = b1; generalize cN K B2 = b2; generalize pwR K x e = u; generalize pwR K x d = v
  generalize pwR K x 5 = w; lib_ring

/-- 10x ≤ 7 gives x ≤ 1, 5x³ ≤ 2, 10x⁵ ≤ 2 -/
theorem small_facts (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x)
    (h7 : O.le (K.mul (cN K 10) x) (cN K 7)) :
    O.le x K.one ∧ O.le (K.mul (cN K 5) (pwR K x 3)) (cN K 2) ∧ O.le (K.mul (cN K 10) (pwR K x 5)) (cN K 2) := by
  have c10 := N0CellD.one_le_cN K O h1 10 (by decide)
  have y0 : O.le K.zero (K.mul (cN K 10) x) := O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 10) hx0
  refine ⟨?_, ?_, ?_⟩
  · apply O.pos_cancel _ _ (cN K 10) (O.le_trans _ _ _ h1 c10) (N0CellD.ne_of_one_le K O h1 h10 _ c10)
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ (K.mul_comm _ _)) (O.le_trans _ _ _ h7
      (O.le_trans _ _ _ (SU2CharExp.cN_mono K O h1 7 10 (by decide)) (N0Close.le_eq K O _ _ (K.one_mul _).symm)))
  · have pm := SU2Strong4D.pw_mono K O h1 _ _ y0 h7 3
    rw [GKS2.pwR_mul K, pw3_cN K, pw3_cN K] at pm
    have c200 := N0CellD.one_le_cN K O h1 200 (by decide)
    apply O.pos_cancel _ _ (cN K 200) (O.le_trans _ _ _ h1 c200) (N0CellD.ne_of_one_le K O h1 h10 _ c200)
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ pm
      (O.le_trans _ _ _ (SU2CharExp.cN_mono K O h1 (7 * 7 * 7) 400 (by decide)) (N0Close.le_eq K O _ _ ?_)))
    · rw [show cN K (10 * 10 * 10) = K.mul (cN K 5) (cN K 200) from SU2CharExp.cN_mul K 5 200]
      generalize cN K 5 = a; generalize cN K 200 = b; generalize pwR K x 3 = p; lib_ring
    · exact SU2CharExp.cN_mul K 2 200
  · have pm := SU2Strong4D.pw_mono K O h1 _ _ y0 h7 5
    rw [GKS2.pwR_mul K, pw5_cN K, pw5_cN K] at pm
    have c4 := N0CellD.one_le_cN K O h1 10000 (by decide)
    apply O.pos_cancel _ _ (cN K 10000) (O.le_trans _ _ _ h1 c4) (N0CellD.ne_of_one_le K O h1 h10 _ c4)
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ pm
      (O.le_trans _ _ _ (SU2CharExp.cN_mono K O h1 (7 * 7 * 7 * 7 * 7) 20000 (by decide)) (N0Close.le_eq K O _ _ ?_)))
    · rw [show cN K (10 * 10 * 10 * 10 * 10) = K.mul (cN K 10) (cN K 10000) from SU2CharExp.cN_mul K 10 10000]
      generalize cN K 10 = a; generalize cN K 10000 = b; generalize pwR K x 5 = p; lib_ring
    · exact SU2CharExp.cN_mul K 2 10000

/-! ## the pairs -/

/-- **6f-A′-2a (even n = 2k, small x)**: 10x ≤ 7 ⇒ every σ-grouped pair is ≥ 0 -/
theorem pairE_small (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x)
    (h7 : O.le (K.mul (cN K 10) x) (cN K 7)) (k m : Nat) : O.le K.zero (pairE K k m x) := by
  have f := small_facts K O h1 h10 x hx0 h7
  unfold pairE
  have hh : O.le K.zero (headE K k m x) := by
    unfold headE
    exact domT K O h1 h10 x hx0 f.1 _ _ _ _ 3 (4 * m) rfl f.2.1 (natE k m)
  have hf : ∀ p, O.le K.zero (funE K m x p) := fun p => by
    unfold funE
    exact domT K O h1 h10 x hx0 f.1 _ _ _ _ 3 (4 * m + 4 * (p.1 + 1)) rfl f.2.1
      (natFE (p.1 + 1 + p.2) (2 * (p.1 + 1) + p.2 + m) (p.2 + 1 + m))
  have := O.add_le_add _ _ _ _ hh (BridgeChain.zero_le_sum K O _ hf (pts k))
  rw [K.zero_add] at this
  exact this

/-- **6f-A′-2a (odd n = 2k + 1, small x)**: 10x ≤ 7 ⇒ every σ-grouped pair is ≥ 0 -/
theorem pairO_small (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x)
    (h7 : O.le (K.mul (cN K 10) x) (cN K 7)) (k m : Nat) : O.le K.zero (pairO K k m x) := by
  have f := small_facts K O h1 h10 x hx0 h7
  unfold pairO
  have hh : O.le K.zero (headO K k m x) := by
    unfold headO
    exact domS15 K O h1 h10 x hx0 f.1 h7 f.2.2 _ _ _ _ _ (natOS k m)
  have hf : ∀ p, O.le K.zero (funO K m x p) := fun p => by
    unfold funO
    exact domT K O h1 h10 x hx0 f.1 _ _ _ _ 3 (4 * m + 4 * (p.1 + 1) + 2) rfl f.2.1
      (natFO (p.1 + 1 + p.2) (2 * (p.1 + 1) + p.2 + m + 1) (p.2 + 1 + m))
  have := O.add_le_add _ _ _ _ hh (BridgeChain.zero_le_sum K O _ hf (pts k))
  rw [K.zero_add] at this
  exact this

/-- **6f-A′-2a′ (even n = 2k, tail)**: 5x^{4m+1} ≤ 2 ⇒ the m-th pair is ≥ 0 -/
theorem pairE_tail (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x)
    (hx1 : O.le x K.one) (k m : Nat) (hy : O.le (K.mul (cN K 5) (pwR K x (4 * m + 1))) (cN K 2)) :
    O.le K.zero (pairE K k m x) := by
  unfold pairE
  have hh : O.le K.zero (headE K k m x) := by
    unfold headE
    exact domT K O h1 h10 x hx0 hx1 _ _ _ _ (4 * m + 1) 2 (by lib_nat) hy (natE k m)
  have hf : ∀ p, O.le K.zero (funE K m x p) := fun p => by
    unfold funE
    exact domT K O h1 h10 x hx0 hx1 _ _ _ _ (4 * m + 1) (4 * (p.1 + 1) + 2) (by lib_nat) hy
      (natFE (p.1 + 1 + p.2) (2 * (p.1 + 1) + p.2 + m) (p.2 + 1 + m))
  have := O.add_le_add _ _ _ _ hh (BridgeChain.zero_le_sum K O _ hf (pts k))
  rw [K.zero_add] at this
  exact this

/-- **6f-A′-2a′ (odd n = 2k + 1, tail)**: 5x^{4m+1} ≤ 2 ⇒ the m-th pair is ≥ 0 -/
theorem pairO_tail (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (x : R) (hx0 : O.le K.zero x)
    (hx1 : O.le x K.one) (k m : Nat) (hy : O.le (K.mul (cN K 5) (pwR K x (4 * m + 1))) (cN K 2)) :
    O.le K.zero (pairO K k m x) := by
  unfold pairO
  have hh : O.le K.zero (headO K k m x) := by
    unfold headO
    exact domT15 K O h1 h10 x hx0 hx1 _ _ _ _ _ (4 * m + 1) 0 4 (by lib_nat) (by lib_nat) hy (natOT k m)
  have hf : ∀ p, O.le K.zero (funO K m x p) := fun p => by
    unfold funO
    exact domT K O h1 h10 x hx0 hx1 _ _ _ _ (4 * m + 1) (4 * (p.1 + 1) + 4) (by lib_nat) hy
      (natFO (p.1 + 1 + p.2) (2 * (p.1 + 1) + p.2 + m + 1) (p.2 + 1 + m))
  have := O.add_le_add _ _ _ _ hh (BridgeChain.zero_le_sum K O _ hf (pts k))
  rw [K.zero_add] at this
  exact this

end PairN

#print axioms PairN.natE
#print axioms PairN.natFE
#print axioms PairN.natFO
#print axioms PairN.natOT
#print axioms PairN.natOS
#print axioms PairN.pw3_cN
#print axioms PairN.pw5_cN
#print axioms PairN.domT
#print axioms PairN.domT15
#print axioms PairN.domS15
#print axioms PairN.small_facts
#print axioms PairN.pairE_small
#print axioms PairN.pairO_small
#print axioms PairN.pairE_tail
#print axioms PairN.pairO_tail

ym/PairNW.lean

/-
  ym/PairNW — T45, G78m 6f-A′-2b (n ≥ 1): the matched pairs of PairN are the σ–δ pairs, written with the heat
  weights wgt(M, d) = (M² − d²) x^{M²+d²} (HeatR.wgt) over the δ-windows, c = k + m + 1:
    winE k M = wgt(M, 0) + Σ_{t=1}^{k} 2 wgt(M, 2t)          (|δ| ≤ 2k, δ even)
    winO k M = Σ_{t=0}^{k} 2 wgt(M, 2t + 1)                  (|δ| ≤ 2k + 1, δ odd)
    pairE_win   pairE k m x = (2k+2) x^{4k+3} winE k (2c) − (2k+1) winO k (2c+1)          (n = 2k)
    pairO_win   pairO k m x = (2k+3) x^{4k+5} winO k (2c+1) − (2k+2) winE (k+1) (2c+2)    (n = 2k + 1)
  i.e. pair_n(σ) = (n+2) x^{2n+3} Σ_{|δ|≤n} wgt(σ, δ) − (n+1) Σ_{|δ′|≤n+1} wgt(σ+1, δ′) with σ = n + 2 + 2m.
  Tools: sum_pts_ext (termwise equality on pts k, using t′ + 1 + r = k), sum_pts_succ (shift of pts (k+1)),
  wgt_eq / wgt2_eq, tm3_gen / tm15_gen (a matched term as coefficient × weight).
  Lean 4 core only, no axioms.
-/
import ym.PairN
import ym.HeatR

namespace PairNW

open YMChain LinkIntegral SU2Char PairN HeatR

variable {R : Type} (K : DetChain.CRing R)

/-! ## sums over pts -/

theorem sum_pts_ext (f g : Nat × Nat → R) : ∀ k, (∀ t r, t + 1 + r = k → f (t, r) = g (t, r)) →
    sumK K f (pts k) = sumK K g (pts k)
  | 0, _ => rfl
  | k + 1, h => by
    have ih := sum_pts_ext (fun p => f (p.1, p.2 + 1)) (fun p => g (p.1, p.2 + 1)) k
      (fun t r hr => h t (r + 1) (by subst hr; rfl))
    show K.add (f (k, 0)) (sumK K f ((pts k).map (fun p => (p.1, p.2 + 1))))
      = K.add (g (k, 0)) (sumK K g ((pts k).map (fun p => (p.1, p.2 + 1))))
    rw [sumK_map K f, sumK_map K g, h k 0 rfl]
    exact congrArg (K.add (g (k, 0))) ih

theorem sum_pts_succ (h : Nat → R) : ∀ k,
    sumK K (fun p => h p.1) (pts (k + 1)) = K.add (h 0) (sumK K (fun p => h (p.1 + 1)) (pts k))
  | 0 => rfl
  | k + 1 => by
    have a1 : sumK K (fun p => h p.1) ((pts (k + 1)).map (fun p => (p.1, p.2 + 1)))
        = sumK K (fun p => h p.1) (pts (k + 1)) := sumK_map K _ _ _
    have a2 : sumK K (fun p => h (p.1 + 1)) ((pts k).map (fun p => (p.1, p.2 + 1)))
        = sumK K (fun p => h (p.1 + 1)) (pts k) := sumK_map K _ _ _
    show K.add (h (k + 1)) (sumK K (fun p => h p.1) ((pts (k + 1)).map (fun p => (p.1, p.2 + 1))))
      = K.add (h 0) (K.add (h (k + 1)) (sumK K (fun p => h (p.1 + 1)) ((pts k).map (fun p => (p.1, p.2 + 1)))))
    rw [a1, a2, sum_pts_succ h k]
    generalize h 0 = u; generalize h (k + 1) = v; generalize sumK K (fun p => h (p.1 + 1)) (pts k) = w; lib_ring

/-! ## weights -/

theorem wgt_eq (x : R) (M d X : Nat) (hX : M * M = d * d + X) :
    wgt K x M d = K.mul (cN K X) (pwR K x (M * M + d * d)) := by
  unfold HeatR.wgt
  rw [hX, SU2Char.cN_add K]
  generalize cN K (d * d) = a; generalize cN K X = b; generalize pwR K x (d * d + X + d * d) = p; lib_ring

theorem wgt2_eq (x : R) (M d X Y : Nat) (hX : M * M = d * d + X) (hY : Y = 2 * X) :
    K.mul (cN K 2) (wgt K x M d) = K.mul (cN K Y) (pwR K x (M * M + d * d)) := by
  rw [wgt_eq K x M d X hX, hY, SU2CharExp.cN_mul K 2 X]
  generalize cN K 2 = a; generalize cN K X = b; generalize pwR K x (M * M + d * d) = p; lib_ring

theorem tm3_gen (x : R) (A B e d a b X Y f g h : Nat) (P N : R) (hA : A = a * X) (hB : B = b * Y)
    (hP : P = K.mul (cN K X) (pwR K x g)) (hN : N = K.mul (cN K Y) (pwR K x h)) (he : e = f + g)
    (hd : e + (d + 3) = h) :
    tm3 K A B e d x = K.add (K.mul (cN K a) (K.mul (pwR K x f) P)) (K.neg (K.mul (cN K b) N)) := by
  unfold tm3
  rw [hd, hP, hN, hA, hB, he, SU2CharExp.cN_mul K a X, SU2CharExp.cN_mul K b Y, GKS.pwR_add K x f g]
  generalize cN K a = a'; generalize cN K X = X'; generalize cN K b = b'; generalize cN K Y = Y'
  generalize pwR K x f = u; generalize pwR K x g = v; generalize pwR K x h = w; lib_ring

theorem tm15_gen (x : R) (A B1 B2 e d a b X Y1 Y2 f g h1 h2 : Nat) (P N1 N2 : R) (hA : A = a * X)
    (hB1 : B1 = b * Y1) (hB2 : B2 = b * Y2) (hP : P = K.mul (cN K X) (pwR K x g))
    (hN1 : N1 = K.mul (cN K Y1) (pwR K x h1)) (hN2 : N2 = K.mul (cN K Y2) (pwR K x h2)) (he : e = f + g)
    (hd1 : e + (d + 1) = h1) (hd5 : e + (d + 5) = h2) :
    tm15 K A B1 B2 e d x = K.add (K.mul (cN K a) (K.mul (pwR K x f) P)) (K.neg (K.mul (cN K b) (K.add N1 N2))) := by
  unfold tm15
  rw [hd1, hd5, hP, hN1, hN2, hA, hB1, hB2, he, SU2CharExp.cN_mul K a X, SU2CharExp.cN_mul K b Y1,
    SU2CharExp.cN_mul K b Y2, GKS.pwR_add K x f g]
  generalize cN K a = a'; generalize cN K X = X'; generalize cN K b = b'; generalize cN K Y1 = Y1'
  generalize cN K Y2 = Y2'; generalize pwR K x f = u; generalize pwR K x g = v; generalize pwR K x h1 = w1
  generalize pwR K x h2 = w2; lib_ring

/-! ## windows -/

def winE (k M : Nat) (x : R) : R :=
  K.add (wgt K x M 0) (sumK K (fun p => K.mul (cN K 2) (wgt K x M (2 * (p.1 + 1)))) (pts k))

def winO (k M : Nat) (x : R) : R :=
  K.add (K.mul (cN K 2) (wgt K x M 1)) (sumK K (fun p => K.mul (cN K 2) (wgt K x M (2 * (p.1 + 1) + 1))) (pts k))

/-! ## matched terms as coefficient × weight -/

theorem headE_id (x : R) (k m : Nat) :
    headE K k m x = K.add (K.mul (cN K (2 * k + 2)) (K.mul (pwR K x (4 * k + 3)) (wgt K x (2 * (k + m + 1)) 0)))
      (K.neg (K.mul (cN K (2 * k + 1)) (K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 1) 1)))) := by
  unfold headE
  generalize hc : k + m + 1 = c
  have hcm : c = k + m + 1 := hc.symm
  exact tm3_gen K x _ _ _ _ (2 * k + 2) (2 * k + 1) (4 * c * c) (8 * c * (c + 1)) (4 * k + 3)
    (2 * c * (2 * c) + 0 * 0) ((2 * c + 1) * (2 * c + 1) + 1 * 1) _ _ rfl rfl
    (wgt_eq K x _ _ _ (by lib_nat))
    (wgt2_eq K x _ _ (4 * c * (c + 1)) _ (by lib_nat) (by lib_nat)) (by lib_nat)
    (by rw [hcm]; lib_nat)

theorem funE_id (x : R) (m t r : Nat) :
    funE K m x (t, r) = K.add (K.mul (cN K (2 * (t + 1 + r) + 2))
        (K.mul (pwR K x (4 * (t + 1 + r) + 3)) (K.mul (cN K 2) (wgt K x (2 * (t + 1 + r + m + 1)) (2 * (t + 1))))))
      (K.neg (K.mul (cN K (2 * (t + 1 + r) + 1))
        (K.mul (cN K 2) (wgt K x (2 * (t + 1 + r + m + 1) + 1) (2 * (t + 1) + 1))))) := by
  unfold funE
  show tm3 K (8 * (2 * (t + 1 + r) + 2) * (r + 1 + m) * (2 * (t + 1) + r + m + 1))
    (8 * (2 * (t + 1 + r) + 1) * (r + 1 + m) * (2 * (t + 1) + r + m + 1 + 1))
    (4 * (t + 1 + r + m + 1) * (t + 1 + r + m + 1) + 4 * (t + 1) * (t + 1) + 4 * (t + 1 + r) + 3)
    (4 * m + 4 * (t + 1)) x = _
  exact tm3_gen K x _ _ _ _ (2 * (t + 1 + r) + 2) (2 * (t + 1 + r) + 1)
    (8 * (r + 1 + m) * (2 * (t + 1) + r + m + 1)) (8 * (r + 1 + m) * (2 * (t + 1) + r + m + 1 + 1))
    (4 * (t + 1 + r) + 3)
    (2 * (t + 1 + r + m + 1) * (2 * (t + 1 + r + m + 1)) + 2 * (t + 1) * (2 * (t + 1)))
    ((2 * (t + 1 + r + m + 1) + 1) * (2 * (t + 1 + r + m + 1) + 1) + (2 * (t + 1) + 1) * (2 * (t + 1) + 1)) _ _
    (by generalize 2 * (t + 1 + r) + 2 = u; generalize r + 1 + m = s; generalize 2 * (t + 1) + r + m + 1 = y; lib_nat)
    (by generalize 2 * (t + 1 + r) + 1 = u; generalize r + 1 + m = s; generalize 2 * (t + 1) + r + m + 1 + 1 = y
        lib_nat)
    (wgt2_eq K x _ _ (4 * (r + 1 + m) * (2 * (t + 1) + r + m + 1)) _ (by lib_nat) (by lib_nat))
    (wgt2_eq K x _ _ (4 * (r + 1 + m) * (2 * (t + 1) + r + m + 1 + 1)) _ (by lib_nat) (by lib_nat))
    (by lib_nat) (by lib_nat)

theorem headO_id (x : R) (k m : Nat) :
    headO K k m x = K.add (K.mul (cN K (2 * k + 3))
        (K.mul (pwR K x (4 * k + 5)) (K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 1) 1))))
      (K.neg (K.mul (cN K (2 * k + 2))
        (K.add (wgt K x (2 * (k + m + 1) + 2) 0) (K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 2) (2 * (0 + 1))))))) := by
  unfold headO
  generalize hc : k + m + 1 = c
  have hcm : c = k + m + 1 := hc.symm
  exact tm15_gen K x _ _ _ _ _ (2 * k + 3) (2 * k + 2) (8 * c * (c + 1)) (4 * (c + 1) * (c + 1)) (8 * c * (c + 2))
    (4 * k + 5) ((2 * c + 1) * (2 * c + 1) + 1 * 1) ((2 * c + 2) * (2 * c + 2) + 0 * 0)
    ((2 * c + 2) * (2 * c + 2) + 2 * (0 + 1) * (2 * (0 + 1))) _ _ _ rfl rfl rfl
    (wgt2_eq K x _ _ (4 * c * (c + 1)) _ (by lib_nat) (by lib_nat))
    (wgt_eq K x _ _ _ (by lib_nat))
    (wgt2_eq K x _ _ (4 * c * (c + 2)) _ (by lib_nat) (by lib_nat)) (by lib_nat)
    (by rw [hcm]; lib_nat) (by rw [hcm]; lib_nat)

theorem funO_id (x : R) (m t r : Nat) :
    funO K m x (t, r) = K.add (K.mul (cN K (2 * (t + 1 + r) + 3))
        (K.mul (pwR K x (4 * (t + 1 + r) + 5)) (K.mul (cN K 2) (wgt K x (2 * (t + 1 + r + m + 1) + 1) (2 * (t + 1) + 1)))))
      (K.neg (K.mul (cN K (2 * (t + 1 + r) + 2))
        (K.mul (cN K 2) (wgt K x (2 * (t + 1 + r + m + 1) + 2) (2 * (t + 1 + 1)))))) := by
  unfold funO
  show tm3 K (8 * (2 * (t + 1 + r) + 3) * (r + 1 + m) * (2 * (t + 1) + r + m + 1 + 1))
    (8 * (2 * (t + 1 + r) + 2) * (r + 1 + m) * (2 * (t + 1) + r + m + 1 + 1 + 1))
    (4 * (t + 1 + r + m + 1) * (t + 1 + r + m + 1) + 4 * (t + 1 + r + m + 1) + 2
      + 4 * (t + 1) * (t + 1) + 4 * (t + 1) + 4 * (t + 1 + r) + 5)
    (4 * m + 4 * (t + 1) + 2) x = _
  exact tm3_gen K x _ _ _ _ (2 * (t + 1 + r) + 3) (2 * (t + 1 + r) + 2)
    (8 * (r + 1 + m) * (2 * (t + 1) + r + m + 1 + 1)) (8 * (r + 1 + m) * (2 * (t + 1) + r + m + 1 + 1 + 1))
    (4 * (t + 1 + r) + 5)
    ((2 * (t + 1 + r + m + 1) + 1) * (2 * (t + 1 + r + m + 1) + 1) + (2 * (t + 1) + 1) * (2 * (t + 1) + 1))
    ((2 * (t + 1 + r + m + 1) + 2) * (2 * (t + 1 + r + m + 1) + 2) + 2 * (t + 1 + 1) * (2 * (t + 1 + 1))) _ _
    (by generalize 2 * (t + 1 + r) + 3 = u; generalize r + 1 + m = s; generalize 2 * (t + 1) + r + m + 1 + 1 = y
        lib_nat)
    (by generalize 2 * (t + 1 + r) + 2 = u; generalize r + 1 + m = s
        generalize 2 * (t + 1) + r + m + 1 + 1 + 1 = y; lib_nat)
    (wgt2_eq K x _ _ (4 * (r + 1 + m) * (2 * (t + 1) + r + m + 1 + 1)) _ (by lib_nat) (by lib_nat))
    (wgt2_eq K x _ _ (4 * (r + 1 + m) * (2 * (t + 1) + r + m + 1 + 1 + 1)) _ (by lib_nat) (by lib_nat))
    (by lib_nat) (by lib_nat)

/-! ## the window identities -/

/-- Σ_p (a·(u·P p) − b·N p) = a·(u·Σ P) − b·Σ N -/
theorem sum_split (a u b : R) (P N : Nat × Nat → R) (l : List (Nat × Nat)) :
    sumK K (fun p => K.add (K.mul a (K.mul u (P p))) (K.neg (K.mul b (N p)))) l
      = K.add (K.mul a (K.mul u (sumK K P l))) (K.neg (K.mul b (sumK K N l))) := by
  have e1 : sumK K (fun p => K.add (K.mul a (K.mul u (P p))) (K.neg (K.mul b (N p)))) l
      = sumK K (fun p => K.add (K.mul (K.mul a u) (P p)) (K.mul (K.neg b) (N p))) l :=
    sumK_ext K _ _ (fun p => by
      generalize P p = y; generalize N p = z; lib_ring) l
  rw [e1, sumK_add K, ← mul_sumK K (K.mul a u) P l, ← mul_sumK K (K.neg b) N l]
  generalize sumK K P l = sp; generalize sumK K N l = sn; lib_ring

/-- **6f-A′-2b (even n = 2k)**: pairE k m x = (2k+2) x^{4k+3} winE k (2c) − (2k+1) winO k (2c+1) -/
theorem pairE_win (x : R) (k m : Nat) :
    pairE K k m x = K.add (K.mul (cN K (2 * k + 2)) (K.mul (pwR K x (4 * k + 3)) (winE K k (2 * (k + m + 1)) x)))
      (K.neg (K.mul (cN K (2 * k + 1)) (winO K k (2 * (k + m + 1) + 1) x))) := by
  unfold pairE winE winO
  have hs := sum_pts_ext K (funE K m x)
    (fun p => K.add (K.mul (cN K (2 * k + 2)) (K.mul (pwR K x (4 * k + 3))
        (K.mul (cN K 2) (wgt K x (2 * (k + m + 1)) (2 * (p.1 + 1))))))
      (K.neg (K.mul (cN K (2 * k + 1)) (K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 1) (2 * (p.1 + 1) + 1))))))
    k (fun t r hr => by subst hr; exact funE_id K x m t r)
  rw [hs, sum_split K, headE_id K x k m]
  generalize cN K (2 * k + 2) = a; generalize cN K (2 * k + 1) = b; generalize pwR K x (4 * k + 3) = u
  generalize wgt K x (2 * (k + m + 1)) 0 = w0; generalize K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 1) 1) = w1
  generalize sumK K (fun p => K.mul (cN K 2) (wgt K x (2 * (k + m + 1)) (2 * (p.1 + 1)))) (pts k) = s1
  generalize sumK K (fun p => K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 1) (2 * (p.1 + 1) + 1))) (pts k) = s2
  lib_ring

/-- **6f-A′-2b (odd n = 2k + 1)**: pairO k m x = (2k+3) x^{4k+5} winO k (2c+1) − (2k+2) winE (k+1) (2c+2) -/
theorem pairO_win (x : R) (k m : Nat) :
    pairO K k m x = K.add (K.mul (cN K (2 * k + 3)) (K.mul (pwR K x (4 * k + 5)) (winO K k (2 * (k + m + 1) + 1) x)))
      (K.neg (K.mul (cN K (2 * k + 2)) (winE K (k + 1) (2 * (k + m + 1) + 2) x))) := by
  unfold pairO winE winO
  have hs := sum_pts_ext K (funO K m x)
    (fun p => K.add (K.mul (cN K (2 * k + 3)) (K.mul (pwR K x (4 * k + 5))
        (K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 1) (2 * (p.1 + 1) + 1)))))
      (K.neg (K.mul (cN K (2 * k + 2)) (K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 2) (2 * (p.1 + 1 + 1)))))))
    k (fun t r hr => by subst hr; exact funO_id K x m t r)
  have hw := sum_pts_succ K (fun t => K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 2) (2 * (t + 1)))) k
  rw [hs, sum_split K, headO_id K x k m, hw]
  generalize cN K (2 * k + 3) = a; generalize cN K (2 * k + 2) = b; generalize pwR K x (4 * k + 5) = u
  generalize K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 1) 1) = w1
  generalize wgt K x (2 * (k + m + 1) + 2) 0 = w0
  generalize K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 2) (2 * (0 + 1))) = w2
  generalize sumK K (fun p => K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 1) (2 * (p.1 + 1) + 1))) (pts k) = s1
  generalize sumK K (fun p => K.mul (cN K 2) (wgt K x (2 * (k + m + 1) + 2) (2 * (p.1 + 1 + 1)))) (pts k) = s2
  lib_ring

end PairNW

#print axioms PairNW.sum_pts_ext
#print axioms PairNW.sum_pts_succ
#print axioms PairNW.wgt_eq
#print axioms PairNW.wgt2_eq
#print axioms PairNW.tm3_gen
#print axioms PairNW.tm15_gen
#print axioms PairNW.headE_id
#print axioms PairNW.funE_id
#print axioms PairNW.headO_id
#print axioms PairNW.funO_id
#print axioms PairNW.sum_split
#print axioms PairNW.pairE_win
#print axioms PairNW.pairO_win

ym/HeatLRPair.lean

/-
  ym/HeatLRPair — T45, G78m 6f-A′-2d (box truncation, even n): the heat-product LR inequality at every
  truncation length,
      (n+1) C_{n+1}(L) ≤ (n+2) Q^{2n+3} C_n(L)        (n = 2p, L = 2p + 2 + r′, 4q² factor kept),
  for 0 ≤ Q, 10Q ≤ 7 — this lifts heat-lr-upper (16Q⁶ ≤ 1) / HeatUpperLR2 (2Q ≤ 1) to Q ≤ 0.7 for even n.
  Proof: HeatAsm.cgc_even / HeatAsmO.cgc_odd write both sides as Σ_{c≤p} μ Σ_t wgt; for each diagonal c the
  negative terms (t < N) are matched with the positive terms of the same t (one step inward, the PairN groups,
  via PairNW.headE_id / funE_id), and the positive term t = N has no partner.
    gE p c t          the matched group (2p+2)Q^{4p+3} μ_{2c} wgt(2p+2+2t, 2c) − (2p+1) μ_{2c+1} wgt(2p+3+2t, 2c+1)
    gE_nn             10Q ≤ 7, c ≤ p ⇒ 0 ≤ gE p c t
    term_nn           one diagonal: Σ_{t<N} groups + the unmatched positive term ≥ 0
    lr_even_small     the inequality above
  Lean 4 core only, no axioms.
-/
import ym.PairNW
import ym.HeatAsmO
import lib.NatSub

namespace HeatLRPair

open YMChain LinkIntegral SU2Char HeatPair ThetaDup2 CgSplit CgCorr HeatR HeatCg HeatWin HeatAsm MigdalCone PairN PairNW

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem sumN_nn_lt (f : Nat → R) : ∀ n, (∀ j, j < n → O.le K.zero (f j)) → O.le K.zero (QBinom.sumN K f n)
  | 0, _ => O.le_refl _
  | n + 1, h => by
    have := O.add_le_add _ _ _ _ (sumN_nn_lt f n (fun j hj => h j (Nat.lt_succ_of_lt hj))) (h n (Nat.lt_succ_self n))
    rw [K.zero_add] at this
    exact this

theorem lin3 (a u b : R) (f g : Nat → R) : ∀ N,
    QBinom.sumN K (fun t => K.add (K.mul a (K.mul u (f t))) (K.neg (K.mul b (g t)))) N
      = K.add (K.mul a (K.mul u (QBinom.sumN K f N))) (K.neg (K.mul b (QBinom.sumN K g N)))
  | 0 => by
    show K.zero = K.add (K.mul a (K.mul u K.zero)) (K.neg (K.mul b K.zero))
    lib_ring
  | N + 1 => by
    show K.add (QBinom.sumN K (fun t => K.add (K.mul a (K.mul u (f t))) (K.neg (K.mul b (g t)))) N)
        (K.add (K.mul a (K.mul u (f N))) (K.neg (K.mul b (g N))))
      = K.add (K.mul a (K.mul u (K.add (QBinom.sumN K f N) (f N)))) (K.neg (K.mul b (K.add (QBinom.sumN K g N) (g N))))
    rw [lin3 a u b f g N]
    generalize QBinom.sumN K f N = sf; generalize QBinom.sumN K g N = sg; generalize f N = x; generalize g N = y
    lib_ring

theorem mulS_add (D : Nat) (a b : R) : mulS K D (K.add a b) = K.add (mulS K D a) (mulS K D b) := by
  cases D with
  | zero => rfl
  | succ D =>
    show K.add (K.add a b) (K.add a b) = K.add (K.add a a) (K.add b b)
    lib_ring

theorem mulS_sum (D : Nat) (f : Nat → R) : ∀ n,
    mulS K D (QBinom.sumN K f n) = QBinom.sumN K (fun t => mulS K D (f t)) n
  | 0 => HeatAsm.mulS_zero K D
  | n + 1 => by
    show mulS K D (K.add (QBinom.sumN K f n) (f n)) = K.add (QBinom.sumN K (fun t => mulS K D (f t)) n) (mulS K D (f n))
    rw [mulS_add K D, mulS_sum D f n]

theorem mulS_nn (D : Nat) (a : R) (ha : O.le K.zero a) : O.le K.zero (mulS K D a) := by
  cases D with
  | zero => exact ha
  | succ D =>
    show O.le K.zero (K.add a a)
    have := O.add_le_add _ _ _ _ ha ha
    rw [K.zero_add] at this
    exact this

theorem mulS_two (D : Nat) (a : R) : mulS K (D + 1) a = K.mul (cN K 2) a := by
  show K.add a a = K.mul (cN K 2) a
  rw [show cN K 2 = K.add K.one (K.add K.one K.zero) from rfl]
  lib_ring

theorem wgt_nn (h1 : O.le K.zero K.one) (Q : R) (hQ : O.le K.zero Q) (M d : Nat) (h : d ≤ M) :
    O.le K.zero (wgt K Q M d) := by
  unfold HeatR.wgt
  exact O.mul_nonneg _ _ (AltSum.le_sub K O _ _ (SU2CharExp.cN_mono K O h1 _ _ (Nat.mul_le_mul h h)))
    (ThetaDup5.pw_nn K O Q hQ h1 _)

/-- the matched group of diagonal c, outer index t -/
def gE (Q : R) (p c t : Nat) : R :=
  K.add (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3)) (mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * t) (2 * c)))))
    (K.neg (K.mul (cN K (2 * p + 1)) (mulS K (2 * c + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)))))

theorem gE_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p c t : Nat) (hc : c ≤ p) : O.le K.zero (gE K Q p c t) := by
  have f := small_facts K O h1 h10 Q hQ0 h7
  cases c with
  | zero =>
    have hh : O.le K.zero (headE K p t Q) := by
      unfold headE
      exact domT K O h1 h10 Q hQ0 f.1 _ _ _ _ 3 (4 * t) rfl f.2.1 (natE p t)
    rw [headE_id K Q p t] at hh
    unfold gE
    rw [show 2 * p + 2 + 2 * t = 2 * (p + t + 1) by lib_nat, show 2 * p + 3 + 2 * t = 2 * (p + t + 1) + 1 by lib_nat]
    refine O.le_trans _ _ _ hh (N0Close.le_eq K O _ _ ?_)
    show _ = K.add (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3)) (wgt K Q (2 * (p + t + 1)) 0)))
      (K.neg (K.mul (cN K (2 * p + 1)) (K.add (wgt K Q (2 * (p + t + 1) + 1) 1) (wgt K Q (2 * (p + t + 1) + 1) 1))))
    rw [show cN K 2 = K.add K.one (K.add K.one K.zero) from rfl]
    generalize wgt K Q (2 * (p + t + 1)) 0 = w0; generalize wgt K Q (2 * (p + t + 1) + 1) 1 = w1
    generalize cN K (2 * p + 2) = a; generalize cN K (2 * p + 1) = b; generalize pwR K Q (4 * p + 3) = u
    lib_ring
  | succ c' =>
    obtain ⟨d, hd⟩ := LibNatSub.le_ex hc
    subst hd
    have hh : O.le K.zero (funE K t Q (c', d)) := by
      unfold funE
      exact domT K O h1 h10 Q hQ0 f.1 _ _ _ _ 3 (4 * t + 4 * (c' + 1)) rfl f.2.1
        (natFE (c' + 1 + d) (2 * (c' + 1) + d + t) (d + 1 + t))
    rw [funE_id K Q t c' d] at hh
    unfold gE
    rw [show 2 * (c' + 1 + d) + 2 + 2 * t = 2 * (c' + 1 + d + t + 1) by lib_nat,
      show 2 * (c' + 1 + d) + 3 + 2 * t = 2 * (c' + 1 + d + t + 1) + 1 by lib_nat]
    refine O.le_trans _ _ _ hh (N0Close.le_eq K O _ _ ?_)
    rw [show mulS K (2 * (c' + 1)) (wgt K Q (2 * (c' + 1 + d + t + 1)) (2 * (c' + 1)))
        = K.mul (cN K 2) (wgt K Q (2 * (c' + 1 + d + t + 1)) (2 * (c' + 1))) from mulS_two K (2 * c' + 1) _,
      mulS_two K (2 * (c' + 1)) _]

/-- one diagonal c ≤ p: the matched groups t < N plus the unmatched positive term t = N -/
theorem term_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p r c : Nat) (hc : c ≤ p) :
    O.le K.zero (K.add (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3))
        (mulS K (2 * c) (QBinom.sumN K (fun t => wgt K Q (2 * p + 2 + 2 * t) (2 * c)) (r + 1 + 1 + (p - c))))))
      (K.neg (K.mul (cN K (2 * p + 1))
        (mulS K (2 * c + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)) (r + 1 + (p - c))))))) := by
  rw [show r + 1 + 1 + (p - c) = r + 1 + (p - c) + 1 by lib_nat]
  generalize r + 1 + (p - c) = N
  show O.le K.zero (K.add (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3))
      (mulS K (2 * c) (K.add (QBinom.sumN K (fun t => wgt K Q (2 * p + 2 + 2 * t) (2 * c)) N)
        (wgt K Q (2 * p + 2 + 2 * N) (2 * c))))))
    (K.neg (K.mul (cN K (2 * p + 1)) (mulS K (2 * c + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)) N)))))
  rw [mulS_add K, mulS_sum K, mulS_sum K]
  have hs := lin3 K (cN K (2 * p + 2)) (pwR K Q (4 * p + 3)) (cN K (2 * p + 1))
    (fun t => mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * t) (2 * c)))
    (fun t => mulS K (2 * c + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1))) N
  have hg : O.le K.zero (QBinom.sumN K (fun t => K.add (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3))
      (mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * t) (2 * c)))))
      (K.neg (K.mul (cN K (2 * p + 1)) (mulS K (2 * c + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)))))) N) :=
    sumN_nn_lt K O _ N (fun t _ => gE_nn K O h1 h10 Q hQ0 h7 p c t hc)
  rw [hs] at hg
  have hx : O.le K.zero (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3)) (mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * N) (2 * c))))) :=
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (O.mul_nonneg _ _ (ThetaDup5.pw_nn K O Q hQ0 h1 _)
      (mulS_nn K O _ _ (wgt_nn K O h1 Q hQ0 _ _ (Nat.le_trans (Nat.mul_le_mul_left 2 hc) (by lib_nat_le)))))
  have := O.add_le_add _ _ _ _ hg hx
  rw [K.zero_add] at this
  refine O.le_trans _ _ _ this (N0Close.le_eq K O _ _ ?_)
  generalize QBinom.sumN K (fun t => mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * t) (2 * c))) N = se
  generalize QBinom.sumN K (fun t => mulS K (2 * c + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1))) N = so
  generalize mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * N) (2 * c)) = w
  generalize cN K (2 * p + 2) = a; generalize cN K (2 * p + 1) = b; generalize pwR K Q (4 * p + 3) = u
  lib_ring

/-- **6f-A′-2d (even n = 2p)**: for 0 ≤ Q, 10Q ≤ 7 and every length L = 2p + 2 + r′,
    (2p+1)·4q²C_{2p+1}(L) ≤ (2p+2) Q^{4p+3}·4q²C_{2p}(L) -/
theorem lr_even_small (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p r : Nat) :
    O.le (K.mul (cN K (2 * p + 1)) (K.mul (cN K 4)
        (K.mul (cgc K (hl K Q (2 * p + 1 + (r + 1))) (hl K Q (2 * p + 1 + (r + 1))) (2 * p + 1)) (pwR K (K.mul Q Q) 2))))
      (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3)) (K.mul (cN K 4)
        (K.mul (cgc K (hl K Q (2 * p + 1 + (r + 1))) (hl K Q (2 * p + 1 + (r + 1))) (2 * p)) (pwR K (K.mul Q Q) 2))))) := by
  have ho := HeatAsmO.cgc_odd K Q p r
  rw [show 2 * (p + 1) + r = 2 * p + 1 + (r + 1) by lib_nat] at ho
  rw [HeatAsm.cgc_even K Q p (r + 1), ho]
  apply ThetaLow.le_of_sub K O
  rw [← lin3 K (cN K (2 * p + 2)) (pwR K Q (4 * p + 3)) (cN K (2 * p + 1))]
  exact sumN_nn_lt K O _ (p + 1) (fun c hc => term_nn K O h1 h10 Q hQ0 h7 p r c (Nat.le_of_lt_succ hc))

end HeatLRPair

#print axioms HeatLRPair.sumN_nn_lt
#print axioms HeatLRPair.lin3
#print axioms HeatLRPair.mulS_add
#print axioms HeatLRPair.mulS_sum
#print axioms HeatLRPair.mulS_nn
#print axioms HeatLRPair.mulS_two
#print axioms HeatLRPair.wgt_nn
#print axioms HeatLRPair.gE_nn
#print axioms HeatLRPair.term_nn
#print axioms HeatLRPair.lr_even_small

ym/HeatLRPairO.lean

/-
  ym/HeatLRPairO — T45, G78m 6f-A′-2d (box truncation, odd n): for n = 2p + 1, 0 ≤ Q, 10Q ≤ 7 and every
  length L = 2(p+1) + (r+1),
      (2p+2)·4q²C_{2p+2}(L) ≤ (2p+3) Q^{4p+5}·4q²C_{2p+1}(L).
  Positive diagonal c (δ = 2c+1, HeatAsmO.cgc_odd) is matched with negative diagonal c+1 (δ′ = 2c+2,
  HeatAsm.cgc_even at p+1); the head (c = 0) also takes the negative diagonal 0. Ranges: negative diagonal c+1
  has one t fewer than positive diagonal c, so every negative term has its partner (PairN groups via
  PairNW.headO_id / funO_id) and one positive term per diagonal is left over; on the head diagonal the last t
  only loses its δ′ = ±2 part.
    succ_sub_succ'    (p+1) − (c+1) = p − c
    lin4              Σ (a(u f) − b(g + h)) = a(u Σf) − b(Σg + Σh)
    hO, hO_nn         head group;  gO, gO_nn  diagonal c ≥ 1 group
    headT_nn, termO_nn   one diagonal
    lr_odd_small      the inequality above
  Lean 4 core only, no axioms.
-/
import ym.HeatLRPair

namespace HeatLRPairO

open YMChain LinkIntegral SU2Char HeatPair ThetaDup2 CgSplit CgCorr HeatR HeatCg HeatWin HeatAsm MigdalCone PairN PairNW
  HeatLRPair

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem succ_sub_succ' (p : Nat) : ∀ c, (p + 1) - (c + 1) = p - c
  | 0 => rfl
  | c + 1 => by
    show Nat.pred ((p + 1) - (c + 1)) = Nat.pred (p - c)
    rw [succ_sub_succ' p c]

theorem lin4 (a u b : R) (f g h : Nat → R) : ∀ N,
    QBinom.sumN K (fun t => K.add (K.mul a (K.mul u (f t))) (K.neg (K.mul b (K.add (g t) (h t))))) N
      = K.add (K.mul a (K.mul u (QBinom.sumN K f N))) (K.neg (K.mul b (K.add (QBinom.sumN K g N) (QBinom.sumN K h N))))
  | 0 => by
    show K.zero = K.add (K.mul a (K.mul u K.zero)) (K.neg (K.mul b (K.add K.zero K.zero)))
    lib_ring
  | N + 1 => by
    show K.add (QBinom.sumN K (fun t => K.add (K.mul a (K.mul u (f t))) (K.neg (K.mul b (K.add (g t) (h t))))) N)
        (K.add (K.mul a (K.mul u (f N))) (K.neg (K.mul b (K.add (g N) (h N)))))
      = K.add (K.mul a (K.mul u (K.add (QBinom.sumN K f N) (f N))))
          (K.neg (K.mul b (K.add (K.add (QBinom.sumN K g N) (g N)) (K.add (QBinom.sumN K h N) (h N)))))
    rw [lin4 a u b f g h N]
    generalize QBinom.sumN K f N = sf; generalize QBinom.sumN K g N = sg; generalize QBinom.sumN K h N = sh
    generalize f N = x; generalize g N = y; generalize h N = z
    lib_ring

/-- head group: positive diagonal 0 against negative diagonals 0 and 1 -/
def hO (Q : R) (p t : Nat) : R :=
  K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5)) (mulS K (2 * 0 + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1)))))
    (K.neg (K.mul (cN K (2 * p + 2)) (K.add (mulS K (2 * 0) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0)))
      (mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1)))))))

/-- diagonal group: positive diagonal c against negative diagonal c + 1 -/
def gO (Q : R) (p c t : Nat) : R :=
  K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5)) (mulS K (2 * c + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)))))
    (K.neg (K.mul (cN K (2 * p + 2)) (mulS K (2 * (c + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1))))))

theorem hO_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p t : Nat) : O.le K.zero (hO K Q p t) := by
  have f := small_facts K O h1 h10 Q hQ0 h7
  have hh : O.le K.zero (headO K p t Q) := by
    unfold headO
    exact domS15 K O h1 h10 Q hQ0 f.1 h7 f.2.2 _ _ _ _ _ (natOS p t)
  rw [headO_id K Q p t] at hh
  unfold hO
  rw [show 2 * p + 3 + 2 * t = 2 * (p + t + 1) + 1 by lib_nat, show 2 * (p + 1) + 2 + 2 * t = 2 * (p + t + 1) + 2 by lib_nat]
  refine O.le_trans _ _ _ hh (N0Close.le_eq K O _ _ ?_)
  show _ = K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (K.add (wgt K Q (2 * (p + t + 1) + 1) 1) (wgt K Q (2 * (p + t + 1) + 1) 1))))
    (K.neg (K.mul (cN K (2 * p + 2)) (K.add (wgt K Q (2 * (p + t + 1) + 2) 0)
      (K.add (wgt K Q (2 * (p + t + 1) + 2) (2 * (0 + 1))) (wgt K Q (2 * (p + t + 1) + 2) (2 * (0 + 1)))))))
  rw [show cN K 2 = K.add K.one (K.add K.one K.zero) from rfl]
  generalize wgt K Q (2 * (p + t + 1) + 1) 1 = w1; generalize wgt K Q (2 * (p + t + 1) + 2) 0 = w0
  generalize wgt K Q (2 * (p + t + 1) + 2) (2 * (0 + 1)) = w2
  generalize cN K (2 * p + 3) = a; generalize cN K (2 * p + 2) = b; generalize pwR K Q (4 * p + 5) = u
  lib_ring

theorem gO_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p c t : Nat) (hc : c + 1 ≤ p) : O.le K.zero (gO K Q p (c + 1) t) := by
  have f := small_facts K O h1 h10 Q hQ0 h7
  obtain ⟨d, hd⟩ := LibNatSub.le_ex hc
  subst hd
  have hh : O.le K.zero (funO K t Q (c, d)) := by
    unfold funO
    exact domT K O h1 h10 Q hQ0 f.1 _ _ _ _ 3 (4 * t + 4 * (c + 1) + 2) rfl f.2.1
      (natFO (c + 1 + d) (2 * (c + 1) + d + t + 1) (d + 1 + t))
  rw [funO_id K Q t c d] at hh
  unfold gO
  rw [show 2 * (c + 1 + d) + 3 + 2 * t = 2 * (c + 1 + d + t + 1) + 1 by lib_nat,
    show 2 * (c + 1 + d + 1) + 2 + 2 * t = 2 * (c + 1 + d + t + 1) + 2 by lib_nat]
  refine O.le_trans _ _ _ hh (N0Close.le_eq K O _ _ ?_)
  rw [mulS_two K (2 * (c + 1)) _,
    show mulS K (2 * (c + 1 + 1)) (wgt K Q (2 * (c + 1 + d + t + 1) + 2) (2 * (c + 1 + 1)))
      = K.mul (cN K 2) (wgt K Q (2 * (c + 1 + d + t + 1) + 2) (2 * (c + 1 + 1))) from mulS_two K (2 * (c + 1) + 1) _]

/-- the head diagonal -/
theorem headT_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p r : Nat) :
    O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
        (mulS K (2 * 0 + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1)) (r + 1 + 1 + (p - 0))))))
      (K.neg (K.mul (cN K (2 * p + 2))
        (K.add (mulS K (2 * 0) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0)) (r + 1 + (p + 1 - 0))))
          (mulS K (2 * (0 + 1)) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1)))
            (r + 1 + (p + 1 - (0 + 1))))))))) := by
  rw [succ_sub_succ' p 0, show r + 1 + 1 + (p - 0) = r + 1 + (p - 0) + 1 by lib_nat,
    show r + 1 + (p + 1 - 0) = r + 1 + (p - 0) + 1 from rfl]
  generalize r + 1 + (p - 0) = M
  show O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (mulS K (2 * 0 + 1) (K.add (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1)) M)
        (wgt K Q (2 * p + 3 + 2 * M) (2 * 0 + 1))))))
    (K.neg (K.mul (cN K (2 * p + 2))
      (K.add (mulS K (2 * 0) (K.add (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0)) M)
          (wgt K Q (2 * (p + 1) + 2 + 2 * M) (2 * 0))))
        (mulS K (2 * (0 + 1)) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1))) M))))))
  rw [mulS_add K, mulS_add K, mulS_sum K, mulS_sum K, mulS_sum K]
  have hs := lin4 K (cN K (2 * p + 3)) (pwR K Q (4 * p + 5)) (cN K (2 * p + 2))
    (fun t => mulS K (2 * 0 + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1)))
    (fun t => mulS K (2 * 0) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0)))
    (fun t => mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1)))) M
  have hg : O.le K.zero (QBinom.sumN K (fun t => K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (mulS K (2 * 0 + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1)))))
      (K.neg (K.mul (cN K (2 * p + 2)) (K.add (mulS K (2 * 0) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0)))
        (mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1)))))))) M) :=
    sumN_nn_lt K O _ M (fun t _ => hO_nn K O h1 h10 Q hQ0 h7 p t)
  rw [hs] at hg
  have hl := hO_nn K O h1 h10 Q hQ0 h7 p M
  unfold hO at hl
  have hn2 : O.le K.zero (K.mul (cN K (2 * p + 2)) (mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * M) (2 * (0 + 1))))) :=
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (mulS_nn K O _ _ (wgt_nn K O h1 Q hQ0 _ _ (by lib_nat_le)))
  have := O.add_le_add _ _ _ _ hg (O.add_le_add _ _ _ _ hl hn2)
  rw [K.zero_add, K.zero_add] at this
  refine O.le_trans _ _ _ this (N0Close.le_eq K O _ _ ?_)
  generalize QBinom.sumN K (fun t => mulS K (2 * 0 + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1))) M = s1
  generalize QBinom.sumN K (fun t => mulS K (2 * 0) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0))) M = s0
  generalize QBinom.sumN K (fun t => mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1)))) M = s2
  generalize mulS K (2 * 0 + 1) (wgt K Q (2 * p + 3 + 2 * M) (2 * 0 + 1)) = w1
  generalize mulS K (2 * 0) (wgt K Q (2 * (p + 1) + 2 + 2 * M) (2 * 0)) = w0
  generalize mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * M) (2 * (0 + 1))) = w2
  generalize cN K (2 * p + 3) = a; generalize cN K (2 * p + 2) = b; generalize pwR K Q (4 * p + 5) = u
  lib_ring

/-- a diagonal c + 1 ≤ p: positive diagonal c+1 against negative diagonal c+2 -/
theorem termO_nn (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p r c : Nat) (hc : c + 1 ≤ p) :
    O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
        (mulS K (2 * (c + 1) + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1))
          (r + 1 + 1 + (p - (c + 1)))))))
      (K.neg (K.mul (cN K (2 * p + 2))
        (mulS K (2 * (c + 1 + 1)) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1)))
          (r + 1 + (p + 1 - (c + 1 + 1)))))))) := by
  rw [succ_sub_succ' p (c + 1), show r + 1 + 1 + (p - (c + 1)) = r + 1 + (p - (c + 1)) + 1 by lib_nat]
  generalize r + 1 + (p - (c + 1)) = N
  show O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (mulS K (2 * (c + 1) + 1) (K.add (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1)) N)
        (wgt K Q (2 * p + 3 + 2 * N) (2 * (c + 1) + 1))))))
    (K.neg (K.mul (cN K (2 * p + 2))
      (mulS K (2 * (c + 1 + 1)) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1))) N)))))
  rw [mulS_add K, mulS_sum K, mulS_sum K]
  have hs := lin3 K (cN K (2 * p + 3)) (pwR K Q (4 * p + 5)) (cN K (2 * p + 2))
    (fun t => mulS K (2 * (c + 1) + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1)))
    (fun t => mulS K (2 * (c + 1 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1)))) N
  have hg : O.le K.zero (QBinom.sumN K (fun t => K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (mulS K (2 * (c + 1) + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1)))))
      (K.neg (K.mul (cN K (2 * p + 2)) (mulS K (2 * (c + 1 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1))))))) N) :=
    sumN_nn_lt K O _ N (fun t _ => gO_nn K O h1 h10 Q hQ0 h7 p c t hc)
  rw [hs] at hg
  have hx : O.le K.zero (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (mulS K (2 * (c + 1) + 1) (wgt K Q (2 * p + 3 + 2 * N) (2 * (c + 1) + 1))))) :=
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (O.mul_nonneg _ _ (ThetaDup5.pw_nn K O Q hQ0 h1 _)
      (mulS_nn K O _ _ (wgt_nn K O h1 Q hQ0 _ _
        (Nat.le_trans (Nat.add_le_add_right (Nat.mul_le_mul_left 2 hc) 1) (by lib_nat_le)))))
  have := O.add_le_add _ _ _ _ hg hx
  rw [K.zero_add] at this
  refine O.le_trans _ _ _ this (N0Close.le_eq K O _ _ ?_)
  generalize QBinom.sumN K (fun t => mulS K (2 * (c + 1) + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1))) N = sp
  generalize QBinom.sumN K (fun t => mulS K (2 * (c + 1 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1)))) N = sn
  generalize mulS K (2 * (c + 1) + 1) (wgt K Q (2 * p + 3 + 2 * N) (2 * (c + 1) + 1)) = w
  generalize cN K (2 * p + 3) = a; generalize cN K (2 * p + 2) = b; generalize pwR K Q (4 * p + 5) = u
  lib_ring

/-- **6f-A′-2d (odd n = 2p + 1)**: for 0 ≤ Q, 10Q ≤ 7 and every length L = 2(p+1) + (r+1),
    (2p+2)·4q²C_{2p+2}(L) ≤ (2p+3) Q^{4p+5}·4q²C_{2p+1}(L) -/
theorem lr_odd_small (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p r : Nat) :
    O.le (K.mul (cN K (2 * p + 2)) (K.mul (cN K 4)
        (K.mul (cgc K (hl K Q (2 * (p + 1) + (r + 1))) (hl K Q (2 * (p + 1) + (r + 1))) (2 * (p + 1))) (pwR K (K.mul Q Q) 2))))
      (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5)) (K.mul (cN K 4)
        (K.mul (cgc K (hl K Q (2 * (p + 1) + (r + 1))) (hl K Q (2 * (p + 1) + (r + 1))) (2 * p + 1)) (pwR K (K.mul Q Q) 2))))) := by
  have he := HeatAsm.cgc_even K Q (p + 1) r
  rw [show 2 * (p + 1) + 1 + r = 2 * (p + 1) + (r + 1) by lib_nat] at he
  rw [HeatAsmO.cgc_odd K Q p (r + 1), he]
  apply ThetaLow.le_of_sub K O
  rw [QBinom.sumN_shift K _ p, QBinom.sumN_shift K _ (p + 1), QBinom.sumN_shift K _ p]
  have hT := headT_nn K O h1 h10 Q hQ0 h7 p r
  have hD := sumN_nn_lt K O _ p (fun c hc => termO_nn K O h1 h10 Q hQ0 h7 p r c hc)
  rw [lin3 K] at hD
  have := O.add_le_add _ _ _ _ hT hD
  rw [K.zero_add] at this
  refine O.le_trans _ _ _ this (N0Close.le_eq K O _ _ ?_)
  lib_ring

end HeatLRPairO

#print axioms HeatLRPairO.succ_sub_succ'
#print axioms HeatLRPairO.lin4
#print axioms HeatLRPairO.hO_nn
#print axioms HeatLRPairO.gO_nn
#print axioms HeatLRPairO.headT_nn
#print axioms HeatLRPairO.termO_nn
#print axioms HeatLRPairO.lr_odd_small

ym/HeatAsmG.lean

/-
  ym/HeatAsmG — T45, G78m 6d(c) for every truncation length: the heat-product formula of HeatAsm/HeatAsmO without
  the condition L ≥ n + 1 (Nat subtraction in the ranges; short diagonals are empty):
    cgc_evenG   4q²c_{2p}(L)   = Σ_{c≤p} μ_{2c}   Σ_{t < L − (p+c)}   wgt(2p+2+2t, 2c)
    cgc_oddG    4q²c_{2p+1}(L) = Σ_{c≤p} μ_{2c+1} Σ_{t < L − (p+c+1)} wgt(2p+3+2t, 2c+1)
  Tools: win_short (a diagonal shorter than its parity offset is empty), sub_le_add (a − b ≤ e ⇒ a ≤ b + e),
  diag_evenG / diag_oddG (one diagonal at any length), stab (a sum whose terms vanish past N or past L).
  Lean 4 core only, no axioms.
-/
import ym.HeatAsmO

namespace HeatAsmG

open YMChain LinkIntegral SU2Char HeatPair ThetaDup2 CgSplit CgCorr HeatR HeatCg HeatWin HeatAsm MigdalCone

variable {R : Type} (K : DetChain.CRing R)

theorem win_short (D e m : Nat) (w : Nat → R) (hm : m ≤ e) :
    QBinom.sumN K (fun i => K.mul (cN K (lp (D + 2 * e) (i + (i + D)))) (w i)) m = K.zero := by
  rw [ThetaDup3.sumN_ext_lt K _ (fun _ => K.zero) m
    (fun i hi => by rw [lp_even_lo D e i (Nat.lt_of_lt_of_le hi hm)]; show K.mul K.zero (w i) = K.zero; lib_ring),
    ThetaDup2.sumN_zero]

theorem sub_le_add (a b e : Nat) (h : a - b ≤ e) : a ≤ b + e := by
  cases Nat.le_total b a with
  | inl hb =>
    have := LibNatSub.add_sub_of_le hb
    rw [← this]
    exact Nat.add_le_add_left h b
  | inr hb => exact Nat.le_trans hb (Nat.le_add_right b e)

/-- one diagonal of the lp-window at any length m: the tail of w from e, of length m − e -/
theorem win_any (D e m : Nat) (w : Nat → R) :
    QBinom.sumN K (fun i => K.mul (cN K (lp (D + 2 * e) (i + (i + D)))) (w i)) m
      = QBinom.sumN K (fun t => w (e + t)) (m - e) := by
  cases Nat.le_total e m with
  | inl h =>
    obtain ⟨k, hk⟩ := LibNatSub.le_ex h
    subst hk
    rw [win_even K D e k w, LibNatSub.add_sub_l]
  | inr h =>
    rw [win_short K D e m w h, LibNatSub.sub_eq_zero h]
    try rfl

/-- an even diagonal D = 2c of c_{2(c+e)} at any length L -/
theorem diag_evenG (Q : R) (c e L : Nat) :
    QBinom.sumN K (fun i => cgw K Q (2 * (c + e)) (2 * c) i) (L - 2 * c)
      = QBinom.sumN K (fun t => wgt K Q (2 * (c + e) + 2 + 2 * t) (2 * c)) (L - (c + e + c)) := by
  rw [QBinom.sumN_ext K _ (fun i => K.mul (cN K (lp (2 * c + 2 * e) (i + (i + 2 * c))))
      (wgt K Q (i + (i + 2 * c) + 2) (2 * c))) (fun i => by
        show K.mul (cN K (Fusion.cg i (i + 2 * c) (2 * (c + e)))) _ = _
        rw [cg_near i (2 * c) (2 * (c + e)) (le2 c e), show 2 * (c + e) = 2 * c + 2 * e by lib_nat]) _,
    win_any K (2 * c) e (L - 2 * c) (fun i => wgt K Q (i + (i + 2 * c) + 2) (2 * c)),
    LibNatSub.sub_sub', show 2 * c + e = c + e + c by lib_nat]
  exact QBinom.sumN_ext K _ _ (fun t => by
    show wgt K Q (e + t + (e + t + 2 * c) + 2) (2 * c) = _
    rw [show e + t + (e + t + 2 * c) + 2 = 2 * (c + e) + 2 + 2 * t by lib_nat]) _

/-- an odd diagonal D = 2c+1 of c_{2(c+e)+1} at any length L -/
theorem diag_oddG (Q : R) (c e L : Nat) :
    QBinom.sumN K (fun i => cgw K Q (2 * (c + e) + 1) (2 * c + 1) i) (L - (2 * c + 1))
      = QBinom.sumN K (fun t => wgt K Q (2 * (c + e) + 3 + 2 * t) (2 * c + 1)) (L - (c + e + c + 1)) := by
  have hle : 2 * c + 1 ≤ 2 * (c + e) + 1 := Nat.succ_le_succ (le2 c e)
  rw [QBinom.sumN_ext K _ (fun i => K.mul (cN K (lp (2 * c + 1 + 2 * e) (i + (i + (2 * c + 1)))))
      (wgt K Q (i + (i + (2 * c + 1)) + 2) (2 * c + 1))) (fun i => by
        show K.mul (cN K (Fusion.cg i (i + (2 * c + 1)) (2 * (c + e) + 1))) _ = _
        rw [cg_near i (2 * c + 1) _ hle, show 2 * (c + e) + 1 = 2 * c + 1 + 2 * e by lib_nat]) _,
    win_any K (2 * c + 1) e (L - (2 * c + 1)) (fun i => wgt K Q (i + (i + (2 * c + 1)) + 2) (2 * c + 1)),
    LibNatSub.sub_sub', show 2 * c + 1 + e = c + e + c + 1 by lib_nat]
  exact QBinom.sumN_ext K _ _ (fun t => by
    show wgt K Q (e + t + (e + t + (2 * c + 1)) + 2) (2 * c + 1) = _
    rw [show e + t + (e + t + (2 * c + 1)) + 2 = 2 * (c + e) + 3 + 2 * t by lib_nat]) _

/-- a sum whose terms vanish from N on and from L on has the same value up to L and up to N -/
theorem stab (F : Nat → R) (N L : Nat) (h : ∀ D, N ≤ D ∨ L ≤ D → F D = K.zero) :
    QBinom.sumN K F L = QBinom.sumN K F N := by
  cases Nat.le_total L N with
  | inl hl =>
    obtain ⟨k, hk⟩ := LibNatSub.le_ex hl
    subst hk
    rw [ThetaDup4.sumN_split K F L k, sumN_zero_fn K _ (fun i => h (L + i) (Or.inr (Nat.le_add_right L i))) k,
      DetChain.add_zero']
  | inr hl =>
    obtain ⟨k, hk⟩ := LibNatSub.le_ex hl
    subst hk
    rw [ThetaDup4.sumN_split K F N k, sumN_zero_fn K _ (fun i => h (N + i) (Or.inl (Nat.le_add_right N i))) k,
      DetChain.add_zero']

/-- **even n, every length** -/
theorem cgc_evenG (Q : R) (p L : Nat) :
    K.mul (cN K 4) (K.mul (cgc K (hl K Q L) (hl K Q L) (2 * p)) (pwR K (K.mul Q Q) 2))
      = QBinom.sumN K (fun c => mulS K (2 * c)
          (QBinom.sumN K (fun t => wgt K Q (2 * p + 2 + 2 * t) (2 * c)) (L - (p + c)))) (p + 1) := by
  rw [cgc_heat]
  have hz : ∀ D, 2 * p + 1 ≤ D ∨ L ≤ D →
      mulS K D (QBinom.sumN K (fun i => cgw K Q (2 * p) D i) (L - D)) = K.zero := by
    intro D hD
    cases hD with
    | inl h =>
      rw [sumN_zero_fn K _ (fun j => by
        show K.mul (cN K (Fusion.cg j (j + D) (2 * p))) _ = K.zero
        rw [cg_far j _ _ h]
        show K.mul K.zero _ = K.zero
        lib_ring), mulS_zero]
    | inr h => rw [LibNatSub.sub_eq_zero h]; exact mulS_zero K D
  rw [stab K (fun D => mulS K D (QBinom.sumN K (fun i => cgw K Q (2 * p) D i) (L - D))) (2 * p + 1) L hz]
  rw [sumN_succ K _ (2 * p), sumN_eo, sumN_succ K _ p]
  have ho : QBinom.sumN K (fun c => mulS K (2 * c + 1)
      (QBinom.sumN K (fun j => cgw K Q (2 * p) (2 * c + 1) j) (L - (2 * c + 1)))) p = K.zero := by
    rw [ThetaDup3.sumN_ext_lt K _ (fun _ => K.zero) p (fun c hc => by
      match le_ex _ _ hc with
      | ⟨e, he⟩ =>
        show mulS K (2 * c + 1) (QBinom.sumN K (fun j => cgw K Q (2 * p) (2 * c + 1) j) (L - (2 * c + 1))) = K.zero
        rw [he, diag_odd, mulS_zero]), ThetaDup2.sumN_zero]
  have hev : ∀ c, c ≤ p → mulS K (2 * c) (QBinom.sumN K (fun j => cgw K Q (2 * p) (2 * c) j) (L - 2 * c))
      = mulS K (2 * c) (QBinom.sumN K (fun t => wgt K Q (2 * p + 2 + 2 * t) (2 * c)) (L - (p + c))) := by
    intro c hc
    match le_ex _ _ hc with
    | ⟨e, he⟩ => rw [he, diag_evenG]
  rw [ho, ThetaDup3.sumN_ext_lt K _ (fun c => mulS K (2 * c)
      (QBinom.sumN K (fun t => wgt K Q (2 * p + 2 + 2 * t) (2 * c)) (L - (p + c)))) p
      (fun c hc => hev c (Nat.le_of_lt hc))]
  rw [hev p (Nat.le_refl p)]
  generalize QBinom.sumN K (fun c => mulS K (2 * c)
      (QBinom.sumN K (fun t => wgt K Q (2 * p + 2 + 2 * t) (2 * c)) (L - (p + c)))) p = S
  lib_ring

/-- **odd n, every length** -/
theorem cgc_oddG (Q : R) (p L : Nat) :
    K.mul (cN K 4) (K.mul (cgc K (hl K Q L) (hl K Q L) (2 * p + 1)) (pwR K (K.mul Q Q) 2))
      = QBinom.sumN K (fun c => mulS K (2 * c + 1)
          (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)) (L - (p + c + 1)))) (p + 1) := by
  rw [cgc_heat]
  have hz : ∀ D, 2 * (p + 1) ≤ D ∨ L ≤ D →
      mulS K D (QBinom.sumN K (fun i => cgw K Q (2 * p + 1) D i) (L - D)) = K.zero := by
    intro D hD
    cases hD with
    | inl h =>
      have h' : 2 * p + 1 < D :=
        Nat.lt_of_lt_of_le (by rw [show 2 * (p + 1) = 2 * p + 1 + 1 by lib_nat]; exact Nat.lt_succ_self _) h
      rw [sumN_zero_fn K _ (fun j => by
        show K.mul (cN K (Fusion.cg j (j + D) (2 * p + 1))) _ = K.zero
        rw [cg_far j _ _ h']
        show K.mul K.zero _ = K.zero
        lib_ring), mulS_zero]
    | inr h => rw [LibNatSub.sub_eq_zero h]; exact mulS_zero K D
  rw [stab K (fun D => mulS K D (QBinom.sumN K (fun i => cgw K Q (2 * p + 1) D i) (L - D))) (2 * (p + 1)) L hz]
  rw [sumN_eo]
  have he0 : QBinom.sumN K (fun c => mulS K (2 * c)
      (QBinom.sumN K (fun j => cgw K Q (2 * p + 1) (2 * c) j) (L - 2 * c))) (p + 1) = K.zero := by
    rw [ThetaDup3.sumN_ext_lt K _ (fun _ => K.zero) (p + 1) (fun c hc => by
      match le_ex _ _ (Nat.le_of_lt_succ hc) with
      | ⟨e, he⟩ =>
        show mulS K (2 * c) (QBinom.sumN K (fun j => cgw K Q (2 * p + 1) (2 * c) j) (L - 2 * c)) = K.zero
        rw [he, HeatAsmO.diag_evenO, mulS_zero]), ThetaDup2.sumN_zero]
  have hod : ∀ c, c ≤ p → mulS K (2 * c + 1)
      (QBinom.sumN K (fun j => cgw K Q (2 * p + 1) (2 * c + 1) j) (L - (2 * c + 1)))
      = mulS K (2 * c + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)) (L - (p + c + 1))) := by
    intro c hc
    match le_ex _ _ hc with
    | ⟨e, he⟩ => rw [he, diag_oddG]
  rw [he0, K.zero_add]
  exact ThetaDup3.sumN_ext_lt K _ _ (p + 1) (fun c hc => hod c (Nat.le_of_lt_succ hc))

end HeatAsmG

#print axioms HeatAsmG.win_short
#print axioms HeatAsmG.sub_le_add
#print axioms HeatAsmG.win_any
#print axioms HeatAsmG.diag_evenG
#print axioms HeatAsmG.diag_oddG
#print axioms HeatAsmG.stab
#print axioms HeatAsmG.cgc_evenG
#print axioms HeatAsmG.cgc_oddG

ym/HeatLRPairG.lean

/-
  ym/HeatLRPairG — T45, G78m 6f-A′-2d at every truncation length: with the all-L formulas of HeatAsmG,
  for 0 ≤ Q, 10Q ≤ 7, every n and every L,
      (n+1)·4q²C_{n+1}(L) ≤ (n+2) Q^{2n+3}·4q²C_n(L).
  A diagonal whose positive range is 0 is empty; otherwise the negative range is one less (L − (a+1) = (L − a) − 1)
  and the groups of HeatLRPair / HeatLRPairO apply (core lemmas with a free length N).
    termE_core, headO_core, termO_core   one diagonal at length N
    rangeE, headR, rangeO                one diagonal at length L
    lr_even_G, lr_odd_G                  the inequality for n = 2p and n = 2p + 1
  Lean 4 core only, no axioms.
-/
import ym.HeatLRPairO
import ym.HeatAsmG

namespace HeatLRPairG

open YMChain LinkIntegral SU2Char HeatPair ThetaDup2 CgSplit CgCorr HeatR HeatCg HeatWin HeatAsm MigdalCone PairN PairNW
  HeatLRPair HeatLRPairO

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem termE_core (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p c N : Nat) (hc : c ≤ p) :
    O.le K.zero (K.add (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3))
        (mulS K (2 * c) (QBinom.sumN K (fun t => wgt K Q (2 * p + 2 + 2 * t) (2 * c)) (N + 1)))))
      (K.neg (K.mul (cN K (2 * p + 1))
        (mulS K (2 * c + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)) N))))) := by
  show O.le K.zero (K.add (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3))
      (mulS K (2 * c) (K.add (QBinom.sumN K (fun t => wgt K Q (2 * p + 2 + 2 * t) (2 * c)) N)
        (wgt K Q (2 * p + 2 + 2 * N) (2 * c))))))
    (K.neg (K.mul (cN K (2 * p + 1)) (mulS K (2 * c + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)) N)))))
  rw [mulS_add K, mulS_sum K, mulS_sum K]
  have hs := lin3 K (cN K (2 * p + 2)) (pwR K Q (4 * p + 3)) (cN K (2 * p + 1))
    (fun t => mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * t) (2 * c)))
    (fun t => mulS K (2 * c + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1))) N
  have hg : O.le K.zero (QBinom.sumN K (fun t => K.add (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3))
      (mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * t) (2 * c)))))
      (K.neg (K.mul (cN K (2 * p + 1)) (mulS K (2 * c + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)))))) N) :=
    sumN_nn_lt K O _ N (fun t _ => gE_nn K O h1 h10 Q hQ0 h7 p c t hc)
  rw [hs] at hg
  have hx : O.le K.zero (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3)) (mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * N) (2 * c))))) :=
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (O.mul_nonneg _ _ (ThetaDup5.pw_nn K O Q hQ0 h1 _)
      (mulS_nn K O _ _ (wgt_nn K O h1 Q hQ0 _ _ (Nat.le_trans (Nat.mul_le_mul_left 2 hc) (by lib_nat_le)))))
  have := O.add_le_add _ _ _ _ hg hx
  rw [K.zero_add] at this
  refine O.le_trans _ _ _ this (N0Close.le_eq K O _ _ ?_)
  generalize QBinom.sumN K (fun t => mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * t) (2 * c))) N = se
  generalize QBinom.sumN K (fun t => mulS K (2 * c + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1))) N = so
  generalize mulS K (2 * c) (wgt K Q (2 * p + 2 + 2 * N) (2 * c)) = w
  generalize cN K (2 * p + 2) = a; generalize cN K (2 * p + 1) = b; generalize pwR K Q (4 * p + 3) = u
  lib_ring

theorem headO_core (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p M : Nat) :
    O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
        (mulS K (2 * 0 + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1)) (M + 1)))))
      (K.neg (K.mul (cN K (2 * p + 2))
        (K.add (mulS K (2 * 0) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0)) (M + 1)))
          (mulS K (2 * (0 + 1)) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1))) M)))))) := by
  show O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (mulS K (2 * 0 + 1) (K.add (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1)) M)
        (wgt K Q (2 * p + 3 + 2 * M) (2 * 0 + 1))))))
    (K.neg (K.mul (cN K (2 * p + 2))
      (K.add (mulS K (2 * 0) (K.add (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0)) M)
          (wgt K Q (2 * (p + 1) + 2 + 2 * M) (2 * 0))))
        (mulS K (2 * (0 + 1)) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1))) M))))))
  rw [mulS_add K, mulS_add K, mulS_sum K, mulS_sum K, mulS_sum K]
  have hs := lin4 K (cN K (2 * p + 3)) (pwR K Q (4 * p + 5)) (cN K (2 * p + 2))
    (fun t => mulS K (2 * 0 + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1)))
    (fun t => mulS K (2 * 0) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0)))
    (fun t => mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1)))) M
  have hg : O.le K.zero (QBinom.sumN K (fun t => K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (mulS K (2 * 0 + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1)))))
      (K.neg (K.mul (cN K (2 * p + 2)) (K.add (mulS K (2 * 0) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0)))
        (mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1)))))))) M) :=
    sumN_nn_lt K O _ M (fun t _ => hO_nn K O h1 h10 Q hQ0 h7 p t)
  rw [hs] at hg
  have hl := hO_nn K O h1 h10 Q hQ0 h7 p M
  unfold hO at hl
  have hn2 : O.le K.zero (K.mul (cN K (2 * p + 2)) (mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * M) (2 * (0 + 1))))) :=
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (mulS_nn K O _ _ (wgt_nn K O h1 Q hQ0 _ _ (by lib_nat_le)))
  have := O.add_le_add _ _ _ _ hg (O.add_le_add _ _ _ _ hl hn2)
  rw [K.zero_add, K.zero_add] at this
  refine O.le_trans _ _ _ this (N0Close.le_eq K O _ _ ?_)
  generalize QBinom.sumN K (fun t => mulS K (2 * 0 + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1))) M = s1
  generalize QBinom.sumN K (fun t => mulS K (2 * 0) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0))) M = s0
  generalize QBinom.sumN K (fun t => mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1)))) M = s2
  generalize mulS K (2 * 0 + 1) (wgt K Q (2 * p + 3 + 2 * M) (2 * 0 + 1)) = w1
  generalize mulS K (2 * 0) (wgt K Q (2 * (p + 1) + 2 + 2 * M) (2 * 0)) = w0
  generalize mulS K (2 * (0 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * M) (2 * (0 + 1))) = w2
  generalize cN K (2 * p + 3) = a; generalize cN K (2 * p + 2) = b; generalize pwR K Q (4 * p + 5) = u
  lib_ring

theorem termO_core (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p c N : Nat) (hc : c + 1 ≤ p) :
    O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
        (mulS K (2 * (c + 1) + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1)) (N + 1)))))
      (K.neg (K.mul (cN K (2 * p + 2))
        (mulS K (2 * (c + 1 + 1)) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1))) N))))) := by
  show O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (mulS K (2 * (c + 1) + 1) (K.add (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1)) N)
        (wgt K Q (2 * p + 3 + 2 * N) (2 * (c + 1) + 1))))))
    (K.neg (K.mul (cN K (2 * p + 2))
      (mulS K (2 * (c + 1 + 1)) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1))) N)))))
  rw [mulS_add K, mulS_sum K, mulS_sum K]
  have hs := lin3 K (cN K (2 * p + 3)) (pwR K Q (4 * p + 5)) (cN K (2 * p + 2))
    (fun t => mulS K (2 * (c + 1) + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1)))
    (fun t => mulS K (2 * (c + 1 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1)))) N
  have hg : O.le K.zero (QBinom.sumN K (fun t => K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (mulS K (2 * (c + 1) + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1)))))
      (K.neg (K.mul (cN K (2 * p + 2)) (mulS K (2 * (c + 1 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1))))))) N) :=
    sumN_nn_lt K O _ N (fun t _ => gO_nn K O h1 h10 Q hQ0 h7 p c t hc)
  rw [hs] at hg
  have hx : O.le K.zero (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
      (mulS K (2 * (c + 1) + 1) (wgt K Q (2 * p + 3 + 2 * N) (2 * (c + 1) + 1))))) :=
    O.mul_nonneg _ _ (SU2CharExp.cN_nn K O h1 _) (O.mul_nonneg _ _ (ThetaDup5.pw_nn K O Q hQ0 h1 _)
      (mulS_nn K O _ _ (wgt_nn K O h1 Q hQ0 _ _
        (Nat.le_trans (Nat.add_le_add_right (Nat.mul_le_mul_left 2 hc) 1) (by lib_nat_le)))))
  have := O.add_le_add _ _ _ _ hg hx
  rw [K.zero_add] at this
  refine O.le_trans _ _ _ this (N0Close.le_eq K O _ _ ?_)
  generalize QBinom.sumN K (fun t => mulS K (2 * (c + 1) + 1) (wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1))) N = sp
  generalize QBinom.sumN K (fun t => mulS K (2 * (c + 1 + 1)) (wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1)))) N = sn
  generalize mulS K (2 * (c + 1) + 1) (wgt K Q (2 * p + 3 + 2 * N) (2 * (c + 1) + 1)) = w
  generalize cN K (2 * p + 3) = a; generalize cN K (2 * p + 2) = b; generalize pwR K Q (4 * p + 5) = u
  lib_ring

theorem sub_succ (L a : Nat) : L - (a + 1) = L - a - 1 := (LibNatSub.sub_sub' L a 1).symm

/-- even n, one diagonal at length L -/
theorem rangeE (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p L c : Nat) (hc : c ≤ p) :
    O.le K.zero (K.add (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3))
        (mulS K (2 * c) (QBinom.sumN K (fun t => wgt K Q (2 * p + 2 + 2 * t) (2 * c)) (L - (p + c))))))
      (K.neg (K.mul (cN K (2 * p + 1))
        (mulS K (2 * c + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * c + 1)) (L - (p + c + 1))))))) := by
  rw [sub_succ L (p + c)]
  cases L - (p + c) with
  | zero =>
    show O.le K.zero (K.add (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3)) (mulS K (2 * c) K.zero)))
      (K.neg (K.mul (cN K (2 * p + 1)) (mulS K (2 * c + 1) K.zero))))
    rw [mulS_zero, mulS_zero]
    exact N0Close.le_eq K O _ _ (by lib_ring)
  | succ N =>
    rw [LibNatSub.add_sub_r N 1]
    exact termE_core K O h1 h10 Q hQ0 h7 p c N hc

/-- odd n, the head diagonal at length L -/
theorem headR (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p L : Nat) :
    O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
        (mulS K (2 * 0 + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * 0 + 1)) (L - (p + 0 + 1))))))
      (K.neg (K.mul (cN K (2 * p + 2))
        (K.add (mulS K (2 * 0) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * 0)) (L - (p + 1 + 0))))
          (mulS K (2 * (0 + 1)) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (0 + 1)))
            (L - (p + 1 + (0 + 1))))))))) := by
  rw [show p + 1 + 0 = p + 0 + 1 by lib_nat, show p + 1 + (0 + 1) = p + 0 + 1 + 1 by lib_nat, sub_succ L (p + 0 + 1)]
  cases L - (p + 0 + 1) with
  | zero =>
    show O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5)) (mulS K (2 * 0 + 1) K.zero)))
      (K.neg (K.mul (cN K (2 * p + 2)) (K.add (mulS K (2 * 0) K.zero) (mulS K (2 * (0 + 1)) K.zero)))))
    rw [mulS_zero, mulS_zero, mulS_zero]
    exact N0Close.le_eq K O _ _ (by lib_ring)
  | succ M =>
    rw [LibNatSub.add_sub_r M 1]
    exact headO_core K O h1 h10 Q hQ0 h7 p M

/-- odd n, a diagonal c + 1 ≤ p at length L -/
theorem rangeO (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p L c : Nat) (hc : c + 1 ≤ p) :
    O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5))
        (mulS K (2 * (c + 1) + 1) (QBinom.sumN K (fun t => wgt K Q (2 * p + 3 + 2 * t) (2 * (c + 1) + 1))
          (L - (p + (c + 1) + 1))))))
      (K.neg (K.mul (cN K (2 * p + 2))
        (mulS K (2 * (c + 1 + 1)) (QBinom.sumN K (fun t => wgt K Q (2 * (p + 1) + 2 + 2 * t) (2 * (c + 1 + 1)))
          (L - (p + 1 + (c + 1 + 1)))))))) := by
  rw [show p + 1 + (c + 1 + 1) = p + (c + 1) + 1 + 1 by lib_nat, sub_succ L (p + (c + 1) + 1)]
  cases L - (p + (c + 1) + 1) with
  | zero =>
    show O.le K.zero (K.add (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5)) (mulS K (2 * (c + 1) + 1) K.zero)))
      (K.neg (K.mul (cN K (2 * p + 2)) (mulS K (2 * (c + 1 + 1)) K.zero))))
    rw [mulS_zero, mulS_zero]
    exact N0Close.le_eq K O _ _ (by lib_ring)
  | succ N =>
    rw [LibNatSub.add_sub_r N 1]
    exact termO_core K O h1 h10 Q hQ0 h7 p c N hc

/-- **6f-A′-2d (even n = 2p), every length L** -/
theorem lr_even_G (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p L : Nat) :
    O.le (K.mul (cN K (2 * p + 1)) (K.mul (cN K 4)
        (K.mul (cgc K (hl K Q L) (hl K Q L) (2 * p + 1)) (pwR K (K.mul Q Q) 2))))
      (K.mul (cN K (2 * p + 2)) (K.mul (pwR K Q (4 * p + 3)) (K.mul (cN K 4)
        (K.mul (cgc K (hl K Q L) (hl K Q L) (2 * p)) (pwR K (K.mul Q Q) 2))))) := by
  rw [HeatAsmG.cgc_evenG K Q p L, HeatAsmG.cgc_oddG K Q p L]
  apply ThetaLow.le_of_sub K O
  rw [← lin3 K (cN K (2 * p + 2)) (pwR K Q (4 * p + 3)) (cN K (2 * p + 1))]
  exact sumN_nn_lt K O _ (p + 1) (fun c hc => rangeE K O h1 h10 Q hQ0 h7 p L c (Nat.le_of_lt_succ hc))

/-- **6f-A′-2d (odd n = 2p + 1), every length L** -/
theorem lr_odd_G (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (p L : Nat) :
    O.le (K.mul (cN K (2 * p + 2)) (K.mul (cN K 4)
        (K.mul (cgc K (hl K Q L) (hl K Q L) (2 * (p + 1))) (pwR K (K.mul Q Q) 2))))
      (K.mul (cN K (2 * p + 3)) (K.mul (pwR K Q (4 * p + 5)) (K.mul (cN K 4)
        (K.mul (cgc K (hl K Q L) (hl K Q L) (2 * p + 1)) (pwR K (K.mul Q Q) 2))))) := by
  rw [HeatAsmG.cgc_evenG K Q (p + 1) L, HeatAsmG.cgc_oddG K Q p L]
  apply ThetaLow.le_of_sub K O
  rw [QBinom.sumN_shift K _ p, QBinom.sumN_shift K _ (p + 1), QBinom.sumN_shift K _ p]
  have hT := headR K O h1 h10 Q hQ0 h7 p L
  have hD := sumN_nn_lt K O _ p (fun c hc => rangeO K O h1 h10 Q hQ0 h7 p L c hc)
  rw [lin3 K] at hD
  have := O.add_le_add _ _ _ _ hT hD
  rw [K.zero_add] at this
  refine O.le_trans _ _ _ this (N0Close.le_eq K O _ _ ?_)
  lib_ring

end HeatLRPairG

#print axioms HeatLRPairG.termE_core
#print axioms HeatLRPairG.headO_core
#print axioms HeatLRPairG.termO_core
#print axioms HeatLRPairG.sub_succ
#print axioms HeatLRPairG.rangeE
#print axioms HeatLRPairG.headR
#print axioms HeatLRPairG.rangeO
#print axioms HeatLRPairG.lr_even_G
#print axioms HeatLRPairG.lr_odd_G

ym/HeatLR7.lean

/-
  ym/HeatLR7 — T45, G56/G60 (b) raised: the heat-product LR comparison of HeatLRUpperK in the forms used by the
  ladder, now for 0 < Q, 10Q ≤ 7 (was 2Q ≤ 1), at every truncation L ≥ n + 2:
    c_eq       cgc(hl Q L, hl Q L, n) = C(hH Q, L, n)
    upper7     (n+1) C_{n+1}(L) ≤ (n+2) Q^{2n+3} C_n(L)          (HeatLRPair / HeatLRPairO, 4q² cancelled)
    lr_upper7  LRat (C(hH Q, L)) K_Q n,  K_Q(n) = (n+1) Q^{n(n+2)}
  Lean 4 core only, no axioms.
-/
import ym.HeatLRPairO
import ym.HeatLRUpperK
import ym.SecondRingStep

namespace HeatLR7

open YMChain LinkIntegral SU2Char HeatR MigdalCone SecondRingC1

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem pw_sq (Q : R) (m : Nat) : pwR K (K.mul Q Q) m = pwR K Q (m + m) := by
  rw [GKS2.pwR_mul K, GKS.pwR_add K]

theorem c_eq (Q : R) (L n : Nat) : cgc K (hl K Q L) (hl K Q L) n = C K (hH K Q) L n := by
  rw [SecondRingStep.cgc_eq K, len_hl K]
  unfold C
  apply SecondRing.sumN_ext_lt K _ _ L
  intro j hj
  apply SecondRing.sumN_ext_lt K _ _ L
  intro k hk
  rw [co_hl K Q L j hj, co_hl K Q L k hk]
  unfold Hh hH
  rw [pw_sq K Q (j * (j + 2)), pw_sq K Q (k * (k + 2))]

theorem parity : ∀ n : Nat, ∃ p, n = 2 * p ∨ n = 2 * p + 1
  | 0 => ⟨0, Or.inl rfl⟩
  | n + 1 => by
    obtain ⟨p, h | h⟩ := parity n
    · exact ⟨p, Or.inr (by rw [h])⟩
    · exact ⟨p + 1, Or.inl (by rw [h]; lib_nat)⟩

theorem nz_mul (a b : R) (ha : O.le K.zero a) (hb : O.le K.zero b) (ha0 : a ≠ K.zero) (hb0 : b ≠ K.zero) :
    K.mul a b ≠ K.zero := fun h => hb0 (O.pos_mul_eq_zero _ _ ha ha0 h)

/-- **heat-lr upper, Q ≤ 0.7**: (n+1) C_{n+1}(L) ≤ (n+2) Q^{2n+3} C_n(L) for 0 < Q, 10Q ≤ 7, L ≥ n + 2 -/
theorem upper7 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q) (hQn : Q ≠ K.zero)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (n L : Nat) (hL : n + 2 ≤ L) :
    O.le (K.mul (cN K (n + 1)) (C K (hH K Q) L (n + 1)))
      (K.mul (cN K (n + 2)) (K.mul (pwR K Q (2 * n + 3)) (C K (hH K Q) L n))) := by
  have qq0 : O.le K.zero (K.mul Q Q) := O.mul_nonneg _ _ hQ0 hQ0
  have qqn : K.mul Q Q ≠ K.zero := nz_mul K O Q Q hQ0 hQ0 hQn hQn
  have q2 : pwR K (K.mul Q Q) 2 = K.mul (K.mul K.one (K.mul Q Q)) (K.mul Q Q) := rfl
  have c4 := N0CellD.one_le_cN K O h1 4 (by decide)
  have c4n := N0CellD.ne_of_one_le K O h1 h10 _ c4
  have c0 : O.le K.zero (K.mul (cN K 4) (pwR K (K.mul Q Q) 2)) :=
    O.mul_nonneg _ _ (O.le_trans _ _ _ h1 c4) (ThetaDup5.pw_nn K O _ qq0 h1 2)
  have cn : K.mul (cN K 4) (pwR K (K.mul Q Q) 2) ≠ K.zero := by
    rw [q2, K.one_mul]
    exact nz_mul K O _ _ (O.le_trans _ _ _ h1 c4) (O.mul_nonneg _ _ qq0 qq0) c4n (nz_mul K O _ _ qq0 qq0 qqn qqn)
  obtain ⟨r, hr⟩ := LibNatSub.le_ex hL
  subst hr
  obtain ⟨p, hp | hp⟩ := parity n
  · subst hp
    have h := HeatLRPair.lr_even_small K O h1 h10 Q hQ0 h7 p r
    rw [show 2 * p + 1 + (r + 1) = 2 * p + 2 + r by lib_nat, c_eq K, c_eq K] at h
    rw [show 2 * (2 * p) + 3 = 4 * p + 3 by lib_nat]
    apply O.pos_cancel _ _ _ c0 cn
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ h (N0Close.le_eq K O _ _ ?_))
    · generalize C K (hH K Q) (2 * p + 2 + r) (2 * p + 1) = a; generalize pwR K (K.mul Q Q) 2 = w
      generalize cN K 4 = d; generalize cN K (2 * p + 1) = b; lib_ring
    · generalize C K (hH K Q) (2 * p + 2 + r) (2 * p) = a; generalize pwR K Q (4 * p + 3) = u
      generalize pwR K (K.mul Q Q) 2 = w; generalize cN K 4 = d; generalize cN K (2 * p + 2) = b; lib_ring
  · subst hp
    have h := HeatLRPairO.lr_odd_small K O h1 h10 Q hQ0 h7 p r
    rw [show 2 * (p + 1) + (r + 1) = 2 * p + 1 + 2 + r by lib_nat, c_eq K, c_eq K] at h
    have h' : O.le (K.mul (cN K (2 * p + 1 + 1)) (K.mul (cN K 4)
        (K.mul (C K (hH K Q) (2 * p + 1 + 2 + r) (2 * p + 1 + 1)) (pwR K (K.mul Q Q) 2))))
      (K.mul (cN K (2 * p + 1 + 2)) (K.mul (pwR K Q (4 * p + 5)) (K.mul (cN K 4)
        (K.mul (C K (hH K Q) (2 * p + 1 + 2 + r) (2 * p + 1)) (pwR K (K.mul Q Q) 2))))) := h
    rw [show 2 * (2 * p + 1) + 3 = 4 * p + 5 by lib_nat]
    apply O.pos_cancel _ _ _ c0 cn
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ h' (N0Close.le_eq K O _ _ ?_))
    · generalize C K (hH K Q) (2 * p + 1 + 2 + r) (2 * p + 1 + 1) = a; generalize pwR K (K.mul Q Q) 2 = w
      generalize cN K 4 = d; generalize cN K (2 * p + 1 + 1) = b; lib_ring
    · generalize C K (hH K Q) (2 * p + 1 + 2 + r) (2 * p + 1) = a; generalize pwR K Q (4 * p + 5) = u
      generalize pwR K (K.mul Q Q) 2 = w; generalize cN K 4 = d; generalize cN K (2 * p + 1 + 2) = b; lib_ring

/-- **G60 (b) at Q ≤ 0.7**: H·H ≼_LR K_Q at every n and every truncation L ≥ n + 2 -/
theorem lr_upper7 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q) (hQn : Q ≠ K.zero)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (n L : Nat) (hL : n + 2 ≤ L) :
    DecLR.LRat K O (C K (hH K Q) L) (HeatLRUpperK.kQ K Q) n := by
  unfold DecLR.LRat HeatLRUpperK.kQ
  have u := upper7 K O h1 h10 Q hQ0 hQn h7 n L hL
  have p0 := AreaLower.pw_nn K O Q hQ0 h1 (n * (n + 2))
  have v := O.mul_le_mul_right _ _ (pwR K Q (n * (n + 2))) u p0
  have eE : (n + 1) * (n + 1 + 2) = n * (n + 2) + (2 * n + 3) := by lib_nat
  rw [eE, GKS.pwR_add K Q (n * (n + 2)) (2 * n + 3), show n + 1 + 1 = n + 2 from rfl]
  rw [show K.mul (K.mul (cN K (n + 1)) (C K (hH K Q) L (n + 1))) (pwR K Q (n * (n + 2)))
      = K.mul (C K (hH K Q) L (n + 1)) (K.mul (cN K (n + 1)) (pwR K Q (n * (n + 2)))) by lib_ring,
    show K.mul (K.mul (cN K (n + 2)) (K.mul (pwR K Q (2 * n + 3)) (C K (hH K Q) L n))) (pwR K Q (n * (n + 2)))
      = K.mul (C K (hH K Q) L n) (K.mul (cN K (n + 2)) (K.mul (pwR K Q (n * (n + 2))) (pwR K Q (2 * n + 3)))) by lib_ring] at v
  exact v

end HeatLR7

#print axioms HeatLR7.pw_sq
#print axioms HeatLR7.c_eq
#print axioms HeatLR7.parity
#print axioms HeatLR7.nz_mul
#print axioms HeatLR7.upper7
#print axioms HeatLR7.lr_upper7

ym/HeatLR7G.lean

/-
  ym/HeatLR7G — T45, G60 raised to Q ≤ 0.7 at every truncation: with HeatLRPairG,
    upper7G        (n+1) C_{n+1}(L) ≤ (n+2) Q^{2n+3} C_n(L)      (0 < Q, 10Q ≤ 7, every n, L)
    lr_upper7G     LRat (C(hH Q, L)) K_Q n
    first_step7    HeatFirstStep.first_step for 0 < Q, 10Q ≤ 7 (was 2Q ≤ 1): a v-good X = K_{Q²}·g of length M+2
                   (g nonincreasing) has X·X ≼_LR K_Q at every level
  Lean 4 core only, no axioms.
-/
import ym.HeatLRPairG
import ym.HeatLR7
import ym.HeatFirstStep

namespace HeatLR7G

open YMChain LinkIntegral SU2Char HeatPair HeatR MigdalCone SecondRingC1 HeatLR7

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

theorem upper7G (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q) (hQn : Q ≠ K.zero)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (n L : Nat) :
    O.le (K.mul (cN K (n + 1)) (C K (hH K Q) L (n + 1)))
      (K.mul (cN K (n + 2)) (K.mul (pwR K Q (2 * n + 3)) (C K (hH K Q) L n))) := by
  have qq0 : O.le K.zero (K.mul Q Q) := O.mul_nonneg _ _ hQ0 hQ0
  have qqn : K.mul Q Q ≠ K.zero := nz_mul K O Q Q hQ0 hQ0 hQn hQn
  have q2 : pwR K (K.mul Q Q) 2 = K.mul (K.mul K.one (K.mul Q Q)) (K.mul Q Q) := rfl
  have c4 := N0CellD.one_le_cN K O h1 4 (by decide)
  have c4n := N0CellD.ne_of_one_le K O h1 h10 _ c4
  have c0 : O.le K.zero (K.mul (cN K 4) (pwR K (K.mul Q Q) 2)) :=
    O.mul_nonneg _ _ (O.le_trans _ _ _ h1 c4) (ThetaDup5.pw_nn K O _ qq0 h1 2)
  have cn : K.mul (cN K 4) (pwR K (K.mul Q Q) 2) ≠ K.zero := by
    rw [q2, K.one_mul]
    exact nz_mul K O _ _ (O.le_trans _ _ _ h1 c4) (O.mul_nonneg _ _ qq0 qq0) c4n (nz_mul K O _ _ qq0 qq0 qqn qqn)
  obtain ⟨p, hp | hp⟩ := parity n
  · subst hp
    have h := HeatLRPairG.lr_even_G K O h1 h10 Q hQ0 h7 p L
    rw [c_eq K, c_eq K] at h
    rw [show 2 * (2 * p) + 3 = 4 * p + 3 by lib_nat]
    apply O.pos_cancel _ _ _ c0 cn
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ h (N0Close.le_eq K O _ _ ?_))
    · generalize C K (hH K Q) L (2 * p + 1) = a; generalize pwR K (K.mul Q Q) 2 = w
      generalize cN K 4 = d; generalize cN K (2 * p + 1) = b; lib_ring
    · generalize C K (hH K Q) L (2 * p) = a; generalize pwR K Q (4 * p + 3) = u
      generalize pwR K (K.mul Q Q) 2 = w; generalize cN K 4 = d; generalize cN K (2 * p + 2) = b; lib_ring
  · subst hp
    have h := HeatLRPairG.lr_odd_G K O h1 h10 Q hQ0 h7 p L
    rw [c_eq K, c_eq K] at h
    have h' : O.le (K.mul (cN K (2 * p + 1 + 1)) (K.mul (cN K 4)
        (K.mul (C K (hH K Q) L (2 * p + 1 + 1)) (pwR K (K.mul Q Q) 2))))
      (K.mul (cN K (2 * p + 1 + 2)) (K.mul (pwR K Q (4 * p + 5)) (K.mul (cN K 4)
        (K.mul (C K (hH K Q) L (2 * p + 1)) (pwR K (K.mul Q Q) 2))))) := h
    rw [show 2 * (2 * p + 1) + 3 = 4 * p + 5 by lib_nat]
    apply O.pos_cancel _ _ _ c0 cn
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ h' (N0Close.le_eq K O _ _ ?_))
    · generalize C K (hH K Q) L (2 * p + 1 + 1) = a; generalize pwR K (K.mul Q Q) 2 = w
      generalize cN K 4 = d; generalize cN K (2 * p + 1 + 1) = b; lib_ring
    · generalize C K (hH K Q) L (2 * p + 1) = a; generalize pwR K Q (4 * p + 5) = u
      generalize pwR K (K.mul Q Q) 2 = w; generalize cN K 4 = d; generalize cN K (2 * p + 1 + 2) = b; lib_ring

theorem lr_upper7G (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q) (hQn : Q ≠ K.zero)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) (L n : Nat) :
    DecLR.LRat K O (C K (hH K Q) L) (HeatLRUpperK.kQ K Q) n := by
  unfold DecLR.LRat HeatLRUpperK.kQ
  have u := upper7G K O h1 h10 Q hQ0 hQn h7 n L
  have p0 := AreaLower.pw_nn K O Q hQ0 h1 (n * (n + 2))
  have v := O.mul_le_mul_right _ _ (pwR K Q (n * (n + 2))) u p0
  have eE : (n + 1) * (n + 1 + 2) = n * (n + 2) + (2 * n + 3) := by lib_nat
  rw [eE, GKS.pwR_add K Q (n * (n + 2)) (2 * n + 3), show n + 1 + 1 = n + 2 from rfl]
  rw [show K.mul (K.mul (cN K (n + 1)) (C K (hH K Q) L (n + 1))) (pwR K Q (n * (n + 2)))
      = K.mul (C K (hH K Q) L (n + 1)) (K.mul (cN K (n + 1)) (pwR K Q (n * (n + 2)))) by lib_ring,
    show K.mul (K.mul (cN K (n + 2)) (K.mul (pwR K Q (2 * n + 3)) (C K (hH K Q) L n))) (pwR K Q (n * (n + 2)))
      = K.mul (C K (hH K Q) L n) (K.mul (cN K (n + 2)) (K.mul (pwR K Q (n * (n + 2))) (pwR K Q (2 * n + 3)))) by lib_ring] at v
  exact v

/-- 10Q ≤ 7 ⇒ 2 (Q²)² ≤ 1 -/
theorem q4 (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (h7 : O.le (K.mul (cN K 10) Q) (cN K 7)) : O.le (K.mul (cN K 2) (pwR K (pwR K Q 2) 2)) K.one := by
  have f := PairN.small_facts K O h1 h10 Q hQ0 h7
  have e : pwR K (pwR K Q 2) 2 = K.mul (pwR K Q 3) Q := by
    show K.mul (K.mul K.one (K.mul (K.mul K.one Q) Q)) (K.mul (K.mul K.one Q) Q)
      = K.mul (K.mul (K.mul (K.mul K.one Q) Q) Q) Q
    lib_ring
  rw [e]
  have p3 := ThetaDup5.pw_nn K O Q hQ0 h1 3
  have a := N0CellC.mul_le_l K O _ _ _ p3 f.1
  rw [show K.mul (pwR K Q 3) K.one = pwR K Q 3 by lib_ring] at a
  have c5 := N0CellD.one_le_cN K O h1 5 (by decide)
  apply O.pos_cancel _ _ (cN K 5) (O.le_trans _ _ _ h1 c5) (N0CellD.ne_of_one_le K O h1 h10 _ c5)
  have b := N0CellC.mul_le_l K O _ _ _ (SU2CharExp.cN_nn K O h1 2) (O.le_trans _ _ _
    (N0CellC.mul_le_l K O _ _ _ (SU2CharExp.cN_nn K O h1 5) a) f.2.1)
  refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ b
    (O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ (SU2CharExp.cN_mono K O h1 4 5 (by decide))
      (N0Close.le_eq K O _ _ (K.one_mul _).symm))))
  · generalize cN K 2 = a2; generalize cN K 5 = a5; generalize pwR K Q 3 = u; lib_ring
  · exact (SU2CharExp.cN_mul K 2 2).symm

/-- **G60 (a) at Q ≤ 0.7**: one product of a weight below K_{Q²} lies below K_Q, at every level -/
theorem first_step7 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (Q : R) (hQ0 : O.le K.zero Q)
    (hQne : Q ≠ K.zero) (h7 : O.le (K.mul (cN K 10) Q) (cN K 7))
    (Xl : List R) (hXg : L1Full.Good K O Xl) (M : Nat) (hX : Xl.length = M + 2)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K (HeatDec.heat K (pwR K Q 2) 0 (M + 2)) k) (g k)) (n : Nat) :
    DecLR.LRat K O (cgc K Xl Xl) (HeatLRUpperK.kQ K Q) n := by
  have ⟨xb, _, _, _⟩ := hXg
  have hQ1 : O.le Q K.one := (PairN.small_facts K O h1 h10 Q hQ0 h7).1
  have q0 := AreaLower.pw_nn K O Q hQ0 h1 2
  have q1 : O.le (pwR K Q 2) K.one := Tail.pw_le_one K O h1 Q hQ0 hQ1 2
  have qne := HeatGood.pw_ne K O h1 Q hQ0 hQne h10 2
  have hq2 := q4 K O h1 h10 Q hQ0 h7
  have Cnn := fun t => SecondRingA1.C_nn K O h1 (SecondRingC1.hH K Q) (SecondRingC1.hH_nn K O h1 Q hQ0) (M + 2) t
  have xxn := fun t => HeatL1All.cgc_nn K O Xl Xl (M + 1) hX xb xb t
  cases Nat.lt_or_ge (n + 1) (2 * M + 3) with
  | inl h =>
    have h1' := HeatL1All.heat_first K O F h1 h10 (pwR K Q 2) q0 q1 qne hq2 Xl hXg M hX g hg hg0 hxy n
    rw [HeatFirstStep.heat_C, HeatFirstStep.heat_C] at h1'
    exact HeatFirstStep.lr_at K O (cgc K Xl Xl) (SecondRingC1.C K (SecondRingC1.hH K Q) (M + 2)) (HeatLRUpperK.kQ K Q) n
      (Cnn n) (Cnn (n + 1))
      (HeatFirstStep.C_ne K O h1 h10 Q hQ0 hQne M n (Nat.le_of_succ_le (Nat.le_of_lt_succ h)))
      (HeatFirstStep.C_ne K O h1 h10 Q hQ0 hQne M (n + 1) (Nat.le_of_lt_succ h))
      (xxn n) (HeatLRUpperK.kQ_nn K O Q h1 hQ0 n) h1'
      (lr_upper7G K O h1 h10 Q hQ0 hQne h7 (M + 2) n)
  | inr h =>
    unfold DecLR.LRat
    rw [L1Full.cgc_far K Xl M hX (n + 1) h, show K.mul K.zero (HeatLRUpperK.kQ K Q n) = K.zero by lib_ring]
    exact O.mul_nonneg _ _ (xxn n) (HeatLRUpperK.kQ_nn K O Q h1 hQ0 (n + 1))

end HeatLR7G

#print axioms HeatLR7G.upper7G
#print axioms HeatLR7G.lr_upper7G
#print axioms HeatLR7G.q4
#print axioms HeatLR7G.first_step7

ym/HeatSecond7.lean

/-
  ym/HeatSecond7 — T45, G60 (c) raised to P ≤ 0.7: two bond-moving products of a weight below K_{P⁴} lie below K_P
  for 0 < P, 10P ≤ 7 (was 2P ≤ 1), via HeatLR7G.first_step7 at P² and at P.
  Lean 4 core only, no axioms.
-/
import ym.HeatLR7G
import ym.HeatSecondStep

namespace HeatSecond7

open YMChain LinkIntegral SU2Char QBinom HeatPair MigdalCone

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

/-- **G60 (c) at P ≤ 0.7**: two products of a weight below K_{P⁴} lie below K_P -/
theorem two_products7 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (P : R) (hP0 : O.le K.zero P)
    (hPne : P ≠ K.zero) (h7 : O.le (K.mul (cN K 10) P) (cN K 7))
    (Xl : List R) (hXg : L1Full.Good K O Xl) (M : Nat) (hX : Xl.length = M + 2)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) k) (g k)) (n : Nat) :
    DecLR.LRat K O (cgc K (L1Full.prodL K Xl M) (L1Full.prodL K Xl M)) (HeatLRUpperK.kQ K P) n := by
  have nn := fun k => SU2CharExp.cN_nn K O h1 k
  have P20 := AreaLower.pw_nn K O P hP0 h1 2
  have P2ne := HeatGood.pw_ne K O h1 P hP0 hPne h10 2
  have hP1 : O.le P K.one := (PairN.small_facts K O h1 h10 P hP0 h7).1
  -- 10P² ≤ 7
  have h72 : O.le (K.mul (cN K 10) (pwR K P 2)) (cN K 7) := by
    have a := O.mul_le_mul_right _ _ P h7 hP0
    have b := N0CellC.mul_le_l K O _ _ _ (nn 7) hP1
    refine O.le_trans _ _ _ (N0Close.le_eq K O _ _ ?_) (O.le_trans _ _ _ a (O.le_trans _ _ _ b (N0Close.le_eq K O _ _ ?_)))
    · show K.mul (cN K 10) (K.mul (K.mul K.one P) P) = _
      generalize cN K 10 = c; lib_ring
    · generalize cN K 7 = c; lib_ring
  -- step 1: X·X ≼ K_{P²}
  have s1 := HeatLR7G.first_step7 K O F h1 h10 (pwR K P 2) P20 P2ne h72 Xl hXg M hX g hg hg0 hxy
  -- Y = X·X as a list, v-good, length 2M+3
  have hYg := L1Full.Good_prod K O Xl hXg M hX F
  have hYl := HeatSecondStep.len_prodL K Xl M
  have coY := L1Full.co_prodL K Xl M hX
  -- B = K_{P²} truncated to length 2M+3
  have hBl := SecondRing.len_heat K (pwR K P 2) (2 * M + 3) 0
  have Bco : ∀ k, k < 2 * M + 3 → co K (HeatDec.heat K (pwR K P 2) 0 (2 * M + 3)) k = HeatLRUpperK.kQ K (pwR K P 2) k :=
    fun k hk => by rw [HeatGood.coY K (pwR K P 2) (2 * M + 3) k hk]; rfl
  have ⟨yb, _, _, _⟩ := hYg
  have fac := HeatSecondStep.lr_factor K O F (L1Full.prodL K Xl M) (HeatDec.heat K (pwR K P 2) 0 (2 * M + 3)) (2 * M + 3) hYl hBl yb
    (HeatGood.co_nn K O h1 (pwR K P 2) P20 (2 * M + 3))
    (fun k hk => by
      rw [HeatGood.coY K (pwR K P 2) (2 * M + 3) k hk]; unfold HalfRatio.Yh; intro e
      exact HeatGood.pw_ne K O h1 (pwR K P 2) P20 P2ne h10 _ (O.pos_mul_eq_zero _ _ (nn _) (GridQ.cN_pos K O h1 h10 k) e))
    (fun m hm => by
      have t := s1 m
      unfold DecLR.LRat at t ⊢
      rw [coY (m + 1), coY m, Bco m (Nat.lt_of_succ_lt hm), Bco (m + 1) hm]
      exact t)
  obtain ⟨fx, fn, fd⟩ := fac
  exact HeatLR7G.first_step7 K O F h1 h10 P hP0 hPne h7 (L1Full.prodL K Xl M) hYg (2 * M + 1) hYl _ fd fn fx n

end HeatSecond7

#print axioms HeatSecond7.two_products7

ym/Flow7.lean

/-
  ym/Flow7 — T45, G60 (heat-lr-mk, heat-lr-mk-iter) raised to P ≤ 0.7: one Migdal–Kadanoff step keeps a v-good weight
  below the heat list K_q (q = P⁴), and the 4D MK flow from the heat-kernel weight stays in the class at every step,
  for 0 < P, 10P ≤ 7 (was 2P ≤ 1). Same proofs as HeatMKClass.mk_class, FlowIter.flow_iter, FlowHeat.flow_heat with
  HeatSecond7.two_products7 in place of HeatSecondStep.two_products.
  Lean 4 core only, no axioms.
-/
import ym.HeatSecond7
import ym.FlowHeat

namespace Flow7

open YMChain

variable {R : Type} (K : DetChain.CRing R) (O : ORing K)

section
open YMChain LinkIntegral SU2Char QBinom HeatPair MigdalCone

theorem mk_class7 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (P : R) (hP0 : O.le K.zero P)
    (hPne : P ≠ K.zero) (h7 : O.le (K.mul (cN K 10) P) (cN K 7))
    (Xl : List R) (hXg : L1Full.Good K O Xl) (M : Nat) (hX : Xl.length = M + 2)
    (g : Nat → R) (hg : ∀ m, O.le (g (m + 1)) (g m)) (hg0 : ∀ m, O.le K.zero (g m))
    (hxy : ∀ k, co K Xl k = K.mul (co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) k) (g k)) (n : Nat) :
    O.le (K.mul (co K (decM K O F 0 (L1Full.prodL K (L1Full.prodL K Xl M) (2 * M + 1))) (n + 1))
        (co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (4 * M + 5)) n))
      (K.mul (co K (decM K O F 0 (L1Full.prodL K (L1Full.prodL K Xl M) (2 * M + 1))) n)
        (co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (4 * M + 5)) (n + 1))) := by
  have ⟨xb, _, _, _⟩ := hXg
  have hYl := HeatSecondStep.len_prodL K (L1Full.prodL K Xl M) (2 * M + 1)
  have hY1 := HeatSecondStep.len_prodL K Xl M
  have coA := L1Full.co_prodL K (L1Full.prodL K Xl M) (2 * M + 1) hY1
  have hYg := L1Full.Good_prod K O Xl hXg M hX F
  have ⟨yb, _, _, _⟩ := hYg
  have Ann : ∀ t, O.le K.zero (co K (L1Full.prodL K (L1Full.prodL K Xl M) (2 * M + 1)) t) := fun t => by
    rw [coA t]; exact HeatL1All.cgc_nn K O _ _ (2 * M + 2) hY1 yb yb t
  have tp := HeatSecond7.two_products7 K O F h1 h10 P hP0 hPne h7 Xl hXg M hX g hg hg0 hxy
  have L5 : 2 * (2 * M + 1) + 3 = 4 * M + 5 := by lib_nat
  have h : ∀ m, O.le (K.mul (co K (L1Full.prodL K (L1Full.prodL K Xl M) (2 * M + 1)) (m + 1)) (co K (HeatDec.heat K P 0 (4 * M + 5)) m))
      (K.mul (co K (L1Full.prodL K (L1Full.prodL K Xl M) (2 * M + 1)) m) (co K (HeatDec.heat K P 0 (4 * M + 5)) (m + 1))) := by
    intro m
    cases Nat.lt_or_ge (m + 1) (4 * M + 5) with
    | inl hm =>
      have t := tp m
      unfold DecLR.LRat at t
      rw [coA (m + 1), coA m, HeatGood.coY K P (4 * M + 5) m (Nat.lt_of_succ_lt hm), HeatGood.coY K P (4 * M + 5) (m + 1) hm]
      exact t
    | inr hm =>
      rw [HeatLR.co_out K _ (m + 1) (by rw [hYl, L5]; exact hm),
        show K.mul K.zero (co K (HeatDec.heat K P 0 (4 * M + 5)) m) = K.zero by lib_ring]
      exact O.mul_nonneg _ _ (Ann m) (HeatGood.co_nn K O h1 P hP0 (4 * M + 5) (m + 1))
  have d := DecLR.dec_LR_heat K O F h1 h10 P hP0 _ (4 * M + 5) Ann (HeatMKClass.w_nn K O F h1 h10) h n
  rw [Migdal.pw_pw K P 2 2] at ⊢
  exact d

end

section
open YMChain SU2Haar SU2Char Fusion MigdalCone CgSplit HeatPair QBinom LinkIntegral

theorem flow_iter7 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (P : R) (hP0 : O.le K.zero P)
    (hPne : P ≠ K.zero) (h7 : O.le (K.mul (cN K 10) P) (cN K 7)) (X : List R) (M : Nat)
    (h0 : FlowIter.Cls K O (pwR K (pwR K P 2) 2) X M) :
    ∀ k, FlowIter.Cls K O (pwR K (pwR K P 2) 2) (FlowIter.itX K O F X M k) (FlowIter.itM M k)
  | 0 => h0
  | k + 1 => by
    have ⟨hg, ha, hl, g, gdec, gnn, gxy⟩ := flow_iter7 F h1 h10 P hP0 hPne h7 X M h0 k
    have fs := FlowStep.flow_step K O F h1 h10 _ hg _ hl ha
    have hq0 : O.le K.zero (pwR K (pwR K P 2) 2) := AreaLower.pw_nn K O _ (AreaLower.pw_nn K O P hP0 h1 2) h1 2
    have hqne : pwR K (pwR K P 2) 2 ≠ K.zero :=
      HeatGood.pw_ne K O h1 _ (AreaLower.pw_nn K O P hP0 h1 2) (HeatGood.pw_ne K O h1 P hP0 hPne h10 2) h10 2
    have hlen : (FlowIter.itX K O F X M (k + 1)).length = 4 * FlowIter.itM M k + 5 := FlowStep.len_mk K O F _ _
    have lr := fun n => mk_class7 K O F h1 h10 P hP0 hPne h7 _ hg _ hl g gdec gnn gxy n
    obtain ⟨g', gd', gn', gx'⟩ := FlowIter.ratio_form K O F h1 h10 _ hq0 hqne (FlowIter.itX K O F X M (k + 1)) (4 * FlowIter.itM M k + 5) hlen
      fs.1.1 lr
    exact ⟨fs.1, fs.2, hlen, g', gd', gn', gx'⟩

theorem flow_heat7 (F : OField.OF K O) (h1 : O.le K.zero K.one) (h10 : K.one ≠ K.zero) (P : R) (hP0 : O.le K.zero P)
    (hPne : P ≠ K.zero) (h7 : O.le (K.mul (cN K 10) P) (cN K 7)) (M k : Nat) :
    FlowIter.Cls K O (pwR K (pwR K P 2) 2)
      (FlowIter.itX K O F (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) M k) (FlowIter.itM M k) := by
  have nn := fun j => SU2CharExp.cN_nn K O h1 j
  have hP1 : O.le P K.one := (PairN.small_facts K O h1 h10 P hP0 h7).1
  have P20 := AreaLower.pw_nn K O P hP0 h1 2
  have hP2 : O.le (pwR K P 2) P := FlowHeat.sq_le K O P hP0 hP1
  have q0 : O.le K.zero (pwR K (pwR K P 2) 2) := AreaLower.pw_nn K O _ P20 h1 2
  have hq2 : O.le (pwR K (pwR K P 2) 2) (pwR K P 2) := FlowHeat.sq_le K O _ P20 (O.le_trans _ _ _ hP2 hP1)
  have hqP : O.le (pwR K (pwR K P 2) 2) P := O.le_trans _ _ _ hq2 hP2
  have hq1 : O.le (pwR K (pwR K P 2) 2) K.one := O.le_trans _ _ _ hqP hP1
  have qne : pwR K (pwR K P 2) 2 ≠ K.zero :=
    HeatGood.pw_ne K O h1 _ P20 (HeatGood.pw_ne K O h1 P hP0 hPne h10 2) h10 2
  have c50 := N0CellD.one_le_cN K O h1 50 (by decide)
  have h2P2 : O.le (K.mul (cN K 2) (pwR K P 2)) K.one := by
    apply O.pos_cancel _ _ (cN K 50) (O.le_trans _ _ _ h1 c50) (N0CellD.ne_of_one_le K O h1 h10 _ c50)
    have s := SecondRingAlg.sq_mono K O _ _ (O.mul_nonneg _ _ (nn 10) hP0) h7
    refine O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ s
      (O.le_trans _ _ _ (TripleE.le_of_eq' K O _ _ ?_) (O.le_trans _ _ _ (SU2CharExp.cN_mono K O h1 49 50 (by decide))
        (TripleE.le_of_eq' K O _ _ (K.one_mul _).symm))))
    · show K.mul (K.mul (cN K 2) (K.mul (K.mul K.one P) P)) (cN K 50) = _
      unfold cN; lib_ring
    · exact (SU2CharExp.cN_mul K 7 7).symm
  have hqq : O.le (pwR K (pwR K (pwR K P 2) 2) 2) (pwR K P 2) :=
    O.le_trans _ _ _ (FlowHeat.sq_le K O _ q0 hq1) hq2
  have h2 : O.le (K.mul (cN K 2) (pwR K (pwR K (pwR K P 2) 2) 2)) K.one :=
    O.le_trans _ _ _ (N0CellC.mul_le_l K O _ _ _ (nn 2) hqq) h2P2
  have h0 : FlowIter.Cls K O (pwR K (pwR K P 2) 2) (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) M :=
    ⟨HeatGood.heat_good K O h1 h10 _ q0 hq1 qne h2 (M + 2), FlowHeat.heat_anoninc K O h1 _ q0 hq1 (M + 2),
      SecondRing.len_heat K _ (M + 2) 0, fun _ => K.one, fun _ => O.le_refl _, fun _ => h1, fun j => by
        show co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) j = K.mul (co K (HeatDec.heat K (pwR K (pwR K P 2) 2) 0 (M + 2)) j) K.one
        lib_ring⟩
  exact flow_iter7 K O F h1 h10 P hP0 hPne h7 _ M h0 k

end

end Flow7

#print axioms Flow7.mk_class7
#print axioms Flow7.flow_iter7
#print axioms Flow7.flow_heat7

#print axioms: 76 theorems, no axioms.

90.150 L8: continuum limit and mass gap (= T45)

Statement (open). Existence of the continuum \(SU(2)\) Yang–Mills theory on \(\mathbb R^4\) satisfying the OS axioms, with a mass gap \(\Delta >0\). It would follow from L5 and L6 together with the open L7 bridge in \(d=4\) (uniform control of the block-spin flow from weak to strong coupling). No Lean file proves it.